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/netdevice.h>
12 #include <linux/usb.h>
13 #include <linux/module.h>
14 #include <linux/ethtool.h>
16 #include <linux/can.h>
17 #include <linux/can/dev.h>
18 #include <linux/can/error.h>
20 #include "pcan_usb_core.h"
22 /* PCAN-USB Endpoints */
23 #define PCAN_USB_EP_CMDOUT 1
24 #define PCAN_USB_EP_CMDIN (PCAN_USB_EP_CMDOUT | USB_DIR_IN)
25 #define PCAN_USB_EP_MSGOUT 2
26 #define PCAN_USB_EP_MSGIN (PCAN_USB_EP_MSGOUT | USB_DIR_IN)
28 /* PCAN-USB command struct */
29 #define PCAN_USB_CMD_FUNC 0
30 #define PCAN_USB_CMD_NUM 1
31 #define PCAN_USB_CMD_ARGS 2
32 #define PCAN_USB_CMD_ARGS_LEN 14
33 #define PCAN_USB_CMD_LEN (PCAN_USB_CMD_ARGS + \
34 PCAN_USB_CMD_ARGS_LEN)
36 /* PCAN-USB commands */
37 #define PCAN_USB_CMD_BITRATE 1
38 #define PCAN_USB_CMD_SET_BUS 3
39 #define PCAN_USB_CMD_DEVID 4
40 #define PCAN_USB_CMD_SN 6
41 #define PCAN_USB_CMD_REGISTER 9
42 #define PCAN_USB_CMD_EXT_VCC 10
43 #define PCAN_USB_CMD_ERR_FR 11
44 #define PCAN_USB_CMD_LED 12
46 /* PCAN_USB_CMD_SET_BUS number arg */
47 #define PCAN_USB_BUS_XCVER 2
48 #define PCAN_USB_BUS_SILENT_MODE 3
50 /* PCAN_USB_CMD_xxx functions */
51 #define PCAN_USB_GET 1
52 #define PCAN_USB_SET 2
54 /* PCAN-USB command timeout (ms.) */
55 #define PCAN_USB_COMMAND_TIMEOUT 1000
57 /* PCAN-USB startup timeout (ms.) */
58 #define PCAN_USB_STARTUP_TIMEOUT 10
60 /* PCAN-USB rx/tx buffers size */
61 #define PCAN_USB_RX_BUFFER_SIZE 64
62 #define PCAN_USB_TX_BUFFER_SIZE 64
64 #define PCAN_USB_MSG_HEADER_LEN 2
66 /* PCAN-USB adapter internal clock (MHz) */
67 #define PCAN_USB_CRYSTAL_HZ 16000000
69 /* PCAN-USB USB message record status/len field */
70 #define PCAN_USB_STATUSLEN_TIMESTAMP (1 << 7)
71 #define PCAN_USB_STATUSLEN_INTERNAL (1 << 6)
72 #define PCAN_USB_STATUSLEN_EXT_ID (1 << 5)
73 #define PCAN_USB_STATUSLEN_RTR (1 << 4)
74 #define PCAN_USB_STATUSLEN_DLC (0xf)
76 /* PCAN-USB error flags */
77 #define PCAN_USB_ERROR_TXFULL 0x01
78 #define PCAN_USB_ERROR_RXQOVR 0x02
79 #define PCAN_USB_ERROR_BUS_LIGHT 0x04
80 #define PCAN_USB_ERROR_BUS_HEAVY 0x08
81 #define PCAN_USB_ERROR_BUS_OFF 0x10
82 #define PCAN_USB_ERROR_RXQEMPTY 0x20
83 #define PCAN_USB_ERROR_QOVR 0x40
84 #define PCAN_USB_ERROR_TXQFULL 0x80
86 #define PCAN_USB_ERROR_BUS (PCAN_USB_ERROR_BUS_LIGHT | \
87 PCAN_USB_ERROR_BUS_HEAVY | \
88 PCAN_USB_ERROR_BUS_OFF)
91 #define SJA1000_MODE_NORMAL 0x00
92 #define SJA1000_MODE_INIT 0x01
95 * tick duration = 42.666 us =>
96 * (tick_number * 44739243) >> 20 ~ (tick_number * 42666) / 1000
99 #define PCAN_USB_TS_DIV_SHIFTER 20
100 #define PCAN_USB_TS_US_PER_TICK 44739243
102 /* PCAN-USB messages record types */
103 #define PCAN_USB_REC_ERROR 1
104 #define PCAN_USB_REC_ANALOG 2
105 #define PCAN_USB_REC_BUSLOAD 3
106 #define PCAN_USB_REC_TS 4
107 #define PCAN_USB_REC_BUSEVT 5
109 /* CAN bus events notifications selection mask */
110 #define PCAN_USB_ERR_RXERR 0x02 /* ask for rxerr counter */
111 #define PCAN_USB_ERR_TXERR 0x04 /* ask for txerr counter */
113 /* This mask generates an usb packet each time the state of the bus changes.
114 * In other words, its interest is to know which side among rx and tx is
115 * responsible of the change of the bus state.
117 #define PCAN_USB_BERR_MASK (PCAN_USB_ERR_RXERR | PCAN_USB_ERR_TXERR)
119 /* identify bus event packets with rx/tx error counters */
120 #define PCAN_USB_ERR_CNT 0x80
122 /* private to PCAN-USB adapter */
124 struct peak_usb_device dev;
125 struct peak_time_ref time_ref;
126 struct timer_list restart_timer;
127 struct can_berr_counter bec;
130 /* incoming message context for decoding */
131 struct pcan_usb_msg_context {
139 struct net_device *netdev;
140 struct pcan_usb *pdev;
146 static int pcan_usb_send_cmd(struct peak_usb_device *dev, u8 f, u8 n, u8 *p)
151 /* usb device unregistered? */
152 if (!(dev->state & PCAN_USB_STATE_CONNECTED))
155 dev->cmd_buf[PCAN_USB_CMD_FUNC] = f;
156 dev->cmd_buf[PCAN_USB_CMD_NUM] = n;
159 memcpy(dev->cmd_buf + PCAN_USB_CMD_ARGS,
160 p, PCAN_USB_CMD_ARGS_LEN);
162 err = usb_bulk_msg(dev->udev,
163 usb_sndbulkpipe(dev->udev, PCAN_USB_EP_CMDOUT),
164 dev->cmd_buf, PCAN_USB_CMD_LEN, &actual_length,
165 PCAN_USB_COMMAND_TIMEOUT);
167 netdev_err(dev->netdev,
168 "sending cmd f=0x%x n=0x%x failure: %d\n",
174 * send a command then wait for its response
176 static int pcan_usb_wait_rsp(struct peak_usb_device *dev, u8 f, u8 n, u8 *p)
181 /* usb device unregistered? */
182 if (!(dev->state & PCAN_USB_STATE_CONNECTED))
185 /* first, send command */
186 err = pcan_usb_send_cmd(dev, f, n, NULL);
190 err = usb_bulk_msg(dev->udev,
191 usb_rcvbulkpipe(dev->udev, PCAN_USB_EP_CMDIN),
192 dev->cmd_buf, PCAN_USB_CMD_LEN, &actual_length,
193 PCAN_USB_COMMAND_TIMEOUT);
195 netdev_err(dev->netdev,
196 "waiting rsp f=0x%x n=0x%x failure: %d\n", f, n, err);
198 memcpy(p, dev->cmd_buf + PCAN_USB_CMD_ARGS,
199 PCAN_USB_CMD_ARGS_LEN);
204 static int pcan_usb_set_sja1000(struct peak_usb_device *dev, u8 mode)
206 u8 args[PCAN_USB_CMD_ARGS_LEN] = {
210 return pcan_usb_send_cmd(dev, PCAN_USB_CMD_REGISTER, PCAN_USB_SET,
214 static int pcan_usb_set_bus(struct peak_usb_device *dev, u8 onoff)
216 u8 args[PCAN_USB_CMD_ARGS_LEN] = {
220 return pcan_usb_send_cmd(dev, PCAN_USB_CMD_SET_BUS, PCAN_USB_BUS_XCVER,
224 static int pcan_usb_set_silent(struct peak_usb_device *dev, u8 onoff)
226 u8 args[PCAN_USB_CMD_ARGS_LEN] = {
230 return pcan_usb_send_cmd(dev, PCAN_USB_CMD_SET_BUS,
231 PCAN_USB_BUS_SILENT_MODE, args);
234 /* send the cmd to be notified from bus errors */
235 static int pcan_usb_set_err_frame(struct peak_usb_device *dev, u8 err_mask)
237 u8 args[PCAN_USB_CMD_ARGS_LEN] = {
241 return pcan_usb_send_cmd(dev, PCAN_USB_CMD_ERR_FR, PCAN_USB_SET, args);
244 static int pcan_usb_set_ext_vcc(struct peak_usb_device *dev, u8 onoff)
246 u8 args[PCAN_USB_CMD_ARGS_LEN] = {
250 return pcan_usb_send_cmd(dev, PCAN_USB_CMD_EXT_VCC, PCAN_USB_SET, args);
253 static int pcan_usb_set_led(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_LED, PCAN_USB_SET, args);
263 * set bittiming value to can
265 static int pcan_usb_set_bittiming(struct peak_usb_device *dev,
266 struct can_bittiming *bt)
268 u8 args[PCAN_USB_CMD_ARGS_LEN];
271 btr0 = ((bt->brp - 1) & 0x3f) | (((bt->sjw - 1) & 0x3) << 6);
272 btr1 = ((bt->prop_seg + bt->phase_seg1 - 1) & 0xf) |
273 (((bt->phase_seg2 - 1) & 0x7) << 4);
274 if (dev->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
277 netdev_info(dev->netdev, "setting BTR0=0x%02x BTR1=0x%02x\n",
283 return pcan_usb_send_cmd(dev, PCAN_USB_CMD_BITRATE, PCAN_USB_SET, args);
289 static int pcan_usb_write_mode(struct peak_usb_device *dev, u8 onoff)
293 err = pcan_usb_set_bus(dev, onoff);
298 err = pcan_usb_set_sja1000(dev, SJA1000_MODE_INIT);
300 /* the PCAN-USB needs time to init */
301 set_current_state(TASK_INTERRUPTIBLE);
302 schedule_timeout(msecs_to_jiffies(PCAN_USB_STARTUP_TIMEOUT));
309 * handle end of waiting for the device to reset
311 static void pcan_usb_restart(struct timer_list *t)
313 struct pcan_usb *pdev = from_timer(pdev, t, restart_timer);
314 struct peak_usb_device *dev = &pdev->dev;
316 /* notify candev and netdev */
317 peak_usb_restart_complete(dev);
321 * handle the submission of the restart urb
323 static void pcan_usb_restart_pending(struct urb *urb)
325 struct pcan_usb *pdev = urb->context;
327 /* the PCAN-USB needs time to restart */
328 mod_timer(&pdev->restart_timer,
329 jiffies + msecs_to_jiffies(PCAN_USB_STARTUP_TIMEOUT));
331 /* can delete usb resources */
332 peak_usb_async_complete(urb);
336 * handle asynchronous restart
338 static int pcan_usb_restart_async(struct peak_usb_device *dev, struct urb *urb,
341 struct pcan_usb *pdev = container_of(dev, struct pcan_usb, dev);
343 if (timer_pending(&pdev->restart_timer))
347 buf[PCAN_USB_CMD_FUNC] = 3;
348 buf[PCAN_USB_CMD_NUM] = 2;
349 buf[PCAN_USB_CMD_ARGS] = 1;
351 usb_fill_bulk_urb(urb, dev->udev,
352 usb_sndbulkpipe(dev->udev, PCAN_USB_EP_CMDOUT),
353 buf, PCAN_USB_CMD_LEN,
354 pcan_usb_restart_pending, pdev);
356 return usb_submit_urb(urb, GFP_ATOMIC);
360 * read serial number from device
362 static int pcan_usb_get_serial(struct peak_usb_device *dev, u32 *serial_number)
364 u8 args[PCAN_USB_CMD_ARGS_LEN];
367 err = pcan_usb_wait_rsp(dev, PCAN_USB_CMD_SN, PCAN_USB_GET, args);
370 *serial_number = le32_to_cpup((__le32 *)args);
376 * read device id from device
378 static int pcan_usb_get_device_id(struct peak_usb_device *dev, u32 *device_id)
380 u8 args[PCAN_USB_CMD_ARGS_LEN];
383 err = pcan_usb_wait_rsp(dev, PCAN_USB_CMD_DEVID, PCAN_USB_GET, args);
385 netdev_err(dev->netdev, "getting device id failure: %d\n", err);
387 *device_id = args[0];
393 * update current time ref with received timestamp
395 static int pcan_usb_update_ts(struct pcan_usb_msg_context *mc)
397 if ((mc->ptr + 2) > mc->end)
400 mc->ts16 = get_unaligned_le16(mc->ptr);
403 peak_usb_update_ts_now(&mc->pdev->time_ref, mc->ts16);
405 peak_usb_set_ts_now(&mc->pdev->time_ref, mc->ts16);
411 * decode received timestamp
413 static int pcan_usb_decode_ts(struct pcan_usb_msg_context *mc, u8 first_packet)
415 /* only 1st packet supplies a word timestamp */
417 if ((mc->ptr + 2) > mc->end)
420 mc->ts16 = get_unaligned_le16(mc->ptr);
421 mc->prev_ts8 = mc->ts16 & 0x00ff;
427 if ((mc->ptr + 1) > mc->end)
432 if (ts8 < mc->prev_ts8)
443 static int pcan_usb_decode_error(struct pcan_usb_msg_context *mc, u8 n,
447 struct can_frame *cf;
448 enum can_state new_state;
450 /* ignore this error until 1st ts received */
451 if (n == PCAN_USB_ERROR_QOVR)
452 if (!mc->pdev->time_ref.tick_count)
455 new_state = mc->pdev->dev.can.state;
457 switch (mc->pdev->dev.can.state) {
458 case CAN_STATE_ERROR_ACTIVE:
459 if (n & PCAN_USB_ERROR_BUS_LIGHT) {
460 new_state = CAN_STATE_ERROR_WARNING;
465 case CAN_STATE_ERROR_WARNING:
466 if (n & PCAN_USB_ERROR_BUS_HEAVY) {
467 new_state = CAN_STATE_ERROR_PASSIVE;
470 if (n & PCAN_USB_ERROR_BUS_OFF) {
471 new_state = CAN_STATE_BUS_OFF;
474 if (n & ~PCAN_USB_ERROR_BUS) {
476 * trick to bypass next comparison and process other
479 new_state = CAN_STATE_MAX;
482 if ((n & PCAN_USB_ERROR_BUS_LIGHT) == 0) {
483 /* no error (back to active state) */
484 new_state = CAN_STATE_ERROR_ACTIVE;
489 case CAN_STATE_ERROR_PASSIVE:
490 if (n & PCAN_USB_ERROR_BUS_OFF) {
491 new_state = CAN_STATE_BUS_OFF;
494 if (n & PCAN_USB_ERROR_BUS_LIGHT) {
495 new_state = CAN_STATE_ERROR_WARNING;
498 if (n & ~PCAN_USB_ERROR_BUS) {
500 * trick to bypass next comparison and process other
503 new_state = CAN_STATE_MAX;
507 if ((n & PCAN_USB_ERROR_BUS_HEAVY) == 0) {
508 /* no error (back to warning state) */
509 new_state = CAN_STATE_ERROR_WARNING;
515 /* do nothing waiting for restart */
519 /* donot post any error if current state didn't change */
520 if (mc->pdev->dev.can.state == new_state)
523 /* allocate an skb to store the error frame */
524 skb = alloc_can_err_skb(mc->netdev, &cf);
529 case CAN_STATE_BUS_OFF:
530 cf->can_id |= CAN_ERR_BUSOFF;
531 mc->pdev->dev.can.can_stats.bus_off++;
532 can_bus_off(mc->netdev);
535 case CAN_STATE_ERROR_PASSIVE:
536 cf->can_id |= CAN_ERR_CRTL;
537 cf->data[1] = (mc->pdev->bec.txerr > mc->pdev->bec.rxerr) ?
538 CAN_ERR_CRTL_TX_PASSIVE :
539 CAN_ERR_CRTL_RX_PASSIVE;
540 cf->data[6] = mc->pdev->bec.txerr;
541 cf->data[7] = mc->pdev->bec.rxerr;
543 mc->pdev->dev.can.can_stats.error_passive++;
546 case CAN_STATE_ERROR_WARNING:
547 cf->can_id |= CAN_ERR_CRTL;
548 cf->data[1] = (mc->pdev->bec.txerr > mc->pdev->bec.rxerr) ?
549 CAN_ERR_CRTL_TX_WARNING :
550 CAN_ERR_CRTL_RX_WARNING;
551 cf->data[6] = mc->pdev->bec.txerr;
552 cf->data[7] = mc->pdev->bec.rxerr;
554 mc->pdev->dev.can.can_stats.error_warning++;
557 case CAN_STATE_ERROR_ACTIVE:
558 cf->can_id |= CAN_ERR_CRTL;
559 cf->data[1] = CAN_ERR_CRTL_ACTIVE;
561 /* sync local copies of rxerr/txerr counters */
562 mc->pdev->bec.txerr = 0;
563 mc->pdev->bec.rxerr = 0;
567 /* CAN_STATE_MAX (trick to handle other errors) */
568 if (n & PCAN_USB_ERROR_TXQFULL)
569 netdev_dbg(mc->netdev, "device Tx queue full)\n");
571 if (n & PCAN_USB_ERROR_RXQOVR) {
572 netdev_dbg(mc->netdev, "data overrun interrupt\n");
573 cf->can_id |= CAN_ERR_CRTL;
574 cf->data[1] |= CAN_ERR_CRTL_RX_OVERFLOW;
575 mc->netdev->stats.rx_over_errors++;
576 mc->netdev->stats.rx_errors++;
579 cf->data[6] = mc->pdev->bec.txerr;
580 cf->data[7] = mc->pdev->bec.rxerr;
582 new_state = mc->pdev->dev.can.state;
586 mc->pdev->dev.can.state = new_state;
588 if (status_len & PCAN_USB_STATUSLEN_TIMESTAMP) {
589 struct skb_shared_hwtstamps *hwts = skb_hwtstamps(skb);
591 peak_usb_get_ts_time(&mc->pdev->time_ref, mc->ts16,
595 mc->netdev->stats.rx_packets++;
596 mc->netdev->stats.rx_bytes += cf->len;
602 /* decode bus event usb packet: first byte contains rxerr while 2nd one contains
605 static int pcan_usb_handle_bus_evt(struct pcan_usb_msg_context *mc, u8 ir)
607 struct pcan_usb *pdev = mc->pdev;
609 /* acccording to the content of the packet */
611 case PCAN_USB_ERR_CNT:
613 /* save rx/tx error counters from in the device context */
614 pdev->bec.rxerr = mc->ptr[0];
615 pdev->bec.txerr = mc->ptr[1];
627 * decode non-data usb message
629 static int pcan_usb_decode_status(struct pcan_usb_msg_context *mc,
632 u8 rec_len = status_len & PCAN_USB_STATUSLEN_DLC;
636 /* check whether function and number can be read */
637 if ((mc->ptr + 2) > mc->end)
640 f = mc->ptr[PCAN_USB_CMD_FUNC];
641 n = mc->ptr[PCAN_USB_CMD_NUM];
642 mc->ptr += PCAN_USB_CMD_ARGS;
644 if (status_len & PCAN_USB_STATUSLEN_TIMESTAMP) {
645 int err = pcan_usb_decode_ts(mc, !mc->rec_ts_idx);
650 /* Next packet in the buffer will have a timestamp on a single
657 case PCAN_USB_REC_ERROR:
658 err = pcan_usb_decode_error(mc, n, status_len);
663 case PCAN_USB_REC_ANALOG:
664 /* analog values (ignored) */
668 case PCAN_USB_REC_BUSLOAD:
669 /* bus load (ignored) */
673 case PCAN_USB_REC_TS:
675 if (pcan_usb_update_ts(mc))
679 case PCAN_USB_REC_BUSEVT:
680 /* bus event notifications (get rxerr/txerr) */
681 err = pcan_usb_handle_bus_evt(mc, n);
686 netdev_err(mc->netdev, "unexpected function %u\n", f);
690 if ((mc->ptr + rec_len) > mc->end)
699 * decode data usb message
701 static int pcan_usb_decode_data(struct pcan_usb_msg_context *mc, u8 status_len)
703 u8 rec_len = status_len & PCAN_USB_STATUSLEN_DLC;
705 struct can_frame *cf;
706 struct skb_shared_hwtstamps *hwts;
708 skb = alloc_can_skb(mc->netdev, &cf);
712 if (status_len & PCAN_USB_STATUSLEN_EXT_ID) {
713 if ((mc->ptr + 4) > mc->end)
716 cf->can_id = get_unaligned_le32(mc->ptr) >> 3 | CAN_EFF_FLAG;
719 if ((mc->ptr + 2) > mc->end)
722 cf->can_id = get_unaligned_le16(mc->ptr) >> 5;
726 can_frame_set_cc_len(cf, rec_len, mc->pdev->dev.can.ctrlmode);
728 /* Only first packet timestamp is a word */
729 if (pcan_usb_decode_ts(mc, !mc->rec_ts_idx))
732 /* Next packet in the buffer will have a timestamp on a single byte */
736 memset(cf->data, 0x0, sizeof(cf->data));
737 if (status_len & PCAN_USB_STATUSLEN_RTR) {
738 cf->can_id |= CAN_RTR_FLAG;
740 if ((mc->ptr + rec_len) > mc->end)
743 memcpy(cf->data, mc->ptr, cf->len);
747 /* convert timestamp into kernel time */
748 hwts = skb_hwtstamps(skb);
749 peak_usb_get_ts_time(&mc->pdev->time_ref, mc->ts16, &hwts->hwtstamp);
751 /* update statistics */
752 mc->netdev->stats.rx_packets++;
753 mc->netdev->stats.rx_bytes += cf->len;
765 * process incoming message
767 static int pcan_usb_decode_msg(struct peak_usb_device *dev, u8 *ibuf, u32 lbuf)
769 struct pcan_usb_msg_context mc = {
771 .ptr = ibuf + PCAN_USB_MSG_HEADER_LEN,
773 .netdev = dev->netdev,
774 .pdev = container_of(dev, struct pcan_usb, dev),
778 for (err = 0; mc.rec_idx < mc.rec_cnt && !err; mc.rec_idx++) {
781 /* handle status and error frames here */
782 if (sl & PCAN_USB_STATUSLEN_INTERNAL) {
783 err = pcan_usb_decode_status(&mc, sl);
784 /* handle normal can frames here */
786 err = pcan_usb_decode_data(&mc, sl);
794 * process any incoming buffer
796 static int pcan_usb_decode_buf(struct peak_usb_device *dev, struct urb *urb)
800 if (urb->actual_length > PCAN_USB_MSG_HEADER_LEN) {
801 err = pcan_usb_decode_msg(dev, urb->transfer_buffer,
804 } else if (urb->actual_length > 0) {
805 netdev_err(dev->netdev, "usb message length error (%u)\n",
814 * process outgoing packet
816 static int pcan_usb_encode_msg(struct peak_usb_device *dev, struct sk_buff *skb,
817 u8 *obuf, size_t *size)
819 struct net_device *netdev = dev->netdev;
820 struct net_device_stats *stats = &netdev->stats;
821 struct can_frame *cf = (struct can_frame *)skb->data;
827 pc = obuf + PCAN_USB_MSG_HEADER_LEN;
829 /* status/len byte */
830 *pc = can_get_cc_dlc(cf, dev->can.ctrlmode);
832 if (cf->can_id & CAN_RTR_FLAG)
833 *pc |= PCAN_USB_STATUSLEN_RTR;
836 if (cf->can_id & CAN_EFF_FLAG) {
837 *pc |= PCAN_USB_STATUSLEN_EXT_ID;
840 put_unaligned_le32((cf->can_id & CAN_ERR_MASK) << 3, pc);
845 put_unaligned_le16((cf->can_id & CAN_ERR_MASK) << 5, pc);
850 if (!(cf->can_id & CAN_RTR_FLAG)) {
851 memcpy(pc, cf->data, cf->len);
855 obuf[(*size)-1] = (u8)(stats->tx_packets & 0xff);
860 /* socket callback used to copy berr counters values received through USB */
861 static int pcan_usb_get_berr_counter(const struct net_device *netdev,
862 struct can_berr_counter *bec)
864 struct peak_usb_device *dev = netdev_priv(netdev);
865 struct pcan_usb *pdev = container_of(dev, struct pcan_usb, dev);
876 static int pcan_usb_start(struct peak_usb_device *dev)
878 struct pcan_usb *pdev = container_of(dev, struct pcan_usb, dev);
881 /* number of bits used in timestamps read from adapter struct */
882 peak_usb_init_time_ref(&pdev->time_ref, &pcan_usb);
887 /* be notified on error counter changes (if requested by user) */
888 if (dev->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) {
889 err = pcan_usb_set_err_frame(dev, PCAN_USB_BERR_MASK);
891 netdev_warn(dev->netdev,
892 "Asking for BERR reporting error %u\n",
896 /* if revision greater than 3, can put silent mode on/off */
897 if (dev->device_rev > 3) {
898 err = pcan_usb_set_silent(dev,
899 dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY);
904 return pcan_usb_set_ext_vcc(dev, 0);
907 static int pcan_usb_init(struct peak_usb_device *dev)
909 struct pcan_usb *pdev = container_of(dev, struct pcan_usb, dev);
913 /* initialize a timer needed to wait for hardware restart */
914 timer_setup(&pdev->restart_timer, pcan_usb_restart, 0);
917 * explicit use of dev_xxx() instead of netdev_xxx() here:
918 * information displayed are related to the device itself, not
919 * to the canx netdevice.
921 err = pcan_usb_get_serial(dev, &serial_number);
923 dev_err(dev->netdev->dev.parent,
924 "unable to read %s serial number (err %d)\n",
929 dev_info(dev->netdev->dev.parent,
930 "PEAK-System %s adapter hwrev %u serial %08X (%u channel)\n",
931 pcan_usb.name, dev->device_rev, serial_number,
932 pcan_usb.ctrl_count);
938 * probe function for new PCAN-USB usb interface
940 static int pcan_usb_probe(struct usb_interface *intf)
942 struct usb_host_interface *if_desc;
945 if_desc = intf->altsetting;
947 /* check interface endpoint addresses */
948 for (i = 0; i < if_desc->desc.bNumEndpoints; i++) {
949 struct usb_endpoint_descriptor *ep = &if_desc->endpoint[i].desc;
951 switch (ep->bEndpointAddress) {
952 case PCAN_USB_EP_CMDOUT:
953 case PCAN_USB_EP_CMDIN:
954 case PCAN_USB_EP_MSGOUT:
955 case PCAN_USB_EP_MSGIN:
965 static int pcan_usb_set_phys_id(struct net_device *netdev,
966 enum ethtool_phys_id_state state)
968 struct peak_usb_device *dev = netdev_priv(netdev);
972 case ETHTOOL_ID_ACTIVE:
973 /* call ON/OFF twice a second */
977 err = pcan_usb_set_led(dev, 0);
983 case ETHTOOL_ID_INACTIVE:
984 /* restore LED default */
985 err = pcan_usb_set_led(dev, 1);
995 static const struct ethtool_ops pcan_usb_ethtool_ops = {
996 .set_phys_id = pcan_usb_set_phys_id,
1000 * describe the PCAN-USB adapter
1002 static const struct can_bittiming_const pcan_usb_const = {
1014 const struct peak_usb_adapter pcan_usb = {
1016 .device_id = PCAN_USB_PRODUCT_ID,
1018 .ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY |
1019 CAN_CTRLMODE_BERR_REPORTING |
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_device_id = pcan_usb_get_device_id,
1050 .dev_decode_buf = pcan_usb_decode_buf,
1051 .dev_encode_msg = pcan_usb_encode_msg,
1052 .dev_start = pcan_usb_start,
1053 .dev_restart_async = pcan_usb_restart_async,
1054 .do_get_berr_counter = pcan_usb_get_berr_counter,