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 MODULE_SUPPORTED_DEVICE("PEAK-System PCAN-USB adapter");
23 /* PCAN-USB Endpoints */
24 #define PCAN_USB_EP_CMDOUT 1
25 #define PCAN_USB_EP_CMDIN (PCAN_USB_EP_CMDOUT | USB_DIR_IN)
26 #define PCAN_USB_EP_MSGOUT 2
27 #define PCAN_USB_EP_MSGIN (PCAN_USB_EP_MSGOUT | USB_DIR_IN)
29 /* PCAN-USB command struct */
30 #define PCAN_USB_CMD_FUNC 0
31 #define PCAN_USB_CMD_NUM 1
32 #define PCAN_USB_CMD_ARGS 2
33 #define PCAN_USB_CMD_ARGS_LEN 14
34 #define PCAN_USB_CMD_LEN (PCAN_USB_CMD_ARGS + \
35 PCAN_USB_CMD_ARGS_LEN)
37 /* PCAN-USB commands */
38 #define PCAN_USB_CMD_BITRATE 1
39 #define PCAN_USB_CMD_SET_BUS 3
40 #define PCAN_USB_CMD_DEVID 4
41 #define PCAN_USB_CMD_SN 6
42 #define PCAN_USB_CMD_REGISTER 9
43 #define PCAN_USB_CMD_EXT_VCC 10
44 #define PCAN_USB_CMD_ERR_FR 11
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
);
254 * set bittiming value to can
256 static int pcan_usb_set_bittiming(struct peak_usb_device
*dev
,
257 struct can_bittiming
*bt
)
259 u8 args
[PCAN_USB_CMD_ARGS_LEN
];
262 btr0
= ((bt
->brp
- 1) & 0x3f) | (((bt
->sjw
- 1) & 0x3) << 6);
263 btr1
= ((bt
->prop_seg
+ bt
->phase_seg1
- 1) & 0xf) |
264 (((bt
->phase_seg2
- 1) & 0x7) << 4);
265 if (dev
->can
.ctrlmode
& CAN_CTRLMODE_3_SAMPLES
)
268 netdev_info(dev
->netdev
, "setting BTR0=0x%02x BTR1=0x%02x\n",
274 return pcan_usb_send_cmd(dev
, PCAN_USB_CMD_BITRATE
, PCAN_USB_SET
, args
);
280 static int pcan_usb_write_mode(struct peak_usb_device
*dev
, u8 onoff
)
284 err
= pcan_usb_set_bus(dev
, onoff
);
289 err
= pcan_usb_set_sja1000(dev
, SJA1000_MODE_INIT
);
291 /* the PCAN-USB needs time to init */
292 set_current_state(TASK_INTERRUPTIBLE
);
293 schedule_timeout(msecs_to_jiffies(PCAN_USB_STARTUP_TIMEOUT
));
300 * handle end of waiting for the device to reset
302 static void pcan_usb_restart(struct timer_list
*t
)
304 struct pcan_usb
*pdev
= from_timer(pdev
, t
, restart_timer
);
305 struct peak_usb_device
*dev
= &pdev
->dev
;
307 /* notify candev and netdev */
308 peak_usb_restart_complete(dev
);
312 * handle the submission of the restart urb
314 static void pcan_usb_restart_pending(struct urb
*urb
)
316 struct pcan_usb
*pdev
= urb
->context
;
318 /* the PCAN-USB needs time to restart */
319 mod_timer(&pdev
->restart_timer
,
320 jiffies
+ msecs_to_jiffies(PCAN_USB_STARTUP_TIMEOUT
));
322 /* can delete usb resources */
323 peak_usb_async_complete(urb
);
327 * handle asynchronous restart
329 static int pcan_usb_restart_async(struct peak_usb_device
*dev
, struct urb
*urb
,
332 struct pcan_usb
*pdev
= container_of(dev
, struct pcan_usb
, dev
);
334 if (timer_pending(&pdev
->restart_timer
))
338 buf
[PCAN_USB_CMD_FUNC
] = 3;
339 buf
[PCAN_USB_CMD_NUM
] = 2;
340 buf
[PCAN_USB_CMD_ARGS
] = 1;
342 usb_fill_bulk_urb(urb
, dev
->udev
,
343 usb_sndbulkpipe(dev
->udev
, PCAN_USB_EP_CMDOUT
),
344 buf
, PCAN_USB_CMD_LEN
,
345 pcan_usb_restart_pending
, pdev
);
347 return usb_submit_urb(urb
, GFP_ATOMIC
);
351 * read serial number from device
353 static int pcan_usb_get_serial(struct peak_usb_device
*dev
, u32
*serial_number
)
355 u8 args
[PCAN_USB_CMD_ARGS_LEN
];
358 err
= pcan_usb_wait_rsp(dev
, PCAN_USB_CMD_SN
, PCAN_USB_GET
, args
);
360 netdev_err(dev
->netdev
, "getting serial failure: %d\n", err
);
361 } else if (serial_number
) {
364 memcpy(&tmp32
, args
, 4);
365 *serial_number
= le32_to_cpu(tmp32
);
372 * read device id from device
374 static int pcan_usb_get_device_id(struct peak_usb_device
*dev
, u32
*device_id
)
376 u8 args
[PCAN_USB_CMD_ARGS_LEN
];
379 err
= pcan_usb_wait_rsp(dev
, PCAN_USB_CMD_DEVID
, PCAN_USB_GET
, args
);
381 netdev_err(dev
->netdev
, "getting device id failure: %d\n", err
);
383 *device_id
= args
[0];
389 * update current time ref with received timestamp
391 static int pcan_usb_update_ts(struct pcan_usb_msg_context
*mc
)
395 if ((mc
->ptr
+2) > mc
->end
)
398 memcpy(&tmp16
, mc
->ptr
, 2);
400 mc
->ts16
= le16_to_cpu(tmp16
);
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 */
419 if ((mc
->ptr
+ 2) > mc
->end
)
422 memcpy(&tmp16
, mc
->ptr
, 2);
425 mc
->ts16
= le16_to_cpu(tmp16
);
426 mc
->prev_ts8
= mc
->ts16
& 0x00ff;
430 if ((mc
->ptr
+ 1) > mc
->end
)
435 if (ts8
< mc
->prev_ts8
)
446 static int pcan_usb_decode_error(struct pcan_usb_msg_context
*mc
, u8 n
,
450 struct can_frame
*cf
;
451 enum can_state new_state
;
453 /* ignore this error until 1st ts received */
454 if (n
== PCAN_USB_ERROR_QOVR
)
455 if (!mc
->pdev
->time_ref
.tick_count
)
458 new_state
= mc
->pdev
->dev
.can
.state
;
460 switch (mc
->pdev
->dev
.can
.state
) {
461 case CAN_STATE_ERROR_ACTIVE
:
462 if (n
& PCAN_USB_ERROR_BUS_LIGHT
) {
463 new_state
= CAN_STATE_ERROR_WARNING
;
468 case CAN_STATE_ERROR_WARNING
:
469 if (n
& PCAN_USB_ERROR_BUS_HEAVY
) {
470 new_state
= CAN_STATE_ERROR_PASSIVE
;
473 if (n
& PCAN_USB_ERROR_BUS_OFF
) {
474 new_state
= CAN_STATE_BUS_OFF
;
477 if (n
& ~PCAN_USB_ERROR_BUS
) {
479 * trick to bypass next comparison and process other
482 new_state
= CAN_STATE_MAX
;
485 if ((n
& PCAN_USB_ERROR_BUS_LIGHT
) == 0) {
486 /* no error (back to active state) */
487 new_state
= CAN_STATE_ERROR_ACTIVE
;
492 case CAN_STATE_ERROR_PASSIVE
:
493 if (n
& PCAN_USB_ERROR_BUS_OFF
) {
494 new_state
= CAN_STATE_BUS_OFF
;
497 if (n
& PCAN_USB_ERROR_BUS_LIGHT
) {
498 new_state
= CAN_STATE_ERROR_WARNING
;
501 if (n
& ~PCAN_USB_ERROR_BUS
) {
503 * trick to bypass next comparison and process other
506 new_state
= CAN_STATE_MAX
;
510 if ((n
& PCAN_USB_ERROR_BUS_HEAVY
) == 0) {
511 /* no error (back to warning state) */
512 new_state
= CAN_STATE_ERROR_WARNING
;
518 /* do nothing waiting for restart */
522 /* donot post any error if current state didn't change */
523 if (mc
->pdev
->dev
.can
.state
== new_state
)
526 /* allocate an skb to store the error frame */
527 skb
= alloc_can_err_skb(mc
->netdev
, &cf
);
532 case CAN_STATE_BUS_OFF
:
533 cf
->can_id
|= CAN_ERR_BUSOFF
;
534 mc
->pdev
->dev
.can
.can_stats
.bus_off
++;
535 can_bus_off(mc
->netdev
);
538 case CAN_STATE_ERROR_PASSIVE
:
539 cf
->can_id
|= CAN_ERR_CRTL
;
540 cf
->data
[1] = (mc
->pdev
->bec
.txerr
> mc
->pdev
->bec
.rxerr
) ?
541 CAN_ERR_CRTL_TX_PASSIVE
:
542 CAN_ERR_CRTL_RX_PASSIVE
;
543 cf
->data
[6] = mc
->pdev
->bec
.txerr
;
544 cf
->data
[7] = mc
->pdev
->bec
.rxerr
;
546 mc
->pdev
->dev
.can
.can_stats
.error_passive
++;
549 case CAN_STATE_ERROR_WARNING
:
550 cf
->can_id
|= CAN_ERR_CRTL
;
551 cf
->data
[1] = (mc
->pdev
->bec
.txerr
> mc
->pdev
->bec
.rxerr
) ?
552 CAN_ERR_CRTL_TX_WARNING
:
553 CAN_ERR_CRTL_RX_WARNING
;
554 cf
->data
[6] = mc
->pdev
->bec
.txerr
;
555 cf
->data
[7] = mc
->pdev
->bec
.rxerr
;
557 mc
->pdev
->dev
.can
.can_stats
.error_warning
++;
560 case CAN_STATE_ERROR_ACTIVE
:
561 cf
->can_id
|= CAN_ERR_CRTL
;
562 cf
->data
[1] = CAN_ERR_CRTL_ACTIVE
;
564 /* sync local copies of rxerr/txerr counters */
565 mc
->pdev
->bec
.txerr
= 0;
566 mc
->pdev
->bec
.rxerr
= 0;
570 /* CAN_STATE_MAX (trick to handle other errors) */
571 if (n
& PCAN_USB_ERROR_TXQFULL
)
572 netdev_dbg(mc
->netdev
, "device Tx queue full)\n");
574 if (n
& PCAN_USB_ERROR_RXQOVR
) {
575 netdev_dbg(mc
->netdev
, "data overrun interrupt\n");
576 cf
->can_id
|= CAN_ERR_CRTL
;
577 cf
->data
[1] |= CAN_ERR_CRTL_RX_OVERFLOW
;
578 mc
->netdev
->stats
.rx_over_errors
++;
579 mc
->netdev
->stats
.rx_errors
++;
582 cf
->data
[6] = mc
->pdev
->bec
.txerr
;
583 cf
->data
[7] = mc
->pdev
->bec
.rxerr
;
585 new_state
= mc
->pdev
->dev
.can
.state
;
589 mc
->pdev
->dev
.can
.state
= new_state
;
591 if (status_len
& PCAN_USB_STATUSLEN_TIMESTAMP
) {
592 struct skb_shared_hwtstamps
*hwts
= skb_hwtstamps(skb
);
594 peak_usb_get_ts_time(&mc
->pdev
->time_ref
, mc
->ts16
,
598 mc
->netdev
->stats
.rx_packets
++;
599 mc
->netdev
->stats
.rx_bytes
+= cf
->len
;
605 /* decode bus event usb packet: first byte contains rxerr while 2nd one contains
608 static int pcan_usb_handle_bus_evt(struct pcan_usb_msg_context
*mc
, u8 ir
)
610 struct pcan_usb
*pdev
= mc
->pdev
;
612 /* acccording to the content of the packet */
614 case PCAN_USB_ERR_CNT
:
616 /* save rx/tx error counters from in the device context */
617 pdev
->bec
.rxerr
= mc
->ptr
[0];
618 pdev
->bec
.txerr
= mc
->ptr
[1];
630 * decode non-data usb message
632 static int pcan_usb_decode_status(struct pcan_usb_msg_context
*mc
,
635 u8 rec_len
= status_len
& PCAN_USB_STATUSLEN_DLC
;
639 /* check whether function and number can be read */
640 if ((mc
->ptr
+ 2) > mc
->end
)
643 f
= mc
->ptr
[PCAN_USB_CMD_FUNC
];
644 n
= mc
->ptr
[PCAN_USB_CMD_NUM
];
645 mc
->ptr
+= PCAN_USB_CMD_ARGS
;
647 if (status_len
& PCAN_USB_STATUSLEN_TIMESTAMP
) {
648 int err
= pcan_usb_decode_ts(mc
, !mc
->rec_ts_idx
);
653 /* Next packet in the buffer will have a timestamp on a single
660 case PCAN_USB_REC_ERROR
:
661 err
= pcan_usb_decode_error(mc
, n
, status_len
);
666 case PCAN_USB_REC_ANALOG
:
667 /* analog values (ignored) */
671 case PCAN_USB_REC_BUSLOAD
:
672 /* bus load (ignored) */
676 case PCAN_USB_REC_TS
:
678 if (pcan_usb_update_ts(mc
))
682 case PCAN_USB_REC_BUSEVT
:
683 /* bus event notifications (get rxerr/txerr) */
684 err
= pcan_usb_handle_bus_evt(mc
, n
);
689 netdev_err(mc
->netdev
, "unexpected function %u\n", f
);
693 if ((mc
->ptr
+ rec_len
) > mc
->end
)
702 * decode data usb message
704 static int pcan_usb_decode_data(struct pcan_usb_msg_context
*mc
, u8 status_len
)
706 u8 rec_len
= status_len
& PCAN_USB_STATUSLEN_DLC
;
708 struct can_frame
*cf
;
709 struct skb_shared_hwtstamps
*hwts
;
711 skb
= alloc_can_skb(mc
->netdev
, &cf
);
715 if (status_len
& PCAN_USB_STATUSLEN_EXT_ID
) {
718 if ((mc
->ptr
+ 4) > mc
->end
)
721 memcpy(&tmp32
, mc
->ptr
, 4);
724 cf
->can_id
= (le32_to_cpu(tmp32
) >> 3) | CAN_EFF_FLAG
;
728 if ((mc
->ptr
+ 2) > mc
->end
)
731 memcpy(&tmp16
, mc
->ptr
, 2);
734 cf
->can_id
= le16_to_cpu(tmp16
) >> 5;
737 can_frame_set_cc_len(cf
, rec_len
, mc
->pdev
->dev
.can
.ctrlmode
);
739 /* Only first packet timestamp is a word */
740 if (pcan_usb_decode_ts(mc
, !mc
->rec_ts_idx
))
743 /* Next packet in the buffer will have a timestamp on a single byte */
747 memset(cf
->data
, 0x0, sizeof(cf
->data
));
748 if (status_len
& PCAN_USB_STATUSLEN_RTR
) {
749 cf
->can_id
|= CAN_RTR_FLAG
;
751 if ((mc
->ptr
+ rec_len
) > mc
->end
)
754 memcpy(cf
->data
, mc
->ptr
, cf
->len
);
758 /* convert timestamp into kernel time */
759 hwts
= skb_hwtstamps(skb
);
760 peak_usb_get_ts_time(&mc
->pdev
->time_ref
, mc
->ts16
, &hwts
->hwtstamp
);
762 /* update statistics */
763 mc
->netdev
->stats
.rx_packets
++;
764 mc
->netdev
->stats
.rx_bytes
+= cf
->len
;
776 * process incoming message
778 static int pcan_usb_decode_msg(struct peak_usb_device
*dev
, u8
*ibuf
, u32 lbuf
)
780 struct pcan_usb_msg_context mc
= {
782 .ptr
= ibuf
+ PCAN_USB_MSG_HEADER_LEN
,
784 .netdev
= dev
->netdev
,
785 .pdev
= container_of(dev
, struct pcan_usb
, dev
),
789 for (err
= 0; mc
.rec_idx
< mc
.rec_cnt
&& !err
; mc
.rec_idx
++) {
792 /* handle status and error frames here */
793 if (sl
& PCAN_USB_STATUSLEN_INTERNAL
) {
794 err
= pcan_usb_decode_status(&mc
, sl
);
795 /* handle normal can frames here */
797 err
= pcan_usb_decode_data(&mc
, sl
);
805 * process any incoming buffer
807 static int pcan_usb_decode_buf(struct peak_usb_device
*dev
, struct urb
*urb
)
811 if (urb
->actual_length
> PCAN_USB_MSG_HEADER_LEN
) {
812 err
= pcan_usb_decode_msg(dev
, urb
->transfer_buffer
,
815 } else if (urb
->actual_length
> 0) {
816 netdev_err(dev
->netdev
, "usb message length error (%u)\n",
825 * process outgoing packet
827 static int pcan_usb_encode_msg(struct peak_usb_device
*dev
, struct sk_buff
*skb
,
828 u8
*obuf
, size_t *size
)
830 struct net_device
*netdev
= dev
->netdev
;
831 struct net_device_stats
*stats
= &netdev
->stats
;
832 struct can_frame
*cf
= (struct can_frame
*)skb
->data
;
838 pc
= obuf
+ PCAN_USB_MSG_HEADER_LEN
;
840 /* status/len byte */
841 *pc
= can_get_cc_dlc(cf
, dev
->can
.ctrlmode
);
843 if (cf
->can_id
& CAN_RTR_FLAG
)
844 *pc
|= PCAN_USB_STATUSLEN_RTR
;
847 if (cf
->can_id
& CAN_EFF_FLAG
) {
848 __le32 tmp32
= cpu_to_le32((cf
->can_id
& CAN_ERR_MASK
) << 3);
850 *pc
|= PCAN_USB_STATUSLEN_EXT_ID
;
851 memcpy(++pc
, &tmp32
, 4);
854 __le16 tmp16
= cpu_to_le16((cf
->can_id
& CAN_ERR_MASK
) << 5);
856 memcpy(++pc
, &tmp16
, 2);
861 if (!(cf
->can_id
& CAN_RTR_FLAG
)) {
862 memcpy(pc
, cf
->data
, cf
->len
);
866 obuf
[(*size
)-1] = (u8
)(stats
->tx_packets
& 0xff);
871 /* socket callback used to copy berr counters values received through USB */
872 static int pcan_usb_get_berr_counter(const struct net_device
*netdev
,
873 struct can_berr_counter
*bec
)
875 struct peak_usb_device
*dev
= netdev_priv(netdev
);
876 struct pcan_usb
*pdev
= container_of(dev
, struct pcan_usb
, dev
);
887 static int pcan_usb_start(struct peak_usb_device
*dev
)
889 struct pcan_usb
*pdev
= container_of(dev
, struct pcan_usb
, dev
);
892 /* number of bits used in timestamps read from adapter struct */
893 peak_usb_init_time_ref(&pdev
->time_ref
, &pcan_usb
);
898 /* be notified on error counter changes (if requested by user) */
899 if (dev
->can
.ctrlmode
& CAN_CTRLMODE_BERR_REPORTING
) {
900 err
= pcan_usb_set_err_frame(dev
, PCAN_USB_BERR_MASK
);
902 netdev_warn(dev
->netdev
,
903 "Asking for BERR reporting error %u\n",
907 /* if revision greater than 3, can put silent mode on/off */
908 if (dev
->device_rev
> 3) {
909 err
= pcan_usb_set_silent(dev
,
910 dev
->can
.ctrlmode
& CAN_CTRLMODE_LISTENONLY
);
915 return pcan_usb_set_ext_vcc(dev
, 0);
918 static int pcan_usb_init(struct peak_usb_device
*dev
)
920 struct pcan_usb
*pdev
= container_of(dev
, struct pcan_usb
, dev
);
924 /* initialize a timer needed to wait for hardware restart */
925 timer_setup(&pdev
->restart_timer
, pcan_usb_restart
, 0);
928 * explicit use of dev_xxx() instead of netdev_xxx() here:
929 * information displayed are related to the device itself, not
930 * to the canx netdevice.
932 err
= pcan_usb_get_serial(dev
, &serial_number
);
934 dev_err(dev
->netdev
->dev
.parent
,
935 "unable to read %s serial number (err %d)\n",
940 dev_info(dev
->netdev
->dev
.parent
,
941 "PEAK-System %s adapter hwrev %u serial %08X (%u channel)\n",
942 pcan_usb
.name
, dev
->device_rev
, serial_number
,
943 pcan_usb
.ctrl_count
);
949 * probe function for new PCAN-USB usb interface
951 static int pcan_usb_probe(struct usb_interface
*intf
)
953 struct usb_host_interface
*if_desc
;
956 if_desc
= intf
->altsetting
;
958 /* check interface endpoint addresses */
959 for (i
= 0; i
< if_desc
->desc
.bNumEndpoints
; i
++) {
960 struct usb_endpoint_descriptor
*ep
= &if_desc
->endpoint
[i
].desc
;
962 switch (ep
->bEndpointAddress
) {
963 case PCAN_USB_EP_CMDOUT
:
964 case PCAN_USB_EP_CMDIN
:
965 case PCAN_USB_EP_MSGOUT
:
966 case PCAN_USB_EP_MSGIN
:
977 * describe the PCAN-USB adapter
979 static const struct can_bittiming_const pcan_usb_const
= {
991 const struct peak_usb_adapter pcan_usb
= {
993 .device_id
= PCAN_USB_PRODUCT_ID
,
995 .ctrlmode_supported
= CAN_CTRLMODE_3_SAMPLES
| CAN_CTRLMODE_LISTENONLY
|
996 CAN_CTRLMODE_BERR_REPORTING
|
997 CAN_CTRLMODE_CC_LEN8_DLC
,
999 .freq
= PCAN_USB_CRYSTAL_HZ
/ 2 ,
1001 .bittiming_const
= &pcan_usb_const
,
1003 /* size of device private data */
1004 .sizeof_dev_private
= sizeof(struct pcan_usb
),
1006 /* timestamps usage */
1008 .ts_period
= 24575, /* calibration period in ts. */
1009 .us_per_ts_scale
= PCAN_USB_TS_US_PER_TICK
, /* us=(ts*scale) */
1010 .us_per_ts_shift
= PCAN_USB_TS_DIV_SHIFTER
, /* >> shift */
1012 /* give here messages in/out endpoints */
1013 .ep_msg_in
= PCAN_USB_EP_MSGIN
,
1014 .ep_msg_out
= {PCAN_USB_EP_MSGOUT
},
1016 /* size of rx/tx usb buffers */
1017 .rx_buffer_size
= PCAN_USB_RX_BUFFER_SIZE
,
1018 .tx_buffer_size
= PCAN_USB_TX_BUFFER_SIZE
,
1020 /* device callbacks */
1021 .intf_probe
= pcan_usb_probe
,
1022 .dev_init
= pcan_usb_init
,
1023 .dev_set_bus
= pcan_usb_write_mode
,
1024 .dev_set_bittiming
= pcan_usb_set_bittiming
,
1025 .dev_get_device_id
= pcan_usb_get_device_id
,
1026 .dev_decode_buf
= pcan_usb_decode_buf
,
1027 .dev_encode_msg
= pcan_usb_encode_msg
,
1028 .dev_start
= pcan_usb_start
,
1029 .dev_restart_async
= pcan_usb_restart_async
,
1030 .do_get_berr_counter
= pcan_usb_get_berr_counter
,