-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathusbcanHWIf.c
2909 lines (2359 loc) · 93.5 KB
/
usbcanHWIf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
** Copyright 2017 by Kvaser AB, Molndal, Sweden
** http://www.kvaser.com
**
** This software is dual licensed under the following two licenses:
** BSD-new and GPLv2. You may use either one. See the included
** COPYING file for details.
**
** License: BSD-new
** ==============================================================================
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in the
** documentation and/or other materials provided with the distribution.
** * Neither the name of the <organization> nor the
** names of its contributors may be used to endorse or promote products
** derived from this software without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
** ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
** CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
** SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
** BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
** IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
** ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
** POSSIBILITY OF SUCH DAMAGE.
**
**
** License: GPLv2
** ==============================================================================
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
**
**
** IMPORTANT NOTICE:
** ==============================================================================
** This source code is made available for free, as an open license, by Kvaser AB,
** for use with its applications. Kvaser AB does not accept any liability
** whatsoever for any third party patent or other immaterial property rights
** violations that may result from any usage of this source code, regardless of
** the combination of source code and various applications that it can be used
** in, or with.
**
** -----------------------------------------------------------------------------
*/
// Linux USBcanII driver
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wtype-limits"
#include <linux/bitops.h>
#pragma GCC diagnostic pop
#include <linux/version.h>
#include <linux/usb.h>
#include <linux/types.h>
#include <linux/seq_file.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/kthread.h>
#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 11, 0))
#include <linux/sched.h>
#else
#include <linux/sched/signal.h>
#endif /* KERNEL_VERSION < 4.11.0 */
// Non system headers
#include "canlib_version.h"
#include "VCanOsIf.h"
#include "usbcanHWIf.h"
#include "helios_cmds.h"
#include "queue.h"
#include "debug.h"
#include "hwnames.h"
#include "vcan_ioctl.h"
#include "capabilities.h"
// Get a minor range for your devices from the usb maintainer
// Use a unique set for each driver
#define USB_USBCAN_MINOR_BASE 96
MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("KVASER");
MODULE_DESCRIPTION("USBcanII CAN module.");
MODULE_VERSION(__stringify(CANLIB_MAJOR_VERSION) "."
__stringify(CANLIB_MINOR_VERSION));
//----------------------------------------------------------------------------
// If you do not define USBCAN_DEBUG at all, all the debug code will be
// left out. If you compile with USBCAN_DEBUG=0, the debug code will
// be present but disabled -- but it can then be enabled for specific
// modules at load time with a 'pc_debug=#' option to insmod.
//
#ifdef USBCAN_DEBUG
static int pc_debug = USBCAN_DEBUG;
MODULE_PARM_DESC(pc_debug, "USBCanII debug level");
module_param(pc_debug, int, 0644);
# define DEBUGPRINT(n, arg) if (pc_debug >= (n)) { DEBUGOUT(n, arg); }
#else
# define DEBUGPRINT(n, arg) if ((n) == 1) { DEBUGOUT(n, arg); }
#endif
//----------------------------------------------------------------------------
#ifndef THIS_MODULE
#define THIS_MODULE 0
#endif
//======================================================================
// HW function pointers
//======================================================================
static int usbcan_init_driver(void);
static int usbcan_set_busparams(VCanChanData *vChd, VCanBusParams *par);
static int usbcan_get_busparams(VCanChanData *vChd, VCanBusParams *par);
static int usbcan_set_silent(VCanChanData *vChd, int silent);
static int usbcan_set_trans_type(VCanChanData *vChd, int linemode, int resnet);
static int usbcan_bus_on(VCanChanData *vChd);
static int usbcan_bus_off(VCanChanData *vChd);
static int usbcan_get_tx_err(VCanChanData *vChd);
static int usbcan_get_rx_err(VCanChanData *vChd);
static int usbcan_outstanding_sync(VCanChanData *vChan);
static int usbcan_close_all(void);
static int usbcan_proc_read(struct seq_file* m, void* v);
static int usbcan_get_chipstate(VCanChanData *vChd);
static int usbcan_get_time(VCanCardData *vCard, uint64_t *time);
static int usbcan_flush_tx_buffer(VCanChanData *vChan);
static void usbcan_schedule_send(VCanCardData *vCard, VCanChanData *vChan);
static unsigned long usbcan_get_hw_tx_q_len(VCanChanData *vChan);
static int usbcan_objbuf_exists(VCanChanData *chd, int bufType, int bufNo);
static int usbcan_objbuf_free(VCanChanData *chd, int bufType, int bufNo);
static int usbcan_objbuf_alloc(VCanChanData *chd, int bufType, int *bufNo);
static int usbcan_objbuf_write(VCanChanData *chd, int bufType, int bufNo,
int id, int flags, int dlc, unsigned char *data);
static int usbcan_objbuf_enable(VCanChanData *chd, int bufType, int bufNo,
int enable);
static int usbcan_objbuf_set_filter(VCanChanData *chd, int bufType, int bufNo,
int code, int mask);
static int usbcan_objbuf_set_flags(VCanChanData *chd, int bufType, int bufNo,
int flags);
static int usbcan_objbuf_set_period(VCanChanData *chd, int bufType, int bufNo,
int period);
static int usbcan_flash_leds(const VCanChanData *chd, int action, int timeout);
static VCanDriverData driverData;
static VCanHWInterface hwIf = {
.initAllDevices = usbcan_init_driver,
.setBusParams = usbcan_set_busparams,
.getBusParams = usbcan_get_busparams,
.setOutputMode = usbcan_set_silent,
.setTranceiverMode = usbcan_set_trans_type,
.busOn = usbcan_bus_on,
.busOff = usbcan_bus_off,
.txAvailable = usbcan_outstanding_sync, // This isn't really a function thats checks if tx is available!
.procRead = usbcan_proc_read,
.closeAllDevices = usbcan_close_all,
.getTime = usbcan_get_time,
.flushSendBuffer = usbcan_flush_tx_buffer,
.getRxErr = usbcan_get_rx_err,
.getTxErr = usbcan_get_tx_err,
.txQLen = usbcan_get_hw_tx_q_len,
.requestChipState = usbcan_get_chipstate,
.requestSend = usbcan_schedule_send,
.flashLeds = usbcan_flash_leds,
.objbufExists = usbcan_objbuf_exists,
.objbufFree = usbcan_objbuf_free,
.objbufAlloc = usbcan_objbuf_alloc,
.objbufWrite = usbcan_objbuf_write,
.objbufEnable = usbcan_objbuf_enable,
.objbufSetFilter = usbcan_objbuf_set_filter,
.objbufSetFlags = usbcan_objbuf_set_flags,
.objbufSetPeriod = usbcan_objbuf_set_period,
.getCardInfo = vCanGetCardInfo,
.getCardInfo2 = vCanGetCardInfo2,
};
//======================================================================
// Static declarations
// USB packet size
#define MAX_PACKET_OUT 3072 // To device
#define MAX_PACKET_IN 3072 // From device
//======================================================================
// Prototypes
static int usbcan_plugin(struct usb_interface *interface,
const struct usb_device_id *id);
static void usbcan_remove(struct usb_interface *interface);
// Interrupt handler prototype changed in 2.6.19.
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
static void usbcan_write_bulk_callback(struct urb *urb, struct pt_regs *regs);
#else
static void usbcan_write_bulk_callback(struct urb *urb);
#endif
static int usbcan_allocate(VCanCardData **vCard);
static void usbcan_deallocate(VCanCardData *vCard);
static void usbcan_start(VCanCardData *vCard);
static int usbcan_tx_available(VCanChanData *vChan);
static int usbcan_transmit(VCanCardData *vCard);
static int usbcan_send_and_wait_reply(VCanCardData *vCard, heliosCmd *cmd,
heliosCmd *replyPtr,
unsigned char cmdNr,
unsigned char transId);
static int usbcan_queue_cmd(VCanCardData *vCard, heliosCmd *cmd,
unsigned int timeout);
static void usbcan_handle_command(heliosCmd *cmd, VCanCardData *vCard);
static int usbcan_get_trans_id(heliosCmd *cmd);
static int usbcan_fill_usb_buffer(VCanCardData *vCard,
unsigned char *buffer, int maxlen);
static void usbcan_translate_can_msg(VCanChanData *vChan,
heliosCmd *helios_msg, CAN_MSG *can_msg);
static void usbcan_get_card_info(VCanCardData *vCard);
//----------------------------------------------------------------------
//----------------------------------------------------------------------------
// Supported KVASER hardware
#define KVASER_VENDOR_ID 0x0bfd
#define USB_USBCAN2_PRODUCT_ID 0x0004
#define USB_USBCAN_REVB_PRODUCT_ID 0x0002
#define USB_MEMORATOR_PRODUCT_ID 0x0005
#define USB_VCI2_PRODUCT_ID 0x0003
// Table of devices that work with this driver
static struct usb_device_id usbcan_table[] = {
{ USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN2_PRODUCT_ID) },
{ USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN_REVB_PRODUCT_ID) },
{ USB_DEVICE(KVASER_VENDOR_ID, USB_MEMORATOR_PRODUCT_ID) },
{ USB_DEVICE(KVASER_VENDOR_ID, USB_VCI2_PRODUCT_ID) },
{ 0 } // Terminating entry
};
MODULE_DEVICE_TABLE(usb, usbcan_table);
//
// USB class driver info in order to get a minor number from the usb core,
// and to have the device registered with devfs and the driver core
//
/*
static struct usb_class_driver usbcan_class = {
// There will be a special file in /dev/usb called the below
.name = "usb/usbcanII%d",
.fops = &fops,
// .mode removed somewhere between 2.6.8 and 2.6.15
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 15))
.mode = S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH,
#endif
.minor_base = USB_USBCAN_MINOR_BASE,
};
*/
// USB specific object needed to register this driver with the usb subsystem
static struct usb_driver usbcan_driver = {
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 15))
.owner = THIS_MODULE,
#endif
.name = "usbcanII",
.probe = usbcan_plugin,
.disconnect = usbcan_remove,
.id_table = usbcan_table,
};
//============================================================================
//------------------------------------------------------
//
// ---- CALLBACKS ----
//
//------------------------------------------------------
//============================================================================
// usbcan_write_bulk_callback
//
// Interrupt handler prototype changed in 2.6.19.
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
static void usbcan_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
#else
static void usbcan_write_bulk_callback (struct urb *urb)
#endif
{
VCanCardData *vCard = (VCanCardData *)urb->context;
UsbcanCardData *dev = (UsbcanCardData *)vCard->hwCardData;
// sync/async unlink faults aren't errors
if (urb->status && !(urb->status == -ENOENT ||
urb->status == -ECONNRESET ||
urb->status == -ESHUTDOWN)) {
DEBUGPRINT(2, (TXT("%s - nonzero write bulk status received: %d\n"),
__FUNCTION__, urb->status));
}
// Notify anyone waiting that the write has finished
complete(&dev->write_finished);
}
//----------------------------------------------------------------------------
//
// ---- THREADS ----
//
//----------------------------------------------------------------------------
//============================================================================
//
// usbcan_rx_thread
//
static int usbcan_rx_thread (void *context)
{
VCanCardData *vCard = (VCanCardData *)context;
UsbcanCardData *dev = (UsbcanCardData *)vCard->hwCardData;
int result = 0;
int usbErrorCounter;
int ret;
int len;
if (!try_module_get(THIS_MODULE)) {
return -ENODEV;
}
DEBUGPRINT(3, (TXT("rx thread started\n")));
dev->read_urb = 0;
usbErrorCounter = 0;
while (vCard->cardPresent) {
// Verify that the device wasn't unplugged
if (!vCard->cardPresent){
DEBUGPRINT(3, (TXT("rx thread Ended - device removed\n")));
result = -ENODEV;
break;
}
len = 0;
// Do a blocking bulk read to get data from the device
ret = usb_bulk_msg(dev->udev,
usb_rcvbulkpipe(dev->udev, dev->bulk_in_endpointAddr),
dev->bulk_in_buffer, dev->bulk_in_size, &len,
// Timeout changed from jiffies to milliseconds in 2.6.12.
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 12))
HZ * 30);
#else
30000);
#endif
if (ret) {
if (ret != -ETIMEDOUT) {
// Save if runaway
if (ret == -EILSEQ || ret == -ESHUTDOWN || ret == -EINVAL) {
DEBUGPRINT(2, (TXT ("usb_bulk_msg error (%d) - Device probably ")
TXT2("removed, closing down\n"), ret));
vCard->cardPresent = 0;
result = -ENODEV;
break;
}
#if DEBUG
if (usbErrorCounter++ % 10 == 0)
DEBUGPRINT(2, (TXT("usb_bulk_msg error (%d) %dth time\n"),
ret, usbErrorCounter));
#endif
if (usbErrorCounter > 100) {
DEBUGPRINT(2, (TXT("rx thread Ended - error (%d)\n"), ret));
// Since this has failed so many times, stop transfers to device
vCard->cardPresent = 0;
result = -ENODEV;
break;
}
}
}
else {
//
// We got a bunch of bytes. Now interpret them.
//
unsigned char *buffer = (unsigned char *)dev->bulk_in_buffer;
heliosCmd *cmd;
int loopCounter = 1000;
unsigned int count = 0;
while (count < len) {
// A loop counter as a safety measure.
if (--loopCounter == 0) {
DEBUGPRINT(2, (TXT("ERROR usbcan_rx_thread() LOOPMAX. \n")));
break;
}
// A command will never straddle a bulk_in_MaxPacketSize byte boundary.
// The firmware will place a zero in the buffer to indicate that
// the next command will follow after the next
// bulk_in_MaxPacketSize bytes boundary.
cmd = (heliosCmd *)&buffer[count];
if (cmd->head.cmdLen == 0) {
count += dev->bulk_in_MaxPacketSize;
count &= -(dev->bulk_in_MaxPacketSize);
continue;
}
else {
count += cmd->head.cmdLen;
}
usbcan_handle_command(cmd, vCard);
usbErrorCounter = 0;
}
}
} // while (vCard->cardPresent)
DEBUGPRINT(3, (TXT("rx thread Ended - finalised\n")));
#if (LINUX_VERSION_CODE < KERNEL_VERSION(5, 17, 0))
module_put(THIS_MODULE);
do_exit(result);
#else
module_put_and_kthread_exit(result);
#endif
return result;
} // _rx_thread
//======================================================================
// Returns whether the transmit queue on a specific channel is empty
// (This is really the same as in VCanOSIf.c, but it may not be
// intended that this file should call there.)
//======================================================================
static int txQEmpty (VCanChanData *chd)
{
return queue_empty(&chd->txChanQueue);
}
//============================================================================
//
// usbcan_handle_command
// Handle a received heliosCmd.
//
static void usbcan_handle_command (heliosCmd *cmd, VCanCardData *vCard)
{
UsbcanCardData *dev = (UsbcanCardData *)vCard->hwCardData;
struct list_head *currHead;
struct list_head *tmpHead;
WaitNode *currNode;
VCAN_EVENT e;
unsigned long irqFlags;
switch (cmd->head.cmdNo) {
case CMD_RX_STD_MESSAGE:
case CMD_RX_EXT_MESSAGE:
{
char dlc;
unsigned char flags;
unsigned int chan = cmd->rxCanMessage.channel;
DEBUGPRINT(4, (TXT("CMD_RX_XXX_MESSAGE\n")));
if (chan < (unsigned)vCard->nrChannels) {
VCanChanData *vChan = vCard->chanData[cmd->rxCanMessage.channel];
e.tag = V_RECEIVE_MSG;
e.transId = 0;
spin_lock_irqsave(&dev->timeHi_lock, irqFlags);
e.timeStamp = (cmd->rxCanMessage.time + vCard->timeHi) /
USBCANII_TICKS_PER_10US;
spin_unlock_irqrestore(&dev->timeHi_lock, irqFlags);
e.tagData.msg.id = cmd->rxCanMessage.rawMessage[0] & 0x1F;
e.tagData.msg.id <<= 6;
e.tagData.msg.id += cmd->rxCanMessage.rawMessage[1] & 0x3F;
if (cmd->head.cmdNo == CMD_RX_EXT_MESSAGE) {
e.tagData.msg.id <<= 4;
e.tagData.msg.id += cmd->rxCanMessage.rawMessage[2] & 0x0F;
e.tagData.msg.id <<= 8;
e.tagData.msg.id += cmd->rxCanMessage.rawMessage[3] & 0xFF;
e.tagData.msg.id <<= 6;
e.tagData.msg.id += cmd->rxCanMessage.rawMessage[4] & 0x3F;
e.tagData.msg.id += EXT_MSG;
}
flags = cmd->rxCanMessage.flags;
e.tagData.msg.flags = 0;
if (flags & MSGFLAG_OVERRUN)
e.tagData.msg.flags |= VCAN_MSG_FLAG_OVERRUN;
if (flags & MSGFLAG_REMOTE_FRAME)
e.tagData.msg.flags |= VCAN_MSG_FLAG_REMOTE_FRAME;
if (flags & MSGFLAG_ERROR_FRAME)
e.tagData.msg.flags |= VCAN_MSG_FLAG_ERROR_FRAME;
if (flags & MSGFLAG_TX)
e.tagData.msg.flags |= VCAN_MSG_FLAG_TXACK;
if (flags & MSGFLAG_TXRQ)
e.tagData.msg.flags |= VCAN_MSG_FLAG_TXRQ;
dlc = cmd->rxCanMessage.rawMessage[5] & 0x0F;
e.tagData.msg.dlc = dlc;
memcpy(e.tagData.msg.data, &cmd->rxCanMessage.rawMessage[6], 8);
DEBUGPRINT(6, (TXT(" - vCanDispatchEvent id: %d (ch:%d), time %lu\n"),
e.tagData.msg.id, vChan->channel, e.timeStamp));
vCanDispatchEvent(vChan, &e);
}
break;
}
case CMD_GET_BUSPARAMS_RESP:
DEBUGPRINT(4, (TXT("CMD_GET_BUSPARAMS_RESP - Ignored\n")));
break;
case CMD_CHIP_STATE_EVENT:
{
unsigned int chan = cmd->chipStateEvent.channel;
VCanChanData *vChd = vCard->chanData[chan];
DEBUGPRINT(4, (TXT("CMD_CHIP_STATE_EVENT\n")));
if (chan < (unsigned)vCard->nrChannels) {
vChd->chipState.txerr = cmd->chipStateEvent.txErrorCounter;
vChd->chipState.rxerr = cmd->chipStateEvent.rxErrorCounter;
if (cmd->chipStateEvent.txErrorCounter ||
cmd->chipStateEvent.rxErrorCounter) {
DEBUGPRINT(6, (TXT("CMD_CHIP_STATE_EVENT, chan %d - "), chan));
DEBUGPRINT(6, (TXT("txErr = %d/rxErr = %d\n"),
cmd->chipStateEvent.txErrorCounter,
cmd->chipStateEvent.rxErrorCounter));
}
// ".busStatus" is the contents of the CnSTRH register.
switch (cmd->chipStateEvent.busStatus &
(M16C_BUS_PASSIVE | M16C_BUS_OFF)) {
case 0:
vChd->chipState.state = CHIPSTAT_ERROR_ACTIVE;
break;
case M16C_BUS_PASSIVE:
vChd->chipState.state = CHIPSTAT_ERROR_PASSIVE |
CHIPSTAT_ERROR_WARNING;
break;
case M16C_BUS_OFF:
vChd->chipState.state = CHIPSTAT_BUSOFF;
break;
case (M16C_BUS_PASSIVE | M16C_BUS_OFF):
vChd->chipState.state = CHIPSTAT_BUSOFF | CHIPSTAT_ERROR_PASSIVE |
CHIPSTAT_ERROR_WARNING;
break;
}
// Reset is treated like bus-off
if (cmd->chipStateEvent.busStatus & M16C_BUS_RESET) {
vChd->chipState.state = CHIPSTAT_BUSOFF;
vChd->chipState.txerr = 0;
vChd->chipState.rxerr = 0;
}
//if (hCd->waitForChipState)
// wake_up(&hCd->waitResponse);
e.tag = V_CHIP_STATE;
spin_lock_irqsave(&dev->timeHi_lock, irqFlags);
e.timeStamp = (cmd->chipStateEvent.time + vCard->timeHi) /
USBCANII_TICKS_PER_10US;
spin_unlock_irqrestore(&dev->timeHi_lock, irqFlags);
e.transId = 0;
e.tagData.chipState.busStatus = (unsigned char)vChd->chipState.state;
e.tagData.chipState.txErrorCounter = (unsigned char)vChd->chipState.txerr;
e.tagData.chipState.rxErrorCounter = (unsigned char)vChd->chipState.rxerr;
vCanDispatchEvent(vChd, &e);
}
break;
}
case CMD_GET_DRIVERMODE_RESP:
DEBUGPRINT(4, (TXT("CMD_GET_DRIVERMODE_RESP - Ignored\n")));
break;
case CMD_START_CHIP_RESP:
DEBUGPRINT(4, (TXT("CMD_START_CHIP_RESP - Ignored\n")));
break;
case CMD_STOP_CHIP_RESP:
DEBUGPRINT(4, (TXT("CMD_STOP_CHIP_RESP - Ignored\n")));
break;
case CMD_READ_CLOCK_RESP:
DEBUGPRINT(4, (TXT("CMD_READ_CLOCK_RESP\n")));
spin_lock_irqsave(&dev->timeHi_lock, irqFlags);
vCard->timeHi = cmd->readClockResp.time[1] << 16;
spin_unlock_irqrestore(&dev->timeHi_lock, irqFlags);
DEBUGPRINT(6, (TXT("C %x\n"), vCard->timeHi));
break;
case CMD_CLOCK_OVERFLOW_EVENT:
DEBUGPRINT(4, (TXT("CMD_CLOCK_OVERFLOW_EVENT\n")));
spin_lock_irqsave(&dev->timeHi_lock, irqFlags);
vCard->timeHi = cmd->clockOverflowEvent.currentTime & 0xFFFF0000;
spin_unlock_irqrestore(&dev->timeHi_lock, irqFlags);
DEBUGPRINT(6, (TXT("O %x\n"), vCard->timeHi));
break;
case CMD_GET_CARD_INFO_RESP:
DEBUGPRINT(4, (TXT("CMD_GET_CARD_INFO_RESP - Ignored\n")));
break;
case CMD_GET_INTERFACE_INFO_RESP:
DEBUGPRINT(4, (TXT("CMD_GET_INTERFACE_INFO_RESP - Ignored\n")));
break;
case CMD_GET_SOFTWARE_INFO_RESP:
DEBUGPRINT(4, (TXT("CMD_GET_SOFTWARE_INFO_RESP - Ignored\n")));
break;
case CMD_GET_BUSLOAD_RESP:
DEBUGPRINT(4, (TXT("CMD_GET_BUSLOAD_RESP - Ignored\n")));
break;
case CMD_RESET_STATISTICS:
DEBUGPRINT(4, (TXT("CMD_RESET_STATISTICS - Ignored\n")));
break;
case CMD_ERROR_EVENT:
DEBUGPRINT(4, (TXT("CMD_ERROR_EVENT - Ignored\n")));
break;
case CMD_RESET_ERROR_COUNTER:
DEBUGPRINT(4, (TXT("CMD_RESET_ERROR_COUNTER - Ignored\n")));
break;
// Send a TxRequest back to the application. This is a
// little-used message that means that the firmware has _started_
// sending the message (it is submitted to the CAN controller)
case CMD_TX_REQUEST:
{
unsigned int transId;
unsigned int chan = cmd->txRequest.channel;
VCanChanData *vChan = vCard->chanData[cmd->txRequest.channel];
DEBUGPRINT(4, (TXT("CMD_TX_REQUEST\n")));
if (chan < (unsigned)vCard->nrChannels) {
// A TxReq. Take the current tx message, modify it to a
// receive message and send it back.
transId = cmd->txRequest.transId;
if ((transId == 0) || (transId > dev->max_outstanding_tx)) {
DEBUGPRINT(6, (TXT ("CMD_TX_REQUEST chan %d ")
TXT2("ERROR transid too high %d\n"), chan, transId));
break;
}
if (((UsbcanChanData *)vChan->hwChanData)->current_tx_message[transId - 1].flags & VCAN_MSG_FLAG_TXRQ) {
VCAN_EVENT *e = (VCAN_EVENT *)&((UsbcanChanData *)vChan->hwChanData)->current_tx_message[transId - 1];
e->tag = V_RECEIVE_MSG;
spin_lock_irqsave(&dev->timeHi_lock, irqFlags);
e->timeStamp = (cmd->txRequest.time + vCard->timeHi) /
USBCANII_TICKS_PER_10US;
spin_unlock_irqrestore(&dev->timeHi_lock, irqFlags);
e->tagData.msg.flags &= ~VCAN_MSG_FLAG_TXACK;
vCanDispatchEvent(vChan, e);
}
}
//else {
//}
break;
}
case CMD_TX_ACKNOWLEDGE:
{
unsigned int transId;
VCanChanData *vChan = vCard->chanData[cmd->txAck.channel];
UsbcanChanData *usbChan = vChan->hwChanData;
UsbcanCardData *dev = (UsbcanCardData *)vCard->hwCardData;
//DEBUGPRINT(4, (TXT("CMD_TX_ACKNOWLEDGE\n")));
if (cmd->txAck.channel < (unsigned)vCard->nrChannels) {
DEBUGPRINT(4, (TXT ("CMD_TX_ACKNOWLEDGE on ch %d ")
TXT2("(outstanding tx = %d)\n"),
cmd->txAck.channel, usbChan->outstanding_tx));
transId = cmd->txAck.transId;
if ((transId == 0) || (transId > dev->max_outstanding_tx)) {
DEBUGPRINT(2, (TXT("CMD_TX_ACKNOWLEDGE chan %d ERROR transid %d\n"),
cmd->txAck.channel, transId));
break;
}
if (usbChan->current_tx_message[transId - 1].flags & VCAN_MSG_FLAG_TXACK) {
VCAN_EVENT *e = (VCAN_EVENT *)&usbChan->current_tx_message[transId - 1];
e->tag = V_RECEIVE_MSG;
spin_lock_irqsave(&dev->timeHi_lock, irqFlags);
e->timeStamp = (cmd->txAck.time + vCard->timeHi) / USBCANII_TICKS_PER_10US;
spin_unlock_irqrestore(&dev->timeHi_lock, irqFlags);
e->tagData.msg.flags &= ~VCAN_MSG_FLAG_TXRQ;
vCanDispatchEvent(vChan, e);
}
usbChan->current_tx_message[transId - 1].user_data = 0;
spin_lock(&usbChan->outTxLock);
if (usbChan->outstanding_tx) {
usbChan->outstanding_tx--;
}
// Outstanding is changing from *full* to at least one open slot?
if (usbChan->outstanding_tx >= (dev->max_outstanding_tx - 1)) {
spin_unlock(&usbChan->outTxLock);
DEBUGPRINT(6, (TXT("Buffer in chan %d not full (%d) anymore\n"),
cmd->txAck.channel, usbChan->outstanding_tx));
queue_work(dev->txTaskQ, &dev->txWork);
}
// Check if we should *wake* canwritesync
else if ((usbChan->outstanding_tx == 0) && txQEmpty(vChan) &&
test_and_clear_bit(0, &vChan->waitEmpty)) {
spin_unlock(&usbChan->outTxLock);
wake_up_interruptible(&vChan->flushQ);
DEBUGPRINT(6, (TXT("W%d\n"), cmd->txAck.channel));
}
else {
#if DEBUG
if (usbChan->outstanding_tx < 4)
DEBUGPRINT(6, (TXT("o%d ql%d we%d s%d\n"),
usbChan->outstanding_tx,
queue_length(&vChan->txChanQueue),
constant_test_bit(0, &vChan->waitEmpty),
dev->max_outstanding_tx));
#endif
spin_unlock(&usbChan->outTxLock);
}
DEBUGPRINT(6, (TXT("X%d\n"), cmd->txAck.channel));
}
break;
}
case CMD_CAN_ERROR_EVENT:
{
int errorCounterChanged;
// <windows> Known problem: if the error counters of both channels
// are max then there is no way of knowing which channel got an errorframe
// </windows>
VCanChanData *vChd = vCard->chanData[0];
DEBUGPRINT(4, (TXT("CMD_CAN_ERROR_EVENT\n")));
// It's an error frame if any of our error counters has
// increased..
errorCounterChanged = (cmd->canErrorEvent.txErrorCounterCh0 >
vChd->chipState.txerr);
errorCounterChanged |= (cmd->canErrorEvent.rxErrorCounterCh0 >
vChd->chipState.rxerr);
// It's also an error frame if we have seen a bus error while
// the other channel hasn't seen any bus errors at all.
errorCounterChanged |= ( (cmd->canErrorEvent.busStatusCh0 & M16C_BUS_ERROR) &&
!(cmd->canErrorEvent.busStatusCh1 & M16C_BUS_ERROR));
vChd->chipState.txerr = cmd->canErrorEvent.txErrorCounterCh0;
vChd->chipState.rxerr = cmd->canErrorEvent.rxErrorCounterCh0;
switch (cmd->canErrorEvent.busStatusCh0 & (M16C_BUS_PASSIVE | M16C_BUS_OFF)) {
case 0:
vChd->chipState.state = CHIPSTAT_ERROR_ACTIVE;
break;
case M16C_BUS_PASSIVE:
vChd->chipState.state = CHIPSTAT_ERROR_PASSIVE |
CHIPSTAT_ERROR_WARNING;
break;
case M16C_BUS_OFF:
vChd->chipState.state = CHIPSTAT_BUSOFF;
errorCounterChanged = 0;
break;
case (M16C_BUS_PASSIVE | M16C_BUS_OFF):
vChd->chipState.state = CHIPSTAT_BUSOFF | CHIPSTAT_ERROR_PASSIVE |
CHIPSTAT_ERROR_WARNING;
errorCounterChanged = 0;
break;
default:
break;
}
// Reset is treated like bus-off
if (cmd->canErrorEvent.busStatusCh0 & M16C_BUS_RESET) {
vChd->chipState.state = CHIPSTAT_BUSOFF;
vChd->chipState.txerr = 0;
vChd->chipState.rxerr = 0;
errorCounterChanged = 0;
}
e.tag = V_CHIP_STATE;
e.timeStamp = (cmd->canErrorEvent.time +
vCard->timeHi) /
USBCANII_TICKS_PER_10US;
e.transId = 0;
e.tagData.chipState.busStatus = vChd->chipState.state;
e.tagData.chipState.txErrorCounter = vChd->chipState.txerr;
e.tagData.chipState.rxErrorCounter = vChd->chipState.rxerr;
vCanDispatchEvent(vChd, &e);
if (errorCounterChanged) {
e.tag = V_RECEIVE_MSG;
e.transId = 0;
e.timeStamp = (cmd->canErrorEvent.time + vCard->timeHi) /
USBCANII_TICKS_PER_10US;
e.tagData.msg.id = 0;
e.tagData.msg.flags = VCAN_MSG_FLAG_ERROR_FRAME;
e.tagData.msg.dlc = 0;
vCanDispatchEvent(vChd, &e);
}
// Next channel
if ((unsigned)vCard->nrChannels > 0) {
VCanChanData *vChd = vCard->chanData[1];
// It's an error frame if any of our error counters has increased..
errorCounterChanged = (cmd->canErrorEvent.txErrorCounterCh1 >
vChd->chipState.txerr);
errorCounterChanged |= (cmd->canErrorEvent.rxErrorCounterCh1 >
vChd->chipState.rxerr);
// It's also an error frame if we have seen a bus error while
// the other channel hasn't seen any bus errors at all.
errorCounterChanged |= ( (cmd->canErrorEvent.busStatusCh1 & M16C_BUS_ERROR) &&
!(cmd->canErrorEvent.busStatusCh0 & M16C_BUS_ERROR));
vChd->chipState.txerr = cmd->canErrorEvent.txErrorCounterCh1;
vChd->chipState.rxerr = cmd->canErrorEvent.rxErrorCounterCh1;
switch (cmd->canErrorEvent.busStatusCh1 & (M16C_BUS_PASSIVE | M16C_BUS_OFF)) {
case 0:
vChd->chipState.state = CHIPSTAT_ERROR_ACTIVE;
break;
case M16C_BUS_PASSIVE:
vChd->chipState.state = CHIPSTAT_ERROR_PASSIVE |
CHIPSTAT_ERROR_WARNING;
break;
case M16C_BUS_OFF:
vChd->chipState.state = CHIPSTAT_BUSOFF;
errorCounterChanged = 0;
break;
case (M16C_BUS_PASSIVE|M16C_BUS_OFF):
vChd->chipState.state = CHIPSTAT_BUSOFF | CHIPSTAT_ERROR_PASSIVE |
CHIPSTAT_ERROR_WARNING;
errorCounterChanged = 0;
break;
default:
break;
}
// Reset is treated like bus-off
if (cmd->canErrorEvent.busStatusCh1 & M16C_BUS_RESET) {
vChd->chipState.state = CHIPSTAT_BUSOFF;
vChd->chipState.txerr = 0;
vChd->chipState.rxerr = 0;
errorCounterChanged = 0;
}
// Dispatch can event
e.tag = V_CHIP_STATE;
e.timeStamp = (cmd->canErrorEvent.time +
vCard->timeHi) /
USBCANII_TICKS_PER_10US;
e.transId = 0;
e.tagData.chipState.busStatus = vChd->chipState.state;
e.tagData.chipState.txErrorCounter = vChd->chipState.txerr;
e.tagData.chipState.rxErrorCounter = vChd->chipState.rxerr;
vCanDispatchEvent(vChd, &e);
if (errorCounterChanged) {
e.tag = V_RECEIVE_MSG;
e.transId = 0;
e.timeStamp = (cmd->canErrorEvent.time + vCard->timeHi) /
USBCANII_TICKS_PER_10US;
e.tagData.msg.id = 0;
e.tagData.msg.flags = VCAN_MSG_FLAG_ERROR_FRAME;
e.tagData.msg.dlc = 0;
vCanDispatchEvent(vChd, &e);
}
}
break;
}
default:
DEBUGPRINT(2, (TXT("UNKNOWN COMMAND - %d\n"), cmd->head.cmdNo));
}
//
// Wake up those who are waiting for a resp
//
spin_lock_irqsave(&dev->replyWaitListLock, irqFlags);
list_for_each_safe(currHead, tmpHead, &dev->replyWaitList)
{
currNode = list_entry(currHead, WaitNode, list);
if (currNode->cmdNr == cmd->head.cmdNo &&
usbcan_get_trans_id(cmd) == currNode->transId) {
memcpy(currNode->replyPtr, cmd, cmd->head.cmdLen);
DEBUGPRINT(4, (TXT ("Match: cN->cmdNr(%d) == cmd->cmdNo(%d) && ")
TXT2("_get_trans_id(%d) == cN->transId(%d)\n"),
currNode->cmdNr, cmd->head.cmdNo,
usbcan_get_trans_id(cmd), currNode->transId));
complete(&currNode->waitCompletion);
}
#if DEBUG
else {
DEBUGPRINT(4, (TXT ("cN->cmdNr(%d) == cmd->cmdNo(%d) && ")
TXT2("_get_trans_id(%d) == cN->transId(%d)\n"),
currNode->cmdNr, cmd->head.cmdNo,
usbcan_get_trans_id(cmd), currNode->transId));
}
#endif
}
spin_unlock_irqrestore(&dev->replyWaitListLock, irqFlags);
} // _handle_command
//============================================================================
// _get_trans_id
//
static int usbcan_get_trans_id (heliosCmd *cmd)
{
// for example: cmdAutoTxBufferReq does not have a transId
if (cmd->head.cmdNo > CMD_TX_EXT_MESSAGE) {
// Any of the commands
return cmd->getBusparamsReq.transId;
}
else {
DEBUGPRINT(2, (TXT("WARNING: won't give a correct transid\n")));
return 0;
}
} // _get_trans_id
//============================================================================
// _send
//
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)