Merge branches 'timers-core-for-linus' and 'timers-urgent-for-linus' of git://git...
[linux/fpc-iii.git] / drivers / net / can / usb / peak_usb / pcan_usb_pro.c
blobbbdd6058cd2f5af5465e8360456adeb88e668da4
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 timeval tv;
535 struct skb_shared_hwtstamps *hwts;
537 skb = alloc_can_skb(netdev, &can_frame);
538 if (!skb)
539 return -ENOMEM;
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;
549 else
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);
556 netdev->stats.rx_packets++;
557 netdev->stats.rx_bytes += can_frame->can_dlc;
558 netif_rx(skb);
560 return 0;
563 static int pcan_usb_pro_handle_error(struct pcan_usb_pro_interface *usb_if,
564 struct pcan_usb_pro_rxstatus *er)
566 const u16 raw_status = le16_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;
572 u8 err_mask = 0;
573 struct sk_buff *skb;
574 struct timeval tv;
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)
579 return 0;
581 if (!raw_status) {
582 /* no error bit (back to active state) */
583 dev->can.state = CAN_STATE_ERROR_ACTIVE;
584 return 0;
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)
619 return 0;
621 /* allocate an skb to store the error frame */
622 skb = alloc_can_err_skb(netdev, &can_frame);
623 if (!skb)
624 return -ENOMEM;
626 switch (new_state) {
627 case CAN_STATE_BUS_OFF:
628 can_frame->can_id |= CAN_ERR_BUSOFF;
629 dev->can.can_stats.bus_off++;
630 can_bus_off(netdev);
631 break;
633 case CAN_STATE_ERROR_PASSIVE:
634 can_frame->can_id |= CAN_ERR_CRTL;
635 can_frame->data[1] |= err_mask;
636 dev->can.can_stats.error_passive++;
637 break;
639 case CAN_STATE_ERROR_WARNING:
640 can_frame->can_id |= CAN_ERR_CRTL;
641 can_frame->data[1] |= err_mask;
642 dev->can.can_stats.error_warning++;
643 break;
645 case CAN_STATE_ERROR_ACTIVE:
646 break;
648 default:
649 /* CAN_STATE_MAX (trick to handle other errors) */
650 if (raw_status & PCAN_USBPRO_STATUS_OVERRUN) {
651 can_frame->can_id |= CAN_ERR_PROT;
652 can_frame->data[2] |= CAN_ERR_PROT_OVERLOAD;
653 netdev->stats.rx_over_errors++;
654 netdev->stats.rx_errors++;
657 if (raw_status & PCAN_USBPRO_STATUS_QOVERRUN) {
658 can_frame->can_id |= CAN_ERR_CRTL;
659 can_frame->data[1] |= CAN_ERR_CRTL_RX_OVERFLOW;
660 netdev->stats.rx_over_errors++;
661 netdev->stats.rx_errors++;
664 new_state = CAN_STATE_ERROR_ACTIVE;
665 break;
668 dev->can.state = new_state;
670 peak_usb_get_ts_tv(&usb_if->time_ref, le32_to_cpu(er->ts32), &tv);
671 hwts = skb_hwtstamps(skb);
672 hwts->hwtstamp = timeval_to_ktime(tv);
673 netdev->stats.rx_packets++;
674 netdev->stats.rx_bytes += can_frame->can_dlc;
675 netif_rx(skb);
677 return 0;
680 static void pcan_usb_pro_handle_ts(struct pcan_usb_pro_interface *usb_if,
681 struct pcan_usb_pro_rxts *ts)
683 /* should wait until clock is stabilized */
684 if (usb_if->cm_ignore_count > 0)
685 usb_if->cm_ignore_count--;
686 else
687 peak_usb_set_ts_now(&usb_if->time_ref,
688 le32_to_cpu(ts->ts64[1]));
692 * callback for bulk IN urb
694 static int pcan_usb_pro_decode_buf(struct peak_usb_device *dev, struct urb *urb)
696 struct pcan_usb_pro_interface *usb_if = pcan_usb_pro_dev_if(dev);
697 struct net_device *netdev = dev->netdev;
698 struct pcan_usb_pro_msg usb_msg;
699 u8 *rec_ptr, *msg_end;
700 u16 rec_cnt;
701 int err = 0;
703 rec_ptr = pcan_msg_init(&usb_msg, urb->transfer_buffer,
704 urb->actual_length);
705 if (!rec_ptr) {
706 netdev_err(netdev, "bad msg hdr len %d\n", urb->actual_length);
707 return -EINVAL;
710 /* loop reading all the records from the incoming message */
711 msg_end = urb->transfer_buffer + urb->actual_length;
712 rec_cnt = le16_to_cpu(*usb_msg.u.rec_cnt_rd);
713 for (; rec_cnt > 0; rec_cnt--) {
714 union pcan_usb_pro_rec *pr = (union pcan_usb_pro_rec *)rec_ptr;
715 u16 sizeof_rec = pcan_usb_pro_sizeof_rec[pr->data_type];
717 if (!sizeof_rec) {
718 netdev_err(netdev,
719 "got unsupported rec in usb msg:\n");
720 err = -ENOTSUPP;
721 break;
724 /* check if the record goes out of current packet */
725 if (rec_ptr + sizeof_rec > msg_end) {
726 netdev_err(netdev,
727 "got frag rec: should inc usb rx buf size\n");
728 err = -EBADMSG;
729 break;
732 switch (pr->data_type) {
733 case PCAN_USBPRO_RXMSG8:
734 case PCAN_USBPRO_RXMSG4:
735 case PCAN_USBPRO_RXMSG0:
736 case PCAN_USBPRO_RXRTR:
737 err = pcan_usb_pro_handle_canmsg(usb_if, &pr->rx_msg);
738 if (err < 0)
739 goto fail;
740 break;
742 case PCAN_USBPRO_RXSTATUS:
743 err = pcan_usb_pro_handle_error(usb_if, &pr->rx_status);
744 if (err < 0)
745 goto fail;
746 break;
748 case PCAN_USBPRO_RXTS:
749 pcan_usb_pro_handle_ts(usb_if, &pr->rx_ts);
750 break;
752 default:
753 netdev_err(netdev,
754 "unhandled rec type 0x%02x (%d): ignored\n",
755 pr->data_type, pr->data_type);
756 break;
759 rec_ptr += sizeof_rec;
762 fail:
763 if (err)
764 pcan_dump_mem("received msg",
765 urb->transfer_buffer, urb->actual_length);
767 return err;
770 static int pcan_usb_pro_encode_msg(struct peak_usb_device *dev,
771 struct sk_buff *skb, u8 *obuf, size_t *size)
773 struct can_frame *cf = (struct can_frame *)skb->data;
774 u8 data_type, len, flags;
775 struct pcan_usb_pro_msg usb_msg;
777 pcan_msg_init_empty(&usb_msg, obuf, *size);
779 if ((cf->can_id & CAN_RTR_FLAG) || (cf->can_dlc == 0))
780 data_type = PCAN_USBPRO_TXMSG0;
781 else if (cf->can_dlc <= 4)
782 data_type = PCAN_USBPRO_TXMSG4;
783 else
784 data_type = PCAN_USBPRO_TXMSG8;
786 len = (dev->ctrl_idx << 4) | (cf->can_dlc & 0x0f);
788 flags = 0;
789 if (cf->can_id & CAN_EFF_FLAG)
790 flags |= 0x02;
791 if (cf->can_id & CAN_RTR_FLAG)
792 flags |= 0x01;
794 pcan_msg_add_rec(&usb_msg, data_type, 0, flags, len, cf->can_id,
795 cf->data);
797 *size = usb_msg.rec_buffer_len;
799 return 0;
802 static int pcan_usb_pro_start(struct peak_usb_device *dev)
804 struct pcan_usb_pro_device *pdev =
805 container_of(dev, struct pcan_usb_pro_device, dev);
806 int err;
808 err = pcan_usb_pro_set_silent(dev,
809 dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY);
810 if (err)
811 return err;
813 /* filter mode: 0-> All OFF; 1->bypass */
814 err = pcan_usb_pro_set_filter(dev, 1);
815 if (err)
816 return err;
818 /* opening first device: */
819 if (pdev->usb_if->dev_opened_count == 0) {
820 /* reset time_ref */
821 peak_usb_init_time_ref(&pdev->usb_if->time_ref, &pcan_usb_pro);
823 /* ask device to send ts messages */
824 err = pcan_usb_pro_set_ts(dev, 1);
827 pdev->usb_if->dev_opened_count++;
829 return err;
833 * stop interface
834 * (last chance before set bus off)
836 static int pcan_usb_pro_stop(struct peak_usb_device *dev)
838 struct pcan_usb_pro_device *pdev =
839 container_of(dev, struct pcan_usb_pro_device, dev);
841 /* turn off ts msgs for that interface if no other dev opened */
842 if (pdev->usb_if->dev_opened_count == 1)
843 pcan_usb_pro_set_ts(dev, 0);
845 pdev->usb_if->dev_opened_count--;
847 return 0;
851 * called when probing to initialize a device object.
853 static int pcan_usb_pro_init(struct peak_usb_device *dev)
855 struct pcan_usb_pro_device *pdev =
856 container_of(dev, struct pcan_usb_pro_device, dev);
857 struct pcan_usb_pro_interface *usb_if = NULL;
858 struct pcan_usb_pro_fwinfo *fi = NULL;
859 struct pcan_usb_pro_blinfo *bi = NULL;
860 int err;
862 /* do this for 1st channel only */
863 if (!dev->prev_siblings) {
864 /* allocate netdevices common structure attached to first one */
865 usb_if = kzalloc(sizeof(struct pcan_usb_pro_interface),
866 GFP_KERNEL);
867 fi = kmalloc(sizeof(struct pcan_usb_pro_fwinfo), GFP_KERNEL);
868 bi = kmalloc(sizeof(struct pcan_usb_pro_blinfo), GFP_KERNEL);
869 if (!usb_if || !fi || !bi) {
870 err = -ENOMEM;
871 goto err_out;
874 /* number of ts msgs to ignore before taking one into account */
875 usb_if->cm_ignore_count = 5;
878 * explicit use of dev_xxx() instead of netdev_xxx() here:
879 * information displayed are related to the device itself, not
880 * to the canx netdevices.
882 err = pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_INFO,
883 PCAN_USBPRO_INFO_FW,
884 fi, sizeof(*fi));
885 if (err) {
886 dev_err(dev->netdev->dev.parent,
887 "unable to read %s firmware info (err %d)\n",
888 pcan_usb_pro.name, err);
889 goto err_out;
892 err = pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_INFO,
893 PCAN_USBPRO_INFO_BL,
894 bi, sizeof(*bi));
895 if (err) {
896 dev_err(dev->netdev->dev.parent,
897 "unable to read %s bootloader info (err %d)\n",
898 pcan_usb_pro.name, err);
899 goto err_out;
902 /* tell the device the can driver is running */
903 err = pcan_usb_pro_drv_loaded(dev, 1);
904 if (err)
905 goto err_out;
907 dev_info(dev->netdev->dev.parent,
908 "PEAK-System %s hwrev %u serial %08X.%08X (%u channels)\n",
909 pcan_usb_pro.name,
910 bi->hw_rev, bi->serial_num_hi, bi->serial_num_lo,
911 pcan_usb_pro.ctrl_count);
912 } else {
913 usb_if = pcan_usb_pro_dev_if(dev->prev_siblings);
916 pdev->usb_if = usb_if;
917 usb_if->dev[dev->ctrl_idx] = dev;
919 /* set LED in default state (end of init phase) */
920 pcan_usb_pro_set_led(dev, 0, 1);
922 kfree(bi);
923 kfree(fi);
925 return 0;
927 err_out:
928 kfree(bi);
929 kfree(fi);
930 kfree(usb_if);
932 return err;
935 static void pcan_usb_pro_exit(struct peak_usb_device *dev)
937 struct pcan_usb_pro_device *pdev =
938 container_of(dev, struct pcan_usb_pro_device, dev);
941 * when rmmod called before unplug and if down, should reset things
942 * before leaving
944 if (dev->can.state != CAN_STATE_STOPPED) {
945 /* set bus off on the corresponding channel */
946 pcan_usb_pro_set_bus(dev, 0);
949 /* if channel #0 (only) */
950 if (dev->ctrl_idx == 0) {
951 /* turn off calibration message if any device were opened */
952 if (pdev->usb_if->dev_opened_count > 0)
953 pcan_usb_pro_set_ts(dev, 0);
955 /* tell the PCAN-USB Pro device the driver is being unloaded */
956 pcan_usb_pro_drv_loaded(dev, 0);
961 * called when PCAN-USB Pro adapter is unplugged
963 static void pcan_usb_pro_free(struct peak_usb_device *dev)
965 /* last device: can free pcan_usb_pro_interface object now */
966 if (!dev->prev_siblings && !dev->next_siblings)
967 kfree(pcan_usb_pro_dev_if(dev));
971 * probe function for new PCAN-USB Pro usb interface
973 int pcan_usb_pro_probe(struct usb_interface *intf)
975 struct usb_host_interface *if_desc;
976 int i;
978 if_desc = intf->altsetting;
980 /* check interface endpoint addresses */
981 for (i = 0; i < if_desc->desc.bNumEndpoints; i++) {
982 struct usb_endpoint_descriptor *ep = &if_desc->endpoint[i].desc;
985 * below is the list of valid ep addreses. Any other ep address
986 * is considered as not-CAN interface address => no dev created
988 switch (ep->bEndpointAddress) {
989 case PCAN_USBPRO_EP_CMDOUT:
990 case PCAN_USBPRO_EP_CMDIN:
991 case PCAN_USBPRO_EP_MSGOUT_0:
992 case PCAN_USBPRO_EP_MSGOUT_1:
993 case PCAN_USBPRO_EP_MSGIN:
994 case PCAN_USBPRO_EP_UNUSED:
995 break;
996 default:
997 return -ENODEV;
1001 return 0;
1005 * describe the PCAN-USB Pro adapter
1007 static const struct can_bittiming_const pcan_usb_pro_const = {
1008 .name = "pcan_usb_pro",
1009 .tseg1_min = 1,
1010 .tseg1_max = 16,
1011 .tseg2_min = 1,
1012 .tseg2_max = 8,
1013 .sjw_max = 4,
1014 .brp_min = 1,
1015 .brp_max = 1024,
1016 .brp_inc = 1,
1019 const struct peak_usb_adapter pcan_usb_pro = {
1020 .name = "PCAN-USB Pro",
1021 .device_id = PCAN_USBPRO_PRODUCT_ID,
1022 .ctrl_count = PCAN_USBPRO_CHANNEL_COUNT,
1023 .ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY,
1024 .clock = {
1025 .freq = PCAN_USBPRO_CRYSTAL_HZ,
1027 .bittiming_const = &pcan_usb_pro_const,
1029 /* size of device private data */
1030 .sizeof_dev_private = sizeof(struct pcan_usb_pro_device),
1032 /* timestamps usage */
1033 .ts_used_bits = 32,
1034 .ts_period = 1000000, /* calibration period in ts. */
1035 .us_per_ts_scale = 1, /* us = (ts * scale) >> shift */
1036 .us_per_ts_shift = 0,
1038 /* give here messages in/out endpoints */
1039 .ep_msg_in = PCAN_USBPRO_EP_MSGIN,
1040 .ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0, PCAN_USBPRO_EP_MSGOUT_1},
1042 /* size of rx/tx usb buffers */
1043 .rx_buffer_size = PCAN_USBPRO_RX_BUFFER_SIZE,
1044 .tx_buffer_size = PCAN_USBPRO_TX_BUFFER_SIZE,
1046 /* device callbacks */
1047 .intf_probe = pcan_usb_pro_probe,
1048 .dev_init = pcan_usb_pro_init,
1049 .dev_exit = pcan_usb_pro_exit,
1050 .dev_free = pcan_usb_pro_free,
1051 .dev_set_bus = pcan_usb_pro_set_bus,
1052 .dev_set_bittiming = pcan_usb_pro_set_bittiming,
1053 .dev_get_device_id = pcan_usb_pro_get_device_id,
1054 .dev_decode_buf = pcan_usb_pro_decode_buf,
1055 .dev_encode_msg = pcan_usb_pro_encode_msg,
1056 .dev_start = pcan_usb_pro_start,
1057 .dev_stop = pcan_usb_pro_stop,
1058 .dev_restart_async = pcan_usb_pro_restart_async,