-
Notifications
You must be signed in to change notification settings - Fork 0
/
toucan.c
992 lines (792 loc) · 22.4 KB
/
toucan.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
/*
* CAN driver for Rusoku Technologies TouCAN adapter
*
* 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; version 2 of the License.
*
* 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.
*
*/
#include <linux/module.h>
#include <linux/netdevice.h>
#include <linux/usb.h>
#include <linux/can.h>
#include <linux/can/dev.h>
/* driver constants */
#define MAX_RX_URBS 20
#define MAX_TX_URBS 20
#define RX_BUFFER_SIZE 64
struct toucan_usb_tx_urb_context {
struct toucan_usb_priv *priv;
u32 echo_index;
u8 dlc;
};
struct toucan_usb_priv {
struct can_priv can; /* must be the first member */
struct sk_buff *echo_skb[MAX_TX_URBS];
struct usb_device *udev;
struct net_device *netdev;
atomic_t active_tx_urbs;
struct usb_anchor tx_submitted;
struct toucan_usb_tx_urb_context tx_contexts[MAX_TX_URBS];
struct usb_anchor rx_submitted;
struct can_berr_counter bec;
struct mutex toucan_cmd_lock;
unsigned int ep_ctrl_in;
unsigned int ep_ctrl_out;
unsigned int ep_bulk_in;
unsigned int ep_bulk_out;
const struct toucan_usb_device *adapter;
};
struct toucan_usb_device {
u8 ep_bulk_in;
u8 ep_bulk_out;
const struct can_bittiming_const *bittiming_const;
void (*usb_rx_pkt_decode)(struct urb *urb);
void (*usb_tx_pkt_encode)(struct sk_buff *skb, void *msg);
__u32 base_clock;
};
#define TOUCAN_MSG_FLG_EXTID 0x01
#define TOUCAN_MSG_FLG_RTR 0x02
#define TOUCAN_MSG_FLG_ERR_FLAG 0x04
struct __packed toucan_usb_msg {
u8 flags;
__be32 id;
u8 dlc;
u8 data[8];
__be32 timestamp;
};
/* Status message flags */
#define TOUCAN_STATE_MSG_OK 0x00 /* Normal condition. */
#define TOUCAN_STATE_MSG_OVERRUN 0x01 /* Overrun occured when sending */
#define TOUCAN_STATE_MSG_BUSLIGHT 0x02 /* Error counter has reached 96 */
#define TOUCAN_STATE_MSG_BUSHEAVY 0x03 /* Error count. has reached 128 */
#define TOUCAN_STATE_MSG_BUSOFF 0x04 /* Device is in BUSOFF */
#define TOUCAN_STATE_MSG_STUFF 0x20 /* Stuff Error */
#define TOUCAN_STATE_MSG_FORM 0x21 /* Form Error */
#define TOUCAN_STATE_MSG_ACK 0x23 /* Ack Error */
#define TOUCAN_STATE_MSG_BIT0 0x24 /* Bit1 Error */
#define TOUCAN_STATE_MSG_BIT1 0x25 /* Bit0 Error */
#define TOUCAN_STATE_MSG_CRC 0x27 /* CRC Error */
struct __packed toucan_status_msg {
u8 state;
u8 rxerr;
u8 txerr;
};
#define TOUCAN_INI_FLG_SILENT 0x01
#define TOUCAN_INI_FLG_LOOPBACK 0x02
#define TOUCAN_INI_FLG_DISABLE_AUTO_RESTRANS 0x04
#define TOUCAN_INI_FLG_STATUS_FRAME 0x100
struct __packed toucan_init_msg {
u8 tseg1;
u8 tseg2;
u8 sjw;
__be16 brp;
__be32 flags;
};
#define TOUCAN_LASTERR_OK 0x0
enum toucan_usb_cmd {
TOUCAN_CAN_INTERFACE_INIT = 0x01,
TOUCAN_CAN_INTERFACE_DEINIT,
TOUCAN_CAN_INTERFACE_START,
TOUCAN_CAN_INTERFACE_STOP,
TOUCAN_GET_HARDWARE_VERSION = 0x10,
TOUCAN_GET_FIRMWARE_VERSION,
TOUCAN_GET_BOOTLOADER_VERSION,
TOUCAN_GET_SERIAL_NUMBER,
TOUCAN_GET_LAST_ERROR_CODE = 0x20,
};
#define USB_RUSOKU_VENDOR_ID 0x16d0
#define USB_RUSOKU_TOUCAN_PRODUCT_ID 0x0eac
static const struct usb_device_id toucan_usb_table[] = {
{ USB_DEVICE(USB_RUSOKU_VENDOR_ID, USB_RUSOKU_TOUCAN_PRODUCT_ID) },
{ } /* Terminating entry */
};
MODULE_DEVICE_TABLE(usb, toucan_usb_table);
static int __toucan_usb_cmd(struct toucan_usb_priv *priv, int direction, u8 cmd,
u16 len, void *msg)
{
int flags;
int num_bytes_read;
int ret;
unsigned int pipe;
switch (direction) {
case USB_DIR_IN:
flags = USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
pipe = priv->ep_ctrl_in;
break;
case USB_DIR_OUT:
flags = USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
pipe = priv->ep_ctrl_out;
break;
default:
return -EINVAL;
}
ret = usb_control_msg(priv->udev, pipe, cmd, flags, 0, 0, msg, len, 1000);
if (ret < 0) {
netdev_err(priv->netdev, "sending command message failed\n");
return ret;
} else {
num_bytes_read = ret;
}
if (direction == USB_DIR_IN && num_bytes_read != len)
ret = -EPROTO;
return ret;
}
static int toucan_usb_cmd(struct toucan_usb_priv *priv, int direction, u8 cmd,
u16 len, void *msg)
{
int ret;
void *buff_msg = NULL;
void *buff_err = NULL;
if (len > 0) {
buff_msg = kmalloc(len, GFP_KERNEL);
if (!buff_msg)
return -ENOMEM;
if (direction == USB_DIR_OUT)
memcpy(buff_msg, msg, len);
}
buff_err = kmalloc(1, GFP_KERNEL);
if (!buff_err) {
ret = -ENOMEM;
goto free_buf;
}
mutex_lock(&priv->toucan_cmd_lock);
ret = __toucan_usb_cmd(priv, direction, cmd, len, buff_msg);
if (ret < 0)
goto cleanup;
ret = __toucan_usb_cmd(priv, USB_DIR_IN, TOUCAN_GET_LAST_ERROR_CODE, 1, buff_err);
if (ret < 0)
goto cleanup;
if (*((u8*)buff_err) != TOUCAN_LASTERR_OK) {
netdev_err(priv->netdev, "command 0x%x produced an error\n", cmd);
ret = -EINVAL;
goto cleanup;
}
if (direction == USB_DIR_IN && len > 0)
memcpy(msg, buff_msg, len);
ret = 0;
cleanup:
mutex_unlock(&priv->toucan_cmd_lock);
free_buf:
if (buff_msg)
kfree(buff_msg);
if (buff_err)
kfree(buff_err);
return ret;
}
static void toucan_usb_unlink_all_urbs(struct toucan_usb_priv *priv)
{
int i;
usb_kill_anchored_urbs(&priv->rx_submitted);
usb_kill_anchored_urbs(&priv->tx_submitted);
atomic_set(&priv->active_tx_urbs, 0);
for (i = 0; i < MAX_TX_URBS; i++)
priv->tx_contexts[i].echo_index = MAX_TX_URBS;
}
static int toucan_usb_get_hw_version(struct toucan_usb_priv *priv, u32 *res)
{
int ret = 0;
__be32 ver;
ret = toucan_usb_cmd(priv, USB_DIR_IN, TOUCAN_GET_HARDWARE_VERSION, 4, &ver);
if (ret)
return ret;
*res = be32_to_cpu(ver);
return ret;
}
static int toucan_usb_get_fw_version(struct toucan_usb_priv *priv, u32 *res)
{
int ret = 0;
__be32 ver;
ret = toucan_usb_cmd(priv, USB_DIR_IN, TOUCAN_GET_FIRMWARE_VERSION, 4, &ver);
if (ret)
return ret;
*res = be32_to_cpu(ver);
return ret;
}
static int toucan_usb_cmd_init(struct toucan_usb_priv *priv)
{
struct can_bittiming *bt = &priv->can.bittiming;
struct toucan_init_msg msg;
u32 ctrlmode = priv->can.ctrlmode;
u32 flags = TOUCAN_INI_FLG_STATUS_FRAME; // enable error message reception
memset(&msg, 0, sizeof(msg));
msg.tseg1 = bt->prop_seg + bt->phase_seg1;
msg.tseg2 = bt->phase_seg2;
msg.sjw = bt->sjw;
msg.brp = cpu_to_be16((u16)bt->brp);
if (ctrlmode & CAN_CTRLMODE_LOOPBACK)
flags |= TOUCAN_INI_FLG_LOOPBACK;
if (ctrlmode & CAN_CTRLMODE_LISTENONLY)
flags |= TOUCAN_INI_FLG_SILENT;
if (ctrlmode & CAN_CTRLMODE_ONE_SHOT)
flags |= TOUCAN_INI_FLG_DISABLE_AUTO_RESTRANS;
msg.flags = cpu_to_be32(flags);
return toucan_usb_cmd(priv, USB_DIR_OUT, TOUCAN_CAN_INTERFACE_INIT,
sizeof(msg), &msg);
}
static int toucan_usb_cmd_deinit(struct toucan_usb_priv *priv)
{
return toucan_usb_cmd(priv, USB_DIR_OUT, TOUCAN_CAN_INTERFACE_DEINIT, 0, NULL);
}
static int toucan_usb_cmd_start(struct toucan_usb_priv *priv)
{
return toucan_usb_cmd(priv, USB_DIR_OUT, TOUCAN_CAN_INTERFACE_START, 0, NULL);
}
static int toucan_usb_cmd_stop(struct toucan_usb_priv *priv)
{
return toucan_usb_cmd(priv, USB_DIR_OUT, TOUCAN_CAN_INTERFACE_STOP, 0, NULL);
}
static int toucan_usb_set_mode(struct net_device *netdev, enum can_mode mode)
{
struct toucan_usb_priv *priv = netdev_priv(netdev);
int err = 0;
switch (mode) {
case CAN_MODE_START:
err = toucan_usb_cmd_stop(priv);
if (err) {
netdev_warn(netdev, "restart: couldn't stop device");
break;
}
err = toucan_usb_cmd_deinit(priv);
if (err) {
netdev_warn(netdev, "restart: couldn't deinit device");
break;
}
err = toucan_usb_cmd_init(priv);
if (err) {
netdev_warn(netdev, "restart: couldn't init device");
break;
}
err = toucan_usb_cmd_start(priv);
if (err){
netdev_warn(netdev, "couldn't start device");
break;
}
break;
default:
return -EOPNOTSUPP;
}
return err;
}
static void toucan_usb_write_bulk_callback(struct urb *urb)
{
struct toucan_usb_tx_urb_context *context = urb->context;
struct toucan_usb_priv *priv;
struct net_device *netdev;
BUG_ON(!context);
priv = context->priv;
netdev = priv->netdev;
/* free up our allocated buffer */
usb_free_coherent(urb->dev, urb->transfer_buffer_length,
urb->transfer_buffer, urb->transfer_dma);
if (!netif_device_present(netdev))
return;
switch(urb->status) {
case 0:
netdev->stats.tx_packets++;
netdev->stats.tx_bytes += context->dlc;
can_get_echo_skb(netdev, context->echo_index);
break;
case -ENOENT:
case -ECONNRESET:
case -EPROTO:
case -ESHUTDOWN:
can_free_echo_skb(netdev, context->echo_index);
break;
default:
if (net_ratelimit())
netdev_err(netdev, "Tx URB aborted (%d)\n",
urb->status);
can_free_echo_skb(netdev, context->echo_index);
break;
}
/* Release context */
context->echo_index = MAX_TX_URBS;
atomic_dec(&priv->active_tx_urbs);
if(!urb->status)
netif_wake_queue(netdev);
}
static void toucan_rx_err_msg(struct toucan_usb_priv *priv,
struct toucan_usb_msg *msg)
{
struct can_frame *cf;
struct sk_buff *skb;
struct net_device_stats *stats = &priv->netdev->stats;
struct toucan_status_msg *status;
int rx_errors = 0;
int tx_errors = 0;
status = (struct toucan_status_msg*)(&msg->data);
skb = alloc_can_err_skb(priv->netdev, &cf);
if (!skb)
return;
switch (status->state) {
case TOUCAN_STATE_MSG_OK:
priv->can.state = CAN_STATE_ERROR_ACTIVE;
cf->can_id |= CAN_ERR_PROT;
cf->data[2] = CAN_ERR_PROT_ACTIVE;
break;
case TOUCAN_STATE_MSG_BUSOFF:
priv->can.state = CAN_STATE_BUS_OFF;
cf->can_id |= CAN_ERR_BUSOFF;
priv->can.can_stats.bus_off++;
can_bus_off(priv->netdev);
break;
case TOUCAN_STATE_MSG_OVERRUN:
case TOUCAN_STATE_MSG_BUSLIGHT:
case TOUCAN_STATE_MSG_BUSHEAVY:
cf->can_id |= CAN_ERR_CRTL;
break;
default:
priv->can.state = CAN_STATE_ERROR_WARNING;
cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
priv->can.can_stats.bus_error++;
break;
}
switch (status->state) {
case TOUCAN_STATE_MSG_OK:
case TOUCAN_STATE_MSG_BUSOFF:
break;
case TOUCAN_STATE_MSG_ACK:
cf->can_id |= CAN_ERR_ACK;
tx_errors = 1;
break;
case TOUCAN_STATE_MSG_CRC:
cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
rx_errors = 1;
break;
case TOUCAN_STATE_MSG_BIT0:
cf->data[2] |= CAN_ERR_PROT_BIT0;
tx_errors = 1;
break;
case TOUCAN_STATE_MSG_BIT1:
cf->data[2] |= CAN_ERR_PROT_BIT1;
tx_errors = 1;
break;
case TOUCAN_STATE_MSG_FORM:
cf->data[2] |= CAN_ERR_PROT_FORM;
rx_errors = 1;
break;
case TOUCAN_STATE_MSG_STUFF:
cf->data[2] |= CAN_ERR_PROT_STUFF;
rx_errors = 1;
break;
case TOUCAN_STATE_MSG_OVERRUN:
cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
stats->rx_over_errors++;
rx_errors = 1;
break;
case TOUCAN_STATE_MSG_BUSLIGHT:
priv->can.state = CAN_STATE_ERROR_WARNING;
cf->data[1] = (status->txerr > status->rxerr) ?
CAN_ERR_CRTL_TX_WARNING :
CAN_ERR_CRTL_RX_WARNING;
priv->can.can_stats.error_warning++;
break;
case TOUCAN_STATE_MSG_BUSHEAVY:
priv->can.state = CAN_STATE_ERROR_PASSIVE;
cf->data[1] = (status->txerr > status->rxerr) ?
CAN_ERR_CRTL_TX_PASSIVE :
CAN_ERR_CRTL_RX_PASSIVE;
priv->can.can_stats.error_passive++;
break;
default:
netdev_warn(priv->netdev,
"Unknown status/error message (%d)\n", status->state);
break;
}
if (tx_errors) {
cf->data[2] |= CAN_ERR_PROT_TX;
stats->tx_errors++;
}
if (rx_errors)
stats->rx_errors++;
cf->data[6] = status->txerr;
cf->data[7] = status->rxerr;
priv->bec.txerr = status->txerr;
priv->bec.rxerr = status->rxerr;
stats->rx_packets++;
stats->rx_bytes += cf->can_dlc;
netif_rx(skb);
}
static void toucan_rx_can_msg(struct toucan_usb_priv *priv,
struct toucan_usb_msg *msg)
{
struct can_frame *cf;
struct sk_buff *skb;
struct net_device_stats *stats = &priv->netdev->stats;
if (msg->flags & TOUCAN_MSG_FLG_ERR_FLAG) {
toucan_rx_err_msg(priv, msg);
} else {
skb = alloc_can_skb(priv->netdev, &cf);
if (!skb)
return;
cf->can_id = be32_to_cpu(msg->id);
cf->can_dlc = get_can_dlc(msg->dlc);
if (msg->flags & TOUCAN_MSG_FLG_EXTID)
cf->can_id |= CAN_EFF_FLAG;
if (msg->flags & TOUCAN_MSG_FLG_RTR)
cf->can_id |= CAN_RTR_FLAG;
else
memcpy(cf->data, msg->data, cf->can_dlc);
stats->rx_packets++;
stats->rx_bytes += cf->can_dlc;
netif_rx(skb);
}
}
static void toucan_rx_usb_pkt(struct urb *urb)
{
int pos = 0;
struct toucan_usb_msg *msg;
struct toucan_usb_priv *priv = urb->context;
while (pos < urb->actual_length) {
if (pos + sizeof(*msg) > urb->actual_length) {
netdev_err(priv->netdev, "format error\n");
break;
}
msg = (struct toucan_usb_msg *)(urb->transfer_buffer + pos);
toucan_rx_can_msg(priv, msg);
pos += sizeof(*msg);
}
}
static void toucan_tx_usb_pkt(struct sk_buff *skb, void *buf)
{
struct can_frame *cf = (struct can_frame *)skb->data;
struct toucan_usb_msg *msg = (struct toucan_usb_msg *)buf;
memset(buf, 0, sizeof(*msg));
if (cf->can_id & CAN_RTR_FLAG)
msg->flags |= TOUCAN_MSG_FLG_RTR;
if (cf->can_id & CAN_EFF_FLAG)
msg->flags |= TOUCAN_MSG_FLG_EXTID;
msg->id = cpu_to_be32(cf->can_id & CAN_ERR_MASK);
msg->dlc = cf->can_dlc;
memcpy(msg->data, cf->data, cf->can_dlc);
}
static void toucan_usb_read_bulk_callback(struct urb *urb)
{
struct toucan_usb_priv *priv = urb->context;
struct net_device *netdev;
int retval;
netdev = priv->netdev;
if (!netif_device_present(netdev))
return;
switch (urb->status) {
case 0: /* success */
break;
case -ENOENT:
case -EPIPE:
case -EPROTO:
case -ESHUTDOWN:
return;
default:
if (net_ratelimit())
netdev_err(netdev, "Rx URB aborted (%d)\n", urb->status);
goto resubmit_urb;
}
priv->adapter->usb_rx_pkt_decode(urb);
resubmit_urb:
usb_fill_bulk_urb(urb, priv->udev, priv->ep_bulk_in,
urb->transfer_buffer, RX_BUFFER_SIZE,
toucan_usb_read_bulk_callback, priv);
retval = usb_submit_urb(urb, GFP_ATOMIC);
if (retval == -ENODEV)
netif_device_detach(netdev);
else if (retval)
netdev_err(netdev, "failed resubmitting read bulk urb: %d\n",
retval);
}
static netdev_tx_t toucan_usb_start_xmit(struct sk_buff *skb,
struct net_device *netdev)
{
struct toucan_usb_priv *priv = netdev_priv(netdev);
struct toucan_usb_tx_urb_context *context = NULL;
struct net_device_stats *stats = &netdev->stats;
struct can_frame *cf;
struct urb *urb;
int i, err;
u8 *buf;
size_t size = sizeof(struct toucan_usb_msg);
if (can_dropped_invalid_skb(netdev, skb))
return NETDEV_TX_OK;
urb = usb_alloc_urb(0, GFP_ATOMIC);
if (!urb)
goto nomem;
buf = usb_alloc_coherent(priv->udev, size, GFP_ATOMIC,
&urb->transfer_dma);
if (!buf) {
netdev_err(netdev, "No memory left for USB buffer\n");
goto nomembuf;
}
priv->adapter->usb_tx_pkt_encode(skb, buf);
for (i = 0; i < MAX_TX_URBS; i++) {
if (priv->tx_contexts[i].echo_index == MAX_TX_URBS) {
context = &priv->tx_contexts[i];
context->echo_index = i;
context->priv = priv;
break;
}
}
/* May never happen! When this happens we'd more URBs in flight as
* allowed (MAX_TX_URBS).
*/
if (!context)
goto nofreecontext;
cf = (struct can_frame *)skb->data;
context->dlc = cf->can_dlc;
usb_fill_bulk_urb(urb, priv->udev, priv->ep_bulk_out, buf,
size, toucan_usb_write_bulk_callback, context);
urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
usb_anchor_urb(urb, &priv->tx_submitted);
can_put_echo_skb(skb, netdev, context->echo_index);
atomic_inc(&priv->active_tx_urbs);
err = usb_submit_urb(urb, GFP_ATOMIC);
if (unlikely(err))
goto failed;
else if (atomic_read(&priv->active_tx_urbs) >= MAX_TX_URBS)
/* Slow down tx path */
netif_stop_queue(netdev);
/* Release our reference to this URB, the USB core will eventually free
* it entirely.
*/
usb_free_urb(urb);
return NETDEV_TX_OK;
nofreecontext:
usb_free_coherent(priv->udev, size, buf, urb->transfer_dma);
usb_free_urb(urb);
netdev_warn(netdev, "couldn't find free context");
return NETDEV_TX_BUSY;
failed:
can_free_echo_skb(netdev, context->echo_index);
usb_unanchor_urb(urb);
usb_free_coherent(priv->udev, size, buf, urb->transfer_dma);
atomic_dec(&priv->active_tx_urbs);
if (err == -ENODEV)
netif_device_detach(netdev);
else
netdev_warn(netdev, "failed tx_urb %d\n", err);
nomembuf:
usb_free_urb(urb);
nomem:
dev_kfree_skb(skb);
stats->tx_dropped++;
return NETDEV_TX_OK;
}
static int toucan_usb_alloc_rx_urbs(struct toucan_usb_priv *priv)
{
struct net_device *netdev = priv->netdev;
int err = 0, i;
for (i = 0; i < MAX_RX_URBS; i++) {
struct urb *urb = NULL;
u8 *buf;
/* create a URB, and a buffer for it */
urb = usb_alloc_urb(0, GFP_KERNEL);
if (!urb) {
err = -ENOMEM;
break;
}
buf = usb_alloc_coherent(priv->udev, RX_BUFFER_SIZE, GFP_KERNEL,
&urb->transfer_dma);
if (!buf) {
usb_free_urb(urb);
err = -ENOMEM;
break;
}
usb_fill_bulk_urb(urb, priv->udev, priv->ep_bulk_in, buf,
RX_BUFFER_SIZE, toucan_usb_read_bulk_callback,
priv);
urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
usb_anchor_urb(urb, &priv->rx_submitted);
err = usb_submit_urb(urb, GFP_KERNEL);
if (err) {
usb_unanchor_urb(urb);
usb_free_coherent(priv->udev, RX_BUFFER_SIZE, buf,
urb->transfer_dma);
usb_free_urb(urb);
break;
}
/* Drop reference, USB core will take care of freeing it */
usb_free_urb(urb);
}
/* Did we submit any URBs */
if (i == 0) {
netdev_warn(netdev, "couldn't setup read URBs\n");
return err;
}
/* Warn if we've couldn't submit all the URBs */
if (i < MAX_RX_URBS)
netdev_warn(netdev, "rx performance may be slow\n");
return err;
}
static int toucan_usb_open(struct net_device *netdev)
{
struct toucan_usb_priv *priv = netdev_priv(netdev);
int err;
err = open_candev(netdev);
if (err)
return err;
err = toucan_usb_alloc_rx_urbs(priv);
if (err)
goto cleanup_candev;
err = toucan_usb_cmd_init(priv);
if (err)
goto cleanup_candev;
err = toucan_usb_cmd_start(priv);
if (err)
goto cleanup_candev;
priv->can.state = CAN_STATE_ERROR_ACTIVE;
netif_start_queue(netdev);
return 0;
cleanup_candev:
close_candev(netdev);
return err;
}
static int toucan_usb_close(struct net_device *netdev)
{
struct toucan_usb_priv *priv = netdev_priv(netdev);
int err = 0;
netif_stop_queue(netdev);
err = toucan_usb_cmd_stop(priv);
err |= toucan_usb_cmd_deinit(priv);
if (err)
netdev_warn(netdev, "couldn't stop device");
priv->can.state = CAN_STATE_STOPPED;
/* Stop polling */
toucan_usb_unlink_all_urbs(priv);
close_candev(netdev);
return err;
}
static int toucan_usb_get_berr_counter(const struct net_device *netdev,
struct can_berr_counter *bec)
{
struct toucan_usb_priv *priv = netdev_priv(netdev);
bec->txerr = priv->bec.txerr;
bec->rxerr = priv->bec.rxerr;
return 0;
}
static const struct net_device_ops toucan_usb_netdev_ops = {
.ndo_open = toucan_usb_open,
.ndo_stop = toucan_usb_close,
.ndo_start_xmit = toucan_usb_start_xmit,
.ndo_change_mtu = can_change_mtu,
};
static const struct can_bittiming_const toucan_bittiming_const = {
.name = "toucan",
.tseg1_min = 1,
.tseg1_max = 16,
.tseg2_min = 1,
.tseg2_max = 8,
.sjw_max = 4,
.brp_min = 1,
.brp_max = 1024,
.brp_inc = 1,
};
static const struct toucan_usb_device toucan = {
.ep_bulk_in = 1,
.ep_bulk_out = 1,
.bittiming_const = &toucan_bittiming_const,
.usb_rx_pkt_decode = toucan_rx_usb_pkt,
.usb_tx_pkt_encode = toucan_tx_usb_pkt,
.base_clock = 50000000,
};
static int toucan_usb_probe(struct usb_interface *intf,
const struct usb_device_id *id)
{
int i, err;
u8 *hw, *fw;
unsigned int hw_ver, fw_ver;
struct net_device *netdev;
struct toucan_usb_priv *priv;
const struct toucan_usb_device *adapter;
struct usb_device *usbdev = interface_to_usbdev(intf);
switch (id->idProduct) {
case USB_RUSOKU_TOUCAN_PRODUCT_ID:
adapter = &toucan;
break;
default:
dev_err(&intf->dev, "Unknown device id:0x%x\n", id->idProduct);
break;
}
netdev = alloc_candev(sizeof(struct toucan_usb_priv), MAX_TX_URBS);
if (!netdev) {
dev_err(&intf->dev, "Can't alloc candev\n");
return -ENOMEM;
}
priv = netdev_priv(netdev);
priv->udev = usbdev;
priv->netdev = netdev;
priv->can.state = CAN_STATE_STOPPED;
priv->can.clock.freq = adapter->base_clock;
priv->can.bittiming_const = adapter->bittiming_const;
priv->can.do_set_mode = toucan_usb_set_mode;
priv->can.do_get_berr_counter = toucan_usb_get_berr_counter;
priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
CAN_CTRLMODE_LISTENONLY |
CAN_CTRLMODE_ONE_SHOT;
priv->adapter = adapter;
priv->ep_ctrl_in = usb_rcvctrlpipe(priv->udev, 0);
priv->ep_ctrl_out = usb_sndctrlpipe(priv->udev, 0);
priv->ep_bulk_in = usb_rcvbulkpipe(priv->udev, adapter->ep_bulk_in);
priv->ep_bulk_out = usb_sndbulkpipe(priv->udev, adapter->ep_bulk_out);
netdev->flags |= IFF_ECHO;
netdev->netdev_ops = &toucan_usb_netdev_ops;
init_usb_anchor(&priv->rx_submitted);
init_usb_anchor(&priv->tx_submitted);
atomic_set(&priv->active_tx_urbs, 0);
for (i = 0; i < MAX_TX_URBS; i++)
priv->tx_contexts[i].echo_index = MAX_TX_URBS;
usb_set_intfdata(intf, priv);
SET_NETDEV_DEV(netdev, &intf->dev);
mutex_init(&priv->toucan_cmd_lock);
err = register_candev(netdev);
if (err) {
dev_err(&intf->dev, "Failed to register CAN device\n");
goto cleanup_candev;
}
err = toucan_usb_get_hw_version(priv, &hw_ver);
if (err) {
netdev_err(netdev, "can't get hardware version\n");
goto cleanup_unregister_candev;
}
err = toucan_usb_get_fw_version(priv, &fw_ver);
if (err) {
netdev_err(netdev, "can't get firmware version\n");
goto cleanup_unregister_candev;
}
hw = (u8 *)(&hw_ver);
fw = (u8 *)(&fw_ver);
netdev_info(netdev, "registered. HW ver: %d.%d.%d, FW ver: %d.%d.%d\n",
hw[3], hw[2], hw[1], fw[3], fw[2], fw[1]);
return 0;
cleanup_unregister_candev:
unregister_netdev(priv->netdev);
cleanup_candev:
free_candev(netdev);
return err;
}
static void toucan_usb_disconnect(struct usb_interface *intf)
{
struct toucan_usb_priv *priv = usb_get_intfdata(intf);
usb_set_intfdata(intf, NULL);
if (priv) {
netdev_info(priv->netdev, "device disconnected\n");
unregister_netdev(priv->netdev);
toucan_usb_unlink_all_urbs(priv);
free_candev(priv->netdev);
}
}
static struct usb_driver toucan_usb_driver = {
.name = "toucan",
.probe = toucan_usb_probe,
.disconnect = toucan_usb_disconnect,
.id_table = toucan_usb_table,
};
module_usb_driver(toucan_usb_driver);
MODULE_DESCRIPTION("CAN driver for Rusoku Technologies TouCAN adapters");
MODULE_LICENSE("GPL");