PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / net / can / usb / peak_usb / pcan_usb_pro.c
blob263dd921edc42342bba78ce9f2885862f0fc3087
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 memset(req_addr, '\0', req_size);
338 req_type = USB_TYPE_VENDOR | USB_RECIP_OTHER;
340 switch (req_id) {
341 case PCAN_USBPRO_REQ_FCT:
342 p = usb_sndctrlpipe(dev->udev, 0);
343 break;
345 default:
346 p = usb_rcvctrlpipe(dev->udev, 0);
347 req_type |= USB_DIR_IN;
348 break;
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);
353 if (err < 0) {
354 netdev_info(dev->netdev,
355 "unable to request usb[type=%d value=%d] err=%d\n",
356 req_id, req_value, err);
357 return err;
360 return 0;
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! */
393 if (onoff) {
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,
427 u32 timeout)
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,
438 u32 *device_id)
440 struct pcan_usb_pro_devid *pdn;
441 struct pcan_usb_pro_msg um;
442 int err;
443 u8 *pc;
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);
449 if (err)
450 return err;
452 err = pcan_usb_pro_wait_rsp(dev, &um);
453 if (err)
454 return err;
456 pdn = (struct pcan_usb_pro_devid *)pc;
457 if (device_id)
458 *device_id = le32_to_cpu(pdn->serial_num);
460 return err;
463 static int pcan_usb_pro_set_bittiming(struct peak_usb_device *dev,
464 struct can_bittiming *bt)
466 u32 ccbt;
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 */
472 ccbt |= bt->brp - 1;
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 int pcan_usb_pro_drv_loaded(struct peak_usb_device *dev, int loaded)
509 u8 *buffer;
510 int err;
512 buffer = kmalloc(PCAN_USBPRO_FCT_DRVLD_REQ_LEN, GFP_KERNEL);
513 if (!buffer)
514 return -ENOMEM;
516 buffer[0] = 0;
517 buffer[1] = !!loaded;
519 err = pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_FCT,
520 PCAN_USBPRO_FCT_DRVLD, buffer,
521 PCAN_USBPRO_FCT_DRVLD_REQ_LEN);
522 kfree(buffer);
524 return err;
527 static inline
528 struct pcan_usb_pro_interface *pcan_usb_pro_dev_if(struct peak_usb_device *dev)
530 struct pcan_usb_pro_device *pdev =
531 container_of(dev, struct pcan_usb_pro_device, dev);
532 return pdev->usb_if;
535 static int pcan_usb_pro_handle_canmsg(struct pcan_usb_pro_interface *usb_if,
536 struct pcan_usb_pro_rxmsg *rx)
538 const unsigned int ctrl_idx = (rx->len >> 4) & 0x0f;
539 struct peak_usb_device *dev = usb_if->dev[ctrl_idx];
540 struct net_device *netdev = dev->netdev;
541 struct can_frame *can_frame;
542 struct sk_buff *skb;
543 struct timeval tv;
544 struct skb_shared_hwtstamps *hwts;
546 skb = alloc_can_skb(netdev, &can_frame);
547 if (!skb)
548 return -ENOMEM;
550 can_frame->can_id = le32_to_cpu(rx->id);
551 can_frame->can_dlc = rx->len & 0x0f;
553 if (rx->flags & PCAN_USBPRO_EXT)
554 can_frame->can_id |= CAN_EFF_FLAG;
556 if (rx->flags & PCAN_USBPRO_RTR)
557 can_frame->can_id |= CAN_RTR_FLAG;
558 else
559 memcpy(can_frame->data, rx->data, can_frame->can_dlc);
561 peak_usb_get_ts_tv(&usb_if->time_ref, le32_to_cpu(rx->ts32), &tv);
562 hwts = skb_hwtstamps(skb);
563 hwts->hwtstamp = timeval_to_ktime(tv);
565 netif_rx(skb);
566 netdev->stats.rx_packets++;
567 netdev->stats.rx_bytes += can_frame->can_dlc;
569 return 0;
572 static int pcan_usb_pro_handle_error(struct pcan_usb_pro_interface *usb_if,
573 struct pcan_usb_pro_rxstatus *er)
575 const u32 raw_status = le32_to_cpu(er->status);
576 const unsigned int ctrl_idx = (er->channel >> 4) & 0x0f;
577 struct peak_usb_device *dev = usb_if->dev[ctrl_idx];
578 struct net_device *netdev = dev->netdev;
579 struct can_frame *can_frame;
580 enum can_state new_state = CAN_STATE_ERROR_ACTIVE;
581 u8 err_mask = 0;
582 struct sk_buff *skb;
583 struct timeval tv;
584 struct skb_shared_hwtstamps *hwts;
586 /* nothing should be sent while in BUS_OFF state */
587 if (dev->can.state == CAN_STATE_BUS_OFF)
588 return 0;
590 if (!raw_status) {
591 /* no error bit (back to active state) */
592 dev->can.state = CAN_STATE_ERROR_ACTIVE;
593 return 0;
596 if (raw_status & (PCAN_USBPRO_STATUS_OVERRUN |
597 PCAN_USBPRO_STATUS_QOVERRUN)) {
598 /* trick to bypass next comparison and process other errors */
599 new_state = CAN_STATE_MAX;
602 if (raw_status & PCAN_USBPRO_STATUS_BUS) {
603 new_state = CAN_STATE_BUS_OFF;
604 } else if (raw_status & PCAN_USBPRO_STATUS_ERROR) {
605 u32 rx_err_cnt = (le32_to_cpu(er->err_frm) & 0x00ff0000) >> 16;
606 u32 tx_err_cnt = (le32_to_cpu(er->err_frm) & 0xff000000) >> 24;
608 if (rx_err_cnt > 127)
609 err_mask |= CAN_ERR_CRTL_RX_PASSIVE;
610 else if (rx_err_cnt > 96)
611 err_mask |= CAN_ERR_CRTL_RX_WARNING;
613 if (tx_err_cnt > 127)
614 err_mask |= CAN_ERR_CRTL_TX_PASSIVE;
615 else if (tx_err_cnt > 96)
616 err_mask |= CAN_ERR_CRTL_TX_WARNING;
618 if (err_mask & (CAN_ERR_CRTL_RX_WARNING |
619 CAN_ERR_CRTL_TX_WARNING))
620 new_state = CAN_STATE_ERROR_WARNING;
621 else if (err_mask & (CAN_ERR_CRTL_RX_PASSIVE |
622 CAN_ERR_CRTL_TX_PASSIVE))
623 new_state = CAN_STATE_ERROR_PASSIVE;
626 /* donot post any error if current state didn't change */
627 if (dev->can.state == new_state)
628 return 0;
630 /* allocate an skb to store the error frame */
631 skb = alloc_can_err_skb(netdev, &can_frame);
632 if (!skb)
633 return -ENOMEM;
635 switch (new_state) {
636 case CAN_STATE_BUS_OFF:
637 can_frame->can_id |= CAN_ERR_BUSOFF;
638 can_bus_off(netdev);
639 break;
641 case CAN_STATE_ERROR_PASSIVE:
642 can_frame->can_id |= CAN_ERR_CRTL;
643 can_frame->data[1] |= err_mask;
644 dev->can.can_stats.error_passive++;
645 break;
647 case CAN_STATE_ERROR_WARNING:
648 can_frame->can_id |= CAN_ERR_CRTL;
649 can_frame->data[1] |= err_mask;
650 dev->can.can_stats.error_warning++;
651 break;
653 case CAN_STATE_ERROR_ACTIVE:
654 break;
656 default:
657 /* CAN_STATE_MAX (trick to handle other errors) */
658 if (raw_status & PCAN_USBPRO_STATUS_OVERRUN) {
659 can_frame->can_id |= CAN_ERR_PROT;
660 can_frame->data[2] |= CAN_ERR_PROT_OVERLOAD;
661 netdev->stats.rx_over_errors++;
662 netdev->stats.rx_errors++;
665 if (raw_status & PCAN_USBPRO_STATUS_QOVERRUN) {
666 can_frame->can_id |= CAN_ERR_CRTL;
667 can_frame->data[1] |= CAN_ERR_CRTL_RX_OVERFLOW;
668 netdev->stats.rx_over_errors++;
669 netdev->stats.rx_errors++;
672 new_state = CAN_STATE_ERROR_ACTIVE;
673 break;
676 dev->can.state = new_state;
678 peak_usb_get_ts_tv(&usb_if->time_ref, le32_to_cpu(er->ts32), &tv);
679 hwts = skb_hwtstamps(skb);
680 hwts->hwtstamp = timeval_to_ktime(tv);
681 netif_rx(skb);
682 netdev->stats.rx_packets++;
683 netdev->stats.rx_bytes += can_frame->can_dlc;
685 return 0;
688 static void pcan_usb_pro_handle_ts(struct pcan_usb_pro_interface *usb_if,
689 struct pcan_usb_pro_rxts *ts)
691 /* should wait until clock is stabilized */
692 if (usb_if->cm_ignore_count > 0)
693 usb_if->cm_ignore_count--;
694 else
695 peak_usb_set_ts_now(&usb_if->time_ref,
696 le32_to_cpu(ts->ts64[1]));
700 * callback for bulk IN urb
702 static int pcan_usb_pro_decode_buf(struct peak_usb_device *dev, struct urb *urb)
704 struct pcan_usb_pro_interface *usb_if = pcan_usb_pro_dev_if(dev);
705 struct net_device *netdev = dev->netdev;
706 struct pcan_usb_pro_msg usb_msg;
707 u8 *rec_ptr, *msg_end;
708 u16 rec_cnt;
709 int err = 0;
711 rec_ptr = pcan_msg_init(&usb_msg, urb->transfer_buffer,
712 urb->actual_length);
713 if (!rec_ptr) {
714 netdev_err(netdev, "bad msg hdr len %d\n", urb->actual_length);
715 return -EINVAL;
718 /* loop reading all the records from the incoming message */
719 msg_end = urb->transfer_buffer + urb->actual_length;
720 rec_cnt = le16_to_cpu(*usb_msg.u.rec_cnt_rd);
721 for (; rec_cnt > 0; rec_cnt--) {
722 union pcan_usb_pro_rec *pr = (union pcan_usb_pro_rec *)rec_ptr;
723 u16 sizeof_rec = pcan_usb_pro_sizeof_rec[pr->data_type];
725 if (!sizeof_rec) {
726 netdev_err(netdev,
727 "got unsupported rec in usb msg:\n");
728 err = -ENOTSUPP;
729 break;
732 /* check if the record goes out of current packet */
733 if (rec_ptr + sizeof_rec > msg_end) {
734 netdev_err(netdev,
735 "got frag rec: should inc usb rx buf size\n");
736 err = -EBADMSG;
737 break;
740 switch (pr->data_type) {
741 case PCAN_USBPRO_RXMSG8:
742 case PCAN_USBPRO_RXMSG4:
743 case PCAN_USBPRO_RXMSG0:
744 case PCAN_USBPRO_RXRTR:
745 err = pcan_usb_pro_handle_canmsg(usb_if, &pr->rx_msg);
746 if (err < 0)
747 goto fail;
748 break;
750 case PCAN_USBPRO_RXSTATUS:
751 err = pcan_usb_pro_handle_error(usb_if, &pr->rx_status);
752 if (err < 0)
753 goto fail;
754 break;
756 case PCAN_USBPRO_RXTS:
757 pcan_usb_pro_handle_ts(usb_if, &pr->rx_ts);
758 break;
760 default:
761 netdev_err(netdev,
762 "unhandled rec type 0x%02x (%d): ignored\n",
763 pr->data_type, pr->data_type);
764 break;
767 rec_ptr += sizeof_rec;
770 fail:
771 if (err)
772 pcan_dump_mem("received msg",
773 urb->transfer_buffer, urb->actual_length);
775 return err;
778 static int pcan_usb_pro_encode_msg(struct peak_usb_device *dev,
779 struct sk_buff *skb, u8 *obuf, size_t *size)
781 struct can_frame *cf = (struct can_frame *)skb->data;
782 u8 data_type, len, flags;
783 struct pcan_usb_pro_msg usb_msg;
785 pcan_msg_init_empty(&usb_msg, obuf, *size);
787 if ((cf->can_id & CAN_RTR_FLAG) || (cf->can_dlc == 0))
788 data_type = PCAN_USBPRO_TXMSG0;
789 else if (cf->can_dlc <= 4)
790 data_type = PCAN_USBPRO_TXMSG4;
791 else
792 data_type = PCAN_USBPRO_TXMSG8;
794 len = (dev->ctrl_idx << 4) | (cf->can_dlc & 0x0f);
796 flags = 0;
797 if (cf->can_id & CAN_EFF_FLAG)
798 flags |= 0x02;
799 if (cf->can_id & CAN_RTR_FLAG)
800 flags |= 0x01;
802 pcan_msg_add_rec(&usb_msg, data_type, 0, flags, len, cf->can_id,
803 cf->data);
805 *size = usb_msg.rec_buffer_len;
807 return 0;
810 static int pcan_usb_pro_start(struct peak_usb_device *dev)
812 struct pcan_usb_pro_device *pdev =
813 container_of(dev, struct pcan_usb_pro_device, dev);
814 int err;
816 err = pcan_usb_pro_set_silent(dev,
817 dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY);
818 if (err)
819 return err;
821 /* filter mode: 0-> All OFF; 1->bypass */
822 err = pcan_usb_pro_set_filter(dev, 1);
823 if (err)
824 return err;
826 /* opening first device: */
827 if (pdev->usb_if->dev_opened_count == 0) {
828 /* reset time_ref */
829 peak_usb_init_time_ref(&pdev->usb_if->time_ref, &pcan_usb_pro);
831 /* ask device to send ts messages */
832 err = pcan_usb_pro_set_ts(dev, 1);
835 pdev->usb_if->dev_opened_count++;
837 return err;
841 * stop interface
842 * (last chance before set bus off)
844 static int pcan_usb_pro_stop(struct peak_usb_device *dev)
846 struct pcan_usb_pro_device *pdev =
847 container_of(dev, struct pcan_usb_pro_device, dev);
849 /* turn off ts msgs for that interface if no other dev opened */
850 if (pdev->usb_if->dev_opened_count == 1)
851 pcan_usb_pro_set_ts(dev, 0);
853 pdev->usb_if->dev_opened_count--;
855 return 0;
859 * called when probing to initialize a device object.
861 static int pcan_usb_pro_init(struct peak_usb_device *dev)
863 struct pcan_usb_pro_device *pdev =
864 container_of(dev, struct pcan_usb_pro_device, dev);
865 struct pcan_usb_pro_interface *usb_if = NULL;
866 struct pcan_usb_pro_fwinfo *fi = NULL;
867 struct pcan_usb_pro_blinfo *bi = NULL;
868 int err;
870 /* do this for 1st channel only */
871 if (!dev->prev_siblings) {
872 /* allocate netdevices common structure attached to first one */
873 usb_if = kzalloc(sizeof(struct pcan_usb_pro_interface),
874 GFP_KERNEL);
875 fi = kmalloc(sizeof(struct pcan_usb_pro_fwinfo), GFP_KERNEL);
876 bi = kmalloc(sizeof(struct pcan_usb_pro_blinfo), GFP_KERNEL);
877 if (!usb_if || !fi || !bi) {
878 err = -ENOMEM;
879 goto err_out;
882 /* number of ts msgs to ignore before taking one into account */
883 usb_if->cm_ignore_count = 5;
886 * explicit use of dev_xxx() instead of netdev_xxx() here:
887 * information displayed are related to the device itself, not
888 * to the canx netdevices.
890 err = pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_INFO,
891 PCAN_USBPRO_INFO_FW,
892 fi, sizeof(*fi));
893 if (err) {
894 dev_err(dev->netdev->dev.parent,
895 "unable to read %s firmware info (err %d)\n",
896 pcan_usb_pro.name, err);
897 goto err_out;
900 err = pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_INFO,
901 PCAN_USBPRO_INFO_BL,
902 bi, sizeof(*bi));
903 if (err) {
904 dev_err(dev->netdev->dev.parent,
905 "unable to read %s bootloader info (err %d)\n",
906 pcan_usb_pro.name, err);
907 goto err_out;
910 /* tell the device the can driver is running */
911 err = pcan_usb_pro_drv_loaded(dev, 1);
912 if (err)
913 goto err_out;
915 dev_info(dev->netdev->dev.parent,
916 "PEAK-System %s hwrev %u serial %08X.%08X (%u channels)\n",
917 pcan_usb_pro.name,
918 bi->hw_rev, bi->serial_num_hi, bi->serial_num_lo,
919 pcan_usb_pro.ctrl_count);
920 } else {
921 usb_if = pcan_usb_pro_dev_if(dev->prev_siblings);
924 pdev->usb_if = usb_if;
925 usb_if->dev[dev->ctrl_idx] = dev;
927 /* set LED in default state (end of init phase) */
928 pcan_usb_pro_set_led(dev, 0, 1);
930 kfree(bi);
931 kfree(fi);
933 return 0;
935 err_out:
936 kfree(bi);
937 kfree(fi);
938 kfree(usb_if);
940 return err;
943 static void pcan_usb_pro_exit(struct peak_usb_device *dev)
945 struct pcan_usb_pro_device *pdev =
946 container_of(dev, struct pcan_usb_pro_device, dev);
949 * when rmmod called before unplug and if down, should reset things
950 * before leaving
952 if (dev->can.state != CAN_STATE_STOPPED) {
953 /* set bus off on the corresponding channel */
954 pcan_usb_pro_set_bus(dev, 0);
957 /* if channel #0 (only) */
958 if (dev->ctrl_idx == 0) {
959 /* turn off calibration message if any device were opened */
960 if (pdev->usb_if->dev_opened_count > 0)
961 pcan_usb_pro_set_ts(dev, 0);
963 /* tell the PCAN-USB Pro device the driver is being unloaded */
964 pcan_usb_pro_drv_loaded(dev, 0);
969 * called when PCAN-USB Pro adapter is unplugged
971 static void pcan_usb_pro_free(struct peak_usb_device *dev)
973 /* last device: can free pcan_usb_pro_interface object now */
974 if (!dev->prev_siblings && !dev->next_siblings)
975 kfree(pcan_usb_pro_dev_if(dev));
979 * probe function for new PCAN-USB Pro usb interface
981 static int pcan_usb_pro_probe(struct usb_interface *intf)
983 struct usb_host_interface *if_desc;
984 int i;
986 if_desc = intf->altsetting;
988 /* check interface endpoint addresses */
989 for (i = 0; i < if_desc->desc.bNumEndpoints; i++) {
990 struct usb_endpoint_descriptor *ep = &if_desc->endpoint[i].desc;
993 * below is the list of valid ep addreses. Any other ep address
994 * is considered as not-CAN interface address => no dev created
996 switch (ep->bEndpointAddress) {
997 case PCAN_USBPRO_EP_CMDOUT:
998 case PCAN_USBPRO_EP_CMDIN:
999 case PCAN_USBPRO_EP_MSGOUT_0:
1000 case PCAN_USBPRO_EP_MSGOUT_1:
1001 case PCAN_USBPRO_EP_MSGIN:
1002 case PCAN_USBPRO_EP_UNUSED:
1003 break;
1004 default:
1005 return -ENODEV;
1009 return 0;
1013 * describe the PCAN-USB Pro adapter
1015 struct peak_usb_adapter pcan_usb_pro = {
1016 .name = "PCAN-USB Pro",
1017 .device_id = PCAN_USBPRO_PRODUCT_ID,
1018 .ctrl_count = PCAN_USBPRO_CHANNEL_COUNT,
1019 .clock = {
1020 .freq = PCAN_USBPRO_CRYSTAL_HZ,
1022 .bittiming_const = {
1023 .name = "pcan_usb_pro",
1024 .tseg1_min = 1,
1025 .tseg1_max = 16,
1026 .tseg2_min = 1,
1027 .tseg2_max = 8,
1028 .sjw_max = 4,
1029 .brp_min = 1,
1030 .brp_max = 1024,
1031 .brp_inc = 1,
1034 /* size of device private data */
1035 .sizeof_dev_private = sizeof(struct pcan_usb_pro_device),
1037 /* timestamps usage */
1038 .ts_used_bits = 32,
1039 .ts_period = 1000000, /* calibration period in ts. */
1040 .us_per_ts_scale = 1, /* us = (ts * scale) >> shift */
1041 .us_per_ts_shift = 0,
1043 /* give here messages in/out endpoints */
1044 .ep_msg_in = PCAN_USBPRO_EP_MSGIN,
1045 .ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0, PCAN_USBPRO_EP_MSGOUT_1},
1047 /* size of rx/tx usb buffers */
1048 .rx_buffer_size = PCAN_USBPRO_RX_BUFFER_SIZE,
1049 .tx_buffer_size = PCAN_USBPRO_TX_BUFFER_SIZE,
1051 /* device callbacks */
1052 .intf_probe = pcan_usb_pro_probe,
1053 .dev_init = pcan_usb_pro_init,
1054 .dev_exit = pcan_usb_pro_exit,
1055 .dev_free = pcan_usb_pro_free,
1056 .dev_set_bus = pcan_usb_pro_set_bus,
1057 .dev_set_bittiming = pcan_usb_pro_set_bittiming,
1058 .dev_get_device_id = pcan_usb_pro_get_device_id,
1059 .dev_decode_buf = pcan_usb_pro_decode_buf,
1060 .dev_encode_msg = pcan_usb_pro_encode_msg,
1061 .dev_start = pcan_usb_pro_start,
1062 .dev_stop = pcan_usb_pro_stop,
1063 .dev_restart_async = pcan_usb_pro_restart_async,