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
29 MODULE_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 */
97 struct 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 */
104 struct 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_ts_idx;
112 struct net_device *netdev;
113 struct pcan_usb *pdev;
114 };
115
116 /*
117 * send a command
118 */
pcan_usb_send_cmd(struct peak_usb_device * dev,u8 f,u8 n,u8 * p)119 static 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 */
pcan_usb_wait_rsp(struct peak_usb_device * dev,u8 f,u8 n,u8 * p)149 static 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
pcan_usb_set_sja1000(struct peak_usb_device * dev,u8 mode)177 static 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
pcan_usb_set_bus(struct peak_usb_device * dev,u8 onoff)186 static 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
pcan_usb_set_silent(struct peak_usb_device * dev,u8 onoff)195 static 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
pcan_usb_set_ext_vcc(struct peak_usb_device * dev,u8 onoff)204 static 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 */
pcan_usb_set_bittiming(struct peak_usb_device * dev,struct can_bittiming * bt)216 static 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 */
pcan_usb_write_mode(struct peak_usb_device * dev,u8 onoff)240 static 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 */
pcan_usb_restart(struct timer_list * t)262 static void pcan_usb_restart(struct timer_list *t)
263 {
264 struct pcan_usb *pdev = from_timer(pdev, t, restart_timer);
265 struct peak_usb_device *dev = &pdev->dev;
266
267 /* notify candev and netdev */
268 peak_usb_restart_complete(dev);
269 }
270
271 /*
272 * handle the submission of the restart urb
273 */
pcan_usb_restart_pending(struct urb * urb)274 static void pcan_usb_restart_pending(struct urb *urb)
275 {
276 struct pcan_usb *pdev = urb->context;
277
278 /* the PCAN-USB needs time to restart */
279 mod_timer(&pdev->restart_timer,
280 jiffies + msecs_to_jiffies(PCAN_USB_STARTUP_TIMEOUT));
281
282 /* can delete usb resources */
283 peak_usb_async_complete(urb);
284 }
285
286 /*
287 * handle asynchronous restart
288 */
pcan_usb_restart_async(struct peak_usb_device * dev,struct urb * urb,u8 * buf)289 static int pcan_usb_restart_async(struct peak_usb_device *dev, struct urb *urb,
290 u8 *buf)
291 {
292 struct pcan_usb *pdev = container_of(dev, struct pcan_usb, dev);
293
294 if (timer_pending(&pdev->restart_timer))
295 return -EBUSY;
296
297 /* set bus on */
298 buf[PCAN_USB_CMD_FUNC] = 3;
299 buf[PCAN_USB_CMD_NUM] = 2;
300 buf[PCAN_USB_CMD_ARGS] = 1;
301
302 usb_fill_bulk_urb(urb, dev->udev,
303 usb_sndbulkpipe(dev->udev, PCAN_USB_EP_CMDOUT),
304 buf, PCAN_USB_CMD_LEN,
305 pcan_usb_restart_pending, pdev);
306
307 return usb_submit_urb(urb, GFP_ATOMIC);
308 }
309
310 /*
311 * read serial number from device
312 */
pcan_usb_get_serial(struct peak_usb_device * dev,u32 * serial_number)313 static int pcan_usb_get_serial(struct peak_usb_device *dev, u32 *serial_number)
314 {
315 u8 args[PCAN_USB_CMD_ARGS_LEN];
316 int err;
317
318 err = pcan_usb_wait_rsp(dev, 6, 1, args);
319 if (err) {
320 netdev_err(dev->netdev, "getting serial failure: %d\n", err);
321 } else if (serial_number) {
322 __le32 tmp32;
323
324 memcpy(&tmp32, args, 4);
325 *serial_number = le32_to_cpu(tmp32);
326 }
327
328 return err;
329 }
330
331 /*
332 * read device id from device
333 */
pcan_usb_get_device_id(struct peak_usb_device * dev,u32 * device_id)334 static int pcan_usb_get_device_id(struct peak_usb_device *dev, u32 *device_id)
335 {
336 u8 args[PCAN_USB_CMD_ARGS_LEN];
337 int err;
338
339 err = pcan_usb_wait_rsp(dev, 4, 1, args);
340 if (err)
341 netdev_err(dev->netdev, "getting device id failure: %d\n", err);
342 else if (device_id)
343 *device_id = args[0];
344
345 return err;
346 }
347
348 /*
349 * update current time ref with received timestamp
350 */
pcan_usb_update_ts(struct pcan_usb_msg_context * mc)351 static int pcan_usb_update_ts(struct pcan_usb_msg_context *mc)
352 {
353 __le16 tmp16;
354
355 if ((mc->ptr+2) > mc->end)
356 return -EINVAL;
357
358 memcpy(&tmp16, mc->ptr, 2);
359
360 mc->ts16 = le16_to_cpu(tmp16);
361
362 if (mc->rec_idx > 0)
363 peak_usb_update_ts_now(&mc->pdev->time_ref, mc->ts16);
364 else
365 peak_usb_set_ts_now(&mc->pdev->time_ref, mc->ts16);
366
367 return 0;
368 }
369
370 /*
371 * decode received timestamp
372 */
pcan_usb_decode_ts(struct pcan_usb_msg_context * mc,u8 first_packet)373 static int pcan_usb_decode_ts(struct pcan_usb_msg_context *mc, u8 first_packet)
374 {
375 /* only 1st packet supplies a word timestamp */
376 if (first_packet) {
377 __le16 tmp16;
378
379 if ((mc->ptr + 2) > mc->end)
380 return -EINVAL;
381
382 memcpy(&tmp16, mc->ptr, 2);
383 mc->ptr += 2;
384
385 mc->ts16 = le16_to_cpu(tmp16);
386 mc->prev_ts8 = mc->ts16 & 0x00ff;
387 } else {
388 u8 ts8;
389
390 if ((mc->ptr + 1) > mc->end)
391 return -EINVAL;
392
393 ts8 = *mc->ptr++;
394
395 if (ts8 < mc->prev_ts8)
396 mc->ts16 += 0x100;
397
398 mc->ts16 &= 0xff00;
399 mc->ts16 |= ts8;
400 mc->prev_ts8 = ts8;
401 }
402
403 return 0;
404 }
405
pcan_usb_decode_error(struct pcan_usb_msg_context * mc,u8 n,u8 status_len)406 static int pcan_usb_decode_error(struct pcan_usb_msg_context *mc, u8 n,
407 u8 status_len)
408 {
409 struct sk_buff *skb;
410 struct can_frame *cf;
411 enum can_state new_state;
412
413 /* ignore this error until 1st ts received */
414 if (n == PCAN_USB_ERROR_QOVR)
415 if (!mc->pdev->time_ref.tick_count)
416 return 0;
417
418 new_state = mc->pdev->dev.can.state;
419
420 switch (mc->pdev->dev.can.state) {
421 case CAN_STATE_ERROR_ACTIVE:
422 if (n & PCAN_USB_ERROR_BUS_LIGHT) {
423 new_state = CAN_STATE_ERROR_WARNING;
424 break;
425 }
426 /* else: fall through */
427
428 case CAN_STATE_ERROR_WARNING:
429 if (n & PCAN_USB_ERROR_BUS_HEAVY) {
430 new_state = CAN_STATE_ERROR_PASSIVE;
431 break;
432 }
433 if (n & PCAN_USB_ERROR_BUS_OFF) {
434 new_state = CAN_STATE_BUS_OFF;
435 break;
436 }
437 if (n & (PCAN_USB_ERROR_RXQOVR | PCAN_USB_ERROR_QOVR)) {
438 /*
439 * trick to bypass next comparison and process other
440 * errors
441 */
442 new_state = CAN_STATE_MAX;
443 break;
444 }
445 if ((n & PCAN_USB_ERROR_BUS_LIGHT) == 0) {
446 /* no error (back to active state) */
447 new_state = CAN_STATE_ERROR_ACTIVE;
448 break;
449 }
450 break;
451
452 case CAN_STATE_ERROR_PASSIVE:
453 if (n & PCAN_USB_ERROR_BUS_OFF) {
454 new_state = CAN_STATE_BUS_OFF;
455 break;
456 }
457 if (n & PCAN_USB_ERROR_BUS_LIGHT) {
458 new_state = CAN_STATE_ERROR_WARNING;
459 break;
460 }
461 if (n & (PCAN_USB_ERROR_RXQOVR | PCAN_USB_ERROR_QOVR)) {
462 /*
463 * trick to bypass next comparison and process other
464 * errors
465 */
466 new_state = CAN_STATE_MAX;
467 break;
468 }
469
470 if ((n & PCAN_USB_ERROR_BUS_HEAVY) == 0) {
471 /* no error (back to warning state) */
472 new_state = CAN_STATE_ERROR_WARNING;
473 break;
474 }
475 break;
476
477 default:
478 /* do nothing waiting for restart */
479 return 0;
480 }
481
482 /* donot post any error if current state didn't change */
483 if (mc->pdev->dev.can.state == new_state)
484 return 0;
485
486 /* allocate an skb to store the error frame */
487 skb = alloc_can_err_skb(mc->netdev, &cf);
488 if (!skb)
489 return -ENOMEM;
490
491 switch (new_state) {
492 case CAN_STATE_BUS_OFF:
493 cf->can_id |= CAN_ERR_BUSOFF;
494 mc->pdev->dev.can.can_stats.bus_off++;
495 can_bus_off(mc->netdev);
496 break;
497
498 case CAN_STATE_ERROR_PASSIVE:
499 cf->can_id |= CAN_ERR_CRTL;
500 cf->data[1] |= CAN_ERR_CRTL_TX_PASSIVE |
501 CAN_ERR_CRTL_RX_PASSIVE;
502 mc->pdev->dev.can.can_stats.error_passive++;
503 break;
504
505 case CAN_STATE_ERROR_WARNING:
506 cf->can_id |= CAN_ERR_CRTL;
507 cf->data[1] |= CAN_ERR_CRTL_TX_WARNING |
508 CAN_ERR_CRTL_RX_WARNING;
509 mc->pdev->dev.can.can_stats.error_warning++;
510 break;
511
512 case CAN_STATE_ERROR_ACTIVE:
513 cf->can_id |= CAN_ERR_CRTL;
514 cf->data[1] = CAN_ERR_CRTL_ACTIVE;
515 break;
516
517 default:
518 /* CAN_STATE_MAX (trick to handle other errors) */
519 cf->can_id |= CAN_ERR_CRTL;
520 cf->data[1] |= CAN_ERR_CRTL_RX_OVERFLOW;
521 mc->netdev->stats.rx_over_errors++;
522 mc->netdev->stats.rx_errors++;
523
524 new_state = mc->pdev->dev.can.state;
525 break;
526 }
527
528 mc->pdev->dev.can.state = new_state;
529
530 if (status_len & PCAN_USB_STATUSLEN_TIMESTAMP) {
531 struct skb_shared_hwtstamps *hwts = skb_hwtstamps(skb);
532
533 peak_usb_get_ts_time(&mc->pdev->time_ref, mc->ts16,
534 &hwts->hwtstamp);
535 }
536
537 mc->netdev->stats.rx_packets++;
538 mc->netdev->stats.rx_bytes += cf->can_dlc;
539 netif_rx(skb);
540
541 return 0;
542 }
543
544 /*
545 * decode non-data usb message
546 */
pcan_usb_decode_status(struct pcan_usb_msg_context * mc,u8 status_len)547 static int pcan_usb_decode_status(struct pcan_usb_msg_context *mc,
548 u8 status_len)
549 {
550 u8 rec_len = status_len & PCAN_USB_STATUSLEN_DLC;
551 u8 f, n;
552 int err;
553
554 /* check whether function and number can be read */
555 if ((mc->ptr + 2) > mc->end)
556 return -EINVAL;
557
558 f = mc->ptr[PCAN_USB_CMD_FUNC];
559 n = mc->ptr[PCAN_USB_CMD_NUM];
560 mc->ptr += PCAN_USB_CMD_ARGS;
561
562 if (status_len & PCAN_USB_STATUSLEN_TIMESTAMP) {
563 int err = pcan_usb_decode_ts(mc, !mc->rec_ts_idx);
564
565 if (err)
566 return err;
567
568 /* Next packet in the buffer will have a timestamp on a single
569 * byte
570 */
571 mc->rec_ts_idx++;
572 }
573
574 switch (f) {
575 case PCAN_USB_REC_ERROR:
576 err = pcan_usb_decode_error(mc, n, status_len);
577 if (err)
578 return err;
579 break;
580
581 case PCAN_USB_REC_ANALOG:
582 /* analog values (ignored) */
583 rec_len = 2;
584 break;
585
586 case PCAN_USB_REC_BUSLOAD:
587 /* bus load (ignored) */
588 rec_len = 1;
589 break;
590
591 case PCAN_USB_REC_TS:
592 /* only timestamp */
593 if (pcan_usb_update_ts(mc))
594 return -EINVAL;
595 break;
596
597 case PCAN_USB_REC_BUSEVT:
598 /* error frame/bus event */
599 if (n & PCAN_USB_ERROR_TXQFULL)
600 netdev_dbg(mc->netdev, "device Tx queue full)\n");
601 break;
602 default:
603 netdev_err(mc->netdev, "unexpected function %u\n", f);
604 break;
605 }
606
607 if ((mc->ptr + rec_len) > mc->end)
608 return -EINVAL;
609
610 mc->ptr += rec_len;
611
612 return 0;
613 }
614
615 /*
616 * decode data usb message
617 */
pcan_usb_decode_data(struct pcan_usb_msg_context * mc,u8 status_len)618 static int pcan_usb_decode_data(struct pcan_usb_msg_context *mc, u8 status_len)
619 {
620 u8 rec_len = status_len & PCAN_USB_STATUSLEN_DLC;
621 struct sk_buff *skb;
622 struct can_frame *cf;
623 struct skb_shared_hwtstamps *hwts;
624
625 skb = alloc_can_skb(mc->netdev, &cf);
626 if (!skb)
627 return -ENOMEM;
628
629 if (status_len & PCAN_USB_STATUSLEN_EXT_ID) {
630 __le32 tmp32;
631
632 if ((mc->ptr + 4) > mc->end)
633 goto decode_failed;
634
635 memcpy(&tmp32, mc->ptr, 4);
636 mc->ptr += 4;
637
638 cf->can_id = (le32_to_cpu(tmp32) >> 3) | CAN_EFF_FLAG;
639 } else {
640 __le16 tmp16;
641
642 if ((mc->ptr + 2) > mc->end)
643 goto decode_failed;
644
645 memcpy(&tmp16, mc->ptr, 2);
646 mc->ptr += 2;
647
648 cf->can_id = le16_to_cpu(tmp16) >> 5;
649 }
650
651 cf->can_dlc = get_can_dlc(rec_len);
652
653 /* Only first packet timestamp is a word */
654 if (pcan_usb_decode_ts(mc, !mc->rec_ts_idx))
655 goto decode_failed;
656
657 /* Next packet in the buffer will have a timestamp on a single byte */
658 mc->rec_ts_idx++;
659
660 /* read data */
661 memset(cf->data, 0x0, sizeof(cf->data));
662 if (status_len & PCAN_USB_STATUSLEN_RTR) {
663 cf->can_id |= CAN_RTR_FLAG;
664 } else {
665 if ((mc->ptr + rec_len) > mc->end)
666 goto decode_failed;
667
668 memcpy(cf->data, mc->ptr, cf->can_dlc);
669 mc->ptr += rec_len;
670 }
671
672 /* convert timestamp into kernel time */
673 hwts = skb_hwtstamps(skb);
674 peak_usb_get_ts_time(&mc->pdev->time_ref, mc->ts16, &hwts->hwtstamp);
675
676 /* update statistics */
677 mc->netdev->stats.rx_packets++;
678 mc->netdev->stats.rx_bytes += cf->can_dlc;
679 /* push the skb */
680 netif_rx(skb);
681
682 return 0;
683
684 decode_failed:
685 dev_kfree_skb(skb);
686 return -EINVAL;
687 }
688
689 /*
690 * process incoming message
691 */
pcan_usb_decode_msg(struct peak_usb_device * dev,u8 * ibuf,u32 lbuf)692 static int pcan_usb_decode_msg(struct peak_usb_device *dev, u8 *ibuf, u32 lbuf)
693 {
694 struct pcan_usb_msg_context mc = {
695 .rec_cnt = ibuf[1],
696 .ptr = ibuf + PCAN_USB_MSG_HEADER_LEN,
697 .end = ibuf + lbuf,
698 .netdev = dev->netdev,
699 .pdev = container_of(dev, struct pcan_usb, dev),
700 };
701 int err;
702
703 for (err = 0; mc.rec_idx < mc.rec_cnt && !err; mc.rec_idx++) {
704 u8 sl = *mc.ptr++;
705
706 /* handle status and error frames here */
707 if (sl & PCAN_USB_STATUSLEN_INTERNAL) {
708 err = pcan_usb_decode_status(&mc, sl);
709 /* handle normal can frames here */
710 } else {
711 err = pcan_usb_decode_data(&mc, sl);
712 }
713 }
714
715 return err;
716 }
717
718 /*
719 * process any incoming buffer
720 */
pcan_usb_decode_buf(struct peak_usb_device * dev,struct urb * urb)721 static int pcan_usb_decode_buf(struct peak_usb_device *dev, struct urb *urb)
722 {
723 int err = 0;
724
725 if (urb->actual_length > PCAN_USB_MSG_HEADER_LEN) {
726 err = pcan_usb_decode_msg(dev, urb->transfer_buffer,
727 urb->actual_length);
728
729 } else if (urb->actual_length > 0) {
730 netdev_err(dev->netdev, "usb message length error (%u)\n",
731 urb->actual_length);
732 err = -EINVAL;
733 }
734
735 return err;
736 }
737
738 /*
739 * process outgoing packet
740 */
pcan_usb_encode_msg(struct peak_usb_device * dev,struct sk_buff * skb,u8 * obuf,size_t * size)741 static int pcan_usb_encode_msg(struct peak_usb_device *dev, struct sk_buff *skb,
742 u8 *obuf, size_t *size)
743 {
744 struct net_device *netdev = dev->netdev;
745 struct net_device_stats *stats = &netdev->stats;
746 struct can_frame *cf = (struct can_frame *)skb->data;
747 u8 *pc;
748
749 obuf[0] = 2;
750 obuf[1] = 1;
751
752 pc = obuf + PCAN_USB_MSG_HEADER_LEN;
753
754 /* status/len byte */
755 *pc = cf->can_dlc;
756 if (cf->can_id & CAN_RTR_FLAG)
757 *pc |= PCAN_USB_STATUSLEN_RTR;
758
759 /* can id */
760 if (cf->can_id & CAN_EFF_FLAG) {
761 __le32 tmp32 = cpu_to_le32((cf->can_id & CAN_ERR_MASK) << 3);
762
763 *pc |= PCAN_USB_STATUSLEN_EXT_ID;
764 memcpy(++pc, &tmp32, 4);
765 pc += 4;
766 } else {
767 __le16 tmp16 = cpu_to_le16((cf->can_id & CAN_ERR_MASK) << 5);
768
769 memcpy(++pc, &tmp16, 2);
770 pc += 2;
771 }
772
773 /* can data */
774 if (!(cf->can_id & CAN_RTR_FLAG)) {
775 memcpy(pc, cf->data, cf->can_dlc);
776 pc += cf->can_dlc;
777 }
778
779 obuf[(*size)-1] = (u8)(stats->tx_packets & 0xff);
780
781 return 0;
782 }
783
784 /*
785 * start interface
786 */
pcan_usb_start(struct peak_usb_device * dev)787 static int pcan_usb_start(struct peak_usb_device *dev)
788 {
789 struct pcan_usb *pdev = container_of(dev, struct pcan_usb, dev);
790
791 /* number of bits used in timestamps read from adapter struct */
792 peak_usb_init_time_ref(&pdev->time_ref, &pcan_usb);
793
794 /* if revision greater than 3, can put silent mode on/off */
795 if (dev->device_rev > 3) {
796 int err;
797
798 err = pcan_usb_set_silent(dev,
799 dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY);
800 if (err)
801 return err;
802 }
803
804 return pcan_usb_set_ext_vcc(dev, 0);
805 }
806
pcan_usb_init(struct peak_usb_device * dev)807 static int pcan_usb_init(struct peak_usb_device *dev)
808 {
809 struct pcan_usb *pdev = container_of(dev, struct pcan_usb, dev);
810 u32 serial_number;
811 int err;
812
813 /* initialize a timer needed to wait for hardware restart */
814 timer_setup(&pdev->restart_timer, pcan_usb_restart, 0);
815
816 /*
817 * explicit use of dev_xxx() instead of netdev_xxx() here:
818 * information displayed are related to the device itself, not
819 * to the canx netdevice.
820 */
821 err = pcan_usb_get_serial(dev, &serial_number);
822 if (err) {
823 dev_err(dev->netdev->dev.parent,
824 "unable to read %s serial number (err %d)\n",
825 pcan_usb.name, err);
826 return err;
827 }
828
829 dev_info(dev->netdev->dev.parent,
830 "PEAK-System %s adapter hwrev %u serial %08X (%u channel)\n",
831 pcan_usb.name, dev->device_rev, serial_number,
832 pcan_usb.ctrl_count);
833
834 return 0;
835 }
836
837 /*
838 * probe function for new PCAN-USB usb interface
839 */
pcan_usb_probe(struct usb_interface * intf)840 static int pcan_usb_probe(struct usb_interface *intf)
841 {
842 struct usb_host_interface *if_desc;
843 int i;
844
845 if_desc = intf->altsetting;
846
847 /* check interface endpoint addresses */
848 for (i = 0; i < if_desc->desc.bNumEndpoints; i++) {
849 struct usb_endpoint_descriptor *ep = &if_desc->endpoint[i].desc;
850
851 switch (ep->bEndpointAddress) {
852 case PCAN_USB_EP_CMDOUT:
853 case PCAN_USB_EP_CMDIN:
854 case PCAN_USB_EP_MSGOUT:
855 case PCAN_USB_EP_MSGIN:
856 break;
857 default:
858 return -ENODEV;
859 }
860 }
861
862 return 0;
863 }
864
865 /*
866 * describe the PCAN-USB adapter
867 */
868 static const struct can_bittiming_const pcan_usb_const = {
869 .name = "pcan_usb",
870 .tseg1_min = 1,
871 .tseg1_max = 16,
872 .tseg2_min = 1,
873 .tseg2_max = 8,
874 .sjw_max = 4,
875 .brp_min = 1,
876 .brp_max = 64,
877 .brp_inc = 1,
878 };
879
880 const struct peak_usb_adapter pcan_usb = {
881 .name = "PCAN-USB",
882 .device_id = PCAN_USB_PRODUCT_ID,
883 .ctrl_count = 1,
884 .ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY,
885 .clock = {
886 .freq = PCAN_USB_CRYSTAL_HZ / 2 ,
887 },
888 .bittiming_const = &pcan_usb_const,
889
890 /* size of device private data */
891 .sizeof_dev_private = sizeof(struct pcan_usb),
892
893 /* timestamps usage */
894 .ts_used_bits = 16,
895 .ts_period = 24575, /* calibration period in ts. */
896 .us_per_ts_scale = PCAN_USB_TS_US_PER_TICK, /* us=(ts*scale) */
897 .us_per_ts_shift = PCAN_USB_TS_DIV_SHIFTER, /* >> shift */
898
899 /* give here messages in/out endpoints */
900 .ep_msg_in = PCAN_USB_EP_MSGIN,
901 .ep_msg_out = {PCAN_USB_EP_MSGOUT},
902
903 /* size of rx/tx usb buffers */
904 .rx_buffer_size = PCAN_USB_RX_BUFFER_SIZE,
905 .tx_buffer_size = PCAN_USB_TX_BUFFER_SIZE,
906
907 /* device callbacks */
908 .intf_probe = pcan_usb_probe,
909 .dev_init = pcan_usb_init,
910 .dev_set_bus = pcan_usb_write_mode,
911 .dev_set_bittiming = pcan_usb_set_bittiming,
912 .dev_get_device_id = pcan_usb_get_device_id,
913 .dev_decode_buf = pcan_usb_decode_buf,
914 .dev_encode_msg = pcan_usb_encode_msg,
915 .dev_start = pcan_usb_start,
916 .dev_restart_async = pcan_usb_restart_async,
917 };
918