-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystec_can.c
3254 lines (2664 loc) · 99.4 KB
/
systec_can.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
/*
*
* (C) Copyright 2011 Heino Gutschmidt
* (C) 2011-2021 SYS TEC electronic AG, Daniel Krueger, Alexander Stein
*
* 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., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
* This driver has been modified for the porpuse of instalation in
* Almalinux 9 machines. The original driver was made for kernel 5.15
* and the changes were made to make it compatible with kernel 5.14
* The changes were made by the students of the Pontificia Universidad Javeriana
* for the ATLAS collaboration.
* Author of the changes:
* Sebastian Vergara
*
* The changes made to the original driver are:
* - Commented the functions that are not compatible with the new kernel
* - can_led_event
* - dev_can_led_event
*
* These chanes cause the LED of the CAN device to not work properly, so keep in mind
* that the LED of the device will not work with this driver depending on the firmware.
*/
#include <asm/unaligned.h>
#include <linux/firmware.h>
#include <linux/if_arp.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/netdevice.h>
#include <linux/printk.h>
#include <linux/signal.h>
#include <linux/slab.h>
#include <linux/usb.h>
#include <linux/version.h>
#include <linux/workqueue.h>
#include <linux/can.h>
#include <linux/can/dev.h>
#include <linux/can/error.h>
#define SYSTEC_MODULE_VERSION "1.0.7"
#define SYSTEC_MODULE_DESC "SYS TEC electronic USB-CANmodul Series Driver modified for Alamlinux 9" SYSTEC_MODULE_VERSION
#define SYSTEC_DEBUG_DRIVER 1
#define SYSTEC_DEBUG_CMD_CB 2
#define SYSTEC_DEBUG_STAT_CB 4
#define SYSTEC_DEBUG_DATA_CB 8
#define SYSTEC_DEBUG_TX 16
#define SYSTEC_DEBUG_BITTIMING 32
#define SYSTEC_DEBUG_CMD 64
#define SYSTEC_DEBUG_ERR_HANDLING 128
#define SYSTEC_DEBUG_USB_DISCONNECT 256
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 33)
#define TX_ECHO_SKB_MAX 20
#else
#define TX_ECHO_SKB_MAX 4
#endif
#define MAX_TX_URBS 10
#define MAX_RX_URBS 10
#define RX_BUFFER_SIZE 64
#define INTR_IN_BUFFER_SIZE 4
/* size of command */
#define SYSTEC_CMD_SIZE 8
/* number of endpoints */
#define SYSTEC_NUM_ENDPOINTS 5
#define SYSTEC_EP_DATA_OUT 0
#define SYSTEC_EP_MSG_OUT 1
#define SYSTEC_EP_DATA_IN 2
#define SYSTEC_EP_MSG_IN 3
#define SYSTEC_EP_STAT_IN 4
/* for can socket internal calculation */
#define SYSTEC_DEVICE_CLOCK_GEN4_SLOW 24000000
#define SYSTEC_DEVICE_CLOCK_GEN4_FAST 30000000
#define SYSTEC_DEVICE_CLOCK_DEFAULT 48000000
/* timeout for bulk transfers - 1000ms */
#define SYSTEC_BULK_TIMEOUT 1000
/* polling time of int (status) urbs - 1ms */
#define SYSTEC_INT_URB_POLL_TIME 1
#define SYSTEC_CAN_CHANNEL 0x00000001
#define SYSTEC_CAN_STATE_MASK 0xFFFF0000
#define SYSTEC_CAN_STATE_ERR_WARN 0x00040000
#define SYSTEC_CAN_STATE_ERR_PASS 0x00080000
#define SYSTEC_CAN_STATE_ERR_BOFF 0x00100000
#define SYSTEC_USB_MASK 0x0000FFFE
#define SYSTEC_USB_ERR_STATUS_TIMEOUT 0x00002000
#define SYSTEC_USB_ERR_WDT_RESET 0x00004000
#define USBCAN_VRREQ_READ_VERSION 0xB0 // IN: dwVersion
#define USBCAN_VRREQ_START_UPDATE 0xB1 // OUT: void
#define USBCAN_VRREQ_WRITE_UPDATE 0xB2 // OUT: INTEL-HEX-RECORD
#define USBCAN_VRREQ_STOPP_UPDATE 0xB3 // OUT: void
#define USBCAN_VRREQ_CHECK_UPDATE 0xB4 // IN: dwErrorCode, dwCryptCrc32, dwReserve
#define USBCAN_VRREQ_WRITE_FLASH 0xB5 // OUT: dwBase, dwSize, fReset
#define USBCAN_VRREQ_RECONNECT 0xB6 // OUT: void
#define USBCAN_VRREQ_READ_FW_VERSION \
0xB9 // IN: dwVersion // always means FwVersion of Firmware (not supported in Firmware <
// V4.06)
#define USBCAN_VRREQ_CHECK_CRC 0xBA // OUT: dwBase, dwSize
#define USBCAN_VRREQ_GET_CRC_RESULT 0xBB // IN: dwErrorCode, dwCryptCrc32, dwReserve
/* from Include/Ucanmcpc.h */
#define USBCAN_CMD_INITIALIZE 1
#define USBCAN_CMD_SHUTDOWN 4
#define USBCAN_CMD_RESET 5
#define USBCAN_CMD_READEEPROM 6
#define USBCAN_CMD_WRITEEEPROM 7
#define USBCAN_CMD_SETAMR 11 // -> dwAMR
#define USBCAN_CMD_SETACR 12 // -> dwACR
#define USBCAN_CMD_SETCANMODE 13 // -> bMode
#define USBCAN_CMD_SETBAUDRATE_EX 25
#define USBCAN_CMD_RESET_HW 26
#define USBCAN_CMD_SET_TX_TIMEOUT 30
#define USBCAN_DATAFF_DLC 0x0F
#define USBCAN_DATAFF_POS_CHANNEL 4
#define USBCAN_DATAFF_CHANNEL1 (1 << USBCAN_DATAFF_POS_CHANNEL)
/* Bits for USBCAN_CMD_SETCANMODE */
#define USBCAN_MODE_NORMAL 0x00
#define USBCAN_MODE_LISTEN_ONLY 0x01
#define USBCAN_MODE_TX_ECHO 0x02
#define USBCAN_MODE_RESERVED 0x04
#define USBCAN_MODE_HIGH_RES_TIMER 0x08
#define USBCAN_MODE_ONE_SHOT 0x10
#define USBCAN_CANID_TX_ECHO_BIT 0x01
#define USBCAN_CANID_TX_ECHO_MASK 0x03
#define USBCAN_CANID_EXT_SHIFT 0x03
#define USBCAN_CANID_STD_SHIFT 0x05
#define USBCAN_DATAFF_ENDOFRESET 0x0F
#define USBCAN_DATACANID_ENDOFRESET 0xFFF0
#define USBCAN_DATACANID_TIMESTAMP2 0xFCFF
/* from Host_GENERIC/Include/Usbcan32.h */
#define USBCAN_MSG_FF_EXT 0x80
#define USBCAN_MSG_FF_RTR 0x40
#define USBCAN_RESET_NO_TXBUFFER_FW 0x00000080 // no TX message buffer reset at firmware level
#define USBCAN_RESET_ONLY_TXBUFFER_FW (0x0000FFFF & ~(USBCAN_RESET_NO_TXBUFFER_FW))
/* from Host_GENERIC/Library/Ucancmd.c */
#define USBCAN_EEPROM_PID 0x03
#define USBCAN_EEPROM_SERIALNR 0x08
#define USBCAN_EEPROM_DEVICENR 0x0c
#define USBCAN_EEPROM_ADDR_STATUS_TIMEOUT 0x11 // sizeof(DWORD) // intel format
#define USBCAN_EEPROM_ADDR_RENUMTYP 0x0000 // sizeof(BYTE)
#define USBCAN_STARTTYPE_PLL_FAST 0x04
/* from Include/Vermco.h */
#define GETPC_MAJOR_VER(ver) ((ver) &0x000000FF)
#define GETPC_MINOR_VER(ver) (((ver) &0x0000FF00) >> 8)
#define GETPC_RELEASE_VER(ver) (((ver) &0xFFFF0000) >> 16)
/* Device capabilities for systec_can_shared::capabilities */
#define CAP_DUAL_CHANNEL BIT (0)
#define CAP_ONE_SHOT BIT (1)
#define CAP_PLL_SLOW BIT (2)
#define CAP_PLL_FAST BIT (3)
#define CAP_PLL_MASK (CAP_PLL_SLOW | CAP_PLL_FAST)
#define FIRMWARE_FILE(type) "systec_can-" __stringify (type) ".fw"
#define FW_VERSION(major, minor, release) (major | (minor << 8) | (release << 16))
/* name of firmware image */
#define FIRMWARE_NAME "systec_can-%.4x.fw"
#define BOOTLOADER_NAME "systec_can-bl-%.4x.fw"
#define SYSTEC_CAN_CHANNEL_FLAGS_TX_ECHO 0x01
#define SYSTEC_CAN_CHANNEL_FLAGS_CLEAR_TX \
0x02 /* This flag is set when the clear buffer mode is enabled */
#define SYSTEC_TYPE_FW 0x00
#define SYSTEC_TYPE_BL 0x01
static struct can_bittiming_const systec_can_bittiming_const = {
.name = KBUILD_MODNAME,
.tseg1_min = 1,
.tseg1_max = 16,
.tseg2_min = 1,
.tseg2_max = 8,
.sjw_max = 4,
.brp_min = 1,
.brp_max = 255, /* if > 127 the CLK flag is used */
.brp_inc = 1,
};
struct firmware_header
{
u8 header_version;
u8 type;
u8 fw_major;
u8 fw_minor;
__le16 fw_release_le;
__le16 product_id_le;
__le32 fw_address_le;
__le32 buffer_size_le;
} __packed;
static const struct usb_device_id systec_can_main_table[] = {
{ USB_DEVICE (0x0878, 0x1103) }, /* 8 or 16 channel device generation 3 */
{ USB_DEVICE (0x0878, 0x1104) }, /* single channel device generation 3 */
{ USB_DEVICE (0x0878, 0x1105) }, /* dual channel device generation 3 */
{ USB_DEVICE (0x0878, 0x1121) }, /* dual channel device generation 4 */
{ USB_DEVICE (0x0878, 0x1122) }, /* single channel device generation 4 */
{ USB_DEVICE (0x0878, 0x1123) }, /* 8 channel device generation 4 */
{ USB_DEVICE (0x0878, 0x1125) }, /* 16 channel device generation 4 */
{ USB_DEVICE (0x0878, 0x1145) }, /* 3204013 */
{ USB_DEVICE (0x0878, 0x1101) }, /* running device (normal Windows driver) */
{ USB_DEVICE (0x0878, 0x1181) }, /* running device (Windows network driver) */
{} /* Terminating entry */
};
MODULE_DEVICE_TABLE (usb, systec_can_main_table);
/* work structure to schedule commands */
struct systec_work
{
struct work_struct work;
struct systec_can_chan* chan;
enum can_state chan_state;
};
struct systec_can_shared;
/* ressources per channel */
struct systec_can_chan
{
struct can_priv can; /* must be the first member */
/* points to shared ressources between both channels */
struct systec_can_shared* shared_res;
/* ressources per channel */
u8 chan_no; /* channel number */
/* restart necessary because of bus-off */
bool restart_necessary;
/* internal device status delivered by status endpoint */
u32 mod_status_int;
/* current bittiming (extended baudrate register) */
u32 baud_ex_reg;
struct
{
/* variables which hold the circular buffer */
int echo_put;
int echo_get;
atomic_t pending;
/* variables for URB management */
atomic_t active_tx_urbs;
struct usb_anchor tx_submitted;
} tx;
/* corresponding net device */
struct net_device* netdev;
/* prepared work to restart channel (bus-off) within timer function */
struct systec_work restart_work;
/* mode flags for e.g. txecho */
int flags;
/* timeout in ms when unsendable messages shall be deleted */
u32 tx_timeout_ms;
};
/* shared ressources between both channels */
struct systec_can_shared
{
/* serial number (device eeprom) */
u8 serial_no[4];
/* device number (device eeprom) */
u8 device_no;
/* usb device */
struct usb_device* udev;
/* reference to private data of each channel */
struct systec_can_chan* channels[2];
struct usb_anchor rx_submitted;
int channels_running;
struct urb* intr_urb;
u8* intr_in_buffer;
u8* tx_cmd_buffer;
u8* rx_cmd_buffer;
struct mutex cmd_pending;
u8 endpoint_addresses[SYSTEC_NUM_ENDPOINTS];
u32 capabilities;
};
/* command message: driver -> CAN-Modul */
struct __attribute__ ((packed)) systec_cmd_msg
{
u32 size;
u8 cmd_data[SYSTEC_CMD_SIZE];
};
/* CAN message with standard ID */
struct __attribute__ ((packed)) systec_can_std_msg
{
u16 can_id;
u8 data[8];
u8 reserved[2];
};
/* CAN message with extended ID */
struct __attribute__ ((packed)) systec_can_ext_msg
{
u32 can_id;
u8 data[8];
};
/* the internal CAN message */
struct __attribute__ ((packed)) systec_can_msg
{
u8 format; /* first byte is always the format byte */
union
{ /* handle both types of ID */
struct systec_can_std_msg can_std_msg;
struct systec_can_ext_msg can_ext_msg;
} msg;
u8 timestamp[3]; /* last 3 bytes are the timestamp */
};
/* binary Intel hex record from usbcan_loader.h */
#define MAX_INTEL_HEX_RECORD_LENGTH 16
struct __attribute__ ((packed)) intel_hex_record
{
u8 length;
u16 address;
u8 type;
u8 data[MAX_INTEL_HEX_RECORD_LENGTH];
};
/* this hex record type indicates a header record */
#define HEX_RECORD_TYPE_HEADER 0xff
/* prototypes */
static void systec_can_main_disconnect (struct usb_interface* intf);
static int systec_can_main_probe (struct usb_interface* intf, const struct usb_device_id* id);
static int systec_can_main_probe_boot (struct usb_interface* intf, const struct usb_device_id* id);
static int systec_can_open (struct net_device* netdev);
static int systec_can_close (struct net_device* netdev);
static netdev_tx_t systec_can_start_xmit (struct sk_buff* skb, struct net_device* netdev);
static int systec_can_ioctl (struct net_device* netdev, struct ifreq* ifr, int cmd);
static int systec_can_set_bittiming (struct net_device* netdev);
static int systec_can_set_mode (struct net_device* netdev, enum can_mode mode);
static void systec_unlink_urbs (struct systec_can_chan* chan);
static int systec_can_send_cmd (struct systec_can_shared* shared_res, struct systec_cmd_msg* msg);
static void systec_dump_cmd_buf (struct systec_can_shared* shared_res, u8* buffer);
static void systec_dump_msg_buf (struct systec_can_shared* shared_res, u8* buffer);
static void systec_dump_buf (struct systec_can_shared* shared_res, u8* buffer, int size);
static void systec_can_read_stat_callback (struct urb* urb);
static void systec_can_read_data_callback (struct urb* urb);
static void systec_can_write_data_callback (struct urb* urb);
static void systec_can_rx_can_msg (struct systec_can_chan* chan, u8* msg_buf);
static int systec_dual_chan_device (struct systec_can_shared* shared_res);
static int systec_status_timeout (struct systec_can_shared* shared_res, u32* status_timeout);
static int systec_high_performance (
struct systec_can_shared* shared_res, bool* high_performance, u8* reg_value);
static void systec_handle_status_change (struct systec_can_chan* chan, u32 mod_status_int_new);
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 33)
#define get_can_dlc(i) (min_t (__u8, (i), 8))
static struct sk_buff* alloc_can_skb (struct net_device* dev, struct can_frame** cf);
static struct sk_buff* alloc_can_err_skb (struct net_device* dev, struct can_frame** cf);
#endif
#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 11, 0)
#define can_cc_dlc2len(dlc) get_can_dlc (dlc)
#endif
static void systec_work_chan_restart (struct work_struct* work);
static int systec_get_serial_no (struct systec_can_shared* shared_res);
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 34)
static inline const char* netdev_name (const struct net_device* dev)
{
if (dev->reg_state != NETREG_REGISTERED)
return "(unregistered net_device)";
return dev->name;
}
#define netdev_printk(level, netdev, format, args...) \
dev_printk (level, (netdev)->dev.parent, "%s: " format, netdev_name (netdev), ##args)
#define netdev_err(dev, format, args...) netdev_printk (KERN_ERR, dev, format, ##args)
#define netdev_warn(dev, format, args...) netdev_printk (KERN_WARNING, dev, format, ##args)
#define netdev_notice(dev, format, args...) netdev_printk (KERN_NOTICE, dev, format, ##args)
#define netdev_info(dev, format, args...) netdev_printk (KERN_INFO, dev, format, ##args)
#if defined(DEBUG)
#define netdev_dbg(__dev, format, args...) netdev_printk (KERN_DEBUG, __dev, format, ##args)
#else
#define netdev_dbg(__dev, format, args...) \
( \
{ \
if (0) \
netdev_printk (KERN_DEBUG, __dev, format, ##args); \
0; \
})
#endif
#endif
/* the usb handles to operate the CAN device */
static struct usb_driver systec_can_driver = {
.name = KBUILD_MODNAME,
.probe = systec_can_main_probe,
.disconnect = systec_can_main_disconnect,
.id_table = systec_can_main_table,
.soft_unbind = 1 /* we want to disable receiver on unbind */
};
/* the network handles to operate the CAN device */
static const struct net_device_ops systec_can_netdev_ops = {
.ndo_open = systec_can_open,
.ndo_stop = systec_can_close,
.ndo_start_xmit = systec_can_start_xmit,
.ndo_do_ioctl = systec_can_ioctl,
};
/* command work queue */
static struct workqueue_struct* cmd_wq;
static unsigned int debug = 0;
static bool hw_txecho = false;
#define systec_netdev_dbg(type, dev, format, args...) \
do \
{ \
if (debug & type) \
{ \
netdev_printk (KERN_DEBUG, dev, format, ##args); \
} \
} while (0)
#define systec_dev_dbg(type, dev, format, args...) \
do \
{ \
if (debug & type) \
{ \
dev_printk (KERN_DEBUG, dev, format, ##args); \
} \
} while (0)
static int systec_dbg (int type, const char* fmt, ...)
{
struct va_format vaf;
va_list args;
int r = 0;
if (debug & type)
{
va_start (args, fmt);
vaf.fmt = fmt;
vaf.va = &args;
r = printk ("%s%pV", KERN_DEBUG, &vaf);
va_end (args);
}
return r;
}
static int systec_setup_tx_timeout (struct systec_can_chan* chan)
{
struct systec_cmd_msg cmd;
struct systec_can_shared* shared_res = chan->shared_res;
int ret_val;
memset (&cmd, 0, sizeof (struct systec_cmd_msg));
cmd.size = SYSTEC_CMD_SIZE;
cmd.cmd_data[0] = USBCAN_CMD_SET_TX_TIMEOUT;
put_unaligned_le32 (chan->tx_timeout_ms, &cmd.cmd_data[1]);
cmd.cmd_data[6] = chan->chan_no;
ret_val = systec_can_send_cmd (shared_res, &cmd);
return ret_val;
}
static int systec_shared_start_urbs (
struct systec_can_shared* shared_res, struct net_device* netdev)
{
int err;
int i;
struct urb* new_urb;
u8* new_buf;
if (shared_res->channels_running++ > 0)
return (0);
/* create the rx URBs since there is not any URB running */
for (i = 0; i < MAX_RX_URBS; i++)
{
new_urb = NULL;
new_buf = NULL;
/* alloc the URB */
new_urb = usb_alloc_urb (0, GFP_KERNEL);
if (!new_urb)
{
netdev_err (netdev, "not enough memory for URB\n");
return (-ENOMEM);
}
/* create the buffer for the URB */
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 34)
new_buf = usb_alloc_coherent (
shared_res->udev, RX_BUFFER_SIZE, GFP_KERNEL, &new_urb->transfer_dma);
#else
new_buf = usb_buffer_alloc (
shared_res->udev, RX_BUFFER_SIZE, GFP_KERNEL, &new_urb->transfer_dma);
#endif
if (!new_buf)
{
netdev_err (netdev, "not enough memory for URB buffer\n");
usb_free_urb (new_urb);
return (-ENOMEM);
}
usb_fill_bulk_urb (new_urb, shared_res->udev,
usb_rcvbulkpipe (shared_res->udev, shared_res->endpoint_addresses[SYSTEC_EP_DATA_IN]),
new_buf, RX_BUFFER_SIZE, systec_can_read_data_callback, shared_res);
new_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
usb_anchor_urb (new_urb, &shared_res->rx_submitted);
err = usb_submit_urb (new_urb, GFP_KERNEL);
if (err)
{
usb_unanchor_urb (new_urb);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 34)
usb_free_coherent (shared_res->udev, RX_BUFFER_SIZE, new_buf, new_urb->transfer_dma);
#else
usb_buffer_free (shared_res->udev, RX_BUFFER_SIZE, new_buf, new_urb->transfer_dma);
#endif
usb_free_urb (new_urb);
break;
}
/* the driver is finished with the URB */
usb_free_urb (new_urb);
}
/* check if one urb could be submited at least */
if (i == 0)
{
netdev_err (netdev, "could not submit any read data urb\n");
return (err);
}
if (i < MAX_RX_URBS)
{
netdev_warn (
netdev, "only %d of %d urbs could be submitted (err = %d)\n", i, MAX_RX_URBS, err);
}
/* setup status urb */
new_urb = usb_alloc_urb (0, GFP_KERNEL);
if (new_urb)
{
shared_res->intr_urb = new_urb;
usb_fill_int_urb (new_urb, shared_res->udev,
usb_rcvintpipe (shared_res->udev, shared_res->endpoint_addresses[SYSTEC_EP_STAT_IN]),
shared_res->intr_in_buffer, INTR_IN_BUFFER_SIZE, systec_can_read_stat_callback,
shared_res, SYSTEC_INT_URB_POLL_TIME);
err = usb_submit_urb (shared_res->intr_urb, GFP_KERNEL);
if (err)
{
usb_free_urb (new_urb);
shared_res->intr_urb = NULL;
netdev_err (netdev, "intr URB submit failed: %d\n", err);
return (err);
}
}
else
{
netdev_err (netdev, "Couldn't alloc intr URB\n");
return (-ENOMEM);
}
return (0);
}
static void systec_shared_stop_urbs (
struct systec_can_shared* shared_res, struct net_device* netdev)
{
if (--shared_res->channels_running > 0)
return;
/* stop status urb */
if (shared_res->intr_urb)
{
usb_kill_urb (shared_res->intr_urb);
usb_free_urb (shared_res->intr_urb);
shared_res->intr_urb = NULL;
systec_netdev_dbg (SYSTEC_DEBUG_DRIVER, netdev, "%s: intr_urb stopped\n", __func__);
}
/* stop rx urb */
usb_kill_anchored_urbs (&shared_res->rx_submitted);
systec_netdev_dbg (SYSTEC_DEBUG_DRIVER, netdev, "%s: rx_urb stopped\n", __func__);
}
static int systec_can_open (struct net_device* netdev)
{
struct systec_can_chan* chan = netdev_priv (netdev);
int err;
struct systec_cmd_msg cmd;
systec_netdev_dbg (SYSTEC_DEBUG_DRIVER, netdev, "systec_can_open\n");
/* open CAN device */
err = open_candev (netdev);
if (err)
{
return err;
}
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0)) \
&& (LINUX_VERSION_CODE < KERNEL_VERSION(5, 19, 0))
// can_led_event (netdev, // can_led_EVENT_OPEN);
#endif
chan->mod_status_int &= ~SYSTEC_CAN_STATE_MASK;
err = systec_shared_start_urbs (chan->shared_res, netdev);
if (err)
{
goto failed;
}
/* clear can status */
memset (&cmd, 0, sizeof (struct systec_cmd_msg));
cmd.size = SYSTEC_CMD_SIZE;
cmd.cmd_data[0] = USBCAN_CMD_RESET;
cmd.cmd_data[6] = chan->chan_no;
err = systec_can_send_cmd (chan->shared_res, &cmd);
if (err)
goto failed;
/* set AMR */
memset (&cmd, 0, sizeof (struct systec_cmd_msg));
cmd.size = SYSTEC_CMD_SIZE;
cmd.cmd_data[0] = USBCAN_CMD_SETAMR;
cmd.cmd_data[6] = chan->chan_no;
put_unaligned_be32 (0xffffffff, &cmd.cmd_data[1]);
err = systec_can_send_cmd (chan->shared_res, &cmd);
if (err)
goto failed;
/* set ACR */
memset (&cmd, 0, sizeof (struct systec_cmd_msg));
cmd.size = SYSTEC_CMD_SIZE;
cmd.cmd_data[0] = USBCAN_CMD_SETACR;
cmd.cmd_data[6] = chan->chan_no;
put_unaligned_be32 (0x0, &cmd.cmd_data[1]);
err = systec_can_send_cmd (chan->shared_res, &cmd);
if (err)
goto failed;
/* set CAN mode */
memset (&cmd, 0, sizeof (struct systec_cmd_msg));
cmd.size = SYSTEC_CMD_SIZE;
cmd.cmd_data[0] = USBCAN_CMD_SETCANMODE;
cmd.cmd_data[1] = USBCAN_MODE_NORMAL; /* standard r/w mode */
if (chan->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
cmd.cmd_data[1] |= USBCAN_MODE_LISTEN_ONLY;
if (chan->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT)
cmd.cmd_data[1] |= USBCAN_MODE_ONE_SHOT;
if (hw_txecho)
{
cmd.cmd_data[1] |= USBCAN_MODE_TX_ECHO;
chan->flags |= SYSTEC_CAN_CHANNEL_FLAGS_TX_ECHO;
}
else
{
chan->flags &= ~SYSTEC_CAN_CHANNEL_FLAGS_TX_ECHO;
}
cmd.cmd_data[6] = chan->chan_no;
err = systec_can_send_cmd (chan->shared_res, &cmd);
if (err)
goto failed;
/* start device */
memset (&cmd, 0, sizeof (struct systec_cmd_msg));
cmd.size = SYSTEC_CMD_SIZE;
cmd.cmd_data[0] = USBCAN_CMD_INITIALIZE;
cmd.cmd_data[6] = chan->chan_no;
err = systec_can_send_cmd (chan->shared_res, &cmd);
if (err)
goto failed;
/* setup Tx timeout which is cleared after USBCAN_CMD_INITIALIZE */
if (systec_dual_chan_device (chan->shared_res))
{
err = systec_setup_tx_timeout (chan);
if (err)
goto failed;
}
chan->can.state = CAN_STATE_ERROR_ACTIVE;
netif_start_queue (netdev);
return (0);
failed:
systec_shared_stop_urbs (chan->shared_res, netdev);
return err;
}
/*
handler of status endpoint (interrupt endpoint)
*/
static void systec_can_read_stat_callback (struct urb* urb)
{
struct systec_can_shared* shared_res = urb->context;
struct systec_can_chan* chan;
u32 mod_status;
int ret_stat;
mod_status = be32_to_cpup (urb->transfer_buffer);
chan = shared_res->channels[(mod_status & SYSTEC_CAN_CHANNEL)];
switch (urb->status)
{
case 0:
/* clear channel number bit */
mod_status &= ~SYSTEC_CAN_CHANNEL;
if ((mod_status != chan->mod_status_int) && netif_device_present (chan->netdev)
&& netif_running (chan->netdev))
{
/* channel status has been changed */
systec_dev_dbg (SYSTEC_DEBUG_STAT_CB, &shared_res->udev->dev,
"systec_can_read_stat_callback (%lu): %d, %x\n", jiffies, urb->status,
mod_status);
systec_dump_buf (shared_res, urb->transfer_buffer, urb->actual_length);
/* handle status */
systec_handle_status_change (chan, mod_status);
/* update internal CAN status */
chan->mod_status_int = mod_status;
}
break;
case -ECONNRESET: /* unlink */
case -ENOENT:
case -EPIPE:
case -EPROTO:
case -ESHUTDOWN:
return;
default:
netdev_info (chan->netdev, "Interrupt URB aborted %d\n", urb->status);
break;
}
/* resubmit status urb */
ret_stat = usb_submit_urb (urb, GFP_ATOMIC);
if (ret_stat == -ENODEV)
netif_device_detach (chan->netdev);
else if (ret_stat)
netdev_err (chan->netdev, "failed resubmitting intr urb: %d\n", ret_stat);
return;
}
static void systec_handle_status_change (struct systec_can_chan* chan, u32 mod_status_int_new)
{
struct can_frame* cf;
struct sk_buff* skb;
struct net_device_stats* stats = &chan->netdev->stats;
enum can_state new_state = chan->can.state;
systec_netdev_dbg (SYSTEC_DEBUG_ERR_HANDLING, chan->netdev,
"systec_handle_status_change (%lu): %x\n", jiffies, chan->mod_status_int);
/* convert device status into CAN state */
if (mod_status_int_new & SYSTEC_CAN_STATE_ERR_BOFF)
{
new_state = CAN_STATE_BUS_OFF;
chan->restart_necessary = 1;
}
else if (!chan->restart_necessary)
{
if (mod_status_int_new & SYSTEC_CAN_STATE_ERR_PASS)
{
new_state = CAN_STATE_ERROR_PASSIVE;
}
else
{
if (mod_status_int_new & SYSTEC_CAN_STATE_ERR_WARN)
{
new_state = CAN_STATE_ERROR_WARNING;
}
else
{
new_state = CAN_STATE_ERROR_ACTIVE;
/*
todo: check other states
*/
}
}
}
systec_netdev_dbg (SYSTEC_DEBUG_ERR_HANDLING, chan->netdev, " new_state: %x, current: %x\n",
new_state, chan->can.state);
if ((mod_status_int_new ^ chan->mod_status_int)
& (SYSTEC_USB_ERR_STATUS_TIMEOUT | SYSTEC_USB_ERR_WDT_RESET))
{ // Generic channel independent state changed
if ((mod_status_int_new & SYSTEC_USB_ERR_STATUS_TIMEOUT)
&& ((chan->mod_status_int & SYSTEC_USB_ERR_STATUS_TIMEOUT) == 0))
{
netdev_warn (chan->netdev, "device has been reset because of Status Timeout.\n");
}
if ((mod_status_int_new & SYSTEC_USB_ERR_WDT_RESET)
&& ((chan->mod_status_int & SYSTEC_USB_ERR_WDT_RESET) == 0))
{
netdev_warn (chan->netdev, "device has been reset by Watchdog.\n");
}
if (new_state == chan->can.state)
{
return;
}
}
else if (new_state == chan->can.state)
{
systec_netdev_dbg (SYSTEC_DEBUG_ERR_HANDLING, chan->netdev,
"systec_handle_status_change: unknown status change 0x%X\n", mod_status_int_new);
return;
}
/* alloc error frame */
skb = alloc_can_err_skb (chan->netdev, &cf);
if (unlikely (skb == NULL))
{
return;
}
switch (chan->can.state)
{
case CAN_STATE_ERROR_ACTIVE:
/* to ERROR_WARNING, ERROR_PASSIVE, BUS_OFF */
if (new_state >= CAN_STATE_ERROR_WARNING && new_state <= CAN_STATE_BUS_OFF)
{
chan->can.can_stats.error_warning++;
cf->can_id |= CAN_ERR_CRTL;
cf->data[1] = CAN_ERR_CRTL_TX_WARNING | CAN_ERR_CRTL_RX_WARNING;
}
/* fall through */
#if LINUX_VERSION_CODE > KERNEL_VERSION(5, 4, 0)
fallthrough;
#endif
case CAN_STATE_ERROR_WARNING:
/* to ERROR_PASSIVE, BUS_OFF */
if (new_state >= CAN_STATE_ERROR_PASSIVE && new_state <= CAN_STATE_BUS_OFF)
{
chan->can.can_stats.error_passive++;
cf->can_id |= CAN_ERR_CRTL;
cf->data[1] = CAN_ERR_CRTL_TX_PASSIVE | CAN_ERR_CRTL_RX_PASSIVE;
chan->can.can_stats.error_passive++;
}
break;
case CAN_STATE_BUS_OFF:
/* to ERROR_ACTIVE, ERROR_WARNING, ERROR_PASSIVE */
break;
default:
break;
}
switch (new_state)
{
case CAN_STATE_ERROR_ACTIVE:
cf->can_id |= CAN_ERR_PROT;
cf->data[2] = CAN_ERR_PROT_ACTIVE;
break;
case CAN_STATE_BUS_OFF:
cf->can_id |= CAN_ERR_BUSOFF;
chan->can.can_stats.bus_off++;
can_bus_off (chan->netdev);
default:
break;
}
chan->can.state = new_state;
stats->rx_packets++;
stats->rx_bytes += cf->can_dlc;
netif_rx (skb);
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0)) \
&& (LINUX_VERSION_CODE < KERNEL_VERSION(5, 19, 0))
// can_led_event (chan->netdev, // can_led_EVENT_RX);
#endif
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 33)
static struct sk_buff* alloc_can_skb (struct net_device* dev, struct can_frame** cf)
{
struct sk_buff* skb;
skb = netdev_alloc_skb (dev, sizeof (struct can_frame));
if (unlikely (!skb))
return NULL;
skb->protocol = htons (ETH_P_CAN);
skb->pkt_type = PACKET_BROADCAST;
skb->ip_summed = CHECKSUM_UNNECESSARY;
*cf = (struct can_frame*) skb_put (skb, sizeof (struct can_frame));
memset (*cf, 0, sizeof (struct can_frame));
return skb;
}
static struct sk_buff* alloc_can_err_skb (struct net_device* dev, struct can_frame** cf)
{
struct sk_buff* skb;
skb = alloc_can_skb (dev, cf);
if (unlikely (!skb))
return NULL;
(*cf)->can_id = CAN_ERR_FLAG;
(*cf)->can_dlc = CAN_ERR_DLC;
return skb;
}
#endif