2 * CAN driver for PEAK System PCAN-USB FD / PCAN-USB Pro FD adapter
4 * Copyright (C) 2013-2014 Stephane Grosjean <s.grosjean@peak-system.com>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published
8 * by the Free Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 #include <linux/netdevice.h>
16 #include <linux/usb.h>
17 #include <linux/module.h>
19 #include <linux/can.h>
20 #include <linux/can/dev.h>
21 #include <linux/can/error.h>
22 #include <linux/can/dev/peak_canfd.h>
24 #include "pcan_usb_core.h"
25 #include "pcan_usb_pro.h"
27 MODULE_SUPPORTED_DEVICE("PEAK-System PCAN-USB FD adapter");
28 MODULE_SUPPORTED_DEVICE("PEAK-System PCAN-USB Pro FD adapter");
30 #define PCAN_USBPROFD_CHANNEL_COUNT 2
31 #define PCAN_USBFD_CHANNEL_COUNT 1
33 /* PCAN-USB Pro FD adapter internal clock (Hz) */
34 #define PCAN_UFD_CRYSTAL_HZ 80000000
36 #define PCAN_UFD_CMD_BUFFER_SIZE 512
37 #define PCAN_UFD_LOSPD_PKT_SIZE 64
39 /* PCAN-USB Pro FD command timeout (ms.) */
40 #define PCAN_UFD_CMD_TIMEOUT_MS 1000
42 /* PCAN-USB Pro FD rx/tx buffers size */
43 #define PCAN_UFD_RX_BUFFER_SIZE 2048
44 #define PCAN_UFD_TX_BUFFER_SIZE 512
46 /* read some versions info from the hw devcie */
47 struct __packed pcan_ufd_fw_info {
48 __le16 size_of; /* sizeof this */
49 __le16 type; /* type of this structure */
50 u8 hw_type; /* Type of hardware (HW_TYPE_xxx) */
51 u8 bl_version[3]; /* Bootloader version */
52 u8 hw_version; /* Hardware version (PCB) */
53 u8 fw_version[3]; /* Firmware version */
54 __le32 dev_id[2]; /* "device id" per CAN */
55 __le32 ser_no; /* S/N */
56 __le32 flags; /* special functions */
59 /* handle device specific info used by the netdevices */
60 struct pcan_usb_fd_if {
61 struct peak_usb_device *dev[PCAN_USB_MAX_CHANNEL];
62 struct pcan_ufd_fw_info fw_info;
63 struct peak_time_ref time_ref;
68 /* device information */
69 struct pcan_usb_fd_device {
70 struct peak_usb_device dev;
71 struct can_berr_counter bec;
72 struct pcan_usb_fd_if *usb_if;
76 /* Extended USB commands (non uCAN commands) */
78 /* Clock Modes command */
79 #define PCAN_UFD_CMD_CLK_SET 0x80
81 #define PCAN_UFD_CLK_80MHZ 0x0
82 #define PCAN_UFD_CLK_60MHZ 0x1
83 #define PCAN_UFD_CLK_40MHZ 0x2
84 #define PCAN_UFD_CLK_30MHZ 0x3
85 #define PCAN_UFD_CLK_24MHZ 0x4
86 #define PCAN_UFD_CLK_20MHZ 0x5
87 #define PCAN_UFD_CLK_DEF PCAN_UFD_CLK_80MHZ
89 struct __packed pcan_ufd_clock {
90 __le16 opcode_channel;
96 /* LED control command */
97 #define PCAN_UFD_CMD_LED_SET 0x86
99 #define PCAN_UFD_LED_DEV 0x00
100 #define PCAN_UFD_LED_FAST 0x01
101 #define PCAN_UFD_LED_SLOW 0x02
102 #define PCAN_UFD_LED_ON 0x03
103 #define PCAN_UFD_LED_OFF 0x04
104 #define PCAN_UFD_LED_DEF PCAN_UFD_LED_DEV
106 struct __packed pcan_ufd_led {
107 __le16 opcode_channel;
113 /* Extended usage of uCAN commands CMD_xxx_xx_OPTION for PCAN-USB Pro FD */
114 #define PCAN_UFD_FLTEXT_CALIBRATION 0x8000
116 struct __packed pcan_ufd_options {
117 __le16 opcode_channel;
124 /* Extended usage of uCAN messages for PCAN-USB Pro FD */
125 #define PCAN_UFD_MSG_CALIBRATION 0x100
127 struct __packed pcan_ufd_ts_msg {
132 __le16 usb_frame_index;
136 #define PCAN_UFD_MSG_OVERRUN 0x101
138 #define PCAN_UFD_OVMSG_CHANNEL(o) ((o)->channel & 0xf)
140 struct __packed pcan_ufd_ovr_msg {
149 static inline int pufd_omsg_get_channel(struct pcan_ufd_ovr_msg *om)
151 return om->channel & 0xf;
154 /* Clock mode frequency values */
155 static const u32 pcan_usb_fd_clk_freq[6] = {
156 [PCAN_UFD_CLK_80MHZ] = 80000000,
157 [PCAN_UFD_CLK_60MHZ] = 60000000,
158 [PCAN_UFD_CLK_40MHZ] = 40000000,
159 [PCAN_UFD_CLK_30MHZ] = 30000000,
160 [PCAN_UFD_CLK_24MHZ] = 24000000,
161 [PCAN_UFD_CLK_20MHZ] = 20000000
164 /* return a device USB interface */
166 struct pcan_usb_fd_if *pcan_usb_fd_dev_if(struct peak_usb_device *dev)
168 struct pcan_usb_fd_device *pdev =
169 container_of(dev, struct pcan_usb_fd_device, dev);
173 /* return a device USB commands buffer */
174 static inline void *pcan_usb_fd_cmd_buffer(struct peak_usb_device *dev)
176 struct pcan_usb_fd_device *pdev =
177 container_of(dev, struct pcan_usb_fd_device, dev);
178 return pdev->cmd_buffer_addr;
181 /* send PCAN-USB Pro FD commands synchronously */
182 static int pcan_usb_fd_send_cmd(struct peak_usb_device *dev, void *cmd_tail)
184 void *cmd_head = pcan_usb_fd_cmd_buffer(dev);
190 /* usb device unregistered? */
191 if (!(dev->state & PCAN_USB_STATE_CONNECTED))
194 /* if a packet is not filled completely by commands, the command list
195 * is terminated with an "end of collection" record.
197 cmd_len = cmd_tail - cmd_head;
198 if (cmd_len <= (PCAN_UFD_CMD_BUFFER_SIZE - sizeof(u64))) {
199 memset(cmd_tail, 0xff, sizeof(u64));
200 cmd_len += sizeof(u64);
203 packet_ptr = cmd_head;
204 packet_len = cmd_len;
206 /* firmware is not able to re-assemble 512 bytes buffer in full-speed */
207 if (unlikely(dev->udev->speed != USB_SPEED_HIGH))
208 packet_len = min(packet_len, PCAN_UFD_LOSPD_PKT_SIZE);
211 err = usb_bulk_msg(dev->udev,
212 usb_sndbulkpipe(dev->udev,
213 PCAN_USBPRO_EP_CMDOUT),
214 packet_ptr, packet_len,
215 NULL, PCAN_UFD_CMD_TIMEOUT_MS);
217 netdev_err(dev->netdev,
218 "sending command failure: %d\n", err);
222 packet_ptr += packet_len;
223 cmd_len -= packet_len;
225 if (cmd_len < PCAN_UFD_LOSPD_PKT_SIZE)
226 packet_len = cmd_len;
228 } while (packet_len > 0);
233 /* build the commands list in the given buffer, to enter operational mode */
234 static int pcan_usb_fd_build_restart_cmd(struct peak_usb_device *dev, u8 *buf)
236 struct pucan_wr_err_cnt *prc;
237 struct pucan_command *cmd;
240 /* 1st, reset error counters: */
241 prc = (struct pucan_wr_err_cnt *)pc;
242 prc->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx,
243 PUCAN_CMD_WR_ERR_CNT);
245 /* select both counters */
246 prc->sel_mask = cpu_to_le16(PUCAN_WRERRCNT_TE|PUCAN_WRERRCNT_RE);
248 /* and reset their values */
252 /* moves the pointer forward */
253 pc += sizeof(struct pucan_wr_err_cnt);
255 /* add command to switch from ISO to non-ISO mode, if fw allows it */
256 if (dev->can.ctrlmode_supported & CAN_CTRLMODE_FD_NON_ISO) {
257 struct pucan_options *puo = (struct pucan_options *)pc;
259 puo->opcode_channel =
260 (dev->can.ctrlmode & CAN_CTRLMODE_FD_NON_ISO) ?
261 pucan_cmd_opcode_channel(dev->ctrl_idx,
262 PUCAN_CMD_CLR_DIS_OPTION) :
263 pucan_cmd_opcode_channel(dev->ctrl_idx,
264 PUCAN_CMD_SET_EN_OPTION);
266 puo->options = cpu_to_le16(PUCAN_OPTION_CANDFDISO);
268 /* to be sure that no other extended bits will be taken into
273 /* moves the pointer forward */
274 pc += sizeof(struct pucan_options);
277 /* next, go back to operational mode */
278 cmd = (struct pucan_command *)pc;
279 cmd->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx,
280 (dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) ?
281 PUCAN_CMD_LISTEN_ONLY_MODE :
282 PUCAN_CMD_NORMAL_MODE);
283 pc += sizeof(struct pucan_command);
288 /* set CAN bus on/off */
289 static int pcan_usb_fd_set_bus(struct peak_usb_device *dev, u8 onoff)
291 u8 *pc = pcan_usb_fd_cmd_buffer(dev);
295 /* build the cmds list to enter operational mode */
296 l = pcan_usb_fd_build_restart_cmd(dev, pc);
298 struct pucan_command *cmd = (struct pucan_command *)pc;
300 /* build cmd to go back to reset mode */
301 cmd->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx,
302 PUCAN_CMD_RESET_MODE);
303 l = sizeof(struct pucan_command);
306 /* send the command */
307 return pcan_usb_fd_send_cmd(dev, pc + l);
310 /* set filtering masks:
312 * idx in range [0..63] selects a row #idx, all rows otherwise
313 * mask in range [0..0xffffffff] defines up to 32 CANIDs in the row(s)
315 * Each bit of this 64 x 32 bits array defines a CANID value:
317 * bit[i,j] = 1 implies that CANID=(i x 32)+j will be received, while
318 * bit[i,j] = 0 implies that CANID=(i x 32)+j will be discarded.
320 static int pcan_usb_fd_set_filter_std(struct peak_usb_device *dev, int idx,
323 struct pucan_filter_std *cmd = pcan_usb_fd_cmd_buffer(dev);
326 /* select all rows when idx is out of range [0..63] */
327 if ((idx < 0) || (idx >= (1 << PUCAN_FLTSTD_ROW_IDX_BITS))) {
328 n = 1 << PUCAN_FLTSTD_ROW_IDX_BITS;
331 /* select the row (and only the row) otherwise */
336 for (i = idx; i < n; i++, cmd++) {
337 cmd->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx,
338 PUCAN_CMD_FILTER_STD);
339 cmd->idx = cpu_to_le16(i);
340 cmd->mask = cpu_to_le32(mask);
343 /* send the command */
344 return pcan_usb_fd_send_cmd(dev, cmd);
349 * onoff set(1)/unset(0) options
350 * mask each bit defines a kind of options to set/unset
352 static int pcan_usb_fd_set_options(struct peak_usb_device *dev,
353 bool onoff, u16 ucan_mask, u16 usb_mask)
355 struct pcan_ufd_options *cmd = pcan_usb_fd_cmd_buffer(dev);
357 cmd->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx,
358 (onoff) ? PUCAN_CMD_SET_EN_OPTION :
359 PUCAN_CMD_CLR_DIS_OPTION);
361 cmd->ucan_mask = cpu_to_le16(ucan_mask);
362 cmd->usb_mask = cpu_to_le16(usb_mask);
364 /* send the command */
365 return pcan_usb_fd_send_cmd(dev, ++cmd);
368 /* setup LED control */
369 static int pcan_usb_fd_set_can_led(struct peak_usb_device *dev, u8 led_mode)
371 struct pcan_ufd_led *cmd = pcan_usb_fd_cmd_buffer(dev);
373 cmd->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx,
374 PCAN_UFD_CMD_LED_SET);
375 cmd->mode = led_mode;
377 /* send the command */
378 return pcan_usb_fd_send_cmd(dev, ++cmd);
381 /* set CAN clock domain */
382 static int pcan_usb_fd_set_clock_domain(struct peak_usb_device *dev,
385 struct pcan_ufd_clock *cmd = pcan_usb_fd_cmd_buffer(dev);
387 cmd->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx,
388 PCAN_UFD_CMD_CLK_SET);
389 cmd->mode = clk_mode;
391 /* send the command */
392 return pcan_usb_fd_send_cmd(dev, ++cmd);
395 /* set bittiming for CAN and CAN-FD header */
396 static int pcan_usb_fd_set_bittiming_slow(struct peak_usb_device *dev,
397 struct can_bittiming *bt)
399 struct pucan_timing_slow *cmd = pcan_usb_fd_cmd_buffer(dev);
401 cmd->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx,
402 PUCAN_CMD_TIMING_SLOW);
403 cmd->sjw_t = PUCAN_TSLOW_SJW_T(bt->sjw - 1,
404 dev->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES);
406 cmd->tseg2 = PUCAN_TSLOW_TSEG2(bt->phase_seg2 - 1);
407 cmd->tseg1 = PUCAN_TSLOW_TSEG1(bt->prop_seg + bt->phase_seg1 - 1);
408 cmd->brp = cpu_to_le16(PUCAN_TSLOW_BRP(bt->brp - 1));
410 cmd->ewl = 96; /* default */
412 /* send the command */
413 return pcan_usb_fd_send_cmd(dev, ++cmd);
416 /* set CAN-FD bittiming for data */
417 static int pcan_usb_fd_set_bittiming_fast(struct peak_usb_device *dev,
418 struct can_bittiming *bt)
420 struct pucan_timing_fast *cmd = pcan_usb_fd_cmd_buffer(dev);
422 cmd->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx,
423 PUCAN_CMD_TIMING_FAST);
424 cmd->sjw = PUCAN_TFAST_SJW(bt->sjw - 1);
425 cmd->tseg2 = PUCAN_TFAST_TSEG2(bt->phase_seg2 - 1);
426 cmd->tseg1 = PUCAN_TFAST_TSEG1(bt->prop_seg + bt->phase_seg1 - 1);
427 cmd->brp = cpu_to_le16(PUCAN_TFAST_BRP(bt->brp - 1));
429 /* send the command */
430 return pcan_usb_fd_send_cmd(dev, ++cmd);
433 /* handle restart but in asynchronously way
434 * (uses PCAN-USB Pro code to complete asynchronous request)
436 static int pcan_usb_fd_restart_async(struct peak_usb_device *dev,
437 struct urb *urb, u8 *buf)
441 /* build the entire cmds list in the provided buffer, to go back into
444 pc += pcan_usb_fd_build_restart_cmd(dev, pc);
447 memset(pc, 0xff, sizeof(struct pucan_command));
448 pc += sizeof(struct pucan_command);
450 /* complete the URB */
451 usb_fill_bulk_urb(urb, dev->udev,
452 usb_sndbulkpipe(dev->udev, PCAN_USBPRO_EP_CMDOUT),
454 pcan_usb_pro_restart_complete, dev);
457 return usb_submit_urb(urb, GFP_ATOMIC);
460 static int pcan_usb_fd_drv_loaded(struct peak_usb_device *dev, bool loaded)
462 struct pcan_usb_fd_device *pdev =
463 container_of(dev, struct pcan_usb_fd_device, dev);
465 pdev->cmd_buffer_addr[0] = 0;
466 pdev->cmd_buffer_addr[1] = !!loaded;
468 return pcan_usb_pro_send_req(dev,
470 PCAN_USBPRO_FCT_DRVLD,
471 pdev->cmd_buffer_addr,
472 PCAN_USBPRO_FCT_DRVLD_REQ_LEN);
475 static int pcan_usb_fd_decode_canmsg(struct pcan_usb_fd_if *usb_if,
476 struct pucan_msg *rx_msg)
478 struct pucan_rx_msg *rm = (struct pucan_rx_msg *)rx_msg;
479 struct peak_usb_device *dev;
480 struct net_device *netdev;
481 struct canfd_frame *cfd;
483 const u16 rx_msg_flags = le16_to_cpu(rm->flags);
485 if (pucan_msg_get_channel(rm) >= ARRAY_SIZE(usb_if->dev))
488 dev = usb_if->dev[pucan_msg_get_channel(rm)];
489 netdev = dev->netdev;
491 if (rx_msg_flags & PUCAN_MSG_EXT_DATA_LEN) {
492 /* CANFD frame case */
493 skb = alloc_canfd_skb(netdev, &cfd);
497 if (rx_msg_flags & PUCAN_MSG_BITRATE_SWITCH)
498 cfd->flags |= CANFD_BRS;
500 if (rx_msg_flags & PUCAN_MSG_ERROR_STATE_IND)
501 cfd->flags |= CANFD_ESI;
503 cfd->len = can_dlc2len(get_canfd_dlc(pucan_msg_get_dlc(rm)));
505 /* CAN 2.0 frame case */
506 skb = alloc_can_skb(netdev, (struct can_frame **)&cfd);
510 cfd->len = get_can_dlc(pucan_msg_get_dlc(rm));
513 cfd->can_id = le32_to_cpu(rm->can_id);
515 if (rx_msg_flags & PUCAN_MSG_EXT_ID)
516 cfd->can_id |= CAN_EFF_FLAG;
518 if (rx_msg_flags & PUCAN_MSG_RTR)
519 cfd->can_id |= CAN_RTR_FLAG;
521 memcpy(cfd->data, rm->d, cfd->len);
523 netdev->stats.rx_packets++;
524 netdev->stats.rx_bytes += cfd->len;
526 peak_usb_netif_rx(skb, &usb_if->time_ref, le32_to_cpu(rm->ts_low));
531 /* handle uCAN status message */
532 static int pcan_usb_fd_decode_status(struct pcan_usb_fd_if *usb_if,
533 struct pucan_msg *rx_msg)
535 struct pucan_status_msg *sm = (struct pucan_status_msg *)rx_msg;
536 struct pcan_usb_fd_device *pdev;
537 enum can_state new_state = CAN_STATE_ERROR_ACTIVE;
538 enum can_state rx_state, tx_state;
539 struct peak_usb_device *dev;
540 struct net_device *netdev;
541 struct can_frame *cf;
544 if (pucan_stmsg_get_channel(sm) >= ARRAY_SIZE(usb_if->dev))
547 dev = usb_if->dev[pucan_stmsg_get_channel(sm)];
548 pdev = container_of(dev, struct pcan_usb_fd_device, dev);
549 netdev = dev->netdev;
551 /* nothing should be sent while in BUS_OFF state */
552 if (dev->can.state == CAN_STATE_BUS_OFF)
555 if (sm->channel_p_w_b & PUCAN_BUS_BUSOFF) {
556 new_state = CAN_STATE_BUS_OFF;
557 } else if (sm->channel_p_w_b & PUCAN_BUS_PASSIVE) {
558 new_state = CAN_STATE_ERROR_PASSIVE;
559 } else if (sm->channel_p_w_b & PUCAN_BUS_WARNING) {
560 new_state = CAN_STATE_ERROR_WARNING;
562 /* back to (or still in) ERROR_ACTIVE state */
563 new_state = CAN_STATE_ERROR_ACTIVE;
568 /* state hasn't changed */
569 if (new_state == dev->can.state)
572 /* handle bus state change */
573 tx_state = (pdev->bec.txerr >= pdev->bec.rxerr) ? new_state : 0;
574 rx_state = (pdev->bec.txerr <= pdev->bec.rxerr) ? new_state : 0;
576 /* allocate an skb to store the error frame */
577 skb = alloc_can_err_skb(netdev, &cf);
579 can_change_state(netdev, cf, tx_state, rx_state);
581 /* things must be done even in case of OOM */
582 if (new_state == CAN_STATE_BUS_OFF)
588 netdev->stats.rx_packets++;
589 netdev->stats.rx_bytes += cf->can_dlc;
591 peak_usb_netif_rx(skb, &usb_if->time_ref, le32_to_cpu(sm->ts_low));
596 /* handle uCAN error message */
597 static int pcan_usb_fd_decode_error(struct pcan_usb_fd_if *usb_if,
598 struct pucan_msg *rx_msg)
600 struct pucan_error_msg *er = (struct pucan_error_msg *)rx_msg;
601 struct pcan_usb_fd_device *pdev;
602 struct peak_usb_device *dev;
604 if (pucan_ermsg_get_channel(er) >= ARRAY_SIZE(usb_if->dev))
607 dev = usb_if->dev[pucan_ermsg_get_channel(er)];
608 pdev = container_of(dev, struct pcan_usb_fd_device, dev);
610 /* keep a trace of tx and rx error counters for later use */
611 pdev->bec.txerr = er->tx_err_cnt;
612 pdev->bec.rxerr = er->rx_err_cnt;
617 /* handle uCAN overrun message */
618 static int pcan_usb_fd_decode_overrun(struct pcan_usb_fd_if *usb_if,
619 struct pucan_msg *rx_msg)
621 struct pcan_ufd_ovr_msg *ov = (struct pcan_ufd_ovr_msg *)rx_msg;
622 struct peak_usb_device *dev;
623 struct net_device *netdev;
624 struct can_frame *cf;
627 if (pufd_omsg_get_channel(ov) >= ARRAY_SIZE(usb_if->dev))
630 dev = usb_if->dev[pufd_omsg_get_channel(ov)];
631 netdev = dev->netdev;
633 /* allocate an skb to store the error frame */
634 skb = alloc_can_err_skb(netdev, &cf);
638 cf->can_id |= CAN_ERR_CRTL;
639 cf->data[1] |= CAN_ERR_CRTL_RX_OVERFLOW;
641 peak_usb_netif_rx(skb, &usb_if->time_ref, le32_to_cpu(ov->ts_low));
643 netdev->stats.rx_over_errors++;
644 netdev->stats.rx_errors++;
649 /* handle USB calibration message */
650 static void pcan_usb_fd_decode_ts(struct pcan_usb_fd_if *usb_if,
651 struct pucan_msg *rx_msg)
653 struct pcan_ufd_ts_msg *ts = (struct pcan_ufd_ts_msg *)rx_msg;
655 /* should wait until clock is stabilized */
656 if (usb_if->cm_ignore_count > 0)
657 usb_if->cm_ignore_count--;
659 peak_usb_set_ts_now(&usb_if->time_ref, le32_to_cpu(ts->ts_low));
662 /* callback for bulk IN urb */
663 static int pcan_usb_fd_decode_buf(struct peak_usb_device *dev, struct urb *urb)
665 struct pcan_usb_fd_if *usb_if = pcan_usb_fd_dev_if(dev);
666 struct net_device *netdev = dev->netdev;
667 struct pucan_msg *rx_msg;
668 u8 *msg_ptr, *msg_end;
671 /* loop reading all the records from the incoming message */
672 msg_ptr = urb->transfer_buffer;
673 msg_end = urb->transfer_buffer + urb->actual_length;
674 for (; msg_ptr < msg_end;) {
675 u16 rx_msg_type, rx_msg_size;
677 rx_msg = (struct pucan_msg *)msg_ptr;
679 /* null packet found: end of list */
683 rx_msg_size = le16_to_cpu(rx_msg->size);
684 rx_msg_type = le16_to_cpu(rx_msg->type);
686 /* check if the record goes out of current packet */
687 if (msg_ptr + rx_msg_size > msg_end) {
689 "got frag rec: should inc usb rx buf sze\n");
694 switch (rx_msg_type) {
695 case PUCAN_MSG_CAN_RX:
696 err = pcan_usb_fd_decode_canmsg(usb_if, rx_msg);
701 case PCAN_UFD_MSG_CALIBRATION:
702 pcan_usb_fd_decode_ts(usb_if, rx_msg);
705 case PUCAN_MSG_ERROR:
706 err = pcan_usb_fd_decode_error(usb_if, rx_msg);
711 case PUCAN_MSG_STATUS:
712 err = pcan_usb_fd_decode_status(usb_if, rx_msg);
717 case PCAN_UFD_MSG_OVERRUN:
718 err = pcan_usb_fd_decode_overrun(usb_if, rx_msg);
725 "unhandled msg type 0x%02x (%d): ignored\n",
726 rx_msg_type, rx_msg_type);
730 msg_ptr += rx_msg_size;
735 pcan_dump_mem("received msg",
736 urb->transfer_buffer, urb->actual_length);
740 /* CAN/CANFD frames encoding callback */
741 static int pcan_usb_fd_encode_msg(struct peak_usb_device *dev,
742 struct sk_buff *skb, u8 *obuf, size_t *size)
744 struct pucan_tx_msg *tx_msg = (struct pucan_tx_msg *)obuf;
745 struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
746 u16 tx_msg_size, tx_msg_flags;
749 if (cfd->len > CANFD_MAX_DLEN)
752 tx_msg_size = ALIGN(sizeof(struct pucan_tx_msg) + cfd->len, 4);
753 tx_msg->size = cpu_to_le16(tx_msg_size);
754 tx_msg->type = cpu_to_le16(PUCAN_MSG_CAN_TX);
757 if (cfd->can_id & CAN_EFF_FLAG) {
758 tx_msg_flags |= PUCAN_MSG_EXT_ID;
759 tx_msg->can_id = cpu_to_le32(cfd->can_id & CAN_EFF_MASK);
761 tx_msg->can_id = cpu_to_le32(cfd->can_id & CAN_SFF_MASK);
764 if (can_is_canfd_skb(skb)) {
765 /* considering a CANFD frame */
766 can_dlc = can_len2dlc(cfd->len);
768 tx_msg_flags |= PUCAN_MSG_EXT_DATA_LEN;
770 if (cfd->flags & CANFD_BRS)
771 tx_msg_flags |= PUCAN_MSG_BITRATE_SWITCH;
773 if (cfd->flags & CANFD_ESI)
774 tx_msg_flags |= PUCAN_MSG_ERROR_STATE_IND;
776 /* CAND 2.0 frames */
779 if (cfd->can_id & CAN_RTR_FLAG)
780 tx_msg_flags |= PUCAN_MSG_RTR;
783 tx_msg->flags = cpu_to_le16(tx_msg_flags);
784 tx_msg->channel_dlc = PUCAN_MSG_CHANNEL_DLC(dev->ctrl_idx, can_dlc);
785 memcpy(tx_msg->d, cfd->data, cfd->len);
787 /* add null size message to tag the end (messages are 32-bits aligned)
789 tx_msg = (struct pucan_tx_msg *)(obuf + tx_msg_size);
793 /* set the whole size of the USB packet to send */
794 *size = tx_msg_size + sizeof(u32);
799 /* start the interface (last chance before set bus on) */
800 static int pcan_usb_fd_start(struct peak_usb_device *dev)
802 struct pcan_usb_fd_device *pdev =
803 container_of(dev, struct pcan_usb_fd_device, dev);
806 /* set filter mode: all acceptance */
807 err = pcan_usb_fd_set_filter_std(dev, -1, 0xffffffff);
811 /* opening first device: */
812 if (pdev->usb_if->dev_opened_count == 0) {
814 peak_usb_init_time_ref(&pdev->usb_if->time_ref,
817 /* enable USB calibration messages */
818 err = pcan_usb_fd_set_options(dev, 1,
820 PCAN_UFD_FLTEXT_CALIBRATION);
823 pdev->usb_if->dev_opened_count++;
825 /* reset cached error counters */
832 /* socket callback used to copy berr counters values receieved through USB */
833 static int pcan_usb_fd_get_berr_counter(const struct net_device *netdev,
834 struct can_berr_counter *bec)
836 struct peak_usb_device *dev = netdev_priv(netdev);
837 struct pcan_usb_fd_device *pdev =
838 container_of(dev, struct pcan_usb_fd_device, dev);
846 /* stop interface (last chance before set bus off) */
847 static int pcan_usb_fd_stop(struct peak_usb_device *dev)
849 struct pcan_usb_fd_device *pdev =
850 container_of(dev, struct pcan_usb_fd_device, dev);
852 /* turn off special msgs for that interface if no other dev opened */
853 if (pdev->usb_if->dev_opened_count == 1)
854 pcan_usb_fd_set_options(dev, 0,
856 PCAN_UFD_FLTEXT_CALIBRATION);
857 pdev->usb_if->dev_opened_count--;
862 /* called when probing, to initialize a device object */
863 static int pcan_usb_fd_init(struct peak_usb_device *dev)
865 struct pcan_usb_fd_device *pdev =
866 container_of(dev, struct pcan_usb_fd_device, dev);
867 int i, err = -ENOMEM;
869 /* do this for 1st channel only */
870 if (!dev->prev_siblings) {
871 /* allocate netdevices common structure attached to first one */
872 pdev->usb_if = kzalloc(sizeof(*pdev->usb_if), GFP_KERNEL);
876 /* allocate command buffer once for all for the interface */
877 pdev->cmd_buffer_addr = kzalloc(PCAN_UFD_CMD_BUFFER_SIZE,
879 if (!pdev->cmd_buffer_addr)
882 /* number of ts msgs to ignore before taking one into account */
883 pdev->usb_if->cm_ignore_count = 5;
885 err = pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_INFO,
887 &pdev->usb_if->fw_info,
888 sizeof(pdev->usb_if->fw_info));
890 dev_err(dev->netdev->dev.parent,
891 "unable to read %s firmware info (err %d)\n",
892 dev->adapter->name, err);
896 /* explicit use of dev_xxx() instead of netdev_xxx() here:
897 * information displayed are related to the device itself, not
898 * to the canx (channel) device.
900 dev_info(dev->netdev->dev.parent,
901 "PEAK-System %s v%u fw v%u.%u.%u (%u channels)\n",
902 dev->adapter->name, pdev->usb_if->fw_info.hw_version,
903 pdev->usb_if->fw_info.fw_version[0],
904 pdev->usb_if->fw_info.fw_version[1],
905 pdev->usb_if->fw_info.fw_version[2],
906 dev->adapter->ctrl_count);
908 /* check for ability to switch between ISO/non-ISO modes */
909 if (pdev->usb_if->fw_info.fw_version[0] >= 2) {
910 /* firmware >= 2.x supports ISO/non-ISO switching */
911 dev->can.ctrlmode_supported |= CAN_CTRLMODE_FD_NON_ISO;
913 /* firmware < 2.x only supports fixed(!) non-ISO */
914 dev->can.ctrlmode |= CAN_CTRLMODE_FD_NON_ISO;
917 /* tell the hardware the can driver is running */
918 err = pcan_usb_fd_drv_loaded(dev, 1);
920 dev_err(dev->netdev->dev.parent,
921 "unable to tell %s driver is loaded (err %d)\n",
922 dev->adapter->name, err);
926 /* otherwise, simply copy previous sibling's values */
927 struct pcan_usb_fd_device *ppdev =
928 container_of(dev->prev_siblings,
929 struct pcan_usb_fd_device, dev);
931 pdev->usb_if = ppdev->usb_if;
932 pdev->cmd_buffer_addr = ppdev->cmd_buffer_addr;
934 /* do a copy of the ctrlmode[_supported] too */
935 dev->can.ctrlmode = ppdev->dev.can.ctrlmode;
936 dev->can.ctrlmode_supported = ppdev->dev.can.ctrlmode_supported;
939 pdev->usb_if->dev[dev->ctrl_idx] = dev;
941 le32_to_cpu(pdev->usb_if->fw_info.dev_id[dev->ctrl_idx]);
943 /* set clock domain */
944 for (i = 0; i < ARRAY_SIZE(pcan_usb_fd_clk_freq); i++)
945 if (dev->adapter->clock.freq == pcan_usb_fd_clk_freq[i])
948 if (i >= ARRAY_SIZE(pcan_usb_fd_clk_freq)) {
949 dev_warn(dev->netdev->dev.parent,
950 "incompatible clock frequencies\n");
955 pcan_usb_fd_set_clock_domain(dev, i);
957 /* set LED in default state (end of init phase) */
958 pcan_usb_fd_set_can_led(dev, PCAN_UFD_LED_DEF);
963 kfree(pdev->cmd_buffer_addr);
970 /* called when driver module is being unloaded */
971 static void pcan_usb_fd_exit(struct peak_usb_device *dev)
973 struct pcan_usb_fd_device *pdev =
974 container_of(dev, struct pcan_usb_fd_device, dev);
976 /* when rmmod called before unplug and if down, should reset things
979 if (dev->can.state != CAN_STATE_STOPPED) {
980 /* set bus off on the corresponding channel */
981 pcan_usb_fd_set_bus(dev, 0);
984 /* switch off corresponding CAN LEDs */
985 pcan_usb_fd_set_can_led(dev, PCAN_UFD_LED_OFF);
987 /* if channel #0 (only) */
988 if (dev->ctrl_idx == 0) {
989 /* turn off calibration message if any device were opened */
990 if (pdev->usb_if->dev_opened_count > 0)
991 pcan_usb_fd_set_options(dev, 0,
993 PCAN_UFD_FLTEXT_CALIBRATION);
995 /* tell USB adapter that the driver is being unloaded */
996 pcan_usb_fd_drv_loaded(dev, 0);
1000 /* called when the USB adapter is unplugged */
1001 static void pcan_usb_fd_free(struct peak_usb_device *dev)
1003 /* last device: can free shared objects now */
1004 if (!dev->prev_siblings && !dev->next_siblings) {
1005 struct pcan_usb_fd_device *pdev =
1006 container_of(dev, struct pcan_usb_fd_device, dev);
1008 /* free commands buffer */
1009 kfree(pdev->cmd_buffer_addr);
1011 /* free usb interface object */
1012 kfree(pdev->usb_if);
1016 /* describes the PCAN-USB FD adapter */
1017 static const struct can_bittiming_const pcan_usb_fd_const = {
1018 .name = "pcan_usb_fd",
1020 .tseg1_max = (1 << PUCAN_TSLOW_TSGEG1_BITS),
1022 .tseg2_max = (1 << PUCAN_TSLOW_TSGEG2_BITS),
1023 .sjw_max = (1 << PUCAN_TSLOW_SJW_BITS),
1025 .brp_max = (1 << PUCAN_TSLOW_BRP_BITS),
1029 static const struct can_bittiming_const pcan_usb_fd_data_const = {
1030 .name = "pcan_usb_fd",
1032 .tseg1_max = (1 << PUCAN_TFAST_TSGEG1_BITS),
1034 .tseg2_max = (1 << PUCAN_TFAST_TSGEG2_BITS),
1035 .sjw_max = (1 << PUCAN_TFAST_SJW_BITS),
1037 .brp_max = (1 << PUCAN_TFAST_BRP_BITS),
1041 const struct peak_usb_adapter pcan_usb_fd = {
1042 .name = "PCAN-USB FD",
1043 .device_id = PCAN_USBFD_PRODUCT_ID,
1044 .ctrl_count = PCAN_USBFD_CHANNEL_COUNT,
1045 .ctrlmode_supported = CAN_CTRLMODE_FD |
1046 CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY,
1048 .freq = PCAN_UFD_CRYSTAL_HZ,
1050 .bittiming_const = &pcan_usb_fd_const,
1051 .data_bittiming_const = &pcan_usb_fd_data_const,
1053 /* size of device private data */
1054 .sizeof_dev_private = sizeof(struct pcan_usb_fd_device),
1056 /* timestamps usage */
1058 .ts_period = 1000000, /* calibration period in ts. */
1059 .us_per_ts_scale = 1, /* us = (ts * scale) >> shift */
1060 .us_per_ts_shift = 0,
1062 /* give here messages in/out endpoints */
1063 .ep_msg_in = PCAN_USBPRO_EP_MSGIN,
1064 .ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0},
1066 /* size of rx/tx usb buffers */
1067 .rx_buffer_size = PCAN_UFD_RX_BUFFER_SIZE,
1068 .tx_buffer_size = PCAN_UFD_TX_BUFFER_SIZE,
1070 /* device callbacks */
1071 .intf_probe = pcan_usb_pro_probe, /* same as PCAN-USB Pro */
1072 .dev_init = pcan_usb_fd_init,
1074 .dev_exit = pcan_usb_fd_exit,
1075 .dev_free = pcan_usb_fd_free,
1076 .dev_set_bus = pcan_usb_fd_set_bus,
1077 .dev_set_bittiming = pcan_usb_fd_set_bittiming_slow,
1078 .dev_set_data_bittiming = pcan_usb_fd_set_bittiming_fast,
1079 .dev_decode_buf = pcan_usb_fd_decode_buf,
1080 .dev_start = pcan_usb_fd_start,
1081 .dev_stop = pcan_usb_fd_stop,
1082 .dev_restart_async = pcan_usb_fd_restart_async,
1083 .dev_encode_msg = pcan_usb_fd_encode_msg,
1085 .do_get_berr_counter = pcan_usb_fd_get_berr_counter,
1088 /* describes the PCAN-CHIP USB */
1089 static const struct can_bittiming_const pcan_usb_chip_const = {
1090 .name = "pcan_chip_usb",
1092 .tseg1_max = (1 << PUCAN_TSLOW_TSGEG1_BITS),
1094 .tseg2_max = (1 << PUCAN_TSLOW_TSGEG2_BITS),
1095 .sjw_max = (1 << PUCAN_TSLOW_SJW_BITS),
1097 .brp_max = (1 << PUCAN_TSLOW_BRP_BITS),
1101 static const struct can_bittiming_const pcan_usb_chip_data_const = {
1102 .name = "pcan_chip_usb",
1104 .tseg1_max = (1 << PUCAN_TFAST_TSGEG1_BITS),
1106 .tseg2_max = (1 << PUCAN_TFAST_TSGEG2_BITS),
1107 .sjw_max = (1 << PUCAN_TFAST_SJW_BITS),
1109 .brp_max = (1 << PUCAN_TFAST_BRP_BITS),
1113 const struct peak_usb_adapter pcan_usb_chip = {
1114 .name = "PCAN-Chip USB",
1115 .device_id = PCAN_USBCHIP_PRODUCT_ID,
1116 .ctrl_count = PCAN_USBFD_CHANNEL_COUNT,
1117 .ctrlmode_supported = CAN_CTRLMODE_FD |
1118 CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY,
1120 .freq = PCAN_UFD_CRYSTAL_HZ,
1122 .bittiming_const = &pcan_usb_chip_const,
1123 .data_bittiming_const = &pcan_usb_chip_data_const,
1125 /* size of device private data */
1126 .sizeof_dev_private = sizeof(struct pcan_usb_fd_device),
1128 /* timestamps usage */
1130 .ts_period = 1000000, /* calibration period in ts. */
1131 .us_per_ts_scale = 1, /* us = (ts * scale) >> shift */
1132 .us_per_ts_shift = 0,
1134 /* give here messages in/out endpoints */
1135 .ep_msg_in = PCAN_USBPRO_EP_MSGIN,
1136 .ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0},
1138 /* size of rx/tx usb buffers */
1139 .rx_buffer_size = PCAN_UFD_RX_BUFFER_SIZE,
1140 .tx_buffer_size = PCAN_UFD_TX_BUFFER_SIZE,
1142 /* device callbacks */
1143 .intf_probe = pcan_usb_pro_probe, /* same as PCAN-USB Pro */
1144 .dev_init = pcan_usb_fd_init,
1146 .dev_exit = pcan_usb_fd_exit,
1147 .dev_free = pcan_usb_fd_free,
1148 .dev_set_bus = pcan_usb_fd_set_bus,
1149 .dev_set_bittiming = pcan_usb_fd_set_bittiming_slow,
1150 .dev_set_data_bittiming = pcan_usb_fd_set_bittiming_fast,
1151 .dev_decode_buf = pcan_usb_fd_decode_buf,
1152 .dev_start = pcan_usb_fd_start,
1153 .dev_stop = pcan_usb_fd_stop,
1154 .dev_restart_async = pcan_usb_fd_restart_async,
1155 .dev_encode_msg = pcan_usb_fd_encode_msg,
1157 .do_get_berr_counter = pcan_usb_fd_get_berr_counter,
1160 /* describes the PCAN-USB Pro FD adapter */
1161 static const struct can_bittiming_const pcan_usb_pro_fd_const = {
1162 .name = "pcan_usb_pro_fd",
1164 .tseg1_max = (1 << PUCAN_TSLOW_TSGEG1_BITS),
1166 .tseg2_max = (1 << PUCAN_TSLOW_TSGEG2_BITS),
1167 .sjw_max = (1 << PUCAN_TSLOW_SJW_BITS),
1169 .brp_max = (1 << PUCAN_TSLOW_BRP_BITS),
1173 static const struct can_bittiming_const pcan_usb_pro_fd_data_const = {
1174 .name = "pcan_usb_pro_fd",
1176 .tseg1_max = (1 << PUCAN_TFAST_TSGEG1_BITS),
1178 .tseg2_max = (1 << PUCAN_TFAST_TSGEG2_BITS),
1179 .sjw_max = (1 << PUCAN_TFAST_SJW_BITS),
1181 .brp_max = (1 << PUCAN_TFAST_BRP_BITS),
1185 const struct peak_usb_adapter pcan_usb_pro_fd = {
1186 .name = "PCAN-USB Pro FD",
1187 .device_id = PCAN_USBPROFD_PRODUCT_ID,
1188 .ctrl_count = PCAN_USBPROFD_CHANNEL_COUNT,
1189 .ctrlmode_supported = CAN_CTRLMODE_FD |
1190 CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY,
1192 .freq = PCAN_UFD_CRYSTAL_HZ,
1194 .bittiming_const = &pcan_usb_pro_fd_const,
1195 .data_bittiming_const = &pcan_usb_pro_fd_data_const,
1197 /* size of device private data */
1198 .sizeof_dev_private = sizeof(struct pcan_usb_fd_device),
1200 /* timestamps usage */
1202 .ts_period = 1000000, /* calibration period in ts. */
1203 .us_per_ts_scale = 1, /* us = (ts * scale) >> shift */
1204 .us_per_ts_shift = 0,
1206 /* give here messages in/out endpoints */
1207 .ep_msg_in = PCAN_USBPRO_EP_MSGIN,
1208 .ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0, PCAN_USBPRO_EP_MSGOUT_1},
1210 /* size of rx/tx usb buffers */
1211 .rx_buffer_size = PCAN_UFD_RX_BUFFER_SIZE,
1212 .tx_buffer_size = PCAN_UFD_TX_BUFFER_SIZE,
1214 /* device callbacks */
1215 .intf_probe = pcan_usb_pro_probe, /* same as PCAN-USB Pro */
1216 .dev_init = pcan_usb_fd_init,
1218 .dev_exit = pcan_usb_fd_exit,
1219 .dev_free = pcan_usb_fd_free,
1220 .dev_set_bus = pcan_usb_fd_set_bus,
1221 .dev_set_bittiming = pcan_usb_fd_set_bittiming_slow,
1222 .dev_set_data_bittiming = pcan_usb_fd_set_bittiming_fast,
1223 .dev_decode_buf = pcan_usb_fd_decode_buf,
1224 .dev_start = pcan_usb_fd_start,
1225 .dev_stop = pcan_usb_fd_stop,
1226 .dev_restart_async = pcan_usb_fd_restart_async,
1227 .dev_encode_msg = pcan_usb_fd_encode_msg,
1229 .do_get_berr_counter = pcan_usb_fd_get_berr_counter,
1232 /* describes the PCAN-USB X6 adapter */
1233 static const struct can_bittiming_const pcan_usb_x6_const = {
1234 .name = "pcan_usb_x6",
1236 .tseg1_max = (1 << PUCAN_TSLOW_TSGEG1_BITS),
1238 .tseg2_max = (1 << PUCAN_TSLOW_TSGEG2_BITS),
1239 .sjw_max = (1 << PUCAN_TSLOW_SJW_BITS),
1241 .brp_max = (1 << PUCAN_TSLOW_BRP_BITS),
1245 static const struct can_bittiming_const pcan_usb_x6_data_const = {
1246 .name = "pcan_usb_x6",
1248 .tseg1_max = (1 << PUCAN_TFAST_TSGEG1_BITS),
1250 .tseg2_max = (1 << PUCAN_TFAST_TSGEG2_BITS),
1251 .sjw_max = (1 << PUCAN_TFAST_SJW_BITS),
1253 .brp_max = (1 << PUCAN_TFAST_BRP_BITS),
1257 const struct peak_usb_adapter pcan_usb_x6 = {
1258 .name = "PCAN-USB X6",
1259 .device_id = PCAN_USBX6_PRODUCT_ID,
1260 .ctrl_count = PCAN_USBPROFD_CHANNEL_COUNT,
1261 .ctrlmode_supported = CAN_CTRLMODE_FD |
1262 CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY,
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 */
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,