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 	/* Single-Shot frame */
778 	if (dev->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT)
779 		tx_msg_flags |= PUCAN_MSG_SINGLE_SHOT;
780 
781 	tx_msg->flags = cpu_to_le16(tx_msg_flags);
782 	tx_msg->channel_dlc = PUCAN_MSG_CHANNEL_DLC(dev->ctrl_idx, dlc);
783 	memcpy(tx_msg->d, cfd->data, cfd->len);
784 
785 	/* add null size message to tag the end (messages are 32-bits aligned)
786 	 */
787 	tx_msg = (struct pucan_tx_msg *)(obuf + tx_msg_size);
788 
789 	tx_msg->size = 0;
790 
791 	/* set the whole size of the USB packet to send */
792 	*size = tx_msg_size + sizeof(u32);
793 
794 	return 0;
795 }
796 
797 /* start the interface (last chance before set bus on) */
798 static int pcan_usb_fd_start(struct peak_usb_device *dev)
799 {
800 	struct pcan_usb_fd_device *pdev =
801 			container_of(dev, struct pcan_usb_fd_device, dev);
802 	int err;
803 
804 	/* set filter mode: all acceptance */
805 	err = pcan_usb_fd_set_filter_std(dev, -1, 0xffffffff);
806 	if (err)
807 		return err;
808 
809 	/* opening first device: */
810 	if (pdev->usb_if->dev_opened_count == 0) {
811 		/* reset time_ref */
812 		peak_usb_init_time_ref(&pdev->usb_if->time_ref,
813 				       &pcan_usb_pro_fd);
814 
815 		/* enable USB calibration messages */
816 		err = pcan_usb_fd_set_options(dev, 1,
817 					      PUCAN_OPTION_ERROR,
818 					      PCAN_UFD_FLTEXT_CALIBRATION);
819 	}
820 
821 	pdev->usb_if->dev_opened_count++;
822 
823 	/* reset cached error counters */
824 	pdev->bec.txerr = 0;
825 	pdev->bec.rxerr = 0;
826 
827 	return err;
828 }
829 
830 /* socket callback used to copy berr counters values received through USB */
831 static int pcan_usb_fd_get_berr_counter(const struct net_device *netdev,
832 					struct can_berr_counter *bec)
833 {
834 	struct peak_usb_device *dev = netdev_priv(netdev);
835 	struct pcan_usb_fd_device *pdev =
836 			container_of(dev, struct pcan_usb_fd_device, dev);
837 
838 	*bec = pdev->bec;
839 
840 	/* must return 0 */
841 	return 0;
842 }
843 
844 /* stop interface (last chance before set bus off) */
845 static int pcan_usb_fd_stop(struct peak_usb_device *dev)
846 {
847 	struct pcan_usb_fd_device *pdev =
848 			container_of(dev, struct pcan_usb_fd_device, dev);
849 
850 	/* turn off special msgs for that interface if no other dev opened */
851 	if (pdev->usb_if->dev_opened_count == 1)
852 		pcan_usb_fd_set_options(dev, 0,
853 					PUCAN_OPTION_ERROR,
854 					PCAN_UFD_FLTEXT_CALIBRATION);
855 	pdev->usb_if->dev_opened_count--;
856 
857 	return 0;
858 }
859 
860 /* called when probing, to initialize a device object */
861 static int pcan_usb_fd_init(struct peak_usb_device *dev)
862 {
863 	struct pcan_usb_fd_device *pdev =
864 			container_of(dev, struct pcan_usb_fd_device, dev);
865 	int i, err = -ENOMEM;
866 
867 	/* do this for 1st channel only */
868 	if (!dev->prev_siblings) {
869 		/* allocate netdevices common structure attached to first one */
870 		pdev->usb_if = kzalloc(sizeof(*pdev->usb_if), GFP_KERNEL);
871 		if (!pdev->usb_if)
872 			goto err_out;
873 
874 		/* allocate command buffer once for all for the interface */
875 		pdev->cmd_buffer_addr = kzalloc(PCAN_UFD_CMD_BUFFER_SIZE,
876 						GFP_KERNEL);
877 		if (!pdev->cmd_buffer_addr)
878 			goto err_out_1;
879 
880 		/* number of ts msgs to ignore before taking one into account */
881 		pdev->usb_if->cm_ignore_count = 5;
882 
883 		err = pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_INFO,
884 					    PCAN_USBPRO_INFO_FW,
885 					    &pdev->usb_if->fw_info,
886 					    sizeof(pdev->usb_if->fw_info));
887 		if (err) {
888 			dev_err(dev->netdev->dev.parent,
889 				"unable to read %s firmware info (err %d)\n",
890 				dev->adapter->name, err);
891 			goto err_out_2;
892 		}
893 
894 		/* explicit use of dev_xxx() instead of netdev_xxx() here:
895 		 * information displayed are related to the device itself, not
896 		 * to the canx (channel) device.
897 		 */
898 		dev_info(dev->netdev->dev.parent,
899 			 "PEAK-System %s v%u fw v%u.%u.%u (%u channels)\n",
900 			 dev->adapter->name, pdev->usb_if->fw_info.hw_version,
901 			 pdev->usb_if->fw_info.fw_version[0],
902 			 pdev->usb_if->fw_info.fw_version[1],
903 			 pdev->usb_if->fw_info.fw_version[2],
904 			 dev->adapter->ctrl_count);
905 
906 		/* check for ability to switch between ISO/non-ISO modes */
907 		if (pdev->usb_if->fw_info.fw_version[0] >= 2) {
908 			/* firmware >= 2.x supports ISO/non-ISO switching */
909 			dev->can.ctrlmode_supported |= CAN_CTRLMODE_FD_NON_ISO;
910 		} else {
911 			/* firmware < 2.x only supports fixed(!) non-ISO */
912 			dev->can.ctrlmode |= CAN_CTRLMODE_FD_NON_ISO;
913 		}
914 
915 		/* tell the hardware the can driver is running */
916 		err = pcan_usb_fd_drv_loaded(dev, 1);
917 		if (err) {
918 			dev_err(dev->netdev->dev.parent,
919 				"unable to tell %s driver is loaded (err %d)\n",
920 				dev->adapter->name, err);
921 			goto err_out_2;
922 		}
923 	} else {
924 		/* otherwise, simply copy previous sibling's values */
925 		struct pcan_usb_fd_device *ppdev =
926 			container_of(dev->prev_siblings,
927 				     struct pcan_usb_fd_device, dev);
928 
929 		pdev->usb_if = ppdev->usb_if;
930 		pdev->cmd_buffer_addr = ppdev->cmd_buffer_addr;
931 
932 		/* do a copy of the ctrlmode[_supported] too */
933 		dev->can.ctrlmode = ppdev->dev.can.ctrlmode;
934 		dev->can.ctrlmode_supported = ppdev->dev.can.ctrlmode_supported;
935 	}
936 
937 	pdev->usb_if->dev[dev->ctrl_idx] = dev;
938 	dev->device_number =
939 		le32_to_cpu(pdev->usb_if->fw_info.dev_id[dev->ctrl_idx]);
940 
941 	/* set clock domain */
942 	for (i = 0; i < ARRAY_SIZE(pcan_usb_fd_clk_freq); i++)
943 		if (dev->adapter->clock.freq == pcan_usb_fd_clk_freq[i])
944 			break;
945 
946 	if (i >= ARRAY_SIZE(pcan_usb_fd_clk_freq)) {
947 		dev_warn(dev->netdev->dev.parent,
948 			 "incompatible clock frequencies\n");
949 		err = -EINVAL;
950 		goto err_out_2;
951 	}
952 
953 	pcan_usb_fd_set_clock_domain(dev, i);
954 
955 	/* set LED in default state (end of init phase) */
956 	pcan_usb_fd_set_can_led(dev, PCAN_UFD_LED_DEF);
957 
958 	return 0;
959 
960 err_out_2:
961 	kfree(pdev->cmd_buffer_addr);
962 err_out_1:
963 	kfree(pdev->usb_if);
964 err_out:
965 	return err;
966 }
967 
968 /* called when driver module is being unloaded */
969 static void pcan_usb_fd_exit(struct peak_usb_device *dev)
970 {
971 	struct pcan_usb_fd_device *pdev =
972 			container_of(dev, struct pcan_usb_fd_device, dev);
973 
974 	/* when rmmod called before unplug and if down, should reset things
975 	 * before leaving
976 	 */
977 	if (dev->can.state != CAN_STATE_STOPPED) {
978 		/* set bus off on the corresponding channel */
979 		pcan_usb_fd_set_bus(dev, 0);
980 	}
981 
982 	/* switch off corresponding CAN LEDs */
983 	pcan_usb_fd_set_can_led(dev, PCAN_UFD_LED_OFF);
984 
985 	/* if channel #0 (only) */
986 	if (dev->ctrl_idx == 0) {
987 		/* turn off calibration message if any device were opened */
988 		if (pdev->usb_if->dev_opened_count > 0)
989 			pcan_usb_fd_set_options(dev, 0,
990 						PUCAN_OPTION_ERROR,
991 						PCAN_UFD_FLTEXT_CALIBRATION);
992 
993 		/* tell USB adapter that the driver is being unloaded */
994 		pcan_usb_fd_drv_loaded(dev, 0);
995 	}
996 }
997 
998 /* called when the USB adapter is unplugged */
999 static void pcan_usb_fd_free(struct peak_usb_device *dev)
1000 {
1001 	/* last device: can free shared objects now */
1002 	if (!dev->prev_siblings && !dev->next_siblings) {
1003 		struct pcan_usb_fd_device *pdev =
1004 			container_of(dev, struct pcan_usb_fd_device, dev);
1005 
1006 		/* free commands buffer */
1007 		kfree(pdev->cmd_buffer_addr);
1008 
1009 		/* free usb interface object */
1010 		kfree(pdev->usb_if);
1011 	}
1012 }
1013 
1014 /* blink LED's */
1015 static int pcan_usb_fd_set_phys_id(struct net_device *netdev,
1016 				   enum ethtool_phys_id_state state)
1017 {
1018 	struct peak_usb_device *dev = netdev_priv(netdev);
1019 	int err = 0;
1020 
1021 	switch (state) {
1022 	case ETHTOOL_ID_ACTIVE:
1023 		err = pcan_usb_fd_set_can_led(dev, PCAN_UFD_LED_FAST);
1024 		break;
1025 	case ETHTOOL_ID_INACTIVE:
1026 		err = pcan_usb_fd_set_can_led(dev, PCAN_UFD_LED_DEF);
1027 		break;
1028 	default:
1029 		break;
1030 	}
1031 
1032 	return err;
1033 }
1034 
1035 static const struct ethtool_ops pcan_usb_fd_ethtool_ops = {
1036 	.set_phys_id = pcan_usb_fd_set_phys_id,
1037 };
1038 
1039 /* describes the PCAN-USB FD adapter */
1040 static const struct can_bittiming_const pcan_usb_fd_const = {
1041 	.name = "pcan_usb_fd",
1042 	.tseg1_min = 1,
1043 	.tseg1_max = (1 << PUCAN_TSLOW_TSGEG1_BITS),
1044 	.tseg2_min = 1,
1045 	.tseg2_max = (1 << PUCAN_TSLOW_TSGEG2_BITS),
1046 	.sjw_max = (1 << PUCAN_TSLOW_SJW_BITS),
1047 	.brp_min = 1,
1048 	.brp_max = (1 << PUCAN_TSLOW_BRP_BITS),
1049 	.brp_inc = 1,
1050 };
1051 
1052 static const struct can_bittiming_const pcan_usb_fd_data_const = {
1053 	.name = "pcan_usb_fd",
1054 	.tseg1_min = 1,
1055 	.tseg1_max = (1 << PUCAN_TFAST_TSGEG1_BITS),
1056 	.tseg2_min = 1,
1057 	.tseg2_max = (1 << PUCAN_TFAST_TSGEG2_BITS),
1058 	.sjw_max = (1 << PUCAN_TFAST_SJW_BITS),
1059 	.brp_min = 1,
1060 	.brp_max = (1 << PUCAN_TFAST_BRP_BITS),
1061 	.brp_inc = 1,
1062 };
1063 
1064 const struct peak_usb_adapter pcan_usb_fd = {
1065 	.name = "PCAN-USB FD",
1066 	.device_id = PCAN_USBFD_PRODUCT_ID,
1067 	.ctrl_count = PCAN_USBFD_CHANNEL_COUNT,
1068 	.ctrlmode_supported = CAN_CTRLMODE_FD |
1069 			CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY |
1070 			CAN_CTRLMODE_ONE_SHOT | CAN_CTRLMODE_CC_LEN8_DLC,
1071 	.clock = {
1072 		.freq = PCAN_UFD_CRYSTAL_HZ,
1073 	},
1074 	.bittiming_const = &pcan_usb_fd_const,
1075 	.data_bittiming_const = &pcan_usb_fd_data_const,
1076 
1077 	/* size of device private data */
1078 	.sizeof_dev_private = sizeof(struct pcan_usb_fd_device),
1079 
1080 	.ethtool_ops = &pcan_usb_fd_ethtool_ops,
1081 
1082 	/* timestamps usage */
1083 	.ts_used_bits = 32,
1084 	.us_per_ts_scale = 1, /* us = (ts * scale) >> shift */
1085 	.us_per_ts_shift = 0,
1086 
1087 	/* give here messages in/out endpoints */
1088 	.ep_msg_in = PCAN_USBPRO_EP_MSGIN,
1089 	.ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0},
1090 
1091 	/* size of rx/tx usb buffers */
1092 	.rx_buffer_size = PCAN_UFD_RX_BUFFER_SIZE,
1093 	.tx_buffer_size = PCAN_UFD_TX_BUFFER_SIZE,
1094 
1095 	/* device callbacks */
1096 	.intf_probe = pcan_usb_pro_probe,	/* same as PCAN-USB Pro */
1097 	.dev_init = pcan_usb_fd_init,
1098 
1099 	.dev_exit = pcan_usb_fd_exit,
1100 	.dev_free = pcan_usb_fd_free,
1101 	.dev_set_bus = pcan_usb_fd_set_bus,
1102 	.dev_set_bittiming = pcan_usb_fd_set_bittiming_slow,
1103 	.dev_set_data_bittiming = pcan_usb_fd_set_bittiming_fast,
1104 	.dev_decode_buf = pcan_usb_fd_decode_buf,
1105 	.dev_start = pcan_usb_fd_start,
1106 	.dev_stop = pcan_usb_fd_stop,
1107 	.dev_restart_async = pcan_usb_fd_restart_async,
1108 	.dev_encode_msg = pcan_usb_fd_encode_msg,
1109 
1110 	.do_get_berr_counter = pcan_usb_fd_get_berr_counter,
1111 };
1112 
1113 /* describes the PCAN-CHIP USB */
1114 static const struct can_bittiming_const pcan_usb_chip_const = {
1115 	.name = "pcan_chip_usb",
1116 	.tseg1_min = 1,
1117 	.tseg1_max = (1 << PUCAN_TSLOW_TSGEG1_BITS),
1118 	.tseg2_min = 1,
1119 	.tseg2_max = (1 << PUCAN_TSLOW_TSGEG2_BITS),
1120 	.sjw_max = (1 << PUCAN_TSLOW_SJW_BITS),
1121 	.brp_min = 1,
1122 	.brp_max = (1 << PUCAN_TSLOW_BRP_BITS),
1123 	.brp_inc = 1,
1124 };
1125 
1126 static const struct can_bittiming_const pcan_usb_chip_data_const = {
1127 	.name = "pcan_chip_usb",
1128 	.tseg1_min = 1,
1129 	.tseg1_max = (1 << PUCAN_TFAST_TSGEG1_BITS),
1130 	.tseg2_min = 1,
1131 	.tseg2_max = (1 << PUCAN_TFAST_TSGEG2_BITS),
1132 	.sjw_max = (1 << PUCAN_TFAST_SJW_BITS),
1133 	.brp_min = 1,
1134 	.brp_max = (1 << PUCAN_TFAST_BRP_BITS),
1135 	.brp_inc = 1,
1136 };
1137 
1138 const struct peak_usb_adapter pcan_usb_chip = {
1139 	.name = "PCAN-Chip USB",
1140 	.device_id = PCAN_USBCHIP_PRODUCT_ID,
1141 	.ctrl_count = PCAN_USBFD_CHANNEL_COUNT,
1142 	.ctrlmode_supported = CAN_CTRLMODE_FD |
1143 		CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY |
1144 		CAN_CTRLMODE_ONE_SHOT | CAN_CTRLMODE_CC_LEN8_DLC,
1145 	.clock = {
1146 		.freq = PCAN_UFD_CRYSTAL_HZ,
1147 	},
1148 	.bittiming_const = &pcan_usb_chip_const,
1149 	.data_bittiming_const = &pcan_usb_chip_data_const,
1150 
1151 	/* size of device private data */
1152 	.sizeof_dev_private = sizeof(struct pcan_usb_fd_device),
1153 
1154 	.ethtool_ops = &pcan_usb_fd_ethtool_ops,
1155 
1156 	/* timestamps usage */
1157 	.ts_used_bits = 32,
1158 	.us_per_ts_scale = 1, /* us = (ts * scale) >> shift */
1159 	.us_per_ts_shift = 0,
1160 
1161 	/* give here messages in/out endpoints */
1162 	.ep_msg_in = PCAN_USBPRO_EP_MSGIN,
1163 	.ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0},
1164 
1165 	/* size of rx/tx usb buffers */
1166 	.rx_buffer_size = PCAN_UFD_RX_BUFFER_SIZE,
1167 	.tx_buffer_size = PCAN_UFD_TX_BUFFER_SIZE,
1168 
1169 	/* device callbacks */
1170 	.intf_probe = pcan_usb_pro_probe,	/* same as PCAN-USB Pro */
1171 	.dev_init = pcan_usb_fd_init,
1172 
1173 	.dev_exit = pcan_usb_fd_exit,
1174 	.dev_free = pcan_usb_fd_free,
1175 	.dev_set_bus = pcan_usb_fd_set_bus,
1176 	.dev_set_bittiming = pcan_usb_fd_set_bittiming_slow,
1177 	.dev_set_data_bittiming = pcan_usb_fd_set_bittiming_fast,
1178 	.dev_decode_buf = pcan_usb_fd_decode_buf,
1179 	.dev_start = pcan_usb_fd_start,
1180 	.dev_stop = pcan_usb_fd_stop,
1181 	.dev_restart_async = pcan_usb_fd_restart_async,
1182 	.dev_encode_msg = pcan_usb_fd_encode_msg,
1183 
1184 	.do_get_berr_counter = pcan_usb_fd_get_berr_counter,
1185 };
1186 
1187 /* describes the PCAN-USB Pro FD adapter */
1188 static const struct can_bittiming_const pcan_usb_pro_fd_const = {
1189 	.name = "pcan_usb_pro_fd",
1190 	.tseg1_min = 1,
1191 	.tseg1_max = (1 << PUCAN_TSLOW_TSGEG1_BITS),
1192 	.tseg2_min = 1,
1193 	.tseg2_max = (1 << PUCAN_TSLOW_TSGEG2_BITS),
1194 	.sjw_max = (1 << PUCAN_TSLOW_SJW_BITS),
1195 	.brp_min = 1,
1196 	.brp_max = (1 << PUCAN_TSLOW_BRP_BITS),
1197 	.brp_inc = 1,
1198 };
1199 
1200 static const struct can_bittiming_const pcan_usb_pro_fd_data_const = {
1201 	.name = "pcan_usb_pro_fd",
1202 	.tseg1_min = 1,
1203 	.tseg1_max = (1 << PUCAN_TFAST_TSGEG1_BITS),
1204 	.tseg2_min = 1,
1205 	.tseg2_max = (1 << PUCAN_TFAST_TSGEG2_BITS),
1206 	.sjw_max = (1 << PUCAN_TFAST_SJW_BITS),
1207 	.brp_min = 1,
1208 	.brp_max = (1 << PUCAN_TFAST_BRP_BITS),
1209 	.brp_inc = 1,
1210 };
1211 
1212 const struct peak_usb_adapter pcan_usb_pro_fd = {
1213 	.name = "PCAN-USB Pro FD",
1214 	.device_id = PCAN_USBPROFD_PRODUCT_ID,
1215 	.ctrl_count = PCAN_USBPROFD_CHANNEL_COUNT,
1216 	.ctrlmode_supported = CAN_CTRLMODE_FD |
1217 			CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY |
1218 			CAN_CTRLMODE_ONE_SHOT | CAN_CTRLMODE_CC_LEN8_DLC,
1219 	.clock = {
1220 		.freq = PCAN_UFD_CRYSTAL_HZ,
1221 	},
1222 	.bittiming_const = &pcan_usb_pro_fd_const,
1223 	.data_bittiming_const = &pcan_usb_pro_fd_data_const,
1224 
1225 	/* size of device private data */
1226 	.sizeof_dev_private = sizeof(struct pcan_usb_fd_device),
1227 
1228 	.ethtool_ops = &pcan_usb_fd_ethtool_ops,
1229 
1230 	/* timestamps usage */
1231 	.ts_used_bits = 32,
1232 	.us_per_ts_scale = 1, /* us = (ts * scale) >> shift */
1233 	.us_per_ts_shift = 0,
1234 
1235 	/* give here messages in/out endpoints */
1236 	.ep_msg_in = PCAN_USBPRO_EP_MSGIN,
1237 	.ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0, PCAN_USBPRO_EP_MSGOUT_1},
1238 
1239 	/* size of rx/tx usb buffers */
1240 	.rx_buffer_size = PCAN_UFD_RX_BUFFER_SIZE,
1241 	.tx_buffer_size = PCAN_UFD_TX_BUFFER_SIZE,
1242 
1243 	/* device callbacks */
1244 	.intf_probe = pcan_usb_pro_probe,	/* same as PCAN-USB Pro */
1245 	.dev_init = pcan_usb_fd_init,
1246 
1247 	.dev_exit = pcan_usb_fd_exit,
1248 	.dev_free = pcan_usb_fd_free,
1249 	.dev_set_bus = pcan_usb_fd_set_bus,
1250 	.dev_set_bittiming = pcan_usb_fd_set_bittiming_slow,
1251 	.dev_set_data_bittiming = pcan_usb_fd_set_bittiming_fast,
1252 	.dev_decode_buf = pcan_usb_fd_decode_buf,
1253 	.dev_start = pcan_usb_fd_start,
1254 	.dev_stop = pcan_usb_fd_stop,
1255 	.dev_restart_async = pcan_usb_fd_restart_async,
1256 	.dev_encode_msg = pcan_usb_fd_encode_msg,
1257 
1258 	.do_get_berr_counter = pcan_usb_fd_get_berr_counter,
1259 };
1260 
1261 /* describes the PCAN-USB X6 adapter */
1262 static const struct can_bittiming_const pcan_usb_x6_const = {
1263 	.name = "pcan_usb_x6",
1264 	.tseg1_min = 1,
1265 	.tseg1_max = (1 << PUCAN_TSLOW_TSGEG1_BITS),
1266 	.tseg2_min = 1,
1267 	.tseg2_max = (1 << PUCAN_TSLOW_TSGEG2_BITS),
1268 	.sjw_max = (1 << PUCAN_TSLOW_SJW_BITS),
1269 	.brp_min = 1,
1270 	.brp_max = (1 << PUCAN_TSLOW_BRP_BITS),
1271 	.brp_inc = 1,
1272 };
1273 
1274 static const struct can_bittiming_const pcan_usb_x6_data_const = {
1275 	.name = "pcan_usb_x6",
1276 	.tseg1_min = 1,
1277 	.tseg1_max = (1 << PUCAN_TFAST_TSGEG1_BITS),
1278 	.tseg2_min = 1,
1279 	.tseg2_max = (1 << PUCAN_TFAST_TSGEG2_BITS),
1280 	.sjw_max = (1 << PUCAN_TFAST_SJW_BITS),
1281 	.brp_min = 1,
1282 	.brp_max = (1 << PUCAN_TFAST_BRP_BITS),
1283 	.brp_inc = 1,
1284 };
1285 
1286 const struct peak_usb_adapter pcan_usb_x6 = {
1287 	.name = "PCAN-USB X6",
1288 	.device_id = PCAN_USBX6_PRODUCT_ID,
1289 	.ctrl_count = PCAN_USBPROFD_CHANNEL_COUNT,
1290 	.ctrlmode_supported = CAN_CTRLMODE_FD |
1291 			CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY |
1292 			CAN_CTRLMODE_ONE_SHOT | CAN_CTRLMODE_CC_LEN8_DLC,
1293 	.clock = {
1294 		.freq = PCAN_UFD_CRYSTAL_HZ,
1295 	},
1296 	.bittiming_const = &pcan_usb_x6_const,
1297 	.data_bittiming_const = &pcan_usb_x6_data_const,
1298 
1299 	/* size of device private data */
1300 	.sizeof_dev_private = sizeof(struct pcan_usb_fd_device),
1301 
1302 	.ethtool_ops = &pcan_usb_fd_ethtool_ops,
1303 
1304 	/* timestamps usage */
1305 	.ts_used_bits = 32,
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