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