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