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 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;
536 skb = alloc_can_skb(netdev, &can_frame);
540 can_frame->can_id = le32_to_cpu(rx->id);
541 can_frame->can_dlc = rx->len & 0x0f;
543 if (rx->flags & PCAN_USBPRO_EXT)
544 can_frame->can_id |= CAN_EFF_FLAG;
546 if (rx->flags & PCAN_USBPRO_RTR)
547 can_frame->can_id |= CAN_RTR_FLAG;
549 memcpy(can_frame->data, rx->data, can_frame->can_dlc);
551 peak_usb_get_ts_tv(&usb_if->time_ref, le32_to_cpu(rx->ts32), &tv);
552 skb->tstamp = timeval_to_ktime(tv);
555 netdev->stats.rx_packets++;
556 netdev->stats.rx_bytes += can_frame->can_dlc;
561 static int pcan_usb_pro_handle_error(struct pcan_usb_pro_interface *usb_if,
562 struct pcan_usb_pro_rxstatus *er)
564 const u32 raw_status = le32_to_cpu(er->status);
565 const unsigned int ctrl_idx = (er->channel >> 4) & 0x0f;
566 struct peak_usb_device *dev = usb_if->dev[ctrl_idx];
567 struct net_device *netdev = dev->netdev;
568 struct can_frame *can_frame;
569 enum can_state new_state = CAN_STATE_ERROR_ACTIVE;
574 /* nothing should be sent while in BUS_OFF state */
575 if (dev->can.state == CAN_STATE_BUS_OFF)
579 /* no error bit (back to active state) */
580 dev->can.state = CAN_STATE_ERROR_ACTIVE;
584 if (raw_status & (PCAN_USBPRO_STATUS_OVERRUN |
585 PCAN_USBPRO_STATUS_QOVERRUN)) {
586 /* trick to bypass next comparison and process other errors */
587 new_state = CAN_STATE_MAX;
590 if (raw_status & PCAN_USBPRO_STATUS_BUS) {
591 new_state = CAN_STATE_BUS_OFF;
592 } else if (raw_status & PCAN_USBPRO_STATUS_ERROR) {
593 u32 rx_err_cnt = (le32_to_cpu(er->err_frm) & 0x00ff0000) >> 16;
594 u32 tx_err_cnt = (le32_to_cpu(er->err_frm) & 0xff000000) >> 24;
596 if (rx_err_cnt > 127)
597 err_mask |= CAN_ERR_CRTL_RX_PASSIVE;
598 else if (rx_err_cnt > 96)
599 err_mask |= CAN_ERR_CRTL_RX_WARNING;
601 if (tx_err_cnt > 127)
602 err_mask |= CAN_ERR_CRTL_TX_PASSIVE;
603 else if (tx_err_cnt > 96)
604 err_mask |= CAN_ERR_CRTL_TX_WARNING;
606 if (err_mask & (CAN_ERR_CRTL_RX_WARNING |
607 CAN_ERR_CRTL_TX_WARNING))
608 new_state = CAN_STATE_ERROR_WARNING;
609 else if (err_mask & (CAN_ERR_CRTL_RX_PASSIVE |
610 CAN_ERR_CRTL_TX_PASSIVE))
611 new_state = CAN_STATE_ERROR_PASSIVE;
614 /* donot post any error if current state didn't change */
615 if (dev->can.state == new_state)
618 /* allocate an skb to store the error frame */
619 skb = alloc_can_err_skb(netdev, &can_frame);
624 case CAN_STATE_BUS_OFF:
625 can_frame->can_id |= CAN_ERR_BUSOFF;
629 case CAN_STATE_ERROR_PASSIVE:
630 can_frame->can_id |= CAN_ERR_CRTL;
631 can_frame->data[1] |= err_mask;
632 dev->can.can_stats.error_passive++;
635 case CAN_STATE_ERROR_WARNING:
636 can_frame->can_id |= CAN_ERR_CRTL;
637 can_frame->data[1] |= err_mask;
638 dev->can.can_stats.error_warning++;
641 case CAN_STATE_ERROR_ACTIVE:
645 /* CAN_STATE_MAX (trick to handle other errors) */
646 if (raw_status & PCAN_USBPRO_STATUS_OVERRUN) {
647 can_frame->can_id |= CAN_ERR_PROT;
648 can_frame->data[2] |= CAN_ERR_PROT_OVERLOAD;
649 netdev->stats.rx_over_errors++;
650 netdev->stats.rx_errors++;
653 if (raw_status & PCAN_USBPRO_STATUS_QOVERRUN) {
654 can_frame->can_id |= CAN_ERR_CRTL;
655 can_frame->data[1] |= CAN_ERR_CRTL_RX_OVERFLOW;
656 netdev->stats.rx_over_errors++;
657 netdev->stats.rx_errors++;
660 new_state = CAN_STATE_ERROR_ACTIVE;
664 dev->can.state = new_state;
666 peak_usb_get_ts_tv(&usb_if->time_ref, le32_to_cpu(er->ts32), &tv);
667 skb->tstamp = timeval_to_ktime(tv);
669 netdev->stats.rx_packets++;
670 netdev->stats.rx_bytes += can_frame->can_dlc;
675 static void pcan_usb_pro_handle_ts(struct pcan_usb_pro_interface *usb_if,
676 struct pcan_usb_pro_rxts *ts)
678 /* should wait until clock is stabilized */
679 if (usb_if->cm_ignore_count > 0)
680 usb_if->cm_ignore_count--;
682 peak_usb_set_ts_now(&usb_if->time_ref,
683 le32_to_cpu(ts->ts64[1]));
687 * callback for bulk IN urb
689 static int pcan_usb_pro_decode_buf(struct peak_usb_device *dev, struct urb *urb)
691 struct pcan_usb_pro_interface *usb_if = pcan_usb_pro_dev_if(dev);
692 struct net_device *netdev = dev->netdev;
693 struct pcan_usb_pro_msg usb_msg;
694 u8 *rec_ptr, *msg_end;
698 rec_ptr = pcan_msg_init(&usb_msg, urb->transfer_buffer,
701 netdev_err(netdev, "bad msg hdr len %d\n", urb->actual_length);
705 /* loop reading all the records from the incoming message */
706 msg_end = urb->transfer_buffer + urb->actual_length;
707 rec_cnt = le16_to_cpu(*usb_msg.u.rec_cnt_rd);
708 for (; rec_cnt > 0; rec_cnt--) {
709 union pcan_usb_pro_rec *pr = (union pcan_usb_pro_rec *)rec_ptr;
710 u16 sizeof_rec = pcan_usb_pro_sizeof_rec[pr->data_type];
714 "got unsupported rec in usb msg:\n");
719 /* check if the record goes out of current packet */
720 if (rec_ptr + sizeof_rec > msg_end) {
722 "got frag rec: should inc usb rx buf size\n");
727 switch (pr->data_type) {
728 case PCAN_USBPRO_RXMSG8:
729 case PCAN_USBPRO_RXMSG4:
730 case PCAN_USBPRO_RXMSG0:
731 case PCAN_USBPRO_RXRTR:
732 err = pcan_usb_pro_handle_canmsg(usb_if, &pr->rx_msg);
737 case PCAN_USBPRO_RXSTATUS:
738 err = pcan_usb_pro_handle_error(usb_if, &pr->rx_status);
743 case PCAN_USBPRO_RXTS:
744 pcan_usb_pro_handle_ts(usb_if, &pr->rx_ts);
749 "unhandled rec type 0x%02x (%d): ignored\n",
750 pr->data_type, pr->data_type);
754 rec_ptr += sizeof_rec;
759 dump_mem("received msg",
760 urb->transfer_buffer, urb->actual_length);
765 static int pcan_usb_pro_encode_msg(struct peak_usb_device *dev,
766 struct sk_buff *skb, u8 *obuf, size_t *size)
768 struct can_frame *cf = (struct can_frame *)skb->data;
769 u8 data_type, len, flags;
770 struct pcan_usb_pro_msg usb_msg;
772 pcan_msg_init_empty(&usb_msg, obuf, *size);
774 if ((cf->can_id & CAN_RTR_FLAG) || (cf->can_dlc == 0))
775 data_type = PCAN_USBPRO_TXMSG0;
776 else if (cf->can_dlc <= 4)
777 data_type = PCAN_USBPRO_TXMSG4;
779 data_type = PCAN_USBPRO_TXMSG8;
781 len = (dev->ctrl_idx << 4) | (cf->can_dlc & 0x0f);
784 if (cf->can_id & CAN_EFF_FLAG)
786 if (cf->can_id & CAN_RTR_FLAG)
789 pcan_msg_add_rec(&usb_msg, data_type, 0, flags, len, cf->can_id,
792 *size = usb_msg.rec_buffer_len;
797 static int pcan_usb_pro_start(struct peak_usb_device *dev)
799 struct pcan_usb_pro_device *pdev =
800 container_of(dev, struct pcan_usb_pro_device, dev);
803 err = pcan_usb_pro_set_silent(dev,
804 dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY);
808 /* filter mode: 0-> All OFF; 1->bypass */
809 err = pcan_usb_pro_set_filter(dev, 1);
813 /* opening first device: */
814 if (pdev->usb_if->dev_opened_count == 0) {
816 peak_usb_init_time_ref(&pdev->usb_if->time_ref, &pcan_usb_pro);
818 /* ask device to send ts messages */
819 err = pcan_usb_pro_set_ts(dev, 1);
822 pdev->usb_if->dev_opened_count++;
829 * (last chance before set bus off)
831 static int pcan_usb_pro_stop(struct peak_usb_device *dev)
833 struct pcan_usb_pro_device *pdev =
834 container_of(dev, struct pcan_usb_pro_device, dev);
836 /* turn off ts msgs for that interface if no other dev opened */
837 if (pdev->usb_if->dev_opened_count == 1)
838 pcan_usb_pro_set_ts(dev, 0);
840 pdev->usb_if->dev_opened_count--;
846 * called when probing to initialize a device object.
848 static int pcan_usb_pro_init(struct peak_usb_device *dev)
850 struct pcan_usb_pro_interface *usb_if;
851 struct pcan_usb_pro_device *pdev =
852 container_of(dev, struct pcan_usb_pro_device, dev);
854 /* do this for 1st channel only */
855 if (!dev->prev_siblings) {
856 struct pcan_usb_pro_fwinfo fi;
857 struct pcan_usb_pro_blinfo bi;
860 /* allocate netdevices common structure attached to first one */
861 usb_if = kzalloc(sizeof(struct pcan_usb_pro_interface),
866 /* number of ts msgs to ignore before taking one into account */
867 usb_if->cm_ignore_count = 5;
870 * explicit use of dev_xxx() instead of netdev_xxx() here:
871 * information displayed are related to the device itself, not
872 * to the canx netdevices.
874 err = pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_INFO,
879 dev_err(dev->netdev->dev.parent,
880 "unable to read %s firmware info (err %d)\n",
881 pcan_usb_pro.name, err);
885 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 dev_info(dev->netdev->dev.parent,
897 "PEAK-System %s hwrev %u serial %08X.%08X (%u channels)\n",
899 bi.hw_rev, bi.serial_num_hi, bi.serial_num_lo,
900 pcan_usb_pro.ctrl_count);
902 /* tell the device the can driver is running */
903 pcan_usb_pro_drv_loaded(dev, 1);
905 usb_if = pcan_usb_pro_dev_if(dev->prev_siblings);
908 pdev->usb_if = usb_if;
909 usb_if->dev[dev->ctrl_idx] = dev;
911 /* set LED in default state (end of init phase) */
912 pcan_usb_pro_set_led(dev, 0, 1);
917 static void pcan_usb_pro_exit(struct peak_usb_device *dev)
919 struct pcan_usb_pro_device *pdev =
920 container_of(dev, struct pcan_usb_pro_device, dev);
923 * when rmmod called before unplug and if down, should reset things
926 if (dev->can.state != CAN_STATE_STOPPED) {
927 /* set bus off on the corresponding channel */
928 pcan_usb_pro_set_bus(dev, 0);
931 /* if channel #0 (only) */
932 if (dev->ctrl_idx == 0) {
933 /* turn off calibration message if any device were opened */
934 if (pdev->usb_if->dev_opened_count > 0)
935 pcan_usb_pro_set_ts(dev, 0);
937 /* tell the PCAN-USB Pro device the driver is being unloaded */
938 pcan_usb_pro_drv_loaded(dev, 0);
943 * called when PCAN-USB Pro adapter is unplugged
945 static void pcan_usb_pro_free(struct peak_usb_device *dev)
947 /* last device: can free pcan_usb_pro_interface object now */
948 if (!dev->prev_siblings && !dev->next_siblings)
949 kfree(pcan_usb_pro_dev_if(dev));
953 * probe function for new PCAN-USB Pro usb interface
955 static int pcan_usb_pro_probe(struct usb_interface *intf)
957 struct usb_host_interface *if_desc;
960 if_desc = intf->altsetting;
962 /* check interface endpoint addresses */
963 for (i = 0; i < if_desc->desc.bNumEndpoints; i++) {
964 struct usb_endpoint_descriptor *ep = &if_desc->endpoint[i].desc;
967 * below is the list of valid ep addreses. Any other ep address
968 * is considered as not-CAN interface address => no dev created
970 switch (ep->bEndpointAddress) {
971 case PCAN_USBPRO_EP_CMDOUT:
972 case PCAN_USBPRO_EP_CMDIN:
973 case PCAN_USBPRO_EP_MSGOUT_0:
974 case PCAN_USBPRO_EP_MSGOUT_1:
975 case PCAN_USBPRO_EP_MSGIN:
976 case PCAN_USBPRO_EP_UNUSED:
987 * describe the PCAN-USB Pro adapter
989 struct peak_usb_adapter pcan_usb_pro = {
990 .name = "PCAN-USB Pro",
991 .device_id = PCAN_USBPRO_PRODUCT_ID,
992 .ctrl_count = PCAN_USBPRO_CHANNEL_COUNT,
994 .freq = PCAN_USBPRO_CRYSTAL_HZ,
997 .name = "pcan_usb_pro",
1008 /* size of device private data */
1009 .sizeof_dev_private = sizeof(struct pcan_usb_pro_device),
1011 /* timestamps usage */
1013 .ts_period = 1000000, /* calibration period in ts. */
1014 .us_per_ts_scale = 1, /* us = (ts * scale) >> shift */
1015 .us_per_ts_shift = 0,
1017 /* give here messages in/out endpoints */
1018 .ep_msg_in = PCAN_USBPRO_EP_MSGIN,
1019 .ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0, PCAN_USBPRO_EP_MSGOUT_1},
1021 /* size of rx/tx usb buffers */
1022 .rx_buffer_size = PCAN_USBPRO_RX_BUFFER_SIZE,
1023 .tx_buffer_size = PCAN_USBPRO_TX_BUFFER_SIZE,
1025 /* device callbacks */
1026 .intf_probe = pcan_usb_pro_probe,
1027 .dev_init = pcan_usb_pro_init,
1028 .dev_exit = pcan_usb_pro_exit,
1029 .dev_free = pcan_usb_pro_free,
1030 .dev_set_bus = pcan_usb_pro_set_bus,
1031 .dev_set_bittiming = pcan_usb_pro_set_bittiming,
1032 .dev_get_device_id = pcan_usb_pro_get_device_id,
1033 .dev_decode_buf = pcan_usb_pro_decode_buf,
1034 .dev_encode_msg = pcan_usb_pro_encode_msg,
1035 .dev_start = pcan_usb_pro_start,
1036 .dev_stop = pcan_usb_pro_stop,
1037 .dev_restart_async = pcan_usb_pro_restart_async,