aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/net/can/usb
diff options
context:
space:
mode:
authorStephane Grosjean <s.grosjean@peak-system.com>2012-03-02 10:13:05 -0500
committerMarc Kleine-Budde <mkl@pengutronix.de>2012-03-03 11:40:55 -0500
commit46be265d3388339d8168c863791f0c7bbf3f2fed (patch)
treee330159305bebee1874001cc2815a8346b61a213 /drivers/net/can/usb
parentbb4785551f64e18b2c8bb15a3bd2b22f5ebf624d (diff)
can: usb: PEAK-System Technik PCAN-USB specific part
This patch adds the specific part which handles the PCAN-USB adapter from PEAK-System Technik (http://www.peak-system.com). The PCAN-USB adapter is a sja1000 based, mono-channel USB 1.1 adapter compliant with CAN specifications 2.0A (11-bit ID) and 2.0B (29-bit ID). Tested-by: Oliver Hartkopp <socketcan@hartkopp.net> Acked-by: Wolfgang Grandegger <wg@grandegger.com> Signed-off-by: Stephane Grosjean <s.grosjean@peak-system.com> Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
Diffstat (limited to 'drivers/net/can/usb')
-rw-r--r--drivers/net/can/usb/peak_usb/Makefile2
-rw-r--r--drivers/net/can/usb/peak_usb/pcan_usb.c901
2 files changed, 902 insertions, 1 deletions
diff --git a/drivers/net/can/usb/peak_usb/Makefile b/drivers/net/can/usb/peak_usb/Makefile
index bbb23ceb65f..4450466fa57 100644
--- a/drivers/net/can/usb/peak_usb/Makefile
+++ b/drivers/net/can/usb/peak_usb/Makefile
@@ -1,2 +1,2 @@
1obj-$(CONFIG_CAN_PEAK_USB) += peak_usb.o 1obj-$(CONFIG_CAN_PEAK_USB) += peak_usb.o
2peak_usb-y = pcan_usb_core.o 2peak_usb-y = pcan_usb_core.o pcan_usb.o
diff --git a/drivers/net/can/usb/peak_usb/pcan_usb.c b/drivers/net/can/usb/peak_usb/pcan_usb.c
new file mode 100644
index 00000000000..8a7982e18e4
--- /dev/null
+++ b/drivers/net/can/usb/peak_usb/pcan_usb.c
@@ -0,0 +1,901 @@
1/*
2 * CAN driver for PEAK System PCAN-USB adapter
3 * Derived from the PCAN project file driver/src/pcan_usb.c
4 *
5 * Copyright (C) 2003-2010 PEAK System-Technik GmbH
6 * Copyright (C) 2011-2012 Stephane Grosjean <s.grosjean@peak-system.com>
7 *
8 * Many thanks to Klaus Hitschler <klaus.hitschler@gmx.de>
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published
12 * by the Free Software Foundation; version 2 of the License.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 */
19#include <linux/netdevice.h>
20#include <linux/usb.h>
21#include <linux/module.h>
22
23#include <linux/can.h>
24#include <linux/can/dev.h>
25#include <linux/can/error.h>
26
27#include "pcan_usb_core.h"
28
29MODULE_SUPPORTED_DEVICE("PEAK-System PCAN-USB adapter");
30
31/* PCAN-USB Endpoints */
32#define PCAN_USB_EP_CMDOUT 1
33#define PCAN_USB_EP_CMDIN (PCAN_USB_EP_CMDOUT | USB_DIR_IN)
34#define PCAN_USB_EP_MSGOUT 2
35#define PCAN_USB_EP_MSGIN (PCAN_USB_EP_MSGOUT | USB_DIR_IN)
36
37/* PCAN-USB command struct */
38#define PCAN_USB_CMD_FUNC 0
39#define PCAN_USB_CMD_NUM 1
40#define PCAN_USB_CMD_ARGS 2
41#define PCAN_USB_CMD_ARGS_LEN 14
42#define PCAN_USB_CMD_LEN (PCAN_USB_CMD_ARGS + \
43 PCAN_USB_CMD_ARGS_LEN)
44
45/* PCAN-USB command timeout (ms.) */
46#define PCAN_USB_COMMAND_TIMEOUT 1000
47
48/* PCAN-USB startup timeout (ms.) */
49#define PCAN_USB_STARTUP_TIMEOUT 10
50
51/* PCAN-USB rx/tx buffers size */
52#define PCAN_USB_RX_BUFFER_SIZE 64
53#define PCAN_USB_TX_BUFFER_SIZE 64
54
55#define PCAN_USB_MSG_HEADER_LEN 2
56
57/* PCAN-USB adapter internal clock (MHz) */
58#define PCAN_USB_CRYSTAL_HZ 16000000
59
60/* PCAN-USB USB message record status/len field */
61#define PCAN_USB_STATUSLEN_TIMESTAMP (1 << 7)
62#define PCAN_USB_STATUSLEN_INTERNAL (1 << 6)
63#define PCAN_USB_STATUSLEN_EXT_ID (1 << 5)
64#define PCAN_USB_STATUSLEN_RTR (1 << 4)
65#define PCAN_USB_STATUSLEN_DLC (0xf)
66
67/* PCAN-USB error flags */
68#define PCAN_USB_ERROR_TXFULL 0x01
69#define PCAN_USB_ERROR_RXQOVR 0x02
70#define PCAN_USB_ERROR_BUS_LIGHT 0x04
71#define PCAN_USB_ERROR_BUS_HEAVY 0x08
72#define PCAN_USB_ERROR_BUS_OFF 0x10
73#define PCAN_USB_ERROR_RXQEMPTY 0x20
74#define PCAN_USB_ERROR_QOVR 0x40
75#define PCAN_USB_ERROR_TXQFULL 0x80
76
77/* SJA1000 modes */
78#define SJA1000_MODE_NORMAL 0x00
79#define SJA1000_MODE_INIT 0x01
80
81/*
82 * tick duration = 42.666 us =>
83 * (tick_number * 44739243) >> 20 ~ (tick_number * 42666) / 1000
84 * accuracy = 10^-7
85 */
86#define PCAN_USB_TS_DIV_SHIFTER 20
87#define PCAN_USB_TS_US_PER_TICK 44739243
88
89/* PCAN-USB messages record types */
90#define PCAN_USB_REC_ERROR 1
91#define PCAN_USB_REC_ANALOG 2
92#define PCAN_USB_REC_BUSLOAD 3
93#define PCAN_USB_REC_TS 4
94#define PCAN_USB_REC_BUSEVT 5
95
96/* private to PCAN-USB adapter */
97struct pcan_usb {
98 struct peak_usb_device dev;
99 struct peak_time_ref time_ref;
100 struct timer_list restart_timer;
101};
102
103/* incoming message context for decoding */
104struct pcan_usb_msg_context {
105 u16 ts16;
106 u8 prev_ts8;
107 u8 *ptr;
108 u8 *end;
109 u8 rec_cnt;
110 u8 rec_idx;
111 u8 rec_data_idx;
112 struct net_device *netdev;
113 struct pcan_usb *pdev;
114};
115
116/*
117 * send a command
118 */
119static int pcan_usb_send_cmd(struct peak_usb_device *dev, u8 f, u8 n, u8 *p)
120{
121 int err;
122 int actual_length;
123
124 /* usb device unregistered? */
125 if (!(dev->state & PCAN_USB_STATE_CONNECTED))
126 return 0;
127
128 dev->cmd_buf[PCAN_USB_CMD_FUNC] = f;
129 dev->cmd_buf[PCAN_USB_CMD_NUM] = n;
130
131 if (p)
132 memcpy(dev->cmd_buf + PCAN_USB_CMD_ARGS,
133 p, PCAN_USB_CMD_ARGS_LEN);
134
135 err = usb_bulk_msg(dev->udev,
136 usb_sndbulkpipe(dev->udev, PCAN_USB_EP_CMDOUT),
137 dev->cmd_buf, PCAN_USB_CMD_LEN, &actual_length,
138 PCAN_USB_COMMAND_TIMEOUT);
139 if (err)
140 netdev_err(dev->netdev,
141 "sending cmd f=0x%x n=0x%x failure: %d\n",
142 f, n, err);
143 return err;
144}
145
146/*
147 * send a command then wait for its response
148 */
149static int pcan_usb_wait_rsp(struct peak_usb_device *dev, u8 f, u8 n, u8 *p)
150{
151 int err;
152 int actual_length;
153
154 /* usb device unregistered? */
155 if (!(dev->state & PCAN_USB_STATE_CONNECTED))
156 return 0;
157
158 /* first, send command */
159 err = pcan_usb_send_cmd(dev, f, n, NULL);
160 if (err)
161 return err;
162
163 err = usb_bulk_msg(dev->udev,
164 usb_rcvbulkpipe(dev->udev, PCAN_USB_EP_CMDIN),
165 dev->cmd_buf, PCAN_USB_CMD_LEN, &actual_length,
166 PCAN_USB_COMMAND_TIMEOUT);
167 if (err)
168 netdev_err(dev->netdev,
169 "waiting rsp f=0x%x n=0x%x failure: %d\n", f, n, err);
170 else if (p)
171 memcpy(p, dev->cmd_buf + PCAN_USB_CMD_ARGS,
172 PCAN_USB_CMD_ARGS_LEN);
173
174 return err;
175}
176
177static int pcan_usb_set_sja1000(struct peak_usb_device *dev, u8 mode)
178{
179 u8 args[PCAN_USB_CMD_ARGS_LEN] = {
180 [1] = mode,
181 };
182
183 return pcan_usb_send_cmd(dev, 9, 2, args);
184}
185
186static int pcan_usb_set_bus(struct peak_usb_device *dev, u8 onoff)
187{
188 u8 args[PCAN_USB_CMD_ARGS_LEN] = {
189 [0] = !!onoff,
190 };
191
192 return pcan_usb_send_cmd(dev, 3, 2, args);
193}
194
195static int pcan_usb_set_silent(struct peak_usb_device *dev, u8 onoff)
196{
197 u8 args[PCAN_USB_CMD_ARGS_LEN] = {
198 [0] = !!onoff,
199 };
200
201 return pcan_usb_send_cmd(dev, 3, 3, args);
202}
203
204static int pcan_usb_set_ext_vcc(struct peak_usb_device *dev, u8 onoff)
205{
206 u8 args[PCAN_USB_CMD_ARGS_LEN] = {
207 [0] = !!onoff,
208 };
209
210 return pcan_usb_send_cmd(dev, 10, 2, args);
211}
212
213/*
214 * set bittiming value to can
215 */
216static int pcan_usb_set_bittiming(struct peak_usb_device *dev,
217 struct can_bittiming *bt)
218{
219 u8 args[PCAN_USB_CMD_ARGS_LEN];
220 u8 btr0, btr1;
221
222 btr0 = ((bt->brp - 1) & 0x3f) | (((bt->sjw - 1) & 0x3) << 6);
223 btr1 = ((bt->prop_seg + bt->phase_seg1 - 1) & 0xf) |
224 (((bt->phase_seg2 - 1) & 0x7) << 4);
225 if (dev->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
226 btr1 |= 0x80;
227
228 netdev_info(dev->netdev, "setting BTR0=0x%02x BTR1=0x%02x\n",
229 btr0, btr1);
230
231 args[0] = btr1;
232 args[1] = btr0;
233
234 return pcan_usb_send_cmd(dev, 1, 2, args);
235}
236
237/*
238 * init/reset can
239 */
240static int pcan_usb_write_mode(struct peak_usb_device *dev, u8 onoff)
241{
242 int err;
243
244 err = pcan_usb_set_bus(dev, onoff);
245 if (err)
246 return err;
247
248 if (!onoff) {
249 err = pcan_usb_set_sja1000(dev, SJA1000_MODE_INIT);
250 } else {
251 /* the PCAN-USB needs time to init */
252 set_current_state(TASK_INTERRUPTIBLE);
253 schedule_timeout(msecs_to_jiffies(PCAN_USB_STARTUP_TIMEOUT));
254 }
255
256 return err;
257}
258
259/*
260 * handle end of waiting for the device to reset
261 */
262static void pcan_usb_restart(unsigned long arg)
263{
264 /* notify candev and netdev */
265 peak_usb_restart_complete((struct peak_usb_device *)arg);
266}
267
268/*
269 * handle the submission of the restart urb
270 */
271static void pcan_usb_restart_pending(struct urb *urb)
272{
273 struct pcan_usb *pdev = urb->context;
274
275 /* the PCAN-USB needs time to restart */
276 mod_timer(&pdev->restart_timer,
277 jiffies + msecs_to_jiffies(PCAN_USB_STARTUP_TIMEOUT));
278
279 /* can delete usb resources */
280 peak_usb_async_complete(urb);
281}
282
283/*
284 * handle asynchronous restart
285 */
286static int pcan_usb_restart_async(struct peak_usb_device *dev, struct urb *urb,
287 u8 *buf)
288{
289 struct pcan_usb *pdev = container_of(dev, struct pcan_usb, dev);
290
291 if (timer_pending(&pdev->restart_timer))
292 return -EBUSY;
293
294 /* set bus on */
295 buf[PCAN_USB_CMD_FUNC] = 3;
296 buf[PCAN_USB_CMD_NUM] = 2;
297 buf[PCAN_USB_CMD_ARGS] = 1;
298
299 usb_fill_bulk_urb(urb, dev->udev,
300 usb_sndbulkpipe(dev->udev, PCAN_USB_EP_CMDOUT),
301 buf, PCAN_USB_CMD_LEN,
302 pcan_usb_restart_pending, pdev);
303
304 return usb_submit_urb(urb, GFP_ATOMIC);
305}
306
307/*
308 * read serial number from device
309 */
310static int pcan_usb_get_serial(struct peak_usb_device *dev, u32 *serial_number)
311{
312 u8 args[PCAN_USB_CMD_ARGS_LEN];
313 int err;
314
315 err = pcan_usb_wait_rsp(dev, 6, 1, args);
316 if (err) {
317 netdev_err(dev->netdev, "getting serial failure: %d\n", err);
318 } else if (serial_number) {
319 u32 tmp32;
320
321 memcpy(&tmp32, args, 4);
322 *serial_number = le32_to_cpu(tmp32);
323 }
324
325 return err;
326}
327
328/*
329 * read device id from device
330 */
331static int pcan_usb_get_device_id(struct peak_usb_device *dev, u32 *device_id)
332{
333 u8 args[PCAN_USB_CMD_ARGS_LEN];
334 int err;
335
336 err = pcan_usb_wait_rsp(dev, 4, 1, args);
337 if (err)
338 netdev_err(dev->netdev, "getting device id failure: %d\n", err);
339 else if (device_id)
340 *device_id = args[0];
341
342 return err;
343}
344
345/*
346 * update current time ref with received timestamp
347 */
348static int pcan_usb_update_ts(struct pcan_usb_msg_context *mc)
349{
350 u16 tmp16;
351
352 if ((mc->ptr+2) > mc->end)
353 return -EINVAL;
354
355 memcpy(&tmp16, mc->ptr, 2);
356
357 mc->ts16 = le16_to_cpu(tmp16);
358
359 if (mc->rec_idx > 0)
360 peak_usb_update_ts_now(&mc->pdev->time_ref, mc->ts16);
361 else
362 peak_usb_set_ts_now(&mc->pdev->time_ref, mc->ts16);
363
364 return 0;
365}
366
367/*
368 * decode received timestamp
369 */
370static int pcan_usb_decode_ts(struct pcan_usb_msg_context *mc, u8 first_packet)
371{
372 /* only 1st packet supplies a word timestamp */
373 if (first_packet) {
374 u16 tmp16;
375
376 if ((mc->ptr + 2) > mc->end)
377 return -EINVAL;
378
379 memcpy(&tmp16, mc->ptr, 2);
380 mc->ptr += 2;
381
382 mc->ts16 = le16_to_cpu(tmp16);
383 mc->prev_ts8 = mc->ts16 & 0x00ff;
384 } else {
385 u8 ts8;
386
387 if ((mc->ptr + 1) > mc->end)
388 return -EINVAL;
389
390 ts8 = *mc->ptr++;
391
392 if (ts8 < mc->prev_ts8)
393 mc->ts16 += 0x100;
394
395 mc->ts16 &= 0xff00;
396 mc->ts16 |= ts8;
397 mc->prev_ts8 = ts8;
398 }
399
400 return 0;
401}
402
403static int pcan_usb_decode_error(struct pcan_usb_msg_context *mc, u8 n,
404 u8 status_len)
405{
406 struct sk_buff *skb;
407 struct can_frame *cf;
408 struct timeval tv;
409 enum can_state new_state;
410
411 /* ignore this error until 1st ts received */
412 if (n == PCAN_USB_ERROR_QOVR)
413 if (!mc->pdev->time_ref.tick_count)
414 return 0;
415
416 new_state = mc->pdev->dev.can.state;
417
418 switch (mc->pdev->dev.can.state) {
419 case CAN_STATE_ERROR_ACTIVE:
420 if (n & PCAN_USB_ERROR_BUS_LIGHT) {
421 new_state = CAN_STATE_ERROR_WARNING;
422 break;
423 }
424
425 case CAN_STATE_ERROR_WARNING:
426 if (n & PCAN_USB_ERROR_BUS_HEAVY) {
427 new_state = CAN_STATE_ERROR_PASSIVE;
428 break;
429 }
430 if (n & PCAN_USB_ERROR_BUS_OFF) {
431 new_state = CAN_STATE_BUS_OFF;
432 break;
433 }
434 if (n & (PCAN_USB_ERROR_RXQOVR | PCAN_USB_ERROR_QOVR)) {
435 /*
436 * trick to bypass next comparison and process other
437 * errors
438 */
439 new_state = CAN_STATE_MAX;
440 break;
441 }
442 if ((n & PCAN_USB_ERROR_BUS_LIGHT) == 0) {
443 /* no error (back to active state) */
444 mc->pdev->dev.can.state = CAN_STATE_ERROR_ACTIVE;
445 return 0;
446 }
447 break;
448
449 case CAN_STATE_ERROR_PASSIVE:
450 if (n & PCAN_USB_ERROR_BUS_OFF) {
451 new_state = CAN_STATE_BUS_OFF;
452 break;
453 }
454 if (n & PCAN_USB_ERROR_BUS_LIGHT) {
455 new_state = CAN_STATE_ERROR_WARNING;
456 break;
457 }
458 if (n & (PCAN_USB_ERROR_RXQOVR | PCAN_USB_ERROR_QOVR)) {
459 /*
460 * trick to bypass next comparison and process other
461 * errors
462 */
463 new_state = CAN_STATE_MAX;
464 break;
465 }
466
467 if ((n & PCAN_USB_ERROR_BUS_HEAVY) == 0) {
468 /* no error (back to active state) */
469 mc->pdev->dev.can.state = CAN_STATE_ERROR_ACTIVE;
470 return 0;
471 }
472 break;
473
474 default:
475 /* do nothing waiting for restart */
476 return 0;
477 }
478
479 /* donot post any error if current state didn't change */
480 if (mc->pdev->dev.can.state == new_state)
481 return 0;
482
483 /* allocate an skb to store the error frame */
484 skb = alloc_can_err_skb(mc->netdev, &cf);
485 if (!skb)
486 return -ENOMEM;
487
488 switch (new_state) {
489 case CAN_STATE_BUS_OFF:
490 cf->can_id |= CAN_ERR_BUSOFF;
491 can_bus_off(mc->netdev);
492 break;
493
494 case CAN_STATE_ERROR_PASSIVE:
495 cf->can_id |= CAN_ERR_CRTL;
496 cf->data[1] |= CAN_ERR_CRTL_TX_PASSIVE |
497 CAN_ERR_CRTL_RX_PASSIVE;
498 mc->pdev->dev.can.can_stats.error_passive++;
499 break;
500
501 case CAN_STATE_ERROR_WARNING:
502 cf->can_id |= CAN_ERR_CRTL;
503 cf->data[1] |= CAN_ERR_CRTL_TX_WARNING |
504 CAN_ERR_CRTL_RX_WARNING;
505 mc->pdev->dev.can.can_stats.error_warning++;
506 break;
507
508 default:
509 /* CAN_STATE_MAX (trick to handle other errors) */
510 cf->can_id |= CAN_ERR_CRTL;
511 cf->data[1] |= CAN_ERR_CRTL_RX_OVERFLOW;
512 mc->netdev->stats.rx_over_errors++;
513 mc->netdev->stats.rx_errors++;
514
515 new_state = mc->pdev->dev.can.state;
516 break;
517 }
518
519 mc->pdev->dev.can.state = new_state;
520
521 if (status_len & PCAN_USB_STATUSLEN_TIMESTAMP) {
522 peak_usb_get_ts_tv(&mc->pdev->time_ref, mc->ts16, &tv);
523 skb->tstamp = timeval_to_ktime(tv);
524 }
525
526 netif_rx(skb);
527 mc->netdev->stats.rx_packets++;
528 mc->netdev->stats.rx_bytes += cf->can_dlc;
529
530 return 0;
531}
532
533/*
534 * decode non-data usb message
535 */
536static int pcan_usb_decode_status(struct pcan_usb_msg_context *mc,
537 u8 status_len)
538{
539 u8 rec_len = status_len & PCAN_USB_STATUSLEN_DLC;
540 u8 f, n;
541 int err;
542
543 /* check whether function and number can be read */
544 if ((mc->ptr + 2) > mc->end)
545 return -EINVAL;
546
547 f = mc->ptr[PCAN_USB_CMD_FUNC];
548 n = mc->ptr[PCAN_USB_CMD_NUM];
549 mc->ptr += PCAN_USB_CMD_ARGS;
550
551 if (status_len & PCAN_USB_STATUSLEN_TIMESTAMP) {
552 int err = pcan_usb_decode_ts(mc, !mc->rec_idx);
553
554 if (err)
555 return err;
556 }
557
558 switch (f) {
559 case PCAN_USB_REC_ERROR:
560 err = pcan_usb_decode_error(mc, n, status_len);
561 if (err)
562 return err;
563 break;
564
565 case PCAN_USB_REC_ANALOG:
566 /* analog values (ignored) */
567 rec_len = 2;
568 break;
569
570 case PCAN_USB_REC_BUSLOAD:
571 /* bus load (ignored) */
572 rec_len = 1;
573 break;
574
575 case PCAN_USB_REC_TS:
576 /* only timestamp */
577 if (pcan_usb_update_ts(mc))
578 return -EINVAL;
579 break;
580
581 case PCAN_USB_REC_BUSEVT:
582 /* error frame/bus event */
583 if (n & PCAN_USB_ERROR_TXQFULL)
584 netdev_dbg(mc->netdev, "device Tx queue full)\n");
585 break;
586 default:
587 netdev_err(mc->netdev, "unexpected function %u\n", f);
588 break;
589 }
590
591 if ((mc->ptr + rec_len) > mc->end)
592 return -EINVAL;
593
594 mc->ptr += rec_len;
595
596 return 0;
597}
598
599/*
600 * decode data usb message
601 */
602static int pcan_usb_decode_data(struct pcan_usb_msg_context *mc, u8 status_len)
603{
604 u8 rec_len = status_len & PCAN_USB_STATUSLEN_DLC;
605 struct sk_buff *skb;
606 struct can_frame *cf;
607 struct timeval tv;
608
609 skb = alloc_can_skb(mc->netdev, &cf);
610 if (!skb)
611 return -ENOMEM;
612
613 if (status_len & PCAN_USB_STATUSLEN_EXT_ID) {
614 u32 tmp32;
615
616 if ((mc->ptr + 4) > mc->end)
617 goto decode_failed;
618
619 memcpy(&tmp32, mc->ptr, 4);
620 mc->ptr += 4;
621
622 cf->can_id = le32_to_cpu(tmp32 >> 3) | CAN_EFF_FLAG;
623 } else {
624 u16 tmp16;
625
626 if ((mc->ptr + 2) > mc->end)
627 goto decode_failed;
628
629 memcpy(&tmp16, mc->ptr, 2);
630 mc->ptr += 2;
631
632 cf->can_id = le16_to_cpu(tmp16 >> 5);
633 }
634
635 cf->can_dlc = get_can_dlc(rec_len);
636
637 /* first data packet timestamp is a word */
638 if (pcan_usb_decode_ts(mc, !mc->rec_data_idx))
639 goto decode_failed;
640
641 /* read data */
642 memset(cf->data, 0x0, sizeof(cf->data));
643 if (status_len & PCAN_USB_STATUSLEN_RTR) {
644 cf->can_id |= CAN_RTR_FLAG;
645 } else {
646 if ((mc->ptr + rec_len) > mc->end)
647 goto decode_failed;
648
649 memcpy(cf->data, mc->ptr, rec_len);
650 mc->ptr += rec_len;
651 }
652
653 /* convert timestamp into kernel time */
654 peak_usb_get_ts_tv(&mc->pdev->time_ref, mc->ts16, &tv);
655 skb->tstamp = timeval_to_ktime(tv);
656
657 /* push the skb */
658 netif_rx(skb);
659
660 /* update statistics */
661 mc->netdev->stats.rx_packets++;
662 mc->netdev->stats.rx_bytes += cf->can_dlc;
663
664 return 0;
665
666decode_failed:
667 dev_kfree_skb(skb);
668 return -EINVAL;
669}
670
671/*
672 * process incoming message
673 */
674static int pcan_usb_decode_msg(struct peak_usb_device *dev, u8 *ibuf, u32 lbuf)
675{
676 struct pcan_usb_msg_context mc = {
677 .rec_cnt = ibuf[1],
678 .ptr = ibuf + PCAN_USB_MSG_HEADER_LEN,
679 .end = ibuf + lbuf,
680 .netdev = dev->netdev,
681 .pdev = container_of(dev, struct pcan_usb, dev),
682 };
683 int err;
684
685 for (err = 0; mc.rec_idx < mc.rec_cnt && !err; mc.rec_idx++) {
686 u8 sl = *mc.ptr++;
687
688 /* handle status and error frames here */
689 if (sl & PCAN_USB_STATUSLEN_INTERNAL) {
690 err = pcan_usb_decode_status(&mc, sl);
691 /* handle normal can frames here */
692 } else {
693 err = pcan_usb_decode_data(&mc, sl);
694 mc.rec_data_idx++;
695 }
696 }
697
698 return err;
699}
700
701/*
702 * process any incoming buffer
703 */
704static int pcan_usb_decode_buf(struct peak_usb_device *dev, struct urb *urb)
705{
706 int err = 0;
707
708 if (urb->actual_length > PCAN_USB_MSG_HEADER_LEN) {
709 err = pcan_usb_decode_msg(dev, urb->transfer_buffer,
710 urb->actual_length);
711
712 } else if (urb->actual_length > 0) {
713 netdev_err(dev->netdev, "usb message length error (%u)\n",
714 urb->actual_length);
715 err = -EINVAL;
716 }
717
718 return err;
719}
720
721/*
722 * process outgoing packet
723 */
724static int pcan_usb_encode_msg(struct peak_usb_device *dev, struct sk_buff *skb,
725 u8 *obuf, size_t *size)
726{
727 struct net_device *netdev = dev->netdev;
728 struct net_device_stats *stats = &netdev->stats;
729 struct can_frame *cf = (struct can_frame *)skb->data;
730 u8 *pc;
731
732 obuf[0] = 2;
733 obuf[1] = 1;
734
735 pc = obuf + PCAN_USB_MSG_HEADER_LEN;
736
737 /* status/len byte */
738 *pc = cf->can_dlc;
739 if (cf->can_id & CAN_RTR_FLAG)
740 *pc |= PCAN_USB_STATUSLEN_RTR;
741
742 /* can id */
743 if (cf->can_id & CAN_EFF_FLAG) {
744 __le32 tmp32 = cpu_to_le32(cf->can_id & CAN_ERR_MASK);
745
746 tmp32 <<= 3;
747 *pc |= PCAN_USB_STATUSLEN_EXT_ID;
748 memcpy(++pc, &tmp32, 4);
749 pc += 4;
750 } else {
751 __le16 tmp16 = cpu_to_le32(cf->can_id & CAN_ERR_MASK);
752
753 tmp16 <<= 5;
754 memcpy(++pc, &tmp16, 2);
755 pc += 2;
756 }
757
758 /* can data */
759 if (!(cf->can_id & CAN_RTR_FLAG)) {
760 memcpy(pc, cf->data, cf->can_dlc);
761 pc += cf->can_dlc;
762 }
763
764 obuf[(*size)-1] = (u8)(stats->tx_packets & 0xff);
765
766 return 0;
767}
768
769/*
770 * start interface
771 */
772static int pcan_usb_start(struct peak_usb_device *dev)
773{
774 struct pcan_usb *pdev = container_of(dev, struct pcan_usb, dev);
775
776 /* number of bits used in timestamps read from adapter struct */
777 peak_usb_init_time_ref(&pdev->time_ref, &pcan_usb);
778
779 /* if revision greater than 3, can put silent mode on/off */
780 if (dev->device_rev > 3) {
781 int err;
782
783 err = pcan_usb_set_silent(dev,
784 dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY);
785 if (err)
786 return err;
787 }
788
789 return pcan_usb_set_ext_vcc(dev, 0);
790}
791
792static int pcan_usb_init(struct peak_usb_device *dev)
793{
794 struct pcan_usb *pdev = container_of(dev, struct pcan_usb, dev);
795 u32 serial_number;
796 int err;
797
798 /* initialize a timer needed to wait for hardware restart */
799 init_timer(&pdev->restart_timer);
800 pdev->restart_timer.function = pcan_usb_restart;
801 pdev->restart_timer.data = (unsigned long)dev;
802
803 /*
804 * explicit use of dev_xxx() instead of netdev_xxx() here:
805 * information displayed are related to the device itself, not
806 * to the canx netdevice.
807 */
808 err = pcan_usb_get_serial(dev, &serial_number);
809 if (err) {
810 dev_err(dev->netdev->dev.parent,
811 "unable to read %s serial number (err %d)\n",
812 pcan_usb.name, err);
813 return err;
814 }
815
816 dev_info(dev->netdev->dev.parent,
817 "PEAK-System %s adapter hwrev %u serial %08X (%u channel)\n",
818 pcan_usb.name, dev->device_rev, serial_number,
819 pcan_usb.ctrl_count);
820
821 return 0;
822}
823
824/*
825 * probe function for new PCAN-USB usb interface
826 */
827static int pcan_usb_probe(struct usb_interface *intf)
828{
829 struct usb_host_interface *if_desc;
830 int i;
831
832 if_desc = intf->altsetting;
833
834 /* check interface endpoint addresses */
835 for (i = 0; i < if_desc->desc.bNumEndpoints; i++) {
836 struct usb_endpoint_descriptor *ep = &if_desc->endpoint[i].desc;
837
838 switch (ep->bEndpointAddress) {
839 case PCAN_USB_EP_CMDOUT:
840 case PCAN_USB_EP_CMDIN:
841 case PCAN_USB_EP_MSGOUT:
842 case PCAN_USB_EP_MSGIN:
843 break;
844 default:
845 return -ENODEV;
846 }
847 }
848
849 return 0;
850}
851
852/*
853 * describe the PCAN-USB adapter
854 */
855struct peak_usb_adapter pcan_usb = {
856 .name = "PCAN-USB",
857 .device_id = PCAN_USB_PRODUCT_ID,
858 .ctrl_count = 1,
859 .clock = {
860 .freq = PCAN_USB_CRYSTAL_HZ / 2 ,
861 },
862 .bittiming_const = {
863 .name = "pcan_usb",
864 .tseg1_min = 1,
865 .tseg1_max = 16,
866 .tseg2_min = 1,
867 .tseg2_max = 8,
868 .sjw_max = 4,
869 .brp_min = 1,
870 .brp_max = 64,
871 .brp_inc = 1,
872 },
873
874 /* size of device private data */
875 .sizeof_dev_private = sizeof(struct pcan_usb),
876
877 /* timestamps usage */
878 .ts_used_bits = 16,
879 .ts_period = 24575, /* calibration period in ts. */
880 .us_per_ts_scale = PCAN_USB_TS_US_PER_TICK, /* us=(ts*scale) */
881 .us_per_ts_shift = PCAN_USB_TS_DIV_SHIFTER, /* >> shift */
882
883 /* give here messages in/out endpoints */
884 .ep_msg_in = PCAN_USB_EP_MSGIN,
885 .ep_msg_out = {PCAN_USB_EP_MSGOUT},
886
887 /* size of rx/tx usb buffers */
888 .rx_buffer_size = PCAN_USB_RX_BUFFER_SIZE,
889 .tx_buffer_size = PCAN_USB_TX_BUFFER_SIZE,
890
891 /* device callbacks */
892 .intf_probe = pcan_usb_probe,
893 .dev_init = pcan_usb_init,
894 .dev_set_bus = pcan_usb_write_mode,
895 .dev_set_bittiming = pcan_usb_set_bittiming,
896 .dev_get_device_id = pcan_usb_get_device_id,
897 .dev_decode_buf = pcan_usb_decode_buf,
898 .dev_encode_msg = pcan_usb_encode_msg,
899 .dev_start = pcan_usb_start,
900 .dev_restart_async = pcan_usb_restart_async,
901};