treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / net / can / usb / peak_usb / pcan_usb.c
blobd2539c95adb65cc24cc7bdb73a954d721071d1a1
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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
69 /* SJA1000 modes */
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
76 * accuracy = 10^-7
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 */
89 struct pcan_usb {
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 {
97 u16 ts16;
98 u8 prev_ts8;
99 u8 *ptr;
100 u8 *end;
101 u8 rec_cnt;
102 u8 rec_idx;
103 u8 rec_ts_idx;
104 struct net_device *netdev;
105 struct pcan_usb *pdev;
109 * send a command
111 static int pcan_usb_send_cmd(struct peak_usb_device *dev, u8 f, u8 n, u8 *p)
113 int err;
114 int actual_length;
116 /* usb device unregistered? */
117 if (!(dev->state & PCAN_USB_STATE_CONNECTED))
118 return 0;
120 dev->cmd_buf[PCAN_USB_CMD_FUNC] = f;
121 dev->cmd_buf[PCAN_USB_CMD_NUM] = n;
123 if (p)
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);
131 if (err)
132 netdev_err(dev->netdev,
133 "sending cmd f=0x%x n=0x%x failure: %d\n",
134 f, n, err);
135 return err;
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)
143 int err;
144 int actual_length;
146 /* usb device unregistered? */
147 if (!(dev->state & PCAN_USB_STATE_CONNECTED))
148 return 0;
150 /* first, send command */
151 err = pcan_usb_send_cmd(dev, f, n, NULL);
152 if (err)
153 return err;
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);
159 if (err)
160 netdev_err(dev->netdev,
161 "waiting rsp f=0x%x n=0x%x failure: %d\n", f, n, err);
162 else if (p)
163 memcpy(p, dev->cmd_buf + PCAN_USB_CMD_ARGS,
164 PCAN_USB_CMD_ARGS_LEN);
166 return err;
169 static int pcan_usb_set_sja1000(struct peak_usb_device *dev, u8 mode)
171 u8 args[PCAN_USB_CMD_ARGS_LEN] = {
172 [1] = mode,
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] = {
181 [0] = !!onoff,
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] = {
190 [0] = !!onoff,
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] = {
199 [0] = !!onoff,
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];
212 u8 btr0, btr1;
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)
218 btr1 |= 0x80;
220 netdev_info(dev->netdev, "setting BTR0=0x%02x BTR1=0x%02x\n",
221 btr0, btr1);
223 args[0] = btr1;
224 args[1] = btr0;
226 return pcan_usb_send_cmd(dev, 1, 2, args);
230 * init/reset can
232 static int pcan_usb_write_mode(struct peak_usb_device *dev, u8 onoff)
234 int err;
236 err = pcan_usb_set_bus(dev, onoff);
237 if (err)
238 return err;
240 if (!onoff) {
241 err = pcan_usb_set_sja1000(dev, SJA1000_MODE_INIT);
242 } else {
243 /* the PCAN-USB needs time to init */
244 set_current_state(TASK_INTERRUPTIBLE);
245 schedule_timeout(msecs_to_jiffies(PCAN_USB_STARTUP_TIMEOUT));
248 return err;
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,
282 u8 *buf)
284 struct pcan_usb *pdev = container_of(dev, struct pcan_usb, dev);
286 if (timer_pending(&pdev->restart_timer))
287 return -EBUSY;
289 /* set bus on */
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];
308 int err;
310 err = pcan_usb_wait_rsp(dev, 6, 1, args);
311 if (err) {
312 netdev_err(dev->netdev, "getting serial failure: %d\n", err);
313 } else if (serial_number) {
314 __le32 tmp32;
316 memcpy(&tmp32, args, 4);
317 *serial_number = le32_to_cpu(tmp32);
320 return err;
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];
329 int err;
331 err = pcan_usb_wait_rsp(dev, 4, 1, args);
332 if (err)
333 netdev_err(dev->netdev, "getting device id failure: %d\n", err);
334 else if (device_id)
335 *device_id = args[0];
337 return err;
341 * update current time ref with received timestamp
343 static int pcan_usb_update_ts(struct pcan_usb_msg_context *mc)
345 __le16 tmp16;
347 if ((mc->ptr+2) > mc->end)
348 return -EINVAL;
350 memcpy(&tmp16, mc->ptr, 2);
352 mc->ts16 = le16_to_cpu(tmp16);
354 if (mc->rec_idx > 0)
355 peak_usb_update_ts_now(&mc->pdev->time_ref, mc->ts16);
356 else
357 peak_usb_set_ts_now(&mc->pdev->time_ref, mc->ts16);
359 return 0;
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 */
368 if (first_packet) {
369 __le16 tmp16;
371 if ((mc->ptr + 2) > mc->end)
372 return -EINVAL;
374 memcpy(&tmp16, mc->ptr, 2);
375 mc->ptr += 2;
377 mc->ts16 = le16_to_cpu(tmp16);
378 mc->prev_ts8 = mc->ts16 & 0x00ff;
379 } else {
380 u8 ts8;
382 if ((mc->ptr + 1) > mc->end)
383 return -EINVAL;
385 ts8 = *mc->ptr++;
387 if (ts8 < mc->prev_ts8)
388 mc->ts16 += 0x100;
390 mc->ts16 &= 0xff00;
391 mc->ts16 |= ts8;
392 mc->prev_ts8 = ts8;
395 return 0;
398 static int pcan_usb_decode_error(struct pcan_usb_msg_context *mc, u8 n,
399 u8 status_len)
401 struct sk_buff *skb;
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)
408 return 0;
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;
416 break;
418 /* fall through */
420 case CAN_STATE_ERROR_WARNING:
421 if (n & PCAN_USB_ERROR_BUS_HEAVY) {
422 new_state = CAN_STATE_ERROR_PASSIVE;
423 break;
425 if (n & PCAN_USB_ERROR_BUS_OFF) {
426 new_state = CAN_STATE_BUS_OFF;
427 break;
429 if (n & (PCAN_USB_ERROR_RXQOVR | PCAN_USB_ERROR_QOVR)) {
431 * trick to bypass next comparison and process other
432 * errors
434 new_state = CAN_STATE_MAX;
435 break;
437 if ((n & PCAN_USB_ERROR_BUS_LIGHT) == 0) {
438 /* no error (back to active state) */
439 new_state = CAN_STATE_ERROR_ACTIVE;
440 break;
442 break;
444 case CAN_STATE_ERROR_PASSIVE:
445 if (n & PCAN_USB_ERROR_BUS_OFF) {
446 new_state = CAN_STATE_BUS_OFF;
447 break;
449 if (n & PCAN_USB_ERROR_BUS_LIGHT) {
450 new_state = CAN_STATE_ERROR_WARNING;
451 break;
453 if (n & (PCAN_USB_ERROR_RXQOVR | PCAN_USB_ERROR_QOVR)) {
455 * trick to bypass next comparison and process other
456 * errors
458 new_state = CAN_STATE_MAX;
459 break;
462 if ((n & PCAN_USB_ERROR_BUS_HEAVY) == 0) {
463 /* no error (back to warning state) */
464 new_state = CAN_STATE_ERROR_WARNING;
465 break;
467 break;
469 default:
470 /* do nothing waiting for restart */
471 return 0;
474 /* donot post any error if current state didn't change */
475 if (mc->pdev->dev.can.state == new_state)
476 return 0;
478 /* allocate an skb to store the error frame */
479 skb = alloc_can_err_skb(mc->netdev, &cf);
480 if (!skb)
481 return -ENOMEM;
483 switch (new_state) {
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);
488 break;
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++;
495 break;
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++;
502 break;
504 case CAN_STATE_ERROR_ACTIVE:
505 cf->can_id |= CAN_ERR_CRTL;
506 cf->data[1] = CAN_ERR_CRTL_ACTIVE;
507 break;
509 default:
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;
517 break;
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,
526 &hwts->hwtstamp);
529 mc->netdev->stats.rx_packets++;
530 mc->netdev->stats.rx_bytes += cf->can_dlc;
531 netif_rx(skb);
533 return 0;
537 * decode non-data usb message
539 static int pcan_usb_decode_status(struct pcan_usb_msg_context *mc,
540 u8 status_len)
542 u8 rec_len = status_len & PCAN_USB_STATUSLEN_DLC;
543 u8 f, n;
544 int err;
546 /* check whether function and number can be read */
547 if ((mc->ptr + 2) > mc->end)
548 return -EINVAL;
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);
557 if (err)
558 return err;
560 /* Next packet in the buffer will have a timestamp on a single
561 * byte
563 mc->rec_ts_idx++;
566 switch (f) {
567 case PCAN_USB_REC_ERROR:
568 err = pcan_usb_decode_error(mc, n, status_len);
569 if (err)
570 return err;
571 break;
573 case PCAN_USB_REC_ANALOG:
574 /* analog values (ignored) */
575 rec_len = 2;
576 break;
578 case PCAN_USB_REC_BUSLOAD:
579 /* bus load (ignored) */
580 rec_len = 1;
581 break;
583 case PCAN_USB_REC_TS:
584 /* only timestamp */
585 if (pcan_usb_update_ts(mc))
586 return -EINVAL;
587 break;
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");
593 break;
594 default:
595 netdev_err(mc->netdev, "unexpected function %u\n", f);
596 break;
599 if ((mc->ptr + rec_len) > mc->end)
600 return -EINVAL;
602 mc->ptr += rec_len;
604 return 0;
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;
613 struct sk_buff *skb;
614 struct can_frame *cf;
615 struct skb_shared_hwtstamps *hwts;
617 skb = alloc_can_skb(mc->netdev, &cf);
618 if (!skb)
619 return -ENOMEM;
621 if (status_len & PCAN_USB_STATUSLEN_EXT_ID) {
622 __le32 tmp32;
624 if ((mc->ptr + 4) > mc->end)
625 goto decode_failed;
627 memcpy(&tmp32, mc->ptr, 4);
628 mc->ptr += 4;
630 cf->can_id = (le32_to_cpu(tmp32) >> 3) | CAN_EFF_FLAG;
631 } else {
632 __le16 tmp16;
634 if ((mc->ptr + 2) > mc->end)
635 goto decode_failed;
637 memcpy(&tmp16, mc->ptr, 2);
638 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))
647 goto decode_failed;
649 /* Next packet in the buffer will have a timestamp on a single byte */
650 mc->rec_ts_idx++;
652 /* read data */
653 memset(cf->data, 0x0, sizeof(cf->data));
654 if (status_len & PCAN_USB_STATUSLEN_RTR) {
655 cf->can_id |= CAN_RTR_FLAG;
656 } else {
657 if ((mc->ptr + rec_len) > mc->end)
658 goto decode_failed;
660 memcpy(cf->data, mc->ptr, cf->can_dlc);
661 mc->ptr += rec_len;
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;
671 /* push the skb */
672 netif_rx(skb);
674 return 0;
676 decode_failed:
677 dev_kfree_skb(skb);
678 return -EINVAL;
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 = {
687 .rec_cnt = ibuf[1],
688 .ptr = ibuf + PCAN_USB_MSG_HEADER_LEN,
689 .end = ibuf + lbuf,
690 .netdev = dev->netdev,
691 .pdev = container_of(dev, struct pcan_usb, dev),
693 int err;
695 for (err = 0; mc.rec_idx < mc.rec_cnt && !err; mc.rec_idx++) {
696 u8 sl = *mc.ptr++;
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 */
702 } else {
703 err = pcan_usb_decode_data(&mc, sl);
707 return err;
711 * process any incoming buffer
713 static int pcan_usb_decode_buf(struct peak_usb_device *dev, struct urb *urb)
715 int err = 0;
717 if (urb->actual_length > PCAN_USB_MSG_HEADER_LEN) {
718 err = pcan_usb_decode_msg(dev, urb->transfer_buffer,
719 urb->actual_length);
721 } else if (urb->actual_length > 0) {
722 netdev_err(dev->netdev, "usb message length error (%u)\n",
723 urb->actual_length);
724 err = -EINVAL;
727 return err;
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;
739 u8 *pc;
741 obuf[0] = 2;
742 obuf[1] = 1;
744 pc = obuf + PCAN_USB_MSG_HEADER_LEN;
746 /* status/len byte */
747 *pc = cf->can_dlc;
748 if (cf->can_id & CAN_RTR_FLAG)
749 *pc |= PCAN_USB_STATUSLEN_RTR;
751 /* can id */
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);
757 pc += 4;
758 } else {
759 __le16 tmp16 = cpu_to_le16((cf->can_id & CAN_ERR_MASK) << 5);
761 memcpy(++pc, &tmp16, 2);
762 pc += 2;
765 /* can data */
766 if (!(cf->can_id & CAN_RTR_FLAG)) {
767 memcpy(pc, cf->data, cf->can_dlc);
768 pc += cf->can_dlc;
771 obuf[(*size)-1] = (u8)(stats->tx_packets & 0xff);
773 return 0;
777 * start interface
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) {
788 int err;
790 err = pcan_usb_set_silent(dev,
791 dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY);
792 if (err)
793 return err;
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);
802 u32 serial_number;
803 int err;
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);
814 if (err) {
815 dev_err(dev->netdev->dev.parent,
816 "unable to read %s serial number (err %d)\n",
817 pcan_usb.name, err);
818 return err;
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);
826 return 0;
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;
835 int i;
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:
848 break;
849 default:
850 return -ENODEV;
854 return 0;
858 * describe the PCAN-USB adapter
860 static const struct can_bittiming_const pcan_usb_const = {
861 .name = "pcan_usb",
862 .tseg1_min = 1,
863 .tseg1_max = 16,
864 .tseg2_min = 1,
865 .tseg2_max = 8,
866 .sjw_max = 4,
867 .brp_min = 1,
868 .brp_max = 64,
869 .brp_inc = 1,
872 const struct peak_usb_adapter pcan_usb = {
873 .name = "PCAN-USB",
874 .device_id = PCAN_USB_PRODUCT_ID,
875 .ctrl_count = 1,
876 .ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY,
877 .clock = {
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 */
886 .ts_used_bits = 16,
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,