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