treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / net / can / usb / kvaser_usb / kvaser_usb_hydra.c
blob7ab87a758754543a189672ad886ce8dbda2f19e3
1 // SPDX-License-Identifier: GPL-2.0
2 /* Parts of this driver are based on the following:
3 * - Kvaser linux mhydra driver (version 5.24)
4 * - CAN driver for esd CAN-USB/2
6 * Copyright (C) 2018 KVASER AB, Sweden. All rights reserved.
7 * Copyright (C) 2010 Matthias Fuchs <matthias.fuchs@esd.eu>, esd gmbh
9 * Known issues:
10 * - Transition from CAN_STATE_ERROR_WARNING to CAN_STATE_ERROR_ACTIVE is only
11 * reported after a call to do_get_berr_counter(), since firmware does not
12 * distinguish between ERROR_WARNING and ERROR_ACTIVE.
13 * - Hardware timestamps are not set for CAN Tx frames.
16 #include <linux/completion.h>
17 #include <linux/device.h>
18 #include <linux/gfp.h>
19 #include <linux/jiffies.h>
20 #include <linux/kernel.h>
21 #include <linux/netdevice.h>
22 #include <linux/spinlock.h>
23 #include <linux/string.h>
24 #include <linux/types.h>
25 #include <linux/usb.h>
27 #include <linux/can.h>
28 #include <linux/can/dev.h>
29 #include <linux/can/error.h>
30 #include <linux/can/netlink.h>
32 #include "kvaser_usb.h"
34 /* Forward declarations */
35 static const struct kvaser_usb_dev_cfg kvaser_usb_hydra_dev_cfg_kcan;
36 static const struct kvaser_usb_dev_cfg kvaser_usb_hydra_dev_cfg_flexc;
38 #define KVASER_USB_HYDRA_BULK_EP_IN_ADDR 0x82
39 #define KVASER_USB_HYDRA_BULK_EP_OUT_ADDR 0x02
41 #define KVASER_USB_HYDRA_MAX_TRANSID 0xff
42 #define KVASER_USB_HYDRA_MIN_TRANSID 0x01
44 /* Minihydra command IDs */
45 #define CMD_SET_BUSPARAMS_REQ 16
46 #define CMD_GET_CHIP_STATE_REQ 19
47 #define CMD_CHIP_STATE_EVENT 20
48 #define CMD_SET_DRIVERMODE_REQ 21
49 #define CMD_START_CHIP_REQ 26
50 #define CMD_START_CHIP_RESP 27
51 #define CMD_STOP_CHIP_REQ 28
52 #define CMD_STOP_CHIP_RESP 29
53 #define CMD_TX_CAN_MESSAGE 33
54 #define CMD_GET_CARD_INFO_REQ 34
55 #define CMD_GET_CARD_INFO_RESP 35
56 #define CMD_GET_SOFTWARE_INFO_REQ 38
57 #define CMD_GET_SOFTWARE_INFO_RESP 39
58 #define CMD_ERROR_EVENT 45
59 #define CMD_FLUSH_QUEUE 48
60 #define CMD_TX_ACKNOWLEDGE 50
61 #define CMD_FLUSH_QUEUE_RESP 66
62 #define CMD_SET_BUSPARAMS_FD_REQ 69
63 #define CMD_SET_BUSPARAMS_FD_RESP 70
64 #define CMD_SET_BUSPARAMS_RESP 85
65 #define CMD_GET_CAPABILITIES_REQ 95
66 #define CMD_GET_CAPABILITIES_RESP 96
67 #define CMD_RX_MESSAGE 106
68 #define CMD_MAP_CHANNEL_REQ 200
69 #define CMD_MAP_CHANNEL_RESP 201
70 #define CMD_GET_SOFTWARE_DETAILS_REQ 202
71 #define CMD_GET_SOFTWARE_DETAILS_RESP 203
72 #define CMD_EXTENDED 255
74 /* Minihydra extended command IDs */
75 #define CMD_TX_CAN_MESSAGE_FD 224
76 #define CMD_TX_ACKNOWLEDGE_FD 225
77 #define CMD_RX_MESSAGE_FD 226
79 /* Hydra commands are handled by different threads in firmware.
80 * The threads are denoted hydra entity (HE). Each HE got a unique 6-bit
81 * address. The address is used in hydra commands to get/set source and
82 * destination HE. There are two predefined HE addresses, the remaining
83 * addresses are different between devices and firmware versions. Hence, we need
84 * to enumerate the addresses (see kvaser_usb_hydra_map_channel()).
87 /* Well-known HE addresses */
88 #define KVASER_USB_HYDRA_HE_ADDRESS_ROUTER 0x00
89 #define KVASER_USB_HYDRA_HE_ADDRESS_ILLEGAL 0x3e
91 #define KVASER_USB_HYDRA_TRANSID_CANHE 0x40
92 #define KVASER_USB_HYDRA_TRANSID_SYSDBG 0x61
94 struct kvaser_cmd_map_ch_req {
95 char name[16];
96 u8 channel;
97 u8 reserved[11];
98 } __packed;
100 struct kvaser_cmd_map_ch_res {
101 u8 he_addr;
102 u8 channel;
103 u8 reserved[26];
104 } __packed;
106 struct kvaser_cmd_card_info {
107 __le32 serial_number;
108 __le32 clock_res;
109 __le32 mfg_date;
110 __le32 ean[2];
111 u8 hw_version;
112 u8 usb_mode;
113 u8 hw_type;
114 u8 reserved0;
115 u8 nchannels;
116 u8 reserved1[3];
117 } __packed;
119 struct kvaser_cmd_sw_info {
120 u8 reserved0[8];
121 __le16 max_outstanding_tx;
122 u8 reserved1[18];
123 } __packed;
125 struct kvaser_cmd_sw_detail_req {
126 u8 use_ext_cmd;
127 u8 reserved[27];
128 } __packed;
130 /* Software detail flags */
131 #define KVASER_USB_HYDRA_SW_FLAG_FW_BETA BIT(2)
132 #define KVASER_USB_HYDRA_SW_FLAG_FW_BAD BIT(4)
133 #define KVASER_USB_HYDRA_SW_FLAG_FREQ_80M BIT(5)
134 #define KVASER_USB_HYDRA_SW_FLAG_EXT_CMD BIT(9)
135 #define KVASER_USB_HYDRA_SW_FLAG_CANFD BIT(10)
136 #define KVASER_USB_HYDRA_SW_FLAG_NONISO BIT(11)
137 #define KVASER_USB_HYDRA_SW_FLAG_EXT_CAP BIT(12)
138 struct kvaser_cmd_sw_detail_res {
139 __le32 sw_flags;
140 __le32 sw_version;
141 __le32 sw_name;
142 __le32 ean[2];
143 __le32 max_bitrate;
144 u8 reserved[4];
145 } __packed;
147 /* Sub commands for cap_req and cap_res */
148 #define KVASER_USB_HYDRA_CAP_CMD_LISTEN_MODE 0x02
149 #define KVASER_USB_HYDRA_CAP_CMD_ERR_REPORT 0x05
150 #define KVASER_USB_HYDRA_CAP_CMD_ONE_SHOT 0x06
151 struct kvaser_cmd_cap_req {
152 __le16 cap_cmd;
153 u8 reserved[26];
154 } __packed;
156 /* Status codes for cap_res */
157 #define KVASER_USB_HYDRA_CAP_STAT_OK 0x00
158 #define KVASER_USB_HYDRA_CAP_STAT_NOT_IMPL 0x01
159 #define KVASER_USB_HYDRA_CAP_STAT_UNAVAIL 0x02
160 struct kvaser_cmd_cap_res {
161 __le16 cap_cmd;
162 __le16 status;
163 __le32 mask;
164 __le32 value;
165 u8 reserved[16];
166 } __packed;
168 /* CMD_ERROR_EVENT error codes */
169 #define KVASER_USB_HYDRA_ERROR_EVENT_CAN 0x01
170 #define KVASER_USB_HYDRA_ERROR_EVENT_PARAM 0x09
171 struct kvaser_cmd_error_event {
172 __le16 timestamp[3];
173 u8 reserved;
174 u8 error_code;
175 __le16 info1;
176 __le16 info2;
177 } __packed;
179 /* Chip state status flags. Used for chip_state_event and err_frame_data. */
180 #define KVASER_USB_HYDRA_BUS_ERR_ACT 0x00
181 #define KVASER_USB_HYDRA_BUS_ERR_PASS BIT(5)
182 #define KVASER_USB_HYDRA_BUS_BUS_OFF BIT(6)
183 struct kvaser_cmd_chip_state_event {
184 __le16 timestamp[3];
185 u8 tx_err_counter;
186 u8 rx_err_counter;
187 u8 bus_status;
188 u8 reserved[19];
189 } __packed;
191 /* Busparam modes */
192 #define KVASER_USB_HYDRA_BUS_MODE_CAN 0x00
193 #define KVASER_USB_HYDRA_BUS_MODE_CANFD_ISO 0x01
194 #define KVASER_USB_HYDRA_BUS_MODE_NONISO 0x02
195 struct kvaser_cmd_set_busparams {
196 __le32 bitrate;
197 u8 tseg1;
198 u8 tseg2;
199 u8 sjw;
200 u8 nsamples;
201 u8 reserved0[4];
202 __le32 bitrate_d;
203 u8 tseg1_d;
204 u8 tseg2_d;
205 u8 sjw_d;
206 u8 nsamples_d;
207 u8 canfd_mode;
208 u8 reserved1[7];
209 } __packed;
211 /* Ctrl modes */
212 #define KVASER_USB_HYDRA_CTRLMODE_NORMAL 0x01
213 #define KVASER_USB_HYDRA_CTRLMODE_LISTEN 0x02
214 struct kvaser_cmd_set_ctrlmode {
215 u8 mode;
216 u8 reserved[27];
217 } __packed;
219 struct kvaser_err_frame_data {
220 u8 bus_status;
221 u8 reserved0;
222 u8 tx_err_counter;
223 u8 rx_err_counter;
224 u8 reserved1[4];
225 } __packed;
227 struct kvaser_cmd_rx_can {
228 u8 cmd_len;
229 u8 cmd_no;
230 u8 channel;
231 u8 flags;
232 __le16 timestamp[3];
233 u8 dlc;
234 u8 padding;
235 __le32 id;
236 union {
237 u8 data[8];
238 struct kvaser_err_frame_data err_frame_data;
240 } __packed;
242 /* Extended CAN ID flag. Used in rx_can and tx_can */
243 #define KVASER_USB_HYDRA_EXTENDED_FRAME_ID BIT(31)
244 struct kvaser_cmd_tx_can {
245 __le32 id;
246 u8 data[8];
247 u8 dlc;
248 u8 flags;
249 __le16 transid;
250 u8 channel;
251 u8 reserved[11];
252 } __packed;
254 struct kvaser_cmd_header {
255 u8 cmd_no;
256 /* The destination HE address is stored in 0..5 of he_addr.
257 * The upper part of source HE address is stored in 6..7 of he_addr, and
258 * the lower part is stored in 12..15 of transid.
260 u8 he_addr;
261 __le16 transid;
262 } __packed;
264 struct kvaser_cmd {
265 struct kvaser_cmd_header header;
266 union {
267 struct kvaser_cmd_map_ch_req map_ch_req;
268 struct kvaser_cmd_map_ch_res map_ch_res;
270 struct kvaser_cmd_card_info card_info;
271 struct kvaser_cmd_sw_info sw_info;
272 struct kvaser_cmd_sw_detail_req sw_detail_req;
273 struct kvaser_cmd_sw_detail_res sw_detail_res;
275 struct kvaser_cmd_cap_req cap_req;
276 struct kvaser_cmd_cap_res cap_res;
278 struct kvaser_cmd_error_event error_event;
280 struct kvaser_cmd_set_busparams set_busparams_req;
282 struct kvaser_cmd_chip_state_event chip_state_event;
284 struct kvaser_cmd_set_ctrlmode set_ctrlmode;
286 struct kvaser_cmd_rx_can rx_can;
287 struct kvaser_cmd_tx_can tx_can;
288 } __packed;
289 } __packed;
291 /* CAN frame flags. Used in rx_can, ext_rx_can, tx_can and ext_tx_can */
292 #define KVASER_USB_HYDRA_CF_FLAG_ERROR_FRAME BIT(0)
293 #define KVASER_USB_HYDRA_CF_FLAG_OVERRUN BIT(1)
294 #define KVASER_USB_HYDRA_CF_FLAG_REMOTE_FRAME BIT(4)
295 #define KVASER_USB_HYDRA_CF_FLAG_EXTENDED_ID BIT(5)
296 /* CAN frame flags. Used in ext_rx_can and ext_tx_can */
297 #define KVASER_USB_HYDRA_CF_FLAG_OSM_NACK BIT(12)
298 #define KVASER_USB_HYDRA_CF_FLAG_ABL BIT(13)
299 #define KVASER_USB_HYDRA_CF_FLAG_FDF BIT(16)
300 #define KVASER_USB_HYDRA_CF_FLAG_BRS BIT(17)
301 #define KVASER_USB_HYDRA_CF_FLAG_ESI BIT(18)
303 /* KCAN packet header macros. Used in ext_rx_can and ext_tx_can */
304 #define KVASER_USB_KCAN_DATA_DLC_BITS 4
305 #define KVASER_USB_KCAN_DATA_DLC_SHIFT 8
306 #define KVASER_USB_KCAN_DATA_DLC_MASK \
307 GENMASK(KVASER_USB_KCAN_DATA_DLC_BITS - 1 + \
308 KVASER_USB_KCAN_DATA_DLC_SHIFT, \
309 KVASER_USB_KCAN_DATA_DLC_SHIFT)
311 #define KVASER_USB_KCAN_DATA_BRS BIT(14)
312 #define KVASER_USB_KCAN_DATA_FDF BIT(15)
313 #define KVASER_USB_KCAN_DATA_OSM BIT(16)
314 #define KVASER_USB_KCAN_DATA_AREQ BIT(31)
315 #define KVASER_USB_KCAN_DATA_SRR BIT(31)
316 #define KVASER_USB_KCAN_DATA_RTR BIT(29)
317 #define KVASER_USB_KCAN_DATA_IDE BIT(30)
318 struct kvaser_cmd_ext_rx_can {
319 __le32 flags;
320 __le32 id;
321 __le32 kcan_id;
322 __le32 kcan_header;
323 __le64 timestamp;
324 union {
325 u8 kcan_payload[64];
326 struct kvaser_err_frame_data err_frame_data;
328 } __packed;
330 struct kvaser_cmd_ext_tx_can {
331 __le32 flags;
332 __le32 id;
333 __le32 kcan_id;
334 __le32 kcan_header;
335 u8 databytes;
336 u8 dlc;
337 u8 reserved[6];
338 u8 kcan_payload[64];
339 } __packed;
341 struct kvaser_cmd_ext_tx_ack {
342 __le32 flags;
343 u8 reserved0[4];
344 __le64 timestamp;
345 u8 reserved1[8];
346 } __packed;
348 /* struct for extended commands (CMD_EXTENDED) */
349 struct kvaser_cmd_ext {
350 struct kvaser_cmd_header header;
351 __le16 len;
352 u8 cmd_no_ext;
353 u8 reserved;
355 union {
356 struct kvaser_cmd_ext_rx_can rx_can;
357 struct kvaser_cmd_ext_tx_can tx_can;
358 struct kvaser_cmd_ext_tx_ack tx_ack;
359 } __packed;
360 } __packed;
362 static const struct can_bittiming_const kvaser_usb_hydra_kcan_bittiming_c = {
363 .name = "kvaser_usb_kcan",
364 .tseg1_min = 1,
365 .tseg1_max = 255,
366 .tseg2_min = 1,
367 .tseg2_max = 32,
368 .sjw_max = 16,
369 .brp_min = 1,
370 .brp_max = 4096,
371 .brp_inc = 1,
374 static const struct can_bittiming_const kvaser_usb_hydra_flexc_bittiming_c = {
375 .name = "kvaser_usb_flex",
376 .tseg1_min = 4,
377 .tseg1_max = 16,
378 .tseg2_min = 2,
379 .tseg2_max = 8,
380 .sjw_max = 4,
381 .brp_min = 1,
382 .brp_max = 256,
383 .brp_inc = 1,
386 #define KVASER_USB_HYDRA_TRANSID_BITS 12
387 #define KVASER_USB_HYDRA_TRANSID_MASK \
388 GENMASK(KVASER_USB_HYDRA_TRANSID_BITS - 1, 0)
389 #define KVASER_USB_HYDRA_HE_ADDR_SRC_MASK GENMASK(7, 6)
390 #define KVASER_USB_HYDRA_HE_ADDR_DEST_MASK GENMASK(5, 0)
391 #define KVASER_USB_HYDRA_HE_ADDR_SRC_BITS 2
392 static inline u16 kvaser_usb_hydra_get_cmd_transid(const struct kvaser_cmd *cmd)
394 return le16_to_cpu(cmd->header.transid) & KVASER_USB_HYDRA_TRANSID_MASK;
397 static inline void kvaser_usb_hydra_set_cmd_transid(struct kvaser_cmd *cmd,
398 u16 transid)
400 cmd->header.transid =
401 cpu_to_le16(transid & KVASER_USB_HYDRA_TRANSID_MASK);
404 static inline u8 kvaser_usb_hydra_get_cmd_src_he(const struct kvaser_cmd *cmd)
406 return (cmd->header.he_addr & KVASER_USB_HYDRA_HE_ADDR_SRC_MASK) >>
407 KVASER_USB_HYDRA_HE_ADDR_SRC_BITS |
408 le16_to_cpu(cmd->header.transid) >>
409 KVASER_USB_HYDRA_TRANSID_BITS;
412 static inline void kvaser_usb_hydra_set_cmd_dest_he(struct kvaser_cmd *cmd,
413 u8 dest_he)
415 cmd->header.he_addr =
416 (cmd->header.he_addr & KVASER_USB_HYDRA_HE_ADDR_SRC_MASK) |
417 (dest_he & KVASER_USB_HYDRA_HE_ADDR_DEST_MASK);
420 static u8 kvaser_usb_hydra_channel_from_cmd(const struct kvaser_usb *dev,
421 const struct kvaser_cmd *cmd)
423 int i;
424 u8 channel = 0xff;
425 u8 src_he = kvaser_usb_hydra_get_cmd_src_he(cmd);
427 for (i = 0; i < KVASER_USB_MAX_NET_DEVICES; i++) {
428 if (dev->card_data.hydra.channel_to_he[i] == src_he) {
429 channel = i;
430 break;
434 return channel;
437 static u16 kvaser_usb_hydra_get_next_transid(struct kvaser_usb *dev)
439 unsigned long flags;
440 u16 transid;
441 struct kvaser_usb_dev_card_data_hydra *card_data =
442 &dev->card_data.hydra;
444 spin_lock_irqsave(&card_data->transid_lock, flags);
445 transid = card_data->transid;
446 if (transid >= KVASER_USB_HYDRA_MAX_TRANSID)
447 transid = KVASER_USB_HYDRA_MIN_TRANSID;
448 else
449 transid++;
450 card_data->transid = transid;
451 spin_unlock_irqrestore(&card_data->transid_lock, flags);
453 return transid;
456 static size_t kvaser_usb_hydra_cmd_size(struct kvaser_cmd *cmd)
458 size_t ret;
460 if (cmd->header.cmd_no == CMD_EXTENDED)
461 ret = le16_to_cpu(((struct kvaser_cmd_ext *)cmd)->len);
462 else
463 ret = sizeof(struct kvaser_cmd);
465 return ret;
468 static struct kvaser_usb_net_priv *
469 kvaser_usb_hydra_net_priv_from_cmd(const struct kvaser_usb *dev,
470 const struct kvaser_cmd *cmd)
472 struct kvaser_usb_net_priv *priv = NULL;
473 u8 channel = kvaser_usb_hydra_channel_from_cmd(dev, cmd);
475 if (channel >= dev->nchannels)
476 dev_err(&dev->intf->dev,
477 "Invalid channel number (%d)\n", channel);
478 else
479 priv = dev->nets[channel];
481 return priv;
484 static ktime_t
485 kvaser_usb_hydra_ktime_from_rx_cmd(const struct kvaser_usb_dev_cfg *cfg,
486 const struct kvaser_cmd *cmd)
488 u64 ticks;
490 if (cmd->header.cmd_no == CMD_EXTENDED) {
491 struct kvaser_cmd_ext *cmd_ext = (struct kvaser_cmd_ext *)cmd;
493 ticks = le64_to_cpu(cmd_ext->rx_can.timestamp);
494 } else {
495 ticks = le16_to_cpu(cmd->rx_can.timestamp[0]);
496 ticks += (u64)(le16_to_cpu(cmd->rx_can.timestamp[1])) << 16;
497 ticks += (u64)(le16_to_cpu(cmd->rx_can.timestamp[2])) << 32;
500 return ns_to_ktime(div_u64(ticks * 1000, cfg->timestamp_freq));
503 static int kvaser_usb_hydra_send_simple_cmd(struct kvaser_usb *dev,
504 u8 cmd_no, int channel)
506 struct kvaser_cmd *cmd;
507 int err;
509 cmd = kcalloc(1, sizeof(struct kvaser_cmd), GFP_KERNEL);
510 if (!cmd)
511 return -ENOMEM;
513 cmd->header.cmd_no = cmd_no;
514 if (channel < 0) {
515 kvaser_usb_hydra_set_cmd_dest_he
516 (cmd, KVASER_USB_HYDRA_HE_ADDRESS_ILLEGAL);
517 } else {
518 if (channel >= KVASER_USB_MAX_NET_DEVICES) {
519 dev_err(&dev->intf->dev, "channel (%d) out of range.\n",
520 channel);
521 err = -EINVAL;
522 goto end;
524 kvaser_usb_hydra_set_cmd_dest_he
525 (cmd, dev->card_data.hydra.channel_to_he[channel]);
527 kvaser_usb_hydra_set_cmd_transid
528 (cmd, kvaser_usb_hydra_get_next_transid(dev));
530 err = kvaser_usb_send_cmd(dev, cmd, kvaser_usb_hydra_cmd_size(cmd));
531 if (err)
532 goto end;
534 end:
535 kfree(cmd);
537 return err;
540 static int
541 kvaser_usb_hydra_send_simple_cmd_async(struct kvaser_usb_net_priv *priv,
542 u8 cmd_no)
544 struct kvaser_cmd *cmd;
545 struct kvaser_usb *dev = priv->dev;
546 int err;
548 cmd = kcalloc(1, sizeof(struct kvaser_cmd), GFP_ATOMIC);
549 if (!cmd)
550 return -ENOMEM;
552 cmd->header.cmd_no = cmd_no;
554 kvaser_usb_hydra_set_cmd_dest_he
555 (cmd, dev->card_data.hydra.channel_to_he[priv->channel]);
556 kvaser_usb_hydra_set_cmd_transid
557 (cmd, kvaser_usb_hydra_get_next_transid(dev));
559 err = kvaser_usb_send_cmd_async(priv, cmd,
560 kvaser_usb_hydra_cmd_size(cmd));
561 if (err)
562 kfree(cmd);
564 return err;
567 /* This function is used for synchronously waiting on hydra control commands.
568 * Note: Compared to kvaser_usb_hydra_read_bulk_callback(), we never need to
569 * handle partial hydra commands. Since hydra control commands are always
570 * non-extended commands.
572 static int kvaser_usb_hydra_wait_cmd(const struct kvaser_usb *dev, u8 cmd_no,
573 struct kvaser_cmd *cmd)
575 void *buf;
576 int err;
577 unsigned long timeout = jiffies + msecs_to_jiffies(KVASER_USB_TIMEOUT);
579 if (cmd->header.cmd_no == CMD_EXTENDED) {
580 dev_err(&dev->intf->dev, "Wait for CMD_EXTENDED not allowed\n");
581 return -EINVAL;
584 buf = kzalloc(KVASER_USB_RX_BUFFER_SIZE, GFP_KERNEL);
585 if (!buf)
586 return -ENOMEM;
588 do {
589 int actual_len = 0;
590 int pos = 0;
592 err = kvaser_usb_recv_cmd(dev, buf, KVASER_USB_RX_BUFFER_SIZE,
593 &actual_len);
594 if (err < 0)
595 goto end;
597 while (pos < actual_len) {
598 struct kvaser_cmd *tmp_cmd;
599 size_t cmd_len;
601 tmp_cmd = buf + pos;
602 cmd_len = kvaser_usb_hydra_cmd_size(tmp_cmd);
603 if (pos + cmd_len > actual_len) {
604 dev_err_ratelimited(&dev->intf->dev,
605 "Format error\n");
606 break;
609 if (tmp_cmd->header.cmd_no == cmd_no) {
610 memcpy(cmd, tmp_cmd, cmd_len);
611 goto end;
613 pos += cmd_len;
615 } while (time_before(jiffies, timeout));
617 err = -EINVAL;
619 end:
620 kfree(buf);
622 return err;
625 static int kvaser_usb_hydra_map_channel_resp(struct kvaser_usb *dev,
626 const struct kvaser_cmd *cmd)
628 u8 he, channel;
629 u16 transid = kvaser_usb_hydra_get_cmd_transid(cmd);
630 struct kvaser_usb_dev_card_data_hydra *card_data =
631 &dev->card_data.hydra;
633 if (transid > 0x007f || transid < 0x0040) {
634 dev_err(&dev->intf->dev,
635 "CMD_MAP_CHANNEL_RESP, invalid transid: 0x%x\n",
636 transid);
637 return -EINVAL;
640 switch (transid) {
641 case KVASER_USB_HYDRA_TRANSID_CANHE:
642 case KVASER_USB_HYDRA_TRANSID_CANHE + 1:
643 case KVASER_USB_HYDRA_TRANSID_CANHE + 2:
644 case KVASER_USB_HYDRA_TRANSID_CANHE + 3:
645 case KVASER_USB_HYDRA_TRANSID_CANHE + 4:
646 channel = transid & 0x000f;
647 he = cmd->map_ch_res.he_addr;
648 card_data->channel_to_he[channel] = he;
649 break;
650 case KVASER_USB_HYDRA_TRANSID_SYSDBG:
651 card_data->sysdbg_he = cmd->map_ch_res.he_addr;
652 break;
653 default:
654 dev_warn(&dev->intf->dev,
655 "Unknown CMD_MAP_CHANNEL_RESP transid=0x%x\n",
656 transid);
657 break;
660 return 0;
663 static int kvaser_usb_hydra_map_channel(struct kvaser_usb *dev, u16 transid,
664 u8 channel, const char *name)
666 struct kvaser_cmd *cmd;
667 int err;
669 cmd = kcalloc(1, sizeof(struct kvaser_cmd), GFP_KERNEL);
670 if (!cmd)
671 return -ENOMEM;
673 strcpy(cmd->map_ch_req.name, name);
674 cmd->header.cmd_no = CMD_MAP_CHANNEL_REQ;
675 kvaser_usb_hydra_set_cmd_dest_he
676 (cmd, KVASER_USB_HYDRA_HE_ADDRESS_ROUTER);
677 cmd->map_ch_req.channel = channel;
679 kvaser_usb_hydra_set_cmd_transid(cmd, transid);
681 err = kvaser_usb_send_cmd(dev, cmd, kvaser_usb_hydra_cmd_size(cmd));
682 if (err)
683 goto end;
685 err = kvaser_usb_hydra_wait_cmd(dev, CMD_MAP_CHANNEL_RESP, cmd);
686 if (err)
687 goto end;
689 err = kvaser_usb_hydra_map_channel_resp(dev, cmd);
690 if (err)
691 goto end;
693 end:
694 kfree(cmd);
696 return err;
699 static int kvaser_usb_hydra_get_single_capability(struct kvaser_usb *dev,
700 u16 cap_cmd_req, u16 *status)
702 struct kvaser_usb_dev_card_data *card_data = &dev->card_data;
703 struct kvaser_cmd *cmd;
704 u32 value = 0;
705 u32 mask = 0;
706 u16 cap_cmd_res;
707 int err;
708 int i;
710 cmd = kcalloc(1, sizeof(struct kvaser_cmd), GFP_KERNEL);
711 if (!cmd)
712 return -ENOMEM;
714 cmd->header.cmd_no = CMD_GET_CAPABILITIES_REQ;
715 cmd->cap_req.cap_cmd = cpu_to_le16(cap_cmd_req);
717 kvaser_usb_hydra_set_cmd_dest_he(cmd, card_data->hydra.sysdbg_he);
718 kvaser_usb_hydra_set_cmd_transid
719 (cmd, kvaser_usb_hydra_get_next_transid(dev));
721 err = kvaser_usb_send_cmd(dev, cmd, kvaser_usb_hydra_cmd_size(cmd));
722 if (err)
723 goto end;
725 err = kvaser_usb_hydra_wait_cmd(dev, CMD_GET_CAPABILITIES_RESP, cmd);
726 if (err)
727 goto end;
729 *status = le16_to_cpu(cmd->cap_res.status);
731 if (*status != KVASER_USB_HYDRA_CAP_STAT_OK)
732 goto end;
734 cap_cmd_res = le16_to_cpu(cmd->cap_res.cap_cmd);
735 switch (cap_cmd_res) {
736 case KVASER_USB_HYDRA_CAP_CMD_LISTEN_MODE:
737 case KVASER_USB_HYDRA_CAP_CMD_ERR_REPORT:
738 case KVASER_USB_HYDRA_CAP_CMD_ONE_SHOT:
739 value = le32_to_cpu(cmd->cap_res.value);
740 mask = le32_to_cpu(cmd->cap_res.mask);
741 break;
742 default:
743 dev_warn(&dev->intf->dev, "Unknown capability command %u\n",
744 cap_cmd_res);
745 break;
748 for (i = 0; i < dev->nchannels; i++) {
749 if (BIT(i) & (value & mask)) {
750 switch (cap_cmd_res) {
751 case KVASER_USB_HYDRA_CAP_CMD_LISTEN_MODE:
752 card_data->ctrlmode_supported |=
753 CAN_CTRLMODE_LISTENONLY;
754 break;
755 case KVASER_USB_HYDRA_CAP_CMD_ERR_REPORT:
756 card_data->capabilities |=
757 KVASER_USB_CAP_BERR_CAP;
758 break;
759 case KVASER_USB_HYDRA_CAP_CMD_ONE_SHOT:
760 card_data->ctrlmode_supported |=
761 CAN_CTRLMODE_ONE_SHOT;
762 break;
767 end:
768 kfree(cmd);
770 return err;
773 static void kvaser_usb_hydra_start_chip_reply(const struct kvaser_usb *dev,
774 const struct kvaser_cmd *cmd)
776 struct kvaser_usb_net_priv *priv;
778 priv = kvaser_usb_hydra_net_priv_from_cmd(dev, cmd);
779 if (!priv)
780 return;
782 if (completion_done(&priv->start_comp) &&
783 netif_queue_stopped(priv->netdev)) {
784 netif_wake_queue(priv->netdev);
785 } else {
786 netif_start_queue(priv->netdev);
787 complete(&priv->start_comp);
791 static void kvaser_usb_hydra_stop_chip_reply(const struct kvaser_usb *dev,
792 const struct kvaser_cmd *cmd)
794 struct kvaser_usb_net_priv *priv;
796 priv = kvaser_usb_hydra_net_priv_from_cmd(dev, cmd);
797 if (!priv)
798 return;
800 complete(&priv->stop_comp);
803 static void kvaser_usb_hydra_flush_queue_reply(const struct kvaser_usb *dev,
804 const struct kvaser_cmd *cmd)
806 struct kvaser_usb_net_priv *priv;
808 priv = kvaser_usb_hydra_net_priv_from_cmd(dev, cmd);
809 if (!priv)
810 return;
812 complete(&priv->flush_comp);
815 static void
816 kvaser_usb_hydra_bus_status_to_can_state(const struct kvaser_usb_net_priv *priv,
817 u8 bus_status,
818 const struct can_berr_counter *bec,
819 enum can_state *new_state)
821 if (bus_status & KVASER_USB_HYDRA_BUS_BUS_OFF) {
822 *new_state = CAN_STATE_BUS_OFF;
823 } else if (bus_status & KVASER_USB_HYDRA_BUS_ERR_PASS) {
824 *new_state = CAN_STATE_ERROR_PASSIVE;
825 } else if (bus_status == KVASER_USB_HYDRA_BUS_ERR_ACT) {
826 if (bec->txerr >= 128 || bec->rxerr >= 128) {
827 netdev_warn(priv->netdev,
828 "ERR_ACTIVE but err tx=%u or rx=%u >=128\n",
829 bec->txerr, bec->rxerr);
830 *new_state = CAN_STATE_ERROR_PASSIVE;
831 } else if (bec->txerr >= 96 || bec->rxerr >= 96) {
832 *new_state = CAN_STATE_ERROR_WARNING;
833 } else {
834 *new_state = CAN_STATE_ERROR_ACTIVE;
839 static void kvaser_usb_hydra_update_state(struct kvaser_usb_net_priv *priv,
840 u8 bus_status,
841 const struct can_berr_counter *bec)
843 struct net_device *netdev = priv->netdev;
844 struct can_frame *cf;
845 struct sk_buff *skb;
846 struct net_device_stats *stats;
847 enum can_state new_state, old_state;
849 old_state = priv->can.state;
851 kvaser_usb_hydra_bus_status_to_can_state(priv, bus_status, bec,
852 &new_state);
854 if (new_state == old_state)
855 return;
857 /* Ignore state change if previous state was STOPPED and the new state
858 * is BUS_OFF. Firmware always report this as BUS_OFF, since firmware
859 * does not distinguish between BUS_OFF and STOPPED.
861 if (old_state == CAN_STATE_STOPPED && new_state == CAN_STATE_BUS_OFF)
862 return;
864 skb = alloc_can_err_skb(netdev, &cf);
865 if (skb) {
866 enum can_state tx_state, rx_state;
868 tx_state = (bec->txerr >= bec->rxerr) ?
869 new_state : CAN_STATE_ERROR_ACTIVE;
870 rx_state = (bec->txerr <= bec->rxerr) ?
871 new_state : CAN_STATE_ERROR_ACTIVE;
872 can_change_state(netdev, cf, tx_state, rx_state);
875 if (new_state == CAN_STATE_BUS_OFF && old_state < CAN_STATE_BUS_OFF) {
876 if (!priv->can.restart_ms)
877 kvaser_usb_hydra_send_simple_cmd_async
878 (priv, CMD_STOP_CHIP_REQ);
880 can_bus_off(netdev);
883 if (!skb) {
884 netdev_warn(netdev, "No memory left for err_skb\n");
885 return;
888 if (priv->can.restart_ms &&
889 old_state >= CAN_STATE_BUS_OFF &&
890 new_state < CAN_STATE_BUS_OFF)
891 priv->can.can_stats.restarts++;
893 cf->data[6] = bec->txerr;
894 cf->data[7] = bec->rxerr;
896 stats = &netdev->stats;
897 stats->rx_packets++;
898 stats->rx_bytes += cf->can_dlc;
899 netif_rx(skb);
902 static void kvaser_usb_hydra_state_event(const struct kvaser_usb *dev,
903 const struct kvaser_cmd *cmd)
905 struct kvaser_usb_net_priv *priv;
906 struct can_berr_counter bec;
907 u8 bus_status;
909 priv = kvaser_usb_hydra_net_priv_from_cmd(dev, cmd);
910 if (!priv)
911 return;
913 bus_status = cmd->chip_state_event.bus_status;
914 bec.txerr = cmd->chip_state_event.tx_err_counter;
915 bec.rxerr = cmd->chip_state_event.rx_err_counter;
917 kvaser_usb_hydra_update_state(priv, bus_status, &bec);
918 priv->bec.txerr = bec.txerr;
919 priv->bec.rxerr = bec.rxerr;
922 static void kvaser_usb_hydra_error_event_parameter(const struct kvaser_usb *dev,
923 const struct kvaser_cmd *cmd)
925 /* info1 will contain the offending cmd_no */
926 switch (le16_to_cpu(cmd->error_event.info1)) {
927 case CMD_START_CHIP_REQ:
928 dev_warn(&dev->intf->dev,
929 "CMD_START_CHIP_REQ error in parameter\n");
930 break;
932 case CMD_STOP_CHIP_REQ:
933 dev_warn(&dev->intf->dev,
934 "CMD_STOP_CHIP_REQ error in parameter\n");
935 break;
937 case CMD_FLUSH_QUEUE:
938 dev_warn(&dev->intf->dev,
939 "CMD_FLUSH_QUEUE error in parameter\n");
940 break;
942 case CMD_SET_BUSPARAMS_REQ:
943 dev_warn(&dev->intf->dev,
944 "Set bittiming failed. Error in parameter\n");
945 break;
947 case CMD_SET_BUSPARAMS_FD_REQ:
948 dev_warn(&dev->intf->dev,
949 "Set data bittiming failed. Error in parameter\n");
950 break;
952 default:
953 dev_warn(&dev->intf->dev,
954 "Unhandled parameter error event cmd_no (%u)\n",
955 le16_to_cpu(cmd->error_event.info1));
956 break;
960 static void kvaser_usb_hydra_error_event(const struct kvaser_usb *dev,
961 const struct kvaser_cmd *cmd)
963 switch (cmd->error_event.error_code) {
964 case KVASER_USB_HYDRA_ERROR_EVENT_PARAM:
965 kvaser_usb_hydra_error_event_parameter(dev, cmd);
966 break;
968 case KVASER_USB_HYDRA_ERROR_EVENT_CAN:
969 /* Wrong channel mapping?! This should never happen!
970 * info1 will contain the offending cmd_no
972 dev_err(&dev->intf->dev,
973 "Received CAN error event for cmd_no (%u)\n",
974 le16_to_cpu(cmd->error_event.info1));
975 break;
977 default:
978 dev_warn(&dev->intf->dev,
979 "Unhandled error event (%d)\n",
980 cmd->error_event.error_code);
981 break;
985 static void
986 kvaser_usb_hydra_error_frame(struct kvaser_usb_net_priv *priv,
987 const struct kvaser_err_frame_data *err_frame_data,
988 ktime_t hwtstamp)
990 struct net_device *netdev = priv->netdev;
991 struct net_device_stats *stats = &netdev->stats;
992 struct can_frame *cf;
993 struct sk_buff *skb;
994 struct skb_shared_hwtstamps *shhwtstamps;
995 struct can_berr_counter bec;
996 enum can_state new_state, old_state;
997 u8 bus_status;
999 priv->can.can_stats.bus_error++;
1000 stats->rx_errors++;
1002 bus_status = err_frame_data->bus_status;
1003 bec.txerr = err_frame_data->tx_err_counter;
1004 bec.rxerr = err_frame_data->rx_err_counter;
1006 old_state = priv->can.state;
1007 kvaser_usb_hydra_bus_status_to_can_state(priv, bus_status, &bec,
1008 &new_state);
1010 skb = alloc_can_err_skb(netdev, &cf);
1012 if (new_state != old_state) {
1013 if (skb) {
1014 enum can_state tx_state, rx_state;
1016 tx_state = (bec.txerr >= bec.rxerr) ?
1017 new_state : CAN_STATE_ERROR_ACTIVE;
1018 rx_state = (bec.txerr <= bec.rxerr) ?
1019 new_state : CAN_STATE_ERROR_ACTIVE;
1021 can_change_state(netdev, cf, tx_state, rx_state);
1023 if (priv->can.restart_ms &&
1024 old_state >= CAN_STATE_BUS_OFF &&
1025 new_state < CAN_STATE_BUS_OFF)
1026 cf->can_id |= CAN_ERR_RESTARTED;
1029 if (new_state == CAN_STATE_BUS_OFF) {
1030 if (!priv->can.restart_ms)
1031 kvaser_usb_hydra_send_simple_cmd_async
1032 (priv, CMD_STOP_CHIP_REQ);
1034 can_bus_off(netdev);
1038 if (!skb) {
1039 stats->rx_dropped++;
1040 netdev_warn(netdev, "No memory left for err_skb\n");
1041 return;
1044 shhwtstamps = skb_hwtstamps(skb);
1045 shhwtstamps->hwtstamp = hwtstamp;
1047 cf->can_id |= CAN_ERR_BUSERROR;
1048 cf->data[6] = bec.txerr;
1049 cf->data[7] = bec.rxerr;
1051 stats->rx_packets++;
1052 stats->rx_bytes += cf->can_dlc;
1053 netif_rx(skb);
1055 priv->bec.txerr = bec.txerr;
1056 priv->bec.rxerr = bec.rxerr;
1059 static void kvaser_usb_hydra_one_shot_fail(struct kvaser_usb_net_priv *priv,
1060 const struct kvaser_cmd_ext *cmd)
1062 struct net_device *netdev = priv->netdev;
1063 struct net_device_stats *stats = &netdev->stats;
1064 struct can_frame *cf;
1065 struct sk_buff *skb;
1066 u32 flags;
1068 skb = alloc_can_err_skb(netdev, &cf);
1069 if (!skb) {
1070 stats->rx_dropped++;
1071 netdev_warn(netdev, "No memory left for err_skb\n");
1072 return;
1075 cf->can_id |= CAN_ERR_BUSERROR;
1076 flags = le32_to_cpu(cmd->tx_ack.flags);
1078 if (flags & KVASER_USB_HYDRA_CF_FLAG_OSM_NACK)
1079 cf->can_id |= CAN_ERR_ACK;
1080 if (flags & KVASER_USB_HYDRA_CF_FLAG_ABL) {
1081 cf->can_id |= CAN_ERR_LOSTARB;
1082 priv->can.can_stats.arbitration_lost++;
1085 stats->tx_errors++;
1086 stats->rx_packets++;
1087 stats->rx_bytes += cf->can_dlc;
1088 netif_rx(skb);
1091 static void kvaser_usb_hydra_tx_acknowledge(const struct kvaser_usb *dev,
1092 const struct kvaser_cmd *cmd)
1094 struct kvaser_usb_tx_urb_context *context;
1095 struct kvaser_usb_net_priv *priv;
1096 unsigned long irq_flags;
1097 bool one_shot_fail = false;
1098 u16 transid = kvaser_usb_hydra_get_cmd_transid(cmd);
1100 priv = kvaser_usb_hydra_net_priv_from_cmd(dev, cmd);
1101 if (!priv)
1102 return;
1104 if (!netif_device_present(priv->netdev))
1105 return;
1107 if (cmd->header.cmd_no == CMD_EXTENDED) {
1108 struct kvaser_cmd_ext *cmd_ext = (struct kvaser_cmd_ext *)cmd;
1109 u32 flags = le32_to_cpu(cmd_ext->tx_ack.flags);
1111 if (flags & (KVASER_USB_HYDRA_CF_FLAG_OSM_NACK |
1112 KVASER_USB_HYDRA_CF_FLAG_ABL)) {
1113 kvaser_usb_hydra_one_shot_fail(priv, cmd_ext);
1114 one_shot_fail = true;
1118 context = &priv->tx_contexts[transid % dev->max_tx_urbs];
1119 if (!one_shot_fail) {
1120 struct net_device_stats *stats = &priv->netdev->stats;
1122 stats->tx_packets++;
1123 stats->tx_bytes += can_dlc2len(context->dlc);
1126 spin_lock_irqsave(&priv->tx_contexts_lock, irq_flags);
1128 can_get_echo_skb(priv->netdev, context->echo_index);
1129 context->echo_index = dev->max_tx_urbs;
1130 --priv->active_tx_contexts;
1131 netif_wake_queue(priv->netdev);
1133 spin_unlock_irqrestore(&priv->tx_contexts_lock, irq_flags);
1136 static void kvaser_usb_hydra_rx_msg_std(const struct kvaser_usb *dev,
1137 const struct kvaser_cmd *cmd)
1139 struct kvaser_usb_net_priv *priv = NULL;
1140 struct can_frame *cf;
1141 struct sk_buff *skb;
1142 struct skb_shared_hwtstamps *shhwtstamps;
1143 struct net_device_stats *stats;
1144 u8 flags;
1145 ktime_t hwtstamp;
1147 priv = kvaser_usb_hydra_net_priv_from_cmd(dev, cmd);
1148 if (!priv)
1149 return;
1151 stats = &priv->netdev->stats;
1153 flags = cmd->rx_can.flags;
1154 hwtstamp = kvaser_usb_hydra_ktime_from_rx_cmd(dev->cfg, cmd);
1156 if (flags & KVASER_USB_HYDRA_CF_FLAG_ERROR_FRAME) {
1157 kvaser_usb_hydra_error_frame(priv, &cmd->rx_can.err_frame_data,
1158 hwtstamp);
1159 return;
1162 skb = alloc_can_skb(priv->netdev, &cf);
1163 if (!skb) {
1164 stats->rx_dropped++;
1165 return;
1168 shhwtstamps = skb_hwtstamps(skb);
1169 shhwtstamps->hwtstamp = hwtstamp;
1171 cf->can_id = le32_to_cpu(cmd->rx_can.id);
1173 if (cf->can_id & KVASER_USB_HYDRA_EXTENDED_FRAME_ID) {
1174 cf->can_id &= CAN_EFF_MASK;
1175 cf->can_id |= CAN_EFF_FLAG;
1176 } else {
1177 cf->can_id &= CAN_SFF_MASK;
1180 if (flags & KVASER_USB_HYDRA_CF_FLAG_OVERRUN)
1181 kvaser_usb_can_rx_over_error(priv->netdev);
1183 cf->can_dlc = get_can_dlc(cmd->rx_can.dlc);
1185 if (flags & KVASER_USB_HYDRA_CF_FLAG_REMOTE_FRAME)
1186 cf->can_id |= CAN_RTR_FLAG;
1187 else
1188 memcpy(cf->data, cmd->rx_can.data, cf->can_dlc);
1190 stats->rx_packets++;
1191 stats->rx_bytes += cf->can_dlc;
1192 netif_rx(skb);
1195 static void kvaser_usb_hydra_rx_msg_ext(const struct kvaser_usb *dev,
1196 const struct kvaser_cmd_ext *cmd)
1198 struct kvaser_cmd *std_cmd = (struct kvaser_cmd *)cmd;
1199 struct kvaser_usb_net_priv *priv;
1200 struct canfd_frame *cf;
1201 struct sk_buff *skb;
1202 struct skb_shared_hwtstamps *shhwtstamps;
1203 struct net_device_stats *stats;
1204 u32 flags;
1205 u8 dlc;
1206 u32 kcan_header;
1207 ktime_t hwtstamp;
1209 priv = kvaser_usb_hydra_net_priv_from_cmd(dev, std_cmd);
1210 if (!priv)
1211 return;
1213 stats = &priv->netdev->stats;
1215 kcan_header = le32_to_cpu(cmd->rx_can.kcan_header);
1216 dlc = (kcan_header & KVASER_USB_KCAN_DATA_DLC_MASK) >>
1217 KVASER_USB_KCAN_DATA_DLC_SHIFT;
1219 flags = le32_to_cpu(cmd->rx_can.flags);
1220 hwtstamp = kvaser_usb_hydra_ktime_from_rx_cmd(dev->cfg, std_cmd);
1222 if (flags & KVASER_USB_HYDRA_CF_FLAG_ERROR_FRAME) {
1223 kvaser_usb_hydra_error_frame(priv, &cmd->rx_can.err_frame_data,
1224 hwtstamp);
1225 return;
1228 if (flags & KVASER_USB_HYDRA_CF_FLAG_FDF)
1229 skb = alloc_canfd_skb(priv->netdev, &cf);
1230 else
1231 skb = alloc_can_skb(priv->netdev, (struct can_frame **)&cf);
1233 if (!skb) {
1234 stats->rx_dropped++;
1235 return;
1238 shhwtstamps = skb_hwtstamps(skb);
1239 shhwtstamps->hwtstamp = hwtstamp;
1241 cf->can_id = le32_to_cpu(cmd->rx_can.id);
1243 if (flags & KVASER_USB_HYDRA_CF_FLAG_EXTENDED_ID) {
1244 cf->can_id &= CAN_EFF_MASK;
1245 cf->can_id |= CAN_EFF_FLAG;
1246 } else {
1247 cf->can_id &= CAN_SFF_MASK;
1250 if (flags & KVASER_USB_HYDRA_CF_FLAG_OVERRUN)
1251 kvaser_usb_can_rx_over_error(priv->netdev);
1253 if (flags & KVASER_USB_HYDRA_CF_FLAG_FDF) {
1254 cf->len = can_dlc2len(get_canfd_dlc(dlc));
1255 if (flags & KVASER_USB_HYDRA_CF_FLAG_BRS)
1256 cf->flags |= CANFD_BRS;
1257 if (flags & KVASER_USB_HYDRA_CF_FLAG_ESI)
1258 cf->flags |= CANFD_ESI;
1259 } else {
1260 cf->len = get_can_dlc(dlc);
1263 if (flags & KVASER_USB_HYDRA_CF_FLAG_REMOTE_FRAME)
1264 cf->can_id |= CAN_RTR_FLAG;
1265 else
1266 memcpy(cf->data, cmd->rx_can.kcan_payload, cf->len);
1268 stats->rx_packets++;
1269 stats->rx_bytes += cf->len;
1270 netif_rx(skb);
1273 static void kvaser_usb_hydra_handle_cmd_std(const struct kvaser_usb *dev,
1274 const struct kvaser_cmd *cmd)
1276 switch (cmd->header.cmd_no) {
1277 case CMD_START_CHIP_RESP:
1278 kvaser_usb_hydra_start_chip_reply(dev, cmd);
1279 break;
1281 case CMD_STOP_CHIP_RESP:
1282 kvaser_usb_hydra_stop_chip_reply(dev, cmd);
1283 break;
1285 case CMD_FLUSH_QUEUE_RESP:
1286 kvaser_usb_hydra_flush_queue_reply(dev, cmd);
1287 break;
1289 case CMD_CHIP_STATE_EVENT:
1290 kvaser_usb_hydra_state_event(dev, cmd);
1291 break;
1293 case CMD_ERROR_EVENT:
1294 kvaser_usb_hydra_error_event(dev, cmd);
1295 break;
1297 case CMD_TX_ACKNOWLEDGE:
1298 kvaser_usb_hydra_tx_acknowledge(dev, cmd);
1299 break;
1301 case CMD_RX_MESSAGE:
1302 kvaser_usb_hydra_rx_msg_std(dev, cmd);
1303 break;
1305 /* Ignored commands */
1306 case CMD_SET_BUSPARAMS_RESP:
1307 case CMD_SET_BUSPARAMS_FD_RESP:
1308 break;
1310 default:
1311 dev_warn(&dev->intf->dev, "Unhandled command (%d)\n",
1312 cmd->header.cmd_no);
1313 break;
1317 static void kvaser_usb_hydra_handle_cmd_ext(const struct kvaser_usb *dev,
1318 const struct kvaser_cmd_ext *cmd)
1320 switch (cmd->cmd_no_ext) {
1321 case CMD_TX_ACKNOWLEDGE_FD:
1322 kvaser_usb_hydra_tx_acknowledge(dev, (struct kvaser_cmd *)cmd);
1323 break;
1325 case CMD_RX_MESSAGE_FD:
1326 kvaser_usb_hydra_rx_msg_ext(dev, cmd);
1327 break;
1329 default:
1330 dev_warn(&dev->intf->dev, "Unhandled extended command (%d)\n",
1331 cmd->header.cmd_no);
1332 break;
1336 static void kvaser_usb_hydra_handle_cmd(const struct kvaser_usb *dev,
1337 const struct kvaser_cmd *cmd)
1339 if (cmd->header.cmd_no == CMD_EXTENDED)
1340 kvaser_usb_hydra_handle_cmd_ext
1341 (dev, (struct kvaser_cmd_ext *)cmd);
1342 else
1343 kvaser_usb_hydra_handle_cmd_std(dev, cmd);
1346 static void *
1347 kvaser_usb_hydra_frame_to_cmd_ext(const struct kvaser_usb_net_priv *priv,
1348 const struct sk_buff *skb, int *frame_len,
1349 int *cmd_len, u16 transid)
1351 struct kvaser_usb *dev = priv->dev;
1352 struct kvaser_cmd_ext *cmd;
1353 struct canfd_frame *cf = (struct canfd_frame *)skb->data;
1354 u8 dlc = can_len2dlc(cf->len);
1355 u8 nbr_of_bytes = cf->len;
1356 u32 flags;
1357 u32 id;
1358 u32 kcan_id;
1359 u32 kcan_header;
1361 *frame_len = nbr_of_bytes;
1363 cmd = kcalloc(1, sizeof(struct kvaser_cmd_ext), GFP_ATOMIC);
1364 if (!cmd)
1365 return NULL;
1367 kvaser_usb_hydra_set_cmd_dest_he
1368 ((struct kvaser_cmd *)cmd,
1369 dev->card_data.hydra.channel_to_he[priv->channel]);
1370 kvaser_usb_hydra_set_cmd_transid((struct kvaser_cmd *)cmd, transid);
1372 cmd->header.cmd_no = CMD_EXTENDED;
1373 cmd->cmd_no_ext = CMD_TX_CAN_MESSAGE_FD;
1375 *cmd_len = ALIGN(sizeof(struct kvaser_cmd_ext) -
1376 sizeof(cmd->tx_can.kcan_payload) + nbr_of_bytes,
1379 cmd->len = cpu_to_le16(*cmd_len);
1381 cmd->tx_can.databytes = nbr_of_bytes;
1382 cmd->tx_can.dlc = dlc;
1384 if (cf->can_id & CAN_EFF_FLAG) {
1385 id = cf->can_id & CAN_EFF_MASK;
1386 flags = KVASER_USB_HYDRA_CF_FLAG_EXTENDED_ID;
1387 kcan_id = (cf->can_id & CAN_EFF_MASK) |
1388 KVASER_USB_KCAN_DATA_IDE | KVASER_USB_KCAN_DATA_SRR;
1389 } else {
1390 id = cf->can_id & CAN_SFF_MASK;
1391 flags = 0;
1392 kcan_id = cf->can_id & CAN_SFF_MASK;
1395 if (cf->can_id & CAN_ERR_FLAG)
1396 flags |= KVASER_USB_HYDRA_CF_FLAG_ERROR_FRAME;
1398 kcan_header = ((dlc << KVASER_USB_KCAN_DATA_DLC_SHIFT) &
1399 KVASER_USB_KCAN_DATA_DLC_MASK) |
1400 KVASER_USB_KCAN_DATA_AREQ |
1401 (priv->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT ?
1402 KVASER_USB_KCAN_DATA_OSM : 0);
1404 if (can_is_canfd_skb(skb)) {
1405 kcan_header |= KVASER_USB_KCAN_DATA_FDF |
1406 (cf->flags & CANFD_BRS ?
1407 KVASER_USB_KCAN_DATA_BRS : 0);
1408 } else {
1409 if (cf->can_id & CAN_RTR_FLAG) {
1410 kcan_id |= KVASER_USB_KCAN_DATA_RTR;
1411 cmd->tx_can.databytes = 0;
1412 flags |= KVASER_USB_HYDRA_CF_FLAG_REMOTE_FRAME;
1416 cmd->tx_can.kcan_id = cpu_to_le32(kcan_id);
1417 cmd->tx_can.id = cpu_to_le32(id);
1418 cmd->tx_can.flags = cpu_to_le32(flags);
1419 cmd->tx_can.kcan_header = cpu_to_le32(kcan_header);
1421 memcpy(cmd->tx_can.kcan_payload, cf->data, nbr_of_bytes);
1423 return cmd;
1426 static void *
1427 kvaser_usb_hydra_frame_to_cmd_std(const struct kvaser_usb_net_priv *priv,
1428 const struct sk_buff *skb, int *frame_len,
1429 int *cmd_len, u16 transid)
1431 struct kvaser_usb *dev = priv->dev;
1432 struct kvaser_cmd *cmd;
1433 struct can_frame *cf = (struct can_frame *)skb->data;
1434 u32 flags;
1435 u32 id;
1437 *frame_len = cf->can_dlc;
1439 cmd = kcalloc(1, sizeof(struct kvaser_cmd), GFP_ATOMIC);
1440 if (!cmd)
1441 return NULL;
1443 kvaser_usb_hydra_set_cmd_dest_he
1444 (cmd, dev->card_data.hydra.channel_to_he[priv->channel]);
1445 kvaser_usb_hydra_set_cmd_transid(cmd, transid);
1447 cmd->header.cmd_no = CMD_TX_CAN_MESSAGE;
1449 *cmd_len = ALIGN(sizeof(struct kvaser_cmd), 8);
1451 if (cf->can_id & CAN_EFF_FLAG) {
1452 id = (cf->can_id & CAN_EFF_MASK);
1453 id |= KVASER_USB_HYDRA_EXTENDED_FRAME_ID;
1454 } else {
1455 id = cf->can_id & CAN_SFF_MASK;
1458 cmd->tx_can.dlc = cf->can_dlc;
1460 flags = (cf->can_id & CAN_EFF_FLAG ?
1461 KVASER_USB_HYDRA_CF_FLAG_EXTENDED_ID : 0);
1463 if (cf->can_id & CAN_RTR_FLAG)
1464 flags |= KVASER_USB_HYDRA_CF_FLAG_REMOTE_FRAME;
1466 flags |= (cf->can_id & CAN_ERR_FLAG ?
1467 KVASER_USB_HYDRA_CF_FLAG_ERROR_FRAME : 0);
1469 cmd->tx_can.id = cpu_to_le32(id);
1470 cmd->tx_can.flags = flags;
1472 memcpy(cmd->tx_can.data, cf->data, *frame_len);
1474 return cmd;
1477 static int kvaser_usb_hydra_set_mode(struct net_device *netdev,
1478 enum can_mode mode)
1480 int err = 0;
1482 switch (mode) {
1483 case CAN_MODE_START:
1484 /* CAN controller automatically recovers from BUS_OFF */
1485 break;
1486 default:
1487 err = -EOPNOTSUPP;
1490 return err;
1493 static int kvaser_usb_hydra_set_bittiming(struct net_device *netdev)
1495 struct kvaser_cmd *cmd;
1496 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1497 struct can_bittiming *bt = &priv->can.bittiming;
1498 struct kvaser_usb *dev = priv->dev;
1499 int tseg1 = bt->prop_seg + bt->phase_seg1;
1500 int tseg2 = bt->phase_seg2;
1501 int sjw = bt->sjw;
1502 int err;
1504 cmd = kcalloc(1, sizeof(struct kvaser_cmd), GFP_KERNEL);
1505 if (!cmd)
1506 return -ENOMEM;
1508 cmd->header.cmd_no = CMD_SET_BUSPARAMS_REQ;
1509 cmd->set_busparams_req.bitrate = cpu_to_le32(bt->bitrate);
1510 cmd->set_busparams_req.sjw = (u8)sjw;
1511 cmd->set_busparams_req.tseg1 = (u8)tseg1;
1512 cmd->set_busparams_req.tseg2 = (u8)tseg2;
1513 cmd->set_busparams_req.nsamples = 1;
1515 kvaser_usb_hydra_set_cmd_dest_he
1516 (cmd, dev->card_data.hydra.channel_to_he[priv->channel]);
1517 kvaser_usb_hydra_set_cmd_transid
1518 (cmd, kvaser_usb_hydra_get_next_transid(dev));
1520 err = kvaser_usb_send_cmd(dev, cmd, kvaser_usb_hydra_cmd_size(cmd));
1522 kfree(cmd);
1524 return err;
1527 static int kvaser_usb_hydra_set_data_bittiming(struct net_device *netdev)
1529 struct kvaser_cmd *cmd;
1530 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1531 struct can_bittiming *dbt = &priv->can.data_bittiming;
1532 struct kvaser_usb *dev = priv->dev;
1533 int tseg1 = dbt->prop_seg + dbt->phase_seg1;
1534 int tseg2 = dbt->phase_seg2;
1535 int sjw = dbt->sjw;
1536 int err;
1538 cmd = kcalloc(1, sizeof(struct kvaser_cmd), GFP_KERNEL);
1539 if (!cmd)
1540 return -ENOMEM;
1542 cmd->header.cmd_no = CMD_SET_BUSPARAMS_FD_REQ;
1543 cmd->set_busparams_req.bitrate_d = cpu_to_le32(dbt->bitrate);
1544 cmd->set_busparams_req.sjw_d = (u8)sjw;
1545 cmd->set_busparams_req.tseg1_d = (u8)tseg1;
1546 cmd->set_busparams_req.tseg2_d = (u8)tseg2;
1547 cmd->set_busparams_req.nsamples_d = 1;
1549 if (priv->can.ctrlmode & CAN_CTRLMODE_FD) {
1550 if (priv->can.ctrlmode & CAN_CTRLMODE_FD_NON_ISO)
1551 cmd->set_busparams_req.canfd_mode =
1552 KVASER_USB_HYDRA_BUS_MODE_NONISO;
1553 else
1554 cmd->set_busparams_req.canfd_mode =
1555 KVASER_USB_HYDRA_BUS_MODE_CANFD_ISO;
1558 kvaser_usb_hydra_set_cmd_dest_he
1559 (cmd, dev->card_data.hydra.channel_to_he[priv->channel]);
1560 kvaser_usb_hydra_set_cmd_transid
1561 (cmd, kvaser_usb_hydra_get_next_transid(dev));
1563 err = kvaser_usb_send_cmd(dev, cmd, kvaser_usb_hydra_cmd_size(cmd));
1565 kfree(cmd);
1567 return err;
1570 static int kvaser_usb_hydra_get_berr_counter(const struct net_device *netdev,
1571 struct can_berr_counter *bec)
1573 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1574 int err;
1576 err = kvaser_usb_hydra_send_simple_cmd(priv->dev,
1577 CMD_GET_CHIP_STATE_REQ,
1578 priv->channel);
1579 if (err)
1580 return err;
1582 *bec = priv->bec;
1584 return 0;
1587 static int kvaser_usb_hydra_setup_endpoints(struct kvaser_usb *dev)
1589 const struct usb_host_interface *iface_desc;
1590 struct usb_endpoint_descriptor *ep;
1591 int i;
1593 iface_desc = dev->intf->cur_altsetting;
1595 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1596 ep = &iface_desc->endpoint[i].desc;
1598 if (!dev->bulk_in && usb_endpoint_is_bulk_in(ep) &&
1599 ep->bEndpointAddress == KVASER_USB_HYDRA_BULK_EP_IN_ADDR)
1600 dev->bulk_in = ep;
1602 if (!dev->bulk_out && usb_endpoint_is_bulk_out(ep) &&
1603 ep->bEndpointAddress == KVASER_USB_HYDRA_BULK_EP_OUT_ADDR)
1604 dev->bulk_out = ep;
1606 if (dev->bulk_in && dev->bulk_out)
1607 return 0;
1610 return -ENODEV;
1613 static int kvaser_usb_hydra_init_card(struct kvaser_usb *dev)
1615 int err;
1616 unsigned int i;
1617 struct kvaser_usb_dev_card_data_hydra *card_data =
1618 &dev->card_data.hydra;
1620 card_data->transid = KVASER_USB_HYDRA_MIN_TRANSID;
1621 spin_lock_init(&card_data->transid_lock);
1623 memset(card_data->usb_rx_leftover, 0, KVASER_USB_HYDRA_MAX_CMD_LEN);
1624 card_data->usb_rx_leftover_len = 0;
1625 spin_lock_init(&card_data->usb_rx_leftover_lock);
1627 memset(card_data->channel_to_he, KVASER_USB_HYDRA_HE_ADDRESS_ILLEGAL,
1628 sizeof(card_data->channel_to_he));
1629 card_data->sysdbg_he = 0;
1631 for (i = 0; i < KVASER_USB_MAX_NET_DEVICES; i++) {
1632 err = kvaser_usb_hydra_map_channel
1633 (dev,
1634 (KVASER_USB_HYDRA_TRANSID_CANHE | i),
1635 i, "CAN");
1636 if (err) {
1637 dev_err(&dev->intf->dev,
1638 "CMD_MAP_CHANNEL_REQ failed for CAN%u\n", i);
1639 return err;
1643 err = kvaser_usb_hydra_map_channel(dev, KVASER_USB_HYDRA_TRANSID_SYSDBG,
1644 0, "SYSDBG");
1645 if (err) {
1646 dev_err(&dev->intf->dev,
1647 "CMD_MAP_CHANNEL_REQ failed for SYSDBG\n");
1648 return err;
1651 return 0;
1654 static int kvaser_usb_hydra_get_software_info(struct kvaser_usb *dev)
1656 struct kvaser_cmd cmd;
1657 int err;
1659 err = kvaser_usb_hydra_send_simple_cmd(dev, CMD_GET_SOFTWARE_INFO_REQ,
1660 -1);
1661 if (err)
1662 return err;
1664 memset(&cmd, 0, sizeof(struct kvaser_cmd));
1665 err = kvaser_usb_hydra_wait_cmd(dev, CMD_GET_SOFTWARE_INFO_RESP, &cmd);
1666 if (err)
1667 return err;
1669 dev->max_tx_urbs = min_t(unsigned int, KVASER_USB_MAX_TX_URBS,
1670 le16_to_cpu(cmd.sw_info.max_outstanding_tx));
1672 return 0;
1675 static int kvaser_usb_hydra_get_software_details(struct kvaser_usb *dev)
1677 struct kvaser_cmd *cmd;
1678 int err;
1679 u32 flags;
1680 struct kvaser_usb_dev_card_data *card_data = &dev->card_data;
1682 cmd = kcalloc(1, sizeof(struct kvaser_cmd), GFP_KERNEL);
1683 if (!cmd)
1684 return -ENOMEM;
1686 cmd->header.cmd_no = CMD_GET_SOFTWARE_DETAILS_REQ;
1687 cmd->sw_detail_req.use_ext_cmd = 1;
1688 kvaser_usb_hydra_set_cmd_dest_he
1689 (cmd, KVASER_USB_HYDRA_HE_ADDRESS_ILLEGAL);
1691 kvaser_usb_hydra_set_cmd_transid
1692 (cmd, kvaser_usb_hydra_get_next_transid(dev));
1694 err = kvaser_usb_send_cmd(dev, cmd, kvaser_usb_hydra_cmd_size(cmd));
1695 if (err)
1696 goto end;
1698 err = kvaser_usb_hydra_wait_cmd(dev, CMD_GET_SOFTWARE_DETAILS_RESP,
1699 cmd);
1700 if (err)
1701 goto end;
1703 dev->fw_version = le32_to_cpu(cmd->sw_detail_res.sw_version);
1704 flags = le32_to_cpu(cmd->sw_detail_res.sw_flags);
1706 if (flags & KVASER_USB_HYDRA_SW_FLAG_FW_BAD) {
1707 dev_err(&dev->intf->dev,
1708 "Bad firmware, device refuse to run!\n");
1709 err = -EINVAL;
1710 goto end;
1713 if (flags & KVASER_USB_HYDRA_SW_FLAG_FW_BETA)
1714 dev_info(&dev->intf->dev, "Beta firmware in use\n");
1716 if (flags & KVASER_USB_HYDRA_SW_FLAG_EXT_CAP)
1717 card_data->capabilities |= KVASER_USB_CAP_EXT_CAP;
1719 if (flags & KVASER_USB_HYDRA_SW_FLAG_EXT_CMD)
1720 card_data->capabilities |= KVASER_USB_HYDRA_CAP_EXT_CMD;
1722 if (flags & KVASER_USB_HYDRA_SW_FLAG_CANFD)
1723 card_data->ctrlmode_supported |= CAN_CTRLMODE_FD;
1725 if (flags & KVASER_USB_HYDRA_SW_FLAG_NONISO)
1726 card_data->ctrlmode_supported |= CAN_CTRLMODE_FD_NON_ISO;
1728 if (flags & KVASER_USB_HYDRA_SW_FLAG_FREQ_80M)
1729 dev->cfg = &kvaser_usb_hydra_dev_cfg_kcan;
1730 else
1731 dev->cfg = &kvaser_usb_hydra_dev_cfg_flexc;
1733 end:
1734 kfree(cmd);
1736 return err;
1739 static int kvaser_usb_hydra_get_card_info(struct kvaser_usb *dev)
1741 struct kvaser_cmd cmd;
1742 int err;
1744 err = kvaser_usb_hydra_send_simple_cmd(dev, CMD_GET_CARD_INFO_REQ, -1);
1745 if (err)
1746 return err;
1748 memset(&cmd, 0, sizeof(struct kvaser_cmd));
1749 err = kvaser_usb_hydra_wait_cmd(dev, CMD_GET_CARD_INFO_RESP, &cmd);
1750 if (err)
1751 return err;
1753 dev->nchannels = cmd.card_info.nchannels;
1754 if (dev->nchannels > KVASER_USB_MAX_NET_DEVICES)
1755 return -EINVAL;
1757 return 0;
1760 static int kvaser_usb_hydra_get_capabilities(struct kvaser_usb *dev)
1762 int err;
1763 u16 status;
1765 if (!(dev->card_data.capabilities & KVASER_USB_CAP_EXT_CAP)) {
1766 dev_info(&dev->intf->dev,
1767 "No extended capability support. Upgrade your device.\n");
1768 return 0;
1771 err = kvaser_usb_hydra_get_single_capability
1772 (dev,
1773 KVASER_USB_HYDRA_CAP_CMD_LISTEN_MODE,
1774 &status);
1775 if (err)
1776 return err;
1777 if (status)
1778 dev_info(&dev->intf->dev,
1779 "KVASER_USB_HYDRA_CAP_CMD_LISTEN_MODE failed %u\n",
1780 status);
1782 err = kvaser_usb_hydra_get_single_capability
1783 (dev,
1784 KVASER_USB_HYDRA_CAP_CMD_ERR_REPORT,
1785 &status);
1786 if (err)
1787 return err;
1788 if (status)
1789 dev_info(&dev->intf->dev,
1790 "KVASER_USB_HYDRA_CAP_CMD_ERR_REPORT failed %u\n",
1791 status);
1793 err = kvaser_usb_hydra_get_single_capability
1794 (dev, KVASER_USB_HYDRA_CAP_CMD_ONE_SHOT,
1795 &status);
1796 if (err)
1797 return err;
1798 if (status)
1799 dev_info(&dev->intf->dev,
1800 "KVASER_USB_HYDRA_CAP_CMD_ONE_SHOT failed %u\n",
1801 status);
1803 return 0;
1806 static int kvaser_usb_hydra_set_opt_mode(const struct kvaser_usb_net_priv *priv)
1808 struct kvaser_usb *dev = priv->dev;
1809 struct kvaser_cmd *cmd;
1810 int err;
1812 if ((priv->can.ctrlmode &
1813 (CAN_CTRLMODE_FD | CAN_CTRLMODE_FD_NON_ISO)) ==
1814 CAN_CTRLMODE_FD_NON_ISO) {
1815 netdev_warn(priv->netdev,
1816 "CTRLMODE_FD shall be on if CTRLMODE_FD_NON_ISO is on\n");
1817 return -EINVAL;
1820 cmd = kcalloc(1, sizeof(struct kvaser_cmd), GFP_KERNEL);
1821 if (!cmd)
1822 return -ENOMEM;
1824 cmd->header.cmd_no = CMD_SET_DRIVERMODE_REQ;
1825 kvaser_usb_hydra_set_cmd_dest_he
1826 (cmd, dev->card_data.hydra.channel_to_he[priv->channel]);
1827 kvaser_usb_hydra_set_cmd_transid
1828 (cmd, kvaser_usb_hydra_get_next_transid(dev));
1829 if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
1830 cmd->set_ctrlmode.mode = KVASER_USB_HYDRA_CTRLMODE_LISTEN;
1831 else
1832 cmd->set_ctrlmode.mode = KVASER_USB_HYDRA_CTRLMODE_NORMAL;
1834 err = kvaser_usb_send_cmd(dev, cmd, kvaser_usb_hydra_cmd_size(cmd));
1835 kfree(cmd);
1837 return err;
1840 static int kvaser_usb_hydra_start_chip(struct kvaser_usb_net_priv *priv)
1842 int err;
1844 init_completion(&priv->start_comp);
1846 err = kvaser_usb_hydra_send_simple_cmd(priv->dev, CMD_START_CHIP_REQ,
1847 priv->channel);
1848 if (err)
1849 return err;
1851 if (!wait_for_completion_timeout(&priv->start_comp,
1852 msecs_to_jiffies(KVASER_USB_TIMEOUT)))
1853 return -ETIMEDOUT;
1855 return 0;
1858 static int kvaser_usb_hydra_stop_chip(struct kvaser_usb_net_priv *priv)
1860 int err;
1862 init_completion(&priv->stop_comp);
1864 /* Make sure we do not report invalid BUS_OFF from CMD_CHIP_STATE_EVENT
1865 * see comment in kvaser_usb_hydra_update_state()
1867 priv->can.state = CAN_STATE_STOPPED;
1869 err = kvaser_usb_hydra_send_simple_cmd(priv->dev, CMD_STOP_CHIP_REQ,
1870 priv->channel);
1871 if (err)
1872 return err;
1874 if (!wait_for_completion_timeout(&priv->stop_comp,
1875 msecs_to_jiffies(KVASER_USB_TIMEOUT)))
1876 return -ETIMEDOUT;
1878 return 0;
1881 static int kvaser_usb_hydra_flush_queue(struct kvaser_usb_net_priv *priv)
1883 int err;
1885 init_completion(&priv->flush_comp);
1887 err = kvaser_usb_hydra_send_simple_cmd(priv->dev, CMD_FLUSH_QUEUE,
1888 priv->channel);
1889 if (err)
1890 return err;
1892 if (!wait_for_completion_timeout(&priv->flush_comp,
1893 msecs_to_jiffies(KVASER_USB_TIMEOUT)))
1894 return -ETIMEDOUT;
1896 return 0;
1899 /* A single extended hydra command can be transmitted in multiple transfers
1900 * We have to buffer partial hydra commands, and handle them on next callback.
1902 static void kvaser_usb_hydra_read_bulk_callback(struct kvaser_usb *dev,
1903 void *buf, int len)
1905 unsigned long irq_flags;
1906 struct kvaser_cmd *cmd;
1907 int pos = 0;
1908 size_t cmd_len;
1909 struct kvaser_usb_dev_card_data_hydra *card_data =
1910 &dev->card_data.hydra;
1911 int usb_rx_leftover_len;
1912 spinlock_t *usb_rx_leftover_lock = &card_data->usb_rx_leftover_lock;
1914 spin_lock_irqsave(usb_rx_leftover_lock, irq_flags);
1915 usb_rx_leftover_len = card_data->usb_rx_leftover_len;
1916 if (usb_rx_leftover_len) {
1917 int remaining_bytes;
1919 cmd = (struct kvaser_cmd *)card_data->usb_rx_leftover;
1921 cmd_len = kvaser_usb_hydra_cmd_size(cmd);
1923 remaining_bytes = min_t(unsigned int, len,
1924 cmd_len - usb_rx_leftover_len);
1925 /* Make sure we do not overflow usb_rx_leftover */
1926 if (remaining_bytes + usb_rx_leftover_len >
1927 KVASER_USB_HYDRA_MAX_CMD_LEN) {
1928 dev_err(&dev->intf->dev, "Format error\n");
1929 spin_unlock_irqrestore(usb_rx_leftover_lock, irq_flags);
1930 return;
1933 memcpy(card_data->usb_rx_leftover + usb_rx_leftover_len, buf,
1934 remaining_bytes);
1935 pos += remaining_bytes;
1937 if (remaining_bytes + usb_rx_leftover_len == cmd_len) {
1938 kvaser_usb_hydra_handle_cmd(dev, cmd);
1939 usb_rx_leftover_len = 0;
1940 } else {
1941 /* Command still not complete */
1942 usb_rx_leftover_len += remaining_bytes;
1944 card_data->usb_rx_leftover_len = usb_rx_leftover_len;
1946 spin_unlock_irqrestore(usb_rx_leftover_lock, irq_flags);
1948 while (pos < len) {
1949 cmd = buf + pos;
1951 cmd_len = kvaser_usb_hydra_cmd_size(cmd);
1953 if (pos + cmd_len > len) {
1954 /* We got first part of a command */
1955 int leftover_bytes;
1957 leftover_bytes = len - pos;
1958 /* Make sure we do not overflow usb_rx_leftover */
1959 if (leftover_bytes > KVASER_USB_HYDRA_MAX_CMD_LEN) {
1960 dev_err(&dev->intf->dev, "Format error\n");
1961 return;
1963 spin_lock_irqsave(usb_rx_leftover_lock, irq_flags);
1964 memcpy(card_data->usb_rx_leftover, buf + pos,
1965 leftover_bytes);
1966 card_data->usb_rx_leftover_len = leftover_bytes;
1967 spin_unlock_irqrestore(usb_rx_leftover_lock, irq_flags);
1968 break;
1971 kvaser_usb_hydra_handle_cmd(dev, cmd);
1972 pos += cmd_len;
1976 static void *
1977 kvaser_usb_hydra_frame_to_cmd(const struct kvaser_usb_net_priv *priv,
1978 const struct sk_buff *skb, int *frame_len,
1979 int *cmd_len, u16 transid)
1981 void *buf;
1983 if (priv->dev->card_data.capabilities & KVASER_USB_HYDRA_CAP_EXT_CMD)
1984 buf = kvaser_usb_hydra_frame_to_cmd_ext(priv, skb, frame_len,
1985 cmd_len, transid);
1986 else
1987 buf = kvaser_usb_hydra_frame_to_cmd_std(priv, skb, frame_len,
1988 cmd_len, transid);
1990 return buf;
1993 const struct kvaser_usb_dev_ops kvaser_usb_hydra_dev_ops = {
1994 .dev_set_mode = kvaser_usb_hydra_set_mode,
1995 .dev_set_bittiming = kvaser_usb_hydra_set_bittiming,
1996 .dev_set_data_bittiming = kvaser_usb_hydra_set_data_bittiming,
1997 .dev_get_berr_counter = kvaser_usb_hydra_get_berr_counter,
1998 .dev_setup_endpoints = kvaser_usb_hydra_setup_endpoints,
1999 .dev_init_card = kvaser_usb_hydra_init_card,
2000 .dev_get_software_info = kvaser_usb_hydra_get_software_info,
2001 .dev_get_software_details = kvaser_usb_hydra_get_software_details,
2002 .dev_get_card_info = kvaser_usb_hydra_get_card_info,
2003 .dev_get_capabilities = kvaser_usb_hydra_get_capabilities,
2004 .dev_set_opt_mode = kvaser_usb_hydra_set_opt_mode,
2005 .dev_start_chip = kvaser_usb_hydra_start_chip,
2006 .dev_stop_chip = kvaser_usb_hydra_stop_chip,
2007 .dev_reset_chip = NULL,
2008 .dev_flush_queue = kvaser_usb_hydra_flush_queue,
2009 .dev_read_bulk_callback = kvaser_usb_hydra_read_bulk_callback,
2010 .dev_frame_to_cmd = kvaser_usb_hydra_frame_to_cmd,
2013 static const struct kvaser_usb_dev_cfg kvaser_usb_hydra_dev_cfg_kcan = {
2014 .clock = {
2015 .freq = 80000000,
2017 .timestamp_freq = 80,
2018 .bittiming_const = &kvaser_usb_hydra_kcan_bittiming_c,
2019 .data_bittiming_const = &kvaser_usb_hydra_kcan_bittiming_c,
2022 static const struct kvaser_usb_dev_cfg kvaser_usb_hydra_dev_cfg_flexc = {
2023 .clock = {
2024 .freq = 24000000,
2026 .timestamp_freq = 1,
2027 .bittiming_const = &kvaser_usb_hydra_flexc_bittiming_c,