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