xref: /linux/drivers/net/can/usb/peak_usb/pcan_usb_pro.c (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * CAN driver for PEAK System PCAN-USB Pro adapter
4  * Derived from the PCAN project file driver/src/pcan_usbpro.c
5  *
6  * Copyright (C) 2003-2011 PEAK System-Technik GmbH
7  * Copyright (C) 2011-2012 Stephane Grosjean <s.grosjean@peak-system.com>
8  */
9 #include <linux/ethtool.h>
10 #include <linux/module.h>
11 #include <linux/netdevice.h>
12 #include <linux/usb.h>
13 
14 #include <linux/can.h>
15 #include <linux/can/dev.h>
16 #include <linux/can/error.h>
17 
18 #include "pcan_usb_core.h"
19 #include "pcan_usb_pro.h"
20 
21 #define PCAN_USBPRO_CHANNEL_COUNT	2
22 
23 /* PCAN-USB Pro adapter internal clock (MHz) */
24 #define PCAN_USBPRO_CRYSTAL_HZ		56000000
25 
26 /* PCAN-USB Pro command timeout (ms.) */
27 #define PCAN_USBPRO_COMMAND_TIMEOUT	1000
28 
29 /* PCAN-USB Pro rx/tx buffers size */
30 #define PCAN_USBPRO_RX_BUFFER_SIZE	1024
31 #define PCAN_USBPRO_TX_BUFFER_SIZE	64
32 
33 #define PCAN_USBPRO_MSG_HEADER_LEN	4
34 
35 /* some commands responses need to be re-submitted */
36 #define PCAN_USBPRO_RSP_SUBMIT_MAX	2
37 
38 #define PCAN_USBPRO_RTR			0x01
39 #define PCAN_USBPRO_EXT			0x02
40 #define PCAN_USBPRO_SS			0x08
41 
42 #define PCAN_USBPRO_CMD_BUFFER_SIZE	512
43 
44 /* handle device specific info used by the netdevices */
45 struct pcan_usb_pro_interface {
46 	struct peak_usb_device *dev[PCAN_USBPRO_CHANNEL_COUNT];
47 	struct peak_time_ref time_ref;
48 	int cm_ignore_count;
49 	int dev_opened_count;
50 };
51 
52 /* device information */
53 struct pcan_usb_pro_device {
54 	struct peak_usb_device dev;
55 	struct pcan_usb_pro_interface *usb_if;
56 	u32 cached_ccbt;
57 };
58 
59 /* internal structure used to handle messages sent to bulk urb */
60 struct pcan_usb_pro_msg {
61 	u8 *rec_ptr;
62 	int rec_buffer_size;
63 	int rec_buffer_len;
64 	union {
65 		__le16 *rec_cnt_rd;
66 		__le32 *rec_cnt;
67 		u8 *rec_buffer;
68 	} u;
69 };
70 
71 /* records sizes table indexed on message id. (8-bits value) */
72 static u16 pcan_usb_pro_sizeof_rec[256] = {
73 	[PCAN_USBPRO_SETBTR] = sizeof(struct pcan_usb_pro_btr),
74 	[PCAN_USBPRO_SETBUSACT] = sizeof(struct pcan_usb_pro_busact),
75 	[PCAN_USBPRO_SETSILENT] = sizeof(struct pcan_usb_pro_silent),
76 	[PCAN_USBPRO_SETFILTR] = sizeof(struct pcan_usb_pro_filter),
77 	[PCAN_USBPRO_SETTS] = sizeof(struct pcan_usb_pro_setts),
78 	[PCAN_USBPRO_GETDEVID] = sizeof(struct pcan_usb_pro_devid),
79 	[PCAN_USBPRO_SETDEVID] = sizeof(struct pcan_usb_pro_devid),
80 	[PCAN_USBPRO_SETLED] = sizeof(struct pcan_usb_pro_setled),
81 	[PCAN_USBPRO_RXMSG8] = sizeof(struct pcan_usb_pro_rxmsg),
82 	[PCAN_USBPRO_RXMSG4] = sizeof(struct pcan_usb_pro_rxmsg) - 4,
83 	[PCAN_USBPRO_RXMSG0] = sizeof(struct pcan_usb_pro_rxmsg) - 8,
84 	[PCAN_USBPRO_RXRTR] = sizeof(struct pcan_usb_pro_rxmsg) - 8,
85 	[PCAN_USBPRO_RXSTATUS] = sizeof(struct pcan_usb_pro_rxstatus),
86 	[PCAN_USBPRO_RXTS] = sizeof(struct pcan_usb_pro_rxts),
87 	[PCAN_USBPRO_TXMSG8] = sizeof(struct pcan_usb_pro_txmsg),
88 	[PCAN_USBPRO_TXMSG4] = sizeof(struct pcan_usb_pro_txmsg) - 4,
89 	[PCAN_USBPRO_TXMSG0] = sizeof(struct pcan_usb_pro_txmsg) - 8,
90 };
91 
92 /*
93  * initialize PCAN-USB Pro message data structure
94  */
pcan_msg_init(struct pcan_usb_pro_msg * pm,void * buffer_addr,int buffer_size)95 static u8 *pcan_msg_init(struct pcan_usb_pro_msg *pm, void *buffer_addr,
96 			 int buffer_size)
97 {
98 	if (buffer_size < PCAN_USBPRO_MSG_HEADER_LEN)
99 		return NULL;
100 
101 	pm->u.rec_buffer = (u8 *)buffer_addr;
102 	pm->rec_buffer_size = pm->rec_buffer_len = buffer_size;
103 	pm->rec_ptr = pm->u.rec_buffer + PCAN_USBPRO_MSG_HEADER_LEN;
104 
105 	return pm->rec_ptr;
106 }
107 
pcan_msg_init_empty(struct pcan_usb_pro_msg * pm,void * buffer_addr,int buffer_size)108 static u8 *pcan_msg_init_empty(struct pcan_usb_pro_msg *pm,
109 			       void *buffer_addr, int buffer_size)
110 {
111 	u8 *pr = pcan_msg_init(pm, buffer_addr, buffer_size);
112 
113 	if (pr) {
114 		pm->rec_buffer_len = PCAN_USBPRO_MSG_HEADER_LEN;
115 		*pm->u.rec_cnt = 0;
116 	}
117 	return pr;
118 }
119 
120 /*
121  * add one record to a message being built
122  */
pcan_msg_add_rec(struct pcan_usb_pro_msg * pm,int id,...)123 static int pcan_msg_add_rec(struct pcan_usb_pro_msg *pm, int id, ...)
124 {
125 	int len, i;
126 	u8 *pc;
127 	va_list ap;
128 
129 	va_start(ap, id);
130 
131 	pc = pm->rec_ptr + 1;
132 
133 	i = 0;
134 	switch (id) {
135 	case PCAN_USBPRO_TXMSG8:
136 		i += 4;
137 		fallthrough;
138 	case PCAN_USBPRO_TXMSG4:
139 		i += 4;
140 		fallthrough;
141 	case PCAN_USBPRO_TXMSG0:
142 		*pc++ = va_arg(ap, int);
143 		*pc++ = va_arg(ap, int);
144 		*pc++ = va_arg(ap, int);
145 		*(__le32 *)pc = cpu_to_le32(va_arg(ap, u32));
146 		pc += 4;
147 		memcpy(pc, va_arg(ap, int *), i);
148 		pc += i;
149 		break;
150 
151 	case PCAN_USBPRO_SETBTR:
152 	case PCAN_USBPRO_GETDEVID:
153 	case PCAN_USBPRO_SETDEVID:
154 		*pc++ = va_arg(ap, int);
155 		pc += 2;
156 		*(__le32 *)pc = cpu_to_le32(va_arg(ap, u32));
157 		pc += 4;
158 		break;
159 
160 	case PCAN_USBPRO_SETFILTR:
161 	case PCAN_USBPRO_SETBUSACT:
162 	case PCAN_USBPRO_SETSILENT:
163 		*pc++ = va_arg(ap, int);
164 		*(__le16 *)pc = cpu_to_le16(va_arg(ap, int));
165 		pc += 2;
166 		break;
167 
168 	case PCAN_USBPRO_SETLED:
169 		*pc++ = va_arg(ap, int);
170 		*(__le16 *)pc = cpu_to_le16(va_arg(ap, int));
171 		pc += 2;
172 		*(__le32 *)pc = cpu_to_le32(va_arg(ap, u32));
173 		pc += 4;
174 		break;
175 
176 	case PCAN_USBPRO_SETTS:
177 		pc++;
178 		*(__le16 *)pc = cpu_to_le16(va_arg(ap, int));
179 		pc += 2;
180 		break;
181 
182 	default:
183 		pr_err("%s: %s(): unknown data type %02Xh (%d)\n",
184 			PCAN_USB_DRIVER_NAME, __func__, id, id);
185 		pc--;
186 		break;
187 	}
188 
189 	len = pc - pm->rec_ptr;
190 	if (len > 0) {
191 		le32_add_cpu(pm->u.rec_cnt, 1);
192 		*pm->rec_ptr = id;
193 
194 		pm->rec_ptr = pc;
195 		pm->rec_buffer_len += len;
196 	}
197 
198 	va_end(ap);
199 
200 	return len;
201 }
202 
203 /*
204  * send PCAN-USB Pro command synchronously
205  */
pcan_usb_pro_send_cmd(struct peak_usb_device * dev,struct pcan_usb_pro_msg * pum)206 static int pcan_usb_pro_send_cmd(struct peak_usb_device *dev,
207 				 struct pcan_usb_pro_msg *pum)
208 {
209 	int actual_length;
210 	int err;
211 
212 	/* usb device unregistered? */
213 	if (!(dev->state & PCAN_USB_STATE_CONNECTED))
214 		return 0;
215 
216 	err = usb_bulk_msg(dev->udev,
217 		usb_sndbulkpipe(dev->udev, PCAN_USBPRO_EP_CMDOUT),
218 		pum->u.rec_buffer, pum->rec_buffer_len,
219 		&actual_length, PCAN_USBPRO_COMMAND_TIMEOUT);
220 	if (err)
221 		netdev_err(dev->netdev, "sending command failure: %d\n", err);
222 
223 	return err;
224 }
225 
226 /*
227  * wait for PCAN-USB Pro command response
228  */
pcan_usb_pro_wait_rsp(struct peak_usb_device * dev,struct pcan_usb_pro_msg * pum)229 static int pcan_usb_pro_wait_rsp(struct peak_usb_device *dev,
230 				 struct pcan_usb_pro_msg *pum)
231 {
232 	u8 req_data_type, req_channel;
233 	int actual_length;
234 	int i, err = 0;
235 
236 	/* usb device unregistered? */
237 	if (!(dev->state & PCAN_USB_STATE_CONNECTED))
238 		return 0;
239 
240 	req_data_type = pum->u.rec_buffer[4];
241 	req_channel = pum->u.rec_buffer[5];
242 
243 	*pum->u.rec_cnt = 0;
244 	for (i = 0; !err && i < PCAN_USBPRO_RSP_SUBMIT_MAX; i++) {
245 		struct pcan_usb_pro_msg rsp;
246 		union pcan_usb_pro_rec *pr;
247 		u32 r, rec_cnt;
248 		u16 rec_len;
249 		u8 *pc;
250 
251 		err = usb_bulk_msg(dev->udev,
252 			usb_rcvbulkpipe(dev->udev, PCAN_USBPRO_EP_CMDIN),
253 			pum->u.rec_buffer, pum->rec_buffer_len,
254 			&actual_length, PCAN_USBPRO_COMMAND_TIMEOUT);
255 		if (err) {
256 			netdev_err(dev->netdev, "waiting rsp error %d\n", err);
257 			break;
258 		}
259 
260 		if (actual_length == 0)
261 			continue;
262 
263 		err = -EBADMSG;
264 		if (actual_length < PCAN_USBPRO_MSG_HEADER_LEN) {
265 			netdev_err(dev->netdev,
266 				   "got abnormal too small rsp (len=%d)\n",
267 				   actual_length);
268 			break;
269 		}
270 
271 		pc = pcan_msg_init(&rsp, pum->u.rec_buffer,
272 			actual_length);
273 
274 		rec_cnt = le32_to_cpu(*rsp.u.rec_cnt);
275 
276 		/* loop on records stored into message */
277 		for (r = 0; r < rec_cnt; r++) {
278 			pr = (union pcan_usb_pro_rec *)pc;
279 			rec_len = pcan_usb_pro_sizeof_rec[pr->data_type];
280 			if (!rec_len) {
281 				netdev_err(dev->netdev,
282 					   "got unprocessed record in msg\n");
283 				pcan_dump_mem("rcvd rsp msg", pum->u.rec_buffer,
284 					      actual_length);
285 				break;
286 			}
287 
288 			/* check if response corresponds to request */
289 			if (pr->data_type != req_data_type)
290 				netdev_err(dev->netdev,
291 					   "got unwanted rsp %xh: ignored\n",
292 					   pr->data_type);
293 
294 			/* check if channel in response corresponds too */
295 			else if ((req_channel != 0xff) &&
296 				(pr->bus_act.channel != req_channel))
297 				netdev_err(dev->netdev,
298 					"got rsp %xh but on chan%u: ignored\n",
299 					req_data_type, pr->bus_act.channel);
300 
301 			/* got the response */
302 			else
303 				return 0;
304 
305 			/* otherwise, go on with next record in message */
306 			pc += rec_len;
307 		}
308 	}
309 
310 	return (i >= PCAN_USBPRO_RSP_SUBMIT_MAX) ? -ERANGE : err;
311 }
312 
pcan_usb_pro_send_req(struct peak_usb_device * dev,int req_id,int req_value,void * req_addr,int req_size)313 int pcan_usb_pro_send_req(struct peak_usb_device *dev, int req_id,
314 			  int req_value, void *req_addr, int req_size)
315 {
316 	int err;
317 	u8 req_type;
318 	unsigned int p;
319 
320 	/* usb device unregistered? */
321 	if (!(dev->state & PCAN_USB_STATE_CONNECTED))
322 		return 0;
323 
324 	req_type = USB_TYPE_VENDOR | USB_RECIP_OTHER;
325 
326 	switch (req_id) {
327 	case PCAN_USBPRO_REQ_FCT:
328 		p = usb_sndctrlpipe(dev->udev, 0);
329 		break;
330 
331 	default:
332 		p = usb_rcvctrlpipe(dev->udev, 0);
333 		req_type |= USB_DIR_IN;
334 		memset(req_addr, '\0', req_size);
335 		break;
336 	}
337 
338 	err = usb_control_msg(dev->udev, p, req_id, req_type, req_value, 0,
339 			      req_addr, req_size, 2 * USB_CTRL_GET_TIMEOUT);
340 	if (err < 0) {
341 		netdev_info(dev->netdev,
342 			    "unable to request usb[type=%d value=%d] err=%d\n",
343 			    req_id, req_value, err);
344 		return err;
345 	}
346 
347 	return 0;
348 }
349 
pcan_usb_pro_set_ts(struct peak_usb_device * dev,u16 onoff)350 static int pcan_usb_pro_set_ts(struct peak_usb_device *dev, u16 onoff)
351 {
352 	struct pcan_usb_pro_msg um;
353 
354 	pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
355 	pcan_msg_add_rec(&um, PCAN_USBPRO_SETTS, onoff);
356 
357 	return pcan_usb_pro_send_cmd(dev, &um);
358 }
359 
pcan_usb_pro_set_bitrate(struct peak_usb_device * dev,u32 ccbt)360 static int pcan_usb_pro_set_bitrate(struct peak_usb_device *dev, u32 ccbt)
361 {
362 	struct pcan_usb_pro_device *pdev =
363 			container_of(dev, struct pcan_usb_pro_device, dev);
364 	struct pcan_usb_pro_msg um;
365 
366 	pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
367 	pcan_msg_add_rec(&um, PCAN_USBPRO_SETBTR, dev->ctrl_idx, ccbt);
368 
369 	/* cache the CCBT value to reuse it before next buson */
370 	pdev->cached_ccbt = ccbt;
371 
372 	return pcan_usb_pro_send_cmd(dev, &um);
373 }
374 
pcan_usb_pro_set_bus(struct peak_usb_device * dev,u8 onoff)375 static int pcan_usb_pro_set_bus(struct peak_usb_device *dev, u8 onoff)
376 {
377 	struct pcan_usb_pro_msg um;
378 
379 	/* if bus=on, be sure the bitrate being set before! */
380 	if (onoff) {
381 		struct pcan_usb_pro_device *pdev =
382 			     container_of(dev, struct pcan_usb_pro_device, dev);
383 
384 		pcan_usb_pro_set_bitrate(dev, pdev->cached_ccbt);
385 	}
386 
387 	pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
388 	pcan_msg_add_rec(&um, PCAN_USBPRO_SETBUSACT, dev->ctrl_idx, onoff);
389 
390 	return pcan_usb_pro_send_cmd(dev, &um);
391 }
392 
pcan_usb_pro_set_silent(struct peak_usb_device * dev,u8 onoff)393 static int pcan_usb_pro_set_silent(struct peak_usb_device *dev, u8 onoff)
394 {
395 	struct pcan_usb_pro_msg um;
396 
397 	pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
398 	pcan_msg_add_rec(&um, PCAN_USBPRO_SETSILENT, dev->ctrl_idx, onoff);
399 
400 	return pcan_usb_pro_send_cmd(dev, &um);
401 }
402 
pcan_usb_pro_set_filter(struct peak_usb_device * dev,u16 filter_mode)403 static int pcan_usb_pro_set_filter(struct peak_usb_device *dev, u16 filter_mode)
404 {
405 	struct pcan_usb_pro_msg um;
406 
407 	pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
408 	pcan_msg_add_rec(&um, PCAN_USBPRO_SETFILTR, dev->ctrl_idx, filter_mode);
409 
410 	return pcan_usb_pro_send_cmd(dev, &um);
411 }
412 
pcan_usb_pro_set_led(struct peak_usb_device * dev,u8 mode,u32 timeout)413 static int pcan_usb_pro_set_led(struct peak_usb_device *dev, u8 mode,
414 				u32 timeout)
415 {
416 	struct pcan_usb_pro_msg um;
417 
418 	pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
419 	pcan_msg_add_rec(&um, PCAN_USBPRO_SETLED, dev->ctrl_idx, mode, timeout);
420 
421 	return pcan_usb_pro_send_cmd(dev, &um);
422 }
423 
pcan_usb_pro_get_can_channel_id(struct peak_usb_device * dev,u32 * can_ch_id)424 static int pcan_usb_pro_get_can_channel_id(struct peak_usb_device *dev,
425 					   u32 *can_ch_id)
426 {
427 	struct pcan_usb_pro_devid *pdn;
428 	struct pcan_usb_pro_msg um;
429 	int err;
430 	u8 *pc;
431 
432 	pc = pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
433 	pcan_msg_add_rec(&um, PCAN_USBPRO_GETDEVID, dev->ctrl_idx);
434 
435 	err =  pcan_usb_pro_send_cmd(dev, &um);
436 	if (err)
437 		return err;
438 
439 	err = pcan_usb_pro_wait_rsp(dev, &um);
440 	if (err)
441 		return err;
442 
443 	pdn = (struct pcan_usb_pro_devid *)pc;
444 	*can_ch_id = le32_to_cpu(pdn->dev_num);
445 
446 	return err;
447 }
448 
pcan_usb_pro_set_can_channel_id(struct peak_usb_device * dev,u32 can_ch_id)449 static int pcan_usb_pro_set_can_channel_id(struct peak_usb_device *dev,
450 					   u32 can_ch_id)
451 {
452 	struct pcan_usb_pro_msg um;
453 
454 	pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
455 	pcan_msg_add_rec(&um, PCAN_USBPRO_SETDEVID, dev->ctrl_idx,
456 			 can_ch_id);
457 
458 	return pcan_usb_pro_send_cmd(dev, &um);
459 }
460 
pcan_usb_pro_set_bittiming(struct peak_usb_device * dev,struct can_bittiming * bt)461 static int pcan_usb_pro_set_bittiming(struct peak_usb_device *dev,
462 				      struct can_bittiming *bt)
463 {
464 	u32 ccbt;
465 
466 	ccbt = (dev->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) ? 0x00800000 : 0;
467 	ccbt |= (bt->sjw - 1) << 24;
468 	ccbt |= (bt->phase_seg2 - 1) << 20;
469 	ccbt |= (bt->prop_seg + bt->phase_seg1 - 1) << 16; /* = tseg1 */
470 	ccbt |= bt->brp - 1;
471 
472 	netdev_info(dev->netdev, "setting ccbt=0x%08x\n", ccbt);
473 
474 	return pcan_usb_pro_set_bitrate(dev, ccbt);
475 }
476 
pcan_usb_pro_restart_complete(struct urb * urb)477 void pcan_usb_pro_restart_complete(struct urb *urb)
478 {
479 	/* can delete usb resources */
480 	peak_usb_async_complete(urb);
481 
482 	/* notify candev and netdev */
483 	peak_usb_restart_complete(urb->context);
484 }
485 
486 /*
487  * handle restart but in asynchronously way
488  */
pcan_usb_pro_restart_async(struct peak_usb_device * dev,struct urb * urb,u8 * buf)489 static int pcan_usb_pro_restart_async(struct peak_usb_device *dev,
490 				      struct urb *urb, u8 *buf)
491 {
492 	struct pcan_usb_pro_msg um;
493 
494 	pcan_msg_init_empty(&um, buf, PCAN_USB_MAX_CMD_LEN);
495 	pcan_msg_add_rec(&um, PCAN_USBPRO_SETBUSACT, dev->ctrl_idx, 1);
496 
497 	usb_fill_bulk_urb(urb, dev->udev,
498 			usb_sndbulkpipe(dev->udev, PCAN_USBPRO_EP_CMDOUT),
499 			buf, PCAN_USB_MAX_CMD_LEN,
500 			pcan_usb_pro_restart_complete, dev);
501 
502 	return usb_submit_urb(urb, GFP_ATOMIC);
503 }
504 
pcan_usb_pro_drv_loaded(struct peak_usb_device * dev,int loaded)505 static int pcan_usb_pro_drv_loaded(struct peak_usb_device *dev, int loaded)
506 {
507 	u8 *buffer;
508 	int err;
509 
510 	buffer = kzalloc(PCAN_USBPRO_FCT_DRVLD_REQ_LEN, GFP_KERNEL);
511 	if (!buffer)
512 		return -ENOMEM;
513 
514 	buffer[0] = 0;
515 	buffer[1] = !!loaded;
516 
517 	err = pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_FCT,
518 				    PCAN_USBPRO_FCT_DRVLD, buffer,
519 				    PCAN_USBPRO_FCT_DRVLD_REQ_LEN);
520 	kfree(buffer);
521 
522 	return err;
523 }
524 
525 static inline
pcan_usb_pro_dev_if(struct peak_usb_device * dev)526 struct pcan_usb_pro_interface *pcan_usb_pro_dev_if(struct peak_usb_device *dev)
527 {
528 	struct pcan_usb_pro_device *pdev =
529 			container_of(dev, struct pcan_usb_pro_device, dev);
530 	return pdev->usb_if;
531 }
532 
pcan_usb_pro_handle_canmsg(struct pcan_usb_pro_interface * usb_if,struct pcan_usb_pro_rxmsg * rx)533 static int pcan_usb_pro_handle_canmsg(struct pcan_usb_pro_interface *usb_if,
534 				      struct pcan_usb_pro_rxmsg *rx)
535 {
536 	const unsigned int ctrl_idx = (rx->len >> 4) & 0x0f;
537 	struct peak_usb_device *dev = usb_if->dev[ctrl_idx];
538 	struct net_device *netdev = dev->netdev;
539 	struct can_frame *can_frame;
540 	struct sk_buff *skb;
541 	struct skb_shared_hwtstamps *hwts;
542 
543 	skb = alloc_can_skb(netdev, &can_frame);
544 	if (!skb)
545 		return -ENOMEM;
546 
547 	can_frame->can_id = le32_to_cpu(rx->id);
548 	can_frame->len = rx->len & 0x0f;
549 
550 	if (rx->flags & PCAN_USBPRO_EXT)
551 		can_frame->can_id |= CAN_EFF_FLAG;
552 
553 	if (rx->flags & PCAN_USBPRO_RTR) {
554 		can_frame->can_id |= CAN_RTR_FLAG;
555 	} else {
556 		memcpy(can_frame->data, rx->data, can_frame->len);
557 
558 		netdev->stats.rx_bytes += can_frame->len;
559 	}
560 	netdev->stats.rx_packets++;
561 
562 	hwts = skb_hwtstamps(skb);
563 	peak_usb_get_ts_time(&usb_if->time_ref, le32_to_cpu(rx->ts32),
564 			     &hwts->hwtstamp);
565 
566 	netif_rx(skb);
567 
568 	return 0;
569 }
570 
pcan_usb_pro_handle_error(struct pcan_usb_pro_interface * usb_if,struct pcan_usb_pro_rxstatus * er)571 static int pcan_usb_pro_handle_error(struct pcan_usb_pro_interface *usb_if,
572 				     struct pcan_usb_pro_rxstatus *er)
573 {
574 	const u16 raw_status = le16_to_cpu(er->status);
575 	const unsigned int ctrl_idx = (er->channel >> 4) & 0x0f;
576 	struct peak_usb_device *dev = usb_if->dev[ctrl_idx];
577 	struct net_device *netdev = dev->netdev;
578 	struct can_frame *can_frame;
579 	enum can_state new_state = CAN_STATE_ERROR_ACTIVE;
580 	u8 err_mask = 0;
581 	struct sk_buff *skb;
582 	struct skb_shared_hwtstamps *hwts;
583 
584 	/* nothing should be sent while in BUS_OFF state */
585 	if (dev->can.state == CAN_STATE_BUS_OFF)
586 		return 0;
587 
588 	if (!raw_status) {
589 		/* no error bit (back to active state) */
590 		dev->can.state = CAN_STATE_ERROR_ACTIVE;
591 		return 0;
592 	}
593 
594 	if (raw_status & (PCAN_USBPRO_STATUS_OVERRUN |
595 			  PCAN_USBPRO_STATUS_QOVERRUN)) {
596 		/* trick to bypass next comparison and process other errors */
597 		new_state = CAN_STATE_MAX;
598 	}
599 
600 	if (raw_status & PCAN_USBPRO_STATUS_BUS) {
601 		new_state = CAN_STATE_BUS_OFF;
602 	} else if (raw_status & PCAN_USBPRO_STATUS_ERROR) {
603 		u32 rx_err_cnt = (le32_to_cpu(er->err_frm) & 0x00ff0000) >> 16;
604 		u32 tx_err_cnt = (le32_to_cpu(er->err_frm) & 0xff000000) >> 24;
605 
606 		if (rx_err_cnt > 127)
607 			err_mask |= CAN_ERR_CRTL_RX_PASSIVE;
608 		else if (rx_err_cnt > 96)
609 			err_mask |= CAN_ERR_CRTL_RX_WARNING;
610 
611 		if (tx_err_cnt > 127)
612 			err_mask |= CAN_ERR_CRTL_TX_PASSIVE;
613 		else if (tx_err_cnt > 96)
614 			err_mask |= CAN_ERR_CRTL_TX_WARNING;
615 
616 		if (err_mask & (CAN_ERR_CRTL_RX_WARNING |
617 				CAN_ERR_CRTL_TX_WARNING))
618 			new_state = CAN_STATE_ERROR_WARNING;
619 		else if (err_mask & (CAN_ERR_CRTL_RX_PASSIVE |
620 				     CAN_ERR_CRTL_TX_PASSIVE))
621 			new_state = CAN_STATE_ERROR_PASSIVE;
622 	}
623 
624 	/* donot post any error if current state didn't change */
625 	if (dev->can.state == new_state)
626 		return 0;
627 
628 	/* allocate an skb to store the error frame */
629 	skb = alloc_can_err_skb(netdev, &can_frame);
630 	if (!skb)
631 		return -ENOMEM;
632 
633 	switch (new_state) {
634 	case CAN_STATE_BUS_OFF:
635 		can_frame->can_id |= CAN_ERR_BUSOFF;
636 		dev->can.can_stats.bus_off++;
637 		can_bus_off(netdev);
638 		break;
639 
640 	case CAN_STATE_ERROR_PASSIVE:
641 		can_frame->can_id |= CAN_ERR_CRTL;
642 		can_frame->data[1] |= err_mask;
643 		dev->can.can_stats.error_passive++;
644 		break;
645 
646 	case CAN_STATE_ERROR_WARNING:
647 		can_frame->can_id |= CAN_ERR_CRTL;
648 		can_frame->data[1] |= err_mask;
649 		dev->can.can_stats.error_warning++;
650 		break;
651 
652 	case CAN_STATE_ERROR_ACTIVE:
653 		break;
654 
655 	default:
656 		/* CAN_STATE_MAX (trick to handle other errors) */
657 		if (raw_status & PCAN_USBPRO_STATUS_OVERRUN) {
658 			can_frame->can_id |= CAN_ERR_PROT;
659 			can_frame->data[2] |= CAN_ERR_PROT_OVERLOAD;
660 			netdev->stats.rx_over_errors++;
661 			netdev->stats.rx_errors++;
662 		}
663 
664 		if (raw_status & PCAN_USBPRO_STATUS_QOVERRUN) {
665 			can_frame->can_id |= CAN_ERR_CRTL;
666 			can_frame->data[1] |= CAN_ERR_CRTL_RX_OVERFLOW;
667 			netdev->stats.rx_over_errors++;
668 			netdev->stats.rx_errors++;
669 		}
670 
671 		new_state = CAN_STATE_ERROR_ACTIVE;
672 		break;
673 	}
674 
675 	dev->can.state = new_state;
676 
677 	hwts = skb_hwtstamps(skb);
678 	peak_usb_get_ts_time(&usb_if->time_ref, le32_to_cpu(er->ts32), &hwts->hwtstamp);
679 	netif_rx(skb);
680 
681 	return 0;
682 }
683 
pcan_usb_pro_handle_ts(struct pcan_usb_pro_interface * usb_if,struct pcan_usb_pro_rxts * ts)684 static void pcan_usb_pro_handle_ts(struct pcan_usb_pro_interface *usb_if,
685 				   struct pcan_usb_pro_rxts *ts)
686 {
687 	/* should wait until clock is stabilized */
688 	if (usb_if->cm_ignore_count > 0)
689 		usb_if->cm_ignore_count--;
690 	else
691 		peak_usb_set_ts_now(&usb_if->time_ref,
692 				    le32_to_cpu(ts->ts64[1]));
693 }
694 
695 /*
696  * callback for bulk IN urb
697  */
pcan_usb_pro_decode_buf(struct peak_usb_device * dev,struct urb * urb)698 static int pcan_usb_pro_decode_buf(struct peak_usb_device *dev, struct urb *urb)
699 {
700 	struct pcan_usb_pro_interface *usb_if = pcan_usb_pro_dev_if(dev);
701 	struct net_device *netdev = dev->netdev;
702 	struct pcan_usb_pro_msg usb_msg;
703 	u8 *rec_ptr, *msg_end;
704 	u16 rec_cnt;
705 	int err = 0;
706 
707 	rec_ptr = pcan_msg_init(&usb_msg, urb->transfer_buffer,
708 					urb->actual_length);
709 	if (!rec_ptr) {
710 		netdev_err(netdev, "bad msg hdr len %d\n", urb->actual_length);
711 		return -EINVAL;
712 	}
713 
714 	/* loop reading all the records from the incoming message */
715 	msg_end = urb->transfer_buffer + urb->actual_length;
716 	rec_cnt = le16_to_cpu(*usb_msg.u.rec_cnt_rd);
717 	for (; rec_cnt > 0; rec_cnt--) {
718 		union pcan_usb_pro_rec *pr = (union pcan_usb_pro_rec *)rec_ptr;
719 		u16 sizeof_rec = pcan_usb_pro_sizeof_rec[pr->data_type];
720 
721 		if (!sizeof_rec) {
722 			netdev_err(netdev,
723 				   "got unsupported rec in usb msg:\n");
724 			err = -ENOTSUPP;
725 			break;
726 		}
727 
728 		/* check if the record goes out of current packet */
729 		if (rec_ptr + sizeof_rec > msg_end) {
730 			netdev_err(netdev,
731 				"got frag rec: should inc usb rx buf size\n");
732 			err = -EBADMSG;
733 			break;
734 		}
735 
736 		switch (pr->data_type) {
737 		case PCAN_USBPRO_RXMSG8:
738 		case PCAN_USBPRO_RXMSG4:
739 		case PCAN_USBPRO_RXMSG0:
740 		case PCAN_USBPRO_RXRTR:
741 			err = pcan_usb_pro_handle_canmsg(usb_if, &pr->rx_msg);
742 			if (err < 0)
743 				goto fail;
744 			break;
745 
746 		case PCAN_USBPRO_RXSTATUS:
747 			err = pcan_usb_pro_handle_error(usb_if, &pr->rx_status);
748 			if (err < 0)
749 				goto fail;
750 			break;
751 
752 		case PCAN_USBPRO_RXTS:
753 			pcan_usb_pro_handle_ts(usb_if, &pr->rx_ts);
754 			break;
755 
756 		default:
757 			netdev_err(netdev,
758 				   "unhandled rec type 0x%02x (%d): ignored\n",
759 				   pr->data_type, pr->data_type);
760 			break;
761 		}
762 
763 		rec_ptr += sizeof_rec;
764 	}
765 
766 fail:
767 	if (err)
768 		pcan_dump_mem("received msg",
769 			      urb->transfer_buffer, urb->actual_length);
770 
771 	return err;
772 }
773 
pcan_usb_pro_encode_msg(struct peak_usb_device * dev,struct sk_buff * skb,u8 * obuf,size_t * size)774 static int pcan_usb_pro_encode_msg(struct peak_usb_device *dev,
775 				   struct sk_buff *skb, u8 *obuf, size_t *size)
776 {
777 	struct can_frame *cf = (struct can_frame *)skb->data;
778 	u8 data_type, len, flags;
779 	struct pcan_usb_pro_msg usb_msg;
780 
781 	pcan_msg_init_empty(&usb_msg, obuf, *size);
782 
783 	if ((cf->can_id & CAN_RTR_FLAG) || (cf->len == 0))
784 		data_type = PCAN_USBPRO_TXMSG0;
785 	else if (cf->len <= 4)
786 		data_type = PCAN_USBPRO_TXMSG4;
787 	else
788 		data_type = PCAN_USBPRO_TXMSG8;
789 
790 	len = (dev->ctrl_idx << 4) | (cf->len & 0x0f);
791 
792 	flags = 0;
793 	if (cf->can_id & CAN_EFF_FLAG)
794 		flags |= PCAN_USBPRO_EXT;
795 	if (cf->can_id & CAN_RTR_FLAG)
796 		flags |= PCAN_USBPRO_RTR;
797 
798 	/* Single-Shot frame */
799 	if (dev->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT)
800 		flags |= PCAN_USBPRO_SS;
801 
802 	pcan_msg_add_rec(&usb_msg, data_type, 0, flags, len, cf->can_id,
803 			 cf->data);
804 
805 	*size = usb_msg.rec_buffer_len;
806 
807 	return 0;
808 }
809 
pcan_usb_pro_start(struct peak_usb_device * dev)810 static int pcan_usb_pro_start(struct peak_usb_device *dev)
811 {
812 	struct pcan_usb_pro_device *pdev =
813 			container_of(dev, struct pcan_usb_pro_device, dev);
814 	int err;
815 
816 	err = pcan_usb_pro_set_silent(dev,
817 				dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY);
818 	if (err)
819 		return err;
820 
821 	/* filter mode: 0-> All OFF; 1->bypass */
822 	err = pcan_usb_pro_set_filter(dev, 1);
823 	if (err)
824 		return err;
825 
826 	/* opening first device: */
827 	if (pdev->usb_if->dev_opened_count == 0) {
828 		/* reset time_ref */
829 		peak_usb_init_time_ref(&pdev->usb_if->time_ref, &pcan_usb_pro);
830 
831 		/* ask device to send ts messages */
832 		err = pcan_usb_pro_set_ts(dev, 1);
833 	}
834 
835 	pdev->usb_if->dev_opened_count++;
836 
837 	return err;
838 }
839 
840 /*
841  * stop interface
842  * (last chance before set bus off)
843  */
pcan_usb_pro_stop(struct peak_usb_device * dev)844 static int pcan_usb_pro_stop(struct peak_usb_device *dev)
845 {
846 	struct pcan_usb_pro_device *pdev =
847 			container_of(dev, struct pcan_usb_pro_device, dev);
848 
849 	/* turn off ts msgs for that interface if no other dev opened */
850 	if (pdev->usb_if->dev_opened_count == 1)
851 		pcan_usb_pro_set_ts(dev, 0);
852 
853 	pdev->usb_if->dev_opened_count--;
854 
855 	return 0;
856 }
857 
858 /*
859  * called when probing to initialize a device object.
860  */
pcan_usb_pro_init(struct peak_usb_device * dev)861 static int pcan_usb_pro_init(struct peak_usb_device *dev)
862 {
863 	struct pcan_usb_pro_device *pdev =
864 			container_of(dev, struct pcan_usb_pro_device, dev);
865 	struct pcan_usb_pro_interface *usb_if = NULL;
866 	struct pcan_usb_pro_fwinfo *fi = NULL;
867 	struct pcan_usb_pro_blinfo *bi = NULL;
868 	int err;
869 
870 	/* do this for 1st channel only */
871 	if (!dev->prev_siblings) {
872 		/* allocate netdevices common structure attached to first one */
873 		usb_if = kzalloc(sizeof(struct pcan_usb_pro_interface),
874 				 GFP_KERNEL);
875 		fi = kmalloc(sizeof(struct pcan_usb_pro_fwinfo), GFP_KERNEL);
876 		bi = kmalloc(sizeof(struct pcan_usb_pro_blinfo), GFP_KERNEL);
877 		if (!usb_if || !fi || !bi) {
878 			err = -ENOMEM;
879 			goto err_out;
880 		}
881 
882 		/* number of ts msgs to ignore before taking one into account */
883 		usb_if->cm_ignore_count = 5;
884 
885 		/*
886 		 * explicit use of dev_xxx() instead of netdev_xxx() here:
887 		 * information displayed are related to the device itself, not
888 		 * to the canx netdevices.
889 		 */
890 		err = pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_INFO,
891 					    PCAN_USBPRO_INFO_FW,
892 					    fi, sizeof(*fi));
893 		if (err) {
894 			dev_err(dev->netdev->dev.parent,
895 				"unable to read %s firmware info (err %d)\n",
896 				pcan_usb_pro.name, err);
897 			goto err_out;
898 		}
899 
900 		err = pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_INFO,
901 					    PCAN_USBPRO_INFO_BL,
902 					    bi, sizeof(*bi));
903 		if (err) {
904 			dev_err(dev->netdev->dev.parent,
905 				"unable to read %s bootloader info (err %d)\n",
906 				pcan_usb_pro.name, err);
907 			goto err_out;
908 		}
909 
910 		/* tell the device the can driver is running */
911 		err = pcan_usb_pro_drv_loaded(dev, 1);
912 		if (err)
913 			goto err_out;
914 
915 		dev_info(dev->netdev->dev.parent,
916 		     "PEAK-System %s hwrev %u serial %08X.%08X (%u channels)\n",
917 		     pcan_usb_pro.name,
918 		     bi->hw_rev, bi->serial_num_hi, bi->serial_num_lo,
919 		     pcan_usb_pro.ctrl_count);
920 	} else {
921 		usb_if = pcan_usb_pro_dev_if(dev->prev_siblings);
922 	}
923 
924 	pdev->usb_if = usb_if;
925 	usb_if->dev[dev->ctrl_idx] = dev;
926 
927 	/* set LED in default state (end of init phase) */
928 	pcan_usb_pro_set_led(dev, PCAN_USBPRO_LED_DEVICE, 1);
929 
930 	kfree(bi);
931 	kfree(fi);
932 
933 	return 0;
934 
935  err_out:
936 	kfree(bi);
937 	kfree(fi);
938 	kfree(usb_if);
939 
940 	return err;
941 }
942 
pcan_usb_pro_exit(struct peak_usb_device * dev)943 static void pcan_usb_pro_exit(struct peak_usb_device *dev)
944 {
945 	struct pcan_usb_pro_device *pdev =
946 			container_of(dev, struct pcan_usb_pro_device, dev);
947 
948 	/*
949 	 * when rmmod called before unplug and if down, should reset things
950 	 * before leaving
951 	 */
952 	if (dev->can.state != CAN_STATE_STOPPED) {
953 		/* set bus off on the corresponding channel */
954 		pcan_usb_pro_set_bus(dev, 0);
955 	}
956 
957 	/* if channel #0 (only) */
958 	if (dev->ctrl_idx == 0) {
959 		/* turn off calibration message if any device were opened */
960 		if (pdev->usb_if->dev_opened_count > 0)
961 			pcan_usb_pro_set_ts(dev, 0);
962 
963 		/* tell the PCAN-USB Pro device the driver is being unloaded */
964 		pcan_usb_pro_drv_loaded(dev, 0);
965 	}
966 }
967 
968 /*
969  * called when PCAN-USB Pro adapter is unplugged
970  */
pcan_usb_pro_free(struct peak_usb_device * dev)971 static void pcan_usb_pro_free(struct peak_usb_device *dev)
972 {
973 	/* last device: can free pcan_usb_pro_interface object now */
974 	if (!dev->prev_siblings && !dev->next_siblings)
975 		kfree(pcan_usb_pro_dev_if(dev));
976 }
977 
978 /*
979  * probe function for new PCAN-USB Pro usb interface
980  */
pcan_usb_pro_probe(struct usb_interface * intf)981 int pcan_usb_pro_probe(struct usb_interface *intf)
982 {
983 	struct usb_host_interface *if_desc;
984 	int i;
985 
986 	if_desc = intf->altsetting;
987 
988 	/* check interface endpoint addresses */
989 	for (i = 0; i < if_desc->desc.bNumEndpoints; i++) {
990 		struct usb_endpoint_descriptor *ep = &if_desc->endpoint[i].desc;
991 
992 		/*
993 		 * below is the list of valid ep addresses. Any other ep address
994 		 * is considered as not-CAN interface address => no dev created
995 		 */
996 		switch (ep->bEndpointAddress) {
997 		case PCAN_USBPRO_EP_CMDOUT:
998 		case PCAN_USBPRO_EP_CMDIN:
999 		case PCAN_USBPRO_EP_MSGOUT_0:
1000 		case PCAN_USBPRO_EP_MSGOUT_1:
1001 		case PCAN_USBPRO_EP_MSGIN:
1002 		case PCAN_USBPRO_EP_UNUSED:
1003 			break;
1004 		default:
1005 			return -ENODEV;
1006 		}
1007 	}
1008 
1009 	return 0;
1010 }
1011 
pcan_usb_pro_set_phys_id(struct net_device * netdev,enum ethtool_phys_id_state state)1012 static int pcan_usb_pro_set_phys_id(struct net_device *netdev,
1013 				    enum ethtool_phys_id_state state)
1014 {
1015 	struct peak_usb_device *dev = netdev_priv(netdev);
1016 	int err = 0;
1017 
1018 	switch (state) {
1019 	case ETHTOOL_ID_ACTIVE:
1020 		/* fast blinking forever */
1021 		err = pcan_usb_pro_set_led(dev, PCAN_USBPRO_LED_BLINK_FAST,
1022 					   0xffffffff);
1023 		break;
1024 
1025 	case ETHTOOL_ID_INACTIVE:
1026 		/* restore LED default */
1027 		err = pcan_usb_pro_set_led(dev, PCAN_USBPRO_LED_DEVICE, 1);
1028 		break;
1029 
1030 	default:
1031 		break;
1032 	}
1033 
1034 	return err;
1035 }
1036 
1037 static const struct ethtool_ops pcan_usb_pro_ethtool_ops = {
1038 	.set_phys_id = pcan_usb_pro_set_phys_id,
1039 	.get_ts_info = pcan_get_ts_info,
1040 	.get_eeprom_len	= peak_usb_get_eeprom_len,
1041 	.get_eeprom = peak_usb_get_eeprom,
1042 	.set_eeprom = peak_usb_set_eeprom,
1043 };
1044 
1045 /*
1046  * describe the PCAN-USB Pro adapter
1047  */
1048 static const struct can_bittiming_const pcan_usb_pro_const = {
1049 	.name = "pcan_usb_pro",
1050 	.tseg1_min = 1,
1051 	.tseg1_max = 16,
1052 	.tseg2_min = 1,
1053 	.tseg2_max = 8,
1054 	.sjw_max = 4,
1055 	.brp_min = 1,
1056 	.brp_max = 1024,
1057 	.brp_inc = 1,
1058 };
1059 
1060 const struct peak_usb_adapter pcan_usb_pro = {
1061 	.name = "PCAN-USB Pro",
1062 	.device_id = PCAN_USBPRO_PRODUCT_ID,
1063 	.ctrl_count = PCAN_USBPRO_CHANNEL_COUNT,
1064 	.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY |
1065 			      CAN_CTRLMODE_ONE_SHOT,
1066 	.clock = {
1067 		.freq = PCAN_USBPRO_CRYSTAL_HZ,
1068 	},
1069 	.bittiming_const = &pcan_usb_pro_const,
1070 
1071 	/* size of device private data */
1072 	.sizeof_dev_private = sizeof(struct pcan_usb_pro_device),
1073 
1074 	.ethtool_ops = &pcan_usb_pro_ethtool_ops,
1075 
1076 	/* timestamps usage */
1077 	.ts_used_bits = 32,
1078 	.us_per_ts_scale = 1, /* us = (ts * scale) >> shift */
1079 	.us_per_ts_shift = 0,
1080 
1081 	/* give here messages in/out endpoints */
1082 	.ep_msg_in = PCAN_USBPRO_EP_MSGIN,
1083 	.ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0, PCAN_USBPRO_EP_MSGOUT_1},
1084 
1085 	/* size of rx/tx usb buffers */
1086 	.rx_buffer_size = PCAN_USBPRO_RX_BUFFER_SIZE,
1087 	.tx_buffer_size = PCAN_USBPRO_TX_BUFFER_SIZE,
1088 
1089 	/* device callbacks */
1090 	.intf_probe = pcan_usb_pro_probe,
1091 	.dev_init = pcan_usb_pro_init,
1092 	.dev_exit = pcan_usb_pro_exit,
1093 	.dev_free = pcan_usb_pro_free,
1094 	.dev_set_bus = pcan_usb_pro_set_bus,
1095 	.dev_set_bittiming = pcan_usb_pro_set_bittiming,
1096 	.dev_get_can_channel_id = pcan_usb_pro_get_can_channel_id,
1097 	.dev_set_can_channel_id = pcan_usb_pro_set_can_channel_id,
1098 	.dev_decode_buf = pcan_usb_pro_decode_buf,
1099 	.dev_encode_msg = pcan_usb_pro_encode_msg,
1100 	.dev_start = pcan_usb_pro_start,
1101 	.dev_stop = pcan_usb_pro_stop,
1102 	.dev_restart_async = pcan_usb_pro_restart_async,
1103 };
1104