1 // SPDX-License-Identifier: GPL-2.0
2 /* Parts of this driver are based on the following:
3 * - Kvaser linux leaf driver (version 4.78)
4 * - CAN driver for esd CAN-USB/2
5 * - Kvaser linux usbcanII driver (version 5.3)
6 * - Kvaser linux mhydra driver (version 5.24)
7 *
8 * Copyright (C) 2002-2018 KVASER AB, Sweden. All rights reserved.
9 * Copyright (C) 2010 Matthias Fuchs <matthias.fuchs@esd.eu>, esd gmbh
10 * Copyright (C) 2012 Olivier Sobrie <olivier@sobrie.be>
11 * Copyright (C) 2015 Valeo S.A.
12 */
13
14 #include <linux/completion.h>
15 #include <linux/device.h>
16 #include <linux/ethtool.h>
17 #include <linux/gfp.h>
18 #include <linux/if.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/netdevice.h>
22 #include <linux/spinlock.h>
23 #include <linux/types.h>
24 #include <linux/usb.h>
25
26 #include <linux/can.h>
27 #include <linux/can/dev.h>
28 #include <linux/can/error.h>
29 #include <linux/can/netlink.h>
30
31 #include "kvaser_usb.h"
32
33 /* Kvaser USB vendor id. */
34 #define KVASER_VENDOR_ID 0x0bfd
35
36 /* Kvaser Leaf USB devices product ids */
37 #define USB_LEAF_DEVEL_PRODUCT_ID 0x000a
38 #define USB_LEAF_LITE_PRODUCT_ID 0x000b
39 #define USB_LEAF_PRO_PRODUCT_ID 0x000c
40 #define USB_LEAF_SPRO_PRODUCT_ID 0x000e
41 #define USB_LEAF_PRO_LS_PRODUCT_ID 0x000f
42 #define USB_LEAF_PRO_SWC_PRODUCT_ID 0x0010
43 #define USB_LEAF_PRO_LIN_PRODUCT_ID 0x0011
44 #define USB_LEAF_SPRO_LS_PRODUCT_ID 0x0012
45 #define USB_LEAF_SPRO_SWC_PRODUCT_ID 0x0013
46 #define USB_MEMO2_DEVEL_PRODUCT_ID 0x0016
47 #define USB_MEMO2_HSHS_PRODUCT_ID 0x0017
48 #define USB_UPRO_HSHS_PRODUCT_ID 0x0018
49 #define USB_LEAF_LITE_GI_PRODUCT_ID 0x0019
50 #define USB_LEAF_PRO_OBDII_PRODUCT_ID 0x001a
51 #define USB_MEMO2_HSLS_PRODUCT_ID 0x001b
52 #define USB_LEAF_LITE_CH_PRODUCT_ID 0x001c
53 #define USB_BLACKBIRD_SPRO_PRODUCT_ID 0x001d
54 #define USB_OEM_MERCURY_PRODUCT_ID 0x0022
55 #define USB_OEM_LEAF_PRODUCT_ID 0x0023
56 #define USB_CAN_R_PRODUCT_ID 0x0027
57 #define USB_LEAF_LITE_V2_PRODUCT_ID 0x0120
58 #define USB_MINI_PCIE_HS_PRODUCT_ID 0x0121
59 #define USB_LEAF_LIGHT_HS_V2_OEM_PRODUCT_ID 0x0122
60 #define USB_USBCAN_LIGHT_2HS_PRODUCT_ID 0x0123
61 #define USB_MINI_PCIE_2HS_PRODUCT_ID 0x0124
62 #define USB_USBCAN_R_V2_PRODUCT_ID 0x0126
63 #define USB_LEAF_LIGHT_R_V2_PRODUCT_ID 0x0127
64 #define USB_LEAF_LIGHT_HS_V2_OEM2_PRODUCT_ID 0x0128
65
66 /* Kvaser USBCan-II devices product ids */
67 #define USB_USBCAN_REVB_PRODUCT_ID 0x0002
68 #define USB_VCI2_PRODUCT_ID 0x0003
69 #define USB_USBCAN2_PRODUCT_ID 0x0004
70 #define USB_MEMORATOR_PRODUCT_ID 0x0005
71
72 /* Kvaser Minihydra USB devices product ids */
73 #define USB_BLACKBIRD_V2_PRODUCT_ID 0x0102
74 #define USB_MEMO_PRO_5HS_PRODUCT_ID 0x0104
75 #define USB_USBCAN_PRO_5HS_PRODUCT_ID 0x0105
76 #define USB_USBCAN_LIGHT_4HS_PRODUCT_ID 0x0106
77 #define USB_LEAF_PRO_HS_V2_PRODUCT_ID 0x0107
78 #define USB_USBCAN_PRO_2HS_V2_PRODUCT_ID 0x0108
79 #define USB_MEMO_2HS_PRODUCT_ID 0x0109
80 #define USB_MEMO_PRO_2HS_V2_PRODUCT_ID 0x010a
81 #define USB_HYBRID_2CANLIN_PRODUCT_ID 0x010b
82 #define USB_ATI_USBCAN_PRO_2HS_V2_PRODUCT_ID 0x010c
83 #define USB_ATI_MEMO_PRO_2HS_V2_PRODUCT_ID 0x010d
84 #define USB_HYBRID_PRO_2CANLIN_PRODUCT_ID 0x010e
85 #define USB_U100_PRODUCT_ID 0x0111
86 #define USB_U100P_PRODUCT_ID 0x0112
87 #define USB_U100S_PRODUCT_ID 0x0113
88 #define USB_USBCAN_PRO_4HS_PRODUCT_ID 0x0114
89 #define USB_HYBRID_CANLIN_PRODUCT_ID 0x0115
90 #define USB_HYBRID_PRO_CANLIN_PRODUCT_ID 0x0116
91
92 static const struct kvaser_usb_driver_info kvaser_usb_driver_info_hydra = {
93 .quirks = KVASER_USB_QUIRK_HAS_HARDWARE_TIMESTAMP,
94 .ops = &kvaser_usb_hydra_dev_ops,
95 };
96
97 static const struct kvaser_usb_driver_info kvaser_usb_driver_info_usbcan = {
98 .quirks = KVASER_USB_QUIRK_HAS_TXRX_ERRORS |
99 KVASER_USB_QUIRK_HAS_SILENT_MODE,
100 .family = KVASER_USBCAN,
101 .ops = &kvaser_usb_leaf_dev_ops,
102 };
103
104 static const struct kvaser_usb_driver_info kvaser_usb_driver_info_leaf = {
105 .quirks = KVASER_USB_QUIRK_IGNORE_CLK_FREQ,
106 .family = KVASER_LEAF,
107 .ops = &kvaser_usb_leaf_dev_ops,
108 };
109
110 static const struct kvaser_usb_driver_info kvaser_usb_driver_info_leaf_err = {
111 .quirks = KVASER_USB_QUIRK_HAS_TXRX_ERRORS |
112 KVASER_USB_QUIRK_IGNORE_CLK_FREQ,
113 .family = KVASER_LEAF,
114 .ops = &kvaser_usb_leaf_dev_ops,
115 };
116
117 static const struct kvaser_usb_driver_info kvaser_usb_driver_info_leaf_err_listen = {
118 .quirks = KVASER_USB_QUIRK_HAS_TXRX_ERRORS |
119 KVASER_USB_QUIRK_HAS_SILENT_MODE |
120 KVASER_USB_QUIRK_IGNORE_CLK_FREQ,
121 .family = KVASER_LEAF,
122 .ops = &kvaser_usb_leaf_dev_ops,
123 };
124
125 static const struct kvaser_usb_driver_info kvaser_usb_driver_info_leafimx = {
126 .quirks = 0,
127 .ops = &kvaser_usb_leaf_dev_ops,
128 };
129
130 static const struct usb_device_id kvaser_usb_table[] = {
131 /* Leaf M32C USB product IDs */
132 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_DEVEL_PRODUCT_ID),
133 .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf },
134 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_PRODUCT_ID),
135 .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf },
136 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_PRODUCT_ID),
137 .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf_err_listen },
138 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_PRODUCT_ID),
139 .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf_err_listen },
140 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_LS_PRODUCT_ID),
141 .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf_err_listen },
142 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_SWC_PRODUCT_ID),
143 .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf_err_listen },
144 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_LIN_PRODUCT_ID),
145 .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf_err_listen },
146 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_LS_PRODUCT_ID),
147 .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf_err_listen },
148 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_SWC_PRODUCT_ID),
149 .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf_err_listen },
150 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_DEVEL_PRODUCT_ID),
151 .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf_err_listen },
152 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_HSHS_PRODUCT_ID),
153 .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf_err_listen },
154 { USB_DEVICE(KVASER_VENDOR_ID, USB_UPRO_HSHS_PRODUCT_ID),
155 .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf_err },
156 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_GI_PRODUCT_ID),
157 .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf },
158 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_OBDII_PRODUCT_ID),
159 .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf_err_listen },
160 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_HSLS_PRODUCT_ID),
161 .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf_err },
162 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_CH_PRODUCT_ID),
163 .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf_err },
164 { USB_DEVICE(KVASER_VENDOR_ID, USB_BLACKBIRD_SPRO_PRODUCT_ID),
165 .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf_err },
166 { USB_DEVICE(KVASER_VENDOR_ID, USB_OEM_MERCURY_PRODUCT_ID),
167 .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf_err },
168 { USB_DEVICE(KVASER_VENDOR_ID, USB_OEM_LEAF_PRODUCT_ID),
169 .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf_err },
170 { USB_DEVICE(KVASER_VENDOR_ID, USB_CAN_R_PRODUCT_ID),
171 .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf_err },
172
173 /* Leaf i.MX28 USB product IDs */
174 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_V2_PRODUCT_ID),
175 .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leafimx },
176 { USB_DEVICE(KVASER_VENDOR_ID, USB_MINI_PCIE_HS_PRODUCT_ID),
177 .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leafimx },
178 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LIGHT_HS_V2_OEM_PRODUCT_ID),
179 .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leafimx },
180 { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN_LIGHT_2HS_PRODUCT_ID),
181 .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leafimx },
182 { USB_DEVICE(KVASER_VENDOR_ID, USB_MINI_PCIE_2HS_PRODUCT_ID),
183 .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leafimx },
184 { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN_R_V2_PRODUCT_ID),
185 .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leafimx },
186 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LIGHT_R_V2_PRODUCT_ID),
187 .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leafimx },
188 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LIGHT_HS_V2_OEM2_PRODUCT_ID),
189 .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leafimx },
190
191 /* USBCANII USB product IDs */
192 { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN2_PRODUCT_ID),
193 .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_usbcan },
194 { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN_REVB_PRODUCT_ID),
195 .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_usbcan },
196 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMORATOR_PRODUCT_ID),
197 .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_usbcan },
198 { USB_DEVICE(KVASER_VENDOR_ID, USB_VCI2_PRODUCT_ID),
199 .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_usbcan },
200
201 /* Minihydra USB product IDs */
202 { USB_DEVICE(KVASER_VENDOR_ID, USB_BLACKBIRD_V2_PRODUCT_ID),
203 .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_hydra },
204 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO_PRO_5HS_PRODUCT_ID),
205 .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_hydra },
206 { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN_PRO_5HS_PRODUCT_ID),
207 .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_hydra },
208 { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN_LIGHT_4HS_PRODUCT_ID),
209 .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_hydra },
210 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_HS_V2_PRODUCT_ID),
211 .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_hydra },
212 { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN_PRO_2HS_V2_PRODUCT_ID),
213 .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_hydra },
214 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO_2HS_PRODUCT_ID),
215 .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_hydra },
216 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO_PRO_2HS_V2_PRODUCT_ID),
217 .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_hydra },
218 { USB_DEVICE(KVASER_VENDOR_ID, USB_HYBRID_2CANLIN_PRODUCT_ID),
219 .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_hydra },
220 { USB_DEVICE(KVASER_VENDOR_ID, USB_ATI_USBCAN_PRO_2HS_V2_PRODUCT_ID),
221 .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_hydra },
222 { USB_DEVICE(KVASER_VENDOR_ID, USB_ATI_MEMO_PRO_2HS_V2_PRODUCT_ID),
223 .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_hydra },
224 { USB_DEVICE(KVASER_VENDOR_ID, USB_HYBRID_PRO_2CANLIN_PRODUCT_ID),
225 .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_hydra },
226 { USB_DEVICE(KVASER_VENDOR_ID, USB_U100_PRODUCT_ID),
227 .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_hydra },
228 { USB_DEVICE(KVASER_VENDOR_ID, USB_U100P_PRODUCT_ID),
229 .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_hydra },
230 { USB_DEVICE(KVASER_VENDOR_ID, USB_U100S_PRODUCT_ID),
231 .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_hydra },
232 { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN_PRO_4HS_PRODUCT_ID),
233 .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_hydra },
234 { USB_DEVICE(KVASER_VENDOR_ID, USB_HYBRID_CANLIN_PRODUCT_ID),
235 .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_hydra },
236 { USB_DEVICE(KVASER_VENDOR_ID, USB_HYBRID_PRO_CANLIN_PRODUCT_ID),
237 .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_hydra },
238 { }
239 };
240 MODULE_DEVICE_TABLE(usb, kvaser_usb_table);
241
kvaser_usb_send_cmd(const struct kvaser_usb * dev,void * cmd,int len)242 int kvaser_usb_send_cmd(const struct kvaser_usb *dev, void *cmd, int len)
243 {
244 return usb_bulk_msg(dev->udev,
245 usb_sndbulkpipe(dev->udev,
246 dev->bulk_out->bEndpointAddress),
247 cmd, len, NULL, KVASER_USB_TIMEOUT);
248 }
249
kvaser_usb_recv_cmd(const struct kvaser_usb * dev,void * cmd,int len,int * actual_len)250 int kvaser_usb_recv_cmd(const struct kvaser_usb *dev, void *cmd, int len,
251 int *actual_len)
252 {
253 return usb_bulk_msg(dev->udev,
254 usb_rcvbulkpipe(dev->udev,
255 dev->bulk_in->bEndpointAddress),
256 cmd, len, actual_len, KVASER_USB_TIMEOUT);
257 }
258
kvaser_usb_send_cmd_callback(struct urb * urb)259 static void kvaser_usb_send_cmd_callback(struct urb *urb)
260 {
261 struct net_device *netdev = urb->context;
262
263 kfree(urb->transfer_buffer);
264
265 if (urb->status)
266 netdev_warn(netdev, "urb status received: %d\n", urb->status);
267 }
268
kvaser_usb_send_cmd_async(struct kvaser_usb_net_priv * priv,void * cmd,int len)269 int kvaser_usb_send_cmd_async(struct kvaser_usb_net_priv *priv, void *cmd,
270 int len)
271 {
272 struct kvaser_usb *dev = priv->dev;
273 struct net_device *netdev = priv->netdev;
274 struct urb *urb;
275 int err;
276
277 urb = usb_alloc_urb(0, GFP_ATOMIC);
278 if (!urb)
279 return -ENOMEM;
280
281 usb_fill_bulk_urb(urb, dev->udev,
282 usb_sndbulkpipe(dev->udev,
283 dev->bulk_out->bEndpointAddress),
284 cmd, len, kvaser_usb_send_cmd_callback, netdev);
285 usb_anchor_urb(urb, &priv->tx_submitted);
286
287 err = usb_submit_urb(urb, GFP_ATOMIC);
288 if (err) {
289 netdev_err(netdev, "Error transmitting URB\n");
290 usb_unanchor_urb(urb);
291 }
292 usb_free_urb(urb);
293
294 return 0;
295 }
296
kvaser_usb_can_rx_over_error(struct net_device * netdev)297 int kvaser_usb_can_rx_over_error(struct net_device *netdev)
298 {
299 struct net_device_stats *stats = &netdev->stats;
300 struct can_frame *cf;
301 struct sk_buff *skb;
302
303 stats->rx_over_errors++;
304 stats->rx_errors++;
305
306 skb = alloc_can_err_skb(netdev, &cf);
307 if (!skb) {
308 stats->rx_dropped++;
309 netdev_warn(netdev, "No memory left for err_skb\n");
310 return -ENOMEM;
311 }
312
313 cf->can_id |= CAN_ERR_CRTL;
314 cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
315
316 netif_rx(skb);
317
318 return 0;
319 }
320
kvaser_usb_read_bulk_callback(struct urb * urb)321 static void kvaser_usb_read_bulk_callback(struct urb *urb)
322 {
323 struct kvaser_usb *dev = urb->context;
324 const struct kvaser_usb_dev_ops *ops = dev->driver_info->ops;
325 int err;
326 unsigned int i;
327
328 switch (urb->status) {
329 case 0:
330 break;
331 case -ENOENT:
332 case -EPIPE:
333 case -EPROTO:
334 case -ESHUTDOWN:
335 return;
336 default:
337 dev_info(&dev->intf->dev, "Rx URB aborted (%d)\n", urb->status);
338 goto resubmit_urb;
339 }
340
341 ops->dev_read_bulk_callback(dev, urb->transfer_buffer,
342 urb->actual_length);
343
344 resubmit_urb:
345 usb_fill_bulk_urb(urb, dev->udev,
346 usb_rcvbulkpipe(dev->udev,
347 dev->bulk_in->bEndpointAddress),
348 urb->transfer_buffer, KVASER_USB_RX_BUFFER_SIZE,
349 kvaser_usb_read_bulk_callback, dev);
350
351 err = usb_submit_urb(urb, GFP_ATOMIC);
352 if (err == -ENODEV) {
353 for (i = 0; i < dev->nchannels; i++) {
354 if (!dev->nets[i])
355 continue;
356
357 netif_device_detach(dev->nets[i]->netdev);
358 }
359 } else if (err) {
360 dev_err(&dev->intf->dev,
361 "Failed resubmitting read bulk urb: %d\n", err);
362 }
363 }
364
kvaser_usb_setup_rx_urbs(struct kvaser_usb * dev)365 static int kvaser_usb_setup_rx_urbs(struct kvaser_usb *dev)
366 {
367 int i, err = 0;
368
369 if (dev->rxinitdone)
370 return 0;
371
372 for (i = 0; i < KVASER_USB_MAX_RX_URBS; i++) {
373 struct urb *urb = NULL;
374 u8 *buf = NULL;
375 dma_addr_t buf_dma;
376
377 urb = usb_alloc_urb(0, GFP_KERNEL);
378 if (!urb) {
379 err = -ENOMEM;
380 break;
381 }
382
383 buf = usb_alloc_coherent(dev->udev, KVASER_USB_RX_BUFFER_SIZE,
384 GFP_KERNEL, &buf_dma);
385 if (!buf) {
386 dev_warn(&dev->intf->dev,
387 "No memory left for USB buffer\n");
388 usb_free_urb(urb);
389 err = -ENOMEM;
390 break;
391 }
392
393 usb_fill_bulk_urb(urb, dev->udev,
394 usb_rcvbulkpipe
395 (dev->udev,
396 dev->bulk_in->bEndpointAddress),
397 buf, KVASER_USB_RX_BUFFER_SIZE,
398 kvaser_usb_read_bulk_callback, dev);
399 urb->transfer_dma = buf_dma;
400 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
401 usb_anchor_urb(urb, &dev->rx_submitted);
402
403 err = usb_submit_urb(urb, GFP_KERNEL);
404 if (err) {
405 usb_unanchor_urb(urb);
406 usb_free_coherent(dev->udev,
407 KVASER_USB_RX_BUFFER_SIZE, buf,
408 buf_dma);
409 usb_free_urb(urb);
410 break;
411 }
412
413 dev->rxbuf[i] = buf;
414 dev->rxbuf_dma[i] = buf_dma;
415
416 usb_free_urb(urb);
417 }
418
419 if (i == 0) {
420 dev_warn(&dev->intf->dev, "Cannot setup read URBs, error %d\n",
421 err);
422 return err;
423 } else if (i < KVASER_USB_MAX_RX_URBS) {
424 dev_warn(&dev->intf->dev, "RX performances may be slow\n");
425 }
426
427 dev->rxinitdone = true;
428
429 return 0;
430 }
431
kvaser_usb_open(struct net_device * netdev)432 static int kvaser_usb_open(struct net_device *netdev)
433 {
434 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
435 struct kvaser_usb *dev = priv->dev;
436 const struct kvaser_usb_dev_ops *ops = dev->driver_info->ops;
437 int err;
438
439 err = open_candev(netdev);
440 if (err)
441 return err;
442
443 err = ops->dev_set_opt_mode(priv);
444 if (err)
445 goto error;
446
447 err = ops->dev_start_chip(priv);
448 if (err) {
449 netdev_warn(netdev, "Cannot start device, error %d\n", err);
450 goto error;
451 }
452
453 priv->can.state = CAN_STATE_ERROR_ACTIVE;
454
455 return 0;
456
457 error:
458 close_candev(netdev);
459 return err;
460 }
461
kvaser_usb_reset_tx_urb_contexts(struct kvaser_usb_net_priv * priv)462 static void kvaser_usb_reset_tx_urb_contexts(struct kvaser_usb_net_priv *priv)
463 {
464 int i, max_tx_urbs;
465
466 max_tx_urbs = priv->dev->max_tx_urbs;
467
468 priv->active_tx_contexts = 0;
469 for (i = 0; i < max_tx_urbs; i++)
470 priv->tx_contexts[i].echo_index = max_tx_urbs;
471 }
472
473 /* This method might sleep. Do not call it in the atomic context
474 * of URB completions.
475 */
kvaser_usb_unlink_tx_urbs(struct kvaser_usb_net_priv * priv)476 void kvaser_usb_unlink_tx_urbs(struct kvaser_usb_net_priv *priv)
477 {
478 usb_kill_anchored_urbs(&priv->tx_submitted);
479 kvaser_usb_reset_tx_urb_contexts(priv);
480 }
481
kvaser_usb_unlink_all_urbs(struct kvaser_usb * dev)482 static void kvaser_usb_unlink_all_urbs(struct kvaser_usb *dev)
483 {
484 int i;
485
486 usb_kill_anchored_urbs(&dev->rx_submitted);
487
488 for (i = 0; i < KVASER_USB_MAX_RX_URBS; i++)
489 usb_free_coherent(dev->udev, KVASER_USB_RX_BUFFER_SIZE,
490 dev->rxbuf[i], dev->rxbuf_dma[i]);
491
492 for (i = 0; i < dev->nchannels; i++) {
493 struct kvaser_usb_net_priv *priv = dev->nets[i];
494
495 if (priv)
496 kvaser_usb_unlink_tx_urbs(priv);
497 }
498 }
499
kvaser_usb_close(struct net_device * netdev)500 static int kvaser_usb_close(struct net_device *netdev)
501 {
502 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
503 struct kvaser_usb *dev = priv->dev;
504 const struct kvaser_usb_dev_ops *ops = dev->driver_info->ops;
505 int err;
506
507 netif_stop_queue(netdev);
508
509 err = ops->dev_flush_queue(priv);
510 if (err)
511 netdev_warn(netdev, "Cannot flush queue, error %d\n", err);
512
513 if (ops->dev_reset_chip) {
514 err = ops->dev_reset_chip(dev, priv->channel);
515 if (err)
516 netdev_warn(netdev, "Cannot reset card, error %d\n",
517 err);
518 }
519
520 err = ops->dev_stop_chip(priv);
521 if (err)
522 netdev_warn(netdev, "Cannot stop device, error %d\n", err);
523
524 /* reset tx contexts */
525 kvaser_usb_unlink_tx_urbs(priv);
526
527 priv->can.state = CAN_STATE_STOPPED;
528 close_candev(priv->netdev);
529
530 return 0;
531 }
532
kvaser_usb_set_bittiming(struct net_device * netdev)533 static int kvaser_usb_set_bittiming(struct net_device *netdev)
534 {
535 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
536 struct kvaser_usb *dev = priv->dev;
537 const struct kvaser_usb_dev_ops *ops = dev->driver_info->ops;
538 struct can_bittiming *bt = &priv->can.bittiming;
539 struct kvaser_usb_busparams busparams;
540 int tseg1 = bt->prop_seg + bt->phase_seg1;
541 int tseg2 = bt->phase_seg2;
542 int sjw = bt->sjw;
543 int err;
544
545 busparams.bitrate = cpu_to_le32(bt->bitrate);
546 busparams.sjw = (u8)sjw;
547 busparams.tseg1 = (u8)tseg1;
548 busparams.tseg2 = (u8)tseg2;
549 if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
550 busparams.nsamples = 3;
551 else
552 busparams.nsamples = 1;
553
554 err = ops->dev_set_bittiming(netdev, &busparams);
555 if (err)
556 return err;
557
558 err = kvaser_usb_setup_rx_urbs(priv->dev);
559 if (err)
560 return err;
561
562 err = ops->dev_get_busparams(priv);
563 if (err) {
564 /* Treat EOPNOTSUPP as success */
565 if (err == -EOPNOTSUPP)
566 err = 0;
567 return err;
568 }
569
570 if (memcmp(&busparams, &priv->busparams_nominal,
571 sizeof(priv->busparams_nominal)) != 0)
572 err = -EINVAL;
573
574 return err;
575 }
576
kvaser_usb_set_data_bittiming(struct net_device * netdev)577 static int kvaser_usb_set_data_bittiming(struct net_device *netdev)
578 {
579 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
580 struct kvaser_usb *dev = priv->dev;
581 const struct kvaser_usb_dev_ops *ops = dev->driver_info->ops;
582 struct can_bittiming *dbt = &priv->can.data_bittiming;
583 struct kvaser_usb_busparams busparams;
584 int tseg1 = dbt->prop_seg + dbt->phase_seg1;
585 int tseg2 = dbt->phase_seg2;
586 int sjw = dbt->sjw;
587 int err;
588
589 if (!ops->dev_set_data_bittiming ||
590 !ops->dev_get_data_busparams)
591 return -EOPNOTSUPP;
592
593 busparams.bitrate = cpu_to_le32(dbt->bitrate);
594 busparams.sjw = (u8)sjw;
595 busparams.tseg1 = (u8)tseg1;
596 busparams.tseg2 = (u8)tseg2;
597 busparams.nsamples = 1;
598
599 err = ops->dev_set_data_bittiming(netdev, &busparams);
600 if (err)
601 return err;
602
603 err = kvaser_usb_setup_rx_urbs(priv->dev);
604 if (err)
605 return err;
606
607 err = ops->dev_get_data_busparams(priv);
608 if (err)
609 return err;
610
611 if (memcmp(&busparams, &priv->busparams_data,
612 sizeof(priv->busparams_data)) != 0)
613 err = -EINVAL;
614
615 return err;
616 }
617
kvaser_usb_write_bulk_callback(struct urb * urb)618 static void kvaser_usb_write_bulk_callback(struct urb *urb)
619 {
620 struct kvaser_usb_tx_urb_context *context = urb->context;
621 struct kvaser_usb_net_priv *priv;
622 struct net_device *netdev;
623
624 if (WARN_ON(!context))
625 return;
626
627 priv = context->priv;
628 netdev = priv->netdev;
629
630 kfree(urb->transfer_buffer);
631
632 if (!netif_device_present(netdev))
633 return;
634
635 if (urb->status)
636 netdev_info(netdev, "Tx URB aborted (%d)\n", urb->status);
637 }
638
kvaser_usb_start_xmit(struct sk_buff * skb,struct net_device * netdev)639 static netdev_tx_t kvaser_usb_start_xmit(struct sk_buff *skb,
640 struct net_device *netdev)
641 {
642 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
643 struct kvaser_usb *dev = priv->dev;
644 const struct kvaser_usb_dev_ops *ops = dev->driver_info->ops;
645 struct net_device_stats *stats = &netdev->stats;
646 struct kvaser_usb_tx_urb_context *context = NULL;
647 struct urb *urb;
648 void *buf;
649 int cmd_len = 0;
650 int err, ret = NETDEV_TX_OK;
651 unsigned int i;
652 unsigned long flags;
653
654 if (can_dev_dropped_skb(netdev, skb))
655 return NETDEV_TX_OK;
656
657 urb = usb_alloc_urb(0, GFP_ATOMIC);
658 if (!urb) {
659 stats->tx_dropped++;
660 dev_kfree_skb(skb);
661 return NETDEV_TX_OK;
662 }
663
664 spin_lock_irqsave(&priv->tx_contexts_lock, flags);
665 for (i = 0; i < dev->max_tx_urbs; i++) {
666 if (priv->tx_contexts[i].echo_index == dev->max_tx_urbs) {
667 context = &priv->tx_contexts[i];
668
669 context->echo_index = i;
670 ++priv->active_tx_contexts;
671 if (priv->active_tx_contexts >= (int)dev->max_tx_urbs)
672 netif_stop_queue(netdev);
673
674 break;
675 }
676 }
677 spin_unlock_irqrestore(&priv->tx_contexts_lock, flags);
678
679 /* This should never happen; it implies a flow control bug */
680 if (!context) {
681 netdev_warn(netdev, "cannot find free context\n");
682
683 ret = NETDEV_TX_BUSY;
684 goto freeurb;
685 }
686
687 buf = ops->dev_frame_to_cmd(priv, skb, &cmd_len, context->echo_index);
688 if (!buf) {
689 stats->tx_dropped++;
690 dev_kfree_skb(skb);
691 spin_lock_irqsave(&priv->tx_contexts_lock, flags);
692
693 context->echo_index = dev->max_tx_urbs;
694 --priv->active_tx_contexts;
695 netif_wake_queue(netdev);
696
697 spin_unlock_irqrestore(&priv->tx_contexts_lock, flags);
698 goto freeurb;
699 }
700
701 context->priv = priv;
702
703 can_put_echo_skb(skb, netdev, context->echo_index, 0);
704
705 usb_fill_bulk_urb(urb, dev->udev,
706 usb_sndbulkpipe(dev->udev,
707 dev->bulk_out->bEndpointAddress),
708 buf, cmd_len, kvaser_usb_write_bulk_callback,
709 context);
710 usb_anchor_urb(urb, &priv->tx_submitted);
711
712 err = usb_submit_urb(urb, GFP_ATOMIC);
713 if (unlikely(err)) {
714 spin_lock_irqsave(&priv->tx_contexts_lock, flags);
715
716 can_free_echo_skb(netdev, context->echo_index, NULL);
717 context->echo_index = dev->max_tx_urbs;
718 --priv->active_tx_contexts;
719 netif_wake_queue(netdev);
720
721 spin_unlock_irqrestore(&priv->tx_contexts_lock, flags);
722
723 usb_unanchor_urb(urb);
724 kfree(buf);
725
726 stats->tx_dropped++;
727
728 if (err == -ENODEV)
729 netif_device_detach(netdev);
730 else
731 netdev_warn(netdev, "Failed tx_urb %d\n", err);
732
733 goto freeurb;
734 }
735
736 ret = NETDEV_TX_OK;
737
738 freeurb:
739 usb_free_urb(urb);
740 return ret;
741 }
742
743 static const struct net_device_ops kvaser_usb_netdev_ops = {
744 .ndo_open = kvaser_usb_open,
745 .ndo_stop = kvaser_usb_close,
746 .ndo_start_xmit = kvaser_usb_start_xmit,
747 .ndo_change_mtu = can_change_mtu,
748 };
749
750 static const struct net_device_ops kvaser_usb_netdev_ops_hwts = {
751 .ndo_open = kvaser_usb_open,
752 .ndo_stop = kvaser_usb_close,
753 .ndo_eth_ioctl = can_eth_ioctl_hwts,
754 .ndo_start_xmit = kvaser_usb_start_xmit,
755 .ndo_change_mtu = can_change_mtu,
756 };
757
758 static const struct ethtool_ops kvaser_usb_ethtool_ops = {
759 .get_ts_info = ethtool_op_get_ts_info,
760 };
761
762 static const struct ethtool_ops kvaser_usb_ethtool_ops_hwts = {
763 .get_ts_info = can_ethtool_op_get_ts_info_hwts,
764 };
765
kvaser_usb_remove_interfaces(struct kvaser_usb * dev)766 static void kvaser_usb_remove_interfaces(struct kvaser_usb *dev)
767 {
768 const struct kvaser_usb_dev_ops *ops = dev->driver_info->ops;
769 int i;
770
771 for (i = 0; i < dev->nchannels; i++) {
772 if (!dev->nets[i])
773 continue;
774
775 unregister_candev(dev->nets[i]->netdev);
776 }
777
778 kvaser_usb_unlink_all_urbs(dev);
779
780 for (i = 0; i < dev->nchannels; i++) {
781 if (!dev->nets[i])
782 continue;
783
784 if (ops->dev_remove_channel)
785 ops->dev_remove_channel(dev->nets[i]);
786
787 free_candev(dev->nets[i]->netdev);
788 }
789 }
790
kvaser_usb_init_one(struct kvaser_usb * dev,int channel)791 static int kvaser_usb_init_one(struct kvaser_usb *dev, int channel)
792 {
793 struct net_device *netdev;
794 struct kvaser_usb_net_priv *priv;
795 const struct kvaser_usb_driver_info *driver_info = dev->driver_info;
796 const struct kvaser_usb_dev_ops *ops = driver_info->ops;
797 int err;
798
799 if (ops->dev_reset_chip) {
800 err = ops->dev_reset_chip(dev, channel);
801 if (err)
802 return err;
803 }
804
805 netdev = alloc_candev(struct_size(priv, tx_contexts, dev->max_tx_urbs),
806 dev->max_tx_urbs);
807 if (!netdev) {
808 dev_err(&dev->intf->dev, "Cannot alloc candev\n");
809 return -ENOMEM;
810 }
811
812 priv = netdev_priv(netdev);
813
814 init_usb_anchor(&priv->tx_submitted);
815 init_completion(&priv->start_comp);
816 init_completion(&priv->stop_comp);
817 init_completion(&priv->flush_comp);
818 init_completion(&priv->get_busparams_comp);
819 priv->can.ctrlmode_supported = CAN_CTRLMODE_CC_LEN8_DLC;
820
821 priv->dev = dev;
822 priv->netdev = netdev;
823 priv->channel = channel;
824
825 spin_lock_init(&priv->tx_contexts_lock);
826 kvaser_usb_reset_tx_urb_contexts(priv);
827
828 priv->can.state = CAN_STATE_STOPPED;
829 priv->can.clock.freq = dev->cfg->clock.freq;
830 priv->can.bittiming_const = dev->cfg->bittiming_const;
831 priv->can.do_set_bittiming = kvaser_usb_set_bittiming;
832 priv->can.do_set_mode = ops->dev_set_mode;
833 if ((driver_info->quirks & KVASER_USB_QUIRK_HAS_TXRX_ERRORS) ||
834 (priv->dev->card_data.capabilities & KVASER_USB_CAP_BERR_CAP))
835 priv->can.do_get_berr_counter = ops->dev_get_berr_counter;
836 if (driver_info->quirks & KVASER_USB_QUIRK_HAS_SILENT_MODE)
837 priv->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY;
838
839 priv->can.ctrlmode_supported |= dev->card_data.ctrlmode_supported;
840
841 if (priv->can.ctrlmode_supported & CAN_CTRLMODE_FD) {
842 priv->can.data_bittiming_const = dev->cfg->data_bittiming_const;
843 priv->can.do_set_data_bittiming = kvaser_usb_set_data_bittiming;
844 }
845
846 netdev->flags |= IFF_ECHO;
847
848 netdev->netdev_ops = &kvaser_usb_netdev_ops;
849 if (driver_info->quirks & KVASER_USB_QUIRK_HAS_HARDWARE_TIMESTAMP) {
850 netdev->netdev_ops = &kvaser_usb_netdev_ops_hwts;
851 netdev->ethtool_ops = &kvaser_usb_ethtool_ops_hwts;
852 } else {
853 netdev->netdev_ops = &kvaser_usb_netdev_ops;
854 netdev->ethtool_ops = &kvaser_usb_ethtool_ops;
855 }
856 SET_NETDEV_DEV(netdev, &dev->intf->dev);
857 netdev->dev_id = channel;
858
859 dev->nets[channel] = priv;
860
861 if (ops->dev_init_channel) {
862 err = ops->dev_init_channel(priv);
863 if (err)
864 goto err;
865 }
866
867 err = register_candev(netdev);
868 if (err) {
869 dev_err(&dev->intf->dev, "Failed to register CAN device\n");
870 goto err;
871 }
872
873 netdev_dbg(netdev, "device registered\n");
874
875 return 0;
876
877 err:
878 free_candev(netdev);
879 dev->nets[channel] = NULL;
880 return err;
881 }
882
kvaser_usb_probe(struct usb_interface * intf,const struct usb_device_id * id)883 static int kvaser_usb_probe(struct usb_interface *intf,
884 const struct usb_device_id *id)
885 {
886 struct kvaser_usb *dev;
887 int err;
888 int i;
889 const struct kvaser_usb_driver_info *driver_info;
890 const struct kvaser_usb_dev_ops *ops;
891
892 driver_info = (const struct kvaser_usb_driver_info *)id->driver_info;
893 if (!driver_info)
894 return -ENODEV;
895
896 dev = devm_kzalloc(&intf->dev, sizeof(*dev), GFP_KERNEL);
897 if (!dev)
898 return -ENOMEM;
899
900 dev->intf = intf;
901 dev->driver_info = driver_info;
902 ops = driver_info->ops;
903
904 err = ops->dev_setup_endpoints(dev);
905 if (err) {
906 dev_err(&intf->dev, "Cannot get usb endpoint(s)");
907 return err;
908 }
909
910 dev->udev = interface_to_usbdev(intf);
911
912 init_usb_anchor(&dev->rx_submitted);
913
914 usb_set_intfdata(intf, dev);
915
916 dev->card_data.ctrlmode_supported = 0;
917 dev->card_data.capabilities = 0;
918 err = ops->dev_init_card(dev);
919 if (err) {
920 dev_err(&intf->dev,
921 "Failed to initialize card, error %d\n", err);
922 return err;
923 }
924
925 err = ops->dev_get_software_info(dev);
926 if (err) {
927 dev_err(&intf->dev,
928 "Cannot get software info, error %d\n", err);
929 return err;
930 }
931
932 if (ops->dev_get_software_details) {
933 err = ops->dev_get_software_details(dev);
934 if (err) {
935 dev_err(&intf->dev,
936 "Cannot get software details, error %d\n", err);
937 return err;
938 }
939 }
940
941 if (WARN_ON(!dev->cfg))
942 return -ENODEV;
943
944 dev_dbg(&intf->dev, "Firmware version: %d.%d.%d\n",
945 ((dev->fw_version >> 24) & 0xff),
946 ((dev->fw_version >> 16) & 0xff),
947 (dev->fw_version & 0xffff));
948
949 dev_dbg(&intf->dev, "Max outstanding tx = %d URBs\n", dev->max_tx_urbs);
950
951 err = ops->dev_get_card_info(dev);
952 if (err) {
953 dev_err(&intf->dev, "Cannot get card info, error %d\n", err);
954 return err;
955 }
956
957 if (ops->dev_get_capabilities) {
958 err = ops->dev_get_capabilities(dev);
959 if (err) {
960 dev_err(&intf->dev,
961 "Cannot get capabilities, error %d\n", err);
962 kvaser_usb_remove_interfaces(dev);
963 return err;
964 }
965 }
966
967 for (i = 0; i < dev->nchannels; i++) {
968 err = kvaser_usb_init_one(dev, i);
969 if (err) {
970 kvaser_usb_remove_interfaces(dev);
971 return err;
972 }
973 }
974
975 return 0;
976 }
977
kvaser_usb_disconnect(struct usb_interface * intf)978 static void kvaser_usb_disconnect(struct usb_interface *intf)
979 {
980 struct kvaser_usb *dev = usb_get_intfdata(intf);
981
982 usb_set_intfdata(intf, NULL);
983
984 if (!dev)
985 return;
986
987 kvaser_usb_remove_interfaces(dev);
988 }
989
990 static struct usb_driver kvaser_usb_driver = {
991 .name = KBUILD_MODNAME,
992 .probe = kvaser_usb_probe,
993 .disconnect = kvaser_usb_disconnect,
994 .id_table = kvaser_usb_table,
995 };
996
997 module_usb_driver(kvaser_usb_driver);
998
999 MODULE_AUTHOR("Olivier Sobrie <olivier@sobrie.be>");
1000 MODULE_AUTHOR("Kvaser AB <support@kvaser.com>");
1001 MODULE_DESCRIPTION("CAN driver for Kvaser CAN/USB devices");
1002 MODULE_LICENSE("GPL v2");
1003