This source file includes following definitions.
- kvaser_is_leaf
- kvaser_is_usbcan
- kvaser_is_hydra
- kvaser_usb_send_cmd
- kvaser_usb_recv_cmd
- kvaser_usb_send_cmd_callback
- kvaser_usb_send_cmd_async
- kvaser_usb_can_rx_over_error
- kvaser_usb_read_bulk_callback
- kvaser_usb_setup_rx_urbs
- kvaser_usb_open
- kvaser_usb_reset_tx_urb_contexts
- kvaser_usb_unlink_tx_urbs
- kvaser_usb_unlink_all_urbs
- kvaser_usb_close
- kvaser_usb_write_bulk_callback
- kvaser_usb_start_xmit
- kvaser_usb_remove_interfaces
- kvaser_usb_init_one
- kvaser_usb_probe
- kvaser_usb_disconnect
1
2
3
4
5
6
7
8
9
10
11
12
13
14 #include <linux/completion.h>
15 #include <linux/device.h>
16 #include <linux/gfp.h>
17 #include <linux/if.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/netdevice.h>
21 #include <linux/spinlock.h>
22 #include <linux/types.h>
23 #include <linux/usb.h>
24
25 #include <linux/can.h>
26 #include <linux/can/dev.h>
27 #include <linux/can/error.h>
28 #include <linux/can/netlink.h>
29
30 #include "kvaser_usb.h"
31
32
33 #define KVASER_VENDOR_ID 0x0bfd
34
35
36 #define USB_LEAF_DEVEL_PRODUCT_ID 10
37 #define USB_LEAF_LITE_PRODUCT_ID 11
38 #define USB_LEAF_PRO_PRODUCT_ID 12
39 #define USB_LEAF_SPRO_PRODUCT_ID 14
40 #define USB_LEAF_PRO_LS_PRODUCT_ID 15
41 #define USB_LEAF_PRO_SWC_PRODUCT_ID 16
42 #define USB_LEAF_PRO_LIN_PRODUCT_ID 17
43 #define USB_LEAF_SPRO_LS_PRODUCT_ID 18
44 #define USB_LEAF_SPRO_SWC_PRODUCT_ID 19
45 #define USB_MEMO2_DEVEL_PRODUCT_ID 22
46 #define USB_MEMO2_HSHS_PRODUCT_ID 23
47 #define USB_UPRO_HSHS_PRODUCT_ID 24
48 #define USB_LEAF_LITE_GI_PRODUCT_ID 25
49 #define USB_LEAF_PRO_OBDII_PRODUCT_ID 26
50 #define USB_MEMO2_HSLS_PRODUCT_ID 27
51 #define USB_LEAF_LITE_CH_PRODUCT_ID 28
52 #define USB_BLACKBIRD_SPRO_PRODUCT_ID 29
53 #define USB_OEM_MERCURY_PRODUCT_ID 34
54 #define USB_OEM_LEAF_PRODUCT_ID 35
55 #define USB_CAN_R_PRODUCT_ID 39
56 #define USB_LEAF_LITE_V2_PRODUCT_ID 288
57 #define USB_MINI_PCIE_HS_PRODUCT_ID 289
58 #define USB_LEAF_LIGHT_HS_V2_OEM_PRODUCT_ID 290
59 #define USB_USBCAN_LIGHT_2HS_PRODUCT_ID 291
60 #define USB_MINI_PCIE_2HS_PRODUCT_ID 292
61
62
63 #define USB_USBCAN_REVB_PRODUCT_ID 2
64 #define USB_VCI2_PRODUCT_ID 3
65 #define USB_USBCAN2_PRODUCT_ID 4
66 #define USB_MEMORATOR_PRODUCT_ID 5
67
68
69 #define USB_BLACKBIRD_V2_PRODUCT_ID 258
70 #define USB_MEMO_PRO_5HS_PRODUCT_ID 260
71 #define USB_USBCAN_PRO_5HS_PRODUCT_ID 261
72 #define USB_USBCAN_LIGHT_4HS_PRODUCT_ID 262
73 #define USB_LEAF_PRO_HS_V2_PRODUCT_ID 263
74 #define USB_USBCAN_PRO_2HS_V2_PRODUCT_ID 264
75 #define USB_MEMO_2HS_PRODUCT_ID 265
76 #define USB_MEMO_PRO_2HS_V2_PRODUCT_ID 266
77 #define USB_HYBRID_CANLIN_PRODUCT_ID 267
78 #define USB_ATI_USBCAN_PRO_2HS_V2_PRODUCT_ID 268
79 #define USB_ATI_MEMO_PRO_2HS_V2_PRODUCT_ID 269
80 #define USB_HYBRID_PRO_CANLIN_PRODUCT_ID 270
81
82 static inline bool kvaser_is_leaf(const struct usb_device_id *id)
83 {
84 return (id->idProduct >= USB_LEAF_DEVEL_PRODUCT_ID &&
85 id->idProduct <= USB_CAN_R_PRODUCT_ID) ||
86 (id->idProduct >= USB_LEAF_LITE_V2_PRODUCT_ID &&
87 id->idProduct <= USB_MINI_PCIE_2HS_PRODUCT_ID);
88 }
89
90 static inline bool kvaser_is_usbcan(const struct usb_device_id *id)
91 {
92 return id->idProduct >= USB_USBCAN_REVB_PRODUCT_ID &&
93 id->idProduct <= USB_MEMORATOR_PRODUCT_ID;
94 }
95
96 static inline bool kvaser_is_hydra(const struct usb_device_id *id)
97 {
98 return id->idProduct >= USB_BLACKBIRD_V2_PRODUCT_ID &&
99 id->idProduct <= USB_HYBRID_PRO_CANLIN_PRODUCT_ID;
100 }
101
102 static const struct usb_device_id kvaser_usb_table[] = {
103
104 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_DEVEL_PRODUCT_ID) },
105 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_PRODUCT_ID) },
106 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_PRODUCT_ID),
107 .driver_info = KVASER_USB_HAS_TXRX_ERRORS |
108 KVASER_USB_HAS_SILENT_MODE },
109 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_PRODUCT_ID),
110 .driver_info = KVASER_USB_HAS_TXRX_ERRORS |
111 KVASER_USB_HAS_SILENT_MODE },
112 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_LS_PRODUCT_ID),
113 .driver_info = KVASER_USB_HAS_TXRX_ERRORS |
114 KVASER_USB_HAS_SILENT_MODE },
115 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_SWC_PRODUCT_ID),
116 .driver_info = KVASER_USB_HAS_TXRX_ERRORS |
117 KVASER_USB_HAS_SILENT_MODE },
118 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_LIN_PRODUCT_ID),
119 .driver_info = KVASER_USB_HAS_TXRX_ERRORS |
120 KVASER_USB_HAS_SILENT_MODE },
121 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_LS_PRODUCT_ID),
122 .driver_info = KVASER_USB_HAS_TXRX_ERRORS |
123 KVASER_USB_HAS_SILENT_MODE },
124 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_SWC_PRODUCT_ID),
125 .driver_info = KVASER_USB_HAS_TXRX_ERRORS |
126 KVASER_USB_HAS_SILENT_MODE },
127 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_DEVEL_PRODUCT_ID),
128 .driver_info = KVASER_USB_HAS_TXRX_ERRORS |
129 KVASER_USB_HAS_SILENT_MODE },
130 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_HSHS_PRODUCT_ID),
131 .driver_info = KVASER_USB_HAS_TXRX_ERRORS |
132 KVASER_USB_HAS_SILENT_MODE },
133 { USB_DEVICE(KVASER_VENDOR_ID, USB_UPRO_HSHS_PRODUCT_ID),
134 .driver_info = KVASER_USB_HAS_TXRX_ERRORS },
135 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_GI_PRODUCT_ID) },
136 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_OBDII_PRODUCT_ID),
137 .driver_info = KVASER_USB_HAS_TXRX_ERRORS |
138 KVASER_USB_HAS_SILENT_MODE },
139 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_HSLS_PRODUCT_ID),
140 .driver_info = KVASER_USB_HAS_TXRX_ERRORS },
141 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_CH_PRODUCT_ID),
142 .driver_info = KVASER_USB_HAS_TXRX_ERRORS },
143 { USB_DEVICE(KVASER_VENDOR_ID, USB_BLACKBIRD_SPRO_PRODUCT_ID),
144 .driver_info = KVASER_USB_HAS_TXRX_ERRORS },
145 { USB_DEVICE(KVASER_VENDOR_ID, USB_OEM_MERCURY_PRODUCT_ID),
146 .driver_info = KVASER_USB_HAS_TXRX_ERRORS },
147 { USB_DEVICE(KVASER_VENDOR_ID, USB_OEM_LEAF_PRODUCT_ID),
148 .driver_info = KVASER_USB_HAS_TXRX_ERRORS },
149 { USB_DEVICE(KVASER_VENDOR_ID, USB_CAN_R_PRODUCT_ID),
150 .driver_info = KVASER_USB_HAS_TXRX_ERRORS },
151 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_V2_PRODUCT_ID) },
152 { USB_DEVICE(KVASER_VENDOR_ID, USB_MINI_PCIE_HS_PRODUCT_ID) },
153 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LIGHT_HS_V2_OEM_PRODUCT_ID) },
154 { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN_LIGHT_2HS_PRODUCT_ID) },
155 { USB_DEVICE(KVASER_VENDOR_ID, USB_MINI_PCIE_2HS_PRODUCT_ID) },
156
157
158 { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN2_PRODUCT_ID),
159 .driver_info = KVASER_USB_HAS_TXRX_ERRORS },
160 { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN_REVB_PRODUCT_ID),
161 .driver_info = KVASER_USB_HAS_TXRX_ERRORS },
162 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMORATOR_PRODUCT_ID),
163 .driver_info = KVASER_USB_HAS_TXRX_ERRORS },
164 { USB_DEVICE(KVASER_VENDOR_ID, USB_VCI2_PRODUCT_ID),
165 .driver_info = KVASER_USB_HAS_TXRX_ERRORS },
166
167
168 { USB_DEVICE(KVASER_VENDOR_ID, USB_BLACKBIRD_V2_PRODUCT_ID) },
169 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO_PRO_5HS_PRODUCT_ID) },
170 { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN_PRO_5HS_PRODUCT_ID) },
171 { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN_LIGHT_4HS_PRODUCT_ID) },
172 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_HS_V2_PRODUCT_ID) },
173 { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN_PRO_2HS_V2_PRODUCT_ID) },
174 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO_2HS_PRODUCT_ID) },
175 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO_PRO_2HS_V2_PRODUCT_ID) },
176 { USB_DEVICE(KVASER_VENDOR_ID, USB_HYBRID_CANLIN_PRODUCT_ID) },
177 { USB_DEVICE(KVASER_VENDOR_ID, USB_ATI_USBCAN_PRO_2HS_V2_PRODUCT_ID) },
178 { USB_DEVICE(KVASER_VENDOR_ID, USB_ATI_MEMO_PRO_2HS_V2_PRODUCT_ID) },
179 { USB_DEVICE(KVASER_VENDOR_ID, USB_HYBRID_PRO_CANLIN_PRODUCT_ID) },
180 { }
181 };
182 MODULE_DEVICE_TABLE(usb, kvaser_usb_table);
183
184 int kvaser_usb_send_cmd(const struct kvaser_usb *dev, void *cmd, int len)
185 {
186 int actual_len;
187
188 return usb_bulk_msg(dev->udev,
189 usb_sndbulkpipe(dev->udev,
190 dev->bulk_out->bEndpointAddress),
191 cmd, len, &actual_len, KVASER_USB_TIMEOUT);
192 }
193
194 int kvaser_usb_recv_cmd(const struct kvaser_usb *dev, void *cmd, int len,
195 int *actual_len)
196 {
197 return usb_bulk_msg(dev->udev,
198 usb_rcvbulkpipe(dev->udev,
199 dev->bulk_in->bEndpointAddress),
200 cmd, len, actual_len, KVASER_USB_TIMEOUT);
201 }
202
203 static void kvaser_usb_send_cmd_callback(struct urb *urb)
204 {
205 struct net_device *netdev = urb->context;
206
207 kfree(urb->transfer_buffer);
208
209 if (urb->status)
210 netdev_warn(netdev, "urb status received: %d\n", urb->status);
211 }
212
213 int kvaser_usb_send_cmd_async(struct kvaser_usb_net_priv *priv, void *cmd,
214 int len)
215 {
216 struct kvaser_usb *dev = priv->dev;
217 struct net_device *netdev = priv->netdev;
218 struct urb *urb;
219 int err;
220
221 urb = usb_alloc_urb(0, GFP_ATOMIC);
222 if (!urb)
223 return -ENOMEM;
224
225 usb_fill_bulk_urb(urb, dev->udev,
226 usb_sndbulkpipe(dev->udev,
227 dev->bulk_out->bEndpointAddress),
228 cmd, len, kvaser_usb_send_cmd_callback, netdev);
229 usb_anchor_urb(urb, &priv->tx_submitted);
230
231 err = usb_submit_urb(urb, GFP_ATOMIC);
232 if (err) {
233 netdev_err(netdev, "Error transmitting URB\n");
234 usb_unanchor_urb(urb);
235 }
236 usb_free_urb(urb);
237
238 return 0;
239 }
240
241 int kvaser_usb_can_rx_over_error(struct net_device *netdev)
242 {
243 struct net_device_stats *stats = &netdev->stats;
244 struct can_frame *cf;
245 struct sk_buff *skb;
246
247 stats->rx_over_errors++;
248 stats->rx_errors++;
249
250 skb = alloc_can_err_skb(netdev, &cf);
251 if (!skb) {
252 stats->rx_dropped++;
253 netdev_warn(netdev, "No memory left for err_skb\n");
254 return -ENOMEM;
255 }
256
257 cf->can_id |= CAN_ERR_CRTL;
258 cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
259
260 stats->rx_packets++;
261 stats->rx_bytes += cf->can_dlc;
262 netif_rx(skb);
263
264 return 0;
265 }
266
267 static void kvaser_usb_read_bulk_callback(struct urb *urb)
268 {
269 struct kvaser_usb *dev = urb->context;
270 int err;
271 unsigned int i;
272
273 switch (urb->status) {
274 case 0:
275 break;
276 case -ENOENT:
277 case -EPIPE:
278 case -EPROTO:
279 case -ESHUTDOWN:
280 return;
281 default:
282 dev_info(&dev->intf->dev, "Rx URB aborted (%d)\n", urb->status);
283 goto resubmit_urb;
284 }
285
286 dev->ops->dev_read_bulk_callback(dev, urb->transfer_buffer,
287 urb->actual_length);
288
289 resubmit_urb:
290 usb_fill_bulk_urb(urb, dev->udev,
291 usb_rcvbulkpipe(dev->udev,
292 dev->bulk_in->bEndpointAddress),
293 urb->transfer_buffer, KVASER_USB_RX_BUFFER_SIZE,
294 kvaser_usb_read_bulk_callback, dev);
295
296 err = usb_submit_urb(urb, GFP_ATOMIC);
297 if (err == -ENODEV) {
298 for (i = 0; i < dev->nchannels; i++) {
299 if (!dev->nets[i])
300 continue;
301
302 netif_device_detach(dev->nets[i]->netdev);
303 }
304 } else if (err) {
305 dev_err(&dev->intf->dev,
306 "Failed resubmitting read bulk urb: %d\n", err);
307 }
308 }
309
310 static int kvaser_usb_setup_rx_urbs(struct kvaser_usb *dev)
311 {
312 int i, err = 0;
313
314 if (dev->rxinitdone)
315 return 0;
316
317 for (i = 0; i < KVASER_USB_MAX_RX_URBS; i++) {
318 struct urb *urb = NULL;
319 u8 *buf = NULL;
320 dma_addr_t buf_dma;
321
322 urb = usb_alloc_urb(0, GFP_KERNEL);
323 if (!urb) {
324 err = -ENOMEM;
325 break;
326 }
327
328 buf = usb_alloc_coherent(dev->udev, KVASER_USB_RX_BUFFER_SIZE,
329 GFP_KERNEL, &buf_dma);
330 if (!buf) {
331 dev_warn(&dev->intf->dev,
332 "No memory left for USB buffer\n");
333 usb_free_urb(urb);
334 err = -ENOMEM;
335 break;
336 }
337
338 usb_fill_bulk_urb(urb, dev->udev,
339 usb_rcvbulkpipe
340 (dev->udev,
341 dev->bulk_in->bEndpointAddress),
342 buf, KVASER_USB_RX_BUFFER_SIZE,
343 kvaser_usb_read_bulk_callback, dev);
344 urb->transfer_dma = buf_dma;
345 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
346 usb_anchor_urb(urb, &dev->rx_submitted);
347
348 err = usb_submit_urb(urb, GFP_KERNEL);
349 if (err) {
350 usb_unanchor_urb(urb);
351 usb_free_coherent(dev->udev,
352 KVASER_USB_RX_BUFFER_SIZE, buf,
353 buf_dma);
354 usb_free_urb(urb);
355 break;
356 }
357
358 dev->rxbuf[i] = buf;
359 dev->rxbuf_dma[i] = buf_dma;
360
361 usb_free_urb(urb);
362 }
363
364 if (i == 0) {
365 dev_warn(&dev->intf->dev, "Cannot setup read URBs, error %d\n",
366 err);
367 return err;
368 } else if (i < KVASER_USB_MAX_RX_URBS) {
369 dev_warn(&dev->intf->dev, "RX performances may be slow\n");
370 }
371
372 dev->rxinitdone = true;
373
374 return 0;
375 }
376
377 static int kvaser_usb_open(struct net_device *netdev)
378 {
379 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
380 struct kvaser_usb *dev = priv->dev;
381 int err;
382
383 err = open_candev(netdev);
384 if (err)
385 return err;
386
387 err = kvaser_usb_setup_rx_urbs(dev);
388 if (err)
389 goto error;
390
391 err = dev->ops->dev_set_opt_mode(priv);
392 if (err)
393 goto error;
394
395 err = dev->ops->dev_start_chip(priv);
396 if (err) {
397 netdev_warn(netdev, "Cannot start device, error %d\n", err);
398 goto error;
399 }
400
401 priv->can.state = CAN_STATE_ERROR_ACTIVE;
402
403 return 0;
404
405 error:
406 close_candev(netdev);
407 return err;
408 }
409
410 static void kvaser_usb_reset_tx_urb_contexts(struct kvaser_usb_net_priv *priv)
411 {
412 int i, max_tx_urbs;
413
414 max_tx_urbs = priv->dev->max_tx_urbs;
415
416 priv->active_tx_contexts = 0;
417 for (i = 0; i < max_tx_urbs; i++)
418 priv->tx_contexts[i].echo_index = max_tx_urbs;
419 }
420
421
422
423
424 static void kvaser_usb_unlink_tx_urbs(struct kvaser_usb_net_priv *priv)
425 {
426 usb_kill_anchored_urbs(&priv->tx_submitted);
427 kvaser_usb_reset_tx_urb_contexts(priv);
428 }
429
430 static void kvaser_usb_unlink_all_urbs(struct kvaser_usb *dev)
431 {
432 int i;
433
434 usb_kill_anchored_urbs(&dev->rx_submitted);
435
436 for (i = 0; i < KVASER_USB_MAX_RX_URBS; i++)
437 usb_free_coherent(dev->udev, KVASER_USB_RX_BUFFER_SIZE,
438 dev->rxbuf[i], dev->rxbuf_dma[i]);
439
440 for (i = 0; i < dev->nchannels; i++) {
441 struct kvaser_usb_net_priv *priv = dev->nets[i];
442
443 if (priv)
444 kvaser_usb_unlink_tx_urbs(priv);
445 }
446 }
447
448 static int kvaser_usb_close(struct net_device *netdev)
449 {
450 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
451 struct kvaser_usb *dev = priv->dev;
452 int err;
453
454 netif_stop_queue(netdev);
455
456 err = dev->ops->dev_flush_queue(priv);
457 if (err)
458 netdev_warn(netdev, "Cannot flush queue, error %d\n", err);
459
460 if (dev->ops->dev_reset_chip) {
461 err = dev->ops->dev_reset_chip(dev, priv->channel);
462 if (err)
463 netdev_warn(netdev, "Cannot reset card, error %d\n",
464 err);
465 }
466
467 err = dev->ops->dev_stop_chip(priv);
468 if (err)
469 netdev_warn(netdev, "Cannot stop device, error %d\n", err);
470
471
472 kvaser_usb_unlink_tx_urbs(priv);
473
474 priv->can.state = CAN_STATE_STOPPED;
475 close_candev(priv->netdev);
476
477 return 0;
478 }
479
480 static void kvaser_usb_write_bulk_callback(struct urb *urb)
481 {
482 struct kvaser_usb_tx_urb_context *context = urb->context;
483 struct kvaser_usb_net_priv *priv;
484 struct net_device *netdev;
485
486 if (WARN_ON(!context))
487 return;
488
489 priv = context->priv;
490 netdev = priv->netdev;
491
492 kfree(urb->transfer_buffer);
493
494 if (!netif_device_present(netdev))
495 return;
496
497 if (urb->status)
498 netdev_info(netdev, "Tx URB aborted (%d)\n", urb->status);
499 }
500
501 static netdev_tx_t kvaser_usb_start_xmit(struct sk_buff *skb,
502 struct net_device *netdev)
503 {
504 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
505 struct kvaser_usb *dev = priv->dev;
506 struct net_device_stats *stats = &netdev->stats;
507 struct kvaser_usb_tx_urb_context *context = NULL;
508 struct urb *urb;
509 void *buf;
510 int cmd_len = 0;
511 int err, ret = NETDEV_TX_OK;
512 unsigned int i;
513 unsigned long flags;
514
515 if (can_dropped_invalid_skb(netdev, skb))
516 return NETDEV_TX_OK;
517
518 urb = usb_alloc_urb(0, GFP_ATOMIC);
519 if (!urb) {
520 stats->tx_dropped++;
521 dev_kfree_skb(skb);
522 return NETDEV_TX_OK;
523 }
524
525 spin_lock_irqsave(&priv->tx_contexts_lock, flags);
526 for (i = 0; i < dev->max_tx_urbs; i++) {
527 if (priv->tx_contexts[i].echo_index == dev->max_tx_urbs) {
528 context = &priv->tx_contexts[i];
529
530 context->echo_index = i;
531 ++priv->active_tx_contexts;
532 if (priv->active_tx_contexts >= (int)dev->max_tx_urbs)
533 netif_stop_queue(netdev);
534
535 break;
536 }
537 }
538 spin_unlock_irqrestore(&priv->tx_contexts_lock, flags);
539
540
541 if (!context) {
542 netdev_warn(netdev, "cannot find free context\n");
543
544 ret = NETDEV_TX_BUSY;
545 goto freeurb;
546 }
547
548 buf = dev->ops->dev_frame_to_cmd(priv, skb, &context->dlc, &cmd_len,
549 context->echo_index);
550 if (!buf) {
551 stats->tx_dropped++;
552 dev_kfree_skb(skb);
553 spin_lock_irqsave(&priv->tx_contexts_lock, flags);
554
555 context->echo_index = dev->max_tx_urbs;
556 --priv->active_tx_contexts;
557 netif_wake_queue(netdev);
558
559 spin_unlock_irqrestore(&priv->tx_contexts_lock, flags);
560 goto freeurb;
561 }
562
563 context->priv = priv;
564
565 can_put_echo_skb(skb, netdev, context->echo_index);
566
567 usb_fill_bulk_urb(urb, dev->udev,
568 usb_sndbulkpipe(dev->udev,
569 dev->bulk_out->bEndpointAddress),
570 buf, cmd_len, kvaser_usb_write_bulk_callback,
571 context);
572 usb_anchor_urb(urb, &priv->tx_submitted);
573
574 err = usb_submit_urb(urb, GFP_ATOMIC);
575 if (unlikely(err)) {
576 spin_lock_irqsave(&priv->tx_contexts_lock, flags);
577
578 can_free_echo_skb(netdev, context->echo_index);
579 context->echo_index = dev->max_tx_urbs;
580 --priv->active_tx_contexts;
581 netif_wake_queue(netdev);
582
583 spin_unlock_irqrestore(&priv->tx_contexts_lock, flags);
584
585 usb_unanchor_urb(urb);
586 kfree(buf);
587
588 stats->tx_dropped++;
589
590 if (err == -ENODEV)
591 netif_device_detach(netdev);
592 else
593 netdev_warn(netdev, "Failed tx_urb %d\n", err);
594
595 goto freeurb;
596 }
597
598 ret = NETDEV_TX_OK;
599
600 freeurb:
601 usb_free_urb(urb);
602 return ret;
603 }
604
605 static const struct net_device_ops kvaser_usb_netdev_ops = {
606 .ndo_open = kvaser_usb_open,
607 .ndo_stop = kvaser_usb_close,
608 .ndo_start_xmit = kvaser_usb_start_xmit,
609 .ndo_change_mtu = can_change_mtu,
610 };
611
612 static void kvaser_usb_remove_interfaces(struct kvaser_usb *dev)
613 {
614 int i;
615
616 for (i = 0; i < dev->nchannels; i++) {
617 if (!dev->nets[i])
618 continue;
619
620 unregister_candev(dev->nets[i]->netdev);
621 }
622
623 kvaser_usb_unlink_all_urbs(dev);
624
625 for (i = 0; i < dev->nchannels; i++) {
626 if (!dev->nets[i])
627 continue;
628
629 free_candev(dev->nets[i]->netdev);
630 }
631 }
632
633 static int kvaser_usb_init_one(struct kvaser_usb *dev,
634 const struct usb_device_id *id, int channel)
635 {
636 struct net_device *netdev;
637 struct kvaser_usb_net_priv *priv;
638 int err;
639
640 if (dev->ops->dev_reset_chip) {
641 err = dev->ops->dev_reset_chip(dev, channel);
642 if (err)
643 return err;
644 }
645
646 netdev = alloc_candev(struct_size(priv, tx_contexts, dev->max_tx_urbs),
647 dev->max_tx_urbs);
648 if (!netdev) {
649 dev_err(&dev->intf->dev, "Cannot alloc candev\n");
650 return -ENOMEM;
651 }
652
653 priv = netdev_priv(netdev);
654
655 init_usb_anchor(&priv->tx_submitted);
656 init_completion(&priv->start_comp);
657 init_completion(&priv->stop_comp);
658 priv->can.ctrlmode_supported = 0;
659
660 priv->dev = dev;
661 priv->netdev = netdev;
662 priv->channel = channel;
663
664 spin_lock_init(&priv->tx_contexts_lock);
665 kvaser_usb_reset_tx_urb_contexts(priv);
666
667 priv->can.state = CAN_STATE_STOPPED;
668 priv->can.clock.freq = dev->cfg->clock.freq;
669 priv->can.bittiming_const = dev->cfg->bittiming_const;
670 priv->can.do_set_bittiming = dev->ops->dev_set_bittiming;
671 priv->can.do_set_mode = dev->ops->dev_set_mode;
672 if ((id->driver_info & KVASER_USB_HAS_TXRX_ERRORS) ||
673 (priv->dev->card_data.capabilities & KVASER_USB_CAP_BERR_CAP))
674 priv->can.do_get_berr_counter = dev->ops->dev_get_berr_counter;
675 if (id->driver_info & KVASER_USB_HAS_SILENT_MODE)
676 priv->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY;
677
678 priv->can.ctrlmode_supported |= dev->card_data.ctrlmode_supported;
679
680 if (priv->can.ctrlmode_supported & CAN_CTRLMODE_FD) {
681 priv->can.data_bittiming_const = dev->cfg->data_bittiming_const;
682 priv->can.do_set_data_bittiming =
683 dev->ops->dev_set_data_bittiming;
684 }
685
686 netdev->flags |= IFF_ECHO;
687
688 netdev->netdev_ops = &kvaser_usb_netdev_ops;
689
690 SET_NETDEV_DEV(netdev, &dev->intf->dev);
691 netdev->dev_id = channel;
692
693 dev->nets[channel] = priv;
694
695 err = register_candev(netdev);
696 if (err) {
697 dev_err(&dev->intf->dev, "Failed to register CAN device\n");
698 free_candev(netdev);
699 dev->nets[channel] = NULL;
700 return err;
701 }
702
703 netdev_dbg(netdev, "device registered\n");
704
705 return 0;
706 }
707
708 static int kvaser_usb_probe(struct usb_interface *intf,
709 const struct usb_device_id *id)
710 {
711 struct kvaser_usb *dev;
712 int err;
713 int i;
714
715 dev = devm_kzalloc(&intf->dev, sizeof(*dev), GFP_KERNEL);
716 if (!dev)
717 return -ENOMEM;
718
719 if (kvaser_is_leaf(id)) {
720 dev->card_data.leaf.family = KVASER_LEAF;
721 dev->ops = &kvaser_usb_leaf_dev_ops;
722 } else if (kvaser_is_usbcan(id)) {
723 dev->card_data.leaf.family = KVASER_USBCAN;
724 dev->ops = &kvaser_usb_leaf_dev_ops;
725 } else if (kvaser_is_hydra(id)) {
726 dev->ops = &kvaser_usb_hydra_dev_ops;
727 } else {
728 dev_err(&intf->dev,
729 "Product ID (%d) is not a supported Kvaser USB device\n",
730 id->idProduct);
731 return -ENODEV;
732 }
733
734 dev->intf = intf;
735
736 err = dev->ops->dev_setup_endpoints(dev);
737 if (err) {
738 dev_err(&intf->dev, "Cannot get usb endpoint(s)");
739 return err;
740 }
741
742 dev->udev = interface_to_usbdev(intf);
743
744 init_usb_anchor(&dev->rx_submitted);
745
746 usb_set_intfdata(intf, dev);
747
748 dev->card_data.ctrlmode_supported = 0;
749 dev->card_data.capabilities = 0;
750 err = dev->ops->dev_init_card(dev);
751 if (err) {
752 dev_err(&intf->dev,
753 "Failed to initialize card, error %d\n", err);
754 return err;
755 }
756
757 err = dev->ops->dev_get_software_info(dev);
758 if (err) {
759 dev_err(&intf->dev,
760 "Cannot get software info, error %d\n", err);
761 return err;
762 }
763
764 if (dev->ops->dev_get_software_details) {
765 err = dev->ops->dev_get_software_details(dev);
766 if (err) {
767 dev_err(&intf->dev,
768 "Cannot get software details, error %d\n", err);
769 return err;
770 }
771 }
772
773 if (WARN_ON(!dev->cfg))
774 return -ENODEV;
775
776 dev_dbg(&intf->dev, "Firmware version: %d.%d.%d\n",
777 ((dev->fw_version >> 24) & 0xff),
778 ((dev->fw_version >> 16) & 0xff),
779 (dev->fw_version & 0xffff));
780
781 dev_dbg(&intf->dev, "Max outstanding tx = %d URBs\n", dev->max_tx_urbs);
782
783 err = dev->ops->dev_get_card_info(dev);
784 if (err) {
785 dev_err(&intf->dev, "Cannot get card info, error %d\n", err);
786 return err;
787 }
788
789 if (dev->ops->dev_get_capabilities) {
790 err = dev->ops->dev_get_capabilities(dev);
791 if (err) {
792 dev_err(&intf->dev,
793 "Cannot get capabilities, error %d\n", err);
794 kvaser_usb_remove_interfaces(dev);
795 return err;
796 }
797 }
798
799 for (i = 0; i < dev->nchannels; i++) {
800 err = kvaser_usb_init_one(dev, id, i);
801 if (err) {
802 kvaser_usb_remove_interfaces(dev);
803 return err;
804 }
805 }
806
807 return 0;
808 }
809
810 static void kvaser_usb_disconnect(struct usb_interface *intf)
811 {
812 struct kvaser_usb *dev = usb_get_intfdata(intf);
813
814 usb_set_intfdata(intf, NULL);
815
816 if (!dev)
817 return;
818
819 kvaser_usb_remove_interfaces(dev);
820 }
821
822 static struct usb_driver kvaser_usb_driver = {
823 .name = "kvaser_usb",
824 .probe = kvaser_usb_probe,
825 .disconnect = kvaser_usb_disconnect,
826 .id_table = kvaser_usb_table,
827 };
828
829 module_usb_driver(kvaser_usb_driver);
830
831 MODULE_AUTHOR("Olivier Sobrie <olivier@sobrie.be>");
832 MODULE_AUTHOR("Kvaser AB <support@kvaser.com>");
833 MODULE_DESCRIPTION("CAN driver for Kvaser CAN/USB devices");
834 MODULE_LICENSE("GPL v2");