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