GNU Linux-libre 4.19.304-gnu1
[releases.git] / drivers / net / can / usb / peak_usb / pcan_usb_fd.c
1 /*
2  * CAN driver for PEAK System PCAN-USB FD / PCAN-USB Pro FD adapter
3  *
4  * Copyright (C) 2013-2014 Stephane Grosjean <s.grosjean@peak-system.com>
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published
8  * by the Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  */
15 #include <linux/netdevice.h>
16 #include <linux/usb.h>
17 #include <linux/module.h>
18
19 #include <linux/can.h>
20 #include <linux/can/dev.h>
21 #include <linux/can/error.h>
22 #include <linux/can/dev/peak_canfd.h>
23
24 #include "pcan_usb_core.h"
25 #include "pcan_usb_pro.h"
26
27 MODULE_SUPPORTED_DEVICE("PEAK-System PCAN-USB FD adapter");
28 MODULE_SUPPORTED_DEVICE("PEAK-System PCAN-USB Pro FD adapter");
29
30 #define PCAN_USBPROFD_CHANNEL_COUNT     2
31 #define PCAN_USBFD_CHANNEL_COUNT        1
32
33 /* PCAN-USB Pro FD adapter internal clock (Hz) */
34 #define PCAN_UFD_CRYSTAL_HZ             80000000
35
36 #define PCAN_UFD_CMD_BUFFER_SIZE        512
37 #define PCAN_UFD_LOSPD_PKT_SIZE         64
38
39 /* PCAN-USB Pro FD command timeout (ms.) */
40 #define PCAN_UFD_CMD_TIMEOUT_MS         1000
41
42 /* PCAN-USB Pro FD rx/tx buffers size */
43 #define PCAN_UFD_RX_BUFFER_SIZE         2048
44 #define PCAN_UFD_TX_BUFFER_SIZE         512
45
46 /* read some versions info from the hw devcie */
47 struct __packed pcan_ufd_fw_info {
48         __le16  size_of;        /* sizeof this */
49         __le16  type;           /* type of this structure */
50         u8      hw_type;        /* Type of hardware (HW_TYPE_xxx) */
51         u8      bl_version[3];  /* Bootloader version */
52         u8      hw_version;     /* Hardware version (PCB) */
53         u8      fw_version[3];  /* Firmware version */
54         __le32  dev_id[2];      /* "device id" per CAN */
55         __le32  ser_no;         /* S/N */
56         __le32  flags;          /* special functions */
57 };
58
59 /* handle device specific info used by the netdevices */
60 struct pcan_usb_fd_if {
61         struct peak_usb_device  *dev[PCAN_USB_MAX_CHANNEL];
62         struct pcan_ufd_fw_info fw_info;
63         struct peak_time_ref    time_ref;
64         int                     cm_ignore_count;
65         int                     dev_opened_count;
66 };
67
68 /* device information */
69 struct pcan_usb_fd_device {
70         struct peak_usb_device  dev;
71         struct can_berr_counter bec;
72         struct pcan_usb_fd_if   *usb_if;
73         u8                      *cmd_buffer_addr;
74 };
75
76 /* Extended USB commands (non uCAN commands) */
77
78 /* Clock Modes command */
79 #define PCAN_UFD_CMD_CLK_SET            0x80
80
81 #define PCAN_UFD_CLK_80MHZ              0x0
82 #define PCAN_UFD_CLK_60MHZ              0x1
83 #define PCAN_UFD_CLK_40MHZ              0x2
84 #define PCAN_UFD_CLK_30MHZ              0x3
85 #define PCAN_UFD_CLK_24MHZ              0x4
86 #define PCAN_UFD_CLK_20MHZ              0x5
87 #define PCAN_UFD_CLK_DEF                PCAN_UFD_CLK_80MHZ
88
89 struct __packed pcan_ufd_clock {
90         __le16  opcode_channel;
91
92         u8      mode;
93         u8      unused[5];
94 };
95
96 /* LED control command */
97 #define PCAN_UFD_CMD_LED_SET            0x86
98
99 #define PCAN_UFD_LED_DEV                0x00
100 #define PCAN_UFD_LED_FAST               0x01
101 #define PCAN_UFD_LED_SLOW               0x02
102 #define PCAN_UFD_LED_ON                 0x03
103 #define PCAN_UFD_LED_OFF                0x04
104 #define PCAN_UFD_LED_DEF                PCAN_UFD_LED_DEV
105
106 struct __packed pcan_ufd_led {
107         __le16  opcode_channel;
108
109         u8      mode;
110         u8      unused[5];
111 };
112
113 /* Extended usage of uCAN commands CMD_xxx_xx_OPTION for PCAN-USB Pro FD */
114 #define PCAN_UFD_FLTEXT_CALIBRATION     0x8000
115
116 struct __packed pcan_ufd_options {
117         __le16  opcode_channel;
118
119         __le16  ucan_mask;
120         u16     unused;
121         __le16  usb_mask;
122 };
123
124 /* Extended usage of uCAN messages for PCAN-USB Pro FD */
125 #define PCAN_UFD_MSG_CALIBRATION        0x100
126
127 struct __packed pcan_ufd_ts_msg {
128         __le16  size;
129         __le16  type;
130         __le32  ts_low;
131         __le32  ts_high;
132         __le16  usb_frame_index;
133         u16     unused;
134 };
135
136 #define PCAN_UFD_MSG_OVERRUN            0x101
137
138 #define PCAN_UFD_OVMSG_CHANNEL(o)       ((o)->channel & 0xf)
139
140 struct __packed pcan_ufd_ovr_msg {
141         __le16  size;
142         __le16  type;
143         __le32  ts_low;
144         __le32  ts_high;
145         u8      channel;
146         u8      unused[3];
147 };
148
149 static inline int pufd_omsg_get_channel(struct pcan_ufd_ovr_msg *om)
150 {
151         return om->channel & 0xf;
152 }
153
154 /* Clock mode frequency values */
155 static const u32 pcan_usb_fd_clk_freq[6] = {
156         [PCAN_UFD_CLK_80MHZ] = 80000000,
157         [PCAN_UFD_CLK_60MHZ] = 60000000,
158         [PCAN_UFD_CLK_40MHZ] = 40000000,
159         [PCAN_UFD_CLK_30MHZ] = 30000000,
160         [PCAN_UFD_CLK_24MHZ] = 24000000,
161         [PCAN_UFD_CLK_20MHZ] = 20000000
162 };
163
164 /* return a device USB interface */
165 static inline
166 struct pcan_usb_fd_if *pcan_usb_fd_dev_if(struct peak_usb_device *dev)
167 {
168         struct pcan_usb_fd_device *pdev =
169                         container_of(dev, struct pcan_usb_fd_device, dev);
170         return pdev->usb_if;
171 }
172
173 /* return a device USB commands buffer */
174 static inline void *pcan_usb_fd_cmd_buffer(struct peak_usb_device *dev)
175 {
176         struct pcan_usb_fd_device *pdev =
177                         container_of(dev, struct pcan_usb_fd_device, dev);
178         return pdev->cmd_buffer_addr;
179 }
180
181 /* send PCAN-USB Pro FD commands synchronously */
182 static int pcan_usb_fd_send_cmd(struct peak_usb_device *dev, void *cmd_tail)
183 {
184         void *cmd_head = pcan_usb_fd_cmd_buffer(dev);
185         int err = 0;
186         u8 *packet_ptr;
187         int packet_len;
188         ptrdiff_t cmd_len;
189
190         /* usb device unregistered? */
191         if (!(dev->state & PCAN_USB_STATE_CONNECTED))
192                 return 0;
193
194         /* if a packet is not filled completely by commands, the command list
195          * is terminated with an "end of collection" record.
196          */
197         cmd_len = cmd_tail - cmd_head;
198         if (cmd_len <= (PCAN_UFD_CMD_BUFFER_SIZE - sizeof(u64))) {
199                 memset(cmd_tail, 0xff, sizeof(u64));
200                 cmd_len += sizeof(u64);
201         }
202
203         packet_ptr = cmd_head;
204         packet_len = cmd_len;
205
206         /* firmware is not able to re-assemble 512 bytes buffer in full-speed */
207         if (unlikely(dev->udev->speed != USB_SPEED_HIGH))
208                 packet_len = min(packet_len, PCAN_UFD_LOSPD_PKT_SIZE);
209
210         do {
211                 err = usb_bulk_msg(dev->udev,
212                                    usb_sndbulkpipe(dev->udev,
213                                                    PCAN_USBPRO_EP_CMDOUT),
214                                    packet_ptr, packet_len,
215                                    NULL, PCAN_UFD_CMD_TIMEOUT_MS);
216                 if (err) {
217                         netdev_err(dev->netdev,
218                                    "sending command failure: %d\n", err);
219                         break;
220                 }
221
222                 packet_ptr += packet_len;
223                 cmd_len -= packet_len;
224
225                 if (cmd_len < PCAN_UFD_LOSPD_PKT_SIZE)
226                         packet_len = cmd_len;
227
228         } while (packet_len > 0);
229
230         return err;
231 }
232
233 /* build the commands list in the given buffer, to enter operational mode */
234 static int pcan_usb_fd_build_restart_cmd(struct peak_usb_device *dev, u8 *buf)
235 {
236         struct pucan_wr_err_cnt *prc;
237         struct pucan_command *cmd;
238         u8 *pc = buf;
239
240         /* 1st, reset error counters: */
241         prc = (struct pucan_wr_err_cnt *)pc;
242         prc->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx,
243                                                        PUCAN_CMD_WR_ERR_CNT);
244
245         /* select both counters */
246         prc->sel_mask = cpu_to_le16(PUCAN_WRERRCNT_TE|PUCAN_WRERRCNT_RE);
247
248         /* and reset their values */
249         prc->tx_counter = 0;
250         prc->rx_counter = 0;
251
252         /* moves the pointer forward */
253         pc += sizeof(struct pucan_wr_err_cnt);
254
255         /* add command to switch from ISO to non-ISO mode, if fw allows it */
256         if (dev->can.ctrlmode_supported & CAN_CTRLMODE_FD_NON_ISO) {
257                 struct pucan_options *puo = (struct pucan_options *)pc;
258
259                 puo->opcode_channel =
260                         (dev->can.ctrlmode & CAN_CTRLMODE_FD_NON_ISO) ?
261                         pucan_cmd_opcode_channel(dev->ctrl_idx,
262                                                  PUCAN_CMD_CLR_DIS_OPTION) :
263                         pucan_cmd_opcode_channel(dev->ctrl_idx,
264                                                  PUCAN_CMD_SET_EN_OPTION);
265
266                 puo->options = cpu_to_le16(PUCAN_OPTION_CANDFDISO);
267
268                 /* to be sure that no other extended bits will be taken into
269                  * account
270                  */
271                 puo->unused = 0;
272
273                 /* moves the pointer forward */
274                 pc += sizeof(struct pucan_options);
275         }
276
277         /* next, go back to operational mode */
278         cmd = (struct pucan_command *)pc;
279         cmd->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx,
280                                 (dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) ?
281                                                 PUCAN_CMD_LISTEN_ONLY_MODE :
282                                                 PUCAN_CMD_NORMAL_MODE);
283         pc += sizeof(struct pucan_command);
284
285         return pc - buf;
286 }
287
288 /* set CAN bus on/off */
289 static int pcan_usb_fd_set_bus(struct peak_usb_device *dev, u8 onoff)
290 {
291         u8 *pc = pcan_usb_fd_cmd_buffer(dev);
292         int l;
293
294         if (onoff) {
295                 /* build the cmds list to enter operational mode */
296                 l = pcan_usb_fd_build_restart_cmd(dev, pc);
297         } else {
298                 struct pucan_command *cmd = (struct pucan_command *)pc;
299
300                 /* build cmd to go back to reset mode */
301                 cmd->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx,
302                                                         PUCAN_CMD_RESET_MODE);
303                 l = sizeof(struct pucan_command);
304         }
305
306         /* send the command */
307         return pcan_usb_fd_send_cmd(dev, pc + l);
308 }
309
310 /* set filtering masks:
311  *
312  *      idx  in range [0..63] selects a row #idx, all rows otherwise
313  *      mask in range [0..0xffffffff] defines up to 32 CANIDs in the row(s)
314  *
315  *      Each bit of this 64 x 32 bits array defines a CANID value:
316  *
317  *      bit[i,j] = 1 implies that CANID=(i x 32)+j will be received, while
318  *      bit[i,j] = 0 implies that CANID=(i x 32)+j will be discarded.
319  */
320 static int pcan_usb_fd_set_filter_std(struct peak_usb_device *dev, int idx,
321                                       u32 mask)
322 {
323         struct pucan_filter_std *cmd = pcan_usb_fd_cmd_buffer(dev);
324         int i, n;
325
326         /* select all rows when idx is out of range [0..63] */
327         if ((idx < 0) || (idx >= (1 << PUCAN_FLTSTD_ROW_IDX_BITS))) {
328                 n = 1 << PUCAN_FLTSTD_ROW_IDX_BITS;
329                 idx = 0;
330
331         /* select the row (and only the row) otherwise */
332         } else {
333                 n = idx + 1;
334         }
335
336         for (i = idx; i < n; i++, cmd++) {
337                 cmd->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx,
338                                                         PUCAN_CMD_FILTER_STD);
339                 cmd->idx = cpu_to_le16(i);
340                 cmd->mask = cpu_to_le32(mask);
341         }
342
343         /* send the command */
344         return pcan_usb_fd_send_cmd(dev, cmd);
345 }
346
347 /* set/unset options
348  *
349  *      onoff   set(1)/unset(0) options
350  *      mask    each bit defines a kind of options to set/unset
351  */
352 static int pcan_usb_fd_set_options(struct peak_usb_device *dev,
353                                    bool onoff, u16 ucan_mask, u16 usb_mask)
354 {
355         struct pcan_ufd_options *cmd = pcan_usb_fd_cmd_buffer(dev);
356
357         cmd->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx,
358                                         (onoff) ? PUCAN_CMD_SET_EN_OPTION :
359                                                   PUCAN_CMD_CLR_DIS_OPTION);
360
361         cmd->ucan_mask = cpu_to_le16(ucan_mask);
362         cmd->usb_mask = cpu_to_le16(usb_mask);
363
364         /* send the command */
365         return pcan_usb_fd_send_cmd(dev, ++cmd);
366 }
367
368 /* setup LED control */
369 static int pcan_usb_fd_set_can_led(struct peak_usb_device *dev, u8 led_mode)
370 {
371         struct pcan_ufd_led *cmd = pcan_usb_fd_cmd_buffer(dev);
372
373         cmd->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx,
374                                                        PCAN_UFD_CMD_LED_SET);
375         cmd->mode = led_mode;
376
377         /* send the command */
378         return pcan_usb_fd_send_cmd(dev, ++cmd);
379 }
380
381 /* set CAN clock domain */
382 static int pcan_usb_fd_set_clock_domain(struct peak_usb_device *dev,
383                                         u8 clk_mode)
384 {
385         struct pcan_ufd_clock *cmd = pcan_usb_fd_cmd_buffer(dev);
386
387         cmd->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx,
388                                                        PCAN_UFD_CMD_CLK_SET);
389         cmd->mode = clk_mode;
390
391         /* send the command */
392         return pcan_usb_fd_send_cmd(dev, ++cmd);
393 }
394
395 /* set bittiming for CAN and CAN-FD header */
396 static int pcan_usb_fd_set_bittiming_slow(struct peak_usb_device *dev,
397                                           struct can_bittiming *bt)
398 {
399         struct pucan_timing_slow *cmd = pcan_usb_fd_cmd_buffer(dev);
400
401         cmd->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx,
402                                                        PUCAN_CMD_TIMING_SLOW);
403         cmd->sjw_t = PUCAN_TSLOW_SJW_T(bt->sjw - 1,
404                                 dev->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES);
405
406         cmd->tseg2 = PUCAN_TSLOW_TSEG2(bt->phase_seg2 - 1);
407         cmd->tseg1 = PUCAN_TSLOW_TSEG1(bt->prop_seg + bt->phase_seg1 - 1);
408         cmd->brp = cpu_to_le16(PUCAN_TSLOW_BRP(bt->brp - 1));
409
410         cmd->ewl = 96;  /* default */
411
412         /* send the command */
413         return pcan_usb_fd_send_cmd(dev, ++cmd);
414 }
415
416 /* set CAN-FD bittiming for data */
417 static int pcan_usb_fd_set_bittiming_fast(struct peak_usb_device *dev,
418                                           struct can_bittiming *bt)
419 {
420         struct pucan_timing_fast *cmd = pcan_usb_fd_cmd_buffer(dev);
421
422         cmd->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx,
423                                                        PUCAN_CMD_TIMING_FAST);
424         cmd->sjw = PUCAN_TFAST_SJW(bt->sjw - 1);
425         cmd->tseg2 = PUCAN_TFAST_TSEG2(bt->phase_seg2 - 1);
426         cmd->tseg1 = PUCAN_TFAST_TSEG1(bt->prop_seg + bt->phase_seg1 - 1);
427         cmd->brp = cpu_to_le16(PUCAN_TFAST_BRP(bt->brp - 1));
428
429         /* send the command */
430         return pcan_usb_fd_send_cmd(dev, ++cmd);
431 }
432
433 /* handle restart but in asynchronously way
434  * (uses PCAN-USB Pro code to complete asynchronous request)
435  */
436 static int pcan_usb_fd_restart_async(struct peak_usb_device *dev,
437                                      struct urb *urb, u8 *buf)
438 {
439         u8 *pc = buf;
440
441         /* build the entire cmds list in the provided buffer, to go back into
442          * operational mode.
443          */
444         pc += pcan_usb_fd_build_restart_cmd(dev, pc);
445
446         /* add EOC */
447         memset(pc, 0xff, sizeof(struct pucan_command));
448         pc += sizeof(struct pucan_command);
449
450         /* complete the URB */
451         usb_fill_bulk_urb(urb, dev->udev,
452                           usb_sndbulkpipe(dev->udev, PCAN_USBPRO_EP_CMDOUT),
453                           buf, pc - buf,
454                           pcan_usb_pro_restart_complete, dev);
455
456         /* and submit it. */
457         return usb_submit_urb(urb, GFP_ATOMIC);
458 }
459
460 static int pcan_usb_fd_drv_loaded(struct peak_usb_device *dev, bool loaded)
461 {
462         struct pcan_usb_fd_device *pdev =
463                         container_of(dev, struct pcan_usb_fd_device, dev);
464
465         pdev->cmd_buffer_addr[0] = 0;
466         pdev->cmd_buffer_addr[1] = !!loaded;
467
468         return pcan_usb_pro_send_req(dev,
469                                 PCAN_USBPRO_REQ_FCT,
470                                 PCAN_USBPRO_FCT_DRVLD,
471                                 pdev->cmd_buffer_addr,
472                                 PCAN_USBPRO_FCT_DRVLD_REQ_LEN);
473 }
474
475 static int pcan_usb_fd_decode_canmsg(struct pcan_usb_fd_if *usb_if,
476                                      struct pucan_msg *rx_msg)
477 {
478         struct pucan_rx_msg *rm = (struct pucan_rx_msg *)rx_msg;
479         struct peak_usb_device *dev;
480         struct net_device *netdev;
481         struct canfd_frame *cfd;
482         struct sk_buff *skb;
483         const u16 rx_msg_flags = le16_to_cpu(rm->flags);
484
485         if (pucan_msg_get_channel(rm) >= ARRAY_SIZE(usb_if->dev))
486                 return -ENOMEM;
487
488         dev = usb_if->dev[pucan_msg_get_channel(rm)];
489         netdev = dev->netdev;
490
491         if (rx_msg_flags & PUCAN_MSG_EXT_DATA_LEN) {
492                 /* CANFD frame case */
493                 skb = alloc_canfd_skb(netdev, &cfd);
494                 if (!skb)
495                         return -ENOMEM;
496
497                 if (rx_msg_flags & PUCAN_MSG_BITRATE_SWITCH)
498                         cfd->flags |= CANFD_BRS;
499
500                 if (rx_msg_flags & PUCAN_MSG_ERROR_STATE_IND)
501                         cfd->flags |= CANFD_ESI;
502
503                 cfd->len = can_dlc2len(get_canfd_dlc(pucan_msg_get_dlc(rm)));
504         } else {
505                 /* CAN 2.0 frame case */
506                 skb = alloc_can_skb(netdev, (struct can_frame **)&cfd);
507                 if (!skb)
508                         return -ENOMEM;
509
510                 cfd->len = get_can_dlc(pucan_msg_get_dlc(rm));
511         }
512
513         cfd->can_id = le32_to_cpu(rm->can_id);
514
515         if (rx_msg_flags & PUCAN_MSG_EXT_ID)
516                 cfd->can_id |= CAN_EFF_FLAG;
517
518         if (rx_msg_flags & PUCAN_MSG_RTR)
519                 cfd->can_id |= CAN_RTR_FLAG;
520         else
521                 memcpy(cfd->data, rm->d, cfd->len);
522
523         netdev->stats.rx_packets++;
524         netdev->stats.rx_bytes += cfd->len;
525
526         peak_usb_netif_rx(skb, &usb_if->time_ref, le32_to_cpu(rm->ts_low));
527
528         return 0;
529 }
530
531 /* handle uCAN status message */
532 static int pcan_usb_fd_decode_status(struct pcan_usb_fd_if *usb_if,
533                                      struct pucan_msg *rx_msg)
534 {
535         struct pucan_status_msg *sm = (struct pucan_status_msg *)rx_msg;
536         struct pcan_usb_fd_device *pdev;
537         enum can_state new_state = CAN_STATE_ERROR_ACTIVE;
538         enum can_state rx_state, tx_state;
539         struct peak_usb_device *dev;
540         struct net_device *netdev;
541         struct can_frame *cf;
542         struct sk_buff *skb;
543
544         if (pucan_stmsg_get_channel(sm) >= ARRAY_SIZE(usb_if->dev))
545                 return -ENOMEM;
546
547         dev = usb_if->dev[pucan_stmsg_get_channel(sm)];
548         pdev = container_of(dev, struct pcan_usb_fd_device, dev);
549         netdev = dev->netdev;
550
551         /* nothing should be sent while in BUS_OFF state */
552         if (dev->can.state == CAN_STATE_BUS_OFF)
553                 return 0;
554
555         if (sm->channel_p_w_b & PUCAN_BUS_BUSOFF) {
556                 new_state = CAN_STATE_BUS_OFF;
557         } else if (sm->channel_p_w_b & PUCAN_BUS_PASSIVE) {
558                 new_state = CAN_STATE_ERROR_PASSIVE;
559         } else if (sm->channel_p_w_b & PUCAN_BUS_WARNING) {
560                 new_state = CAN_STATE_ERROR_WARNING;
561         } else {
562                 /* back to (or still in) ERROR_ACTIVE state */
563                 new_state = CAN_STATE_ERROR_ACTIVE;
564                 pdev->bec.txerr = 0;
565                 pdev->bec.rxerr = 0;
566         }
567
568         /* state hasn't changed */
569         if (new_state == dev->can.state)
570                 return 0;
571
572         /* handle bus state change */
573         tx_state = (pdev->bec.txerr >= pdev->bec.rxerr) ? new_state : 0;
574         rx_state = (pdev->bec.txerr <= pdev->bec.rxerr) ? new_state : 0;
575
576         /* allocate an skb to store the error frame */
577         skb = alloc_can_err_skb(netdev, &cf);
578         if (skb)
579                 can_change_state(netdev, cf, tx_state, rx_state);
580
581         /* things must be done even in case of OOM */
582         if (new_state == CAN_STATE_BUS_OFF)
583                 can_bus_off(netdev);
584
585         if (!skb)
586                 return -ENOMEM;
587
588         netdev->stats.rx_packets++;
589         netdev->stats.rx_bytes += cf->can_dlc;
590
591         peak_usb_netif_rx(skb, &usb_if->time_ref, le32_to_cpu(sm->ts_low));
592
593         return 0;
594 }
595
596 /* handle uCAN error message */
597 static int pcan_usb_fd_decode_error(struct pcan_usb_fd_if *usb_if,
598                                     struct pucan_msg *rx_msg)
599 {
600         struct pucan_error_msg *er = (struct pucan_error_msg *)rx_msg;
601         struct pcan_usb_fd_device *pdev;
602         struct peak_usb_device *dev;
603
604         if (pucan_ermsg_get_channel(er) >= ARRAY_SIZE(usb_if->dev))
605                 return -EINVAL;
606
607         dev = usb_if->dev[pucan_ermsg_get_channel(er)];
608         pdev = container_of(dev, struct pcan_usb_fd_device, dev);
609
610         /* keep a trace of tx and rx error counters for later use */
611         pdev->bec.txerr = er->tx_err_cnt;
612         pdev->bec.rxerr = er->rx_err_cnt;
613
614         return 0;
615 }
616
617 /* handle uCAN overrun message */
618 static int pcan_usb_fd_decode_overrun(struct pcan_usb_fd_if *usb_if,
619                                       struct pucan_msg *rx_msg)
620 {
621         struct pcan_ufd_ovr_msg *ov = (struct pcan_ufd_ovr_msg *)rx_msg;
622         struct peak_usb_device *dev;
623         struct net_device *netdev;
624         struct can_frame *cf;
625         struct sk_buff *skb;
626
627         if (pufd_omsg_get_channel(ov) >= ARRAY_SIZE(usb_if->dev))
628                 return -EINVAL;
629
630         dev = usb_if->dev[pufd_omsg_get_channel(ov)];
631         netdev = dev->netdev;
632
633         /* allocate an skb to store the error frame */
634         skb = alloc_can_err_skb(netdev, &cf);
635         if (!skb)
636                 return -ENOMEM;
637
638         cf->can_id |= CAN_ERR_CRTL;
639         cf->data[1] |= CAN_ERR_CRTL_RX_OVERFLOW;
640
641         peak_usb_netif_rx(skb, &usb_if->time_ref, le32_to_cpu(ov->ts_low));
642
643         netdev->stats.rx_over_errors++;
644         netdev->stats.rx_errors++;
645
646         return 0;
647 }
648
649 /* handle USB calibration message */
650 static void pcan_usb_fd_decode_ts(struct pcan_usb_fd_if *usb_if,
651                                   struct pucan_msg *rx_msg)
652 {
653         struct pcan_ufd_ts_msg *ts = (struct pcan_ufd_ts_msg *)rx_msg;
654
655         /* should wait until clock is stabilized */
656         if (usb_if->cm_ignore_count > 0)
657                 usb_if->cm_ignore_count--;
658         else
659                 peak_usb_set_ts_now(&usb_if->time_ref, le32_to_cpu(ts->ts_low));
660 }
661
662 /* callback for bulk IN urb */
663 static int pcan_usb_fd_decode_buf(struct peak_usb_device *dev, struct urb *urb)
664 {
665         struct pcan_usb_fd_if *usb_if = pcan_usb_fd_dev_if(dev);
666         struct net_device *netdev = dev->netdev;
667         struct pucan_msg *rx_msg;
668         u8 *msg_ptr, *msg_end;
669         int err = 0;
670
671         /* loop reading all the records from the incoming message */
672         msg_ptr = urb->transfer_buffer;
673         msg_end = urb->transfer_buffer + urb->actual_length;
674         for (; msg_ptr < msg_end;) {
675                 u16 rx_msg_type, rx_msg_size;
676
677                 rx_msg = (struct pucan_msg *)msg_ptr;
678                 if (!rx_msg->size) {
679                         /* null packet found: end of list */
680                         break;
681                 }
682
683                 rx_msg_size = le16_to_cpu(rx_msg->size);
684                 rx_msg_type = le16_to_cpu(rx_msg->type);
685
686                 /* check if the record goes out of current packet */
687                 if (msg_ptr + rx_msg_size > msg_end) {
688                         netdev_err(netdev,
689                                    "got frag rec: should inc usb rx buf sze\n");
690                         err = -EBADMSG;
691                         break;
692                 }
693
694                 switch (rx_msg_type) {
695                 case PUCAN_MSG_CAN_RX:
696                         err = pcan_usb_fd_decode_canmsg(usb_if, rx_msg);
697                         if (err < 0)
698                                 goto fail;
699                         break;
700
701                 case PCAN_UFD_MSG_CALIBRATION:
702                         pcan_usb_fd_decode_ts(usb_if, rx_msg);
703                         break;
704
705                 case PUCAN_MSG_ERROR:
706                         err = pcan_usb_fd_decode_error(usb_if, rx_msg);
707                         if (err < 0)
708                                 goto fail;
709                         break;
710
711                 case PUCAN_MSG_STATUS:
712                         err = pcan_usb_fd_decode_status(usb_if, rx_msg);
713                         if (err < 0)
714                                 goto fail;
715                         break;
716
717                 case PCAN_UFD_MSG_OVERRUN:
718                         err = pcan_usb_fd_decode_overrun(usb_if, rx_msg);
719                         if (err < 0)
720                                 goto fail;
721                         break;
722
723                 default:
724                         netdev_err(netdev,
725                                    "unhandled msg type 0x%02x (%d): ignored\n",
726                                    rx_msg_type, rx_msg_type);
727                         break;
728                 }
729
730                 msg_ptr += rx_msg_size;
731         }
732
733 fail:
734         if (err)
735                 pcan_dump_mem("received msg",
736                               urb->transfer_buffer, urb->actual_length);
737         return err;
738 }
739
740 /* CAN/CANFD frames encoding callback */
741 static int pcan_usb_fd_encode_msg(struct peak_usb_device *dev,
742                                   struct sk_buff *skb, u8 *obuf, size_t *size)
743 {
744         struct pucan_tx_msg *tx_msg = (struct pucan_tx_msg *)obuf;
745         struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
746         u16 tx_msg_size, tx_msg_flags;
747         u8 can_dlc;
748
749         if (cfd->len > CANFD_MAX_DLEN)
750                 return -EINVAL;
751
752         tx_msg_size = ALIGN(sizeof(struct pucan_tx_msg) + cfd->len, 4);
753         tx_msg->size = cpu_to_le16(tx_msg_size);
754         tx_msg->type = cpu_to_le16(PUCAN_MSG_CAN_TX);
755
756         tx_msg_flags = 0;
757         if (cfd->can_id & CAN_EFF_FLAG) {
758                 tx_msg_flags |= PUCAN_MSG_EXT_ID;
759                 tx_msg->can_id = cpu_to_le32(cfd->can_id & CAN_EFF_MASK);
760         } else {
761                 tx_msg->can_id = cpu_to_le32(cfd->can_id & CAN_SFF_MASK);
762         }
763
764         if (can_is_canfd_skb(skb)) {
765                 /* considering a CANFD frame */
766                 can_dlc = can_len2dlc(cfd->len);
767
768                 tx_msg_flags |= PUCAN_MSG_EXT_DATA_LEN;
769
770                 if (cfd->flags & CANFD_BRS)
771                         tx_msg_flags |= PUCAN_MSG_BITRATE_SWITCH;
772
773                 if (cfd->flags & CANFD_ESI)
774                         tx_msg_flags |= PUCAN_MSG_ERROR_STATE_IND;
775         } else {
776                 /* CAND 2.0 frames */
777                 can_dlc = cfd->len;
778
779                 if (cfd->can_id & CAN_RTR_FLAG)
780                         tx_msg_flags |= PUCAN_MSG_RTR;
781         }
782
783         tx_msg->flags = cpu_to_le16(tx_msg_flags);
784         tx_msg->channel_dlc = PUCAN_MSG_CHANNEL_DLC(dev->ctrl_idx, can_dlc);
785         memcpy(tx_msg->d, cfd->data, cfd->len);
786
787         /* add null size message to tag the end (messages are 32-bits aligned)
788          */
789         tx_msg = (struct pucan_tx_msg *)(obuf + tx_msg_size);
790
791         tx_msg->size = 0;
792
793         /* set the whole size of the USB packet to send */
794         *size = tx_msg_size + sizeof(u32);
795
796         return 0;
797 }
798
799 /* start the interface (last chance before set bus on) */
800 static int pcan_usb_fd_start(struct peak_usb_device *dev)
801 {
802         struct pcan_usb_fd_device *pdev =
803                         container_of(dev, struct pcan_usb_fd_device, dev);
804         int err;
805
806         /* set filter mode: all acceptance */
807         err = pcan_usb_fd_set_filter_std(dev, -1, 0xffffffff);
808         if (err)
809                 return err;
810
811         /* opening first device: */
812         if (pdev->usb_if->dev_opened_count == 0) {
813                 /* reset time_ref */
814                 peak_usb_init_time_ref(&pdev->usb_if->time_ref,
815                                        &pcan_usb_pro_fd);
816
817                 /* enable USB calibration messages */
818                 err = pcan_usb_fd_set_options(dev, 1,
819                                               PUCAN_OPTION_ERROR,
820                                               PCAN_UFD_FLTEXT_CALIBRATION);
821         }
822
823         pdev->usb_if->dev_opened_count++;
824
825         /* reset cached error counters */
826         pdev->bec.txerr = 0;
827         pdev->bec.rxerr = 0;
828
829         return err;
830 }
831
832 /* socket callback used to copy berr counters values receieved through USB */
833 static int pcan_usb_fd_get_berr_counter(const struct net_device *netdev,
834                                         struct can_berr_counter *bec)
835 {
836         struct peak_usb_device *dev = netdev_priv(netdev);
837         struct pcan_usb_fd_device *pdev =
838                         container_of(dev, struct pcan_usb_fd_device, dev);
839
840         *bec = pdev->bec;
841
842         /* must return 0 */
843         return 0;
844 }
845
846 /* stop interface (last chance before set bus off) */
847 static int pcan_usb_fd_stop(struct peak_usb_device *dev)
848 {
849         struct pcan_usb_fd_device *pdev =
850                         container_of(dev, struct pcan_usb_fd_device, dev);
851
852         /* turn off special msgs for that interface if no other dev opened */
853         if (pdev->usb_if->dev_opened_count == 1)
854                 pcan_usb_fd_set_options(dev, 0,
855                                         PUCAN_OPTION_ERROR,
856                                         PCAN_UFD_FLTEXT_CALIBRATION);
857         pdev->usb_if->dev_opened_count--;
858
859         return 0;
860 }
861
862 /* called when probing, to initialize a device object */
863 static int pcan_usb_fd_init(struct peak_usb_device *dev)
864 {
865         struct pcan_usb_fd_device *pdev =
866                         container_of(dev, struct pcan_usb_fd_device, dev);
867         int i, err = -ENOMEM;
868
869         /* do this for 1st channel only */
870         if (!dev->prev_siblings) {
871                 /* allocate netdevices common structure attached to first one */
872                 pdev->usb_if = kzalloc(sizeof(*pdev->usb_if), GFP_KERNEL);
873                 if (!pdev->usb_if)
874                         goto err_out;
875
876                 /* allocate command buffer once for all for the interface */
877                 pdev->cmd_buffer_addr = kzalloc(PCAN_UFD_CMD_BUFFER_SIZE,
878                                                 GFP_KERNEL);
879                 if (!pdev->cmd_buffer_addr)
880                         goto err_out_1;
881
882                 /* number of ts msgs to ignore before taking one into account */
883                 pdev->usb_if->cm_ignore_count = 5;
884
885                 err = pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_INFO,
886                                             PCAN_USBPRO_INFO_FW,
887                                             &pdev->usb_if->fw_info,
888                                             sizeof(pdev->usb_if->fw_info));
889                 if (err) {
890                         dev_err(dev->netdev->dev.parent,
891                                 "unable to read %s firmware info (err %d)\n",
892                                 dev->adapter->name, err);
893                         goto err_out_2;
894                 }
895
896                 /* explicit use of dev_xxx() instead of netdev_xxx() here:
897                  * information displayed are related to the device itself, not
898                  * to the canx (channel) device.
899                  */
900                 dev_info(dev->netdev->dev.parent,
901                          "PEAK-System %s v%u fw v%u.%u.%u (%u channels)\n",
902                          dev->adapter->name, pdev->usb_if->fw_info.hw_version,
903                          pdev->usb_if->fw_info.fw_version[0],
904                          pdev->usb_if->fw_info.fw_version[1],
905                          pdev->usb_if->fw_info.fw_version[2],
906                          dev->adapter->ctrl_count);
907
908                 /* check for ability to switch between ISO/non-ISO modes */
909                 if (pdev->usb_if->fw_info.fw_version[0] >= 2) {
910                         /* firmware >= 2.x supports ISO/non-ISO switching */
911                         dev->can.ctrlmode_supported |= CAN_CTRLMODE_FD_NON_ISO;
912                 } else {
913                         /* firmware < 2.x only supports fixed(!) non-ISO */
914                         dev->can.ctrlmode |= CAN_CTRLMODE_FD_NON_ISO;
915                 }
916
917                 /* tell the hardware the can driver is running */
918                 err = pcan_usb_fd_drv_loaded(dev, 1);
919                 if (err) {
920                         dev_err(dev->netdev->dev.parent,
921                                 "unable to tell %s driver is loaded (err %d)\n",
922                                 dev->adapter->name, err);
923                         goto err_out_2;
924                 }
925         } else {
926                 /* otherwise, simply copy previous sibling's values */
927                 struct pcan_usb_fd_device *ppdev =
928                         container_of(dev->prev_siblings,
929                                      struct pcan_usb_fd_device, dev);
930
931                 pdev->usb_if = ppdev->usb_if;
932                 pdev->cmd_buffer_addr = ppdev->cmd_buffer_addr;
933
934                 /* do a copy of the ctrlmode[_supported] too */
935                 dev->can.ctrlmode = ppdev->dev.can.ctrlmode;
936                 dev->can.ctrlmode_supported = ppdev->dev.can.ctrlmode_supported;
937         }
938
939         pdev->usb_if->dev[dev->ctrl_idx] = dev;
940         dev->device_number =
941                 le32_to_cpu(pdev->usb_if->fw_info.dev_id[dev->ctrl_idx]);
942
943         /* set clock domain */
944         for (i = 0; i < ARRAY_SIZE(pcan_usb_fd_clk_freq); i++)
945                 if (dev->adapter->clock.freq == pcan_usb_fd_clk_freq[i])
946                         break;
947
948         if (i >= ARRAY_SIZE(pcan_usb_fd_clk_freq)) {
949                 dev_warn(dev->netdev->dev.parent,
950                          "incompatible clock frequencies\n");
951                 err = -EINVAL;
952                 goto err_out_2;
953         }
954
955         pcan_usb_fd_set_clock_domain(dev, i);
956
957         /* set LED in default state (end of init phase) */
958         pcan_usb_fd_set_can_led(dev, PCAN_UFD_LED_DEF);
959
960         return 0;
961
962 err_out_2:
963         kfree(pdev->cmd_buffer_addr);
964 err_out_1:
965         kfree(pdev->usb_if);
966 err_out:
967         return err;
968 }
969
970 /* called when driver module is being unloaded */
971 static void pcan_usb_fd_exit(struct peak_usb_device *dev)
972 {
973         struct pcan_usb_fd_device *pdev =
974                         container_of(dev, struct pcan_usb_fd_device, dev);
975
976         /* when rmmod called before unplug and if down, should reset things
977          * before leaving
978          */
979         if (dev->can.state != CAN_STATE_STOPPED) {
980                 /* set bus off on the corresponding channel */
981                 pcan_usb_fd_set_bus(dev, 0);
982         }
983
984         /* switch off corresponding CAN LEDs */
985         pcan_usb_fd_set_can_led(dev, PCAN_UFD_LED_OFF);
986
987         /* if channel #0 (only) */
988         if (dev->ctrl_idx == 0) {
989                 /* turn off calibration message if any device were opened */
990                 if (pdev->usb_if->dev_opened_count > 0)
991                         pcan_usb_fd_set_options(dev, 0,
992                                                 PUCAN_OPTION_ERROR,
993                                                 PCAN_UFD_FLTEXT_CALIBRATION);
994
995                 /* tell USB adapter that the driver is being unloaded */
996                 pcan_usb_fd_drv_loaded(dev, 0);
997         }
998 }
999
1000 /* called when the USB adapter is unplugged */
1001 static void pcan_usb_fd_free(struct peak_usb_device *dev)
1002 {
1003         /* last device: can free shared objects now */
1004         if (!dev->prev_siblings && !dev->next_siblings) {
1005                 struct pcan_usb_fd_device *pdev =
1006                         container_of(dev, struct pcan_usb_fd_device, dev);
1007
1008                 /* free commands buffer */
1009                 kfree(pdev->cmd_buffer_addr);
1010
1011                 /* free usb interface object */
1012                 kfree(pdev->usb_if);
1013         }
1014 }
1015
1016 /* describes the PCAN-USB FD adapter */
1017 static const struct can_bittiming_const pcan_usb_fd_const = {
1018         .name = "pcan_usb_fd",
1019         .tseg1_min = 1,
1020         .tseg1_max = (1 << PUCAN_TSLOW_TSGEG1_BITS),
1021         .tseg2_min = 1,
1022         .tseg2_max = (1 << PUCAN_TSLOW_TSGEG2_BITS),
1023         .sjw_max = (1 << PUCAN_TSLOW_SJW_BITS),
1024         .brp_min = 1,
1025         .brp_max = (1 << PUCAN_TSLOW_BRP_BITS),
1026         .brp_inc = 1,
1027 };
1028
1029 static const struct can_bittiming_const pcan_usb_fd_data_const = {
1030         .name = "pcan_usb_fd",
1031         .tseg1_min = 1,
1032         .tseg1_max = (1 << PUCAN_TFAST_TSGEG1_BITS),
1033         .tseg2_min = 1,
1034         .tseg2_max = (1 << PUCAN_TFAST_TSGEG2_BITS),
1035         .sjw_max = (1 << PUCAN_TFAST_SJW_BITS),
1036         .brp_min = 1,
1037         .brp_max = (1 << PUCAN_TFAST_BRP_BITS),
1038         .brp_inc = 1,
1039 };
1040
1041 const struct peak_usb_adapter pcan_usb_fd = {
1042         .name = "PCAN-USB FD",
1043         .device_id = PCAN_USBFD_PRODUCT_ID,
1044         .ctrl_count = PCAN_USBFD_CHANNEL_COUNT,
1045         .ctrlmode_supported = CAN_CTRLMODE_FD |
1046                         CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY,
1047         .clock = {
1048                 .freq = PCAN_UFD_CRYSTAL_HZ,
1049         },
1050         .bittiming_const = &pcan_usb_fd_const,
1051         .data_bittiming_const = &pcan_usb_fd_data_const,
1052
1053         /* size of device private data */
1054         .sizeof_dev_private = sizeof(struct pcan_usb_fd_device),
1055
1056         /* timestamps usage */
1057         .ts_used_bits = 32,
1058         .ts_period = 1000000, /* calibration period in ts. */
1059         .us_per_ts_scale = 1, /* us = (ts * scale) >> shift */
1060         .us_per_ts_shift = 0,
1061
1062         /* give here messages in/out endpoints */
1063         .ep_msg_in = PCAN_USBPRO_EP_MSGIN,
1064         .ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0},
1065
1066         /* size of rx/tx usb buffers */
1067         .rx_buffer_size = PCAN_UFD_RX_BUFFER_SIZE,
1068         .tx_buffer_size = PCAN_UFD_TX_BUFFER_SIZE,
1069
1070         /* device callbacks */
1071         .intf_probe = pcan_usb_pro_probe,       /* same as PCAN-USB Pro */
1072         .dev_init = pcan_usb_fd_init,
1073
1074         .dev_exit = pcan_usb_fd_exit,
1075         .dev_free = pcan_usb_fd_free,
1076         .dev_set_bus = pcan_usb_fd_set_bus,
1077         .dev_set_bittiming = pcan_usb_fd_set_bittiming_slow,
1078         .dev_set_data_bittiming = pcan_usb_fd_set_bittiming_fast,
1079         .dev_decode_buf = pcan_usb_fd_decode_buf,
1080         .dev_start = pcan_usb_fd_start,
1081         .dev_stop = pcan_usb_fd_stop,
1082         .dev_restart_async = pcan_usb_fd_restart_async,
1083         .dev_encode_msg = pcan_usb_fd_encode_msg,
1084
1085         .do_get_berr_counter = pcan_usb_fd_get_berr_counter,
1086 };
1087
1088 /* describes the PCAN-CHIP USB */
1089 static const struct can_bittiming_const pcan_usb_chip_const = {
1090         .name = "pcan_chip_usb",
1091         .tseg1_min = 1,
1092         .tseg1_max = (1 << PUCAN_TSLOW_TSGEG1_BITS),
1093         .tseg2_min = 1,
1094         .tseg2_max = (1 << PUCAN_TSLOW_TSGEG2_BITS),
1095         .sjw_max = (1 << PUCAN_TSLOW_SJW_BITS),
1096         .brp_min = 1,
1097         .brp_max = (1 << PUCAN_TSLOW_BRP_BITS),
1098         .brp_inc = 1,
1099 };
1100
1101 static const struct can_bittiming_const pcan_usb_chip_data_const = {
1102         .name = "pcan_chip_usb",
1103         .tseg1_min = 1,
1104         .tseg1_max = (1 << PUCAN_TFAST_TSGEG1_BITS),
1105         .tseg2_min = 1,
1106         .tseg2_max = (1 << PUCAN_TFAST_TSGEG2_BITS),
1107         .sjw_max = (1 << PUCAN_TFAST_SJW_BITS),
1108         .brp_min = 1,
1109         .brp_max = (1 << PUCAN_TFAST_BRP_BITS),
1110         .brp_inc = 1,
1111 };
1112
1113 const struct peak_usb_adapter pcan_usb_chip = {
1114         .name = "PCAN-Chip USB",
1115         .device_id = PCAN_USBCHIP_PRODUCT_ID,
1116         .ctrl_count = PCAN_USBFD_CHANNEL_COUNT,
1117         .ctrlmode_supported = CAN_CTRLMODE_FD |
1118                 CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY,
1119         .clock = {
1120                 .freq = PCAN_UFD_CRYSTAL_HZ,
1121         },
1122         .bittiming_const = &pcan_usb_chip_const,
1123         .data_bittiming_const = &pcan_usb_chip_data_const,
1124
1125         /* size of device private data */
1126         .sizeof_dev_private = sizeof(struct pcan_usb_fd_device),
1127
1128         /* timestamps usage */
1129         .ts_used_bits = 32,
1130         .ts_period = 1000000, /* calibration period in ts. */
1131         .us_per_ts_scale = 1, /* us = (ts * scale) >> shift */
1132         .us_per_ts_shift = 0,
1133
1134         /* give here messages in/out endpoints */
1135         .ep_msg_in = PCAN_USBPRO_EP_MSGIN,
1136         .ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0},
1137
1138         /* size of rx/tx usb buffers */
1139         .rx_buffer_size = PCAN_UFD_RX_BUFFER_SIZE,
1140         .tx_buffer_size = PCAN_UFD_TX_BUFFER_SIZE,
1141
1142         /* device callbacks */
1143         .intf_probe = pcan_usb_pro_probe,       /* same as PCAN-USB Pro */
1144         .dev_init = pcan_usb_fd_init,
1145
1146         .dev_exit = pcan_usb_fd_exit,
1147         .dev_free = pcan_usb_fd_free,
1148         .dev_set_bus = pcan_usb_fd_set_bus,
1149         .dev_set_bittiming = pcan_usb_fd_set_bittiming_slow,
1150         .dev_set_data_bittiming = pcan_usb_fd_set_bittiming_fast,
1151         .dev_decode_buf = pcan_usb_fd_decode_buf,
1152         .dev_start = pcan_usb_fd_start,
1153         .dev_stop = pcan_usb_fd_stop,
1154         .dev_restart_async = pcan_usb_fd_restart_async,
1155         .dev_encode_msg = pcan_usb_fd_encode_msg,
1156
1157         .do_get_berr_counter = pcan_usb_fd_get_berr_counter,
1158 };
1159
1160 /* describes the PCAN-USB Pro FD adapter */
1161 static const struct can_bittiming_const pcan_usb_pro_fd_const = {
1162         .name = "pcan_usb_pro_fd",
1163         .tseg1_min = 1,
1164         .tseg1_max = (1 << PUCAN_TSLOW_TSGEG1_BITS),
1165         .tseg2_min = 1,
1166         .tseg2_max = (1 << PUCAN_TSLOW_TSGEG2_BITS),
1167         .sjw_max = (1 << PUCAN_TSLOW_SJW_BITS),
1168         .brp_min = 1,
1169         .brp_max = (1 << PUCAN_TSLOW_BRP_BITS),
1170         .brp_inc = 1,
1171 };
1172
1173 static const struct can_bittiming_const pcan_usb_pro_fd_data_const = {
1174         .name = "pcan_usb_pro_fd",
1175         .tseg1_min = 1,
1176         .tseg1_max = (1 << PUCAN_TFAST_TSGEG1_BITS),
1177         .tseg2_min = 1,
1178         .tseg2_max = (1 << PUCAN_TFAST_TSGEG2_BITS),
1179         .sjw_max = (1 << PUCAN_TFAST_SJW_BITS),
1180         .brp_min = 1,
1181         .brp_max = (1 << PUCAN_TFAST_BRP_BITS),
1182         .brp_inc = 1,
1183 };
1184
1185 const struct peak_usb_adapter pcan_usb_pro_fd = {
1186         .name = "PCAN-USB Pro FD",
1187         .device_id = PCAN_USBPROFD_PRODUCT_ID,
1188         .ctrl_count = PCAN_USBPROFD_CHANNEL_COUNT,
1189         .ctrlmode_supported = CAN_CTRLMODE_FD |
1190                         CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY,
1191         .clock = {
1192                 .freq = PCAN_UFD_CRYSTAL_HZ,
1193         },
1194         .bittiming_const = &pcan_usb_pro_fd_const,
1195         .data_bittiming_const = &pcan_usb_pro_fd_data_const,
1196
1197         /* size of device private data */
1198         .sizeof_dev_private = sizeof(struct pcan_usb_fd_device),
1199
1200         /* timestamps usage */
1201         .ts_used_bits = 32,
1202         .ts_period = 1000000, /* calibration period in ts. */
1203         .us_per_ts_scale = 1, /* us = (ts * scale) >> shift */
1204         .us_per_ts_shift = 0,
1205
1206         /* give here messages in/out endpoints */
1207         .ep_msg_in = PCAN_USBPRO_EP_MSGIN,
1208         .ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0, PCAN_USBPRO_EP_MSGOUT_1},
1209
1210         /* size of rx/tx usb buffers */
1211         .rx_buffer_size = PCAN_UFD_RX_BUFFER_SIZE,
1212         .tx_buffer_size = PCAN_UFD_TX_BUFFER_SIZE,
1213
1214         /* device callbacks */
1215         .intf_probe = pcan_usb_pro_probe,       /* same as PCAN-USB Pro */
1216         .dev_init = pcan_usb_fd_init,
1217
1218         .dev_exit = pcan_usb_fd_exit,
1219         .dev_free = pcan_usb_fd_free,
1220         .dev_set_bus = pcan_usb_fd_set_bus,
1221         .dev_set_bittiming = pcan_usb_fd_set_bittiming_slow,
1222         .dev_set_data_bittiming = pcan_usb_fd_set_bittiming_fast,
1223         .dev_decode_buf = pcan_usb_fd_decode_buf,
1224         .dev_start = pcan_usb_fd_start,
1225         .dev_stop = pcan_usb_fd_stop,
1226         .dev_restart_async = pcan_usb_fd_restart_async,
1227         .dev_encode_msg = pcan_usb_fd_encode_msg,
1228
1229         .do_get_berr_counter = pcan_usb_fd_get_berr_counter,
1230 };
1231
1232 /* describes the PCAN-USB X6 adapter */
1233 static const struct can_bittiming_const pcan_usb_x6_const = {
1234         .name = "pcan_usb_x6",
1235         .tseg1_min = 1,
1236         .tseg1_max = (1 << PUCAN_TSLOW_TSGEG1_BITS),
1237         .tseg2_min = 1,
1238         .tseg2_max = (1 << PUCAN_TSLOW_TSGEG2_BITS),
1239         .sjw_max = (1 << PUCAN_TSLOW_SJW_BITS),
1240         .brp_min = 1,
1241         .brp_max = (1 << PUCAN_TSLOW_BRP_BITS),
1242         .brp_inc = 1,
1243 };
1244
1245 static const struct can_bittiming_const pcan_usb_x6_data_const = {
1246         .name = "pcan_usb_x6",
1247         .tseg1_min = 1,
1248         .tseg1_max = (1 << PUCAN_TFAST_TSGEG1_BITS),
1249         .tseg2_min = 1,
1250         .tseg2_max = (1 << PUCAN_TFAST_TSGEG2_BITS),
1251         .sjw_max = (1 << PUCAN_TFAST_SJW_BITS),
1252         .brp_min = 1,
1253         .brp_max = (1 << PUCAN_TFAST_BRP_BITS),
1254         .brp_inc = 1,
1255 };
1256
1257 const struct peak_usb_adapter pcan_usb_x6 = {
1258         .name = "PCAN-USB X6",
1259         .device_id = PCAN_USBX6_PRODUCT_ID,
1260         .ctrl_count = PCAN_USBPROFD_CHANNEL_COUNT,
1261         .ctrlmode_supported = CAN_CTRLMODE_FD |
1262                         CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY,
1263         .clock = {
1264                 .freq = PCAN_UFD_CRYSTAL_HZ,
1265         },
1266         .bittiming_const = &pcan_usb_x6_const,
1267         .data_bittiming_const = &pcan_usb_x6_data_const,
1268
1269         /* size of device private data */
1270         .sizeof_dev_private = sizeof(struct pcan_usb_fd_device),
1271
1272         /* timestamps usage */
1273         .ts_used_bits = 32,
1274         .ts_period = 1000000, /* calibration period in ts. */
1275         .us_per_ts_scale = 1, /* us = (ts * scale) >> shift */
1276         .us_per_ts_shift = 0,
1277
1278         /* give here messages in/out endpoints */
1279         .ep_msg_in = PCAN_USBPRO_EP_MSGIN,
1280         .ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0, PCAN_USBPRO_EP_MSGOUT_1},
1281
1282         /* size of rx/tx usb buffers */
1283         .rx_buffer_size = PCAN_UFD_RX_BUFFER_SIZE,
1284         .tx_buffer_size = PCAN_UFD_TX_BUFFER_SIZE,
1285
1286         /* device callbacks */
1287         .intf_probe = pcan_usb_pro_probe,       /* same as PCAN-USB Pro */
1288         .dev_init = pcan_usb_fd_init,
1289
1290         .dev_exit = pcan_usb_fd_exit,
1291         .dev_free = pcan_usb_fd_free,
1292         .dev_set_bus = pcan_usb_fd_set_bus,
1293         .dev_set_bittiming = pcan_usb_fd_set_bittiming_slow,
1294         .dev_set_data_bittiming = pcan_usb_fd_set_bittiming_fast,
1295         .dev_decode_buf = pcan_usb_fd_decode_buf,
1296         .dev_start = pcan_usb_fd_start,
1297         .dev_stop = pcan_usb_fd_stop,
1298         .dev_restart_async = pcan_usb_fd_restart_async,
1299         .dev_encode_msg = pcan_usb_fd_encode_msg,
1300
1301         .do_get_berr_counter = pcan_usb_fd_get_berr_counter,
1302 };