WIP FPC-III support
[linux/fpc-iii.git] / drivers / net / can / usb / peak_usb / pcan_usb_fd.c
blob61631f4fd92a1d2dea864873431d60beca8b4318
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * CAN driver for PEAK System PCAN-USB FD / PCAN-USB Pro FD adapter
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>
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>
16 #include "pcan_usb_core.h"
17 #include "pcan_usb_pro.h"
19 MODULE_SUPPORTED_DEVICE("PEAK-System PCAN-USB FD adapter");
20 MODULE_SUPPORTED_DEVICE("PEAK-System PCAN-USB Pro FD adapter");
22 #define PCAN_USBPROFD_CHANNEL_COUNT 2
23 #define PCAN_USBFD_CHANNEL_COUNT 1
25 /* PCAN-USB Pro FD adapter internal clock (Hz) */
26 #define PCAN_UFD_CRYSTAL_HZ 80000000
28 #define PCAN_UFD_CMD_BUFFER_SIZE 512
29 #define PCAN_UFD_LOSPD_PKT_SIZE 64
31 /* PCAN-USB Pro FD command timeout (ms.) */
32 #define PCAN_UFD_CMD_TIMEOUT_MS 1000
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
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 */
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;
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;
68 /* Extended USB commands (non uCAN commands) */
70 /* Clock Modes command */
71 #define PCAN_UFD_CMD_CLK_SET 0x80
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
81 struct __packed pcan_ufd_clock {
82 __le16 opcode_channel;
84 u8 mode;
85 u8 unused[5];
88 /* LED control command */
89 #define PCAN_UFD_CMD_LED_SET 0x86
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
98 struct __packed pcan_ufd_led {
99 __le16 opcode_channel;
101 u8 mode;
102 u8 unused[5];
105 /* Extended usage of uCAN commands CMD_xxx_xx_OPTION for PCAN-USB Pro FD */
106 #define PCAN_UFD_FLTEXT_CALIBRATION 0x8000
108 struct __packed pcan_ufd_options {
109 __le16 opcode_channel;
111 __le16 ucan_mask;
112 u16 unused;
113 __le16 usb_mask;
116 /* Extended usage of uCAN messages for PCAN-USB Pro FD */
117 #define PCAN_UFD_MSG_CALIBRATION 0x100
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;
128 #define PCAN_UFD_MSG_OVERRUN 0x101
130 #define PCAN_UFD_OVMSG_CHANNEL(o) ((o)->channel & 0xf)
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];
141 static inline int pufd_omsg_get_channel(struct pcan_ufd_ovr_msg *om)
143 return om->channel & 0xf;
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
156 /* return a device USB interface */
157 static inline
158 struct pcan_usb_fd_if *pcan_usb_fd_dev_if(struct peak_usb_device *dev)
160 struct pcan_usb_fd_device *pdev =
161 container_of(dev, struct pcan_usb_fd_device, dev);
162 return pdev->usb_if;
165 /* return a device USB commands buffer */
166 static inline void *pcan_usb_fd_cmd_buffer(struct peak_usb_device *dev)
168 struct pcan_usb_fd_device *pdev =
169 container_of(dev, struct pcan_usb_fd_device, dev);
170 return pdev->cmd_buffer_addr;
173 /* send PCAN-USB Pro FD commands synchronously */
174 static int pcan_usb_fd_send_cmd(struct peak_usb_device *dev, void *cmd_tail)
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;
182 /* usb device unregistered? */
183 if (!(dev->state & PCAN_USB_STATE_CONNECTED))
184 return 0;
186 /* if a packet is not filled completely by commands, the command list
187 * is terminated with an "end of collection" record.
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);
195 packet_ptr = cmd_head;
196 packet_len = cmd_len;
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);
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;
214 packet_ptr += packet_len;
215 cmd_len -= packet_len;
217 if (cmd_len < PCAN_UFD_LOSPD_PKT_SIZE)
218 packet_len = cmd_len;
220 } while (packet_len > 0);
222 return err;
225 /* build the commands list in the given buffer, to enter operational mode */
226 static int pcan_usb_fd_build_restart_cmd(struct peak_usb_device *dev, u8 *buf)
228 struct pucan_wr_err_cnt *prc;
229 struct pucan_command *cmd;
230 u8 *pc = buf;
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);
237 /* select both counters */
238 prc->sel_mask = cpu_to_le16(PUCAN_WRERRCNT_TE|PUCAN_WRERRCNT_RE);
240 /* and reset their values */
241 prc->tx_counter = 0;
242 prc->rx_counter = 0;
244 /* moves the pointer forward */
245 pc += sizeof(struct pucan_wr_err_cnt);
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;
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);
258 puo->options = cpu_to_le16(PUCAN_OPTION_CANDFDISO);
260 /* to be sure that no other extended bits will be taken into
261 * account
263 puo->unused = 0;
265 /* moves the pointer forward */
266 pc += sizeof(struct pucan_options);
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);
277 return pc - buf;
280 /* set CAN bus on/off */
281 static int pcan_usb_fd_set_bus(struct peak_usb_device *dev, u8 onoff)
283 u8 *pc = pcan_usb_fd_cmd_buffer(dev);
284 int l;
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;
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);
298 /* send the command */
299 return pcan_usb_fd_send_cmd(dev, pc + l);
302 /* set filtering masks:
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)
307 * Each bit of this 64 x 32 bits array defines a CANID value:
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.
312 static int pcan_usb_fd_set_filter_std(struct peak_usb_device *dev, int idx,
313 u32 mask)
315 struct pucan_filter_std *cmd = pcan_usb_fd_cmd_buffer(dev);
316 int i, n;
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;
323 /* select the row (and only the row) otherwise */
324 } else {
325 n = idx + 1;
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);
335 /* send the command */
336 return pcan_usb_fd_send_cmd(dev, cmd);
339 /* set/unset options
341 * onoff set(1)/unset(0) options
342 * mask each bit defines a kind of options to set/unset
344 static int pcan_usb_fd_set_options(struct peak_usb_device *dev,
345 bool onoff, u16 ucan_mask, u16 usb_mask)
347 struct pcan_ufd_options *cmd = pcan_usb_fd_cmd_buffer(dev);
349 cmd->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx,
350 (onoff) ? PUCAN_CMD_SET_EN_OPTION :
351 PUCAN_CMD_CLR_DIS_OPTION);
353 cmd->ucan_mask = cpu_to_le16(ucan_mask);
354 cmd->usb_mask = cpu_to_le16(usb_mask);
356 /* send the command */
357 return pcan_usb_fd_send_cmd(dev, ++cmd);
360 /* setup LED control */
361 static int pcan_usb_fd_set_can_led(struct peak_usb_device *dev, u8 led_mode)
363 struct pcan_ufd_led *cmd = pcan_usb_fd_cmd_buffer(dev);
365 cmd->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx,
366 PCAN_UFD_CMD_LED_SET);
367 cmd->mode = led_mode;
369 /* send the command */
370 return pcan_usb_fd_send_cmd(dev, ++cmd);
373 /* set CAN clock domain */
374 static int pcan_usb_fd_set_clock_domain(struct peak_usb_device *dev,
375 u8 clk_mode)
377 struct pcan_ufd_clock *cmd = pcan_usb_fd_cmd_buffer(dev);
379 cmd->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx,
380 PCAN_UFD_CMD_CLK_SET);
381 cmd->mode = clk_mode;
383 /* send the command */
384 return pcan_usb_fd_send_cmd(dev, ++cmd);
387 /* set bittiming for CAN and CAN-FD header */
388 static int pcan_usb_fd_set_bittiming_slow(struct peak_usb_device *dev,
389 struct can_bittiming *bt)
391 struct pucan_timing_slow *cmd = pcan_usb_fd_cmd_buffer(dev);
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);
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));
402 cmd->ewl = 96; /* default */
404 /* send the command */
405 return pcan_usb_fd_send_cmd(dev, ++cmd);
408 /* set CAN-FD bittiming for data */
409 static int pcan_usb_fd_set_bittiming_fast(struct peak_usb_device *dev,
410 struct can_bittiming *bt)
412 struct pucan_timing_fast *cmd = pcan_usb_fd_cmd_buffer(dev);
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));
421 /* send the command */
422 return pcan_usb_fd_send_cmd(dev, ++cmd);
425 /* handle restart but in asynchronously way
426 * (uses PCAN-USB Pro code to complete asynchronous request)
428 static int pcan_usb_fd_restart_async(struct peak_usb_device *dev,
429 struct urb *urb, u8 *buf)
431 u8 *pc = buf;
433 /* build the entire cmds list in the provided buffer, to go back into
434 * operational mode.
436 pc += pcan_usb_fd_build_restart_cmd(dev, pc);
438 /* add EOC */
439 memset(pc, 0xff, sizeof(struct pucan_command));
440 pc += sizeof(struct pucan_command);
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);
448 /* and submit it. */
449 return usb_submit_urb(urb, GFP_ATOMIC);
452 static int pcan_usb_fd_drv_loaded(struct peak_usb_device *dev, bool loaded)
454 struct pcan_usb_fd_device *pdev =
455 container_of(dev, struct pcan_usb_fd_device, dev);
457 pdev->cmd_buffer_addr[0] = 0;
458 pdev->cmd_buffer_addr[1] = !!loaded;
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);
467 static int pcan_usb_fd_decode_canmsg(struct pcan_usb_fd_if *usb_if,
468 struct pucan_msg *rx_msg)
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);
477 if (pucan_msg_get_channel(rm) >= ARRAY_SIZE(usb_if->dev))
478 return -ENOMEM;
480 dev = usb_if->dev[pucan_msg_get_channel(rm)];
481 netdev = dev->netdev;
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;
489 if (rx_msg_flags & PUCAN_MSG_BITRATE_SWITCH)
490 cfd->flags |= CANFD_BRS;
492 if (rx_msg_flags & PUCAN_MSG_ERROR_STATE_IND)
493 cfd->flags |= CANFD_ESI;
495 cfd->len = can_fd_dlc2len(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;
502 can_frame_set_cc_len((struct can_frame *)cfd,
503 pucan_msg_get_dlc(rm),
504 dev->can.ctrlmode);
507 cfd->can_id = le32_to_cpu(rm->can_id);
509 if (rx_msg_flags & PUCAN_MSG_EXT_ID)
510 cfd->can_id |= CAN_EFF_FLAG;
512 if (rx_msg_flags & PUCAN_MSG_RTR)
513 cfd->can_id |= CAN_RTR_FLAG;
514 else
515 memcpy(cfd->data, rm->d, cfd->len);
517 peak_usb_netif_rx(skb, &usb_if->time_ref, le32_to_cpu(rm->ts_low));
519 netdev->stats.rx_packets++;
520 netdev->stats.rx_bytes += cfd->len;
522 return 0;
525 /* handle uCAN status message */
526 static int pcan_usb_fd_decode_status(struct pcan_usb_fd_if *usb_if,
527 struct pucan_msg *rx_msg)
529 struct pucan_status_msg *sm = (struct pucan_status_msg *)rx_msg;
530 struct pcan_usb_fd_device *pdev;
531 enum can_state new_state = CAN_STATE_ERROR_ACTIVE;
532 enum can_state rx_state, tx_state;
533 struct peak_usb_device *dev;
534 struct net_device *netdev;
535 struct can_frame *cf;
536 struct sk_buff *skb;
538 if (pucan_stmsg_get_channel(sm) >= ARRAY_SIZE(usb_if->dev))
539 return -ENOMEM;
541 dev = usb_if->dev[pucan_stmsg_get_channel(sm)];
542 pdev = container_of(dev, struct pcan_usb_fd_device, dev);
543 netdev = dev->netdev;
545 /* nothing should be sent while in BUS_OFF state */
546 if (dev->can.state == CAN_STATE_BUS_OFF)
547 return 0;
549 if (sm->channel_p_w_b & PUCAN_BUS_BUSOFF) {
550 new_state = CAN_STATE_BUS_OFF;
551 } else if (sm->channel_p_w_b & PUCAN_BUS_PASSIVE) {
552 new_state = CAN_STATE_ERROR_PASSIVE;
553 } else if (sm->channel_p_w_b & PUCAN_BUS_WARNING) {
554 new_state = CAN_STATE_ERROR_WARNING;
555 } else {
556 /* no error bit (so, no error skb, back to active state) */
557 dev->can.state = CAN_STATE_ERROR_ACTIVE;
558 pdev->bec.txerr = 0;
559 pdev->bec.rxerr = 0;
560 return 0;
563 /* state hasn't changed */
564 if (new_state == dev->can.state)
565 return 0;
567 /* handle bus state change */
568 tx_state = (pdev->bec.txerr >= pdev->bec.rxerr) ? new_state : 0;
569 rx_state = (pdev->bec.txerr <= pdev->bec.rxerr) ? new_state : 0;
571 /* allocate an skb to store the error frame */
572 skb = alloc_can_err_skb(netdev, &cf);
573 if (skb)
574 can_change_state(netdev, cf, tx_state, rx_state);
576 /* things must be done even in case of OOM */
577 if (new_state == CAN_STATE_BUS_OFF)
578 can_bus_off(netdev);
580 if (!skb)
581 return -ENOMEM;
583 peak_usb_netif_rx(skb, &usb_if->time_ref, le32_to_cpu(sm->ts_low));
585 netdev->stats.rx_packets++;
586 netdev->stats.rx_bytes += cf->len;
588 return 0;
591 /* handle uCAN error message */
592 static int pcan_usb_fd_decode_error(struct pcan_usb_fd_if *usb_if,
593 struct pucan_msg *rx_msg)
595 struct pucan_error_msg *er = (struct pucan_error_msg *)rx_msg;
596 struct pcan_usb_fd_device *pdev;
597 struct peak_usb_device *dev;
599 if (pucan_ermsg_get_channel(er) >= ARRAY_SIZE(usb_if->dev))
600 return -EINVAL;
602 dev = usb_if->dev[pucan_ermsg_get_channel(er)];
603 pdev = container_of(dev, struct pcan_usb_fd_device, dev);
605 /* keep a trace of tx and rx error counters for later use */
606 pdev->bec.txerr = er->tx_err_cnt;
607 pdev->bec.rxerr = er->rx_err_cnt;
609 return 0;
612 /* handle uCAN overrun message */
613 static int pcan_usb_fd_decode_overrun(struct pcan_usb_fd_if *usb_if,
614 struct pucan_msg *rx_msg)
616 struct pcan_ufd_ovr_msg *ov = (struct pcan_ufd_ovr_msg *)rx_msg;
617 struct peak_usb_device *dev;
618 struct net_device *netdev;
619 struct can_frame *cf;
620 struct sk_buff *skb;
622 if (pufd_omsg_get_channel(ov) >= ARRAY_SIZE(usb_if->dev))
623 return -EINVAL;
625 dev = usb_if->dev[pufd_omsg_get_channel(ov)];
626 netdev = dev->netdev;
628 /* allocate an skb to store the error frame */
629 skb = alloc_can_err_skb(netdev, &cf);
630 if (!skb)
631 return -ENOMEM;
633 cf->can_id |= CAN_ERR_CRTL;
634 cf->data[1] |= CAN_ERR_CRTL_RX_OVERFLOW;
636 peak_usb_netif_rx(skb, &usb_if->time_ref, le32_to_cpu(ov->ts_low));
638 netdev->stats.rx_over_errors++;
639 netdev->stats.rx_errors++;
641 return 0;
644 /* handle USB calibration message */
645 static void pcan_usb_fd_decode_ts(struct pcan_usb_fd_if *usb_if,
646 struct pucan_msg *rx_msg)
648 struct pcan_ufd_ts_msg *ts = (struct pcan_ufd_ts_msg *)rx_msg;
650 /* should wait until clock is stabilized */
651 if (usb_if->cm_ignore_count > 0)
652 usb_if->cm_ignore_count--;
653 else
654 peak_usb_set_ts_now(&usb_if->time_ref, le32_to_cpu(ts->ts_low));
657 /* callback for bulk IN urb */
658 static int pcan_usb_fd_decode_buf(struct peak_usb_device *dev, struct urb *urb)
660 struct pcan_usb_fd_if *usb_if = pcan_usb_fd_dev_if(dev);
661 struct net_device *netdev = dev->netdev;
662 struct pucan_msg *rx_msg;
663 u8 *msg_ptr, *msg_end;
664 int err = 0;
666 /* loop reading all the records from the incoming message */
667 msg_ptr = urb->transfer_buffer;
668 msg_end = urb->transfer_buffer + urb->actual_length;
669 for (; msg_ptr < msg_end;) {
670 u16 rx_msg_type, rx_msg_size;
672 rx_msg = (struct pucan_msg *)msg_ptr;
673 if (!rx_msg->size) {
674 /* null packet found: end of list */
675 break;
678 rx_msg_size = le16_to_cpu(rx_msg->size);
679 rx_msg_type = le16_to_cpu(rx_msg->type);
681 /* check if the record goes out of current packet */
682 if (msg_ptr + rx_msg_size > msg_end) {
683 netdev_err(netdev,
684 "got frag rec: should inc usb rx buf sze\n");
685 err = -EBADMSG;
686 break;
689 switch (rx_msg_type) {
690 case PUCAN_MSG_CAN_RX:
691 err = pcan_usb_fd_decode_canmsg(usb_if, rx_msg);
692 if (err < 0)
693 goto fail;
694 break;
696 case PCAN_UFD_MSG_CALIBRATION:
697 pcan_usb_fd_decode_ts(usb_if, rx_msg);
698 break;
700 case PUCAN_MSG_ERROR:
701 err = pcan_usb_fd_decode_error(usb_if, rx_msg);
702 if (err < 0)
703 goto fail;
704 break;
706 case PUCAN_MSG_STATUS:
707 err = pcan_usb_fd_decode_status(usb_if, rx_msg);
708 if (err < 0)
709 goto fail;
710 break;
712 case PCAN_UFD_MSG_OVERRUN:
713 err = pcan_usb_fd_decode_overrun(usb_if, rx_msg);
714 if (err < 0)
715 goto fail;
716 break;
718 default:
719 netdev_err(netdev,
720 "unhandled msg type 0x%02x (%d): ignored\n",
721 rx_msg_type, rx_msg_type);
722 break;
725 msg_ptr += rx_msg_size;
728 fail:
729 if (err)
730 pcan_dump_mem("received msg",
731 urb->transfer_buffer, urb->actual_length);
732 return err;
735 /* CAN/CANFD frames encoding callback */
736 static int pcan_usb_fd_encode_msg(struct peak_usb_device *dev,
737 struct sk_buff *skb, u8 *obuf, size_t *size)
739 struct pucan_tx_msg *tx_msg = (struct pucan_tx_msg *)obuf;
740 struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
741 u16 tx_msg_size, tx_msg_flags;
742 u8 dlc;
744 if (cfd->len > CANFD_MAX_DLEN)
745 return -EINVAL;
747 tx_msg_size = ALIGN(sizeof(struct pucan_tx_msg) + cfd->len, 4);
748 tx_msg->size = cpu_to_le16(tx_msg_size);
749 tx_msg->type = cpu_to_le16(PUCAN_MSG_CAN_TX);
751 tx_msg_flags = 0;
752 if (cfd->can_id & CAN_EFF_FLAG) {
753 tx_msg_flags |= PUCAN_MSG_EXT_ID;
754 tx_msg->can_id = cpu_to_le32(cfd->can_id & CAN_EFF_MASK);
755 } else {
756 tx_msg->can_id = cpu_to_le32(cfd->can_id & CAN_SFF_MASK);
759 if (can_is_canfd_skb(skb)) {
760 /* considering a CANFD frame */
761 dlc = can_fd_len2dlc(cfd->len);
763 tx_msg_flags |= PUCAN_MSG_EXT_DATA_LEN;
765 if (cfd->flags & CANFD_BRS)
766 tx_msg_flags |= PUCAN_MSG_BITRATE_SWITCH;
768 if (cfd->flags & CANFD_ESI)
769 tx_msg_flags |= PUCAN_MSG_ERROR_STATE_IND;
770 } else {
771 /* CAND 2.0 frames */
772 dlc = can_get_cc_dlc((struct can_frame *)cfd,
773 dev->can.ctrlmode);
775 if (cfd->can_id & CAN_RTR_FLAG)
776 tx_msg_flags |= PUCAN_MSG_RTR;
779 tx_msg->flags = cpu_to_le16(tx_msg_flags);
780 tx_msg->channel_dlc = PUCAN_MSG_CHANNEL_DLC(dev->ctrl_idx, dlc);
781 memcpy(tx_msg->d, cfd->data, cfd->len);
783 /* add null size message to tag the end (messages are 32-bits aligned)
785 tx_msg = (struct pucan_tx_msg *)(obuf + tx_msg_size);
787 tx_msg->size = 0;
789 /* set the whole size of the USB packet to send */
790 *size = tx_msg_size + sizeof(u32);
792 return 0;
795 /* start the interface (last chance before set bus on) */
796 static int pcan_usb_fd_start(struct peak_usb_device *dev)
798 struct pcan_usb_fd_device *pdev =
799 container_of(dev, struct pcan_usb_fd_device, dev);
800 int err;
802 /* set filter mode: all acceptance */
803 err = pcan_usb_fd_set_filter_std(dev, -1, 0xffffffff);
804 if (err)
805 return err;
807 /* opening first device: */
808 if (pdev->usb_if->dev_opened_count == 0) {
809 /* reset time_ref */
810 peak_usb_init_time_ref(&pdev->usb_if->time_ref,
811 &pcan_usb_pro_fd);
813 /* enable USB calibration messages */
814 err = pcan_usb_fd_set_options(dev, 1,
815 PUCAN_OPTION_ERROR,
816 PCAN_UFD_FLTEXT_CALIBRATION);
819 pdev->usb_if->dev_opened_count++;
821 /* reset cached error counters */
822 pdev->bec.txerr = 0;
823 pdev->bec.rxerr = 0;
825 return err;
828 /* socket callback used to copy berr counters values received through USB */
829 static int pcan_usb_fd_get_berr_counter(const struct net_device *netdev,
830 struct can_berr_counter *bec)
832 struct peak_usb_device *dev = netdev_priv(netdev);
833 struct pcan_usb_fd_device *pdev =
834 container_of(dev, struct pcan_usb_fd_device, dev);
836 *bec = pdev->bec;
838 /* must return 0 */
839 return 0;
842 /* stop interface (last chance before set bus off) */
843 static int pcan_usb_fd_stop(struct peak_usb_device *dev)
845 struct pcan_usb_fd_device *pdev =
846 container_of(dev, struct pcan_usb_fd_device, dev);
848 /* turn off special msgs for that interface if no other dev opened */
849 if (pdev->usb_if->dev_opened_count == 1)
850 pcan_usb_fd_set_options(dev, 0,
851 PUCAN_OPTION_ERROR,
852 PCAN_UFD_FLTEXT_CALIBRATION);
853 pdev->usb_if->dev_opened_count--;
855 return 0;
858 /* called when probing, to initialize a device object */
859 static int pcan_usb_fd_init(struct peak_usb_device *dev)
861 struct pcan_usb_fd_device *pdev =
862 container_of(dev, struct pcan_usb_fd_device, dev);
863 int i, err = -ENOMEM;
865 /* do this for 1st channel only */
866 if (!dev->prev_siblings) {
867 /* allocate netdevices common structure attached to first one */
868 pdev->usb_if = kzalloc(sizeof(*pdev->usb_if), GFP_KERNEL);
869 if (!pdev->usb_if)
870 goto err_out;
872 /* allocate command buffer once for all for the interface */
873 pdev->cmd_buffer_addr = kzalloc(PCAN_UFD_CMD_BUFFER_SIZE,
874 GFP_KERNEL);
875 if (!pdev->cmd_buffer_addr)
876 goto err_out_1;
878 /* number of ts msgs to ignore before taking one into account */
879 pdev->usb_if->cm_ignore_count = 5;
881 err = pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_INFO,
882 PCAN_USBPRO_INFO_FW,
883 &pdev->usb_if->fw_info,
884 sizeof(pdev->usb_if->fw_info));
885 if (err) {
886 dev_err(dev->netdev->dev.parent,
887 "unable to read %s firmware info (err %d)\n",
888 dev->adapter->name, err);
889 goto err_out_2;
892 /* explicit use of dev_xxx() instead of netdev_xxx() here:
893 * information displayed are related to the device itself, not
894 * to the canx (channel) device.
896 dev_info(dev->netdev->dev.parent,
897 "PEAK-System %s v%u fw v%u.%u.%u (%u channels)\n",
898 dev->adapter->name, pdev->usb_if->fw_info.hw_version,
899 pdev->usb_if->fw_info.fw_version[0],
900 pdev->usb_if->fw_info.fw_version[1],
901 pdev->usb_if->fw_info.fw_version[2],
902 dev->adapter->ctrl_count);
904 /* check for ability to switch between ISO/non-ISO modes */
905 if (pdev->usb_if->fw_info.fw_version[0] >= 2) {
906 /* firmware >= 2.x supports ISO/non-ISO switching */
907 dev->can.ctrlmode_supported |= CAN_CTRLMODE_FD_NON_ISO;
908 } else {
909 /* firmware < 2.x only supports fixed(!) non-ISO */
910 dev->can.ctrlmode |= CAN_CTRLMODE_FD_NON_ISO;
913 /* tell the hardware the can driver is running */
914 err = pcan_usb_fd_drv_loaded(dev, 1);
915 if (err) {
916 dev_err(dev->netdev->dev.parent,
917 "unable to tell %s driver is loaded (err %d)\n",
918 dev->adapter->name, err);
919 goto err_out_2;
921 } else {
922 /* otherwise, simply copy previous sibling's values */
923 struct pcan_usb_fd_device *ppdev =
924 container_of(dev->prev_siblings,
925 struct pcan_usb_fd_device, dev);
927 pdev->usb_if = ppdev->usb_if;
928 pdev->cmd_buffer_addr = ppdev->cmd_buffer_addr;
930 /* do a copy of the ctrlmode[_supported] too */
931 dev->can.ctrlmode = ppdev->dev.can.ctrlmode;
932 dev->can.ctrlmode_supported = ppdev->dev.can.ctrlmode_supported;
935 pdev->usb_if->dev[dev->ctrl_idx] = dev;
936 dev->device_number =
937 le32_to_cpu(pdev->usb_if->fw_info.dev_id[dev->ctrl_idx]);
939 /* set clock domain */
940 for (i = 0; i < ARRAY_SIZE(pcan_usb_fd_clk_freq); i++)
941 if (dev->adapter->clock.freq == pcan_usb_fd_clk_freq[i])
942 break;
944 if (i >= ARRAY_SIZE(pcan_usb_fd_clk_freq)) {
945 dev_warn(dev->netdev->dev.parent,
946 "incompatible clock frequencies\n");
947 err = -EINVAL;
948 goto err_out_2;
951 pcan_usb_fd_set_clock_domain(dev, i);
953 /* set LED in default state (end of init phase) */
954 pcan_usb_fd_set_can_led(dev, PCAN_UFD_LED_DEF);
956 return 0;
958 err_out_2:
959 kfree(pdev->cmd_buffer_addr);
960 err_out_1:
961 kfree(pdev->usb_if);
962 err_out:
963 return err;
966 /* called when driver module is being unloaded */
967 static void pcan_usb_fd_exit(struct peak_usb_device *dev)
969 struct pcan_usb_fd_device *pdev =
970 container_of(dev, struct pcan_usb_fd_device, dev);
972 /* when rmmod called before unplug and if down, should reset things
973 * before leaving
975 if (dev->can.state != CAN_STATE_STOPPED) {
976 /* set bus off on the corresponding channel */
977 pcan_usb_fd_set_bus(dev, 0);
980 /* switch off corresponding CAN LEDs */
981 pcan_usb_fd_set_can_led(dev, PCAN_UFD_LED_OFF);
983 /* if channel #0 (only) */
984 if (dev->ctrl_idx == 0) {
985 /* turn off calibration message if any device were opened */
986 if (pdev->usb_if->dev_opened_count > 0)
987 pcan_usb_fd_set_options(dev, 0,
988 PUCAN_OPTION_ERROR,
989 PCAN_UFD_FLTEXT_CALIBRATION);
991 /* tell USB adapter that the driver is being unloaded */
992 pcan_usb_fd_drv_loaded(dev, 0);
996 /* called when the USB adapter is unplugged */
997 static void pcan_usb_fd_free(struct peak_usb_device *dev)
999 /* last device: can free shared objects now */
1000 if (!dev->prev_siblings && !dev->next_siblings) {
1001 struct pcan_usb_fd_device *pdev =
1002 container_of(dev, struct pcan_usb_fd_device, dev);
1004 /* free commands buffer */
1005 kfree(pdev->cmd_buffer_addr);
1007 /* free usb interface object */
1008 kfree(pdev->usb_if);
1012 /* describes the PCAN-USB FD adapter */
1013 static const struct can_bittiming_const pcan_usb_fd_const = {
1014 .name = "pcan_usb_fd",
1015 .tseg1_min = 1,
1016 .tseg1_max = (1 << PUCAN_TSLOW_TSGEG1_BITS),
1017 .tseg2_min = 1,
1018 .tseg2_max = (1 << PUCAN_TSLOW_TSGEG2_BITS),
1019 .sjw_max = (1 << PUCAN_TSLOW_SJW_BITS),
1020 .brp_min = 1,
1021 .brp_max = (1 << PUCAN_TSLOW_BRP_BITS),
1022 .brp_inc = 1,
1025 static const struct can_bittiming_const pcan_usb_fd_data_const = {
1026 .name = "pcan_usb_fd",
1027 .tseg1_min = 1,
1028 .tseg1_max = (1 << PUCAN_TFAST_TSGEG1_BITS),
1029 .tseg2_min = 1,
1030 .tseg2_max = (1 << PUCAN_TFAST_TSGEG2_BITS),
1031 .sjw_max = (1 << PUCAN_TFAST_SJW_BITS),
1032 .brp_min = 1,
1033 .brp_max = (1 << PUCAN_TFAST_BRP_BITS),
1034 .brp_inc = 1,
1037 const struct peak_usb_adapter pcan_usb_fd = {
1038 .name = "PCAN-USB FD",
1039 .device_id = PCAN_USBFD_PRODUCT_ID,
1040 .ctrl_count = PCAN_USBFD_CHANNEL_COUNT,
1041 .ctrlmode_supported = CAN_CTRLMODE_FD |
1042 CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY |
1043 CAN_CTRLMODE_CC_LEN8_DLC,
1044 .clock = {
1045 .freq = PCAN_UFD_CRYSTAL_HZ,
1047 .bittiming_const = &pcan_usb_fd_const,
1048 .data_bittiming_const = &pcan_usb_fd_data_const,
1050 /* size of device private data */
1051 .sizeof_dev_private = sizeof(struct pcan_usb_fd_device),
1053 /* timestamps usage */
1054 .ts_used_bits = 32,
1055 .ts_period = 1000000, /* calibration period in ts. */
1056 .us_per_ts_scale = 1, /* us = (ts * scale) >> shift */
1057 .us_per_ts_shift = 0,
1059 /* give here messages in/out endpoints */
1060 .ep_msg_in = PCAN_USBPRO_EP_MSGIN,
1061 .ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0},
1063 /* size of rx/tx usb buffers */
1064 .rx_buffer_size = PCAN_UFD_RX_BUFFER_SIZE,
1065 .tx_buffer_size = PCAN_UFD_TX_BUFFER_SIZE,
1067 /* device callbacks */
1068 .intf_probe = pcan_usb_pro_probe, /* same as PCAN-USB Pro */
1069 .dev_init = pcan_usb_fd_init,
1071 .dev_exit = pcan_usb_fd_exit,
1072 .dev_free = pcan_usb_fd_free,
1073 .dev_set_bus = pcan_usb_fd_set_bus,
1074 .dev_set_bittiming = pcan_usb_fd_set_bittiming_slow,
1075 .dev_set_data_bittiming = pcan_usb_fd_set_bittiming_fast,
1076 .dev_decode_buf = pcan_usb_fd_decode_buf,
1077 .dev_start = pcan_usb_fd_start,
1078 .dev_stop = pcan_usb_fd_stop,
1079 .dev_restart_async = pcan_usb_fd_restart_async,
1080 .dev_encode_msg = pcan_usb_fd_encode_msg,
1082 .do_get_berr_counter = pcan_usb_fd_get_berr_counter,
1085 /* describes the PCAN-CHIP USB */
1086 static const struct can_bittiming_const pcan_usb_chip_const = {
1087 .name = "pcan_chip_usb",
1088 .tseg1_min = 1,
1089 .tseg1_max = (1 << PUCAN_TSLOW_TSGEG1_BITS),
1090 .tseg2_min = 1,
1091 .tseg2_max = (1 << PUCAN_TSLOW_TSGEG2_BITS),
1092 .sjw_max = (1 << PUCAN_TSLOW_SJW_BITS),
1093 .brp_min = 1,
1094 .brp_max = (1 << PUCAN_TSLOW_BRP_BITS),
1095 .brp_inc = 1,
1098 static const struct can_bittiming_const pcan_usb_chip_data_const = {
1099 .name = "pcan_chip_usb",
1100 .tseg1_min = 1,
1101 .tseg1_max = (1 << PUCAN_TFAST_TSGEG1_BITS),
1102 .tseg2_min = 1,
1103 .tseg2_max = (1 << PUCAN_TFAST_TSGEG2_BITS),
1104 .sjw_max = (1 << PUCAN_TFAST_SJW_BITS),
1105 .brp_min = 1,
1106 .brp_max = (1 << PUCAN_TFAST_BRP_BITS),
1107 .brp_inc = 1,
1110 const struct peak_usb_adapter pcan_usb_chip = {
1111 .name = "PCAN-Chip USB",
1112 .device_id = PCAN_USBCHIP_PRODUCT_ID,
1113 .ctrl_count = PCAN_USBFD_CHANNEL_COUNT,
1114 .ctrlmode_supported = CAN_CTRLMODE_FD |
1115 CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY |
1116 CAN_CTRLMODE_CC_LEN8_DLC,
1117 .clock = {
1118 .freq = PCAN_UFD_CRYSTAL_HZ,
1120 .bittiming_const = &pcan_usb_chip_const,
1121 .data_bittiming_const = &pcan_usb_chip_data_const,
1123 /* size of device private data */
1124 .sizeof_dev_private = sizeof(struct pcan_usb_fd_device),
1126 /* timestamps usage */
1127 .ts_used_bits = 32,
1128 .ts_period = 1000000, /* calibration period in ts. */
1129 .us_per_ts_scale = 1, /* us = (ts * scale) >> shift */
1130 .us_per_ts_shift = 0,
1132 /* give here messages in/out endpoints */
1133 .ep_msg_in = PCAN_USBPRO_EP_MSGIN,
1134 .ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0},
1136 /* size of rx/tx usb buffers */
1137 .rx_buffer_size = PCAN_UFD_RX_BUFFER_SIZE,
1138 .tx_buffer_size = PCAN_UFD_TX_BUFFER_SIZE,
1140 /* device callbacks */
1141 .intf_probe = pcan_usb_pro_probe, /* same as PCAN-USB Pro */
1142 .dev_init = pcan_usb_fd_init,
1144 .dev_exit = pcan_usb_fd_exit,
1145 .dev_free = pcan_usb_fd_free,
1146 .dev_set_bus = pcan_usb_fd_set_bus,
1147 .dev_set_bittiming = pcan_usb_fd_set_bittiming_slow,
1148 .dev_set_data_bittiming = pcan_usb_fd_set_bittiming_fast,
1149 .dev_decode_buf = pcan_usb_fd_decode_buf,
1150 .dev_start = pcan_usb_fd_start,
1151 .dev_stop = pcan_usb_fd_stop,
1152 .dev_restart_async = pcan_usb_fd_restart_async,
1153 .dev_encode_msg = pcan_usb_fd_encode_msg,
1155 .do_get_berr_counter = pcan_usb_fd_get_berr_counter,
1158 /* describes the PCAN-USB Pro FD adapter */
1159 static const struct can_bittiming_const pcan_usb_pro_fd_const = {
1160 .name = "pcan_usb_pro_fd",
1161 .tseg1_min = 1,
1162 .tseg1_max = (1 << PUCAN_TSLOW_TSGEG1_BITS),
1163 .tseg2_min = 1,
1164 .tseg2_max = (1 << PUCAN_TSLOW_TSGEG2_BITS),
1165 .sjw_max = (1 << PUCAN_TSLOW_SJW_BITS),
1166 .brp_min = 1,
1167 .brp_max = (1 << PUCAN_TSLOW_BRP_BITS),
1168 .brp_inc = 1,
1171 static const struct can_bittiming_const pcan_usb_pro_fd_data_const = {
1172 .name = "pcan_usb_pro_fd",
1173 .tseg1_min = 1,
1174 .tseg1_max = (1 << PUCAN_TFAST_TSGEG1_BITS),
1175 .tseg2_min = 1,
1176 .tseg2_max = (1 << PUCAN_TFAST_TSGEG2_BITS),
1177 .sjw_max = (1 << PUCAN_TFAST_SJW_BITS),
1178 .brp_min = 1,
1179 .brp_max = (1 << PUCAN_TFAST_BRP_BITS),
1180 .brp_inc = 1,
1183 const struct peak_usb_adapter pcan_usb_pro_fd = {
1184 .name = "PCAN-USB Pro FD",
1185 .device_id = PCAN_USBPROFD_PRODUCT_ID,
1186 .ctrl_count = PCAN_USBPROFD_CHANNEL_COUNT,
1187 .ctrlmode_supported = CAN_CTRLMODE_FD |
1188 CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY |
1189 CAN_CTRLMODE_CC_LEN8_DLC,
1190 .clock = {
1191 .freq = PCAN_UFD_CRYSTAL_HZ,
1193 .bittiming_const = &pcan_usb_pro_fd_const,
1194 .data_bittiming_const = &pcan_usb_pro_fd_data_const,
1196 /* size of device private data */
1197 .sizeof_dev_private = sizeof(struct pcan_usb_fd_device),
1199 /* timestamps usage */
1200 .ts_used_bits = 32,
1201 .ts_period = 1000000, /* calibration period in ts. */
1202 .us_per_ts_scale = 1, /* us = (ts * scale) >> shift */
1203 .us_per_ts_shift = 0,
1205 /* give here messages in/out endpoints */
1206 .ep_msg_in = PCAN_USBPRO_EP_MSGIN,
1207 .ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0, PCAN_USBPRO_EP_MSGOUT_1},
1209 /* size of rx/tx usb buffers */
1210 .rx_buffer_size = PCAN_UFD_RX_BUFFER_SIZE,
1211 .tx_buffer_size = PCAN_UFD_TX_BUFFER_SIZE,
1213 /* device callbacks */
1214 .intf_probe = pcan_usb_pro_probe, /* same as PCAN-USB Pro */
1215 .dev_init = pcan_usb_fd_init,
1217 .dev_exit = pcan_usb_fd_exit,
1218 .dev_free = pcan_usb_fd_free,
1219 .dev_set_bus = pcan_usb_fd_set_bus,
1220 .dev_set_bittiming = pcan_usb_fd_set_bittiming_slow,
1221 .dev_set_data_bittiming = pcan_usb_fd_set_bittiming_fast,
1222 .dev_decode_buf = pcan_usb_fd_decode_buf,
1223 .dev_start = pcan_usb_fd_start,
1224 .dev_stop = pcan_usb_fd_stop,
1225 .dev_restart_async = pcan_usb_fd_restart_async,
1226 .dev_encode_msg = pcan_usb_fd_encode_msg,
1228 .do_get_berr_counter = pcan_usb_fd_get_berr_counter,
1231 /* describes the PCAN-USB X6 adapter */
1232 static const struct can_bittiming_const pcan_usb_x6_const = {
1233 .name = "pcan_usb_x6",
1234 .tseg1_min = 1,
1235 .tseg1_max = (1 << PUCAN_TSLOW_TSGEG1_BITS),
1236 .tseg2_min = 1,
1237 .tseg2_max = (1 << PUCAN_TSLOW_TSGEG2_BITS),
1238 .sjw_max = (1 << PUCAN_TSLOW_SJW_BITS),
1239 .brp_min = 1,
1240 .brp_max = (1 << PUCAN_TSLOW_BRP_BITS),
1241 .brp_inc = 1,
1244 static const struct can_bittiming_const pcan_usb_x6_data_const = {
1245 .name = "pcan_usb_x6",
1246 .tseg1_min = 1,
1247 .tseg1_max = (1 << PUCAN_TFAST_TSGEG1_BITS),
1248 .tseg2_min = 1,
1249 .tseg2_max = (1 << PUCAN_TFAST_TSGEG2_BITS),
1250 .sjw_max = (1 << PUCAN_TFAST_SJW_BITS),
1251 .brp_min = 1,
1252 .brp_max = (1 << PUCAN_TFAST_BRP_BITS),
1253 .brp_inc = 1,
1256 const struct peak_usb_adapter pcan_usb_x6 = {
1257 .name = "PCAN-USB X6",
1258 .device_id = PCAN_USBX6_PRODUCT_ID,
1259 .ctrl_count = PCAN_USBPROFD_CHANNEL_COUNT,
1260 .ctrlmode_supported = CAN_CTRLMODE_FD |
1261 CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY |
1262 CAN_CTRLMODE_CC_LEN8_DLC,
1263 .clock = {
1264 .freq = PCAN_UFD_CRYSTAL_HZ,
1266 .bittiming_const = &pcan_usb_x6_const,
1267 .data_bittiming_const = &pcan_usb_x6_data_const,
1269 /* size of device private data */
1270 .sizeof_dev_private = sizeof(struct pcan_usb_fd_device),
1272 /* timestamps usage */
1273 .ts_used_bits = 32,
1274 .ts_period = 1000000, /* calibration period in ts. */
1275 .us_per_ts_scale = 1, /* us = (ts * scale) >> shift */
1276 .us_per_ts_shift = 0,
1278 /* give here messages in/out endpoints */
1279 .ep_msg_in = PCAN_USBPRO_EP_MSGIN,
1280 .ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0, PCAN_USBPRO_EP_MSGOUT_1},
1282 /* size of rx/tx usb buffers */
1283 .rx_buffer_size = PCAN_UFD_RX_BUFFER_SIZE,
1284 .tx_buffer_size = PCAN_UFD_TX_BUFFER_SIZE,
1286 /* device callbacks */
1287 .intf_probe = pcan_usb_pro_probe, /* same as PCAN-USB Pro */
1288 .dev_init = pcan_usb_fd_init,
1290 .dev_exit = pcan_usb_fd_exit,
1291 .dev_free = pcan_usb_fd_free,
1292 .dev_set_bus = pcan_usb_fd_set_bus,
1293 .dev_set_bittiming = pcan_usb_fd_set_bittiming_slow,
1294 .dev_set_data_bittiming = pcan_usb_fd_set_bittiming_fast,
1295 .dev_decode_buf = pcan_usb_fd_decode_buf,
1296 .dev_start = pcan_usb_fd_start,
1297 .dev_stop = pcan_usb_fd_stop,
1298 .dev_restart_async = pcan_usb_fd_restart_async,
1299 .dev_encode_msg = pcan_usb_fd_encode_msg,
1301 .do_get_berr_counter = pcan_usb_fd_get_berr_counter,