2 * CAN driver for PEAK System PCAN-USB Pro adapter
3 * Derived from the PCAN project file driver/src/pcan_usbpro.c
5 * Copyright (C) 2003-2011 PEAK System-Technik GmbH
6 * Copyright (C) 2011-2012 Stephane Grosjean <s.grosjean@peak-system.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published
10 * by the Free Software Foundation; version 2 of the License.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 #include <linux/netdevice.h>
18 #include <linux/usb.h>
19 #include <linux/module.h>
21 #include <linux/can.h>
22 #include <linux/can/dev.h>
23 #include <linux/can/error.h>
25 #include "pcan_usb_core.h"
26 #include "pcan_usb_pro.h"
28 MODULE_SUPPORTED_DEVICE("PEAK-System PCAN-USB Pro adapter");
30 /* PCAN-USB Pro Endpoints */
31 #define PCAN_USBPRO_EP_CMDOUT 1
32 #define PCAN_USBPRO_EP_CMDIN (PCAN_USBPRO_EP_CMDOUT | USB_DIR_IN)
33 #define PCAN_USBPRO_EP_MSGOUT_0 2
34 #define PCAN_USBPRO_EP_MSGIN (PCAN_USBPRO_EP_MSGOUT_0 | USB_DIR_IN)
35 #define PCAN_USBPRO_EP_MSGOUT_1 3
36 #define PCAN_USBPRO_EP_UNUSED (PCAN_USBPRO_EP_MSGOUT_1 | USB_DIR_IN)
38 #define PCAN_USBPRO_CHANNEL_COUNT 2
40 /* PCAN-USB Pro adapter internal clock (MHz) */
41 #define PCAN_USBPRO_CRYSTAL_HZ 56000000
43 /* PCAN-USB Pro command timeout (ms.) */
44 #define PCAN_USBPRO_COMMAND_TIMEOUT 1000
46 /* PCAN-USB Pro rx/tx buffers size */
47 #define PCAN_USBPRO_RX_BUFFER_SIZE 1024
48 #define PCAN_USBPRO_TX_BUFFER_SIZE 64
50 #define PCAN_USBPRO_MSG_HEADER_LEN 4
52 /* some commands responses need to be re-submitted */
53 #define PCAN_USBPRO_RSP_SUBMIT_MAX 2
55 #define PCAN_USBPRO_RTR 0x01
56 #define PCAN_USBPRO_EXT 0x02
58 #define PCAN_USBPRO_CMD_BUFFER_SIZE 512
60 /* handle device specific info used by the netdevices */
61 struct pcan_usb_pro_interface {
62 struct peak_usb_device *dev[PCAN_USBPRO_CHANNEL_COUNT];
63 struct peak_time_ref time_ref;
68 /* device information */
69 struct pcan_usb_pro_device {
70 struct peak_usb_device dev;
71 struct pcan_usb_pro_interface *usb_if;
75 /* internal structure used to handle messages sent to bulk urb */
76 struct pcan_usb_pro_msg {
87 /* records sizes table indexed on message id. (8-bits value) */
88 static u16 pcan_usb_pro_sizeof_rec[256] = {
89 [PCAN_USBPRO_SETBTR] = sizeof(struct pcan_usb_pro_btr),
90 [PCAN_USBPRO_SETBUSACT] = sizeof(struct pcan_usb_pro_busact),
91 [PCAN_USBPRO_SETSILENT] = sizeof(struct pcan_usb_pro_silent),
92 [PCAN_USBPRO_SETFILTR] = sizeof(struct pcan_usb_pro_filter),
93 [PCAN_USBPRO_SETTS] = sizeof(struct pcan_usb_pro_setts),
94 [PCAN_USBPRO_GETDEVID] = sizeof(struct pcan_usb_pro_devid),
95 [PCAN_USBPRO_SETLED] = sizeof(struct pcan_usb_pro_setled),
96 [PCAN_USBPRO_RXMSG8] = sizeof(struct pcan_usb_pro_rxmsg),
97 [PCAN_USBPRO_RXMSG4] = sizeof(struct pcan_usb_pro_rxmsg) - 4,
98 [PCAN_USBPRO_RXMSG0] = sizeof(struct pcan_usb_pro_rxmsg) - 8,
99 [PCAN_USBPRO_RXRTR] = sizeof(struct pcan_usb_pro_rxmsg) - 8,
100 [PCAN_USBPRO_RXSTATUS] = sizeof(struct pcan_usb_pro_rxstatus),
101 [PCAN_USBPRO_RXTS] = sizeof(struct pcan_usb_pro_rxts),
102 [PCAN_USBPRO_TXMSG8] = sizeof(struct pcan_usb_pro_txmsg),
103 [PCAN_USBPRO_TXMSG4] = sizeof(struct pcan_usb_pro_txmsg) - 4,
104 [PCAN_USBPRO_TXMSG0] = sizeof(struct pcan_usb_pro_txmsg) - 8,
108 * initialize PCAN-USB Pro message data structure
110 static u8 *pcan_msg_init(struct pcan_usb_pro_msg *pm, void *buffer_addr,
113 if (buffer_size < PCAN_USBPRO_MSG_HEADER_LEN)
116 pm->u.rec_buffer = (u8 *)buffer_addr;
117 pm->rec_buffer_size = pm->rec_buffer_len = buffer_size;
118 pm->rec_ptr = pm->u.rec_buffer + PCAN_USBPRO_MSG_HEADER_LEN;
123 static u8 *pcan_msg_init_empty(struct pcan_usb_pro_msg *pm,
124 void *buffer_addr, int buffer_size)
126 u8 *pr = pcan_msg_init(pm, buffer_addr, buffer_size);
129 pm->rec_buffer_len = PCAN_USBPRO_MSG_HEADER_LEN;
136 * add one record to a message being built
138 static int pcan_msg_add_rec(struct pcan_usb_pro_msg *pm, u8 id, ...)
146 pc = pm->rec_ptr + 1;
150 case PCAN_USBPRO_TXMSG8:
152 case PCAN_USBPRO_TXMSG4:
154 case PCAN_USBPRO_TXMSG0:
155 *pc++ = va_arg(ap, int);
156 *pc++ = va_arg(ap, int);
157 *pc++ = va_arg(ap, int);
158 *(u32 *)pc = cpu_to_le32(va_arg(ap, u32));
160 memcpy(pc, va_arg(ap, int *), i);
164 case PCAN_USBPRO_SETBTR:
165 case PCAN_USBPRO_GETDEVID:
166 *pc++ = va_arg(ap, int);
168 *(u32 *)pc = cpu_to_le32(va_arg(ap, u32));
172 case PCAN_USBPRO_SETFILTR:
173 case PCAN_USBPRO_SETBUSACT:
174 case PCAN_USBPRO_SETSILENT:
175 *pc++ = va_arg(ap, int);
176 *(u16 *)pc = cpu_to_le16(va_arg(ap, int));
180 case PCAN_USBPRO_SETLED:
181 *pc++ = va_arg(ap, int);
182 *(u16 *)pc = cpu_to_le16(va_arg(ap, int));
184 *(u32 *)pc = cpu_to_le32(va_arg(ap, u32));
188 case PCAN_USBPRO_SETTS:
190 *(u16 *)pc = cpu_to_le16(va_arg(ap, int));
195 pr_err("%s: %s(): unknown data type %02Xh (%d)\n",
196 PCAN_USB_DRIVER_NAME, __func__, id, id);
201 len = pc - pm->rec_ptr;
203 *pm->u.rec_cnt = cpu_to_le32(*pm->u.rec_cnt+1);
207 pm->rec_buffer_len += len;
216 * send PCAN-USB Pro command synchronously
218 static int pcan_usb_pro_send_cmd(struct peak_usb_device *dev,
219 struct pcan_usb_pro_msg *pum)
224 /* usb device unregistered? */
225 if (!(dev->state & PCAN_USB_STATE_CONNECTED))
228 err = usb_bulk_msg(dev->udev,
229 usb_sndbulkpipe(dev->udev, PCAN_USBPRO_EP_CMDOUT),
230 pum->u.rec_buffer, pum->rec_buffer_len,
231 &actual_length, PCAN_USBPRO_COMMAND_TIMEOUT);
233 netdev_err(dev->netdev, "sending command failure: %d\n", err);
239 * wait for PCAN-USB Pro command response
241 static int pcan_usb_pro_wait_rsp(struct peak_usb_device *dev,
242 struct pcan_usb_pro_msg *pum)
244 u8 req_data_type, req_channel;
248 /* usb device unregistered? */
249 if (!(dev->state & PCAN_USB_STATE_CONNECTED))
252 req_data_type = pum->u.rec_buffer[4];
253 req_channel = pum->u.rec_buffer[5];
256 for (i = 0; !err && i < PCAN_USBPRO_RSP_SUBMIT_MAX; i++) {
257 struct pcan_usb_pro_msg rsp;
258 union pcan_usb_pro_rec *pr;
263 err = usb_bulk_msg(dev->udev,
264 usb_rcvbulkpipe(dev->udev, PCAN_USBPRO_EP_CMDIN),
265 pum->u.rec_buffer, pum->rec_buffer_len,
266 &actual_length, PCAN_USBPRO_COMMAND_TIMEOUT);
268 netdev_err(dev->netdev, "waiting rsp error %d\n", err);
272 if (actual_length == 0)
276 if (actual_length < PCAN_USBPRO_MSG_HEADER_LEN) {
277 netdev_err(dev->netdev,
278 "got abnormal too small rsp (len=%d)\n",
283 pc = pcan_msg_init(&rsp, pum->u.rec_buffer,
286 rec_cnt = le32_to_cpu(*rsp.u.rec_cnt);
288 /* loop on records stored into message */
289 for (r = 0; r < rec_cnt; r++) {
290 pr = (union pcan_usb_pro_rec *)pc;
291 rec_len = pcan_usb_pro_sizeof_rec[pr->data_type];
293 netdev_err(dev->netdev,
294 "got unprocessed record in msg\n");
295 pcan_dump_mem("rcvd rsp msg", pum->u.rec_buffer,
300 /* check if response corresponds to request */
301 if (pr->data_type != req_data_type)
302 netdev_err(dev->netdev,
303 "got unwanted rsp %xh: ignored\n",
306 /* check if channel in response corresponds too */
307 else if ((req_channel != 0xff) && \
308 (pr->bus_act.channel != req_channel))
309 netdev_err(dev->netdev,
310 "got rsp %xh but on chan%u: ignored\n",
311 req_data_type, pr->bus_act.channel);
313 /* got the response */
317 /* otherwise, go on with next record in message */
322 return (i >= PCAN_USBPRO_RSP_SUBMIT_MAX) ? -ERANGE : err;
325 static int pcan_usb_pro_send_req(struct peak_usb_device *dev, int req_id,
326 int req_value, void *req_addr, int req_size)
332 /* usb device unregistered? */
333 if (!(dev->state & PCAN_USB_STATE_CONNECTED))
336 memset(req_addr, '\0', req_size);
338 req_type = USB_TYPE_VENDOR | USB_RECIP_OTHER;
341 case PCAN_USBPRO_REQ_FCT:
342 p = usb_sndctrlpipe(dev->udev, 0);
346 p = usb_rcvctrlpipe(dev->udev, 0);
347 req_type |= USB_DIR_IN;
351 err = usb_control_msg(dev->udev, p, req_id, req_type, req_value, 0,
352 req_addr, req_size, 2 * USB_CTRL_GET_TIMEOUT);
354 netdev_info(dev->netdev,
355 "unable to request usb[type=%d value=%d] err=%d\n",
356 req_id, req_value, err);
363 static int pcan_usb_pro_set_ts(struct peak_usb_device *dev, u16 onoff)
365 struct pcan_usb_pro_msg um;
367 pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
368 pcan_msg_add_rec(&um, PCAN_USBPRO_SETTS, onoff);
370 return pcan_usb_pro_send_cmd(dev, &um);
373 static int pcan_usb_pro_set_bitrate(struct peak_usb_device *dev, u32 ccbt)
375 struct pcan_usb_pro_device *pdev =
376 container_of(dev, struct pcan_usb_pro_device, dev);
377 struct pcan_usb_pro_msg um;
379 pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
380 pcan_msg_add_rec(&um, PCAN_USBPRO_SETBTR, dev->ctrl_idx, ccbt);
382 /* cache the CCBT value to reuse it before next buson */
383 pdev->cached_ccbt = ccbt;
385 return pcan_usb_pro_send_cmd(dev, &um);
388 static int pcan_usb_pro_set_bus(struct peak_usb_device *dev, u8 onoff)
390 struct pcan_usb_pro_msg um;
392 /* if bus=on, be sure the bitrate being set before! */
394 struct pcan_usb_pro_device *pdev =
395 container_of(dev, struct pcan_usb_pro_device, dev);
397 pcan_usb_pro_set_bitrate(dev, pdev->cached_ccbt);
400 pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
401 pcan_msg_add_rec(&um, PCAN_USBPRO_SETBUSACT, dev->ctrl_idx, onoff);
403 return pcan_usb_pro_send_cmd(dev, &um);
406 static int pcan_usb_pro_set_silent(struct peak_usb_device *dev, u8 onoff)
408 struct pcan_usb_pro_msg um;
410 pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
411 pcan_msg_add_rec(&um, PCAN_USBPRO_SETSILENT, dev->ctrl_idx, onoff);
413 return pcan_usb_pro_send_cmd(dev, &um);
416 static int pcan_usb_pro_set_filter(struct peak_usb_device *dev, u16 filter_mode)
418 struct pcan_usb_pro_msg um;
420 pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
421 pcan_msg_add_rec(&um, PCAN_USBPRO_SETFILTR, dev->ctrl_idx, filter_mode);
423 return pcan_usb_pro_send_cmd(dev, &um);
426 static int pcan_usb_pro_set_led(struct peak_usb_device *dev, u8 mode,
429 struct pcan_usb_pro_msg um;
431 pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
432 pcan_msg_add_rec(&um, PCAN_USBPRO_SETLED, dev->ctrl_idx, mode, timeout);
434 return pcan_usb_pro_send_cmd(dev, &um);
437 static int pcan_usb_pro_get_device_id(struct peak_usb_device *dev,
440 struct pcan_usb_pro_devid *pdn;
441 struct pcan_usb_pro_msg um;
445 pc = pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
446 pcan_msg_add_rec(&um, PCAN_USBPRO_GETDEVID, dev->ctrl_idx);
448 err = pcan_usb_pro_send_cmd(dev, &um);
452 err = pcan_usb_pro_wait_rsp(dev, &um);
456 pdn = (struct pcan_usb_pro_devid *)pc;
458 *device_id = le32_to_cpu(pdn->serial_num);
463 static int pcan_usb_pro_set_bittiming(struct peak_usb_device *dev,
464 struct can_bittiming *bt)
468 ccbt = (dev->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) ? 0x00800000 : 0;
469 ccbt |= (bt->sjw - 1) << 24;
470 ccbt |= (bt->phase_seg2 - 1) << 20;
471 ccbt |= (bt->prop_seg + bt->phase_seg1 - 1) << 16; /* = tseg1 */
474 netdev_info(dev->netdev, "setting ccbt=0x%08x\n", ccbt);
476 return pcan_usb_pro_set_bitrate(dev, ccbt);
479 static void pcan_usb_pro_restart_complete(struct urb *urb)
481 /* can delete usb resources */
482 peak_usb_async_complete(urb);
484 /* notify candev and netdev */
485 peak_usb_restart_complete(urb->context);
489 * handle restart but in asynchronously way
491 static int pcan_usb_pro_restart_async(struct peak_usb_device *dev,
492 struct urb *urb, u8 *buf)
494 struct pcan_usb_pro_msg um;
496 pcan_msg_init_empty(&um, buf, PCAN_USB_MAX_CMD_LEN);
497 pcan_msg_add_rec(&um, PCAN_USBPRO_SETBUSACT, dev->ctrl_idx, 1);
499 usb_fill_bulk_urb(urb, dev->udev,
500 usb_sndbulkpipe(dev->udev, PCAN_USBPRO_EP_CMDOUT),
501 buf, PCAN_USB_MAX_CMD_LEN,
502 pcan_usb_pro_restart_complete, dev);
504 return usb_submit_urb(urb, GFP_ATOMIC);
507 static void pcan_usb_pro_drv_loaded(struct peak_usb_device *dev, int loaded)
512 buffer[1] = !!loaded;
514 pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_FCT,
515 PCAN_USBPRO_FCT_DRVLD, buffer, sizeof(buffer));
519 struct pcan_usb_pro_interface *pcan_usb_pro_dev_if(struct peak_usb_device *dev)
521 struct pcan_usb_pro_device *pdev =
522 container_of(dev, struct pcan_usb_pro_device, dev);
526 static int pcan_usb_pro_handle_canmsg(struct pcan_usb_pro_interface *usb_if,
527 struct pcan_usb_pro_rxmsg *rx)
529 const unsigned int ctrl_idx = (rx->len >> 4) & 0x0f;
530 struct peak_usb_device *dev = usb_if->dev[ctrl_idx];
531 struct net_device *netdev = dev->netdev;
532 struct can_frame *can_frame;
535 struct skb_shared_hwtstamps *hwts;
537 skb = alloc_can_skb(netdev, &can_frame);
541 can_frame->can_id = le32_to_cpu(rx->id);
542 can_frame->can_dlc = rx->len & 0x0f;
544 if (rx->flags & PCAN_USBPRO_EXT)
545 can_frame->can_id |= CAN_EFF_FLAG;
547 if (rx->flags & PCAN_USBPRO_RTR)
548 can_frame->can_id |= CAN_RTR_FLAG;
550 memcpy(can_frame->data, rx->data, can_frame->can_dlc);
552 peak_usb_get_ts_tv(&usb_if->time_ref, le32_to_cpu(rx->ts32), &tv);
553 hwts = skb_hwtstamps(skb);
554 hwts->hwtstamp = timeval_to_ktime(tv);
557 netdev->stats.rx_packets++;
558 netdev->stats.rx_bytes += can_frame->can_dlc;
563 static int pcan_usb_pro_handle_error(struct pcan_usb_pro_interface *usb_if,
564 struct pcan_usb_pro_rxstatus *er)
566 const u32 raw_status = le32_to_cpu(er->status);
567 const unsigned int ctrl_idx = (er->channel >> 4) & 0x0f;
568 struct peak_usb_device *dev = usb_if->dev[ctrl_idx];
569 struct net_device *netdev = dev->netdev;
570 struct can_frame *can_frame;
571 enum can_state new_state = CAN_STATE_ERROR_ACTIVE;
575 struct skb_shared_hwtstamps *hwts;
577 /* nothing should be sent while in BUS_OFF state */
578 if (dev->can.state == CAN_STATE_BUS_OFF)
582 /* no error bit (back to active state) */
583 dev->can.state = CAN_STATE_ERROR_ACTIVE;
587 if (raw_status & (PCAN_USBPRO_STATUS_OVERRUN |
588 PCAN_USBPRO_STATUS_QOVERRUN)) {
589 /* trick to bypass next comparison and process other errors */
590 new_state = CAN_STATE_MAX;
593 if (raw_status & PCAN_USBPRO_STATUS_BUS) {
594 new_state = CAN_STATE_BUS_OFF;
595 } else if (raw_status & PCAN_USBPRO_STATUS_ERROR) {
596 u32 rx_err_cnt = (le32_to_cpu(er->err_frm) & 0x00ff0000) >> 16;
597 u32 tx_err_cnt = (le32_to_cpu(er->err_frm) & 0xff000000) >> 24;
599 if (rx_err_cnt > 127)
600 err_mask |= CAN_ERR_CRTL_RX_PASSIVE;
601 else if (rx_err_cnt > 96)
602 err_mask |= CAN_ERR_CRTL_RX_WARNING;
604 if (tx_err_cnt > 127)
605 err_mask |= CAN_ERR_CRTL_TX_PASSIVE;
606 else if (tx_err_cnt > 96)
607 err_mask |= CAN_ERR_CRTL_TX_WARNING;
609 if (err_mask & (CAN_ERR_CRTL_RX_WARNING |
610 CAN_ERR_CRTL_TX_WARNING))
611 new_state = CAN_STATE_ERROR_WARNING;
612 else if (err_mask & (CAN_ERR_CRTL_RX_PASSIVE |
613 CAN_ERR_CRTL_TX_PASSIVE))
614 new_state = CAN_STATE_ERROR_PASSIVE;
617 /* donot post any error if current state didn't change */
618 if (dev->can.state == new_state)
621 /* allocate an skb to store the error frame */
622 skb = alloc_can_err_skb(netdev, &can_frame);
627 case CAN_STATE_BUS_OFF:
628 can_frame->can_id |= CAN_ERR_BUSOFF;
632 case CAN_STATE_ERROR_PASSIVE:
633 can_frame->can_id |= CAN_ERR_CRTL;
634 can_frame->data[1] |= err_mask;
635 dev->can.can_stats.error_passive++;
638 case CAN_STATE_ERROR_WARNING:
639 can_frame->can_id |= CAN_ERR_CRTL;
640 can_frame->data[1] |= err_mask;
641 dev->can.can_stats.error_warning++;
644 case CAN_STATE_ERROR_ACTIVE:
648 /* CAN_STATE_MAX (trick to handle other errors) */
649 if (raw_status & PCAN_USBPRO_STATUS_OVERRUN) {
650 can_frame->can_id |= CAN_ERR_PROT;
651 can_frame->data[2] |= CAN_ERR_PROT_OVERLOAD;
652 netdev->stats.rx_over_errors++;
653 netdev->stats.rx_errors++;
656 if (raw_status & PCAN_USBPRO_STATUS_QOVERRUN) {
657 can_frame->can_id |= CAN_ERR_CRTL;
658 can_frame->data[1] |= CAN_ERR_CRTL_RX_OVERFLOW;
659 netdev->stats.rx_over_errors++;
660 netdev->stats.rx_errors++;
663 new_state = CAN_STATE_ERROR_ACTIVE;
667 dev->can.state = new_state;
669 peak_usb_get_ts_tv(&usb_if->time_ref, le32_to_cpu(er->ts32), &tv);
670 hwts = skb_hwtstamps(skb);
671 hwts->hwtstamp = timeval_to_ktime(tv);
673 netdev->stats.rx_packets++;
674 netdev->stats.rx_bytes += can_frame->can_dlc;
679 static void pcan_usb_pro_handle_ts(struct pcan_usb_pro_interface *usb_if,
680 struct pcan_usb_pro_rxts *ts)
682 /* should wait until clock is stabilized */
683 if (usb_if->cm_ignore_count > 0)
684 usb_if->cm_ignore_count--;
686 peak_usb_set_ts_now(&usb_if->time_ref,
687 le32_to_cpu(ts->ts64[1]));
691 * callback for bulk IN urb
693 static int pcan_usb_pro_decode_buf(struct peak_usb_device *dev, struct urb *urb)
695 struct pcan_usb_pro_interface *usb_if = pcan_usb_pro_dev_if(dev);
696 struct net_device *netdev = dev->netdev;
697 struct pcan_usb_pro_msg usb_msg;
698 u8 *rec_ptr, *msg_end;
702 rec_ptr = pcan_msg_init(&usb_msg, urb->transfer_buffer,
705 netdev_err(netdev, "bad msg hdr len %d\n", urb->actual_length);
709 /* loop reading all the records from the incoming message */
710 msg_end = urb->transfer_buffer + urb->actual_length;
711 rec_cnt = le16_to_cpu(*usb_msg.u.rec_cnt_rd);
712 for (; rec_cnt > 0; rec_cnt--) {
713 union pcan_usb_pro_rec *pr = (union pcan_usb_pro_rec *)rec_ptr;
714 u16 sizeof_rec = pcan_usb_pro_sizeof_rec[pr->data_type];
718 "got unsupported rec in usb msg:\n");
723 /* check if the record goes out of current packet */
724 if (rec_ptr + sizeof_rec > msg_end) {
726 "got frag rec: should inc usb rx buf size\n");
731 switch (pr->data_type) {
732 case PCAN_USBPRO_RXMSG8:
733 case PCAN_USBPRO_RXMSG4:
734 case PCAN_USBPRO_RXMSG0:
735 case PCAN_USBPRO_RXRTR:
736 err = pcan_usb_pro_handle_canmsg(usb_if, &pr->rx_msg);
741 case PCAN_USBPRO_RXSTATUS:
742 err = pcan_usb_pro_handle_error(usb_if, &pr->rx_status);
747 case PCAN_USBPRO_RXTS:
748 pcan_usb_pro_handle_ts(usb_if, &pr->rx_ts);
753 "unhandled rec type 0x%02x (%d): ignored\n",
754 pr->data_type, pr->data_type);
758 rec_ptr += sizeof_rec;
763 pcan_dump_mem("received msg",
764 urb->transfer_buffer, urb->actual_length);
769 static int pcan_usb_pro_encode_msg(struct peak_usb_device *dev,
770 struct sk_buff *skb, u8 *obuf, size_t *size)
772 struct can_frame *cf = (struct can_frame *)skb->data;
773 u8 data_type, len, flags;
774 struct pcan_usb_pro_msg usb_msg;
776 pcan_msg_init_empty(&usb_msg, obuf, *size);
778 if ((cf->can_id & CAN_RTR_FLAG) || (cf->can_dlc == 0))
779 data_type = PCAN_USBPRO_TXMSG0;
780 else if (cf->can_dlc <= 4)
781 data_type = PCAN_USBPRO_TXMSG4;
783 data_type = PCAN_USBPRO_TXMSG8;
785 len = (dev->ctrl_idx << 4) | (cf->can_dlc & 0x0f);
788 if (cf->can_id & CAN_EFF_FLAG)
790 if (cf->can_id & CAN_RTR_FLAG)
793 pcan_msg_add_rec(&usb_msg, data_type, 0, flags, len, cf->can_id,
796 *size = usb_msg.rec_buffer_len;
801 static int pcan_usb_pro_start(struct peak_usb_device *dev)
803 struct pcan_usb_pro_device *pdev =
804 container_of(dev, struct pcan_usb_pro_device, dev);
807 err = pcan_usb_pro_set_silent(dev,
808 dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY);
812 /* filter mode: 0-> All OFF; 1->bypass */
813 err = pcan_usb_pro_set_filter(dev, 1);
817 /* opening first device: */
818 if (pdev->usb_if->dev_opened_count == 0) {
820 peak_usb_init_time_ref(&pdev->usb_if->time_ref, &pcan_usb_pro);
822 /* ask device to send ts messages */
823 err = pcan_usb_pro_set_ts(dev, 1);
826 pdev->usb_if->dev_opened_count++;
833 * (last chance before set bus off)
835 static int pcan_usb_pro_stop(struct peak_usb_device *dev)
837 struct pcan_usb_pro_device *pdev =
838 container_of(dev, struct pcan_usb_pro_device, dev);
840 /* turn off ts msgs for that interface if no other dev opened */
841 if (pdev->usb_if->dev_opened_count == 1)
842 pcan_usb_pro_set_ts(dev, 0);
844 pdev->usb_if->dev_opened_count--;
850 * called when probing to initialize a device object.
852 static int pcan_usb_pro_init(struct peak_usb_device *dev)
854 struct pcan_usb_pro_interface *usb_if;
855 struct pcan_usb_pro_device *pdev =
856 container_of(dev, struct pcan_usb_pro_device, dev);
858 /* do this for 1st channel only */
859 if (!dev->prev_siblings) {
860 struct pcan_usb_pro_fwinfo fi;
861 struct pcan_usb_pro_blinfo bi;
864 /* allocate netdevices common structure attached to first one */
865 usb_if = kzalloc(sizeof(struct pcan_usb_pro_interface),
870 /* number of ts msgs to ignore before taking one into account */
871 usb_if->cm_ignore_count = 5;
874 * explicit use of dev_xxx() instead of netdev_xxx() here:
875 * information displayed are related to the device itself, not
876 * to the canx netdevices.
878 err = pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_INFO,
883 dev_err(dev->netdev->dev.parent,
884 "unable to read %s firmware info (err %d)\n",
885 pcan_usb_pro.name, err);
889 err = pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_INFO,
894 dev_err(dev->netdev->dev.parent,
895 "unable to read %s bootloader info (err %d)\n",
896 pcan_usb_pro.name, err);
900 dev_info(dev->netdev->dev.parent,
901 "PEAK-System %s hwrev %u serial %08X.%08X (%u channels)\n",
903 bi.hw_rev, bi.serial_num_hi, bi.serial_num_lo,
904 pcan_usb_pro.ctrl_count);
906 /* tell the device the can driver is running */
907 pcan_usb_pro_drv_loaded(dev, 1);
909 usb_if = pcan_usb_pro_dev_if(dev->prev_siblings);
912 pdev->usb_if = usb_if;
913 usb_if->dev[dev->ctrl_idx] = dev;
915 /* set LED in default state (end of init phase) */
916 pcan_usb_pro_set_led(dev, 0, 1);
921 static void pcan_usb_pro_exit(struct peak_usb_device *dev)
923 struct pcan_usb_pro_device *pdev =
924 container_of(dev, struct pcan_usb_pro_device, dev);
927 * when rmmod called before unplug and if down, should reset things
930 if (dev->can.state != CAN_STATE_STOPPED) {
931 /* set bus off on the corresponding channel */
932 pcan_usb_pro_set_bus(dev, 0);
935 /* if channel #0 (only) */
936 if (dev->ctrl_idx == 0) {
937 /* turn off calibration message if any device were opened */
938 if (pdev->usb_if->dev_opened_count > 0)
939 pcan_usb_pro_set_ts(dev, 0);
941 /* tell the PCAN-USB Pro device the driver is being unloaded */
942 pcan_usb_pro_drv_loaded(dev, 0);
947 * called when PCAN-USB Pro adapter is unplugged
949 static void pcan_usb_pro_free(struct peak_usb_device *dev)
951 /* last device: can free pcan_usb_pro_interface object now */
952 if (!dev->prev_siblings && !dev->next_siblings)
953 kfree(pcan_usb_pro_dev_if(dev));
957 * probe function for new PCAN-USB Pro usb interface
959 static int pcan_usb_pro_probe(struct usb_interface *intf)
961 struct usb_host_interface *if_desc;
964 if_desc = intf->altsetting;
966 /* check interface endpoint addresses */
967 for (i = 0; i < if_desc->desc.bNumEndpoints; i++) {
968 struct usb_endpoint_descriptor *ep = &if_desc->endpoint[i].desc;
971 * below is the list of valid ep addreses. Any other ep address
972 * is considered as not-CAN interface address => no dev created
974 switch (ep->bEndpointAddress) {
975 case PCAN_USBPRO_EP_CMDOUT:
976 case PCAN_USBPRO_EP_CMDIN:
977 case PCAN_USBPRO_EP_MSGOUT_0:
978 case PCAN_USBPRO_EP_MSGOUT_1:
979 case PCAN_USBPRO_EP_MSGIN:
980 case PCAN_USBPRO_EP_UNUSED:
991 * describe the PCAN-USB Pro adapter
993 struct peak_usb_adapter pcan_usb_pro = {
994 .name = "PCAN-USB Pro",
995 .device_id = PCAN_USBPRO_PRODUCT_ID,
996 .ctrl_count = PCAN_USBPRO_CHANNEL_COUNT,
998 .freq = PCAN_USBPRO_CRYSTAL_HZ,
1000 .bittiming_const = {
1001 .name = "pcan_usb_pro",
1012 /* size of device private data */
1013 .sizeof_dev_private = sizeof(struct pcan_usb_pro_device),
1015 /* timestamps usage */
1017 .ts_period = 1000000, /* calibration period in ts. */
1018 .us_per_ts_scale = 1, /* us = (ts * scale) >> shift */
1019 .us_per_ts_shift = 0,
1021 /* give here messages in/out endpoints */
1022 .ep_msg_in = PCAN_USBPRO_EP_MSGIN,
1023 .ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0, PCAN_USBPRO_EP_MSGOUT_1},
1025 /* size of rx/tx usb buffers */
1026 .rx_buffer_size = PCAN_USBPRO_RX_BUFFER_SIZE,
1027 .tx_buffer_size = PCAN_USBPRO_TX_BUFFER_SIZE,
1029 /* device callbacks */
1030 .intf_probe = pcan_usb_pro_probe,
1031 .dev_init = pcan_usb_pro_init,
1032 .dev_exit = pcan_usb_pro_exit,
1033 .dev_free = pcan_usb_pro_free,
1034 .dev_set_bus = pcan_usb_pro_set_bus,
1035 .dev_set_bittiming = pcan_usb_pro_set_bittiming,
1036 .dev_get_device_id = pcan_usb_pro_get_device_id,
1037 .dev_decode_buf = pcan_usb_pro_decode_buf,
1038 .dev_encode_msg = pcan_usb_pro_encode_msg,
1039 .dev_start = pcan_usb_pro_start,
1040 .dev_stop = pcan_usb_pro_stop,
1041 .dev_restart_async = pcan_usb_pro_restart_async,