aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/net/can/usb
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/net/can/usb')
-rw-r--r--drivers/net/can/usb/Kconfig22
-rw-r--r--drivers/net/can/usb/ems_usb.c1
-rw-r--r--drivers/net/can/usb/esd_usb2.c1
-rw-r--r--drivers/net/can/usb/kvaser_usb.c723
-rw-r--r--drivers/net/can/usb/peak_usb/Makefile2
-rw-r--r--drivers/net/can/usb/peak_usb/pcan_ucan.h222
-rw-r--r--drivers/net/can/usb/peak_usb/pcan_usb.c4
-rw-r--r--drivers/net/can/usb/peak_usb/pcan_usb_core.c83
-rw-r--r--drivers/net/can/usb/peak_usb/pcan_usb_core.h26
-rw-r--r--drivers/net/can/usb/peak_usb/pcan_usb_fd.c1095
-rw-r--r--drivers/net/can/usb/peak_usb/pcan_usb_pro.c20
-rw-r--r--drivers/net/can/usb/peak_usb/pcan_usb_pro.h13
-rw-r--r--drivers/net/can/usb/usb_8dev.c1
13 files changed, 1987 insertions, 226 deletions
diff --git a/drivers/net/can/usb/Kconfig b/drivers/net/can/usb/Kconfig
index a77db919363c..bcb272f6c68a 100644
--- a/drivers/net/can/usb/Kconfig
+++ b/drivers/net/can/usb/Kconfig
@@ -25,7 +25,7 @@ config CAN_KVASER_USB
25 tristate "Kvaser CAN/USB interface" 25 tristate "Kvaser CAN/USB interface"
26 ---help--- 26 ---help---
27 This driver adds support for Kvaser CAN/USB devices like Kvaser 27 This driver adds support for Kvaser CAN/USB devices like Kvaser
28 Leaf Light. 28 Leaf Light and Kvaser USBcan II.
29 29
30 The driver provides support for the following devices: 30 The driver provides support for the following devices:
31 - Kvaser Leaf Light 31 - Kvaser Leaf Light
@@ -46,6 +46,12 @@ config CAN_KVASER_USB
46 - Kvaser USBcan R 46 - Kvaser USBcan R
47 - Kvaser Leaf Light v2 47 - Kvaser Leaf Light v2
48 - Kvaser Mini PCI Express HS 48 - Kvaser Mini PCI Express HS
49 - Kvaser USBcan II HS/HS
50 - Kvaser USBcan II HS/LS
51 - Kvaser USBcan Rugged ("USBcan Rev B")
52 - Kvaser Memorator HS/HS
53 - Kvaser Memorator HS/LS
54 - Scania VCI2 (if you have the Kvaser logo on top)
49 55
50 If unsure, say N. 56 If unsure, say N.
51 57
@@ -53,10 +59,18 @@ config CAN_KVASER_USB
53 module will be called kvaser_usb. 59 module will be called kvaser_usb.
54 60
55config CAN_PEAK_USB 61config CAN_PEAK_USB
56 tristate "PEAK PCAN-USB/USB Pro interfaces" 62 tristate "PEAK PCAN-USB/USB Pro interfaces for CAN 2.0b/CAN-FD"
57 ---help--- 63 ---help---
58 This driver supports the PCAN-USB and PCAN-USB Pro adapters 64 This driver supports the PEAK-System Technik USB adapters that enable
59 from PEAK-System Technik (http://www.peak-system.com). 65 access to the CAN bus, with repect to the CAN 2.0b and/or CAN-FD
66 standards, that is:
67
68 PCAN-USB single CAN 2.0b channel USB adapter
69 PCAN-USB Pro dual CAN 2.0b channels USB adapter
70 PCAN-USB FD single CAN-FD channel USB adapter
71 PCAN-USB Pro FD dual CAN-FD channels USB adapter
72
73 (see also http://www.peak-system.com).
60 74
61config CAN_8DEV_USB 75config CAN_8DEV_USB
62 tristate "8 devices USB2CAN interface" 76 tristate "8 devices USB2CAN interface"
diff --git a/drivers/net/can/usb/ems_usb.c b/drivers/net/can/usb/ems_usb.c
index 29d3f0938eb8..9376f5e5b94e 100644
--- a/drivers/net/can/usb/ems_usb.c
+++ b/drivers/net/can/usb/ems_usb.c
@@ -347,6 +347,7 @@ static void ems_usb_rx_err(struct ems_usb *dev, struct ems_cpc_msg *msg)
347 dev->can.state = CAN_STATE_BUS_OFF; 347 dev->can.state = CAN_STATE_BUS_OFF;
348 cf->can_id |= CAN_ERR_BUSOFF; 348 cf->can_id |= CAN_ERR_BUSOFF;
349 349
350 dev->can.can_stats.bus_off++;
350 can_bus_off(dev->netdev); 351 can_bus_off(dev->netdev);
351 } else if (state & SJA1000_SR_ES) { 352 } else if (state & SJA1000_SR_ES) {
352 dev->can.state = CAN_STATE_ERROR_WARNING; 353 dev->can.state = CAN_STATE_ERROR_WARNING;
diff --git a/drivers/net/can/usb/esd_usb2.c b/drivers/net/can/usb/esd_usb2.c
index c063a54ab8dd..bacca0bd89c1 100644
--- a/drivers/net/can/usb/esd_usb2.c
+++ b/drivers/net/can/usb/esd_usb2.c
@@ -250,6 +250,7 @@ static void esd_usb2_rx_event(struct esd_usb2_net_priv *priv,
250 case ESD_BUSSTATE_BUSOFF: 250 case ESD_BUSSTATE_BUSOFF:
251 priv->can.state = CAN_STATE_BUS_OFF; 251 priv->can.state = CAN_STATE_BUS_OFF;
252 cf->can_id |= CAN_ERR_BUSOFF; 252 cf->can_id |= CAN_ERR_BUSOFF;
253 priv->can.can_stats.bus_off++;
253 can_bus_off(priv->netdev); 254 can_bus_off(priv->netdev);
254 break; 255 break;
255 case ESD_BUSSTATE_WARN: 256 case ESD_BUSSTATE_WARN:
diff --git a/drivers/net/can/usb/kvaser_usb.c b/drivers/net/can/usb/kvaser_usb.c
index 7af379ca861b..2928f7003041 100644
--- a/drivers/net/can/usb/kvaser_usb.c
+++ b/drivers/net/can/usb/kvaser_usb.c
@@ -6,10 +6,12 @@
6 * Parts of this driver are based on the following: 6 * Parts of this driver are based on the following:
7 * - Kvaser linux leaf driver (version 4.78) 7 * - Kvaser linux leaf driver (version 4.78)
8 * - CAN driver for esd CAN-USB/2 8 * - CAN driver for esd CAN-USB/2
9 * - Kvaser linux usbcanII driver (version 5.3)
9 * 10 *
10 * Copyright (C) 2002-2006 KVASER AB, Sweden. All rights reserved. 11 * Copyright (C) 2002-2006 KVASER AB, Sweden. All rights reserved.
11 * Copyright (C) 2010 Matthias Fuchs <matthias.fuchs@esd.eu>, esd gmbh 12 * Copyright (C) 2010 Matthias Fuchs <matthias.fuchs@esd.eu>, esd gmbh
12 * Copyright (C) 2012 Olivier Sobrie <olivier@sobrie.be> 13 * Copyright (C) 2012 Olivier Sobrie <olivier@sobrie.be>
14 * Copyright (C) 2015 Valeo S.A.
13 */ 15 */
14 16
15#include <linux/completion.h> 17#include <linux/completion.h>
@@ -30,8 +32,9 @@
30#define RX_BUFFER_SIZE 3072 32#define RX_BUFFER_SIZE 3072
31#define CAN_USB_CLOCK 8000000 33#define CAN_USB_CLOCK 8000000
32#define MAX_NET_DEVICES 3 34#define MAX_NET_DEVICES 3
35#define MAX_USBCAN_NET_DEVICES 2
33 36
34/* Kvaser USB devices */ 37/* Kvaser Leaf USB devices */
35#define KVASER_VENDOR_ID 0x0bfd 38#define KVASER_VENDOR_ID 0x0bfd
36#define USB_LEAF_DEVEL_PRODUCT_ID 10 39#define USB_LEAF_DEVEL_PRODUCT_ID 10
37#define USB_LEAF_LITE_PRODUCT_ID 11 40#define USB_LEAF_LITE_PRODUCT_ID 11
@@ -56,6 +59,24 @@
56#define USB_LEAF_LITE_V2_PRODUCT_ID 288 59#define USB_LEAF_LITE_V2_PRODUCT_ID 288
57#define USB_MINI_PCIE_HS_PRODUCT_ID 289 60#define USB_MINI_PCIE_HS_PRODUCT_ID 289
58 61
62static inline bool kvaser_is_leaf(const struct usb_device_id *id)
63{
64 return id->idProduct >= USB_LEAF_DEVEL_PRODUCT_ID &&
65 id->idProduct <= USB_MINI_PCIE_HS_PRODUCT_ID;
66}
67
68/* Kvaser USBCan-II devices */
69#define USB_USBCAN_REVB_PRODUCT_ID 2
70#define USB_VCI2_PRODUCT_ID 3
71#define USB_USBCAN2_PRODUCT_ID 4
72#define USB_MEMORATOR_PRODUCT_ID 5
73
74static inline bool kvaser_is_usbcan(const struct usb_device_id *id)
75{
76 return id->idProduct >= USB_USBCAN_REVB_PRODUCT_ID &&
77 id->idProduct <= USB_MEMORATOR_PRODUCT_ID;
78}
79
59/* USB devices features */ 80/* USB devices features */
60#define KVASER_HAS_SILENT_MODE BIT(0) 81#define KVASER_HAS_SILENT_MODE BIT(0)
61#define KVASER_HAS_TXRX_ERRORS BIT(1) 82#define KVASER_HAS_TXRX_ERRORS BIT(1)
@@ -73,7 +94,7 @@
73#define MSG_FLAG_TX_ACK BIT(6) 94#define MSG_FLAG_TX_ACK BIT(6)
74#define MSG_FLAG_TX_REQUEST BIT(7) 95#define MSG_FLAG_TX_REQUEST BIT(7)
75 96
76/* Can states */ 97/* Can states (M16C CxSTRH register) */
77#define M16C_STATE_BUS_RESET BIT(0) 98#define M16C_STATE_BUS_RESET BIT(0)
78#define M16C_STATE_BUS_ERROR BIT(4) 99#define M16C_STATE_BUS_ERROR BIT(4)
79#define M16C_STATE_BUS_PASSIVE BIT(5) 100#define M16C_STATE_BUS_PASSIVE BIT(5)
@@ -98,7 +119,11 @@
98#define CMD_START_CHIP_REPLY 27 119#define CMD_START_CHIP_REPLY 27
99#define CMD_STOP_CHIP 28 120#define CMD_STOP_CHIP 28
100#define CMD_STOP_CHIP_REPLY 29 121#define CMD_STOP_CHIP_REPLY 29
101#define CMD_GET_CARD_INFO2 32 122
123#define CMD_LEAF_GET_CARD_INFO2 32
124#define CMD_USBCAN_RESET_CLOCK 32
125#define CMD_USBCAN_CLOCK_OVERFLOW_EVENT 33
126
102#define CMD_GET_CARD_INFO 34 127#define CMD_GET_CARD_INFO 34
103#define CMD_GET_CARD_INFO_REPLY 35 128#define CMD_GET_CARD_INFO_REPLY 35
104#define CMD_GET_SOFTWARE_INFO 38 129#define CMD_GET_SOFTWARE_INFO 38
@@ -108,8 +133,9 @@
108#define CMD_RESET_ERROR_COUNTER 49 133#define CMD_RESET_ERROR_COUNTER 49
109#define CMD_TX_ACKNOWLEDGE 50 134#define CMD_TX_ACKNOWLEDGE 50
110#define CMD_CAN_ERROR_EVENT 51 135#define CMD_CAN_ERROR_EVENT 51
111#define CMD_USB_THROTTLE 77 136
112#define CMD_LOG_MESSAGE 106 137#define CMD_LEAF_USB_THROTTLE 77
138#define CMD_LEAF_LOG_MESSAGE 106
113 139
114/* error factors */ 140/* error factors */
115#define M16C_EF_ACKE BIT(0) 141#define M16C_EF_ACKE BIT(0)
@@ -121,6 +147,14 @@
121#define M16C_EF_RCVE BIT(6) 147#define M16C_EF_RCVE BIT(6)
122#define M16C_EF_TRE BIT(7) 148#define M16C_EF_TRE BIT(7)
123 149
150/* Only Leaf-based devices can report M16C error factors,
151 * thus define our own error status flags for USBCANII
152 */
153#define USBCAN_ERROR_STATE_NONE 0
154#define USBCAN_ERROR_STATE_TX_ERROR BIT(0)
155#define USBCAN_ERROR_STATE_RX_ERROR BIT(1)
156#define USBCAN_ERROR_STATE_BUSERROR BIT(2)
157
124/* bittiming parameters */ 158/* bittiming parameters */
125#define KVASER_USB_TSEG1_MIN 1 159#define KVASER_USB_TSEG1_MIN 1
126#define KVASER_USB_TSEG1_MAX 16 160#define KVASER_USB_TSEG1_MAX 16
@@ -137,9 +171,18 @@
137#define KVASER_CTRL_MODE_SELFRECEPTION 3 171#define KVASER_CTRL_MODE_SELFRECEPTION 3
138#define KVASER_CTRL_MODE_OFF 4 172#define KVASER_CTRL_MODE_OFF 4
139 173
140/* log message */ 174/* Extended CAN identifier flag */
141#define KVASER_EXTENDED_FRAME BIT(31) 175#define KVASER_EXTENDED_FRAME BIT(31)
142 176
177/* Kvaser USB CAN dongles are divided into two major families:
178 * - Leaf: Based on Renesas M32C, running firmware labeled as 'filo'
179 * - UsbcanII: Based on Renesas M16C, running firmware labeled as 'helios'
180 */
181enum kvaser_usb_family {
182 KVASER_LEAF,
183 KVASER_USBCAN,
184};
185
143struct kvaser_msg_simple { 186struct kvaser_msg_simple {
144 u8 tid; 187 u8 tid;
145 u8 channel; 188 u8 channel;
@@ -148,30 +191,55 @@ struct kvaser_msg_simple {
148struct kvaser_msg_cardinfo { 191struct kvaser_msg_cardinfo {
149 u8 tid; 192 u8 tid;
150 u8 nchannels; 193 u8 nchannels;
151 __le32 serial_number; 194 union {
152 __le32 padding; 195 struct {
196 __le32 serial_number;
197 __le32 padding;
198 } __packed leaf0;
199 struct {
200 __le32 serial_number_low;
201 __le32 serial_number_high;
202 } __packed usbcan0;
203 } __packed;
153 __le32 clock_resolution; 204 __le32 clock_resolution;
154 __le32 mfgdate; 205 __le32 mfgdate;
155 u8 ean[8]; 206 u8 ean[8];
156 u8 hw_revision; 207 u8 hw_revision;
157 u8 usb_hs_mode; 208 union {
158 __le16 padding2; 209 struct {
210 u8 usb_hs_mode;
211 } __packed leaf1;
212 struct {
213 u8 padding;
214 } __packed usbcan1;
215 } __packed;
216 __le16 padding;
159} __packed; 217} __packed;
160 218
161struct kvaser_msg_cardinfo2 { 219struct kvaser_msg_cardinfo2 {
162 u8 tid; 220 u8 tid;
163 u8 channel; 221 u8 reserved;
164 u8 pcb_id[24]; 222 u8 pcb_id[24];
165 __le32 oem_unlock_code; 223 __le32 oem_unlock_code;
166} __packed; 224} __packed;
167 225
168struct kvaser_msg_softinfo { 226struct leaf_msg_softinfo {
169 u8 tid; 227 u8 tid;
170 u8 channel; 228 u8 padding0;
171 __le32 sw_options; 229 __le32 sw_options;
172 __le32 fw_version; 230 __le32 fw_version;
173 __le16 max_outstanding_tx; 231 __le16 max_outstanding_tx;
174 __le16 padding[9]; 232 __le16 padding1[9];
233} __packed;
234
235struct usbcan_msg_softinfo {
236 u8 tid;
237 u8 fw_name[5];
238 __le16 max_outstanding_tx;
239 u8 padding[6];
240 __le32 fw_version;
241 __le16 checksum;
242 __le16 sw_options;
175} __packed; 243} __packed;
176 244
177struct kvaser_msg_busparams { 245struct kvaser_msg_busparams {
@@ -188,36 +256,86 @@ struct kvaser_msg_tx_can {
188 u8 channel; 256 u8 channel;
189 u8 tid; 257 u8 tid;
190 u8 msg[14]; 258 u8 msg[14];
191 u8 padding; 259 union {
192 u8 flags; 260 struct {
261 u8 padding;
262 u8 flags;
263 } __packed leaf;
264 struct {
265 u8 flags;
266 u8 padding;
267 } __packed usbcan;
268 } __packed;
269} __packed;
270
271struct kvaser_msg_rx_can_header {
272 u8 channel;
273 u8 flag;
193} __packed; 274} __packed;
194 275
195struct kvaser_msg_rx_can { 276struct leaf_msg_rx_can {
196 u8 channel; 277 u8 channel;
197 u8 flag; 278 u8 flag;
279
198 __le16 time[3]; 280 __le16 time[3];
199 u8 msg[14]; 281 u8 msg[14];
200} __packed; 282} __packed;
201 283
202struct kvaser_msg_chip_state_event { 284struct usbcan_msg_rx_can {
285 u8 channel;
286 u8 flag;
287
288 u8 msg[14];
289 __le16 time;
290} __packed;
291
292struct leaf_msg_chip_state_event {
203 u8 tid; 293 u8 tid;
204 u8 channel; 294 u8 channel;
295
205 __le16 time[3]; 296 __le16 time[3];
206 u8 tx_errors_count; 297 u8 tx_errors_count;
207 u8 rx_errors_count; 298 u8 rx_errors_count;
299
208 u8 status; 300 u8 status;
209 u8 padding[3]; 301 u8 padding[3];
210} __packed; 302} __packed;
211 303
212struct kvaser_msg_tx_acknowledge { 304struct usbcan_msg_chip_state_event {
305 u8 tid;
306 u8 channel;
307
308 u8 tx_errors_count;
309 u8 rx_errors_count;
310 __le16 time;
311
312 u8 status;
313 u8 padding[3];
314} __packed;
315
316struct kvaser_msg_tx_acknowledge_header {
213 u8 channel; 317 u8 channel;
214 u8 tid; 318 u8 tid;
319} __packed;
320
321struct leaf_msg_tx_acknowledge {
322 u8 channel;
323 u8 tid;
324
215 __le16 time[3]; 325 __le16 time[3];
216 u8 flags; 326 u8 flags;
217 u8 time_offset; 327 u8 time_offset;
218} __packed; 328} __packed;
219 329
220struct kvaser_msg_error_event { 330struct usbcan_msg_tx_acknowledge {
331 u8 channel;
332 u8 tid;
333
334 __le16 time;
335 __le16 padding;
336} __packed;
337
338struct leaf_msg_error_event {
221 u8 tid; 339 u8 tid;
222 u8 flags; 340 u8 flags;
223 __le16 time[3]; 341 __le16 time[3];
@@ -229,6 +347,18 @@ struct kvaser_msg_error_event {
229 u8 error_factor; 347 u8 error_factor;
230} __packed; 348} __packed;
231 349
350struct usbcan_msg_error_event {
351 u8 tid;
352 u8 padding;
353 u8 tx_errors_count_ch0;
354 u8 rx_errors_count_ch0;
355 u8 tx_errors_count_ch1;
356 u8 rx_errors_count_ch1;
357 u8 status_ch0;
358 u8 status_ch1;
359 __le16 time;
360} __packed;
361
232struct kvaser_msg_ctrl_mode { 362struct kvaser_msg_ctrl_mode {
233 u8 tid; 363 u8 tid;
234 u8 channel; 364 u8 channel;
@@ -243,7 +373,7 @@ struct kvaser_msg_flush_queue {
243 u8 padding[3]; 373 u8 padding[3];
244} __packed; 374} __packed;
245 375
246struct kvaser_msg_log_message { 376struct leaf_msg_log_message {
247 u8 channel; 377 u8 channel;
248 u8 flags; 378 u8 flags;
249 __le16 time[3]; 379 __le16 time[3];
@@ -260,19 +390,57 @@ struct kvaser_msg {
260 struct kvaser_msg_simple simple; 390 struct kvaser_msg_simple simple;
261 struct kvaser_msg_cardinfo cardinfo; 391 struct kvaser_msg_cardinfo cardinfo;
262 struct kvaser_msg_cardinfo2 cardinfo2; 392 struct kvaser_msg_cardinfo2 cardinfo2;
263 struct kvaser_msg_softinfo softinfo;
264 struct kvaser_msg_busparams busparams; 393 struct kvaser_msg_busparams busparams;
394
395 struct kvaser_msg_rx_can_header rx_can_header;
396 struct kvaser_msg_tx_acknowledge_header tx_acknowledge_header;
397
398 union {
399 struct leaf_msg_softinfo softinfo;
400 struct leaf_msg_rx_can rx_can;
401 struct leaf_msg_chip_state_event chip_state_event;
402 struct leaf_msg_tx_acknowledge tx_acknowledge;
403 struct leaf_msg_error_event error_event;
404 struct leaf_msg_log_message log_message;
405 } __packed leaf;
406
407 union {
408 struct usbcan_msg_softinfo softinfo;
409 struct usbcan_msg_rx_can rx_can;
410 struct usbcan_msg_chip_state_event chip_state_event;
411 struct usbcan_msg_tx_acknowledge tx_acknowledge;
412 struct usbcan_msg_error_event error_event;
413 } __packed usbcan;
414
265 struct kvaser_msg_tx_can tx_can; 415 struct kvaser_msg_tx_can tx_can;
266 struct kvaser_msg_rx_can rx_can;
267 struct kvaser_msg_chip_state_event chip_state_event;
268 struct kvaser_msg_tx_acknowledge tx_acknowledge;
269 struct kvaser_msg_error_event error_event;
270 struct kvaser_msg_ctrl_mode ctrl_mode; 416 struct kvaser_msg_ctrl_mode ctrl_mode;
271 struct kvaser_msg_flush_queue flush_queue; 417 struct kvaser_msg_flush_queue flush_queue;
272 struct kvaser_msg_log_message log_message;
273 } u; 418 } u;
274} __packed; 419} __packed;
275 420
421/* Summary of a kvaser error event, for a unified Leaf/Usbcan error
422 * handling. Some discrepancies between the two families exist:
423 *
424 * - USBCAN firmware does not report M16C "error factors"
425 * - USBCAN controllers has difficulties reporting if the raised error
426 * event is for ch0 or ch1. They leave such arbitration to the OS
427 * driver by letting it compare error counters with previous values
428 * and decide the error event's channel. Thus for USBCAN, the channel
429 * field is only advisory.
430 */
431struct kvaser_usb_error_summary {
432 u8 channel, status, txerr, rxerr;
433 union {
434 struct {
435 u8 error_factor;
436 } leaf;
437 struct {
438 u8 other_ch_status;
439 u8 error_state;
440 } usbcan;
441 };
442};
443
276struct kvaser_usb_tx_urb_context { 444struct kvaser_usb_tx_urb_context {
277 struct kvaser_usb_net_priv *priv; 445 struct kvaser_usb_net_priv *priv;
278 u32 echo_index; 446 u32 echo_index;
@@ -288,6 +456,7 @@ struct kvaser_usb {
288 456
289 u32 fw_version; 457 u32 fw_version;
290 unsigned int nchannels; 458 unsigned int nchannels;
459 enum kvaser_usb_family family;
291 460
292 bool rxinitdone; 461 bool rxinitdone;
293 void *rxbuf[MAX_RX_URBS]; 462 void *rxbuf[MAX_RX_URBS];
@@ -311,6 +480,7 @@ struct kvaser_usb_net_priv {
311}; 480};
312 481
313static const struct usb_device_id kvaser_usb_table[] = { 482static const struct usb_device_id kvaser_usb_table[] = {
483 /* Leaf family IDs */
314 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_DEVEL_PRODUCT_ID) }, 484 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_DEVEL_PRODUCT_ID) },
315 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_PRODUCT_ID) }, 485 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_PRODUCT_ID) },
316 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_PRODUCT_ID), 486 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_PRODUCT_ID),
@@ -360,6 +530,17 @@ static const struct usb_device_id kvaser_usb_table[] = {
360 .driver_info = KVASER_HAS_TXRX_ERRORS }, 530 .driver_info = KVASER_HAS_TXRX_ERRORS },
361 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_V2_PRODUCT_ID) }, 531 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_V2_PRODUCT_ID) },
362 { USB_DEVICE(KVASER_VENDOR_ID, USB_MINI_PCIE_HS_PRODUCT_ID) }, 532 { USB_DEVICE(KVASER_VENDOR_ID, USB_MINI_PCIE_HS_PRODUCT_ID) },
533
534 /* USBCANII family IDs */
535 { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN2_PRODUCT_ID),
536 .driver_info = KVASER_HAS_TXRX_ERRORS },
537 { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN_REVB_PRODUCT_ID),
538 .driver_info = KVASER_HAS_TXRX_ERRORS },
539 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMORATOR_PRODUCT_ID),
540 .driver_info = KVASER_HAS_TXRX_ERRORS },
541 { USB_DEVICE(KVASER_VENDOR_ID, USB_VCI2_PRODUCT_ID),
542 .driver_info = KVASER_HAS_TXRX_ERRORS },
543
363 { } 544 { }
364}; 545};
365MODULE_DEVICE_TABLE(usb, kvaser_usb_table); 546MODULE_DEVICE_TABLE(usb, kvaser_usb_table);
@@ -463,7 +644,14 @@ static int kvaser_usb_get_software_info(struct kvaser_usb *dev)
463 if (err) 644 if (err)
464 return err; 645 return err;
465 646
466 dev->fw_version = le32_to_cpu(msg.u.softinfo.fw_version); 647 switch (dev->family) {
648 case KVASER_LEAF:
649 dev->fw_version = le32_to_cpu(msg.u.leaf.softinfo.fw_version);
650 break;
651 case KVASER_USBCAN:
652 dev->fw_version = le32_to_cpu(msg.u.usbcan.softinfo.fw_version);
653 break;
654 }
467 655
468 return 0; 656 return 0;
469} 657}
@@ -482,7 +670,9 @@ static int kvaser_usb_get_card_info(struct kvaser_usb *dev)
482 return err; 670 return err;
483 671
484 dev->nchannels = msg.u.cardinfo.nchannels; 672 dev->nchannels = msg.u.cardinfo.nchannels;
485 if (dev->nchannels > MAX_NET_DEVICES) 673 if ((dev->nchannels > MAX_NET_DEVICES) ||
674 (dev->family == KVASER_USBCAN &&
675 dev->nchannels > MAX_USBCAN_NET_DEVICES))
486 return -EINVAL; 676 return -EINVAL;
487 677
488 return 0; 678 return 0;
@@ -496,8 +686,10 @@ static void kvaser_usb_tx_acknowledge(const struct kvaser_usb *dev,
496 struct kvaser_usb_net_priv *priv; 686 struct kvaser_usb_net_priv *priv;
497 struct sk_buff *skb; 687 struct sk_buff *skb;
498 struct can_frame *cf; 688 struct can_frame *cf;
499 u8 channel = msg->u.tx_acknowledge.channel; 689 u8 channel, tid;
500 u8 tid = msg->u.tx_acknowledge.tid; 690
691 channel = msg->u.tx_acknowledge_header.channel;
692 tid = msg->u.tx_acknowledge_header.tid;
501 693
502 if (channel >= dev->nchannels) { 694 if (channel >= dev->nchannels) {
503 dev_err(dev->udev->dev.parent, 695 dev_err(dev->udev->dev.parent,
@@ -615,158 +807,280 @@ static void kvaser_usb_unlink_tx_urbs(struct kvaser_usb_net_priv *priv)
615 priv->tx_contexts[i].echo_index = MAX_TX_URBS; 807 priv->tx_contexts[i].echo_index = MAX_TX_URBS;
616} 808}
617 809
618static void kvaser_usb_rx_error(const struct kvaser_usb *dev, 810static void kvaser_usb_rx_error_update_can_state(struct kvaser_usb_net_priv *priv,
619 const struct kvaser_msg *msg) 811 const struct kvaser_usb_error_summary *es,
812 struct can_frame *cf)
620{ 813{
621 struct can_frame *cf; 814 struct kvaser_usb *dev = priv->dev;
622 struct sk_buff *skb; 815 struct net_device_stats *stats = &priv->netdev->stats;
623 struct net_device_stats *stats; 816 enum can_state cur_state, new_state, tx_state, rx_state;
624 struct kvaser_usb_net_priv *priv;
625 unsigned int new_state;
626 u8 channel, status, txerr, rxerr, error_factor;
627 817
628 switch (msg->id) { 818 netdev_dbg(priv->netdev, "Error status: 0x%02x\n", es->status);
629 case CMD_CAN_ERROR_EVENT: 819
630 channel = msg->u.error_event.channel; 820 new_state = cur_state = priv->can.state;
631 status = msg->u.error_event.status; 821
632 txerr = msg->u.error_event.tx_errors_count; 822 if (es->status & (M16C_STATE_BUS_OFF | M16C_STATE_BUS_RESET))
633 rxerr = msg->u.error_event.rx_errors_count; 823 new_state = CAN_STATE_BUS_OFF;
634 error_factor = msg->u.error_event.error_factor; 824 else if (es->status & M16C_STATE_BUS_PASSIVE)
635 break; 825 new_state = CAN_STATE_ERROR_PASSIVE;
636 case CMD_LOG_MESSAGE: 826 else if (es->status & M16C_STATE_BUS_ERROR) {
637 channel = msg->u.log_message.channel; 827 /* Guard against spurious error events after a busoff */
638 status = msg->u.log_message.data[0]; 828 if (cur_state < CAN_STATE_BUS_OFF) {
639 txerr = msg->u.log_message.data[2]; 829 if ((es->txerr >= 128) || (es->rxerr >= 128))
640 rxerr = msg->u.log_message.data[3]; 830 new_state = CAN_STATE_ERROR_PASSIVE;
641 error_factor = msg->u.log_message.data[1]; 831 else if ((es->txerr >= 96) || (es->rxerr >= 96))
832 new_state = CAN_STATE_ERROR_WARNING;
833 else if (cur_state > CAN_STATE_ERROR_ACTIVE)
834 new_state = CAN_STATE_ERROR_ACTIVE;
835 }
836 }
837
838 if (!es->status)
839 new_state = CAN_STATE_ERROR_ACTIVE;
840
841 if (new_state != cur_state) {
842 tx_state = (es->txerr >= es->rxerr) ? new_state : 0;
843 rx_state = (es->txerr <= es->rxerr) ? new_state : 0;
844
845 can_change_state(priv->netdev, cf, tx_state, rx_state);
846 }
847
848 if (priv->can.restart_ms &&
849 (cur_state >= CAN_STATE_BUS_OFF) &&
850 (new_state < CAN_STATE_BUS_OFF)) {
851 priv->can.can_stats.restarts++;
852 }
853
854 switch (dev->family) {
855 case KVASER_LEAF:
856 if (es->leaf.error_factor) {
857 priv->can.can_stats.bus_error++;
858 stats->rx_errors++;
859 }
642 break; 860 break;
643 case CMD_CHIP_STATE_EVENT: 861 case KVASER_USBCAN:
644 channel = msg->u.chip_state_event.channel; 862 if (es->usbcan.error_state & USBCAN_ERROR_STATE_TX_ERROR)
645 status = msg->u.chip_state_event.status; 863 stats->tx_errors++;
646 txerr = msg->u.chip_state_event.tx_errors_count; 864 if (es->usbcan.error_state & USBCAN_ERROR_STATE_RX_ERROR)
647 rxerr = msg->u.chip_state_event.rx_errors_count; 865 stats->rx_errors++;
648 error_factor = 0; 866 if (es->usbcan.error_state & USBCAN_ERROR_STATE_BUSERROR) {
867 priv->can.can_stats.bus_error++;
868 }
649 break; 869 break;
650 default:
651 dev_err(dev->udev->dev.parent, "Invalid msg id (%d)\n",
652 msg->id);
653 return;
654 } 870 }
655 871
656 if (channel >= dev->nchannels) { 872 priv->bec.txerr = es->txerr;
873 priv->bec.rxerr = es->rxerr;
874}
875
876static void kvaser_usb_rx_error(const struct kvaser_usb *dev,
877 const struct kvaser_usb_error_summary *es)
878{
879 struct can_frame *cf, tmp_cf = { .can_id = CAN_ERR_FLAG, .can_dlc = CAN_ERR_DLC };
880 struct sk_buff *skb;
881 struct net_device_stats *stats;
882 struct kvaser_usb_net_priv *priv;
883 enum can_state old_state, new_state;
884
885 if (es->channel >= dev->nchannels) {
657 dev_err(dev->udev->dev.parent, 886 dev_err(dev->udev->dev.parent,
658 "Invalid channel number (%d)\n", channel); 887 "Invalid channel number (%d)\n", es->channel);
659 return; 888 return;
660 } 889 }
661 890
662 priv = dev->nets[channel]; 891 priv = dev->nets[es->channel];
663 stats = &priv->netdev->stats; 892 stats = &priv->netdev->stats;
664 893
894 /* Update all of the can interface's state and error counters before
895 * trying any memory allocation that can actually fail with -ENOMEM.
896 *
897 * We send a temporary stack-allocated error can frame to
898 * can_change_state() for the very same reason.
899 *
900 * TODO: Split can_change_state() responsibility between updating the
901 * can interface's state and counters, and the setting up of can error
902 * frame ID and data to userspace. Remove stack allocation afterwards.
903 */
904 old_state = priv->can.state;
905 kvaser_usb_rx_error_update_can_state(priv, es, &tmp_cf);
906 new_state = priv->can.state;
907
665 skb = alloc_can_err_skb(priv->netdev, &cf); 908 skb = alloc_can_err_skb(priv->netdev, &cf);
666 if (!skb) { 909 if (!skb) {
667 stats->rx_dropped++; 910 stats->rx_dropped++;
668 return; 911 return;
669 } 912 }
913 memcpy(cf, &tmp_cf, sizeof(*cf));
914
915 if (new_state != old_state) {
916 if (es->status &
917 (M16C_STATE_BUS_OFF | M16C_STATE_BUS_RESET)) {
918 if (!priv->can.restart_ms)
919 kvaser_usb_simple_msg_async(priv, CMD_STOP_CHIP);
920 netif_carrier_off(priv->netdev);
921 }
670 922
671 new_state = priv->can.state; 923 if (priv->can.restart_ms &&
672 924 (old_state >= CAN_STATE_BUS_OFF) &&
673 netdev_dbg(priv->netdev, "Error status: 0x%02x\n", status); 925 (new_state < CAN_STATE_BUS_OFF)) {
926 cf->can_id |= CAN_ERR_RESTARTED;
927 netif_carrier_on(priv->netdev);
928 }
929 }
674 930
675 if (status & (M16C_STATE_BUS_OFF | M16C_STATE_BUS_RESET)) { 931 switch (dev->family) {
676 cf->can_id |= CAN_ERR_BUSOFF; 932 case KVASER_LEAF:
933 if (es->leaf.error_factor) {
934 cf->can_id |= CAN_ERR_BUSERROR | CAN_ERR_PROT;
935
936 if (es->leaf.error_factor & M16C_EF_ACKE)
937 cf->data[3] |= (CAN_ERR_PROT_LOC_ACK);
938 if (es->leaf.error_factor & M16C_EF_CRCE)
939 cf->data[3] |= (CAN_ERR_PROT_LOC_CRC_SEQ |
940 CAN_ERR_PROT_LOC_CRC_DEL);
941 if (es->leaf.error_factor & M16C_EF_FORME)
942 cf->data[2] |= CAN_ERR_PROT_FORM;
943 if (es->leaf.error_factor & M16C_EF_STFE)
944 cf->data[2] |= CAN_ERR_PROT_STUFF;
945 if (es->leaf.error_factor & M16C_EF_BITE0)
946 cf->data[2] |= CAN_ERR_PROT_BIT0;
947 if (es->leaf.error_factor & M16C_EF_BITE1)
948 cf->data[2] |= CAN_ERR_PROT_BIT1;
949 if (es->leaf.error_factor & M16C_EF_TRE)
950 cf->data[2] |= CAN_ERR_PROT_TX;
951 }
952 break;
953 case KVASER_USBCAN:
954 if (es->usbcan.error_state & USBCAN_ERROR_STATE_BUSERROR) {
955 cf->can_id |= CAN_ERR_BUSERROR;
956 }
957 break;
958 }
677 959
678 priv->can.can_stats.bus_off++; 960 cf->data[6] = es->txerr;
679 if (!priv->can.restart_ms) 961 cf->data[7] = es->rxerr;
680 kvaser_usb_simple_msg_async(priv, CMD_STOP_CHIP);
681 962
682 netif_carrier_off(priv->netdev); 963 stats->rx_packets++;
964 stats->rx_bytes += cf->can_dlc;
965 netif_rx(skb);
966}
683 967
684 new_state = CAN_STATE_BUS_OFF; 968/* For USBCAN, report error to userspace iff the channels's errors counter
685 } else if (status & M16C_STATE_BUS_PASSIVE) { 969 * has changed, or we're the only channel seeing a bus error state.
686 if (priv->can.state != CAN_STATE_ERROR_PASSIVE) { 970 */
687 cf->can_id |= CAN_ERR_CRTL; 971static void kvaser_usbcan_conditionally_rx_error(const struct kvaser_usb *dev,
688 972 struct kvaser_usb_error_summary *es)
689 if (txerr || rxerr) 973{
690 cf->data[1] = (txerr > rxerr) 974 struct kvaser_usb_net_priv *priv;
691 ? CAN_ERR_CRTL_TX_PASSIVE 975 int channel;
692 : CAN_ERR_CRTL_RX_PASSIVE; 976 bool report_error;
693 else
694 cf->data[1] = CAN_ERR_CRTL_TX_PASSIVE |
695 CAN_ERR_CRTL_RX_PASSIVE;
696
697 priv->can.can_stats.error_passive++;
698 }
699 977
700 new_state = CAN_STATE_ERROR_PASSIVE; 978 channel = es->channel;
701 } else if (status & M16C_STATE_BUS_ERROR) { 979 if (channel >= dev->nchannels) {
702 if ((priv->can.state < CAN_STATE_ERROR_WARNING) && 980 dev_err(dev->udev->dev.parent,
703 ((txerr >= 96) || (rxerr >= 96))) { 981 "Invalid channel number (%d)\n", channel);
704 cf->can_id |= CAN_ERR_CRTL; 982 return;
705 cf->data[1] = (txerr > rxerr)
706 ? CAN_ERR_CRTL_TX_WARNING
707 : CAN_ERR_CRTL_RX_WARNING;
708
709 priv->can.can_stats.error_warning++;
710 new_state = CAN_STATE_ERROR_WARNING;
711 } else if ((priv->can.state > CAN_STATE_ERROR_ACTIVE) &&
712 ((txerr < 96) && (rxerr < 96))) {
713 cf->can_id |= CAN_ERR_PROT;
714 cf->data[2] = CAN_ERR_PROT_ACTIVE;
715
716 new_state = CAN_STATE_ERROR_ACTIVE;
717 }
718 } 983 }
719 984
720 if (!status) { 985 priv = dev->nets[channel];
721 cf->can_id |= CAN_ERR_PROT; 986 report_error = false;
722 cf->data[2] = CAN_ERR_PROT_ACTIVE;
723 987
724 new_state = CAN_STATE_ERROR_ACTIVE; 988 if (es->txerr != priv->bec.txerr) {
989 es->usbcan.error_state |= USBCAN_ERROR_STATE_TX_ERROR;
990 report_error = true;
991 }
992 if (es->rxerr != priv->bec.rxerr) {
993 es->usbcan.error_state |= USBCAN_ERROR_STATE_RX_ERROR;
994 report_error = true;
995 }
996 if ((es->status & M16C_STATE_BUS_ERROR) &&
997 !(es->usbcan.other_ch_status & M16C_STATE_BUS_ERROR)) {
998 es->usbcan.error_state |= USBCAN_ERROR_STATE_BUSERROR;
999 report_error = true;
725 } 1000 }
726 1001
727 if (priv->can.restart_ms && 1002 if (report_error)
728 (priv->can.state >= CAN_STATE_BUS_OFF) && 1003 kvaser_usb_rx_error(dev, es);
729 (new_state < CAN_STATE_BUS_OFF)) { 1004}
730 cf->can_id |= CAN_ERR_RESTARTED;
731 netif_carrier_on(priv->netdev);
732 1005
733 priv->can.can_stats.restarts++; 1006static void kvaser_usbcan_rx_error(const struct kvaser_usb *dev,
734 } 1007 const struct kvaser_msg *msg)
1008{
1009 struct kvaser_usb_error_summary es = { };
735 1010
736 if (error_factor) { 1011 switch (msg->id) {
737 priv->can.can_stats.bus_error++; 1012 /* Sometimes errors are sent as unsolicited chip state events */
738 stats->rx_errors++; 1013 case CMD_CHIP_STATE_EVENT:
1014 es.channel = msg->u.usbcan.chip_state_event.channel;
1015 es.status = msg->u.usbcan.chip_state_event.status;
1016 es.txerr = msg->u.usbcan.chip_state_event.tx_errors_count;
1017 es.rxerr = msg->u.usbcan.chip_state_event.rx_errors_count;
1018 kvaser_usbcan_conditionally_rx_error(dev, &es);
1019 break;
739 1020
740 cf->can_id |= CAN_ERR_BUSERROR | CAN_ERR_PROT; 1021 case CMD_CAN_ERROR_EVENT:
741 1022 es.channel = 0;
742 if (error_factor & M16C_EF_ACKE) 1023 es.status = msg->u.usbcan.error_event.status_ch0;
743 cf->data[3] |= (CAN_ERR_PROT_LOC_ACK); 1024 es.txerr = msg->u.usbcan.error_event.tx_errors_count_ch0;
744 if (error_factor & M16C_EF_CRCE) 1025 es.rxerr = msg->u.usbcan.error_event.rx_errors_count_ch0;
745 cf->data[3] |= (CAN_ERR_PROT_LOC_CRC_SEQ | 1026 es.usbcan.other_ch_status =
746 CAN_ERR_PROT_LOC_CRC_DEL); 1027 msg->u.usbcan.error_event.status_ch1;
747 if (error_factor & M16C_EF_FORME) 1028 kvaser_usbcan_conditionally_rx_error(dev, &es);
748 cf->data[2] |= CAN_ERR_PROT_FORM; 1029
749 if (error_factor & M16C_EF_STFE) 1030 /* The USBCAN firmware supports up to 2 channels.
750 cf->data[2] |= CAN_ERR_PROT_STUFF; 1031 * Now that ch0 was checked, check if ch1 has any errors.
751 if (error_factor & M16C_EF_BITE0) 1032 */
752 cf->data[2] |= CAN_ERR_PROT_BIT0; 1033 if (dev->nchannels == MAX_USBCAN_NET_DEVICES) {
753 if (error_factor & M16C_EF_BITE1) 1034 es.channel = 1;
754 cf->data[2] |= CAN_ERR_PROT_BIT1; 1035 es.status = msg->u.usbcan.error_event.status_ch1;
755 if (error_factor & M16C_EF_TRE) 1036 es.txerr = msg->u.usbcan.error_event.tx_errors_count_ch1;
756 cf->data[2] |= CAN_ERR_PROT_TX; 1037 es.rxerr = msg->u.usbcan.error_event.rx_errors_count_ch1;
757 } 1038 es.usbcan.other_ch_status =
1039 msg->u.usbcan.error_event.status_ch0;
1040 kvaser_usbcan_conditionally_rx_error(dev, &es);
1041 }
1042 break;
758 1043
759 cf->data[6] = txerr; 1044 default:
760 cf->data[7] = rxerr; 1045 dev_err(dev->udev->dev.parent, "Invalid msg id (%d)\n",
1046 msg->id);
1047 }
1048}
761 1049
762 priv->bec.txerr = txerr; 1050static void kvaser_leaf_rx_error(const struct kvaser_usb *dev,
763 priv->bec.rxerr = rxerr; 1051 const struct kvaser_msg *msg)
1052{
1053 struct kvaser_usb_error_summary es = { };
764 1054
765 priv->can.state = new_state; 1055 switch (msg->id) {
1056 case CMD_CAN_ERROR_EVENT:
1057 es.channel = msg->u.leaf.error_event.channel;
1058 es.status = msg->u.leaf.error_event.status;
1059 es.txerr = msg->u.leaf.error_event.tx_errors_count;
1060 es.rxerr = msg->u.leaf.error_event.rx_errors_count;
1061 es.leaf.error_factor = msg->u.leaf.error_event.error_factor;
1062 break;
1063 case CMD_LEAF_LOG_MESSAGE:
1064 es.channel = msg->u.leaf.log_message.channel;
1065 es.status = msg->u.leaf.log_message.data[0];
1066 es.txerr = msg->u.leaf.log_message.data[2];
1067 es.rxerr = msg->u.leaf.log_message.data[3];
1068 es.leaf.error_factor = msg->u.leaf.log_message.data[1];
1069 break;
1070 case CMD_CHIP_STATE_EVENT:
1071 es.channel = msg->u.leaf.chip_state_event.channel;
1072 es.status = msg->u.leaf.chip_state_event.status;
1073 es.txerr = msg->u.leaf.chip_state_event.tx_errors_count;
1074 es.rxerr = msg->u.leaf.chip_state_event.rx_errors_count;
1075 es.leaf.error_factor = 0;
1076 break;
1077 default:
1078 dev_err(dev->udev->dev.parent, "Invalid msg id (%d)\n",
1079 msg->id);
1080 return;
1081 }
766 1082
767 stats->rx_packets++; 1083 kvaser_usb_rx_error(dev, &es);
768 stats->rx_bytes += cf->can_dlc;
769 netif_rx(skb);
770} 1084}
771 1085
772static void kvaser_usb_rx_can_err(const struct kvaser_usb_net_priv *priv, 1086static void kvaser_usb_rx_can_err(const struct kvaser_usb_net_priv *priv,
@@ -776,16 +1090,19 @@ static void kvaser_usb_rx_can_err(const struct kvaser_usb_net_priv *priv,
776 struct sk_buff *skb; 1090 struct sk_buff *skb;
777 struct net_device_stats *stats = &priv->netdev->stats; 1091 struct net_device_stats *stats = &priv->netdev->stats;
778 1092
779 if (msg->u.rx_can.flag & (MSG_FLAG_ERROR_FRAME | 1093 if (msg->u.rx_can_header.flag & (MSG_FLAG_ERROR_FRAME |
780 MSG_FLAG_NERR)) { 1094 MSG_FLAG_NERR)) {
781 netdev_err(priv->netdev, "Unknow error (flags: 0x%02x)\n", 1095 netdev_err(priv->netdev, "Unknow error (flags: 0x%02x)\n",
782 msg->u.rx_can.flag); 1096 msg->u.rx_can_header.flag);
783 1097
784 stats->rx_errors++; 1098 stats->rx_errors++;
785 return; 1099 return;
786 } 1100 }
787 1101
788 if (msg->u.rx_can.flag & MSG_FLAG_OVERRUN) { 1102 if (msg->u.rx_can_header.flag & MSG_FLAG_OVERRUN) {
1103 stats->rx_over_errors++;
1104 stats->rx_errors++;
1105
789 skb = alloc_can_err_skb(priv->netdev, &cf); 1106 skb = alloc_can_err_skb(priv->netdev, &cf);
790 if (!skb) { 1107 if (!skb) {
791 stats->rx_dropped++; 1108 stats->rx_dropped++;
@@ -795,9 +1112,6 @@ static void kvaser_usb_rx_can_err(const struct kvaser_usb_net_priv *priv,
795 cf->can_id |= CAN_ERR_CRTL; 1112 cf->can_id |= CAN_ERR_CRTL;
796 cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW; 1113 cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
797 1114
798 stats->rx_over_errors++;
799 stats->rx_errors++;
800
801 stats->rx_packets++; 1115 stats->rx_packets++;
802 stats->rx_bytes += cf->can_dlc; 1116 stats->rx_bytes += cf->can_dlc;
803 netif_rx(skb); 1117 netif_rx(skb);
@@ -811,7 +1125,8 @@ static void kvaser_usb_rx_can_msg(const struct kvaser_usb *dev,
811 struct can_frame *cf; 1125 struct can_frame *cf;
812 struct sk_buff *skb; 1126 struct sk_buff *skb;
813 struct net_device_stats *stats; 1127 struct net_device_stats *stats;
814 u8 channel = msg->u.rx_can.channel; 1128 u8 channel = msg->u.rx_can_header.channel;
1129 const u8 *rx_msg = NULL; /* GCC */
815 1130
816 if (channel >= dev->nchannels) { 1131 if (channel >= dev->nchannels) {
817 dev_err(dev->udev->dev.parent, 1132 dev_err(dev->udev->dev.parent,
@@ -822,60 +1137,68 @@ static void kvaser_usb_rx_can_msg(const struct kvaser_usb *dev,
822 priv = dev->nets[channel]; 1137 priv = dev->nets[channel];
823 stats = &priv->netdev->stats; 1138 stats = &priv->netdev->stats;
824 1139
825 if ((msg->u.rx_can.flag & MSG_FLAG_ERROR_FRAME) && 1140 if ((msg->u.rx_can_header.flag & MSG_FLAG_ERROR_FRAME) &&
826 (msg->id == CMD_LOG_MESSAGE)) { 1141 (dev->family == KVASER_LEAF && msg->id == CMD_LEAF_LOG_MESSAGE)) {
827 kvaser_usb_rx_error(dev, msg); 1142 kvaser_leaf_rx_error(dev, msg);
828 return; 1143 return;
829 } else if (msg->u.rx_can.flag & (MSG_FLAG_ERROR_FRAME | 1144 } else if (msg->u.rx_can_header.flag & (MSG_FLAG_ERROR_FRAME |
830 MSG_FLAG_NERR | 1145 MSG_FLAG_NERR |
831 MSG_FLAG_OVERRUN)) { 1146 MSG_FLAG_OVERRUN)) {
832 kvaser_usb_rx_can_err(priv, msg); 1147 kvaser_usb_rx_can_err(priv, msg);
833 return; 1148 return;
834 } else if (msg->u.rx_can.flag & ~MSG_FLAG_REMOTE_FRAME) { 1149 } else if (msg->u.rx_can_header.flag & ~MSG_FLAG_REMOTE_FRAME) {
835 netdev_warn(priv->netdev, 1150 netdev_warn(priv->netdev,
836 "Unhandled frame (flags: 0x%02x)", 1151 "Unhandled frame (flags: 0x%02x)",
837 msg->u.rx_can.flag); 1152 msg->u.rx_can_header.flag);
838 return; 1153 return;
839 } 1154 }
840 1155
1156 switch (dev->family) {
1157 case KVASER_LEAF:
1158 rx_msg = msg->u.leaf.rx_can.msg;
1159 break;
1160 case KVASER_USBCAN:
1161 rx_msg = msg->u.usbcan.rx_can.msg;
1162 break;
1163 }
1164
841 skb = alloc_can_skb(priv->netdev, &cf); 1165 skb = alloc_can_skb(priv->netdev, &cf);
842 if (!skb) { 1166 if (!skb) {
843 stats->tx_dropped++; 1167 stats->tx_dropped++;
844 return; 1168 return;
845 } 1169 }
846 1170
847 if (msg->id == CMD_LOG_MESSAGE) { 1171 if (dev->family == KVASER_LEAF && msg->id == CMD_LEAF_LOG_MESSAGE) {
848 cf->can_id = le32_to_cpu(msg->u.log_message.id); 1172 cf->can_id = le32_to_cpu(msg->u.leaf.log_message.id);
849 if (cf->can_id & KVASER_EXTENDED_FRAME) 1173 if (cf->can_id & KVASER_EXTENDED_FRAME)
850 cf->can_id &= CAN_EFF_MASK | CAN_EFF_FLAG; 1174 cf->can_id &= CAN_EFF_MASK | CAN_EFF_FLAG;
851 else 1175 else
852 cf->can_id &= CAN_SFF_MASK; 1176 cf->can_id &= CAN_SFF_MASK;
853 1177
854 cf->can_dlc = get_can_dlc(msg->u.log_message.dlc); 1178 cf->can_dlc = get_can_dlc(msg->u.leaf.log_message.dlc);
855 1179
856 if (msg->u.log_message.flags & MSG_FLAG_REMOTE_FRAME) 1180 if (msg->u.leaf.log_message.flags & MSG_FLAG_REMOTE_FRAME)
857 cf->can_id |= CAN_RTR_FLAG; 1181 cf->can_id |= CAN_RTR_FLAG;
858 else 1182 else
859 memcpy(cf->data, &msg->u.log_message.data, 1183 memcpy(cf->data, &msg->u.leaf.log_message.data,
860 cf->can_dlc); 1184 cf->can_dlc);
861 } else { 1185 } else {
862 cf->can_id = ((msg->u.rx_can.msg[0] & 0x1f) << 6) | 1186 cf->can_id = ((rx_msg[0] & 0x1f) << 6) | (rx_msg[1] & 0x3f);
863 (msg->u.rx_can.msg[1] & 0x3f);
864 1187
865 if (msg->id == CMD_RX_EXT_MESSAGE) { 1188 if (msg->id == CMD_RX_EXT_MESSAGE) {
866 cf->can_id <<= 18; 1189 cf->can_id <<= 18;
867 cf->can_id |= ((msg->u.rx_can.msg[2] & 0x0f) << 14) | 1190 cf->can_id |= ((rx_msg[2] & 0x0f) << 14) |
868 ((msg->u.rx_can.msg[3] & 0xff) << 6) | 1191 ((rx_msg[3] & 0xff) << 6) |
869 (msg->u.rx_can.msg[4] & 0x3f); 1192 (rx_msg[4] & 0x3f);
870 cf->can_id |= CAN_EFF_FLAG; 1193 cf->can_id |= CAN_EFF_FLAG;
871 } 1194 }
872 1195
873 cf->can_dlc = get_can_dlc(msg->u.rx_can.msg[5]); 1196 cf->can_dlc = get_can_dlc(rx_msg[5]);
874 1197
875 if (msg->u.rx_can.flag & MSG_FLAG_REMOTE_FRAME) 1198 if (msg->u.rx_can_header.flag & MSG_FLAG_REMOTE_FRAME)
876 cf->can_id |= CAN_RTR_FLAG; 1199 cf->can_id |= CAN_RTR_FLAG;
877 else 1200 else
878 memcpy(cf->data, &msg->u.rx_can.msg[6], 1201 memcpy(cf->data, &rx_msg[6],
879 cf->can_dlc); 1202 cf->can_dlc);
880 } 1203 }
881 1204
@@ -938,21 +1261,35 @@ static void kvaser_usb_handle_message(const struct kvaser_usb *dev,
938 1261
939 case CMD_RX_STD_MESSAGE: 1262 case CMD_RX_STD_MESSAGE:
940 case CMD_RX_EXT_MESSAGE: 1263 case CMD_RX_EXT_MESSAGE:
941 case CMD_LOG_MESSAGE: 1264 kvaser_usb_rx_can_msg(dev, msg);
1265 break;
1266
1267 case CMD_LEAF_LOG_MESSAGE:
1268 if (dev->family != KVASER_LEAF)
1269 goto warn;
942 kvaser_usb_rx_can_msg(dev, msg); 1270 kvaser_usb_rx_can_msg(dev, msg);
943 break; 1271 break;
944 1272
945 case CMD_CHIP_STATE_EVENT: 1273 case CMD_CHIP_STATE_EVENT:
946 case CMD_CAN_ERROR_EVENT: 1274 case CMD_CAN_ERROR_EVENT:
947 kvaser_usb_rx_error(dev, msg); 1275 if (dev->family == KVASER_LEAF)
1276 kvaser_leaf_rx_error(dev, msg);
1277 else
1278 kvaser_usbcan_rx_error(dev, msg);
948 break; 1279 break;
949 1280
950 case CMD_TX_ACKNOWLEDGE: 1281 case CMD_TX_ACKNOWLEDGE:
951 kvaser_usb_tx_acknowledge(dev, msg); 1282 kvaser_usb_tx_acknowledge(dev, msg);
952 break; 1283 break;
953 1284
1285 /* Ignored messages */
1286 case CMD_USBCAN_CLOCK_OVERFLOW_EVENT:
1287 if (dev->family != KVASER_USBCAN)
1288 goto warn;
1289 break;
1290
954 default: 1291 default:
955 dev_warn(dev->udev->dev.parent, 1292warn: dev_warn(dev->udev->dev.parent,
956 "Unhandled message (%d)\n", msg->id); 1293 "Unhandled message (%d)\n", msg->id);
957 break; 1294 break;
958 } 1295 }
@@ -1172,7 +1509,7 @@ static void kvaser_usb_unlink_all_urbs(struct kvaser_usb *dev)
1172 dev->rxbuf[i], 1509 dev->rxbuf[i],
1173 dev->rxbuf_dma[i]); 1510 dev->rxbuf_dma[i]);
1174 1511
1175 for (i = 0; i < MAX_NET_DEVICES; i++) { 1512 for (i = 0; i < dev->nchannels; i++) {
1176 struct kvaser_usb_net_priv *priv = dev->nets[i]; 1513 struct kvaser_usb_net_priv *priv = dev->nets[i];
1177 1514
1178 if (priv) 1515 if (priv)
@@ -1280,6 +1617,7 @@ static netdev_tx_t kvaser_usb_start_xmit(struct sk_buff *skb,
1280 struct kvaser_msg *msg; 1617 struct kvaser_msg *msg;
1281 int i, err; 1618 int i, err;
1282 int ret = NETDEV_TX_OK; 1619 int ret = NETDEV_TX_OK;
1620 u8 *msg_tx_can_flags = NULL; /* GCC */
1283 1621
1284 if (can_dropped_invalid_skb(netdev, skb)) 1622 if (can_dropped_invalid_skb(netdev, skb))
1285 return NETDEV_TX_OK; 1623 return NETDEV_TX_OK;
@@ -1301,9 +1639,19 @@ static netdev_tx_t kvaser_usb_start_xmit(struct sk_buff *skb,
1301 1639
1302 msg = buf; 1640 msg = buf;
1303 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_tx_can); 1641 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_tx_can);
1304 msg->u.tx_can.flags = 0;
1305 msg->u.tx_can.channel = priv->channel; 1642 msg->u.tx_can.channel = priv->channel;
1306 1643
1644 switch (dev->family) {
1645 case KVASER_LEAF:
1646 msg_tx_can_flags = &msg->u.tx_can.leaf.flags;
1647 break;
1648 case KVASER_USBCAN:
1649 msg_tx_can_flags = &msg->u.tx_can.usbcan.flags;
1650 break;
1651 }
1652
1653 *msg_tx_can_flags = 0;
1654
1307 if (cf->can_id & CAN_EFF_FLAG) { 1655 if (cf->can_id & CAN_EFF_FLAG) {
1308 msg->id = CMD_TX_EXT_MESSAGE; 1656 msg->id = CMD_TX_EXT_MESSAGE;
1309 msg->u.tx_can.msg[0] = (cf->can_id >> 24) & 0x1f; 1657 msg->u.tx_can.msg[0] = (cf->can_id >> 24) & 0x1f;
@@ -1321,7 +1669,7 @@ static netdev_tx_t kvaser_usb_start_xmit(struct sk_buff *skb,
1321 memcpy(&msg->u.tx_can.msg[6], cf->data, cf->can_dlc); 1669 memcpy(&msg->u.tx_can.msg[6], cf->data, cf->can_dlc);
1322 1670
1323 if (cf->can_id & CAN_RTR_FLAG) 1671 if (cf->can_id & CAN_RTR_FLAG)
1324 msg->u.tx_can.flags |= MSG_FLAG_REMOTE_FRAME; 1672 *msg_tx_can_flags |= MSG_FLAG_REMOTE_FRAME;
1325 1673
1326 for (i = 0; i < ARRAY_SIZE(priv->tx_contexts); i++) { 1674 for (i = 0; i < ARRAY_SIZE(priv->tx_contexts); i++) {
1327 if (priv->tx_contexts[i].echo_index == MAX_TX_URBS) { 1675 if (priv->tx_contexts[i].echo_index == MAX_TX_URBS) {
@@ -1590,6 +1938,17 @@ static int kvaser_usb_probe(struct usb_interface *intf,
1590 if (!dev) 1938 if (!dev)
1591 return -ENOMEM; 1939 return -ENOMEM;
1592 1940
1941 if (kvaser_is_leaf(id)) {
1942 dev->family = KVASER_LEAF;
1943 } else if (kvaser_is_usbcan(id)) {
1944 dev->family = KVASER_USBCAN;
1945 } else {
1946 dev_err(&intf->dev,
1947 "Product ID (%d) does not belong to any known Kvaser USB family",
1948 id->idProduct);
1949 return -ENODEV;
1950 }
1951
1593 err = kvaser_usb_get_endpoints(intf, &dev->bulk_in, &dev->bulk_out); 1952 err = kvaser_usb_get_endpoints(intf, &dev->bulk_in, &dev->bulk_out);
1594 if (err) { 1953 if (err) {
1595 dev_err(&intf->dev, "Cannot get usb endpoint(s)"); 1954 dev_err(&intf->dev, "Cannot get usb endpoint(s)");
diff --git a/drivers/net/can/usb/peak_usb/Makefile b/drivers/net/can/usb/peak_usb/Makefile
index 1aefbc88d643..1839e9ca62e7 100644
--- a/drivers/net/can/usb/peak_usb/Makefile
+++ b/drivers/net/can/usb/peak_usb/Makefile
@@ -1,2 +1,2 @@
1obj-$(CONFIG_CAN_PEAK_USB) += peak_usb.o 1obj-$(CONFIG_CAN_PEAK_USB) += peak_usb.o
2peak_usb-y = pcan_usb_core.o pcan_usb.o pcan_usb_pro.o 2peak_usb-y = pcan_usb_core.o pcan_usb.o pcan_usb_pro.o pcan_usb_fd.o
diff --git a/drivers/net/can/usb/peak_usb/pcan_ucan.h b/drivers/net/can/usb/peak_usb/pcan_ucan.h
new file mode 100644
index 000000000000..1ba7c25002e1
--- /dev/null
+++ b/drivers/net/can/usb/peak_usb/pcan_ucan.h
@@ -0,0 +1,222 @@
1/*
2 * CAN driver for PEAK System micro-CAN based adapters
3 *
4 * Copyright (C) 2003-2011 PEAK System-Technik GmbH
5 * Copyright (C) 2011-2013 Stephane Grosjean <s.grosjean@peak-system.com>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 */
16#ifndef PUCAN_H
17#define PUCAN_H
18
19/* uCAN commands opcodes list (low-order 10 bits) */
20#define PUCAN_CMD_NOP 0x000
21#define PUCAN_CMD_RESET_MODE 0x001
22#define PUCAN_CMD_NORMAL_MODE 0x002
23#define PUCAN_CMD_LISTEN_ONLY_MODE 0x003
24#define PUCAN_CMD_TIMING_SLOW 0x004
25#define PUCAN_CMD_TIMING_FAST 0x005
26#define PUCAN_CMD_FILTER_STD 0x008
27#define PUCAN_CMD_TX_ABORT 0x009
28#define PUCAN_CMD_WR_ERR_CNT 0x00a
29#define PUCAN_CMD_RX_FRAME_ENABLE 0x00b
30#define PUCAN_CMD_RX_FRAME_DISABLE 0x00c
31#define PUCAN_CMD_END_OF_COLLECTION 0x3ff
32
33/* uCAN received messages list */
34#define PUCAN_MSG_CAN_RX 0x0001
35#define PUCAN_MSG_ERROR 0x0002
36#define PUCAN_MSG_STATUS 0x0003
37#define PUCAN_MSG_BUSLOAD 0x0004
38#define PUCAN_MSG_CAN_TX 0x1000
39
40/* uCAN command common header */
41struct __packed pucan_command {
42 __le16 opcode_channel;
43 u16 args[3];
44};
45
46/* uCAN TIMING_SLOW command fields */
47#define PUCAN_TSLOW_SJW_T(s, t) (((s) & 0xf) | ((!!(t)) << 7))
48#define PUCAN_TSLOW_TSEG2(t) ((t) & 0xf)
49#define PUCAN_TSLOW_TSEG1(t) ((t) & 0x3f)
50#define PUCAN_TSLOW_BRP(b) ((b) & 0x3ff)
51
52struct __packed pucan_timing_slow {
53 __le16 opcode_channel;
54
55 u8 ewl; /* Error Warning limit */
56 u8 sjw_t; /* Sync Jump Width + Triple sampling */
57 u8 tseg2; /* Timing SEGment 2 */
58 u8 tseg1; /* Timing SEGment 1 */
59
60 __le16 brp; /* BaudRate Prescaler */
61};
62
63/* uCAN TIMING_FAST command fields */
64#define PUCAN_TFAST_SJW(s) ((s) & 0x3)
65#define PUCAN_TFAST_TSEG2(t) ((t) & 0x7)
66#define PUCAN_TFAST_TSEG1(t) ((t) & 0xf)
67#define PUCAN_TFAST_BRP(b) ((b) & 0x3ff)
68
69struct __packed pucan_timing_fast {
70 __le16 opcode_channel;
71
72 u8 unused;
73 u8 sjw; /* Sync Jump Width */
74 u8 tseg2; /* Timing SEGment 2 */
75 u8 tseg1; /* Timing SEGment 1 */
76
77 __le16 brp; /* BaudRate Prescaler */
78};
79
80/* uCAN FILTER_STD command fields */
81#define PUCAN_FLTSTD_ROW_IDX_BITS 6
82
83struct __packed pucan_filter_std {
84 __le16 opcode_channel;
85
86 __le16 idx;
87 __le32 mask; /* CAN-ID bitmask in idx range */
88};
89
90/* uCAN WR_ERR_CNT command fields */
91#define PUCAN_WRERRCNT_TE 0x4000 /* Tx error cntr write Enable */
92#define PUCAN_WRERRCNT_RE 0x8000 /* Rx error cntr write Enable */
93
94struct __packed pucan_wr_err_cnt {
95 __le16 opcode_channel;
96
97 __le16 sel_mask;
98 u8 tx_counter; /* Tx error counter new value */
99 u8 rx_counter; /* Rx error counter new value */
100
101 u16 unused;
102};
103
104/* uCAN RX_FRAME_ENABLE command fields */
105#define PUCAN_FLTEXT_ERROR 0x0001
106#define PUCAN_FLTEXT_BUSLOAD 0x0002
107
108struct __packed pucan_filter_ext {
109 __le16 opcode_channel;
110
111 __le16 ext_mask;
112 u32 unused;
113};
114
115/* uCAN received messages global format */
116struct __packed pucan_msg {
117 __le16 size;
118 __le16 type;
119 __le32 ts_low;
120 __le32 ts_high;
121};
122
123/* uCAN flags for CAN/CANFD messages */
124#define PUCAN_MSG_SELF_RECEIVE 0x80
125#define PUCAN_MSG_ERROR_STATE_IND 0x40 /* error state indicator */
126#define PUCAN_MSG_BITRATE_SWITCH 0x20 /* bitrate switch */
127#define PUCAN_MSG_EXT_DATA_LEN 0x10 /* extended data length */
128#define PUCAN_MSG_SINGLE_SHOT 0x08
129#define PUCAN_MSG_LOOPED_BACK 0x04
130#define PUCAN_MSG_EXT_ID 0x02
131#define PUCAN_MSG_RTR 0x01
132
133struct __packed pucan_rx_msg {
134 __le16 size;
135 __le16 type;
136 __le32 ts_low;
137 __le32 ts_high;
138 __le32 tag_low;
139 __le32 tag_high;
140 u8 channel_dlc;
141 u8 client;
142 __le16 flags;
143 __le32 can_id;
144 u8 d[0];
145};
146
147/* uCAN error types */
148#define PUCAN_ERMSG_BIT_ERROR 0
149#define PUCAN_ERMSG_FORM_ERROR 1
150#define PUCAN_ERMSG_STUFF_ERROR 2
151#define PUCAN_ERMSG_OTHER_ERROR 3
152#define PUCAN_ERMSG_ERR_CNT_DEC 4
153
154struct __packed pucan_error_msg {
155 __le16 size;
156 __le16 type;
157 __le32 ts_low;
158 __le32 ts_high;
159 u8 channel_type_d;
160 u8 code_g;
161 u8 tx_err_cnt;
162 u8 rx_err_cnt;
163};
164
165#define PUCAN_BUS_PASSIVE 0x20
166#define PUCAN_BUS_WARNING 0x40
167#define PUCAN_BUS_BUSOFF 0x80
168
169struct __packed pucan_status_msg {
170 __le16 size;
171 __le16 type;
172 __le32 ts_low;
173 __le32 ts_high;
174 u8 channel_p_w_b;
175 u8 unused[3];
176};
177
178/* uCAN transmitted message format */
179#define PUCAN_MSG_CHANNEL_DLC(c, d) (((c) & 0xf) | ((d) << 4))
180
181struct __packed pucan_tx_msg {
182 __le16 size;
183 __le16 type;
184 __le32 tag_low;
185 __le32 tag_high;
186 u8 channel_dlc;
187 u8 client;
188 __le16 flags;
189 __le32 can_id;
190 u8 d[0];
191};
192
193/* build the cmd opcode_channel field with respect to the correct endianness */
194static inline __le16 pucan_cmd_opcode_channel(struct peak_usb_device *dev,
195 int opcode)
196{
197 return cpu_to_le16(((dev->ctrl_idx) << 12) | ((opcode) & 0x3ff));
198}
199
200/* return the channel number part from any received message channel_dlc field */
201static inline int pucan_msg_get_channel(struct pucan_rx_msg *rm)
202{
203 return rm->channel_dlc & 0xf;
204}
205
206/* return the dlc value from any received message channel_dlc field */
207static inline int pucan_msg_get_dlc(struct pucan_rx_msg *rm)
208{
209 return rm->channel_dlc >> 4;
210}
211
212static inline int pucan_ermsg_get_channel(struct pucan_error_msg *em)
213{
214 return em->channel_type_d & 0x0f;
215}
216
217static inline int pucan_stmsg_get_channel(struct pucan_status_msg *sm)
218{
219 return sm->channel_p_w_b & 0x0f;
220}
221
222#endif
diff --git a/drivers/net/can/usb/peak_usb/pcan_usb.c b/drivers/net/can/usb/peak_usb/pcan_usb.c
index 4e1659d07979..72427f21edff 100644
--- a/drivers/net/can/usb/peak_usb/pcan_usb.c
+++ b/drivers/net/can/usb/peak_usb/pcan_usb.c
@@ -488,6 +488,7 @@ static int pcan_usb_decode_error(struct pcan_usb_msg_context *mc, u8 n,
488 switch (new_state) { 488 switch (new_state) {
489 case CAN_STATE_BUS_OFF: 489 case CAN_STATE_BUS_OFF:
490 cf->can_id |= CAN_ERR_BUSOFF; 490 cf->can_id |= CAN_ERR_BUSOFF;
491 mc->pdev->dev.can.can_stats.bus_off++;
491 can_bus_off(mc->netdev); 492 can_bus_off(mc->netdev);
492 break; 493 break;
493 494
@@ -854,10 +855,11 @@ static int pcan_usb_probe(struct usb_interface *intf)
854/* 855/*
855 * describe the PCAN-USB adapter 856 * describe the PCAN-USB adapter
856 */ 857 */
857struct peak_usb_adapter pcan_usb = { 858const struct peak_usb_adapter pcan_usb = {
858 .name = "PCAN-USB", 859 .name = "PCAN-USB",
859 .device_id = PCAN_USB_PRODUCT_ID, 860 .device_id = PCAN_USB_PRODUCT_ID,
860 .ctrl_count = 1, 861 .ctrl_count = 1,
862 .ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY,
861 .clock = { 863 .clock = {
862 .freq = PCAN_USB_CRYSTAL_HZ / 2 , 864 .freq = PCAN_USB_CRYSTAL_HZ / 2 ,
863 }, 865 },
diff --git a/drivers/net/can/usb/peak_usb/pcan_usb_core.c b/drivers/net/can/usb/peak_usb/pcan_usb_core.c
index c62f48a1161d..7921cff93a63 100644
--- a/drivers/net/can/usb/peak_usb/pcan_usb_core.c
+++ b/drivers/net/can/usb/peak_usb/pcan_usb_core.c
@@ -37,16 +37,19 @@ MODULE_LICENSE("GPL v2");
37static struct usb_device_id peak_usb_table[] = { 37static struct usb_device_id peak_usb_table[] = {
38 {USB_DEVICE(PCAN_USB_VENDOR_ID, PCAN_USB_PRODUCT_ID)}, 38 {USB_DEVICE(PCAN_USB_VENDOR_ID, PCAN_USB_PRODUCT_ID)},
39 {USB_DEVICE(PCAN_USB_VENDOR_ID, PCAN_USBPRO_PRODUCT_ID)}, 39 {USB_DEVICE(PCAN_USB_VENDOR_ID, PCAN_USBPRO_PRODUCT_ID)},
40 {USB_DEVICE(PCAN_USB_VENDOR_ID, PCAN_USBFD_PRODUCT_ID)},
41 {USB_DEVICE(PCAN_USB_VENDOR_ID, PCAN_USBPROFD_PRODUCT_ID)},
40 {} /* Terminating entry */ 42 {} /* Terminating entry */
41}; 43};
42 44
43MODULE_DEVICE_TABLE(usb, peak_usb_table); 45MODULE_DEVICE_TABLE(usb, peak_usb_table);
44 46
45/* List of supported PCAN-USB adapters (NULL terminated list) */ 47/* List of supported PCAN-USB adapters (NULL terminated list) */
46static struct peak_usb_adapter *peak_usb_adapters_list[] = { 48static const struct peak_usb_adapter *const peak_usb_adapters_list[] = {
47 &pcan_usb, 49 &pcan_usb,
48 &pcan_usb_pro, 50 &pcan_usb_pro,
49 NULL, 51 &pcan_usb_fd,
52 &pcan_usb_pro_fd,
50}; 53};
51 54
52/* 55/*
@@ -65,7 +68,7 @@ void pcan_dump_mem(char *prompt, void *p, int l)
65 * initialize a time_ref object with usb adapter own settings 68 * initialize a time_ref object with usb adapter own settings
66 */ 69 */
67void peak_usb_init_time_ref(struct peak_time_ref *time_ref, 70void peak_usb_init_time_ref(struct peak_time_ref *time_ref,
68 struct peak_usb_adapter *adapter) 71 const struct peak_usb_adapter *adapter)
69{ 72{
70 if (time_ref) { 73 if (time_ref) {
71 memset(time_ref, 0, sizeof(struct peak_time_ref)); 74 memset(time_ref, 0, sizeof(struct peak_time_ref));
@@ -165,6 +168,21 @@ void peak_usb_get_ts_tv(struct peak_time_ref *time_ref, u32 ts,
165} 168}
166 169
167/* 170/*
171 * post received skb after having set any hw timestamp
172 */
173int peak_usb_netif_rx(struct sk_buff *skb,
174 struct peak_time_ref *time_ref, u32 ts_low, u32 ts_high)
175{
176 struct skb_shared_hwtstamps *hwts = skb_hwtstamps(skb);
177 struct timeval tv;
178
179 peak_usb_get_ts_tv(time_ref, ts_low, &tv);
180 hwts->hwtstamp = timeval_to_ktime(tv);
181
182 return netif_rx(skb);
183}
184
185/*
168 * callback for bulk Rx urb 186 * callback for bulk Rx urb
169 */ 187 */
170static void peak_usb_read_bulk_callback(struct urb *urb) 188static void peak_usb_read_bulk_callback(struct urb *urb)
@@ -253,7 +271,7 @@ static void peak_usb_write_bulk_callback(struct urb *urb)
253 case 0: 271 case 0:
254 /* transmission complete */ 272 /* transmission complete */
255 netdev->stats.tx_packets++; 273 netdev->stats.tx_packets++;
256 netdev->stats.tx_bytes += context->dlc; 274 netdev->stats.tx_bytes += context->data_len;
257 275
258 /* prevent tx timeout */ 276 /* prevent tx timeout */
259 netdev->trans_start = jiffies; 277 netdev->trans_start = jiffies;
@@ -289,7 +307,7 @@ static netdev_tx_t peak_usb_ndo_start_xmit(struct sk_buff *skb,
289 struct peak_usb_device *dev = netdev_priv(netdev); 307 struct peak_usb_device *dev = netdev_priv(netdev);
290 struct peak_tx_urb_context *context = NULL; 308 struct peak_tx_urb_context *context = NULL;
291 struct net_device_stats *stats = &netdev->stats; 309 struct net_device_stats *stats = &netdev->stats;
292 struct can_frame *cf = (struct can_frame *)skb->data; 310 struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
293 struct urb *urb; 311 struct urb *urb;
294 u8 *obuf; 312 u8 *obuf;
295 int i, err; 313 int i, err;
@@ -322,7 +340,9 @@ static netdev_tx_t peak_usb_ndo_start_xmit(struct sk_buff *skb,
322 } 340 }
323 341
324 context->echo_index = i; 342 context->echo_index = i;
325 context->dlc = cf->can_dlc; 343
344 /* Note: this works with CANFD frames too */
345 context->data_len = cfd->len;
326 346
327 usb_anchor_urb(urb, &dev->tx_submitted); 347 usb_anchor_urb(urb, &dev->tx_submitted);
328 348
@@ -679,19 +699,43 @@ static int peak_usb_set_mode(struct net_device *netdev, enum can_mode mode)
679} 699}
680 700
681/* 701/*
682 * candev callback used to set device bitrate. 702 * candev callback used to set device nominal/arbitration bitrate.
683 */ 703 */
684static int peak_usb_set_bittiming(struct net_device *netdev) 704static int peak_usb_set_bittiming(struct net_device *netdev)
685{ 705{
686 struct peak_usb_device *dev = netdev_priv(netdev); 706 struct peak_usb_device *dev = netdev_priv(netdev);
687 struct can_bittiming *bt = &dev->can.bittiming; 707 const struct peak_usb_adapter *pa = dev->adapter;
688 708
689 if (dev->adapter->dev_set_bittiming) { 709 if (pa->dev_set_bittiming) {
690 int err = dev->adapter->dev_set_bittiming(dev, bt); 710 struct can_bittiming *bt = &dev->can.bittiming;
711 int err = pa->dev_set_bittiming(dev, bt);
691 712
692 if (err) 713 if (err)
693 netdev_info(netdev, "couldn't set bitrate (err %d)\n", 714 netdev_info(netdev, "couldn't set bitrate (err %d)\n",
694 err); 715 err);
716 return err;
717 }
718
719 return 0;
720}
721
722/*
723 * candev callback used to set device data bitrate.
724 */
725static int peak_usb_set_data_bittiming(struct net_device *netdev)
726{
727 struct peak_usb_device *dev = netdev_priv(netdev);
728 const struct peak_usb_adapter *pa = dev->adapter;
729
730 if (pa->dev_set_data_bittiming) {
731 struct can_bittiming *bt = &dev->can.data_bittiming;
732 int err = pa->dev_set_data_bittiming(dev, bt);
733
734 if (err)
735 netdev_info(netdev,
736 "couldn't set data bitrate (err %d)\n",
737 err);
738
695 return err; 739 return err;
696 } 740 }
697 741
@@ -709,7 +753,7 @@ static const struct net_device_ops peak_usb_netdev_ops = {
709 * create one device which is attached to CAN controller #ctrl_idx of the 753 * create one device which is attached to CAN controller #ctrl_idx of the
710 * usb adapter. 754 * usb adapter.
711 */ 755 */
712static int peak_usb_create_dev(struct peak_usb_adapter *peak_usb_adapter, 756static int peak_usb_create_dev(const struct peak_usb_adapter *peak_usb_adapter,
713 struct usb_interface *intf, int ctrl_idx) 757 struct usb_interface *intf, int ctrl_idx)
714{ 758{
715 struct usb_device *usb_dev = interface_to_usbdev(intf); 759 struct usb_device *usb_dev = interface_to_usbdev(intf);
@@ -750,9 +794,11 @@ static int peak_usb_create_dev(struct peak_usb_adapter *peak_usb_adapter,
750 dev->can.clock = peak_usb_adapter->clock; 794 dev->can.clock = peak_usb_adapter->clock;
751 dev->can.bittiming_const = &peak_usb_adapter->bittiming_const; 795 dev->can.bittiming_const = &peak_usb_adapter->bittiming_const;
752 dev->can.do_set_bittiming = peak_usb_set_bittiming; 796 dev->can.do_set_bittiming = peak_usb_set_bittiming;
797 dev->can.data_bittiming_const = &peak_usb_adapter->data_bittiming_const;
798 dev->can.do_set_data_bittiming = peak_usb_set_data_bittiming;
753 dev->can.do_set_mode = peak_usb_set_mode; 799 dev->can.do_set_mode = peak_usb_set_mode;
754 dev->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES | 800 dev->can.do_get_berr_counter = peak_usb_adapter->do_get_berr_counter;
755 CAN_CTRLMODE_LISTENONLY; 801 dev->can.ctrlmode_supported = peak_usb_adapter->ctrlmode_supported;
756 802
757 netdev->netdev_ops = &peak_usb_netdev_ops; 803 netdev->netdev_ops = &peak_usb_netdev_ops;
758 804
@@ -857,17 +903,18 @@ static int peak_usb_probe(struct usb_interface *intf,
857{ 903{
858 struct usb_device *usb_dev = interface_to_usbdev(intf); 904 struct usb_device *usb_dev = interface_to_usbdev(intf);
859 const u16 usb_id_product = le16_to_cpu(usb_dev->descriptor.idProduct); 905 const u16 usb_id_product = le16_to_cpu(usb_dev->descriptor.idProduct);
860 struct peak_usb_adapter *peak_usb_adapter, **pp; 906 const struct peak_usb_adapter *peak_usb_adapter = NULL;
861 int i, err = -ENOMEM; 907 int i, err = -ENOMEM;
862 908
863 usb_dev = interface_to_usbdev(intf); 909 usb_dev = interface_to_usbdev(intf);
864 910
865 /* get corresponding PCAN-USB adapter */ 911 /* get corresponding PCAN-USB adapter */
866 for (pp = peak_usb_adapters_list; *pp; pp++) 912 for (i = 0; i < ARRAY_SIZE(peak_usb_adapters_list); i++)
867 if ((*pp)->device_id == usb_id_product) 913 if (peak_usb_adapters_list[i]->device_id == usb_id_product) {
914 peak_usb_adapter = peak_usb_adapters_list[i];
868 break; 915 break;
916 }
869 917
870 peak_usb_adapter = *pp;
871 if (!peak_usb_adapter) { 918 if (!peak_usb_adapter) {
872 /* should never come except device_id bad usage in this file */ 919 /* should never come except device_id bad usage in this file */
873 pr_err("%s: didn't find device id. 0x%x in devices list\n", 920 pr_err("%s: didn't find device id. 0x%x in devices list\n",
diff --git a/drivers/net/can/usb/peak_usb/pcan_usb_core.h b/drivers/net/can/usb/peak_usb/pcan_usb_core.h
index 073b47ff8eee..9e624f05ad4d 100644
--- a/drivers/net/can/usb/peak_usb/pcan_usb_core.h
+++ b/drivers/net/can/usb/peak_usb/pcan_usb_core.h
@@ -25,6 +25,8 @@
25/* supported device ids. */ 25/* supported device ids. */
26#define PCAN_USB_PRODUCT_ID 0x000c 26#define PCAN_USB_PRODUCT_ID 0x000c
27#define PCAN_USBPRO_PRODUCT_ID 0x000d 27#define PCAN_USBPRO_PRODUCT_ID 0x000d
28#define PCAN_USBPROFD_PRODUCT_ID 0x0011
29#define PCAN_USBFD_PRODUCT_ID 0x0012
28 30
29#define PCAN_USB_DRIVER_NAME "peak_usb" 31#define PCAN_USB_DRIVER_NAME "peak_usb"
30 32
@@ -44,8 +46,10 @@ struct peak_usb_device;
44struct peak_usb_adapter { 46struct peak_usb_adapter {
45 char *name; 47 char *name;
46 u32 device_id; 48 u32 device_id;
49 u32 ctrlmode_supported;
47 struct can_clock clock; 50 struct can_clock clock;
48 const struct can_bittiming_const bittiming_const; 51 const struct can_bittiming_const bittiming_const;
52 const struct can_bittiming_const data_bittiming_const;
49 unsigned int ctrl_count; 53 unsigned int ctrl_count;
50 54
51 int (*intf_probe)(struct usb_interface *intf); 55 int (*intf_probe)(struct usb_interface *intf);
@@ -57,6 +61,8 @@ struct peak_usb_adapter {
57 int (*dev_close)(struct peak_usb_device *dev); 61 int (*dev_close)(struct peak_usb_device *dev);
58 int (*dev_set_bittiming)(struct peak_usb_device *dev, 62 int (*dev_set_bittiming)(struct peak_usb_device *dev,
59 struct can_bittiming *bt); 63 struct can_bittiming *bt);
64 int (*dev_set_data_bittiming)(struct peak_usb_device *dev,
65 struct can_bittiming *bt);
60 int (*dev_set_bus)(struct peak_usb_device *dev, u8 onoff); 66 int (*dev_set_bus)(struct peak_usb_device *dev, u8 onoff);
61 int (*dev_get_device_id)(struct peak_usb_device *dev, u32 *device_id); 67 int (*dev_get_device_id)(struct peak_usb_device *dev, u32 *device_id);
62 int (*dev_decode_buf)(struct peak_usb_device *dev, struct urb *urb); 68 int (*dev_decode_buf)(struct peak_usb_device *dev, struct urb *urb);
@@ -66,6 +72,8 @@ struct peak_usb_adapter {
66 int (*dev_stop)(struct peak_usb_device *dev); 72 int (*dev_stop)(struct peak_usb_device *dev);
67 int (*dev_restart_async)(struct peak_usb_device *dev, struct urb *urb, 73 int (*dev_restart_async)(struct peak_usb_device *dev, struct urb *urb,
68 u8 *buf); 74 u8 *buf);
75 int (*do_get_berr_counter)(const struct net_device *netdev,
76 struct can_berr_counter *bec);
69 u8 ep_msg_in; 77 u8 ep_msg_in;
70 u8 ep_msg_out[PCAN_USB_MAX_CHANNEL]; 78 u8 ep_msg_out[PCAN_USB_MAX_CHANNEL];
71 u8 ts_used_bits; 79 u8 ts_used_bits;
@@ -78,21 +86,23 @@ struct peak_usb_adapter {
78 int sizeof_dev_private; 86 int sizeof_dev_private;
79}; 87};
80 88
81extern struct peak_usb_adapter pcan_usb; 89extern const struct peak_usb_adapter pcan_usb;
82extern struct peak_usb_adapter pcan_usb_pro; 90extern const struct peak_usb_adapter pcan_usb_pro;
91extern const struct peak_usb_adapter pcan_usb_fd;
92extern const struct peak_usb_adapter pcan_usb_pro_fd;
83 93
84struct peak_time_ref { 94struct peak_time_ref {
85 struct timeval tv_host_0, tv_host; 95 struct timeval tv_host_0, tv_host;
86 u32 ts_dev_1, ts_dev_2; 96 u32 ts_dev_1, ts_dev_2;
87 u64 ts_total; 97 u64 ts_total;
88 u32 tick_count; 98 u32 tick_count;
89 struct peak_usb_adapter *adapter; 99 const struct peak_usb_adapter *adapter;
90}; 100};
91 101
92struct peak_tx_urb_context { 102struct peak_tx_urb_context {
93 struct peak_usb_device *dev; 103 struct peak_usb_device *dev;
94 u32 echo_index; 104 u32 echo_index;
95 u8 dlc; 105 u8 data_len;
96 struct urb *urb; 106 struct urb *urb;
97}; 107};
98 108
@@ -102,7 +112,7 @@ struct peak_tx_urb_context {
102/* PEAK-System USB device */ 112/* PEAK-System USB device */
103struct peak_usb_device { 113struct peak_usb_device {
104 struct can_priv can; 114 struct can_priv can;
105 struct peak_usb_adapter *adapter; 115 const struct peak_usb_adapter *adapter;
106 unsigned int ctrl_idx; 116 unsigned int ctrl_idx;
107 u32 state; 117 u32 state;
108 118
@@ -134,12 +144,14 @@ void pcan_dump_mem(char *prompt, void *p, int l);
134 144
135/* common timestamp management */ 145/* common timestamp management */
136void peak_usb_init_time_ref(struct peak_time_ref *time_ref, 146void peak_usb_init_time_ref(struct peak_time_ref *time_ref,
137 struct peak_usb_adapter *adapter); 147 const struct peak_usb_adapter *adapter);
138void peak_usb_update_ts_now(struct peak_time_ref *time_ref, u32 ts_now); 148void peak_usb_update_ts_now(struct peak_time_ref *time_ref, u32 ts_now);
139void peak_usb_set_ts_now(struct peak_time_ref *time_ref, u32 ts_now); 149void peak_usb_set_ts_now(struct peak_time_ref *time_ref, u32 ts_now);
140void peak_usb_get_ts_tv(struct peak_time_ref *time_ref, u32 ts, 150void peak_usb_get_ts_tv(struct peak_time_ref *time_ref, u32 ts,
141 struct timeval *tv); 151 struct timeval *tv);
142 152int peak_usb_netif_rx(struct sk_buff *skb,
153 struct peak_time_ref *time_ref, u32 ts_low, u32 ts_high);
143void peak_usb_async_complete(struct urb *urb); 154void peak_usb_async_complete(struct urb *urb);
144void peak_usb_restart_complete(struct peak_usb_device *dev); 155void peak_usb_restart_complete(struct peak_usb_device *dev);
156
145#endif 157#endif
diff --git a/drivers/net/can/usb/peak_usb/pcan_usb_fd.c b/drivers/net/can/usb/peak_usb/pcan_usb_fd.c
new file mode 100644
index 000000000000..962c3f027383
--- /dev/null
+++ b/drivers/net/can/usb/peak_usb/pcan_usb_fd.c
@@ -0,0 +1,1095 @@
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
23#include "pcan_usb_core.h"
24#include "pcan_usb_pro.h"
25#include "pcan_ucan.h"
26
27MODULE_SUPPORTED_DEVICE("PEAK-System PCAN-USB FD adapter");
28MODULE_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 */
47struct __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 */
60struct 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 */
69struct 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
89struct __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
106struct __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_RX_FRAME_xxxABLE for PCAN-USB Pro FD */
114#define PCAN_UFD_FLTEXT_CALIBRATION 0x8000
115
116struct __packed pcan_ufd_filter_ext {
117 __le16 opcode_channel;
118
119 __le16 ext_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
127struct __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
140struct __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
149static 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 */
155static 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 */
165static inline
166struct 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 */
174static 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 */
182static 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;
186 u8 *packet_ptr;
187 int i, n = 1, 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
205 /* firmware is not able to re-assemble 512 bytes buffer in full-speed */
206 if ((dev->udev->speed != USB_SPEED_HIGH) &&
207 (cmd_len > PCAN_UFD_LOSPD_PKT_SIZE)) {
208 packet_len = PCAN_UFD_LOSPD_PKT_SIZE;
209 n += cmd_len / packet_len;
210 } else {
211 packet_len = cmd_len;
212 }
213
214 for (i = 0; i < n; i++) {
215 err = usb_bulk_msg(dev->udev,
216 usb_sndbulkpipe(dev->udev,
217 PCAN_USBPRO_EP_CMDOUT),
218 packet_ptr, packet_len,
219 NULL, PCAN_UFD_CMD_TIMEOUT_MS);
220 if (err) {
221 netdev_err(dev->netdev,
222 "sending command failure: %d\n", err);
223 break;
224 }
225
226 packet_ptr += packet_len;
227 }
228
229 return err;
230}
231
232/* build the commands list in the given buffer, to enter operational mode */
233static int pcan_usb_fd_build_restart_cmd(struct peak_usb_device *dev, u8 *buf)
234{
235 struct pucan_wr_err_cnt *prc;
236 struct pucan_command *cmd;
237 u8 *pc = buf;
238
239 /* 1st, reset error counters: */
240 prc = (struct pucan_wr_err_cnt *)pc;
241 prc->opcode_channel = pucan_cmd_opcode_channel(dev,
242 PUCAN_CMD_WR_ERR_CNT);
243
244 /* select both counters */
245 prc->sel_mask = cpu_to_le16(PUCAN_WRERRCNT_TE|PUCAN_WRERRCNT_RE);
246
247 /* and reset their values */
248 prc->tx_counter = 0;
249 prc->rx_counter = 0;
250
251 /* moves the pointer forward */
252 pc += sizeof(struct pucan_wr_err_cnt);
253
254 /* next, go back to operational mode */
255 cmd = (struct pucan_command *)pc;
256 cmd->opcode_channel = pucan_cmd_opcode_channel(dev,
257 (dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) ?
258 PUCAN_CMD_LISTEN_ONLY_MODE :
259 PUCAN_CMD_NORMAL_MODE);
260 pc += sizeof(struct pucan_command);
261
262 return pc - buf;
263}
264
265/* set CAN bus on/off */
266static int pcan_usb_fd_set_bus(struct peak_usb_device *dev, u8 onoff)
267{
268 u8 *pc = pcan_usb_fd_cmd_buffer(dev);
269 int l;
270
271 if (onoff) {
272 /* build the cmds list to enter operational mode */
273 l = pcan_usb_fd_build_restart_cmd(dev, pc);
274 } else {
275 struct pucan_command *cmd = (struct pucan_command *)pc;
276
277 /* build cmd to go back to reset mode */
278 cmd->opcode_channel = pucan_cmd_opcode_channel(dev,
279 PUCAN_CMD_RESET_MODE);
280 l = sizeof(struct pucan_command);
281 }
282
283 /* send the command */
284 return pcan_usb_fd_send_cmd(dev, pc + l);
285}
286
287/* set filtering masks:
288 *
289 * idx in range [0..63] selects a row #idx, all rows otherwise
290 * mask in range [0..0xffffffff] defines up to 32 CANIDs in the row(s)
291 *
292 * Each bit of this 64 x 32 bits array defines a CANID value:
293 *
294 * bit[i,j] = 1 implies that CANID=(i x 32)+j will be received, while
295 * bit[i,j] = 0 implies that CANID=(i x 32)+j will be discarded.
296 */
297static int pcan_usb_fd_set_filter_std(struct peak_usb_device *dev, int idx,
298 u32 mask)
299{
300 struct pucan_filter_std *cmd = pcan_usb_fd_cmd_buffer(dev);
301 int i, n;
302
303 /* select all rows when idx is out of range [0..63] */
304 if ((idx < 0) || (idx >= (1 << PUCAN_FLTSTD_ROW_IDX_BITS))) {
305 n = 1 << PUCAN_FLTSTD_ROW_IDX_BITS;
306 idx = 0;
307
308 /* select the row (and only the row) otherwise */
309 } else {
310 n = idx + 1;
311 }
312
313 for (i = idx; i < n; i++, cmd++) {
314 cmd->opcode_channel = pucan_cmd_opcode_channel(dev,
315 PUCAN_CMD_FILTER_STD);
316 cmd->idx = cpu_to_le16(i);
317 cmd->mask = cpu_to_le32(mask);
318 }
319
320 /* send the command */
321 return pcan_usb_fd_send_cmd(dev, cmd);
322}
323
324/* set/unset notifications filter:
325 *
326 * onoff sets(1)/unset(0) notifications
327 * mask each bit defines a kind of notification to set/unset
328 */
329static int pcan_usb_fd_set_filter_ext(struct peak_usb_device *dev,
330 bool onoff, u16 ext_mask, u16 usb_mask)
331{
332 struct pcan_ufd_filter_ext *cmd = pcan_usb_fd_cmd_buffer(dev);
333
334 cmd->opcode_channel = pucan_cmd_opcode_channel(dev,
335 (onoff) ? PUCAN_CMD_RX_FRAME_ENABLE :
336 PUCAN_CMD_RX_FRAME_DISABLE);
337
338 cmd->ext_mask = cpu_to_le16(ext_mask);
339 cmd->usb_mask = cpu_to_le16(usb_mask);
340
341 /* send the command */
342 return pcan_usb_fd_send_cmd(dev, ++cmd);
343}
344
345/* setup LED control */
346static int pcan_usb_fd_set_can_led(struct peak_usb_device *dev, u8 led_mode)
347{
348 struct pcan_ufd_led *cmd = pcan_usb_fd_cmd_buffer(dev);
349
350 cmd->opcode_channel = pucan_cmd_opcode_channel(dev,
351 PCAN_UFD_CMD_LED_SET);
352 cmd->mode = led_mode;
353
354 /* send the command */
355 return pcan_usb_fd_send_cmd(dev, ++cmd);
356}
357
358/* set CAN clock domain */
359static int pcan_usb_fd_set_clock_domain(struct peak_usb_device *dev,
360 u8 clk_mode)
361{
362 struct pcan_ufd_clock *cmd = pcan_usb_fd_cmd_buffer(dev);
363
364 cmd->opcode_channel = pucan_cmd_opcode_channel(dev,
365 PCAN_UFD_CMD_CLK_SET);
366 cmd->mode = clk_mode;
367
368 /* send the command */
369 return pcan_usb_fd_send_cmd(dev, ++cmd);
370}
371
372/* set bittiming for CAN and CAN-FD header */
373static int pcan_usb_fd_set_bittiming_slow(struct peak_usb_device *dev,
374 struct can_bittiming *bt)
375{
376 struct pucan_timing_slow *cmd = pcan_usb_fd_cmd_buffer(dev);
377
378 cmd->opcode_channel = pucan_cmd_opcode_channel(dev,
379 PUCAN_CMD_TIMING_SLOW);
380 cmd->sjw_t = PUCAN_TSLOW_SJW_T(bt->sjw - 1,
381 dev->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES);
382
383 cmd->tseg2 = PUCAN_TSLOW_TSEG2(bt->phase_seg2 - 1);
384 cmd->tseg1 = PUCAN_TSLOW_TSEG1(bt->prop_seg + bt->phase_seg1 - 1);
385 cmd->brp = cpu_to_le16(PUCAN_TSLOW_BRP(bt->brp - 1));
386
387 cmd->ewl = 96; /* default */
388
389 /* send the command */
390 return pcan_usb_fd_send_cmd(dev, ++cmd);
391}
392
393/* set CAN-FD bittiming for data */
394static int pcan_usb_fd_set_bittiming_fast(struct peak_usb_device *dev,
395 struct can_bittiming *bt)
396{
397 struct pucan_timing_fast *cmd = pcan_usb_fd_cmd_buffer(dev);
398
399 cmd->opcode_channel = pucan_cmd_opcode_channel(dev,
400 PUCAN_CMD_TIMING_FAST);
401 cmd->sjw = PUCAN_TFAST_SJW(bt->sjw - 1);
402 cmd->tseg2 = PUCAN_TFAST_TSEG2(bt->phase_seg2 - 1);
403 cmd->tseg1 = PUCAN_TFAST_TSEG1(bt->prop_seg + bt->phase_seg1 - 1);
404 cmd->brp = cpu_to_le16(PUCAN_TFAST_BRP(bt->brp - 1));
405
406 /* send the command */
407 return pcan_usb_fd_send_cmd(dev, ++cmd);
408}
409
410/* handle restart but in asynchronously way
411 * (uses PCAN-USB Pro code to complete asynchronous request)
412 */
413static int pcan_usb_fd_restart_async(struct peak_usb_device *dev,
414 struct urb *urb, u8 *buf)
415{
416 u8 *pc = buf;
417
418 /* build the entire cmds list in the provided buffer, to go back into
419 * operational mode.
420 */
421 pc += pcan_usb_fd_build_restart_cmd(dev, pc);
422
423 /* add EOC */
424 memset(pc, 0xff, sizeof(struct pucan_command));
425 pc += sizeof(struct pucan_command);
426
427 /* complete the URB */
428 usb_fill_bulk_urb(urb, dev->udev,
429 usb_sndbulkpipe(dev->udev, PCAN_USBPRO_EP_CMDOUT),
430 buf, pc - buf,
431 pcan_usb_pro_restart_complete, dev);
432
433 /* and submit it. */
434 return usb_submit_urb(urb, GFP_ATOMIC);
435}
436
437static int pcan_usb_fd_drv_loaded(struct peak_usb_device *dev, bool loaded)
438{
439 struct pcan_usb_fd_device *pdev =
440 container_of(dev, struct pcan_usb_fd_device, dev);
441
442 pdev->cmd_buffer_addr[0] = 0;
443 pdev->cmd_buffer_addr[1] = !!loaded;
444
445 return pcan_usb_pro_send_req(dev,
446 PCAN_USBPRO_REQ_FCT,
447 PCAN_USBPRO_FCT_DRVLD,
448 pdev->cmd_buffer_addr,
449 PCAN_USBPRO_FCT_DRVLD_REQ_LEN);
450}
451
452static int pcan_usb_fd_decode_canmsg(struct pcan_usb_fd_if *usb_if,
453 struct pucan_msg *rx_msg)
454{
455 struct pucan_rx_msg *rm = (struct pucan_rx_msg *)rx_msg;
456 struct peak_usb_device *dev = usb_if->dev[pucan_msg_get_channel(rm)];
457 struct net_device *netdev = dev->netdev;
458 struct canfd_frame *cfd;
459 struct sk_buff *skb;
460 const u16 rx_msg_flags = le16_to_cpu(rm->flags);
461
462 if (rx_msg_flags & PUCAN_MSG_EXT_DATA_LEN) {
463 /* CANFD frame case */
464 skb = alloc_canfd_skb(netdev, &cfd);
465 if (!skb)
466 return -ENOMEM;
467
468 if (rx_msg_flags & PUCAN_MSG_BITRATE_SWITCH)
469 cfd->flags |= CANFD_BRS;
470
471 if (rx_msg_flags & PUCAN_MSG_ERROR_STATE_IND)
472 cfd->flags |= CANFD_ESI;
473
474 cfd->len = can_dlc2len(get_canfd_dlc(pucan_msg_get_dlc(rm)));
475 } else {
476 /* CAN 2.0 frame case */
477 skb = alloc_can_skb(netdev, (struct can_frame **)&cfd);
478 if (!skb)
479 return -ENOMEM;
480
481 cfd->len = get_can_dlc(pucan_msg_get_dlc(rm));
482 }
483
484 cfd->can_id = le32_to_cpu(rm->can_id);
485
486 if (rx_msg_flags & PUCAN_MSG_EXT_ID)
487 cfd->can_id |= CAN_EFF_FLAG;
488
489 if (rx_msg_flags & PUCAN_MSG_RTR)
490 cfd->can_id |= CAN_RTR_FLAG;
491 else
492 memcpy(cfd->data, rm->d, cfd->len);
493
494 peak_usb_netif_rx(skb, &usb_if->time_ref,
495 le32_to_cpu(rm->ts_low), le32_to_cpu(rm->ts_high));
496
497 netdev->stats.rx_packets++;
498 netdev->stats.rx_bytes += cfd->len;
499
500 return 0;
501}
502
503/* handle uCAN status message */
504static int pcan_usb_fd_decode_status(struct pcan_usb_fd_if *usb_if,
505 struct pucan_msg *rx_msg)
506{
507 struct pucan_status_msg *sm = (struct pucan_status_msg *)rx_msg;
508 struct peak_usb_device *dev = usb_if->dev[pucan_stmsg_get_channel(sm)];
509 struct pcan_usb_fd_device *pdev =
510 container_of(dev, struct pcan_usb_fd_device, dev);
511 enum can_state new_state = CAN_STATE_ERROR_ACTIVE;
512 enum can_state rx_state, tx_state;
513 struct net_device *netdev = dev->netdev;
514 struct can_frame *cf;
515 struct sk_buff *skb;
516
517 /* nothing should be sent while in BUS_OFF state */
518 if (dev->can.state == CAN_STATE_BUS_OFF)
519 return 0;
520
521 if (sm->channel_p_w_b & PUCAN_BUS_BUSOFF) {
522 new_state = CAN_STATE_BUS_OFF;
523 } else if (sm->channel_p_w_b & PUCAN_BUS_PASSIVE) {
524 new_state = CAN_STATE_ERROR_PASSIVE;
525 } else if (sm->channel_p_w_b & PUCAN_BUS_WARNING) {
526 new_state = CAN_STATE_ERROR_WARNING;
527 } else {
528 /* no error bit (so, no error skb, back to active state) */
529 dev->can.state = CAN_STATE_ERROR_ACTIVE;
530 pdev->bec.txerr = 0;
531 pdev->bec.rxerr = 0;
532 return 0;
533 }
534
535 /* state hasn't changed */
536 if (new_state == dev->can.state)
537 return 0;
538
539 /* handle bus state change */
540 tx_state = (pdev->bec.txerr >= pdev->bec.rxerr) ? new_state : 0;
541 rx_state = (pdev->bec.txerr <= pdev->bec.rxerr) ? new_state : 0;
542
543 /* allocate an skb to store the error frame */
544 skb = alloc_can_err_skb(netdev, &cf);
545 if (skb)
546 can_change_state(netdev, cf, tx_state, rx_state);
547
548 /* things must be done even in case of OOM */
549 if (new_state == CAN_STATE_BUS_OFF)
550 can_bus_off(netdev);
551
552 if (!skb)
553 return -ENOMEM;
554
555 peak_usb_netif_rx(skb, &usb_if->time_ref,
556 le32_to_cpu(sm->ts_low), le32_to_cpu(sm->ts_high));
557
558 netdev->stats.rx_packets++;
559 netdev->stats.rx_bytes += cf->can_dlc;
560
561 return 0;
562}
563
564/* handle uCAN error message */
565static int pcan_usb_fd_decode_error(struct pcan_usb_fd_if *usb_if,
566 struct pucan_msg *rx_msg)
567{
568 struct pucan_error_msg *er = (struct pucan_error_msg *)rx_msg;
569 struct peak_usb_device *dev = usb_if->dev[pucan_ermsg_get_channel(er)];
570 struct pcan_usb_fd_device *pdev =
571 container_of(dev, struct pcan_usb_fd_device, dev);
572
573 /* keep a trace of tx and rx error counters for later use */
574 pdev->bec.txerr = er->tx_err_cnt;
575 pdev->bec.rxerr = er->rx_err_cnt;
576
577 return 0;
578}
579
580/* handle uCAN overrun message */
581static int pcan_usb_fd_decode_overrun(struct pcan_usb_fd_if *usb_if,
582 struct pucan_msg *rx_msg)
583{
584 struct pcan_ufd_ovr_msg *ov = (struct pcan_ufd_ovr_msg *)rx_msg;
585 struct peak_usb_device *dev = usb_if->dev[pufd_omsg_get_channel(ov)];
586 struct net_device *netdev = dev->netdev;
587 struct can_frame *cf;
588 struct sk_buff *skb;
589
590 /* allocate an skb to store the error frame */
591 skb = alloc_can_err_skb(netdev, &cf);
592 if (!skb)
593 return -ENOMEM;
594
595 cf->can_id |= CAN_ERR_CRTL;
596 cf->data[1] |= CAN_ERR_CRTL_RX_OVERFLOW;
597
598 peak_usb_netif_rx(skb, &usb_if->time_ref,
599 le32_to_cpu(ov->ts_low), le32_to_cpu(ov->ts_high));
600
601 netdev->stats.rx_over_errors++;
602 netdev->stats.rx_errors++;
603
604 return 0;
605}
606
607/* handle USB calibration message */
608static void pcan_usb_fd_decode_ts(struct pcan_usb_fd_if *usb_if,
609 struct pucan_msg *rx_msg)
610{
611 struct pcan_ufd_ts_msg *ts = (struct pcan_ufd_ts_msg *)rx_msg;
612
613 /* should wait until clock is stabilized */
614 if (usb_if->cm_ignore_count > 0)
615 usb_if->cm_ignore_count--;
616 else
617 peak_usb_set_ts_now(&usb_if->time_ref, le32_to_cpu(ts->ts_low));
618}
619
620/* callback for bulk IN urb */
621static int pcan_usb_fd_decode_buf(struct peak_usb_device *dev, struct urb *urb)
622{
623 struct pcan_usb_fd_if *usb_if = pcan_usb_fd_dev_if(dev);
624 struct net_device *netdev = dev->netdev;
625 struct pucan_msg *rx_msg;
626 u8 *msg_ptr, *msg_end;
627 int err = 0;
628
629 /* loop reading all the records from the incoming message */
630 msg_ptr = urb->transfer_buffer;
631 msg_end = urb->transfer_buffer + urb->actual_length;
632 for (; msg_ptr < msg_end;) {
633 u16 rx_msg_type, rx_msg_size;
634
635 rx_msg = (struct pucan_msg *)msg_ptr;
636 if (!rx_msg->size) {
637 /* null packet found: end of list */
638 break;
639 }
640
641 rx_msg_size = le16_to_cpu(rx_msg->size);
642 rx_msg_type = le16_to_cpu(rx_msg->type);
643
644 /* check if the record goes out of current packet */
645 if (msg_ptr + rx_msg_size > msg_end) {
646 netdev_err(netdev,
647 "got frag rec: should inc usb rx buf sze\n");
648 err = -EBADMSG;
649 break;
650 }
651
652 switch (rx_msg_type) {
653 case PUCAN_MSG_CAN_RX:
654 err = pcan_usb_fd_decode_canmsg(usb_if, rx_msg);
655 if (err < 0)
656 goto fail;
657 break;
658
659 case PCAN_UFD_MSG_CALIBRATION:
660 pcan_usb_fd_decode_ts(usb_if, rx_msg);
661 break;
662
663 case PUCAN_MSG_ERROR:
664 err = pcan_usb_fd_decode_error(usb_if, rx_msg);
665 if (err < 0)
666 goto fail;
667 break;
668
669 case PUCAN_MSG_STATUS:
670 err = pcan_usb_fd_decode_status(usb_if, rx_msg);
671 if (err < 0)
672 goto fail;
673 break;
674
675 case PCAN_UFD_MSG_OVERRUN:
676 err = pcan_usb_fd_decode_overrun(usb_if, rx_msg);
677 if (err < 0)
678 goto fail;
679 break;
680
681 default:
682 netdev_err(netdev,
683 "unhandled msg type 0x%02x (%d): ignored\n",
684 rx_msg_type, rx_msg_type);
685 break;
686 }
687
688 msg_ptr += rx_msg_size;
689 }
690
691fail:
692 if (err)
693 pcan_dump_mem("received msg",
694 urb->transfer_buffer, urb->actual_length);
695 return err;
696}
697
698/* CAN/CANFD frames encoding callback */
699static int pcan_usb_fd_encode_msg(struct peak_usb_device *dev,
700 struct sk_buff *skb, u8 *obuf, size_t *size)
701{
702 struct pucan_tx_msg *tx_msg = (struct pucan_tx_msg *)obuf;
703 struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
704 u16 tx_msg_size, tx_msg_flags;
705 u8 can_dlc;
706
707 tx_msg_size = ALIGN(sizeof(struct pucan_tx_msg) + cfd->len, 4);
708 tx_msg->size = cpu_to_le16(tx_msg_size);
709 tx_msg->type = cpu_to_le16(PUCAN_MSG_CAN_TX);
710
711 tx_msg_flags = 0;
712 if (cfd->can_id & CAN_EFF_FLAG) {
713 tx_msg_flags |= PUCAN_MSG_EXT_ID;
714 tx_msg->can_id = cpu_to_le32(cfd->can_id & CAN_EFF_MASK);
715 } else {
716 tx_msg->can_id = cpu_to_le32(cfd->can_id & CAN_SFF_MASK);
717 }
718
719 if (can_is_canfd_skb(skb)) {
720 /* considering a CANFD frame */
721 can_dlc = can_len2dlc(cfd->len);
722
723 tx_msg_flags |= PUCAN_MSG_EXT_DATA_LEN;
724
725 if (cfd->flags & CANFD_BRS)
726 tx_msg_flags |= PUCAN_MSG_BITRATE_SWITCH;
727
728 if (cfd->flags & CANFD_ESI)
729 tx_msg_flags |= PUCAN_MSG_ERROR_STATE_IND;
730 } else {
731 /* CAND 2.0 frames */
732 can_dlc = cfd->len;
733
734 if (cfd->can_id & CAN_RTR_FLAG)
735 tx_msg_flags |= PUCAN_MSG_RTR;
736 }
737
738 tx_msg->flags = cpu_to_le16(tx_msg_flags);
739 tx_msg->channel_dlc = PUCAN_MSG_CHANNEL_DLC(dev->ctrl_idx, can_dlc);
740 memcpy(tx_msg->d, cfd->data, cfd->len);
741
742 /* add null size message to tag the end (messages are 32-bits aligned)
743 */
744 tx_msg = (struct pucan_tx_msg *)(obuf + tx_msg_size);
745
746 tx_msg->size = 0;
747
748 /* set the whole size of the USB packet to send */
749 *size = tx_msg_size + sizeof(u32);
750
751 return 0;
752}
753
754/* start the interface (last chance before set bus on) */
755static int pcan_usb_fd_start(struct peak_usb_device *dev)
756{
757 struct pcan_usb_fd_device *pdev =
758 container_of(dev, struct pcan_usb_fd_device, dev);
759 int err;
760
761 /* set filter mode: all acceptance */
762 err = pcan_usb_fd_set_filter_std(dev, -1, 0xffffffff);
763 if (err)
764 return err;
765
766 /* opening first device: */
767 if (pdev->usb_if->dev_opened_count == 0) {
768 /* reset time_ref */
769 peak_usb_init_time_ref(&pdev->usb_if->time_ref,
770 &pcan_usb_pro_fd);
771
772 /* enable USB calibration messages */
773 err = pcan_usb_fd_set_filter_ext(dev, 1,
774 PUCAN_FLTEXT_ERROR,
775 PCAN_UFD_FLTEXT_CALIBRATION);
776 }
777
778 pdev->usb_if->dev_opened_count++;
779
780 /* reset cached error counters */
781 pdev->bec.txerr = 0;
782 pdev->bec.rxerr = 0;
783
784 return err;
785}
786
787/* socket callback used to copy berr counters values receieved through USB */
788static int pcan_usb_fd_get_berr_counter(const struct net_device *netdev,
789 struct can_berr_counter *bec)
790{
791 struct peak_usb_device *dev = netdev_priv(netdev);
792 struct pcan_usb_fd_device *pdev =
793 container_of(dev, struct pcan_usb_fd_device, dev);
794
795 *bec = pdev->bec;
796
797 /* must return 0 */
798 return 0;
799}
800
801/* stop interface (last chance before set bus off) */
802static int pcan_usb_fd_stop(struct peak_usb_device *dev)
803{
804 struct pcan_usb_fd_device *pdev =
805 container_of(dev, struct pcan_usb_fd_device, dev);
806
807 /* turn off special msgs for that interface if no other dev opened */
808 if (pdev->usb_if->dev_opened_count == 1)
809 pcan_usb_fd_set_filter_ext(dev, 0,
810 PUCAN_FLTEXT_ERROR,
811 PCAN_UFD_FLTEXT_CALIBRATION);
812 pdev->usb_if->dev_opened_count--;
813
814 return 0;
815}
816
817/* called when probing, to initialize a device object */
818static int pcan_usb_fd_init(struct peak_usb_device *dev)
819{
820 struct pcan_usb_fd_device *pdev =
821 container_of(dev, struct pcan_usb_fd_device, dev);
822 int i, err = -ENOMEM;
823
824 /* do this for 1st channel only */
825 if (!dev->prev_siblings) {
826 /* allocate netdevices common structure attached to first one */
827 pdev->usb_if = kzalloc(sizeof(*pdev->usb_if), GFP_KERNEL);
828 if (!pdev->usb_if)
829 goto err_out;
830
831 /* allocate command buffer once for all for the interface */
832 pdev->cmd_buffer_addr = kmalloc(PCAN_UFD_CMD_BUFFER_SIZE,
833 GFP_KERNEL);
834 if (!pdev->cmd_buffer_addr)
835 goto err_out_1;
836
837 /* number of ts msgs to ignore before taking one into account */
838 pdev->usb_if->cm_ignore_count = 5;
839
840 err = pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_INFO,
841 PCAN_USBPRO_INFO_FW,
842 &pdev->usb_if->fw_info,
843 sizeof(pdev->usb_if->fw_info));
844 if (err) {
845 dev_err(dev->netdev->dev.parent,
846 "unable to read %s firmware info (err %d)\n",
847 dev->adapter->name, err);
848 goto err_out_2;
849 }
850
851 /* explicit use of dev_xxx() instead of netdev_xxx() here:
852 * information displayed are related to the device itself, not
853 * to the canx (channel) device.
854 */
855 dev_info(dev->netdev->dev.parent,
856 "PEAK-System %s v%u fw v%u.%u.%u (%u channels)\n",
857 dev->adapter->name, pdev->usb_if->fw_info.hw_version,
858 pdev->usb_if->fw_info.fw_version[0],
859 pdev->usb_if->fw_info.fw_version[1],
860 pdev->usb_if->fw_info.fw_version[2],
861 dev->adapter->ctrl_count);
862
863 /* the currently supported hw is non-ISO */
864 dev->can.ctrlmode = CAN_CTRLMODE_FD_NON_ISO;
865
866 /* tell the hardware the can driver is running */
867 err = pcan_usb_fd_drv_loaded(dev, 1);
868 if (err) {
869 dev_err(dev->netdev->dev.parent,
870 "unable to tell %s driver is loaded (err %d)\n",
871 dev->adapter->name, err);
872 goto err_out_2;
873 }
874 } else {
875 /* otherwise, simply copy previous sibling's values */
876 struct pcan_usb_fd_device *ppdev =
877 container_of(dev->prev_siblings,
878 struct pcan_usb_fd_device, dev);
879
880 pdev->usb_if = ppdev->usb_if;
881 pdev->cmd_buffer_addr = ppdev->cmd_buffer_addr;
882 }
883
884 pdev->usb_if->dev[dev->ctrl_idx] = dev;
885 dev->device_number =
886 le32_to_cpu(pdev->usb_if->fw_info.dev_id[dev->ctrl_idx]);
887
888 /* set clock domain */
889 for (i = 0; i < ARRAY_SIZE(pcan_usb_fd_clk_freq); i++)
890 if (dev->adapter->clock.freq == pcan_usb_fd_clk_freq[i])
891 break;
892
893 if (i >= ARRAY_SIZE(pcan_usb_fd_clk_freq)) {
894 dev_warn(dev->netdev->dev.parent,
895 "incompatible clock frequencies\n");
896 err = -EINVAL;
897 goto err_out_2;
898 }
899
900 pcan_usb_fd_set_clock_domain(dev, i);
901
902 /* set LED in default state (end of init phase) */
903 pcan_usb_fd_set_can_led(dev, PCAN_UFD_LED_DEF);
904
905 return 0;
906
907err_out_2:
908 kfree(pdev->cmd_buffer_addr);
909err_out_1:
910 kfree(pdev->usb_if);
911err_out:
912 return err;
913}
914
915/* called when driver module is being unloaded */
916static void pcan_usb_fd_exit(struct peak_usb_device *dev)
917{
918 struct pcan_usb_fd_device *pdev =
919 container_of(dev, struct pcan_usb_fd_device, dev);
920
921 /* when rmmod called before unplug and if down, should reset things
922 * before leaving
923 */
924 if (dev->can.state != CAN_STATE_STOPPED) {
925 /* set bus off on the corresponding channel */
926 pcan_usb_fd_set_bus(dev, 0);
927 }
928
929 /* switch off corresponding CAN LEDs */
930 pcan_usb_fd_set_can_led(dev, PCAN_UFD_LED_OFF);
931
932 /* if channel #0 (only) */
933 if (dev->ctrl_idx == 0) {
934 /* turn off calibration message if any device were opened */
935 if (pdev->usb_if->dev_opened_count > 0)
936 pcan_usb_fd_set_filter_ext(dev, 0,
937 PUCAN_FLTEXT_ERROR,
938 PCAN_UFD_FLTEXT_CALIBRATION);
939
940 /* tell USB adapter that the driver is being unloaded */
941 pcan_usb_fd_drv_loaded(dev, 0);
942 }
943}
944
945/* called when the USB adapter is unplugged */
946static void pcan_usb_fd_free(struct peak_usb_device *dev)
947{
948 /* last device: can free shared objects now */
949 if (!dev->prev_siblings && !dev->next_siblings) {
950 struct pcan_usb_fd_device *pdev =
951 container_of(dev, struct pcan_usb_fd_device, dev);
952
953 /* free commands buffer */
954 kfree(pdev->cmd_buffer_addr);
955
956 /* free usb interface object */
957 kfree(pdev->usb_if);
958 }
959}
960
961/* describes the PCAN-USB FD adapter */
962const struct peak_usb_adapter pcan_usb_fd = {
963 .name = "PCAN-USB FD",
964 .device_id = PCAN_USBFD_PRODUCT_ID,
965 .ctrl_count = PCAN_USBFD_CHANNEL_COUNT,
966 .ctrlmode_supported = CAN_CTRLMODE_FD |
967 CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY,
968 .clock = {
969 .freq = PCAN_UFD_CRYSTAL_HZ,
970 },
971 .bittiming_const = {
972 .name = "pcan_usb_fd",
973 .tseg1_min = 1,
974 .tseg1_max = 64,
975 .tseg2_min = 1,
976 .tseg2_max = 16,
977 .sjw_max = 16,
978 .brp_min = 1,
979 .brp_max = 1024,
980 .brp_inc = 1,
981 },
982 .data_bittiming_const = {
983 .name = "pcan_usb_fd",
984 .tseg1_min = 1,
985 .tseg1_max = 16,
986 .tseg2_min = 1,
987 .tseg2_max = 8,
988 .sjw_max = 4,
989 .brp_min = 1,
990 .brp_max = 1024,
991 .brp_inc = 1,
992 },
993
994 /* size of device private data */
995 .sizeof_dev_private = sizeof(struct pcan_usb_fd_device),
996
997 /* timestamps usage */
998 .ts_used_bits = 32,
999 .ts_period = 1000000, /* calibration period in ts. */
1000 .us_per_ts_scale = 1, /* us = (ts * scale) >> shift */
1001 .us_per_ts_shift = 0,
1002
1003 /* give here messages in/out endpoints */
1004 .ep_msg_in = PCAN_USBPRO_EP_MSGIN,
1005 .ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0},
1006
1007 /* size of rx/tx usb buffers */
1008 .rx_buffer_size = PCAN_UFD_RX_BUFFER_SIZE,
1009 .tx_buffer_size = PCAN_UFD_TX_BUFFER_SIZE,
1010
1011 /* device callbacks */
1012 .intf_probe = pcan_usb_pro_probe, /* same as PCAN-USB Pro */
1013 .dev_init = pcan_usb_fd_init,
1014
1015 .dev_exit = pcan_usb_fd_exit,
1016 .dev_free = pcan_usb_fd_free,
1017 .dev_set_bus = pcan_usb_fd_set_bus,
1018 .dev_set_bittiming = pcan_usb_fd_set_bittiming_slow,
1019 .dev_set_data_bittiming = pcan_usb_fd_set_bittiming_fast,
1020 .dev_decode_buf = pcan_usb_fd_decode_buf,
1021 .dev_start = pcan_usb_fd_start,
1022 .dev_stop = pcan_usb_fd_stop,
1023 .dev_restart_async = pcan_usb_fd_restart_async,
1024 .dev_encode_msg = pcan_usb_fd_encode_msg,
1025
1026 .do_get_berr_counter = pcan_usb_fd_get_berr_counter,
1027};
1028
1029/* describes the PCAN-USB Pro FD adapter */
1030const struct peak_usb_adapter pcan_usb_pro_fd = {
1031 .name = "PCAN-USB Pro FD",
1032 .device_id = PCAN_USBPROFD_PRODUCT_ID,
1033 .ctrl_count = PCAN_USBPROFD_CHANNEL_COUNT,
1034 .ctrlmode_supported = CAN_CTRLMODE_FD |
1035 CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY,
1036 .clock = {
1037 .freq = PCAN_UFD_CRYSTAL_HZ,
1038 },
1039 .bittiming_const = {
1040 .name = "pcan_usb_pro_fd",
1041 .tseg1_min = 1,
1042 .tseg1_max = 64,
1043 .tseg2_min = 1,
1044 .tseg2_max = 16,
1045 .sjw_max = 16,
1046 .brp_min = 1,
1047 .brp_max = 1024,
1048 .brp_inc = 1,
1049 },
1050 .data_bittiming_const = {
1051 .name = "pcan_usb_pro_fd",
1052 .tseg1_min = 1,
1053 .tseg1_max = 16,
1054 .tseg2_min = 1,
1055 .tseg2_max = 8,
1056 .sjw_max = 4,
1057 .brp_min = 1,
1058 .brp_max = 1024,
1059 .brp_inc = 1,
1060 },
1061
1062 /* size of device private data */
1063 .sizeof_dev_private = sizeof(struct pcan_usb_fd_device),
1064
1065 /* timestamps usage */
1066 .ts_used_bits = 32,
1067 .ts_period = 1000000, /* calibration period in ts. */
1068 .us_per_ts_scale = 1, /* us = (ts * scale) >> shift */
1069 .us_per_ts_shift = 0,
1070
1071 /* give here messages in/out endpoints */
1072 .ep_msg_in = PCAN_USBPRO_EP_MSGIN,
1073 .ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0, PCAN_USBPRO_EP_MSGOUT_1},
1074
1075 /* size of rx/tx usb buffers */
1076 .rx_buffer_size = PCAN_UFD_RX_BUFFER_SIZE,
1077 .tx_buffer_size = PCAN_UFD_TX_BUFFER_SIZE,
1078
1079 /* device callbacks */
1080 .intf_probe = pcan_usb_pro_probe, /* same as PCAN-USB Pro */
1081 .dev_init = pcan_usb_fd_init,
1082
1083 .dev_exit = pcan_usb_fd_exit,
1084 .dev_free = pcan_usb_fd_free,
1085 .dev_set_bus = pcan_usb_fd_set_bus,
1086 .dev_set_bittiming = pcan_usb_fd_set_bittiming_slow,
1087 .dev_set_data_bittiming = pcan_usb_fd_set_bittiming_fast,
1088 .dev_decode_buf = pcan_usb_fd_decode_buf,
1089 .dev_start = pcan_usb_fd_start,
1090 .dev_stop = pcan_usb_fd_stop,
1091 .dev_restart_async = pcan_usb_fd_restart_async,
1092 .dev_encode_msg = pcan_usb_fd_encode_msg,
1093
1094 .do_get_berr_counter = pcan_usb_fd_get_berr_counter,
1095};
diff --git a/drivers/net/can/usb/peak_usb/pcan_usb_pro.c b/drivers/net/can/usb/peak_usb/pcan_usb_pro.c
index 4cfa3b8605b1..dec51717635e 100644
--- a/drivers/net/can/usb/peak_usb/pcan_usb_pro.c
+++ b/drivers/net/can/usb/peak_usb/pcan_usb_pro.c
@@ -27,14 +27,6 @@
27 27
28MODULE_SUPPORTED_DEVICE("PEAK-System PCAN-USB Pro adapter"); 28MODULE_SUPPORTED_DEVICE("PEAK-System PCAN-USB Pro adapter");
29 29
30/* PCAN-USB Pro Endpoints */
31#define PCAN_USBPRO_EP_CMDOUT 1
32#define PCAN_USBPRO_EP_CMDIN (PCAN_USBPRO_EP_CMDOUT | USB_DIR_IN)
33#define PCAN_USBPRO_EP_MSGOUT_0 2
34#define PCAN_USBPRO_EP_MSGIN (PCAN_USBPRO_EP_MSGOUT_0 | USB_DIR_IN)
35#define PCAN_USBPRO_EP_MSGOUT_1 3
36#define PCAN_USBPRO_EP_UNUSED (PCAN_USBPRO_EP_MSGOUT_1 | USB_DIR_IN)
37
38#define PCAN_USBPRO_CHANNEL_COUNT 2 30#define PCAN_USBPRO_CHANNEL_COUNT 2
39 31
40/* PCAN-USB Pro adapter internal clock (MHz) */ 32/* PCAN-USB Pro adapter internal clock (MHz) */
@@ -322,8 +314,8 @@ static int pcan_usb_pro_wait_rsp(struct peak_usb_device *dev,
322 return (i >= PCAN_USBPRO_RSP_SUBMIT_MAX) ? -ERANGE : err; 314 return (i >= PCAN_USBPRO_RSP_SUBMIT_MAX) ? -ERANGE : err;
323} 315}
324 316
325static int pcan_usb_pro_send_req(struct peak_usb_device *dev, int req_id, 317int pcan_usb_pro_send_req(struct peak_usb_device *dev, int req_id,
326 int req_value, void *req_addr, int req_size) 318 int req_value, void *req_addr, int req_size)
327{ 319{
328 int err; 320 int err;
329 u8 req_type; 321 u8 req_type;
@@ -475,7 +467,7 @@ static int pcan_usb_pro_set_bittiming(struct peak_usb_device *dev,
475 return pcan_usb_pro_set_bitrate(dev, ccbt); 467 return pcan_usb_pro_set_bitrate(dev, ccbt);
476} 468}
477 469
478static void pcan_usb_pro_restart_complete(struct urb *urb) 470void pcan_usb_pro_restart_complete(struct urb *urb)
479{ 471{
480 /* can delete usb resources */ 472 /* can delete usb resources */
481 peak_usb_async_complete(urb); 473 peak_usb_async_complete(urb);
@@ -634,6 +626,7 @@ static int pcan_usb_pro_handle_error(struct pcan_usb_pro_interface *usb_if,
634 switch (new_state) { 626 switch (new_state) {
635 case CAN_STATE_BUS_OFF: 627 case CAN_STATE_BUS_OFF:
636 can_frame->can_id |= CAN_ERR_BUSOFF; 628 can_frame->can_id |= CAN_ERR_BUSOFF;
629 dev->can.can_stats.bus_off++;
637 can_bus_off(netdev); 630 can_bus_off(netdev);
638 break; 631 break;
639 632
@@ -977,7 +970,7 @@ static void pcan_usb_pro_free(struct peak_usb_device *dev)
977/* 970/*
978 * probe function for new PCAN-USB Pro usb interface 971 * probe function for new PCAN-USB Pro usb interface
979 */ 972 */
980static int pcan_usb_pro_probe(struct usb_interface *intf) 973int pcan_usb_pro_probe(struct usb_interface *intf)
981{ 974{
982 struct usb_host_interface *if_desc; 975 struct usb_host_interface *if_desc;
983 int i; 976 int i;
@@ -1011,10 +1004,11 @@ static int pcan_usb_pro_probe(struct usb_interface *intf)
1011/* 1004/*
1012 * describe the PCAN-USB Pro adapter 1005 * describe the PCAN-USB Pro adapter
1013 */ 1006 */
1014struct peak_usb_adapter pcan_usb_pro = { 1007const struct peak_usb_adapter pcan_usb_pro = {
1015 .name = "PCAN-USB Pro", 1008 .name = "PCAN-USB Pro",
1016 .device_id = PCAN_USBPRO_PRODUCT_ID, 1009 .device_id = PCAN_USBPRO_PRODUCT_ID,
1017 .ctrl_count = PCAN_USBPRO_CHANNEL_COUNT, 1010 .ctrl_count = PCAN_USBPRO_CHANNEL_COUNT,
1011 .ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY,
1018 .clock = { 1012 .clock = {
1019 .freq = PCAN_USBPRO_CRYSTAL_HZ, 1013 .freq = PCAN_USBPRO_CRYSTAL_HZ,
1020 }, 1014 },
diff --git a/drivers/net/can/usb/peak_usb/pcan_usb_pro.h b/drivers/net/can/usb/peak_usb/pcan_usb_pro.h
index 837cee267132..a62f7ab8980f 100644
--- a/drivers/net/can/usb/peak_usb/pcan_usb_pro.h
+++ b/drivers/net/can/usb/peak_usb/pcan_usb_pro.h
@@ -27,6 +27,14 @@
27#define PCAN_USBPRO_INFO_BL 0 27#define PCAN_USBPRO_INFO_BL 0
28#define PCAN_USBPRO_INFO_FW 1 28#define PCAN_USBPRO_INFO_FW 1
29 29
30/* PCAN-USB Pro (FD) Endpoints */
31#define PCAN_USBPRO_EP_CMDOUT 1
32#define PCAN_USBPRO_EP_CMDIN (PCAN_USBPRO_EP_CMDOUT | USB_DIR_IN)
33#define PCAN_USBPRO_EP_MSGOUT_0 2
34#define PCAN_USBPRO_EP_MSGIN (PCAN_USBPRO_EP_MSGOUT_0 | USB_DIR_IN)
35#define PCAN_USBPRO_EP_MSGOUT_1 3
36#define PCAN_USBPRO_EP_UNUSED (PCAN_USBPRO_EP_MSGOUT_1 | USB_DIR_IN)
37
30/* Vendor Request value for XXX_FCT */ 38/* Vendor Request value for XXX_FCT */
31#define PCAN_USBPRO_FCT_DRVLD 5 /* tell device driver is loaded */ 39#define PCAN_USBPRO_FCT_DRVLD 5 /* tell device driver is loaded */
32#define PCAN_USBPRO_FCT_DRVLD_REQ_LEN 16 40#define PCAN_USBPRO_FCT_DRVLD_REQ_LEN 16
@@ -176,4 +184,9 @@ union pcan_usb_pro_rec {
176 struct pcan_usb_pro_txmsg tx_msg; 184 struct pcan_usb_pro_txmsg tx_msg;
177}; 185};
178 186
187int pcan_usb_pro_probe(struct usb_interface *intf);
188int pcan_usb_pro_send_req(struct peak_usb_device *dev, int req_id,
189 int req_value, void *req_addr, int req_size);
190void pcan_usb_pro_restart_complete(struct urb *urb);
191
179#endif 192#endif
diff --git a/drivers/net/can/usb/usb_8dev.c b/drivers/net/can/usb/usb_8dev.c
index ef674ecb82f8..dd52c7a4c80d 100644
--- a/drivers/net/can/usb/usb_8dev.c
+++ b/drivers/net/can/usb/usb_8dev.c
@@ -377,6 +377,7 @@ static void usb_8dev_rx_err_msg(struct usb_8dev_priv *priv,
377 case USB_8DEV_STATUSMSG_BUSOFF: 377 case USB_8DEV_STATUSMSG_BUSOFF:
378 priv->can.state = CAN_STATE_BUS_OFF; 378 priv->can.state = CAN_STATE_BUS_OFF;
379 cf->can_id |= CAN_ERR_BUSOFF; 379 cf->can_id |= CAN_ERR_BUSOFF;
380 priv->can.can_stats.bus_off++;
380 can_bus_off(priv->netdev); 381 can_bus_off(priv->netdev);
381 break; 382 break;
382 case USB_8DEV_STATUSMSG_OVERRUN: 383 case USB_8DEV_STATUSMSG_OVERRUN: