xref: /linux/drivers/net/can/usb/peak_usb/pcan_usb_fd.c (revision cc1e6315e83db0e517dd9279050b88adc83a7eba)
1 /*
2  * CAN driver for PEAK System PCAN-USB FD / PCAN-USB Pro FD adapter
3  *
4  * Copyright (C) 2013-2014 Stephane Grosjean <s.grosjean@peak-system.com>
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published
8  * by the Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  */
15 #include <linux/netdevice.h>
16 #include <linux/usb.h>
17 #include <linux/module.h>
18 
19 #include <linux/can.h>
20 #include <linux/can/dev.h>
21 #include <linux/can/error.h>
22 #include <linux/can/dev/peak_canfd.h>
23 
24 #include "pcan_usb_core.h"
25 #include "pcan_usb_pro.h"
26 
27 MODULE_SUPPORTED_DEVICE("PEAK-System PCAN-USB FD adapter");
28 MODULE_SUPPORTED_DEVICE("PEAK-System PCAN-USB Pro FD adapter");
29 
30 #define PCAN_USBPROFD_CHANNEL_COUNT	2
31 #define PCAN_USBFD_CHANNEL_COUNT	1
32 
33 /* PCAN-USB Pro FD adapter internal clock (Hz) */
34 #define PCAN_UFD_CRYSTAL_HZ		80000000
35 
36 #define PCAN_UFD_CMD_BUFFER_SIZE	512
37 #define PCAN_UFD_LOSPD_PKT_SIZE		64
38 
39 /* PCAN-USB Pro FD command timeout (ms.) */
40 #define PCAN_UFD_CMD_TIMEOUT_MS		1000
41 
42 /* PCAN-USB Pro FD rx/tx buffers size */
43 #define PCAN_UFD_RX_BUFFER_SIZE		2048
44 #define PCAN_UFD_TX_BUFFER_SIZE		512
45 
46 /* read some versions info from the hw devcie */
47 struct __packed pcan_ufd_fw_info {
48 	__le16	size_of;	/* sizeof this */
49 	__le16	type;		/* type of this structure */
50 	u8	hw_type;	/* Type of hardware (HW_TYPE_xxx) */
51 	u8	bl_version[3];	/* Bootloader version */
52 	u8	hw_version;	/* Hardware version (PCB) */
53 	u8	fw_version[3];	/* Firmware version */
54 	__le32	dev_id[2];	/* "device id" per CAN */
55 	__le32	ser_no;		/* S/N */
56 	__le32	flags;		/* special functions */
57 };
58 
59 /* handle device specific info used by the netdevices */
60 struct pcan_usb_fd_if {
61 	struct peak_usb_device	*dev[PCAN_USB_MAX_CHANNEL];
62 	struct pcan_ufd_fw_info	fw_info;
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_fd_device {
70 	struct peak_usb_device	dev;
71 	struct can_berr_counter	bec;
72 	struct pcan_usb_fd_if	*usb_if;
73 	u8			*cmd_buffer_addr;
74 };
75 
76 /* Extended USB commands (non uCAN commands) */
77 
78 /* Clock Modes command */
79 #define PCAN_UFD_CMD_CLK_SET		0x80
80 
81 #define PCAN_UFD_CLK_80MHZ		0x0
82 #define PCAN_UFD_CLK_60MHZ		0x1
83 #define PCAN_UFD_CLK_40MHZ		0x2
84 #define PCAN_UFD_CLK_30MHZ		0x3
85 #define PCAN_UFD_CLK_24MHZ		0x4
86 #define PCAN_UFD_CLK_20MHZ		0x5
87 #define PCAN_UFD_CLK_DEF		PCAN_UFD_CLK_80MHZ
88 
89 struct __packed pcan_ufd_clock {
90 	__le16	opcode_channel;
91 
92 	u8	mode;
93 	u8	unused[5];
94 };
95 
96 /* LED control command */
97 #define PCAN_UFD_CMD_LED_SET		0x86
98 
99 #define PCAN_UFD_LED_DEV		0x00
100 #define PCAN_UFD_LED_FAST		0x01
101 #define PCAN_UFD_LED_SLOW		0x02
102 #define PCAN_UFD_LED_ON			0x03
103 #define PCAN_UFD_LED_OFF		0x04
104 #define PCAN_UFD_LED_DEF		PCAN_UFD_LED_DEV
105 
106 struct __packed pcan_ufd_led {
107 	__le16	opcode_channel;
108 
109 	u8	mode;
110 	u8	unused[5];
111 };
112 
113 /* Extended usage of uCAN commands CMD_xxx_xx_OPTION for PCAN-USB Pro FD */
114 #define PCAN_UFD_FLTEXT_CALIBRATION	0x8000
115 
116 struct __packed pcan_ufd_options {
117 	__le16	opcode_channel;
118 
119 	__le16	ucan_mask;
120 	u16	unused;
121 	__le16	usb_mask;
122 };
123 
124 /* Extended usage of uCAN messages for PCAN-USB Pro FD */
125 #define PCAN_UFD_MSG_CALIBRATION	0x100
126 
127 struct __packed pcan_ufd_ts_msg {
128 	__le16	size;
129 	__le16	type;
130 	__le32	ts_low;
131 	__le32	ts_high;
132 	__le16	usb_frame_index;
133 	u16	unused;
134 };
135 
136 #define PCAN_UFD_MSG_OVERRUN		0x101
137 
138 #define PCAN_UFD_OVMSG_CHANNEL(o)	((o)->channel & 0xf)
139 
140 struct __packed pcan_ufd_ovr_msg {
141 	__le16	size;
142 	__le16	type;
143 	__le32	ts_low;
144 	__le32	ts_high;
145 	u8	channel;
146 	u8	unused[3];
147 };
148 
149 static inline int pufd_omsg_get_channel(struct pcan_ufd_ovr_msg *om)
150 {
151 	return om->channel & 0xf;
152 }
153 
154 /* Clock mode frequency values */
155 static const u32 pcan_usb_fd_clk_freq[6] = {
156 	[PCAN_UFD_CLK_80MHZ] = 80000000,
157 	[PCAN_UFD_CLK_60MHZ] = 60000000,
158 	[PCAN_UFD_CLK_40MHZ] = 40000000,
159 	[PCAN_UFD_CLK_30MHZ] = 30000000,
160 	[PCAN_UFD_CLK_24MHZ] = 24000000,
161 	[PCAN_UFD_CLK_20MHZ] = 20000000
162 };
163 
164 /* return a device USB interface */
165 static inline
166 struct pcan_usb_fd_if *pcan_usb_fd_dev_if(struct peak_usb_device *dev)
167 {
168 	struct pcan_usb_fd_device *pdev =
169 			container_of(dev, struct pcan_usb_fd_device, dev);
170 	return pdev->usb_if;
171 }
172 
173 /* return a device USB commands buffer */
174 static inline void *pcan_usb_fd_cmd_buffer(struct peak_usb_device *dev)
175 {
176 	struct pcan_usb_fd_device *pdev =
177 			container_of(dev, struct pcan_usb_fd_device, dev);
178 	return pdev->cmd_buffer_addr;
179 }
180 
181 /* send PCAN-USB Pro FD commands synchronously */
182 static int pcan_usb_fd_send_cmd(struct peak_usb_device *dev, void *cmd_tail)
183 {
184 	void *cmd_head = pcan_usb_fd_cmd_buffer(dev);
185 	int err = 0;
186 	u8 *packet_ptr;
187 	int packet_len;
188 	ptrdiff_t cmd_len;
189 
190 	/* usb device unregistered? */
191 	if (!(dev->state & PCAN_USB_STATE_CONNECTED))
192 		return 0;
193 
194 	/* if a packet is not filled completely by commands, the command list
195 	 * is terminated with an "end of collection" record.
196 	 */
197 	cmd_len = cmd_tail - cmd_head;
198 	if (cmd_len <= (PCAN_UFD_CMD_BUFFER_SIZE - sizeof(u64))) {
199 		memset(cmd_tail, 0xff, sizeof(u64));
200 		cmd_len += sizeof(u64);
201 	}
202 
203 	packet_ptr = cmd_head;
204 	packet_len = cmd_len;
205 
206 	/* firmware is not able to re-assemble 512 bytes buffer in full-speed */
207 	if (unlikely(dev->udev->speed != USB_SPEED_HIGH))
208 		packet_len = min(packet_len, PCAN_UFD_LOSPD_PKT_SIZE);
209 
210 	do {
211 		err = usb_bulk_msg(dev->udev,
212 				   usb_sndbulkpipe(dev->udev,
213 						   PCAN_USBPRO_EP_CMDOUT),
214 				   packet_ptr, packet_len,
215 				   NULL, PCAN_UFD_CMD_TIMEOUT_MS);
216 		if (err) {
217 			netdev_err(dev->netdev,
218 				   "sending command failure: %d\n", err);
219 			break;
220 		}
221 
222 		packet_ptr += packet_len;
223 		cmd_len -= packet_len;
224 
225 		if (cmd_len < PCAN_UFD_LOSPD_PKT_SIZE)
226 			packet_len = cmd_len;
227 
228 	} while (packet_len > 0);
229 
230 	return err;
231 }
232 
233 /* build the commands list in the given buffer, to enter operational mode */
234 static int pcan_usb_fd_build_restart_cmd(struct peak_usb_device *dev, u8 *buf)
235 {
236 	struct pucan_wr_err_cnt *prc;
237 	struct pucan_command *cmd;
238 	u8 *pc = buf;
239 
240 	/* 1st, reset error counters: */
241 	prc = (struct pucan_wr_err_cnt *)pc;
242 	prc->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx,
243 						       PUCAN_CMD_WR_ERR_CNT);
244 
245 	/* select both counters */
246 	prc->sel_mask = cpu_to_le16(PUCAN_WRERRCNT_TE|PUCAN_WRERRCNT_RE);
247 
248 	/* and reset their values */
249 	prc->tx_counter = 0;
250 	prc->rx_counter = 0;
251 
252 	/* moves the pointer forward */
253 	pc += sizeof(struct pucan_wr_err_cnt);
254 
255 	/* add command to switch from ISO to non-ISO mode, if fw allows it */
256 	if (dev->can.ctrlmode_supported & CAN_CTRLMODE_FD_NON_ISO) {
257 		struct pucan_options *puo = (struct pucan_options *)pc;
258 
259 		puo->opcode_channel =
260 			(dev->can.ctrlmode & CAN_CTRLMODE_FD_NON_ISO) ?
261 			pucan_cmd_opcode_channel(dev->ctrl_idx,
262 						 PUCAN_CMD_CLR_DIS_OPTION) :
263 			pucan_cmd_opcode_channel(dev->ctrl_idx,
264 						 PUCAN_CMD_SET_EN_OPTION);
265 
266 		puo->options = cpu_to_le16(PUCAN_OPTION_CANDFDISO);
267 
268 		/* to be sure that no other extended bits will be taken into
269 		 * account
270 		 */
271 		puo->unused = 0;
272 
273 		/* moves the pointer forward */
274 		pc += sizeof(struct pucan_options);
275 	}
276 
277 	/* next, go back to operational mode */
278 	cmd = (struct pucan_command *)pc;
279 	cmd->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx,
280 				(dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) ?
281 						PUCAN_CMD_LISTEN_ONLY_MODE :
282 						PUCAN_CMD_NORMAL_MODE);
283 	pc += sizeof(struct pucan_command);
284 
285 	return pc - buf;
286 }
287 
288 /* set CAN bus on/off */
289 static int pcan_usb_fd_set_bus(struct peak_usb_device *dev, u8 onoff)
290 {
291 	u8 *pc = pcan_usb_fd_cmd_buffer(dev);
292 	int l;
293 
294 	if (onoff) {
295 		/* build the cmds list to enter operational mode */
296 		l = pcan_usb_fd_build_restart_cmd(dev, pc);
297 	} else {
298 		struct pucan_command *cmd = (struct pucan_command *)pc;
299 
300 		/* build cmd to go back to reset mode */
301 		cmd->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx,
302 							PUCAN_CMD_RESET_MODE);
303 		l = sizeof(struct pucan_command);
304 	}
305 
306 	/* send the command */
307 	return pcan_usb_fd_send_cmd(dev, pc + l);
308 }
309 
310 /* set filtering masks:
311  *
312  *	idx  in range [0..63] selects a row #idx, all rows otherwise
313  *	mask in range [0..0xffffffff] defines up to 32 CANIDs in the row(s)
314  *
315  *	Each bit of this 64 x 32 bits array defines a CANID value:
316  *
317  *	bit[i,j] = 1 implies that CANID=(i x 32)+j will be received, while
318  *	bit[i,j] = 0 implies that CANID=(i x 32)+j will be discarded.
319  */
320 static int pcan_usb_fd_set_filter_std(struct peak_usb_device *dev, int idx,
321 				      u32 mask)
322 {
323 	struct pucan_filter_std *cmd = pcan_usb_fd_cmd_buffer(dev);
324 	int i, n;
325 
326 	/* select all rows when idx is out of range [0..63] */
327 	if ((idx < 0) || (idx >= (1 << PUCAN_FLTSTD_ROW_IDX_BITS))) {
328 		n = 1 << PUCAN_FLTSTD_ROW_IDX_BITS;
329 		idx = 0;
330 
331 	/* select the row (and only the row) otherwise */
332 	} else {
333 		n = idx + 1;
334 	}
335 
336 	for (i = idx; i < n; i++, cmd++) {
337 		cmd->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx,
338 							PUCAN_CMD_FILTER_STD);
339 		cmd->idx = cpu_to_le16(i);
340 		cmd->mask = cpu_to_le32(mask);
341 	}
342 
343 	/* send the command */
344 	return pcan_usb_fd_send_cmd(dev, cmd);
345 }
346 
347 /* set/unset options
348  *
349  *	onoff	set(1)/unset(0) options
350  *	mask	each bit defines a kind of options to set/unset
351  */
352 static int pcan_usb_fd_set_options(struct peak_usb_device *dev,
353 				   bool onoff, u16 ucan_mask, u16 usb_mask)
354 {
355 	struct pcan_ufd_options *cmd = pcan_usb_fd_cmd_buffer(dev);
356 
357 	cmd->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx,
358 					(onoff) ? PUCAN_CMD_SET_EN_OPTION :
359 						  PUCAN_CMD_CLR_DIS_OPTION);
360 
361 	cmd->ucan_mask = cpu_to_le16(ucan_mask);
362 	cmd->usb_mask = cpu_to_le16(usb_mask);
363 
364 	/* send the command */
365 	return pcan_usb_fd_send_cmd(dev, ++cmd);
366 }
367 
368 /* setup LED control */
369 static int pcan_usb_fd_set_can_led(struct peak_usb_device *dev, u8 led_mode)
370 {
371 	struct pcan_ufd_led *cmd = pcan_usb_fd_cmd_buffer(dev);
372 
373 	cmd->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx,
374 						       PCAN_UFD_CMD_LED_SET);
375 	cmd->mode = led_mode;
376 
377 	/* send the command */
378 	return pcan_usb_fd_send_cmd(dev, ++cmd);
379 }
380 
381 /* set CAN clock domain */
382 static int pcan_usb_fd_set_clock_domain(struct peak_usb_device *dev,
383 					u8 clk_mode)
384 {
385 	struct pcan_ufd_clock *cmd = pcan_usb_fd_cmd_buffer(dev);
386 
387 	cmd->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx,
388 						       PCAN_UFD_CMD_CLK_SET);
389 	cmd->mode = clk_mode;
390 
391 	/* send the command */
392 	return pcan_usb_fd_send_cmd(dev, ++cmd);
393 }
394 
395 /* set bittiming for CAN and CAN-FD header */
396 static int pcan_usb_fd_set_bittiming_slow(struct peak_usb_device *dev,
397 					  struct can_bittiming *bt)
398 {
399 	struct pucan_timing_slow *cmd = pcan_usb_fd_cmd_buffer(dev);
400 
401 	cmd->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx,
402 						       PUCAN_CMD_TIMING_SLOW);
403 	cmd->sjw_t = PUCAN_TSLOW_SJW_T(bt->sjw - 1,
404 				dev->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES);
405 
406 	cmd->tseg2 = PUCAN_TSLOW_TSEG2(bt->phase_seg2 - 1);
407 	cmd->tseg1 = PUCAN_TSLOW_TSEG1(bt->prop_seg + bt->phase_seg1 - 1);
408 	cmd->brp = cpu_to_le16(PUCAN_TSLOW_BRP(bt->brp - 1));
409 
410 	cmd->ewl = 96;	/* default */
411 
412 	/* send the command */
413 	return pcan_usb_fd_send_cmd(dev, ++cmd);
414 }
415 
416 /* set CAN-FD bittiming for data */
417 static int pcan_usb_fd_set_bittiming_fast(struct peak_usb_device *dev,
418 					  struct can_bittiming *bt)
419 {
420 	struct pucan_timing_fast *cmd = pcan_usb_fd_cmd_buffer(dev);
421 
422 	cmd->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx,
423 						       PUCAN_CMD_TIMING_FAST);
424 	cmd->sjw = PUCAN_TFAST_SJW(bt->sjw - 1);
425 	cmd->tseg2 = PUCAN_TFAST_TSEG2(bt->phase_seg2 - 1);
426 	cmd->tseg1 = PUCAN_TFAST_TSEG1(bt->prop_seg + bt->phase_seg1 - 1);
427 	cmd->brp = cpu_to_le16(PUCAN_TFAST_BRP(bt->brp - 1));
428 
429 	/* send the command */
430 	return pcan_usb_fd_send_cmd(dev, ++cmd);
431 }
432 
433 /* handle restart but in asynchronously way
434  * (uses PCAN-USB Pro code to complete asynchronous request)
435  */
436 static int pcan_usb_fd_restart_async(struct peak_usb_device *dev,
437 				     struct urb *urb, u8 *buf)
438 {
439 	u8 *pc = buf;
440 
441 	/* build the entire cmds list in the provided buffer, to go back into
442 	 * operational mode.
443 	 */
444 	pc += pcan_usb_fd_build_restart_cmd(dev, pc);
445 
446 	/* add EOC */
447 	memset(pc, 0xff, sizeof(struct pucan_command));
448 	pc += sizeof(struct pucan_command);
449 
450 	/* complete the URB */
451 	usb_fill_bulk_urb(urb, dev->udev,
452 			  usb_sndbulkpipe(dev->udev, PCAN_USBPRO_EP_CMDOUT),
453 			  buf, pc - buf,
454 			  pcan_usb_pro_restart_complete, dev);
455 
456 	/* and submit it. */
457 	return usb_submit_urb(urb, GFP_ATOMIC);
458 }
459 
460 static int pcan_usb_fd_drv_loaded(struct peak_usb_device *dev, bool loaded)
461 {
462 	struct pcan_usb_fd_device *pdev =
463 			container_of(dev, struct pcan_usb_fd_device, dev);
464 
465 	pdev->cmd_buffer_addr[0] = 0;
466 	pdev->cmd_buffer_addr[1] = !!loaded;
467 
468 	return pcan_usb_pro_send_req(dev,
469 				PCAN_USBPRO_REQ_FCT,
470 				PCAN_USBPRO_FCT_DRVLD,
471 				pdev->cmd_buffer_addr,
472 				PCAN_USBPRO_FCT_DRVLD_REQ_LEN);
473 }
474 
475 static int pcan_usb_fd_decode_canmsg(struct pcan_usb_fd_if *usb_if,
476 				     struct pucan_msg *rx_msg)
477 {
478 	struct pucan_rx_msg *rm = (struct pucan_rx_msg *)rx_msg;
479 	struct peak_usb_device *dev = usb_if->dev[pucan_msg_get_channel(rm)];
480 	struct net_device *netdev = dev->netdev;
481 	struct canfd_frame *cfd;
482 	struct sk_buff *skb;
483 	const u16 rx_msg_flags = le16_to_cpu(rm->flags);
484 
485 	if (rx_msg_flags & PUCAN_MSG_EXT_DATA_LEN) {
486 		/* CANFD frame case */
487 		skb = alloc_canfd_skb(netdev, &cfd);
488 		if (!skb)
489 			return -ENOMEM;
490 
491 		if (rx_msg_flags & PUCAN_MSG_BITRATE_SWITCH)
492 			cfd->flags |= CANFD_BRS;
493 
494 		if (rx_msg_flags & PUCAN_MSG_ERROR_STATE_IND)
495 			cfd->flags |= CANFD_ESI;
496 
497 		cfd->len = can_dlc2len(get_canfd_dlc(pucan_msg_get_dlc(rm)));
498 	} else {
499 		/* CAN 2.0 frame case */
500 		skb = alloc_can_skb(netdev, (struct can_frame **)&cfd);
501 		if (!skb)
502 			return -ENOMEM;
503 
504 		cfd->len = get_can_dlc(pucan_msg_get_dlc(rm));
505 	}
506 
507 	cfd->can_id = le32_to_cpu(rm->can_id);
508 
509 	if (rx_msg_flags & PUCAN_MSG_EXT_ID)
510 		cfd->can_id |= CAN_EFF_FLAG;
511 
512 	if (rx_msg_flags & PUCAN_MSG_RTR)
513 		cfd->can_id |= CAN_RTR_FLAG;
514 	else
515 		memcpy(cfd->data, rm->d, cfd->len);
516 
517 	peak_usb_netif_rx(skb, &usb_if->time_ref, le32_to_cpu(rm->ts_low));
518 
519 	netdev->stats.rx_packets++;
520 	netdev->stats.rx_bytes += cfd->len;
521 
522 	return 0;
523 }
524 
525 /* handle uCAN status message */
526 static int pcan_usb_fd_decode_status(struct pcan_usb_fd_if *usb_if,
527 				     struct pucan_msg *rx_msg)
528 {
529 	struct pucan_status_msg *sm = (struct pucan_status_msg *)rx_msg;
530 	struct peak_usb_device *dev = usb_if->dev[pucan_stmsg_get_channel(sm)];
531 	struct pcan_usb_fd_device *pdev =
532 			container_of(dev, struct pcan_usb_fd_device, dev);
533 	enum can_state new_state = CAN_STATE_ERROR_ACTIVE;
534 	enum can_state rx_state, tx_state;
535 	struct net_device *netdev = dev->netdev;
536 	struct can_frame *cf;
537 	struct sk_buff *skb;
538 
539 	/* nothing should be sent while in BUS_OFF state */
540 	if (dev->can.state == CAN_STATE_BUS_OFF)
541 		return 0;
542 
543 	if (sm->channel_p_w_b & PUCAN_BUS_BUSOFF) {
544 		new_state = CAN_STATE_BUS_OFF;
545 	} else if (sm->channel_p_w_b & PUCAN_BUS_PASSIVE) {
546 		new_state = CAN_STATE_ERROR_PASSIVE;
547 	} else if (sm->channel_p_w_b & PUCAN_BUS_WARNING) {
548 		new_state = CAN_STATE_ERROR_WARNING;
549 	} else {
550 		/* no error bit (so, no error skb, back to active state) */
551 		dev->can.state = CAN_STATE_ERROR_ACTIVE;
552 		pdev->bec.txerr = 0;
553 		pdev->bec.rxerr = 0;
554 		return 0;
555 	}
556 
557 	/* state hasn't changed */
558 	if (new_state == dev->can.state)
559 		return 0;
560 
561 	/* handle bus state change */
562 	tx_state = (pdev->bec.txerr >= pdev->bec.rxerr) ? new_state : 0;
563 	rx_state = (pdev->bec.txerr <= pdev->bec.rxerr) ? new_state : 0;
564 
565 	/* allocate an skb to store the error frame */
566 	skb = alloc_can_err_skb(netdev, &cf);
567 	if (skb)
568 		can_change_state(netdev, cf, tx_state, rx_state);
569 
570 	/* things must be done even in case of OOM */
571 	if (new_state == CAN_STATE_BUS_OFF)
572 		can_bus_off(netdev);
573 
574 	if (!skb)
575 		return -ENOMEM;
576 
577 	peak_usb_netif_rx(skb, &usb_if->time_ref, le32_to_cpu(sm->ts_low));
578 
579 	netdev->stats.rx_packets++;
580 	netdev->stats.rx_bytes += cf->can_dlc;
581 
582 	return 0;
583 }
584 
585 /* handle uCAN error message */
586 static int pcan_usb_fd_decode_error(struct pcan_usb_fd_if *usb_if,
587 				    struct pucan_msg *rx_msg)
588 {
589 	struct pucan_error_msg *er = (struct pucan_error_msg *)rx_msg;
590 	struct peak_usb_device *dev = usb_if->dev[pucan_ermsg_get_channel(er)];
591 	struct pcan_usb_fd_device *pdev =
592 			container_of(dev, struct pcan_usb_fd_device, dev);
593 
594 	/* keep a trace of tx and rx error counters for later use */
595 	pdev->bec.txerr = er->tx_err_cnt;
596 	pdev->bec.rxerr = er->rx_err_cnt;
597 
598 	return 0;
599 }
600 
601 /* handle uCAN overrun message */
602 static int pcan_usb_fd_decode_overrun(struct pcan_usb_fd_if *usb_if,
603 				      struct pucan_msg *rx_msg)
604 {
605 	struct pcan_ufd_ovr_msg *ov = (struct pcan_ufd_ovr_msg *)rx_msg;
606 	struct peak_usb_device *dev = usb_if->dev[pufd_omsg_get_channel(ov)];
607 	struct net_device *netdev = dev->netdev;
608 	struct can_frame *cf;
609 	struct sk_buff *skb;
610 
611 	/* allocate an skb to store the error frame */
612 	skb = alloc_can_err_skb(netdev, &cf);
613 	if (!skb)
614 		return -ENOMEM;
615 
616 	cf->can_id |= CAN_ERR_CRTL;
617 	cf->data[1] |= CAN_ERR_CRTL_RX_OVERFLOW;
618 
619 	peak_usb_netif_rx(skb, &usb_if->time_ref, le32_to_cpu(ov->ts_low));
620 
621 	netdev->stats.rx_over_errors++;
622 	netdev->stats.rx_errors++;
623 
624 	return 0;
625 }
626 
627 /* handle USB calibration message */
628 static void pcan_usb_fd_decode_ts(struct pcan_usb_fd_if *usb_if,
629 				  struct pucan_msg *rx_msg)
630 {
631 	struct pcan_ufd_ts_msg *ts = (struct pcan_ufd_ts_msg *)rx_msg;
632 
633 	/* should wait until clock is stabilized */
634 	if (usb_if->cm_ignore_count > 0)
635 		usb_if->cm_ignore_count--;
636 	else
637 		peak_usb_set_ts_now(&usb_if->time_ref, le32_to_cpu(ts->ts_low));
638 }
639 
640 /* callback for bulk IN urb */
641 static int pcan_usb_fd_decode_buf(struct peak_usb_device *dev, struct urb *urb)
642 {
643 	struct pcan_usb_fd_if *usb_if = pcan_usb_fd_dev_if(dev);
644 	struct net_device *netdev = dev->netdev;
645 	struct pucan_msg *rx_msg;
646 	u8 *msg_ptr, *msg_end;
647 	int err = 0;
648 
649 	/* loop reading all the records from the incoming message */
650 	msg_ptr = urb->transfer_buffer;
651 	msg_end = urb->transfer_buffer + urb->actual_length;
652 	for (; msg_ptr < msg_end;) {
653 		u16 rx_msg_type, rx_msg_size;
654 
655 		rx_msg = (struct pucan_msg *)msg_ptr;
656 		if (!rx_msg->size) {
657 			/* null packet found: end of list */
658 			break;
659 		}
660 
661 		rx_msg_size = le16_to_cpu(rx_msg->size);
662 		rx_msg_type = le16_to_cpu(rx_msg->type);
663 
664 		/* check if the record goes out of current packet */
665 		if (msg_ptr + rx_msg_size > msg_end) {
666 			netdev_err(netdev,
667 				   "got frag rec: should inc usb rx buf sze\n");
668 			err = -EBADMSG;
669 			break;
670 		}
671 
672 		switch (rx_msg_type) {
673 		case PUCAN_MSG_CAN_RX:
674 			err = pcan_usb_fd_decode_canmsg(usb_if, rx_msg);
675 			if (err < 0)
676 				goto fail;
677 			break;
678 
679 		case PCAN_UFD_MSG_CALIBRATION:
680 			pcan_usb_fd_decode_ts(usb_if, rx_msg);
681 			break;
682 
683 		case PUCAN_MSG_ERROR:
684 			err = pcan_usb_fd_decode_error(usb_if, rx_msg);
685 			if (err < 0)
686 				goto fail;
687 			break;
688 
689 		case PUCAN_MSG_STATUS:
690 			err = pcan_usb_fd_decode_status(usb_if, rx_msg);
691 			if (err < 0)
692 				goto fail;
693 			break;
694 
695 		case PCAN_UFD_MSG_OVERRUN:
696 			err = pcan_usb_fd_decode_overrun(usb_if, rx_msg);
697 			if (err < 0)
698 				goto fail;
699 			break;
700 
701 		default:
702 			netdev_err(netdev,
703 				   "unhandled msg type 0x%02x (%d): ignored\n",
704 				   rx_msg_type, rx_msg_type);
705 			break;
706 		}
707 
708 		msg_ptr += rx_msg_size;
709 	}
710 
711 fail:
712 	if (err)
713 		pcan_dump_mem("received msg",
714 			      urb->transfer_buffer, urb->actual_length);
715 	return err;
716 }
717 
718 /* CAN/CANFD frames encoding callback */
719 static int pcan_usb_fd_encode_msg(struct peak_usb_device *dev,
720 				  struct sk_buff *skb, u8 *obuf, size_t *size)
721 {
722 	struct pucan_tx_msg *tx_msg = (struct pucan_tx_msg *)obuf;
723 	struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
724 	u16 tx_msg_size, tx_msg_flags;
725 	u8 can_dlc;
726 
727 	tx_msg_size = ALIGN(sizeof(struct pucan_tx_msg) + cfd->len, 4);
728 	tx_msg->size = cpu_to_le16(tx_msg_size);
729 	tx_msg->type = cpu_to_le16(PUCAN_MSG_CAN_TX);
730 
731 	tx_msg_flags = 0;
732 	if (cfd->can_id & CAN_EFF_FLAG) {
733 		tx_msg_flags |= PUCAN_MSG_EXT_ID;
734 		tx_msg->can_id = cpu_to_le32(cfd->can_id & CAN_EFF_MASK);
735 	} else {
736 		tx_msg->can_id = cpu_to_le32(cfd->can_id & CAN_SFF_MASK);
737 	}
738 
739 	if (can_is_canfd_skb(skb)) {
740 		/* considering a CANFD frame */
741 		can_dlc = can_len2dlc(cfd->len);
742 
743 		tx_msg_flags |= PUCAN_MSG_EXT_DATA_LEN;
744 
745 		if (cfd->flags & CANFD_BRS)
746 			tx_msg_flags |= PUCAN_MSG_BITRATE_SWITCH;
747 
748 		if (cfd->flags & CANFD_ESI)
749 			tx_msg_flags |= PUCAN_MSG_ERROR_STATE_IND;
750 	} else {
751 		/* CAND 2.0 frames */
752 		can_dlc = cfd->len;
753 
754 		if (cfd->can_id & CAN_RTR_FLAG)
755 			tx_msg_flags |= PUCAN_MSG_RTR;
756 	}
757 
758 	tx_msg->flags = cpu_to_le16(tx_msg_flags);
759 	tx_msg->channel_dlc = PUCAN_MSG_CHANNEL_DLC(dev->ctrl_idx, can_dlc);
760 	memcpy(tx_msg->d, cfd->data, cfd->len);
761 
762 	/* add null size message to tag the end (messages are 32-bits aligned)
763 	 */
764 	tx_msg = (struct pucan_tx_msg *)(obuf + tx_msg_size);
765 
766 	tx_msg->size = 0;
767 
768 	/* set the whole size of the USB packet to send */
769 	*size = tx_msg_size + sizeof(u32);
770 
771 	return 0;
772 }
773 
774 /* start the interface (last chance before set bus on) */
775 static int pcan_usb_fd_start(struct peak_usb_device *dev)
776 {
777 	struct pcan_usb_fd_device *pdev =
778 			container_of(dev, struct pcan_usb_fd_device, dev);
779 	int err;
780 
781 	/* set filter mode: all acceptance */
782 	err = pcan_usb_fd_set_filter_std(dev, -1, 0xffffffff);
783 	if (err)
784 		return err;
785 
786 	/* opening first device: */
787 	if (pdev->usb_if->dev_opened_count == 0) {
788 		/* reset time_ref */
789 		peak_usb_init_time_ref(&pdev->usb_if->time_ref,
790 				       &pcan_usb_pro_fd);
791 
792 		/* enable USB calibration messages */
793 		err = pcan_usb_fd_set_options(dev, 1,
794 					      PUCAN_OPTION_ERROR,
795 					      PCAN_UFD_FLTEXT_CALIBRATION);
796 	}
797 
798 	pdev->usb_if->dev_opened_count++;
799 
800 	/* reset cached error counters */
801 	pdev->bec.txerr = 0;
802 	pdev->bec.rxerr = 0;
803 
804 	return err;
805 }
806 
807 /* socket callback used to copy berr counters values receieved through USB */
808 static int pcan_usb_fd_get_berr_counter(const struct net_device *netdev,
809 					struct can_berr_counter *bec)
810 {
811 	struct peak_usb_device *dev = netdev_priv(netdev);
812 	struct pcan_usb_fd_device *pdev =
813 			container_of(dev, struct pcan_usb_fd_device, dev);
814 
815 	*bec = pdev->bec;
816 
817 	/* must return 0 */
818 	return 0;
819 }
820 
821 /* stop interface (last chance before set bus off) */
822 static int pcan_usb_fd_stop(struct peak_usb_device *dev)
823 {
824 	struct pcan_usb_fd_device *pdev =
825 			container_of(dev, struct pcan_usb_fd_device, dev);
826 
827 	/* turn off special msgs for that interface if no other dev opened */
828 	if (pdev->usb_if->dev_opened_count == 1)
829 		pcan_usb_fd_set_options(dev, 0,
830 					PUCAN_OPTION_ERROR,
831 					PCAN_UFD_FLTEXT_CALIBRATION);
832 	pdev->usb_if->dev_opened_count--;
833 
834 	return 0;
835 }
836 
837 /* called when probing, to initialize a device object */
838 static int pcan_usb_fd_init(struct peak_usb_device *dev)
839 {
840 	struct pcan_usb_fd_device *pdev =
841 			container_of(dev, struct pcan_usb_fd_device, dev);
842 	int i, err = -ENOMEM;
843 
844 	/* do this for 1st channel only */
845 	if (!dev->prev_siblings) {
846 		/* allocate netdevices common structure attached to first one */
847 		pdev->usb_if = kzalloc(sizeof(*pdev->usb_if), GFP_KERNEL);
848 		if (!pdev->usb_if)
849 			goto err_out;
850 
851 		/* allocate command buffer once for all for the interface */
852 		pdev->cmd_buffer_addr = kmalloc(PCAN_UFD_CMD_BUFFER_SIZE,
853 						GFP_KERNEL);
854 		if (!pdev->cmd_buffer_addr)
855 			goto err_out_1;
856 
857 		/* number of ts msgs to ignore before taking one into account */
858 		pdev->usb_if->cm_ignore_count = 5;
859 
860 		err = pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_INFO,
861 					    PCAN_USBPRO_INFO_FW,
862 					    &pdev->usb_if->fw_info,
863 					    sizeof(pdev->usb_if->fw_info));
864 		if (err) {
865 			dev_err(dev->netdev->dev.parent,
866 				"unable to read %s firmware info (err %d)\n",
867 				dev->adapter->name, err);
868 			goto err_out_2;
869 		}
870 
871 		/* explicit use of dev_xxx() instead of netdev_xxx() here:
872 		 * information displayed are related to the device itself, not
873 		 * to the canx (channel) device.
874 		 */
875 		dev_info(dev->netdev->dev.parent,
876 			 "PEAK-System %s v%u fw v%u.%u.%u (%u channels)\n",
877 			 dev->adapter->name, pdev->usb_if->fw_info.hw_version,
878 			 pdev->usb_if->fw_info.fw_version[0],
879 			 pdev->usb_if->fw_info.fw_version[1],
880 			 pdev->usb_if->fw_info.fw_version[2],
881 			 dev->adapter->ctrl_count);
882 
883 		/* check for ability to switch between ISO/non-ISO modes */
884 		if (pdev->usb_if->fw_info.fw_version[0] >= 2) {
885 			/* firmware >= 2.x supports ISO/non-ISO switching */
886 			dev->can.ctrlmode_supported |= CAN_CTRLMODE_FD_NON_ISO;
887 		} else {
888 			/* firmware < 2.x only supports fixed(!) non-ISO */
889 			dev->can.ctrlmode |= CAN_CTRLMODE_FD_NON_ISO;
890 		}
891 
892 		/* tell the hardware the can driver is running */
893 		err = pcan_usb_fd_drv_loaded(dev, 1);
894 		if (err) {
895 			dev_err(dev->netdev->dev.parent,
896 				"unable to tell %s driver is loaded (err %d)\n",
897 				dev->adapter->name, err);
898 			goto err_out_2;
899 		}
900 	} else {
901 		/* otherwise, simply copy previous sibling's values */
902 		struct pcan_usb_fd_device *ppdev =
903 			container_of(dev->prev_siblings,
904 				     struct pcan_usb_fd_device, dev);
905 
906 		pdev->usb_if = ppdev->usb_if;
907 		pdev->cmd_buffer_addr = ppdev->cmd_buffer_addr;
908 
909 		/* do a copy of the ctrlmode[_supported] too */
910 		dev->can.ctrlmode = ppdev->dev.can.ctrlmode;
911 		dev->can.ctrlmode_supported = ppdev->dev.can.ctrlmode_supported;
912 	}
913 
914 	pdev->usb_if->dev[dev->ctrl_idx] = dev;
915 	dev->device_number =
916 		le32_to_cpu(pdev->usb_if->fw_info.dev_id[dev->ctrl_idx]);
917 
918 	/* set clock domain */
919 	for (i = 0; i < ARRAY_SIZE(pcan_usb_fd_clk_freq); i++)
920 		if (dev->adapter->clock.freq == pcan_usb_fd_clk_freq[i])
921 			break;
922 
923 	if (i >= ARRAY_SIZE(pcan_usb_fd_clk_freq)) {
924 		dev_warn(dev->netdev->dev.parent,
925 			 "incompatible clock frequencies\n");
926 		err = -EINVAL;
927 		goto err_out_2;
928 	}
929 
930 	pcan_usb_fd_set_clock_domain(dev, i);
931 
932 	/* set LED in default state (end of init phase) */
933 	pcan_usb_fd_set_can_led(dev, PCAN_UFD_LED_DEF);
934 
935 	return 0;
936 
937 err_out_2:
938 	kfree(pdev->cmd_buffer_addr);
939 err_out_1:
940 	kfree(pdev->usb_if);
941 err_out:
942 	return err;
943 }
944 
945 /* called when driver module is being unloaded */
946 static void pcan_usb_fd_exit(struct peak_usb_device *dev)
947 {
948 	struct pcan_usb_fd_device *pdev =
949 			container_of(dev, struct pcan_usb_fd_device, dev);
950 
951 	/* when rmmod called before unplug and if down, should reset things
952 	 * before leaving
953 	 */
954 	if (dev->can.state != CAN_STATE_STOPPED) {
955 		/* set bus off on the corresponding channel */
956 		pcan_usb_fd_set_bus(dev, 0);
957 	}
958 
959 	/* switch off corresponding CAN LEDs */
960 	pcan_usb_fd_set_can_led(dev, PCAN_UFD_LED_OFF);
961 
962 	/* if channel #0 (only) */
963 	if (dev->ctrl_idx == 0) {
964 		/* turn off calibration message if any device were opened */
965 		if (pdev->usb_if->dev_opened_count > 0)
966 			pcan_usb_fd_set_options(dev, 0,
967 						PUCAN_OPTION_ERROR,
968 						PCAN_UFD_FLTEXT_CALIBRATION);
969 
970 		/* tell USB adapter that the driver is being unloaded */
971 		pcan_usb_fd_drv_loaded(dev, 0);
972 	}
973 }
974 
975 /* called when the USB adapter is unplugged */
976 static void pcan_usb_fd_free(struct peak_usb_device *dev)
977 {
978 	/* last device: can free shared objects now */
979 	if (!dev->prev_siblings && !dev->next_siblings) {
980 		struct pcan_usb_fd_device *pdev =
981 			container_of(dev, struct pcan_usb_fd_device, dev);
982 
983 		/* free commands buffer */
984 		kfree(pdev->cmd_buffer_addr);
985 
986 		/* free usb interface object */
987 		kfree(pdev->usb_if);
988 	}
989 }
990 
991 /* describes the PCAN-USB FD adapter */
992 static const struct can_bittiming_const pcan_usb_fd_const = {
993 	.name = "pcan_usb_fd",
994 	.tseg1_min = 1,
995 	.tseg1_max = (1 << PUCAN_TSLOW_TSGEG1_BITS),
996 	.tseg2_min = 1,
997 	.tseg2_max = (1 << PUCAN_TSLOW_TSGEG2_BITS),
998 	.sjw_max = (1 << PUCAN_TSLOW_SJW_BITS),
999 	.brp_min = 1,
1000 	.brp_max = (1 << PUCAN_TSLOW_BRP_BITS),
1001 	.brp_inc = 1,
1002 };
1003 
1004 static const struct can_bittiming_const pcan_usb_fd_data_const = {
1005 	.name = "pcan_usb_fd",
1006 	.tseg1_min = 1,
1007 	.tseg1_max = (1 << PUCAN_TFAST_TSGEG1_BITS),
1008 	.tseg2_min = 1,
1009 	.tseg2_max = (1 << PUCAN_TFAST_TSGEG2_BITS),
1010 	.sjw_max = (1 << PUCAN_TFAST_SJW_BITS),
1011 	.brp_min = 1,
1012 	.brp_max = (1 << PUCAN_TFAST_BRP_BITS),
1013 	.brp_inc = 1,
1014 };
1015 
1016 const struct peak_usb_adapter pcan_usb_fd = {
1017 	.name = "PCAN-USB FD",
1018 	.device_id = PCAN_USBFD_PRODUCT_ID,
1019 	.ctrl_count = PCAN_USBFD_CHANNEL_COUNT,
1020 	.ctrlmode_supported = CAN_CTRLMODE_FD |
1021 			CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY,
1022 	.clock = {
1023 		.freq = PCAN_UFD_CRYSTAL_HZ,
1024 	},
1025 	.bittiming_const = &pcan_usb_fd_const,
1026 	.data_bittiming_const = &pcan_usb_fd_data_const,
1027 
1028 	/* size of device private data */
1029 	.sizeof_dev_private = sizeof(struct pcan_usb_fd_device),
1030 
1031 	/* timestamps usage */
1032 	.ts_used_bits = 32,
1033 	.ts_period = 1000000, /* calibration period in ts. */
1034 	.us_per_ts_scale = 1, /* us = (ts * scale) >> shift */
1035 	.us_per_ts_shift = 0,
1036 
1037 	/* give here messages in/out endpoints */
1038 	.ep_msg_in = PCAN_USBPRO_EP_MSGIN,
1039 	.ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0},
1040 
1041 	/* size of rx/tx usb buffers */
1042 	.rx_buffer_size = PCAN_UFD_RX_BUFFER_SIZE,
1043 	.tx_buffer_size = PCAN_UFD_TX_BUFFER_SIZE,
1044 
1045 	/* device callbacks */
1046 	.intf_probe = pcan_usb_pro_probe,	/* same as PCAN-USB Pro */
1047 	.dev_init = pcan_usb_fd_init,
1048 
1049 	.dev_exit = pcan_usb_fd_exit,
1050 	.dev_free = pcan_usb_fd_free,
1051 	.dev_set_bus = pcan_usb_fd_set_bus,
1052 	.dev_set_bittiming = pcan_usb_fd_set_bittiming_slow,
1053 	.dev_set_data_bittiming = pcan_usb_fd_set_bittiming_fast,
1054 	.dev_decode_buf = pcan_usb_fd_decode_buf,
1055 	.dev_start = pcan_usb_fd_start,
1056 	.dev_stop = pcan_usb_fd_stop,
1057 	.dev_restart_async = pcan_usb_fd_restart_async,
1058 	.dev_encode_msg = pcan_usb_fd_encode_msg,
1059 
1060 	.do_get_berr_counter = pcan_usb_fd_get_berr_counter,
1061 };
1062 
1063 /* describes the PCAN-CHIP USB */
1064 static const struct can_bittiming_const pcan_usb_chip_const = {
1065 	.name = "pcan_chip_usb",
1066 	.tseg1_min = 1,
1067 	.tseg1_max = (1 << PUCAN_TSLOW_TSGEG1_BITS),
1068 	.tseg2_min = 1,
1069 	.tseg2_max = (1 << PUCAN_TSLOW_TSGEG2_BITS),
1070 	.sjw_max = (1 << PUCAN_TSLOW_SJW_BITS),
1071 	.brp_min = 1,
1072 	.brp_max = (1 << PUCAN_TSLOW_BRP_BITS),
1073 	.brp_inc = 1,
1074 };
1075 
1076 static const struct can_bittiming_const pcan_usb_chip_data_const = {
1077 	.name = "pcan_chip_usb",
1078 	.tseg1_min = 1,
1079 	.tseg1_max = (1 << PUCAN_TFAST_TSGEG1_BITS),
1080 	.tseg2_min = 1,
1081 	.tseg2_max = (1 << PUCAN_TFAST_TSGEG2_BITS),
1082 	.sjw_max = (1 << PUCAN_TFAST_SJW_BITS),
1083 	.brp_min = 1,
1084 	.brp_max = (1 << PUCAN_TFAST_BRP_BITS),
1085 	.brp_inc = 1,
1086 };
1087 
1088 const struct peak_usb_adapter pcan_usb_chip = {
1089 	.name = "PCAN-Chip USB",
1090 	.device_id = PCAN_USBCHIP_PRODUCT_ID,
1091 	.ctrl_count = PCAN_USBFD_CHANNEL_COUNT,
1092 	.ctrlmode_supported = CAN_CTRLMODE_FD |
1093 		CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY,
1094 	.clock = {
1095 		.freq = PCAN_UFD_CRYSTAL_HZ,
1096 	},
1097 	.bittiming_const = &pcan_usb_chip_const,
1098 	.data_bittiming_const = &pcan_usb_chip_data_const,
1099 
1100 	/* size of device private data */
1101 	.sizeof_dev_private = sizeof(struct pcan_usb_fd_device),
1102 
1103 	/* timestamps usage */
1104 	.ts_used_bits = 32,
1105 	.ts_period = 1000000, /* calibration period in ts. */
1106 	.us_per_ts_scale = 1, /* us = (ts * scale) >> shift */
1107 	.us_per_ts_shift = 0,
1108 
1109 	/* give here messages in/out endpoints */
1110 	.ep_msg_in = PCAN_USBPRO_EP_MSGIN,
1111 	.ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0},
1112 
1113 	/* size of rx/tx usb buffers */
1114 	.rx_buffer_size = PCAN_UFD_RX_BUFFER_SIZE,
1115 	.tx_buffer_size = PCAN_UFD_TX_BUFFER_SIZE,
1116 
1117 	/* device callbacks */
1118 	.intf_probe = pcan_usb_pro_probe,	/* same as PCAN-USB Pro */
1119 	.dev_init = pcan_usb_fd_init,
1120 
1121 	.dev_exit = pcan_usb_fd_exit,
1122 	.dev_free = pcan_usb_fd_free,
1123 	.dev_set_bus = pcan_usb_fd_set_bus,
1124 	.dev_set_bittiming = pcan_usb_fd_set_bittiming_slow,
1125 	.dev_set_data_bittiming = pcan_usb_fd_set_bittiming_fast,
1126 	.dev_decode_buf = pcan_usb_fd_decode_buf,
1127 	.dev_start = pcan_usb_fd_start,
1128 	.dev_stop = pcan_usb_fd_stop,
1129 	.dev_restart_async = pcan_usb_fd_restart_async,
1130 	.dev_encode_msg = pcan_usb_fd_encode_msg,
1131 
1132 	.do_get_berr_counter = pcan_usb_fd_get_berr_counter,
1133 };
1134 
1135 /* describes the PCAN-USB Pro FD adapter */
1136 static const struct can_bittiming_const pcan_usb_pro_fd_const = {
1137 	.name = "pcan_usb_pro_fd",
1138 	.tseg1_min = 1,
1139 	.tseg1_max = (1 << PUCAN_TSLOW_TSGEG1_BITS),
1140 	.tseg2_min = 1,
1141 	.tseg2_max = (1 << PUCAN_TSLOW_TSGEG2_BITS),
1142 	.sjw_max = (1 << PUCAN_TSLOW_SJW_BITS),
1143 	.brp_min = 1,
1144 	.brp_max = (1 << PUCAN_TSLOW_BRP_BITS),
1145 	.brp_inc = 1,
1146 };
1147 
1148 static const struct can_bittiming_const pcan_usb_pro_fd_data_const = {
1149 	.name = "pcan_usb_pro_fd",
1150 	.tseg1_min = 1,
1151 	.tseg1_max = (1 << PUCAN_TFAST_TSGEG1_BITS),
1152 	.tseg2_min = 1,
1153 	.tseg2_max = (1 << PUCAN_TFAST_TSGEG2_BITS),
1154 	.sjw_max = (1 << PUCAN_TFAST_SJW_BITS),
1155 	.brp_min = 1,
1156 	.brp_max = (1 << PUCAN_TFAST_BRP_BITS),
1157 	.brp_inc = 1,
1158 };
1159 
1160 const struct peak_usb_adapter pcan_usb_pro_fd = {
1161 	.name = "PCAN-USB Pro FD",
1162 	.device_id = PCAN_USBPROFD_PRODUCT_ID,
1163 	.ctrl_count = PCAN_USBPROFD_CHANNEL_COUNT,
1164 	.ctrlmode_supported = CAN_CTRLMODE_FD |
1165 			CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY,
1166 	.clock = {
1167 		.freq = PCAN_UFD_CRYSTAL_HZ,
1168 	},
1169 	.bittiming_const = &pcan_usb_pro_fd_const,
1170 	.data_bittiming_const = &pcan_usb_pro_fd_data_const,
1171 
1172 	/* size of device private data */
1173 	.sizeof_dev_private = sizeof(struct pcan_usb_fd_device),
1174 
1175 	/* timestamps usage */
1176 	.ts_used_bits = 32,
1177 	.ts_period = 1000000, /* calibration period in ts. */
1178 	.us_per_ts_scale = 1, /* us = (ts * scale) >> shift */
1179 	.us_per_ts_shift = 0,
1180 
1181 	/* give here messages in/out endpoints */
1182 	.ep_msg_in = PCAN_USBPRO_EP_MSGIN,
1183 	.ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0, PCAN_USBPRO_EP_MSGOUT_1},
1184 
1185 	/* size of rx/tx usb buffers */
1186 	.rx_buffer_size = PCAN_UFD_RX_BUFFER_SIZE,
1187 	.tx_buffer_size = PCAN_UFD_TX_BUFFER_SIZE,
1188 
1189 	/* device callbacks */
1190 	.intf_probe = pcan_usb_pro_probe,	/* same as PCAN-USB Pro */
1191 	.dev_init = pcan_usb_fd_init,
1192 
1193 	.dev_exit = pcan_usb_fd_exit,
1194 	.dev_free = pcan_usb_fd_free,
1195 	.dev_set_bus = pcan_usb_fd_set_bus,
1196 	.dev_set_bittiming = pcan_usb_fd_set_bittiming_slow,
1197 	.dev_set_data_bittiming = pcan_usb_fd_set_bittiming_fast,
1198 	.dev_decode_buf = pcan_usb_fd_decode_buf,
1199 	.dev_start = pcan_usb_fd_start,
1200 	.dev_stop = pcan_usb_fd_stop,
1201 	.dev_restart_async = pcan_usb_fd_restart_async,
1202 	.dev_encode_msg = pcan_usb_fd_encode_msg,
1203 
1204 	.do_get_berr_counter = pcan_usb_fd_get_berr_counter,
1205 };
1206 
1207 /* describes the PCAN-USB X6 adapter */
1208 static const struct can_bittiming_const pcan_usb_x6_const = {
1209 	.name = "pcan_usb_x6",
1210 	.tseg1_min = 1,
1211 	.tseg1_max = (1 << PUCAN_TSLOW_TSGEG1_BITS),
1212 	.tseg2_min = 1,
1213 	.tseg2_max = (1 << PUCAN_TSLOW_TSGEG2_BITS),
1214 	.sjw_max = (1 << PUCAN_TSLOW_SJW_BITS),
1215 	.brp_min = 1,
1216 	.brp_max = (1 << PUCAN_TSLOW_BRP_BITS),
1217 	.brp_inc = 1,
1218 };
1219 
1220 static const struct can_bittiming_const pcan_usb_x6_data_const = {
1221 	.name = "pcan_usb_x6",
1222 	.tseg1_min = 1,
1223 	.tseg1_max = (1 << PUCAN_TFAST_TSGEG1_BITS),
1224 	.tseg2_min = 1,
1225 	.tseg2_max = (1 << PUCAN_TFAST_TSGEG2_BITS),
1226 	.sjw_max = (1 << PUCAN_TFAST_SJW_BITS),
1227 	.brp_min = 1,
1228 	.brp_max = (1 << PUCAN_TFAST_BRP_BITS),
1229 	.brp_inc = 1,
1230 };
1231 
1232 const struct peak_usb_adapter pcan_usb_x6 = {
1233 	.name = "PCAN-USB X6",
1234 	.device_id = PCAN_USBX6_PRODUCT_ID,
1235 	.ctrl_count = PCAN_USBPROFD_CHANNEL_COUNT,
1236 	.ctrlmode_supported = CAN_CTRLMODE_FD |
1237 			CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY,
1238 	.clock = {
1239 		.freq = PCAN_UFD_CRYSTAL_HZ,
1240 	},
1241 	.bittiming_const = &pcan_usb_x6_const,
1242 	.data_bittiming_const = &pcan_usb_x6_data_const,
1243 
1244 	/* size of device private data */
1245 	.sizeof_dev_private = sizeof(struct pcan_usb_fd_device),
1246 
1247 	/* timestamps usage */
1248 	.ts_used_bits = 32,
1249 	.ts_period = 1000000, /* calibration period in ts. */
1250 	.us_per_ts_scale = 1, /* us = (ts * scale) >> shift */
1251 	.us_per_ts_shift = 0,
1252 
1253 	/* give here messages in/out endpoints */
1254 	.ep_msg_in = PCAN_USBPRO_EP_MSGIN,
1255 	.ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0, PCAN_USBPRO_EP_MSGOUT_1},
1256 
1257 	/* size of rx/tx usb buffers */
1258 	.rx_buffer_size = PCAN_UFD_RX_BUFFER_SIZE,
1259 	.tx_buffer_size = PCAN_UFD_TX_BUFFER_SIZE,
1260 
1261 	/* device callbacks */
1262 	.intf_probe = pcan_usb_pro_probe,	/* same as PCAN-USB Pro */
1263 	.dev_init = pcan_usb_fd_init,
1264 
1265 	.dev_exit = pcan_usb_fd_exit,
1266 	.dev_free = pcan_usb_fd_free,
1267 	.dev_set_bus = pcan_usb_fd_set_bus,
1268 	.dev_set_bittiming = pcan_usb_fd_set_bittiming_slow,
1269 	.dev_set_data_bittiming = pcan_usb_fd_set_bittiming_fast,
1270 	.dev_decode_buf = pcan_usb_fd_decode_buf,
1271 	.dev_start = pcan_usb_fd_start,
1272 	.dev_stop = pcan_usb_fd_stop,
1273 	.dev_restart_async = pcan_usb_fd_restart_async,
1274 	.dev_encode_msg = pcan_usb_fd_encode_msg,
1275 
1276 	.do_get_berr_counter = pcan_usb_fd_get_berr_counter,
1277 };
1278