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