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