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