Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / net / can / usb / peak_usb / pcan_usb_pro.c
blob0105fbfea273ad4c874006d72e5c0e94dbfb8b95
1 /*
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 #define PCAN_USBPRO_CHANNEL_COUNT 2
32 /* PCAN-USB Pro adapter internal clock (MHz) */
33 #define PCAN_USBPRO_CRYSTAL_HZ 56000000
35 /* PCAN-USB Pro command timeout (ms.) */
36 #define PCAN_USBPRO_COMMAND_TIMEOUT 1000
38 /* PCAN-USB Pro rx/tx buffers size */
39 #define PCAN_USBPRO_RX_BUFFER_SIZE 1024
40 #define PCAN_USBPRO_TX_BUFFER_SIZE 64
42 #define PCAN_USBPRO_MSG_HEADER_LEN 4
44 /* some commands responses need to be re-submitted */
45 #define PCAN_USBPRO_RSP_SUBMIT_MAX 2
47 #define PCAN_USBPRO_RTR 0x01
48 #define PCAN_USBPRO_EXT 0x02
50 #define PCAN_USBPRO_CMD_BUFFER_SIZE 512
52 /* handle device specific info used by the netdevices */
53 struct pcan_usb_pro_interface {
54 struct peak_usb_device *dev[PCAN_USBPRO_CHANNEL_COUNT];
55 struct peak_time_ref time_ref;
56 int cm_ignore_count;
57 int dev_opened_count;
60 /* device information */
61 struct pcan_usb_pro_device {
62 struct peak_usb_device dev;
63 struct pcan_usb_pro_interface *usb_if;
64 u32 cached_ccbt;
67 /* internal structure used to handle messages sent to bulk urb */
68 struct pcan_usb_pro_msg {
69 u8 *rec_ptr;
70 int rec_buffer_size;
71 int rec_buffer_len;
72 union {
73 __le16 *rec_cnt_rd;
74 __le32 *rec_cnt;
75 u8 *rec_buffer;
76 } u;
79 /* records sizes table indexed on message id. (8-bits value) */
80 static u16 pcan_usb_pro_sizeof_rec[256] = {
81 [PCAN_USBPRO_SETBTR] = sizeof(struct pcan_usb_pro_btr),
82 [PCAN_USBPRO_SETBUSACT] = sizeof(struct pcan_usb_pro_busact),
83 [PCAN_USBPRO_SETSILENT] = sizeof(struct pcan_usb_pro_silent),
84 [PCAN_USBPRO_SETFILTR] = sizeof(struct pcan_usb_pro_filter),
85 [PCAN_USBPRO_SETTS] = sizeof(struct pcan_usb_pro_setts),
86 [PCAN_USBPRO_GETDEVID] = sizeof(struct pcan_usb_pro_devid),
87 [PCAN_USBPRO_SETLED] = sizeof(struct pcan_usb_pro_setled),
88 [PCAN_USBPRO_RXMSG8] = sizeof(struct pcan_usb_pro_rxmsg),
89 [PCAN_USBPRO_RXMSG4] = sizeof(struct pcan_usb_pro_rxmsg) - 4,
90 [PCAN_USBPRO_RXMSG0] = sizeof(struct pcan_usb_pro_rxmsg) - 8,
91 [PCAN_USBPRO_RXRTR] = sizeof(struct pcan_usb_pro_rxmsg) - 8,
92 [PCAN_USBPRO_RXSTATUS] = sizeof(struct pcan_usb_pro_rxstatus),
93 [PCAN_USBPRO_RXTS] = sizeof(struct pcan_usb_pro_rxts),
94 [PCAN_USBPRO_TXMSG8] = sizeof(struct pcan_usb_pro_txmsg),
95 [PCAN_USBPRO_TXMSG4] = sizeof(struct pcan_usb_pro_txmsg) - 4,
96 [PCAN_USBPRO_TXMSG0] = sizeof(struct pcan_usb_pro_txmsg) - 8,
100 * initialize PCAN-USB Pro message data structure
102 static u8 *pcan_msg_init(struct pcan_usb_pro_msg *pm, void *buffer_addr,
103 int buffer_size)
105 if (buffer_size < PCAN_USBPRO_MSG_HEADER_LEN)
106 return NULL;
108 pm->u.rec_buffer = (u8 *)buffer_addr;
109 pm->rec_buffer_size = pm->rec_buffer_len = buffer_size;
110 pm->rec_ptr = pm->u.rec_buffer + PCAN_USBPRO_MSG_HEADER_LEN;
112 return pm->rec_ptr;
115 static u8 *pcan_msg_init_empty(struct pcan_usb_pro_msg *pm,
116 void *buffer_addr, int buffer_size)
118 u8 *pr = pcan_msg_init(pm, buffer_addr, buffer_size);
120 if (pr) {
121 pm->rec_buffer_len = PCAN_USBPRO_MSG_HEADER_LEN;
122 *pm->u.rec_cnt = 0;
124 return pr;
128 * add one record to a message being built
130 static int pcan_msg_add_rec(struct pcan_usb_pro_msg *pm, u8 id, ...)
132 int len, i;
133 u8 *pc;
134 va_list ap;
136 va_start(ap, id);
138 pc = pm->rec_ptr + 1;
140 i = 0;
141 switch (id) {
142 case PCAN_USBPRO_TXMSG8:
143 i += 4;
144 case PCAN_USBPRO_TXMSG4:
145 i += 4;
146 case PCAN_USBPRO_TXMSG0:
147 *pc++ = va_arg(ap, int);
148 *pc++ = va_arg(ap, int);
149 *pc++ = va_arg(ap, int);
150 *(__le32 *)pc = cpu_to_le32(va_arg(ap, u32));
151 pc += 4;
152 memcpy(pc, va_arg(ap, int *), i);
153 pc += i;
154 break;
156 case PCAN_USBPRO_SETBTR:
157 case PCAN_USBPRO_GETDEVID:
158 *pc++ = va_arg(ap, int);
159 pc += 2;
160 *(__le32 *)pc = cpu_to_le32(va_arg(ap, u32));
161 pc += 4;
162 break;
164 case PCAN_USBPRO_SETFILTR:
165 case PCAN_USBPRO_SETBUSACT:
166 case PCAN_USBPRO_SETSILENT:
167 *pc++ = va_arg(ap, int);
168 *(__le16 *)pc = cpu_to_le16(va_arg(ap, int));
169 pc += 2;
170 break;
172 case PCAN_USBPRO_SETLED:
173 *pc++ = va_arg(ap, int);
174 *(__le16 *)pc = cpu_to_le16(va_arg(ap, int));
175 pc += 2;
176 *(__le32 *)pc = cpu_to_le32(va_arg(ap, u32));
177 pc += 4;
178 break;
180 case PCAN_USBPRO_SETTS:
181 pc++;
182 *(__le16 *)pc = cpu_to_le16(va_arg(ap, int));
183 pc += 2;
184 break;
186 default:
187 pr_err("%s: %s(): unknown data type %02Xh (%d)\n",
188 PCAN_USB_DRIVER_NAME, __func__, id, id);
189 pc--;
190 break;
193 len = pc - pm->rec_ptr;
194 if (len > 0) {
195 *pm->u.rec_cnt = cpu_to_le32(le32_to_cpu(*pm->u.rec_cnt) + 1);
196 *pm->rec_ptr = id;
198 pm->rec_ptr = pc;
199 pm->rec_buffer_len += len;
202 va_end(ap);
204 return len;
208 * send PCAN-USB Pro command synchronously
210 static int pcan_usb_pro_send_cmd(struct peak_usb_device *dev,
211 struct pcan_usb_pro_msg *pum)
213 int actual_length;
214 int err;
216 /* usb device unregistered? */
217 if (!(dev->state & PCAN_USB_STATE_CONNECTED))
218 return 0;
220 err = usb_bulk_msg(dev->udev,
221 usb_sndbulkpipe(dev->udev, PCAN_USBPRO_EP_CMDOUT),
222 pum->u.rec_buffer, pum->rec_buffer_len,
223 &actual_length, PCAN_USBPRO_COMMAND_TIMEOUT);
224 if (err)
225 netdev_err(dev->netdev, "sending command failure: %d\n", err);
227 return err;
231 * wait for PCAN-USB Pro command response
233 static int pcan_usb_pro_wait_rsp(struct peak_usb_device *dev,
234 struct pcan_usb_pro_msg *pum)
236 u8 req_data_type, req_channel;
237 int actual_length;
238 int i, err = 0;
240 /* usb device unregistered? */
241 if (!(dev->state & PCAN_USB_STATE_CONNECTED))
242 return 0;
244 req_data_type = pum->u.rec_buffer[4];
245 req_channel = pum->u.rec_buffer[5];
247 *pum->u.rec_cnt = 0;
248 for (i = 0; !err && i < PCAN_USBPRO_RSP_SUBMIT_MAX; i++) {
249 struct pcan_usb_pro_msg rsp;
250 union pcan_usb_pro_rec *pr;
251 u32 r, rec_cnt;
252 u16 rec_len;
253 u8 *pc;
255 err = usb_bulk_msg(dev->udev,
256 usb_rcvbulkpipe(dev->udev, PCAN_USBPRO_EP_CMDIN),
257 pum->u.rec_buffer, pum->rec_buffer_len,
258 &actual_length, PCAN_USBPRO_COMMAND_TIMEOUT);
259 if (err) {
260 netdev_err(dev->netdev, "waiting rsp error %d\n", err);
261 break;
264 if (actual_length == 0)
265 continue;
267 err = -EBADMSG;
268 if (actual_length < PCAN_USBPRO_MSG_HEADER_LEN) {
269 netdev_err(dev->netdev,
270 "got abnormal too small rsp (len=%d)\n",
271 actual_length);
272 break;
275 pc = pcan_msg_init(&rsp, pum->u.rec_buffer,
276 actual_length);
278 rec_cnt = le32_to_cpu(*rsp.u.rec_cnt);
280 /* loop on records stored into message */
281 for (r = 0; r < rec_cnt; r++) {
282 pr = (union pcan_usb_pro_rec *)pc;
283 rec_len = pcan_usb_pro_sizeof_rec[pr->data_type];
284 if (!rec_len) {
285 netdev_err(dev->netdev,
286 "got unprocessed record in msg\n");
287 pcan_dump_mem("rcvd rsp msg", pum->u.rec_buffer,
288 actual_length);
289 break;
292 /* check if response corresponds to request */
293 if (pr->data_type != req_data_type)
294 netdev_err(dev->netdev,
295 "got unwanted rsp %xh: ignored\n",
296 pr->data_type);
298 /* check if channel in response corresponds too */
299 else if ((req_channel != 0xff) && \
300 (pr->bus_act.channel != req_channel))
301 netdev_err(dev->netdev,
302 "got rsp %xh but on chan%u: ignored\n",
303 req_data_type, pr->bus_act.channel);
305 /* got the response */
306 else
307 return 0;
309 /* otherwise, go on with next record in message */
310 pc += rec_len;
314 return (i >= PCAN_USBPRO_RSP_SUBMIT_MAX) ? -ERANGE : err;
317 int pcan_usb_pro_send_req(struct peak_usb_device *dev, int req_id,
318 int req_value, void *req_addr, int req_size)
320 int err;
321 u8 req_type;
322 unsigned int p;
324 /* usb device unregistered? */
325 if (!(dev->state & PCAN_USB_STATE_CONNECTED))
326 return 0;
328 req_type = USB_TYPE_VENDOR | USB_RECIP_OTHER;
330 switch (req_id) {
331 case PCAN_USBPRO_REQ_FCT:
332 p = usb_sndctrlpipe(dev->udev, 0);
333 break;
335 default:
336 p = usb_rcvctrlpipe(dev->udev, 0);
337 req_type |= USB_DIR_IN;
338 memset(req_addr, '\0', req_size);
339 break;
342 err = usb_control_msg(dev->udev, p, req_id, req_type, req_value, 0,
343 req_addr, req_size, 2 * USB_CTRL_GET_TIMEOUT);
344 if (err < 0) {
345 netdev_info(dev->netdev,
346 "unable to request usb[type=%d value=%d] err=%d\n",
347 req_id, req_value, err);
348 return err;
351 return 0;
354 static int pcan_usb_pro_set_ts(struct peak_usb_device *dev, u16 onoff)
356 struct pcan_usb_pro_msg um;
358 pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
359 pcan_msg_add_rec(&um, PCAN_USBPRO_SETTS, onoff);
361 return pcan_usb_pro_send_cmd(dev, &um);
364 static int pcan_usb_pro_set_bitrate(struct peak_usb_device *dev, u32 ccbt)
366 struct pcan_usb_pro_device *pdev =
367 container_of(dev, struct pcan_usb_pro_device, dev);
368 struct pcan_usb_pro_msg um;
370 pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
371 pcan_msg_add_rec(&um, PCAN_USBPRO_SETBTR, dev->ctrl_idx, ccbt);
373 /* cache the CCBT value to reuse it before next buson */
374 pdev->cached_ccbt = ccbt;
376 return pcan_usb_pro_send_cmd(dev, &um);
379 static int pcan_usb_pro_set_bus(struct peak_usb_device *dev, u8 onoff)
381 struct pcan_usb_pro_msg um;
383 /* if bus=on, be sure the bitrate being set before! */
384 if (onoff) {
385 struct pcan_usb_pro_device *pdev =
386 container_of(dev, struct pcan_usb_pro_device, dev);
388 pcan_usb_pro_set_bitrate(dev, pdev->cached_ccbt);
391 pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
392 pcan_msg_add_rec(&um, PCAN_USBPRO_SETBUSACT, dev->ctrl_idx, onoff);
394 return pcan_usb_pro_send_cmd(dev, &um);
397 static int pcan_usb_pro_set_silent(struct peak_usb_device *dev, u8 onoff)
399 struct pcan_usb_pro_msg um;
401 pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
402 pcan_msg_add_rec(&um, PCAN_USBPRO_SETSILENT, dev->ctrl_idx, onoff);
404 return pcan_usb_pro_send_cmd(dev, &um);
407 static int pcan_usb_pro_set_filter(struct peak_usb_device *dev, u16 filter_mode)
409 struct pcan_usb_pro_msg um;
411 pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
412 pcan_msg_add_rec(&um, PCAN_USBPRO_SETFILTR, dev->ctrl_idx, filter_mode);
414 return pcan_usb_pro_send_cmd(dev, &um);
417 static int pcan_usb_pro_set_led(struct peak_usb_device *dev, u8 mode,
418 u32 timeout)
420 struct pcan_usb_pro_msg um;
422 pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
423 pcan_msg_add_rec(&um, PCAN_USBPRO_SETLED, dev->ctrl_idx, mode, timeout);
425 return pcan_usb_pro_send_cmd(dev, &um);
428 static int pcan_usb_pro_get_device_id(struct peak_usb_device *dev,
429 u32 *device_id)
431 struct pcan_usb_pro_devid *pdn;
432 struct pcan_usb_pro_msg um;
433 int err;
434 u8 *pc;
436 pc = pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
437 pcan_msg_add_rec(&um, PCAN_USBPRO_GETDEVID, dev->ctrl_idx);
439 err = pcan_usb_pro_send_cmd(dev, &um);
440 if (err)
441 return err;
443 err = pcan_usb_pro_wait_rsp(dev, &um);
444 if (err)
445 return err;
447 pdn = (struct pcan_usb_pro_devid *)pc;
448 if (device_id)
449 *device_id = le32_to_cpu(pdn->serial_num);
451 return err;
454 static int pcan_usb_pro_set_bittiming(struct peak_usb_device *dev,
455 struct can_bittiming *bt)
457 u32 ccbt;
459 ccbt = (dev->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) ? 0x00800000 : 0;
460 ccbt |= (bt->sjw - 1) << 24;
461 ccbt |= (bt->phase_seg2 - 1) << 20;
462 ccbt |= (bt->prop_seg + bt->phase_seg1 - 1) << 16; /* = tseg1 */
463 ccbt |= bt->brp - 1;
465 netdev_info(dev->netdev, "setting ccbt=0x%08x\n", ccbt);
467 return pcan_usb_pro_set_bitrate(dev, ccbt);
470 void pcan_usb_pro_restart_complete(struct urb *urb)
472 /* can delete usb resources */
473 peak_usb_async_complete(urb);
475 /* notify candev and netdev */
476 peak_usb_restart_complete(urb->context);
480 * handle restart but in asynchronously way
482 static int pcan_usb_pro_restart_async(struct peak_usb_device *dev,
483 struct urb *urb, u8 *buf)
485 struct pcan_usb_pro_msg um;
487 pcan_msg_init_empty(&um, buf, PCAN_USB_MAX_CMD_LEN);
488 pcan_msg_add_rec(&um, PCAN_USBPRO_SETBUSACT, dev->ctrl_idx, 1);
490 usb_fill_bulk_urb(urb, dev->udev,
491 usb_sndbulkpipe(dev->udev, PCAN_USBPRO_EP_CMDOUT),
492 buf, PCAN_USB_MAX_CMD_LEN,
493 pcan_usb_pro_restart_complete, dev);
495 return usb_submit_urb(urb, GFP_ATOMIC);
498 static int pcan_usb_pro_drv_loaded(struct peak_usb_device *dev, int loaded)
500 u8 *buffer;
501 int err;
503 buffer = kmalloc(PCAN_USBPRO_FCT_DRVLD_REQ_LEN, GFP_KERNEL);
504 if (!buffer)
505 return -ENOMEM;
507 buffer[0] = 0;
508 buffer[1] = !!loaded;
510 err = pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_FCT,
511 PCAN_USBPRO_FCT_DRVLD, buffer,
512 PCAN_USBPRO_FCT_DRVLD_REQ_LEN);
513 kfree(buffer);
515 return err;
518 static inline
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);
523 return pdev->usb_if;
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;
533 struct sk_buff *skb;
534 struct skb_shared_hwtstamps *hwts;
536 skb = alloc_can_skb(netdev, &can_frame);
537 if (!skb)
538 return -ENOMEM;
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;
548 else
549 memcpy(can_frame->data, rx->data, can_frame->can_dlc);
551 hwts = skb_hwtstamps(skb);
552 peak_usb_get_ts_time(&usb_if->time_ref, le32_to_cpu(rx->ts32),
553 &hwts->hwtstamp);
555 netdev->stats.rx_packets++;
556 netdev->stats.rx_bytes += can_frame->can_dlc;
557 netif_rx(skb);
559 return 0;
562 static int pcan_usb_pro_handle_error(struct pcan_usb_pro_interface *usb_if,
563 struct pcan_usb_pro_rxstatus *er)
565 const u16 raw_status = le16_to_cpu(er->status);
566 const unsigned int ctrl_idx = (er->channel >> 4) & 0x0f;
567 struct peak_usb_device *dev = usb_if->dev[ctrl_idx];
568 struct net_device *netdev = dev->netdev;
569 struct can_frame *can_frame;
570 enum can_state new_state = CAN_STATE_ERROR_ACTIVE;
571 u8 err_mask = 0;
572 struct sk_buff *skb;
573 struct skb_shared_hwtstamps *hwts;
575 /* nothing should be sent while in BUS_OFF state */
576 if (dev->can.state == CAN_STATE_BUS_OFF)
577 return 0;
579 if (!raw_status) {
580 /* no error bit (back to active state) */
581 dev->can.state = CAN_STATE_ERROR_ACTIVE;
582 return 0;
585 if (raw_status & (PCAN_USBPRO_STATUS_OVERRUN |
586 PCAN_USBPRO_STATUS_QOVERRUN)) {
587 /* trick to bypass next comparison and process other errors */
588 new_state = CAN_STATE_MAX;
591 if (raw_status & PCAN_USBPRO_STATUS_BUS) {
592 new_state = CAN_STATE_BUS_OFF;
593 } else if (raw_status & PCAN_USBPRO_STATUS_ERROR) {
594 u32 rx_err_cnt = (le32_to_cpu(er->err_frm) & 0x00ff0000) >> 16;
595 u32 tx_err_cnt = (le32_to_cpu(er->err_frm) & 0xff000000) >> 24;
597 if (rx_err_cnt > 127)
598 err_mask |= CAN_ERR_CRTL_RX_PASSIVE;
599 else if (rx_err_cnt > 96)
600 err_mask |= CAN_ERR_CRTL_RX_WARNING;
602 if (tx_err_cnt > 127)
603 err_mask |= CAN_ERR_CRTL_TX_PASSIVE;
604 else if (tx_err_cnt > 96)
605 err_mask |= CAN_ERR_CRTL_TX_WARNING;
607 if (err_mask & (CAN_ERR_CRTL_RX_WARNING |
608 CAN_ERR_CRTL_TX_WARNING))
609 new_state = CAN_STATE_ERROR_WARNING;
610 else if (err_mask & (CAN_ERR_CRTL_RX_PASSIVE |
611 CAN_ERR_CRTL_TX_PASSIVE))
612 new_state = CAN_STATE_ERROR_PASSIVE;
615 /* donot post any error if current state didn't change */
616 if (dev->can.state == new_state)
617 return 0;
619 /* allocate an skb to store the error frame */
620 skb = alloc_can_err_skb(netdev, &can_frame);
621 if (!skb)
622 return -ENOMEM;
624 switch (new_state) {
625 case CAN_STATE_BUS_OFF:
626 can_frame->can_id |= CAN_ERR_BUSOFF;
627 dev->can.can_stats.bus_off++;
628 can_bus_off(netdev);
629 break;
631 case CAN_STATE_ERROR_PASSIVE:
632 can_frame->can_id |= CAN_ERR_CRTL;
633 can_frame->data[1] |= err_mask;
634 dev->can.can_stats.error_passive++;
635 break;
637 case CAN_STATE_ERROR_WARNING:
638 can_frame->can_id |= CAN_ERR_CRTL;
639 can_frame->data[1] |= err_mask;
640 dev->can.can_stats.error_warning++;
641 break;
643 case CAN_STATE_ERROR_ACTIVE:
644 break;
646 default:
647 /* CAN_STATE_MAX (trick to handle other errors) */
648 if (raw_status & PCAN_USBPRO_STATUS_OVERRUN) {
649 can_frame->can_id |= CAN_ERR_PROT;
650 can_frame->data[2] |= CAN_ERR_PROT_OVERLOAD;
651 netdev->stats.rx_over_errors++;
652 netdev->stats.rx_errors++;
655 if (raw_status & PCAN_USBPRO_STATUS_QOVERRUN) {
656 can_frame->can_id |= CAN_ERR_CRTL;
657 can_frame->data[1] |= CAN_ERR_CRTL_RX_OVERFLOW;
658 netdev->stats.rx_over_errors++;
659 netdev->stats.rx_errors++;
662 new_state = CAN_STATE_ERROR_ACTIVE;
663 break;
666 dev->can.state = new_state;
668 hwts = skb_hwtstamps(skb);
669 peak_usb_get_ts_time(&usb_if->time_ref, le32_to_cpu(er->ts32), &hwts->hwtstamp);
670 netdev->stats.rx_packets++;
671 netdev->stats.rx_bytes += can_frame->can_dlc;
672 netif_rx(skb);
674 return 0;
677 static void pcan_usb_pro_handle_ts(struct pcan_usb_pro_interface *usb_if,
678 struct pcan_usb_pro_rxts *ts)
680 /* should wait until clock is stabilized */
681 if (usb_if->cm_ignore_count > 0)
682 usb_if->cm_ignore_count--;
683 else
684 peak_usb_set_ts_now(&usb_if->time_ref,
685 le32_to_cpu(ts->ts64[1]));
689 * callback for bulk IN urb
691 static int pcan_usb_pro_decode_buf(struct peak_usb_device *dev, struct urb *urb)
693 struct pcan_usb_pro_interface *usb_if = pcan_usb_pro_dev_if(dev);
694 struct net_device *netdev = dev->netdev;
695 struct pcan_usb_pro_msg usb_msg;
696 u8 *rec_ptr, *msg_end;
697 u16 rec_cnt;
698 int err = 0;
700 rec_ptr = pcan_msg_init(&usb_msg, urb->transfer_buffer,
701 urb->actual_length);
702 if (!rec_ptr) {
703 netdev_err(netdev, "bad msg hdr len %d\n", urb->actual_length);
704 return -EINVAL;
707 /* loop reading all the records from the incoming message */
708 msg_end = urb->transfer_buffer + urb->actual_length;
709 rec_cnt = le16_to_cpu(*usb_msg.u.rec_cnt_rd);
710 for (; rec_cnt > 0; rec_cnt--) {
711 union pcan_usb_pro_rec *pr = (union pcan_usb_pro_rec *)rec_ptr;
712 u16 sizeof_rec = pcan_usb_pro_sizeof_rec[pr->data_type];
714 if (!sizeof_rec) {
715 netdev_err(netdev,
716 "got unsupported rec in usb msg:\n");
717 err = -ENOTSUPP;
718 break;
721 /* check if the record goes out of current packet */
722 if (rec_ptr + sizeof_rec > msg_end) {
723 netdev_err(netdev,
724 "got frag rec: should inc usb rx buf size\n");
725 err = -EBADMSG;
726 break;
729 switch (pr->data_type) {
730 case PCAN_USBPRO_RXMSG8:
731 case PCAN_USBPRO_RXMSG4:
732 case PCAN_USBPRO_RXMSG0:
733 case PCAN_USBPRO_RXRTR:
734 err = pcan_usb_pro_handle_canmsg(usb_if, &pr->rx_msg);
735 if (err < 0)
736 goto fail;
737 break;
739 case PCAN_USBPRO_RXSTATUS:
740 err = pcan_usb_pro_handle_error(usb_if, &pr->rx_status);
741 if (err < 0)
742 goto fail;
743 break;
745 case PCAN_USBPRO_RXTS:
746 pcan_usb_pro_handle_ts(usb_if, &pr->rx_ts);
747 break;
749 default:
750 netdev_err(netdev,
751 "unhandled rec type 0x%02x (%d): ignored\n",
752 pr->data_type, pr->data_type);
753 break;
756 rec_ptr += sizeof_rec;
759 fail:
760 if (err)
761 pcan_dump_mem("received msg",
762 urb->transfer_buffer, urb->actual_length);
764 return err;
767 static int pcan_usb_pro_encode_msg(struct peak_usb_device *dev,
768 struct sk_buff *skb, u8 *obuf, size_t *size)
770 struct can_frame *cf = (struct can_frame *)skb->data;
771 u8 data_type, len, flags;
772 struct pcan_usb_pro_msg usb_msg;
774 pcan_msg_init_empty(&usb_msg, obuf, *size);
776 if ((cf->can_id & CAN_RTR_FLAG) || (cf->can_dlc == 0))
777 data_type = PCAN_USBPRO_TXMSG0;
778 else if (cf->can_dlc <= 4)
779 data_type = PCAN_USBPRO_TXMSG4;
780 else
781 data_type = PCAN_USBPRO_TXMSG8;
783 len = (dev->ctrl_idx << 4) | (cf->can_dlc & 0x0f);
785 flags = 0;
786 if (cf->can_id & CAN_EFF_FLAG)
787 flags |= 0x02;
788 if (cf->can_id & CAN_RTR_FLAG)
789 flags |= 0x01;
791 pcan_msg_add_rec(&usb_msg, data_type, 0, flags, len, cf->can_id,
792 cf->data);
794 *size = usb_msg.rec_buffer_len;
796 return 0;
799 static int pcan_usb_pro_start(struct peak_usb_device *dev)
801 struct pcan_usb_pro_device *pdev =
802 container_of(dev, struct pcan_usb_pro_device, dev);
803 int err;
805 err = pcan_usb_pro_set_silent(dev,
806 dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY);
807 if (err)
808 return err;
810 /* filter mode: 0-> All OFF; 1->bypass */
811 err = pcan_usb_pro_set_filter(dev, 1);
812 if (err)
813 return err;
815 /* opening first device: */
816 if (pdev->usb_if->dev_opened_count == 0) {
817 /* reset time_ref */
818 peak_usb_init_time_ref(&pdev->usb_if->time_ref, &pcan_usb_pro);
820 /* ask device to send ts messages */
821 err = pcan_usb_pro_set_ts(dev, 1);
824 pdev->usb_if->dev_opened_count++;
826 return err;
830 * stop interface
831 * (last chance before set bus off)
833 static int pcan_usb_pro_stop(struct peak_usb_device *dev)
835 struct pcan_usb_pro_device *pdev =
836 container_of(dev, struct pcan_usb_pro_device, dev);
838 /* turn off ts msgs for that interface if no other dev opened */
839 if (pdev->usb_if->dev_opened_count == 1)
840 pcan_usb_pro_set_ts(dev, 0);
842 pdev->usb_if->dev_opened_count--;
844 return 0;
848 * called when probing to initialize a device object.
850 static int pcan_usb_pro_init(struct peak_usb_device *dev)
852 struct pcan_usb_pro_device *pdev =
853 container_of(dev, struct pcan_usb_pro_device, dev);
854 struct pcan_usb_pro_interface *usb_if = NULL;
855 struct pcan_usb_pro_fwinfo *fi = NULL;
856 struct pcan_usb_pro_blinfo *bi = NULL;
857 int err;
859 /* do this for 1st channel only */
860 if (!dev->prev_siblings) {
861 /* allocate netdevices common structure attached to first one */
862 usb_if = kzalloc(sizeof(struct pcan_usb_pro_interface),
863 GFP_KERNEL);
864 fi = kmalloc(sizeof(struct pcan_usb_pro_fwinfo), GFP_KERNEL);
865 bi = kmalloc(sizeof(struct pcan_usb_pro_blinfo), GFP_KERNEL);
866 if (!usb_if || !fi || !bi) {
867 err = -ENOMEM;
868 goto err_out;
871 /* number of ts msgs to ignore before taking one into account */
872 usb_if->cm_ignore_count = 5;
875 * explicit use of dev_xxx() instead of netdev_xxx() here:
876 * information displayed are related to the device itself, not
877 * to the canx netdevices.
879 err = pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_INFO,
880 PCAN_USBPRO_INFO_FW,
881 fi, sizeof(*fi));
882 if (err) {
883 dev_err(dev->netdev->dev.parent,
884 "unable to read %s firmware info (err %d)\n",
885 pcan_usb_pro.name, err);
886 goto err_out;
889 err = pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_INFO,
890 PCAN_USBPRO_INFO_BL,
891 bi, sizeof(*bi));
892 if (err) {
893 dev_err(dev->netdev->dev.parent,
894 "unable to read %s bootloader info (err %d)\n",
895 pcan_usb_pro.name, err);
896 goto err_out;
899 /* tell the device the can driver is running */
900 err = pcan_usb_pro_drv_loaded(dev, 1);
901 if (err)
902 goto err_out;
904 dev_info(dev->netdev->dev.parent,
905 "PEAK-System %s hwrev %u serial %08X.%08X (%u channels)\n",
906 pcan_usb_pro.name,
907 bi->hw_rev, bi->serial_num_hi, bi->serial_num_lo,
908 pcan_usb_pro.ctrl_count);
909 } else {
910 usb_if = pcan_usb_pro_dev_if(dev->prev_siblings);
913 pdev->usb_if = usb_if;
914 usb_if->dev[dev->ctrl_idx] = dev;
916 /* set LED in default state (end of init phase) */
917 pcan_usb_pro_set_led(dev, 0, 1);
919 kfree(bi);
920 kfree(fi);
922 return 0;
924 err_out:
925 kfree(bi);
926 kfree(fi);
927 kfree(usb_if);
929 return err;
932 static void pcan_usb_pro_exit(struct peak_usb_device *dev)
934 struct pcan_usb_pro_device *pdev =
935 container_of(dev, struct pcan_usb_pro_device, dev);
938 * when rmmod called before unplug and if down, should reset things
939 * before leaving
941 if (dev->can.state != CAN_STATE_STOPPED) {
942 /* set bus off on the corresponding channel */
943 pcan_usb_pro_set_bus(dev, 0);
946 /* if channel #0 (only) */
947 if (dev->ctrl_idx == 0) {
948 /* turn off calibration message if any device were opened */
949 if (pdev->usb_if->dev_opened_count > 0)
950 pcan_usb_pro_set_ts(dev, 0);
952 /* tell the PCAN-USB Pro device the driver is being unloaded */
953 pcan_usb_pro_drv_loaded(dev, 0);
958 * called when PCAN-USB Pro adapter is unplugged
960 static void pcan_usb_pro_free(struct peak_usb_device *dev)
962 /* last device: can free pcan_usb_pro_interface object now */
963 if (!dev->prev_siblings && !dev->next_siblings)
964 kfree(pcan_usb_pro_dev_if(dev));
968 * probe function for new PCAN-USB Pro usb interface
970 int pcan_usb_pro_probe(struct usb_interface *intf)
972 struct usb_host_interface *if_desc;
973 int i;
975 if_desc = intf->altsetting;
977 /* check interface endpoint addresses */
978 for (i = 0; i < if_desc->desc.bNumEndpoints; i++) {
979 struct usb_endpoint_descriptor *ep = &if_desc->endpoint[i].desc;
982 * below is the list of valid ep addreses. Any other ep address
983 * is considered as not-CAN interface address => no dev created
985 switch (ep->bEndpointAddress) {
986 case PCAN_USBPRO_EP_CMDOUT:
987 case PCAN_USBPRO_EP_CMDIN:
988 case PCAN_USBPRO_EP_MSGOUT_0:
989 case PCAN_USBPRO_EP_MSGOUT_1:
990 case PCAN_USBPRO_EP_MSGIN:
991 case PCAN_USBPRO_EP_UNUSED:
992 break;
993 default:
994 return -ENODEV;
998 return 0;
1002 * describe the PCAN-USB Pro adapter
1004 static const struct can_bittiming_const pcan_usb_pro_const = {
1005 .name = "pcan_usb_pro",
1006 .tseg1_min = 1,
1007 .tseg1_max = 16,
1008 .tseg2_min = 1,
1009 .tseg2_max = 8,
1010 .sjw_max = 4,
1011 .brp_min = 1,
1012 .brp_max = 1024,
1013 .brp_inc = 1,
1016 const struct peak_usb_adapter pcan_usb_pro = {
1017 .name = "PCAN-USB Pro",
1018 .device_id = PCAN_USBPRO_PRODUCT_ID,
1019 .ctrl_count = PCAN_USBPRO_CHANNEL_COUNT,
1020 .ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY,
1021 .clock = {
1022 .freq = PCAN_USBPRO_CRYSTAL_HZ,
1024 .bittiming_const = &pcan_usb_pro_const,
1026 /* size of device private data */
1027 .sizeof_dev_private = sizeof(struct pcan_usb_pro_device),
1029 /* timestamps usage */
1030 .ts_used_bits = 32,
1031 .ts_period = 1000000, /* calibration period in ts. */
1032 .us_per_ts_scale = 1, /* us = (ts * scale) >> shift */
1033 .us_per_ts_shift = 0,
1035 /* give here messages in/out endpoints */
1036 .ep_msg_in = PCAN_USBPRO_EP_MSGIN,
1037 .ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0, PCAN_USBPRO_EP_MSGOUT_1},
1039 /* size of rx/tx usb buffers */
1040 .rx_buffer_size = PCAN_USBPRO_RX_BUFFER_SIZE,
1041 .tx_buffer_size = PCAN_USBPRO_TX_BUFFER_SIZE,
1043 /* device callbacks */
1044 .intf_probe = pcan_usb_pro_probe,
1045 .dev_init = pcan_usb_pro_init,
1046 .dev_exit = pcan_usb_pro_exit,
1047 .dev_free = pcan_usb_pro_free,
1048 .dev_set_bus = pcan_usb_pro_set_bus,
1049 .dev_set_bittiming = pcan_usb_pro_set_bittiming,
1050 .dev_get_device_id = pcan_usb_pro_get_device_id,
1051 .dev_decode_buf = pcan_usb_pro_decode_buf,
1052 .dev_encode_msg = pcan_usb_pro_encode_msg,
1053 .dev_start = pcan_usb_pro_start,
1054 .dev_stop = pcan_usb_pro_stop,
1055 .dev_restart_async = pcan_usb_pro_restart_async,