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 command timeout (ms.) */
38 #define PCAN_USB_COMMAND_TIMEOUT 1000
40 /* PCAN-USB startup timeout (ms.) */
41 #define PCAN_USB_STARTUP_TIMEOUT 10
43 /* PCAN-USB rx/tx buffers size */
44 #define PCAN_USB_RX_BUFFER_SIZE 64
45 #define PCAN_USB_TX_BUFFER_SIZE 64
47 #define PCAN_USB_MSG_HEADER_LEN 2
49 /* PCAN-USB adapter internal clock (MHz) */
50 #define PCAN_USB_CRYSTAL_HZ 16000000
52 /* PCAN-USB USB message record status/len field */
53 #define PCAN_USB_STATUSLEN_TIMESTAMP (1 << 7)
54 #define PCAN_USB_STATUSLEN_INTERNAL (1 << 6)
55 #define PCAN_USB_STATUSLEN_EXT_ID (1 << 5)
56 #define PCAN_USB_STATUSLEN_RTR (1 << 4)
57 #define PCAN_USB_STATUSLEN_DLC (0xf)
59 /* PCAN-USB error flags */
60 #define PCAN_USB_ERROR_TXFULL 0x01
61 #define PCAN_USB_ERROR_RXQOVR 0x02
62 #define PCAN_USB_ERROR_BUS_LIGHT 0x04
63 #define PCAN_USB_ERROR_BUS_HEAVY 0x08
64 #define PCAN_USB_ERROR_BUS_OFF 0x10
65 #define PCAN_USB_ERROR_RXQEMPTY 0x20
66 #define PCAN_USB_ERROR_QOVR 0x40
67 #define PCAN_USB_ERROR_TXQFULL 0x80
70 #define SJA1000_MODE_NORMAL 0x00
71 #define SJA1000_MODE_INIT 0x01
74 * tick duration = 42.666 us =>
75 * (tick_number * 44739243) >> 20 ~ (tick_number * 42666) / 1000
78 #define PCAN_USB_TS_DIV_SHIFTER 20
79 #define PCAN_USB_TS_US_PER_TICK 44739243
81 /* PCAN-USB messages record types */
82 #define PCAN_USB_REC_ERROR 1
83 #define PCAN_USB_REC_ANALOG 2
84 #define PCAN_USB_REC_BUSLOAD 3
85 #define PCAN_USB_REC_TS 4
86 #define PCAN_USB_REC_BUSEVT 5
88 /* private to PCAN-USB adapter */
90 struct peak_usb_device dev
;
91 struct peak_time_ref time_ref
;
92 struct timer_list restart_timer
;
95 /* incoming message context for decoding */
96 struct pcan_usb_msg_context
{
104 struct net_device
*netdev
;
105 struct pcan_usb
*pdev
;
111 static int pcan_usb_send_cmd(struct peak_usb_device
*dev
, u8 f
, u8 n
, u8
*p
)
116 /* usb device unregistered? */
117 if (!(dev
->state
& PCAN_USB_STATE_CONNECTED
))
120 dev
->cmd_buf
[PCAN_USB_CMD_FUNC
] = f
;
121 dev
->cmd_buf
[PCAN_USB_CMD_NUM
] = n
;
124 memcpy(dev
->cmd_buf
+ PCAN_USB_CMD_ARGS
,
125 p
, PCAN_USB_CMD_ARGS_LEN
);
127 err
= usb_bulk_msg(dev
->udev
,
128 usb_sndbulkpipe(dev
->udev
, PCAN_USB_EP_CMDOUT
),
129 dev
->cmd_buf
, PCAN_USB_CMD_LEN
, &actual_length
,
130 PCAN_USB_COMMAND_TIMEOUT
);
132 netdev_err(dev
->netdev
,
133 "sending cmd f=0x%x n=0x%x failure: %d\n",
139 * send a command then wait for its response
141 static int pcan_usb_wait_rsp(struct peak_usb_device
*dev
, u8 f
, u8 n
, u8
*p
)
146 /* usb device unregistered? */
147 if (!(dev
->state
& PCAN_USB_STATE_CONNECTED
))
150 /* first, send command */
151 err
= pcan_usb_send_cmd(dev
, f
, n
, NULL
);
155 err
= usb_bulk_msg(dev
->udev
,
156 usb_rcvbulkpipe(dev
->udev
, PCAN_USB_EP_CMDIN
),
157 dev
->cmd_buf
, PCAN_USB_CMD_LEN
, &actual_length
,
158 PCAN_USB_COMMAND_TIMEOUT
);
160 netdev_err(dev
->netdev
,
161 "waiting rsp f=0x%x n=0x%x failure: %d\n", f
, n
, err
);
163 memcpy(p
, dev
->cmd_buf
+ PCAN_USB_CMD_ARGS
,
164 PCAN_USB_CMD_ARGS_LEN
);
169 static int pcan_usb_set_sja1000(struct peak_usb_device
*dev
, u8 mode
)
171 u8 args
[PCAN_USB_CMD_ARGS_LEN
] = {
175 return pcan_usb_send_cmd(dev
, 9, 2, args
);
178 static int pcan_usb_set_bus(struct peak_usb_device
*dev
, u8 onoff
)
180 u8 args
[PCAN_USB_CMD_ARGS_LEN
] = {
184 return pcan_usb_send_cmd(dev
, 3, 2, args
);
187 static int pcan_usb_set_silent(struct peak_usb_device
*dev
, u8 onoff
)
189 u8 args
[PCAN_USB_CMD_ARGS_LEN
] = {
193 return pcan_usb_send_cmd(dev
, 3, 3, args
);
196 static int pcan_usb_set_ext_vcc(struct peak_usb_device
*dev
, u8 onoff
)
198 u8 args
[PCAN_USB_CMD_ARGS_LEN
] = {
202 return pcan_usb_send_cmd(dev
, 10, 2, args
);
206 * set bittiming value to can
208 static int pcan_usb_set_bittiming(struct peak_usb_device
*dev
,
209 struct can_bittiming
*bt
)
211 u8 args
[PCAN_USB_CMD_ARGS_LEN
];
214 btr0
= ((bt
->brp
- 1) & 0x3f) | (((bt
->sjw
- 1) & 0x3) << 6);
215 btr1
= ((bt
->prop_seg
+ bt
->phase_seg1
- 1) & 0xf) |
216 (((bt
->phase_seg2
- 1) & 0x7) << 4);
217 if (dev
->can
.ctrlmode
& CAN_CTRLMODE_3_SAMPLES
)
220 netdev_info(dev
->netdev
, "setting BTR0=0x%02x BTR1=0x%02x\n",
226 return pcan_usb_send_cmd(dev
, 1, 2, args
);
232 static int pcan_usb_write_mode(struct peak_usb_device
*dev
, u8 onoff
)
236 err
= pcan_usb_set_bus(dev
, onoff
);
241 err
= pcan_usb_set_sja1000(dev
, SJA1000_MODE_INIT
);
243 /* the PCAN-USB needs time to init */
244 set_current_state(TASK_INTERRUPTIBLE
);
245 schedule_timeout(msecs_to_jiffies(PCAN_USB_STARTUP_TIMEOUT
));
252 * handle end of waiting for the device to reset
254 static void pcan_usb_restart(struct timer_list
*t
)
256 struct pcan_usb
*pdev
= from_timer(pdev
, t
, restart_timer
);
257 struct peak_usb_device
*dev
= &pdev
->dev
;
259 /* notify candev and netdev */
260 peak_usb_restart_complete(dev
);
264 * handle the submission of the restart urb
266 static void pcan_usb_restart_pending(struct urb
*urb
)
268 struct pcan_usb
*pdev
= urb
->context
;
270 /* the PCAN-USB needs time to restart */
271 mod_timer(&pdev
->restart_timer
,
272 jiffies
+ msecs_to_jiffies(PCAN_USB_STARTUP_TIMEOUT
));
274 /* can delete usb resources */
275 peak_usb_async_complete(urb
);
279 * handle asynchronous restart
281 static int pcan_usb_restart_async(struct peak_usb_device
*dev
, struct urb
*urb
,
284 struct pcan_usb
*pdev
= container_of(dev
, struct pcan_usb
, dev
);
286 if (timer_pending(&pdev
->restart_timer
))
290 buf
[PCAN_USB_CMD_FUNC
] = 3;
291 buf
[PCAN_USB_CMD_NUM
] = 2;
292 buf
[PCAN_USB_CMD_ARGS
] = 1;
294 usb_fill_bulk_urb(urb
, dev
->udev
,
295 usb_sndbulkpipe(dev
->udev
, PCAN_USB_EP_CMDOUT
),
296 buf
, PCAN_USB_CMD_LEN
,
297 pcan_usb_restart_pending
, pdev
);
299 return usb_submit_urb(urb
, GFP_ATOMIC
);
303 * read serial number from device
305 static int pcan_usb_get_serial(struct peak_usb_device
*dev
, u32
*serial_number
)
307 u8 args
[PCAN_USB_CMD_ARGS_LEN
];
310 err
= pcan_usb_wait_rsp(dev
, 6, 1, args
);
312 netdev_err(dev
->netdev
, "getting serial failure: %d\n", err
);
313 } else if (serial_number
) {
316 memcpy(&tmp32
, args
, 4);
317 *serial_number
= le32_to_cpu(tmp32
);
324 * read device id from device
326 static int pcan_usb_get_device_id(struct peak_usb_device
*dev
, u32
*device_id
)
328 u8 args
[PCAN_USB_CMD_ARGS_LEN
];
331 err
= pcan_usb_wait_rsp(dev
, 4, 1, args
);
333 netdev_err(dev
->netdev
, "getting device id failure: %d\n", err
);
335 *device_id
= args
[0];
341 * update current time ref with received timestamp
343 static int pcan_usb_update_ts(struct pcan_usb_msg_context
*mc
)
347 if ((mc
->ptr
+2) > mc
->end
)
350 memcpy(&tmp16
, mc
->ptr
, 2);
352 mc
->ts16
= le16_to_cpu(tmp16
);
355 peak_usb_update_ts_now(&mc
->pdev
->time_ref
, mc
->ts16
);
357 peak_usb_set_ts_now(&mc
->pdev
->time_ref
, mc
->ts16
);
363 * decode received timestamp
365 static int pcan_usb_decode_ts(struct pcan_usb_msg_context
*mc
, u8 first_packet
)
367 /* only 1st packet supplies a word timestamp */
371 if ((mc
->ptr
+ 2) > mc
->end
)
374 memcpy(&tmp16
, mc
->ptr
, 2);
377 mc
->ts16
= le16_to_cpu(tmp16
);
378 mc
->prev_ts8
= mc
->ts16
& 0x00ff;
382 if ((mc
->ptr
+ 1) > mc
->end
)
387 if (ts8
< mc
->prev_ts8
)
398 static int pcan_usb_decode_error(struct pcan_usb_msg_context
*mc
, u8 n
,
402 struct can_frame
*cf
;
403 enum can_state new_state
;
405 /* ignore this error until 1st ts received */
406 if (n
== PCAN_USB_ERROR_QOVR
)
407 if (!mc
->pdev
->time_ref
.tick_count
)
410 new_state
= mc
->pdev
->dev
.can
.state
;
412 switch (mc
->pdev
->dev
.can
.state
) {
413 case CAN_STATE_ERROR_ACTIVE
:
414 if (n
& PCAN_USB_ERROR_BUS_LIGHT
) {
415 new_state
= CAN_STATE_ERROR_WARNING
;
420 case CAN_STATE_ERROR_WARNING
:
421 if (n
& PCAN_USB_ERROR_BUS_HEAVY
) {
422 new_state
= CAN_STATE_ERROR_PASSIVE
;
425 if (n
& PCAN_USB_ERROR_BUS_OFF
) {
426 new_state
= CAN_STATE_BUS_OFF
;
429 if (n
& (PCAN_USB_ERROR_RXQOVR
| PCAN_USB_ERROR_QOVR
)) {
431 * trick to bypass next comparison and process other
434 new_state
= CAN_STATE_MAX
;
437 if ((n
& PCAN_USB_ERROR_BUS_LIGHT
) == 0) {
438 /* no error (back to active state) */
439 new_state
= CAN_STATE_ERROR_ACTIVE
;
444 case CAN_STATE_ERROR_PASSIVE
:
445 if (n
& PCAN_USB_ERROR_BUS_OFF
) {
446 new_state
= CAN_STATE_BUS_OFF
;
449 if (n
& PCAN_USB_ERROR_BUS_LIGHT
) {
450 new_state
= CAN_STATE_ERROR_WARNING
;
453 if (n
& (PCAN_USB_ERROR_RXQOVR
| PCAN_USB_ERROR_QOVR
)) {
455 * trick to bypass next comparison and process other
458 new_state
= CAN_STATE_MAX
;
462 if ((n
& PCAN_USB_ERROR_BUS_HEAVY
) == 0) {
463 /* no error (back to warning state) */
464 new_state
= CAN_STATE_ERROR_WARNING
;
470 /* do nothing waiting for restart */
474 /* donot post any error if current state didn't change */
475 if (mc
->pdev
->dev
.can
.state
== new_state
)
478 /* allocate an skb to store the error frame */
479 skb
= alloc_can_err_skb(mc
->netdev
, &cf
);
484 case CAN_STATE_BUS_OFF
:
485 cf
->can_id
|= CAN_ERR_BUSOFF
;
486 mc
->pdev
->dev
.can
.can_stats
.bus_off
++;
487 can_bus_off(mc
->netdev
);
490 case CAN_STATE_ERROR_PASSIVE
:
491 cf
->can_id
|= CAN_ERR_CRTL
;
492 cf
->data
[1] |= CAN_ERR_CRTL_TX_PASSIVE
|
493 CAN_ERR_CRTL_RX_PASSIVE
;
494 mc
->pdev
->dev
.can
.can_stats
.error_passive
++;
497 case CAN_STATE_ERROR_WARNING
:
498 cf
->can_id
|= CAN_ERR_CRTL
;
499 cf
->data
[1] |= CAN_ERR_CRTL_TX_WARNING
|
500 CAN_ERR_CRTL_RX_WARNING
;
501 mc
->pdev
->dev
.can
.can_stats
.error_warning
++;
504 case CAN_STATE_ERROR_ACTIVE
:
505 cf
->can_id
|= CAN_ERR_CRTL
;
506 cf
->data
[1] = CAN_ERR_CRTL_ACTIVE
;
510 /* CAN_STATE_MAX (trick to handle other errors) */
511 cf
->can_id
|= CAN_ERR_CRTL
;
512 cf
->data
[1] |= CAN_ERR_CRTL_RX_OVERFLOW
;
513 mc
->netdev
->stats
.rx_over_errors
++;
514 mc
->netdev
->stats
.rx_errors
++;
516 new_state
= mc
->pdev
->dev
.can
.state
;
520 mc
->pdev
->dev
.can
.state
= new_state
;
522 if (status_len
& PCAN_USB_STATUSLEN_TIMESTAMP
) {
523 struct skb_shared_hwtstamps
*hwts
= skb_hwtstamps(skb
);
525 peak_usb_get_ts_time(&mc
->pdev
->time_ref
, mc
->ts16
,
529 mc
->netdev
->stats
.rx_packets
++;
530 mc
->netdev
->stats
.rx_bytes
+= cf
->can_dlc
;
537 * decode non-data usb message
539 static int pcan_usb_decode_status(struct pcan_usb_msg_context
*mc
,
542 u8 rec_len
= status_len
& PCAN_USB_STATUSLEN_DLC
;
546 /* check whether function and number can be read */
547 if ((mc
->ptr
+ 2) > mc
->end
)
550 f
= mc
->ptr
[PCAN_USB_CMD_FUNC
];
551 n
= mc
->ptr
[PCAN_USB_CMD_NUM
];
552 mc
->ptr
+= PCAN_USB_CMD_ARGS
;
554 if (status_len
& PCAN_USB_STATUSLEN_TIMESTAMP
) {
555 int err
= pcan_usb_decode_ts(mc
, !mc
->rec_ts_idx
);
560 /* Next packet in the buffer will have a timestamp on a single
567 case PCAN_USB_REC_ERROR
:
568 err
= pcan_usb_decode_error(mc
, n
, status_len
);
573 case PCAN_USB_REC_ANALOG
:
574 /* analog values (ignored) */
578 case PCAN_USB_REC_BUSLOAD
:
579 /* bus load (ignored) */
583 case PCAN_USB_REC_TS
:
585 if (pcan_usb_update_ts(mc
))
589 case PCAN_USB_REC_BUSEVT
:
590 /* error frame/bus event */
591 if (n
& PCAN_USB_ERROR_TXQFULL
)
592 netdev_dbg(mc
->netdev
, "device Tx queue full)\n");
595 netdev_err(mc
->netdev
, "unexpected function %u\n", f
);
599 if ((mc
->ptr
+ rec_len
) > mc
->end
)
608 * decode data usb message
610 static int pcan_usb_decode_data(struct pcan_usb_msg_context
*mc
, u8 status_len
)
612 u8 rec_len
= status_len
& PCAN_USB_STATUSLEN_DLC
;
614 struct can_frame
*cf
;
615 struct skb_shared_hwtstamps
*hwts
;
617 skb
= alloc_can_skb(mc
->netdev
, &cf
);
621 if (status_len
& PCAN_USB_STATUSLEN_EXT_ID
) {
624 if ((mc
->ptr
+ 4) > mc
->end
)
627 memcpy(&tmp32
, mc
->ptr
, 4);
630 cf
->can_id
= (le32_to_cpu(tmp32
) >> 3) | CAN_EFF_FLAG
;
634 if ((mc
->ptr
+ 2) > mc
->end
)
637 memcpy(&tmp16
, mc
->ptr
, 2);
640 cf
->can_id
= le16_to_cpu(tmp16
) >> 5;
643 cf
->can_dlc
= get_can_dlc(rec_len
);
645 /* Only first packet timestamp is a word */
646 if (pcan_usb_decode_ts(mc
, !mc
->rec_ts_idx
))
649 /* Next packet in the buffer will have a timestamp on a single byte */
653 memset(cf
->data
, 0x0, sizeof(cf
->data
));
654 if (status_len
& PCAN_USB_STATUSLEN_RTR
) {
655 cf
->can_id
|= CAN_RTR_FLAG
;
657 if ((mc
->ptr
+ rec_len
) > mc
->end
)
660 memcpy(cf
->data
, mc
->ptr
, cf
->can_dlc
);
664 /* convert timestamp into kernel time */
665 hwts
= skb_hwtstamps(skb
);
666 peak_usb_get_ts_time(&mc
->pdev
->time_ref
, mc
->ts16
, &hwts
->hwtstamp
);
668 /* update statistics */
669 mc
->netdev
->stats
.rx_packets
++;
670 mc
->netdev
->stats
.rx_bytes
+= cf
->can_dlc
;
682 * process incoming message
684 static int pcan_usb_decode_msg(struct peak_usb_device
*dev
, u8
*ibuf
, u32 lbuf
)
686 struct pcan_usb_msg_context mc
= {
688 .ptr
= ibuf
+ PCAN_USB_MSG_HEADER_LEN
,
690 .netdev
= dev
->netdev
,
691 .pdev
= container_of(dev
, struct pcan_usb
, dev
),
695 for (err
= 0; mc
.rec_idx
< mc
.rec_cnt
&& !err
; mc
.rec_idx
++) {
698 /* handle status and error frames here */
699 if (sl
& PCAN_USB_STATUSLEN_INTERNAL
) {
700 err
= pcan_usb_decode_status(&mc
, sl
);
701 /* handle normal can frames here */
703 err
= pcan_usb_decode_data(&mc
, sl
);
711 * process any incoming buffer
713 static int pcan_usb_decode_buf(struct peak_usb_device
*dev
, struct urb
*urb
)
717 if (urb
->actual_length
> PCAN_USB_MSG_HEADER_LEN
) {
718 err
= pcan_usb_decode_msg(dev
, urb
->transfer_buffer
,
721 } else if (urb
->actual_length
> 0) {
722 netdev_err(dev
->netdev
, "usb message length error (%u)\n",
731 * process outgoing packet
733 static int pcan_usb_encode_msg(struct peak_usb_device
*dev
, struct sk_buff
*skb
,
734 u8
*obuf
, size_t *size
)
736 struct net_device
*netdev
= dev
->netdev
;
737 struct net_device_stats
*stats
= &netdev
->stats
;
738 struct can_frame
*cf
= (struct can_frame
*)skb
->data
;
744 pc
= obuf
+ PCAN_USB_MSG_HEADER_LEN
;
746 /* status/len byte */
748 if (cf
->can_id
& CAN_RTR_FLAG
)
749 *pc
|= PCAN_USB_STATUSLEN_RTR
;
752 if (cf
->can_id
& CAN_EFF_FLAG
) {
753 __le32 tmp32
= cpu_to_le32((cf
->can_id
& CAN_ERR_MASK
) << 3);
755 *pc
|= PCAN_USB_STATUSLEN_EXT_ID
;
756 memcpy(++pc
, &tmp32
, 4);
759 __le16 tmp16
= cpu_to_le16((cf
->can_id
& CAN_ERR_MASK
) << 5);
761 memcpy(++pc
, &tmp16
, 2);
766 if (!(cf
->can_id
& CAN_RTR_FLAG
)) {
767 memcpy(pc
, cf
->data
, cf
->can_dlc
);
771 obuf
[(*size
)-1] = (u8
)(stats
->tx_packets
& 0xff);
779 static int pcan_usb_start(struct peak_usb_device
*dev
)
781 struct pcan_usb
*pdev
= container_of(dev
, struct pcan_usb
, dev
);
783 /* number of bits used in timestamps read from adapter struct */
784 peak_usb_init_time_ref(&pdev
->time_ref
, &pcan_usb
);
786 /* if revision greater than 3, can put silent mode on/off */
787 if (dev
->device_rev
> 3) {
790 err
= pcan_usb_set_silent(dev
,
791 dev
->can
.ctrlmode
& CAN_CTRLMODE_LISTENONLY
);
796 return pcan_usb_set_ext_vcc(dev
, 0);
799 static int pcan_usb_init(struct peak_usb_device
*dev
)
801 struct pcan_usb
*pdev
= container_of(dev
, struct pcan_usb
, dev
);
805 /* initialize a timer needed to wait for hardware restart */
806 timer_setup(&pdev
->restart_timer
, pcan_usb_restart
, 0);
809 * explicit use of dev_xxx() instead of netdev_xxx() here:
810 * information displayed are related to the device itself, not
811 * to the canx netdevice.
813 err
= pcan_usb_get_serial(dev
, &serial_number
);
815 dev_err(dev
->netdev
->dev
.parent
,
816 "unable to read %s serial number (err %d)\n",
821 dev_info(dev
->netdev
->dev
.parent
,
822 "PEAK-System %s adapter hwrev %u serial %08X (%u channel)\n",
823 pcan_usb
.name
, dev
->device_rev
, serial_number
,
824 pcan_usb
.ctrl_count
);
830 * probe function for new PCAN-USB usb interface
832 static int pcan_usb_probe(struct usb_interface
*intf
)
834 struct usb_host_interface
*if_desc
;
837 if_desc
= intf
->altsetting
;
839 /* check interface endpoint addresses */
840 for (i
= 0; i
< if_desc
->desc
.bNumEndpoints
; i
++) {
841 struct usb_endpoint_descriptor
*ep
= &if_desc
->endpoint
[i
].desc
;
843 switch (ep
->bEndpointAddress
) {
844 case PCAN_USB_EP_CMDOUT
:
845 case PCAN_USB_EP_CMDIN
:
846 case PCAN_USB_EP_MSGOUT
:
847 case PCAN_USB_EP_MSGIN
:
858 * describe the PCAN-USB adapter
860 static const struct can_bittiming_const pcan_usb_const
= {
872 const struct peak_usb_adapter pcan_usb
= {
874 .device_id
= PCAN_USB_PRODUCT_ID
,
876 .ctrlmode_supported
= CAN_CTRLMODE_3_SAMPLES
| CAN_CTRLMODE_LISTENONLY
,
878 .freq
= PCAN_USB_CRYSTAL_HZ
/ 2 ,
880 .bittiming_const
= &pcan_usb_const
,
882 /* size of device private data */
883 .sizeof_dev_private
= sizeof(struct pcan_usb
),
885 /* timestamps usage */
887 .ts_period
= 24575, /* calibration period in ts. */
888 .us_per_ts_scale
= PCAN_USB_TS_US_PER_TICK
, /* us=(ts*scale) */
889 .us_per_ts_shift
= PCAN_USB_TS_DIV_SHIFTER
, /* >> shift */
891 /* give here messages in/out endpoints */
892 .ep_msg_in
= PCAN_USB_EP_MSGIN
,
893 .ep_msg_out
= {PCAN_USB_EP_MSGOUT
},
895 /* size of rx/tx usb buffers */
896 .rx_buffer_size
= PCAN_USB_RX_BUFFER_SIZE
,
897 .tx_buffer_size
= PCAN_USB_TX_BUFFER_SIZE
,
899 /* device callbacks */
900 .intf_probe
= pcan_usb_probe
,
901 .dev_init
= pcan_usb_init
,
902 .dev_set_bus
= pcan_usb_write_mode
,
903 .dev_set_bittiming
= pcan_usb_set_bittiming
,
904 .dev_get_device_id
= pcan_usb_get_device_id
,
905 .dev_decode_buf
= pcan_usb_decode_buf
,
906 .dev_encode_msg
= pcan_usb_encode_msg
,
907 .dev_start
= pcan_usb_start
,
908 .dev_restart_async
= pcan_usb_restart_async
,