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