xref: /linux/drivers/net/can/usb/peak_usb/pcan_usb_fd.c (revision a7e8511f)
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(skb, &usb_if->time_ref, le32_to_cpu(rm->ts_low));
519 
520 	return 0;
521 }
522 
523 /* handle uCAN status message */
524 static int pcan_usb_fd_decode_status(struct pcan_usb_fd_if *usb_if,
525 				     struct pucan_msg *rx_msg)
526 {
527 	struct pucan_status_msg *sm = (struct pucan_status_msg *)rx_msg;
528 	struct pcan_usb_fd_device *pdev;
529 	enum can_state new_state = CAN_STATE_ERROR_ACTIVE;
530 	enum can_state rx_state, tx_state;
531 	struct peak_usb_device *dev;
532 	struct net_device *netdev;
533 	struct can_frame *cf;
534 	struct sk_buff *skb;
535 
536 	if (pucan_stmsg_get_channel(sm) >= ARRAY_SIZE(usb_if->dev))
537 		return -ENOMEM;
538 
539 	dev = usb_if->dev[pucan_stmsg_get_channel(sm)];
540 	pdev = container_of(dev, struct pcan_usb_fd_device, dev);
541 	netdev = dev->netdev;
542 
543 	/* nothing should be sent while in BUS_OFF state */
544 	if (dev->can.state == CAN_STATE_BUS_OFF)
545 		return 0;
546 
547 	if (sm->channel_p_w_b & PUCAN_BUS_BUSOFF) {
548 		new_state = CAN_STATE_BUS_OFF;
549 	} else if (sm->channel_p_w_b & PUCAN_BUS_PASSIVE) {
550 		new_state = CAN_STATE_ERROR_PASSIVE;
551 	} else if (sm->channel_p_w_b & PUCAN_BUS_WARNING) {
552 		new_state = CAN_STATE_ERROR_WARNING;
553 	} else {
554 		/* no error bit (so, no error skb, back to active state) */
555 		dev->can.state = CAN_STATE_ERROR_ACTIVE;
556 		pdev->bec.txerr = 0;
557 		pdev->bec.rxerr = 0;
558 		return 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 	if (skb)
572 		can_change_state(netdev, cf, tx_state, rx_state);
573 
574 	/* things must be done even in case of OOM */
575 	if (new_state == CAN_STATE_BUS_OFF)
576 		can_bus_off(netdev);
577 
578 	if (!skb)
579 		return -ENOMEM;
580 
581 	netdev->stats.rx_packets++;
582 	netdev->stats.rx_bytes += cf->len;
583 
584 	peak_usb_netif_rx(skb, &usb_if->time_ref, le32_to_cpu(sm->ts_low));
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(skb, &usb_if->time_ref, le32_to_cpu(ov->ts_low));
635 
636 	netdev->stats.rx_over_errors++;
637 	netdev->stats.rx_errors++;
638 
639 	return 0;
640 }
641 
642 /* handle USB calibration message */
643 static void pcan_usb_fd_decode_ts(struct pcan_usb_fd_if *usb_if,
644 				  struct pucan_msg *rx_msg)
645 {
646 	struct pcan_ufd_ts_msg *ts = (struct pcan_ufd_ts_msg *)rx_msg;
647 
648 	/* should wait until clock is stabilized */
649 	if (usb_if->cm_ignore_count > 0)
650 		usb_if->cm_ignore_count--;
651 	else
652 		peak_usb_set_ts_now(&usb_if->time_ref, le32_to_cpu(ts->ts_low));
653 }
654 
655 /* callback for bulk IN urb */
656 static int pcan_usb_fd_decode_buf(struct peak_usb_device *dev, struct urb *urb)
657 {
658 	struct pcan_usb_fd_if *usb_if = pcan_usb_fd_dev_if(dev);
659 	struct net_device *netdev = dev->netdev;
660 	struct pucan_msg *rx_msg;
661 	u8 *msg_ptr, *msg_end;
662 	int err = 0;
663 
664 	/* loop reading all the records from the incoming message */
665 	msg_ptr = urb->transfer_buffer;
666 	msg_end = urb->transfer_buffer + urb->actual_length;
667 	for (; msg_ptr < msg_end;) {
668 		u16 rx_msg_type, rx_msg_size;
669 
670 		rx_msg = (struct pucan_msg *)msg_ptr;
671 		if (!rx_msg->size) {
672 			/* null packet found: end of list */
673 			break;
674 		}
675 
676 		rx_msg_size = le16_to_cpu(rx_msg->size);
677 		rx_msg_type = le16_to_cpu(rx_msg->type);
678 
679 		/* check if the record goes out of current packet */
680 		if (msg_ptr + rx_msg_size > msg_end) {
681 			netdev_err(netdev,
682 				   "got frag rec: should inc usb rx buf sze\n");
683 			err = -EBADMSG;
684 			break;
685 		}
686 
687 		switch (rx_msg_type) {
688 		case PUCAN_MSG_CAN_RX:
689 			err = pcan_usb_fd_decode_canmsg(usb_if, rx_msg);
690 			if (err < 0)
691 				goto fail;
692 			break;
693 
694 		case PCAN_UFD_MSG_CALIBRATION:
695 			pcan_usb_fd_decode_ts(usb_if, rx_msg);
696 			break;
697 
698 		case PUCAN_MSG_ERROR:
699 			err = pcan_usb_fd_decode_error(usb_if, rx_msg);
700 			if (err < 0)
701 				goto fail;
702 			break;
703 
704 		case PUCAN_MSG_STATUS:
705 			err = pcan_usb_fd_decode_status(usb_if, rx_msg);
706 			if (err < 0)
707 				goto fail;
708 			break;
709 
710 		case PCAN_UFD_MSG_OVERRUN:
711 			err = pcan_usb_fd_decode_overrun(usb_if, rx_msg);
712 			if (err < 0)
713 				goto fail;
714 			break;
715 
716 		default:
717 			netdev_err(netdev,
718 				   "unhandled msg type 0x%02x (%d): ignored\n",
719 				   rx_msg_type, rx_msg_type);
720 			break;
721 		}
722 
723 		msg_ptr += rx_msg_size;
724 	}
725 
726 fail:
727 	if (err)
728 		pcan_dump_mem("received msg",
729 			      urb->transfer_buffer, urb->actual_length);
730 	return err;
731 }
732 
733 /* CAN/CANFD frames encoding callback */
734 static int pcan_usb_fd_encode_msg(struct peak_usb_device *dev,
735 				  struct sk_buff *skb, u8 *obuf, size_t *size)
736 {
737 	struct pucan_tx_msg *tx_msg = (struct pucan_tx_msg *)obuf;
738 	struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
739 	u16 tx_msg_size, tx_msg_flags;
740 	u8 dlc;
741 
742 	if (cfd->len > CANFD_MAX_DLEN)
743 		return -EINVAL;
744 
745 	tx_msg_size = ALIGN(sizeof(struct pucan_tx_msg) + cfd->len, 4);
746 	tx_msg->size = cpu_to_le16(tx_msg_size);
747 	tx_msg->type = cpu_to_le16(PUCAN_MSG_CAN_TX);
748 
749 	tx_msg_flags = 0;
750 	if (cfd->can_id & CAN_EFF_FLAG) {
751 		tx_msg_flags |= PUCAN_MSG_EXT_ID;
752 		tx_msg->can_id = cpu_to_le32(cfd->can_id & CAN_EFF_MASK);
753 	} else {
754 		tx_msg->can_id = cpu_to_le32(cfd->can_id & CAN_SFF_MASK);
755 	}
756 
757 	if (can_is_canfd_skb(skb)) {
758 		/* considering a CANFD frame */
759 		dlc = can_fd_len2dlc(cfd->len);
760 
761 		tx_msg_flags |= PUCAN_MSG_EXT_DATA_LEN;
762 
763 		if (cfd->flags & CANFD_BRS)
764 			tx_msg_flags |= PUCAN_MSG_BITRATE_SWITCH;
765 
766 		if (cfd->flags & CANFD_ESI)
767 			tx_msg_flags |= PUCAN_MSG_ERROR_STATE_IND;
768 	} else {
769 		/* CAND 2.0 frames */
770 		dlc = can_get_cc_dlc((struct can_frame *)cfd,
771 				     dev->can.ctrlmode);
772 
773 		if (cfd->can_id & CAN_RTR_FLAG)
774 			tx_msg_flags |= PUCAN_MSG_RTR;
775 	}
776 
777 	tx_msg->flags = cpu_to_le16(tx_msg_flags);
778 	tx_msg->channel_dlc = PUCAN_MSG_CHANNEL_DLC(dev->ctrl_idx, dlc);
779 	memcpy(tx_msg->d, cfd->data, cfd->len);
780 
781 	/* add null size message to tag the end (messages are 32-bits aligned)
782 	 */
783 	tx_msg = (struct pucan_tx_msg *)(obuf + tx_msg_size);
784 
785 	tx_msg->size = 0;
786 
787 	/* set the whole size of the USB packet to send */
788 	*size = tx_msg_size + sizeof(u32);
789 
790 	return 0;
791 }
792 
793 /* start the interface (last chance before set bus on) */
794 static int pcan_usb_fd_start(struct peak_usb_device *dev)
795 {
796 	struct pcan_usb_fd_device *pdev =
797 			container_of(dev, struct pcan_usb_fd_device, dev);
798 	int err;
799 
800 	/* set filter mode: all acceptance */
801 	err = pcan_usb_fd_set_filter_std(dev, -1, 0xffffffff);
802 	if (err)
803 		return err;
804 
805 	/* opening first device: */
806 	if (pdev->usb_if->dev_opened_count == 0) {
807 		/* reset time_ref */
808 		peak_usb_init_time_ref(&pdev->usb_if->time_ref,
809 				       &pcan_usb_pro_fd);
810 
811 		/* enable USB calibration messages */
812 		err = pcan_usb_fd_set_options(dev, 1,
813 					      PUCAN_OPTION_ERROR,
814 					      PCAN_UFD_FLTEXT_CALIBRATION);
815 	}
816 
817 	pdev->usb_if->dev_opened_count++;
818 
819 	/* reset cached error counters */
820 	pdev->bec.txerr = 0;
821 	pdev->bec.rxerr = 0;
822 
823 	return err;
824 }
825 
826 /* socket callback used to copy berr counters values received through USB */
827 static int pcan_usb_fd_get_berr_counter(const struct net_device *netdev,
828 					struct can_berr_counter *bec)
829 {
830 	struct peak_usb_device *dev = netdev_priv(netdev);
831 	struct pcan_usb_fd_device *pdev =
832 			container_of(dev, struct pcan_usb_fd_device, dev);
833 
834 	*bec = pdev->bec;
835 
836 	/* must return 0 */
837 	return 0;
838 }
839 
840 /* stop interface (last chance before set bus off) */
841 static int pcan_usb_fd_stop(struct peak_usb_device *dev)
842 {
843 	struct pcan_usb_fd_device *pdev =
844 			container_of(dev, struct pcan_usb_fd_device, dev);
845 
846 	/* turn off special msgs for that interface if no other dev opened */
847 	if (pdev->usb_if->dev_opened_count == 1)
848 		pcan_usb_fd_set_options(dev, 0,
849 					PUCAN_OPTION_ERROR,
850 					PCAN_UFD_FLTEXT_CALIBRATION);
851 	pdev->usb_if->dev_opened_count--;
852 
853 	return 0;
854 }
855 
856 /* called when probing, to initialize a device object */
857 static int pcan_usb_fd_init(struct peak_usb_device *dev)
858 {
859 	struct pcan_usb_fd_device *pdev =
860 			container_of(dev, struct pcan_usb_fd_device, dev);
861 	int i, err = -ENOMEM;
862 
863 	/* do this for 1st channel only */
864 	if (!dev->prev_siblings) {
865 		/* allocate netdevices common structure attached to first one */
866 		pdev->usb_if = kzalloc(sizeof(*pdev->usb_if), GFP_KERNEL);
867 		if (!pdev->usb_if)
868 			goto err_out;
869 
870 		/* allocate command buffer once for all for the interface */
871 		pdev->cmd_buffer_addr = kzalloc(PCAN_UFD_CMD_BUFFER_SIZE,
872 						GFP_KERNEL);
873 		if (!pdev->cmd_buffer_addr)
874 			goto err_out_1;
875 
876 		/* number of ts msgs to ignore before taking one into account */
877 		pdev->usb_if->cm_ignore_count = 5;
878 
879 		err = pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_INFO,
880 					    PCAN_USBPRO_INFO_FW,
881 					    &pdev->usb_if->fw_info,
882 					    sizeof(pdev->usb_if->fw_info));
883 		if (err) {
884 			dev_err(dev->netdev->dev.parent,
885 				"unable to read %s firmware info (err %d)\n",
886 				dev->adapter->name, err);
887 			goto err_out_2;
888 		}
889 
890 		/* explicit use of dev_xxx() instead of netdev_xxx() here:
891 		 * information displayed are related to the device itself, not
892 		 * to the canx (channel) device.
893 		 */
894 		dev_info(dev->netdev->dev.parent,
895 			 "PEAK-System %s v%u fw v%u.%u.%u (%u channels)\n",
896 			 dev->adapter->name, pdev->usb_if->fw_info.hw_version,
897 			 pdev->usb_if->fw_info.fw_version[0],
898 			 pdev->usb_if->fw_info.fw_version[1],
899 			 pdev->usb_if->fw_info.fw_version[2],
900 			 dev->adapter->ctrl_count);
901 
902 		/* check for ability to switch between ISO/non-ISO modes */
903 		if (pdev->usb_if->fw_info.fw_version[0] >= 2) {
904 			/* firmware >= 2.x supports ISO/non-ISO switching */
905 			dev->can.ctrlmode_supported |= CAN_CTRLMODE_FD_NON_ISO;
906 		} else {
907 			/* firmware < 2.x only supports fixed(!) non-ISO */
908 			dev->can.ctrlmode |= CAN_CTRLMODE_FD_NON_ISO;
909 		}
910 
911 		/* tell the hardware the can driver is running */
912 		err = pcan_usb_fd_drv_loaded(dev, 1);
913 		if (err) {
914 			dev_err(dev->netdev->dev.parent,
915 				"unable to tell %s driver is loaded (err %d)\n",
916 				dev->adapter->name, err);
917 			goto err_out_2;
918 		}
919 	} else {
920 		/* otherwise, simply copy previous sibling's values */
921 		struct pcan_usb_fd_device *ppdev =
922 			container_of(dev->prev_siblings,
923 				     struct pcan_usb_fd_device, dev);
924 
925 		pdev->usb_if = ppdev->usb_if;
926 		pdev->cmd_buffer_addr = ppdev->cmd_buffer_addr;
927 
928 		/* do a copy of the ctrlmode[_supported] too */
929 		dev->can.ctrlmode = ppdev->dev.can.ctrlmode;
930 		dev->can.ctrlmode_supported = ppdev->dev.can.ctrlmode_supported;
931 	}
932 
933 	pdev->usb_if->dev[dev->ctrl_idx] = dev;
934 	dev->device_number =
935 		le32_to_cpu(pdev->usb_if->fw_info.dev_id[dev->ctrl_idx]);
936 
937 	/* set clock domain */
938 	for (i = 0; i < ARRAY_SIZE(pcan_usb_fd_clk_freq); i++)
939 		if (dev->adapter->clock.freq == pcan_usb_fd_clk_freq[i])
940 			break;
941 
942 	if (i >= ARRAY_SIZE(pcan_usb_fd_clk_freq)) {
943 		dev_warn(dev->netdev->dev.parent,
944 			 "incompatible clock frequencies\n");
945 		err = -EINVAL;
946 		goto err_out_2;
947 	}
948 
949 	pcan_usb_fd_set_clock_domain(dev, i);
950 
951 	/* set LED in default state (end of init phase) */
952 	pcan_usb_fd_set_can_led(dev, PCAN_UFD_LED_DEF);
953 
954 	return 0;
955 
956 err_out_2:
957 	kfree(pdev->cmd_buffer_addr);
958 err_out_1:
959 	kfree(pdev->usb_if);
960 err_out:
961 	return err;
962 }
963 
964 /* called when driver module is being unloaded */
965 static void pcan_usb_fd_exit(struct peak_usb_device *dev)
966 {
967 	struct pcan_usb_fd_device *pdev =
968 			container_of(dev, struct pcan_usb_fd_device, dev);
969 
970 	/* when rmmod called before unplug and if down, should reset things
971 	 * before leaving
972 	 */
973 	if (dev->can.state != CAN_STATE_STOPPED) {
974 		/* set bus off on the corresponding channel */
975 		pcan_usb_fd_set_bus(dev, 0);
976 	}
977 
978 	/* switch off corresponding CAN LEDs */
979 	pcan_usb_fd_set_can_led(dev, PCAN_UFD_LED_OFF);
980 
981 	/* if channel #0 (only) */
982 	if (dev->ctrl_idx == 0) {
983 		/* turn off calibration message if any device were opened */
984 		if (pdev->usb_if->dev_opened_count > 0)
985 			pcan_usb_fd_set_options(dev, 0,
986 						PUCAN_OPTION_ERROR,
987 						PCAN_UFD_FLTEXT_CALIBRATION);
988 
989 		/* tell USB adapter that the driver is being unloaded */
990 		pcan_usb_fd_drv_loaded(dev, 0);
991 	}
992 }
993 
994 /* called when the USB adapter is unplugged */
995 static void pcan_usb_fd_free(struct peak_usb_device *dev)
996 {
997 	/* last device: can free shared objects now */
998 	if (!dev->prev_siblings && !dev->next_siblings) {
999 		struct pcan_usb_fd_device *pdev =
1000 			container_of(dev, struct pcan_usb_fd_device, dev);
1001 
1002 		/* free commands buffer */
1003 		kfree(pdev->cmd_buffer_addr);
1004 
1005 		/* free usb interface object */
1006 		kfree(pdev->usb_if);
1007 	}
1008 }
1009 
1010 /* blink LED's */
1011 static int pcan_usb_fd_set_phys_id(struct net_device *netdev,
1012 				   enum ethtool_phys_id_state state)
1013 {
1014 	struct peak_usb_device *dev = netdev_priv(netdev);
1015 	int err = 0;
1016 
1017 	switch (state) {
1018 	case ETHTOOL_ID_ACTIVE:
1019 		err = pcan_usb_fd_set_can_led(dev, PCAN_UFD_LED_FAST);
1020 		break;
1021 	case ETHTOOL_ID_INACTIVE:
1022 		err = pcan_usb_fd_set_can_led(dev, PCAN_UFD_LED_DEF);
1023 		break;
1024 	default:
1025 		break;
1026 	}
1027 
1028 	return err;
1029 }
1030 
1031 static const struct ethtool_ops pcan_usb_fd_ethtool_ops = {
1032 	.set_phys_id = pcan_usb_fd_set_phys_id,
1033 };
1034 
1035 /* describes the PCAN-USB FD adapter */
1036 static const struct can_bittiming_const pcan_usb_fd_const = {
1037 	.name = "pcan_usb_fd",
1038 	.tseg1_min = 1,
1039 	.tseg1_max = (1 << PUCAN_TSLOW_TSGEG1_BITS),
1040 	.tseg2_min = 1,
1041 	.tseg2_max = (1 << PUCAN_TSLOW_TSGEG2_BITS),
1042 	.sjw_max = (1 << PUCAN_TSLOW_SJW_BITS),
1043 	.brp_min = 1,
1044 	.brp_max = (1 << PUCAN_TSLOW_BRP_BITS),
1045 	.brp_inc = 1,
1046 };
1047 
1048 static const struct can_bittiming_const pcan_usb_fd_data_const = {
1049 	.name = "pcan_usb_fd",
1050 	.tseg1_min = 1,
1051 	.tseg1_max = (1 << PUCAN_TFAST_TSGEG1_BITS),
1052 	.tseg2_min = 1,
1053 	.tseg2_max = (1 << PUCAN_TFAST_TSGEG2_BITS),
1054 	.sjw_max = (1 << PUCAN_TFAST_SJW_BITS),
1055 	.brp_min = 1,
1056 	.brp_max = (1 << PUCAN_TFAST_BRP_BITS),
1057 	.brp_inc = 1,
1058 };
1059 
1060 const struct peak_usb_adapter pcan_usb_fd = {
1061 	.name = "PCAN-USB FD",
1062 	.device_id = PCAN_USBFD_PRODUCT_ID,
1063 	.ctrl_count = PCAN_USBFD_CHANNEL_COUNT,
1064 	.ctrlmode_supported = CAN_CTRLMODE_FD |
1065 			CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY |
1066 			CAN_CTRLMODE_CC_LEN8_DLC,
1067 	.clock = {
1068 		.freq = PCAN_UFD_CRYSTAL_HZ,
1069 	},
1070 	.bittiming_const = &pcan_usb_fd_const,
1071 	.data_bittiming_const = &pcan_usb_fd_data_const,
1072 
1073 	/* size of device private data */
1074 	.sizeof_dev_private = sizeof(struct pcan_usb_fd_device),
1075 
1076 	.ethtool_ops = &pcan_usb_fd_ethtool_ops,
1077 
1078 	/* timestamps usage */
1079 	.ts_used_bits = 32,
1080 	.ts_period = 1000000, /* calibration period in ts. */
1081 	.us_per_ts_scale = 1, /* us = (ts * scale) >> shift */
1082 	.us_per_ts_shift = 0,
1083 
1084 	/* give here messages in/out endpoints */
1085 	.ep_msg_in = PCAN_USBPRO_EP_MSGIN,
1086 	.ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0},
1087 
1088 	/* size of rx/tx usb buffers */
1089 	.rx_buffer_size = PCAN_UFD_RX_BUFFER_SIZE,
1090 	.tx_buffer_size = PCAN_UFD_TX_BUFFER_SIZE,
1091 
1092 	/* device callbacks */
1093 	.intf_probe = pcan_usb_pro_probe,	/* same as PCAN-USB Pro */
1094 	.dev_init = pcan_usb_fd_init,
1095 
1096 	.dev_exit = pcan_usb_fd_exit,
1097 	.dev_free = pcan_usb_fd_free,
1098 	.dev_set_bus = pcan_usb_fd_set_bus,
1099 	.dev_set_bittiming = pcan_usb_fd_set_bittiming_slow,
1100 	.dev_set_data_bittiming = pcan_usb_fd_set_bittiming_fast,
1101 	.dev_decode_buf = pcan_usb_fd_decode_buf,
1102 	.dev_start = pcan_usb_fd_start,
1103 	.dev_stop = pcan_usb_fd_stop,
1104 	.dev_restart_async = pcan_usb_fd_restart_async,
1105 	.dev_encode_msg = pcan_usb_fd_encode_msg,
1106 
1107 	.do_get_berr_counter = pcan_usb_fd_get_berr_counter,
1108 };
1109 
1110 /* describes the PCAN-CHIP USB */
1111 static const struct can_bittiming_const pcan_usb_chip_const = {
1112 	.name = "pcan_chip_usb",
1113 	.tseg1_min = 1,
1114 	.tseg1_max = (1 << PUCAN_TSLOW_TSGEG1_BITS),
1115 	.tseg2_min = 1,
1116 	.tseg2_max = (1 << PUCAN_TSLOW_TSGEG2_BITS),
1117 	.sjw_max = (1 << PUCAN_TSLOW_SJW_BITS),
1118 	.brp_min = 1,
1119 	.brp_max = (1 << PUCAN_TSLOW_BRP_BITS),
1120 	.brp_inc = 1,
1121 };
1122 
1123 static const struct can_bittiming_const pcan_usb_chip_data_const = {
1124 	.name = "pcan_chip_usb",
1125 	.tseg1_min = 1,
1126 	.tseg1_max = (1 << PUCAN_TFAST_TSGEG1_BITS),
1127 	.tseg2_min = 1,
1128 	.tseg2_max = (1 << PUCAN_TFAST_TSGEG2_BITS),
1129 	.sjw_max = (1 << PUCAN_TFAST_SJW_BITS),
1130 	.brp_min = 1,
1131 	.brp_max = (1 << PUCAN_TFAST_BRP_BITS),
1132 	.brp_inc = 1,
1133 };
1134 
1135 const struct peak_usb_adapter pcan_usb_chip = {
1136 	.name = "PCAN-Chip USB",
1137 	.device_id = PCAN_USBCHIP_PRODUCT_ID,
1138 	.ctrl_count = PCAN_USBFD_CHANNEL_COUNT,
1139 	.ctrlmode_supported = CAN_CTRLMODE_FD |
1140 		CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY |
1141 		CAN_CTRLMODE_CC_LEN8_DLC,
1142 	.clock = {
1143 		.freq = PCAN_UFD_CRYSTAL_HZ,
1144 	},
1145 	.bittiming_const = &pcan_usb_chip_const,
1146 	.data_bittiming_const = &pcan_usb_chip_data_const,
1147 
1148 	/* size of device private data */
1149 	.sizeof_dev_private = sizeof(struct pcan_usb_fd_device),
1150 
1151 	.ethtool_ops = &pcan_usb_fd_ethtool_ops,
1152 
1153 	/* timestamps usage */
1154 	.ts_used_bits = 32,
1155 	.ts_period = 1000000, /* calibration period in ts. */
1156 	.us_per_ts_scale = 1, /* us = (ts * scale) >> shift */
1157 	.us_per_ts_shift = 0,
1158 
1159 	/* give here messages in/out endpoints */
1160 	.ep_msg_in = PCAN_USBPRO_EP_MSGIN,
1161 	.ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0},
1162 
1163 	/* size of rx/tx usb buffers */
1164 	.rx_buffer_size = PCAN_UFD_RX_BUFFER_SIZE,
1165 	.tx_buffer_size = PCAN_UFD_TX_BUFFER_SIZE,
1166 
1167 	/* device callbacks */
1168 	.intf_probe = pcan_usb_pro_probe,	/* same as PCAN-USB Pro */
1169 	.dev_init = pcan_usb_fd_init,
1170 
1171 	.dev_exit = pcan_usb_fd_exit,
1172 	.dev_free = pcan_usb_fd_free,
1173 	.dev_set_bus = pcan_usb_fd_set_bus,
1174 	.dev_set_bittiming = pcan_usb_fd_set_bittiming_slow,
1175 	.dev_set_data_bittiming = pcan_usb_fd_set_bittiming_fast,
1176 	.dev_decode_buf = pcan_usb_fd_decode_buf,
1177 	.dev_start = pcan_usb_fd_start,
1178 	.dev_stop = pcan_usb_fd_stop,
1179 	.dev_restart_async = pcan_usb_fd_restart_async,
1180 	.dev_encode_msg = pcan_usb_fd_encode_msg,
1181 
1182 	.do_get_berr_counter = pcan_usb_fd_get_berr_counter,
1183 };
1184 
1185 /* describes the PCAN-USB Pro FD adapter */
1186 static const struct can_bittiming_const pcan_usb_pro_fd_const = {
1187 	.name = "pcan_usb_pro_fd",
1188 	.tseg1_min = 1,
1189 	.tseg1_max = (1 << PUCAN_TSLOW_TSGEG1_BITS),
1190 	.tseg2_min = 1,
1191 	.tseg2_max = (1 << PUCAN_TSLOW_TSGEG2_BITS),
1192 	.sjw_max = (1 << PUCAN_TSLOW_SJW_BITS),
1193 	.brp_min = 1,
1194 	.brp_max = (1 << PUCAN_TSLOW_BRP_BITS),
1195 	.brp_inc = 1,
1196 };
1197 
1198 static const struct can_bittiming_const pcan_usb_pro_fd_data_const = {
1199 	.name = "pcan_usb_pro_fd",
1200 	.tseg1_min = 1,
1201 	.tseg1_max = (1 << PUCAN_TFAST_TSGEG1_BITS),
1202 	.tseg2_min = 1,
1203 	.tseg2_max = (1 << PUCAN_TFAST_TSGEG2_BITS),
1204 	.sjw_max = (1 << PUCAN_TFAST_SJW_BITS),
1205 	.brp_min = 1,
1206 	.brp_max = (1 << PUCAN_TFAST_BRP_BITS),
1207 	.brp_inc = 1,
1208 };
1209 
1210 const struct peak_usb_adapter pcan_usb_pro_fd = {
1211 	.name = "PCAN-USB Pro FD",
1212 	.device_id = PCAN_USBPROFD_PRODUCT_ID,
1213 	.ctrl_count = PCAN_USBPROFD_CHANNEL_COUNT,
1214 	.ctrlmode_supported = CAN_CTRLMODE_FD |
1215 			CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY |
1216 			CAN_CTRLMODE_CC_LEN8_DLC,
1217 	.clock = {
1218 		.freq = PCAN_UFD_CRYSTAL_HZ,
1219 	},
1220 	.bittiming_const = &pcan_usb_pro_fd_const,
1221 	.data_bittiming_const = &pcan_usb_pro_fd_data_const,
1222 
1223 	/* size of device private data */
1224 	.sizeof_dev_private = sizeof(struct pcan_usb_fd_device),
1225 
1226 	.ethtool_ops = &pcan_usb_fd_ethtool_ops,
1227 
1228 	/* timestamps usage */
1229 	.ts_used_bits = 32,
1230 	.ts_period = 1000000, /* calibration period in ts. */
1231 	.us_per_ts_scale = 1, /* us = (ts * scale) >> shift */
1232 	.us_per_ts_shift = 0,
1233 
1234 	/* give here messages in/out endpoints */
1235 	.ep_msg_in = PCAN_USBPRO_EP_MSGIN,
1236 	.ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0, PCAN_USBPRO_EP_MSGOUT_1},
1237 
1238 	/* size of rx/tx usb buffers */
1239 	.rx_buffer_size = PCAN_UFD_RX_BUFFER_SIZE,
1240 	.tx_buffer_size = PCAN_UFD_TX_BUFFER_SIZE,
1241 
1242 	/* device callbacks */
1243 	.intf_probe = pcan_usb_pro_probe,	/* same as PCAN-USB Pro */
1244 	.dev_init = pcan_usb_fd_init,
1245 
1246 	.dev_exit = pcan_usb_fd_exit,
1247 	.dev_free = pcan_usb_fd_free,
1248 	.dev_set_bus = pcan_usb_fd_set_bus,
1249 	.dev_set_bittiming = pcan_usb_fd_set_bittiming_slow,
1250 	.dev_set_data_bittiming = pcan_usb_fd_set_bittiming_fast,
1251 	.dev_decode_buf = pcan_usb_fd_decode_buf,
1252 	.dev_start = pcan_usb_fd_start,
1253 	.dev_stop = pcan_usb_fd_stop,
1254 	.dev_restart_async = pcan_usb_fd_restart_async,
1255 	.dev_encode_msg = pcan_usb_fd_encode_msg,
1256 
1257 	.do_get_berr_counter = pcan_usb_fd_get_berr_counter,
1258 };
1259 
1260 /* describes the PCAN-USB X6 adapter */
1261 static const struct can_bittiming_const pcan_usb_x6_const = {
1262 	.name = "pcan_usb_x6",
1263 	.tseg1_min = 1,
1264 	.tseg1_max = (1 << PUCAN_TSLOW_TSGEG1_BITS),
1265 	.tseg2_min = 1,
1266 	.tseg2_max = (1 << PUCAN_TSLOW_TSGEG2_BITS),
1267 	.sjw_max = (1 << PUCAN_TSLOW_SJW_BITS),
1268 	.brp_min = 1,
1269 	.brp_max = (1 << PUCAN_TSLOW_BRP_BITS),
1270 	.brp_inc = 1,
1271 };
1272 
1273 static const struct can_bittiming_const pcan_usb_x6_data_const = {
1274 	.name = "pcan_usb_x6",
1275 	.tseg1_min = 1,
1276 	.tseg1_max = (1 << PUCAN_TFAST_TSGEG1_BITS),
1277 	.tseg2_min = 1,
1278 	.tseg2_max = (1 << PUCAN_TFAST_TSGEG2_BITS),
1279 	.sjw_max = (1 << PUCAN_TFAST_SJW_BITS),
1280 	.brp_min = 1,
1281 	.brp_max = (1 << PUCAN_TFAST_BRP_BITS),
1282 	.brp_inc = 1,
1283 };
1284 
1285 const struct peak_usb_adapter pcan_usb_x6 = {
1286 	.name = "PCAN-USB X6",
1287 	.device_id = PCAN_USBX6_PRODUCT_ID,
1288 	.ctrl_count = PCAN_USBPROFD_CHANNEL_COUNT,
1289 	.ctrlmode_supported = CAN_CTRLMODE_FD |
1290 			CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY |
1291 			CAN_CTRLMODE_CC_LEN8_DLC,
1292 	.clock = {
1293 		.freq = PCAN_UFD_CRYSTAL_HZ,
1294 	},
1295 	.bittiming_const = &pcan_usb_x6_const,
1296 	.data_bittiming_const = &pcan_usb_x6_data_const,
1297 
1298 	/* size of device private data */
1299 	.sizeof_dev_private = sizeof(struct pcan_usb_fd_device),
1300 
1301 	.ethtool_ops = &pcan_usb_fd_ethtool_ops,
1302 
1303 	/* timestamps usage */
1304 	.ts_used_bits = 32,
1305 	.ts_period = 1000000, /* calibration period in ts. */
1306 	.us_per_ts_scale = 1, /* us = (ts * scale) >> shift */
1307 	.us_per_ts_shift = 0,
1308 
1309 	/* give here messages in/out endpoints */
1310 	.ep_msg_in = PCAN_USBPRO_EP_MSGIN,
1311 	.ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0, PCAN_USBPRO_EP_MSGOUT_1},
1312 
1313 	/* size of rx/tx usb buffers */
1314 	.rx_buffer_size = PCAN_UFD_RX_BUFFER_SIZE,
1315 	.tx_buffer_size = PCAN_UFD_TX_BUFFER_SIZE,
1316 
1317 	/* device callbacks */
1318 	.intf_probe = pcan_usb_pro_probe,	/* same as PCAN-USB Pro */
1319 	.dev_init = pcan_usb_fd_init,
1320 
1321 	.dev_exit = pcan_usb_fd_exit,
1322 	.dev_free = pcan_usb_fd_free,
1323 	.dev_set_bus = pcan_usb_fd_set_bus,
1324 	.dev_set_bittiming = pcan_usb_fd_set_bittiming_slow,
1325 	.dev_set_data_bittiming = pcan_usb_fd_set_bittiming_fast,
1326 	.dev_decode_buf = pcan_usb_fd_decode_buf,
1327 	.dev_start = pcan_usb_fd_start,
1328 	.dev_stop = pcan_usb_fd_stop,
1329 	.dev_restart_async = pcan_usb_fd_restart_async,
1330 	.dev_encode_msg = pcan_usb_fd_encode_msg,
1331 
1332 	.do_get_berr_counter = pcan_usb_fd_get_berr_counter,
1333 };
1334