1 // SPDX-License-Identifier: GPL-2.0-only
3 * CAN driver for PEAK System PCAN-USB adapter
4 * Derived from the PCAN project file driver/src/pcan_usb.c
6 * Copyright (C) 2003-2010 PEAK System-Technik GmbH
11 #include <linux/unaligned.h>
13 #include <linux/ethtool.h>
14 #include <linux/module.h>
15 #include <linux/netdevice.h>
16 #include <linux/usb.h>
18 #include <linux/can.h>
19 #include <linux/can/dev.h>
20 #include <linux/can/error.h>
22 #include "pcan_usb_core.h"
24 /* PCAN-USB Endpoints */
25 #define PCAN_USB_EP_CMDOUT 1
26 #define PCAN_USB_EP_CMDIN (PCAN_USB_EP_CMDOUT | USB_DIR_IN)
27 #define PCAN_USB_EP_MSGOUT 2
28 #define PCAN_USB_EP_MSGIN (PCAN_USB_EP_MSGOUT | USB_DIR_IN)
30 /* PCAN-USB command struct */
31 #define PCAN_USB_CMD_FUNC 0
32 #define PCAN_USB_CMD_NUM 1
33 #define PCAN_USB_CMD_ARGS 2
34 #define PCAN_USB_CMD_ARGS_LEN 14
35 #define PCAN_USB_CMD_LEN (PCAN_USB_CMD_ARGS + \
36 PCAN_USB_CMD_ARGS_LEN)
38 /* PCAN-USB commands */
39 #define PCAN_USB_CMD_BITRATE 1
40 #define PCAN_USB_CMD_SET_BUS 3
41 #define PCAN_USB_CMD_DEVID 4
42 #define PCAN_USB_CMD_SN 6
43 #define PCAN_USB_CMD_REGISTER 9
44 #define PCAN_USB_CMD_EXT_VCC 10
45 #define PCAN_USB_CMD_ERR_FR 11
46 #define PCAN_USB_CMD_LED 12
48 /* PCAN_USB_CMD_SET_BUS number arg */
49 #define PCAN_USB_BUS_XCVER 2
50 #define PCAN_USB_BUS_SILENT_MODE 3
52 /* PCAN_USB_CMD_xxx functions */
53 #define PCAN_USB_GET 1
54 #define PCAN_USB_SET 2
56 /* PCAN-USB command timeout (ms.) */
57 #define PCAN_USB_COMMAND_TIMEOUT 1000
59 /* PCAN-USB startup timeout (ms.) */
60 #define PCAN_USB_STARTUP_TIMEOUT 10
62 /* PCAN-USB rx/tx buffers size */
63 #define PCAN_USB_RX_BUFFER_SIZE 64
64 #define PCAN_USB_TX_BUFFER_SIZE 64
66 #define PCAN_USB_MSG_HEADER_LEN 2
68 #define PCAN_USB_MSG_TX_CAN 2 /* Tx msg is a CAN frame */
70 /* PCAN-USB adapter internal clock (MHz) */
71 #define PCAN_USB_CRYSTAL_HZ 16000000
73 /* PCAN-USB USB message record status/len field */
74 #define PCAN_USB_STATUSLEN_TIMESTAMP (1 << 7)
75 #define PCAN_USB_STATUSLEN_INTERNAL (1 << 6)
76 #define PCAN_USB_STATUSLEN_EXT_ID (1 << 5)
77 #define PCAN_USB_STATUSLEN_RTR (1 << 4)
78 #define PCAN_USB_STATUSLEN_DLC (0xf)
80 /* PCAN-USB 4.1 CAN Id tx extended flags */
81 #define PCAN_USB_TX_SRR 0x01 /* SJA1000 SRR command */
82 #define PCAN_USB_TX_AT 0x02 /* SJA1000 AT command */
84 /* PCAN-USB error flags */
85 #define PCAN_USB_ERROR_TXFULL 0x01
86 #define PCAN_USB_ERROR_RXQOVR 0x02
87 #define PCAN_USB_ERROR_BUS_LIGHT 0x04
88 #define PCAN_USB_ERROR_BUS_HEAVY 0x08
89 #define PCAN_USB_ERROR_BUS_OFF 0x10
90 #define PCAN_USB_ERROR_RXQEMPTY 0x20
91 #define PCAN_USB_ERROR_QOVR 0x40
92 #define PCAN_USB_ERROR_TXQFULL 0x80
94 #define PCAN_USB_ERROR_BUS (PCAN_USB_ERROR_BUS_LIGHT | \
95 PCAN_USB_ERROR_BUS_HEAVY | \
96 PCAN_USB_ERROR_BUS_OFF)
99 #define SJA1000_MODE_NORMAL 0x00
100 #define SJA1000_MODE_INIT 0x01
103 * tick duration = 42.666 us =>
104 * (tick_number * 44739243) >> 20 ~ (tick_number * 42666) / 1000
107 #define PCAN_USB_TS_DIV_SHIFTER 20
108 #define PCAN_USB_TS_US_PER_TICK 44739243
110 /* PCAN-USB messages record types */
111 #define PCAN_USB_REC_ERROR 1
112 #define PCAN_USB_REC_ANALOG 2
113 #define PCAN_USB_REC_BUSLOAD 3
114 #define PCAN_USB_REC_TS 4
115 #define PCAN_USB_REC_BUSEVT 5
117 /* CAN bus events notifications selection mask */
118 #define PCAN_USB_ERR_RXERR 0x02 /* ask for rxerr counter */
119 #define PCAN_USB_ERR_TXERR 0x04 /* ask for txerr counter */
121 /* This mask generates an usb packet each time the state of the bus changes.
122 * In other words, its interest is to know which side among rx and tx is
123 * responsible of the change of the bus state.
125 #define PCAN_USB_BERR_MASK (PCAN_USB_ERR_RXERR | PCAN_USB_ERR_TXERR)
127 /* identify bus event packets with rx/tx error counters */
128 #define PCAN_USB_ERR_CNT_DEC 0x00 /* counters are decreasing */
129 #define PCAN_USB_ERR_CNT_INC 0x80 /* counters are increasing */
131 /* private to PCAN-USB adapter */
133 struct peak_usb_device dev;
134 struct peak_time_ref time_ref;
135 struct timer_list restart_timer;
136 struct can_berr_counter bec;
139 /* incoming message context for decoding */
140 struct pcan_usb_msg_context {
148 struct net_device *netdev;
149 struct pcan_usb *pdev;
155 static int pcan_usb_send_cmd(struct peak_usb_device *dev, u8 f, u8 n, u8 *p)
160 /* usb device unregistered? */
161 if (!(dev->state & PCAN_USB_STATE_CONNECTED))
164 dev->cmd_buf[PCAN_USB_CMD_FUNC] = f;
165 dev->cmd_buf[PCAN_USB_CMD_NUM] = n;
168 memcpy(dev->cmd_buf + PCAN_USB_CMD_ARGS,
169 p, PCAN_USB_CMD_ARGS_LEN);
171 err = usb_bulk_msg(dev->udev,
172 usb_sndbulkpipe(dev->udev, PCAN_USB_EP_CMDOUT),
173 dev->cmd_buf, PCAN_USB_CMD_LEN, &actual_length,
174 PCAN_USB_COMMAND_TIMEOUT);
176 netdev_err(dev->netdev,
177 "sending cmd f=0x%x n=0x%x failure: %d\n",
183 * send a command then wait for its response
185 static int pcan_usb_wait_rsp(struct peak_usb_device *dev, u8 f, u8 n, u8 *p)
190 /* usb device unregistered? */
191 if (!(dev->state & PCAN_USB_STATE_CONNECTED))
194 /* first, send command */
195 err = pcan_usb_send_cmd(dev, f, n, NULL);
199 err = usb_bulk_msg(dev->udev,
200 usb_rcvbulkpipe(dev->udev, PCAN_USB_EP_CMDIN),
201 dev->cmd_buf, PCAN_USB_CMD_LEN, &actual_length,
202 PCAN_USB_COMMAND_TIMEOUT);
204 netdev_err(dev->netdev,
205 "waiting rsp f=0x%x n=0x%x failure: %d\n", f, n, err);
207 memcpy(p, dev->cmd_buf + PCAN_USB_CMD_ARGS,
208 PCAN_USB_CMD_ARGS_LEN);
213 static int pcan_usb_set_sja1000(struct peak_usb_device *dev, u8 mode)
215 u8 args[PCAN_USB_CMD_ARGS_LEN] = {
219 return pcan_usb_send_cmd(dev, PCAN_USB_CMD_REGISTER, PCAN_USB_SET,
223 static int pcan_usb_set_bus(struct peak_usb_device *dev, u8 onoff)
225 u8 args[PCAN_USB_CMD_ARGS_LEN] = {
229 return pcan_usb_send_cmd(dev, PCAN_USB_CMD_SET_BUS, PCAN_USB_BUS_XCVER,
233 static int pcan_usb_set_silent(struct peak_usb_device *dev, u8 onoff)
235 u8 args[PCAN_USB_CMD_ARGS_LEN] = {
239 return pcan_usb_send_cmd(dev, PCAN_USB_CMD_SET_BUS,
240 PCAN_USB_BUS_SILENT_MODE, args);
243 /* send the cmd to be notified from bus errors */
244 static int pcan_usb_set_err_frame(struct peak_usb_device *dev, u8 err_mask)
246 u8 args[PCAN_USB_CMD_ARGS_LEN] = {
250 return pcan_usb_send_cmd(dev, PCAN_USB_CMD_ERR_FR, PCAN_USB_SET, args);
253 static int pcan_usb_set_ext_vcc(struct peak_usb_device *dev, u8 onoff)
255 u8 args[PCAN_USB_CMD_ARGS_LEN] = {
259 return pcan_usb_send_cmd(dev, PCAN_USB_CMD_EXT_VCC, PCAN_USB_SET, args);
262 static int pcan_usb_set_led(struct peak_usb_device *dev, u8 onoff)
264 u8 args[PCAN_USB_CMD_ARGS_LEN] = {
268 return pcan_usb_send_cmd(dev, PCAN_USB_CMD_LED, PCAN_USB_SET, args);
272 * set bittiming value to can
274 static int pcan_usb_set_bittiming(struct peak_usb_device *dev,
275 struct can_bittiming *bt)
277 u8 args[PCAN_USB_CMD_ARGS_LEN];
280 btr0 = ((bt->brp - 1) & 0x3f) | (((bt->sjw - 1) & 0x3) << 6);
281 btr1 = ((bt->prop_seg + bt->phase_seg1 - 1) & 0xf) |
282 (((bt->phase_seg2 - 1) & 0x7) << 4);
283 if (dev->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
286 netdev_info(dev->netdev, "setting BTR0=0x%02x BTR1=0x%02x\n",
292 return pcan_usb_send_cmd(dev, PCAN_USB_CMD_BITRATE, PCAN_USB_SET, args);
298 static int pcan_usb_write_mode(struct peak_usb_device *dev, u8 onoff)
302 err = pcan_usb_set_bus(dev, onoff);
307 err = pcan_usb_set_sja1000(dev, SJA1000_MODE_INIT);
309 /* the PCAN-USB needs time to init */
310 set_current_state(TASK_INTERRUPTIBLE);
311 schedule_timeout(msecs_to_jiffies(PCAN_USB_STARTUP_TIMEOUT));
318 * handle end of waiting for the device to reset
320 static void pcan_usb_restart(struct timer_list *t)
322 struct pcan_usb *pdev = from_timer(pdev, t, restart_timer);
323 struct peak_usb_device *dev = &pdev->dev;
325 /* notify candev and netdev */
326 peak_usb_restart_complete(dev);
330 * handle the submission of the restart urb
332 static void pcan_usb_restart_pending(struct urb *urb)
334 struct pcan_usb *pdev = urb->context;
336 /* the PCAN-USB needs time to restart */
337 mod_timer(&pdev->restart_timer,
338 jiffies + msecs_to_jiffies(PCAN_USB_STARTUP_TIMEOUT));
340 /* can delete usb resources */
341 peak_usb_async_complete(urb);
345 * handle asynchronous restart
347 static int pcan_usb_restart_async(struct peak_usb_device *dev, struct urb *urb,
350 struct pcan_usb *pdev = container_of(dev, struct pcan_usb, dev);
352 if (timer_pending(&pdev->restart_timer))
356 buf[PCAN_USB_CMD_FUNC] = 3;
357 buf[PCAN_USB_CMD_NUM] = 2;
358 buf[PCAN_USB_CMD_ARGS] = 1;
360 usb_fill_bulk_urb(urb, dev->udev,
361 usb_sndbulkpipe(dev->udev, PCAN_USB_EP_CMDOUT),
362 buf, PCAN_USB_CMD_LEN,
363 pcan_usb_restart_pending, pdev);
365 return usb_submit_urb(urb, GFP_ATOMIC);
369 * read serial number from device
371 static int pcan_usb_get_serial(struct peak_usb_device *dev, u32 *serial_number)
373 u8 args[PCAN_USB_CMD_ARGS_LEN];
376 err = pcan_usb_wait_rsp(dev, PCAN_USB_CMD_SN, PCAN_USB_GET, args);
379 *serial_number = le32_to_cpup((__le32 *)args);
385 * read can channel id from device
387 static int pcan_usb_get_can_channel_id(struct peak_usb_device *dev, u32 *can_ch_id)
389 u8 args[PCAN_USB_CMD_ARGS_LEN];
392 err = pcan_usb_wait_rsp(dev, PCAN_USB_CMD_DEVID, PCAN_USB_GET, args);
394 netdev_err(dev->netdev, "getting can channel id failure: %d\n", err);
397 *can_ch_id = args[0];
402 /* set a new CAN channel id in the flash memory of the device */
403 static int pcan_usb_set_can_channel_id(struct peak_usb_device *dev, u32 can_ch_id)
405 u8 args[PCAN_USB_CMD_ARGS_LEN];
407 /* this kind of device supports 8-bit values only */
408 if (can_ch_id > U8_MAX)
411 /* during the flash process the device disconnects during ~1.25 s.:
412 * prohibit access when interface is UP
414 if (dev->netdev->flags & IFF_UP)
418 return pcan_usb_send_cmd(dev, PCAN_USB_CMD_DEVID, PCAN_USB_SET, args);
422 * update current time ref with received timestamp
424 static int pcan_usb_update_ts(struct pcan_usb_msg_context *mc)
426 if ((mc->ptr + 2) > mc->end)
429 mc->ts16 = get_unaligned_le16(mc->ptr);
432 peak_usb_update_ts_now(&mc->pdev->time_ref, mc->ts16);
434 peak_usb_set_ts_now(&mc->pdev->time_ref, mc->ts16);
440 * decode received timestamp
442 static int pcan_usb_decode_ts(struct pcan_usb_msg_context *mc, u8 first_packet)
444 /* only 1st packet supplies a word timestamp */
446 if ((mc->ptr + 2) > mc->end)
449 mc->ts16 = get_unaligned_le16(mc->ptr);
450 mc->prev_ts8 = mc->ts16 & 0x00ff;
456 if ((mc->ptr + 1) > mc->end)
461 if (ts8 < mc->prev_ts8)
472 static int pcan_usb_decode_error(struct pcan_usb_msg_context *mc, u8 n,
476 struct can_frame *cf;
477 enum can_state new_state = CAN_STATE_ERROR_ACTIVE;
479 /* ignore this error until 1st ts received */
480 if (n == PCAN_USB_ERROR_QOVR)
481 if (!mc->pdev->time_ref.tick_count)
484 /* allocate an skb to store the error frame */
485 skb = alloc_can_err_skb(mc->netdev, &cf);
487 if (n & PCAN_USB_ERROR_RXQOVR) {
488 /* data overrun interrupt */
489 netdev_dbg(mc->netdev, "data overrun interrupt\n");
490 mc->netdev->stats.rx_over_errors++;
491 mc->netdev->stats.rx_errors++;
493 cf->can_id |= CAN_ERR_CRTL;
494 cf->data[1] |= CAN_ERR_CRTL_RX_OVERFLOW;
498 if (n & PCAN_USB_ERROR_TXQFULL)
499 netdev_dbg(mc->netdev, "device Tx queue full)\n");
501 if (n & PCAN_USB_ERROR_BUS_OFF) {
502 new_state = CAN_STATE_BUS_OFF;
503 } else if (n & PCAN_USB_ERROR_BUS_HEAVY) {
504 new_state = ((mc->pdev->bec.txerr >= 128) ||
505 (mc->pdev->bec.rxerr >= 128)) ?
506 CAN_STATE_ERROR_PASSIVE :
507 CAN_STATE_ERROR_WARNING;
509 new_state = CAN_STATE_ERROR_ACTIVE;
512 /* handle change of state */
513 if (new_state != mc->pdev->dev.can.state) {
514 enum can_state tx_state =
515 (mc->pdev->bec.txerr >= mc->pdev->bec.rxerr) ?
517 enum can_state rx_state =
518 (mc->pdev->bec.txerr <= mc->pdev->bec.rxerr) ?
521 can_change_state(mc->netdev, cf, tx_state, rx_state);
523 if (new_state == CAN_STATE_BUS_OFF) {
524 can_bus_off(mc->netdev);
525 } else if (cf && (cf->can_id & CAN_ERR_CRTL)) {
526 /* Supply TX/RX error counters in case of
529 cf->can_id = CAN_ERR_CNT;
530 cf->data[6] = mc->pdev->bec.txerr;
531 cf->data[7] = mc->pdev->bec.rxerr;
538 if (status_len & PCAN_USB_STATUSLEN_TIMESTAMP) {
539 struct skb_shared_hwtstamps *hwts = skb_hwtstamps(skb);
541 peak_usb_get_ts_time(&mc->pdev->time_ref, mc->ts16,
550 /* decode bus event usb packet: first byte contains rxerr while 2nd one contains
553 static int pcan_usb_handle_bus_evt(struct pcan_usb_msg_context *mc, u8 ir)
555 struct pcan_usb *pdev = mc->pdev;
557 /* according to the content of the packet */
559 case PCAN_USB_ERR_CNT_DEC:
560 case PCAN_USB_ERR_CNT_INC:
562 /* save rx/tx error counters from in the device context */
563 pdev->bec.rxerr = mc->ptr[1];
564 pdev->bec.txerr = mc->ptr[2];
576 * decode non-data usb message
578 static int pcan_usb_decode_status(struct pcan_usb_msg_context *mc,
581 u8 rec_len = status_len & PCAN_USB_STATUSLEN_DLC;
585 /* check whether function and number can be read */
586 if ((mc->ptr + 2) > mc->end)
589 f = mc->ptr[PCAN_USB_CMD_FUNC];
590 n = mc->ptr[PCAN_USB_CMD_NUM];
591 mc->ptr += PCAN_USB_CMD_ARGS;
593 if (status_len & PCAN_USB_STATUSLEN_TIMESTAMP) {
594 int err = pcan_usb_decode_ts(mc, !mc->rec_ts_idx);
599 /* Next packet in the buffer will have a timestamp on a single
606 case PCAN_USB_REC_ERROR:
607 err = pcan_usb_decode_error(mc, n, status_len);
612 case PCAN_USB_REC_ANALOG:
613 /* analog values (ignored) */
617 case PCAN_USB_REC_BUSLOAD:
618 /* bus load (ignored) */
622 case PCAN_USB_REC_TS:
624 if (pcan_usb_update_ts(mc))
628 case PCAN_USB_REC_BUSEVT:
629 /* bus event notifications (get rxerr/txerr) */
630 err = pcan_usb_handle_bus_evt(mc, n);
635 netdev_err(mc->netdev, "unexpected function %u\n", f);
639 if ((mc->ptr + rec_len) > mc->end)
648 * decode data usb message
650 static int pcan_usb_decode_data(struct pcan_usb_msg_context *mc, u8 status_len)
652 u8 rec_len = status_len & PCAN_USB_STATUSLEN_DLC;
654 struct can_frame *cf;
655 struct skb_shared_hwtstamps *hwts;
658 skb = alloc_can_skb(mc->netdev, &cf);
662 if (status_len & PCAN_USB_STATUSLEN_EXT_ID) {
663 if ((mc->ptr + 4) > mc->end)
666 can_id_flags = get_unaligned_le32(mc->ptr);
667 cf->can_id = can_id_flags >> 3 | CAN_EFF_FLAG;
670 if ((mc->ptr + 2) > mc->end)
673 can_id_flags = get_unaligned_le16(mc->ptr);
674 cf->can_id = can_id_flags >> 5;
678 can_frame_set_cc_len(cf, rec_len, mc->pdev->dev.can.ctrlmode);
680 /* Only first packet timestamp is a word */
681 if (pcan_usb_decode_ts(mc, !mc->rec_ts_idx))
684 /* Next packet in the buffer will have a timestamp on a single byte */
688 memset(cf->data, 0x0, sizeof(cf->data));
689 if (status_len & PCAN_USB_STATUSLEN_RTR) {
690 cf->can_id |= CAN_RTR_FLAG;
692 if ((mc->ptr + rec_len) > mc->end)
695 memcpy(cf->data, mc->ptr, cf->len);
698 /* Ignore next byte (client private id) if SRR bit is set */
699 if (can_id_flags & PCAN_USB_TX_SRR)
702 /* update statistics */
703 mc->netdev->stats.rx_bytes += cf->len;
705 mc->netdev->stats.rx_packets++;
707 /* convert timestamp into kernel time */
708 hwts = skb_hwtstamps(skb);
709 peak_usb_get_ts_time(&mc->pdev->time_ref, mc->ts16, &hwts->hwtstamp);
722 * process incoming message
724 static int pcan_usb_decode_msg(struct peak_usb_device *dev, u8 *ibuf, u32 lbuf)
726 struct pcan_usb_msg_context mc = {
728 .ptr = ibuf + PCAN_USB_MSG_HEADER_LEN,
730 .netdev = dev->netdev,
731 .pdev = container_of(dev, struct pcan_usb, dev),
735 for (err = 0; mc.rec_idx < mc.rec_cnt && !err; mc.rec_idx++) {
738 /* handle status and error frames here */
739 if (sl & PCAN_USB_STATUSLEN_INTERNAL) {
740 err = pcan_usb_decode_status(&mc, sl);
741 /* handle normal can frames here */
743 err = pcan_usb_decode_data(&mc, sl);
751 * process any incoming buffer
753 static int pcan_usb_decode_buf(struct peak_usb_device *dev, struct urb *urb)
757 if (urb->actual_length > PCAN_USB_MSG_HEADER_LEN) {
758 err = pcan_usb_decode_msg(dev, urb->transfer_buffer,
761 } else if (urb->actual_length > 0) {
762 netdev_err(dev->netdev, "usb message length error (%u)\n",
771 * process outgoing packet
773 static int pcan_usb_encode_msg(struct peak_usb_device *dev, struct sk_buff *skb,
774 u8 *obuf, size_t *size)
776 struct net_device *netdev = dev->netdev;
777 struct net_device_stats *stats = &netdev->stats;
778 struct can_frame *cf = (struct can_frame *)skb->data;
779 u32 can_id_flags = cf->can_id & CAN_ERR_MASK;
782 obuf[0] = PCAN_USB_MSG_TX_CAN;
783 obuf[1] = 1; /* only one CAN frame is stored in the packet */
785 pc = obuf + PCAN_USB_MSG_HEADER_LEN;
787 /* status/len byte */
788 *pc = can_get_cc_dlc(cf, dev->can.ctrlmode);
790 if (cf->can_id & CAN_RTR_FLAG)
791 *pc |= PCAN_USB_STATUSLEN_RTR;
794 if (cf->can_id & CAN_EFF_FLAG) {
795 *pc |= PCAN_USB_STATUSLEN_EXT_ID;
800 if (dev->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
801 can_id_flags |= PCAN_USB_TX_SRR;
803 if (dev->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT)
804 can_id_flags |= PCAN_USB_TX_AT;
806 put_unaligned_le32(can_id_flags, pc);
813 if (dev->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
814 can_id_flags |= PCAN_USB_TX_SRR;
816 if (dev->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT)
817 can_id_flags |= PCAN_USB_TX_AT;
819 put_unaligned_le16(can_id_flags, pc);
824 if (!(cf->can_id & CAN_RTR_FLAG)) {
825 memcpy(pc, cf->data, cf->len);
829 /* SRR bit needs a writer id (useless here) */
830 if (can_id_flags & PCAN_USB_TX_SRR)
833 obuf[(*size)-1] = (u8)(stats->tx_packets & 0xff);
838 /* socket callback used to copy berr counters values received through USB */
839 static int pcan_usb_get_berr_counter(const struct net_device *netdev,
840 struct can_berr_counter *bec)
842 struct peak_usb_device *dev = netdev_priv(netdev);
843 struct pcan_usb *pdev = container_of(dev, struct pcan_usb, dev);
854 static int pcan_usb_start(struct peak_usb_device *dev)
856 struct pcan_usb *pdev = container_of(dev, struct pcan_usb, dev);
859 /* number of bits used in timestamps read from adapter struct */
860 peak_usb_init_time_ref(&pdev->time_ref, &pcan_usb);
865 /* always ask the device for BERR reporting, to be able to switch from
866 * WARNING to PASSIVE state
868 err = pcan_usb_set_err_frame(dev, PCAN_USB_BERR_MASK);
870 netdev_warn(dev->netdev,
871 "Asking for BERR reporting error %u\n",
874 /* if revision greater than 3, can put silent mode on/off */
875 if (dev->device_rev > 3) {
876 err = pcan_usb_set_silent(dev,
877 dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY);
882 return pcan_usb_set_ext_vcc(dev, 0);
885 static int pcan_usb_init(struct peak_usb_device *dev)
887 struct pcan_usb *pdev = container_of(dev, struct pcan_usb, dev);
891 /* initialize a timer needed to wait for hardware restart */
892 timer_setup(&pdev->restart_timer, pcan_usb_restart, 0);
895 * explicit use of dev_xxx() instead of netdev_xxx() here:
896 * information displayed are related to the device itself, not
897 * to the canx netdevice.
899 err = pcan_usb_get_serial(dev, &serial_number);
901 dev_err(dev->netdev->dev.parent,
902 "unable to read %s serial number (err %d)\n",
907 dev_info(dev->netdev->dev.parent,
908 "PEAK-System %s adapter hwrev %u serial %08X (%u channel)\n",
909 pcan_usb.name, dev->device_rev, serial_number,
910 pcan_usb.ctrl_count);
912 /* Since rev 4.1, PCAN-USB is able to make single-shot as well as
913 * looped back frames.
915 if (dev->device_rev >= 41) {
916 struct can_priv *priv = netdev_priv(dev->netdev);
918 priv->ctrlmode_supported |= CAN_CTRLMODE_ONE_SHOT |
919 CAN_CTRLMODE_LOOPBACK;
921 dev_info(dev->netdev->dev.parent,
929 * probe function for new PCAN-USB usb interface
931 static int pcan_usb_probe(struct usb_interface *intf)
933 struct usb_host_interface *if_desc;
936 if_desc = intf->altsetting;
938 /* check interface endpoint addresses */
939 for (i = 0; i < if_desc->desc.bNumEndpoints; i++) {
940 struct usb_endpoint_descriptor *ep = &if_desc->endpoint[i].desc;
942 switch (ep->bEndpointAddress) {
943 case PCAN_USB_EP_CMDOUT:
944 case PCAN_USB_EP_CMDIN:
945 case PCAN_USB_EP_MSGOUT:
946 case PCAN_USB_EP_MSGIN:
956 static int pcan_usb_set_phys_id(struct net_device *netdev,
957 enum ethtool_phys_id_state state)
959 struct peak_usb_device *dev = netdev_priv(netdev);
963 case ETHTOOL_ID_ACTIVE:
964 /* call ON/OFF twice a second */
968 err = pcan_usb_set_led(dev, 0);
974 case ETHTOOL_ID_INACTIVE:
975 /* restore LED default */
976 err = pcan_usb_set_led(dev, 1);
986 /* This device only handles 8-bit CAN channel id. */
987 static int pcan_usb_get_eeprom_len(struct net_device *netdev)
992 static const struct ethtool_ops pcan_usb_ethtool_ops = {
993 .set_phys_id = pcan_usb_set_phys_id,
994 .get_ts_info = pcan_get_ts_info,
995 .get_eeprom_len = pcan_usb_get_eeprom_len,
996 .get_eeprom = peak_usb_get_eeprom,
997 .set_eeprom = peak_usb_set_eeprom,
1001 * describe the PCAN-USB adapter
1003 static const struct can_bittiming_const pcan_usb_const = {
1015 const struct peak_usb_adapter pcan_usb = {
1017 .device_id = PCAN_USB_PRODUCT_ID,
1019 .ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY |
1020 CAN_CTRLMODE_CC_LEN8_DLC,
1022 .freq = PCAN_USB_CRYSTAL_HZ / 2,
1024 .bittiming_const = &pcan_usb_const,
1026 /* size of device private data */
1027 .sizeof_dev_private = sizeof(struct pcan_usb),
1029 .ethtool_ops = &pcan_usb_ethtool_ops,
1031 /* timestamps usage */
1033 .us_per_ts_scale = PCAN_USB_TS_US_PER_TICK, /* us=(ts*scale) */
1034 .us_per_ts_shift = PCAN_USB_TS_DIV_SHIFTER, /* >> shift */
1036 /* give here messages in/out endpoints */
1037 .ep_msg_in = PCAN_USB_EP_MSGIN,
1038 .ep_msg_out = {PCAN_USB_EP_MSGOUT},
1040 /* size of rx/tx usb buffers */
1041 .rx_buffer_size = PCAN_USB_RX_BUFFER_SIZE,
1042 .tx_buffer_size = PCAN_USB_TX_BUFFER_SIZE,
1044 /* device callbacks */
1045 .intf_probe = pcan_usb_probe,
1046 .dev_init = pcan_usb_init,
1047 .dev_set_bus = pcan_usb_write_mode,
1048 .dev_set_bittiming = pcan_usb_set_bittiming,
1049 .dev_get_can_channel_id = pcan_usb_get_can_channel_id,
1050 .dev_set_can_channel_id = pcan_usb_set_can_channel_id,
1051 .dev_decode_buf = pcan_usb_decode_buf,
1052 .dev_encode_msg = pcan_usb_encode_msg,
1053 .dev_start = pcan_usb_start,
1054 .dev_restart_async = pcan_usb_restart_async,
1055 .do_get_berr_counter = pcan_usb_get_berr_counter,