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