1 // SPDX-License-Identifier: GPL-2.0-only
3 * CAN driver for PEAK System PCAN-USB Pro adapter
4 * Derived from the PCAN project file driver/src/pcan_usbpro.c
6 * Copyright (C) 2003-2011 PEAK System-Technik GmbH
9 #include <linux/netdevice.h>
10 #include <linux/usb.h>
11 #include <linux/module.h>
12 #include <linux/ethtool.h>
14 #include <linux/can.h>
15 #include <linux/can/dev.h>
16 #include <linux/can/error.h>
18 #include "pcan_usb_core.h"
19 #include "pcan_usb_pro.h"
21 #define PCAN_USBPRO_CHANNEL_COUNT 2
23 /* PCAN-USB Pro adapter internal clock (MHz) */
24 #define PCAN_USBPRO_CRYSTAL_HZ 56000000
26 /* PCAN-USB Pro command timeout (ms.) */
27 #define PCAN_USBPRO_COMMAND_TIMEOUT 1000
29 /* PCAN-USB Pro rx/tx buffers size */
30 #define PCAN_USBPRO_RX_BUFFER_SIZE 1024
31 #define PCAN_USBPRO_TX_BUFFER_SIZE 64
33 #define PCAN_USBPRO_MSG_HEADER_LEN 4
35 /* some commands responses need to be re-submitted */
36 #define PCAN_USBPRO_RSP_SUBMIT_MAX 2
38 #define PCAN_USBPRO_RTR 0x01
39 #define PCAN_USBPRO_EXT 0x02
40 #define PCAN_USBPRO_SS 0x08
42 #define PCAN_USBPRO_CMD_BUFFER_SIZE 512
44 /* handle device specific info used by the netdevices */
45 struct pcan_usb_pro_interface {
46 struct peak_usb_device *dev[PCAN_USBPRO_CHANNEL_COUNT];
47 struct peak_time_ref time_ref;
52 /* device information */
53 struct pcan_usb_pro_device {
54 struct peak_usb_device dev;
55 struct pcan_usb_pro_interface *usb_if;
59 /* internal structure used to handle messages sent to bulk urb */
60 struct pcan_usb_pro_msg {
71 /* records sizes table indexed on message id. (8-bits value) */
72 static u16 pcan_usb_pro_sizeof_rec[256] = {
73 [PCAN_USBPRO_SETBTR] = sizeof(struct pcan_usb_pro_btr),
74 [PCAN_USBPRO_SETBUSACT] = sizeof(struct pcan_usb_pro_busact),
75 [PCAN_USBPRO_SETSILENT] = sizeof(struct pcan_usb_pro_silent),
76 [PCAN_USBPRO_SETFILTR] = sizeof(struct pcan_usb_pro_filter),
77 [PCAN_USBPRO_SETTS] = sizeof(struct pcan_usb_pro_setts),
78 [PCAN_USBPRO_GETDEVID] = sizeof(struct pcan_usb_pro_devid),
79 [PCAN_USBPRO_SETLED] = sizeof(struct pcan_usb_pro_setled),
80 [PCAN_USBPRO_RXMSG8] = sizeof(struct pcan_usb_pro_rxmsg),
81 [PCAN_USBPRO_RXMSG4] = sizeof(struct pcan_usb_pro_rxmsg) - 4,
82 [PCAN_USBPRO_RXMSG0] = sizeof(struct pcan_usb_pro_rxmsg) - 8,
83 [PCAN_USBPRO_RXRTR] = sizeof(struct pcan_usb_pro_rxmsg) - 8,
84 [PCAN_USBPRO_RXSTATUS] = sizeof(struct pcan_usb_pro_rxstatus),
85 [PCAN_USBPRO_RXTS] = sizeof(struct pcan_usb_pro_rxts),
86 [PCAN_USBPRO_TXMSG8] = sizeof(struct pcan_usb_pro_txmsg),
87 [PCAN_USBPRO_TXMSG4] = sizeof(struct pcan_usb_pro_txmsg) - 4,
88 [PCAN_USBPRO_TXMSG0] = sizeof(struct pcan_usb_pro_txmsg) - 8,
92 * initialize PCAN-USB Pro message data structure
94 static u8 *pcan_msg_init(struct pcan_usb_pro_msg *pm, void *buffer_addr,
97 if (buffer_size < PCAN_USBPRO_MSG_HEADER_LEN)
100 pm->u.rec_buffer = (u8 *)buffer_addr;
101 pm->rec_buffer_size = pm->rec_buffer_len = buffer_size;
102 pm->rec_ptr = pm->u.rec_buffer + PCAN_USBPRO_MSG_HEADER_LEN;
107 static u8 *pcan_msg_init_empty(struct pcan_usb_pro_msg *pm,
108 void *buffer_addr, int buffer_size)
110 u8 *pr = pcan_msg_init(pm, buffer_addr, buffer_size);
113 pm->rec_buffer_len = PCAN_USBPRO_MSG_HEADER_LEN;
120 * add one record to a message being built
122 static int pcan_msg_add_rec(struct pcan_usb_pro_msg *pm, int id, ...)
130 pc = pm->rec_ptr + 1;
134 case PCAN_USBPRO_TXMSG8:
137 case PCAN_USBPRO_TXMSG4:
140 case PCAN_USBPRO_TXMSG0:
141 *pc++ = va_arg(ap, int);
142 *pc++ = va_arg(ap, int);
143 *pc++ = va_arg(ap, int);
144 *(__le32 *)pc = cpu_to_le32(va_arg(ap, u32));
146 memcpy(pc, va_arg(ap, int *), i);
150 case PCAN_USBPRO_SETBTR:
151 case PCAN_USBPRO_GETDEVID:
152 *pc++ = va_arg(ap, int);
154 *(__le32 *)pc = cpu_to_le32(va_arg(ap, u32));
158 case PCAN_USBPRO_SETFILTR:
159 case PCAN_USBPRO_SETBUSACT:
160 case PCAN_USBPRO_SETSILENT:
161 *pc++ = va_arg(ap, int);
162 *(__le16 *)pc = cpu_to_le16(va_arg(ap, int));
166 case PCAN_USBPRO_SETLED:
167 *pc++ = va_arg(ap, int);
168 *(__le16 *)pc = cpu_to_le16(va_arg(ap, int));
170 *(__le32 *)pc = cpu_to_le32(va_arg(ap, u32));
174 case PCAN_USBPRO_SETTS:
176 *(__le16 *)pc = cpu_to_le16(va_arg(ap, int));
181 pr_err("%s: %s(): unknown data type %02Xh (%d)\n",
182 PCAN_USB_DRIVER_NAME, __func__, id, id);
187 len = pc - pm->rec_ptr;
189 le32_add_cpu(pm->u.rec_cnt, 1);
193 pm->rec_buffer_len += len;
202 * send PCAN-USB Pro command synchronously
204 static int pcan_usb_pro_send_cmd(struct peak_usb_device *dev,
205 struct pcan_usb_pro_msg *pum)
210 /* usb device unregistered? */
211 if (!(dev->state & PCAN_USB_STATE_CONNECTED))
214 err = usb_bulk_msg(dev->udev,
215 usb_sndbulkpipe(dev->udev, PCAN_USBPRO_EP_CMDOUT),
216 pum->u.rec_buffer, pum->rec_buffer_len,
217 &actual_length, PCAN_USBPRO_COMMAND_TIMEOUT);
219 netdev_err(dev->netdev, "sending command failure: %d\n", err);
225 * wait for PCAN-USB Pro command response
227 static int pcan_usb_pro_wait_rsp(struct peak_usb_device *dev,
228 struct pcan_usb_pro_msg *pum)
230 u8 req_data_type, req_channel;
234 /* usb device unregistered? */
235 if (!(dev->state & PCAN_USB_STATE_CONNECTED))
238 req_data_type = pum->u.rec_buffer[4];
239 req_channel = pum->u.rec_buffer[5];
242 for (i = 0; !err && i < PCAN_USBPRO_RSP_SUBMIT_MAX; i++) {
243 struct pcan_usb_pro_msg rsp;
244 union pcan_usb_pro_rec *pr;
249 err = usb_bulk_msg(dev->udev,
250 usb_rcvbulkpipe(dev->udev, PCAN_USBPRO_EP_CMDIN),
251 pum->u.rec_buffer, pum->rec_buffer_len,
252 &actual_length, PCAN_USBPRO_COMMAND_TIMEOUT);
254 netdev_err(dev->netdev, "waiting rsp error %d\n", err);
258 if (actual_length == 0)
262 if (actual_length < PCAN_USBPRO_MSG_HEADER_LEN) {
263 netdev_err(dev->netdev,
264 "got abnormal too small rsp (len=%d)\n",
269 pc = pcan_msg_init(&rsp, pum->u.rec_buffer,
272 rec_cnt = le32_to_cpu(*rsp.u.rec_cnt);
274 /* loop on records stored into message */
275 for (r = 0; r < rec_cnt; r++) {
276 pr = (union pcan_usb_pro_rec *)pc;
277 rec_len = pcan_usb_pro_sizeof_rec[pr->data_type];
279 netdev_err(dev->netdev,
280 "got unprocessed record in msg\n");
281 pcan_dump_mem("rcvd rsp msg", pum->u.rec_buffer,
286 /* check if response corresponds to request */
287 if (pr->data_type != req_data_type)
288 netdev_err(dev->netdev,
289 "got unwanted rsp %xh: ignored\n",
292 /* check if channel in response corresponds too */
293 else if ((req_channel != 0xff) &&
294 (pr->bus_act.channel != req_channel))
295 netdev_err(dev->netdev,
296 "got rsp %xh but on chan%u: ignored\n",
297 req_data_type, pr->bus_act.channel);
299 /* got the response */
303 /* otherwise, go on with next record in message */
308 return (i >= PCAN_USBPRO_RSP_SUBMIT_MAX) ? -ERANGE : err;
311 int pcan_usb_pro_send_req(struct peak_usb_device *dev, int req_id,
312 int req_value, void *req_addr, int req_size)
318 /* usb device unregistered? */
319 if (!(dev->state & PCAN_USB_STATE_CONNECTED))
322 req_type = USB_TYPE_VENDOR | USB_RECIP_OTHER;
325 case PCAN_USBPRO_REQ_FCT:
326 p = usb_sndctrlpipe(dev->udev, 0);
330 p = usb_rcvctrlpipe(dev->udev, 0);
331 req_type |= USB_DIR_IN;
332 memset(req_addr, '\0', req_size);
336 err = usb_control_msg(dev->udev, p, req_id, req_type, req_value, 0,
337 req_addr, req_size, 2 * USB_CTRL_GET_TIMEOUT);
339 netdev_info(dev->netdev,
340 "unable to request usb[type=%d value=%d] err=%d\n",
341 req_id, req_value, err);
348 static int pcan_usb_pro_set_ts(struct peak_usb_device *dev, u16 onoff)
350 struct pcan_usb_pro_msg um;
352 pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
353 pcan_msg_add_rec(&um, PCAN_USBPRO_SETTS, onoff);
355 return pcan_usb_pro_send_cmd(dev, &um);
358 static int pcan_usb_pro_set_bitrate(struct peak_usb_device *dev, u32 ccbt)
360 struct pcan_usb_pro_device *pdev =
361 container_of(dev, struct pcan_usb_pro_device, dev);
362 struct pcan_usb_pro_msg um;
364 pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
365 pcan_msg_add_rec(&um, PCAN_USBPRO_SETBTR, dev->ctrl_idx, ccbt);
367 /* cache the CCBT value to reuse it before next buson */
368 pdev->cached_ccbt = ccbt;
370 return pcan_usb_pro_send_cmd(dev, &um);
373 static int pcan_usb_pro_set_bus(struct peak_usb_device *dev, u8 onoff)
375 struct pcan_usb_pro_msg um;
377 /* if bus=on, be sure the bitrate being set before! */
379 struct pcan_usb_pro_device *pdev =
380 container_of(dev, struct pcan_usb_pro_device, dev);
382 pcan_usb_pro_set_bitrate(dev, pdev->cached_ccbt);
385 pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
386 pcan_msg_add_rec(&um, PCAN_USBPRO_SETBUSACT, dev->ctrl_idx, onoff);
388 return pcan_usb_pro_send_cmd(dev, &um);
391 static int pcan_usb_pro_set_silent(struct peak_usb_device *dev, u8 onoff)
393 struct pcan_usb_pro_msg um;
395 pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
396 pcan_msg_add_rec(&um, PCAN_USBPRO_SETSILENT, dev->ctrl_idx, onoff);
398 return pcan_usb_pro_send_cmd(dev, &um);
401 static int pcan_usb_pro_set_filter(struct peak_usb_device *dev, u16 filter_mode)
403 struct pcan_usb_pro_msg um;
405 pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
406 pcan_msg_add_rec(&um, PCAN_USBPRO_SETFILTR, dev->ctrl_idx, filter_mode);
408 return pcan_usb_pro_send_cmd(dev, &um);
411 static int pcan_usb_pro_set_led(struct peak_usb_device *dev, u8 mode,
414 struct pcan_usb_pro_msg um;
416 pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
417 pcan_msg_add_rec(&um, PCAN_USBPRO_SETLED, dev->ctrl_idx, mode, timeout);
419 return pcan_usb_pro_send_cmd(dev, &um);
422 static int pcan_usb_pro_get_device_id(struct peak_usb_device *dev,
425 struct pcan_usb_pro_devid *pdn;
426 struct pcan_usb_pro_msg um;
430 pc = pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
431 pcan_msg_add_rec(&um, PCAN_USBPRO_GETDEVID, dev->ctrl_idx);
433 err = pcan_usb_pro_send_cmd(dev, &um);
437 err = pcan_usb_pro_wait_rsp(dev, &um);
441 pdn = (struct pcan_usb_pro_devid *)pc;
442 *device_id = le32_to_cpu(pdn->serial_num);
447 static int pcan_usb_pro_set_bittiming(struct peak_usb_device *dev,
448 struct can_bittiming *bt)
452 ccbt = (dev->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) ? 0x00800000 : 0;
453 ccbt |= (bt->sjw - 1) << 24;
454 ccbt |= (bt->phase_seg2 - 1) << 20;
455 ccbt |= (bt->prop_seg + bt->phase_seg1 - 1) << 16; /* = tseg1 */
458 netdev_info(dev->netdev, "setting ccbt=0x%08x\n", ccbt);
460 return pcan_usb_pro_set_bitrate(dev, ccbt);
463 void pcan_usb_pro_restart_complete(struct urb *urb)
465 /* can delete usb resources */
466 peak_usb_async_complete(urb);
468 /* notify candev and netdev */
469 peak_usb_restart_complete(urb->context);
473 * handle restart but in asynchronously way
475 static int pcan_usb_pro_restart_async(struct peak_usb_device *dev,
476 struct urb *urb, u8 *buf)
478 struct pcan_usb_pro_msg um;
480 pcan_msg_init_empty(&um, buf, PCAN_USB_MAX_CMD_LEN);
481 pcan_msg_add_rec(&um, PCAN_USBPRO_SETBUSACT, dev->ctrl_idx, 1);
483 usb_fill_bulk_urb(urb, dev->udev,
484 usb_sndbulkpipe(dev->udev, PCAN_USBPRO_EP_CMDOUT),
485 buf, PCAN_USB_MAX_CMD_LEN,
486 pcan_usb_pro_restart_complete, dev);
488 return usb_submit_urb(urb, GFP_ATOMIC);
491 static int pcan_usb_pro_drv_loaded(struct peak_usb_device *dev, int loaded)
496 buffer = kzalloc(PCAN_USBPRO_FCT_DRVLD_REQ_LEN, GFP_KERNEL);
501 buffer[1] = !!loaded;
503 err = pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_FCT,
504 PCAN_USBPRO_FCT_DRVLD, buffer,
505 PCAN_USBPRO_FCT_DRVLD_REQ_LEN);
512 struct pcan_usb_pro_interface *pcan_usb_pro_dev_if(struct peak_usb_device *dev)
514 struct pcan_usb_pro_device *pdev =
515 container_of(dev, struct pcan_usb_pro_device, dev);
519 static int pcan_usb_pro_handle_canmsg(struct pcan_usb_pro_interface *usb_if,
520 struct pcan_usb_pro_rxmsg *rx)
522 const unsigned int ctrl_idx = (rx->len >> 4) & 0x0f;
523 struct peak_usb_device *dev = usb_if->dev[ctrl_idx];
524 struct net_device *netdev = dev->netdev;
525 struct can_frame *can_frame;
527 struct skb_shared_hwtstamps *hwts;
529 skb = alloc_can_skb(netdev, &can_frame);
533 can_frame->can_id = le32_to_cpu(rx->id);
534 can_frame->len = rx->len & 0x0f;
536 if (rx->flags & PCAN_USBPRO_EXT)
537 can_frame->can_id |= CAN_EFF_FLAG;
539 if (rx->flags & PCAN_USBPRO_RTR)
540 can_frame->can_id |= CAN_RTR_FLAG;
542 memcpy(can_frame->data, rx->data, can_frame->len);
544 hwts = skb_hwtstamps(skb);
545 peak_usb_get_ts_time(&usb_if->time_ref, le32_to_cpu(rx->ts32),
548 netdev->stats.rx_packets++;
549 netdev->stats.rx_bytes += can_frame->len;
555 static int pcan_usb_pro_handle_error(struct pcan_usb_pro_interface *usb_if,
556 struct pcan_usb_pro_rxstatus *er)
558 const u16 raw_status = le16_to_cpu(er->status);
559 const unsigned int ctrl_idx = (er->channel >> 4) & 0x0f;
560 struct peak_usb_device *dev = usb_if->dev[ctrl_idx];
561 struct net_device *netdev = dev->netdev;
562 struct can_frame *can_frame;
563 enum can_state new_state = CAN_STATE_ERROR_ACTIVE;
566 struct skb_shared_hwtstamps *hwts;
568 /* nothing should be sent while in BUS_OFF state */
569 if (dev->can.state == CAN_STATE_BUS_OFF)
573 /* no error bit (back to active state) */
574 dev->can.state = CAN_STATE_ERROR_ACTIVE;
578 if (raw_status & (PCAN_USBPRO_STATUS_OVERRUN |
579 PCAN_USBPRO_STATUS_QOVERRUN)) {
580 /* trick to bypass next comparison and process other errors */
581 new_state = CAN_STATE_MAX;
584 if (raw_status & PCAN_USBPRO_STATUS_BUS) {
585 new_state = CAN_STATE_BUS_OFF;
586 } else if (raw_status & PCAN_USBPRO_STATUS_ERROR) {
587 u32 rx_err_cnt = (le32_to_cpu(er->err_frm) & 0x00ff0000) >> 16;
588 u32 tx_err_cnt = (le32_to_cpu(er->err_frm) & 0xff000000) >> 24;
590 if (rx_err_cnt > 127)
591 err_mask |= CAN_ERR_CRTL_RX_PASSIVE;
592 else if (rx_err_cnt > 96)
593 err_mask |= CAN_ERR_CRTL_RX_WARNING;
595 if (tx_err_cnt > 127)
596 err_mask |= CAN_ERR_CRTL_TX_PASSIVE;
597 else if (tx_err_cnt > 96)
598 err_mask |= CAN_ERR_CRTL_TX_WARNING;
600 if (err_mask & (CAN_ERR_CRTL_RX_WARNING |
601 CAN_ERR_CRTL_TX_WARNING))
602 new_state = CAN_STATE_ERROR_WARNING;
603 else if (err_mask & (CAN_ERR_CRTL_RX_PASSIVE |
604 CAN_ERR_CRTL_TX_PASSIVE))
605 new_state = CAN_STATE_ERROR_PASSIVE;
608 /* donot post any error if current state didn't change */
609 if (dev->can.state == new_state)
612 /* allocate an skb to store the error frame */
613 skb = alloc_can_err_skb(netdev, &can_frame);
618 case CAN_STATE_BUS_OFF:
619 can_frame->can_id |= CAN_ERR_BUSOFF;
620 dev->can.can_stats.bus_off++;
624 case CAN_STATE_ERROR_PASSIVE:
625 can_frame->can_id |= CAN_ERR_CRTL;
626 can_frame->data[1] |= err_mask;
627 dev->can.can_stats.error_passive++;
630 case CAN_STATE_ERROR_WARNING:
631 can_frame->can_id |= CAN_ERR_CRTL;
632 can_frame->data[1] |= err_mask;
633 dev->can.can_stats.error_warning++;
636 case CAN_STATE_ERROR_ACTIVE:
640 /* CAN_STATE_MAX (trick to handle other errors) */
641 if (raw_status & PCAN_USBPRO_STATUS_OVERRUN) {
642 can_frame->can_id |= CAN_ERR_PROT;
643 can_frame->data[2] |= CAN_ERR_PROT_OVERLOAD;
644 netdev->stats.rx_over_errors++;
645 netdev->stats.rx_errors++;
648 if (raw_status & PCAN_USBPRO_STATUS_QOVERRUN) {
649 can_frame->can_id |= CAN_ERR_CRTL;
650 can_frame->data[1] |= CAN_ERR_CRTL_RX_OVERFLOW;
651 netdev->stats.rx_over_errors++;
652 netdev->stats.rx_errors++;
655 new_state = CAN_STATE_ERROR_ACTIVE;
659 dev->can.state = new_state;
661 hwts = skb_hwtstamps(skb);
662 peak_usb_get_ts_time(&usb_if->time_ref, le32_to_cpu(er->ts32), &hwts->hwtstamp);
663 netdev->stats.rx_packets++;
664 netdev->stats.rx_bytes += can_frame->len;
670 static void pcan_usb_pro_handle_ts(struct pcan_usb_pro_interface *usb_if,
671 struct pcan_usb_pro_rxts *ts)
673 /* should wait until clock is stabilized */
674 if (usb_if->cm_ignore_count > 0)
675 usb_if->cm_ignore_count--;
677 peak_usb_set_ts_now(&usb_if->time_ref,
678 le32_to_cpu(ts->ts64[1]));
682 * callback for bulk IN urb
684 static int pcan_usb_pro_decode_buf(struct peak_usb_device *dev, struct urb *urb)
686 struct pcan_usb_pro_interface *usb_if = pcan_usb_pro_dev_if(dev);
687 struct net_device *netdev = dev->netdev;
688 struct pcan_usb_pro_msg usb_msg;
689 u8 *rec_ptr, *msg_end;
693 rec_ptr = pcan_msg_init(&usb_msg, urb->transfer_buffer,
696 netdev_err(netdev, "bad msg hdr len %d\n", urb->actual_length);
700 /* loop reading all the records from the incoming message */
701 msg_end = urb->transfer_buffer + urb->actual_length;
702 rec_cnt = le16_to_cpu(*usb_msg.u.rec_cnt_rd);
703 for (; rec_cnt > 0; rec_cnt--) {
704 union pcan_usb_pro_rec *pr = (union pcan_usb_pro_rec *)rec_ptr;
705 u16 sizeof_rec = pcan_usb_pro_sizeof_rec[pr->data_type];
709 "got unsupported rec in usb msg:\n");
714 /* check if the record goes out of current packet */
715 if (rec_ptr + sizeof_rec > msg_end) {
717 "got frag rec: should inc usb rx buf size\n");
722 switch (pr->data_type) {
723 case PCAN_USBPRO_RXMSG8:
724 case PCAN_USBPRO_RXMSG4:
725 case PCAN_USBPRO_RXMSG0:
726 case PCAN_USBPRO_RXRTR:
727 err = pcan_usb_pro_handle_canmsg(usb_if, &pr->rx_msg);
732 case PCAN_USBPRO_RXSTATUS:
733 err = pcan_usb_pro_handle_error(usb_if, &pr->rx_status);
738 case PCAN_USBPRO_RXTS:
739 pcan_usb_pro_handle_ts(usb_if, &pr->rx_ts);
744 "unhandled rec type 0x%02x (%d): ignored\n",
745 pr->data_type, pr->data_type);
749 rec_ptr += sizeof_rec;
754 pcan_dump_mem("received msg",
755 urb->transfer_buffer, urb->actual_length);
760 static int pcan_usb_pro_encode_msg(struct peak_usb_device *dev,
761 struct sk_buff *skb, u8 *obuf, size_t *size)
763 struct can_frame *cf = (struct can_frame *)skb->data;
764 u8 data_type, len, flags;
765 struct pcan_usb_pro_msg usb_msg;
767 pcan_msg_init_empty(&usb_msg, obuf, *size);
769 if ((cf->can_id & CAN_RTR_FLAG) || (cf->len == 0))
770 data_type = PCAN_USBPRO_TXMSG0;
771 else if (cf->len <= 4)
772 data_type = PCAN_USBPRO_TXMSG4;
774 data_type = PCAN_USBPRO_TXMSG8;
776 len = (dev->ctrl_idx << 4) | (cf->len & 0x0f);
779 if (cf->can_id & CAN_EFF_FLAG)
780 flags |= PCAN_USBPRO_EXT;
781 if (cf->can_id & CAN_RTR_FLAG)
782 flags |= PCAN_USBPRO_RTR;
784 /* Single-Shot frame */
785 if (dev->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT)
786 flags |= PCAN_USBPRO_SS;
788 pcan_msg_add_rec(&usb_msg, data_type, 0, flags, len, cf->can_id,
791 *size = usb_msg.rec_buffer_len;
796 static int pcan_usb_pro_start(struct peak_usb_device *dev)
798 struct pcan_usb_pro_device *pdev =
799 container_of(dev, struct pcan_usb_pro_device, dev);
802 err = pcan_usb_pro_set_silent(dev,
803 dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY);
807 /* filter mode: 0-> All OFF; 1->bypass */
808 err = pcan_usb_pro_set_filter(dev, 1);
812 /* opening first device: */
813 if (pdev->usb_if->dev_opened_count == 0) {
815 peak_usb_init_time_ref(&pdev->usb_if->time_ref, &pcan_usb_pro);
817 /* ask device to send ts messages */
818 err = pcan_usb_pro_set_ts(dev, 1);
821 pdev->usb_if->dev_opened_count++;
828 * (last chance before set bus off)
830 static int pcan_usb_pro_stop(struct peak_usb_device *dev)
832 struct pcan_usb_pro_device *pdev =
833 container_of(dev, struct pcan_usb_pro_device, dev);
835 /* turn off ts msgs for that interface if no other dev opened */
836 if (pdev->usb_if->dev_opened_count == 1)
837 pcan_usb_pro_set_ts(dev, 0);
839 pdev->usb_if->dev_opened_count--;
845 * called when probing to initialize a device object.
847 static int pcan_usb_pro_init(struct peak_usb_device *dev)
849 struct pcan_usb_pro_device *pdev =
850 container_of(dev, struct pcan_usb_pro_device, dev);
851 struct pcan_usb_pro_interface *usb_if = NULL;
852 struct pcan_usb_pro_fwinfo *fi = NULL;
853 struct pcan_usb_pro_blinfo *bi = NULL;
856 /* do this for 1st channel only */
857 if (!dev->prev_siblings) {
858 /* allocate netdevices common structure attached to first one */
859 usb_if = kzalloc(sizeof(struct pcan_usb_pro_interface),
861 fi = kmalloc(sizeof(struct pcan_usb_pro_fwinfo), GFP_KERNEL);
862 bi = kmalloc(sizeof(struct pcan_usb_pro_blinfo), GFP_KERNEL);
863 if (!usb_if || !fi || !bi) {
868 /* number of ts msgs to ignore before taking one into account */
869 usb_if->cm_ignore_count = 5;
872 * explicit use of dev_xxx() instead of netdev_xxx() here:
873 * information displayed are related to the device itself, not
874 * to the canx netdevices.
876 err = pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_INFO,
880 dev_err(dev->netdev->dev.parent,
881 "unable to read %s firmware info (err %d)\n",
882 pcan_usb_pro.name, err);
886 err = pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_INFO,
890 dev_err(dev->netdev->dev.parent,
891 "unable to read %s bootloader info (err %d)\n",
892 pcan_usb_pro.name, err);
896 /* tell the device the can driver is running */
897 err = pcan_usb_pro_drv_loaded(dev, 1);
901 dev_info(dev->netdev->dev.parent,
902 "PEAK-System %s hwrev %u serial %08X.%08X (%u channels)\n",
904 bi->hw_rev, bi->serial_num_hi, bi->serial_num_lo,
905 pcan_usb_pro.ctrl_count);
907 usb_if = pcan_usb_pro_dev_if(dev->prev_siblings);
910 pdev->usb_if = usb_if;
911 usb_if->dev[dev->ctrl_idx] = dev;
913 /* set LED in default state (end of init phase) */
914 pcan_usb_pro_set_led(dev, PCAN_USBPRO_LED_DEVICE, 1);
929 static void pcan_usb_pro_exit(struct peak_usb_device *dev)
931 struct pcan_usb_pro_device *pdev =
932 container_of(dev, struct pcan_usb_pro_device, dev);
935 * when rmmod called before unplug and if down, should reset things
938 if (dev->can.state != CAN_STATE_STOPPED) {
939 /* set bus off on the corresponding channel */
940 pcan_usb_pro_set_bus(dev, 0);
943 /* if channel #0 (only) */
944 if (dev->ctrl_idx == 0) {
945 /* turn off calibration message if any device were opened */
946 if (pdev->usb_if->dev_opened_count > 0)
947 pcan_usb_pro_set_ts(dev, 0);
949 /* tell the PCAN-USB Pro device the driver is being unloaded */
950 pcan_usb_pro_drv_loaded(dev, 0);
955 * called when PCAN-USB Pro adapter is unplugged
957 static void pcan_usb_pro_free(struct peak_usb_device *dev)
959 /* last device: can free pcan_usb_pro_interface object now */
960 if (!dev->prev_siblings && !dev->next_siblings)
961 kfree(pcan_usb_pro_dev_if(dev));
965 * probe function for new PCAN-USB Pro usb interface
967 int pcan_usb_pro_probe(struct usb_interface *intf)
969 struct usb_host_interface *if_desc;
972 if_desc = intf->altsetting;
974 /* check interface endpoint addresses */
975 for (i = 0; i < if_desc->desc.bNumEndpoints; i++) {
976 struct usb_endpoint_descriptor *ep = &if_desc->endpoint[i].desc;
979 * below is the list of valid ep addresses. Any other ep address
980 * is considered as not-CAN interface address => no dev created
982 switch (ep->bEndpointAddress) {
983 case PCAN_USBPRO_EP_CMDOUT:
984 case PCAN_USBPRO_EP_CMDIN:
985 case PCAN_USBPRO_EP_MSGOUT_0:
986 case PCAN_USBPRO_EP_MSGOUT_1:
987 case PCAN_USBPRO_EP_MSGIN:
988 case PCAN_USBPRO_EP_UNUSED:
998 static int pcan_usb_pro_set_phys_id(struct net_device *netdev,
999 enum ethtool_phys_id_state state)
1001 struct peak_usb_device *dev = netdev_priv(netdev);
1005 case ETHTOOL_ID_ACTIVE:
1006 /* fast blinking forever */
1007 err = pcan_usb_pro_set_led(dev, PCAN_USBPRO_LED_BLINK_FAST,
1011 case ETHTOOL_ID_INACTIVE:
1012 /* restore LED default */
1013 err = pcan_usb_pro_set_led(dev, PCAN_USBPRO_LED_DEVICE, 1);
1023 static const struct ethtool_ops pcan_usb_pro_ethtool_ops = {
1024 .set_phys_id = pcan_usb_pro_set_phys_id,
1028 * describe the PCAN-USB Pro adapter
1030 static const struct can_bittiming_const pcan_usb_pro_const = {
1031 .name = "pcan_usb_pro",
1042 const struct peak_usb_adapter pcan_usb_pro = {
1043 .name = "PCAN-USB Pro",
1044 .device_id = PCAN_USBPRO_PRODUCT_ID,
1045 .ctrl_count = PCAN_USBPRO_CHANNEL_COUNT,
1046 .ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY |
1047 CAN_CTRLMODE_ONE_SHOT,
1049 .freq = PCAN_USBPRO_CRYSTAL_HZ,
1051 .bittiming_const = &pcan_usb_pro_const,
1053 /* size of device private data */
1054 .sizeof_dev_private = sizeof(struct pcan_usb_pro_device),
1056 .ethtool_ops = &pcan_usb_pro_ethtool_ops,
1058 /* timestamps usage */
1060 .us_per_ts_scale = 1, /* us = (ts * scale) >> shift */
1061 .us_per_ts_shift = 0,
1063 /* give here messages in/out endpoints */
1064 .ep_msg_in = PCAN_USBPRO_EP_MSGIN,
1065 .ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0, PCAN_USBPRO_EP_MSGOUT_1},
1067 /* size of rx/tx usb buffers */
1068 .rx_buffer_size = PCAN_USBPRO_RX_BUFFER_SIZE,
1069 .tx_buffer_size = PCAN_USBPRO_TX_BUFFER_SIZE,
1071 /* device callbacks */
1072 .intf_probe = pcan_usb_pro_probe,
1073 .dev_init = pcan_usb_pro_init,
1074 .dev_exit = pcan_usb_pro_exit,
1075 .dev_free = pcan_usb_pro_free,
1076 .dev_set_bus = pcan_usb_pro_set_bus,
1077 .dev_set_bittiming = pcan_usb_pro_set_bittiming,
1078 .dev_get_device_id = pcan_usb_pro_get_device_id,
1079 .dev_decode_buf = pcan_usb_pro_decode_buf,
1080 .dev_encode_msg = pcan_usb_pro_encode_msg,
1081 .dev_start = pcan_usb_pro_start,
1082 .dev_stop = pcan_usb_pro_stop,
1083 .dev_restart_async = pcan_usb_pro_restart_async,