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