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