History

Merge tag 'linux-can-next-for-4.19-20180727' of ssh://gitolite.kernel.org/pub/scm/linux/kernel/git/mkl/linux-can-next

Marc Kleine-Budde says: ==================== pull-request: can-next 2018-01-16 this is a pull request for net-next/master consisting of 38 patches. Dan Murphy's patch fixes the path to a file in the comment of the CAN Error Message Frame Mask structure. A patch by Colin Ian King fixes a typo in the cc770 driver. The next patch is by me an sorts the Kconfigand Makefile entries of the CAN-USB driver subdir alphabetically. The patch by Jakob Unterwurzacher adds support for the UCAN USB-CAN adapter. YueHaibing's patch replaces a open coded skb_put()+memset() by skb_put_zero() in the CAN-dev infrastructure. Zhu Yi provides a patch to enable multi-queue CAN devices. Three patches by Luc Van Oostenryck fix the return value of several driver's xmit function, I contribute a patch for the a fourth driver. Fabio Estevam's patch switches the flexcan driver to SPDX identifier. Two patches by Jia-Ju Bai replace the mdelay() by a usleep_range() in the sja1000 drivers. The next 6 patches are by Anssi Hannula and refactor the xilinx CAN driver and add support for the xilinx CAN FD core. A patch by Gustavo A. R. Silva adds fallthrough annotation to the peak_usb driver. 5 patches by Stephane Grosjean for the peak CANFD driver do some cleanups and provide more improvements for further firmware releases. The remaining 13 patches are by Jimmy Assarsson and the first clean up the kvaser_usb driver, so that the later patches add support for the Kvaser USB hydra family. ====================

David S. Miller committed 8 years ago

Commit 41627cd2 parents 4be90c7 1f6ed4227 files · +6841 −2295Browse files

Unified · Split

27 files
  1. Documentation/devicetree/bindings/net/can/xilinx_can.txt +26 −9
  2. Documentation/networking/can_ucan_protocol.rst +332 −0
  3. Documentation/networking/index.rst +1 −0
  4. drivers/net/can/cc770/cc770.c +1 −1
  5. drivers/net/can/dev.c +7 −7
  6. drivers/net/can/flexcan.c +11 −22
  7. drivers/net/can/janz-ican3.c +1 −1
  8. drivers/net/can/peak_canfd/peak_canfd.c +1 −1
  9. drivers/net/can/peak_canfd/peak_pciefd_main.c +10 −10
  10. drivers/net/can/sja1000/peak_pci.c +1 −1
  11. drivers/net/can/sja1000/peak_pcmcia.c +1 −1
  12. drivers/net/can/sun4i_can.c +1 −1
  13. drivers/net/can/usb/Kconfig +41 −7
  14. drivers/net/can/usb/Makefile +4 −3
  15. drivers/net/can/usb/kvaser_usb.c +0 −2085
  16. drivers/net/can/usb/kvaser_usb/Makefile +2 −0
  17. drivers/net/can/usb/kvaser_usb/kvaser_usb.h +188 −0
  18. drivers/net/can/usb/kvaser_usb/kvaser_usb_core.c +835 −0
  19. drivers/net/can/usb/kvaser_usb/kvaser_usb_hydra.c +2028 −0
  20. drivers/net/can/usb/kvaser_usb/kvaser_usb_leaf.c +1358 −0
  21. drivers/net/can/usb/peak_usb/pcan_usb.c +1 −0
  22. drivers/net/can/usb/peak_usb/pcan_usb_core.c +1 −0
  23. drivers/net/can/usb/peak_usb/pcan_usb_pro.c +2 −0
  24. drivers/net/can/usb/ucan.c +1613 −0
  25. drivers/net/can/xilinx_can.c +368 −144
  26. include/linux/can/dev.h +6 −1
  27. include/uapi/linux/can.h +1 −1
Documentation/devicetree/bindings/net/can/xilinx_can.txt+26−9
@@ -2,20 +2,26 @@ Xilinx Axi CAN/Zynq CANPS controller Device Tree Bindings · 1 unmodified line
22 ---------------------------------------------------------
33
44 Required properties:
5−- compatible : Should be "xlnx,zynq-can-1.0" for Zynq CAN
6− controllers and "xlnx,axi-can-1.00.a" for Axi CAN
7− controllers.
8−- reg : Physical base address and size of the Axi CAN/Zynq
9− CANPS registers map.
5+- compatible : Should be:
6+ - "xlnx,zynq-can-1.0" for Zynq CAN controllers
7+ - "xlnx,axi-can-1.00.a" for Axi CAN controllers
8+ - "xlnx,canfd-1.0" for CAN FD controllers
9+- reg : Physical base address and size of the controller
10+ registers map.
1011 - interrupts : Property with a value describing the interrupt
1112 number.
1213 - interrupt-parent : Must be core interrupt controller
13−- clock-names : List of input clock names - "can_clk", "pclk"
14− (For CANPS), "can_clk" , "s_axi_aclk"(For AXI CAN)
14+- clock-names : List of input clock names
15+ - "can_clk", "pclk" (For CANPS),
16+ - "can_clk", "s_axi_aclk" (For AXI CAN and CAN FD).
1517 (See clock bindings for details).
1618 - clocks : Clock phandles (see clock bindings for details).
17−- tx-fifo-depth : Can Tx fifo depth.
18−- rx-fifo-depth : Can Rx fifo depth.
19+- tx-fifo-depth : Can Tx fifo depth (Zynq, Axi CAN).
20+- rx-fifo-depth : Can Rx fifo depth (Zynq, Axi CAN, CAN FD in
21+ sequential Rx mode).
22+- tx-mailbox-count : Can Tx mailbox buffer count (CAN FD).
23+- rx-mailbox-count : Can Rx mailbox buffer count (CAN FD in mailbox Rx
24+ mode).
1925
2026
2127 Example:
@@ -42,3 +48,14 @@ For Axi CAN Dts file: · 20 unmodified lines
4248 tx-fifo-depth = <0x40>;
4349 rx-fifo-depth = <0x40>;
4450 };
51+For CAN FD Dts file:
52+ canfd_0: canfd@40000000 {
53+ compatible = "xlnx,canfd-1.0";
54+ clocks = <&clkc 0>, <&clkc 1>;
55+ clock-names = "can_clk", "s_axi_aclk";
56+ reg = <0x40000000 0x2000>;
57+ interrupt-parent = <&intc>;
58+ interrupts = <0 59 1>;
59+ tx-mailbox-count = <0x20>;
60+ rx-fifo-depth = <0x20>;
61+ };
Documentation/networking/can_ucan_protocol.rst+332−0
@@ -0,0 +1,332 @@
1+=================
2+The UCAN Protocol
3+=================
4+
5+UCAN is the protocol used by the microcontroller-based USB-CAN
6+adapter that is integrated on System-on-Modules from Theobroma Systems
7+and that is also available as a standalone USB stick.
8+
9+The UCAN protocol has been designed to be hardware-independent.
10+It is modeled closely after how Linux represents CAN devices
11+internally. All multi-byte integers are encoded as Little Endian.
12+
13+All structures mentioned in this document are defined in
14+``drivers/net/can/usb/ucan.c``.
15+
16+USB Endpoints
17+=============
18+
19+UCAN devices use three USB endpoints:
20+
21+CONTROL endpoint
22+ The driver sends device management commands on this endpoint
23+
24+IN endpoint
25+ The device sends CAN data frames and CAN error frames
26+
27+OUT endpoint
28+ The driver sends CAN data frames on the out endpoint
29+
30+
31+CONTROL Messages
32+================
33+
34+UCAN devices are configured using vendor requests on the control pipe.
35+
36+To support multiple CAN interfaces in a single USB device all
37+configuration commands target the corresponding interface in the USB
38+descriptor.
39+
40+The driver uses ``ucan_ctrl_command_in/out`` and
41+``ucan_device_request_in`` to deliver commands to the device.
42+
43+Setup Packet
44+------------
45+
46+================= =====================================================
47+``bmRequestType`` Direction | Vendor | (Interface or Device)
48+``bRequest`` Command Number
49+``wValue`` Subcommand Number (16 Bit) or 0 if not used
50+``wIndex`` USB Interface Index (0 for device commands)
51+``wLength`` * Host to Device - Number of bytes to transmit
52+ * Device to Host - Maximum Number of bytes to
53+ receive. If the device send less. Commom ZLP
54+ semantics are used.
55+================= =====================================================
56+
57+Error Handling
58+--------------
59+
60+The device indicates failed control commands by stalling the
61+pipe.
62+
63+Device Commands
64+---------------
65+
66+UCAN_DEVICE_GET_FW_STRING
67+~~~~~~~~~~~~~~~~~~~~~~~~~
68+
69+*Dev2Host; optional*
70+
71+Request the device firmware string.
72+
73+
74+Interface Commands
75+------------------
76+
77+UCAN_COMMAND_START
78+~~~~~~~~~~~~~~~~~~
79+
80+*Host2Dev; mandatory*
81+
82+Bring the CAN interface up.
83+
84+Payload Format
85+ ``ucan_ctl_payload_t.cmd_start``
86+
87+==== ============================
88+mode or mask of ``UCAN_MODE_*``
89+==== ============================
90+
91+UCAN_COMMAND_STOP
92+~~~~~~~~~~~~~~~~~~
93+
94+*Host2Dev; mandatory*
95+
96+Stop the CAN interface
97+
98+Payload Format
99+ *empty*
100+
101+UCAN_COMMAND_RESET
102+~~~~~~~~~~~~~~~~~~
103+
104+*Host2Dev; mandatory*
105+
106+Reset the CAN controller (including error counters)
107+
108+Payload Format
109+ *empty*
110+
111+UCAN_COMMAND_GET
112+~~~~~~~~~~~~~~~~
113+
114+*Host2Dev; mandatory*
115+
116+Get Information from the Device
117+
118+Subcommands
119+^^^^^^^^^^^
120+
121+UCAN_COMMAND_GET_INFO
122+ Request the device information structure ``ucan_ctl_payload_t.device_info``.
123+
124+ See the ``device_info`` field for details, and
125+ ``uapi/linux/can/netlink.h`` for an explanation of the
126+ ``can_bittiming fields``.
127+
128+ Payload Format
129+ ``ucan_ctl_payload_t.device_info``
130+
131+UCAN_COMMAND_GET_PROTOCOL_VERSION
132+
133+ Request the device protocol version
134+ ``ucan_ctl_payload_t.protocol_version``. The current protocol version is 3.
135+
136+ Payload Format
137+ ``ucan_ctl_payload_t.protocol_version``
138+
139+.. note:: Devices that do not implement this command use the old
140+ protocol version 1
141+
142+UCAN_COMMAND_SET_BITTIMING
143+~~~~~~~~~~~~~~~~~~~~~~~~~~
144+
145+*Host2Dev; mandatory*
146+
147+Setup bittiming by sending the the structure
148+``ucan_ctl_payload_t.cmd_set_bittiming`` (see ``struct bittiming`` for
149+details)
150+
151+Payload Format
152+ ``ucan_ctl_payload_t.cmd_set_bittiming``.
153+
154+UCAN_SLEEP/WAKE
155+~~~~~~~~~~~~~~~
156+
157+*Host2Dev; optional*
158+
159+Configure sleep and wake modes. Not yet supported by the driver.
160+
161+UCAN_FILTER
162+~~~~~~~~~~~
163+
164+*Host2Dev; optional*
165+
166+Setup hardware CAN filters. Not yet supported by the driver.
167+
168+Allowed interface commands
169+--------------------------
170+
171+================== =================== ==================
172+Legal Device State Command New Device State
173+================== =================== ==================
174+stopped SET_BITTIMING stopped
175+stopped START started
176+started STOP or RESET stopped
177+stopped STOP or RESET stopped
178+started RESTART started
179+any GET *no change*
180+================== =================== ==================
181+
182+IN Message Format
183+=================
184+
185+A data packet on the USB IN endpoint contains one or more
186+``ucan_message_in`` values. If multiple messages are batched in a USB
187+data packet, the ``len`` field can be used to jump to the next
188+``ucan_message_in`` value (take care to sanity-check the ``len`` value
189+against the actual data size).
190+
191+.. _can_ucan_in_message_len:
192+
193+``len`` field
194+-------------
195+
196+Each ``ucan_message_in`` must be aligned to a 4-byte boundary (relative
197+to the start of the start of the data buffer). That means that there
198+may be padding bytes between multiple ``ucan_message_in`` values:
199+
200+.. code::
201+
202+ +----------------------------+ < 0
203+ | |
204+ | struct ucan_message_in |
205+ | |
206+ +----------------------------+ < len
207+ [padding]
208+ +----------------------------+ < round_up(len, 4)
209+ | |
210+ | struct ucan_message_in |
211+ | |
212+ +----------------------------+
213+ [...]
214+
215+``type`` field
216+--------------
217+
218+The ``type`` field specifies the type of the message.
219+
220+UCAN_IN_RX
221+~~~~~~~~~~
222+
223+``subtype``
224+ zero
225+
226+Data received from the CAN bus (ID + payload).
227+
228+UCAN_IN_TX_COMPLETE
229+~~~~~~~~~~~~~~~~~~~
230+
231+``subtype``
232+ zero
233+
234+The CAN device has sent a message to the CAN bus. It answers with a
235+list of of tuples <echo-ids, flags>.
236+
237+The echo-id identifies the frame from (echos the id from a previous
238+UCAN_OUT_TX message). The flag indicates the result of the
239+transmission. Whereas a set Bit 0 indicates success. All other bits
240+are reserved and set to zero.
241+
242+Flow Control
243+------------
244+
245+When receiving CAN messages there is no flow control on the USB
246+buffer. The driver has to handle inbound message quickly enough to
247+avoid drops. I case the device buffer overflow the condition is
248+reported by sending corresponding error frames (see
249+:ref:`can_ucan_error_handling`)
250+
251+
252+OUT Message Format
253+==================
254+
255+A data packet on the USB OUT endpoint contains one or more ``struct
256+ucan_message_out`` values. If multiple messages are batched into one
257+data packet, the device uses the ``len`` field to jump to the next
258+ucan_message_out value. Each ucan_message_out must be aligned to 4
259+bytes (relative to the start of the data buffer). The mechanism is
260+same as described in :ref:`can_ucan_in_message_len`.
261+
262+.. code::
263+
264+ +----------------------------+ < 0
265+ | |
266+ | struct ucan_message_out |
267+ | |
268+ +----------------------------+ < len
269+ [padding]
270+ +----------------------------+ < round_up(len, 4)
271+ | |
272+ | struct ucan_message_out |
273+ | |
274+ +----------------------------+
275+ [...]
276+
277+``type`` field
278+--------------
279+
280+In protocol version 3 only ``UCAN_OUT_TX`` is defined, others are used
281+only by legacy devices (protocol version 1).
282+
283+UCAN_OUT_TX
284+~~~~~~~~~~~
285+``subtype``
286+ echo id to be replied within a CAN_IN_TX_COMPLETE message
287+
288+Transmit a CAN frame. (parameters: ``id``, ``data``)
289+
290+Flow Control
291+------------
292+
293+When the device outbound buffers are full it starts sending *NAKs* on
294+the *OUT* pipe until more buffers are available. The driver stops the
295+queue when a certain threshold of out packets are incomplete.
296+
297+.. _can_ucan_error_handling:
298+
299+CAN Error Handling
300+==================
301+
302+If error reporting is turned on the device encodes errors into CAN
303+error frames (see ``uapi/linux/can/error.h``) and sends it using the
304+IN endpoint. The driver updates its error statistics and forwards
305+it.
306+
307+Although UCAN devices can suppress error frames completely, in Linux
308+the driver is always interested. Hence, the device is always started with
309+the ``UCAN_MODE_BERR_REPORT`` set. Filtering those messages for the
310+user space is done by the driver.
311+
312+Bus OFF
313+-------
314+
315+- The device does not recover from bus of automatically.
316+- Bus OFF is indicated by an error frame (see ``uapi/linux/can/error.h``)
317+- Bus OFF recovery is started by ``UCAN_COMMAND_RESTART``
318+- Once Bus OFF recover is completed the device sends an error frame
319+ indicating that it is on ERROR-ACTIVE state.
320+- During Bus OFF no frames are sent by the device.
321+- During Bus OFF transmission requests from the host are completed
322+ immediately with the success bit left unset.
323+
324+Example Conversation
325+====================
326+
327+#) Device is connected to USB
328+#) Host sends command ``UCAN_COMMAND_RESET``, subcmd 0
329+#) Host sends command ``UCAN_COMMAND_GET``, subcmd ``UCAN_COMMAND_GET_INFO``
330+#) Device sends ``UCAN_IN_DEVICE_INFO``
331+#) Host sends command ``UCAN_OUT_SET_BITTIMING``
332+#) Host sends command ``UCAN_COMMAND_START``, subcmd 0, mode ``UCAN_MODE_BERR_REPORT``
Documentation/networking/index.rst+1−0
@@ -10,6 +10,7 @@ Contents: · 9 unmodified lines
1010 af_xdp
1111 batman-adv
1212 can
13+ can_ucan_protocol
1314 dpaa2/index
1415 e100
1516 e1000
drivers/net/can/cc770/cc770.c+1−1
@@ -73,7 +73,7 @@ MODULE_PARM_DESC(msgobj15_eff, "Extended 29-bit frames for message object 15 " · 72 unmodified lines
7373
7474 static int i82527_compat;
7575 module_param(i82527_compat, int, 0444);
76−MODULE_PARM_DESC(i82527_compat, "Strict Intel 82527 comptibility mode "
76+MODULE_PARM_DESC(i82527_compat, "Strict Intel 82527 compatibility mode "
7777 "without using additional functions");
7878
7979 /*
drivers/net/can/dev.c+7−7
@@ -649,8 +649,7 @@ struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf) · 648 unmodified lines
649649 can_skb_prv(skb)->ifindex = dev->ifindex;
650650 can_skb_prv(skb)->skbcnt = 0;
651651
652− *cf = skb_put(skb, sizeof(struct can_frame));
653− memset(*cf, 0, sizeof(struct can_frame));
652+ *cf = skb_put_zero(skb, sizeof(struct can_frame));
654653
655654 return skb;
656655 }
@@ -678,8 +677,7 @@ struct sk_buff *alloc_canfd_skb(struct net_device *dev, · 21 unmodified lines
678677 can_skb_prv(skb)->ifindex = dev->ifindex;
679678 can_skb_prv(skb)->skbcnt = 0;
680679
681− *cfd = skb_put(skb, sizeof(struct canfd_frame));
682− memset(*cfd, 0, sizeof(struct canfd_frame));
680+ *cfd = skb_put_zero(skb, sizeof(struct canfd_frame));
683681
684682 return skb;
685683 }
@@ -703,7 +701,8 @@ EXPORT_SYMBOL_GPL(alloc_can_err_skb); · 17 unmodified lines
703701 /*
704702 * Allocate and setup space for the CAN network device
705703 */
706−struct net_device *alloc_candev(int sizeof_priv, unsigned int echo_skb_max)
704+struct net_device *alloc_candev_mqs(int sizeof_priv, unsigned int echo_skb_max,
705+ unsigned int txqs, unsigned int rxqs)
707706 {
708707 struct net_device *dev;
709708 struct can_priv *priv;
@@ -715,7 +714,8 @@ struct net_device *alloc_candev(int sizeof_priv, unsigned int echo_skb_max) · 5 unmodified lines
715714 else
716715 size = sizeof_priv;
717716
718− dev = alloc_netdev(size, "can%d", NET_NAME_UNKNOWN, can_setup);
717+ dev = alloc_netdev_mqs(size, "can%d", NET_NAME_UNKNOWN, can_setup,
718+ txqs, rxqs);
719719 if (!dev)
720720 return NULL;
721721
@@ -734,7 +734,7 @@ struct net_device *alloc_candev(int sizeof_priv, unsigned int echo_skb_max) · 12 unmodified lines
734734
735735 return dev;
736736 }
737−EXPORT_SYMBOL_GPL(alloc_candev);
737+EXPORT_SYMBOL_GPL(alloc_candev_mqs);
738738
739739 /*
740740 * Free space of the CAN network device
drivers/net/can/flexcan.c+11−22
@@ -1,24 +1,13 @@
1−/*
2− * flexcan.c - FLEXCAN CAN controller driver
3− *
4− * Copyright (c) 2005-2006 Varma Electronics Oy
5− * Copyright (c) 2009 Sascha Hauer, Pengutronix
6− * Copyright (c) 2010-2017 Pengutronix, Marc Kleine-Budde <kernel@pengutronix.de>
7− * Copyright (c) 2014 David Jander, Protonic Holland
8− *
9− * Based on code originally by Andrey Volkov <avolkov@varma-el.com>
10− *
11− * LICENCE:
12− * This program is free software; you can redistribute it and/or
13− * modify it under the terms of the GNU General Public License as
14− * published by the Free Software Foundation version 2.
15− *
16− * This program is distributed in the hope that it will be useful,
17− * but WITHOUT ANY WARRANTY; without even the implied warranty of
18− * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19− * GNU General Public License for more details.
20− *
21− */
1+// SPDX-License-Identifier: GPL-2.0
2+//
3+// flexcan.c - FLEXCAN CAN controller driver
4+//
5+// Copyright (c) 2005-2006 Varma Electronics Oy
6+// Copyright (c) 2009 Sascha Hauer, Pengutronix
7+// Copyright (c) 2010-2017 Pengutronix, Marc Kleine-Budde <kernel@pengutronix.de>
8+// Copyright (c) 2014 David Jander, Protonic Holland
9+//
10+// Based on code originally by Andrey Volkov <avolkov@varma-el.com>
2211
2312 #include <linux/netdevice.h>
2413 #include <linux/can.h>
@@ -523,7 +512,7 @@ static int flexcan_get_berr_counter(const struct net_device *dev, · 498 unmodified lines
523512 return err;
524513 }
525514
526−static int flexcan_start_xmit(struct sk_buff *skb, struct net_device *dev)
515+static netdev_tx_t flexcan_start_xmit(struct sk_buff *skb, struct net_device *dev)
527516 {
528517 const struct flexcan_priv *priv = netdev_priv(dev);
529518 struct can_frame *cf = (struct can_frame *)skb->data;
drivers/net/can/janz-ican3.c+1−1
@@ -1684,7 +1684,7 @@ static int ican3_stop(struct net_device *ndev) · 1683 unmodified lines
16841684 return 0;
16851685 }
16861686
1687−static int ican3_xmit(struct sk_buff *skb, struct net_device *ndev)
1687+static netdev_tx_t ican3_xmit(struct sk_buff *skb, struct net_device *ndev)
16881688 {
16891689 struct ican3_dev *mod = netdev_priv(ndev);
16901690 struct can_frame *cf = (struct can_frame *)skb->data;
drivers/net/can/peak_canfd/peak_canfd.c+1−1
@@ -486,7 +486,7 @@ int peak_canfd_handle_msgs_list(struct peak_canfd_priv *priv, · 485 unmodified lines
486486 if (msg_size <= 0)
487487 break;
488488
489− msg_ptr += msg_size;
489+ msg_ptr += ALIGN(msg_size, 4);
490490 }
491491
492492 if (msg_size < 0)
drivers/net/can/peak_canfd/peak_pciefd_main.c+10−10
@@ -174,9 +174,6 @@ struct pciefd_page { · 173 unmodified lines
174174 u32 size;
175175 };
176176
177−#define CANFD_IRQ_SET 0x00000001
178−#define CANFD_TX_PATH_SET 0x00000002
179−
180177 /* CAN-FD channel object */
181178 struct pciefd_board;
182179 struct pciefd_can {
@@ -418,7 +415,7 @@ static int pciefd_pre_cmd(struct peak_canfd_priv *ucan) · 235 unmodified lines
418415 break;
419416
420417 /* going into operational mode: setup IRQ handler */
421− err = request_irq(priv->board->pci_dev->irq,
418+ err = request_irq(priv->ucan.ndev->irq,
422419 pciefd_irq_handler,
423420 IRQF_SHARED,
424421 PCIEFD_DRV_NAME,
@@ -491,15 +488,18 @@ static int pciefd_post_cmd(struct peak_canfd_priv *ucan) · 66 unmodified lines
491488
492489 /* controller now in reset mode: */
493490
491+ /* disable IRQ for this CAN */
492+ pciefd_can_writereg(priv, CANFD_CTL_IEN_BIT,
493+ PCIEFD_REG_CAN_RX_CTL_CLR);
494+
494495 /* stop and reset DMA addresses in Tx/Rx engines */
495496 pciefd_can_clear_tx_dma(priv);
496497 pciefd_can_clear_rx_dma(priv);
497498
498− /* disable IRQ for this CAN */
499− pciefd_can_writereg(priv, CANFD_CTL_IEN_BIT,
500− PCIEFD_REG_CAN_RX_CTL_CLR);
499+ /* wait for above commands to complete (read cycle) */
500+ (void)pciefd_sys_readreg(priv->board, PCIEFD_REG_SYS_VER1);
501501
502− free_irq(priv->board->pci_dev->irq, priv);
502+ free_irq(priv->ucan.ndev->irq, priv);
503503
504504 ucan->can.state = CAN_STATE_STOPPED;
505505
@@ -638,7 +638,7 @@ static int pciefd_can_probe(struct pciefd_board *pciefd) · 132 unmodified lines
638638 GFP_KERNEL);
639639 if (!priv->tx_dma_vaddr) {
640640 dev_err(&pciefd->pci_dev->dev,
641− "Tx dmaim_alloc_coherent(%u) failure\n",
641+ "Tx dmam_alloc_coherent(%u) failure\n",
642642 PCIEFD_TX_DMA_SIZE);
643643 goto err_free_candev;
644644 }
@@ -691,7 +691,7 @@ static int pciefd_can_probe(struct pciefd_board *pciefd) · 46 unmodified lines
691691 pciefd->can[pciefd->can_count] = priv;
692692
693693 dev_info(&pciefd->pci_dev->dev, "%s at reg_base=0x%p irq=%d\n",
694− ndev->name, priv->reg_base, pciefd->pci_dev->irq);
694+ ndev->name, priv->reg_base, ndev->irq);
695695
696696 return 0;
697697
drivers/net/can/sja1000/peak_pci.c+1−1
@@ -608,7 +608,7 @@ static int peak_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent) · 607 unmodified lines
608608 writeb(0x00, cfg_base + PITA_GPIOICR);
609609 /* Toggle reset */
610610 writeb(0x05, cfg_base + PITA_MISC + 3);
611− mdelay(5);
611+ usleep_range(5000, 6000);
612612 /* Leave parport mux mode */
613613 writeb(0x04, cfg_base + PITA_MISC + 3);
614614
drivers/net/can/sja1000/peak_pcmcia.c+1−1
@@ -530,7 +530,7 @@ static int pcan_add_channels(struct pcan_pccard *card) · 529 unmodified lines
530530 pcan_write_reg(card, PCC_CCR, ccr);
531531
532532 /* wait 2ms before unresetting channels */
533− mdelay(2);
533+ usleep_range(2000, 3000);
534534
535535 ccr &= ~PCC_CCR_RST_ALL;
536536 pcan_write_reg(card, PCC_CCR, ccr);
drivers/net/can/sun4i_can.c+1−1
@@ -409,7 +409,7 @@ static int sun4ican_set_mode(struct net_device *dev, enum can_mode mode) · 408 unmodified lines
409409 * xx xx xx xx ff ll 00 11 22 33 44 55 66 77
410410 * [ can_id ] [flags] [len] [can data (up to 8 bytes]
411411 */
412−static int sun4ican_start_xmit(struct sk_buff *skb, struct net_device *dev)
412+static netdev_tx_t sun4ican_start_xmit(struct sk_buff *skb, struct net_device *dev)
413413 {
414414 struct sun4ican_priv *priv = netdev_priv(dev);
415415 struct can_frame *cf = (struct can_frame *)skb->data;
drivers/net/can/usb/Kconfig+41−7
@@ -1,6 +1,12 @@
11 menu "CAN USB interfaces"
22 depends on USB
33
4+config CAN_8DEV_USB
5+ tristate "8 devices USB2CAN interface"
6+ ---help---
7+ This driver supports the USB2CAN interface
8+ from 8 devices (http://www.8devices.com).
9+
410 config CAN_EMS_USB
511 tristate "EMS CPC-USB/ARM7 CAN/USB interface"
612 ---help---
@@ -26,7 +32,7 @@ config CAN_KVASER_USB · 19 unmodified lines
2632 tristate "Kvaser CAN/USB interface"
2733 ---help---
2834 This driver adds support for Kvaser CAN/USB devices like Kvaser
29− Leaf Light and Kvaser USBcan II.
35+ Leaf Light, Kvaser USBcan II and Kvaser Memorator Pro 5xHS.
3036
3137 The driver provides support for the following devices:
3238 - Kvaser Leaf Light
@@ -55,12 +61,30 @@ config CAN_KVASER_USB · 22 unmodified lines
5561 - Kvaser Memorator HS/HS
5662 - Kvaser Memorator HS/LS
5763 - Scania VCI2 (if you have the Kvaser logo on top)
64+ - Kvaser BlackBird v2
65+ - Kvaser Leaf Pro HS v2
66+ - Kvaser Hybrid 2xCAN/LIN
67+ - Kvaser Hybrid Pro 2xCAN/LIN
68+ - Kvaser Memorator 2xHS v2
69+ - Kvaser Memorator Pro 2xHS v2
70+ - Kvaser Memorator Pro 5xHS
71+ - Kvaser USBcan Light 4xHS
72+ - Kvaser USBcan Pro 2xHS v2
73+ - Kvaser USBcan Pro 5xHS
74+ - ATI Memorator Pro 2xHS v2
75+ - ATI USBcan Pro 2xHS v2
5876
5977 If unsure, say N.
6078
6179 To compile this driver as a module, choose M here: the
6280 module will be called kvaser_usb.
6381
82+config CAN_MCBA_USB
83+ tristate "Microchip CAN BUS Analyzer interface"
84+ ---help---
85+ This driver supports the CAN BUS Analyzer interface
86+ from Microchip (http://www.microchip.com/development-tools/).
87+
6488 config CAN_PEAK_USB
6589 tristate "PEAK PCAN-USB/USB Pro interfaces for CAN 2.0b/CAN-FD"
6690 ---help---
@@ -77,16 +101,26 @@ config CAN_PEAK_USB · 10 unmodified lines
77101
78102 (see also http://www.peak-system.com).
79103
80−config CAN_8DEV_USB
81− tristate "8 devices USB2CAN interface"
82− ---help---
83− This driver supports the USB2CAN interface
84− from 8 devices (http://www.8devices.com).
85−
86104 config CAN_MCBA_USB
87105 tristate "Microchip CAN BUS Analyzer interface"
88106 ---help---
89107 This driver supports the CAN BUS Analyzer interface
90108 from Microchip (http://www.microchip.com/development-tools/).
91109
110+config CAN_UCAN
111+ tristate "Theobroma Systems UCAN interface"
112+ ---help---
113+ This driver supports the Theobroma Systems
114+ UCAN USB-CAN interface.
115+
116+ The UCAN driver supports the microcontroller-based USB/CAN
117+ adapters from Theobroma Systems. There are two form-factors
118+ that run essentially the same firmware:
119+
120+ * Seal: standalone USB stick
121+ https://www.theobroma-systems.com/seal)
122+ * Mule: integrated on the PCB of various System-on-Modules
123+ from Theobroma Systems like the A31-µQ7 and the RK3399-Q7
124+ (https://www.theobroma-systems.com/rk3399-q7)
125+
92126 endmenu
drivers/net/can/usb/Makefile+4−3
@@ -3,10 +3,11 @@ · 2 unmodified lines
33 # Makefile for the Linux Controller Area Network USB drivers.
44 #
55
6+obj-$(CONFIG_CAN_8DEV_USB) += usb_8dev.o
67 obj-$(CONFIG_CAN_EMS_USB) += ems_usb.o
78 obj-$(CONFIG_CAN_ESD_USB2) += esd_usb2.o
89 obj-$(CONFIG_CAN_GS_USB) += gs_usb.o
9−obj-$(CONFIG_CAN_KVASER_USB) += kvaser_usb.o
10−obj-$(CONFIG_CAN_PEAK_USB) += peak_usb/
11−obj-$(CONFIG_CAN_8DEV_USB) += usb_8dev.o
10+obj-$(CONFIG_CAN_KVASER_USB) += kvaser_usb/
1211 obj-$(CONFIG_CAN_MCBA_USB) += mcba_usb.o
12+obj-$(CONFIG_CAN_PEAK_USB) += peak_usb/
13+obj-$(CONFIG_CAN_UCAN) += ucan.o
drivers/net/can/usb/kvaser_usb.c+0−2085
@@ -1,2085 +0,0 @@
1−/*
2− * This program is free software; you can redistribute it and/or
3− * modify it under the terms of the GNU General Public License as
4− * published by the Free Software Foundation version 2.
5− *
6− * Parts of this driver are based on the following:
7− * - Kvaser linux leaf driver (version 4.78)
8− * - CAN driver for esd CAN-USB/2
9− * - Kvaser linux usbcanII driver (version 5.3)
10− *
11− * Copyright (C) 2002-2006 KVASER AB, Sweden. All rights reserved.
12− * Copyright (C) 2010 Matthias Fuchs <matthias.fuchs@esd.eu>, esd gmbh
13− * Copyright (C) 2012 Olivier Sobrie <olivier@sobrie.be>
14− * Copyright (C) 2015 Valeo S.A.
15− */
16−
17−#include <linux/spinlock.h>
18−#include <linux/kernel.h>
19−#include <linux/completion.h>
20−#include <linux/module.h>
21−#include <linux/netdevice.h>
22−#include <linux/usb.h>
23−
24−#include <linux/can.h>
25−#include <linux/can/dev.h>
26−#include <linux/can/error.h>
27−
28−#define MAX_RX_URBS 4
29−#define START_TIMEOUT 1000 /* msecs */
30−#define STOP_TIMEOUT 1000 /* msecs */
31−#define USB_SEND_TIMEOUT 1000 /* msecs */
32−#define USB_RECV_TIMEOUT 1000 /* msecs */
33−#define RX_BUFFER_SIZE 3072
34−#define CAN_USB_CLOCK 8000000
35−#define MAX_NET_DEVICES 3
36−#define MAX_USBCAN_NET_DEVICES 2
37−
38−/* Kvaser Leaf USB devices */
39−#define KVASER_VENDOR_ID 0x0bfd
40−#define USB_LEAF_DEVEL_PRODUCT_ID 10
41−#define USB_LEAF_LITE_PRODUCT_ID 11
42−#define USB_LEAF_PRO_PRODUCT_ID 12
43−#define USB_LEAF_SPRO_PRODUCT_ID 14
44−#define USB_LEAF_PRO_LS_PRODUCT_ID 15
45−#define USB_LEAF_PRO_SWC_PRODUCT_ID 16
46−#define USB_LEAF_PRO_LIN_PRODUCT_ID 17
47−#define USB_LEAF_SPRO_LS_PRODUCT_ID 18
48−#define USB_LEAF_SPRO_SWC_PRODUCT_ID 19
49−#define USB_MEMO2_DEVEL_PRODUCT_ID 22
50−#define USB_MEMO2_HSHS_PRODUCT_ID 23
51−#define USB_UPRO_HSHS_PRODUCT_ID 24
52−#define USB_LEAF_LITE_GI_PRODUCT_ID 25
53−#define USB_LEAF_PRO_OBDII_PRODUCT_ID 26
54−#define USB_MEMO2_HSLS_PRODUCT_ID 27
55−#define USB_LEAF_LITE_CH_PRODUCT_ID 28
56−#define USB_BLACKBIRD_SPRO_PRODUCT_ID 29
57−#define USB_OEM_MERCURY_PRODUCT_ID 34
58−#define USB_OEM_LEAF_PRODUCT_ID 35
59−#define USB_CAN_R_PRODUCT_ID 39
60−#define USB_LEAF_LITE_V2_PRODUCT_ID 288
61−#define USB_MINI_PCIE_HS_PRODUCT_ID 289
62−#define USB_LEAF_LIGHT_HS_V2_OEM_PRODUCT_ID 290
63−#define USB_USBCAN_LIGHT_2HS_PRODUCT_ID 291
64−#define USB_MINI_PCIE_2HS_PRODUCT_ID 292
65−
66−static inline bool kvaser_is_leaf(const struct usb_device_id *id)
67−{
68− return id->idProduct >= USB_LEAF_DEVEL_PRODUCT_ID &&
69− id->idProduct <= USB_MINI_PCIE_2HS_PRODUCT_ID;
70−}
71−
72−/* Kvaser USBCan-II devices */
73−#define USB_USBCAN_REVB_PRODUCT_ID 2
74−#define USB_VCI2_PRODUCT_ID 3
75−#define USB_USBCAN2_PRODUCT_ID 4
76−#define USB_MEMORATOR_PRODUCT_ID 5
77−
78−static inline bool kvaser_is_usbcan(const struct usb_device_id *id)
79−{
80− return id->idProduct >= USB_USBCAN_REVB_PRODUCT_ID &&
81− id->idProduct <= USB_MEMORATOR_PRODUCT_ID;
82−}
83−
84−/* USB devices features */
85−#define KVASER_HAS_SILENT_MODE BIT(0)
86−#define KVASER_HAS_TXRX_ERRORS BIT(1)
87−
88−/* Message header size */
89−#define MSG_HEADER_LEN 2
90−
91−/* Can message flags */
92−#define MSG_FLAG_ERROR_FRAME BIT(0)
93−#define MSG_FLAG_OVERRUN BIT(1)
94−#define MSG_FLAG_NERR BIT(2)
95−#define MSG_FLAG_WAKEUP BIT(3)
96−#define MSG_FLAG_REMOTE_FRAME BIT(4)
97−#define MSG_FLAG_RESERVED BIT(5)
98−#define MSG_FLAG_TX_ACK BIT(6)
99−#define MSG_FLAG_TX_REQUEST BIT(7)
100−
101−/* Can states (M16C CxSTRH register) */
102−#define M16C_STATE_BUS_RESET BIT(0)
103−#define M16C_STATE_BUS_ERROR BIT(4)
104−#define M16C_STATE_BUS_PASSIVE BIT(5)
105−#define M16C_STATE_BUS_OFF BIT(6)
106−
107−/* Can msg ids */
108−#define CMD_RX_STD_MESSAGE 12
109−#define CMD_TX_STD_MESSAGE 13
110−#define CMD_RX_EXT_MESSAGE 14
111−#define CMD_TX_EXT_MESSAGE 15
112−#define CMD_SET_BUS_PARAMS 16
113−#define CMD_GET_BUS_PARAMS 17
114−#define CMD_GET_BUS_PARAMS_REPLY 18
115−#define CMD_GET_CHIP_STATE 19
116−#define CMD_CHIP_STATE_EVENT 20
117−#define CMD_SET_CTRL_MODE 21
118−#define CMD_GET_CTRL_MODE 22
119−#define CMD_GET_CTRL_MODE_REPLY 23
120−#define CMD_RESET_CHIP 24
121−#define CMD_RESET_CARD 25
122−#define CMD_START_CHIP 26
123−#define CMD_START_CHIP_REPLY 27
124−#define CMD_STOP_CHIP 28
125−#define CMD_STOP_CHIP_REPLY 29
126−
127−#define CMD_LEAF_GET_CARD_INFO2 32
128−#define CMD_USBCAN_RESET_CLOCK 32
129−#define CMD_USBCAN_CLOCK_OVERFLOW_EVENT 33
130−
131−#define CMD_GET_CARD_INFO 34
132−#define CMD_GET_CARD_INFO_REPLY 35
133−#define CMD_GET_SOFTWARE_INFO 38
134−#define CMD_GET_SOFTWARE_INFO_REPLY 39
135−#define CMD_ERROR_EVENT 45
136−#define CMD_FLUSH_QUEUE 48
137−#define CMD_RESET_ERROR_COUNTER 49
138−#define CMD_TX_ACKNOWLEDGE 50
139−#define CMD_CAN_ERROR_EVENT 51
140−#define CMD_FLUSH_QUEUE_REPLY 68
141−
142−#define CMD_LEAF_USB_THROTTLE 77
143−#define CMD_LEAF_LOG_MESSAGE 106
144−
145−/* error factors */
146−#define M16C_EF_ACKE BIT(0)
147−#define M16C_EF_CRCE BIT(1)
148−#define M16C_EF_FORME BIT(2)
149−#define M16C_EF_STFE BIT(3)
150−#define M16C_EF_BITE0 BIT(4)
151−#define M16C_EF_BITE1 BIT(5)
152−#define M16C_EF_RCVE BIT(6)
153−#define M16C_EF_TRE BIT(7)
154−
155−/* Only Leaf-based devices can report M16C error factors,
156− * thus define our own error status flags for USBCANII
157− */
158−#define USBCAN_ERROR_STATE_NONE 0
159−#define USBCAN_ERROR_STATE_TX_ERROR BIT(0)
160−#define USBCAN_ERROR_STATE_RX_ERROR BIT(1)
161−#define USBCAN_ERROR_STATE_BUSERROR BIT(2)
162−
163−/* bittiming parameters */
164−#define KVASER_USB_TSEG1_MIN 1
165−#define KVASER_USB_TSEG1_MAX 16
166−#define KVASER_USB_TSEG2_MIN 1
167−#define KVASER_USB_TSEG2_MAX 8
168−#define KVASER_USB_SJW_MAX 4
169−#define KVASER_USB_BRP_MIN 1
170−#define KVASER_USB_BRP_MAX 64
171−#define KVASER_USB_BRP_INC 1
172−
173−/* ctrl modes */
174−#define KVASER_CTRL_MODE_NORMAL 1
175−#define KVASER_CTRL_MODE_SILENT 2
176−#define KVASER_CTRL_MODE_SELFRECEPTION 3
177−#define KVASER_CTRL_MODE_OFF 4
178−
179−/* Extended CAN identifier flag */
180−#define KVASER_EXTENDED_FRAME BIT(31)
181−
182−/* Kvaser USB CAN dongles are divided into two major families:
183− * - Leaf: Based on Renesas M32C, running firmware labeled as 'filo'
184− * - UsbcanII: Based on Renesas M16C, running firmware labeled as 'helios'
185− */
186−enum kvaser_usb_family {
187− KVASER_LEAF,
188− KVASER_USBCAN,
189−};
190−
191−struct kvaser_msg_simple {
192− u8 tid;
193− u8 channel;
194−} __packed;
195−
196−struct kvaser_msg_cardinfo {
197− u8 tid;
198− u8 nchannels;
199− union {
200− struct {
201− __le32 serial_number;
202− __le32 padding;
203− } __packed leaf0;
204− struct {
205− __le32 serial_number_low;
206− __le32 serial_number_high;
207− } __packed usbcan0;
208− } __packed;
209− __le32 clock_resolution;
210− __le32 mfgdate;
211− u8 ean[8];
212− u8 hw_revision;
213− union {
214− struct {
215− u8 usb_hs_mode;
216− } __packed leaf1;
217− struct {
218− u8 padding;
219− } __packed usbcan1;
220− } __packed;
221− __le16 padding;
222−} __packed;
223−
224−struct kvaser_msg_cardinfo2 {
225− u8 tid;
226− u8 reserved;
227− u8 pcb_id[24];
228− __le32 oem_unlock_code;
229−} __packed;
230−
231−struct leaf_msg_softinfo {
232− u8 tid;
233− u8 padding0;
234− __le32 sw_options;
235− __le32 fw_version;
236− __le16 max_outstanding_tx;
237− __le16 padding1[9];
238−} __packed;
239−
240−struct usbcan_msg_softinfo {
241− u8 tid;
242− u8 fw_name[5];
243− __le16 max_outstanding_tx;
244− u8 padding[6];
245− __le32 fw_version;
246− __le16 checksum;
247− __le16 sw_options;
248−} __packed;
249−
250−struct kvaser_msg_busparams {
251− u8 tid;
252− u8 channel;
253− __le32 bitrate;
254− u8 tseg1;
255− u8 tseg2;
256− u8 sjw;
257− u8 no_samp;
258−} __packed;
259−
260−struct kvaser_msg_tx_can {
261− u8 channel;
262− u8 tid;
263− u8 msg[14];
264− union {
265− struct {
266− u8 padding;
267− u8 flags;
268− } __packed leaf;
269− struct {
270− u8 flags;
271− u8 padding;
272− } __packed usbcan;
273− } __packed;
274−} __packed;
275−
276−struct kvaser_msg_rx_can_header {
277− u8 channel;
278− u8 flag;
279−} __packed;
280−
281−struct leaf_msg_rx_can {
282− u8 channel;
283− u8 flag;
284−
285− __le16 time[3];
286− u8 msg[14];
287−} __packed;
288−
289−struct usbcan_msg_rx_can {
290− u8 channel;
291− u8 flag;
292−
293− u8 msg[14];
294− __le16 time;
295−} __packed;
296−
297−struct leaf_msg_chip_state_event {
298− u8 tid;
299− u8 channel;
300−
301− __le16 time[3];
302− u8 tx_errors_count;
303− u8 rx_errors_count;
304−
305− u8 status;
306− u8 padding[3];
307−} __packed;
308−
309−struct usbcan_msg_chip_state_event {
310− u8 tid;
311− u8 channel;
312−
313− u8 tx_errors_count;
314− u8 rx_errors_count;
315− __le16 time;
316−
317− u8 status;
318− u8 padding[3];
319−} __packed;
320−
321−struct kvaser_msg_tx_acknowledge_header {
322− u8 channel;
323− u8 tid;
324−} __packed;
325−
326−struct leaf_msg_tx_acknowledge {
327− u8 channel;
328− u8 tid;
329−
330− __le16 time[3];
331− u8 flags;
332− u8 time_offset;
333−} __packed;
334−
335−struct usbcan_msg_tx_acknowledge {
336− u8 channel;
337− u8 tid;
338−
339− __le16 time;
340− __le16 padding;
341−} __packed;
342−
343−struct leaf_msg_error_event {
344− u8 tid;
345− u8 flags;
346− __le16 time[3];
347− u8 channel;
348− u8 padding;
349− u8 tx_errors_count;
350− u8 rx_errors_count;
351− u8 status;
352− u8 error_factor;
353−} __packed;
354−
355−struct usbcan_msg_error_event {
356− u8 tid;
357− u8 padding;
358− u8 tx_errors_count_ch0;
359− u8 rx_errors_count_ch0;
360− u8 tx_errors_count_ch1;
361− u8 rx_errors_count_ch1;
362− u8 status_ch0;
363− u8 status_ch1;
364− __le16 time;
365−} __packed;
366−
367−struct kvaser_msg_ctrl_mode {
368− u8 tid;
369− u8 channel;
370− u8 ctrl_mode;
371− u8 padding[3];
372−} __packed;
373−
374−struct kvaser_msg_flush_queue {
375− u8 tid;
376− u8 channel;
377− u8 flags;
378− u8 padding[3];
379−} __packed;
380−
381−struct leaf_msg_log_message {
382− u8 channel;
383− u8 flags;
384− __le16 time[3];
385− u8 dlc;
386− u8 time_offset;
387− __le32 id;
388− u8 data[8];
389−} __packed;
390−
391−struct kvaser_msg {
392− u8 len;
393− u8 id;
394− union {
395− struct kvaser_msg_simple simple;
396− struct kvaser_msg_cardinfo cardinfo;
397− struct kvaser_msg_cardinfo2 cardinfo2;
398− struct kvaser_msg_busparams busparams;
399−
400− struct kvaser_msg_rx_can_header rx_can_header;
401− struct kvaser_msg_tx_acknowledge_header tx_acknowledge_header;
402−
403− union {
404− struct leaf_msg_softinfo softinfo;
405− struct leaf_msg_rx_can rx_can;
406− struct leaf_msg_chip_state_event chip_state_event;
407− struct leaf_msg_tx_acknowledge tx_acknowledge;
408− struct leaf_msg_error_event error_event;
409− struct leaf_msg_log_message log_message;
410− } __packed leaf;
411−
412− union {
413− struct usbcan_msg_softinfo softinfo;
414− struct usbcan_msg_rx_can rx_can;
415− struct usbcan_msg_chip_state_event chip_state_event;
416− struct usbcan_msg_tx_acknowledge tx_acknowledge;
417− struct usbcan_msg_error_event error_event;
418− } __packed usbcan;
419−
420− struct kvaser_msg_tx_can tx_can;
421− struct kvaser_msg_ctrl_mode ctrl_mode;
422− struct kvaser_msg_flush_queue flush_queue;
423− } u;
424−} __packed;
425−
426−/* Summary of a kvaser error event, for a unified Leaf/Usbcan error
427− * handling. Some discrepancies between the two families exist:
428− *
429− * - USBCAN firmware does not report M16C "error factors"
430− * - USBCAN controllers has difficulties reporting if the raised error
431− * event is for ch0 or ch1. They leave such arbitration to the OS
432− * driver by letting it compare error counters with previous values
433− * and decide the error event's channel. Thus for USBCAN, the channel
434− * field is only advisory.
435− */
436−struct kvaser_usb_error_summary {
437− u8 channel, status, txerr, rxerr;
438− union {
439− struct {
440− u8 error_factor;
441− } leaf;
442− struct {
443− u8 other_ch_status;
444− u8 error_state;
445− } usbcan;
446− };
447−};
448−
449−/* Context for an outstanding, not yet ACKed, transmission */
450−struct kvaser_usb_tx_urb_context {
451− struct kvaser_usb_net_priv *priv;
452− u32 echo_index;
453− int dlc;
454−};
455−
456−struct kvaser_usb {
457− struct usb_device *udev;
458− struct kvaser_usb_net_priv *nets[MAX_NET_DEVICES];
459−
460− struct usb_endpoint_descriptor *bulk_in, *bulk_out;
461− struct usb_anchor rx_submitted;
462−
463− /* @max_tx_urbs: Firmware-reported maximum number of outstanding,
464− * not yet ACKed, transmissions on this device. This value is
465− * also used as a sentinel for marking free tx contexts.
466− */
467− u32 fw_version;
468− unsigned int nchannels;
469− unsigned int max_tx_urbs;
470− enum kvaser_usb_family family;
471−
472− bool rxinitdone;
473− void *rxbuf[MAX_RX_URBS];
474− dma_addr_t rxbuf_dma[MAX_RX_URBS];
475−};
476−
477−struct kvaser_usb_net_priv {
478− struct can_priv can;
479− struct can_berr_counter bec;
480−
481− struct kvaser_usb *dev;
482− struct net_device *netdev;
483− int channel;
484−
485− struct completion start_comp, stop_comp;
486− struct usb_anchor tx_submitted;
487−
488− spinlock_t tx_contexts_lock;
489− int active_tx_contexts;
490− struct kvaser_usb_tx_urb_context tx_contexts[];
491−};
492−
493−static const struct usb_device_id kvaser_usb_table[] = {
494− /* Leaf family IDs */
495− { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_DEVEL_PRODUCT_ID) },
496− { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_PRODUCT_ID) },
497− { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_PRODUCT_ID),
498− .driver_info = KVASER_HAS_TXRX_ERRORS |
499− KVASER_HAS_SILENT_MODE },
500− { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_PRODUCT_ID),
501− .driver_info = KVASER_HAS_TXRX_ERRORS |
502− KVASER_HAS_SILENT_MODE },
503− { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_LS_PRODUCT_ID),
504− .driver_info = KVASER_HAS_TXRX_ERRORS |
505− KVASER_HAS_SILENT_MODE },
506− { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_SWC_PRODUCT_ID),
507− .driver_info = KVASER_HAS_TXRX_ERRORS |
508− KVASER_HAS_SILENT_MODE },
509− { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_LIN_PRODUCT_ID),
510− .driver_info = KVASER_HAS_TXRX_ERRORS |
511− KVASER_HAS_SILENT_MODE },
512− { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_LS_PRODUCT_ID),
513− .driver_info = KVASER_HAS_TXRX_ERRORS |
514− KVASER_HAS_SILENT_MODE },
515− { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_SWC_PRODUCT_ID),
516− .driver_info = KVASER_HAS_TXRX_ERRORS |
517− KVASER_HAS_SILENT_MODE },
518− { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_DEVEL_PRODUCT_ID),
519− .driver_info = KVASER_HAS_TXRX_ERRORS |
520− KVASER_HAS_SILENT_MODE },
521− { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_HSHS_PRODUCT_ID),
522− .driver_info = KVASER_HAS_TXRX_ERRORS |
523− KVASER_HAS_SILENT_MODE },
524− { USB_DEVICE(KVASER_VENDOR_ID, USB_UPRO_HSHS_PRODUCT_ID),
525− .driver_info = KVASER_HAS_TXRX_ERRORS },
526− { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_GI_PRODUCT_ID) },
527− { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_OBDII_PRODUCT_ID),
528− .driver_info = KVASER_HAS_TXRX_ERRORS |
529− KVASER_HAS_SILENT_MODE },
530− { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_HSLS_PRODUCT_ID),
531− .driver_info = KVASER_HAS_TXRX_ERRORS },
532− { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_CH_PRODUCT_ID),
533− .driver_info = KVASER_HAS_TXRX_ERRORS },
534− { USB_DEVICE(KVASER_VENDOR_ID, USB_BLACKBIRD_SPRO_PRODUCT_ID),
535− .driver_info = KVASER_HAS_TXRX_ERRORS },
536− { USB_DEVICE(KVASER_VENDOR_ID, USB_OEM_MERCURY_PRODUCT_ID),
537− .driver_info = KVASER_HAS_TXRX_ERRORS },
538− { USB_DEVICE(KVASER_VENDOR_ID, USB_OEM_LEAF_PRODUCT_ID),
539− .driver_info = KVASER_HAS_TXRX_ERRORS },
540− { USB_DEVICE(KVASER_VENDOR_ID, USB_CAN_R_PRODUCT_ID),
541− .driver_info = KVASER_HAS_TXRX_ERRORS },
542− { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_V2_PRODUCT_ID) },
543− { USB_DEVICE(KVASER_VENDOR_ID, USB_MINI_PCIE_HS_PRODUCT_ID) },
544− { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LIGHT_HS_V2_OEM_PRODUCT_ID) },
545− { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN_LIGHT_2HS_PRODUCT_ID) },
546− { USB_DEVICE(KVASER_VENDOR_ID, USB_MINI_PCIE_2HS_PRODUCT_ID) },
547−
548− /* USBCANII family IDs */
549− { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN2_PRODUCT_ID),
550− .driver_info = KVASER_HAS_TXRX_ERRORS },
551− { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN_REVB_PRODUCT_ID),
552− .driver_info = KVASER_HAS_TXRX_ERRORS },
553− { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMORATOR_PRODUCT_ID),
554− .driver_info = KVASER_HAS_TXRX_ERRORS },
555− { USB_DEVICE(KVASER_VENDOR_ID, USB_VCI2_PRODUCT_ID),
556− .driver_info = KVASER_HAS_TXRX_ERRORS },
557−
558− { }
559−};
560−MODULE_DEVICE_TABLE(usb, kvaser_usb_table);
561−
562−static inline int kvaser_usb_send_msg(const struct kvaser_usb *dev,
563− struct kvaser_msg *msg)
564−{
565− int actual_len;
566−
567− return usb_bulk_msg(dev->udev,
568− usb_sndbulkpipe(dev->udev,
569− dev->bulk_out->bEndpointAddress),
570− msg, msg->len, &actual_len,
571− USB_SEND_TIMEOUT);
572−}
573−
574−static int kvaser_usb_wait_msg(const struct kvaser_usb *dev, u8 id,
575− struct kvaser_msg *msg)
576−{
577− struct kvaser_msg *tmp;
578− void *buf;
579− int actual_len;
580− int err;
581− int pos;
582− unsigned long to = jiffies + msecs_to_jiffies(USB_RECV_TIMEOUT);
583−
584− buf = kzalloc(RX_BUFFER_SIZE, GFP_KERNEL);
585− if (!buf)
586− return -ENOMEM;
587−
588− do {
589− err = usb_bulk_msg(dev->udev,
590− usb_rcvbulkpipe(dev->udev,
591− dev->bulk_in->bEndpointAddress),
592− buf, RX_BUFFER_SIZE, &actual_len,
593− USB_RECV_TIMEOUT);
594− if (err < 0)
595− goto end;
596−
597− pos = 0;
598− while (pos <= actual_len - MSG_HEADER_LEN) {
599− tmp = buf + pos;
600−
601− /* Handle messages crossing the USB endpoint max packet
602− * size boundary. Check kvaser_usb_read_bulk_callback()
603− * for further details.
604− */
605− if (tmp->len == 0) {
606− pos = round_up(pos, le16_to_cpu(dev->bulk_in->
607− wMaxPacketSize));
608− continue;
609− }
610−
611− if (pos + tmp->len > actual_len) {
612− dev_err_ratelimited(dev->udev->dev.parent,
613− "Format error\n");
614− break;
615− }
616−
617− if (tmp->id == id) {
618− memcpy(msg, tmp, tmp->len);
619− goto end;
620− }
621−
622− pos += tmp->len;
623− }
624− } while (time_before(jiffies, to));
625−
626− err = -EINVAL;
627−
628−end:
629− kfree(buf);
630−
631− return err;
632−}
633−
634−static int kvaser_usb_send_simple_msg(const struct kvaser_usb *dev,
635− u8 msg_id, int channel)
636−{
637− struct kvaser_msg *msg;
638− int rc;
639−
640− msg = kmalloc(sizeof(*msg), GFP_KERNEL);
641− if (!msg)
642− return -ENOMEM;
643−
644− msg->id = msg_id;
645− msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_simple);
646− msg->u.simple.channel = channel;
647− msg->u.simple.tid = 0xff;
648−
649− rc = kvaser_usb_send_msg(dev, msg);
650−
651− kfree(msg);
652− return rc;
653−}
654−
655−static int kvaser_usb_get_software_info(struct kvaser_usb *dev)
656−{
657− struct kvaser_msg msg;
658− int err;
659−
660− err = kvaser_usb_send_simple_msg(dev, CMD_GET_SOFTWARE_INFO, 0);
661− if (err)
662− return err;
663−
664− err = kvaser_usb_wait_msg(dev, CMD_GET_SOFTWARE_INFO_REPLY, &msg);
665− if (err)
666− return err;
667−
668− switch (dev->family) {
669− case KVASER_LEAF:
670− dev->fw_version = le32_to_cpu(msg.u.leaf.softinfo.fw_version);
671− dev->max_tx_urbs =
672− le16_to_cpu(msg.u.leaf.softinfo.max_outstanding_tx);
673− break;
674− case KVASER_USBCAN:
675− dev->fw_version = le32_to_cpu(msg.u.usbcan.softinfo.fw_version);
676− dev->max_tx_urbs =
677− le16_to_cpu(msg.u.usbcan.softinfo.max_outstanding_tx);
678− break;
679− }
680−
681− return 0;
682−}
683−
684−static int kvaser_usb_get_card_info(struct kvaser_usb *dev)
685−{
686− struct kvaser_msg msg;
687− int err;
688−
689− err = kvaser_usb_send_simple_msg(dev, CMD_GET_CARD_INFO, 0);
690− if (err)
691− return err;
692−
693− err = kvaser_usb_wait_msg(dev, CMD_GET_CARD_INFO_REPLY, &msg);
694− if (err)
695− return err;
696−
697− dev->nchannels = msg.u.cardinfo.nchannels;
698− if ((dev->nchannels > MAX_NET_DEVICES) ||
699− (dev->family == KVASER_USBCAN &&
700− dev->nchannels > MAX_USBCAN_NET_DEVICES))
701− return -EINVAL;
702−
703− return 0;
704−}
705−
706−static void kvaser_usb_tx_acknowledge(const struct kvaser_usb *dev,
707− const struct kvaser_msg *msg)
708−{
709− struct net_device_stats *stats;
710− struct kvaser_usb_tx_urb_context *context;
711− struct kvaser_usb_net_priv *priv;
712− struct sk_buff *skb;
713− struct can_frame *cf;
714− unsigned long flags;
715− u8 channel, tid;
716−
717− channel = msg->u.tx_acknowledge_header.channel;
718− tid = msg->u.tx_acknowledge_header.tid;
719−
720− if (channel >= dev->nchannels) {
721− dev_err(dev->udev->dev.parent,
722− "Invalid channel number (%d)\n", channel);
723− return;
724− }
725−
726− priv = dev->nets[channel];
727−
728− if (!netif_device_present(priv->netdev))
729− return;
730−
731− stats = &priv->netdev->stats;
732−
733− context = &priv->tx_contexts[tid % dev->max_tx_urbs];
734−
735− /* Sometimes the state change doesn't come after a bus-off event */
736− if (priv->can.restart_ms &&
737− (priv->can.state >= CAN_STATE_BUS_OFF)) {
738− skb = alloc_can_err_skb(priv->netdev, &cf);
739− if (skb) {
740− cf->can_id |= CAN_ERR_RESTARTED;
741−
742− stats->rx_packets++;
743− stats->rx_bytes += cf->can_dlc;
744− netif_rx(skb);
745− } else {
746− netdev_err(priv->netdev,
747− "No memory left for err_skb\n");
748− }
749−
750− priv->can.can_stats.restarts++;
751− netif_carrier_on(priv->netdev);
752−
753− priv->can.state = CAN_STATE_ERROR_ACTIVE;
754− }
755−
756− stats->tx_packets++;
757− stats->tx_bytes += context->dlc;
758−
759− spin_lock_irqsave(&priv->tx_contexts_lock, flags);
760−
761− can_get_echo_skb(priv->netdev, context->echo_index);
762− context->echo_index = dev->max_tx_urbs;
763− --priv->active_tx_contexts;
764− netif_wake_queue(priv->netdev);
765−
766− spin_unlock_irqrestore(&priv->tx_contexts_lock, flags);
767−}
768−
769−static void kvaser_usb_simple_msg_callback(struct urb *urb)
770−{
771− struct net_device *netdev = urb->context;
772−
773− kfree(urb->transfer_buffer);
774−
775− if (urb->status)
776− netdev_warn(netdev, "urb status received: %d\n",
777− urb->status);
778−}
779−
780−static int kvaser_usb_simple_msg_async(struct kvaser_usb_net_priv *priv,
781− u8 msg_id)
782−{
783− struct kvaser_usb *dev = priv->dev;
784− struct net_device *netdev = priv->netdev;
785− struct kvaser_msg *msg;
786− struct urb *urb;
787− void *buf;
788− int err;
789−
790− urb = usb_alloc_urb(0, GFP_ATOMIC);
791− if (!urb)
792− return -ENOMEM;
793−
794− buf = kmalloc(sizeof(struct kvaser_msg), GFP_ATOMIC);
795− if (!buf) {
796− usb_free_urb(urb);
797− return -ENOMEM;
798− }
799−
800− msg = (struct kvaser_msg *)buf;
801− msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_simple);
802− msg->id = msg_id;
803− msg->u.simple.channel = priv->channel;
804−
805− usb_fill_bulk_urb(urb, dev->udev,
806− usb_sndbulkpipe(dev->udev,
807− dev->bulk_out->bEndpointAddress),
808− buf, msg->len,
809− kvaser_usb_simple_msg_callback, netdev);
810− usb_anchor_urb(urb, &priv->tx_submitted);
811−
812− err = usb_submit_urb(urb, GFP_ATOMIC);
813− if (err) {
814− netdev_err(netdev, "Error transmitting URB\n");
815− usb_unanchor_urb(urb);
816− kfree(buf);
817− usb_free_urb(urb);
818− return err;
819− }
820−
821− usb_free_urb(urb);
822−
823− return 0;
824−}
825−
826−static void kvaser_usb_rx_error_update_can_state(struct kvaser_usb_net_priv *priv,
827− const struct kvaser_usb_error_summary *es,
828− struct can_frame *cf)
829−{
830− struct kvaser_usb *dev = priv->dev;
831− struct net_device_stats *stats = &priv->netdev->stats;
832− enum can_state cur_state, new_state, tx_state, rx_state;
833−
834− netdev_dbg(priv->netdev, "Error status: 0x%02x\n", es->status);
835−
836− new_state = cur_state = priv->can.state;
837−
838− if (es->status & (M16C_STATE_BUS_OFF | M16C_STATE_BUS_RESET))
839− new_state = CAN_STATE_BUS_OFF;
840− else if (es->status & M16C_STATE_BUS_PASSIVE)
841− new_state = CAN_STATE_ERROR_PASSIVE;
842− else if (es->status & M16C_STATE_BUS_ERROR) {
843− /* Guard against spurious error events after a busoff */
844− if (cur_state < CAN_STATE_BUS_OFF) {
845− if ((es->txerr >= 128) || (es->rxerr >= 128))
846− new_state = CAN_STATE_ERROR_PASSIVE;
847− else if ((es->txerr >= 96) || (es->rxerr >= 96))
848− new_state = CAN_STATE_ERROR_WARNING;
849− else if (cur_state > CAN_STATE_ERROR_ACTIVE)
850− new_state = CAN_STATE_ERROR_ACTIVE;
851− }
852− }
853−
854− if (!es->status)
855− new_state = CAN_STATE_ERROR_ACTIVE;
856−
857− if (new_state != cur_state) {
858− tx_state = (es->txerr >= es->rxerr) ? new_state : 0;
859− rx_state = (es->txerr <= es->rxerr) ? new_state : 0;
860−
861− can_change_state(priv->netdev, cf, tx_state, rx_state);
862− }
863−
864− if (priv->can.restart_ms &&
865− (cur_state >= CAN_STATE_BUS_OFF) &&
866− (new_state < CAN_STATE_BUS_OFF)) {
867− priv->can.can_stats.restarts++;
868− }
869−
870− switch (dev->family) {
871− case KVASER_LEAF:
872− if (es->leaf.error_factor) {
873− priv->can.can_stats.bus_error++;
874− stats->rx_errors++;
875− }
876− break;
877− case KVASER_USBCAN:
878− if (es->usbcan.error_state & USBCAN_ERROR_STATE_TX_ERROR)
879− stats->tx_errors++;
880− if (es->usbcan.error_state & USBCAN_ERROR_STATE_RX_ERROR)
881− stats->rx_errors++;
882− if (es->usbcan.error_state & USBCAN_ERROR_STATE_BUSERROR) {
883− priv->can.can_stats.bus_error++;
884− }
885− break;
886− }
887−
888− priv->bec.txerr = es->txerr;
889− priv->bec.rxerr = es->rxerr;
890−}
891−
892−static void kvaser_usb_rx_error(const struct kvaser_usb *dev,
893− const struct kvaser_usb_error_summary *es)
894−{
895− struct can_frame *cf, tmp_cf = { .can_id = CAN_ERR_FLAG, .can_dlc = CAN_ERR_DLC };
896− struct sk_buff *skb;
897− struct net_device_stats *stats;
898− struct kvaser_usb_net_priv *priv;
899− enum can_state old_state, new_state;
900−
901− if (es->channel >= dev->nchannels) {
902− dev_err(dev->udev->dev.parent,
903− "Invalid channel number (%d)\n", es->channel);
904− return;
905− }
906−
907− priv = dev->nets[es->channel];
908− stats = &priv->netdev->stats;
909−
910− /* Update all of the can interface's state and error counters before
911− * trying any memory allocation that can actually fail with -ENOMEM.
912− *
913− * We send a temporary stack-allocated error can frame to
914− * can_change_state() for the very same reason.
915− *
916− * TODO: Split can_change_state() responsibility between updating the
917− * can interface's state and counters, and the setting up of can error
918− * frame ID and data to userspace. Remove stack allocation afterwards.
919− */
920− old_state = priv->can.state;
921− kvaser_usb_rx_error_update_can_state(priv, es, &tmp_cf);
922− new_state = priv->can.state;
923−
924− skb = alloc_can_err_skb(priv->netdev, &cf);
925− if (!skb) {
926− stats->rx_dropped++;
927− return;
928− }
929− memcpy(cf, &tmp_cf, sizeof(*cf));
930−
931− if (new_state != old_state) {
932− if (es->status &
933− (M16C_STATE_BUS_OFF | M16C_STATE_BUS_RESET)) {
934− if (!priv->can.restart_ms)
935− kvaser_usb_simple_msg_async(priv, CMD_STOP_CHIP);
936− netif_carrier_off(priv->netdev);
937− }
938−
939− if (priv->can.restart_ms &&
940− (old_state >= CAN_STATE_BUS_OFF) &&
941− (new_state < CAN_STATE_BUS_OFF)) {
942− cf->can_id |= CAN_ERR_RESTARTED;
943− netif_carrier_on(priv->netdev);
944− }
945− }
946−
947− switch (dev->family) {
948− case KVASER_LEAF:
949− if (es->leaf.error_factor) {
950− cf->can_id |= CAN_ERR_BUSERROR | CAN_ERR_PROT;
951−
952− if (es->leaf.error_factor & M16C_EF_ACKE)
953− cf->data[3] = CAN_ERR_PROT_LOC_ACK;
954− if (es->leaf.error_factor & M16C_EF_CRCE)
955− cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
956− if (es->leaf.error_factor & M16C_EF_FORME)
957− cf->data[2] |= CAN_ERR_PROT_FORM;
958− if (es->leaf.error_factor & M16C_EF_STFE)
959− cf->data[2] |= CAN_ERR_PROT_STUFF;
960− if (es->leaf.error_factor & M16C_EF_BITE0)
961− cf->data[2] |= CAN_ERR_PROT_BIT0;
962− if (es->leaf.error_factor & M16C_EF_BITE1)
963− cf->data[2] |= CAN_ERR_PROT_BIT1;
964− if (es->leaf.error_factor & M16C_EF_TRE)
965− cf->data[2] |= CAN_ERR_PROT_TX;
966− }
967− break;
968− case KVASER_USBCAN:
969− if (es->usbcan.error_state & USBCAN_ERROR_STATE_BUSERROR) {
970− cf->can_id |= CAN_ERR_BUSERROR;
971− }
972− break;
973− }
974−
975− cf->data[6] = es->txerr;
976− cf->data[7] = es->rxerr;
977−
978− stats->rx_packets++;
979− stats->rx_bytes += cf->can_dlc;
980− netif_rx(skb);
981−}
982−
983−/* For USBCAN, report error to userspace iff the channels's errors counter
984− * has changed, or we're the only channel seeing a bus error state.
985− */
986−static void kvaser_usbcan_conditionally_rx_error(const struct kvaser_usb *dev,
987− struct kvaser_usb_error_summary *es)
988−{
989− struct kvaser_usb_net_priv *priv;
990− int channel;
991− bool report_error;
992−
993− channel = es->channel;
994− if (channel >= dev->nchannels) {
995− dev_err(dev->udev->dev.parent,
996− "Invalid channel number (%d)\n", channel);
997− return;
998− }
999−
1000− priv = dev->nets[channel];
1001− report_error = false;
1002−
1003− if (es->txerr != priv->bec.txerr) {
1004− es->usbcan.error_state |= USBCAN_ERROR_STATE_TX_ERROR;
1005− report_error = true;
1006− }
1007− if (es->rxerr != priv->bec.rxerr) {
1008− es->usbcan.error_state |= USBCAN_ERROR_STATE_RX_ERROR;
1009− report_error = true;
1010− }
1011− if ((es->status & M16C_STATE_BUS_ERROR) &&
1012− !(es->usbcan.other_ch_status & M16C_STATE_BUS_ERROR)) {
1013− es->usbcan.error_state |= USBCAN_ERROR_STATE_BUSERROR;
1014− report_error = true;
1015− }
1016−
1017− if (report_error)
1018− kvaser_usb_rx_error(dev, es);
1019−}
1020−
1021−static void kvaser_usbcan_rx_error(const struct kvaser_usb *dev,
1022− const struct kvaser_msg *msg)
1023−{
1024− struct kvaser_usb_error_summary es = { };
1025−
1026− switch (msg->id) {
1027− /* Sometimes errors are sent as unsolicited chip state events */
1028− case CMD_CHIP_STATE_EVENT:
1029− es.channel = msg->u.usbcan.chip_state_event.channel;
1030− es.status = msg->u.usbcan.chip_state_event.status;
1031− es.txerr = msg->u.usbcan.chip_state_event.tx_errors_count;
1032− es.rxerr = msg->u.usbcan.chip_state_event.rx_errors_count;
1033− kvaser_usbcan_conditionally_rx_error(dev, &es);
1034− break;
1035−
1036− case CMD_CAN_ERROR_EVENT:
1037− es.channel = 0;
1038− es.status = msg->u.usbcan.error_event.status_ch0;
1039− es.txerr = msg->u.usbcan.error_event.tx_errors_count_ch0;
1040− es.rxerr = msg->u.usbcan.error_event.rx_errors_count_ch0;
1041− es.usbcan.other_ch_status =
1042− msg->u.usbcan.error_event.status_ch1;
1043− kvaser_usbcan_conditionally_rx_error(dev, &es);
1044−
1045− /* The USBCAN firmware supports up to 2 channels.
1046− * Now that ch0 was checked, check if ch1 has any errors.
1047− */
1048− if (dev->nchannels == MAX_USBCAN_NET_DEVICES) {
1049− es.channel = 1;
1050− es.status = msg->u.usbcan.error_event.status_ch1;
1051− es.txerr = msg->u.usbcan.error_event.tx_errors_count_ch1;
1052− es.rxerr = msg->u.usbcan.error_event.rx_errors_count_ch1;
1053− es.usbcan.other_ch_status =
1054− msg->u.usbcan.error_event.status_ch0;
1055− kvaser_usbcan_conditionally_rx_error(dev, &es);
1056− }
1057− break;
1058−
1059− default:
1060− dev_err(dev->udev->dev.parent, "Invalid msg id (%d)\n",
1061− msg->id);
1062− }
1063−}
1064−
1065−static void kvaser_leaf_rx_error(const struct kvaser_usb *dev,
1066− const struct kvaser_msg *msg)
1067−{
1068− struct kvaser_usb_error_summary es = { };
1069−
1070− switch (msg->id) {
1071− case CMD_CAN_ERROR_EVENT:
1072− es.channel = msg->u.leaf.error_event.channel;
1073− es.status = msg->u.leaf.error_event.status;
1074− es.txerr = msg->u.leaf.error_event.tx_errors_count;
1075− es.rxerr = msg->u.leaf.error_event.rx_errors_count;
1076− es.leaf.error_factor = msg->u.leaf.error_event.error_factor;
1077− break;
1078− case CMD_LEAF_LOG_MESSAGE:
1079− es.channel = msg->u.leaf.log_message.channel;
1080− es.status = msg->u.leaf.log_message.data[0];
1081− es.txerr = msg->u.leaf.log_message.data[2];
1082− es.rxerr = msg->u.leaf.log_message.data[3];
1083− es.leaf.error_factor = msg->u.leaf.log_message.data[1];
1084− break;
1085− case CMD_CHIP_STATE_EVENT:
1086− es.channel = msg->u.leaf.chip_state_event.channel;
1087− es.status = msg->u.leaf.chip_state_event.status;
1088− es.txerr = msg->u.leaf.chip_state_event.tx_errors_count;
1089− es.rxerr = msg->u.leaf.chip_state_event.rx_errors_count;
1090− es.leaf.error_factor = 0;
1091− break;
1092− default:
1093− dev_err(dev->udev->dev.parent, "Invalid msg id (%d)\n",
1094− msg->id);
1095− return;
1096− }
1097−
1098− kvaser_usb_rx_error(dev, &es);
1099−}
1100−
1101−static void kvaser_usb_rx_can_err(const struct kvaser_usb_net_priv *priv,
1102− const struct kvaser_msg *msg)
1103−{
1104− struct can_frame *cf;
1105− struct sk_buff *skb;
1106− struct net_device_stats *stats = &priv->netdev->stats;
1107−
1108− if (msg->u.rx_can_header.flag & (MSG_FLAG_ERROR_FRAME |
1109− MSG_FLAG_NERR)) {
1110− netdev_err(priv->netdev, "Unknown error (flags: 0x%02x)\n",
1111− msg->u.rx_can_header.flag);
1112−
1113− stats->rx_errors++;
1114− return;
1115− }
1116−
1117− if (msg->u.rx_can_header.flag & MSG_FLAG_OVERRUN) {
1118− stats->rx_over_errors++;
1119− stats->rx_errors++;
1120−
1121− skb = alloc_can_err_skb(priv->netdev, &cf);
1122− if (!skb) {
1123− stats->rx_dropped++;
1124− return;
1125− }
1126−
1127− cf->can_id |= CAN_ERR_CRTL;
1128− cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
1129−
1130− stats->rx_packets++;
1131− stats->rx_bytes += cf->can_dlc;
1132− netif_rx(skb);
1133− }
1134−}
1135−
1136−static void kvaser_usb_rx_can_msg(const struct kvaser_usb *dev,
1137− const struct kvaser_msg *msg)
1138−{
1139− struct kvaser_usb_net_priv *priv;
1140− struct can_frame *cf;
1141− struct sk_buff *skb;
1142− struct net_device_stats *stats;
1143− u8 channel = msg->u.rx_can_header.channel;
1144− const u8 *rx_msg = NULL; /* GCC */
1145−
1146− if (channel >= dev->nchannels) {
1147− dev_err(dev->udev->dev.parent,
1148− "Invalid channel number (%d)\n", channel);
1149− return;
1150− }
1151−
1152− priv = dev->nets[channel];
1153− stats = &priv->netdev->stats;
1154−
1155− if ((msg->u.rx_can_header.flag & MSG_FLAG_ERROR_FRAME) &&
1156− (dev->family == KVASER_LEAF && msg->id == CMD_LEAF_LOG_MESSAGE)) {
1157− kvaser_leaf_rx_error(dev, msg);
1158− return;
1159− } else if (msg->u.rx_can_header.flag & (MSG_FLAG_ERROR_FRAME |
1160− MSG_FLAG_NERR |
1161− MSG_FLAG_OVERRUN)) {
1162− kvaser_usb_rx_can_err(priv, msg);
1163− return;
1164− } else if (msg->u.rx_can_header.flag & ~MSG_FLAG_REMOTE_FRAME) {
1165− netdev_warn(priv->netdev,
1166− "Unhandled frame (flags: 0x%02x)",
1167− msg->u.rx_can_header.flag);
1168− return;
1169− }
1170−
1171− switch (dev->family) {
1172− case KVASER_LEAF:
1173− rx_msg = msg->u.leaf.rx_can.msg;
1174− break;
1175− case KVASER_USBCAN:
1176− rx_msg = msg->u.usbcan.rx_can.msg;
1177− break;
1178− }
1179−
1180− skb = alloc_can_skb(priv->netdev, &cf);
1181− if (!skb) {
1182− stats->rx_dropped++;
1183− return;
1184− }
1185−
1186− if (dev->family == KVASER_LEAF && msg->id == CMD_LEAF_LOG_MESSAGE) {
1187− cf->can_id = le32_to_cpu(msg->u.leaf.log_message.id);
1188− if (cf->can_id & KVASER_EXTENDED_FRAME)
1189− cf->can_id &= CAN_EFF_MASK | CAN_EFF_FLAG;
1190− else
1191− cf->can_id &= CAN_SFF_MASK;
1192−
1193− cf->can_dlc = get_can_dlc(msg->u.leaf.log_message.dlc);
1194−
1195− if (msg->u.leaf.log_message.flags & MSG_FLAG_REMOTE_FRAME)
1196− cf->can_id |= CAN_RTR_FLAG;
1197− else
1198− memcpy(cf->data, &msg->u.leaf.log_message.data,
1199− cf->can_dlc);
1200− } else {
1201− cf->can_id = ((rx_msg[0] & 0x1f) << 6) | (rx_msg[1] & 0x3f);
1202−
1203− if (msg->id == CMD_RX_EXT_MESSAGE) {
1204− cf->can_id <<= 18;
1205− cf->can_id |= ((rx_msg[2] & 0x0f) << 14) |
1206− ((rx_msg[3] & 0xff) << 6) |
1207− (rx_msg[4] & 0x3f);
1208− cf->can_id |= CAN_EFF_FLAG;
1209− }
1210−
1211− cf->can_dlc = get_can_dlc(rx_msg[5]);
1212−
1213− if (msg->u.rx_can_header.flag & MSG_FLAG_REMOTE_FRAME)
1214− cf->can_id |= CAN_RTR_FLAG;
1215− else
1216− memcpy(cf->data, &rx_msg[6],
1217− cf->can_dlc);
1218− }
1219−
1220− stats->rx_packets++;
1221− stats->rx_bytes += cf->can_dlc;
1222− netif_rx(skb);
1223−}
1224−
1225−static void kvaser_usb_start_chip_reply(const struct kvaser_usb *dev,
1226− const struct kvaser_msg *msg)
1227−{
1228− struct kvaser_usb_net_priv *priv;
1229− u8 channel = msg->u.simple.channel;
1230−
1231− if (channel >= dev->nchannels) {
1232− dev_err(dev->udev->dev.parent,
1233− "Invalid channel number (%d)\n", channel);
1234− return;
1235− }
1236−
1237− priv = dev->nets[channel];
1238−
1239− if (completion_done(&priv->start_comp) &&
1240− netif_queue_stopped(priv->netdev)) {
1241− netif_wake_queue(priv->netdev);
1242− } else {
1243− netif_start_queue(priv->netdev);
1244− complete(&priv->start_comp);
1245− }
1246−}
1247−
1248−static void kvaser_usb_stop_chip_reply(const struct kvaser_usb *dev,
1249− const struct kvaser_msg *msg)
1250−{
1251− struct kvaser_usb_net_priv *priv;
1252− u8 channel = msg->u.simple.channel;
1253−
1254− if (channel >= dev->nchannels) {
1255− dev_err(dev->udev->dev.parent,
1256− "Invalid channel number (%d)\n", channel);
1257− return;
1258− }
1259−
1260− priv = dev->nets[channel];
1261−
1262− complete(&priv->stop_comp);
1263−}
1264−
1265−static void kvaser_usb_handle_message(const struct kvaser_usb *dev,
1266− const struct kvaser_msg *msg)
1267−{
1268− switch (msg->id) {
1269− case CMD_START_CHIP_REPLY:
1270− kvaser_usb_start_chip_reply(dev, msg);
1271− break;
1272−
1273− case CMD_STOP_CHIP_REPLY:
1274− kvaser_usb_stop_chip_reply(dev, msg);
1275− break;
1276−
1277− case CMD_RX_STD_MESSAGE:
1278− case CMD_RX_EXT_MESSAGE:
1279− kvaser_usb_rx_can_msg(dev, msg);
1280− break;
1281−
1282− case CMD_LEAF_LOG_MESSAGE:
1283− if (dev->family != KVASER_LEAF)
1284− goto warn;
1285− kvaser_usb_rx_can_msg(dev, msg);
1286− break;
1287−
1288− case CMD_CHIP_STATE_EVENT:
1289− case CMD_CAN_ERROR_EVENT:
1290− if (dev->family == KVASER_LEAF)
1291− kvaser_leaf_rx_error(dev, msg);
1292− else
1293− kvaser_usbcan_rx_error(dev, msg);
1294− break;
1295−
1296− case CMD_TX_ACKNOWLEDGE:
1297− kvaser_usb_tx_acknowledge(dev, msg);
1298− break;
1299−
1300− /* Ignored messages */
1301− case CMD_USBCAN_CLOCK_OVERFLOW_EVENT:
1302− if (dev->family != KVASER_USBCAN)
1303− goto warn;
1304− break;
1305−
1306− case CMD_FLUSH_QUEUE_REPLY:
1307− if (dev->family != KVASER_LEAF)
1308− goto warn;
1309− break;
1310−
1311− default:
1312−warn: dev_warn(dev->udev->dev.parent,
1313− "Unhandled message (%d)\n", msg->id);
1314− break;
1315− }
1316−}
1317−
1318−static void kvaser_usb_read_bulk_callback(struct urb *urb)
1319−{
1320− struct kvaser_usb *dev = urb->context;
1321− struct kvaser_msg *msg;
1322− int pos = 0;
1323− int err, i;
1324−
1325− switch (urb->status) {
1326− case 0:
1327− break;
1328− case -ENOENT:
1329− case -EPIPE:
1330− case -EPROTO:
1331− case -ESHUTDOWN:
1332− return;
1333− default:
1334− dev_info(dev->udev->dev.parent, "Rx URB aborted (%d)\n",
1335− urb->status);
1336− goto resubmit_urb;
1337− }
1338−
1339− while (pos <= (int)(urb->actual_length - MSG_HEADER_LEN)) {
1340− msg = urb->transfer_buffer + pos;
1341−
1342− /* The Kvaser firmware can only read and write messages that
1343− * does not cross the USB's endpoint wMaxPacketSize boundary.
1344− * If a follow-up command crosses such boundary, firmware puts
1345− * a placeholder zero-length command in its place then aligns
1346− * the real command to the next max packet size.
1347− *
1348− * Handle such cases or we're going to miss a significant
1349− * number of events in case of a heavy rx load on the bus.
1350− */
1351− if (msg->len == 0) {
1352− pos = round_up(pos, le16_to_cpu(dev->bulk_in->
1353− wMaxPacketSize));
1354− continue;
1355− }
1356−
1357− if (pos + msg->len > urb->actual_length) {
1358− dev_err_ratelimited(dev->udev->dev.parent,
1359− "Format error\n");
1360− break;
1361− }
1362−
1363− kvaser_usb_handle_message(dev, msg);
1364− pos += msg->len;
1365− }
1366−
1367−resubmit_urb:
1368− usb_fill_bulk_urb(urb, dev->udev,
1369− usb_rcvbulkpipe(dev->udev,
1370− dev->bulk_in->bEndpointAddress),
1371− urb->transfer_buffer, RX_BUFFER_SIZE,
1372− kvaser_usb_read_bulk_callback, dev);
1373−
1374− err = usb_submit_urb(urb, GFP_ATOMIC);
1375− if (err == -ENODEV) {
1376− for (i = 0; i < dev->nchannels; i++) {
1377− if (!dev->nets[i])
1378− continue;
1379−
1380− netif_device_detach(dev->nets[i]->netdev);
1381− }
1382− } else if (err) {
1383− dev_err(dev->udev->dev.parent,
1384− "Failed resubmitting read bulk urb: %d\n", err);
1385− }
1386−
1387− return;
1388−}
1389−
1390−static int kvaser_usb_setup_rx_urbs(struct kvaser_usb *dev)
1391−{
1392− int i, err = 0;
1393−
1394− if (dev->rxinitdone)
1395− return 0;
1396−
1397− for (i = 0; i < MAX_RX_URBS; i++) {
1398− struct urb *urb = NULL;
1399− u8 *buf = NULL;
1400− dma_addr_t buf_dma;
1401−
1402− urb = usb_alloc_urb(0, GFP_KERNEL);
1403− if (!urb) {
1404− err = -ENOMEM;
1405− break;
1406− }
1407−
1408− buf = usb_alloc_coherent(dev->udev, RX_BUFFER_SIZE,
1409− GFP_KERNEL, &buf_dma);
1410− if (!buf) {
1411− dev_warn(dev->udev->dev.parent,
1412− "No memory left for USB buffer\n");
1413− usb_free_urb(urb);
1414− err = -ENOMEM;
1415− break;
1416− }
1417−
1418− usb_fill_bulk_urb(urb, dev->udev,
1419− usb_rcvbulkpipe(dev->udev,
1420− dev->bulk_in->bEndpointAddress),
1421− buf, RX_BUFFER_SIZE,
1422− kvaser_usb_read_bulk_callback,
1423− dev);
1424− urb->transfer_dma = buf_dma;
1425− urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1426− usb_anchor_urb(urb, &dev->rx_submitted);
1427−
1428− err = usb_submit_urb(urb, GFP_KERNEL);
1429− if (err) {
1430− usb_unanchor_urb(urb);
1431− usb_free_coherent(dev->udev, RX_BUFFER_SIZE, buf,
1432− buf_dma);
1433− usb_free_urb(urb);
1434− break;
1435− }
1436−
1437− dev->rxbuf[i] = buf;
1438− dev->rxbuf_dma[i] = buf_dma;
1439−
1440− usb_free_urb(urb);
1441− }
1442−
1443− if (i == 0) {
1444− dev_warn(dev->udev->dev.parent,
1445− "Cannot setup read URBs, error %d\n", err);
1446− return err;
1447− } else if (i < MAX_RX_URBS) {
1448− dev_warn(dev->udev->dev.parent,
1449− "RX performances may be slow\n");
1450− }
1451−
1452− dev->rxinitdone = true;
1453−
1454− return 0;
1455−}
1456−
1457−static int kvaser_usb_set_opt_mode(const struct kvaser_usb_net_priv *priv)
1458−{
1459− struct kvaser_msg *msg;
1460− int rc;
1461−
1462− msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1463− if (!msg)
1464− return -ENOMEM;
1465−
1466− msg->id = CMD_SET_CTRL_MODE;
1467− msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_ctrl_mode);
1468− msg->u.ctrl_mode.tid = 0xff;
1469− msg->u.ctrl_mode.channel = priv->channel;
1470−
1471− if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
1472− msg->u.ctrl_mode.ctrl_mode = KVASER_CTRL_MODE_SILENT;
1473− else
1474− msg->u.ctrl_mode.ctrl_mode = KVASER_CTRL_MODE_NORMAL;
1475−
1476− rc = kvaser_usb_send_msg(priv->dev, msg);
1477−
1478− kfree(msg);
1479− return rc;
1480−}
1481−
1482−static int kvaser_usb_start_chip(struct kvaser_usb_net_priv *priv)
1483−{
1484− int err;
1485−
1486− init_completion(&priv->start_comp);
1487−
1488− err = kvaser_usb_send_simple_msg(priv->dev, CMD_START_CHIP,
1489− priv->channel);
1490− if (err)
1491− return err;
1492−
1493− if (!wait_for_completion_timeout(&priv->start_comp,
1494− msecs_to_jiffies(START_TIMEOUT)))
1495− return -ETIMEDOUT;
1496−
1497− return 0;
1498−}
1499−
1500−static int kvaser_usb_open(struct net_device *netdev)
1501−{
1502− struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1503− struct kvaser_usb *dev = priv->dev;
1504− int err;
1505−
1506− err = open_candev(netdev);
1507− if (err)
1508− return err;
1509−
1510− err = kvaser_usb_setup_rx_urbs(dev);
1511− if (err)
1512− goto error;
1513−
1514− err = kvaser_usb_set_opt_mode(priv);
1515− if (err)
1516− goto error;
1517−
1518− err = kvaser_usb_start_chip(priv);
1519− if (err) {
1520− netdev_warn(netdev, "Cannot start device, error %d\n", err);
1521− goto error;
1522− }
1523−
1524− priv->can.state = CAN_STATE_ERROR_ACTIVE;
1525−
1526− return 0;
1527−
1528−error:
1529− close_candev(netdev);
1530− return err;
1531−}
1532−
1533−static void kvaser_usb_reset_tx_urb_contexts(struct kvaser_usb_net_priv *priv)
1534−{
1535− int i, max_tx_urbs;
1536−
1537− max_tx_urbs = priv->dev->max_tx_urbs;
1538−
1539− priv->active_tx_contexts = 0;
1540− for (i = 0; i < max_tx_urbs; i++)
1541− priv->tx_contexts[i].echo_index = max_tx_urbs;
1542−}
1543−
1544−/* This method might sleep. Do not call it in the atomic context
1545− * of URB completions.
1546− */
1547−static void kvaser_usb_unlink_tx_urbs(struct kvaser_usb_net_priv *priv)
1548−{
1549− usb_kill_anchored_urbs(&priv->tx_submitted);
1550− kvaser_usb_reset_tx_urb_contexts(priv);
1551−}
1552−
1553−static void kvaser_usb_unlink_all_urbs(struct kvaser_usb *dev)
1554−{
1555− int i;
1556−
1557− usb_kill_anchored_urbs(&dev->rx_submitted);
1558−
1559− for (i = 0; i < MAX_RX_URBS; i++)
1560− usb_free_coherent(dev->udev, RX_BUFFER_SIZE,
1561− dev->rxbuf[i],
1562− dev->rxbuf_dma[i]);
1563−
1564− for (i = 0; i < dev->nchannels; i++) {
1565− struct kvaser_usb_net_priv *priv = dev->nets[i];
1566−
1567− if (priv)
1568− kvaser_usb_unlink_tx_urbs(priv);
1569− }
1570−}
1571−
1572−static int kvaser_usb_stop_chip(struct kvaser_usb_net_priv *priv)
1573−{
1574− int err;
1575−
1576− init_completion(&priv->stop_comp);
1577−
1578− err = kvaser_usb_send_simple_msg(priv->dev, CMD_STOP_CHIP,
1579− priv->channel);
1580− if (err)
1581− return err;
1582−
1583− if (!wait_for_completion_timeout(&priv->stop_comp,
1584− msecs_to_jiffies(STOP_TIMEOUT)))
1585− return -ETIMEDOUT;
1586−
1587− return 0;
1588−}
1589−
1590−static int kvaser_usb_flush_queue(struct kvaser_usb_net_priv *priv)
1591−{
1592− struct kvaser_msg *msg;
1593− int rc;
1594−
1595− msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1596− if (!msg)
1597− return -ENOMEM;
1598−
1599− msg->id = CMD_FLUSH_QUEUE;
1600− msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_flush_queue);
1601− msg->u.flush_queue.channel = priv->channel;
1602− msg->u.flush_queue.flags = 0x00;
1603−
1604− rc = kvaser_usb_send_msg(priv->dev, msg);
1605−
1606− kfree(msg);
1607− return rc;
1608−}
1609−
1610−static int kvaser_usb_close(struct net_device *netdev)
1611−{
1612− struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1613− struct kvaser_usb *dev = priv->dev;
1614− int err;
1615−
1616− netif_stop_queue(netdev);
1617−
1618− err = kvaser_usb_flush_queue(priv);
1619− if (err)
1620− netdev_warn(netdev, "Cannot flush queue, error %d\n", err);
1621−
1622− err = kvaser_usb_send_simple_msg(dev, CMD_RESET_CHIP, priv->channel);
1623− if (err)
1624− netdev_warn(netdev, "Cannot reset card, error %d\n", err);
1625−
1626− err = kvaser_usb_stop_chip(priv);
1627− if (err)
1628− netdev_warn(netdev, "Cannot stop device, error %d\n", err);
1629−
1630− /* reset tx contexts */
1631− kvaser_usb_unlink_tx_urbs(priv);
1632−
1633− priv->can.state = CAN_STATE_STOPPED;
1634− close_candev(priv->netdev);
1635−
1636− return 0;
1637−}
1638−
1639−static void kvaser_usb_write_bulk_callback(struct urb *urb)
1640−{
1641− struct kvaser_usb_tx_urb_context *context = urb->context;
1642− struct kvaser_usb_net_priv *priv;
1643− struct net_device *netdev;
1644−
1645− if (WARN_ON(!context))
1646− return;
1647−
1648− priv = context->priv;
1649− netdev = priv->netdev;
1650−
1651− kfree(urb->transfer_buffer);
1652−
1653− if (!netif_device_present(netdev))
1654− return;
1655−
1656− if (urb->status)
1657− netdev_info(netdev, "Tx URB aborted (%d)\n", urb->status);
1658−}
1659−
1660−static netdev_tx_t kvaser_usb_start_xmit(struct sk_buff *skb,
1661− struct net_device *netdev)
1662−{
1663− struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1664− struct kvaser_usb *dev = priv->dev;
1665− struct net_device_stats *stats = &netdev->stats;
1666− struct can_frame *cf = (struct can_frame *)skb->data;
1667− struct kvaser_usb_tx_urb_context *context = NULL;
1668− struct urb *urb;
1669− void *buf;
1670− struct kvaser_msg *msg;
1671− int i, err, ret = NETDEV_TX_OK;
1672− u8 *msg_tx_can_flags = NULL; /* GCC */
1673− unsigned long flags;
1674−
1675− if (can_dropped_invalid_skb(netdev, skb))
1676− return NETDEV_TX_OK;
1677−
1678− urb = usb_alloc_urb(0, GFP_ATOMIC);
1679− if (!urb) {
1680− stats->tx_dropped++;
1681− dev_kfree_skb(skb);
1682− return NETDEV_TX_OK;
1683− }
1684−
1685− buf = kmalloc(sizeof(struct kvaser_msg), GFP_ATOMIC);
1686− if (!buf) {
1687− stats->tx_dropped++;
1688− dev_kfree_skb(skb);
1689− goto freeurb;
1690− }
1691−
1692− msg = buf;
1693− msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_tx_can);
1694− msg->u.tx_can.channel = priv->channel;
1695−
1696− switch (dev->family) {
1697− case KVASER_LEAF:
1698− msg_tx_can_flags = &msg->u.tx_can.leaf.flags;
1699− break;
1700− case KVASER_USBCAN:
1701− msg_tx_can_flags = &msg->u.tx_can.usbcan.flags;
1702− break;
1703− }
1704−
1705− *msg_tx_can_flags = 0;
1706−
1707− if (cf->can_id & CAN_EFF_FLAG) {
1708− msg->id = CMD_TX_EXT_MESSAGE;
1709− msg->u.tx_can.msg[0] = (cf->can_id >> 24) & 0x1f;
1710− msg->u.tx_can.msg[1] = (cf->can_id >> 18) & 0x3f;
1711− msg->u.tx_can.msg[2] = (cf->can_id >> 14) & 0x0f;
1712− msg->u.tx_can.msg[3] = (cf->can_id >> 6) & 0xff;
1713− msg->u.tx_can.msg[4] = cf->can_id & 0x3f;
1714− } else {
1715− msg->id = CMD_TX_STD_MESSAGE;
1716− msg->u.tx_can.msg[0] = (cf->can_id >> 6) & 0x1f;
1717− msg->u.tx_can.msg[1] = cf->can_id & 0x3f;
1718− }
1719−
1720− msg->u.tx_can.msg[5] = cf->can_dlc;
1721− memcpy(&msg->u.tx_can.msg[6], cf->data, cf->can_dlc);
1722−
1723− if (cf->can_id & CAN_RTR_FLAG)
1724− *msg_tx_can_flags |= MSG_FLAG_REMOTE_FRAME;
1725−
1726− spin_lock_irqsave(&priv->tx_contexts_lock, flags);
1727− for (i = 0; i < dev->max_tx_urbs; i++) {
1728− if (priv->tx_contexts[i].echo_index == dev->max_tx_urbs) {
1729− context = &priv->tx_contexts[i];
1730−
1731− context->echo_index = i;
1732− can_put_echo_skb(skb, netdev, context->echo_index);
1733− ++priv->active_tx_contexts;
1734− if (priv->active_tx_contexts >= dev->max_tx_urbs)
1735− netif_stop_queue(netdev);
1736−
1737− break;
1738− }
1739− }
1740− spin_unlock_irqrestore(&priv->tx_contexts_lock, flags);
1741−
1742− /* This should never happen; it implies a flow control bug */
1743− if (!context) {
1744− netdev_warn(netdev, "cannot find free context\n");
1745−
1746− kfree(buf);
1747− ret = NETDEV_TX_BUSY;
1748− goto freeurb;
1749− }
1750−
1751− context->priv = priv;
1752− context->dlc = cf->can_dlc;
1753−
1754− msg->u.tx_can.tid = context->echo_index;
1755−
1756− usb_fill_bulk_urb(urb, dev->udev,
1757− usb_sndbulkpipe(dev->udev,
1758− dev->bulk_out->bEndpointAddress),
1759− buf, msg->len,
1760− kvaser_usb_write_bulk_callback, context);
1761− usb_anchor_urb(urb, &priv->tx_submitted);
1762−
1763− err = usb_submit_urb(urb, GFP_ATOMIC);
1764− if (unlikely(err)) {
1765− spin_lock_irqsave(&priv->tx_contexts_lock, flags);
1766−
1767− can_free_echo_skb(netdev, context->echo_index);
1768− context->echo_index = dev->max_tx_urbs;
1769− --priv->active_tx_contexts;
1770− netif_wake_queue(netdev);
1771−
1772− spin_unlock_irqrestore(&priv->tx_contexts_lock, flags);
1773−
1774− usb_unanchor_urb(urb);
1775− kfree(buf);
1776−
1777− stats->tx_dropped++;
1778−
1779− if (err == -ENODEV)
1780− netif_device_detach(netdev);
1781− else
1782− netdev_warn(netdev, "Failed tx_urb %d\n", err);
1783−
1784− goto freeurb;
1785− }
1786−
1787− ret = NETDEV_TX_OK;
1788−
1789−freeurb:
1790− usb_free_urb(urb);
1791− return ret;
1792−}
1793−
1794−static const struct net_device_ops kvaser_usb_netdev_ops = {
1795− .ndo_open = kvaser_usb_open,
1796− .ndo_stop = kvaser_usb_close,
1797− .ndo_start_xmit = kvaser_usb_start_xmit,
1798− .ndo_change_mtu = can_change_mtu,
1799−};
1800−
1801−static const struct can_bittiming_const kvaser_usb_bittiming_const = {
1802− .name = "kvaser_usb",
1803− .tseg1_min = KVASER_USB_TSEG1_MIN,
1804− .tseg1_max = KVASER_USB_TSEG1_MAX,
1805− .tseg2_min = KVASER_USB_TSEG2_MIN,
1806− .tseg2_max = KVASER_USB_TSEG2_MAX,
1807− .sjw_max = KVASER_USB_SJW_MAX,
1808− .brp_min = KVASER_USB_BRP_MIN,
1809− .brp_max = KVASER_USB_BRP_MAX,
1810− .brp_inc = KVASER_USB_BRP_INC,
1811−};
1812−
1813−static int kvaser_usb_set_bittiming(struct net_device *netdev)
1814−{
1815− struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1816− struct can_bittiming *bt = &priv->can.bittiming;
1817− struct kvaser_usb *dev = priv->dev;
1818− struct kvaser_msg *msg;
1819− int rc;
1820−
1821− msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1822− if (!msg)
1823− return -ENOMEM;
1824−
1825− msg->id = CMD_SET_BUS_PARAMS;
1826− msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_busparams);
1827− msg->u.busparams.channel = priv->channel;
1828− msg->u.busparams.tid = 0xff;
1829− msg->u.busparams.bitrate = cpu_to_le32(bt->bitrate);
1830− msg->u.busparams.sjw = bt->sjw;
1831− msg->u.busparams.tseg1 = bt->prop_seg + bt->phase_seg1;
1832− msg->u.busparams.tseg2 = bt->phase_seg2;
1833−
1834− if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
1835− msg->u.busparams.no_samp = 3;
1836− else
1837− msg->u.busparams.no_samp = 1;
1838−
1839− rc = kvaser_usb_send_msg(dev, msg);
1840−
1841− kfree(msg);
1842− return rc;
1843−}
1844−
1845−static int kvaser_usb_set_mode(struct net_device *netdev,
1846− enum can_mode mode)
1847−{
1848− struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1849− int err;
1850−
1851− switch (mode) {
1852− case CAN_MODE_START:
1853− err = kvaser_usb_simple_msg_async(priv, CMD_START_CHIP);
1854− if (err)
1855− return err;
1856− break;
1857− default:
1858− return -EOPNOTSUPP;
1859− }
1860−
1861− return 0;
1862−}
1863−
1864−static int kvaser_usb_get_berr_counter(const struct net_device *netdev,
1865− struct can_berr_counter *bec)
1866−{
1867− struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1868−
1869− *bec = priv->bec;
1870−
1871− return 0;
1872−}
1873−
1874−static void kvaser_usb_remove_interfaces(struct kvaser_usb *dev)
1875−{
1876− int i;
1877−
1878− for (i = 0; i < dev->nchannels; i++) {
1879− if (!dev->nets[i])
1880− continue;
1881−
1882− unregister_candev(dev->nets[i]->netdev);
1883− }
1884−
1885− kvaser_usb_unlink_all_urbs(dev);
1886−
1887− for (i = 0; i < dev->nchannels; i++) {
1888− if (!dev->nets[i])
1889− continue;
1890−
1891− free_candev(dev->nets[i]->netdev);
1892− }
1893−}
1894−
1895−static int kvaser_usb_init_one(struct usb_interface *intf,
1896− const struct usb_device_id *id, int channel)
1897−{
1898− struct kvaser_usb *dev = usb_get_intfdata(intf);
1899− struct net_device *netdev;
1900− struct kvaser_usb_net_priv *priv;
1901− int err;
1902−
1903− err = kvaser_usb_send_simple_msg(dev, CMD_RESET_CHIP, channel);
1904− if (err)
1905− return err;
1906−
1907− netdev = alloc_candev(sizeof(*priv) +
1908− dev->max_tx_urbs * sizeof(*priv->tx_contexts),
1909− dev->max_tx_urbs);
1910− if (!netdev) {
1911− dev_err(&intf->dev, "Cannot alloc candev\n");
1912− return -ENOMEM;
1913− }
1914−
1915− priv = netdev_priv(netdev);
1916−
1917− init_usb_anchor(&priv->tx_submitted);
1918− init_completion(&priv->start_comp);
1919− init_completion(&priv->stop_comp);
1920−
1921− priv->dev = dev;
1922− priv->netdev = netdev;
1923− priv->channel = channel;
1924−
1925− spin_lock_init(&priv->tx_contexts_lock);
1926− kvaser_usb_reset_tx_urb_contexts(priv);
1927−
1928− priv->can.state = CAN_STATE_STOPPED;
1929− priv->can.clock.freq = CAN_USB_CLOCK;
1930− priv->can.bittiming_const = &kvaser_usb_bittiming_const;
1931− priv->can.do_set_bittiming = kvaser_usb_set_bittiming;
1932− priv->can.do_set_mode = kvaser_usb_set_mode;
1933− if (id->driver_info & KVASER_HAS_TXRX_ERRORS)
1934− priv->can.do_get_berr_counter = kvaser_usb_get_berr_counter;
1935− priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES;
1936− if (id->driver_info & KVASER_HAS_SILENT_MODE)
1937− priv->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY;
1938−
1939− netdev->flags |= IFF_ECHO;
1940−
1941− netdev->netdev_ops = &kvaser_usb_netdev_ops;
1942−
1943− SET_NETDEV_DEV(netdev, &intf->dev);
1944− netdev->dev_id = channel;
1945−
1946− dev->nets[channel] = priv;
1947−
1948− err = register_candev(netdev);
1949− if (err) {
1950− dev_err(&intf->dev, "Failed to register can device\n");
1951− free_candev(netdev);
1952− dev->nets[channel] = NULL;
1953− return err;
1954− }
1955−
1956− netdev_dbg(netdev, "device registered\n");
1957−
1958− return 0;
1959−}
1960−
1961−static int kvaser_usb_get_endpoints(const struct usb_interface *intf,
1962− struct usb_endpoint_descriptor **in,
1963− struct usb_endpoint_descriptor **out)
1964−{
1965− const struct usb_host_interface *iface_desc;
1966− struct usb_endpoint_descriptor *endpoint;
1967− int i;
1968−
1969− iface_desc = &intf->altsetting[0];
1970−
1971− for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1972− endpoint = &iface_desc->endpoint[i].desc;
1973−
1974− if (!*in && usb_endpoint_is_bulk_in(endpoint))
1975− *in = endpoint;
1976−
1977− if (!*out && usb_endpoint_is_bulk_out(endpoint))
1978− *out = endpoint;
1979−
1980− /* use first bulk endpoint for in and out */
1981− if (*in && *out)
1982− return 0;
1983− }
1984−
1985− return -ENODEV;
1986−}
1987−
1988−static int kvaser_usb_probe(struct usb_interface *intf,
1989− const struct usb_device_id *id)
1990−{
1991− struct kvaser_usb *dev;
1992− int err = -ENOMEM;
1993− int i, retry = 3;
1994−
1995− dev = devm_kzalloc(&intf->dev, sizeof(*dev), GFP_KERNEL);
1996− if (!dev)
1997− return -ENOMEM;
1998−
1999− if (kvaser_is_leaf(id)) {
2000− dev->family = KVASER_LEAF;
2001− } else if (kvaser_is_usbcan(id)) {
2002− dev->family = KVASER_USBCAN;
2003− } else {
2004− dev_err(&intf->dev,
2005− "Product ID (%d) does not belong to any known Kvaser USB family",
2006− id->idProduct);
2007− return -ENODEV;
2008− }
2009−
2010− err = kvaser_usb_get_endpoints(intf, &dev->bulk_in, &dev->bulk_out);
2011− if (err) {
2012− dev_err(&intf->dev, "Cannot get usb endpoint(s)");
2013− return err;
2014− }
2015−
2016− dev->udev = interface_to_usbdev(intf);
2017−
2018− init_usb_anchor(&dev->rx_submitted);
2019−
2020− usb_set_intfdata(intf, dev);
2021−
2022− /* On some x86 laptops, plugging a Kvaser device again after
2023− * an unplug makes the firmware always ignore the very first
2024− * command. For such a case, provide some room for retries
2025− * instead of completely exiting the driver.
2026− */
2027− do {
2028− err = kvaser_usb_get_software_info(dev);
2029− } while (--retry && err == -ETIMEDOUT);
2030−
2031− if (err) {
2032− dev_err(&intf->dev,
2033− "Cannot get software infos, error %d\n", err);
2034− return err;
2035− }
2036−
2037− dev_dbg(&intf->dev, "Firmware version: %d.%d.%d\n",
2038− ((dev->fw_version >> 24) & 0xff),
2039− ((dev->fw_version >> 16) & 0xff),
2040− (dev->fw_version & 0xffff));
2041−
2042− dev_dbg(&intf->dev, "Max outstanding tx = %d URBs\n", dev->max_tx_urbs);
2043−
2044− err = kvaser_usb_get_card_info(dev);
2045− if (err) {
2046− dev_err(&intf->dev,
2047− "Cannot get card infos, error %d\n", err);
2048− return err;
2049− }
2050−
2051− for (i = 0; i < dev->nchannels; i++) {
2052− err = kvaser_usb_init_one(intf, id, i);
2053− if (err) {
2054− kvaser_usb_remove_interfaces(dev);
2055− return err;
2056− }
2057− }
2058−
2059− return 0;
2060−}
2061−
2062−static void kvaser_usb_disconnect(struct usb_interface *intf)
2063−{
2064− struct kvaser_usb *dev = usb_get_intfdata(intf);
2065−
2066− usb_set_intfdata(intf, NULL);
2067−
2068− if (!dev)
2069− return;
2070−
2071− kvaser_usb_remove_interfaces(dev);
2072−}
2073−
2074−static struct usb_driver kvaser_usb_driver = {
2075− .name = "kvaser_usb",
2076− .probe = kvaser_usb_probe,
2077− .disconnect = kvaser_usb_disconnect,
2078− .id_table = kvaser_usb_table,
2079−};
2080−
2081−module_usb_driver(kvaser_usb_driver);
2082−
2083−MODULE_AUTHOR("Olivier Sobrie <olivier@sobrie.be>");
2084−MODULE_DESCRIPTION("CAN driver for Kvaser CAN/USB devices");
2085−MODULE_LICENSE("GPL v2");
drivers/net/can/usb/kvaser_usb/Makefile+2−0
@@ -0,0 +1,2 @@
1+obj-$(CONFIG_CAN_KVASER_USB) += kvaser_usb.o
2+kvaser_usb-y = kvaser_usb_core.o kvaser_usb_leaf.o kvaser_usb_hydra.o
drivers/net/can/usb/kvaser_usb/kvaser_usb.h+188−0
@@ -0,0 +1,188 @@
1+/* SPDX-License-Identifier: GPL-2.0 */
2+/* Parts of this driver are based on the following:
3+ * - Kvaser linux leaf driver (version 4.78)
4+ * - CAN driver for esd CAN-USB/2
5+ * - Kvaser linux usbcanII driver (version 5.3)
6+ * - Kvaser linux mhydra driver (version 5.24)
7+ *
8+ * Copyright (C) 2002-2018 KVASER AB, Sweden. All rights reserved.
9+ * Copyright (C) 2010 Matthias Fuchs <matthias.fuchs@esd.eu>, esd gmbh
10+ * Copyright (C) 2012 Olivier Sobrie <olivier@sobrie.be>
11+ * Copyright (C) 2015 Valeo S.A.
12+ */
13+
14+#ifndef KVASER_USB_H
15+#define KVASER_USB_H
16+
17+/* Kvaser USB CAN dongles are divided into three major platforms:
18+ * - Hydra: Running firmware labeled as 'mhydra'
19+ * - Leaf: Based on Renesas M32C or Freescale i.MX28, running firmware labeled
20+ * as 'filo'
21+ * - UsbcanII: Based on Renesas M16C, running firmware labeled as 'helios'
22+ */
23+
24+#include <linux/completion.h>
25+#include <linux/spinlock.h>
26+#include <linux/types.h>
27+#include <linux/usb.h>
28+
29+#include <linux/can.h>
30+#include <linux/can/dev.h>
31+
32+#define KVASER_USB_MAX_RX_URBS 4
33+#define KVASER_USB_MAX_TX_URBS 128
34+#define KVASER_USB_TIMEOUT 1000 /* msecs */
35+#define KVASER_USB_RX_BUFFER_SIZE 3072
36+#define KVASER_USB_MAX_NET_DEVICES 5
37+
38+/* USB devices features */
39+#define KVASER_USB_HAS_SILENT_MODE BIT(0)
40+#define KVASER_USB_HAS_TXRX_ERRORS BIT(1)
41+
42+/* Device capabilities */
43+#define KVASER_USB_CAP_BERR_CAP 0x01
44+#define KVASER_USB_CAP_EXT_CAP 0x02
45+#define KVASER_USB_HYDRA_CAP_EXT_CMD 0x04
46+
47+struct kvaser_usb_dev_cfg;
48+
49+enum kvaser_usb_leaf_family {
50+ KVASER_LEAF,
51+ KVASER_USBCAN,
52+};
53+
54+#define KVASER_USB_HYDRA_MAX_CMD_LEN 128
55+struct kvaser_usb_dev_card_data_hydra {
56+ u8 channel_to_he[KVASER_USB_MAX_NET_DEVICES];
57+ u8 sysdbg_he;
58+ spinlock_t transid_lock; /* lock for transid */
59+ u16 transid;
60+ /* lock for usb_rx_leftover and usb_rx_leftover_len */
61+ spinlock_t usb_rx_leftover_lock;
62+ u8 usb_rx_leftover[KVASER_USB_HYDRA_MAX_CMD_LEN];
63+ u8 usb_rx_leftover_len;
64+};
65+struct kvaser_usb_dev_card_data {
66+ u32 ctrlmode_supported;
67+ u32 capabilities;
68+ union {
69+ struct {
70+ enum kvaser_usb_leaf_family family;
71+ } leaf;
72+ struct kvaser_usb_dev_card_data_hydra hydra;
73+ };
74+};
75+
76+/* Context for an outstanding, not yet ACKed, transmission */
77+struct kvaser_usb_tx_urb_context {
78+ struct kvaser_usb_net_priv *priv;
79+ u32 echo_index;
80+ int dlc;
81+};
82+
83+struct kvaser_usb {
84+ struct usb_device *udev;
85+ struct usb_interface *intf;
86+ struct kvaser_usb_net_priv *nets[KVASER_USB_MAX_NET_DEVICES];
87+ const struct kvaser_usb_dev_ops *ops;
88+ const struct kvaser_usb_dev_cfg *cfg;
89+
90+ struct usb_endpoint_descriptor *bulk_in, *bulk_out;
91+ struct usb_anchor rx_submitted;
92+
93+ /* @max_tx_urbs: Firmware-reported maximum number of outstanding,
94+ * not yet ACKed, transmissions on this device. This value is
95+ * also used as a sentinel for marking free tx contexts.
96+ */
97+ u32 fw_version;
98+ unsigned int nchannels;
99+ unsigned int max_tx_urbs;
100+ struct kvaser_usb_dev_card_data card_data;
101+
102+ bool rxinitdone;
103+ void *rxbuf[KVASER_USB_MAX_RX_URBS];
104+ dma_addr_t rxbuf_dma[KVASER_USB_MAX_RX_URBS];
105+};
106+
107+struct kvaser_usb_net_priv {
108+ struct can_priv can;
109+ struct can_berr_counter bec;
110+
111+ struct kvaser_usb *dev;
112+ struct net_device *netdev;
113+ int channel;
114+
115+ struct completion start_comp, stop_comp, flush_comp;
116+ struct usb_anchor tx_submitted;
117+
118+ spinlock_t tx_contexts_lock; /* lock for active_tx_contexts */
119+ int active_tx_contexts;
120+ struct kvaser_usb_tx_urb_context tx_contexts[];
121+};
122+
123+/**
124+ * struct kvaser_usb_dev_ops - Device specific functions
125+ * @dev_set_mode: used for can.do_set_mode
126+ * @dev_set_bittiming: used for can.do_set_bittiming
127+ * @dev_set_data_bittiming: used for can.do_set_data_bittiming
128+ * @dev_get_berr_counter: used for can.do_get_berr_counter
129+ *
130+ * @dev_setup_endpoints: setup USB in and out endpoints
131+ * @dev_init_card: initialize card
132+ * @dev_get_software_info: get software info
133+ * @dev_get_software_details: get software details
134+ * @dev_get_card_info: get card info
135+ * @dev_get_capabilities: discover device capabilities
136+ *
137+ * @dev_set_opt_mode: set ctrlmod
138+ * @dev_start_chip: start the CAN controller
139+ * @dev_stop_chip: stop the CAN controller
140+ * @dev_reset_chip: reset the CAN controller
141+ * @dev_flush_queue: flush outstanding CAN messages
142+ * @dev_read_bulk_callback: handle incoming commands
143+ * @dev_frame_to_cmd: translate struct can_frame into device command
144+ */
145+struct kvaser_usb_dev_ops {
146+ int (*dev_set_mode)(struct net_device *netdev, enum can_mode mode);
147+ int (*dev_set_bittiming)(struct net_device *netdev);
148+ int (*dev_set_data_bittiming)(struct net_device *netdev);
149+ int (*dev_get_berr_counter)(const struct net_device *netdev,
150+ struct can_berr_counter *bec);
151+ int (*dev_setup_endpoints)(struct kvaser_usb *dev);
152+ int (*dev_init_card)(struct kvaser_usb *dev);
153+ int (*dev_get_software_info)(struct kvaser_usb *dev);
154+ int (*dev_get_software_details)(struct kvaser_usb *dev);
155+ int (*dev_get_card_info)(struct kvaser_usb *dev);
156+ int (*dev_get_capabilities)(struct kvaser_usb *dev);
157+ int (*dev_set_opt_mode)(const struct kvaser_usb_net_priv *priv);
158+ int (*dev_start_chip)(struct kvaser_usb_net_priv *priv);
159+ int (*dev_stop_chip)(struct kvaser_usb_net_priv *priv);
160+ int (*dev_reset_chip)(struct kvaser_usb *dev, int channel);
161+ int (*dev_flush_queue)(struct kvaser_usb_net_priv *priv);
162+ void (*dev_read_bulk_callback)(struct kvaser_usb *dev, void *buf,
163+ int len);
164+ void *(*dev_frame_to_cmd)(const struct kvaser_usb_net_priv *priv,
165+ const struct sk_buff *skb, int *frame_len,
166+ int *cmd_len, u16 transid);
167+};
168+
169+struct kvaser_usb_dev_cfg {
170+ const struct can_clock clock;
171+ const unsigned int timestamp_freq;
172+ const struct can_bittiming_const * const bittiming_const;
173+ const struct can_bittiming_const * const data_bittiming_const;
174+};
175+
176+extern const struct kvaser_usb_dev_ops kvaser_usb_hydra_dev_ops;
177+extern const struct kvaser_usb_dev_ops kvaser_usb_leaf_dev_ops;
178+
179+int kvaser_usb_recv_cmd(const struct kvaser_usb *dev, void *cmd, int len,
180+ int *actual_len);
181+
182+int kvaser_usb_send_cmd(const struct kvaser_usb *dev, void *cmd, int len);
183+
184+int kvaser_usb_send_cmd_async(struct kvaser_usb_net_priv *priv, void *cmd,
185+ int len);
186+
187+int kvaser_usb_can_rx_over_error(struct net_device *netdev);
188+#endif /* KVASER_USB_H */
drivers/net/can/usb/kvaser_usb/kvaser_usb_core.c+835−0
@@ -0,0 +1,835 @@
1+// SPDX-License-Identifier: GPL-2.0
2+/* Parts of this driver are based on the following:
3+ * - Kvaser linux leaf driver (version 4.78)
4+ * - CAN driver for esd CAN-USB/2
5+ * - Kvaser linux usbcanII driver (version 5.3)
6+ * - Kvaser linux mhydra driver (version 5.24)
7+ *
8+ * Copyright (C) 2002-2018 KVASER AB, Sweden. All rights reserved.
9+ * Copyright (C) 2010 Matthias Fuchs <matthias.fuchs@esd.eu>, esd gmbh
10+ * Copyright (C) 2012 Olivier Sobrie <olivier@sobrie.be>
11+ * Copyright (C) 2015 Valeo S.A.
12+ */
13+
14+#include <linux/completion.h>
15+#include <linux/device.h>
16+#include <linux/gfp.h>
17+#include <linux/if.h>
18+#include <linux/kernel.h>
19+#include <linux/module.h>
20+#include <linux/netdevice.h>
21+#include <linux/spinlock.h>
22+#include <linux/types.h>
23+#include <linux/usb.h>
24+
25+#include <linux/can.h>
26+#include <linux/can/dev.h>
27+#include <linux/can/error.h>
28+#include <linux/can/netlink.h>
29+
30+#include "kvaser_usb.h"
31+
32+/* Kvaser USB vendor id. */
33+#define KVASER_VENDOR_ID 0x0bfd
34+
35+/* Kvaser Leaf USB devices product ids */
36+#define USB_LEAF_DEVEL_PRODUCT_ID 10
37+#define USB_LEAF_LITE_PRODUCT_ID 11
38+#define USB_LEAF_PRO_PRODUCT_ID 12
39+#define USB_LEAF_SPRO_PRODUCT_ID 14
40+#define USB_LEAF_PRO_LS_PRODUCT_ID 15
41+#define USB_LEAF_PRO_SWC_PRODUCT_ID 16
42+#define USB_LEAF_PRO_LIN_PRODUCT_ID 17
43+#define USB_LEAF_SPRO_LS_PRODUCT_ID 18
44+#define USB_LEAF_SPRO_SWC_PRODUCT_ID 19
45+#define USB_MEMO2_DEVEL_PRODUCT_ID 22
46+#define USB_MEMO2_HSHS_PRODUCT_ID 23
47+#define USB_UPRO_HSHS_PRODUCT_ID 24
48+#define USB_LEAF_LITE_GI_PRODUCT_ID 25
49+#define USB_LEAF_PRO_OBDII_PRODUCT_ID 26
50+#define USB_MEMO2_HSLS_PRODUCT_ID 27
51+#define USB_LEAF_LITE_CH_PRODUCT_ID 28
52+#define USB_BLACKBIRD_SPRO_PRODUCT_ID 29
53+#define USB_OEM_MERCURY_PRODUCT_ID 34
54+#define USB_OEM_LEAF_PRODUCT_ID 35
55+#define USB_CAN_R_PRODUCT_ID 39
56+#define USB_LEAF_LITE_V2_PRODUCT_ID 288
57+#define USB_MINI_PCIE_HS_PRODUCT_ID 289
58+#define USB_LEAF_LIGHT_HS_V2_OEM_PRODUCT_ID 290
59+#define USB_USBCAN_LIGHT_2HS_PRODUCT_ID 291
60+#define USB_MINI_PCIE_2HS_PRODUCT_ID 292
61+
62+/* Kvaser USBCan-II devices product ids */
63+#define USB_USBCAN_REVB_PRODUCT_ID 2
64+#define USB_VCI2_PRODUCT_ID 3
65+#define USB_USBCAN2_PRODUCT_ID 4
66+#define USB_MEMORATOR_PRODUCT_ID 5
67+
68+/* Kvaser Minihydra USB devices product ids */
69+#define USB_BLACKBIRD_V2_PRODUCT_ID 258
70+#define USB_MEMO_PRO_5HS_PRODUCT_ID 260
71+#define USB_USBCAN_PRO_5HS_PRODUCT_ID 261
72+#define USB_USBCAN_LIGHT_4HS_PRODUCT_ID 262
73+#define USB_LEAF_PRO_HS_V2_PRODUCT_ID 263
74+#define USB_USBCAN_PRO_2HS_V2_PRODUCT_ID 264
75+#define USB_MEMO_2HS_PRODUCT_ID 265
76+#define USB_MEMO_PRO_2HS_V2_PRODUCT_ID 266
77+#define USB_HYBRID_CANLIN_PRODUCT_ID 267
78+#define USB_ATI_USBCAN_PRO_2HS_V2_PRODUCT_ID 268
79+#define USB_ATI_MEMO_PRO_2HS_V2_PRODUCT_ID 269
80+#define USB_HYBRID_PRO_CANLIN_PRODUCT_ID 270
81+
82+static inline bool kvaser_is_leaf(const struct usb_device_id *id)
83+{
84+ return (id->idProduct >= USB_LEAF_DEVEL_PRODUCT_ID &&
85+ id->idProduct <= USB_CAN_R_PRODUCT_ID) ||
86+ (id->idProduct >= USB_LEAF_LITE_V2_PRODUCT_ID &&
87+ id->idProduct <= USB_MINI_PCIE_2HS_PRODUCT_ID);
88+}
89+
90+static inline bool kvaser_is_usbcan(const struct usb_device_id *id)
91+{
92+ return id->idProduct >= USB_USBCAN_REVB_PRODUCT_ID &&
93+ id->idProduct <= USB_MEMORATOR_PRODUCT_ID;
94+}
95+
96+static inline bool kvaser_is_hydra(const struct usb_device_id *id)
97+{
98+ return id->idProduct >= USB_BLACKBIRD_V2_PRODUCT_ID &&
99+ id->idProduct <= USB_HYBRID_PRO_CANLIN_PRODUCT_ID;
100+}
101+
102+static const struct usb_device_id kvaser_usb_table[] = {
103+ /* Leaf USB product IDs */
104+ { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_DEVEL_PRODUCT_ID) },
105+ { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_PRODUCT_ID) },
106+ { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_PRODUCT_ID),
107+ .driver_info = KVASER_USB_HAS_TXRX_ERRORS |
108+ KVASER_USB_HAS_SILENT_MODE },
109+ { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_PRODUCT_ID),
110+ .driver_info = KVASER_USB_HAS_TXRX_ERRORS |
111+ KVASER_USB_HAS_SILENT_MODE },
112+ { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_LS_PRODUCT_ID),
113+ .driver_info = KVASER_USB_HAS_TXRX_ERRORS |
114+ KVASER_USB_HAS_SILENT_MODE },
115+ { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_SWC_PRODUCT_ID),
116+ .driver_info = KVASER_USB_HAS_TXRX_ERRORS |
117+ KVASER_USB_HAS_SILENT_MODE },
118+ { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_LIN_PRODUCT_ID),
119+ .driver_info = KVASER_USB_HAS_TXRX_ERRORS |
120+ KVASER_USB_HAS_SILENT_MODE },
121+ { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_LS_PRODUCT_ID),
122+ .driver_info = KVASER_USB_HAS_TXRX_ERRORS |
123+ KVASER_USB_HAS_SILENT_MODE },
124+ { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_SWC_PRODUCT_ID),
125+ .driver_info = KVASER_USB_HAS_TXRX_ERRORS |
126+ KVASER_USB_HAS_SILENT_MODE },
127+ { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_DEVEL_PRODUCT_ID),
128+ .driver_info = KVASER_USB_HAS_TXRX_ERRORS |
129+ KVASER_USB_HAS_SILENT_MODE },
130+ { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_HSHS_PRODUCT_ID),
131+ .driver_info = KVASER_USB_HAS_TXRX_ERRORS |
132+ KVASER_USB_HAS_SILENT_MODE },
133+ { USB_DEVICE(KVASER_VENDOR_ID, USB_UPRO_HSHS_PRODUCT_ID),
134+ .driver_info = KVASER_USB_HAS_TXRX_ERRORS },
135+ { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_GI_PRODUCT_ID) },
136+ { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_OBDII_PRODUCT_ID),
137+ .driver_info = KVASER_USB_HAS_TXRX_ERRORS |
138+ KVASER_USB_HAS_SILENT_MODE },
139+ { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_HSLS_PRODUCT_ID),
140+ .driver_info = KVASER_USB_HAS_TXRX_ERRORS },
141+ { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_CH_PRODUCT_ID),
142+ .driver_info = KVASER_USB_HAS_TXRX_ERRORS },
143+ { USB_DEVICE(KVASER_VENDOR_ID, USB_BLACKBIRD_SPRO_PRODUCT_ID),
144+ .driver_info = KVASER_USB_HAS_TXRX_ERRORS },
145+ { USB_DEVICE(KVASER_VENDOR_ID, USB_OEM_MERCURY_PRODUCT_ID),
146+ .driver_info = KVASER_USB_HAS_TXRX_ERRORS },
147+ { USB_DEVICE(KVASER_VENDOR_ID, USB_OEM_LEAF_PRODUCT_ID),
148+ .driver_info = KVASER_USB_HAS_TXRX_ERRORS },
149+ { USB_DEVICE(KVASER_VENDOR_ID, USB_CAN_R_PRODUCT_ID),
150+ .driver_info = KVASER_USB_HAS_TXRX_ERRORS },
151+ { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_V2_PRODUCT_ID) },
152+ { USB_DEVICE(KVASER_VENDOR_ID, USB_MINI_PCIE_HS_PRODUCT_ID) },
153+ { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LIGHT_HS_V2_OEM_PRODUCT_ID) },
154+ { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN_LIGHT_2HS_PRODUCT_ID) },
155+ { USB_DEVICE(KVASER_VENDOR_ID, USB_MINI_PCIE_2HS_PRODUCT_ID) },
156+
157+ /* USBCANII USB product IDs */
158+ { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN2_PRODUCT_ID),
159+ .driver_info = KVASER_USB_HAS_TXRX_ERRORS },
160+ { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN_REVB_PRODUCT_ID),
161+ .driver_info = KVASER_USB_HAS_TXRX_ERRORS },
162+ { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMORATOR_PRODUCT_ID),
163+ .driver_info = KVASER_USB_HAS_TXRX_ERRORS },
164+ { USB_DEVICE(KVASER_VENDOR_ID, USB_VCI2_PRODUCT_ID),
165+ .driver_info = KVASER_USB_HAS_TXRX_ERRORS },
166+
167+ /* Minihydra USB product IDs */
168+ { USB_DEVICE(KVASER_VENDOR_ID, USB_BLACKBIRD_V2_PRODUCT_ID) },
169+ { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO_PRO_5HS_PRODUCT_ID) },
170+ { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN_PRO_5HS_PRODUCT_ID) },
171+ { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN_LIGHT_4HS_PRODUCT_ID) },
172+ { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_HS_V2_PRODUCT_ID) },
173+ { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN_PRO_2HS_V2_PRODUCT_ID) },
174+ { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO_2HS_PRODUCT_ID) },
175+ { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO_PRO_2HS_V2_PRODUCT_ID) },
176+ { USB_DEVICE(KVASER_VENDOR_ID, USB_HYBRID_CANLIN_PRODUCT_ID) },
177+ { USB_DEVICE(KVASER_VENDOR_ID, USB_ATI_USBCAN_PRO_2HS_V2_PRODUCT_ID) },
178+ { USB_DEVICE(KVASER_VENDOR_ID, USB_ATI_MEMO_PRO_2HS_V2_PRODUCT_ID) },
179+ { USB_DEVICE(KVASER_VENDOR_ID, USB_HYBRID_PRO_CANLIN_PRODUCT_ID) },
180+ { }
181+};
182+MODULE_DEVICE_TABLE(usb, kvaser_usb_table);
183+
184+int kvaser_usb_send_cmd(const struct kvaser_usb *dev, void *cmd, int len)
185+{
186+ int actual_len; /* Not used */
187+
188+ return usb_bulk_msg(dev->udev,
189+ usb_sndbulkpipe(dev->udev,
190+ dev->bulk_out->bEndpointAddress),
191+ cmd, len, &actual_len, KVASER_USB_TIMEOUT);
192+}
193+
194+int kvaser_usb_recv_cmd(const struct kvaser_usb *dev, void *cmd, int len,
195+ int *actual_len)
196+{
197+ return usb_bulk_msg(dev->udev,
198+ usb_rcvbulkpipe(dev->udev,
199+ dev->bulk_in->bEndpointAddress),
200+ cmd, len, actual_len, KVASER_USB_TIMEOUT);
201+}
202+
203+static void kvaser_usb_send_cmd_callback(struct urb *urb)
204+{
205+ struct net_device *netdev = urb->context;
206+
207+ kfree(urb->transfer_buffer);
208+
209+ if (urb->status)
210+ netdev_warn(netdev, "urb status received: %d\n", urb->status);
211+}
212+
213+int kvaser_usb_send_cmd_async(struct kvaser_usb_net_priv *priv, void *cmd,
214+ int len)
215+{
216+ struct kvaser_usb *dev = priv->dev;
217+ struct net_device *netdev = priv->netdev;
218+ struct urb *urb;
219+ int err;
220+
221+ urb = usb_alloc_urb(0, GFP_ATOMIC);
222+ if (!urb)
223+ return -ENOMEM;
224+
225+ usb_fill_bulk_urb(urb, dev->udev,
226+ usb_sndbulkpipe(dev->udev,
227+ dev->bulk_out->bEndpointAddress),
228+ cmd, len, kvaser_usb_send_cmd_callback, netdev);
229+ usb_anchor_urb(urb, &priv->tx_submitted);
230+
231+ err = usb_submit_urb(urb, GFP_ATOMIC);
232+ if (err) {
233+ netdev_err(netdev, "Error transmitting URB\n");
234+ usb_unanchor_urb(urb);
235+ }
236+ usb_free_urb(urb);
237+
238+ return 0;
239+}
240+
241+int kvaser_usb_can_rx_over_error(struct net_device *netdev)
242+{
243+ struct net_device_stats *stats = &netdev->stats;
244+ struct can_frame *cf;
245+ struct sk_buff *skb;
246+
247+ stats->rx_over_errors++;
248+ stats->rx_errors++;
249+
250+ skb = alloc_can_err_skb(netdev, &cf);
251+ if (!skb) {
252+ stats->rx_dropped++;
253+ netdev_warn(netdev, "No memory left for err_skb\n");
254+ return -ENOMEM;
255+ }
256+
257+ cf->can_id |= CAN_ERR_CRTL;
258+ cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
259+
260+ stats->rx_packets++;
261+ stats->rx_bytes += cf->can_dlc;
262+ netif_rx(skb);
263+
264+ return 0;
265+}
266+
267+static void kvaser_usb_read_bulk_callback(struct urb *urb)
268+{
269+ struct kvaser_usb *dev = urb->context;
270+ int err;
271+ unsigned int i;
272+
273+ switch (urb->status) {
274+ case 0:
275+ break;
276+ case -ENOENT:
277+ case -EPIPE:
278+ case -EPROTO:
279+ case -ESHUTDOWN:
280+ return;
281+ default:
282+ dev_info(&dev->intf->dev, "Rx URB aborted (%d)\n", urb->status);
283+ goto resubmit_urb;
284+ }
285+
286+ dev->ops->dev_read_bulk_callback(dev, urb->transfer_buffer,
287+ urb->actual_length);
288+
289+resubmit_urb:
290+ usb_fill_bulk_urb(urb, dev->udev,
291+ usb_rcvbulkpipe(dev->udev,
292+ dev->bulk_in->bEndpointAddress),
293+ urb->transfer_buffer, KVASER_USB_RX_BUFFER_SIZE,
294+ kvaser_usb_read_bulk_callback, dev);
295+
296+ err = usb_submit_urb(urb, GFP_ATOMIC);
297+ if (err == -ENODEV) {
298+ for (i = 0; i < dev->nchannels; i++) {
299+ if (!dev->nets[i])
300+ continue;
301+
302+ netif_device_detach(dev->nets[i]->netdev);
303+ }
304+ } else if (err) {
305+ dev_err(&dev->intf->dev,
306+ "Failed resubmitting read bulk urb: %d\n", err);
307+ }
308+}
309+
310+static int kvaser_usb_setup_rx_urbs(struct kvaser_usb *dev)
311+{
312+ int i, err = 0;
313+
314+ if (dev->rxinitdone)
315+ return 0;
316+
317+ for (i = 0; i < KVASER_USB_MAX_RX_URBS; i++) {
318+ struct urb *urb = NULL;
319+ u8 *buf = NULL;
320+ dma_addr_t buf_dma;
321+
322+ urb = usb_alloc_urb(0, GFP_KERNEL);
323+ if (!urb) {
324+ err = -ENOMEM;
325+ break;
326+ }
327+
328+ buf = usb_alloc_coherent(dev->udev, KVASER_USB_RX_BUFFER_SIZE,
329+ GFP_KERNEL, &buf_dma);
330+ if (!buf) {
331+ dev_warn(&dev->intf->dev,
332+ "No memory left for USB buffer\n");
333+ usb_free_urb(urb);
334+ err = -ENOMEM;
335+ break;
336+ }
337+
338+ usb_fill_bulk_urb(urb, dev->udev,
339+ usb_rcvbulkpipe
340+ (dev->udev,
341+ dev->bulk_in->bEndpointAddress),
342+ buf, KVASER_USB_RX_BUFFER_SIZE,
343+ kvaser_usb_read_bulk_callback, dev);
344+ urb->transfer_dma = buf_dma;
345+ urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
346+ usb_anchor_urb(urb, &dev->rx_submitted);
347+
348+ err = usb_submit_urb(urb, GFP_KERNEL);
349+ if (err) {
350+ usb_unanchor_urb(urb);
351+ usb_free_coherent(dev->udev,
352+ KVASER_USB_RX_BUFFER_SIZE, buf,
353+ buf_dma);
354+ usb_free_urb(urb);
355+ break;
356+ }
357+
358+ dev->rxbuf[i] = buf;
359+ dev->rxbuf_dma[i] = buf_dma;
360+
361+ usb_free_urb(urb);
362+ }
363+
364+ if (i == 0) {
365+ dev_warn(&dev->intf->dev, "Cannot setup read URBs, error %d\n",
366+ err);
367+ return err;
368+ } else if (i < KVASER_USB_MAX_RX_URBS) {
369+ dev_warn(&dev->intf->dev, "RX performances may be slow\n");
370+ }
371+
372+ dev->rxinitdone = true;
373+
374+ return 0;
375+}
376+
377+static int kvaser_usb_open(struct net_device *netdev)
378+{
379+ struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
380+ struct kvaser_usb *dev = priv->dev;
381+ int err;
382+
383+ err = open_candev(netdev);
384+ if (err)
385+ return err;
386+
387+ err = kvaser_usb_setup_rx_urbs(dev);
388+ if (err)
389+ goto error;
390+
391+ err = dev->ops->dev_set_opt_mode(priv);
392+ if (err)
393+ goto error;
394+
395+ err = dev->ops->dev_start_chip(priv);
396+ if (err) {
397+ netdev_warn(netdev, "Cannot start device, error %d\n", err);
398+ goto error;
399+ }
400+
401+ priv->can.state = CAN_STATE_ERROR_ACTIVE;
402+
403+ return 0;
404+
405+error:
406+ close_candev(netdev);
407+ return err;
408+}
409+
410+static void kvaser_usb_reset_tx_urb_contexts(struct kvaser_usb_net_priv *priv)
411+{
412+ int i, max_tx_urbs;
413+
414+ max_tx_urbs = priv->dev->max_tx_urbs;
415+
416+ priv->active_tx_contexts = 0;
417+ for (i = 0; i < max_tx_urbs; i++)
418+ priv->tx_contexts[i].echo_index = max_tx_urbs;
419+}
420+
421+/* This method might sleep. Do not call it in the atomic context
422+ * of URB completions.
423+ */
424+static void kvaser_usb_unlink_tx_urbs(struct kvaser_usb_net_priv *priv)
425+{
426+ usb_kill_anchored_urbs(&priv->tx_submitted);
427+ kvaser_usb_reset_tx_urb_contexts(priv);
428+}
429+
430+static void kvaser_usb_unlink_all_urbs(struct kvaser_usb *dev)
431+{
432+ int i;
433+
434+ usb_kill_anchored_urbs(&dev->rx_submitted);
435+
436+ for (i = 0; i < KVASER_USB_MAX_RX_URBS; i++)
437+ usb_free_coherent(dev->udev, KVASER_USB_RX_BUFFER_SIZE,
438+ dev->rxbuf[i], dev->rxbuf_dma[i]);
439+
440+ for (i = 0; i < dev->nchannels; i++) {
441+ struct kvaser_usb_net_priv *priv = dev->nets[i];
442+
443+ if (priv)
444+ kvaser_usb_unlink_tx_urbs(priv);
445+ }
446+}
447+
448+static int kvaser_usb_close(struct net_device *netdev)
449+{
450+ struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
451+ struct kvaser_usb *dev = priv->dev;
452+ int err;
453+
454+ netif_stop_queue(netdev);
455+
456+ err = dev->ops->dev_flush_queue(priv);
457+ if (err)
458+ netdev_warn(netdev, "Cannot flush queue, error %d\n", err);
459+
460+ if (dev->ops->dev_reset_chip) {
461+ err = dev->ops->dev_reset_chip(dev, priv->channel);
462+ if (err)
463+ netdev_warn(netdev, "Cannot reset card, error %d\n",
464+ err);
465+ }
466+
467+ err = dev->ops->dev_stop_chip(priv);
468+ if (err)
469+ netdev_warn(netdev, "Cannot stop device, error %d\n", err);
470+
471+ /* reset tx contexts */
472+ kvaser_usb_unlink_tx_urbs(priv);
473+
474+ priv->can.state = CAN_STATE_STOPPED;
475+ close_candev(priv->netdev);
476+
477+ return 0;
478+}
479+
480+static void kvaser_usb_write_bulk_callback(struct urb *urb)
481+{
482+ struct kvaser_usb_tx_urb_context *context = urb->context;
483+ struct kvaser_usb_net_priv *priv;
484+ struct net_device *netdev;
485+
486+ if (WARN_ON(!context))
487+ return;
488+
489+ priv = context->priv;
490+ netdev = priv->netdev;
491+
492+ kfree(urb->transfer_buffer);
493+
494+ if (!netif_device_present(netdev))
495+ return;
496+
497+ if (urb->status)
498+ netdev_info(netdev, "Tx URB aborted (%d)\n", urb->status);
499+}
500+
501+static netdev_tx_t kvaser_usb_start_xmit(struct sk_buff *skb,
502+ struct net_device *netdev)
503+{
504+ struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
505+ struct kvaser_usb *dev = priv->dev;
506+ struct net_device_stats *stats = &netdev->stats;
507+ struct kvaser_usb_tx_urb_context *context = NULL;
508+ struct urb *urb;
509+ void *buf;
510+ int cmd_len = 0;
511+ int err, ret = NETDEV_TX_OK;
512+ unsigned int i;
513+ unsigned long flags;
514+
515+ if (can_dropped_invalid_skb(netdev, skb))
516+ return NETDEV_TX_OK;
517+
518+ urb = usb_alloc_urb(0, GFP_ATOMIC);
519+ if (!urb) {
520+ stats->tx_dropped++;
521+ dev_kfree_skb(skb);
522+ return NETDEV_TX_OK;
523+ }
524+
525+ spin_lock_irqsave(&priv->tx_contexts_lock, flags);
526+ for (i = 0; i < dev->max_tx_urbs; i++) {
527+ if (priv->tx_contexts[i].echo_index == dev->max_tx_urbs) {
528+ context = &priv->tx_contexts[i];
529+
530+ context->echo_index = i;
531+ can_put_echo_skb(skb, netdev, context->echo_index);
532+ ++priv->active_tx_contexts;
533+ if (priv->active_tx_contexts >= (int)dev->max_tx_urbs)
534+ netif_stop_queue(netdev);
535+
536+ break;
537+ }
538+ }
539+ spin_unlock_irqrestore(&priv->tx_contexts_lock, flags);
540+
541+ /* This should never happen; it implies a flow control bug */
542+ if (!context) {
543+ netdev_warn(netdev, "cannot find free context\n");
544+
545+ ret = NETDEV_TX_BUSY;
546+ goto freeurb;
547+ }
548+
549+ buf = dev->ops->dev_frame_to_cmd(priv, skb, &context->dlc, &cmd_len,
550+ context->echo_index);
551+ if (!buf) {
552+ stats->tx_dropped++;
553+ dev_kfree_skb(skb);
554+ spin_lock_irqsave(&priv->tx_contexts_lock, flags);
555+
556+ can_free_echo_skb(netdev, context->echo_index);
557+ context->echo_index = dev->max_tx_urbs;
558+ --priv->active_tx_contexts;
559+ netif_wake_queue(netdev);
560+
561+ spin_unlock_irqrestore(&priv->tx_contexts_lock, flags);
562+ goto freeurb;
563+ }
564+
565+ context->priv = priv;
566+
567+ usb_fill_bulk_urb(urb, dev->udev,
568+ usb_sndbulkpipe(dev->udev,
569+ dev->bulk_out->bEndpointAddress),
570+ buf, cmd_len, kvaser_usb_write_bulk_callback,
571+ context);
572+ usb_anchor_urb(urb, &priv->tx_submitted);
573+
574+ err = usb_submit_urb(urb, GFP_ATOMIC);
575+ if (unlikely(err)) {
576+ spin_lock_irqsave(&priv->tx_contexts_lock, flags);
577+
578+ can_free_echo_skb(netdev, context->echo_index);
579+ context->echo_index = dev->max_tx_urbs;
580+ --priv->active_tx_contexts;
581+ netif_wake_queue(netdev);
582+
583+ spin_unlock_irqrestore(&priv->tx_contexts_lock, flags);
584+
585+ usb_unanchor_urb(urb);
586+ kfree(buf);
587+
588+ stats->tx_dropped++;
589+
590+ if (err == -ENODEV)
591+ netif_device_detach(netdev);
592+ else
593+ netdev_warn(netdev, "Failed tx_urb %d\n", err);
594+
595+ goto freeurb;
596+ }
597+
598+ ret = NETDEV_TX_OK;
599+
600+freeurb:
601+ usb_free_urb(urb);
602+ return ret;
603+}
604+
605+static const struct net_device_ops kvaser_usb_netdev_ops = {
606+ .ndo_open = kvaser_usb_open,
607+ .ndo_stop = kvaser_usb_close,
608+ .ndo_start_xmit = kvaser_usb_start_xmit,
609+ .ndo_change_mtu = can_change_mtu,
610+};
611+
612+static void kvaser_usb_remove_interfaces(struct kvaser_usb *dev)
613+{
614+ int i;
615+
616+ for (i = 0; i < dev->nchannels; i++) {
617+ if (!dev->nets[i])
618+ continue;
619+
620+ unregister_candev(dev->nets[i]->netdev);
621+ }
622+
623+ kvaser_usb_unlink_all_urbs(dev);
624+
625+ for (i = 0; i < dev->nchannels; i++) {
626+ if (!dev->nets[i])
627+ continue;
628+
629+ free_candev(dev->nets[i]->netdev);
630+ }
631+}
632+
633+static int kvaser_usb_init_one(struct kvaser_usb *dev,
634+ const struct usb_device_id *id, int channel)
635+{
636+ struct net_device *netdev;
637+ struct kvaser_usb_net_priv *priv;
638+ int err;
639+
640+ if (dev->ops->dev_reset_chip) {
641+ err = dev->ops->dev_reset_chip(dev, channel);
642+ if (err)
643+ return err;
644+ }
645+
646+ netdev = alloc_candev(sizeof(*priv) +
647+ dev->max_tx_urbs * sizeof(*priv->tx_contexts),
648+ dev->max_tx_urbs);
649+ if (!netdev) {
650+ dev_err(&dev->intf->dev, "Cannot alloc candev\n");
651+ return -ENOMEM;
652+ }
653+
654+ priv = netdev_priv(netdev);
655+
656+ init_usb_anchor(&priv->tx_submitted);
657+ init_completion(&priv->start_comp);
658+ init_completion(&priv->stop_comp);
659+ priv->can.ctrlmode_supported = 0;
660+
661+ priv->dev = dev;
662+ priv->netdev = netdev;
663+ priv->channel = channel;
664+
665+ spin_lock_init(&priv->tx_contexts_lock);
666+ kvaser_usb_reset_tx_urb_contexts(priv);
667+
668+ priv->can.state = CAN_STATE_STOPPED;
669+ priv->can.clock.freq = dev->cfg->clock.freq;
670+ priv->can.bittiming_const = dev->cfg->bittiming_const;
671+ priv->can.do_set_bittiming = dev->ops->dev_set_bittiming;
672+ priv->can.do_set_mode = dev->ops->dev_set_mode;
673+ if ((id->driver_info & KVASER_USB_HAS_TXRX_ERRORS) ||
674+ (priv->dev->card_data.capabilities & KVASER_USB_CAP_BERR_CAP))
675+ priv->can.do_get_berr_counter = dev->ops->dev_get_berr_counter;
676+ if (id->driver_info & KVASER_USB_HAS_SILENT_MODE)
677+ priv->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY;
678+
679+ priv->can.ctrlmode_supported |= dev->card_data.ctrlmode_supported;
680+
681+ if (priv->can.ctrlmode_supported & CAN_CTRLMODE_FD) {
682+ priv->can.data_bittiming_const = dev->cfg->data_bittiming_const;
683+ priv->can.do_set_data_bittiming =
684+ dev->ops->dev_set_data_bittiming;
685+ }
686+
687+ netdev->flags |= IFF_ECHO;
688+
689+ netdev->netdev_ops = &kvaser_usb_netdev_ops;
690+
691+ SET_NETDEV_DEV(netdev, &dev->intf->dev);
692+ netdev->dev_id = channel;
693+
694+ dev->nets[channel] = priv;
695+
696+ err = register_candev(netdev);
697+ if (err) {
698+ dev_err(&dev->intf->dev, "Failed to register CAN device\n");
699+ free_candev(netdev);
700+ dev->nets[channel] = NULL;
701+ return err;
702+ }
703+
704+ netdev_dbg(netdev, "device registered\n");
705+
706+ return 0;
707+}
708+
709+static int kvaser_usb_probe(struct usb_interface *intf,
710+ const struct usb_device_id *id)
711+{
712+ struct kvaser_usb *dev;
713+ int err;
714+ int i;
715+
716+ dev = devm_kzalloc(&intf->dev, sizeof(*dev), GFP_KERNEL);
717+ if (!dev)
718+ return -ENOMEM;
719+
720+ if (kvaser_is_leaf(id)) {
721+ dev->card_data.leaf.family = KVASER_LEAF;
722+ dev->ops = &kvaser_usb_leaf_dev_ops;
723+ } else if (kvaser_is_usbcan(id)) {
724+ dev->card_data.leaf.family = KVASER_USBCAN;
725+ dev->ops = &kvaser_usb_leaf_dev_ops;
726+ } else if (kvaser_is_hydra(id)) {
727+ dev->ops = &kvaser_usb_hydra_dev_ops;
728+ } else {
729+ dev_err(&intf->dev,
730+ "Product ID (%d) is not a supported Kvaser USB device\n",
731+ id->idProduct);
732+ return -ENODEV;
733+ }
734+
735+ dev->intf = intf;
736+
737+ err = dev->ops->dev_setup_endpoints(dev);
738+ if (err) {
739+ dev_err(&intf->dev, "Cannot get usb endpoint(s)");
740+ return err;
741+ }
742+
743+ dev->udev = interface_to_usbdev(intf);
744+
745+ init_usb_anchor(&dev->rx_submitted);
746+
747+ usb_set_intfdata(intf, dev);
748+
749+ dev->card_data.ctrlmode_supported = 0;
750+ dev->card_data.capabilities = 0;
751+ err = dev->ops->dev_init_card(dev);
752+ if (err) {
753+ dev_err(&intf->dev,
754+ "Failed to initialize card, error %d\n", err);
755+ return err;
756+ }
757+
758+ err = dev->ops->dev_get_software_info(dev);
759+ if (err) {
760+ dev_err(&intf->dev,
761+ "Cannot get software info, error %d\n", err);
762+ return err;
763+ }
764+
765+ if (dev->ops->dev_get_software_details) {
766+ err = dev->ops->dev_get_software_details(dev);
767+ if (err) {
768+ dev_err(&intf->dev,
769+ "Cannot get software details, error %d\n", err);
770+ return err;
771+ }
772+ }
773+
774+ if (WARN_ON(!dev->cfg))
775+ return -ENODEV;
776+
777+ dev_dbg(&intf->dev, "Firmware version: %d.%d.%d\n",
778+ ((dev->fw_version >> 24) & 0xff),
779+ ((dev->fw_version >> 16) & 0xff),
780+ (dev->fw_version & 0xffff));
781+
782+ dev_dbg(&intf->dev, "Max outstanding tx = %d URBs\n", dev->max_tx_urbs);
783+
784+ err = dev->ops->dev_get_card_info(dev);
785+ if (err) {
786+ dev_err(&intf->dev, "Cannot get card info, error %d\n", err);
787+ return err;
788+ }
789+
790+ if (dev->ops->dev_get_capabilities) {
791+ err = dev->ops->dev_get_capabilities(dev);
792+ if (err) {
793+ dev_err(&intf->dev,
794+ "Cannot get capabilities, error %d\n", err);
795+ kvaser_usb_remove_interfaces(dev);
796+ return err;
797+ }
798+ }
799+
800+ for (i = 0; i < dev->nchannels; i++) {
801+ err = kvaser_usb_init_one(dev, id, i);
802+ if (err) {
803+ kvaser_usb_remove_interfaces(dev);
804+ return err;
805+ }
806+ }
807+
808+ return 0;
809+}
810+
811+static void kvaser_usb_disconnect(struct usb_interface *intf)
812+{
813+ struct kvaser_usb *dev = usb_get_intfdata(intf);
814+
815+ usb_set_intfdata(intf, NULL);
816+
817+ if (!dev)
818+ return;
819+
820+ kvaser_usb_remove_interfaces(dev);
821+}
822+
823+static struct usb_driver kvaser_usb_driver = {
824+ .name = "kvaser_usb",
825+ .probe = kvaser_usb_probe,
826+ .disconnect = kvaser_usb_disconnect,
827+ .id_table = kvaser_usb_table,
828+};
829+
830+module_usb_driver(kvaser_usb_driver);
831+
832+MODULE_AUTHOR("Olivier Sobrie <olivier@sobrie.be>");
833+MODULE_AUTHOR("Kvaser AB <support@kvaser.com>");
834+MODULE_DESCRIPTION("CAN driver for Kvaser CAN/USB devices");
835+MODULE_LICENSE("GPL v2");
drivers/net/can/usb/kvaser_usb/kvaser_usb_hydra.c+2028−0
@@ -0,0 +1,2028 @@
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
5+ *
6+ * Copyright (C) 2018 KVASER AB, Sweden. All rights reserved.
7+ * Copyright (C) 2010 Matthias Fuchs <matthias.fuchs@esd.eu>, esd gmbh
8+ *
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.
14+ */
15+
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>
26+
27+#include <linux/can.h>
28+#include <linux/can/dev.h>
29+#include <linux/can/error.h>
30+#include <linux/can/netlink.h>
31+
32+#include "kvaser_usb.h"
33+
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;
37+
38+#define KVASER_USB_HYDRA_BULK_EP_IN_ADDR 0x82
39+#define KVASER_USB_HYDRA_BULK_EP_OUT_ADDR 0x02
40+
41+#define KVASER_USB_HYDRA_MAX_TRANSID 0xff
42+#define KVASER_USB_HYDRA_MIN_TRANSID 0x01
43+
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
73+
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
78+
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()).
85+ */
86+
87+/* Well-known HE addresses */
88+#define KVASER_USB_HYDRA_HE_ADDRESS_ROUTER 0x00
89+#define KVASER_USB_HYDRA_HE_ADDRESS_ILLEGAL 0x3e
90+
91+#define KVASER_USB_HYDRA_TRANSID_CANHE 0x40
92+#define KVASER_USB_HYDRA_TRANSID_SYSDBG 0x61
93+
94+struct kvaser_cmd_map_ch_req {
95+ char name[16];
96+ u8 channel;
97+ u8 reserved[11];
98+} __packed;
99+
100+struct kvaser_cmd_map_ch_res {
101+ u8 he_addr;
102+ u8 channel;
103+ u8 reserved[26];
104+} __packed;
105+
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;
118+
119+struct kvaser_cmd_sw_info {
120+ u8 reserved0[8];
121+ __le16 max_outstanding_tx;
122+ u8 reserved1[18];
123+} __packed;
124+
125+struct kvaser_cmd_sw_detail_req {
126+ u8 use_ext_cmd;
127+ u8 reserved[27];
128+} __packed;
129+
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;
146+
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;
155+
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;
167+
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;
178+
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;
190+
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;
210+
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;
218+
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;
226+
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;
239+ };
240+} __packed;
241+
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;
253+
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.
259+ */
260+ u8 he_addr;
261+ __le16 transid;
262+} __packed;
263+
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;
269+
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;
274+
275+ struct kvaser_cmd_cap_req cap_req;
276+ struct kvaser_cmd_cap_res cap_res;
277+
278+ struct kvaser_cmd_error_event error_event;
279+
280+ struct kvaser_cmd_set_busparams set_busparams_req;
281+
282+ struct kvaser_cmd_chip_state_event chip_state_event;
283+
284+ struct kvaser_cmd_set_ctrlmode set_ctrlmode;
285+
286+ struct kvaser_cmd_rx_can rx_can;
287+ struct kvaser_cmd_tx_can tx_can;
288+ } __packed;
289+} __packed;
290+
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)
302+
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)
310+
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;
327+ };
328+} __packed;
329+
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;
340+
341+struct kvaser_cmd_ext_tx_ack {
342+ __le32 flags;
343+ u8 reserved0[4];
344+ __le64 timestamp;
345+ u8 reserved1[8];
346+} __packed;
347+
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;
354+
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;
361+
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,
372+};
373+
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,
384+};
385+
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)
393+{
394+ return le16_to_cpu(cmd->header.transid) & KVASER_USB_HYDRA_TRANSID_MASK;
395+}
396+
397+static inline void kvaser_usb_hydra_set_cmd_transid(struct kvaser_cmd *cmd,
398+ u16 transid)
399+{
400+ cmd->header.transid =
401+ cpu_to_le16(transid & KVASER_USB_HYDRA_TRANSID_MASK);
402+}
403+
404+static inline u8 kvaser_usb_hydra_get_cmd_src_he(const struct kvaser_cmd *cmd)
405+{
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;
410+}
411+
412+static inline void kvaser_usb_hydra_set_cmd_dest_he(struct kvaser_cmd *cmd,
413+ u8 dest_he)
414+{
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);
418+}
419+
420+static u8 kvaser_usb_hydra_channel_from_cmd(const struct kvaser_usb *dev,
421+ const struct kvaser_cmd *cmd)
422+{
423+ int i;
424+ u8 channel = 0xff;
425+ u8 src_he = kvaser_usb_hydra_get_cmd_src_he(cmd);
426+
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;
431+ }
432+ }
433+
434+ return channel;
435+}
436+
437+static u16 kvaser_usb_hydra_get_next_transid(struct kvaser_usb *dev)
438+{
439+ unsigned long flags;
440+ u16 transid;
441+ struct kvaser_usb_dev_card_data_hydra *card_data =
442+ &dev->card_data.hydra;
443+
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);
452+
453+ return transid;
454+}
455+
456+static size_t kvaser_usb_hydra_cmd_size(struct kvaser_cmd *cmd)
457+{
458+ size_t ret;
459+
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);
464+
465+ return ret;
466+}
467+
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)
471+{
472+ struct kvaser_usb_net_priv *priv = NULL;
473+ u8 channel = kvaser_usb_hydra_channel_from_cmd(dev, cmd);
474+
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];
480+
481+ return priv;
482+}
483+
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)
487+{
488+ u64 ticks;
489+
490+ if (cmd->header.cmd_no == CMD_EXTENDED) {
491+ struct kvaser_cmd_ext *cmd_ext = (struct kvaser_cmd_ext *)cmd;
492+
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;
498+ }
499+
500+ return ns_to_ktime(div_u64(ticks * 1000, cfg->timestamp_freq));
501+}
502+
503+static int kvaser_usb_hydra_send_simple_cmd(struct kvaser_usb *dev,
504+ u8 cmd_no, int channel)
505+{
506+ struct kvaser_cmd *cmd;
507+ int err;
508+
509+ cmd = kcalloc(1, sizeof(struct kvaser_cmd), GFP_KERNEL);
510+ if (!cmd)
511+ return -ENOMEM;
512+
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;
523+ }
524+ kvaser_usb_hydra_set_cmd_dest_he
525+ (cmd, dev->card_data.hydra.channel_to_he[channel]);
526+ }
527+ kvaser_usb_hydra_set_cmd_transid
528+ (cmd, kvaser_usb_hydra_get_next_transid(dev));
529+
530+ err = kvaser_usb_send_cmd(dev, cmd, kvaser_usb_hydra_cmd_size(cmd));
531+ if (err)
532+ goto end;
533+
534+end:
535+ kfree(cmd);
536+
537+ return err;
538+}
539+
540+static int
541+kvaser_usb_hydra_send_simple_cmd_async(struct kvaser_usb_net_priv *priv,
542+ u8 cmd_no)
543+{
544+ struct kvaser_cmd *cmd;
545+ struct kvaser_usb *dev = priv->dev;
546+ int err;
547+
548+ cmd = kcalloc(1, sizeof(struct kvaser_cmd), GFP_ATOMIC);
549+ if (!cmd)
550+ return -ENOMEM;
551+
552+ cmd->header.cmd_no = cmd_no;
553+
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));
558+
559+ err = kvaser_usb_send_cmd_async(priv, cmd,
560+ kvaser_usb_hydra_cmd_size(cmd));
561+ if (err)
562+ kfree(cmd);
563+
564+ return err;
565+}
566+
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.
571+ */
572+static int kvaser_usb_hydra_wait_cmd(const struct kvaser_usb *dev, u8 cmd_no,
573+ struct kvaser_cmd *cmd)
574+{
575+ void *buf;
576+ int err;
577+ unsigned long timeout = jiffies + msecs_to_jiffies(KVASER_USB_TIMEOUT);
578+
579+ if (cmd->header.cmd_no == CMD_EXTENDED) {
580+ dev_err(&dev->intf->dev, "Wait for CMD_EXTENDED not allowed\n");
581+ return -EINVAL;
582+ }
583+
584+ buf = kzalloc(KVASER_USB_RX_BUFFER_SIZE, GFP_KERNEL);
585+ if (!buf)
586+ return -ENOMEM;
587+
588+ do {
589+ int actual_len = 0;
590+ int pos = 0;
591+
592+ err = kvaser_usb_recv_cmd(dev, buf, KVASER_USB_RX_BUFFER_SIZE,
593+ &actual_len);
594+ if (err < 0)
595+ goto end;
596+
597+ while (pos < actual_len) {
598+ struct kvaser_cmd *tmp_cmd;
599+ size_t cmd_len;
600+
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;
607+ }
608+
609+ if (tmp_cmd->header.cmd_no == cmd_no) {
610+ memcpy(cmd, tmp_cmd, cmd_len);
611+ goto end;
612+ }
613+ pos += cmd_len;
614+ }
615+ } while (time_before(jiffies, timeout));
616+
617+ err = -EINVAL;
618+
619+end:
620+ kfree(buf);
621+
622+ return err;
623+}
624+
625+static int kvaser_usb_hydra_map_channel_resp(struct kvaser_usb *dev,
626+ const struct kvaser_cmd *cmd)
627+{
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;
632+
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;
638+ }
639+
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;
658+ }
659+
660+ return 0;
661+}
662+
663+static int kvaser_usb_hydra_map_channel(struct kvaser_usb *dev, u16 transid,
664+ u8 channel, const char *name)
665+{
666+ struct kvaser_cmd *cmd;
667+ int err;
668+
669+ cmd = kcalloc(1, sizeof(struct kvaser_cmd), GFP_KERNEL);
670+ if (!cmd)
671+ return -ENOMEM;
672+
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;
678+
679+ kvaser_usb_hydra_set_cmd_transid(cmd, transid);
680+
681+ err = kvaser_usb_send_cmd(dev, cmd, kvaser_usb_hydra_cmd_size(cmd));
682+ if (err)
683+ goto end;
684+
685+ err = kvaser_usb_hydra_wait_cmd(dev, CMD_MAP_CHANNEL_RESP, cmd);
686+ if (err)
687+ goto end;
688+
689+ err = kvaser_usb_hydra_map_channel_resp(dev, cmd);
690+ if (err)
691+ goto end;
692+
693+end:
694+ kfree(cmd);
695+
696+ return err;
697+}
698+
699+static int kvaser_usb_hydra_get_single_capability(struct kvaser_usb *dev,
700+ u16 cap_cmd_req, u16 *status)
701+{
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;
709+
710+ cmd = kcalloc(1, sizeof(struct kvaser_cmd), GFP_KERNEL);
711+ if (!cmd)
712+ return -ENOMEM;
713+
714+ cmd->header.cmd_no = CMD_GET_CAPABILITIES_REQ;
715+ cmd->cap_req.cap_cmd = cpu_to_le16(cap_cmd_req);
716+
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));
720+
721+ err = kvaser_usb_send_cmd(dev, cmd, kvaser_usb_hydra_cmd_size(cmd));
722+ if (err)
723+ goto end;
724+
725+ err = kvaser_usb_hydra_wait_cmd(dev, CMD_GET_CAPABILITIES_RESP, cmd);
726+ if (err)
727+ goto end;
728+
729+ *status = le16_to_cpu(cmd->cap_res.status);
730+
731+ if (*status != KVASER_USB_HYDRA_CAP_STAT_OK)
732+ goto end;
733+
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;
746+ }
747+
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;
763+ }
764+ }
765+ }
766+
767+end:
768+ kfree(cmd);
769+
770+ return err;
771+}
772+
773+static void kvaser_usb_hydra_start_chip_reply(const struct kvaser_usb *dev,
774+ const struct kvaser_cmd *cmd)
775+{
776+ struct kvaser_usb_net_priv *priv;
777+
778+ priv = kvaser_usb_hydra_net_priv_from_cmd(dev, cmd);
779+ if (!priv)
780+ return;
781+
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);
788+ }
789+}
790+
791+static void kvaser_usb_hydra_stop_chip_reply(const struct kvaser_usb *dev,
792+ const struct kvaser_cmd *cmd)
793+{
794+ struct kvaser_usb_net_priv *priv;
795+
796+ priv = kvaser_usb_hydra_net_priv_from_cmd(dev, cmd);
797+ if (!priv)
798+ return;
799+
800+ complete(&priv->stop_comp);
801+}
802+
803+static void kvaser_usb_hydra_flush_queue_reply(const struct kvaser_usb *dev,
804+ const struct kvaser_cmd *cmd)
805+{
806+ struct kvaser_usb_net_priv *priv;
807+
808+ priv = kvaser_usb_hydra_net_priv_from_cmd(dev, cmd);
809+ if (!priv)
810+ return;
811+
812+ complete(&priv->flush_comp);
813+}
814+
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)
820+{
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;
835+ }
836+ }
837+}
838+
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)
842+{
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;
848+
849+ old_state = priv->can.state;
850+
851+ kvaser_usb_hydra_bus_status_to_can_state(priv, bus_status, bec,
852+ &new_state);
853+
854+ if (new_state == old_state)
855+ return;
856+
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.
860+ */
861+ if (old_state == CAN_STATE_STOPPED && new_state == CAN_STATE_BUS_OFF)
862+ return;
863+
864+ skb = alloc_can_err_skb(netdev, &cf);
865+ if (skb) {
866+ enum can_state tx_state, rx_state;
867+
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);
873+ }
874+
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);
879+
880+ can_bus_off(netdev);
881+ }
882+
883+ if (!skb) {
884+ netdev_warn(netdev, "No memory left for err_skb\n");
885+ return;
886+ }
887+
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++;
892+
893+ cf->data[6] = bec->txerr;
894+ cf->data[7] = bec->rxerr;
895+
896+ stats = &netdev->stats;
897+ stats->rx_packets++;
898+ stats->rx_bytes += cf->can_dlc;
899+ netif_rx(skb);
900+}
901+
902+static void kvaser_usb_hydra_state_event(const struct kvaser_usb *dev,
903+ const struct kvaser_cmd *cmd)
904+{
905+ struct kvaser_usb_net_priv *priv;
906+ struct can_berr_counter bec;
907+ u8 bus_status;
908+
909+ priv = kvaser_usb_hydra_net_priv_from_cmd(dev, cmd);
910+ if (!priv)
911+ return;
912+
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;
916+
917+ kvaser_usb_hydra_update_state(priv, bus_status, &bec);
918+ priv->bec.txerr = bec.txerr;
919+ priv->bec.rxerr = bec.rxerr;
920+}
921+
922+static void kvaser_usb_hydra_error_event_parameter(const struct kvaser_usb *dev,
923+ const struct kvaser_cmd *cmd)
924+{
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;
931+
932+ case CMD_STOP_CHIP_REQ:
933+ dev_warn(&dev->intf->dev,
934+ "CMD_STOP_CHIP_REQ error in parameter\n");
935+ break;
936+
937+ case CMD_FLUSH_QUEUE:
938+ dev_warn(&dev->intf->dev,
939+ "CMD_FLUSH_QUEUE error in parameter\n");
940+ break;
941+
942+ case CMD_SET_BUSPARAMS_REQ:
943+ dev_warn(&dev->intf->dev,
944+ "Set bittiming failed. Error in parameter\n");
945+ break;
946+
947+ case CMD_SET_BUSPARAMS_FD_REQ:
948+ dev_warn(&dev->intf->dev,
949+ "Set data bittiming failed. Error in parameter\n");
950+ break;
951+
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;
957+ }
958+}
959+
960+static void kvaser_usb_hydra_error_event(const struct kvaser_usb *dev,
961+ const struct kvaser_cmd *cmd)
962+{
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;
967+
968+ case KVASER_USB_HYDRA_ERROR_EVENT_CAN:
969+ /* Wrong channel mapping?! This should never happen!
970+ * info1 will contain the offending cmd_no
971+ */
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;
976+
977+ default:
978+ dev_warn(&dev->intf->dev,
979+ "Unhandled error event (%d)\n",
980+ cmd->error_event.error_code);
981+ break;
982+ }
983+}
984+
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)
989+{
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;
998+
999+ priv->can.can_stats.bus_error++;
1000+ stats->rx_errors++;
1001+
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;
1005+
1006+ old_state = priv->can.state;
1007+ kvaser_usb_hydra_bus_status_to_can_state(priv, bus_status, &bec,
1008+ &new_state);
1009+
1010+ skb = alloc_can_err_skb(netdev, &cf);
1011+
1012+ if (new_state != old_state) {
1013+ if (skb) {
1014+ enum can_state tx_state, rx_state;
1015+
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;
1020+
1021+ can_change_state(netdev, cf, tx_state, rx_state);
1022+ }
1023+
1024+ if (new_state == CAN_STATE_BUS_OFF) {
1025+ if (!priv->can.restart_ms)
1026+ kvaser_usb_hydra_send_simple_cmd_async
1027+ (priv, CMD_STOP_CHIP_REQ);
1028+
1029+ can_bus_off(netdev);
1030+ }
1031+
1032+ if (priv->can.restart_ms &&
1033+ old_state >= CAN_STATE_BUS_OFF &&
1034+ new_state < CAN_STATE_BUS_OFF)
1035+ cf->can_id |= CAN_ERR_RESTARTED;
1036+ }
1037+
1038+ if (!skb) {
1039+ stats->rx_dropped++;
1040+ netdev_warn(netdev, "No memory left for err_skb\n");
1041+ return;
1042+ }
1043+
1044+ shhwtstamps = skb_hwtstamps(skb);
1045+ shhwtstamps->hwtstamp = hwtstamp;
1046+
1047+ cf->can_id |= CAN_ERR_BUSERROR;
1048+ cf->data[6] = bec.txerr;
1049+ cf->data[7] = bec.rxerr;
1050+
1051+ stats->rx_packets++;
1052+ stats->rx_bytes += cf->can_dlc;
1053+ netif_rx(skb);
1054+
1055+ priv->bec.txerr = bec.txerr;
1056+ priv->bec.rxerr = bec.rxerr;
1057+}
1058+
1059+static void kvaser_usb_hydra_one_shot_fail(struct kvaser_usb_net_priv *priv,
1060+ const struct kvaser_cmd_ext *cmd)
1061+{
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;
1067+
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;
1073+ }
1074+
1075+ cf->can_id |= CAN_ERR_BUSERROR;
1076+ flags = le32_to_cpu(cmd->tx_ack.flags);
1077+
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++;
1083+ }
1084+
1085+ stats->tx_errors++;
1086+ stats->rx_packets++;
1087+ stats->rx_bytes += cf->can_dlc;
1088+ netif_rx(skb);
1089+}
1090+
1091+static void kvaser_usb_hydra_tx_acknowledge(const struct kvaser_usb *dev,
1092+ const struct kvaser_cmd *cmd)
1093+{
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);
1099+
1100+ priv = kvaser_usb_hydra_net_priv_from_cmd(dev, cmd);
1101+ if (!priv)
1102+ return;
1103+
1104+ if (!netif_device_present(priv->netdev))
1105+ return;
1106+
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);
1110+
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;
1115+ }
1116+ }
1117+
1118+ context = &priv->tx_contexts[transid % dev->max_tx_urbs];
1119+ if (!one_shot_fail) {
1120+ struct net_device_stats *stats = &priv->netdev->stats;
1121+
1122+ stats->tx_packets++;
1123+ stats->tx_bytes += can_dlc2len(context->dlc);
1124+ }
1125+
1126+ spin_lock_irqsave(&priv->tx_contexts_lock, irq_flags);
1127+
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);
1132+
1133+ spin_unlock_irqrestore(&priv->tx_contexts_lock, irq_flags);
1134+}
1135+
1136+static void kvaser_usb_hydra_rx_msg_std(const struct kvaser_usb *dev,
1137+ const struct kvaser_cmd *cmd)
1138+{
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;
1146+
1147+ priv = kvaser_usb_hydra_net_priv_from_cmd(dev, cmd);
1148+ if (!priv)
1149+ return;
1150+
1151+ stats = &priv->netdev->stats;
1152+
1153+ flags = cmd->rx_can.flags;
1154+ hwtstamp = kvaser_usb_hydra_ktime_from_rx_cmd(dev->cfg, cmd);
1155+
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;
1160+ }
1161+
1162+ skb = alloc_can_skb(priv->netdev, &cf);
1163+ if (!skb) {
1164+ stats->rx_dropped++;
1165+ return;
1166+ }
1167+
1168+ shhwtstamps = skb_hwtstamps(skb);
1169+ shhwtstamps->hwtstamp = hwtstamp;
1170+
1171+ cf->can_id = le32_to_cpu(cmd->rx_can.id);
1172+
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;
1178+ }
1179+
1180+ if (flags & KVASER_USB_HYDRA_CF_FLAG_OVERRUN)
1181+ kvaser_usb_can_rx_over_error(priv->netdev);
1182+
1183+ cf->can_dlc = get_can_dlc(cmd->rx_can.dlc);
1184+
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);
1189+
1190+ stats->rx_packets++;
1191+ stats->rx_bytes += cf->can_dlc;
1192+ netif_rx(skb);
1193+}
1194+
1195+static void kvaser_usb_hydra_rx_msg_ext(const struct kvaser_usb *dev,
1196+ const struct kvaser_cmd_ext *cmd)
1197+{
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;
1208+
1209+ priv = kvaser_usb_hydra_net_priv_from_cmd(dev, std_cmd);
1210+ if (!priv)
1211+ return;
1212+
1213+ stats = &priv->netdev->stats;
1214+
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;
1218+
1219+ flags = le32_to_cpu(cmd->rx_can.flags);
1220+ hwtstamp = kvaser_usb_hydra_ktime_from_rx_cmd(dev->cfg, std_cmd);
1221+
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;
1226+ }
1227+
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);
1232+
1233+ if (!skb) {
1234+ stats->rx_dropped++;
1235+ return;
1236+ }
1237+
1238+ shhwtstamps = skb_hwtstamps(skb);
1239+ shhwtstamps->hwtstamp = hwtstamp;
1240+
1241+ cf->can_id = le32_to_cpu(cmd->rx_can.id);
1242+
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;
1248+ }
1249+
1250+ if (flags & KVASER_USB_HYDRA_CF_FLAG_OVERRUN)
1251+ kvaser_usb_can_rx_over_error(priv->netdev);
1252+
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);
1261+ }
1262+
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);
1267+
1268+ stats->rx_packets++;
1269+ stats->rx_bytes += cf->len;
1270+ netif_rx(skb);
1271+}
1272+
1273+static void kvaser_usb_hydra_handle_cmd_std(const struct kvaser_usb *dev,
1274+ const struct kvaser_cmd *cmd)
1275+{
1276+ switch (cmd->header.cmd_no) {
1277+ case CMD_START_CHIP_RESP:
1278+ kvaser_usb_hydra_start_chip_reply(dev, cmd);
1279+ break;
1280+
1281+ case CMD_STOP_CHIP_RESP:
1282+ kvaser_usb_hydra_stop_chip_reply(dev, cmd);
1283+ break;
1284+
1285+ case CMD_FLUSH_QUEUE_RESP:
1286+ kvaser_usb_hydra_flush_queue_reply(dev, cmd);
1287+ break;
1288+
1289+ case CMD_CHIP_STATE_EVENT:
1290+ kvaser_usb_hydra_state_event(dev, cmd);
1291+ break;
1292+
1293+ case CMD_ERROR_EVENT:
1294+ kvaser_usb_hydra_error_event(dev, cmd);
1295+ break;
1296+
1297+ case CMD_TX_ACKNOWLEDGE:
1298+ kvaser_usb_hydra_tx_acknowledge(dev, cmd);
1299+ break;
1300+
1301+ case CMD_RX_MESSAGE:
1302+ kvaser_usb_hydra_rx_msg_std(dev, cmd);
1303+ break;
1304+
1305+ /* Ignored commands */
1306+ case CMD_SET_BUSPARAMS_RESP:
1307+ case CMD_SET_BUSPARAMS_FD_RESP:
1308+ break;
1309+
1310+ default:
1311+ dev_warn(&dev->intf->dev, "Unhandled command (%d)\n",
1312+ cmd->header.cmd_no);
1313+ break;
1314+ }
1315+}
1316+
1317+static void kvaser_usb_hydra_handle_cmd_ext(const struct kvaser_usb *dev,
1318+ const struct kvaser_cmd_ext *cmd)
1319+{
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;
1324+
1325+ case CMD_RX_MESSAGE_FD:
1326+ kvaser_usb_hydra_rx_msg_ext(dev, cmd);
1327+ break;
1328+
1329+ default:
1330+ dev_warn(&dev->intf->dev, "Unhandled extended command (%d)\n",
1331+ cmd->header.cmd_no);
1332+ break;
1333+ }
1334+}
1335+
1336+static void kvaser_usb_hydra_handle_cmd(const struct kvaser_usb *dev,
1337+ const struct kvaser_cmd *cmd)
1338+{
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);
1344+}
1345+
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)
1350+{
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;
1360+
1361+ *frame_len = nbr_of_bytes;
1362+
1363+ cmd = kcalloc(1, sizeof(struct kvaser_cmd_ext), GFP_ATOMIC);
1364+ if (!cmd)
1365+ return NULL;
1366+
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);
1371+
1372+ cmd->header.cmd_no = CMD_EXTENDED;
1373+ cmd->cmd_no_ext = CMD_TX_CAN_MESSAGE_FD;
1374+
1375+ *cmd_len = ALIGN(sizeof(struct kvaser_cmd_ext) -
1376+ sizeof(cmd->tx_can.kcan_payload) + nbr_of_bytes,
1377+ 8);
1378+
1379+ cmd->len = cpu_to_le16(*cmd_len);
1380+
1381+ cmd->tx_can.databytes = nbr_of_bytes;
1382+ cmd->tx_can.dlc = dlc;
1383+
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;
1393+ }
1394+
1395+ if (cf->can_id & CAN_ERR_FLAG)
1396+ flags |= KVASER_USB_HYDRA_CF_FLAG_ERROR_FRAME;
1397+
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);
1403+
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;
1413+ }
1414+ }
1415+
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);
1420+
1421+ memcpy(cmd->tx_can.kcan_payload, cf->data, nbr_of_bytes);
1422+
1423+ return cmd;
1424+}
1425+
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)
1430+{
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;
1436+
1437+ *frame_len = cf->can_dlc;
1438+
1439+ cmd = kcalloc(1, sizeof(struct kvaser_cmd), GFP_ATOMIC);
1440+ if (!cmd)
1441+ return NULL;
1442+
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);
1446+
1447+ cmd->header.cmd_no = CMD_TX_CAN_MESSAGE;
1448+
1449+ *cmd_len = ALIGN(sizeof(struct kvaser_cmd), 8);
1450+
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;
1456+ }
1457+
1458+ cmd->tx_can.dlc = cf->can_dlc;
1459+
1460+ flags = (cf->can_id & CAN_EFF_FLAG ?
1461+ KVASER_USB_HYDRA_CF_FLAG_EXTENDED_ID : 0);
1462+
1463+ if (cf->can_id & CAN_RTR_FLAG)
1464+ flags |= KVASER_USB_HYDRA_CF_FLAG_REMOTE_FRAME;
1465+
1466+ flags |= (cf->can_id & CAN_ERR_FLAG ?
1467+ KVASER_USB_HYDRA_CF_FLAG_ERROR_FRAME : 0);
1468+
1469+ cmd->tx_can.id = cpu_to_le32(id);
1470+ cmd->tx_can.flags = flags;
1471+
1472+ memcpy(cmd->tx_can.data, cf->data, *frame_len);
1473+
1474+ return cmd;
1475+}
1476+
1477+static int kvaser_usb_hydra_set_mode(struct net_device *netdev,
1478+ enum can_mode mode)
1479+{
1480+ int err = 0;
1481+
1482+ switch (mode) {
1483+ case CAN_MODE_START:
1484+ /* CAN controller automatically recovers from BUS_OFF */
1485+ break;
1486+ default:
1487+ err = -EOPNOTSUPP;
1488+ }
1489+
1490+ return err;
1491+}
1492+
1493+static int kvaser_usb_hydra_set_bittiming(struct net_device *netdev)
1494+{
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;
1503+
1504+ cmd = kcalloc(1, sizeof(struct kvaser_cmd), GFP_KERNEL);
1505+ if (!cmd)
1506+ return -ENOMEM;
1507+
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;
1514+
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));
1519+
1520+ err = kvaser_usb_send_cmd(dev, cmd, kvaser_usb_hydra_cmd_size(cmd));
1521+
1522+ kfree(cmd);
1523+
1524+ return err;
1525+}
1526+
1527+static int kvaser_usb_hydra_set_data_bittiming(struct net_device *netdev)
1528+{
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;
1537+
1538+ cmd = kcalloc(1, sizeof(struct kvaser_cmd), GFP_KERNEL);
1539+ if (!cmd)
1540+ return -ENOMEM;
1541+
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;
1548+
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;
1556+ }
1557+
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));
1562+
1563+ err = kvaser_usb_send_cmd(dev, cmd, kvaser_usb_hydra_cmd_size(cmd));
1564+
1565+ kfree(cmd);
1566+
1567+ return err;
1568+}
1569+
1570+static int kvaser_usb_hydra_get_berr_counter(const struct net_device *netdev,
1571+ struct can_berr_counter *bec)
1572+{
1573+ struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1574+ int err;
1575+
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;
1581+
1582+ *bec = priv->bec;
1583+
1584+ return 0;
1585+}
1586+
1587+static int kvaser_usb_hydra_setup_endpoints(struct kvaser_usb *dev)
1588+{
1589+ const struct usb_host_interface *iface_desc;
1590+ struct usb_endpoint_descriptor *ep;
1591+ int i;
1592+
1593+ iface_desc = &dev->intf->altsetting[0];
1594+
1595+ for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1596+ ep = &iface_desc->endpoint[i].desc;
1597+
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;
1601+
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;
1605+
1606+ if (dev->bulk_in && dev->bulk_out)
1607+ return 0;
1608+ }
1609+
1610+ return -ENODEV;
1611+}
1612+
1613+static int kvaser_usb_hydra_init_card(struct kvaser_usb *dev)
1614+{
1615+ int err;
1616+ unsigned int i;
1617+ struct kvaser_usb_dev_card_data_hydra *card_data =
1618+ &dev->card_data.hydra;
1619+
1620+ card_data->transid = KVASER_USB_HYDRA_MIN_TRANSID;
1621+ spin_lock_init(&card_data->transid_lock);
1622+
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);
1626+
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;
1630+
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;
1640+ }
1641+ }
1642+
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;
1649+ }
1650+
1651+ return 0;
1652+}
1653+
1654+static int kvaser_usb_hydra_get_software_info(struct kvaser_usb *dev)
1655+{
1656+ struct kvaser_cmd cmd;
1657+ int err;
1658+
1659+ err = kvaser_usb_hydra_send_simple_cmd(dev, CMD_GET_SOFTWARE_INFO_REQ,
1660+ -1);
1661+ if (err)
1662+ return err;
1663+
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;
1668+
1669+ dev->max_tx_urbs = min_t(unsigned int, KVASER_USB_MAX_TX_URBS,
1670+ le16_to_cpu(cmd.sw_info.max_outstanding_tx));
1671+
1672+ return 0;
1673+}
1674+
1675+static int kvaser_usb_hydra_get_software_details(struct kvaser_usb *dev)
1676+{
1677+ struct kvaser_cmd *cmd;
1678+ int err;
1679+ u32 flags;
1680+ struct kvaser_usb_dev_card_data *card_data = &dev->card_data;
1681+
1682+ cmd = kcalloc(1, sizeof(struct kvaser_cmd), GFP_KERNEL);
1683+ if (!cmd)
1684+ return -ENOMEM;
1685+
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);
1690+
1691+ kvaser_usb_hydra_set_cmd_transid
1692+ (cmd, kvaser_usb_hydra_get_next_transid(dev));
1693+
1694+ err = kvaser_usb_send_cmd(dev, cmd, kvaser_usb_hydra_cmd_size(cmd));
1695+ if (err)
1696+ goto end;
1697+
1698+ err = kvaser_usb_hydra_wait_cmd(dev, CMD_GET_SOFTWARE_DETAILS_RESP,
1699+ cmd);
1700+ if (err)
1701+ goto end;
1702+
1703+ dev->fw_version = le32_to_cpu(cmd->sw_detail_res.sw_version);
1704+ flags = le32_to_cpu(cmd->sw_detail_res.sw_flags);
1705+
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;
1711+ }
1712+
1713+ if (flags & KVASER_USB_HYDRA_SW_FLAG_FW_BETA)
1714+ dev_info(&dev->intf->dev, "Beta firmware in use\n");
1715+
1716+ if (flags & KVASER_USB_HYDRA_SW_FLAG_EXT_CAP)
1717+ card_data->capabilities |= KVASER_USB_CAP_EXT_CAP;
1718+
1719+ if (flags & KVASER_USB_HYDRA_SW_FLAG_EXT_CMD)
1720+ card_data->capabilities |= KVASER_USB_HYDRA_CAP_EXT_CMD;
1721+
1722+ if (flags & KVASER_USB_HYDRA_SW_FLAG_CANFD)
1723+ card_data->ctrlmode_supported |= CAN_CTRLMODE_FD;
1724+
1725+ if (flags & KVASER_USB_HYDRA_SW_FLAG_NONISO)
1726+ card_data->ctrlmode_supported |= CAN_CTRLMODE_FD_NON_ISO;
1727+
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;
1732+
1733+end:
1734+ kfree(cmd);
1735+
1736+ return err;
1737+}
1738+
1739+static int kvaser_usb_hydra_get_card_info(struct kvaser_usb *dev)
1740+{
1741+ struct kvaser_cmd cmd;
1742+ int err;
1743+
1744+ err = kvaser_usb_hydra_send_simple_cmd(dev, CMD_GET_CARD_INFO_REQ, -1);
1745+ if (err)
1746+ return err;
1747+
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;
1752+
1753+ dev->nchannels = cmd.card_info.nchannels;
1754+ if (dev->nchannels > KVASER_USB_MAX_NET_DEVICES)
1755+ return -EINVAL;
1756+
1757+ return 0;
1758+}
1759+
1760+static int kvaser_usb_hydra_get_capabilities(struct kvaser_usb *dev)
1761+{
1762+ int err;
1763+ u16 status;
1764+
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;
1769+ }
1770+
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);
1781+
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);
1792+
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);
1802+
1803+ return 0;
1804+}
1805+
1806+static int kvaser_usb_hydra_set_opt_mode(const struct kvaser_usb_net_priv *priv)
1807+{
1808+ struct kvaser_usb *dev = priv->dev;
1809+ struct kvaser_cmd *cmd;
1810+ int err;
1811+
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;
1818+ }
1819+
1820+ cmd = kcalloc(1, sizeof(struct kvaser_cmd), GFP_KERNEL);
1821+ if (!cmd)
1822+ return -ENOMEM;
1823+
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;
1833+
1834+ err = kvaser_usb_send_cmd(dev, cmd, kvaser_usb_hydra_cmd_size(cmd));
1835+ kfree(cmd);
1836+
1837+ return err;
1838+}
1839+
1840+static int kvaser_usb_hydra_start_chip(struct kvaser_usb_net_priv *priv)
1841+{
1842+ int err;
1843+
1844+ init_completion(&priv->start_comp);
1845+
1846+ err = kvaser_usb_hydra_send_simple_cmd(priv->dev, CMD_START_CHIP_REQ,
1847+ priv->channel);
1848+ if (err)
1849+ return err;
1850+
1851+ if (!wait_for_completion_timeout(&priv->start_comp,
1852+ msecs_to_jiffies(KVASER_USB_TIMEOUT)))
1853+ return -ETIMEDOUT;
1854+
1855+ return 0;
1856+}
1857+
1858+static int kvaser_usb_hydra_stop_chip(struct kvaser_usb_net_priv *priv)
1859+{
1860+ int err;
1861+
1862+ init_completion(&priv->stop_comp);
1863+
1864+ /* Make sure we do not report invalid BUS_OFF from CMD_CHIP_STATE_EVENT
1865+ * see comment in kvaser_usb_hydra_update_state()
1866+ */
1867+ priv->can.state = CAN_STATE_STOPPED;
1868+
1869+ err = kvaser_usb_hydra_send_simple_cmd(priv->dev, CMD_STOP_CHIP_REQ,
1870+ priv->channel);
1871+ if (err)
1872+ return err;
1873+
1874+ if (!wait_for_completion_timeout(&priv->stop_comp,
1875+ msecs_to_jiffies(KVASER_USB_TIMEOUT)))
1876+ return -ETIMEDOUT;
1877+
1878+ return 0;
1879+}
1880+
1881+static int kvaser_usb_hydra_flush_queue(struct kvaser_usb_net_priv *priv)
1882+{
1883+ int err;
1884+
1885+ init_completion(&priv->flush_comp);
1886+
1887+ err = kvaser_usb_hydra_send_simple_cmd(priv->dev, CMD_FLUSH_QUEUE,
1888+ priv->channel);
1889+ if (err)
1890+ return err;
1891+
1892+ if (!wait_for_completion_timeout(&priv->flush_comp,
1893+ msecs_to_jiffies(KVASER_USB_TIMEOUT)))
1894+ return -ETIMEDOUT;
1895+
1896+ return 0;
1897+}
1898+
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.
1901+ */
1902+static void kvaser_usb_hydra_read_bulk_callback(struct kvaser_usb *dev,
1903+ void *buf, int len)
1904+{
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;
1913+
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;
1918+
1919+ cmd = (struct kvaser_cmd *)card_data->usb_rx_leftover;
1920+
1921+ cmd_len = kvaser_usb_hydra_cmd_size(cmd);
1922+
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;
1931+ }
1932+
1933+ memcpy(card_data->usb_rx_leftover + usb_rx_leftover_len, buf,
1934+ remaining_bytes);
1935+ pos += remaining_bytes;
1936+
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;
1943+ }
1944+ card_data->usb_rx_leftover_len = usb_rx_leftover_len;
1945+ }
1946+ spin_unlock_irqrestore(usb_rx_leftover_lock, irq_flags);
1947+
1948+ while (pos < len) {
1949+ cmd = buf + pos;
1950+
1951+ cmd_len = kvaser_usb_hydra_cmd_size(cmd);
1952+
1953+ if (pos + cmd_len > len) {
1954+ /* We got first part of a command */
1955+ int leftover_bytes;
1956+
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;
1962+ }
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;
1969+ }
1970+
1971+ kvaser_usb_hydra_handle_cmd(dev, cmd);
1972+ pos += cmd_len;
1973+ }
1974+}
1975+
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)
1980+{
1981+ void *buf;
1982+
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);
1989+
1990+ return buf;
1991+}
1992+
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,
2011+};
2012+
2013+static const struct kvaser_usb_dev_cfg kvaser_usb_hydra_dev_cfg_kcan = {
2014+ .clock = {
2015+ .freq = 80000000,
2016+ },
2017+ .timestamp_freq = 80,
2018+ .bittiming_const = &kvaser_usb_hydra_kcan_bittiming_c,
2019+ .data_bittiming_const = &kvaser_usb_hydra_kcan_bittiming_c,
2020+};
2021+
2022+static const struct kvaser_usb_dev_cfg kvaser_usb_hydra_dev_cfg_flexc = {
2023+ .clock = {
2024+ .freq = 24000000,
2025+ },
2026+ .timestamp_freq = 1,
2027+ .bittiming_const = &kvaser_usb_hydra_flexc_bittiming_c,
2028+};
drivers/net/can/usb/kvaser_usb/kvaser_usb_leaf.c+1358−0
@@ -0,0 +1,1358 @@
1+// SPDX-License-Identifier: GPL-2.0
2+/* Parts of this driver are based on the following:
3+ * - Kvaser linux leaf driver (version 4.78)
4+ * - CAN driver for esd CAN-USB/2
5+ * - Kvaser linux usbcanII driver (version 5.3)
6+ *
7+ * Copyright (C) 2002-2018 KVASER AB, Sweden. All rights reserved.
8+ * Copyright (C) 2010 Matthias Fuchs <matthias.fuchs@esd.eu>, esd gmbh
9+ * Copyright (C) 2012 Olivier Sobrie <olivier@sobrie.be>
10+ * Copyright (C) 2015 Valeo S.A.
11+ */
12+
13+#include <linux/completion.h>
14+#include <linux/device.h>
15+#include <linux/gfp.h>
16+#include <linux/jiffies.h>
17+#include <linux/kernel.h>
18+#include <linux/netdevice.h>
19+#include <linux/spinlock.h>
20+#include <linux/string.h>
21+#include <linux/types.h>
22+#include <linux/usb.h>
23+
24+#include <linux/can.h>
25+#include <linux/can/dev.h>
26+#include <linux/can/error.h>
27+#include <linux/can/netlink.h>
28+
29+#include "kvaser_usb.h"
30+
31+/* Forward declaration */
32+static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_dev_cfg;
33+
34+#define CAN_USB_CLOCK 8000000
35+#define MAX_USBCAN_NET_DEVICES 2
36+
37+/* Command header size */
38+#define CMD_HEADER_LEN 2
39+
40+/* Kvaser CAN message flags */
41+#define MSG_FLAG_ERROR_FRAME BIT(0)
42+#define MSG_FLAG_OVERRUN BIT(1)
43+#define MSG_FLAG_NERR BIT(2)
44+#define MSG_FLAG_WAKEUP BIT(3)
45+#define MSG_FLAG_REMOTE_FRAME BIT(4)
46+#define MSG_FLAG_RESERVED BIT(5)
47+#define MSG_FLAG_TX_ACK BIT(6)
48+#define MSG_FLAG_TX_REQUEST BIT(7)
49+
50+/* CAN states (M16C CxSTRH register) */
51+#define M16C_STATE_BUS_RESET BIT(0)
52+#define M16C_STATE_BUS_ERROR BIT(4)
53+#define M16C_STATE_BUS_PASSIVE BIT(5)
54+#define M16C_STATE_BUS_OFF BIT(6)
55+
56+/* Leaf/usbcan command ids */
57+#define CMD_RX_STD_MESSAGE 12
58+#define CMD_TX_STD_MESSAGE 13
59+#define CMD_RX_EXT_MESSAGE 14
60+#define CMD_TX_EXT_MESSAGE 15
61+#define CMD_SET_BUS_PARAMS 16
62+#define CMD_CHIP_STATE_EVENT 20
63+#define CMD_SET_CTRL_MODE 21
64+#define CMD_RESET_CHIP 24
65+#define CMD_START_CHIP 26
66+#define CMD_START_CHIP_REPLY 27
67+#define CMD_STOP_CHIP 28
68+#define CMD_STOP_CHIP_REPLY 29
69+
70+#define CMD_USBCAN_CLOCK_OVERFLOW_EVENT 33
71+
72+#define CMD_GET_CARD_INFO 34
73+#define CMD_GET_CARD_INFO_REPLY 35
74+#define CMD_GET_SOFTWARE_INFO 38
75+#define CMD_GET_SOFTWARE_INFO_REPLY 39
76+#define CMD_FLUSH_QUEUE 48
77+#define CMD_TX_ACKNOWLEDGE 50
78+#define CMD_CAN_ERROR_EVENT 51
79+#define CMD_FLUSH_QUEUE_REPLY 68
80+
81+#define CMD_LEAF_LOG_MESSAGE 106
82+
83+/* error factors */
84+#define M16C_EF_ACKE BIT(0)
85+#define M16C_EF_CRCE BIT(1)
86+#define M16C_EF_FORME BIT(2)
87+#define M16C_EF_STFE BIT(3)
88+#define M16C_EF_BITE0 BIT(4)
89+#define M16C_EF_BITE1 BIT(5)
90+#define M16C_EF_RCVE BIT(6)
91+#define M16C_EF_TRE BIT(7)
92+
93+/* Only Leaf-based devices can report M16C error factors,
94+ * thus define our own error status flags for USBCANII
95+ */
96+#define USBCAN_ERROR_STATE_NONE 0
97+#define USBCAN_ERROR_STATE_TX_ERROR BIT(0)
98+#define USBCAN_ERROR_STATE_RX_ERROR BIT(1)
99+#define USBCAN_ERROR_STATE_BUSERROR BIT(2)
100+
101+/* bittiming parameters */
102+#define KVASER_USB_TSEG1_MIN 1
103+#define KVASER_USB_TSEG1_MAX 16
104+#define KVASER_USB_TSEG2_MIN 1
105+#define KVASER_USB_TSEG2_MAX 8
106+#define KVASER_USB_SJW_MAX 4
107+#define KVASER_USB_BRP_MIN 1
108+#define KVASER_USB_BRP_MAX 64
109+#define KVASER_USB_BRP_INC 1
110+
111+/* ctrl modes */
112+#define KVASER_CTRL_MODE_NORMAL 1
113+#define KVASER_CTRL_MODE_SILENT 2
114+#define KVASER_CTRL_MODE_SELFRECEPTION 3
115+#define KVASER_CTRL_MODE_OFF 4
116+
117+/* Extended CAN identifier flag */
118+#define KVASER_EXTENDED_FRAME BIT(31)
119+
120+struct kvaser_cmd_simple {
121+ u8 tid;
122+ u8 channel;
123+} __packed;
124+
125+struct kvaser_cmd_cardinfo {
126+ u8 tid;
127+ u8 nchannels;
128+ __le32 serial_number;
129+ __le32 padding0;
130+ __le32 clock_resolution;
131+ __le32 mfgdate;
132+ u8 ean[8];
133+ u8 hw_revision;
134+ union {
135+ struct {
136+ u8 usb_hs_mode;
137+ } __packed leaf1;
138+ struct {
139+ u8 padding;
140+ } __packed usbcan1;
141+ } __packed;
142+ __le16 padding1;
143+} __packed;
144+
145+struct leaf_cmd_softinfo {
146+ u8 tid;
147+ u8 padding0;
148+ __le32 sw_options;
149+ __le32 fw_version;
150+ __le16 max_outstanding_tx;
151+ __le16 padding1[9];
152+} __packed;
153+
154+struct usbcan_cmd_softinfo {
155+ u8 tid;
156+ u8 fw_name[5];
157+ __le16 max_outstanding_tx;
158+ u8 padding[6];
159+ __le32 fw_version;
160+ __le16 checksum;
161+ __le16 sw_options;
162+} __packed;
163+
164+struct kvaser_cmd_busparams {
165+ u8 tid;
166+ u8 channel;
167+ __le32 bitrate;
168+ u8 tseg1;
169+ u8 tseg2;
170+ u8 sjw;
171+ u8 no_samp;
172+} __packed;
173+
174+struct kvaser_cmd_tx_can {
175+ u8 channel;
176+ u8 tid;
177+ u8 data[14];
178+ union {
179+ struct {
180+ u8 padding;
181+ u8 flags;
182+ } __packed leaf;
183+ struct {
184+ u8 flags;
185+ u8 padding;
186+ } __packed usbcan;
187+ } __packed;
188+} __packed;
189+
190+struct kvaser_cmd_rx_can_header {
191+ u8 channel;
192+ u8 flag;
193+} __packed;
194+
195+struct leaf_cmd_rx_can {
196+ u8 channel;
197+ u8 flag;
198+
199+ __le16 time[3];
200+ u8 data[14];
201+} __packed;
202+
203+struct usbcan_cmd_rx_can {
204+ u8 channel;
205+ u8 flag;
206+
207+ u8 data[14];
208+ __le16 time;
209+} __packed;
210+
211+struct leaf_cmd_chip_state_event {
212+ u8 tid;
213+ u8 channel;
214+
215+ __le16 time[3];
216+ u8 tx_errors_count;
217+ u8 rx_errors_count;
218+
219+ u8 status;
220+ u8 padding[3];
221+} __packed;
222+
223+struct usbcan_cmd_chip_state_event {
224+ u8 tid;
225+ u8 channel;
226+
227+ u8 tx_errors_count;
228+ u8 rx_errors_count;
229+ __le16 time;
230+
231+ u8 status;
232+ u8 padding[3];
233+} __packed;
234+
235+struct kvaser_cmd_tx_acknowledge_header {
236+ u8 channel;
237+ u8 tid;
238+} __packed;
239+
240+struct leaf_cmd_error_event {
241+ u8 tid;
242+ u8 flags;
243+ __le16 time[3];
244+ u8 channel;
245+ u8 padding;
246+ u8 tx_errors_count;
247+ u8 rx_errors_count;
248+ u8 status;
249+ u8 error_factor;
250+} __packed;
251+
252+struct usbcan_cmd_error_event {
253+ u8 tid;
254+ u8 padding;
255+ u8 tx_errors_count_ch0;
256+ u8 rx_errors_count_ch0;
257+ u8 tx_errors_count_ch1;
258+ u8 rx_errors_count_ch1;
259+ u8 status_ch0;
260+ u8 status_ch1;
261+ __le16 time;
262+} __packed;
263+
264+struct kvaser_cmd_ctrl_mode {
265+ u8 tid;
266+ u8 channel;
267+ u8 ctrl_mode;
268+ u8 padding[3];
269+} __packed;
270+
271+struct kvaser_cmd_flush_queue {
272+ u8 tid;
273+ u8 channel;
274+ u8 flags;
275+ u8 padding[3];
276+} __packed;
277+
278+struct leaf_cmd_log_message {
279+ u8 channel;
280+ u8 flags;
281+ __le16 time[3];
282+ u8 dlc;
283+ u8 time_offset;
284+ __le32 id;
285+ u8 data[8];
286+} __packed;
287+
288+struct kvaser_cmd {
289+ u8 len;
290+ u8 id;
291+ union {
292+ struct kvaser_cmd_simple simple;
293+ struct kvaser_cmd_cardinfo cardinfo;
294+ struct kvaser_cmd_busparams busparams;
295+
296+ struct kvaser_cmd_rx_can_header rx_can_header;
297+ struct kvaser_cmd_tx_acknowledge_header tx_acknowledge_header;
298+
299+ union {
300+ struct leaf_cmd_softinfo softinfo;
301+ struct leaf_cmd_rx_can rx_can;
302+ struct leaf_cmd_chip_state_event chip_state_event;
303+ struct leaf_cmd_error_event error_event;
304+ struct leaf_cmd_log_message log_message;
305+ } __packed leaf;
306+
307+ union {
308+ struct usbcan_cmd_softinfo softinfo;
309+ struct usbcan_cmd_rx_can rx_can;
310+ struct usbcan_cmd_chip_state_event chip_state_event;
311+ struct usbcan_cmd_error_event error_event;
312+ } __packed usbcan;
313+
314+ struct kvaser_cmd_tx_can tx_can;
315+ struct kvaser_cmd_ctrl_mode ctrl_mode;
316+ struct kvaser_cmd_flush_queue flush_queue;
317+ } u;
318+} __packed;
319+
320+/* Summary of a kvaser error event, for a unified Leaf/Usbcan error
321+ * handling. Some discrepancies between the two families exist:
322+ *
323+ * - USBCAN firmware does not report M16C "error factors"
324+ * - USBCAN controllers has difficulties reporting if the raised error
325+ * event is for ch0 or ch1. They leave such arbitration to the OS
326+ * driver by letting it compare error counters with previous values
327+ * and decide the error event's channel. Thus for USBCAN, the channel
328+ * field is only advisory.
329+ */
330+struct kvaser_usb_err_summary {
331+ u8 channel, status, txerr, rxerr;
332+ union {
333+ struct {
334+ u8 error_factor;
335+ } leaf;
336+ struct {
337+ u8 other_ch_status;
338+ u8 error_state;
339+ } usbcan;
340+ };
341+};
342+
343+static void *
344+kvaser_usb_leaf_frame_to_cmd(const struct kvaser_usb_net_priv *priv,
345+ const struct sk_buff *skb, int *frame_len,
346+ int *cmd_len, u16 transid)
347+{
348+ struct kvaser_usb *dev = priv->dev;
349+ struct kvaser_cmd *cmd;
350+ u8 *cmd_tx_can_flags = NULL; /* GCC */
351+ struct can_frame *cf = (struct can_frame *)skb->data;
352+
353+ *frame_len = cf->can_dlc;
354+
355+ cmd = kmalloc(sizeof(*cmd), GFP_ATOMIC);
356+ if (cmd) {
357+ cmd->u.tx_can.tid = transid & 0xff;
358+ cmd->len = *cmd_len = CMD_HEADER_LEN +
359+ sizeof(struct kvaser_cmd_tx_can);
360+ cmd->u.tx_can.channel = priv->channel;
361+
362+ switch (dev->card_data.leaf.family) {
363+ case KVASER_LEAF:
364+ cmd_tx_can_flags = &cmd->u.tx_can.leaf.flags;
365+ break;
366+ case KVASER_USBCAN:
367+ cmd_tx_can_flags = &cmd->u.tx_can.usbcan.flags;
368+ break;
369+ }
370+
371+ *cmd_tx_can_flags = 0;
372+
373+ if (cf->can_id & CAN_EFF_FLAG) {
374+ cmd->id = CMD_TX_EXT_MESSAGE;
375+ cmd->u.tx_can.data[0] = (cf->can_id >> 24) & 0x1f;
376+ cmd->u.tx_can.data[1] = (cf->can_id >> 18) & 0x3f;
377+ cmd->u.tx_can.data[2] = (cf->can_id >> 14) & 0x0f;
378+ cmd->u.tx_can.data[3] = (cf->can_id >> 6) & 0xff;
379+ cmd->u.tx_can.data[4] = cf->can_id & 0x3f;
380+ } else {
381+ cmd->id = CMD_TX_STD_MESSAGE;
382+ cmd->u.tx_can.data[0] = (cf->can_id >> 6) & 0x1f;
383+ cmd->u.tx_can.data[1] = cf->can_id & 0x3f;
384+ }
385+
386+ cmd->u.tx_can.data[5] = cf->can_dlc;
387+ memcpy(&cmd->u.tx_can.data[6], cf->data, cf->can_dlc);
388+
389+ if (cf->can_id & CAN_RTR_FLAG)
390+ *cmd_tx_can_flags |= MSG_FLAG_REMOTE_FRAME;
391+ }
392+ return cmd;
393+}
394+
395+static int kvaser_usb_leaf_wait_cmd(const struct kvaser_usb *dev, u8 id,
396+ struct kvaser_cmd *cmd)
397+{
398+ struct kvaser_cmd *tmp;
399+ void *buf;
400+ int actual_len;
401+ int err;
402+ int pos;
403+ unsigned long to = jiffies + msecs_to_jiffies(KVASER_USB_TIMEOUT);
404+
405+ buf = kzalloc(KVASER_USB_RX_BUFFER_SIZE, GFP_KERNEL);
406+ if (!buf)
407+ return -ENOMEM;
408+
409+ do {
410+ err = kvaser_usb_recv_cmd(dev, buf, KVASER_USB_RX_BUFFER_SIZE,
411+ &actual_len);
412+ if (err < 0)
413+ goto end;
414+
415+ pos = 0;
416+ while (pos <= actual_len - CMD_HEADER_LEN) {
417+ tmp = buf + pos;
418+
419+ /* Handle commands crossing the USB endpoint max packet
420+ * size boundary. Check kvaser_usb_read_bulk_callback()
421+ * for further details.
422+ */
423+ if (tmp->len == 0) {
424+ pos = round_up(pos,
425+ le16_to_cpu
426+ (dev->bulk_in->wMaxPacketSize));
427+ continue;
428+ }
429+
430+ if (pos + tmp->len > actual_len) {
431+ dev_err_ratelimited(&dev->intf->dev,
432+ "Format error\n");
433+ break;
434+ }
435+
436+ if (tmp->id == id) {
437+ memcpy(cmd, tmp, tmp->len);
438+ goto end;
439+ }
440+
441+ pos += tmp->len;
442+ }
443+ } while (time_before(jiffies, to));
444+
445+ err = -EINVAL;
446+
447+end:
448+ kfree(buf);
449+
450+ return err;
451+}
452+
453+static int kvaser_usb_leaf_send_simple_cmd(const struct kvaser_usb *dev,
454+ u8 cmd_id, int channel)
455+{
456+ struct kvaser_cmd *cmd;
457+ int rc;
458+
459+ cmd = kmalloc(sizeof(*cmd), GFP_KERNEL);
460+ if (!cmd)
461+ return -ENOMEM;
462+
463+ cmd->id = cmd_id;
464+ cmd->len = CMD_HEADER_LEN + sizeof(struct kvaser_cmd_simple);
465+ cmd->u.simple.channel = channel;
466+ cmd->u.simple.tid = 0xff;
467+
468+ rc = kvaser_usb_send_cmd(dev, cmd, cmd->len);
469+
470+ kfree(cmd);
471+ return rc;
472+}
473+
474+static int kvaser_usb_leaf_get_software_info_inner(struct kvaser_usb *dev)
475+{
476+ struct kvaser_cmd cmd;
477+ int err;
478+
479+ err = kvaser_usb_leaf_send_simple_cmd(dev, CMD_GET_SOFTWARE_INFO, 0);
480+ if (err)
481+ return err;
482+
483+ err = kvaser_usb_leaf_wait_cmd(dev, CMD_GET_SOFTWARE_INFO_REPLY, &cmd);
484+ if (err)
485+ return err;
486+
487+ switch (dev->card_data.leaf.family) {
488+ case KVASER_LEAF:
489+ dev->fw_version = le32_to_cpu(cmd.u.leaf.softinfo.fw_version);
490+ dev->max_tx_urbs =
491+ le16_to_cpu(cmd.u.leaf.softinfo.max_outstanding_tx);
492+ break;
493+ case KVASER_USBCAN:
494+ dev->fw_version = le32_to_cpu(cmd.u.usbcan.softinfo.fw_version);
495+ dev->max_tx_urbs =
496+ le16_to_cpu(cmd.u.usbcan.softinfo.max_outstanding_tx);
497+ break;
498+ }
499+
500+ return 0;
501+}
502+
503+static int kvaser_usb_leaf_get_software_info(struct kvaser_usb *dev)
504+{
505+ int err;
506+ int retry = 3;
507+
508+ /* On some x86 laptops, plugging a Kvaser device again after
509+ * an unplug makes the firmware always ignore the very first
510+ * command. For such a case, provide some room for retries
511+ * instead of completely exiting the driver.
512+ */
513+ do {
514+ err = kvaser_usb_leaf_get_software_info_inner(dev);
515+ } while (--retry && err == -ETIMEDOUT);
516+
517+ return err;
518+}
519+
520+static int kvaser_usb_leaf_get_card_info(struct kvaser_usb *dev)
521+{
522+ struct kvaser_cmd cmd;
523+ int err;
524+
525+ err = kvaser_usb_leaf_send_simple_cmd(dev, CMD_GET_CARD_INFO, 0);
526+ if (err)
527+ return err;
528+
529+ err = kvaser_usb_leaf_wait_cmd(dev, CMD_GET_CARD_INFO_REPLY, &cmd);
530+ if (err)
531+ return err;
532+
533+ dev->nchannels = cmd.u.cardinfo.nchannels;
534+ if (dev->nchannels > KVASER_USB_MAX_NET_DEVICES ||
535+ (dev->card_data.leaf.family == KVASER_USBCAN &&
536+ dev->nchannels > MAX_USBCAN_NET_DEVICES))
537+ return -EINVAL;
538+
539+ return 0;
540+}
541+
542+static void kvaser_usb_leaf_tx_acknowledge(const struct kvaser_usb *dev,
543+ const struct kvaser_cmd *cmd)
544+{
545+ struct net_device_stats *stats;
546+ struct kvaser_usb_tx_urb_context *context;
547+ struct kvaser_usb_net_priv *priv;
548+ unsigned long flags;
549+ u8 channel, tid;
550+
551+ channel = cmd->u.tx_acknowledge_header.channel;
552+ tid = cmd->u.tx_acknowledge_header.tid;
553+
554+ if (channel >= dev->nchannels) {
555+ dev_err(&dev->intf->dev,
556+ "Invalid channel number (%d)\n", channel);
557+ return;
558+ }
559+
560+ priv = dev->nets[channel];
561+
562+ if (!netif_device_present(priv->netdev))
563+ return;
564+
565+ stats = &priv->netdev->stats;
566+
567+ context = &priv->tx_contexts[tid % dev->max_tx_urbs];
568+
569+ /* Sometimes the state change doesn't come after a bus-off event */
570+ if (priv->can.restart_ms && priv->can.state >= CAN_STATE_BUS_OFF) {
571+ struct sk_buff *skb;
572+ struct can_frame *cf;
573+
574+ skb = alloc_can_err_skb(priv->netdev, &cf);
575+ if (skb) {
576+ cf->can_id |= CAN_ERR_RESTARTED;
577+
578+ stats->rx_packets++;
579+ stats->rx_bytes += cf->can_dlc;
580+ netif_rx(skb);
581+ } else {
582+ netdev_err(priv->netdev,
583+ "No memory left for err_skb\n");
584+ }
585+
586+ priv->can.can_stats.restarts++;
587+ netif_carrier_on(priv->netdev);
588+
589+ priv->can.state = CAN_STATE_ERROR_ACTIVE;
590+ }
591+
592+ stats->tx_packets++;
593+ stats->tx_bytes += context->dlc;
594+
595+ spin_lock_irqsave(&priv->tx_contexts_lock, flags);
596+
597+ can_get_echo_skb(priv->netdev, context->echo_index);
598+ context->echo_index = dev->max_tx_urbs;
599+ --priv->active_tx_contexts;
600+ netif_wake_queue(priv->netdev);
601+
602+ spin_unlock_irqrestore(&priv->tx_contexts_lock, flags);
603+}
604+
605+static int kvaser_usb_leaf_simple_cmd_async(struct kvaser_usb_net_priv *priv,
606+ u8 cmd_id)
607+{
608+ struct kvaser_cmd *cmd;
609+ int err;
610+
611+ cmd = kmalloc(sizeof(*cmd), GFP_ATOMIC);
612+ if (!cmd)
613+ return -ENOMEM;
614+
615+ cmd->len = CMD_HEADER_LEN + sizeof(struct kvaser_cmd_simple);
616+ cmd->id = cmd_id;
617+ cmd->u.simple.channel = priv->channel;
618+
619+ err = kvaser_usb_send_cmd_async(priv, cmd, cmd->len);
620+ if (err)
621+ kfree(cmd);
622+
623+ return err;
624+}
625+
626+static void
627+kvaser_usb_leaf_rx_error_update_can_state(struct kvaser_usb_net_priv *priv,
628+ const struct kvaser_usb_err_summary *es,
629+ struct can_frame *cf)
630+{
631+ struct kvaser_usb *dev = priv->dev;
632+ struct net_device_stats *stats = &priv->netdev->stats;
633+ enum can_state cur_state, new_state, tx_state, rx_state;
634+
635+ netdev_dbg(priv->netdev, "Error status: 0x%02x\n", es->status);
636+
637+ new_state = priv->can.state;
638+ cur_state = priv->can.state;
639+
640+ if (es->status & (M16C_STATE_BUS_OFF | M16C_STATE_BUS_RESET)) {
641+ new_state = CAN_STATE_BUS_OFF;
642+ } else if (es->status & M16C_STATE_BUS_PASSIVE) {
643+ new_state = CAN_STATE_ERROR_PASSIVE;
644+ } else if (es->status & M16C_STATE_BUS_ERROR) {
645+ /* Guard against spurious error events after a busoff */
646+ if (cur_state < CAN_STATE_BUS_OFF) {
647+ if (es->txerr >= 128 || es->rxerr >= 128)
648+ new_state = CAN_STATE_ERROR_PASSIVE;
649+ else if (es->txerr >= 96 || es->rxerr >= 96)
650+ new_state = CAN_STATE_ERROR_WARNING;
651+ else if (cur_state > CAN_STATE_ERROR_ACTIVE)
652+ new_state = CAN_STATE_ERROR_ACTIVE;
653+ }
654+ }
655+
656+ if (!es->status)
657+ new_state = CAN_STATE_ERROR_ACTIVE;
658+
659+ if (new_state != cur_state) {
660+ tx_state = (es->txerr >= es->rxerr) ? new_state : 0;
661+ rx_state = (es->txerr <= es->rxerr) ? new_state : 0;
662+
663+ can_change_state(priv->netdev, cf, tx_state, rx_state);
664+ }
665+
666+ if (priv->can.restart_ms &&
667+ cur_state >= CAN_STATE_BUS_OFF &&
668+ new_state < CAN_STATE_BUS_OFF)
669+ priv->can.can_stats.restarts++;
670+
671+ switch (dev->card_data.leaf.family) {
672+ case KVASER_LEAF:
673+ if (es->leaf.error_factor) {
674+ priv->can.can_stats.bus_error++;
675+ stats->rx_errors++;
676+ }
677+ break;
678+ case KVASER_USBCAN:
679+ if (es->usbcan.error_state & USBCAN_ERROR_STATE_TX_ERROR)
680+ stats->tx_errors++;
681+ if (es->usbcan.error_state & USBCAN_ERROR_STATE_RX_ERROR)
682+ stats->rx_errors++;
683+ if (es->usbcan.error_state & USBCAN_ERROR_STATE_BUSERROR)
684+ priv->can.can_stats.bus_error++;
685+ break;
686+ }
687+
688+ priv->bec.txerr = es->txerr;
689+ priv->bec.rxerr = es->rxerr;
690+}
691+
692+static void kvaser_usb_leaf_rx_error(const struct kvaser_usb *dev,
693+ const struct kvaser_usb_err_summary *es)
694+{
695+ struct can_frame *cf;
696+ struct can_frame tmp_cf = { .can_id = CAN_ERR_FLAG,
697+ .can_dlc = CAN_ERR_DLC };
698+ struct sk_buff *skb;
699+ struct net_device_stats *stats;
700+ struct kvaser_usb_net_priv *priv;
701+ enum can_state old_state, new_state;
702+
703+ if (es->channel >= dev->nchannels) {
704+ dev_err(&dev->intf->dev,
705+ "Invalid channel number (%d)\n", es->channel);
706+ return;
707+ }
708+
709+ priv = dev->nets[es->channel];
710+ stats = &priv->netdev->stats;
711+
712+ /* Update all of the CAN interface's state and error counters before
713+ * trying any memory allocation that can actually fail with -ENOMEM.
714+ *
715+ * We send a temporary stack-allocated error CAN frame to
716+ * can_change_state() for the very same reason.
717+ *
718+ * TODO: Split can_change_state() responsibility between updating the
719+ * CAN interface's state and counters, and the setting up of CAN error
720+ * frame ID and data to userspace. Remove stack allocation afterwards.
721+ */
722+ old_state = priv->can.state;
723+ kvaser_usb_leaf_rx_error_update_can_state(priv, es, &tmp_cf);
724+ new_state = priv->can.state;
725+
726+ skb = alloc_can_err_skb(priv->netdev, &cf);
727+ if (!skb) {
728+ stats->rx_dropped++;
729+ return;
730+ }
731+ memcpy(cf, &tmp_cf, sizeof(*cf));
732+
733+ if (new_state != old_state) {
734+ if (es->status &
735+ (M16C_STATE_BUS_OFF | M16C_STATE_BUS_RESET)) {
736+ if (!priv->can.restart_ms)
737+ kvaser_usb_leaf_simple_cmd_async(priv,
738+ CMD_STOP_CHIP);
739+ netif_carrier_off(priv->netdev);
740+ }
741+
742+ if (priv->can.restart_ms &&
743+ old_state >= CAN_STATE_BUS_OFF &&
744+ new_state < CAN_STATE_BUS_OFF) {
745+ cf->can_id |= CAN_ERR_RESTARTED;
746+ netif_carrier_on(priv->netdev);
747+ }
748+ }
749+
750+ switch (dev->card_data.leaf.family) {
751+ case KVASER_LEAF:
752+ if (es->leaf.error_factor) {
753+ cf->can_id |= CAN_ERR_BUSERROR | CAN_ERR_PROT;
754+
755+ if (es->leaf.error_factor & M16C_EF_ACKE)
756+ cf->data[3] = CAN_ERR_PROT_LOC_ACK;
757+ if (es->leaf.error_factor & M16C_EF_CRCE)
758+ cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
759+ if (es->leaf.error_factor & M16C_EF_FORME)
760+ cf->data[2] |= CAN_ERR_PROT_FORM;
761+ if (es->leaf.error_factor & M16C_EF_STFE)
762+ cf->data[2] |= CAN_ERR_PROT_STUFF;
763+ if (es->leaf.error_factor & M16C_EF_BITE0)
764+ cf->data[2] |= CAN_ERR_PROT_BIT0;
765+ if (es->leaf.error_factor & M16C_EF_BITE1)
766+ cf->data[2] |= CAN_ERR_PROT_BIT1;
767+ if (es->leaf.error_factor & M16C_EF_TRE)
768+ cf->data[2] |= CAN_ERR_PROT_TX;
769+ }
770+ break;
771+ case KVASER_USBCAN:
772+ if (es->usbcan.error_state & USBCAN_ERROR_STATE_BUSERROR)
773+ cf->can_id |= CAN_ERR_BUSERROR;
774+ break;
775+ }
776+
777+ cf->data[6] = es->txerr;
778+ cf->data[7] = es->rxerr;
779+
780+ stats->rx_packets++;
781+ stats->rx_bytes += cf->can_dlc;
782+ netif_rx(skb);
783+}
784+
785+/* For USBCAN, report error to userspace if the channels's errors counter
786+ * has changed, or we're the only channel seeing a bus error state.
787+ */
788+static void
789+kvaser_usb_leaf_usbcan_conditionally_rx_error(const struct kvaser_usb *dev,
790+ struct kvaser_usb_err_summary *es)
791+{
792+ struct kvaser_usb_net_priv *priv;
793+ unsigned int channel;
794+ bool report_error;
795+
796+ channel = es->channel;
797+ if (channel >= dev->nchannels) {
798+ dev_err(&dev->intf->dev,
799+ "Invalid channel number (%d)\n", channel);
800+ return;
801+ }
802+
803+ priv = dev->nets[channel];
804+ report_error = false;
805+
806+ if (es->txerr != priv->bec.txerr) {
807+ es->usbcan.error_state |= USBCAN_ERROR_STATE_TX_ERROR;
808+ report_error = true;
809+ }
810+ if (es->rxerr != priv->bec.rxerr) {
811+ es->usbcan.error_state |= USBCAN_ERROR_STATE_RX_ERROR;
812+ report_error = true;
813+ }
814+ if ((es->status & M16C_STATE_BUS_ERROR) &&
815+ !(es->usbcan.other_ch_status & M16C_STATE_BUS_ERROR)) {
816+ es->usbcan.error_state |= USBCAN_ERROR_STATE_BUSERROR;
817+ report_error = true;
818+ }
819+
820+ if (report_error)
821+ kvaser_usb_leaf_rx_error(dev, es);
822+}
823+
824+static void kvaser_usb_leaf_usbcan_rx_error(const struct kvaser_usb *dev,
825+ const struct kvaser_cmd *cmd)
826+{
827+ struct kvaser_usb_err_summary es = { };
828+
829+ switch (cmd->id) {
830+ /* Sometimes errors are sent as unsolicited chip state events */
831+ case CMD_CHIP_STATE_EVENT:
832+ es.channel = cmd->u.usbcan.chip_state_event.channel;
833+ es.status = cmd->u.usbcan.chip_state_event.status;
834+ es.txerr = cmd->u.usbcan.chip_state_event.tx_errors_count;
835+ es.rxerr = cmd->u.usbcan.chip_state_event.rx_errors_count;
836+ kvaser_usb_leaf_usbcan_conditionally_rx_error(dev, &es);
837+ break;
838+
839+ case CMD_CAN_ERROR_EVENT:
840+ es.channel = 0;
841+ es.status = cmd->u.usbcan.error_event.status_ch0;
842+ es.txerr = cmd->u.usbcan.error_event.tx_errors_count_ch0;
843+ es.rxerr = cmd->u.usbcan.error_event.rx_errors_count_ch0;
844+ es.usbcan.other_ch_status =
845+ cmd->u.usbcan.error_event.status_ch1;
846+ kvaser_usb_leaf_usbcan_conditionally_rx_error(dev, &es);
847+
848+ /* The USBCAN firmware supports up to 2 channels.
849+ * Now that ch0 was checked, check if ch1 has any errors.
850+ */
851+ if (dev->nchannels == MAX_USBCAN_NET_DEVICES) {
852+ es.channel = 1;
853+ es.status = cmd->u.usbcan.error_event.status_ch1;
854+ es.txerr =
855+ cmd->u.usbcan.error_event.tx_errors_count_ch1;
856+ es.rxerr =
857+ cmd->u.usbcan.error_event.rx_errors_count_ch1;
858+ es.usbcan.other_ch_status =
859+ cmd->u.usbcan.error_event.status_ch0;
860+ kvaser_usb_leaf_usbcan_conditionally_rx_error(dev, &es);
861+ }
862+ break;
863+
864+ default:
865+ dev_err(&dev->intf->dev, "Invalid cmd id (%d)\n", cmd->id);
866+ }
867+}
868+
869+static void kvaser_usb_leaf_leaf_rx_error(const struct kvaser_usb *dev,
870+ const struct kvaser_cmd *cmd)
871+{
872+ struct kvaser_usb_err_summary es = { };
873+
874+ switch (cmd->id) {
875+ case CMD_CAN_ERROR_EVENT:
876+ es.channel = cmd->u.leaf.error_event.channel;
877+ es.status = cmd->u.leaf.error_event.status;
878+ es.txerr = cmd->u.leaf.error_event.tx_errors_count;
879+ es.rxerr = cmd->u.leaf.error_event.rx_errors_count;
880+ es.leaf.error_factor = cmd->u.leaf.error_event.error_factor;
881+ break;
882+ case CMD_LEAF_LOG_MESSAGE:
883+ es.channel = cmd->u.leaf.log_message.channel;
884+ es.status = cmd->u.leaf.log_message.data[0];
885+ es.txerr = cmd->u.leaf.log_message.data[2];
886+ es.rxerr = cmd->u.leaf.log_message.data[3];
887+ es.leaf.error_factor = cmd->u.leaf.log_message.data[1];
888+ break;
889+ case CMD_CHIP_STATE_EVENT:
890+ es.channel = cmd->u.leaf.chip_state_event.channel;
891+ es.status = cmd->u.leaf.chip_state_event.status;
892+ es.txerr = cmd->u.leaf.chip_state_event.tx_errors_count;
893+ es.rxerr = cmd->u.leaf.chip_state_event.rx_errors_count;
894+ es.leaf.error_factor = 0;
895+ break;
896+ default:
897+ dev_err(&dev->intf->dev, "Invalid cmd id (%d)\n", cmd->id);
898+ return;
899+ }
900+
901+ kvaser_usb_leaf_rx_error(dev, &es);
902+}
903+
904+static void kvaser_usb_leaf_rx_can_err(const struct kvaser_usb_net_priv *priv,
905+ const struct kvaser_cmd *cmd)
906+{
907+ if (cmd->u.rx_can_header.flag & (MSG_FLAG_ERROR_FRAME |
908+ MSG_FLAG_NERR)) {
909+ struct net_device_stats *stats = &priv->netdev->stats;
910+
911+ netdev_err(priv->netdev, "Unknown error (flags: 0x%02x)\n",
912+ cmd->u.rx_can_header.flag);
913+
914+ stats->rx_errors++;
915+ return;
916+ }
917+
918+ if (cmd->u.rx_can_header.flag & MSG_FLAG_OVERRUN)
919+ kvaser_usb_can_rx_over_error(priv->netdev);
920+}
921+
922+static void kvaser_usb_leaf_rx_can_msg(const struct kvaser_usb *dev,
923+ const struct kvaser_cmd *cmd)
924+{
925+ struct kvaser_usb_net_priv *priv;
926+ struct can_frame *cf;
927+ struct sk_buff *skb;
928+ struct net_device_stats *stats;
929+ u8 channel = cmd->u.rx_can_header.channel;
930+ const u8 *rx_data = NULL; /* GCC */
931+
932+ if (channel >= dev->nchannels) {
933+ dev_err(&dev->intf->dev,
934+ "Invalid channel number (%d)\n", channel);
935+ return;
936+ }
937+
938+ priv = dev->nets[channel];
939+ stats = &priv->netdev->stats;
940+
941+ if ((cmd->u.rx_can_header.flag & MSG_FLAG_ERROR_FRAME) &&
942+ (dev->card_data.leaf.family == KVASER_LEAF &&
943+ cmd->id == CMD_LEAF_LOG_MESSAGE)) {
944+ kvaser_usb_leaf_leaf_rx_error(dev, cmd);
945+ return;
946+ } else if (cmd->u.rx_can_header.flag & (MSG_FLAG_ERROR_FRAME |
947+ MSG_FLAG_NERR |
948+ MSG_FLAG_OVERRUN)) {
949+ kvaser_usb_leaf_rx_can_err(priv, cmd);
950+ return;
951+ } else if (cmd->u.rx_can_header.flag & ~MSG_FLAG_REMOTE_FRAME) {
952+ netdev_warn(priv->netdev,
953+ "Unhandled frame (flags: 0x%02x)\n",
954+ cmd->u.rx_can_header.flag);
955+ return;
956+ }
957+
958+ switch (dev->card_data.leaf.family) {
959+ case KVASER_LEAF:
960+ rx_data = cmd->u.leaf.rx_can.data;
961+ break;
962+ case KVASER_USBCAN:
963+ rx_data = cmd->u.usbcan.rx_can.data;
964+ break;
965+ }
966+
967+ skb = alloc_can_skb(priv->netdev, &cf);
968+ if (!skb) {
969+ stats->rx_dropped++;
970+ return;
971+ }
972+
973+ if (dev->card_data.leaf.family == KVASER_LEAF && cmd->id ==
974+ CMD_LEAF_LOG_MESSAGE) {
975+ cf->can_id = le32_to_cpu(cmd->u.leaf.log_message.id);
976+ if (cf->can_id & KVASER_EXTENDED_FRAME)
977+ cf->can_id &= CAN_EFF_MASK | CAN_EFF_FLAG;
978+ else
979+ cf->can_id &= CAN_SFF_MASK;
980+
981+ cf->can_dlc = get_can_dlc(cmd->u.leaf.log_message.dlc);
982+
983+ if (cmd->u.leaf.log_message.flags & MSG_FLAG_REMOTE_FRAME)
984+ cf->can_id |= CAN_RTR_FLAG;
985+ else
986+ memcpy(cf->data, &cmd->u.leaf.log_message.data,
987+ cf->can_dlc);
988+ } else {
989+ cf->can_id = ((rx_data[0] & 0x1f) << 6) | (rx_data[1] & 0x3f);
990+
991+ if (cmd->id == CMD_RX_EXT_MESSAGE) {
992+ cf->can_id <<= 18;
993+ cf->can_id |= ((rx_data[2] & 0x0f) << 14) |
994+ ((rx_data[3] & 0xff) << 6) |
995+ (rx_data[4] & 0x3f);
996+ cf->can_id |= CAN_EFF_FLAG;
997+ }
998+
999+ cf->can_dlc = get_can_dlc(rx_data[5]);
1000+
1001+ if (cmd->u.rx_can_header.flag & MSG_FLAG_REMOTE_FRAME)
1002+ cf->can_id |= CAN_RTR_FLAG;
1003+ else
1004+ memcpy(cf->data, &rx_data[6], cf->can_dlc);
1005+ }
1006+
1007+ stats->rx_packets++;
1008+ stats->rx_bytes += cf->can_dlc;
1009+ netif_rx(skb);
1010+}
1011+
1012+static void kvaser_usb_leaf_start_chip_reply(const struct kvaser_usb *dev,
1013+ const struct kvaser_cmd *cmd)
1014+{
1015+ struct kvaser_usb_net_priv *priv;
1016+ u8 channel = cmd->u.simple.channel;
1017+
1018+ if (channel >= dev->nchannels) {
1019+ dev_err(&dev->intf->dev,
1020+ "Invalid channel number (%d)\n", channel);
1021+ return;
1022+ }
1023+
1024+ priv = dev->nets[channel];
1025+
1026+ if (completion_done(&priv->start_comp) &&
1027+ netif_queue_stopped(priv->netdev)) {
1028+ netif_wake_queue(priv->netdev);
1029+ } else {
1030+ netif_start_queue(priv->netdev);
1031+ complete(&priv->start_comp);
1032+ }
1033+}
1034+
1035+static void kvaser_usb_leaf_stop_chip_reply(const struct kvaser_usb *dev,
1036+ const struct kvaser_cmd *cmd)
1037+{
1038+ struct kvaser_usb_net_priv *priv;
1039+ u8 channel = cmd->u.simple.channel;
1040+
1041+ if (channel >= dev->nchannels) {
1042+ dev_err(&dev->intf->dev,
1043+ "Invalid channel number (%d)\n", channel);
1044+ return;
1045+ }
1046+
1047+ priv = dev->nets[channel];
1048+
1049+ complete(&priv->stop_comp);
1050+}
1051+
1052+static void kvaser_usb_leaf_handle_command(const struct kvaser_usb *dev,
1053+ const struct kvaser_cmd *cmd)
1054+{
1055+ switch (cmd->id) {
1056+ case CMD_START_CHIP_REPLY:
1057+ kvaser_usb_leaf_start_chip_reply(dev, cmd);
1058+ break;
1059+
1060+ case CMD_STOP_CHIP_REPLY:
1061+ kvaser_usb_leaf_stop_chip_reply(dev, cmd);
1062+ break;
1063+
1064+ case CMD_RX_STD_MESSAGE:
1065+ case CMD_RX_EXT_MESSAGE:
1066+ kvaser_usb_leaf_rx_can_msg(dev, cmd);
1067+ break;
1068+
1069+ case CMD_LEAF_LOG_MESSAGE:
1070+ if (dev->card_data.leaf.family != KVASER_LEAF)
1071+ goto warn;
1072+ kvaser_usb_leaf_rx_can_msg(dev, cmd);
1073+ break;
1074+
1075+ case CMD_CHIP_STATE_EVENT:
1076+ case CMD_CAN_ERROR_EVENT:
1077+ if (dev->card_data.leaf.family == KVASER_LEAF)
1078+ kvaser_usb_leaf_leaf_rx_error(dev, cmd);
1079+ else
1080+ kvaser_usb_leaf_usbcan_rx_error(dev, cmd);
1081+ break;
1082+
1083+ case CMD_TX_ACKNOWLEDGE:
1084+ kvaser_usb_leaf_tx_acknowledge(dev, cmd);
1085+ break;
1086+
1087+ /* Ignored commands */
1088+ case CMD_USBCAN_CLOCK_OVERFLOW_EVENT:
1089+ if (dev->card_data.leaf.family != KVASER_USBCAN)
1090+ goto warn;
1091+ break;
1092+
1093+ case CMD_FLUSH_QUEUE_REPLY:
1094+ if (dev->card_data.leaf.family != KVASER_LEAF)
1095+ goto warn;
1096+ break;
1097+
1098+ default:
1099+warn: dev_warn(&dev->intf->dev, "Unhandled command (%d)\n", cmd->id);
1100+ break;
1101+ }
1102+}
1103+
1104+static void kvaser_usb_leaf_read_bulk_callback(struct kvaser_usb *dev,
1105+ void *buf, int len)
1106+{
1107+ struct kvaser_cmd *cmd;
1108+ int pos = 0;
1109+
1110+ while (pos <= len - CMD_HEADER_LEN) {
1111+ cmd = buf + pos;
1112+
1113+ /* The Kvaser firmware can only read and write commands that
1114+ * does not cross the USB's endpoint wMaxPacketSize boundary.
1115+ * If a follow-up command crosses such boundary, firmware puts
1116+ * a placeholder zero-length command in its place then aligns
1117+ * the real command to the next max packet size.
1118+ *
1119+ * Handle such cases or we're going to miss a significant
1120+ * number of events in case of a heavy rx load on the bus.
1121+ */
1122+ if (cmd->len == 0) {
1123+ pos = round_up(pos, le16_to_cpu
1124+ (dev->bulk_in->wMaxPacketSize));
1125+ continue;
1126+ }
1127+
1128+ if (pos + cmd->len > len) {
1129+ dev_err_ratelimited(&dev->intf->dev, "Format error\n");
1130+ break;
1131+ }
1132+
1133+ kvaser_usb_leaf_handle_command(dev, cmd);
1134+ pos += cmd->len;
1135+ }
1136+}
1137+
1138+static int kvaser_usb_leaf_set_opt_mode(const struct kvaser_usb_net_priv *priv)
1139+{
1140+ struct kvaser_cmd *cmd;
1141+ int rc;
1142+
1143+ cmd = kmalloc(sizeof(*cmd), GFP_KERNEL);
1144+ if (!cmd)
1145+ return -ENOMEM;
1146+
1147+ cmd->id = CMD_SET_CTRL_MODE;
1148+ cmd->len = CMD_HEADER_LEN + sizeof(struct kvaser_cmd_ctrl_mode);
1149+ cmd->u.ctrl_mode.tid = 0xff;
1150+ cmd->u.ctrl_mode.channel = priv->channel;
1151+
1152+ if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
1153+ cmd->u.ctrl_mode.ctrl_mode = KVASER_CTRL_MODE_SILENT;
1154+ else
1155+ cmd->u.ctrl_mode.ctrl_mode = KVASER_CTRL_MODE_NORMAL;
1156+
1157+ rc = kvaser_usb_send_cmd(priv->dev, cmd, cmd->len);
1158+
1159+ kfree(cmd);
1160+ return rc;
1161+}
1162+
1163+static int kvaser_usb_leaf_start_chip(struct kvaser_usb_net_priv *priv)
1164+{
1165+ int err;
1166+
1167+ init_completion(&priv->start_comp);
1168+
1169+ err = kvaser_usb_leaf_send_simple_cmd(priv->dev, CMD_START_CHIP,
1170+ priv->channel);
1171+ if (err)
1172+ return err;
1173+
1174+ if (!wait_for_completion_timeout(&priv->start_comp,
1175+ msecs_to_jiffies(KVASER_USB_TIMEOUT)))
1176+ return -ETIMEDOUT;
1177+
1178+ return 0;
1179+}
1180+
1181+static int kvaser_usb_leaf_stop_chip(struct kvaser_usb_net_priv *priv)
1182+{
1183+ int err;
1184+
1185+ init_completion(&priv->stop_comp);
1186+
1187+ err = kvaser_usb_leaf_send_simple_cmd(priv->dev, CMD_STOP_CHIP,
1188+ priv->channel);
1189+ if (err)
1190+ return err;
1191+
1192+ if (!wait_for_completion_timeout(&priv->stop_comp,
1193+ msecs_to_jiffies(KVASER_USB_TIMEOUT)))
1194+ return -ETIMEDOUT;
1195+
1196+ return 0;
1197+}
1198+
1199+static int kvaser_usb_leaf_reset_chip(struct kvaser_usb *dev, int channel)
1200+{
1201+ return kvaser_usb_leaf_send_simple_cmd(dev, CMD_RESET_CHIP, channel);
1202+}
1203+
1204+static int kvaser_usb_leaf_flush_queue(struct kvaser_usb_net_priv *priv)
1205+{
1206+ struct kvaser_cmd *cmd;
1207+ int rc;
1208+
1209+ cmd = kmalloc(sizeof(*cmd), GFP_KERNEL);
1210+ if (!cmd)
1211+ return -ENOMEM;
1212+
1213+ cmd->id = CMD_FLUSH_QUEUE;
1214+ cmd->len = CMD_HEADER_LEN + sizeof(struct kvaser_cmd_flush_queue);
1215+ cmd->u.flush_queue.channel = priv->channel;
1216+ cmd->u.flush_queue.flags = 0x00;
1217+
1218+ rc = kvaser_usb_send_cmd(priv->dev, cmd, cmd->len);
1219+
1220+ kfree(cmd);
1221+ return rc;
1222+}
1223+
1224+static int kvaser_usb_leaf_init_card(struct kvaser_usb *dev)
1225+{
1226+ struct kvaser_usb_dev_card_data *card_data = &dev->card_data;
1227+
1228+ dev->cfg = &kvaser_usb_leaf_dev_cfg;
1229+ card_data->ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
1230+
1231+ return 0;
1232+}
1233+
1234+static const struct can_bittiming_const kvaser_usb_leaf_bittiming_const = {
1235+ .name = "kvaser_usb",
1236+ .tseg1_min = KVASER_USB_TSEG1_MIN,
1237+ .tseg1_max = KVASER_USB_TSEG1_MAX,
1238+ .tseg2_min = KVASER_USB_TSEG2_MIN,
1239+ .tseg2_max = KVASER_USB_TSEG2_MAX,
1240+ .sjw_max = KVASER_USB_SJW_MAX,
1241+ .brp_min = KVASER_USB_BRP_MIN,
1242+ .brp_max = KVASER_USB_BRP_MAX,
1243+ .brp_inc = KVASER_USB_BRP_INC,
1244+};
1245+
1246+static int kvaser_usb_leaf_set_bittiming(struct net_device *netdev)
1247+{
1248+ struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1249+ struct can_bittiming *bt = &priv->can.bittiming;
1250+ struct kvaser_usb *dev = priv->dev;
1251+ struct kvaser_cmd *cmd;
1252+ int rc;
1253+
1254+ cmd = kmalloc(sizeof(*cmd), GFP_KERNEL);
1255+ if (!cmd)
1256+ return -ENOMEM;
1257+
1258+ cmd->id = CMD_SET_BUS_PARAMS;
1259+ cmd->len = CMD_HEADER_LEN + sizeof(struct kvaser_cmd_busparams);
1260+ cmd->u.busparams.channel = priv->channel;
1261+ cmd->u.busparams.tid = 0xff;
1262+ cmd->u.busparams.bitrate = cpu_to_le32(bt->bitrate);
1263+ cmd->u.busparams.sjw = bt->sjw;
1264+ cmd->u.busparams.tseg1 = bt->prop_seg + bt->phase_seg1;
1265+ cmd->u.busparams.tseg2 = bt->phase_seg2;
1266+
1267+ if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
1268+ cmd->u.busparams.no_samp = 3;
1269+ else
1270+ cmd->u.busparams.no_samp = 1;
1271+
1272+ rc = kvaser_usb_send_cmd(dev, cmd, cmd->len);
1273+
1274+ kfree(cmd);
1275+ return rc;
1276+}
1277+
1278+static int kvaser_usb_leaf_set_mode(struct net_device *netdev,
1279+ enum can_mode mode)
1280+{
1281+ struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1282+ int err;
1283+
1284+ switch (mode) {
1285+ case CAN_MODE_START:
1286+ err = kvaser_usb_leaf_simple_cmd_async(priv, CMD_START_CHIP);
1287+ if (err)
1288+ return err;
1289+ break;
1290+ default:
1291+ return -EOPNOTSUPP;
1292+ }
1293+
1294+ return 0;
1295+}
1296+
1297+static int kvaser_usb_leaf_get_berr_counter(const struct net_device *netdev,
1298+ struct can_berr_counter *bec)
1299+{
1300+ struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1301+
1302+ *bec = priv->bec;
1303+
1304+ return 0;
1305+}
1306+
1307+static int kvaser_usb_leaf_setup_endpoints(struct kvaser_usb *dev)
1308+{
1309+ const struct usb_host_interface *iface_desc;
1310+ struct usb_endpoint_descriptor *endpoint;
1311+ int i;
1312+
1313+ iface_desc = &dev->intf->altsetting[0];
1314+
1315+ for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1316+ endpoint = &iface_desc->endpoint[i].desc;
1317+
1318+ if (!dev->bulk_in && usb_endpoint_is_bulk_in(endpoint))
1319+ dev->bulk_in = endpoint;
1320+
1321+ if (!dev->bulk_out && usb_endpoint_is_bulk_out(endpoint))
1322+ dev->bulk_out = endpoint;
1323+
1324+ /* use first bulk endpoint for in and out */
1325+ if (dev->bulk_in && dev->bulk_out)
1326+ return 0;
1327+ }
1328+
1329+ return -ENODEV;
1330+}
1331+
1332+const struct kvaser_usb_dev_ops kvaser_usb_leaf_dev_ops = {
1333+ .dev_set_mode = kvaser_usb_leaf_set_mode,
1334+ .dev_set_bittiming = kvaser_usb_leaf_set_bittiming,
1335+ .dev_set_data_bittiming = NULL,
1336+ .dev_get_berr_counter = kvaser_usb_leaf_get_berr_counter,
1337+ .dev_setup_endpoints = kvaser_usb_leaf_setup_endpoints,
1338+ .dev_init_card = kvaser_usb_leaf_init_card,
1339+ .dev_get_software_info = kvaser_usb_leaf_get_software_info,
1340+ .dev_get_software_details = NULL,
1341+ .dev_get_card_info = kvaser_usb_leaf_get_card_info,
1342+ .dev_get_capabilities = NULL,
1343+ .dev_set_opt_mode = kvaser_usb_leaf_set_opt_mode,
1344+ .dev_start_chip = kvaser_usb_leaf_start_chip,
1345+ .dev_stop_chip = kvaser_usb_leaf_stop_chip,
1346+ .dev_reset_chip = kvaser_usb_leaf_reset_chip,
1347+ .dev_flush_queue = kvaser_usb_leaf_flush_queue,
1348+ .dev_read_bulk_callback = kvaser_usb_leaf_read_bulk_callback,
1349+ .dev_frame_to_cmd = kvaser_usb_leaf_frame_to_cmd,
1350+};
1351+
1352+static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_dev_cfg = {
1353+ .clock = {
1354+ .freq = CAN_USB_CLOCK,
1355+ },
1356+ .timestamp_freq = 1,
1357+ .bittiming_const = &kvaser_usb_leaf_bittiming_const,
1358+};
drivers/net/can/usb/peak_usb/pcan_usb.c+1−0
@@ -423,6 +423,7 @@ static int pcan_usb_decode_error(struct pcan_usb_msg_context *mc, u8 n, · 422 unmodified lines
423423 new_state = CAN_STATE_ERROR_WARNING;
424424 break;
425425 }
426+ /* else: fall through */
426427
427428 case CAN_STATE_ERROR_WARNING:
428429 if (n & PCAN_USB_ERROR_BUS_HEAVY) {
drivers/net/can/usb/peak_usb/pcan_usb_core.c+1−0
@@ -353,6 +353,7 @@ static netdev_tx_t peak_usb_ndo_start_xmit(struct sk_buff *skb, · 352 unmodified lines
353353 default:
354354 netdev_warn(netdev, "tx urb submitting failed err=%d\n",
355355 err);
356+ /* fall through */
356357 case -ENOENT:
357358 /* cable unplugged */
358359 stats->tx_dropped++;
drivers/net/can/usb/peak_usb/pcan_usb_pro.c+2−0
@@ -141,8 +141,10 @@ static int pcan_msg_add_rec(struct pcan_usb_pro_msg *pm, u8 id, ...) · 140 unmodified lines
141141 switch (id) {
142142 case PCAN_USBPRO_TXMSG8:
143143 i += 4;
144+ /* fall through */
144145 case PCAN_USBPRO_TXMSG4:
145146 i += 4;
147+ /* fall through */
146148 case PCAN_USBPRO_TXMSG0:
147149 *pc++ = va_arg(ap, int);
148150 *pc++ = va_arg(ap, int);
drivers/net/can/usb/ucan.c+1613−0
@@ -0,0 +1,1613 @@
1+// SPDX-License-Identifier: GPL-2.0
2+
3+/* Driver for Theobroma Systems UCAN devices, Protocol Version 3
4+ *
5+ * Copyright (C) 2018 Theobroma Systems Design und Consulting GmbH
6+ *
7+ *
8+ * General Description:
9+ *
10+ * The USB Device uses three Endpoints:
11+ *
12+ * CONTROL Endpoint: Is used the setup the device (start, stop,
13+ * info, configure).
14+ *
15+ * IN Endpoint: The device sends CAN Frame Messages and Device
16+ * Information using the IN endpoint.
17+ *
18+ * OUT Endpoint: The driver sends configuration requests, and CAN
19+ * Frames on the out endpoint.
20+ *
21+ * Error Handling:
22+ *
23+ * If error reporting is turned on the device encodes error into CAN
24+ * error frames (see uapi/linux/can/error.h) and sends it using the
25+ * IN Endpoint. The driver updates statistics and forward it.
26+ */
27+
28+#include <linux/can.h>
29+#include <linux/can/dev.h>
30+#include <linux/can/error.h>
31+#include <linux/module.h>
32+#include <linux/netdevice.h>
33+#include <linux/signal.h>
34+#include <linux/skbuff.h>
35+#include <linux/slab.h>
36+#include <linux/usb.h>
37+
38+#include <linux/can.h>
39+#include <linux/can/dev.h>
40+#include <linux/can/error.h>
41+
42+#define UCAN_DRIVER_NAME "ucan"
43+#define UCAN_MAX_RX_URBS 8
44+/* the CAN controller needs a while to enable/disable the bus */
45+#define UCAN_USB_CTL_PIPE_TIMEOUT 1000
46+/* this driver currently supports protocol version 3 only */
47+#define UCAN_PROTOCOL_VERSION_MIN 3
48+#define UCAN_PROTOCOL_VERSION_MAX 3
49+
50+/* UCAN Message Definitions
51+ * ------------------------
52+ *
53+ * ucan_message_out_t and ucan_message_in_t define the messages
54+ * transmitted on the OUT and IN endpoint.
55+ *
56+ * Multibyte fields are transmitted with little endianness
57+ *
58+ * INTR Endpoint: a single uint32_t storing the current space in the fifo
59+ *
60+ * OUT Endpoint: single message of type ucan_message_out_t is
61+ * transmitted on the out endpoint
62+ *
63+ * IN Endpoint: multiple messages ucan_message_in_t concateted in
64+ * the following way:
65+ *
66+ * m[n].len <=> the length if message n(including the header in bytes)
67+ * m[n] is is aligned to a 4 byte boundary, hence
68+ * offset(m[0]) := 0;
69+ * offset(m[n+1]) := offset(m[n]) + (m[n].len + 3) & 3
70+ *
71+ * this implies that
72+ * offset(m[n]) % 4 <=> 0
73+ */
74+
75+/* Device Global Commands */
76+enum {
77+ UCAN_DEVICE_GET_FW_STRING = 0,
78+};
79+
80+/* UCAN Commands */
81+enum {
82+ /* start the can transceiver - val defines the operation mode */
83+ UCAN_COMMAND_START = 0,
84+ /* cancel pending transmissions and stop the can transceiver */
85+ UCAN_COMMAND_STOP = 1,
86+ /* send can transceiver into low-power sleep mode */
87+ UCAN_COMMAND_SLEEP = 2,
88+ /* wake up can transceiver from low-power sleep mode */
89+ UCAN_COMMAND_WAKEUP = 3,
90+ /* reset the can transceiver */
91+ UCAN_COMMAND_RESET = 4,
92+ /* get piece of info from the can transceiver - subcmd defines what
93+ * piece
94+ */
95+ UCAN_COMMAND_GET = 5,
96+ /* clear or disable hardware filter - subcmd defines which of the two */
97+ UCAN_COMMAND_FILTER = 6,
98+ /* Setup bittiming */
99+ UCAN_COMMAND_SET_BITTIMING = 7,
100+ /* recover from bus-off state */
101+ UCAN_COMMAND_RESTART = 8,
102+};
103+
104+/* UCAN_COMMAND_START and UCAN_COMMAND_GET_INFO operation modes (bitmap).
105+ * Undefined bits must be set to 0.
106+ */
107+enum {
108+ UCAN_MODE_LOOPBACK = BIT(0),
109+ UCAN_MODE_SILENT = BIT(1),
110+ UCAN_MODE_3_SAMPLES = BIT(2),
111+ UCAN_MODE_ONE_SHOT = BIT(3),
112+ UCAN_MODE_BERR_REPORT = BIT(4),
113+};
114+
115+/* UCAN_COMMAND_GET subcommands */
116+enum {
117+ UCAN_COMMAND_GET_INFO = 0,
118+ UCAN_COMMAND_GET_PROTOCOL_VERSION = 1,
119+};
120+
121+/* UCAN_COMMAND_FILTER subcommands */
122+enum {
123+ UCAN_FILTER_CLEAR = 0,
124+ UCAN_FILTER_DISABLE = 1,
125+ UCAN_FILTER_ENABLE = 2,
126+};
127+
128+/* OUT endpoint message types */
129+enum {
130+ UCAN_OUT_TX = 2, /* transmit a CAN frame */
131+};
132+
133+/* IN endpoint message types */
134+enum {
135+ UCAN_IN_TX_COMPLETE = 1, /* CAN frame transmission completed */
136+ UCAN_IN_RX = 2, /* CAN frame received */
137+};
138+
139+struct ucan_ctl_cmd_start {
140+ __le16 mode; /* OR-ing any of UCAN_MODE_* */
141+} __packed;
142+
143+struct ucan_ctl_cmd_set_bittiming {
144+ __le32 tq; /* Time quanta (TQ) in nanoseconds */
145+ __le16 brp; /* TQ Prescaler */
146+ __le16 sample_point; /* Samplepoint on tenth percent */
147+ u8 prop_seg; /* Propagation segment in TQs */
148+ u8 phase_seg1; /* Phase buffer segment 1 in TQs */
149+ u8 phase_seg2; /* Phase buffer segment 2 in TQs */
150+ u8 sjw; /* Synchronisation jump width in TQs */
151+} __packed;
152+
153+struct ucan_ctl_cmd_device_info {
154+ __le32 freq; /* Clock Frequency for tq generation */
155+ u8 tx_fifo; /* Size of the transmission fifo */
156+ u8 sjw_max; /* can_bittiming fields... */
157+ u8 tseg1_min;
158+ u8 tseg1_max;
159+ u8 tseg2_min;
160+ u8 tseg2_max;
161+ __le16 brp_inc;
162+ __le32 brp_min;
163+ __le32 brp_max; /* ...can_bittiming fields */
164+ __le16 ctrlmodes; /* supported control modes */
165+ __le16 hwfilter; /* Number of HW filter banks */
166+ __le16 rxmboxes; /* Number of receive Mailboxes */
167+} __packed;
168+
169+struct ucan_ctl_cmd_get_protocol_version {
170+ __le32 version;
171+} __packed;
172+
173+union ucan_ctl_payload {
174+ /* Setup Bittiming
175+ * bmRequest == UCAN_COMMAND_START
176+ */
177+ struct ucan_ctl_cmd_start cmd_start;
178+ /* Setup Bittiming
179+ * bmRequest == UCAN_COMMAND_SET_BITTIMING
180+ */
181+ struct ucan_ctl_cmd_set_bittiming cmd_set_bittiming;
182+ /* Get Device Information
183+ * bmRequest == UCAN_COMMAND_GET; wValue = UCAN_COMMAND_GET_INFO
184+ */
185+ struct ucan_ctl_cmd_device_info cmd_get_device_info;
186+ /* Get Protocol Version
187+ * bmRequest == UCAN_COMMAND_GET;
188+ * wValue = UCAN_COMMAND_GET_PROTOCOL_VERSION
189+ */
190+ struct ucan_ctl_cmd_get_protocol_version cmd_get_protocol_version;
191+
192+ u8 raw[128];
193+} __packed;
194+
195+enum {
196+ UCAN_TX_COMPLETE_SUCCESS = BIT(0),
197+};
198+
199+/* Transmission Complete within ucan_message_in */
200+struct ucan_tx_complete_entry_t {
201+ u8 echo_index;
202+ u8 flags;
203+} __packed __aligned(0x2);
204+
205+/* CAN Data message format within ucan_message_in/out */
206+struct ucan_can_msg {
207+ /* note DLC is computed by
208+ * msg.len - sizeof (msg.len)
209+ * - sizeof (msg.type)
210+ * - sizeof (msg.can_msg.id)
211+ */
212+ __le32 id;
213+
214+ union {
215+ u8 data[CAN_MAX_DLEN]; /* Data of CAN frames */
216+ u8 dlc; /* RTR dlc */
217+ };
218+} __packed;
219+
220+/* OUT Endpoint, outbound messages */
221+struct ucan_message_out {
222+ __le16 len; /* Length of the content include header */
223+ u8 type; /* UCAN_OUT_TX and friends */
224+ u8 subtype; /* command sub type */
225+
226+ union {
227+ /* Transmit CAN frame
228+ * (type == UCAN_TX) && ((msg.can_msg.id & CAN_RTR_FLAG) == 0)
229+ * subtype stores the echo id
230+ */
231+ struct ucan_can_msg can_msg;
232+ } msg;
233+} __packed __aligned(0x4);
234+
235+/* IN Endpoint, inbound messages */
236+struct ucan_message_in {
237+ __le16 len; /* Length of the content include header */
238+ u8 type; /* UCAN_IN_RX and friends */
239+ u8 subtype; /* command sub type */
240+
241+ union {
242+ /* CAN Frame received
243+ * (type == UCAN_IN_RX)
244+ * && ((msg.can_msg.id & CAN_RTR_FLAG) == 0)
245+ */
246+ struct ucan_can_msg can_msg;
247+
248+ /* CAN transmission complete
249+ * (type == UCAN_IN_TX_COMPLETE)
250+ */
251+ struct ucan_tx_complete_entry_t can_tx_complete_msg[0];
252+ } __aligned(0x4) msg;
253+} __packed;
254+
255+/* Macros to calculate message lengths */
256+#define UCAN_OUT_HDR_SIZE offsetof(struct ucan_message_out, msg)
257+
258+#define UCAN_IN_HDR_SIZE offsetof(struct ucan_message_in, msg)
259+#define UCAN_IN_LEN(member) (UCAN_OUT_HDR_SIZE + sizeof(member))
260+
261+struct ucan_priv;
262+
263+/* Context Information for transmission URBs */
264+struct ucan_urb_context {
265+ struct ucan_priv *up;
266+ u8 dlc;
267+ bool allocated;
268+};
269+
270+/* Information reported by the USB device */
271+struct ucan_device_info {
272+ struct can_bittiming_const bittiming_const;
273+ u8 tx_fifo;
274+};
275+
276+/* Driver private data */
277+struct ucan_priv {
278+ /* must be the first member */
279+ struct can_priv can;
280+
281+ /* linux USB device structures */
282+ struct usb_device *udev;
283+ struct usb_interface *intf;
284+ struct net_device *netdev;
285+
286+ /* lock for can->echo_skb (used around
287+ * can_put/get/free_echo_skb
288+ */
289+ spinlock_t echo_skb_lock;
290+
291+ /* usb device information information */
292+ u8 intf_index;
293+ u8 in_ep_addr;
294+ u8 out_ep_addr;
295+ u16 in_ep_size;
296+
297+ /* transmission and reception buffers */
298+ struct usb_anchor rx_urbs;
299+ struct usb_anchor tx_urbs;
300+
301+ union ucan_ctl_payload *ctl_msg_buffer;
302+ struct ucan_device_info device_info;
303+
304+ /* transmission control information and locks */
305+ spinlock_t context_lock;
306+ unsigned int available_tx_urbs;
307+ struct ucan_urb_context *context_array;
308+};
309+
310+static u8 ucan_get_can_dlc(struct ucan_can_msg *msg, u16 len)
311+{
312+ if (le32_to_cpu(msg->id) & CAN_RTR_FLAG)
313+ return get_can_dlc(msg->dlc);
314+ else
315+ return get_can_dlc(len - (UCAN_IN_HDR_SIZE + sizeof(msg->id)));
316+}
317+
318+static void ucan_release_context_array(struct ucan_priv *up)
319+{
320+ if (!up->context_array)
321+ return;
322+
323+ /* lock is not needed because, driver is currently opening or closing */
324+ up->available_tx_urbs = 0;
325+
326+ kfree(up->context_array);
327+ up->context_array = NULL;
328+}
329+
330+static int ucan_alloc_context_array(struct ucan_priv *up)
331+{
332+ int i;
333+
334+ /* release contexts if any */
335+ ucan_release_context_array(up);
336+
337+ up->context_array = kcalloc(up->device_info.tx_fifo,
338+ sizeof(*up->context_array),
339+ GFP_KERNEL);
340+ if (!up->context_array) {
341+ netdev_err(up->netdev,
342+ "Not enough memory to allocate tx contexts\n");
343+ return -ENOMEM;
344+ }
345+
346+ for (i = 0; i < up->device_info.tx_fifo; i++) {
347+ up->context_array[i].allocated = false;
348+ up->context_array[i].up = up;
349+ }
350+
351+ /* lock is not needed because, driver is currently opening */
352+ up->available_tx_urbs = up->device_info.tx_fifo;
353+
354+ return 0;
355+}
356+
357+static struct ucan_urb_context *ucan_alloc_context(struct ucan_priv *up)
358+{
359+ int i;
360+ unsigned long flags;
361+ struct ucan_urb_context *ret = NULL;
362+
363+ if (WARN_ON_ONCE(!up->context_array))
364+ return NULL;
365+
366+ /* execute context operation atomically */
367+ spin_lock_irqsave(&up->context_lock, flags);
368+
369+ for (i = 0; i < up->device_info.tx_fifo; i++) {
370+ if (!up->context_array[i].allocated) {
371+ /* update context */
372+ ret = &up->context_array[i];
373+ up->context_array[i].allocated = true;
374+
375+ /* stop queue if necessary */
376+ up->available_tx_urbs--;
377+ if (!up->available_tx_urbs)
378+ netif_stop_queue(up->netdev);
379+
380+ break;
381+ }
382+ }
383+
384+ spin_unlock_irqrestore(&up->context_lock, flags);
385+ return ret;
386+}
387+
388+static bool ucan_release_context(struct ucan_priv *up,
389+ struct ucan_urb_context *ctx)
390+{
391+ unsigned long flags;
392+ bool ret = false;
393+
394+ if (WARN_ON_ONCE(!up->context_array))
395+ return false;
396+
397+ /* execute context operation atomically */
398+ spin_lock_irqsave(&up->context_lock, flags);
399+
400+ /* context was not allocated, maybe the device sent garbage */
401+ if (ctx->allocated) {
402+ ctx->allocated = false;
403+
404+ /* check if the queue needs to be woken */
405+ if (!up->available_tx_urbs)
406+ netif_wake_queue(up->netdev);
407+ up->available_tx_urbs++;
408+
409+ ret = true;
410+ }
411+
412+ spin_unlock_irqrestore(&up->context_lock, flags);
413+ return ret;
414+}
415+
416+static int ucan_ctrl_command_out(struct ucan_priv *up,
417+ u8 cmd, u16 subcmd, u16 datalen)
418+{
419+ return usb_control_msg(up->udev,
420+ usb_sndctrlpipe(up->udev, 0),
421+ cmd,
422+ USB_DIR_OUT | USB_TYPE_VENDOR |
423+ USB_RECIP_INTERFACE,
424+ subcmd,
425+ up->intf_index,
426+ up->ctl_msg_buffer,
427+ datalen,
428+ UCAN_USB_CTL_PIPE_TIMEOUT);
429+}
430+
431+static int ucan_device_request_in(struct ucan_priv *up,
432+ u8 cmd, u16 subcmd, u16 datalen)
433+{
434+ return usb_control_msg(up->udev,
435+ usb_rcvctrlpipe(up->udev, 0),
436+ cmd,
437+ USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
438+ subcmd,
439+ 0,
440+ up->ctl_msg_buffer,
441+ datalen,
442+ UCAN_USB_CTL_PIPE_TIMEOUT);
443+}
444+
445+/* Parse the device information structure reported by the device and
446+ * setup private variables accordingly
447+ */
448+static void ucan_parse_device_info(struct ucan_priv *up,
449+ struct ucan_ctl_cmd_device_info *device_info)
450+{
451+ struct can_bittiming_const *bittiming =
452+ &up->device_info.bittiming_const;
453+ u16 ctrlmodes;
454+
455+ /* store the data */
456+ up->can.clock.freq = le32_to_cpu(device_info->freq);
457+ up->device_info.tx_fifo = device_info->tx_fifo;
458+ strcpy(bittiming->name, "ucan");
459+ bittiming->tseg1_min = device_info->tseg1_min;
460+ bittiming->tseg1_max = device_info->tseg1_max;
461+ bittiming->tseg2_min = device_info->tseg2_min;
462+ bittiming->tseg2_max = device_info->tseg2_max;
463+ bittiming->sjw_max = device_info->sjw_max;
464+ bittiming->brp_min = le32_to_cpu(device_info->brp_min);
465+ bittiming->brp_max = le32_to_cpu(device_info->brp_max);
466+ bittiming->brp_inc = le16_to_cpu(device_info->brp_inc);
467+
468+ ctrlmodes = le16_to_cpu(device_info->ctrlmodes);
469+
470+ up->can.ctrlmode_supported = 0;
471+
472+ if (ctrlmodes & UCAN_MODE_LOOPBACK)
473+ up->can.ctrlmode_supported |= CAN_CTRLMODE_LOOPBACK;
474+ if (ctrlmodes & UCAN_MODE_SILENT)
475+ up->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY;
476+ if (ctrlmodes & UCAN_MODE_3_SAMPLES)
477+ up->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
478+ if (ctrlmodes & UCAN_MODE_ONE_SHOT)
479+ up->can.ctrlmode_supported |= CAN_CTRLMODE_ONE_SHOT;
480+ if (ctrlmodes & UCAN_MODE_BERR_REPORT)
481+ up->can.ctrlmode_supported |= CAN_CTRLMODE_BERR_REPORTING;
482+}
483+
484+/* Handle a CAN error frame that we have received from the device.
485+ * Returns true if the can state has changed.
486+ */
487+static bool ucan_handle_error_frame(struct ucan_priv *up,
488+ struct ucan_message_in *m,
489+ canid_t canid)
490+{
491+ enum can_state new_state = up->can.state;
492+ struct net_device_stats *net_stats = &up->netdev->stats;
493+ struct can_device_stats *can_stats = &up->can.can_stats;
494+
495+ if (canid & CAN_ERR_LOSTARB)
496+ can_stats->arbitration_lost++;
497+
498+ if (canid & CAN_ERR_BUSERROR)
499+ can_stats->bus_error++;
500+
501+ if (canid & CAN_ERR_ACK)
502+ net_stats->tx_errors++;
503+
504+ if (canid & CAN_ERR_BUSOFF)
505+ new_state = CAN_STATE_BUS_OFF;
506+
507+ /* controller problems, details in data[1] */
508+ if (canid & CAN_ERR_CRTL) {
509+ u8 d1 = m->msg.can_msg.data[1];
510+
511+ if (d1 & CAN_ERR_CRTL_RX_OVERFLOW)
512+ net_stats->rx_over_errors++;
513+
514+ /* controller state bits: if multiple are set the worst wins */
515+ if (d1 & CAN_ERR_CRTL_ACTIVE)
516+ new_state = CAN_STATE_ERROR_ACTIVE;
517+
518+ if (d1 & (CAN_ERR_CRTL_RX_WARNING | CAN_ERR_CRTL_TX_WARNING))
519+ new_state = CAN_STATE_ERROR_WARNING;
520+
521+ if (d1 & (CAN_ERR_CRTL_RX_PASSIVE | CAN_ERR_CRTL_TX_PASSIVE))
522+ new_state = CAN_STATE_ERROR_PASSIVE;
523+ }
524+
525+ /* protocol error, details in data[2] */
526+ if (canid & CAN_ERR_PROT) {
527+ u8 d2 = m->msg.can_msg.data[2];
528+
529+ if (d2 & CAN_ERR_PROT_TX)
530+ net_stats->tx_errors++;
531+ else
532+ net_stats->rx_errors++;
533+ }
534+
535+ /* no state change - we are done */
536+ if (up->can.state == new_state)
537+ return false;
538+
539+ /* we switched into a better state */
540+ if (up->can.state > new_state) {
541+ up->can.state = new_state;
542+ return true;
543+ }
544+
545+ /* we switched into a worse state */
546+ up->can.state = new_state;
547+ switch (new_state) {
548+ case CAN_STATE_BUS_OFF:
549+ can_stats->bus_off++;
550+ can_bus_off(up->netdev);
551+ break;
552+ case CAN_STATE_ERROR_PASSIVE:
553+ can_stats->error_passive++;
554+ break;
555+ case CAN_STATE_ERROR_WARNING:
556+ can_stats->error_warning++;
557+ break;
558+ default:
559+ break;
560+ }
561+ return true;
562+}
563+
564+/* Callback on reception of a can frame via the IN endpoint
565+ *
566+ * This function allocates an skb and transferres it to the Linux
567+ * network stack
568+ */
569+static void ucan_rx_can_msg(struct ucan_priv *up, struct ucan_message_in *m)
570+{
571+ int len;
572+ canid_t canid;
573+ struct can_frame *cf;
574+ struct sk_buff *skb;
575+ struct net_device_stats *stats = &up->netdev->stats;
576+
577+ /* get the contents of the length field */
578+ len = le16_to_cpu(m->len);
579+
580+ /* check sanity */
581+ if (len < UCAN_IN_HDR_SIZE + sizeof(m->msg.can_msg.id)) {
582+ netdev_warn(up->netdev, "invalid input message len: %d\n", len);
583+ return;
584+ }
585+
586+ /* handle error frames */
587+ canid = le32_to_cpu(m->msg.can_msg.id);
588+ if (canid & CAN_ERR_FLAG) {
589+ bool busstate_changed = ucan_handle_error_frame(up, m, canid);
590+
591+ /* if berr-reporting is off only state changes get through */
592+ if (!(up->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) &&
593+ !busstate_changed)
594+ return;
595+ } else {
596+ canid_t canid_mask;
597+ /* compute the mask for canid */
598+ canid_mask = CAN_RTR_FLAG;
599+ if (canid & CAN_EFF_FLAG)
600+ canid_mask |= CAN_EFF_MASK | CAN_EFF_FLAG;
601+ else
602+ canid_mask |= CAN_SFF_MASK;
603+
604+ if (canid & ~canid_mask)
605+ netdev_warn(up->netdev,
606+ "unexpected bits set (canid %x, mask %x)",
607+ canid, canid_mask);
608+
609+ canid &= canid_mask;
610+ }
611+
612+ /* allocate skb */
613+ skb = alloc_can_skb(up->netdev, &cf);
614+ if (!skb)
615+ return;
616+
617+ /* fill the can frame */
618+ cf->can_id = canid;
619+
620+ /* compute DLC taking RTR_FLAG into account */
621+ cf->can_dlc = ucan_get_can_dlc(&m->msg.can_msg, len);
622+
623+ /* copy the payload of non RTR frames */
624+ if (!(cf->can_id & CAN_RTR_FLAG) || (cf->can_id & CAN_ERR_FLAG))
625+ memcpy(cf->data, m->msg.can_msg.data, cf->can_dlc);
626+
627+ /* don't count error frames as real packets */
628+ stats->rx_packets++;
629+ stats->rx_bytes += cf->can_dlc;
630+
631+ /* pass it to Linux */
632+ netif_rx(skb);
633+}
634+
635+/* callback indicating completed transmission */
636+static void ucan_tx_complete_msg(struct ucan_priv *up,
637+ struct ucan_message_in *m)
638+{
639+ unsigned long flags;
640+ u16 count, i;
641+ u8 echo_index, dlc;
642+ u16 len = le16_to_cpu(m->len);
643+
644+ struct ucan_urb_context *context;
645+
646+ if (len < UCAN_IN_HDR_SIZE || (len % 2 != 0)) {
647+ netdev_err(up->netdev, "invalid tx complete length\n");
648+ return;
649+ }
650+
651+ count = (len - UCAN_IN_HDR_SIZE) / 2;
652+ for (i = 0; i < count; i++) {
653+ /* we did not submit such echo ids */
654+ echo_index = m->msg.can_tx_complete_msg[i].echo_index;
655+ if (echo_index >= up->device_info.tx_fifo) {
656+ up->netdev->stats.tx_errors++;
657+ netdev_err(up->netdev,
658+ "invalid echo_index %d received\n",
659+ echo_index);
660+ continue;
661+ }
662+
663+ /* gather information from the context */
664+ context = &up->context_array[echo_index];
665+ dlc = READ_ONCE(context->dlc);
666+
667+ /* Release context and restart queue if necessary.
668+ * Also check if the context was allocated
669+ */
670+ if (!ucan_release_context(up, context))
671+ continue;
672+
673+ spin_lock_irqsave(&up->echo_skb_lock, flags);
674+ if (m->msg.can_tx_complete_msg[i].flags &
675+ UCAN_TX_COMPLETE_SUCCESS) {
676+ /* update statistics */
677+ up->netdev->stats.tx_packets++;
678+ up->netdev->stats.tx_bytes += dlc;
679+ can_get_echo_skb(up->netdev, echo_index);
680+ } else {
681+ up->netdev->stats.tx_dropped++;
682+ can_free_echo_skb(up->netdev, echo_index);
683+ }
684+ spin_unlock_irqrestore(&up->echo_skb_lock, flags);
685+ }
686+}
687+
688+/* callback on reception of a USB message */
689+static void ucan_read_bulk_callback(struct urb *urb)
690+{
691+ int ret;
692+ int pos;
693+ struct ucan_priv *up = urb->context;
694+ struct net_device *netdev = up->netdev;
695+ struct ucan_message_in *m;
696+
697+ /* the device is not up and the driver should not receive any
698+ * data on the bulk in pipe
699+ */
700+ if (WARN_ON(!up->context_array)) {
701+ usb_free_coherent(up->udev,
702+ up->in_ep_size,
703+ urb->transfer_buffer,
704+ urb->transfer_dma);
705+ return;
706+ }
707+
708+ /* check URB status */
709+ switch (urb->status) {
710+ case 0:
711+ break;
712+ case -ENOENT:
713+ case -EPIPE:
714+ case -EPROTO:
715+ case -ESHUTDOWN:
716+ case -ETIME:
717+ /* urb is not resubmitted -> free dma data */
718+ usb_free_coherent(up->udev,
719+ up->in_ep_size,
720+ urb->transfer_buffer,
721+ urb->transfer_dma);
722+ netdev_dbg(up->netdev, "not resumbmitting urb; status: %d\n",
723+ urb->status);
724+ return;
725+ default:
726+ goto resubmit;
727+ }
728+
729+ /* sanity check */
730+ if (!netif_device_present(netdev))
731+ return;
732+
733+ /* iterate over input */
734+ pos = 0;
735+ while (pos < urb->actual_length) {
736+ int len;
737+
738+ /* check sanity (length of header) */
739+ if ((urb->actual_length - pos) < UCAN_IN_HDR_SIZE) {
740+ netdev_warn(up->netdev,
741+ "invalid message (short; no hdr; l:%d)\n",
742+ urb->actual_length);
743+ goto resubmit;
744+ }
745+
746+ /* setup the message address */
747+ m = (struct ucan_message_in *)
748+ ((u8 *)urb->transfer_buffer + pos);
749+ len = le16_to_cpu(m->len);
750+
751+ /* check sanity (length of content) */
752+ if (urb->actual_length - pos < len) {
753+ netdev_warn(up->netdev,
754+ "invalid message (short; no data; l:%d)\n",
755+ urb->actual_length);
756+ print_hex_dump(KERN_WARNING,
757+ "raw data: ",
758+ DUMP_PREFIX_ADDRESS,
759+ 16,
760+ 1,
761+ urb->transfer_buffer,
762+ urb->actual_length,
763+ true);
764+
765+ goto resubmit;
766+ }
767+
768+ switch (m->type) {
769+ case UCAN_IN_RX:
770+ ucan_rx_can_msg(up, m);
771+ break;
772+ case UCAN_IN_TX_COMPLETE:
773+ ucan_tx_complete_msg(up, m);
774+ break;
775+ default:
776+ netdev_warn(up->netdev,
777+ "invalid message (type; t:%d)\n",
778+ m->type);
779+ break;
780+ }
781+
782+ /* proceed to next message */
783+ pos += len;
784+ /* align to 4 byte boundary */
785+ pos = round_up(pos, 4);
786+ }
787+
788+resubmit:
789+ /* resubmit urb when done */
790+ usb_fill_bulk_urb(urb, up->udev,
791+ usb_rcvbulkpipe(up->udev,
792+ up->in_ep_addr),
793+ urb->transfer_buffer,
794+ up->in_ep_size,
795+ ucan_read_bulk_callback,
796+ up);
797+
798+ usb_anchor_urb(urb, &up->rx_urbs);
799+ ret = usb_submit_urb(urb, GFP_KERNEL);
800+
801+ if (ret < 0) {
802+ netdev_err(up->netdev,
803+ "failed resubmitting read bulk urb: %d\n",
804+ ret);
805+
806+ usb_unanchor_urb(urb);
807+ usb_free_coherent(up->udev,
808+ up->in_ep_size,
809+ urb->transfer_buffer,
810+ urb->transfer_dma);
811+
812+ if (ret == -ENODEV)
813+ netif_device_detach(netdev);
814+ }
815+}
816+
817+/* callback after transmission of a USB message */
818+static void ucan_write_bulk_callback(struct urb *urb)
819+{
820+ unsigned long flags;
821+ struct ucan_priv *up;
822+ struct ucan_urb_context *context = urb->context;
823+
824+ /* get the urb context */
825+ if (WARN_ON_ONCE(!context))
826+ return;
827+
828+ /* free up our allocated buffer */
829+ usb_free_coherent(urb->dev,
830+ sizeof(struct ucan_message_out),
831+ urb->transfer_buffer,
832+ urb->transfer_dma);
833+
834+ up = context->up;
835+ if (WARN_ON_ONCE(!up))
836+ return;
837+
838+ /* sanity check */
839+ if (!netif_device_present(up->netdev))
840+ return;
841+
842+ /* transmission failed (USB - the device will not send a TX complete) */
843+ if (urb->status) {
844+ netdev_warn(up->netdev,
845+ "failed to transmit USB message to device: %d\n",
846+ urb->status);
847+
848+ /* update counters an cleanup */
849+ spin_lock_irqsave(&up->echo_skb_lock, flags);
850+ can_free_echo_skb(up->netdev, context - up->context_array);
851+ spin_unlock_irqrestore(&up->echo_skb_lock, flags);
852+
853+ up->netdev->stats.tx_dropped++;
854+
855+ /* release context and restart the queue if necessary */
856+ if (!ucan_release_context(up, context))
857+ netdev_err(up->netdev,
858+ "urb failed, failed to release context\n");
859+ }
860+}
861+
862+static void ucan_cleanup_rx_urbs(struct ucan_priv *up, struct urb **urbs)
863+{
864+ int i;
865+
866+ for (i = 0; i < UCAN_MAX_RX_URBS; i++) {
867+ if (urbs[i]) {
868+ usb_unanchor_urb(urbs[i]);
869+ usb_free_coherent(up->udev,
870+ up->in_ep_size,
871+ urbs[i]->transfer_buffer,
872+ urbs[i]->transfer_dma);
873+ usb_free_urb(urbs[i]);
874+ }
875+ }
876+
877+ memset(urbs, 0, sizeof(*urbs) * UCAN_MAX_RX_URBS);
878+}
879+
880+static int ucan_prepare_and_anchor_rx_urbs(struct ucan_priv *up,
881+ struct urb **urbs)
882+{
883+ int i;
884+
885+ memset(urbs, 0, sizeof(*urbs) * UCAN_MAX_RX_URBS);
886+
887+ for (i = 0; i < UCAN_MAX_RX_URBS; i++) {
888+ void *buf;
889+
890+ urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
891+ if (!urbs[i])
892+ goto err;
893+
894+ buf = usb_alloc_coherent(up->udev,
895+ up->in_ep_size,
896+ GFP_KERNEL, &urbs[i]->transfer_dma);
897+ if (!buf) {
898+ /* cleanup this urb */
899+ usb_free_urb(urbs[i]);
900+ urbs[i] = NULL;
901+ goto err;
902+ }
903+
904+ usb_fill_bulk_urb(urbs[i], up->udev,
905+ usb_rcvbulkpipe(up->udev,
906+ up->in_ep_addr),
907+ buf,
908+ up->in_ep_size,
909+ ucan_read_bulk_callback,
910+ up);
911+
912+ urbs[i]->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
913+
914+ usb_anchor_urb(urbs[i], &up->rx_urbs);
915+ }
916+ return 0;
917+
918+err:
919+ /* cleanup other unsubmitted urbs */
920+ ucan_cleanup_rx_urbs(up, urbs);
921+ return -ENOMEM;
922+}
923+
924+/* Submits rx urbs with the semantic: Either submit all, or cleanup
925+ * everything. I case of errors submitted urbs are killed and all urbs in
926+ * the array are freed. I case of no errors every entry in the urb
927+ * array is set to NULL.
928+ */
929+static int ucan_submit_rx_urbs(struct ucan_priv *up, struct urb **urbs)
930+{
931+ int i, ret;
932+
933+ /* Iterate over all urbs to submit. On success remove the urb
934+ * from the list.
935+ */
936+ for (i = 0; i < UCAN_MAX_RX_URBS; i++) {
937+ ret = usb_submit_urb(urbs[i], GFP_KERNEL);
938+ if (ret) {
939+ netdev_err(up->netdev,
940+ "could not submit urb; code: %d\n",
941+ ret);
942+ goto err;
943+ }
944+
945+ /* Anchor URB and drop reference, USB core will take
946+ * care of freeing it
947+ */
948+ usb_free_urb(urbs[i]);
949+ urbs[i] = NULL;
950+ }
951+ return 0;
952+
953+err:
954+ /* Cleanup unsubmitted urbs */
955+ ucan_cleanup_rx_urbs(up, urbs);
956+
957+ /* Kill urbs that are already submitted */
958+ usb_kill_anchored_urbs(&up->rx_urbs);
959+
960+ return ret;
961+}
962+
963+/* Open the network device */
964+static int ucan_open(struct net_device *netdev)
965+{
966+ int ret, ret_cleanup;
967+ u16 ctrlmode;
968+ struct urb *urbs[UCAN_MAX_RX_URBS];
969+ struct ucan_priv *up = netdev_priv(netdev);
970+
971+ ret = ucan_alloc_context_array(up);
972+ if (ret)
973+ return ret;
974+
975+ /* Allocate and prepare IN URBS - allocated and anchored
976+ * urbs are stored in urbs[] for clean
977+ */
978+ ret = ucan_prepare_and_anchor_rx_urbs(up, urbs);
979+ if (ret)
980+ goto err_contexts;
981+
982+ /* Check the control mode */
983+ ctrlmode = 0;
984+ if (up->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
985+ ctrlmode |= UCAN_MODE_LOOPBACK;
986+ if (up->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
987+ ctrlmode |= UCAN_MODE_SILENT;
988+ if (up->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
989+ ctrlmode |= UCAN_MODE_3_SAMPLES;
990+ if (up->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT)
991+ ctrlmode |= UCAN_MODE_ONE_SHOT;
992+
993+ /* Enable this in any case - filtering is down within the
994+ * receive path
995+ */
996+ ctrlmode |= UCAN_MODE_BERR_REPORT;
997+ up->ctl_msg_buffer->cmd_start.mode = cpu_to_le16(ctrlmode);
998+
999+ /* Driver is ready to receive data - start the USB device */
1000+ ret = ucan_ctrl_command_out(up, UCAN_COMMAND_START, 0, 2);
1001+ if (ret < 0) {
1002+ netdev_err(up->netdev,
1003+ "could not start device, code: %d\n",
1004+ ret);
1005+ goto err_reset;
1006+ }
1007+
1008+ /* Call CAN layer open */
1009+ ret = open_candev(netdev);
1010+ if (ret)
1011+ goto err_stop;
1012+
1013+ /* Driver is ready to receive data. Submit RX URBS */
1014+ ret = ucan_submit_rx_urbs(up, urbs);
1015+ if (ret)
1016+ goto err_stop;
1017+
1018+ up->can.state = CAN_STATE_ERROR_ACTIVE;
1019+
1020+ /* Start the network queue */
1021+ netif_start_queue(netdev);
1022+
1023+ return 0;
1024+
1025+err_stop:
1026+ /* The device have started already stop it */
1027+ ret_cleanup = ucan_ctrl_command_out(up, UCAN_COMMAND_STOP, 0, 0);
1028+ if (ret_cleanup < 0)
1029+ netdev_err(up->netdev,
1030+ "could not stop device, code: %d\n",
1031+ ret_cleanup);
1032+
1033+err_reset:
1034+ /* The device might have received data, reset it for
1035+ * consistent state
1036+ */
1037+ ret_cleanup = ucan_ctrl_command_out(up, UCAN_COMMAND_RESET, 0, 0);
1038+ if (ret_cleanup < 0)
1039+ netdev_err(up->netdev,
1040+ "could not reset device, code: %d\n",
1041+ ret_cleanup);
1042+
1043+ /* clean up unsubmitted urbs */
1044+ ucan_cleanup_rx_urbs(up, urbs);
1045+
1046+err_contexts:
1047+ ucan_release_context_array(up);
1048+ return ret;
1049+}
1050+
1051+static struct urb *ucan_prepare_tx_urb(struct ucan_priv *up,
1052+ struct ucan_urb_context *context,
1053+ struct can_frame *cf,
1054+ u8 echo_index)
1055+{
1056+ int mlen;
1057+ struct urb *urb;
1058+ struct ucan_message_out *m;
1059+
1060+ /* create a URB, and a buffer for it, and copy the data to the URB */
1061+ urb = usb_alloc_urb(0, GFP_ATOMIC);
1062+ if (!urb) {
1063+ netdev_err(up->netdev, "no memory left for URBs\n");
1064+ return NULL;
1065+ }
1066+
1067+ m = usb_alloc_coherent(up->udev,
1068+ sizeof(struct ucan_message_out),
1069+ GFP_ATOMIC,
1070+ &urb->transfer_dma);
1071+ if (!m) {
1072+ netdev_err(up->netdev, "no memory left for USB buffer\n");
1073+ usb_free_urb(urb);
1074+ return NULL;
1075+ }
1076+
1077+ /* build the USB message */
1078+ m->type = UCAN_OUT_TX;
1079+ m->msg.can_msg.id = cpu_to_le32(cf->can_id);
1080+
1081+ if (cf->can_id & CAN_RTR_FLAG) {
1082+ mlen = UCAN_OUT_HDR_SIZE +
1083+ offsetof(struct ucan_can_msg, dlc) +
1084+ sizeof(m->msg.can_msg.dlc);
1085+ m->msg.can_msg.dlc = cf->can_dlc;
1086+ } else {
1087+ mlen = UCAN_OUT_HDR_SIZE +
1088+ sizeof(m->msg.can_msg.id) + cf->can_dlc;
1089+ memcpy(m->msg.can_msg.data, cf->data, cf->can_dlc);
1090+ }
1091+ m->len = cpu_to_le16(mlen);
1092+
1093+ context->dlc = cf->can_dlc;
1094+
1095+ m->subtype = echo_index;
1096+
1097+ /* build the urb */
1098+ usb_fill_bulk_urb(urb, up->udev,
1099+ usb_sndbulkpipe(up->udev,
1100+ up->out_ep_addr),
1101+ m, mlen, ucan_write_bulk_callback, context);
1102+ urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1103+
1104+ return urb;
1105+}
1106+
1107+static void ucan_clean_up_tx_urb(struct ucan_priv *up, struct urb *urb)
1108+{
1109+ usb_free_coherent(up->udev, sizeof(struct ucan_message_out),
1110+ urb->transfer_buffer, urb->transfer_dma);
1111+ usb_free_urb(urb);
1112+}
1113+
1114+/* callback when Linux needs to send a can frame */
1115+static netdev_tx_t ucan_start_xmit(struct sk_buff *skb,
1116+ struct net_device *netdev)
1117+{
1118+ unsigned long flags;
1119+ int ret;
1120+ u8 echo_index;
1121+ struct urb *urb;
1122+ struct ucan_urb_context *context;
1123+ struct ucan_priv *up = netdev_priv(netdev);
1124+ struct can_frame *cf = (struct can_frame *)skb->data;
1125+
1126+ /* check skb */
1127+ if (can_dropped_invalid_skb(netdev, skb))
1128+ return NETDEV_TX_OK;
1129+
1130+ /* allocate a context and slow down tx path, if fifo state is low */
1131+ context = ucan_alloc_context(up);
1132+ echo_index = context - up->context_array;
1133+
1134+ if (WARN_ON_ONCE(!context))
1135+ return NETDEV_TX_BUSY;
1136+
1137+ /* prepare urb for transmission */
1138+ urb = ucan_prepare_tx_urb(up, context, cf, echo_index);
1139+ if (!urb)
1140+ goto drop;
1141+
1142+ /* put the skb on can loopback stack */
1143+ spin_lock_irqsave(&up->echo_skb_lock, flags);
1144+ can_put_echo_skb(skb, up->netdev, echo_index);
1145+ spin_unlock_irqrestore(&up->echo_skb_lock, flags);
1146+
1147+ /* transmit it */
1148+ usb_anchor_urb(urb, &up->tx_urbs);
1149+ ret = usb_submit_urb(urb, GFP_ATOMIC);
1150+
1151+ /* cleanup urb */
1152+ if (ret) {
1153+ /* on error, clean up */
1154+ usb_unanchor_urb(urb);
1155+ ucan_clean_up_tx_urb(up, urb);
1156+ if (!ucan_release_context(up, context))
1157+ netdev_err(up->netdev,
1158+ "xmit err: failed to release context\n");
1159+
1160+ /* remove the skb from the echo stack - this also
1161+ * frees the skb
1162+ */
1163+ spin_lock_irqsave(&up->echo_skb_lock, flags);
1164+ can_free_echo_skb(up->netdev, echo_index);
1165+ spin_unlock_irqrestore(&up->echo_skb_lock, flags);
1166+
1167+ if (ret == -ENODEV) {
1168+ netif_device_detach(up->netdev);
1169+ } else {
1170+ netdev_warn(up->netdev,
1171+ "xmit err: failed to submit urb %d\n",
1172+ ret);
1173+ up->netdev->stats.tx_dropped++;
1174+ }
1175+ return NETDEV_TX_OK;
1176+ }
1177+
1178+ netif_trans_update(netdev);
1179+
1180+ /* release ref, as we do not need the urb anymore */
1181+ usb_free_urb(urb);
1182+
1183+ return NETDEV_TX_OK;
1184+
1185+drop:
1186+ if (!ucan_release_context(up, context))
1187+ netdev_err(up->netdev,
1188+ "xmit drop: failed to release context\n");
1189+ dev_kfree_skb(skb);
1190+ up->netdev->stats.tx_dropped++;
1191+
1192+ return NETDEV_TX_OK;
1193+}
1194+
1195+/* Device goes down
1196+ *
1197+ * Clean up used resources
1198+ */
1199+static int ucan_close(struct net_device *netdev)
1200+{
1201+ int ret;
1202+ struct ucan_priv *up = netdev_priv(netdev);
1203+
1204+ up->can.state = CAN_STATE_STOPPED;
1205+
1206+ /* stop sending data */
1207+ usb_kill_anchored_urbs(&up->tx_urbs);
1208+
1209+ /* stop receiving data */
1210+ usb_kill_anchored_urbs(&up->rx_urbs);
1211+
1212+ /* stop and reset can device */
1213+ ret = ucan_ctrl_command_out(up, UCAN_COMMAND_STOP, 0, 0);
1214+ if (ret < 0)
1215+ netdev_err(up->netdev,
1216+ "could not stop device, code: %d\n",
1217+ ret);
1218+
1219+ ret = ucan_ctrl_command_out(up, UCAN_COMMAND_RESET, 0, 0);
1220+ if (ret < 0)
1221+ netdev_err(up->netdev,
1222+ "could not reset device, code: %d\n",
1223+ ret);
1224+
1225+ netif_stop_queue(netdev);
1226+
1227+ ucan_release_context_array(up);
1228+
1229+ close_candev(up->netdev);
1230+ return 0;
1231+}
1232+
1233+/* CAN driver callbacks */
1234+static const struct net_device_ops ucan_netdev_ops = {
1235+ .ndo_open = ucan_open,
1236+ .ndo_stop = ucan_close,
1237+ .ndo_start_xmit = ucan_start_xmit,
1238+ .ndo_change_mtu = can_change_mtu,
1239+};
1240+
1241+/* Request to set bittiming
1242+ *
1243+ * This function generates an USB set bittiming message and transmits
1244+ * it to the device
1245+ */
1246+static int ucan_set_bittiming(struct net_device *netdev)
1247+{
1248+ int ret;
1249+ struct ucan_priv *up = netdev_priv(netdev);
1250+ struct ucan_ctl_cmd_set_bittiming *cmd_set_bittiming;
1251+
1252+ cmd_set_bittiming = &up->ctl_msg_buffer->cmd_set_bittiming;
1253+ cmd_set_bittiming->tq = cpu_to_le32(up->can.bittiming.tq);
1254+ cmd_set_bittiming->brp = cpu_to_le16(up->can.bittiming.brp);
1255+ cmd_set_bittiming->sample_point =
1256+ cpu_to_le16(up->can.bittiming.sample_point);
1257+ cmd_set_bittiming->prop_seg = up->can.bittiming.prop_seg;
1258+ cmd_set_bittiming->phase_seg1 = up->can.bittiming.phase_seg1;
1259+ cmd_set_bittiming->phase_seg2 = up->can.bittiming.phase_seg2;
1260+ cmd_set_bittiming->sjw = up->can.bittiming.sjw;
1261+
1262+ ret = ucan_ctrl_command_out(up, UCAN_COMMAND_SET_BITTIMING, 0,
1263+ sizeof(*cmd_set_bittiming));
1264+ return (ret < 0) ? ret : 0;
1265+}
1266+
1267+/* Restart the device to get it out of BUS-OFF state.
1268+ * Called when the user runs "ip link set can1 type can restart".
1269+ */
1270+static int ucan_set_mode(struct net_device *netdev, enum can_mode mode)
1271+{
1272+ int ret;
1273+ unsigned long flags;
1274+ struct ucan_priv *up = netdev_priv(netdev);
1275+
1276+ switch (mode) {
1277+ case CAN_MODE_START:
1278+ netdev_dbg(up->netdev, "restarting device\n");
1279+
1280+ ret = ucan_ctrl_command_out(up, UCAN_COMMAND_RESTART, 0, 0);
1281+ up->can.state = CAN_STATE_ERROR_ACTIVE;
1282+
1283+ /* check if queue can be restarted,
1284+ * up->available_tx_urbs must be protected by the
1285+ * lock
1286+ */
1287+ spin_lock_irqsave(&up->context_lock, flags);
1288+
1289+ if (up->available_tx_urbs > 0)
1290+ netif_wake_queue(up->netdev);
1291+
1292+ spin_unlock_irqrestore(&up->context_lock, flags);
1293+
1294+ return ret;
1295+ default:
1296+ return -EOPNOTSUPP;
1297+ }
1298+}
1299+
1300+/* Probe the device, reset it and gather general device information */
1301+static int ucan_probe(struct usb_interface *intf,
1302+ const struct usb_device_id *id)
1303+{
1304+ int ret;
1305+ int i;
1306+ u32 protocol_version;
1307+ struct usb_device *udev;
1308+ struct net_device *netdev;
1309+ struct usb_host_interface *iface_desc;
1310+ struct ucan_priv *up;
1311+ struct usb_endpoint_descriptor *ep;
1312+ u16 in_ep_size;
1313+ u16 out_ep_size;
1314+ u8 in_ep_addr;
1315+ u8 out_ep_addr;
1316+ union ucan_ctl_payload *ctl_msg_buffer;
1317+ char firmware_str[sizeof(union ucan_ctl_payload) + 1];
1318+
1319+ udev = interface_to_usbdev(intf);
1320+
1321+ /* Stage 1 - Interface Parsing
1322+ * ---------------------------
1323+ *
1324+ * Identifie the device USB interface descriptor and its
1325+ * endpoints. Probing is aborted on errors.
1326+ */
1327+
1328+ /* check if the interface is sane */
1329+ iface_desc = intf->cur_altsetting;
1330+ if (!iface_desc)
1331+ return -ENODEV;
1332+
1333+ dev_info(&udev->dev,
1334+ "%s: probing device on interface #%d\n",
1335+ UCAN_DRIVER_NAME,
1336+ iface_desc->desc.bInterfaceNumber);
1337+
1338+ /* interface sanity check */
1339+ if (iface_desc->desc.bNumEndpoints != 2) {
1340+ dev_err(&udev->dev,
1341+ "%s: invalid EP count (%d)",
1342+ UCAN_DRIVER_NAME, iface_desc->desc.bNumEndpoints);
1343+ goto err_firmware_needs_update;
1344+ }
1345+
1346+ /* check interface endpoints */
1347+ in_ep_addr = 0;
1348+ out_ep_addr = 0;
1349+ in_ep_size = 0;
1350+ out_ep_size = 0;
1351+ for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
1352+ ep = &iface_desc->endpoint[i].desc;
1353+
1354+ if (((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != 0) &&
1355+ ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
1356+ USB_ENDPOINT_XFER_BULK)) {
1357+ /* In Endpoint */
1358+ in_ep_addr = ep->bEndpointAddress;
1359+ in_ep_addr &= USB_ENDPOINT_NUMBER_MASK;
1360+ in_ep_size = le16_to_cpu(ep->wMaxPacketSize);
1361+ } else if (((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ==
1362+ 0) &&
1363+ ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
1364+ USB_ENDPOINT_XFER_BULK)) {
1365+ /* Out Endpoint */
1366+ out_ep_addr = ep->bEndpointAddress;
1367+ out_ep_addr &= USB_ENDPOINT_NUMBER_MASK;
1368+ out_ep_size = le16_to_cpu(ep->wMaxPacketSize);
1369+ }
1370+ }
1371+
1372+ /* check if interface is sane */
1373+ if (!in_ep_addr || !out_ep_addr) {
1374+ dev_err(&udev->dev, "%s: invalid endpoint configuration\n",
1375+ UCAN_DRIVER_NAME);
1376+ goto err_firmware_needs_update;
1377+ }
1378+ if (in_ep_size < sizeof(struct ucan_message_in)) {
1379+ dev_err(&udev->dev, "%s: invalid in_ep MaxPacketSize\n",
1380+ UCAN_DRIVER_NAME);
1381+ goto err_firmware_needs_update;
1382+ }
1383+ if (out_ep_size < sizeof(struct ucan_message_out)) {
1384+ dev_err(&udev->dev, "%s: invalid out_ep MaxPacketSize\n",
1385+ UCAN_DRIVER_NAME);
1386+ goto err_firmware_needs_update;
1387+ }
1388+
1389+ /* Stage 2 - Device Identification
1390+ * -------------------------------
1391+ *
1392+ * The device interface seems to be a ucan device. Do further
1393+ * compatibility checks. On error probing is aborted, on
1394+ * success this stage leaves the ctl_msg_buffer with the
1395+ * reported contents of a GET_INFO command (supported
1396+ * bittimings, tx_fifo depth). This information is used in
1397+ * Stage 3 for the final driver initialisation.
1398+ */
1399+
1400+ /* Prepare Memory for control transferes */
1401+ ctl_msg_buffer = devm_kzalloc(&udev->dev,
1402+ sizeof(union ucan_ctl_payload),
1403+ GFP_KERNEL);
1404+ if (!ctl_msg_buffer) {
1405+ dev_err(&udev->dev,
1406+ "%s: failed to allocate control pipe memory\n",
1407+ UCAN_DRIVER_NAME);
1408+ return -ENOMEM;
1409+ }
1410+
1411+ /* get protocol version
1412+ *
1413+ * note: ucan_ctrl_command_* wrappers cannot be used yet
1414+ * because `up` is initialised in Stage 3
1415+ */
1416+ ret = usb_control_msg(udev,
1417+ usb_rcvctrlpipe(udev, 0),
1418+ UCAN_COMMAND_GET,
1419+ USB_DIR_IN | USB_TYPE_VENDOR |
1420+ USB_RECIP_INTERFACE,
1421+ UCAN_COMMAND_GET_PROTOCOL_VERSION,
1422+ iface_desc->desc.bInterfaceNumber,
1423+ ctl_msg_buffer,
1424+ sizeof(union ucan_ctl_payload),
1425+ UCAN_USB_CTL_PIPE_TIMEOUT);
1426+
1427+ /* older firmware version do not support this command - those
1428+ * are not supported by this drive
1429+ */
1430+ if (ret != 4) {
1431+ dev_err(&udev->dev,
1432+ "%s: could not read protocol version, ret=%d\n",
1433+ UCAN_DRIVER_NAME, ret);
1434+ if (ret >= 0)
1435+ ret = -EINVAL;
1436+ goto err_firmware_needs_update;
1437+ }
1438+
1439+ /* this driver currently supports protocol version 3 only */
1440+ protocol_version =
1441+ le32_to_cpu(ctl_msg_buffer->cmd_get_protocol_version.version);
1442+ if (protocol_version < UCAN_PROTOCOL_VERSION_MIN ||
1443+ protocol_version > UCAN_PROTOCOL_VERSION_MAX) {
1444+ dev_err(&udev->dev,
1445+ "%s: device protocol version %d is not supported\n",
1446+ UCAN_DRIVER_NAME, protocol_version);
1447+ goto err_firmware_needs_update;
1448+ }
1449+
1450+ /* request the device information and store it in ctl_msg_buffer
1451+ *
1452+ * note: ucan_ctrl_command_* wrappers connot be used yet
1453+ * because `up` is initialised in Stage 3
1454+ */
1455+ ret = usb_control_msg(udev,
1456+ usb_rcvctrlpipe(udev, 0),
1457+ UCAN_COMMAND_GET,
1458+ USB_DIR_IN | USB_TYPE_VENDOR |
1459+ USB_RECIP_INTERFACE,
1460+ UCAN_COMMAND_GET_INFO,
1461+ iface_desc->desc.bInterfaceNumber,
1462+ ctl_msg_buffer,
1463+ sizeof(ctl_msg_buffer->cmd_get_device_info),
1464+ UCAN_USB_CTL_PIPE_TIMEOUT);
1465+
1466+ if (ret < 0) {
1467+ dev_err(&udev->dev, "%s: failed to retrieve device info\n",
1468+ UCAN_DRIVER_NAME);
1469+ goto err_firmware_needs_update;
1470+ }
1471+ if (ret < sizeof(ctl_msg_buffer->cmd_get_device_info)) {
1472+ dev_err(&udev->dev, "%s: device reported invalid device info\n",
1473+ UCAN_DRIVER_NAME);
1474+ goto err_firmware_needs_update;
1475+ }
1476+ if (ctl_msg_buffer->cmd_get_device_info.tx_fifo == 0) {
1477+ dev_err(&udev->dev,
1478+ "%s: device reported invalid tx-fifo size\n",
1479+ UCAN_DRIVER_NAME);
1480+ goto err_firmware_needs_update;
1481+ }
1482+
1483+ /* Stage 3 - Driver Initialisation
1484+ * -------------------------------
1485+ *
1486+ * Register device to Linux, prepare private structures and
1487+ * reset the device.
1488+ */
1489+
1490+ /* allocate driver resources */
1491+ netdev = alloc_candev(sizeof(struct ucan_priv),
1492+ ctl_msg_buffer->cmd_get_device_info.tx_fifo);
1493+ if (!netdev) {
1494+ dev_err(&udev->dev,
1495+ "%s: cannot allocate candev\n", UCAN_DRIVER_NAME);
1496+ return -ENOMEM;
1497+ }
1498+
1499+ up = netdev_priv(netdev);
1500+
1501+ /* initialze data */
1502+ up->udev = udev;
1503+ up->intf = intf;
1504+ up->netdev = netdev;
1505+ up->intf_index = iface_desc->desc.bInterfaceNumber;
1506+ up->in_ep_addr = in_ep_addr;
1507+ up->out_ep_addr = out_ep_addr;
1508+ up->in_ep_size = in_ep_size;
1509+ up->ctl_msg_buffer = ctl_msg_buffer;
1510+ up->context_array = NULL;
1511+ up->available_tx_urbs = 0;
1512+
1513+ up->can.state = CAN_STATE_STOPPED;
1514+ up->can.bittiming_const = &up->device_info.bittiming_const;
1515+ up->can.do_set_bittiming = ucan_set_bittiming;
1516+ up->can.do_set_mode = &ucan_set_mode;
1517+ spin_lock_init(&up->context_lock);
1518+ spin_lock_init(&up->echo_skb_lock);
1519+ netdev->netdev_ops = &ucan_netdev_ops;
1520+
1521+ usb_set_intfdata(intf, up);
1522+ SET_NETDEV_DEV(netdev, &intf->dev);
1523+
1524+ /* parse device information
1525+ * the data retrieved in Stage 2 is still available in
1526+ * up->ctl_msg_buffer
1527+ */
1528+ ucan_parse_device_info(up, &ctl_msg_buffer->cmd_get_device_info);
1529+
1530+ /* just print some device information - if available */
1531+ ret = ucan_device_request_in(up, UCAN_DEVICE_GET_FW_STRING, 0,
1532+ sizeof(union ucan_ctl_payload));
1533+ if (ret > 0) {
1534+ /* copy string while ensuring zero terminiation */
1535+ strncpy(firmware_str, up->ctl_msg_buffer->raw,
1536+ sizeof(union ucan_ctl_payload));
1537+ firmware_str[sizeof(union ucan_ctl_payload)] = '\0';
1538+ } else {
1539+ strcpy(firmware_str, "unknown");
1540+ }
1541+
1542+ /* device is compatible, reset it */
1543+ ret = ucan_ctrl_command_out(up, UCAN_COMMAND_RESET, 0, 0);
1544+ if (ret < 0)
1545+ goto err_free_candev;
1546+
1547+ init_usb_anchor(&up->rx_urbs);
1548+ init_usb_anchor(&up->tx_urbs);
1549+
1550+ up->can.state = CAN_STATE_STOPPED;
1551+
1552+ /* register the device */
1553+ ret = register_candev(netdev);
1554+ if (ret)
1555+ goto err_free_candev;
1556+
1557+ /* initialisation complete, log device info */
1558+ netdev_info(up->netdev, "registered device\n");
1559+ netdev_info(up->netdev, "firmware string: %s\n", firmware_str);
1560+
1561+ /* success */
1562+ return 0;
1563+
1564+err_free_candev:
1565+ free_candev(netdev);
1566+ return ret;
1567+
1568+err_firmware_needs_update:
1569+ dev_err(&udev->dev,
1570+ "%s: probe failed; try to update the device firmware\n",
1571+ UCAN_DRIVER_NAME);
1572+ return -ENODEV;
1573+}
1574+
1575+/* disconnect the device */
1576+static void ucan_disconnect(struct usb_interface *intf)
1577+{
1578+ struct usb_device *udev;
1579+ struct ucan_priv *up = usb_get_intfdata(intf);
1580+
1581+ udev = interface_to_usbdev(intf);
1582+
1583+ usb_set_intfdata(intf, NULL);
1584+
1585+ if (up) {
1586+ unregister_netdev(up->netdev);
1587+ free_candev(up->netdev);
1588+ }
1589+}
1590+
1591+static struct usb_device_id ucan_table[] = {
1592+ /* Mule (soldered onto compute modules) */
1593+ {USB_DEVICE_INTERFACE_NUMBER(0x2294, 0x425a, 0)},
1594+ /* Seal (standalone USB stick) */
1595+ {USB_DEVICE_INTERFACE_NUMBER(0x2294, 0x425b, 0)},
1596+ {} /* Terminating entry */
1597+};
1598+
1599+MODULE_DEVICE_TABLE(usb, ucan_table);
1600+/* driver callbacks */
1601+static struct usb_driver ucan_driver = {
1602+ .name = UCAN_DRIVER_NAME,
1603+ .probe = ucan_probe,
1604+ .disconnect = ucan_disconnect,
1605+ .id_table = ucan_table,
1606+};
1607+
1608+module_usb_driver(ucan_driver);
1609+
1610+MODULE_LICENSE("GPL v2");
1611+MODULE_AUTHOR("Martin Elshuber <martin.elshuber@theobroma-systems.com>");
1612+MODULE_AUTHOR("Jakob Unterwurzacher <jakob.unterwurzacher@theobroma-systems.com>");
1613+MODULE_DESCRIPTION("Driver for Theobroma Systems UCAN devices");
drivers/net/can/xilinx_can.c+368−144
@@ -2,7 +2,7 @@ · 1 unmodified line
22 *
33 * Copyright (C) 2012 - 2014 Xilinx, Inc.
44 * Copyright (C) 2009 PetaLogix. All rights reserved.
5− * Copyright (C) 2017 Sandvik Mining and Construction Oy
5+ * Copyright (C) 2017 - 2018 Sandvik Mining and Construction Oy
66 *
77 * Description:
88 * This driver is developed for Axi CAN IP and for Zynq CANPS Controller.
@@ -51,16 +51,34 @@ enum xcan_reg { · 42 unmodified lines
5151 XCAN_ISR_OFFSET = 0x1C, /* Interrupt status */
5252 XCAN_IER_OFFSET = 0x20, /* Interrupt enable */
5353 XCAN_ICR_OFFSET = 0x24, /* Interrupt clear */
54− XCAN_TXFIFO_ID_OFFSET = 0x30,/* TX FIFO ID */
55− XCAN_TXFIFO_DLC_OFFSET = 0x34, /* TX FIFO DLC */
56− XCAN_TXFIFO_DW1_OFFSET = 0x38, /* TX FIFO Data Word 1 */
57− XCAN_TXFIFO_DW2_OFFSET = 0x3C, /* TX FIFO Data Word 2 */
58− XCAN_RXFIFO_ID_OFFSET = 0x50, /* RX FIFO ID */
59− XCAN_RXFIFO_DLC_OFFSET = 0x54, /* RX FIFO DLC */
60− XCAN_RXFIFO_DW1_OFFSET = 0x58, /* RX FIFO Data Word 1 */
61− XCAN_RXFIFO_DW2_OFFSET = 0x5C, /* RX FIFO Data Word 2 */
54+
55+ /* not on CAN FD cores */
56+ XCAN_TXFIFO_OFFSET = 0x30, /* TX FIFO base */
57+ XCAN_RXFIFO_OFFSET = 0x50, /* RX FIFO base */
58+ XCAN_AFR_OFFSET = 0x60, /* Acceptance Filter */
59+
60+ /* only on CAN FD cores */
61+ XCAN_TRR_OFFSET = 0x0090, /* TX Buffer Ready Request */
62+ XCAN_AFR_EXT_OFFSET = 0x00E0, /* Acceptance Filter */
63+ XCAN_FSR_OFFSET = 0x00E8, /* RX FIFO Status */
64+ XCAN_TXMSG_BASE_OFFSET = 0x0100, /* TX Message Space */
65+ XCAN_RXMSG_BASE_OFFSET = 0x1100, /* RX Message Space */
6266 };
6367
68+#define XCAN_FRAME_ID_OFFSET(frame_base) ((frame_base) + 0x00)
69+#define XCAN_FRAME_DLC_OFFSET(frame_base) ((frame_base) + 0x04)
70+#define XCAN_FRAME_DW1_OFFSET(frame_base) ((frame_base) + 0x08)
71+#define XCAN_FRAME_DW2_OFFSET(frame_base) ((frame_base) + 0x0C)
72+
73+#define XCAN_CANFD_FRAME_SIZE 0x48
74+#define XCAN_TXMSG_FRAME_OFFSET(n) (XCAN_TXMSG_BASE_OFFSET + \
75+ XCAN_CANFD_FRAME_SIZE * (n))
76+#define XCAN_RXMSG_FRAME_OFFSET(n) (XCAN_RXMSG_BASE_OFFSET + \
77+ XCAN_CANFD_FRAME_SIZE * (n))
78+
79+/* the single TX mailbox used by this driver on CAN FD HW */
80+#define XCAN_TX_MAILBOX_IDX 0
81+
6482 /* CAN register bit masks - XCAN_<REG>_<BIT>_MASK */
6583 #define XCAN_SRR_CEN_MASK 0x00000002 /* CAN enable */
6684 #define XCAN_SRR_RESET_MASK 0x00000001 /* Soft Reset the CAN core */
@@ -70,6 +88,9 @@ enum xcan_reg { · 3 unmodified lines
7088 #define XCAN_BTR_SJW_MASK 0x00000180 /* Synchronous jump width */
7189 #define XCAN_BTR_TS2_MASK 0x00000070 /* Time segment 2 */
7290 #define XCAN_BTR_TS1_MASK 0x0000000F /* Time segment 1 */
91+#define XCAN_BTR_SJW_MASK_CANFD 0x000F0000 /* Synchronous jump width */
92+#define XCAN_BTR_TS2_MASK_CANFD 0x00000F00 /* Time segment 2 */
93+#define XCAN_BTR_TS1_MASK_CANFD 0x0000003F /* Time segment 1 */
7394 #define XCAN_ECR_REC_MASK 0x0000FF00 /* Receive error counter */
7495 #define XCAN_ECR_TEC_MASK 0x000000FF /* Transmit error counter */
7596 #define XCAN_ESR_ACKER_MASK 0x00000010 /* ACK error */
@@ -83,6 +104,7 @@ enum xcan_reg { · 7 unmodified lines
83104 #define XCAN_SR_NORMAL_MASK 0x00000008 /* Normal mode */
84105 #define XCAN_SR_LBACK_MASK 0x00000002 /* Loop back mode */
85106 #define XCAN_SR_CONFIG_MASK 0x00000001 /* Configuration mode */
107+#define XCAN_IXR_RXMNF_MASK 0x00020000 /* RX match not finished */
86108 #define XCAN_IXR_TXFEMP_MASK 0x00004000 /* TX FIFO Empty */
87109 #define XCAN_IXR_WKUP_MASK 0x00000800 /* Wake up interrupt */
88110 #define XCAN_IXR_SLP_MASK 0x00000400 /* Sleep interrupt */
@@ -100,15 +122,15 @@ enum xcan_reg { · 11 unmodified lines
100122 #define XCAN_IDR_ID2_MASK 0x0007FFFE /* Extended message ident */
101123 #define XCAN_IDR_RTR_MASK 0x00000001 /* Remote TX request */
102124 #define XCAN_DLCR_DLC_MASK 0xF0000000 /* Data length code */
103−
104−#define XCAN_INTR_ALL (XCAN_IXR_TXOK_MASK | XCAN_IXR_BSOFF_MASK |\
105− XCAN_IXR_WKUP_MASK | XCAN_IXR_SLP_MASK | \
106− XCAN_IXR_RXNEMP_MASK | XCAN_IXR_ERROR_MASK | \
107− XCAN_IXR_RXOFLW_MASK | XCAN_IXR_ARBLST_MASK)
125+#define XCAN_FSR_FL_MASK 0x00003F00 /* RX Fill Level */
126+#define XCAN_FSR_IRI_MASK 0x00000080 /* RX Increment Read Index */
127+#define XCAN_FSR_RI_MASK 0x0000001F /* RX Read Index */
108128
109129 /* CAN register bit shift - XCAN_<REG>_<BIT>_SHIFT */
110130 #define XCAN_BTR_SJW_SHIFT 7 /* Synchronous jump width */
111131 #define XCAN_BTR_TS2_SHIFT 4 /* Time segment 2 */
132+#define XCAN_BTR_SJW_SHIFT_CANFD 16 /* Synchronous jump width */
133+#define XCAN_BTR_TS2_SHIFT_CANFD 8 /* Time segment 2 */
112134 #define XCAN_IDR_ID1_SHIFT 21 /* Standard Messg Identifier */
113135 #define XCAN_IDR_ID2_SHIFT 1 /* Extended Message Identifier */
114136 #define XCAN_DLCR_DLC_SHIFT 28 /* Data length code */
@@ -118,6 +140,27 @@ enum xcan_reg { · 3 unmodified lines
118140 #define XCAN_FRAME_MAX_DATA_LEN 8
119141 #define XCAN_TIMEOUT (1 * HZ)
120142
143+/* TX-FIFO-empty interrupt available */
144+#define XCAN_FLAG_TXFEMP 0x0001
145+/* RX Match Not Finished interrupt available */
146+#define XCAN_FLAG_RXMNF 0x0002
147+/* Extended acceptance filters with control at 0xE0 */
148+#define XCAN_FLAG_EXT_FILTERS 0x0004
149+/* TX mailboxes instead of TX FIFO */
150+#define XCAN_FLAG_TX_MAILBOXES 0x0008
151+/* RX FIFO with each buffer in separate registers at 0x1100
152+ * instead of the regular FIFO at 0x50
153+ */
154+#define XCAN_FLAG_RX_FIFO_MULTI 0x0010
155+
156+struct xcan_devtype_data {
157+ unsigned int flags;
158+ const struct can_bittiming_const *bittiming_const;
159+ const char *bus_clk_name;
160+ unsigned int btr_ts2_shift;
161+ unsigned int btr_sjw_shift;
162+};
163+
121164 /**
122165 * struct xcan_priv - This definition define CAN driver instance
123166 * @can: CAN private data structure.
@@ -133,6 +176,7 @@ enum xcan_reg { · 9 unmodified lines
133176 * @irq_flags: For request_irq()
134177 * @bus_clk: Pointer to struct clk
135178 * @can_clk: Pointer to struct clk
179+ * @devtype: Device type specific constants
136180 */
137181 struct xcan_priv {
138182 struct can_priv can;
@@ -149,6 +193,7 @@ struct xcan_priv { · 10 unmodified lines
149193 unsigned long irq_flags;
150194 struct clk *bus_clk;
151195 struct clk *can_clk;
196+ struct xcan_devtype_data devtype;
152197 };
153198
154199 /* CAN Bittiming constants as per Xilinx CAN specs */
@@ -164,9 +209,16 @@ static const struct can_bittiming_const xcan_bittiming_const = { · 9 unmodified lines
164209 .brp_inc = 1,
165210 };
166211
167−#define XCAN_CAP_WATERMARK 0x0001
168−struct xcan_devtype_data {
169− unsigned int caps;
212+static const struct can_bittiming_const xcan_bittiming_const_canfd = {
213+ .name = DRIVER_NAME,
214+ .tseg1_min = 1,
215+ .tseg1_max = 64,
216+ .tseg2_min = 1,
217+ .tseg2_max = 16,
218+ .sjw_max = 16,
219+ .brp_min = 1,
220+ .brp_max = 256,
221+ .brp_inc = 1,
170222 };
171223
172224 /**
@@ -223,6 +275,23 @@ static u32 xcan_read_reg_be(const struct xcan_priv *priv, enum xcan_reg reg) · 50 unmodified lines
223275 return ioread32be(priv->reg_base + reg);
224276 }
225277
278+/**
279+ * xcan_rx_int_mask - Get the mask for the receive interrupt
280+ * @priv: Driver private data structure
281+ *
282+ * Return: The receive interrupt mask used by the driver on this HW
283+ */
284+static u32 xcan_rx_int_mask(const struct xcan_priv *priv)
285+{
286+ /* RXNEMP is better suited for our use case as it cannot be cleared
287+ * while the FIFO is non-empty, but CAN FD HW does not have it
288+ */
289+ if (priv->devtype.flags & XCAN_FLAG_RX_FIFO_MULTI)
290+ return XCAN_IXR_RXOK_MASK;
291+ else
292+ return XCAN_IXR_RXNEMP_MASK;
293+}
294+
226295 /**
227296 * set_reset_mode - Resets the CAN device mode
228297 * @ndev: Pointer to net_device structure
@@ -287,10 +356,10 @@ static int xcan_set_bittiming(struct net_device *ndev) · 58 unmodified lines
287356 btr1 = (bt->prop_seg + bt->phase_seg1 - 1);
288357
289358 /* Setting Time Segment 2 in BTR Register */
290− btr1 |= (bt->phase_seg2 - 1) << XCAN_BTR_TS2_SHIFT;
359+ btr1 |= (bt->phase_seg2 - 1) << priv->devtype.btr_ts2_shift;
291360
292361 /* Setting Synchronous jump width in BTR Register */
293− btr1 |= (bt->sjw - 1) << XCAN_BTR_SJW_SHIFT;
362+ btr1 |= (bt->sjw - 1) << priv->devtype.btr_sjw_shift;
294363
295364 priv->write_reg(priv, XCAN_BRPR_OFFSET, btr0);
296365 priv->write_reg(priv, XCAN_BTR_OFFSET, btr1);
@@ -318,6 +387,7 @@ static int xcan_chip_start(struct net_device *ndev) · 21 unmodified lines
318387 u32 reg_msr, reg_sr_mask;
319388 int err;
320389 unsigned long timeout;
390+ u32 ier;
321391
322392 /* Check if it is in reset mode */
323393 err = set_reset_mode(ndev);
@@ -329,7 +399,15 @@ static int xcan_chip_start(struct net_device *ndev) · 5 unmodified lines
329399 return err;
330400
331401 /* Enable interrupts */
332− priv->write_reg(priv, XCAN_IER_OFFSET, XCAN_INTR_ALL);
402+ ier = XCAN_IXR_TXOK_MASK | XCAN_IXR_BSOFF_MASK |
403+ XCAN_IXR_WKUP_MASK | XCAN_IXR_SLP_MASK |
404+ XCAN_IXR_ERROR_MASK | XCAN_IXR_RXOFLW_MASK |
405+ XCAN_IXR_ARBLST_MASK | xcan_rx_int_mask(priv);
406+
407+ if (priv->devtype.flags & XCAN_FLAG_RXMNF)
408+ ier |= XCAN_IXR_RXMNF_MASK;
409+
410+ priv->write_reg(priv, XCAN_IER_OFFSET, ier);
333411
334412 /* Check whether it is loopback mode or normal mode */
335413 if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK) {
@@ -340,6 +418,12 @@ static int xcan_chip_start(struct net_device *ndev) · 4 unmodified lines
340418 reg_sr_mask = XCAN_SR_NORMAL_MASK;
341419 }
342420
421+ /* enable the first extended filter, if any, as cores with extended
422+ * filtering default to non-receipt if all filters are disabled
423+ */
424+ if (priv->devtype.flags & XCAN_FLAG_EXT_FILTERS)
425+ priv->write_reg(priv, XCAN_AFR_EXT_OFFSET, 0x00000001);
426+
343427 priv->write_reg(priv, XCAN_MSR_OFFSET, reg_msr);
344428 priv->write_reg(priv, XCAN_SRR_OFFSET, XCAN_SRR_CEN_MASK);
345429
@@ -390,34 +474,15 @@ static int xcan_do_set_mode(struct net_device *ndev, enum can_mode mode) · 44 unmodified lines
390474 }
391475
392476 /**
393− * xcan_start_xmit - Starts the transmission
394− * @skb: sk_buff pointer that contains data to be Txed
395− * @ndev: Pointer to net_device structure
396− *
397− * This function is invoked from upper layers to initiate transmission. This
398− * function uses the next available free txbuff and populates their fields to
399− * start the transmission.
400− *
401− * Return: 0 on success and failure value on error
477+ * xcan_write_frame - Write a frame to HW
478+ * @skb: sk_buff pointer that contains data to be Txed
479+ * @frame_offset: Register offset to write the frame to
402480 */
403−static int xcan_start_xmit(struct sk_buff *skb, struct net_device *ndev)
481+static void xcan_write_frame(struct xcan_priv *priv, struct sk_buff *skb,
482+ int frame_offset)
404483 {
405− struct xcan_priv *priv = netdev_priv(ndev);
406− struct net_device_stats *stats = &ndev->stats;
407− struct can_frame *cf = (struct can_frame *)skb->data;
408484 u32 id, dlc, data[2] = {0, 0};
409− unsigned long flags;
410−
411− if (can_dropped_invalid_skb(ndev, skb))
412− return NETDEV_TX_OK;
413−
414− /* Check if the TX buffer is full */
415− if (unlikely(priv->read_reg(priv, XCAN_SR_OFFSET) &
416− XCAN_SR_TXFLL_MASK)) {
417− netif_stop_queue(ndev);
418− netdev_err(ndev, "BUG!, TX FIFO full when queue awake!\n");
419− return NETDEV_TX_BUSY;
420− }
485+ struct can_frame *cf = (struct can_frame *)skb->data;
421486
422487 /* Watch carefully on the bit sequence */
423488 if (cf->can_id & CAN_EFF_FLAG) {
@@ -453,24 +518,44 @@ static int xcan_start_xmit(struct sk_buff *skb, struct net_device *ndev) · 29 unmodified lines
453518 if (cf->can_dlc > 4)
454519 data[1] = be32_to_cpup((__be32 *)(cf->data + 4));
455520
521+ priv->write_reg(priv, XCAN_FRAME_ID_OFFSET(frame_offset), id);
522+ /* If the CAN frame is RTR frame this write triggers transmission
523+ * (not on CAN FD)
524+ */
525+ priv->write_reg(priv, XCAN_FRAME_DLC_OFFSET(frame_offset), dlc);
526+ if (!(cf->can_id & CAN_RTR_FLAG)) {
527+ priv->write_reg(priv, XCAN_FRAME_DW1_OFFSET(frame_offset),
528+ data[0]);
529+ /* If the CAN frame is Standard/Extended frame this
530+ * write triggers transmission (not on CAN FD)
531+ */
532+ priv->write_reg(priv, XCAN_FRAME_DW2_OFFSET(frame_offset),
533+ data[1]);
534+ }
535+}
536+
537+/**
538+ * xcan_start_xmit_fifo - Starts the transmission (FIFO mode)
539+ *
540+ * Return: 0 on success, -ENOSPC if FIFO is full.
541+ */
542+static int xcan_start_xmit_fifo(struct sk_buff *skb, struct net_device *ndev)
543+{
544+ struct xcan_priv *priv = netdev_priv(ndev);
545+ unsigned long flags;
546+
547+ /* Check if the TX buffer is full */
548+ if (unlikely(priv->read_reg(priv, XCAN_SR_OFFSET) &
549+ XCAN_SR_TXFLL_MASK))
550+ return -ENOSPC;
551+
456552 can_put_echo_skb(skb, ndev, priv->tx_head % priv->tx_max);
457553
458554 spin_lock_irqsave(&priv->tx_lock, flags);
459555
460556 priv->tx_head++;
461557
462− /* Write the Frame to Xilinx CAN TX FIFO */
463− priv->write_reg(priv, XCAN_TXFIFO_ID_OFFSET, id);
464− /* If the CAN frame is RTR frame this write triggers tranmission */
465− priv->write_reg(priv, XCAN_TXFIFO_DLC_OFFSET, dlc);
466− if (!(cf->can_id & CAN_RTR_FLAG)) {
467− priv->write_reg(priv, XCAN_TXFIFO_DW1_OFFSET, data[0]);
468− /* If the CAN frame is Standard/Extended frame this
469− * write triggers tranmission
470− */
471− priv->write_reg(priv, XCAN_TXFIFO_DW2_OFFSET, data[1]);
472− stats->tx_bytes += cf->can_dlc;
473− }
558+ xcan_write_frame(priv, skb, XCAN_TXFIFO_OFFSET);
474559
475560 /* Clear TX-FIFO-empty interrupt for xcan_tx_interrupt() */
476561 if (priv->tx_max > 1)
@@ -482,6 +567,70 @@ static int xcan_start_xmit(struct sk_buff *skb, struct net_device *ndev) · 5 unmodified lines
482567
483568 spin_unlock_irqrestore(&priv->tx_lock, flags);
484569
570+ return 0;
571+}
572+
573+/**
574+ * xcan_start_xmit_mailbox - Starts the transmission (mailbox mode)
575+ *
576+ * Return: 0 on success, -ENOSPC if there is no space
577+ */
578+static int xcan_start_xmit_mailbox(struct sk_buff *skb, struct net_device *ndev)
579+{
580+ struct xcan_priv *priv = netdev_priv(ndev);
581+ unsigned long flags;
582+
583+ if (unlikely(priv->read_reg(priv, XCAN_TRR_OFFSET) &
584+ BIT(XCAN_TX_MAILBOX_IDX)))
585+ return -ENOSPC;
586+
587+ can_put_echo_skb(skb, ndev, 0);
588+
589+ spin_lock_irqsave(&priv->tx_lock, flags);
590+
591+ priv->tx_head++;
592+
593+ xcan_write_frame(priv, skb,
594+ XCAN_TXMSG_FRAME_OFFSET(XCAN_TX_MAILBOX_IDX));
595+
596+ /* Mark buffer as ready for transmit */
597+ priv->write_reg(priv, XCAN_TRR_OFFSET, BIT(XCAN_TX_MAILBOX_IDX));
598+
599+ netif_stop_queue(ndev);
600+
601+ spin_unlock_irqrestore(&priv->tx_lock, flags);
602+
603+ return 0;
604+}
605+
606+/**
607+ * xcan_start_xmit - Starts the transmission
608+ * @skb: sk_buff pointer that contains data to be Txed
609+ * @ndev: Pointer to net_device structure
610+ *
611+ * This function is invoked from upper layers to initiate transmission.
612+ *
613+ * Return: NETDEV_TX_OK on success and NETDEV_TX_BUSY when the tx queue is full
614+ */
615+static int xcan_start_xmit(struct sk_buff *skb, struct net_device *ndev)
616+{
617+ struct xcan_priv *priv = netdev_priv(ndev);
618+ int ret;
619+
620+ if (can_dropped_invalid_skb(ndev, skb))
621+ return NETDEV_TX_OK;
622+
623+ if (priv->devtype.flags & XCAN_FLAG_TX_MAILBOXES)
624+ ret = xcan_start_xmit_mailbox(skb, ndev);
625+ else
626+ ret = xcan_start_xmit_fifo(skb, ndev);
627+
628+ if (ret < 0) {
629+ netdev_err(ndev, "BUG!, TX full when queue awake!\n");
630+ netif_stop_queue(ndev);
631+ return NETDEV_TX_BUSY;
632+ }
633+
485634 return NETDEV_TX_OK;
486635 }
487636
@@ -489,13 +638,14 @@ static int xcan_start_xmit(struct sk_buff *skb, struct net_device *ndev) · 1 unmodified line
489638 * xcan_rx - Is called from CAN isr to complete the received
490639 * frame processing
491640 * @ndev: Pointer to net_device structure
641+ * @frame_base: Register offset to the frame to be read
492642 *
493643 * This function is invoked from the CAN isr(poll) to process the Rx frames. It
494644 * does minimal processing and invokes "netif_receive_skb" to complete further
495645 * processing.
496646 * Return: 1 on success and 0 on failure.
497647 */
498−static int xcan_rx(struct net_device *ndev)
648+static int xcan_rx(struct net_device *ndev, int frame_base)
499649 {
500650 struct xcan_priv *priv = netdev_priv(ndev);
501651 struct net_device_stats *stats = &ndev->stats;
@@ -510,9 +660,9 @@ static int xcan_rx(struct net_device *ndev) · 8 unmodified lines
510660 }
511661
512662 /* Read a frame from Xilinx zynq CANPS */
513− id_xcan = priv->read_reg(priv, XCAN_RXFIFO_ID_OFFSET);
514− dlc = priv->read_reg(priv, XCAN_RXFIFO_DLC_OFFSET) >>
515− XCAN_DLCR_DLC_SHIFT;
663+ id_xcan = priv->read_reg(priv, XCAN_FRAME_ID_OFFSET(frame_base));
664+ dlc = priv->read_reg(priv, XCAN_FRAME_DLC_OFFSET(frame_base)) >>
665+ XCAN_DLCR_DLC_SHIFT;
516666
517667 /* Change Xilinx CAN data length format to socketCAN data format */
518668 cf->can_dlc = get_can_dlc(dlc);
@@ -535,8 +685,8 @@ static int xcan_rx(struct net_device *ndev) · 16 unmodified lines
535685 }
536686
537687 /* DW1/DW2 must always be read to remove message from RXFIFO */
538− data[0] = priv->read_reg(priv, XCAN_RXFIFO_DW1_OFFSET);
539− data[1] = priv->read_reg(priv, XCAN_RXFIFO_DW2_OFFSET);
688+ data[0] = priv->read_reg(priv, XCAN_FRAME_DW1_OFFSET(frame_base));
689+ data[1] = priv->read_reg(priv, XCAN_FRAME_DW2_OFFSET(frame_base));
540690
541691 if (!(cf->can_id & CAN_RTR_FLAG)) {
542692 /* Change Xilinx CAN data format to socketCAN data format */
@@ -594,39 +744,19 @@ static void xcan_set_error_state(struct net_device *ndev, · 51 unmodified lines
594744 u32 ecr = priv->read_reg(priv, XCAN_ECR_OFFSET);
595745 u32 txerr = ecr & XCAN_ECR_TEC_MASK;
596746 u32 rxerr = (ecr & XCAN_ECR_REC_MASK) >> XCAN_ESR_REC_SHIFT;
747+ enum can_state tx_state = txerr >= rxerr ? new_state : 0;
748+ enum can_state rx_state = txerr <= rxerr ? new_state : 0;
749+
750+ /* non-ERROR states are handled elsewhere */
751+ if (WARN_ON(new_state > CAN_STATE_ERROR_PASSIVE))
752+ return;
597753
598− priv->can.state = new_state;
754+ can_change_state(ndev, cf, tx_state, rx_state);
599755
600756 if (cf) {
601− cf->can_id |= CAN_ERR_CRTL;
602757 cf->data[6] = txerr;
603758 cf->data[7] = rxerr;
604759 }
605−
606− switch (new_state) {
607− case CAN_STATE_ERROR_PASSIVE:
608− priv->can.can_stats.error_passive++;
609− if (cf)
610− cf->data[1] = (rxerr > 127) ?
611− CAN_ERR_CRTL_RX_PASSIVE :
612− CAN_ERR_CRTL_TX_PASSIVE;
613− break;
614− case CAN_STATE_ERROR_WARNING:
615− priv->can.can_stats.error_warning++;
616− if (cf)
617− cf->data[1] |= (txerr > rxerr) ?
618− CAN_ERR_CRTL_TX_WARNING :
619− CAN_ERR_CRTL_RX_WARNING;
620− break;
621− case CAN_STATE_ERROR_ACTIVE:
622− if (cf)
623− cf->data[1] |= CAN_ERR_CRTL_ACTIVE;
624− break;
625− default:
626− /* non-ERROR states are handled elsewhere */
627− WARN_ON(1);
628− break;
629− }
630760 }
631761
632762 /**
@@ -703,7 +833,8 @@ static void xcan_err_interrupt(struct net_device *ndev, u32 isr) · 70 unmodified lines
703833 } else {
704834 enum can_state new_state = xcan_current_error_state(ndev);
705835
706− xcan_set_error_state(ndev, new_state, skb ? cf : NULL);
836+ if (new_state != priv->can.state)
837+ xcan_set_error_state(ndev, new_state, skb ? cf : NULL);
707838 }
708839
709840 /* Check for Arbitration lost interrupt */
@@ -725,6 +856,17 @@ static void xcan_err_interrupt(struct net_device *ndev, u32 isr) · 15 unmodified lines
725856 }
726857 }
727858
859+ /* Check for RX Match Not Finished interrupt */
860+ if (isr & XCAN_IXR_RXMNF_MASK) {
861+ stats->rx_dropped++;
862+ stats->rx_errors++;
863+ netdev_err(ndev, "RX match not finished, frame discarded\n");
864+ if (skb) {
865+ cf->can_id |= CAN_ERR_CRTL;
866+ cf->data[1] |= CAN_ERR_CRTL_UNSPEC;
867+ }
868+ }
869+
728870 /* Check for error interrupt */
729871 if (isr & XCAN_IXR_ERROR_MASK) {
730872 if (skb)
@@ -808,6 +950,44 @@ static void xcan_state_interrupt(struct net_device *ndev, u32 isr) · 77 unmodified lines
808950 priv->can.state = CAN_STATE_ERROR_ACTIVE;
809951 }
810952
953+/**
954+ * xcan_rx_fifo_get_next_frame - Get register offset of next RX frame
955+ *
956+ * Return: Register offset of the next frame in RX FIFO.
957+ */
958+static int xcan_rx_fifo_get_next_frame(struct xcan_priv *priv)
959+{
960+ int offset;
961+
962+ if (priv->devtype.flags & XCAN_FLAG_RX_FIFO_MULTI) {
963+ u32 fsr;
964+
965+ /* clear RXOK before the is-empty check so that any newly
966+ * received frame will reassert it without a race
967+ */
968+ priv->write_reg(priv, XCAN_ICR_OFFSET, XCAN_IXR_RXOK_MASK);
969+
970+ fsr = priv->read_reg(priv, XCAN_FSR_OFFSET);
971+
972+ /* check if RX FIFO is empty */
973+ if (!(fsr & XCAN_FSR_FL_MASK))
974+ return -ENOENT;
975+
976+ offset = XCAN_RXMSG_FRAME_OFFSET(fsr & XCAN_FSR_RI_MASK);
977+
978+ } else {
979+ /* check if RX FIFO is empty */
980+ if (!(priv->read_reg(priv, XCAN_ISR_OFFSET) &
981+ XCAN_IXR_RXNEMP_MASK))
982+ return -ENOENT;
983+
984+ /* frames are read from a static offset */
985+ offset = XCAN_RXFIFO_OFFSET;
986+ }
987+
988+ return offset;
989+}
990+
811991 /**
812992 * xcan_rx_poll - Poll routine for rx packets (NAPI)
813993 * @napi: napi structure pointer
@@ -822,14 +1002,24 @@ static int xcan_rx_poll(struct napi_struct *napi, int quota) · 8 unmodified lines
8221002 {
8231003 struct net_device *ndev = napi->dev;
8241004 struct xcan_priv *priv = netdev_priv(ndev);
825− u32 isr, ier;
1005+ u32 ier;
8261006 int work_done = 0;
827−
828− isr = priv->read_reg(priv, XCAN_ISR_OFFSET);
829− while ((isr & XCAN_IXR_RXNEMP_MASK) && (work_done < quota)) {
830− work_done += xcan_rx(ndev);
831− priv->write_reg(priv, XCAN_ICR_OFFSET, XCAN_IXR_RXNEMP_MASK);
832− isr = priv->read_reg(priv, XCAN_ISR_OFFSET);
1007+ int frame_offset;
1008+
1009+ while ((frame_offset = xcan_rx_fifo_get_next_frame(priv)) >= 0 &&
1010+ (work_done < quota)) {
1011+ work_done += xcan_rx(ndev, frame_offset);
1012+
1013+ if (priv->devtype.flags & XCAN_FLAG_RX_FIFO_MULTI)
1014+ /* increment read index */
1015+ priv->write_reg(priv, XCAN_FSR_OFFSET,
1016+ XCAN_FSR_IRI_MASK);
1017+ else
1018+ /* clear rx-not-empty (will actually clear only if
1019+ * empty)
1020+ */
1021+ priv->write_reg(priv, XCAN_ICR_OFFSET,
1022+ XCAN_IXR_RXNEMP_MASK);
8331023 }
8341024
8351025 if (work_done) {
@@ -840,7 +1030,7 @@ static int xcan_rx_poll(struct napi_struct *napi, int quota) · 4 unmodified lines
8401030 if (work_done < quota) {
8411031 napi_complete_done(napi, work_done);
8421032 ier = priv->read_reg(priv, XCAN_IER_OFFSET);
843− ier |= XCAN_IXR_RXNEMP_MASK;
1033+ ier |= xcan_rx_int_mask(priv);
8441034 priv->write_reg(priv, XCAN_IER_OFFSET, ier);
8451035 }
8461036 return work_done;
@@ -908,8 +1098,8 @@ static void xcan_tx_interrupt(struct net_device *ndev, u32 isr) · 61 unmodified lines
9081098 }
9091099
9101100 while (frames_sent--) {
911− can_get_echo_skb(ndev, priv->tx_tail %
912− priv->tx_max);
1101+ stats->tx_bytes += can_get_echo_skb(ndev, priv->tx_tail %
1102+ priv->tx_max);
9131103 priv->tx_tail++;
9141104 stats->tx_packets++;
9151105 }
@@ -939,6 +1129,7 @@ static irqreturn_t xcan_interrupt(int irq, void *dev_id) · 23 unmodified lines
9391129 struct xcan_priv *priv = netdev_priv(ndev);
9401130 u32 isr, ier;
9411131 u32 isr_errors;
1132+ u32 rx_int_mask = xcan_rx_int_mask(priv);
9421133
9431134 /* Get the interrupt status from Xilinx CAN */
9441135 isr = priv->read_reg(priv, XCAN_ISR_OFFSET);
@@ -958,16 +1149,17 @@ static irqreturn_t xcan_interrupt(int irq, void *dev_id) · 13 unmodified lines
9581149
9591150 /* Check for the type of error interrupt and Processing it */
9601151 isr_errors = isr & (XCAN_IXR_ERROR_MASK | XCAN_IXR_RXOFLW_MASK |
961− XCAN_IXR_BSOFF_MASK | XCAN_IXR_ARBLST_MASK);
1152+ XCAN_IXR_BSOFF_MASK | XCAN_IXR_ARBLST_MASK |
1153+ XCAN_IXR_RXMNF_MASK);
9621154 if (isr_errors) {
9631155 priv->write_reg(priv, XCAN_ICR_OFFSET, isr_errors);
9641156 xcan_err_interrupt(ndev, isr);
9651157 }
9661158
9671159 /* Check for the type of receive interrupt and Processing it */
968− if (isr & XCAN_IXR_RXNEMP_MASK) {
1160+ if (isr & rx_int_mask) {
9691161 ier = priv->read_reg(priv, XCAN_IER_OFFSET);
970− ier &= ~XCAN_IXR_RXNEMP_MASK;
1162+ ier &= ~rx_int_mask;
9711163 priv->write_reg(priv, XCAN_IER_OFFSET, ier);
9721164 napi_schedule(&priv->napi);
9731165 }
@@ -1214,13 +1406,35 @@ static const struct dev_pm_ops xcan_dev_pm_ops = { · 240 unmodified lines
12141406 };
12151407
12161408 static const struct xcan_devtype_data xcan_zynq_data = {
1217− .caps = XCAN_CAP_WATERMARK,
1409+ .bittiming_const = &xcan_bittiming_const,
1410+ .btr_ts2_shift = XCAN_BTR_TS2_SHIFT,
1411+ .btr_sjw_shift = XCAN_BTR_SJW_SHIFT,
1412+ .bus_clk_name = "pclk",
1413+};
1414+
1415+static const struct xcan_devtype_data xcan_axi_data = {
1416+ .bittiming_const = &xcan_bittiming_const,
1417+ .btr_ts2_shift = XCAN_BTR_TS2_SHIFT,
1418+ .btr_sjw_shift = XCAN_BTR_SJW_SHIFT,
1419+ .bus_clk_name = "s_axi_aclk",
1420+};
1421+
1422+static const struct xcan_devtype_data xcan_canfd_data = {
1423+ .flags = XCAN_FLAG_EXT_FILTERS |
1424+ XCAN_FLAG_RXMNF |
1425+ XCAN_FLAG_TX_MAILBOXES |
1426+ XCAN_FLAG_RX_FIFO_MULTI,
1427+ .bittiming_const = &xcan_bittiming_const,
1428+ .btr_ts2_shift = XCAN_BTR_TS2_SHIFT_CANFD,
1429+ .btr_sjw_shift = XCAN_BTR_SJW_SHIFT_CANFD,
1430+ .bus_clk_name = "s_axi_aclk",
12181431 };
12191432
12201433 /* Match table for OF platform binding */
12211434 static const struct of_device_id xcan_of_match[] = {
12221435 { .compatible = "xlnx,zynq-can-1.0", .data = &xcan_zynq_data },
1223− { .compatible = "xlnx,axi-can-1.00.a", },
1436+ { .compatible = "xlnx,axi-can-1.00.a", .data = &xcan_axi_data },
1437+ { .compatible = "xlnx,canfd-1.0", .data = &xcan_canfd_data },
12241438 { /* end of list */ },
12251439 };
12261440 MODULE_DEVICE_TABLE(of, xcan_of_match);
@@ -1240,9 +1454,12 @@ static int xcan_probe(struct platform_device *pdev) · 13 unmodified lines
12401454 struct net_device *ndev;
12411455 struct xcan_priv *priv;
12421456 const struct of_device_id *of_id;
1243− int caps = 0;
1457+ const struct xcan_devtype_data *devtype = &xcan_axi_data;
12441458 void __iomem *addr;
1245− int ret, rx_max, tx_max, tx_fifo_depth;
1459+ int ret;
1460+ int rx_max, tx_max;
1461+ int hw_tx_max, hw_rx_max;
1462+ const char *hw_tx_max_property;
12461463
12471464 /* Get the virtual base address for the device */
12481465 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
@@ -1252,25 +1469,33 @@ static int xcan_probe(struct platform_device *pdev) · 3 unmodified lines
12521469 goto err;
12531470 }
12541471
1255− ret = of_property_read_u32(pdev->dev.of_node, "tx-fifo-depth",
1256− &tx_fifo_depth);
1257− if (ret < 0)
1258− goto err;
1472+ of_id = of_match_device(xcan_of_match, &pdev->dev);
1473+ if (of_id && of_id->data)
1474+ devtype = of_id->data;
12591475
1260− ret = of_property_read_u32(pdev->dev.of_node, "rx-fifo-depth", &rx_max);
1261− if (ret < 0)
1262− goto err;
1476+ hw_tx_max_property = devtype->flags & XCAN_FLAG_TX_MAILBOXES ?
1477+ "tx-mailbox-count" : "tx-fifo-depth";
12631478
1264− of_id = of_match_device(xcan_of_match, &pdev->dev);
1265− if (of_id) {
1266− const struct xcan_devtype_data *devtype_data = of_id->data;
1479+ ret = of_property_read_u32(pdev->dev.of_node, hw_tx_max_property,
1480+ &hw_tx_max);
1481+ if (ret < 0) {
1482+ dev_err(&pdev->dev, "missing %s property\n",
1483+ hw_tx_max_property);
1484+ goto err;
1485+ }
12671486
1268− if (devtype_data)
1269− caps = devtype_data->caps;
1487+ ret = of_property_read_u32(pdev->dev.of_node, "rx-fifo-depth",
1488+ &hw_rx_max);
1489+ if (ret < 0) {
1490+ dev_err(&pdev->dev,
1491+ "missing rx-fifo-depth property (mailbox mode is not supported)\n");
1492+ goto err;
12701493 }
12711494
1272− /* There is no way to directly figure out how many frames have been
1273− * sent when the TXOK interrupt is processed. If watermark programming
1495+ /* With TX FIFO:
1496+ *
1497+ * There is no way to directly figure out how many frames have been
1498+ * sent when the TXOK interrupt is processed. If TXFEMP
12741499 * is supported, we can have 2 frames in the FIFO and use TXFEMP
12751500 * to determine if 1 or 2 frames have been sent.
12761501 * Theoretically we should be able to use TXFWMEMP to determine up
@@ -1279,12 +1504,20 @@ static int xcan_probe(struct platform_device *pdev) · 2 unmodified lines
12791504 * than 2 frames in FIFO) is set anyway with no TXOK (a frame was
12801505 * sent), which is not a sensible state - possibly TXFWMEMP is not
12811506 * completely synchronized with the rest of the bits?
1507+ *
1508+ * With TX mailboxes:
1509+ *
1510+ * HW sends frames in CAN ID priority order. To preserve FIFO ordering
1511+ * we submit frames one at a time.
12821512 */
1283− if (caps & XCAN_CAP_WATERMARK)
1284− tx_max = min(tx_fifo_depth, 2);
1513+ if (!(devtype->flags & XCAN_FLAG_TX_MAILBOXES) &&
1514+ (devtype->flags & XCAN_FLAG_TXFEMP))
1515+ tx_max = min(hw_tx_max, 2);
12851516 else
12861517 tx_max = 1;
12871518
1519+ rx_max = hw_rx_max;
1520+
12881521 /* Create a CAN device instance */
12891522 ndev = alloc_candev(sizeof(struct xcan_priv), tx_max);
12901523 if (!ndev)
@@ -1292,13 +1525,14 @@ static int xcan_probe(struct platform_device *pdev) · 1 unmodified line
12921525
12931526 priv = netdev_priv(ndev);
12941527 priv->dev = &pdev->dev;
1295− priv->can.bittiming_const = &xcan_bittiming_const;
1528+ priv->can.bittiming_const = devtype->bittiming_const;
12961529 priv->can.do_set_mode = xcan_do_set_mode;
12971530 priv->can.do_get_berr_counter = xcan_get_berr_counter;
12981531 priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
12991532 CAN_CTRLMODE_BERR_REPORTING;
13001533 priv->reg_base = addr;
13011534 priv->tx_max = tx_max;
1535+ priv->devtype = *devtype;
13021536 spin_lock_init(&priv->tx_lock);
13031537
13041538 /* Get IRQ for the device */
@@ -1316,22 +1550,12 @@ static int xcan_probe(struct platform_device *pdev) · 11 unmodified lines
13161550 ret = PTR_ERR(priv->can_clk);
13171551 goto err_free;
13181552 }
1319− /* Check for type of CAN device */
1320− if (of_device_is_compatible(pdev->dev.of_node,
1321− "xlnx,zynq-can-1.0")) {
1322− priv->bus_clk = devm_clk_get(&pdev->dev, "pclk");
1323− if (IS_ERR(priv->bus_clk)) {
1324− dev_err(&pdev->dev, "bus clock not found\n");
1325− ret = PTR_ERR(priv->bus_clk);
1326− goto err_free;
1327− }
1328− } else {
1329− priv->bus_clk = devm_clk_get(&pdev->dev, "s_axi_aclk");
1330− if (IS_ERR(priv->bus_clk)) {
1331− dev_err(&pdev->dev, "bus clock not found\n");
1332− ret = PTR_ERR(priv->bus_clk);
1333− goto err_free;
1334− }
1553+
1554+ priv->bus_clk = devm_clk_get(&pdev->dev, devtype->bus_clk_name);
1555+ if (IS_ERR(priv->bus_clk)) {
1556+ dev_err(&pdev->dev, "bus clock not found\n");
1557+ ret = PTR_ERR(priv->bus_clk);
1558+ goto err_free;
13351559 }
13361560
13371561 priv->write_reg = xcan_write_reg_le;
@@ -1364,9 +1588,9 @@ static int xcan_probe(struct platform_device *pdev) · 26 unmodified lines
13641588
13651589 pm_runtime_put(&pdev->dev);
13661590
1367− netdev_dbg(ndev, "reg_base=0x%p irq=%d clock=%d, tx fifo depth: actual %d, using %d\n",
1368− priv->reg_base, ndev->irq, priv->can.clock.freq,
1369− tx_fifo_depth, priv->tx_max);
1591+ netdev_dbg(ndev, "reg_base=0x%p irq=%d clock=%d, tx buffers: actual %d, using %d\n",
1592+ priv->reg_base, ndev->irq, priv->can.clock.freq,
1593+ hw_tx_max, priv->tx_max);
13701594
13711595 return 0;
13721596
include/linux/can/dev.h+6−1
@@ -143,7 +143,12 @@ u8 can_dlc2len(u8 can_dlc); · 142 unmodified lines
143143 /* map the sanitized data length to an appropriate data length code */
144144 u8 can_len2dlc(u8 len);
145145
146−struct net_device *alloc_candev(int sizeof_priv, unsigned int echo_skb_max);
146+struct net_device *alloc_candev_mqs(int sizeof_priv, unsigned int echo_skb_max,
147+ unsigned int txqs, unsigned int rxqs);
148+#define alloc_candev(sizeof_priv, echo_skb_max) \
149+ alloc_candev_mqs(sizeof_priv, echo_skb_max, 1, 1)
150+#define alloc_candev_mq(sizeof_priv, echo_skb_max, count) \
151+ alloc_candev_mqs(sizeof_priv, echo_skb_max, count, count)
147152 void free_candev(struct net_device *dev);
148153
149154 /* a candev safe wrapper around netdev_priv */
include/uapi/linux/can.h+1−1
@@ -77,7 +77,7 @@ typedef __u32 canid_t; · 76 unmodified lines
7777 /*
7878 * Controller Area Network Error Message Frame Mask structure
7979 *
80− * bit 0-28 : error class mask (see include/linux/can/error.h)
80+ * bit 0-28 : error class mask (see include/uapi/linux/can/error.h)
8181 * bit 29-31 : set to zero
8282 */
8383 typedef __u32 can_err_mask_t;