• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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		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 #define USB_LEAF_LITE_V2_PRODUCT_ID		288
58 #define USB_MINI_PCIE_HS_PRODUCT_ID		289
59 #define USB_LEAF_LIGHT_HS_V2_OEM_PRODUCT_ID	290
60 #define USB_USBCAN_LIGHT_2HS_PRODUCT_ID		291
61 #define USB_MINI_PCIE_2HS_PRODUCT_ID		292
62 #define USB_USBCAN_R_V2_PRODUCT_ID		294
63 #define USB_LEAF_LIGHT_R_V2_PRODUCT_ID		295
64 #define USB_LEAF_LIGHT_HS_V2_OEM2_PRODUCT_ID	296
65 
66 /* Kvaser USBCan-II devices product ids */
67 #define USB_USBCAN_REVB_PRODUCT_ID		2
68 #define USB_VCI2_PRODUCT_ID			3
69 #define USB_USBCAN2_PRODUCT_ID			4
70 #define USB_MEMORATOR_PRODUCT_ID		5
71 
72 /* Kvaser Minihydra USB devices product ids */
73 #define USB_BLACKBIRD_V2_PRODUCT_ID		258
74 #define USB_MEMO_PRO_5HS_PRODUCT_ID		260
75 #define USB_USBCAN_PRO_5HS_PRODUCT_ID		261
76 #define USB_USBCAN_LIGHT_4HS_PRODUCT_ID		262
77 #define USB_LEAF_PRO_HS_V2_PRODUCT_ID		263
78 #define USB_USBCAN_PRO_2HS_V2_PRODUCT_ID	264
79 #define USB_MEMO_2HS_PRODUCT_ID			265
80 #define USB_MEMO_PRO_2HS_V2_PRODUCT_ID		266
81 #define USB_HYBRID_2CANLIN_PRODUCT_ID		267
82 #define USB_ATI_USBCAN_PRO_2HS_V2_PRODUCT_ID	268
83 #define USB_ATI_MEMO_PRO_2HS_V2_PRODUCT_ID	269
84 #define USB_HYBRID_PRO_2CANLIN_PRODUCT_ID	270
85 #define USB_U100_PRODUCT_ID			273
86 #define USB_U100P_PRODUCT_ID			274
87 #define USB_U100S_PRODUCT_ID			275
88 #define USB_USBCAN_PRO_4HS_PRODUCT_ID		276
89 #define USB_HYBRID_CANLIN_PRODUCT_ID		277
90 #define USB_HYBRID_PRO_CANLIN_PRODUCT_ID	278
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 
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 = -EOPNOTSUPP;
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 
585 	struct kvaser_usb_busparams busparams;
586 	int tseg1 = dbt->prop_seg + dbt->phase_seg1;
587 	int tseg2 = dbt->phase_seg2;
588 	int sjw = dbt->sjw;
589 	int err;
590 
591 	if (!ops->dev_set_data_bittiming ||
592 	    !ops->dev_get_data_busparams)
593 		return -EOPNOTSUPP;
594 
595 	busparams.bitrate = cpu_to_le32(dbt->bitrate);
596 	busparams.sjw = (u8)sjw;
597 	busparams.tseg1 = (u8)tseg1;
598 	busparams.tseg2 = (u8)tseg2;
599 	busparams.nsamples = 1;
600 
601 	err = ops->dev_set_data_bittiming(netdev, &busparams);
602 	if (err)
603 		return err;
604 
605 	err = kvaser_usb_setup_rx_urbs(priv->dev);
606 	if (err)
607 		return err;
608 
609 	err = ops->dev_get_data_busparams(priv);
610 	if (err)
611 		return err;
612 
613 	if (memcmp(&busparams, &priv->busparams_data,
614 		   sizeof(priv->busparams_data)) != 0)
615 		err = -EINVAL;
616 
617 	return err;
618 }
619 
kvaser_usb_write_bulk_callback(struct urb * urb)620 static void kvaser_usb_write_bulk_callback(struct urb *urb)
621 {
622 	struct kvaser_usb_tx_urb_context *context = urb->context;
623 	struct kvaser_usb_net_priv *priv;
624 	struct net_device *netdev;
625 
626 	if (WARN_ON(!context))
627 		return;
628 
629 	priv = context->priv;
630 	netdev = priv->netdev;
631 
632 	kfree(urb->transfer_buffer);
633 
634 	if (!netif_device_present(netdev))
635 		return;
636 
637 	if (urb->status)
638 		netdev_info(netdev, "Tx URB aborted (%d)\n", urb->status);
639 }
640 
kvaser_usb_start_xmit(struct sk_buff * skb,struct net_device * netdev)641 static netdev_tx_t kvaser_usb_start_xmit(struct sk_buff *skb,
642 					 struct net_device *netdev)
643 {
644 	struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
645 	struct kvaser_usb *dev = priv->dev;
646 	const struct kvaser_usb_dev_ops *ops = dev->driver_info->ops;
647 	struct net_device_stats *stats = &netdev->stats;
648 	struct kvaser_usb_tx_urb_context *context = NULL;
649 	struct urb *urb;
650 	void *buf;
651 	int cmd_len = 0;
652 	int err, ret = NETDEV_TX_OK;
653 	unsigned int i;
654 	unsigned long flags;
655 
656 	if (can_dev_dropped_skb(netdev, skb))
657 		return NETDEV_TX_OK;
658 
659 	urb = usb_alloc_urb(0, GFP_ATOMIC);
660 	if (!urb) {
661 		stats->tx_dropped++;
662 		dev_kfree_skb(skb);
663 		return NETDEV_TX_OK;
664 	}
665 
666 	spin_lock_irqsave(&priv->tx_contexts_lock, flags);
667 	for (i = 0; i < dev->max_tx_urbs; i++) {
668 		if (priv->tx_contexts[i].echo_index == dev->max_tx_urbs) {
669 			context = &priv->tx_contexts[i];
670 
671 			context->echo_index = i;
672 			++priv->active_tx_contexts;
673 			if (priv->active_tx_contexts >= (int)dev->max_tx_urbs)
674 				netif_stop_queue(netdev);
675 
676 			break;
677 		}
678 	}
679 	spin_unlock_irqrestore(&priv->tx_contexts_lock, flags);
680 
681 	/* This should never happen; it implies a flow control bug */
682 	if (!context) {
683 		netdev_warn(netdev, "cannot find free context\n");
684 
685 		ret = NETDEV_TX_BUSY;
686 		goto freeurb;
687 	}
688 
689 	buf = ops->dev_frame_to_cmd(priv, skb, &cmd_len, context->echo_index);
690 	if (!buf) {
691 		stats->tx_dropped++;
692 		dev_kfree_skb(skb);
693 		spin_lock_irqsave(&priv->tx_contexts_lock, flags);
694 
695 		context->echo_index = dev->max_tx_urbs;
696 		--priv->active_tx_contexts;
697 		netif_wake_queue(netdev);
698 
699 		spin_unlock_irqrestore(&priv->tx_contexts_lock, flags);
700 		goto freeurb;
701 	}
702 
703 	context->priv = priv;
704 
705 	can_put_echo_skb(skb, netdev, context->echo_index, 0);
706 
707 	usb_fill_bulk_urb(urb, dev->udev,
708 			  usb_sndbulkpipe(dev->udev,
709 					  dev->bulk_out->bEndpointAddress),
710 			  buf, cmd_len, kvaser_usb_write_bulk_callback,
711 			  context);
712 	usb_anchor_urb(urb, &priv->tx_submitted);
713 
714 	err = usb_submit_urb(urb, GFP_ATOMIC);
715 	if (unlikely(err)) {
716 		spin_lock_irqsave(&priv->tx_contexts_lock, flags);
717 
718 		can_free_echo_skb(netdev, context->echo_index, NULL);
719 		context->echo_index = dev->max_tx_urbs;
720 		--priv->active_tx_contexts;
721 		netif_wake_queue(netdev);
722 
723 		spin_unlock_irqrestore(&priv->tx_contexts_lock, flags);
724 
725 		usb_unanchor_urb(urb);
726 		kfree(buf);
727 
728 		stats->tx_dropped++;
729 
730 		if (err == -ENODEV)
731 			netif_device_detach(netdev);
732 		else
733 			netdev_warn(netdev, "Failed tx_urb %d\n", err);
734 
735 		goto freeurb;
736 	}
737 
738 	ret = NETDEV_TX_OK;
739 
740 freeurb:
741 	usb_free_urb(urb);
742 	return ret;
743 }
744 
745 static const struct net_device_ops kvaser_usb_netdev_ops = {
746 	.ndo_open = kvaser_usb_open,
747 	.ndo_stop = kvaser_usb_close,
748 	.ndo_start_xmit = kvaser_usb_start_xmit,
749 	.ndo_change_mtu = can_change_mtu,
750 };
751 
752 static const struct net_device_ops kvaser_usb_netdev_ops_hwts = {
753 	.ndo_open = kvaser_usb_open,
754 	.ndo_stop = kvaser_usb_close,
755 	.ndo_eth_ioctl = can_eth_ioctl_hwts,
756 	.ndo_start_xmit = kvaser_usb_start_xmit,
757 	.ndo_change_mtu = can_change_mtu,
758 };
759 
760 static const struct ethtool_ops kvaser_usb_ethtool_ops = {
761 	.get_ts_info = ethtool_op_get_ts_info,
762 };
763 
764 static const struct ethtool_ops kvaser_usb_ethtool_ops_hwts = {
765 	.get_ts_info = can_ethtool_op_get_ts_info_hwts,
766 };
767 
kvaser_usb_remove_interfaces(struct kvaser_usb * dev)768 static void kvaser_usb_remove_interfaces(struct kvaser_usb *dev)
769 {
770 	const struct kvaser_usb_dev_ops *ops = dev->driver_info->ops;
771 	int i;
772 
773 	for (i = 0; i < dev->nchannels; i++) {
774 		if (!dev->nets[i])
775 			continue;
776 
777 		unregister_candev(dev->nets[i]->netdev);
778 	}
779 
780 	kvaser_usb_unlink_all_urbs(dev);
781 
782 	for (i = 0; i < dev->nchannels; i++) {
783 		if (!dev->nets[i])
784 			continue;
785 
786 		if (ops->dev_remove_channel)
787 			ops->dev_remove_channel(dev->nets[i]);
788 
789 		free_candev(dev->nets[i]->netdev);
790 	}
791 }
792 
kvaser_usb_init_one(struct kvaser_usb * dev,int channel)793 static int kvaser_usb_init_one(struct kvaser_usb *dev, int channel)
794 {
795 	struct net_device *netdev;
796 	struct kvaser_usb_net_priv *priv;
797 	const struct kvaser_usb_driver_info *driver_info = dev->driver_info;
798 	const struct kvaser_usb_dev_ops *ops = driver_info->ops;
799 	int err;
800 
801 	if (ops->dev_reset_chip) {
802 		err = ops->dev_reset_chip(dev, channel);
803 		if (err)
804 			return err;
805 	}
806 
807 	netdev = alloc_candev(struct_size(priv, tx_contexts, dev->max_tx_urbs),
808 			      dev->max_tx_urbs);
809 	if (!netdev) {
810 		dev_err(&dev->intf->dev, "Cannot alloc candev\n");
811 		return -ENOMEM;
812 	}
813 
814 	priv = netdev_priv(netdev);
815 
816 	init_usb_anchor(&priv->tx_submitted);
817 	init_completion(&priv->start_comp);
818 	init_completion(&priv->stop_comp);
819 	init_completion(&priv->flush_comp);
820 	init_completion(&priv->get_busparams_comp);
821 	priv->can.ctrlmode_supported = 0;
822 
823 	priv->dev = dev;
824 	priv->netdev = netdev;
825 	priv->channel = channel;
826 
827 	spin_lock_init(&priv->tx_contexts_lock);
828 	kvaser_usb_reset_tx_urb_contexts(priv);
829 
830 	priv->can.state = CAN_STATE_STOPPED;
831 	priv->can.clock.freq = dev->cfg->clock.freq;
832 	priv->can.bittiming_const = dev->cfg->bittiming_const;
833 	priv->can.do_set_bittiming = kvaser_usb_set_bittiming;
834 	priv->can.do_set_mode = ops->dev_set_mode;
835 	if ((driver_info->quirks & KVASER_USB_QUIRK_HAS_TXRX_ERRORS) ||
836 	    (priv->dev->card_data.capabilities & KVASER_USB_CAP_BERR_CAP))
837 		priv->can.do_get_berr_counter = ops->dev_get_berr_counter;
838 	if (driver_info->quirks & KVASER_USB_QUIRK_HAS_SILENT_MODE)
839 		priv->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY;
840 
841 	priv->can.ctrlmode_supported |= dev->card_data.ctrlmode_supported;
842 
843 	if (priv->can.ctrlmode_supported & CAN_CTRLMODE_FD) {
844 		priv->can.data_bittiming_const = dev->cfg->data_bittiming_const;
845 		priv->can.do_set_data_bittiming = kvaser_usb_set_data_bittiming;
846 	}
847 
848 	netdev->flags |= IFF_ECHO;
849 
850 	netdev->netdev_ops = &kvaser_usb_netdev_ops;
851 	if (driver_info->quirks & KVASER_USB_QUIRK_HAS_HARDWARE_TIMESTAMP) {
852 		netdev->netdev_ops = &kvaser_usb_netdev_ops_hwts;
853 		netdev->ethtool_ops = &kvaser_usb_ethtool_ops_hwts;
854 	} else {
855 		netdev->netdev_ops = &kvaser_usb_netdev_ops;
856 		netdev->ethtool_ops = &kvaser_usb_ethtool_ops;
857 	}
858 	SET_NETDEV_DEV(netdev, &dev->intf->dev);
859 	netdev->dev_id = channel;
860 
861 	dev->nets[channel] = priv;
862 
863 	if (ops->dev_init_channel) {
864 		err = ops->dev_init_channel(priv);
865 		if (err)
866 			goto err;
867 	}
868 
869 	err = register_candev(netdev);
870 	if (err) {
871 		dev_err(&dev->intf->dev, "Failed to register CAN device\n");
872 		goto err;
873 	}
874 
875 	netdev_dbg(netdev, "device registered\n");
876 
877 	return 0;
878 
879 err:
880 	free_candev(netdev);
881 	dev->nets[channel] = NULL;
882 	return err;
883 }
884 
kvaser_usb_probe(struct usb_interface * intf,const struct usb_device_id * id)885 static int kvaser_usb_probe(struct usb_interface *intf,
886 			    const struct usb_device_id *id)
887 {
888 	struct kvaser_usb *dev;
889 	int err;
890 	int i;
891 	const struct kvaser_usb_driver_info *driver_info;
892 	const struct kvaser_usb_dev_ops *ops;
893 
894 	driver_info = (const struct kvaser_usb_driver_info *)id->driver_info;
895 	if (!driver_info)
896 		return -ENODEV;
897 
898 	dev = devm_kzalloc(&intf->dev, sizeof(*dev), GFP_KERNEL);
899 	if (!dev)
900 		return -ENOMEM;
901 
902 	dev->intf = intf;
903 	dev->driver_info = driver_info;
904 	ops = driver_info->ops;
905 
906 	err = ops->dev_setup_endpoints(dev);
907 	if (err) {
908 		dev_err(&intf->dev, "Cannot get usb endpoint(s)");
909 		return err;
910 	}
911 
912 	dev->udev = interface_to_usbdev(intf);
913 
914 	init_usb_anchor(&dev->rx_submitted);
915 
916 	usb_set_intfdata(intf, dev);
917 
918 	dev->card_data.ctrlmode_supported = 0;
919 	dev->card_data.capabilities = 0;
920 	err = ops->dev_init_card(dev);
921 	if (err) {
922 		dev_err(&intf->dev,
923 			"Failed to initialize card, error %d\n", err);
924 		return err;
925 	}
926 
927 	err = ops->dev_get_software_info(dev);
928 	if (err) {
929 		dev_err(&intf->dev,
930 			"Cannot get software info, error %d\n", err);
931 		return err;
932 	}
933 
934 	if (ops->dev_get_software_details) {
935 		err = ops->dev_get_software_details(dev);
936 		if (err) {
937 			dev_err(&intf->dev,
938 				"Cannot get software details, error %d\n", err);
939 			return err;
940 		}
941 	}
942 
943 	if (WARN_ON(!dev->cfg))
944 		return -ENODEV;
945 
946 	dev_dbg(&intf->dev, "Firmware version: %d.%d.%d\n",
947 		((dev->fw_version >> 24) & 0xff),
948 		((dev->fw_version >> 16) & 0xff),
949 		(dev->fw_version & 0xffff));
950 
951 	dev_dbg(&intf->dev, "Max outstanding tx = %d URBs\n", dev->max_tx_urbs);
952 
953 	err = ops->dev_get_card_info(dev);
954 	if (err) {
955 		dev_err(&intf->dev, "Cannot get card info, error %d\n", err);
956 		return err;
957 	}
958 
959 	if (ops->dev_get_capabilities) {
960 		err = ops->dev_get_capabilities(dev);
961 		if (err) {
962 			dev_err(&intf->dev,
963 				"Cannot get capabilities, error %d\n", err);
964 			kvaser_usb_remove_interfaces(dev);
965 			return err;
966 		}
967 	}
968 
969 	for (i = 0; i < dev->nchannels; i++) {
970 		err = kvaser_usb_init_one(dev, i);
971 		if (err) {
972 			kvaser_usb_remove_interfaces(dev);
973 			return err;
974 		}
975 	}
976 
977 	return 0;
978 }
979 
kvaser_usb_disconnect(struct usb_interface * intf)980 static void kvaser_usb_disconnect(struct usb_interface *intf)
981 {
982 	struct kvaser_usb *dev = usb_get_intfdata(intf);
983 
984 	usb_set_intfdata(intf, NULL);
985 
986 	if (!dev)
987 		return;
988 
989 	kvaser_usb_remove_interfaces(dev);
990 }
991 
992 static struct usb_driver kvaser_usb_driver = {
993 	.name = KBUILD_MODNAME,
994 	.probe = kvaser_usb_probe,
995 	.disconnect = kvaser_usb_disconnect,
996 	.id_table = kvaser_usb_table,
997 };
998 
999 module_usb_driver(kvaser_usb_driver);
1000 
1001 MODULE_AUTHOR("Olivier Sobrie <olivier@sobrie.be>");
1002 MODULE_AUTHOR("Kvaser AB <support@kvaser.com>");
1003 MODULE_DESCRIPTION("CAN driver for Kvaser CAN/USB devices");
1004 MODULE_LICENSE("GPL v2");
1005