1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <linux/init.h>
16#include <linux/completion.h>
17#include <linux/module.h>
18#include <linux/netdevice.h>
19#include <linux/usb.h>
20
21#include <linux/can.h>
22#include <linux/can/dev.h>
23#include <linux/can/error.h>
24
25#define MAX_TX_URBS 16
26#define MAX_RX_URBS 4
27#define START_TIMEOUT 1000
28#define STOP_TIMEOUT 1000
29#define USB_SEND_TIMEOUT 1000
30#define USB_RECV_TIMEOUT 1000
31#define RX_BUFFER_SIZE 3072
32#define CAN_USB_CLOCK 8000000
33#define MAX_NET_DEVICES 3
34
35
36#define KVASER_VENDOR_ID 0x0bfd
37#define USB_LEAF_DEVEL_PRODUCT_ID 10
38#define USB_LEAF_LITE_PRODUCT_ID 11
39#define USB_LEAF_PRO_PRODUCT_ID 12
40#define USB_LEAF_SPRO_PRODUCT_ID 14
41#define USB_LEAF_PRO_LS_PRODUCT_ID 15
42#define USB_LEAF_PRO_SWC_PRODUCT_ID 16
43#define USB_LEAF_PRO_LIN_PRODUCT_ID 17
44#define USB_LEAF_SPRO_LS_PRODUCT_ID 18
45#define USB_LEAF_SPRO_SWC_PRODUCT_ID 19
46#define USB_MEMO2_DEVEL_PRODUCT_ID 22
47#define USB_MEMO2_HSHS_PRODUCT_ID 23
48#define USB_UPRO_HSHS_PRODUCT_ID 24
49#define USB_LEAF_LITE_GI_PRODUCT_ID 25
50#define USB_LEAF_PRO_OBDII_PRODUCT_ID 26
51#define USB_MEMO2_HSLS_PRODUCT_ID 27
52#define USB_LEAF_LITE_CH_PRODUCT_ID 28
53#define USB_BLACKBIRD_SPRO_PRODUCT_ID 29
54#define USB_OEM_MERCURY_PRODUCT_ID 34
55#define USB_OEM_LEAF_PRODUCT_ID 35
56#define USB_CAN_R_PRODUCT_ID 39
57
58
59#define KVASER_HAS_SILENT_MODE BIT(0)
60#define KVASER_HAS_TXRX_ERRORS BIT(1)
61
62
63#define MSG_HEADER_LEN 2
64
65
66#define MSG_FLAG_ERROR_FRAME BIT(0)
67#define MSG_FLAG_OVERRUN BIT(1)
68#define MSG_FLAG_NERR BIT(2)
69#define MSG_FLAG_WAKEUP BIT(3)
70#define MSG_FLAG_REMOTE_FRAME BIT(4)
71#define MSG_FLAG_RESERVED BIT(5)
72#define MSG_FLAG_TX_ACK BIT(6)
73#define MSG_FLAG_TX_REQUEST BIT(7)
74
75
76#define M16C_STATE_BUS_RESET BIT(0)
77#define M16C_STATE_BUS_ERROR BIT(4)
78#define M16C_STATE_BUS_PASSIVE BIT(5)
79#define M16C_STATE_BUS_OFF BIT(6)
80
81
82#define CMD_RX_STD_MESSAGE 12
83#define CMD_TX_STD_MESSAGE 13
84#define CMD_RX_EXT_MESSAGE 14
85#define CMD_TX_EXT_MESSAGE 15
86#define CMD_SET_BUS_PARAMS 16
87#define CMD_GET_BUS_PARAMS 17
88#define CMD_GET_BUS_PARAMS_REPLY 18
89#define CMD_GET_CHIP_STATE 19
90#define CMD_CHIP_STATE_EVENT 20
91#define CMD_SET_CTRL_MODE 21
92#define CMD_GET_CTRL_MODE 22
93#define CMD_GET_CTRL_MODE_REPLY 23
94#define CMD_RESET_CHIP 24
95#define CMD_RESET_CARD 25
96#define CMD_START_CHIP 26
97#define CMD_START_CHIP_REPLY 27
98#define CMD_STOP_CHIP 28
99#define CMD_STOP_CHIP_REPLY 29
100#define CMD_GET_CARD_INFO2 32
101#define CMD_GET_CARD_INFO 34
102#define CMD_GET_CARD_INFO_REPLY 35
103#define CMD_GET_SOFTWARE_INFO 38
104#define CMD_GET_SOFTWARE_INFO_REPLY 39
105#define CMD_ERROR_EVENT 45
106#define CMD_FLUSH_QUEUE 48
107#define CMD_RESET_ERROR_COUNTER 49
108#define CMD_TX_ACKNOWLEDGE 50
109#define CMD_CAN_ERROR_EVENT 51
110#define CMD_USB_THROTTLE 77
111#define CMD_LOG_MESSAGE 106
112
113
114#define M16C_EF_ACKE BIT(0)
115#define M16C_EF_CRCE BIT(1)
116#define M16C_EF_FORME BIT(2)
117#define M16C_EF_STFE BIT(3)
118#define M16C_EF_BITE0 BIT(4)
119#define M16C_EF_BITE1 BIT(5)
120#define M16C_EF_RCVE BIT(6)
121#define M16C_EF_TRE BIT(7)
122
123
124#define KVASER_USB_TSEG1_MIN 1
125#define KVASER_USB_TSEG1_MAX 16
126#define KVASER_USB_TSEG2_MIN 1
127#define KVASER_USB_TSEG2_MAX 8
128#define KVASER_USB_SJW_MAX 4
129#define KVASER_USB_BRP_MIN 1
130#define KVASER_USB_BRP_MAX 64
131#define KVASER_USB_BRP_INC 1
132
133
134#define KVASER_CTRL_MODE_NORMAL 1
135#define KVASER_CTRL_MODE_SILENT 2
136#define KVASER_CTRL_MODE_SELFRECEPTION 3
137#define KVASER_CTRL_MODE_OFF 4
138
139
140#define KVASER_EXTENDED_FRAME BIT(31)
141
142struct kvaser_msg_simple {
143 u8 tid;
144 u8 channel;
145} __packed;
146
147struct kvaser_msg_cardinfo {
148 u8 tid;
149 u8 nchannels;
150 __le32 serial_number;
151 __le32 padding;
152 __le32 clock_resolution;
153 __le32 mfgdate;
154 u8 ean[8];
155 u8 hw_revision;
156 u8 usb_hs_mode;
157 __le16 padding2;
158} __packed;
159
160struct kvaser_msg_cardinfo2 {
161 u8 tid;
162 u8 channel;
163 u8 pcb_id[24];
164 __le32 oem_unlock_code;
165} __packed;
166
167struct kvaser_msg_softinfo {
168 u8 tid;
169 u8 channel;
170 __le32 sw_options;
171 __le32 fw_version;
172 __le16 max_outstanding_tx;
173 __le16 padding[9];
174} __packed;
175
176struct kvaser_msg_busparams {
177 u8 tid;
178 u8 channel;
179 __le32 bitrate;
180 u8 tseg1;
181 u8 tseg2;
182 u8 sjw;
183 u8 no_samp;
184} __packed;
185
186struct kvaser_msg_tx_can {
187 u8 channel;
188 u8 tid;
189 u8 msg[14];
190 u8 padding;
191 u8 flags;
192} __packed;
193
194struct kvaser_msg_rx_can {
195 u8 channel;
196 u8 flag;
197 __le16 time[3];
198 u8 msg[14];
199} __packed;
200
201struct kvaser_msg_chip_state_event {
202 u8 tid;
203 u8 channel;
204 __le16 time[3];
205 u8 tx_errors_count;
206 u8 rx_errors_count;
207 u8 status;
208 u8 padding[3];
209} __packed;
210
211struct kvaser_msg_tx_acknowledge {
212 u8 channel;
213 u8 tid;
214 __le16 time[3];
215 u8 flags;
216 u8 time_offset;
217} __packed;
218
219struct kvaser_msg_error_event {
220 u8 tid;
221 u8 flags;
222 __le16 time[3];
223 u8 channel;
224 u8 padding;
225 u8 tx_errors_count;
226 u8 rx_errors_count;
227 u8 status;
228 u8 error_factor;
229} __packed;
230
231struct kvaser_msg_ctrl_mode {
232 u8 tid;
233 u8 channel;
234 u8 ctrl_mode;
235 u8 padding[3];
236} __packed;
237
238struct kvaser_msg_flush_queue {
239 u8 tid;
240 u8 channel;
241 u8 flags;
242 u8 padding[3];
243} __packed;
244
245struct kvaser_msg_log_message {
246 u8 channel;
247 u8 flags;
248 __le16 time[3];
249 u8 dlc;
250 u8 time_offset;
251 __le32 id;
252 u8 data[8];
253} __packed;
254
255struct kvaser_msg {
256 u8 len;
257 u8 id;
258 union {
259 struct kvaser_msg_simple simple;
260 struct kvaser_msg_cardinfo cardinfo;
261 struct kvaser_msg_cardinfo2 cardinfo2;
262 struct kvaser_msg_softinfo softinfo;
263 struct kvaser_msg_busparams busparams;
264 struct kvaser_msg_tx_can tx_can;
265 struct kvaser_msg_rx_can rx_can;
266 struct kvaser_msg_chip_state_event chip_state_event;
267 struct kvaser_msg_tx_acknowledge tx_acknowledge;
268 struct kvaser_msg_error_event error_event;
269 struct kvaser_msg_ctrl_mode ctrl_mode;
270 struct kvaser_msg_flush_queue flush_queue;
271 struct kvaser_msg_log_message log_message;
272 } u;
273} __packed;
274
275struct kvaser_usb_tx_urb_context {
276 struct kvaser_usb_net_priv *priv;
277 u32 echo_index;
278 int dlc;
279};
280
281struct kvaser_usb {
282 struct usb_device *udev;
283 struct kvaser_usb_net_priv *nets[MAX_NET_DEVICES];
284
285 struct usb_endpoint_descriptor *bulk_in, *bulk_out;
286 struct usb_anchor rx_submitted;
287
288 u32 fw_version;
289 unsigned int nchannels;
290
291 bool rxinitdone;
292 void *rxbuf[MAX_RX_URBS];
293 dma_addr_t rxbuf_dma[MAX_RX_URBS];
294};
295
296struct kvaser_usb_net_priv {
297 struct can_priv can;
298
299 atomic_t active_tx_urbs;
300 struct usb_anchor tx_submitted;
301 struct kvaser_usb_tx_urb_context tx_contexts[MAX_TX_URBS];
302
303 struct completion start_comp, stop_comp;
304
305 struct kvaser_usb *dev;
306 struct net_device *netdev;
307 int channel;
308
309 struct can_berr_counter bec;
310};
311
312static const struct usb_device_id kvaser_usb_table[] = {
313 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_DEVEL_PRODUCT_ID) },
314 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_PRODUCT_ID) },
315 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_PRODUCT_ID),
316 .driver_info = KVASER_HAS_TXRX_ERRORS |
317 KVASER_HAS_SILENT_MODE },
318 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_PRODUCT_ID),
319 .driver_info = KVASER_HAS_TXRX_ERRORS |
320 KVASER_HAS_SILENT_MODE },
321 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_LS_PRODUCT_ID),
322 .driver_info = KVASER_HAS_TXRX_ERRORS |
323 KVASER_HAS_SILENT_MODE },
324 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_SWC_PRODUCT_ID),
325 .driver_info = KVASER_HAS_TXRX_ERRORS |
326 KVASER_HAS_SILENT_MODE },
327 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_LIN_PRODUCT_ID),
328 .driver_info = KVASER_HAS_TXRX_ERRORS |
329 KVASER_HAS_SILENT_MODE },
330 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_LS_PRODUCT_ID),
331 .driver_info = KVASER_HAS_TXRX_ERRORS |
332 KVASER_HAS_SILENT_MODE },
333 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_SWC_PRODUCT_ID),
334 .driver_info = KVASER_HAS_TXRX_ERRORS |
335 KVASER_HAS_SILENT_MODE },
336 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_DEVEL_PRODUCT_ID),
337 .driver_info = KVASER_HAS_TXRX_ERRORS |
338 KVASER_HAS_SILENT_MODE },
339 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_HSHS_PRODUCT_ID),
340 .driver_info = KVASER_HAS_TXRX_ERRORS |
341 KVASER_HAS_SILENT_MODE },
342 { USB_DEVICE(KVASER_VENDOR_ID, USB_UPRO_HSHS_PRODUCT_ID),
343 .driver_info = KVASER_HAS_TXRX_ERRORS },
344 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_GI_PRODUCT_ID) },
345 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_OBDII_PRODUCT_ID),
346 .driver_info = KVASER_HAS_TXRX_ERRORS |
347 KVASER_HAS_SILENT_MODE },
348 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_HSLS_PRODUCT_ID),
349 .driver_info = KVASER_HAS_TXRX_ERRORS },
350 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_CH_PRODUCT_ID),
351 .driver_info = KVASER_HAS_TXRX_ERRORS },
352 { USB_DEVICE(KVASER_VENDOR_ID, USB_BLACKBIRD_SPRO_PRODUCT_ID),
353 .driver_info = KVASER_HAS_TXRX_ERRORS },
354 { USB_DEVICE(KVASER_VENDOR_ID, USB_OEM_MERCURY_PRODUCT_ID),
355 .driver_info = KVASER_HAS_TXRX_ERRORS },
356 { USB_DEVICE(KVASER_VENDOR_ID, USB_OEM_LEAF_PRODUCT_ID),
357 .driver_info = KVASER_HAS_TXRX_ERRORS },
358 { USB_DEVICE(KVASER_VENDOR_ID, USB_CAN_R_PRODUCT_ID),
359 .driver_info = KVASER_HAS_TXRX_ERRORS },
360 { }
361};
362MODULE_DEVICE_TABLE(usb, kvaser_usb_table);
363
364static inline int kvaser_usb_send_msg(const struct kvaser_usb *dev,
365 struct kvaser_msg *msg)
366{
367 int actual_len;
368
369 return usb_bulk_msg(dev->udev,
370 usb_sndbulkpipe(dev->udev,
371 dev->bulk_out->bEndpointAddress),
372 msg, msg->len, &actual_len,
373 USB_SEND_TIMEOUT);
374}
375
376static int kvaser_usb_wait_msg(const struct kvaser_usb *dev, u8 id,
377 struct kvaser_msg *msg)
378{
379 struct kvaser_msg *tmp;
380 void *buf;
381 int actual_len;
382 int err;
383 int pos = 0;
384
385 buf = kzalloc(RX_BUFFER_SIZE, GFP_KERNEL);
386 if (!buf)
387 return -ENOMEM;
388
389 err = usb_bulk_msg(dev->udev,
390 usb_rcvbulkpipe(dev->udev,
391 dev->bulk_in->bEndpointAddress),
392 buf, RX_BUFFER_SIZE, &actual_len,
393 USB_RECV_TIMEOUT);
394 if (err < 0)
395 goto end;
396
397 while (pos <= actual_len - MSG_HEADER_LEN) {
398 tmp = buf + pos;
399
400 if (!tmp->len)
401 break;
402
403 if (pos + tmp->len > actual_len) {
404 dev_err(dev->udev->dev.parent, "Format error\n");
405 break;
406 }
407
408 if (tmp->id == id) {
409 memcpy(msg, tmp, tmp->len);
410 goto end;
411 }
412
413 pos += tmp->len;
414 }
415
416 err = -EINVAL;
417
418end:
419 kfree(buf);
420
421 return err;
422}
423
424static int kvaser_usb_send_simple_msg(const struct kvaser_usb *dev,
425 u8 msg_id, int channel)
426{
427 struct kvaser_msg *msg;
428 int rc;
429
430 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
431 if (!msg)
432 return -ENOMEM;
433
434 msg->id = msg_id;
435 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_simple);
436 msg->u.simple.channel = channel;
437 msg->u.simple.tid = 0xff;
438
439 rc = kvaser_usb_send_msg(dev, msg);
440
441 kfree(msg);
442 return rc;
443}
444
445static int kvaser_usb_get_software_info(struct kvaser_usb *dev)
446{
447 struct kvaser_msg msg;
448 int err;
449
450 err = kvaser_usb_send_simple_msg(dev, CMD_GET_SOFTWARE_INFO, 0);
451 if (err)
452 return err;
453
454 err = kvaser_usb_wait_msg(dev, CMD_GET_SOFTWARE_INFO_REPLY, &msg);
455 if (err)
456 return err;
457
458 dev->fw_version = le32_to_cpu(msg.u.softinfo.fw_version);
459
460 return 0;
461}
462
463static int kvaser_usb_get_card_info(struct kvaser_usb *dev)
464{
465 struct kvaser_msg msg;
466 int err;
467
468 err = kvaser_usb_send_simple_msg(dev, CMD_GET_CARD_INFO, 0);
469 if (err)
470 return err;
471
472 err = kvaser_usb_wait_msg(dev, CMD_GET_CARD_INFO_REPLY, &msg);
473 if (err)
474 return err;
475
476 dev->nchannels = msg.u.cardinfo.nchannels;
477
478 return 0;
479}
480
481static void kvaser_usb_tx_acknowledge(const struct kvaser_usb *dev,
482 const struct kvaser_msg *msg)
483{
484 struct net_device_stats *stats;
485 struct kvaser_usb_tx_urb_context *context;
486 struct kvaser_usb_net_priv *priv;
487 struct sk_buff *skb;
488 struct can_frame *cf;
489 u8 channel = msg->u.tx_acknowledge.channel;
490 u8 tid = msg->u.tx_acknowledge.tid;
491
492 if (channel >= dev->nchannels) {
493 dev_err(dev->udev->dev.parent,
494 "Invalid channel number (%d)\n", channel);
495 return;
496 }
497
498 priv = dev->nets[channel];
499
500 if (!netif_device_present(priv->netdev))
501 return;
502
503 stats = &priv->netdev->stats;
504
505 context = &priv->tx_contexts[tid % MAX_TX_URBS];
506
507
508 if (priv->can.restart_ms &&
509 (priv->can.state >= CAN_STATE_BUS_OFF)) {
510 skb = alloc_can_err_skb(priv->netdev, &cf);
511 if (skb) {
512 cf->can_id |= CAN_ERR_RESTARTED;
513 netif_rx(skb);
514
515 stats->rx_packets++;
516 stats->rx_bytes += cf->can_dlc;
517 } else {
518 netdev_err(priv->netdev,
519 "No memory left for err_skb\n");
520 }
521
522 priv->can.can_stats.restarts++;
523 netif_carrier_on(priv->netdev);
524
525 priv->can.state = CAN_STATE_ERROR_ACTIVE;
526 }
527
528 stats->tx_packets++;
529 stats->tx_bytes += context->dlc;
530 can_get_echo_skb(priv->netdev, context->echo_index);
531
532 context->echo_index = MAX_TX_URBS;
533 atomic_dec(&priv->active_tx_urbs);
534
535 netif_wake_queue(priv->netdev);
536}
537
538static void kvaser_usb_simple_msg_callback(struct urb *urb)
539{
540 struct net_device *netdev = urb->context;
541
542 kfree(urb->transfer_buffer);
543
544 if (urb->status)
545 netdev_warn(netdev, "urb status received: %d\n",
546 urb->status);
547}
548
549static int kvaser_usb_simple_msg_async(struct kvaser_usb_net_priv *priv,
550 u8 msg_id)
551{
552 struct kvaser_usb *dev = priv->dev;
553 struct net_device *netdev = priv->netdev;
554 struct kvaser_msg *msg;
555 struct urb *urb;
556 void *buf;
557 int err;
558
559 urb = usb_alloc_urb(0, GFP_ATOMIC);
560 if (!urb) {
561 netdev_err(netdev, "No memory left for URBs\n");
562 return -ENOMEM;
563 }
564
565 buf = kmalloc(sizeof(struct kvaser_msg), GFP_ATOMIC);
566 if (!buf) {
567 usb_free_urb(urb);
568 return -ENOMEM;
569 }
570
571 msg = (struct kvaser_msg *)buf;
572 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_simple);
573 msg->id = msg_id;
574 msg->u.simple.channel = priv->channel;
575
576 usb_fill_bulk_urb(urb, dev->udev,
577 usb_sndbulkpipe(dev->udev,
578 dev->bulk_out->bEndpointAddress),
579 buf, msg->len,
580 kvaser_usb_simple_msg_callback, priv);
581 usb_anchor_urb(urb, &priv->tx_submitted);
582
583 err = usb_submit_urb(urb, GFP_ATOMIC);
584 if (err) {
585 netdev_err(netdev, "Error transmitting URB\n");
586 usb_unanchor_urb(urb);
587 usb_free_urb(urb);
588 kfree(buf);
589 return err;
590 }
591
592 usb_free_urb(urb);
593
594 return 0;
595}
596
597static void kvaser_usb_unlink_tx_urbs(struct kvaser_usb_net_priv *priv)
598{
599 int i;
600
601 usb_kill_anchored_urbs(&priv->tx_submitted);
602 atomic_set(&priv->active_tx_urbs, 0);
603
604 for (i = 0; i < MAX_TX_URBS; i++)
605 priv->tx_contexts[i].echo_index = MAX_TX_URBS;
606}
607
608static void kvaser_usb_rx_error(const struct kvaser_usb *dev,
609 const struct kvaser_msg *msg)
610{
611 struct can_frame *cf;
612 struct sk_buff *skb;
613 struct net_device_stats *stats;
614 struct kvaser_usb_net_priv *priv;
615 unsigned int new_state;
616 u8 channel, status, txerr, rxerr, error_factor;
617
618 switch (msg->id) {
619 case CMD_CAN_ERROR_EVENT:
620 channel = msg->u.error_event.channel;
621 status = msg->u.error_event.status;
622 txerr = msg->u.error_event.tx_errors_count;
623 rxerr = msg->u.error_event.rx_errors_count;
624 error_factor = msg->u.error_event.error_factor;
625 break;
626 case CMD_LOG_MESSAGE:
627 channel = msg->u.log_message.channel;
628 status = msg->u.log_message.data[0];
629 txerr = msg->u.log_message.data[2];
630 rxerr = msg->u.log_message.data[3];
631 error_factor = msg->u.log_message.data[1];
632 break;
633 case CMD_CHIP_STATE_EVENT:
634 channel = msg->u.chip_state_event.channel;
635 status = msg->u.chip_state_event.status;
636 txerr = msg->u.chip_state_event.tx_errors_count;
637 rxerr = msg->u.chip_state_event.rx_errors_count;
638 error_factor = 0;
639 break;
640 default:
641 dev_err(dev->udev->dev.parent, "Invalid msg id (%d)\n",
642 msg->id);
643 return;
644 }
645
646 if (channel >= dev->nchannels) {
647 dev_err(dev->udev->dev.parent,
648 "Invalid channel number (%d)\n", channel);
649 return;
650 }
651
652 priv = dev->nets[channel];
653 stats = &priv->netdev->stats;
654
655 skb = alloc_can_err_skb(priv->netdev, &cf);
656 if (!skb) {
657 stats->rx_dropped++;
658 return;
659 }
660
661 new_state = priv->can.state;
662
663 netdev_dbg(priv->netdev, "Error status: 0x%02x\n", status);
664
665 if (status & (M16C_STATE_BUS_OFF | M16C_STATE_BUS_RESET)) {
666 cf->can_id |= CAN_ERR_BUSOFF;
667
668 priv->can.can_stats.bus_off++;
669 if (!priv->can.restart_ms)
670 kvaser_usb_simple_msg_async(priv, CMD_STOP_CHIP);
671
672 netif_carrier_off(priv->netdev);
673
674 new_state = CAN_STATE_BUS_OFF;
675 } else if (status & M16C_STATE_BUS_PASSIVE) {
676 if (priv->can.state != CAN_STATE_ERROR_PASSIVE) {
677 cf->can_id |= CAN_ERR_CRTL;
678
679 if (txerr || rxerr)
680 cf->data[1] = (txerr > rxerr)
681 ? CAN_ERR_CRTL_TX_PASSIVE
682 : CAN_ERR_CRTL_RX_PASSIVE;
683 else
684 cf->data[1] = CAN_ERR_CRTL_TX_PASSIVE |
685 CAN_ERR_CRTL_RX_PASSIVE;
686
687 priv->can.can_stats.error_passive++;
688 }
689
690 new_state = CAN_STATE_ERROR_PASSIVE;
691 }
692
693 if (status == M16C_STATE_BUS_ERROR) {
694 if ((priv->can.state < CAN_STATE_ERROR_WARNING) &&
695 ((txerr >= 96) || (rxerr >= 96))) {
696 cf->can_id |= CAN_ERR_CRTL;
697 cf->data[1] = (txerr > rxerr)
698 ? CAN_ERR_CRTL_TX_WARNING
699 : CAN_ERR_CRTL_RX_WARNING;
700
701 priv->can.can_stats.error_warning++;
702 new_state = CAN_STATE_ERROR_WARNING;
703 } else if (priv->can.state > CAN_STATE_ERROR_ACTIVE) {
704 cf->can_id |= CAN_ERR_PROT;
705 cf->data[2] = CAN_ERR_PROT_ACTIVE;
706
707 new_state = CAN_STATE_ERROR_ACTIVE;
708 }
709 }
710
711 if (!status) {
712 cf->can_id |= CAN_ERR_PROT;
713 cf->data[2] = CAN_ERR_PROT_ACTIVE;
714
715 new_state = CAN_STATE_ERROR_ACTIVE;
716 }
717
718 if (priv->can.restart_ms &&
719 (priv->can.state >= CAN_STATE_BUS_OFF) &&
720 (new_state < CAN_STATE_BUS_OFF)) {
721 cf->can_id |= CAN_ERR_RESTARTED;
722 netif_carrier_on(priv->netdev);
723
724 priv->can.can_stats.restarts++;
725 }
726
727 if (error_factor) {
728 priv->can.can_stats.bus_error++;
729 stats->rx_errors++;
730
731 cf->can_id |= CAN_ERR_BUSERROR | CAN_ERR_PROT;
732
733 if (error_factor & M16C_EF_ACKE)
734 cf->data[3] |= (CAN_ERR_PROT_LOC_ACK);
735 if (error_factor & M16C_EF_CRCE)
736 cf->data[3] |= (CAN_ERR_PROT_LOC_CRC_SEQ |
737 CAN_ERR_PROT_LOC_CRC_DEL);
738 if (error_factor & M16C_EF_FORME)
739 cf->data[2] |= CAN_ERR_PROT_FORM;
740 if (error_factor & M16C_EF_STFE)
741 cf->data[2] |= CAN_ERR_PROT_STUFF;
742 if (error_factor & M16C_EF_BITE0)
743 cf->data[2] |= CAN_ERR_PROT_BIT0;
744 if (error_factor & M16C_EF_BITE1)
745 cf->data[2] |= CAN_ERR_PROT_BIT1;
746 if (error_factor & M16C_EF_TRE)
747 cf->data[2] |= CAN_ERR_PROT_TX;
748 }
749
750 cf->data[6] = txerr;
751 cf->data[7] = rxerr;
752
753 priv->bec.txerr = txerr;
754 priv->bec.rxerr = rxerr;
755
756 priv->can.state = new_state;
757
758 netif_rx(skb);
759
760 stats->rx_packets++;
761 stats->rx_bytes += cf->can_dlc;
762}
763
764static void kvaser_usb_rx_can_err(const struct kvaser_usb_net_priv *priv,
765 const struct kvaser_msg *msg)
766{
767 struct can_frame *cf;
768 struct sk_buff *skb;
769 struct net_device_stats *stats = &priv->netdev->stats;
770
771 if (msg->u.rx_can.flag & (MSG_FLAG_ERROR_FRAME |
772 MSG_FLAG_NERR)) {
773 netdev_err(priv->netdev, "Unknow error (flags: 0x%02x)\n",
774 msg->u.rx_can.flag);
775
776 stats->rx_errors++;
777 return;
778 }
779
780 if (msg->u.rx_can.flag & MSG_FLAG_OVERRUN) {
781 skb = alloc_can_err_skb(priv->netdev, &cf);
782 if (!skb) {
783 stats->rx_dropped++;
784 return;
785 }
786
787 cf->can_id |= CAN_ERR_CRTL;
788 cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
789
790 stats->rx_over_errors++;
791 stats->rx_errors++;
792
793 netif_rx(skb);
794
795 stats->rx_packets++;
796 stats->rx_bytes += cf->can_dlc;
797 }
798}
799
800static void kvaser_usb_rx_can_msg(const struct kvaser_usb *dev,
801 const struct kvaser_msg *msg)
802{
803 struct kvaser_usb_net_priv *priv;
804 struct can_frame *cf;
805 struct sk_buff *skb;
806 struct net_device_stats *stats;
807 u8 channel = msg->u.rx_can.channel;
808
809 if (channel >= dev->nchannels) {
810 dev_err(dev->udev->dev.parent,
811 "Invalid channel number (%d)\n", channel);
812 return;
813 }
814
815 priv = dev->nets[channel];
816 stats = &priv->netdev->stats;
817
818 if ((msg->u.rx_can.flag & MSG_FLAG_ERROR_FRAME) &&
819 (msg->id == CMD_LOG_MESSAGE)) {
820 kvaser_usb_rx_error(dev, msg);
821 return;
822 } else if (msg->u.rx_can.flag & (MSG_FLAG_ERROR_FRAME |
823 MSG_FLAG_NERR |
824 MSG_FLAG_OVERRUN)) {
825 kvaser_usb_rx_can_err(priv, msg);
826 return;
827 } else if (msg->u.rx_can.flag & ~MSG_FLAG_REMOTE_FRAME) {
828 netdev_warn(priv->netdev,
829 "Unhandled frame (flags: 0x%02x)",
830 msg->u.rx_can.flag);
831 return;
832 }
833
834 skb = alloc_can_skb(priv->netdev, &cf);
835 if (!skb) {
836 stats->tx_dropped++;
837 return;
838 }
839
840 if (msg->id == CMD_LOG_MESSAGE) {
841 cf->can_id = le32_to_cpu(msg->u.log_message.id);
842 if (cf->can_id & KVASER_EXTENDED_FRAME)
843 cf->can_id &= CAN_EFF_MASK | CAN_EFF_FLAG;
844 else
845 cf->can_id &= CAN_SFF_MASK;
846
847 cf->can_dlc = get_can_dlc(msg->u.log_message.dlc);
848
849 if (msg->u.log_message.flags & MSG_FLAG_REMOTE_FRAME)
850 cf->can_id |= CAN_RTR_FLAG;
851 else
852 memcpy(cf->data, &msg->u.log_message.data,
853 cf->can_dlc);
854 } else {
855 cf->can_id = ((msg->u.rx_can.msg[0] & 0x1f) << 6) |
856 (msg->u.rx_can.msg[1] & 0x3f);
857
858 if (msg->id == CMD_RX_EXT_MESSAGE) {
859 cf->can_id <<= 18;
860 cf->can_id |= ((msg->u.rx_can.msg[2] & 0x0f) << 14) |
861 ((msg->u.rx_can.msg[3] & 0xff) << 6) |
862 (msg->u.rx_can.msg[4] & 0x3f);
863 cf->can_id |= CAN_EFF_FLAG;
864 }
865
866 cf->can_dlc = get_can_dlc(msg->u.rx_can.msg[5]);
867
868 if (msg->u.rx_can.flag & MSG_FLAG_REMOTE_FRAME)
869 cf->can_id |= CAN_RTR_FLAG;
870 else
871 memcpy(cf->data, &msg->u.rx_can.msg[6],
872 cf->can_dlc);
873 }
874
875 netif_rx(skb);
876
877 stats->rx_packets++;
878 stats->rx_bytes += cf->can_dlc;
879}
880
881static void kvaser_usb_start_chip_reply(const struct kvaser_usb *dev,
882 const struct kvaser_msg *msg)
883{
884 struct kvaser_usb_net_priv *priv;
885 u8 channel = msg->u.simple.channel;
886
887 if (channel >= dev->nchannels) {
888 dev_err(dev->udev->dev.parent,
889 "Invalid channel number (%d)\n", channel);
890 return;
891 }
892
893 priv = dev->nets[channel];
894
895 if (completion_done(&priv->start_comp) &&
896 netif_queue_stopped(priv->netdev)) {
897 netif_wake_queue(priv->netdev);
898 } else {
899 netif_start_queue(priv->netdev);
900 complete(&priv->start_comp);
901 }
902}
903
904static void kvaser_usb_stop_chip_reply(const struct kvaser_usb *dev,
905 const struct kvaser_msg *msg)
906{
907 struct kvaser_usb_net_priv *priv;
908 u8 channel = msg->u.simple.channel;
909
910 if (channel >= dev->nchannels) {
911 dev_err(dev->udev->dev.parent,
912 "Invalid channel number (%d)\n", channel);
913 return;
914 }
915
916 priv = dev->nets[channel];
917
918 complete(&priv->stop_comp);
919}
920
921static void kvaser_usb_handle_message(const struct kvaser_usb *dev,
922 const struct kvaser_msg *msg)
923{
924 switch (msg->id) {
925 case CMD_START_CHIP_REPLY:
926 kvaser_usb_start_chip_reply(dev, msg);
927 break;
928
929 case CMD_STOP_CHIP_REPLY:
930 kvaser_usb_stop_chip_reply(dev, msg);
931 break;
932
933 case CMD_RX_STD_MESSAGE:
934 case CMD_RX_EXT_MESSAGE:
935 case CMD_LOG_MESSAGE:
936 kvaser_usb_rx_can_msg(dev, msg);
937 break;
938
939 case CMD_CHIP_STATE_EVENT:
940 case CMD_CAN_ERROR_EVENT:
941 kvaser_usb_rx_error(dev, msg);
942 break;
943
944 case CMD_TX_ACKNOWLEDGE:
945 kvaser_usb_tx_acknowledge(dev, msg);
946 break;
947
948 default:
949 dev_warn(dev->udev->dev.parent,
950 "Unhandled message (%d)\n", msg->id);
951 break;
952 }
953}
954
955static void kvaser_usb_read_bulk_callback(struct urb *urb)
956{
957 struct kvaser_usb *dev = urb->context;
958 struct kvaser_msg *msg;
959 int pos = 0;
960 int err, i;
961
962 switch (urb->status) {
963 case 0:
964 break;
965 case -ENOENT:
966 case -ESHUTDOWN:
967 return;
968 default:
969 dev_info(dev->udev->dev.parent, "Rx URB aborted (%d)\n",
970 urb->status);
971 goto resubmit_urb;
972 }
973
974 while (pos <= urb->actual_length - MSG_HEADER_LEN) {
975 msg = urb->transfer_buffer + pos;
976
977 if (!msg->len)
978 break;
979
980 if (pos + msg->len > urb->actual_length) {
981 dev_err(dev->udev->dev.parent, "Format error\n");
982 break;
983 }
984
985 kvaser_usb_handle_message(dev, msg);
986
987 pos += msg->len;
988 }
989
990resubmit_urb:
991 usb_fill_bulk_urb(urb, dev->udev,
992 usb_rcvbulkpipe(dev->udev,
993 dev->bulk_in->bEndpointAddress),
994 urb->transfer_buffer, RX_BUFFER_SIZE,
995 kvaser_usb_read_bulk_callback, dev);
996
997 err = usb_submit_urb(urb, GFP_ATOMIC);
998 if (err == -ENODEV) {
999 for (i = 0; i < dev->nchannels; i++) {
1000 if (!dev->nets[i])
1001 continue;
1002
1003 netif_device_detach(dev->nets[i]->netdev);
1004 }
1005 } else if (err) {
1006 dev_err(dev->udev->dev.parent,
1007 "Failed resubmitting read bulk urb: %d\n", err);
1008 }
1009
1010 return;
1011}
1012
1013static int kvaser_usb_setup_rx_urbs(struct kvaser_usb *dev)
1014{
1015 int i, err = 0;
1016
1017 if (dev->rxinitdone)
1018 return 0;
1019
1020 for (i = 0; i < MAX_RX_URBS; i++) {
1021 struct urb *urb = NULL;
1022 u8 *buf = NULL;
1023 dma_addr_t buf_dma;
1024
1025 urb = usb_alloc_urb(0, GFP_KERNEL);
1026 if (!urb) {
1027 dev_warn(dev->udev->dev.parent,
1028 "No memory left for URBs\n");
1029 err = -ENOMEM;
1030 break;
1031 }
1032
1033 buf = usb_alloc_coherent(dev->udev, RX_BUFFER_SIZE,
1034 GFP_KERNEL, &buf_dma);
1035 if (!buf) {
1036 dev_warn(dev->udev->dev.parent,
1037 "No memory left for USB buffer\n");
1038 usb_free_urb(urb);
1039 err = -ENOMEM;
1040 break;
1041 }
1042
1043 usb_fill_bulk_urb(urb, dev->udev,
1044 usb_rcvbulkpipe(dev->udev,
1045 dev->bulk_in->bEndpointAddress),
1046 buf, RX_BUFFER_SIZE,
1047 kvaser_usb_read_bulk_callback,
1048 dev);
1049 urb->transfer_dma = buf_dma;
1050 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1051 usb_anchor_urb(urb, &dev->rx_submitted);
1052
1053 err = usb_submit_urb(urb, GFP_KERNEL);
1054 if (err) {
1055 usb_unanchor_urb(urb);
1056 usb_free_coherent(dev->udev, RX_BUFFER_SIZE, buf,
1057 buf_dma);
1058 usb_free_urb(urb);
1059 break;
1060 }
1061
1062 dev->rxbuf[i] = buf;
1063 dev->rxbuf_dma[i] = buf_dma;
1064
1065 usb_free_urb(urb);
1066 }
1067
1068 if (i == 0) {
1069 dev_warn(dev->udev->dev.parent,
1070 "Cannot setup read URBs, error %d\n", err);
1071 return err;
1072 } else if (i < MAX_RX_URBS) {
1073 dev_warn(dev->udev->dev.parent,
1074 "RX performances may be slow\n");
1075 }
1076
1077 dev->rxinitdone = true;
1078
1079 return 0;
1080}
1081
1082static int kvaser_usb_set_opt_mode(const struct kvaser_usb_net_priv *priv)
1083{
1084 struct kvaser_msg *msg;
1085 int rc;
1086
1087 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1088 if (!msg)
1089 return -ENOMEM;
1090
1091 msg->id = CMD_SET_CTRL_MODE;
1092 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_ctrl_mode);
1093 msg->u.ctrl_mode.tid = 0xff;
1094 msg->u.ctrl_mode.channel = priv->channel;
1095
1096 if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
1097 msg->u.ctrl_mode.ctrl_mode = KVASER_CTRL_MODE_SILENT;
1098 else
1099 msg->u.ctrl_mode.ctrl_mode = KVASER_CTRL_MODE_NORMAL;
1100
1101 rc = kvaser_usb_send_msg(priv->dev, msg);
1102
1103 kfree(msg);
1104 return rc;
1105}
1106
1107static int kvaser_usb_start_chip(struct kvaser_usb_net_priv *priv)
1108{
1109 int err;
1110
1111 init_completion(&priv->start_comp);
1112
1113 err = kvaser_usb_send_simple_msg(priv->dev, CMD_START_CHIP,
1114 priv->channel);
1115 if (err)
1116 return err;
1117
1118 if (!wait_for_completion_timeout(&priv->start_comp,
1119 msecs_to_jiffies(START_TIMEOUT)))
1120 return -ETIMEDOUT;
1121
1122 return 0;
1123}
1124
1125static int kvaser_usb_open(struct net_device *netdev)
1126{
1127 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1128 struct kvaser_usb *dev = priv->dev;
1129 int err;
1130
1131 err = open_candev(netdev);
1132 if (err)
1133 return err;
1134
1135 err = kvaser_usb_setup_rx_urbs(dev);
1136 if (err)
1137 goto error;
1138
1139 err = kvaser_usb_set_opt_mode(priv);
1140 if (err)
1141 goto error;
1142
1143 err = kvaser_usb_start_chip(priv);
1144 if (err) {
1145 netdev_warn(netdev, "Cannot start device, error %d\n", err);
1146 goto error;
1147 }
1148
1149 priv->can.state = CAN_STATE_ERROR_ACTIVE;
1150
1151 return 0;
1152
1153error:
1154 close_candev(netdev);
1155 return err;
1156}
1157
1158static void kvaser_usb_unlink_all_urbs(struct kvaser_usb *dev)
1159{
1160 int i;
1161
1162 usb_kill_anchored_urbs(&dev->rx_submitted);
1163
1164 for (i = 0; i < MAX_RX_URBS; i++)
1165 usb_free_coherent(dev->udev, RX_BUFFER_SIZE,
1166 dev->rxbuf[i],
1167 dev->rxbuf_dma[i]);
1168
1169 for (i = 0; i < MAX_NET_DEVICES; i++) {
1170 struct kvaser_usb_net_priv *priv = dev->nets[i];
1171
1172 if (priv)
1173 kvaser_usb_unlink_tx_urbs(priv);
1174 }
1175}
1176
1177static int kvaser_usb_stop_chip(struct kvaser_usb_net_priv *priv)
1178{
1179 int err;
1180
1181 init_completion(&priv->stop_comp);
1182
1183 err = kvaser_usb_send_simple_msg(priv->dev, CMD_STOP_CHIP,
1184 priv->channel);
1185 if (err)
1186 return err;
1187
1188 if (!wait_for_completion_timeout(&priv->stop_comp,
1189 msecs_to_jiffies(STOP_TIMEOUT)))
1190 return -ETIMEDOUT;
1191
1192 return 0;
1193}
1194
1195static int kvaser_usb_flush_queue(struct kvaser_usb_net_priv *priv)
1196{
1197 struct kvaser_msg *msg;
1198 int rc;
1199
1200 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1201 if (!msg)
1202 return -ENOMEM;
1203
1204 msg->id = CMD_FLUSH_QUEUE;
1205 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_flush_queue);
1206 msg->u.flush_queue.channel = priv->channel;
1207 msg->u.flush_queue.flags = 0x00;
1208
1209 rc = kvaser_usb_send_msg(priv->dev, msg);
1210
1211 kfree(msg);
1212 return rc;
1213}
1214
1215static int kvaser_usb_close(struct net_device *netdev)
1216{
1217 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1218 struct kvaser_usb *dev = priv->dev;
1219 int err;
1220
1221 netif_stop_queue(netdev);
1222
1223 err = kvaser_usb_flush_queue(priv);
1224 if (err)
1225 netdev_warn(netdev, "Cannot flush queue, error %d\n", err);
1226
1227 if (kvaser_usb_send_simple_msg(dev, CMD_RESET_CHIP, priv->channel))
1228 netdev_warn(netdev, "Cannot reset card, error %d\n", err);
1229
1230 err = kvaser_usb_stop_chip(priv);
1231 if (err)
1232 netdev_warn(netdev, "Cannot stop device, error %d\n", err);
1233
1234
1235 kvaser_usb_unlink_tx_urbs(priv);
1236
1237 priv->can.state = CAN_STATE_STOPPED;
1238 close_candev(priv->netdev);
1239
1240 return 0;
1241}
1242
1243static void kvaser_usb_write_bulk_callback(struct urb *urb)
1244{
1245 struct kvaser_usb_tx_urb_context *context = urb->context;
1246 struct kvaser_usb_net_priv *priv;
1247 struct net_device *netdev;
1248
1249 if (WARN_ON(!context))
1250 return;
1251
1252 priv = context->priv;
1253 netdev = priv->netdev;
1254
1255 kfree(urb->transfer_buffer);
1256
1257 if (!netif_device_present(netdev))
1258 return;
1259
1260 if (urb->status)
1261 netdev_info(netdev, "Tx URB aborted (%d)\n", urb->status);
1262}
1263
1264static netdev_tx_t kvaser_usb_start_xmit(struct sk_buff *skb,
1265 struct net_device *netdev)
1266{
1267 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1268 struct kvaser_usb *dev = priv->dev;
1269 struct net_device_stats *stats = &netdev->stats;
1270 struct can_frame *cf = (struct can_frame *)skb->data;
1271 struct kvaser_usb_tx_urb_context *context = NULL;
1272 struct urb *urb;
1273 void *buf;
1274 struct kvaser_msg *msg;
1275 int i, err;
1276 int ret = NETDEV_TX_OK;
1277
1278 if (can_dropped_invalid_skb(netdev, skb))
1279 return NETDEV_TX_OK;
1280
1281 urb = usb_alloc_urb(0, GFP_ATOMIC);
1282 if (!urb) {
1283 netdev_err(netdev, "No memory left for URBs\n");
1284 stats->tx_dropped++;
1285 dev_kfree_skb(skb);
1286 return NETDEV_TX_OK;
1287 }
1288
1289 buf = kmalloc(sizeof(struct kvaser_msg), GFP_ATOMIC);
1290 if (!buf) {
1291 stats->tx_dropped++;
1292 dev_kfree_skb(skb);
1293 goto nobufmem;
1294 }
1295
1296 msg = buf;
1297 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_tx_can);
1298 msg->u.tx_can.flags = 0;
1299 msg->u.tx_can.channel = priv->channel;
1300
1301 if (cf->can_id & CAN_EFF_FLAG) {
1302 msg->id = CMD_TX_EXT_MESSAGE;
1303 msg->u.tx_can.msg[0] = (cf->can_id >> 24) & 0x1f;
1304 msg->u.tx_can.msg[1] = (cf->can_id >> 18) & 0x3f;
1305 msg->u.tx_can.msg[2] = (cf->can_id >> 14) & 0x0f;
1306 msg->u.tx_can.msg[3] = (cf->can_id >> 6) & 0xff;
1307 msg->u.tx_can.msg[4] = cf->can_id & 0x3f;
1308 } else {
1309 msg->id = CMD_TX_STD_MESSAGE;
1310 msg->u.tx_can.msg[0] = (cf->can_id >> 6) & 0x1f;
1311 msg->u.tx_can.msg[1] = cf->can_id & 0x3f;
1312 }
1313
1314 msg->u.tx_can.msg[5] = cf->can_dlc;
1315 memcpy(&msg->u.tx_can.msg[6], cf->data, cf->can_dlc);
1316
1317 if (cf->can_id & CAN_RTR_FLAG)
1318 msg->u.tx_can.flags |= MSG_FLAG_REMOTE_FRAME;
1319
1320 for (i = 0; i < ARRAY_SIZE(priv->tx_contexts); i++) {
1321 if (priv->tx_contexts[i].echo_index == MAX_TX_URBS) {
1322 context = &priv->tx_contexts[i];
1323 break;
1324 }
1325 }
1326
1327
1328 if (!context) {
1329 netdev_warn(netdev, "cannot find free context\n");
1330 ret = NETDEV_TX_BUSY;
1331 goto releasebuf;
1332 }
1333
1334 context->priv = priv;
1335 context->echo_index = i;
1336 context->dlc = cf->can_dlc;
1337
1338 msg->u.tx_can.tid = context->echo_index;
1339
1340 usb_fill_bulk_urb(urb, dev->udev,
1341 usb_sndbulkpipe(dev->udev,
1342 dev->bulk_out->bEndpointAddress),
1343 buf, msg->len,
1344 kvaser_usb_write_bulk_callback, context);
1345 usb_anchor_urb(urb, &priv->tx_submitted);
1346
1347 can_put_echo_skb(skb, netdev, context->echo_index);
1348
1349 atomic_inc(&priv->active_tx_urbs);
1350
1351 if (atomic_read(&priv->active_tx_urbs) >= MAX_TX_URBS)
1352 netif_stop_queue(netdev);
1353
1354 err = usb_submit_urb(urb, GFP_ATOMIC);
1355 if (unlikely(err)) {
1356 can_free_echo_skb(netdev, context->echo_index);
1357
1358 atomic_dec(&priv->active_tx_urbs);
1359 usb_unanchor_urb(urb);
1360
1361 stats->tx_dropped++;
1362
1363 if (err == -ENODEV)
1364 netif_device_detach(netdev);
1365 else
1366 netdev_warn(netdev, "Failed tx_urb %d\n", err);
1367
1368 goto releasebuf;
1369 }
1370
1371 usb_free_urb(urb);
1372
1373 return NETDEV_TX_OK;
1374
1375releasebuf:
1376 kfree(buf);
1377nobufmem:
1378 usb_free_urb(urb);
1379 return ret;
1380}
1381
1382static const struct net_device_ops kvaser_usb_netdev_ops = {
1383 .ndo_open = kvaser_usb_open,
1384 .ndo_stop = kvaser_usb_close,
1385 .ndo_start_xmit = kvaser_usb_start_xmit,
1386};
1387
1388static const struct can_bittiming_const kvaser_usb_bittiming_const = {
1389 .name = "kvaser_usb",
1390 .tseg1_min = KVASER_USB_TSEG1_MIN,
1391 .tseg1_max = KVASER_USB_TSEG1_MAX,
1392 .tseg2_min = KVASER_USB_TSEG2_MIN,
1393 .tseg2_max = KVASER_USB_TSEG2_MAX,
1394 .sjw_max = KVASER_USB_SJW_MAX,
1395 .brp_min = KVASER_USB_BRP_MIN,
1396 .brp_max = KVASER_USB_BRP_MAX,
1397 .brp_inc = KVASER_USB_BRP_INC,
1398};
1399
1400static int kvaser_usb_set_bittiming(struct net_device *netdev)
1401{
1402 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1403 struct can_bittiming *bt = &priv->can.bittiming;
1404 struct kvaser_usb *dev = priv->dev;
1405 struct kvaser_msg *msg;
1406 int rc;
1407
1408 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1409 if (!msg)
1410 return -ENOMEM;
1411
1412 msg->id = CMD_SET_BUS_PARAMS;
1413 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_busparams);
1414 msg->u.busparams.channel = priv->channel;
1415 msg->u.busparams.tid = 0xff;
1416 msg->u.busparams.bitrate = cpu_to_le32(bt->bitrate);
1417 msg->u.busparams.sjw = bt->sjw;
1418 msg->u.busparams.tseg1 = bt->prop_seg + bt->phase_seg1;
1419 msg->u.busparams.tseg2 = bt->phase_seg2;
1420
1421 if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
1422 msg->u.busparams.no_samp = 3;
1423 else
1424 msg->u.busparams.no_samp = 1;
1425
1426 rc = kvaser_usb_send_msg(dev, msg);
1427
1428 kfree(msg);
1429 return rc;
1430}
1431
1432static int kvaser_usb_set_mode(struct net_device *netdev,
1433 enum can_mode mode)
1434{
1435 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1436 int err;
1437
1438 switch (mode) {
1439 case CAN_MODE_START:
1440 err = kvaser_usb_simple_msg_async(priv, CMD_START_CHIP);
1441 if (err)
1442 return err;
1443 break;
1444 default:
1445 return -EOPNOTSUPP;
1446 }
1447
1448 return 0;
1449}
1450
1451static int kvaser_usb_get_berr_counter(const struct net_device *netdev,
1452 struct can_berr_counter *bec)
1453{
1454 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1455
1456 *bec = priv->bec;
1457
1458 return 0;
1459}
1460
1461static void kvaser_usb_remove_interfaces(struct kvaser_usb *dev)
1462{
1463 int i;
1464
1465 for (i = 0; i < dev->nchannels; i++) {
1466 if (!dev->nets[i])
1467 continue;
1468
1469 unregister_netdev(dev->nets[i]->netdev);
1470 }
1471
1472 kvaser_usb_unlink_all_urbs(dev);
1473
1474 for (i = 0; i < dev->nchannels; i++) {
1475 if (!dev->nets[i])
1476 continue;
1477
1478 free_candev(dev->nets[i]->netdev);
1479 }
1480}
1481
1482static int kvaser_usb_init_one(struct usb_interface *intf,
1483 const struct usb_device_id *id, int channel)
1484{
1485 struct kvaser_usb *dev = usb_get_intfdata(intf);
1486 struct net_device *netdev;
1487 struct kvaser_usb_net_priv *priv;
1488 int i, err;
1489
1490 netdev = alloc_candev(sizeof(*priv), MAX_TX_URBS);
1491 if (!netdev) {
1492 dev_err(&intf->dev, "Cannot alloc candev\n");
1493 return -ENOMEM;
1494 }
1495
1496 priv = netdev_priv(netdev);
1497
1498 init_completion(&priv->start_comp);
1499 init_completion(&priv->stop_comp);
1500
1501 init_usb_anchor(&priv->tx_submitted);
1502 atomic_set(&priv->active_tx_urbs, 0);
1503
1504 for (i = 0; i < ARRAY_SIZE(priv->tx_contexts); i++)
1505 priv->tx_contexts[i].echo_index = MAX_TX_URBS;
1506
1507 priv->dev = dev;
1508 priv->netdev = netdev;
1509 priv->channel = channel;
1510
1511 priv->can.state = CAN_STATE_STOPPED;
1512 priv->can.clock.freq = CAN_USB_CLOCK;
1513 priv->can.bittiming_const = &kvaser_usb_bittiming_const;
1514 priv->can.do_set_bittiming = kvaser_usb_set_bittiming;
1515 priv->can.do_set_mode = kvaser_usb_set_mode;
1516 if (id->driver_info & KVASER_HAS_TXRX_ERRORS)
1517 priv->can.do_get_berr_counter = kvaser_usb_get_berr_counter;
1518 priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES;
1519 if (id->driver_info & KVASER_HAS_SILENT_MODE)
1520 priv->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY;
1521
1522 netdev->flags |= IFF_ECHO;
1523
1524 netdev->netdev_ops = &kvaser_usb_netdev_ops;
1525
1526 SET_NETDEV_DEV(netdev, &intf->dev);
1527
1528 dev->nets[channel] = priv;
1529
1530 err = register_candev(netdev);
1531 if (err) {
1532 dev_err(&intf->dev, "Failed to register can device\n");
1533 free_candev(netdev);
1534 dev->nets[channel] = NULL;
1535 return err;
1536 }
1537
1538 netdev_dbg(netdev, "device registered\n");
1539
1540 return 0;
1541}
1542
1543static int kvaser_usb_get_endpoints(const struct usb_interface *intf,
1544 struct usb_endpoint_descriptor **in,
1545 struct usb_endpoint_descriptor **out)
1546{
1547 const struct usb_host_interface *iface_desc;
1548 struct usb_endpoint_descriptor *endpoint;
1549 int i;
1550
1551 iface_desc = &intf->altsetting[0];
1552
1553 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1554 endpoint = &iface_desc->endpoint[i].desc;
1555
1556 if (!*in && usb_endpoint_is_bulk_in(endpoint))
1557 *in = endpoint;
1558
1559 if (!*out && usb_endpoint_is_bulk_out(endpoint))
1560 *out = endpoint;
1561
1562
1563 if (*in && *out)
1564 return 0;
1565 }
1566
1567 return -ENODEV;
1568}
1569
1570static int kvaser_usb_probe(struct usb_interface *intf,
1571 const struct usb_device_id *id)
1572{
1573 struct kvaser_usb *dev;
1574 int err = -ENOMEM;
1575 int i;
1576
1577 dev = devm_kzalloc(&intf->dev, sizeof(*dev), GFP_KERNEL);
1578 if (!dev)
1579 return -ENOMEM;
1580
1581 err = kvaser_usb_get_endpoints(intf, &dev->bulk_in, &dev->bulk_out);
1582 if (err) {
1583 dev_err(&intf->dev, "Cannot get usb endpoint(s)");
1584 return err;
1585 }
1586
1587 dev->udev = interface_to_usbdev(intf);
1588
1589 init_usb_anchor(&dev->rx_submitted);
1590
1591 usb_set_intfdata(intf, dev);
1592
1593 for (i = 0; i < MAX_NET_DEVICES; i++)
1594 kvaser_usb_send_simple_msg(dev, CMD_RESET_CHIP, i);
1595
1596 err = kvaser_usb_get_software_info(dev);
1597 if (err) {
1598 dev_err(&intf->dev,
1599 "Cannot get software infos, error %d\n", err);
1600 return err;
1601 }
1602
1603 err = kvaser_usb_get_card_info(dev);
1604 if (err) {
1605 dev_err(&intf->dev,
1606 "Cannot get card infos, error %d\n", err);
1607 return err;
1608 }
1609
1610 dev_dbg(&intf->dev, "Firmware version: %d.%d.%d\n",
1611 ((dev->fw_version >> 24) & 0xff),
1612 ((dev->fw_version >> 16) & 0xff),
1613 (dev->fw_version & 0xffff));
1614
1615 for (i = 0; i < dev->nchannels; i++) {
1616 err = kvaser_usb_init_one(intf, id, i);
1617 if (err) {
1618 kvaser_usb_remove_interfaces(dev);
1619 return err;
1620 }
1621 }
1622
1623 return 0;
1624}
1625
1626static void kvaser_usb_disconnect(struct usb_interface *intf)
1627{
1628 struct kvaser_usb *dev = usb_get_intfdata(intf);
1629
1630 usb_set_intfdata(intf, NULL);
1631
1632 if (!dev)
1633 return;
1634
1635 kvaser_usb_remove_interfaces(dev);
1636}
1637
1638static struct usb_driver kvaser_usb_driver = {
1639 .name = "kvaser_usb",
1640 .probe = kvaser_usb_probe,
1641 .disconnect = kvaser_usb_disconnect,
1642 .id_table = kvaser_usb_table,
1643};
1644
1645module_usb_driver(kvaser_usb_driver);
1646
1647MODULE_AUTHOR("Olivier Sobrie <olivier@sobrie.be>");
1648MODULE_DESCRIPTION("CAN driver for Kvaser CAN/USB devices");
1649MODULE_LICENSE("GPL v2");
1650