mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / drivers / net / can / usb / peak_usb / pcan_usb_pro.c
blobf7f796a2c50bc036f7817b6fab070615e1795d37
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 /* 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;
64 int cm_ignore_count;
65 int dev_opened_count;
68 /* device information */
69 struct pcan_usb_pro_device {
70 struct peak_usb_device dev;
71 struct pcan_usb_pro_interface *usb_if;
72 u32 cached_ccbt;
75 /* internal structure used to handle messages sent to bulk urb */
76 struct pcan_usb_pro_msg {
77 u8 *rec_ptr;
78 int rec_buffer_size;
79 int rec_buffer_len;
80 union {
81 u16 *rec_cnt_rd;
82 u32 *rec_cnt;
83 u8 *rec_buffer;
84 } u;
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,
111 int buffer_size)
113 if (buffer_size < PCAN_USBPRO_MSG_HEADER_LEN)
114 return NULL;
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;
120 return pm->rec_ptr;
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);
128 if (pr) {
129 pm->rec_buffer_len = PCAN_USBPRO_MSG_HEADER_LEN;
130 *pm->u.rec_cnt = 0;
132 return pr;
136 * add one record to a message being built
138 static int pcan_msg_add_rec(struct pcan_usb_pro_msg *pm, u8 id, ...)
140 int len, i;
141 u8 *pc;
142 va_list ap;
144 va_start(ap, id);
146 pc = pm->rec_ptr + 1;
148 i = 0;
149 switch (id) {
150 case PCAN_USBPRO_TXMSG8:
151 i += 4;
152 case PCAN_USBPRO_TXMSG4:
153 i += 4;
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));
159 pc += 4;
160 memcpy(pc, va_arg(ap, int *), i);
161 pc += i;
162 break;
164 case PCAN_USBPRO_SETBTR:
165 case PCAN_USBPRO_GETDEVID:
166 *pc++ = va_arg(ap, int);
167 pc += 2;
168 *(u32 *)pc = cpu_to_le32(va_arg(ap, u32));
169 pc += 4;
170 break;
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));
177 pc += 2;
178 break;
180 case PCAN_USBPRO_SETLED:
181 *pc++ = va_arg(ap, int);
182 *(u16 *)pc = cpu_to_le16(va_arg(ap, int));
183 pc += 2;
184 *(u32 *)pc = cpu_to_le32(va_arg(ap, u32));
185 pc += 4;
186 break;
188 case PCAN_USBPRO_SETTS:
189 pc++;
190 *(u16 *)pc = cpu_to_le16(va_arg(ap, int));
191 pc += 2;
192 break;
194 default:
195 pr_err("%s: %s(): unknown data type %02Xh (%d)\n",
196 PCAN_USB_DRIVER_NAME, __func__, id, id);
197 pc--;
198 break;
201 len = pc - pm->rec_ptr;
202 if (len > 0) {
203 *pm->u.rec_cnt = cpu_to_le32(*pm->u.rec_cnt+1);
204 *pm->rec_ptr = id;
206 pm->rec_ptr = pc;
207 pm->rec_buffer_len += len;
210 va_end(ap);
212 return 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)
221 int actual_length;
222 int err;
224 /* usb device unregistered? */
225 if (!(dev->state & PCAN_USB_STATE_CONNECTED))
226 return 0;
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);
232 if (err)
233 netdev_err(dev->netdev, "sending command failure: %d\n", err);
235 return 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;
245 int actual_length;
246 int i, err = 0;
248 /* usb device unregistered? */
249 if (!(dev->state & PCAN_USB_STATE_CONNECTED))
250 return 0;
252 req_data_type = pum->u.rec_buffer[4];
253 req_channel = pum->u.rec_buffer[5];
255 *pum->u.rec_cnt = 0;
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;
259 u32 r, rec_cnt;
260 u16 rec_len;
261 u8 *pc;
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);
267 if (err) {
268 netdev_err(dev->netdev, "waiting rsp error %d\n", err);
269 break;
272 if (actual_length == 0)
273 continue;
275 err = -EBADMSG;
276 if (actual_length < PCAN_USBPRO_MSG_HEADER_LEN) {
277 netdev_err(dev->netdev,
278 "got abnormal too small rsp (len=%d)\n",
279 actual_length);
280 break;
283 pc = pcan_msg_init(&rsp, pum->u.rec_buffer,
284 actual_length);
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];
292 if (!rec_len) {
293 netdev_err(dev->netdev,
294 "got unprocessed record in msg\n");
295 pcan_dump_mem("rcvd rsp msg", pum->u.rec_buffer,
296 actual_length);
297 break;
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",
304 pr->data_type);
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 */
314 else
315 return 0;
317 /* otherwise, go on with next record in message */
318 pc += rec_len;
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)
328 int err;
329 u8 req_type;
330 unsigned int p;
332 /* usb device unregistered? */
333 if (!(dev->state & PCAN_USB_STATE_CONNECTED))
334 return 0;
336 req_type = USB_TYPE_VENDOR | USB_RECIP_OTHER;
338 switch (req_id) {
339 case PCAN_USBPRO_REQ_FCT:
340 p = usb_sndctrlpipe(dev->udev, 0);
341 break;
343 default:
344 p = usb_rcvctrlpipe(dev->udev, 0);
345 req_type |= USB_DIR_IN;
346 memset(req_addr, '\0', req_size);
347 break;
350 err = usb_control_msg(dev->udev, p, req_id, req_type, req_value, 0,
351 req_addr, req_size, 2 * USB_CTRL_GET_TIMEOUT);
352 if (err < 0) {
353 netdev_info(dev->netdev,
354 "unable to request usb[type=%d value=%d] err=%d\n",
355 req_id, req_value, err);
356 return err;
359 return 0;
362 static int pcan_usb_pro_set_ts(struct peak_usb_device *dev, u16 onoff)
364 struct pcan_usb_pro_msg um;
366 pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
367 pcan_msg_add_rec(&um, PCAN_USBPRO_SETTS, onoff);
369 return pcan_usb_pro_send_cmd(dev, &um);
372 static int pcan_usb_pro_set_bitrate(struct peak_usb_device *dev, u32 ccbt)
374 struct pcan_usb_pro_device *pdev =
375 container_of(dev, struct pcan_usb_pro_device, dev);
376 struct pcan_usb_pro_msg um;
378 pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
379 pcan_msg_add_rec(&um, PCAN_USBPRO_SETBTR, dev->ctrl_idx, ccbt);
381 /* cache the CCBT value to reuse it before next buson */
382 pdev->cached_ccbt = ccbt;
384 return pcan_usb_pro_send_cmd(dev, &um);
387 static int pcan_usb_pro_set_bus(struct peak_usb_device *dev, u8 onoff)
389 struct pcan_usb_pro_msg um;
391 /* if bus=on, be sure the bitrate being set before! */
392 if (onoff) {
393 struct pcan_usb_pro_device *pdev =
394 container_of(dev, struct pcan_usb_pro_device, dev);
396 pcan_usb_pro_set_bitrate(dev, pdev->cached_ccbt);
399 pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
400 pcan_msg_add_rec(&um, PCAN_USBPRO_SETBUSACT, dev->ctrl_idx, onoff);
402 return pcan_usb_pro_send_cmd(dev, &um);
405 static int pcan_usb_pro_set_silent(struct peak_usb_device *dev, u8 onoff)
407 struct pcan_usb_pro_msg um;
409 pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
410 pcan_msg_add_rec(&um, PCAN_USBPRO_SETSILENT, dev->ctrl_idx, onoff);
412 return pcan_usb_pro_send_cmd(dev, &um);
415 static int pcan_usb_pro_set_filter(struct peak_usb_device *dev, u16 filter_mode)
417 struct pcan_usb_pro_msg um;
419 pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
420 pcan_msg_add_rec(&um, PCAN_USBPRO_SETFILTR, dev->ctrl_idx, filter_mode);
422 return pcan_usb_pro_send_cmd(dev, &um);
425 static int pcan_usb_pro_set_led(struct peak_usb_device *dev, u8 mode,
426 u32 timeout)
428 struct pcan_usb_pro_msg um;
430 pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
431 pcan_msg_add_rec(&um, PCAN_USBPRO_SETLED, dev->ctrl_idx, mode, timeout);
433 return pcan_usb_pro_send_cmd(dev, &um);
436 static int pcan_usb_pro_get_device_id(struct peak_usb_device *dev,
437 u32 *device_id)
439 struct pcan_usb_pro_devid *pdn;
440 struct pcan_usb_pro_msg um;
441 int err;
442 u8 *pc;
444 pc = pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
445 pcan_msg_add_rec(&um, PCAN_USBPRO_GETDEVID, dev->ctrl_idx);
447 err = pcan_usb_pro_send_cmd(dev, &um);
448 if (err)
449 return err;
451 err = pcan_usb_pro_wait_rsp(dev, &um);
452 if (err)
453 return err;
455 pdn = (struct pcan_usb_pro_devid *)pc;
456 if (device_id)
457 *device_id = le32_to_cpu(pdn->serial_num);
459 return err;
462 static int pcan_usb_pro_set_bittiming(struct peak_usb_device *dev,
463 struct can_bittiming *bt)
465 u32 ccbt;
467 ccbt = (dev->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) ? 0x00800000 : 0;
468 ccbt |= (bt->sjw - 1) << 24;
469 ccbt |= (bt->phase_seg2 - 1) << 20;
470 ccbt |= (bt->prop_seg + bt->phase_seg1 - 1) << 16; /* = tseg1 */
471 ccbt |= bt->brp - 1;
473 netdev_info(dev->netdev, "setting ccbt=0x%08x\n", ccbt);
475 return pcan_usb_pro_set_bitrate(dev, ccbt);
478 static void pcan_usb_pro_restart_complete(struct urb *urb)
480 /* can delete usb resources */
481 peak_usb_async_complete(urb);
483 /* notify candev and netdev */
484 peak_usb_restart_complete(urb->context);
488 * handle restart but in asynchronously way
490 static int pcan_usb_pro_restart_async(struct peak_usb_device *dev,
491 struct urb *urb, u8 *buf)
493 struct pcan_usb_pro_msg um;
495 pcan_msg_init_empty(&um, buf, PCAN_USB_MAX_CMD_LEN);
496 pcan_msg_add_rec(&um, PCAN_USBPRO_SETBUSACT, dev->ctrl_idx, 1);
498 usb_fill_bulk_urb(urb, dev->udev,
499 usb_sndbulkpipe(dev->udev, PCAN_USBPRO_EP_CMDOUT),
500 buf, PCAN_USB_MAX_CMD_LEN,
501 pcan_usb_pro_restart_complete, dev);
503 return usb_submit_urb(urb, GFP_ATOMIC);
506 static int pcan_usb_pro_drv_loaded(struct peak_usb_device *dev, int loaded)
508 u8 *buffer;
509 int err;
511 buffer = kmalloc(PCAN_USBPRO_FCT_DRVLD_REQ_LEN, GFP_KERNEL);
512 if (!buffer)
513 return -ENOMEM;
515 buffer[0] = 0;
516 buffer[1] = !!loaded;
518 err = pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_FCT,
519 PCAN_USBPRO_FCT_DRVLD, buffer,
520 PCAN_USBPRO_FCT_DRVLD_REQ_LEN);
521 kfree(buffer);
523 return err;
526 static inline
527 struct pcan_usb_pro_interface *pcan_usb_pro_dev_if(struct peak_usb_device *dev)
529 struct pcan_usb_pro_device *pdev =
530 container_of(dev, struct pcan_usb_pro_device, dev);
531 return pdev->usb_if;
534 static int pcan_usb_pro_handle_canmsg(struct pcan_usb_pro_interface *usb_if,
535 struct pcan_usb_pro_rxmsg *rx)
537 const unsigned int ctrl_idx = (rx->len >> 4) & 0x0f;
538 struct peak_usb_device *dev = usb_if->dev[ctrl_idx];
539 struct net_device *netdev = dev->netdev;
540 struct can_frame *can_frame;
541 struct sk_buff *skb;
542 struct timeval tv;
543 struct skb_shared_hwtstamps *hwts;
545 skb = alloc_can_skb(netdev, &can_frame);
546 if (!skb)
547 return -ENOMEM;
549 can_frame->can_id = le32_to_cpu(rx->id);
550 can_frame->can_dlc = rx->len & 0x0f;
552 if (rx->flags & PCAN_USBPRO_EXT)
553 can_frame->can_id |= CAN_EFF_FLAG;
555 if (rx->flags & PCAN_USBPRO_RTR)
556 can_frame->can_id |= CAN_RTR_FLAG;
557 else
558 memcpy(can_frame->data, rx->data, can_frame->can_dlc);
560 peak_usb_get_ts_tv(&usb_if->time_ref, le32_to_cpu(rx->ts32), &tv);
561 hwts = skb_hwtstamps(skb);
562 hwts->hwtstamp = timeval_to_ktime(tv);
564 netif_rx(skb);
565 netdev->stats.rx_packets++;
566 netdev->stats.rx_bytes += can_frame->can_dlc;
568 return 0;
571 static int pcan_usb_pro_handle_error(struct pcan_usb_pro_interface *usb_if,
572 struct pcan_usb_pro_rxstatus *er)
574 const u32 raw_status = le32_to_cpu(er->status);
575 const unsigned int ctrl_idx = (er->channel >> 4) & 0x0f;
576 struct peak_usb_device *dev = usb_if->dev[ctrl_idx];
577 struct net_device *netdev = dev->netdev;
578 struct can_frame *can_frame;
579 enum can_state new_state = CAN_STATE_ERROR_ACTIVE;
580 u8 err_mask = 0;
581 struct sk_buff *skb;
582 struct timeval tv;
583 struct skb_shared_hwtstamps *hwts;
585 /* nothing should be sent while in BUS_OFF state */
586 if (dev->can.state == CAN_STATE_BUS_OFF)
587 return 0;
589 if (!raw_status) {
590 /* no error bit (back to active state) */
591 dev->can.state = CAN_STATE_ERROR_ACTIVE;
592 return 0;
595 if (raw_status & (PCAN_USBPRO_STATUS_OVERRUN |
596 PCAN_USBPRO_STATUS_QOVERRUN)) {
597 /* trick to bypass next comparison and process other errors */
598 new_state = CAN_STATE_MAX;
601 if (raw_status & PCAN_USBPRO_STATUS_BUS) {
602 new_state = CAN_STATE_BUS_OFF;
603 } else if (raw_status & PCAN_USBPRO_STATUS_ERROR) {
604 u32 rx_err_cnt = (le32_to_cpu(er->err_frm) & 0x00ff0000) >> 16;
605 u32 tx_err_cnt = (le32_to_cpu(er->err_frm) & 0xff000000) >> 24;
607 if (rx_err_cnt > 127)
608 err_mask |= CAN_ERR_CRTL_RX_PASSIVE;
609 else if (rx_err_cnt > 96)
610 err_mask |= CAN_ERR_CRTL_RX_WARNING;
612 if (tx_err_cnt > 127)
613 err_mask |= CAN_ERR_CRTL_TX_PASSIVE;
614 else if (tx_err_cnt > 96)
615 err_mask |= CAN_ERR_CRTL_TX_WARNING;
617 if (err_mask & (CAN_ERR_CRTL_RX_WARNING |
618 CAN_ERR_CRTL_TX_WARNING))
619 new_state = CAN_STATE_ERROR_WARNING;
620 else if (err_mask & (CAN_ERR_CRTL_RX_PASSIVE |
621 CAN_ERR_CRTL_TX_PASSIVE))
622 new_state = CAN_STATE_ERROR_PASSIVE;
625 /* donot post any error if current state didn't change */
626 if (dev->can.state == new_state)
627 return 0;
629 /* allocate an skb to store the error frame */
630 skb = alloc_can_err_skb(netdev, &can_frame);
631 if (!skb)
632 return -ENOMEM;
634 switch (new_state) {
635 case CAN_STATE_BUS_OFF:
636 can_frame->can_id |= CAN_ERR_BUSOFF;
637 can_bus_off(netdev);
638 break;
640 case CAN_STATE_ERROR_PASSIVE:
641 can_frame->can_id |= CAN_ERR_CRTL;
642 can_frame->data[1] |= err_mask;
643 dev->can.can_stats.error_passive++;
644 break;
646 case CAN_STATE_ERROR_WARNING:
647 can_frame->can_id |= CAN_ERR_CRTL;
648 can_frame->data[1] |= err_mask;
649 dev->can.can_stats.error_warning++;
650 break;
652 case CAN_STATE_ERROR_ACTIVE:
653 break;
655 default:
656 /* CAN_STATE_MAX (trick to handle other errors) */
657 if (raw_status & PCAN_USBPRO_STATUS_OVERRUN) {
658 can_frame->can_id |= CAN_ERR_PROT;
659 can_frame->data[2] |= CAN_ERR_PROT_OVERLOAD;
660 netdev->stats.rx_over_errors++;
661 netdev->stats.rx_errors++;
664 if (raw_status & PCAN_USBPRO_STATUS_QOVERRUN) {
665 can_frame->can_id |= CAN_ERR_CRTL;
666 can_frame->data[1] |= CAN_ERR_CRTL_RX_OVERFLOW;
667 netdev->stats.rx_over_errors++;
668 netdev->stats.rx_errors++;
671 new_state = CAN_STATE_ERROR_ACTIVE;
672 break;
675 dev->can.state = new_state;
677 peak_usb_get_ts_tv(&usb_if->time_ref, le32_to_cpu(er->ts32), &tv);
678 hwts = skb_hwtstamps(skb);
679 hwts->hwtstamp = timeval_to_ktime(tv);
680 netif_rx(skb);
681 netdev->stats.rx_packets++;
682 netdev->stats.rx_bytes += can_frame->can_dlc;
684 return 0;
687 static void pcan_usb_pro_handle_ts(struct pcan_usb_pro_interface *usb_if,
688 struct pcan_usb_pro_rxts *ts)
690 /* should wait until clock is stabilized */
691 if (usb_if->cm_ignore_count > 0)
692 usb_if->cm_ignore_count--;
693 else
694 peak_usb_set_ts_now(&usb_if->time_ref,
695 le32_to_cpu(ts->ts64[1]));
699 * callback for bulk IN urb
701 static int pcan_usb_pro_decode_buf(struct peak_usb_device *dev, struct urb *urb)
703 struct pcan_usb_pro_interface *usb_if = pcan_usb_pro_dev_if(dev);
704 struct net_device *netdev = dev->netdev;
705 struct pcan_usb_pro_msg usb_msg;
706 u8 *rec_ptr, *msg_end;
707 u16 rec_cnt;
708 int err = 0;
710 rec_ptr = pcan_msg_init(&usb_msg, urb->transfer_buffer,
711 urb->actual_length);
712 if (!rec_ptr) {
713 netdev_err(netdev, "bad msg hdr len %d\n", urb->actual_length);
714 return -EINVAL;
717 /* loop reading all the records from the incoming message */
718 msg_end = urb->transfer_buffer + urb->actual_length;
719 rec_cnt = le16_to_cpu(*usb_msg.u.rec_cnt_rd);
720 for (; rec_cnt > 0; rec_cnt--) {
721 union pcan_usb_pro_rec *pr = (union pcan_usb_pro_rec *)rec_ptr;
722 u16 sizeof_rec = pcan_usb_pro_sizeof_rec[pr->data_type];
724 if (!sizeof_rec) {
725 netdev_err(netdev,
726 "got unsupported rec in usb msg:\n");
727 err = -ENOTSUPP;
728 break;
731 /* check if the record goes out of current packet */
732 if (rec_ptr + sizeof_rec > msg_end) {
733 netdev_err(netdev,
734 "got frag rec: should inc usb rx buf size\n");
735 err = -EBADMSG;
736 break;
739 switch (pr->data_type) {
740 case PCAN_USBPRO_RXMSG8:
741 case PCAN_USBPRO_RXMSG4:
742 case PCAN_USBPRO_RXMSG0:
743 case PCAN_USBPRO_RXRTR:
744 err = pcan_usb_pro_handle_canmsg(usb_if, &pr->rx_msg);
745 if (err < 0)
746 goto fail;
747 break;
749 case PCAN_USBPRO_RXSTATUS:
750 err = pcan_usb_pro_handle_error(usb_if, &pr->rx_status);
751 if (err < 0)
752 goto fail;
753 break;
755 case PCAN_USBPRO_RXTS:
756 pcan_usb_pro_handle_ts(usb_if, &pr->rx_ts);
757 break;
759 default:
760 netdev_err(netdev,
761 "unhandled rec type 0x%02x (%d): ignored\n",
762 pr->data_type, pr->data_type);
763 break;
766 rec_ptr += sizeof_rec;
769 fail:
770 if (err)
771 pcan_dump_mem("received msg",
772 urb->transfer_buffer, urb->actual_length);
774 return err;
777 static int pcan_usb_pro_encode_msg(struct peak_usb_device *dev,
778 struct sk_buff *skb, u8 *obuf, size_t *size)
780 struct can_frame *cf = (struct can_frame *)skb->data;
781 u8 data_type, len, flags;
782 struct pcan_usb_pro_msg usb_msg;
784 pcan_msg_init_empty(&usb_msg, obuf, *size);
786 if ((cf->can_id & CAN_RTR_FLAG) || (cf->can_dlc == 0))
787 data_type = PCAN_USBPRO_TXMSG0;
788 else if (cf->can_dlc <= 4)
789 data_type = PCAN_USBPRO_TXMSG4;
790 else
791 data_type = PCAN_USBPRO_TXMSG8;
793 len = (dev->ctrl_idx << 4) | (cf->can_dlc & 0x0f);
795 flags = 0;
796 if (cf->can_id & CAN_EFF_FLAG)
797 flags |= 0x02;
798 if (cf->can_id & CAN_RTR_FLAG)
799 flags |= 0x01;
801 pcan_msg_add_rec(&usb_msg, data_type, 0, flags, len, cf->can_id,
802 cf->data);
804 *size = usb_msg.rec_buffer_len;
806 return 0;
809 static int pcan_usb_pro_start(struct peak_usb_device *dev)
811 struct pcan_usb_pro_device *pdev =
812 container_of(dev, struct pcan_usb_pro_device, dev);
813 int err;
815 err = pcan_usb_pro_set_silent(dev,
816 dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY);
817 if (err)
818 return err;
820 /* filter mode: 0-> All OFF; 1->bypass */
821 err = pcan_usb_pro_set_filter(dev, 1);
822 if (err)
823 return err;
825 /* opening first device: */
826 if (pdev->usb_if->dev_opened_count == 0) {
827 /* reset time_ref */
828 peak_usb_init_time_ref(&pdev->usb_if->time_ref, &pcan_usb_pro);
830 /* ask device to send ts messages */
831 err = pcan_usb_pro_set_ts(dev, 1);
834 pdev->usb_if->dev_opened_count++;
836 return err;
840 * stop interface
841 * (last chance before set bus off)
843 static int pcan_usb_pro_stop(struct peak_usb_device *dev)
845 struct pcan_usb_pro_device *pdev =
846 container_of(dev, struct pcan_usb_pro_device, dev);
848 /* turn off ts msgs for that interface if no other dev opened */
849 if (pdev->usb_if->dev_opened_count == 1)
850 pcan_usb_pro_set_ts(dev, 0);
852 pdev->usb_if->dev_opened_count--;
854 return 0;
858 * called when probing to initialize a device object.
860 static int pcan_usb_pro_init(struct peak_usb_device *dev)
862 struct pcan_usb_pro_device *pdev =
863 container_of(dev, struct pcan_usb_pro_device, dev);
864 struct pcan_usb_pro_interface *usb_if = NULL;
865 struct pcan_usb_pro_fwinfo *fi = NULL;
866 struct pcan_usb_pro_blinfo *bi = NULL;
867 int err;
869 /* do this for 1st channel only */
870 if (!dev->prev_siblings) {
871 /* allocate netdevices common structure attached to first one */
872 usb_if = kzalloc(sizeof(struct pcan_usb_pro_interface),
873 GFP_KERNEL);
874 fi = kmalloc(sizeof(struct pcan_usb_pro_fwinfo), GFP_KERNEL);
875 bi = kmalloc(sizeof(struct pcan_usb_pro_blinfo), GFP_KERNEL);
876 if (!usb_if || !fi || !bi) {
877 err = -ENOMEM;
878 goto err_out;
881 /* number of ts msgs to ignore before taking one into account */
882 usb_if->cm_ignore_count = 5;
885 * explicit use of dev_xxx() instead of netdev_xxx() here:
886 * information displayed are related to the device itself, not
887 * to the canx netdevices.
889 err = pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_INFO,
890 PCAN_USBPRO_INFO_FW,
891 fi, sizeof(*fi));
892 if (err) {
893 dev_err(dev->netdev->dev.parent,
894 "unable to read %s firmware info (err %d)\n",
895 pcan_usb_pro.name, err);
896 goto err_out;
899 err = pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_INFO,
900 PCAN_USBPRO_INFO_BL,
901 bi, sizeof(*bi));
902 if (err) {
903 dev_err(dev->netdev->dev.parent,
904 "unable to read %s bootloader info (err %d)\n",
905 pcan_usb_pro.name, err);
906 goto err_out;
909 /* tell the device the can driver is running */
910 err = pcan_usb_pro_drv_loaded(dev, 1);
911 if (err)
912 goto err_out;
914 dev_info(dev->netdev->dev.parent,
915 "PEAK-System %s hwrev %u serial %08X.%08X (%u channels)\n",
916 pcan_usb_pro.name,
917 bi->hw_rev, bi->serial_num_hi, bi->serial_num_lo,
918 pcan_usb_pro.ctrl_count);
919 } else {
920 usb_if = pcan_usb_pro_dev_if(dev->prev_siblings);
923 pdev->usb_if = usb_if;
924 usb_if->dev[dev->ctrl_idx] = dev;
926 /* set LED in default state (end of init phase) */
927 pcan_usb_pro_set_led(dev, 0, 1);
929 kfree(bi);
930 kfree(fi);
932 return 0;
934 err_out:
935 kfree(bi);
936 kfree(fi);
937 kfree(usb_if);
939 return err;
942 static void pcan_usb_pro_exit(struct peak_usb_device *dev)
944 struct pcan_usb_pro_device *pdev =
945 container_of(dev, struct pcan_usb_pro_device, dev);
948 * when rmmod called before unplug and if down, should reset things
949 * before leaving
951 if (dev->can.state != CAN_STATE_STOPPED) {
952 /* set bus off on the corresponding channel */
953 pcan_usb_pro_set_bus(dev, 0);
956 /* if channel #0 (only) */
957 if (dev->ctrl_idx == 0) {
958 /* turn off calibration message if any device were opened */
959 if (pdev->usb_if->dev_opened_count > 0)
960 pcan_usb_pro_set_ts(dev, 0);
962 /* tell the PCAN-USB Pro device the driver is being unloaded */
963 pcan_usb_pro_drv_loaded(dev, 0);
968 * called when PCAN-USB Pro adapter is unplugged
970 static void pcan_usb_pro_free(struct peak_usb_device *dev)
972 /* last device: can free pcan_usb_pro_interface object now */
973 if (!dev->prev_siblings && !dev->next_siblings)
974 kfree(pcan_usb_pro_dev_if(dev));
978 * probe function for new PCAN-USB Pro usb interface
980 static int pcan_usb_pro_probe(struct usb_interface *intf)
982 struct usb_host_interface *if_desc;
983 int i;
985 if_desc = intf->altsetting;
987 /* check interface endpoint addresses */
988 for (i = 0; i < if_desc->desc.bNumEndpoints; i++) {
989 struct usb_endpoint_descriptor *ep = &if_desc->endpoint[i].desc;
992 * below is the list of valid ep addreses. Any other ep address
993 * is considered as not-CAN interface address => no dev created
995 switch (ep->bEndpointAddress) {
996 case PCAN_USBPRO_EP_CMDOUT:
997 case PCAN_USBPRO_EP_CMDIN:
998 case PCAN_USBPRO_EP_MSGOUT_0:
999 case PCAN_USBPRO_EP_MSGOUT_1:
1000 case PCAN_USBPRO_EP_MSGIN:
1001 case PCAN_USBPRO_EP_UNUSED:
1002 break;
1003 default:
1004 return -ENODEV;
1008 return 0;
1012 * describe the PCAN-USB Pro adapter
1014 struct peak_usb_adapter pcan_usb_pro = {
1015 .name = "PCAN-USB Pro",
1016 .device_id = PCAN_USBPRO_PRODUCT_ID,
1017 .ctrl_count = PCAN_USBPRO_CHANNEL_COUNT,
1018 .clock = {
1019 .freq = PCAN_USBPRO_CRYSTAL_HZ,
1021 .bittiming_const = {
1022 .name = "pcan_usb_pro",
1023 .tseg1_min = 1,
1024 .tseg1_max = 16,
1025 .tseg2_min = 1,
1026 .tseg2_max = 8,
1027 .sjw_max = 4,
1028 .brp_min = 1,
1029 .brp_max = 1024,
1030 .brp_inc = 1,
1033 /* size of device private data */
1034 .sizeof_dev_private = sizeof(struct pcan_usb_pro_device),
1036 /* timestamps usage */
1037 .ts_used_bits = 32,
1038 .ts_period = 1000000, /* calibration period in ts. */
1039 .us_per_ts_scale = 1, /* us = (ts * scale) >> shift */
1040 .us_per_ts_shift = 0,
1042 /* give here messages in/out endpoints */
1043 .ep_msg_in = PCAN_USBPRO_EP_MSGIN,
1044 .ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0, PCAN_USBPRO_EP_MSGOUT_1},
1046 /* size of rx/tx usb buffers */
1047 .rx_buffer_size = PCAN_USBPRO_RX_BUFFER_SIZE,
1048 .tx_buffer_size = PCAN_USBPRO_TX_BUFFER_SIZE,
1050 /* device callbacks */
1051 .intf_probe = pcan_usb_pro_probe,
1052 .dev_init = pcan_usb_pro_init,
1053 .dev_exit = pcan_usb_pro_exit,
1054 .dev_free = pcan_usb_pro_free,
1055 .dev_set_bus = pcan_usb_pro_set_bus,
1056 .dev_set_bittiming = pcan_usb_pro_set_bittiming,
1057 .dev_get_device_id = pcan_usb_pro_get_device_id,
1058 .dev_decode_buf = pcan_usb_pro_decode_buf,
1059 .dev_encode_msg = pcan_usb_pro_encode_msg,
1060 .dev_start = pcan_usb_pro_start,
1061 .dev_stop = pcan_usb_pro_stop,
1062 .dev_restart_async = pcan_usb_pro_restart_async,