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
7 * Copyright (C) 2011-2012 Stephane Grosjean <s.grosjean@peak-system.com>
9 * Many thanks to Klaus Hitschler <klaus.hitschler@gmx.de>
11 #include <linux/netdevice.h>
12 #include <linux/usb.h>
13 #include <linux/module.h>
15 #include <linux/can.h>
16 #include <linux/can/dev.h>
17 #include <linux/can/error.h>
19 #include "pcan_usb_core.h"
21 /* PCAN-USB Endpoints */
22 #define PCAN_USB_EP_CMDOUT 1
23 #define PCAN_USB_EP_CMDIN (PCAN_USB_EP_CMDOUT | USB_DIR_IN)
24 #define PCAN_USB_EP_MSGOUT 2
25 #define PCAN_USB_EP_MSGIN (PCAN_USB_EP_MSGOUT | USB_DIR_IN)
27 /* PCAN-USB command struct */
28 #define PCAN_USB_CMD_FUNC 0
29 #define PCAN_USB_CMD_NUM 1
30 #define PCAN_USB_CMD_ARGS 2
31 #define PCAN_USB_CMD_ARGS_LEN 14
32 #define PCAN_USB_CMD_LEN (PCAN_USB_CMD_ARGS + \
33 PCAN_USB_CMD_ARGS_LEN)
35 /* PCAN-USB commands */
36 #define PCAN_USB_CMD_BITRATE 1
37 #define PCAN_USB_CMD_SET_BUS 3
38 #define PCAN_USB_CMD_DEVID 4
39 #define PCAN_USB_CMD_SN 6
40 #define PCAN_USB_CMD_REGISTER 9
41 #define PCAN_USB_CMD_EXT_VCC 10
42 #define PCAN_USB_CMD_ERR_FR 11
44 /* PCAN_USB_CMD_SET_BUS number arg */
45 #define PCAN_USB_BUS_XCVER 2
46 #define PCAN_USB_BUS_SILENT_MODE 3
48 /* PCAN_USB_CMD_xxx functions */
49 #define PCAN_USB_GET 1
50 #define PCAN_USB_SET 2
52 /* PCAN-USB command timeout (ms.) */
53 #define PCAN_USB_COMMAND_TIMEOUT 1000
55 /* PCAN-USB startup timeout (ms.) */
56 #define PCAN_USB_STARTUP_TIMEOUT 10
58 /* PCAN-USB rx/tx buffers size */
59 #define PCAN_USB_RX_BUFFER_SIZE 64
60 #define PCAN_USB_TX_BUFFER_SIZE 64
62 #define PCAN_USB_MSG_HEADER_LEN 2
64 /* PCAN-USB adapter internal clock (MHz) */
65 #define PCAN_USB_CRYSTAL_HZ 16000000
67 /* PCAN-USB USB message record status/len field */
68 #define PCAN_USB_STATUSLEN_TIMESTAMP (1 << 7)
69 #define PCAN_USB_STATUSLEN_INTERNAL (1 << 6)
70 #define PCAN_USB_STATUSLEN_EXT_ID (1 << 5)
71 #define PCAN_USB_STATUSLEN_RTR (1 << 4)
72 #define PCAN_USB_STATUSLEN_DLC (0xf)
74 /* PCAN-USB error flags */
75 #define PCAN_USB_ERROR_TXFULL 0x01
76 #define PCAN_USB_ERROR_RXQOVR 0x02
77 #define PCAN_USB_ERROR_BUS_LIGHT 0x04
78 #define PCAN_USB_ERROR_BUS_HEAVY 0x08
79 #define PCAN_USB_ERROR_BUS_OFF 0x10
80 #define PCAN_USB_ERROR_RXQEMPTY 0x20
81 #define PCAN_USB_ERROR_QOVR 0x40
82 #define PCAN_USB_ERROR_TXQFULL 0x80
84 #define PCAN_USB_ERROR_BUS (PCAN_USB_ERROR_BUS_LIGHT | \
85 PCAN_USB_ERROR_BUS_HEAVY | \
86 PCAN_USB_ERROR_BUS_OFF)
89 #define SJA1000_MODE_NORMAL 0x00
90 #define SJA1000_MODE_INIT 0x01
93 * tick duration = 42.666 us =>
94 * (tick_number * 44739243) >> 20 ~ (tick_number * 42666) / 1000
97 #define PCAN_USB_TS_DIV_SHIFTER 20
98 #define PCAN_USB_TS_US_PER_TICK 44739243
100 /* PCAN-USB messages record types */
101 #define PCAN_USB_REC_ERROR 1
102 #define PCAN_USB_REC_ANALOG 2
103 #define PCAN_USB_REC_BUSLOAD 3
104 #define PCAN_USB_REC_TS 4
105 #define PCAN_USB_REC_BUSEVT 5
107 /* CAN bus events notifications selection mask */
108 #define PCAN_USB_ERR_RXERR 0x02 /* ask for rxerr counter */
109 #define PCAN_USB_ERR_TXERR 0x04 /* ask for txerr counter */
111 /* This mask generates an usb packet each time the state of the bus changes.
112 * In other words, its interest is to know which side among rx and tx is
113 * responsible of the change of the bus state.
115 #define PCAN_USB_BERR_MASK (PCAN_USB_ERR_RXERR | PCAN_USB_ERR_TXERR)
117 /* identify bus event packets with rx/tx error counters */
118 #define PCAN_USB_ERR_CNT 0x80
120 /* private to PCAN-USB adapter */
122 struct peak_usb_device dev;
123 struct peak_time_ref time_ref;
124 struct timer_list restart_timer;
125 struct can_berr_counter bec;
128 /* incoming message context for decoding */
129 struct pcan_usb_msg_context {
137 struct net_device *netdev;
138 struct pcan_usb *pdev;
144 static int pcan_usb_send_cmd(struct peak_usb_device *dev, u8 f, u8 n, u8 *p)
149 /* usb device unregistered? */
150 if (!(dev->state & PCAN_USB_STATE_CONNECTED))
153 dev->cmd_buf[PCAN_USB_CMD_FUNC] = f;
154 dev->cmd_buf[PCAN_USB_CMD_NUM] = n;
157 memcpy(dev->cmd_buf + PCAN_USB_CMD_ARGS,
158 p, PCAN_USB_CMD_ARGS_LEN);
160 err = usb_bulk_msg(dev->udev,
161 usb_sndbulkpipe(dev->udev, PCAN_USB_EP_CMDOUT),
162 dev->cmd_buf, PCAN_USB_CMD_LEN, &actual_length,
163 PCAN_USB_COMMAND_TIMEOUT);
165 netdev_err(dev->netdev,
166 "sending cmd f=0x%x n=0x%x failure: %d\n",
172 * send a command then wait for its response
174 static int pcan_usb_wait_rsp(struct peak_usb_device *dev, u8 f, u8 n, u8 *p)
179 /* usb device unregistered? */
180 if (!(dev->state & PCAN_USB_STATE_CONNECTED))
183 /* first, send command */
184 err = pcan_usb_send_cmd(dev, f, n, NULL);
188 err = usb_bulk_msg(dev->udev,
189 usb_rcvbulkpipe(dev->udev, PCAN_USB_EP_CMDIN),
190 dev->cmd_buf, PCAN_USB_CMD_LEN, &actual_length,
191 PCAN_USB_COMMAND_TIMEOUT);
193 netdev_err(dev->netdev,
194 "waiting rsp f=0x%x n=0x%x failure: %d\n", f, n, err);
196 memcpy(p, dev->cmd_buf + PCAN_USB_CMD_ARGS,
197 PCAN_USB_CMD_ARGS_LEN);
202 static int pcan_usb_set_sja1000(struct peak_usb_device *dev, u8 mode)
204 u8 args[PCAN_USB_CMD_ARGS_LEN] = {
208 return pcan_usb_send_cmd(dev, PCAN_USB_CMD_REGISTER, PCAN_USB_SET,
212 static int pcan_usb_set_bus(struct peak_usb_device *dev, u8 onoff)
214 u8 args[PCAN_USB_CMD_ARGS_LEN] = {
218 return pcan_usb_send_cmd(dev, PCAN_USB_CMD_SET_BUS, PCAN_USB_BUS_XCVER,
222 static int pcan_usb_set_silent(struct peak_usb_device *dev, u8 onoff)
224 u8 args[PCAN_USB_CMD_ARGS_LEN] = {
228 return pcan_usb_send_cmd(dev, PCAN_USB_CMD_SET_BUS,
229 PCAN_USB_BUS_SILENT_MODE, args);
232 /* send the cmd to be notified from bus errors */
233 static int pcan_usb_set_err_frame(struct peak_usb_device *dev, u8 err_mask)
235 u8 args[PCAN_USB_CMD_ARGS_LEN] = {
239 return pcan_usb_send_cmd(dev, PCAN_USB_CMD_ERR_FR, PCAN_USB_SET, args);
242 static int pcan_usb_set_ext_vcc(struct peak_usb_device *dev, u8 onoff)
244 u8 args[PCAN_USB_CMD_ARGS_LEN] = {
248 return pcan_usb_send_cmd(dev, PCAN_USB_CMD_EXT_VCC, PCAN_USB_SET, args);
252 * set bittiming value to can
254 static int pcan_usb_set_bittiming(struct peak_usb_device *dev,
255 struct can_bittiming *bt)
257 u8 args[PCAN_USB_CMD_ARGS_LEN];
260 btr0 = ((bt->brp - 1) & 0x3f) | (((bt->sjw - 1) & 0x3) << 6);
261 btr1 = ((bt->prop_seg + bt->phase_seg1 - 1) & 0xf) |
262 (((bt->phase_seg2 - 1) & 0x7) << 4);
263 if (dev->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
266 netdev_info(dev->netdev, "setting BTR0=0x%02x BTR1=0x%02x\n",
272 return pcan_usb_send_cmd(dev, PCAN_USB_CMD_BITRATE, PCAN_USB_SET, args);
278 static int pcan_usb_write_mode(struct peak_usb_device *dev, u8 onoff)
282 err = pcan_usb_set_bus(dev, onoff);
287 err = pcan_usb_set_sja1000(dev, SJA1000_MODE_INIT);
289 /* the PCAN-USB needs time to init */
290 set_current_state(TASK_INTERRUPTIBLE);
291 schedule_timeout(msecs_to_jiffies(PCAN_USB_STARTUP_TIMEOUT));
298 * handle end of waiting for the device to reset
300 static void pcan_usb_restart(struct timer_list *t)
302 struct pcan_usb *pdev = from_timer(pdev, t, restart_timer);
303 struct peak_usb_device *dev = &pdev->dev;
305 /* notify candev and netdev */
306 peak_usb_restart_complete(dev);
310 * handle the submission of the restart urb
312 static void pcan_usb_restart_pending(struct urb *urb)
314 struct pcan_usb *pdev = urb->context;
316 /* the PCAN-USB needs time to restart */
317 mod_timer(&pdev->restart_timer,
318 jiffies + msecs_to_jiffies(PCAN_USB_STARTUP_TIMEOUT));
320 /* can delete usb resources */
321 peak_usb_async_complete(urb);
325 * handle asynchronous restart
327 static int pcan_usb_restart_async(struct peak_usb_device *dev, struct urb *urb,
330 struct pcan_usb *pdev = container_of(dev, struct pcan_usb, dev);
332 if (timer_pending(&pdev->restart_timer))
336 buf[PCAN_USB_CMD_FUNC] = 3;
337 buf[PCAN_USB_CMD_NUM] = 2;
338 buf[PCAN_USB_CMD_ARGS] = 1;
340 usb_fill_bulk_urb(urb, dev->udev,
341 usb_sndbulkpipe(dev->udev, PCAN_USB_EP_CMDOUT),
342 buf, PCAN_USB_CMD_LEN,
343 pcan_usb_restart_pending, pdev);
345 return usb_submit_urb(urb, GFP_ATOMIC);
349 * read serial number from device
351 static int pcan_usb_get_serial(struct peak_usb_device *dev, u32 *serial_number)
353 u8 args[PCAN_USB_CMD_ARGS_LEN];
356 err = pcan_usb_wait_rsp(dev, PCAN_USB_CMD_SN, PCAN_USB_GET, args);
358 netdev_err(dev->netdev, "getting serial failure: %d\n", err);
359 } else if (serial_number) {
362 memcpy(&tmp32, args, 4);
363 *serial_number = le32_to_cpu(tmp32);
370 * read device id from device
372 static int pcan_usb_get_device_id(struct peak_usb_device *dev, u32 *device_id)
374 u8 args[PCAN_USB_CMD_ARGS_LEN];
377 err = pcan_usb_wait_rsp(dev, PCAN_USB_CMD_DEVID, PCAN_USB_GET, args);
379 netdev_err(dev->netdev, "getting device id failure: %d\n", err);
381 *device_id = args[0];
387 * update current time ref with received timestamp
389 static int pcan_usb_update_ts(struct pcan_usb_msg_context *mc)
393 if ((mc->ptr+2) > mc->end)
396 memcpy(&tmp16, mc->ptr, 2);
398 mc->ts16 = le16_to_cpu(tmp16);
401 peak_usb_update_ts_now(&mc->pdev->time_ref, mc->ts16);
403 peak_usb_set_ts_now(&mc->pdev->time_ref, mc->ts16);
409 * decode received timestamp
411 static int pcan_usb_decode_ts(struct pcan_usb_msg_context *mc, u8 first_packet)
413 /* only 1st packet supplies a word timestamp */
417 if ((mc->ptr + 2) > mc->end)
420 memcpy(&tmp16, mc->ptr, 2);
423 mc->ts16 = le16_to_cpu(tmp16);
424 mc->prev_ts8 = mc->ts16 & 0x00ff;
428 if ((mc->ptr + 1) > mc->end)
433 if (ts8 < mc->prev_ts8)
444 static int pcan_usb_decode_error(struct pcan_usb_msg_context *mc, u8 n,
448 struct can_frame *cf;
449 enum can_state new_state;
451 /* ignore this error until 1st ts received */
452 if (n == PCAN_USB_ERROR_QOVR)
453 if (!mc->pdev->time_ref.tick_count)
456 new_state = mc->pdev->dev.can.state;
458 switch (mc->pdev->dev.can.state) {
459 case CAN_STATE_ERROR_ACTIVE:
460 if (n & PCAN_USB_ERROR_BUS_LIGHT) {
461 new_state = CAN_STATE_ERROR_WARNING;
466 case CAN_STATE_ERROR_WARNING:
467 if (n & PCAN_USB_ERROR_BUS_HEAVY) {
468 new_state = CAN_STATE_ERROR_PASSIVE;
471 if (n & PCAN_USB_ERROR_BUS_OFF) {
472 new_state = CAN_STATE_BUS_OFF;
475 if (n & ~PCAN_USB_ERROR_BUS) {
477 * trick to bypass next comparison and process other
480 new_state = CAN_STATE_MAX;
483 if ((n & PCAN_USB_ERROR_BUS_LIGHT) == 0) {
484 /* no error (back to active state) */
485 new_state = CAN_STATE_ERROR_ACTIVE;
490 case CAN_STATE_ERROR_PASSIVE:
491 if (n & PCAN_USB_ERROR_BUS_OFF) {
492 new_state = CAN_STATE_BUS_OFF;
495 if (n & PCAN_USB_ERROR_BUS_LIGHT) {
496 new_state = CAN_STATE_ERROR_WARNING;
499 if (n & ~PCAN_USB_ERROR_BUS) {
501 * trick to bypass next comparison and process other
504 new_state = CAN_STATE_MAX;
508 if ((n & PCAN_USB_ERROR_BUS_HEAVY) == 0) {
509 /* no error (back to warning state) */
510 new_state = CAN_STATE_ERROR_WARNING;
516 /* do nothing waiting for restart */
520 /* donot post any error if current state didn't change */
521 if (mc->pdev->dev.can.state == new_state)
524 /* allocate an skb to store the error frame */
525 skb = alloc_can_err_skb(mc->netdev, &cf);
530 case CAN_STATE_BUS_OFF:
531 cf->can_id |= CAN_ERR_BUSOFF;
532 mc->pdev->dev.can.can_stats.bus_off++;
533 can_bus_off(mc->netdev);
536 case CAN_STATE_ERROR_PASSIVE:
537 cf->can_id |= CAN_ERR_CRTL;
538 cf->data[1] = (mc->pdev->bec.txerr > mc->pdev->bec.rxerr) ?
539 CAN_ERR_CRTL_TX_PASSIVE :
540 CAN_ERR_CRTL_RX_PASSIVE;
541 cf->data[6] = mc->pdev->bec.txerr;
542 cf->data[7] = mc->pdev->bec.rxerr;
544 mc->pdev->dev.can.can_stats.error_passive++;
547 case CAN_STATE_ERROR_WARNING:
548 cf->can_id |= CAN_ERR_CRTL;
549 cf->data[1] = (mc->pdev->bec.txerr > mc->pdev->bec.rxerr) ?
550 CAN_ERR_CRTL_TX_WARNING :
551 CAN_ERR_CRTL_RX_WARNING;
552 cf->data[6] = mc->pdev->bec.txerr;
553 cf->data[7] = mc->pdev->bec.rxerr;
555 mc->pdev->dev.can.can_stats.error_warning++;
558 case CAN_STATE_ERROR_ACTIVE:
559 cf->can_id |= CAN_ERR_CRTL;
560 cf->data[1] = CAN_ERR_CRTL_ACTIVE;
562 /* sync local copies of rxerr/txerr counters */
563 mc->pdev->bec.txerr = 0;
564 mc->pdev->bec.rxerr = 0;
568 /* CAN_STATE_MAX (trick to handle other errors) */
569 if (n & PCAN_USB_ERROR_TXQFULL)
570 netdev_dbg(mc->netdev, "device Tx queue full)\n");
572 if (n & PCAN_USB_ERROR_RXQOVR) {
573 netdev_dbg(mc->netdev, "data overrun interrupt\n");
574 cf->can_id |= CAN_ERR_CRTL;
575 cf->data[1] |= CAN_ERR_CRTL_RX_OVERFLOW;
576 mc->netdev->stats.rx_over_errors++;
577 mc->netdev->stats.rx_errors++;
580 cf->data[6] = mc->pdev->bec.txerr;
581 cf->data[7] = mc->pdev->bec.rxerr;
583 new_state = mc->pdev->dev.can.state;
587 mc->pdev->dev.can.state = new_state;
589 if (status_len & PCAN_USB_STATUSLEN_TIMESTAMP) {
590 struct skb_shared_hwtstamps *hwts = skb_hwtstamps(skb);
592 peak_usb_get_ts_time(&mc->pdev->time_ref, mc->ts16,
596 mc->netdev->stats.rx_packets++;
597 mc->netdev->stats.rx_bytes += cf->len;
603 /* decode bus event usb packet: first byte contains rxerr while 2nd one contains
606 static int pcan_usb_handle_bus_evt(struct pcan_usb_msg_context *mc, u8 ir)
608 struct pcan_usb *pdev = mc->pdev;
610 /* acccording to the content of the packet */
612 case PCAN_USB_ERR_CNT:
614 /* save rx/tx error counters from in the device context */
615 pdev->bec.rxerr = mc->ptr[0];
616 pdev->bec.txerr = mc->ptr[1];
628 * decode non-data usb message
630 static int pcan_usb_decode_status(struct pcan_usb_msg_context *mc,
633 u8 rec_len = status_len & PCAN_USB_STATUSLEN_DLC;
637 /* check whether function and number can be read */
638 if ((mc->ptr + 2) > mc->end)
641 f = mc->ptr[PCAN_USB_CMD_FUNC];
642 n = mc->ptr[PCAN_USB_CMD_NUM];
643 mc->ptr += PCAN_USB_CMD_ARGS;
645 if (status_len & PCAN_USB_STATUSLEN_TIMESTAMP) {
646 int err = pcan_usb_decode_ts(mc, !mc->rec_ts_idx);
651 /* Next packet in the buffer will have a timestamp on a single
658 case PCAN_USB_REC_ERROR:
659 err = pcan_usb_decode_error(mc, n, status_len);
664 case PCAN_USB_REC_ANALOG:
665 /* analog values (ignored) */
669 case PCAN_USB_REC_BUSLOAD:
670 /* bus load (ignored) */
674 case PCAN_USB_REC_TS:
676 if (pcan_usb_update_ts(mc))
680 case PCAN_USB_REC_BUSEVT:
681 /* bus event notifications (get rxerr/txerr) */
682 err = pcan_usb_handle_bus_evt(mc, n);
687 netdev_err(mc->netdev, "unexpected function %u\n", f);
691 if ((mc->ptr + rec_len) > mc->end)
700 * decode data usb message
702 static int pcan_usb_decode_data(struct pcan_usb_msg_context *mc, u8 status_len)
704 u8 rec_len = status_len & PCAN_USB_STATUSLEN_DLC;
706 struct can_frame *cf;
707 struct skb_shared_hwtstamps *hwts;
709 skb = alloc_can_skb(mc->netdev, &cf);
713 if (status_len & PCAN_USB_STATUSLEN_EXT_ID) {
716 if ((mc->ptr + 4) > mc->end)
719 memcpy(&tmp32, mc->ptr, 4);
722 cf->can_id = (le32_to_cpu(tmp32) >> 3) | CAN_EFF_FLAG;
726 if ((mc->ptr + 2) > mc->end)
729 memcpy(&tmp16, mc->ptr, 2);
732 cf->can_id = le16_to_cpu(tmp16) >> 5;
735 can_frame_set_cc_len(cf, rec_len, mc->pdev->dev.can.ctrlmode);
737 /* Only first packet timestamp is a word */
738 if (pcan_usb_decode_ts(mc, !mc->rec_ts_idx))
741 /* Next packet in the buffer will have a timestamp on a single byte */
745 memset(cf->data, 0x0, sizeof(cf->data));
746 if (status_len & PCAN_USB_STATUSLEN_RTR) {
747 cf->can_id |= CAN_RTR_FLAG;
749 if ((mc->ptr + rec_len) > mc->end)
752 memcpy(cf->data, mc->ptr, cf->len);
756 /* convert timestamp into kernel time */
757 hwts = skb_hwtstamps(skb);
758 peak_usb_get_ts_time(&mc->pdev->time_ref, mc->ts16, &hwts->hwtstamp);
760 /* update statistics */
761 mc->netdev->stats.rx_packets++;
762 mc->netdev->stats.rx_bytes += cf->len;
774 * process incoming message
776 static int pcan_usb_decode_msg(struct peak_usb_device *dev, u8 *ibuf, u32 lbuf)
778 struct pcan_usb_msg_context mc = {
780 .ptr = ibuf + PCAN_USB_MSG_HEADER_LEN,
782 .netdev = dev->netdev,
783 .pdev = container_of(dev, struct pcan_usb, dev),
787 for (err = 0; mc.rec_idx < mc.rec_cnt && !err; mc.rec_idx++) {
790 /* handle status and error frames here */
791 if (sl & PCAN_USB_STATUSLEN_INTERNAL) {
792 err = pcan_usb_decode_status(&mc, sl);
793 /* handle normal can frames here */
795 err = pcan_usb_decode_data(&mc, sl);
803 * process any incoming buffer
805 static int pcan_usb_decode_buf(struct peak_usb_device *dev, struct urb *urb)
809 if (urb->actual_length > PCAN_USB_MSG_HEADER_LEN) {
810 err = pcan_usb_decode_msg(dev, urb->transfer_buffer,
813 } else if (urb->actual_length > 0) {
814 netdev_err(dev->netdev, "usb message length error (%u)\n",
823 * process outgoing packet
825 static int pcan_usb_encode_msg(struct peak_usb_device *dev, struct sk_buff *skb,
826 u8 *obuf, size_t *size)
828 struct net_device *netdev = dev->netdev;
829 struct net_device_stats *stats = &netdev->stats;
830 struct can_frame *cf = (struct can_frame *)skb->data;
836 pc = obuf + PCAN_USB_MSG_HEADER_LEN;
838 /* status/len byte */
839 *pc = can_get_cc_dlc(cf, dev->can.ctrlmode);
841 if (cf->can_id & CAN_RTR_FLAG)
842 *pc |= PCAN_USB_STATUSLEN_RTR;
845 if (cf->can_id & CAN_EFF_FLAG) {
846 __le32 tmp32 = cpu_to_le32((cf->can_id & CAN_ERR_MASK) << 3);
848 *pc |= PCAN_USB_STATUSLEN_EXT_ID;
849 memcpy(++pc, &tmp32, 4);
852 __le16 tmp16 = cpu_to_le16((cf->can_id & CAN_ERR_MASK) << 5);
854 memcpy(++pc, &tmp16, 2);
859 if (!(cf->can_id & CAN_RTR_FLAG)) {
860 memcpy(pc, cf->data, cf->len);
864 obuf[(*size)-1] = (u8)(stats->tx_packets & 0xff);
869 /* socket callback used to copy berr counters values received through USB */
870 static int pcan_usb_get_berr_counter(const struct net_device *netdev,
871 struct can_berr_counter *bec)
873 struct peak_usb_device *dev = netdev_priv(netdev);
874 struct pcan_usb *pdev = container_of(dev, struct pcan_usb, dev);
885 static int pcan_usb_start(struct peak_usb_device *dev)
887 struct pcan_usb *pdev = container_of(dev, struct pcan_usb, dev);
890 /* number of bits used in timestamps read from adapter struct */
891 peak_usb_init_time_ref(&pdev->time_ref, &pcan_usb);
896 /* be notified on error counter changes (if requested by user) */
897 if (dev->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) {
898 err = pcan_usb_set_err_frame(dev, PCAN_USB_BERR_MASK);
900 netdev_warn(dev->netdev,
901 "Asking for BERR reporting error %u\n",
905 /* if revision greater than 3, can put silent mode on/off */
906 if (dev->device_rev > 3) {
907 err = pcan_usb_set_silent(dev,
908 dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY);
913 return pcan_usb_set_ext_vcc(dev, 0);
916 static int pcan_usb_init(struct peak_usb_device *dev)
918 struct pcan_usb *pdev = container_of(dev, struct pcan_usb, dev);
922 /* initialize a timer needed to wait for hardware restart */
923 timer_setup(&pdev->restart_timer, pcan_usb_restart, 0);
926 * explicit use of dev_xxx() instead of netdev_xxx() here:
927 * information displayed are related to the device itself, not
928 * to the canx netdevice.
930 err = pcan_usb_get_serial(dev, &serial_number);
932 dev_err(dev->netdev->dev.parent,
933 "unable to read %s serial number (err %d)\n",
938 dev_info(dev->netdev->dev.parent,
939 "PEAK-System %s adapter hwrev %u serial %08X (%u channel)\n",
940 pcan_usb.name, dev->device_rev, serial_number,
941 pcan_usb.ctrl_count);
947 * probe function for new PCAN-USB usb interface
949 static int pcan_usb_probe(struct usb_interface *intf)
951 struct usb_host_interface *if_desc;
954 if_desc = intf->altsetting;
956 /* check interface endpoint addresses */
957 for (i = 0; i < if_desc->desc.bNumEndpoints; i++) {
958 struct usb_endpoint_descriptor *ep = &if_desc->endpoint[i].desc;
960 switch (ep->bEndpointAddress) {
961 case PCAN_USB_EP_CMDOUT:
962 case PCAN_USB_EP_CMDIN:
963 case PCAN_USB_EP_MSGOUT:
964 case PCAN_USB_EP_MSGIN:
975 * describe the PCAN-USB adapter
977 static const struct can_bittiming_const pcan_usb_const = {
989 const struct peak_usb_adapter pcan_usb = {
991 .device_id = PCAN_USB_PRODUCT_ID,
993 .ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY |
994 CAN_CTRLMODE_BERR_REPORTING |
995 CAN_CTRLMODE_CC_LEN8_DLC,
997 .freq = PCAN_USB_CRYSTAL_HZ / 2 ,
999 .bittiming_const = &pcan_usb_const,
1001 /* size of device private data */
1002 .sizeof_dev_private = sizeof(struct pcan_usb),
1004 /* timestamps usage */
1006 .ts_period = 24575, /* calibration period in ts. */
1007 .us_per_ts_scale = PCAN_USB_TS_US_PER_TICK, /* us=(ts*scale) */
1008 .us_per_ts_shift = PCAN_USB_TS_DIV_SHIFTER, /* >> shift */
1010 /* give here messages in/out endpoints */
1011 .ep_msg_in = PCAN_USB_EP_MSGIN,
1012 .ep_msg_out = {PCAN_USB_EP_MSGOUT},
1014 /* size of rx/tx usb buffers */
1015 .rx_buffer_size = PCAN_USB_RX_BUFFER_SIZE,
1016 .tx_buffer_size = PCAN_USB_TX_BUFFER_SIZE,
1018 /* device callbacks */
1019 .intf_probe = pcan_usb_probe,
1020 .dev_init = pcan_usb_init,
1021 .dev_set_bus = pcan_usb_write_mode,
1022 .dev_set_bittiming = pcan_usb_set_bittiming,
1023 .dev_get_device_id = pcan_usb_get_device_id,
1024 .dev_decode_buf = pcan_usb_decode_buf,
1025 .dev_encode_msg = pcan_usb_encode_msg,
1026 .dev_start = pcan_usb_start,
1027 .dev_restart_async = pcan_usb_restart_async,
1028 .do_get_berr_counter = pcan_usb_get_berr_counter,