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#include <linux/can.h>
29#include <linux/can/dev.h>
30#include <linux/can/error.h>
31#include <linux/module.h>
32#include <linux/netdevice.h>
33#include <linux/signal.h>
34#include <linux/skbuff.h>
35#include <linux/slab.h>
36#include <linux/usb.h>
37
38#define UCAN_DRIVER_NAME "ucan"
39#define UCAN_MAX_RX_URBS 8
40
41#define UCAN_USB_CTL_PIPE_TIMEOUT 1000
42
43#define UCAN_PROTOCOL_VERSION_MIN 3
44#define UCAN_PROTOCOL_VERSION_MAX 3
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
72enum {
73 UCAN_DEVICE_GET_FW_STRING = 0,
74};
75
76
77enum {
78
79 UCAN_COMMAND_START = 0,
80
81 UCAN_COMMAND_STOP = 1,
82
83 UCAN_COMMAND_SLEEP = 2,
84
85 UCAN_COMMAND_WAKEUP = 3,
86
87 UCAN_COMMAND_RESET = 4,
88
89
90
91 UCAN_COMMAND_GET = 5,
92
93 UCAN_COMMAND_FILTER = 6,
94
95 UCAN_COMMAND_SET_BITTIMING = 7,
96
97 UCAN_COMMAND_RESTART = 8,
98};
99
100
101
102
103enum {
104 UCAN_MODE_LOOPBACK = BIT(0),
105 UCAN_MODE_SILENT = BIT(1),
106 UCAN_MODE_3_SAMPLES = BIT(2),
107 UCAN_MODE_ONE_SHOT = BIT(3),
108 UCAN_MODE_BERR_REPORT = BIT(4),
109};
110
111
112enum {
113 UCAN_COMMAND_GET_INFO = 0,
114 UCAN_COMMAND_GET_PROTOCOL_VERSION = 1,
115};
116
117
118enum {
119 UCAN_FILTER_CLEAR = 0,
120 UCAN_FILTER_DISABLE = 1,
121 UCAN_FILTER_ENABLE = 2,
122};
123
124
125enum {
126 UCAN_OUT_TX = 2,
127};
128
129
130enum {
131 UCAN_IN_TX_COMPLETE = 1,
132 UCAN_IN_RX = 2,
133};
134
135struct ucan_ctl_cmd_start {
136 __le16 mode;
137} __packed;
138
139struct ucan_ctl_cmd_set_bittiming {
140 __le32 tq;
141 __le16 brp;
142 __le16 sample_point;
143 u8 prop_seg;
144 u8 phase_seg1;
145 u8 phase_seg2;
146 u8 sjw;
147} __packed;
148
149struct ucan_ctl_cmd_device_info {
150 __le32 freq;
151 u8 tx_fifo;
152 u8 sjw_max;
153 u8 tseg1_min;
154 u8 tseg1_max;
155 u8 tseg2_min;
156 u8 tseg2_max;
157 __le16 brp_inc;
158 __le32 brp_min;
159 __le32 brp_max;
160 __le16 ctrlmodes;
161 __le16 hwfilter;
162 __le16 rxmboxes;
163} __packed;
164
165struct ucan_ctl_cmd_get_protocol_version {
166 __le32 version;
167} __packed;
168
169union ucan_ctl_payload {
170
171
172
173 struct ucan_ctl_cmd_start cmd_start;
174
175
176
177 struct ucan_ctl_cmd_set_bittiming cmd_set_bittiming;
178
179
180
181 struct ucan_ctl_cmd_device_info cmd_get_device_info;
182
183
184
185
186 struct ucan_ctl_cmd_get_protocol_version cmd_get_protocol_version;
187
188 u8 raw[128];
189} __packed;
190
191enum {
192 UCAN_TX_COMPLETE_SUCCESS = BIT(0),
193};
194
195
196struct ucan_tx_complete_entry_t {
197 u8 echo_index;
198 u8 flags;
199} __packed __aligned(0x2);
200
201
202struct ucan_can_msg {
203
204
205
206
207
208 __le32 id;
209
210 union {
211 u8 data[CAN_MAX_DLEN];
212 u8 dlc;
213 };
214} __packed;
215
216
217struct ucan_message_out {
218 __le16 len;
219 u8 type;
220 u8 subtype;
221
222 union {
223
224
225
226
227 struct ucan_can_msg can_msg;
228 } msg;
229} __packed __aligned(0x4);
230
231
232struct ucan_message_in {
233 __le16 len;
234 u8 type;
235 u8 subtype;
236
237 union {
238
239
240
241
242 struct ucan_can_msg can_msg;
243
244
245
246
247 struct ucan_tx_complete_entry_t can_tx_complete_msg[0];
248 } __aligned(0x4) msg;
249} __packed __aligned(0x4);
250
251
252#define UCAN_OUT_HDR_SIZE offsetof(struct ucan_message_out, msg)
253
254#define UCAN_IN_HDR_SIZE offsetof(struct ucan_message_in, msg)
255#define UCAN_IN_LEN(member) (UCAN_OUT_HDR_SIZE + sizeof(member))
256
257struct ucan_priv;
258
259
260struct ucan_urb_context {
261 struct ucan_priv *up;
262 bool allocated;
263};
264
265
266struct ucan_device_info {
267 struct can_bittiming_const bittiming_const;
268 u8 tx_fifo;
269};
270
271
272struct ucan_priv {
273
274 struct can_priv can;
275
276
277 struct usb_device *udev;
278 struct usb_interface *intf;
279 struct net_device *netdev;
280
281
282
283
284 spinlock_t echo_skb_lock;
285
286
287 u8 intf_index;
288 u8 in_ep_addr;
289 u8 out_ep_addr;
290 u16 in_ep_size;
291
292
293 struct usb_anchor rx_urbs;
294 struct usb_anchor tx_urbs;
295
296 union ucan_ctl_payload *ctl_msg_buffer;
297 struct ucan_device_info device_info;
298
299
300 spinlock_t context_lock;
301 unsigned int available_tx_urbs;
302 struct ucan_urb_context *context_array;
303};
304
305static u8 ucan_can_cc_dlc2len(struct ucan_can_msg *msg, u16 len)
306{
307 if (le32_to_cpu(msg->id) & CAN_RTR_FLAG)
308 return can_cc_dlc2len(msg->dlc);
309 else
310 return can_cc_dlc2len(len - (UCAN_IN_HDR_SIZE + sizeof(msg->id)));
311}
312
313static void ucan_release_context_array(struct ucan_priv *up)
314{
315 if (!up->context_array)
316 return;
317
318
319 up->available_tx_urbs = 0;
320
321 kfree(up->context_array);
322 up->context_array = NULL;
323}
324
325static int ucan_alloc_context_array(struct ucan_priv *up)
326{
327 int i;
328
329
330 ucan_release_context_array(up);
331
332 up->context_array = kcalloc(up->device_info.tx_fifo,
333 sizeof(*up->context_array),
334 GFP_KERNEL);
335 if (!up->context_array) {
336 netdev_err(up->netdev,
337 "Not enough memory to allocate tx contexts\n");
338 return -ENOMEM;
339 }
340
341 for (i = 0; i < up->device_info.tx_fifo; i++) {
342 up->context_array[i].allocated = false;
343 up->context_array[i].up = up;
344 }
345
346
347 up->available_tx_urbs = up->device_info.tx_fifo;
348
349 return 0;
350}
351
352static struct ucan_urb_context *ucan_alloc_context(struct ucan_priv *up)
353{
354 int i;
355 unsigned long flags;
356 struct ucan_urb_context *ret = NULL;
357
358 if (WARN_ON_ONCE(!up->context_array))
359 return NULL;
360
361
362 spin_lock_irqsave(&up->context_lock, flags);
363
364 for (i = 0; i < up->device_info.tx_fifo; i++) {
365 if (!up->context_array[i].allocated) {
366
367 ret = &up->context_array[i];
368 up->context_array[i].allocated = true;
369
370
371 up->available_tx_urbs--;
372 if (!up->available_tx_urbs)
373 netif_stop_queue(up->netdev);
374
375 break;
376 }
377 }
378
379 spin_unlock_irqrestore(&up->context_lock, flags);
380 return ret;
381}
382
383static bool ucan_release_context(struct ucan_priv *up,
384 struct ucan_urb_context *ctx)
385{
386 unsigned long flags;
387 bool ret = false;
388
389 if (WARN_ON_ONCE(!up->context_array))
390 return false;
391
392
393 spin_lock_irqsave(&up->context_lock, flags);
394
395
396 if (ctx->allocated) {
397 ctx->allocated = false;
398
399
400 if (!up->available_tx_urbs)
401 netif_wake_queue(up->netdev);
402 up->available_tx_urbs++;
403
404 ret = true;
405 }
406
407 spin_unlock_irqrestore(&up->context_lock, flags);
408 return ret;
409}
410
411static int ucan_ctrl_command_out(struct ucan_priv *up,
412 u8 cmd, u16 subcmd, u16 datalen)
413{
414 return usb_control_msg(up->udev,
415 usb_sndctrlpipe(up->udev, 0),
416 cmd,
417 USB_DIR_OUT | USB_TYPE_VENDOR |
418 USB_RECIP_INTERFACE,
419 subcmd,
420 up->intf_index,
421 up->ctl_msg_buffer,
422 datalen,
423 UCAN_USB_CTL_PIPE_TIMEOUT);
424}
425
426static int ucan_device_request_in(struct ucan_priv *up,
427 u8 cmd, u16 subcmd, u16 datalen)
428{
429 return usb_control_msg(up->udev,
430 usb_rcvctrlpipe(up->udev, 0),
431 cmd,
432 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
433 subcmd,
434 0,
435 up->ctl_msg_buffer,
436 datalen,
437 UCAN_USB_CTL_PIPE_TIMEOUT);
438}
439
440
441
442
443static void ucan_parse_device_info(struct ucan_priv *up,
444 struct ucan_ctl_cmd_device_info *device_info)
445{
446 struct can_bittiming_const *bittiming =
447 &up->device_info.bittiming_const;
448 u16 ctrlmodes;
449
450
451 up->can.clock.freq = le32_to_cpu(device_info->freq);
452 up->device_info.tx_fifo = device_info->tx_fifo;
453 strcpy(bittiming->name, "ucan");
454 bittiming->tseg1_min = device_info->tseg1_min;
455 bittiming->tseg1_max = device_info->tseg1_max;
456 bittiming->tseg2_min = device_info->tseg2_min;
457 bittiming->tseg2_max = device_info->tseg2_max;
458 bittiming->sjw_max = device_info->sjw_max;
459 bittiming->brp_min = le32_to_cpu(device_info->brp_min);
460 bittiming->brp_max = le32_to_cpu(device_info->brp_max);
461 bittiming->brp_inc = le16_to_cpu(device_info->brp_inc);
462
463 ctrlmodes = le16_to_cpu(device_info->ctrlmodes);
464
465 up->can.ctrlmode_supported = 0;
466
467 if (ctrlmodes & UCAN_MODE_LOOPBACK)
468 up->can.ctrlmode_supported |= CAN_CTRLMODE_LOOPBACK;
469 if (ctrlmodes & UCAN_MODE_SILENT)
470 up->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY;
471 if (ctrlmodes & UCAN_MODE_3_SAMPLES)
472 up->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
473 if (ctrlmodes & UCAN_MODE_ONE_SHOT)
474 up->can.ctrlmode_supported |= CAN_CTRLMODE_ONE_SHOT;
475 if (ctrlmodes & UCAN_MODE_BERR_REPORT)
476 up->can.ctrlmode_supported |= CAN_CTRLMODE_BERR_REPORTING;
477}
478
479
480
481
482static bool ucan_handle_error_frame(struct ucan_priv *up,
483 struct ucan_message_in *m,
484 canid_t canid)
485{
486 enum can_state new_state = up->can.state;
487 struct net_device_stats *net_stats = &up->netdev->stats;
488 struct can_device_stats *can_stats = &up->can.can_stats;
489
490 if (canid & CAN_ERR_LOSTARB)
491 can_stats->arbitration_lost++;
492
493 if (canid & CAN_ERR_BUSERROR)
494 can_stats->bus_error++;
495
496 if (canid & CAN_ERR_ACK)
497 net_stats->tx_errors++;
498
499 if (canid & CAN_ERR_BUSOFF)
500 new_state = CAN_STATE_BUS_OFF;
501
502
503 if (canid & CAN_ERR_CRTL) {
504 u8 d1 = m->msg.can_msg.data[1];
505
506 if (d1 & CAN_ERR_CRTL_RX_OVERFLOW)
507 net_stats->rx_over_errors++;
508
509
510 if (d1 & CAN_ERR_CRTL_ACTIVE)
511 new_state = CAN_STATE_ERROR_ACTIVE;
512
513 if (d1 & (CAN_ERR_CRTL_RX_WARNING | CAN_ERR_CRTL_TX_WARNING))
514 new_state = CAN_STATE_ERROR_WARNING;
515
516 if (d1 & (CAN_ERR_CRTL_RX_PASSIVE | CAN_ERR_CRTL_TX_PASSIVE))
517 new_state = CAN_STATE_ERROR_PASSIVE;
518 }
519
520
521 if (canid & CAN_ERR_PROT) {
522 u8 d2 = m->msg.can_msg.data[2];
523
524 if (d2 & CAN_ERR_PROT_TX)
525 net_stats->tx_errors++;
526 else
527 net_stats->rx_errors++;
528 }
529
530
531 if (up->can.state == new_state)
532 return false;
533
534
535 if (up->can.state > new_state) {
536 up->can.state = new_state;
537 return true;
538 }
539
540
541 up->can.state = new_state;
542 switch (new_state) {
543 case CAN_STATE_BUS_OFF:
544 can_stats->bus_off++;
545 can_bus_off(up->netdev);
546 break;
547 case CAN_STATE_ERROR_PASSIVE:
548 can_stats->error_passive++;
549 break;
550 case CAN_STATE_ERROR_WARNING:
551 can_stats->error_warning++;
552 break;
553 default:
554 break;
555 }
556 return true;
557}
558
559
560
561
562
563
564static void ucan_rx_can_msg(struct ucan_priv *up, struct ucan_message_in *m)
565{
566 int len;
567 canid_t canid;
568 struct can_frame *cf;
569 struct sk_buff *skb;
570 struct net_device_stats *stats = &up->netdev->stats;
571
572
573 len = le16_to_cpu(m->len);
574
575
576 if (len < UCAN_IN_HDR_SIZE + sizeof(m->msg.can_msg.id)) {
577 netdev_warn(up->netdev, "invalid input message len: %d\n", len);
578 return;
579 }
580
581
582 canid = le32_to_cpu(m->msg.can_msg.id);
583 if (canid & CAN_ERR_FLAG) {
584 bool busstate_changed = ucan_handle_error_frame(up, m, canid);
585
586
587 if (!(up->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) &&
588 !busstate_changed)
589 return;
590 } else {
591 canid_t canid_mask;
592
593 canid_mask = CAN_RTR_FLAG;
594 if (canid & CAN_EFF_FLAG)
595 canid_mask |= CAN_EFF_MASK | CAN_EFF_FLAG;
596 else
597 canid_mask |= CAN_SFF_MASK;
598
599 if (canid & ~canid_mask)
600 netdev_warn(up->netdev,
601 "unexpected bits set (canid %x, mask %x)",
602 canid, canid_mask);
603
604 canid &= canid_mask;
605 }
606
607
608 skb = alloc_can_skb(up->netdev, &cf);
609 if (!skb)
610 return;
611
612
613 cf->can_id = canid;
614
615
616 cf->len = ucan_can_cc_dlc2len(&m->msg.can_msg, len);
617
618
619 if (!(cf->can_id & CAN_RTR_FLAG) || (cf->can_id & CAN_ERR_FLAG))
620 memcpy(cf->data, m->msg.can_msg.data, cf->len);
621
622
623 if (!(cf->can_id & CAN_ERR_FLAG)) {
624 stats->rx_packets++;
625 if (!(cf->can_id & CAN_RTR_FLAG))
626 stats->rx_bytes += cf->len;
627 }
628
629
630 netif_rx(skb);
631}
632
633
634static void ucan_tx_complete_msg(struct ucan_priv *up,
635 struct ucan_message_in *m)
636{
637 unsigned long flags;
638 u16 count, i;
639 u8 echo_index;
640 u16 len = le16_to_cpu(m->len);
641
642 struct ucan_urb_context *context;
643
644 if (len < UCAN_IN_HDR_SIZE || (len % 2 != 0)) {
645 netdev_err(up->netdev, "invalid tx complete length\n");
646 return;
647 }
648
649 count = (len - UCAN_IN_HDR_SIZE) / 2;
650 for (i = 0; i < count; i++) {
651
652 echo_index = m->msg.can_tx_complete_msg[i].echo_index;
653 if (echo_index >= up->device_info.tx_fifo) {
654 up->netdev->stats.tx_errors++;
655 netdev_err(up->netdev,
656 "invalid echo_index %d received\n",
657 echo_index);
658 continue;
659 }
660
661
662 context = &up->context_array[echo_index];
663
664
665
666
667 if (!ucan_release_context(up, context))
668 continue;
669
670 spin_lock_irqsave(&up->echo_skb_lock, flags);
671 if (m->msg.can_tx_complete_msg[i].flags &
672 UCAN_TX_COMPLETE_SUCCESS) {
673
674 up->netdev->stats.tx_packets++;
675 up->netdev->stats.tx_bytes +=
676 can_get_echo_skb(up->netdev, echo_index, NULL);
677 } else {
678 up->netdev->stats.tx_dropped++;
679 can_free_echo_skb(up->netdev, echo_index, NULL);
680 }
681 spin_unlock_irqrestore(&up->echo_skb_lock, flags);
682 }
683}
684
685
686static void ucan_read_bulk_callback(struct urb *urb)
687{
688 int ret;
689 int pos;
690 struct ucan_priv *up = urb->context;
691 struct net_device *netdev = up->netdev;
692 struct ucan_message_in *m;
693
694
695
696
697 if (WARN_ON(!up->context_array)) {
698 usb_free_coherent(up->udev,
699 up->in_ep_size,
700 urb->transfer_buffer,
701 urb->transfer_dma);
702 return;
703 }
704
705
706 switch (urb->status) {
707 case 0:
708 break;
709 case -ENOENT:
710 case -EPIPE:
711 case -EPROTO:
712 case -ESHUTDOWN:
713 case -ETIME:
714
715 usb_free_coherent(up->udev,
716 up->in_ep_size,
717 urb->transfer_buffer,
718 urb->transfer_dma);
719 netdev_dbg(up->netdev, "not resubmitting urb; status: %d\n",
720 urb->status);
721 return;
722 default:
723 goto resubmit;
724 }
725
726
727 if (!netif_device_present(netdev))
728 return;
729
730
731 pos = 0;
732 while (pos < urb->actual_length) {
733 int len;
734
735
736 if ((urb->actual_length - pos) < UCAN_IN_HDR_SIZE) {
737 netdev_warn(up->netdev,
738 "invalid message (short; no hdr; l:%d)\n",
739 urb->actual_length);
740 goto resubmit;
741 }
742
743
744 m = (struct ucan_message_in *)
745 ((u8 *)urb->transfer_buffer + pos);
746 len = le16_to_cpu(m->len);
747
748
749 if (urb->actual_length - pos < len) {
750 netdev_warn(up->netdev,
751 "invalid message (short; no data; l:%d)\n",
752 urb->actual_length);
753 print_hex_dump(KERN_WARNING,
754 "raw data: ",
755 DUMP_PREFIX_ADDRESS,
756 16,
757 1,
758 urb->transfer_buffer,
759 urb->actual_length,
760 true);
761
762 goto resubmit;
763 }
764
765 switch (m->type) {
766 case UCAN_IN_RX:
767 ucan_rx_can_msg(up, m);
768 break;
769 case UCAN_IN_TX_COMPLETE:
770 ucan_tx_complete_msg(up, m);
771 break;
772 default:
773 netdev_warn(up->netdev,
774 "invalid message (type; t:%d)\n",
775 m->type);
776 break;
777 }
778
779
780 pos += len;
781
782 pos = round_up(pos, 4);
783 }
784
785resubmit:
786
787 usb_fill_bulk_urb(urb, up->udev,
788 usb_rcvbulkpipe(up->udev,
789 up->in_ep_addr),
790 urb->transfer_buffer,
791 up->in_ep_size,
792 ucan_read_bulk_callback,
793 up);
794
795 usb_anchor_urb(urb, &up->rx_urbs);
796 ret = usb_submit_urb(urb, GFP_ATOMIC);
797
798 if (ret < 0) {
799 netdev_err(up->netdev,
800 "failed resubmitting read bulk urb: %d\n",
801 ret);
802
803 usb_unanchor_urb(urb);
804 usb_free_coherent(up->udev,
805 up->in_ep_size,
806 urb->transfer_buffer,
807 urb->transfer_dma);
808
809 if (ret == -ENODEV)
810 netif_device_detach(netdev);
811 }
812}
813
814
815static void ucan_write_bulk_callback(struct urb *urb)
816{
817 unsigned long flags;
818 struct ucan_priv *up;
819 struct ucan_urb_context *context = urb->context;
820
821
822 if (WARN_ON_ONCE(!context))
823 return;
824
825
826 usb_free_coherent(urb->dev,
827 sizeof(struct ucan_message_out),
828 urb->transfer_buffer,
829 urb->transfer_dma);
830
831 up = context->up;
832 if (WARN_ON_ONCE(!up))
833 return;
834
835
836 if (!netif_device_present(up->netdev))
837 return;
838
839
840 if (urb->status) {
841 netdev_warn(up->netdev,
842 "failed to transmit USB message to device: %d\n",
843 urb->status);
844
845
846 spin_lock_irqsave(&up->echo_skb_lock, flags);
847 can_free_echo_skb(up->netdev, context - up->context_array, NULL);
848 spin_unlock_irqrestore(&up->echo_skb_lock, flags);
849
850 up->netdev->stats.tx_dropped++;
851
852
853 if (!ucan_release_context(up, context))
854 netdev_err(up->netdev,
855 "urb failed, failed to release context\n");
856 }
857}
858
859static void ucan_cleanup_rx_urbs(struct ucan_priv *up, struct urb **urbs)
860{
861 int i;
862
863 for (i = 0; i < UCAN_MAX_RX_URBS; i++) {
864 if (urbs[i]) {
865 usb_unanchor_urb(urbs[i]);
866 usb_free_coherent(up->udev,
867 up->in_ep_size,
868 urbs[i]->transfer_buffer,
869 urbs[i]->transfer_dma);
870 usb_free_urb(urbs[i]);
871 }
872 }
873
874 memset(urbs, 0, sizeof(*urbs) * UCAN_MAX_RX_URBS);
875}
876
877static int ucan_prepare_and_anchor_rx_urbs(struct ucan_priv *up,
878 struct urb **urbs)
879{
880 int i;
881
882 memset(urbs, 0, sizeof(*urbs) * UCAN_MAX_RX_URBS);
883
884 for (i = 0; i < UCAN_MAX_RX_URBS; i++) {
885 void *buf;
886
887 urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
888 if (!urbs[i])
889 goto err;
890
891 buf = usb_alloc_coherent(up->udev,
892 up->in_ep_size,
893 GFP_KERNEL, &urbs[i]->transfer_dma);
894 if (!buf) {
895
896 usb_free_urb(urbs[i]);
897 urbs[i] = NULL;
898 goto err;
899 }
900
901 usb_fill_bulk_urb(urbs[i], up->udev,
902 usb_rcvbulkpipe(up->udev,
903 up->in_ep_addr),
904 buf,
905 up->in_ep_size,
906 ucan_read_bulk_callback,
907 up);
908
909 urbs[i]->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
910
911 usb_anchor_urb(urbs[i], &up->rx_urbs);
912 }
913 return 0;
914
915err:
916
917 ucan_cleanup_rx_urbs(up, urbs);
918 return -ENOMEM;
919}
920
921
922
923
924
925
926static int ucan_submit_rx_urbs(struct ucan_priv *up, struct urb **urbs)
927{
928 int i, ret;
929
930
931
932
933 for (i = 0; i < UCAN_MAX_RX_URBS; i++) {
934 ret = usb_submit_urb(urbs[i], GFP_KERNEL);
935 if (ret) {
936 netdev_err(up->netdev,
937 "could not submit urb; code: %d\n",
938 ret);
939 goto err;
940 }
941
942
943
944
945 usb_free_urb(urbs[i]);
946 urbs[i] = NULL;
947 }
948 return 0;
949
950err:
951
952 ucan_cleanup_rx_urbs(up, urbs);
953
954
955 usb_kill_anchored_urbs(&up->rx_urbs);
956
957 return ret;
958}
959
960
961static int ucan_open(struct net_device *netdev)
962{
963 int ret, ret_cleanup;
964 u16 ctrlmode;
965 struct urb *urbs[UCAN_MAX_RX_URBS];
966 struct ucan_priv *up = netdev_priv(netdev);
967
968 ret = ucan_alloc_context_array(up);
969 if (ret)
970 return ret;
971
972
973
974
975 ret = ucan_prepare_and_anchor_rx_urbs(up, urbs);
976 if (ret)
977 goto err_contexts;
978
979
980 ctrlmode = 0;
981 if (up->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
982 ctrlmode |= UCAN_MODE_LOOPBACK;
983 if (up->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
984 ctrlmode |= UCAN_MODE_SILENT;
985 if (up->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
986 ctrlmode |= UCAN_MODE_3_SAMPLES;
987 if (up->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT)
988 ctrlmode |= UCAN_MODE_ONE_SHOT;
989
990
991
992
993 ctrlmode |= UCAN_MODE_BERR_REPORT;
994 up->ctl_msg_buffer->cmd_start.mode = cpu_to_le16(ctrlmode);
995
996
997 ret = ucan_ctrl_command_out(up, UCAN_COMMAND_START, 0, 2);
998 if (ret < 0) {
999 netdev_err(up->netdev,
1000 "could not start device, code: %d\n",
1001 ret);
1002 goto err_reset;
1003 }
1004
1005
1006 ret = open_candev(netdev);
1007 if (ret)
1008 goto err_stop;
1009
1010
1011 ret = ucan_submit_rx_urbs(up, urbs);
1012 if (ret)
1013 goto err_stop;
1014
1015 up->can.state = CAN_STATE_ERROR_ACTIVE;
1016
1017
1018 netif_start_queue(netdev);
1019
1020 return 0;
1021
1022err_stop:
1023
1024 ret_cleanup = ucan_ctrl_command_out(up, UCAN_COMMAND_STOP, 0, 0);
1025 if (ret_cleanup < 0)
1026 netdev_err(up->netdev,
1027 "could not stop device, code: %d\n",
1028 ret_cleanup);
1029
1030err_reset:
1031
1032
1033
1034 ret_cleanup = ucan_ctrl_command_out(up, UCAN_COMMAND_RESET, 0, 0);
1035 if (ret_cleanup < 0)
1036 netdev_err(up->netdev,
1037 "could not reset device, code: %d\n",
1038 ret_cleanup);
1039
1040
1041 ucan_cleanup_rx_urbs(up, urbs);
1042
1043err_contexts:
1044 ucan_release_context_array(up);
1045 return ret;
1046}
1047
1048static struct urb *ucan_prepare_tx_urb(struct ucan_priv *up,
1049 struct ucan_urb_context *context,
1050 struct can_frame *cf,
1051 u8 echo_index)
1052{
1053 int mlen;
1054 struct urb *urb;
1055 struct ucan_message_out *m;
1056
1057
1058 urb = usb_alloc_urb(0, GFP_ATOMIC);
1059 if (!urb) {
1060 netdev_err(up->netdev, "no memory left for URBs\n");
1061 return NULL;
1062 }
1063
1064 m = usb_alloc_coherent(up->udev,
1065 sizeof(struct ucan_message_out),
1066 GFP_ATOMIC,
1067 &urb->transfer_dma);
1068 if (!m) {
1069 netdev_err(up->netdev, "no memory left for USB buffer\n");
1070 usb_free_urb(urb);
1071 return NULL;
1072 }
1073
1074
1075 m->type = UCAN_OUT_TX;
1076 m->msg.can_msg.id = cpu_to_le32(cf->can_id);
1077
1078 if (cf->can_id & CAN_RTR_FLAG) {
1079 mlen = UCAN_OUT_HDR_SIZE +
1080 offsetof(struct ucan_can_msg, dlc) +
1081 sizeof(m->msg.can_msg.dlc);
1082 m->msg.can_msg.dlc = cf->len;
1083 } else {
1084 mlen = UCAN_OUT_HDR_SIZE +
1085 sizeof(m->msg.can_msg.id) + cf->len;
1086 memcpy(m->msg.can_msg.data, cf->data, cf->len);
1087 }
1088 m->len = cpu_to_le16(mlen);
1089
1090 m->subtype = echo_index;
1091
1092
1093 usb_fill_bulk_urb(urb, up->udev,
1094 usb_sndbulkpipe(up->udev,
1095 up->out_ep_addr),
1096 m, mlen, ucan_write_bulk_callback, context);
1097 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1098
1099 return urb;
1100}
1101
1102static void ucan_clean_up_tx_urb(struct ucan_priv *up, struct urb *urb)
1103{
1104 usb_free_coherent(up->udev, sizeof(struct ucan_message_out),
1105 urb->transfer_buffer, urb->transfer_dma);
1106 usb_free_urb(urb);
1107}
1108
1109
1110static netdev_tx_t ucan_start_xmit(struct sk_buff *skb,
1111 struct net_device *netdev)
1112{
1113 unsigned long flags;
1114 int ret;
1115 u8 echo_index;
1116 struct urb *urb;
1117 struct ucan_urb_context *context;
1118 struct ucan_priv *up = netdev_priv(netdev);
1119 struct can_frame *cf = (struct can_frame *)skb->data;
1120
1121
1122 if (can_dropped_invalid_skb(netdev, skb))
1123 return NETDEV_TX_OK;
1124
1125
1126 context = ucan_alloc_context(up);
1127 echo_index = context - up->context_array;
1128
1129 if (WARN_ON_ONCE(!context))
1130 return NETDEV_TX_BUSY;
1131
1132
1133 urb = ucan_prepare_tx_urb(up, context, cf, echo_index);
1134 if (!urb)
1135 goto drop;
1136
1137
1138 spin_lock_irqsave(&up->echo_skb_lock, flags);
1139 can_put_echo_skb(skb, up->netdev, echo_index, 0);
1140 spin_unlock_irqrestore(&up->echo_skb_lock, flags);
1141
1142
1143 usb_anchor_urb(urb, &up->tx_urbs);
1144 ret = usb_submit_urb(urb, GFP_ATOMIC);
1145
1146
1147 if (ret) {
1148
1149 usb_unanchor_urb(urb);
1150 ucan_clean_up_tx_urb(up, urb);
1151 if (!ucan_release_context(up, context))
1152 netdev_err(up->netdev,
1153 "xmit err: failed to release context\n");
1154
1155
1156
1157
1158 spin_lock_irqsave(&up->echo_skb_lock, flags);
1159 can_free_echo_skb(up->netdev, echo_index, NULL);
1160 spin_unlock_irqrestore(&up->echo_skb_lock, flags);
1161
1162 if (ret == -ENODEV) {
1163 netif_device_detach(up->netdev);
1164 } else {
1165 netdev_warn(up->netdev,
1166 "xmit err: failed to submit urb %d\n",
1167 ret);
1168 up->netdev->stats.tx_dropped++;
1169 }
1170 return NETDEV_TX_OK;
1171 }
1172
1173 netif_trans_update(netdev);
1174
1175
1176 usb_free_urb(urb);
1177
1178 return NETDEV_TX_OK;
1179
1180drop:
1181 if (!ucan_release_context(up, context))
1182 netdev_err(up->netdev,
1183 "xmit drop: failed to release context\n");
1184 dev_kfree_skb(skb);
1185 up->netdev->stats.tx_dropped++;
1186
1187 return NETDEV_TX_OK;
1188}
1189
1190
1191
1192
1193
1194static int ucan_close(struct net_device *netdev)
1195{
1196 int ret;
1197 struct ucan_priv *up = netdev_priv(netdev);
1198
1199 up->can.state = CAN_STATE_STOPPED;
1200
1201
1202 usb_kill_anchored_urbs(&up->tx_urbs);
1203
1204
1205 usb_kill_anchored_urbs(&up->rx_urbs);
1206
1207
1208 ret = ucan_ctrl_command_out(up, UCAN_COMMAND_STOP, 0, 0);
1209 if (ret < 0)
1210 netdev_err(up->netdev,
1211 "could not stop device, code: %d\n",
1212 ret);
1213
1214 ret = ucan_ctrl_command_out(up, UCAN_COMMAND_RESET, 0, 0);
1215 if (ret < 0)
1216 netdev_err(up->netdev,
1217 "could not reset device, code: %d\n",
1218 ret);
1219
1220 netif_stop_queue(netdev);
1221
1222 ucan_release_context_array(up);
1223
1224 close_candev(up->netdev);
1225 return 0;
1226}
1227
1228
1229static const struct net_device_ops ucan_netdev_ops = {
1230 .ndo_open = ucan_open,
1231 .ndo_stop = ucan_close,
1232 .ndo_start_xmit = ucan_start_xmit,
1233 .ndo_change_mtu = can_change_mtu,
1234};
1235
1236
1237
1238
1239
1240
1241static int ucan_set_bittiming(struct net_device *netdev)
1242{
1243 int ret;
1244 struct ucan_priv *up = netdev_priv(netdev);
1245 struct ucan_ctl_cmd_set_bittiming *cmd_set_bittiming;
1246
1247 cmd_set_bittiming = &up->ctl_msg_buffer->cmd_set_bittiming;
1248 cmd_set_bittiming->tq = cpu_to_le32(up->can.bittiming.tq);
1249 cmd_set_bittiming->brp = cpu_to_le16(up->can.bittiming.brp);
1250 cmd_set_bittiming->sample_point =
1251 cpu_to_le16(up->can.bittiming.sample_point);
1252 cmd_set_bittiming->prop_seg = up->can.bittiming.prop_seg;
1253 cmd_set_bittiming->phase_seg1 = up->can.bittiming.phase_seg1;
1254 cmd_set_bittiming->phase_seg2 = up->can.bittiming.phase_seg2;
1255 cmd_set_bittiming->sjw = up->can.bittiming.sjw;
1256
1257 ret = ucan_ctrl_command_out(up, UCAN_COMMAND_SET_BITTIMING, 0,
1258 sizeof(*cmd_set_bittiming));
1259 return (ret < 0) ? ret : 0;
1260}
1261
1262
1263
1264
1265static int ucan_set_mode(struct net_device *netdev, enum can_mode mode)
1266{
1267 int ret;
1268 unsigned long flags;
1269 struct ucan_priv *up = netdev_priv(netdev);
1270
1271 switch (mode) {
1272 case CAN_MODE_START:
1273 netdev_dbg(up->netdev, "restarting device\n");
1274
1275 ret = ucan_ctrl_command_out(up, UCAN_COMMAND_RESTART, 0, 0);
1276 up->can.state = CAN_STATE_ERROR_ACTIVE;
1277
1278
1279
1280
1281
1282 spin_lock_irqsave(&up->context_lock, flags);
1283
1284 if (up->available_tx_urbs > 0)
1285 netif_wake_queue(up->netdev);
1286
1287 spin_unlock_irqrestore(&up->context_lock, flags);
1288
1289 return ret;
1290 default:
1291 return -EOPNOTSUPP;
1292 }
1293}
1294
1295
1296static int ucan_probe(struct usb_interface *intf,
1297 const struct usb_device_id *id)
1298{
1299 int ret;
1300 int i;
1301 u32 protocol_version;
1302 struct usb_device *udev;
1303 struct net_device *netdev;
1304 struct usb_host_interface *iface_desc;
1305 struct ucan_priv *up;
1306 struct usb_endpoint_descriptor *ep;
1307 u16 in_ep_size;
1308 u16 out_ep_size;
1309 u8 in_ep_addr;
1310 u8 out_ep_addr;
1311 union ucan_ctl_payload *ctl_msg_buffer;
1312 char firmware_str[sizeof(union ucan_ctl_payload) + 1];
1313
1314 udev = interface_to_usbdev(intf);
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324 iface_desc = intf->cur_altsetting;
1325 if (!iface_desc)
1326 return -ENODEV;
1327
1328 dev_info(&udev->dev,
1329 "%s: probing device on interface #%d\n",
1330 UCAN_DRIVER_NAME,
1331 iface_desc->desc.bInterfaceNumber);
1332
1333
1334 if (iface_desc->desc.bNumEndpoints != 2) {
1335 dev_err(&udev->dev,
1336 "%s: invalid EP count (%d)",
1337 UCAN_DRIVER_NAME, iface_desc->desc.bNumEndpoints);
1338 goto err_firmware_needs_update;
1339 }
1340
1341
1342 in_ep_addr = 0;
1343 out_ep_addr = 0;
1344 in_ep_size = 0;
1345 out_ep_size = 0;
1346 for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
1347 ep = &iface_desc->endpoint[i].desc;
1348
1349 if (((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != 0) &&
1350 ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
1351 USB_ENDPOINT_XFER_BULK)) {
1352
1353 in_ep_addr = ep->bEndpointAddress;
1354 in_ep_addr &= USB_ENDPOINT_NUMBER_MASK;
1355 in_ep_size = le16_to_cpu(ep->wMaxPacketSize);
1356 } else if (((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ==
1357 0) &&
1358 ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
1359 USB_ENDPOINT_XFER_BULK)) {
1360
1361 out_ep_addr = ep->bEndpointAddress;
1362 out_ep_addr &= USB_ENDPOINT_NUMBER_MASK;
1363 out_ep_size = le16_to_cpu(ep->wMaxPacketSize);
1364 }
1365 }
1366
1367
1368 if (!in_ep_addr || !out_ep_addr) {
1369 dev_err(&udev->dev, "%s: invalid endpoint configuration\n",
1370 UCAN_DRIVER_NAME);
1371 goto err_firmware_needs_update;
1372 }
1373 if (in_ep_size < sizeof(struct ucan_message_in)) {
1374 dev_err(&udev->dev, "%s: invalid in_ep MaxPacketSize\n",
1375 UCAN_DRIVER_NAME);
1376 goto err_firmware_needs_update;
1377 }
1378 if (out_ep_size < sizeof(struct ucan_message_out)) {
1379 dev_err(&udev->dev, "%s: invalid out_ep MaxPacketSize\n",
1380 UCAN_DRIVER_NAME);
1381 goto err_firmware_needs_update;
1382 }
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396 ctl_msg_buffer = devm_kzalloc(&udev->dev,
1397 sizeof(union ucan_ctl_payload),
1398 GFP_KERNEL);
1399 if (!ctl_msg_buffer) {
1400 dev_err(&udev->dev,
1401 "%s: failed to allocate control pipe memory\n",
1402 UCAN_DRIVER_NAME);
1403 return -ENOMEM;
1404 }
1405
1406
1407
1408
1409
1410
1411 ret = usb_control_msg(udev,
1412 usb_rcvctrlpipe(udev, 0),
1413 UCAN_COMMAND_GET,
1414 USB_DIR_IN | USB_TYPE_VENDOR |
1415 USB_RECIP_INTERFACE,
1416 UCAN_COMMAND_GET_PROTOCOL_VERSION,
1417 iface_desc->desc.bInterfaceNumber,
1418 ctl_msg_buffer,
1419 sizeof(union ucan_ctl_payload),
1420 UCAN_USB_CTL_PIPE_TIMEOUT);
1421
1422
1423
1424
1425 if (ret != 4) {
1426 dev_err(&udev->dev,
1427 "%s: could not read protocol version, ret=%d\n",
1428 UCAN_DRIVER_NAME, ret);
1429 if (ret >= 0)
1430 ret = -EINVAL;
1431 goto err_firmware_needs_update;
1432 }
1433
1434
1435 protocol_version =
1436 le32_to_cpu(ctl_msg_buffer->cmd_get_protocol_version.version);
1437 if (protocol_version < UCAN_PROTOCOL_VERSION_MIN ||
1438 protocol_version > UCAN_PROTOCOL_VERSION_MAX) {
1439 dev_err(&udev->dev,
1440 "%s: device protocol version %d is not supported\n",
1441 UCAN_DRIVER_NAME, protocol_version);
1442 goto err_firmware_needs_update;
1443 }
1444
1445
1446
1447
1448
1449
1450 ret = usb_control_msg(udev,
1451 usb_rcvctrlpipe(udev, 0),
1452 UCAN_COMMAND_GET,
1453 USB_DIR_IN | USB_TYPE_VENDOR |
1454 USB_RECIP_INTERFACE,
1455 UCAN_COMMAND_GET_INFO,
1456 iface_desc->desc.bInterfaceNumber,
1457 ctl_msg_buffer,
1458 sizeof(ctl_msg_buffer->cmd_get_device_info),
1459 UCAN_USB_CTL_PIPE_TIMEOUT);
1460
1461 if (ret < 0) {
1462 dev_err(&udev->dev, "%s: failed to retrieve device info\n",
1463 UCAN_DRIVER_NAME);
1464 goto err_firmware_needs_update;
1465 }
1466 if (ret < sizeof(ctl_msg_buffer->cmd_get_device_info)) {
1467 dev_err(&udev->dev, "%s: device reported invalid device info\n",
1468 UCAN_DRIVER_NAME);
1469 goto err_firmware_needs_update;
1470 }
1471 if (ctl_msg_buffer->cmd_get_device_info.tx_fifo == 0) {
1472 dev_err(&udev->dev,
1473 "%s: device reported invalid tx-fifo size\n",
1474 UCAN_DRIVER_NAME);
1475 goto err_firmware_needs_update;
1476 }
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486 netdev = alloc_candev(sizeof(struct ucan_priv),
1487 ctl_msg_buffer->cmd_get_device_info.tx_fifo);
1488 if (!netdev) {
1489 dev_err(&udev->dev,
1490 "%s: cannot allocate candev\n", UCAN_DRIVER_NAME);
1491 return -ENOMEM;
1492 }
1493
1494 up = netdev_priv(netdev);
1495
1496
1497 up->udev = udev;
1498 up->intf = intf;
1499 up->netdev = netdev;
1500 up->intf_index = iface_desc->desc.bInterfaceNumber;
1501 up->in_ep_addr = in_ep_addr;
1502 up->out_ep_addr = out_ep_addr;
1503 up->in_ep_size = in_ep_size;
1504 up->ctl_msg_buffer = ctl_msg_buffer;
1505 up->context_array = NULL;
1506 up->available_tx_urbs = 0;
1507
1508 up->can.state = CAN_STATE_STOPPED;
1509 up->can.bittiming_const = &up->device_info.bittiming_const;
1510 up->can.do_set_bittiming = ucan_set_bittiming;
1511 up->can.do_set_mode = &ucan_set_mode;
1512 spin_lock_init(&up->context_lock);
1513 spin_lock_init(&up->echo_skb_lock);
1514 netdev->netdev_ops = &ucan_netdev_ops;
1515
1516 usb_set_intfdata(intf, up);
1517 SET_NETDEV_DEV(netdev, &intf->dev);
1518
1519
1520
1521
1522
1523 ucan_parse_device_info(up, &ctl_msg_buffer->cmd_get_device_info);
1524
1525
1526 ret = ucan_device_request_in(up, UCAN_DEVICE_GET_FW_STRING, 0,
1527 sizeof(union ucan_ctl_payload));
1528 if (ret > 0) {
1529
1530 strncpy(firmware_str, up->ctl_msg_buffer->raw,
1531 sizeof(union ucan_ctl_payload));
1532 firmware_str[sizeof(union ucan_ctl_payload)] = '\0';
1533 } else {
1534 strcpy(firmware_str, "unknown");
1535 }
1536
1537
1538 ret = ucan_ctrl_command_out(up, UCAN_COMMAND_RESET, 0, 0);
1539 if (ret < 0)
1540 goto err_free_candev;
1541
1542 init_usb_anchor(&up->rx_urbs);
1543 init_usb_anchor(&up->tx_urbs);
1544
1545 up->can.state = CAN_STATE_STOPPED;
1546
1547
1548 ret = register_candev(netdev);
1549 if (ret)
1550 goto err_free_candev;
1551
1552
1553 netdev_info(up->netdev, "registered device\n");
1554 netdev_info(up->netdev, "firmware string: %s\n", firmware_str);
1555
1556
1557 return 0;
1558
1559err_free_candev:
1560 free_candev(netdev);
1561 return ret;
1562
1563err_firmware_needs_update:
1564 dev_err(&udev->dev,
1565 "%s: probe failed; try to update the device firmware\n",
1566 UCAN_DRIVER_NAME);
1567 return -ENODEV;
1568}
1569
1570
1571static void ucan_disconnect(struct usb_interface *intf)
1572{
1573 struct ucan_priv *up = usb_get_intfdata(intf);
1574
1575 usb_set_intfdata(intf, NULL);
1576
1577 if (up) {
1578 unregister_netdev(up->netdev);
1579 free_candev(up->netdev);
1580 }
1581}
1582
1583static struct usb_device_id ucan_table[] = {
1584
1585 {USB_DEVICE_INTERFACE_NUMBER(0x2294, 0x425a, 0)},
1586
1587 {USB_DEVICE_INTERFACE_NUMBER(0x2294, 0x425b, 0)},
1588 {}
1589};
1590
1591MODULE_DEVICE_TABLE(usb, ucan_table);
1592
1593static struct usb_driver ucan_driver = {
1594 .name = UCAN_DRIVER_NAME,
1595 .probe = ucan_probe,
1596 .disconnect = ucan_disconnect,
1597 .id_table = ucan_table,
1598};
1599
1600module_usb_driver(ucan_driver);
1601
1602MODULE_LICENSE("GPL v2");
1603MODULE_AUTHOR("Martin Elshuber <martin.elshuber@theobroma-systems.com>");
1604MODULE_AUTHOR("Jakob Unterwurzacher <jakob.unterwurzacher@theobroma-systems.com>");
1605MODULE_DESCRIPTION("Driver for Theobroma Systems UCAN devices");
1606