xref: /openbmc/linux/drivers/net/can/usb/gs_usb.c (revision 5886e4d5ecec3e22844efed90b2dd383ef804b3a)
18e8e69d6SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2f155d9c0SMaximilian Schneider /* CAN driver for Geschwister Schneider USB/CAN devices
3f155d9c0SMaximilian Schneider  * and bytewerk.org candleLight USB CAN interfaces.
4d08e973aSMaximilian Schneider  *
5f155d9c0SMaximilian Schneider  * Copyright (C) 2013-2016 Geschwister Schneider Technologie-,
6d08e973aSMaximilian Schneider  * Entwicklungs- und Vertriebs UG (Haftungsbeschränkt).
7f155d9c0SMaximilian Schneider  * Copyright (C) 2016 Hubert Denkmair
8d08e973aSMaximilian Schneider  *
9d08e973aSMaximilian Schneider  * Many thanks to all socketcan devs!
10d08e973aSMaximilian Schneider  */
11d08e973aSMaximilian Schneider 
12eb9fa77aSPeter Fink #include <linux/bitfield.h>
1345dfa45fSJohn Whittington #include <linux/clocksource.h>
14cc69837fSJakub Kicinski #include <linux/ethtool.h>
15d08e973aSMaximilian Schneider #include <linux/init.h>
16d08e973aSMaximilian Schneider #include <linux/module.h>
17d08e973aSMaximilian Schneider #include <linux/netdevice.h>
18f6bb2510SMarc Kleine-Budde #include <linux/signal.h>
1945dfa45fSJohn Whittington #include <linux/timecounter.h>
2045dfa45fSJohn Whittington #include <linux/units.h>
21d08e973aSMaximilian Schneider #include <linux/usb.h>
2245dfa45fSJohn Whittington #include <linux/workqueue.h>
23d08e973aSMaximilian Schneider 
24d08e973aSMaximilian Schneider #include <linux/can.h>
25d08e973aSMaximilian Schneider #include <linux/can/dev.h>
26d08e973aSMaximilian Schneider #include <linux/can/error.h>
27d08e973aSMaximilian Schneider 
28d08e973aSMaximilian Schneider /* Device specific constants */
2949c007b9SMarc Kleine-Budde #define USB_GS_USB_1_VENDOR_ID 0x1d50
3049c007b9SMarc Kleine-Budde #define USB_GS_USB_1_PRODUCT_ID 0x606f
31d08e973aSMaximilian Schneider 
32f155d9c0SMaximilian Schneider #define USB_CANDLELIGHT_VENDOR_ID 0x1209
33f155d9c0SMaximilian Schneider #define USB_CANDLELIGHT_PRODUCT_ID 0x2323
34f155d9c0SMaximilian Schneider 
35d03bb08eSPeter Fink #define USB_CES_CANEXT_FD_VENDOR_ID 0x1cd2
36d03bb08eSPeter Fink #define USB_CES_CANEXT_FD_PRODUCT_ID 0x606f
37d03bb08eSPeter Fink 
380691a4b5SBen Evans #define USB_ABE_CANDEBUGGER_FD_VENDOR_ID 0x16d0
390691a4b5SBen Evans #define USB_ABE_CANDEBUGGER_FD_PRODUCT_ID 0x10b8
400691a4b5SBen Evans 
4149c007b9SMarc Kleine-Budde #define GS_USB_ENDPOINT_IN 1
4249c007b9SMarc Kleine-Budde #define GS_USB_ENDPOINT_OUT 2
43d08e973aSMaximilian Schneider 
4445dfa45fSJohn Whittington /* Timestamp 32 bit timer runs at 1 MHz (1 µs tick). Worker accounts
4545dfa45fSJohn Whittington  * for timer overflow (will be after ~71 minutes)
4645dfa45fSJohn Whittington  */
4745dfa45fSJohn Whittington #define GS_USB_TIMESTAMP_TIMER_HZ (1 * HZ_PER_MHZ)
4845dfa45fSJohn Whittington #define GS_USB_TIMESTAMP_WORK_DELAY_SEC 1800
4945dfa45fSJohn Whittington static_assert(GS_USB_TIMESTAMP_WORK_DELAY_SEC <
5045dfa45fSJohn Whittington 	      CYCLECOUNTER_MASK(32) / GS_USB_TIMESTAMP_TIMER_HZ / 2);
5145dfa45fSJohn Whittington 
52d08e973aSMaximilian Schneider /* Device specific constants */
53d08e973aSMaximilian Schneider enum gs_usb_breq {
54d08e973aSMaximilian Schneider 	GS_USB_BREQ_HOST_FORMAT = 0,
55d08e973aSMaximilian Schneider 	GS_USB_BREQ_BITTIMING,
56d08e973aSMaximilian Schneider 	GS_USB_BREQ_MODE,
57d08e973aSMaximilian Schneider 	GS_USB_BREQ_BERR,
58d08e973aSMaximilian Schneider 	GS_USB_BREQ_BT_CONST,
5905ca5270SMaximilian Schneider 	GS_USB_BREQ_DEVICE_CONFIG,
6005ca5270SMaximilian Schneider 	GS_USB_BREQ_TIMESTAMP,
6105ca5270SMaximilian Schneider 	GS_USB_BREQ_IDENTIFY,
62e0902cadSMarc Kleine-Budde 	GS_USB_BREQ_GET_USER_ID,
6332cd9013SMarc Kleine-Budde 	GS_USB_BREQ_QUIRK_CANTACT_PRO_DATA_BITTIMING = GS_USB_BREQ_GET_USER_ID,
64e0902cadSMarc Kleine-Budde 	GS_USB_BREQ_SET_USER_ID,
6526949ac9SPeter Fink 	GS_USB_BREQ_DATA_BITTIMING,
666679f4c5SPeter Fink 	GS_USB_BREQ_BT_CONST_EXT,
67906e0e68SMarc Kleine-Budde 	GS_USB_BREQ_SET_TERMINATION,
68906e0e68SMarc Kleine-Budde 	GS_USB_BREQ_GET_TERMINATION,
6940e1997dSJeroen Hofstee 	GS_USB_BREQ_GET_STATE,
70d08e973aSMaximilian Schneider };
71d08e973aSMaximilian Schneider 
72d08e973aSMaximilian Schneider enum gs_can_mode {
73d08e973aSMaximilian Schneider 	/* reset a channel. turns it off */
74d08e973aSMaximilian Schneider 	GS_CAN_MODE_RESET = 0,
75d08e973aSMaximilian Schneider 	/* starts a channel */
76d08e973aSMaximilian Schneider 	GS_CAN_MODE_START
77d08e973aSMaximilian Schneider };
78d08e973aSMaximilian Schneider 
79d08e973aSMaximilian Schneider enum gs_can_state {
80d08e973aSMaximilian Schneider 	GS_CAN_STATE_ERROR_ACTIVE = 0,
81d08e973aSMaximilian Schneider 	GS_CAN_STATE_ERROR_WARNING,
82d08e973aSMaximilian Schneider 	GS_CAN_STATE_ERROR_PASSIVE,
83d08e973aSMaximilian Schneider 	GS_CAN_STATE_BUS_OFF,
84d08e973aSMaximilian Schneider 	GS_CAN_STATE_STOPPED,
85d08e973aSMaximilian Schneider 	GS_CAN_STATE_SLEEPING
86d08e973aSMaximilian Schneider };
87d08e973aSMaximilian Schneider 
8805ca5270SMaximilian Schneider enum gs_can_identify_mode {
8905ca5270SMaximilian Schneider 	GS_CAN_IDENTIFY_OFF = 0,
9005ca5270SMaximilian Schneider 	GS_CAN_IDENTIFY_ON
9105ca5270SMaximilian Schneider };
9205ca5270SMaximilian Schneider 
93906e0e68SMarc Kleine-Budde enum gs_can_termination_state {
94906e0e68SMarc Kleine-Budde 	GS_CAN_TERMINATION_STATE_OFF = 0,
95906e0e68SMarc Kleine-Budde 	GS_CAN_TERMINATION_STATE_ON
96906e0e68SMarc Kleine-Budde };
97906e0e68SMarc Kleine-Budde 
98906e0e68SMarc Kleine-Budde #define GS_USB_TERMINATION_DISABLED CAN_TERMINATION_DISABLED
99906e0e68SMarc Kleine-Budde #define GS_USB_TERMINATION_ENABLED 120
100906e0e68SMarc Kleine-Budde 
101d08e973aSMaximilian Schneider /* data types passed between host and device */
1024ba1cb39SMarc Kleine-Budde 
1034ba1cb39SMarc Kleine-Budde /* The firmware on the original USB2CAN by Geschwister Schneider
1044ba1cb39SMarc Kleine-Budde  * Technologie Entwicklungs- und Vertriebs UG exchanges all data
1054ba1cb39SMarc Kleine-Budde  * between the host and the device in host byte order. This is done
1064ba1cb39SMarc Kleine-Budde  * with the struct gs_host_config::byte_order member, which is sent
1074ba1cb39SMarc Kleine-Budde  * first to indicate the desired byte order.
1084ba1cb39SMarc Kleine-Budde  *
1094ba1cb39SMarc Kleine-Budde  * The widely used open source firmware candleLight doesn't support
1104ba1cb39SMarc Kleine-Budde  * this feature and exchanges the data in little endian byte order.
111d08e973aSMaximilian Schneider  */
1124ba1cb39SMarc Kleine-Budde struct gs_host_config {
1134ba1cb39SMarc Kleine-Budde 	__le32 byte_order;
1144ba1cb39SMarc Kleine-Budde } __packed;
115d08e973aSMaximilian Schneider 
116d08e973aSMaximilian Schneider struct gs_device_config {
117d08e973aSMaximilian Schneider 	u8 reserved1;
118d08e973aSMaximilian Schneider 	u8 reserved2;
119d08e973aSMaximilian Schneider 	u8 reserved3;
120d08e973aSMaximilian Schneider 	u8 icount;
1214ba1cb39SMarc Kleine-Budde 	__le32 sw_version;
1224ba1cb39SMarc Kleine-Budde 	__le32 hw_version;
123d08e973aSMaximilian Schneider } __packed;
124d08e973aSMaximilian Schneider 
125d08e973aSMaximilian Schneider #define GS_CAN_MODE_NORMAL 0
1269be95c91SMaximilian Schneider #define GS_CAN_MODE_LISTEN_ONLY BIT(0)
1279be95c91SMaximilian Schneider #define GS_CAN_MODE_LOOP_BACK BIT(1)
1289be95c91SMaximilian Schneider #define GS_CAN_MODE_TRIPLE_SAMPLE BIT(2)
1299be95c91SMaximilian Schneider #define GS_CAN_MODE_ONE_SHOT BIT(3)
130d42d2111SMarc Kleine-Budde #define GS_CAN_MODE_HW_TIMESTAMP BIT(4)
13115564f82SMarc Kleine-Budde /* GS_CAN_FEATURE_IDENTIFY BIT(5) */
132e0902cadSMarc Kleine-Budde /* GS_CAN_FEATURE_USER_ID BIT(6) */
1334643e34eSMarc Kleine-Budde #define GS_CAN_MODE_PAD_PKTS_TO_MAX_PKT_SIZE BIT(7)
13426949ac9SPeter Fink #define GS_CAN_MODE_FD BIT(8)
135eb9fa77aSPeter Fink /* GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX BIT(9) */
1366679f4c5SPeter Fink /* GS_CAN_FEATURE_BT_CONST_EXT BIT(10) */
137906e0e68SMarc Kleine-Budde /* GS_CAN_FEATURE_TERMINATION BIT(11) */
1381f183526SJeroen Hofstee #define GS_CAN_MODE_BERR_REPORTING BIT(12)
13940e1997dSJeroen Hofstee /* GS_CAN_FEATURE_GET_STATE BIT(13) */
140d08e973aSMaximilian Schneider 
141d08e973aSMaximilian Schneider struct gs_device_mode {
1424ba1cb39SMarc Kleine-Budde 	__le32 mode;
1434ba1cb39SMarc Kleine-Budde 	__le32 flags;
144d08e973aSMaximilian Schneider } __packed;
145d08e973aSMaximilian Schneider 
146d08e973aSMaximilian Schneider struct gs_device_state {
1474ba1cb39SMarc Kleine-Budde 	__le32 state;
1484ba1cb39SMarc Kleine-Budde 	__le32 rxerr;
1494ba1cb39SMarc Kleine-Budde 	__le32 txerr;
150d08e973aSMaximilian Schneider } __packed;
151d08e973aSMaximilian Schneider 
152d08e973aSMaximilian Schneider struct gs_device_bittiming {
1534ba1cb39SMarc Kleine-Budde 	__le32 prop_seg;
1544ba1cb39SMarc Kleine-Budde 	__le32 phase_seg1;
1554ba1cb39SMarc Kleine-Budde 	__le32 phase_seg2;
1564ba1cb39SMarc Kleine-Budde 	__le32 sjw;
1574ba1cb39SMarc Kleine-Budde 	__le32 brp;
158d08e973aSMaximilian Schneider } __packed;
159d08e973aSMaximilian Schneider 
16005ca5270SMaximilian Schneider struct gs_identify_mode {
1614ba1cb39SMarc Kleine-Budde 	__le32 mode;
16205ca5270SMaximilian Schneider } __packed;
16305ca5270SMaximilian Schneider 
164906e0e68SMarc Kleine-Budde struct gs_device_termination_state {
165906e0e68SMarc Kleine-Budde 	__le32 state;
166906e0e68SMarc Kleine-Budde } __packed;
167906e0e68SMarc Kleine-Budde 
1689be95c91SMaximilian Schneider #define GS_CAN_FEATURE_LISTEN_ONLY BIT(0)
1699be95c91SMaximilian Schneider #define GS_CAN_FEATURE_LOOP_BACK BIT(1)
1709be95c91SMaximilian Schneider #define GS_CAN_FEATURE_TRIPLE_SAMPLE BIT(2)
1719be95c91SMaximilian Schneider #define GS_CAN_FEATURE_ONE_SHOT BIT(3)
17205ca5270SMaximilian Schneider #define GS_CAN_FEATURE_HW_TIMESTAMP BIT(4)
17305ca5270SMaximilian Schneider #define GS_CAN_FEATURE_IDENTIFY BIT(5)
174e0902cadSMarc Kleine-Budde #define GS_CAN_FEATURE_USER_ID BIT(6)
1754643e34eSMarc Kleine-Budde #define GS_CAN_FEATURE_PAD_PKTS_TO_MAX_PKT_SIZE BIT(7)
17626949ac9SPeter Fink #define GS_CAN_FEATURE_FD BIT(8)
177eb9fa77aSPeter Fink #define GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX BIT(9)
1786679f4c5SPeter Fink #define GS_CAN_FEATURE_BT_CONST_EXT BIT(10)
179906e0e68SMarc Kleine-Budde #define GS_CAN_FEATURE_TERMINATION BIT(11)
1801f183526SJeroen Hofstee #define GS_CAN_FEATURE_BERR_REPORTING BIT(12)
18140e1997dSJeroen Hofstee #define GS_CAN_FEATURE_GET_STATE BIT(13)
18240e1997dSJeroen Hofstee #define GS_CAN_FEATURE_MASK GENMASK(13, 0)
183d08e973aSMaximilian Schneider 
18432cd9013SMarc Kleine-Budde /* internal quirks - keep in GS_CAN_FEATURE space for now */
18532cd9013SMarc Kleine-Budde 
18632cd9013SMarc Kleine-Budde /* CANtact Pro original firmware:
18732cd9013SMarc Kleine-Budde  * BREQ DATA_BITTIMING overlaps with GET_USER_ID
18832cd9013SMarc Kleine-Budde  */
18932cd9013SMarc Kleine-Budde #define GS_CAN_FEATURE_QUIRK_BREQ_CANTACT_PRO BIT(31)
19032cd9013SMarc Kleine-Budde 
191d08e973aSMaximilian Schneider struct gs_device_bt_const {
1924ba1cb39SMarc Kleine-Budde 	__le32 feature;
1934ba1cb39SMarc Kleine-Budde 	__le32 fclk_can;
1944ba1cb39SMarc Kleine-Budde 	__le32 tseg1_min;
1954ba1cb39SMarc Kleine-Budde 	__le32 tseg1_max;
1964ba1cb39SMarc Kleine-Budde 	__le32 tseg2_min;
1974ba1cb39SMarc Kleine-Budde 	__le32 tseg2_max;
1984ba1cb39SMarc Kleine-Budde 	__le32 sjw_max;
1994ba1cb39SMarc Kleine-Budde 	__le32 brp_min;
2004ba1cb39SMarc Kleine-Budde 	__le32 brp_max;
2014ba1cb39SMarc Kleine-Budde 	__le32 brp_inc;
202d08e973aSMaximilian Schneider } __packed;
203d08e973aSMaximilian Schneider 
2046679f4c5SPeter Fink struct gs_device_bt_const_extended {
2056679f4c5SPeter Fink 	__le32 feature;
2066679f4c5SPeter Fink 	__le32 fclk_can;
2076679f4c5SPeter Fink 	__le32 tseg1_min;
2086679f4c5SPeter Fink 	__le32 tseg1_max;
2096679f4c5SPeter Fink 	__le32 tseg2_min;
2106679f4c5SPeter Fink 	__le32 tseg2_max;
2116679f4c5SPeter Fink 	__le32 sjw_max;
2126679f4c5SPeter Fink 	__le32 brp_min;
2136679f4c5SPeter Fink 	__le32 brp_max;
2146679f4c5SPeter Fink 	__le32 brp_inc;
2156679f4c5SPeter Fink 
2166679f4c5SPeter Fink 	__le32 dtseg1_min;
2176679f4c5SPeter Fink 	__le32 dtseg1_max;
2186679f4c5SPeter Fink 	__le32 dtseg2_min;
2196679f4c5SPeter Fink 	__le32 dtseg2_max;
2206679f4c5SPeter Fink 	__le32 dsjw_max;
2216679f4c5SPeter Fink 	__le32 dbrp_min;
2226679f4c5SPeter Fink 	__le32 dbrp_max;
2236679f4c5SPeter Fink 	__le32 dbrp_inc;
2246679f4c5SPeter Fink } __packed;
2256679f4c5SPeter Fink 
2264b8f03e3SMarc Kleine-Budde #define GS_CAN_FLAG_OVERFLOW BIT(0)
22726949ac9SPeter Fink #define GS_CAN_FLAG_FD BIT(1)
22826949ac9SPeter Fink #define GS_CAN_FLAG_BRS BIT(2)
22926949ac9SPeter Fink #define GS_CAN_FLAG_ESI BIT(3)
230d08e973aSMaximilian Schneider 
231c359931dSPeter Fink struct classic_can {
232c359931dSPeter Fink 	u8 data[8];
233c359931dSPeter Fink } __packed;
234c359931dSPeter Fink 
23545dfa45fSJohn Whittington struct classic_can_ts {
23645dfa45fSJohn Whittington 	u8 data[8];
23745dfa45fSJohn Whittington 	__le32 timestamp_us;
23845dfa45fSJohn Whittington } __packed;
23945dfa45fSJohn Whittington 
240eb9fa77aSPeter Fink struct classic_can_quirk {
241eb9fa77aSPeter Fink 	u8 data[8];
242eb9fa77aSPeter Fink 	u8 quirk;
243eb9fa77aSPeter Fink } __packed;
244eb9fa77aSPeter Fink 
24526949ac9SPeter Fink struct canfd {
24626949ac9SPeter Fink 	u8 data[64];
24726949ac9SPeter Fink } __packed;
24826949ac9SPeter Fink 
24945dfa45fSJohn Whittington struct canfd_ts {
25045dfa45fSJohn Whittington 	u8 data[64];
25145dfa45fSJohn Whittington 	__le32 timestamp_us;
25245dfa45fSJohn Whittington } __packed;
25345dfa45fSJohn Whittington 
254eb9fa77aSPeter Fink struct canfd_quirk {
255eb9fa77aSPeter Fink 	u8 data[64];
256eb9fa77aSPeter Fink 	u8 quirk;
257eb9fa77aSPeter Fink } __packed;
258eb9fa77aSPeter Fink 
259d08e973aSMaximilian Schneider struct gs_host_frame {
260d08e973aSMaximilian Schneider 	u32 echo_id;
2614ba1cb39SMarc Kleine-Budde 	__le32 can_id;
262d08e973aSMaximilian Schneider 
263d08e973aSMaximilian Schneider 	u8 can_dlc;
264d08e973aSMaximilian Schneider 	u8 channel;
265d08e973aSMaximilian Schneider 	u8 flags;
266d08e973aSMaximilian Schneider 	u8 reserved;
267d08e973aSMaximilian Schneider 
268c359931dSPeter Fink 	union {
269c359931dSPeter Fink 		DECLARE_FLEX_ARRAY(struct classic_can, classic_can);
27045dfa45fSJohn Whittington 		DECLARE_FLEX_ARRAY(struct classic_can_ts, classic_can_ts);
271eb9fa77aSPeter Fink 		DECLARE_FLEX_ARRAY(struct classic_can_quirk, classic_can_quirk);
27226949ac9SPeter Fink 		DECLARE_FLEX_ARRAY(struct canfd, canfd);
27345dfa45fSJohn Whittington 		DECLARE_FLEX_ARRAY(struct canfd_ts, canfd_ts);
274eb9fa77aSPeter Fink 		DECLARE_FLEX_ARRAY(struct canfd_quirk, canfd_quirk);
275c359931dSPeter Fink 	};
276d08e973aSMaximilian Schneider } __packed;
277d08e973aSMaximilian Schneider /* The GS USB devices make use of the same flags and masks as in
278d08e973aSMaximilian Schneider  * linux/can.h and linux/can/error.h, and no additional mapping is necessary.
279d08e973aSMaximilian Schneider  */
280d08e973aSMaximilian Schneider 
281d08e973aSMaximilian Schneider /* Only send a max of GS_MAX_TX_URBS frames per channel at a time. */
282d08e973aSMaximilian Schneider #define GS_MAX_TX_URBS 10
283d08e973aSMaximilian Schneider /* Only launch a max of GS_MAX_RX_URBS usb requests at a time. */
284d08e973aSMaximilian Schneider #define GS_MAX_RX_URBS 30
285d08e973aSMaximilian Schneider /* Maximum number of interfaces the driver supports per device.
286e10ab8b3SMarc Kleine-Budde  * Current hardware only supports 3 interfaces. The future may vary.
287d08e973aSMaximilian Schneider  */
288e10ab8b3SMarc Kleine-Budde #define GS_MAX_INTF 3
289d08e973aSMaximilian Schneider 
290d08e973aSMaximilian Schneider struct gs_tx_context {
291d08e973aSMaximilian Schneider 	struct gs_can *dev;
292d08e973aSMaximilian Schneider 	unsigned int echo_id;
293d08e973aSMaximilian Schneider };
294d08e973aSMaximilian Schneider 
295d08e973aSMaximilian Schneider struct gs_can {
296d08e973aSMaximilian Schneider 	struct can_priv can; /* must be the first member */
297d08e973aSMaximilian Schneider 
298d08e973aSMaximilian Schneider 	struct gs_usb *parent;
299d08e973aSMaximilian Schneider 
300d08e973aSMaximilian Schneider 	struct net_device *netdev;
301d08e973aSMaximilian Schneider 	struct usb_device *udev;
302d08e973aSMaximilian Schneider 
3036679f4c5SPeter Fink 	struct can_bittiming_const bt_const, data_bt_const;
304d08e973aSMaximilian Schneider 	unsigned int channel;	/* channel number */
305d08e973aSMaximilian Schneider 
306eb9fa77aSPeter Fink 	u32 feature;
307c359931dSPeter Fink 	unsigned int hf_size_tx;
308c359931dSPeter Fink 
309ae421e33SNik Nyby 	/* This lock prevents a race condition between xmit and receive. */
310d08e973aSMaximilian Schneider 	spinlock_t tx_ctx_lock;
311d08e973aSMaximilian Schneider 	struct gs_tx_context tx_context[GS_MAX_TX_URBS];
312d08e973aSMaximilian Schneider 
313d08e973aSMaximilian Schneider 	struct usb_anchor tx_submitted;
314d08e973aSMaximilian Schneider 	atomic_t active_tx_urbs;
315d08e973aSMaximilian Schneider };
316d08e973aSMaximilian Schneider 
317d08e973aSMaximilian Schneider /* usb interface struct */
318d08e973aSMaximilian Schneider struct gs_usb {
319d08e973aSMaximilian Schneider 	struct gs_can *canch[GS_MAX_INTF];
320d08e973aSMaximilian Schneider 	struct usb_anchor rx_submitted;
321d08e973aSMaximilian Schneider 	struct usb_device *udev;
322*5886e4d5SMarc Kleine-Budde 
323*5886e4d5SMarc Kleine-Budde 	/* time counter for hardware timestamps */
324*5886e4d5SMarc Kleine-Budde 	struct cyclecounter cc;
325*5886e4d5SMarc Kleine-Budde 	struct timecounter tc;
326*5886e4d5SMarc Kleine-Budde 	spinlock_t tc_lock; /* spinlock to guard access tc->cycle_last */
327*5886e4d5SMarc Kleine-Budde 	struct delayed_work timestamp;
328*5886e4d5SMarc Kleine-Budde 
329c359931dSPeter Fink 	unsigned int hf_size_rx;
330035b0fcfSVincent Mailhol 	u8 active_channels;
331d08e973aSMaximilian Schneider };
332d08e973aSMaximilian Schneider 
333d08e973aSMaximilian Schneider /* 'allocate' a tx context.
334d08e973aSMaximilian Schneider  * returns a valid tx context or NULL if there is no space.
335d08e973aSMaximilian Schneider  */
336d08e973aSMaximilian Schneider static struct gs_tx_context *gs_alloc_tx_context(struct gs_can *dev)
337d08e973aSMaximilian Schneider {
338d08e973aSMaximilian Schneider 	int i = 0;
339d08e973aSMaximilian Schneider 	unsigned long flags;
340d08e973aSMaximilian Schneider 
341d08e973aSMaximilian Schneider 	spin_lock_irqsave(&dev->tx_ctx_lock, flags);
342d08e973aSMaximilian Schneider 
343d08e973aSMaximilian Schneider 	for (; i < GS_MAX_TX_URBS; i++) {
344d08e973aSMaximilian Schneider 		if (dev->tx_context[i].echo_id == GS_MAX_TX_URBS) {
345d08e973aSMaximilian Schneider 			dev->tx_context[i].echo_id = i;
346d08e973aSMaximilian Schneider 			spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
347d08e973aSMaximilian Schneider 			return &dev->tx_context[i];
348d08e973aSMaximilian Schneider 		}
349d08e973aSMaximilian Schneider 	}
350d08e973aSMaximilian Schneider 
351d08e973aSMaximilian Schneider 	spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
352d08e973aSMaximilian Schneider 	return NULL;
353d08e973aSMaximilian Schneider }
354d08e973aSMaximilian Schneider 
355d08e973aSMaximilian Schneider /* releases a tx context
356d08e973aSMaximilian Schneider  */
357d08e973aSMaximilian Schneider static void gs_free_tx_context(struct gs_tx_context *txc)
358d08e973aSMaximilian Schneider {
359d08e973aSMaximilian Schneider 	txc->echo_id = GS_MAX_TX_URBS;
360d08e973aSMaximilian Schneider }
361d08e973aSMaximilian Schneider 
362d08e973aSMaximilian Schneider /* Get a tx context by id.
363d08e973aSMaximilian Schneider  */
3649be95c91SMaximilian Schneider static struct gs_tx_context *gs_get_tx_context(struct gs_can *dev,
3659be95c91SMaximilian Schneider 					       unsigned int id)
366d08e973aSMaximilian Schneider {
367d08e973aSMaximilian Schneider 	unsigned long flags;
368d08e973aSMaximilian Schneider 
369d08e973aSMaximilian Schneider 	if (id < GS_MAX_TX_URBS) {
370d08e973aSMaximilian Schneider 		spin_lock_irqsave(&dev->tx_ctx_lock, flags);
371d08e973aSMaximilian Schneider 		if (dev->tx_context[id].echo_id == id) {
372d08e973aSMaximilian Schneider 			spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
373d08e973aSMaximilian Schneider 			return &dev->tx_context[id];
374d08e973aSMaximilian Schneider 		}
375d08e973aSMaximilian Schneider 		spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
376d08e973aSMaximilian Schneider 	}
377d08e973aSMaximilian Schneider 	return NULL;
378d08e973aSMaximilian Schneider }
379d08e973aSMaximilian Schneider 
38000246751SMarc Kleine-Budde static int gs_cmd_reset(struct gs_can *dev)
381d08e973aSMaximilian Schneider {
3823814ed27SMarc Kleine-Budde 	struct gs_device_mode dm = {
3833814ed27SMarc Kleine-Budde 		.mode = GS_CAN_MODE_RESET,
3843814ed27SMarc Kleine-Budde 	};
385d08e973aSMaximilian Schneider 
38656c56a30SVincent Mailhol 	return usb_control_msg_send(dev->udev, 0, GS_USB_BREQ_MODE,
387d08e973aSMaximilian Schneider 				    USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
3883814ed27SMarc Kleine-Budde 				    dev->channel, 0, &dm, sizeof(dm), 1000,
3893814ed27SMarc Kleine-Budde 				    GFP_KERNEL);
390d08e973aSMaximilian Schneider }
391d08e973aSMaximilian Schneider 
392*5886e4d5SMarc Kleine-Budde static inline int gs_usb_get_timestamp(const struct gs_usb *parent,
39345dfa45fSJohn Whittington 				       u32 *timestamp_p)
39445dfa45fSJohn Whittington {
39545dfa45fSJohn Whittington 	__le32 timestamp;
39645dfa45fSJohn Whittington 	int rc;
39745dfa45fSJohn Whittington 
398*5886e4d5SMarc Kleine-Budde 	rc = usb_control_msg_recv(parent->udev, 0, GS_USB_BREQ_TIMESTAMP,
39945dfa45fSJohn Whittington 				  USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
400*5886e4d5SMarc Kleine-Budde 				  0, 0,
40145dfa45fSJohn Whittington 				  &timestamp, sizeof(timestamp),
40245dfa45fSJohn Whittington 				  USB_CTRL_GET_TIMEOUT,
40345dfa45fSJohn Whittington 				  GFP_KERNEL);
40445dfa45fSJohn Whittington 	if (rc)
40545dfa45fSJohn Whittington 		return rc;
40645dfa45fSJohn Whittington 
40745dfa45fSJohn Whittington 	*timestamp_p = le32_to_cpu(timestamp);
40845dfa45fSJohn Whittington 
40945dfa45fSJohn Whittington 	return 0;
41045dfa45fSJohn Whittington }
41145dfa45fSJohn Whittington 
41229a8c9ecSMarc Kleine-Budde static u64 gs_usb_timestamp_read(const struct cyclecounter *cc) __must_hold(&dev->tc_lock)
41345dfa45fSJohn Whittington {
414*5886e4d5SMarc Kleine-Budde 	struct gs_usb *parent = container_of(cc, struct gs_usb, cc);
41545dfa45fSJohn Whittington 	u32 timestamp = 0;
41645dfa45fSJohn Whittington 	int err;
41745dfa45fSJohn Whittington 
418*5886e4d5SMarc Kleine-Budde 	lockdep_assert_held(&parent->tc_lock);
41929a8c9ecSMarc Kleine-Budde 
42029a8c9ecSMarc Kleine-Budde 	/* drop lock for synchronous USB transfer */
421*5886e4d5SMarc Kleine-Budde 	spin_unlock_bh(&parent->tc_lock);
422*5886e4d5SMarc Kleine-Budde 	err = gs_usb_get_timestamp(parent, &timestamp);
423*5886e4d5SMarc Kleine-Budde 	spin_lock_bh(&parent->tc_lock);
42445dfa45fSJohn Whittington 	if (err)
425*5886e4d5SMarc Kleine-Budde 		dev_err(&parent->udev->dev,
42645dfa45fSJohn Whittington 			"Error %d while reading timestamp. HW timestamps may be inaccurate.",
42745dfa45fSJohn Whittington 			err);
42845dfa45fSJohn Whittington 
42945dfa45fSJohn Whittington 	return timestamp;
43045dfa45fSJohn Whittington }
43145dfa45fSJohn Whittington 
43245dfa45fSJohn Whittington static void gs_usb_timestamp_work(struct work_struct *work)
43345dfa45fSJohn Whittington {
43445dfa45fSJohn Whittington 	struct delayed_work *delayed_work = to_delayed_work(work);
435*5886e4d5SMarc Kleine-Budde 	struct gs_usb *parent;
43645dfa45fSJohn Whittington 
437*5886e4d5SMarc Kleine-Budde 	parent = container_of(delayed_work, struct gs_usb, timestamp);
438*5886e4d5SMarc Kleine-Budde 	spin_lock_bh(&parent->tc_lock);
439*5886e4d5SMarc Kleine-Budde 	timecounter_read(&parent->tc);
440*5886e4d5SMarc Kleine-Budde 	spin_unlock_bh(&parent->tc_lock);
44145dfa45fSJohn Whittington 
442*5886e4d5SMarc Kleine-Budde 	schedule_delayed_work(&parent->timestamp,
44345dfa45fSJohn Whittington 			      GS_USB_TIMESTAMP_WORK_DELAY_SEC * HZ);
44445dfa45fSJohn Whittington }
44545dfa45fSJohn Whittington 
44629a8c9ecSMarc Kleine-Budde static void gs_usb_skb_set_timestamp(struct gs_can *dev,
44745dfa45fSJohn Whittington 				     struct sk_buff *skb, u32 timestamp)
44845dfa45fSJohn Whittington {
44945dfa45fSJohn Whittington 	struct skb_shared_hwtstamps *hwtstamps = skb_hwtstamps(skb);
450*5886e4d5SMarc Kleine-Budde 	struct gs_usb *parent = dev->parent;
45145dfa45fSJohn Whittington 	u64 ns;
45245dfa45fSJohn Whittington 
453*5886e4d5SMarc Kleine-Budde 	spin_lock_bh(&parent->tc_lock);
454*5886e4d5SMarc Kleine-Budde 	ns = timecounter_cyc2time(&parent->tc, timestamp);
455*5886e4d5SMarc Kleine-Budde 	spin_unlock_bh(&parent->tc_lock);
45629a8c9ecSMarc Kleine-Budde 
45745dfa45fSJohn Whittington 	hwtstamps->hwtstamp = ns_to_ktime(ns);
45845dfa45fSJohn Whittington }
45945dfa45fSJohn Whittington 
460*5886e4d5SMarc Kleine-Budde static void gs_usb_timestamp_init(struct gs_usb *parent)
46145dfa45fSJohn Whittington {
462*5886e4d5SMarc Kleine-Budde 	struct cyclecounter *cc = &parent->cc;
46345dfa45fSJohn Whittington 
46445dfa45fSJohn Whittington 	cc->read = gs_usb_timestamp_read;
46545dfa45fSJohn Whittington 	cc->mask = CYCLECOUNTER_MASK(32);
46645dfa45fSJohn Whittington 	cc->shift = 32 - bits_per(NSEC_PER_SEC / GS_USB_TIMESTAMP_TIMER_HZ);
46745dfa45fSJohn Whittington 	cc->mult = clocksource_hz2mult(GS_USB_TIMESTAMP_TIMER_HZ, cc->shift);
46845dfa45fSJohn Whittington 
469*5886e4d5SMarc Kleine-Budde 	spin_lock_init(&parent->tc_lock);
470*5886e4d5SMarc Kleine-Budde 	spin_lock_bh(&parent->tc_lock);
471*5886e4d5SMarc Kleine-Budde 	timecounter_init(&parent->tc, &parent->cc, ktime_get_real_ns());
472*5886e4d5SMarc Kleine-Budde 	spin_unlock_bh(&parent->tc_lock);
47345dfa45fSJohn Whittington 
474*5886e4d5SMarc Kleine-Budde 	INIT_DELAYED_WORK(&parent->timestamp, gs_usb_timestamp_work);
475*5886e4d5SMarc Kleine-Budde 	schedule_delayed_work(&parent->timestamp,
47645dfa45fSJohn Whittington 			      GS_USB_TIMESTAMP_WORK_DELAY_SEC * HZ);
47745dfa45fSJohn Whittington }
47845dfa45fSJohn Whittington 
479*5886e4d5SMarc Kleine-Budde static void gs_usb_timestamp_stop(struct gs_usb *parent)
48045dfa45fSJohn Whittington {
481*5886e4d5SMarc Kleine-Budde 	cancel_delayed_work_sync(&parent->timestamp);
48245dfa45fSJohn Whittington }
48345dfa45fSJohn Whittington 
484d08e973aSMaximilian Schneider static void gs_update_state(struct gs_can *dev, struct can_frame *cf)
485d08e973aSMaximilian Schneider {
486d08e973aSMaximilian Schneider 	struct can_device_stats *can_stats = &dev->can.can_stats;
487d08e973aSMaximilian Schneider 
488d08e973aSMaximilian Schneider 	if (cf->can_id & CAN_ERR_RESTARTED) {
489d08e973aSMaximilian Schneider 		dev->can.state = CAN_STATE_ERROR_ACTIVE;
490d08e973aSMaximilian Schneider 		can_stats->restarts++;
491d08e973aSMaximilian Schneider 	} else if (cf->can_id & CAN_ERR_BUSOFF) {
492d08e973aSMaximilian Schneider 		dev->can.state = CAN_STATE_BUS_OFF;
493d08e973aSMaximilian Schneider 		can_stats->bus_off++;
494d08e973aSMaximilian Schneider 	} else if (cf->can_id & CAN_ERR_CRTL) {
495d08e973aSMaximilian Schneider 		if ((cf->data[1] & CAN_ERR_CRTL_TX_WARNING) ||
496d08e973aSMaximilian Schneider 		    (cf->data[1] & CAN_ERR_CRTL_RX_WARNING)) {
497d08e973aSMaximilian Schneider 			dev->can.state = CAN_STATE_ERROR_WARNING;
498d08e973aSMaximilian Schneider 			can_stats->error_warning++;
499d08e973aSMaximilian Schneider 		} else if ((cf->data[1] & CAN_ERR_CRTL_TX_PASSIVE) ||
500d08e973aSMaximilian Schneider 			   (cf->data[1] & CAN_ERR_CRTL_RX_PASSIVE)) {
501d08e973aSMaximilian Schneider 			dev->can.state = CAN_STATE_ERROR_PASSIVE;
502d08e973aSMaximilian Schneider 			can_stats->error_passive++;
503d08e973aSMaximilian Schneider 		} else {
504d08e973aSMaximilian Schneider 			dev->can.state = CAN_STATE_ERROR_ACTIVE;
505d08e973aSMaximilian Schneider 		}
506d08e973aSMaximilian Schneider 	}
507d08e973aSMaximilian Schneider }
508d08e973aSMaximilian Schneider 
50929a8c9ecSMarc Kleine-Budde static void gs_usb_set_timestamp(struct gs_can *dev, struct sk_buff *skb,
51045dfa45fSJohn Whittington 				 const struct gs_host_frame *hf)
51145dfa45fSJohn Whittington {
51245dfa45fSJohn Whittington 	u32 timestamp;
51345dfa45fSJohn Whittington 
51445dfa45fSJohn Whittington 	if (!(dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP))
51545dfa45fSJohn Whittington 		return;
51645dfa45fSJohn Whittington 
51745dfa45fSJohn Whittington 	if (hf->flags & GS_CAN_FLAG_FD)
51845dfa45fSJohn Whittington 		timestamp = le32_to_cpu(hf->canfd_ts->timestamp_us);
51945dfa45fSJohn Whittington 	else
52045dfa45fSJohn Whittington 		timestamp = le32_to_cpu(hf->classic_can_ts->timestamp_us);
52145dfa45fSJohn Whittington 
52245dfa45fSJohn Whittington 	gs_usb_skb_set_timestamp(dev, skb, timestamp);
52345dfa45fSJohn Whittington 
52445dfa45fSJohn Whittington 	return;
52545dfa45fSJohn Whittington }
52645dfa45fSJohn Whittington 
527ae421e33SNik Nyby static void gs_usb_receive_bulk_callback(struct urb *urb)
528d08e973aSMaximilian Schneider {
529d08e973aSMaximilian Schneider 	struct gs_usb *usbcan = urb->context;
530d08e973aSMaximilian Schneider 	struct gs_can *dev;
531d08e973aSMaximilian Schneider 	struct net_device *netdev;
532d08e973aSMaximilian Schneider 	int rc;
533d08e973aSMaximilian Schneider 	struct net_device_stats *stats;
534d08e973aSMaximilian Schneider 	struct gs_host_frame *hf = urb->transfer_buffer;
535d08e973aSMaximilian Schneider 	struct gs_tx_context *txc;
536d08e973aSMaximilian Schneider 	struct can_frame *cf;
53726949ac9SPeter Fink 	struct canfd_frame *cfd;
538d08e973aSMaximilian Schneider 	struct sk_buff *skb;
539d08e973aSMaximilian Schneider 
540d08e973aSMaximilian Schneider 	BUG_ON(!usbcan);
541d08e973aSMaximilian Schneider 
542d08e973aSMaximilian Schneider 	switch (urb->status) {
543d08e973aSMaximilian Schneider 	case 0: /* success */
544d08e973aSMaximilian Schneider 		break;
545d08e973aSMaximilian Schneider 	case -ENOENT:
546d08e973aSMaximilian Schneider 	case -ESHUTDOWN:
547d08e973aSMaximilian Schneider 		return;
548d08e973aSMaximilian Schneider 	default:
549d08e973aSMaximilian Schneider 		/* do not resubmit aborted urbs. eg: when device goes down */
550d08e973aSMaximilian Schneider 		return;
551d08e973aSMaximilian Schneider 	}
552d08e973aSMaximilian Schneider 
553d08e973aSMaximilian Schneider 	/* device reports out of range channel id */
554d08e973aSMaximilian Schneider 	if (hf->channel >= GS_MAX_INTF)
5554a8737ffSMarc Kleine-Budde 		goto device_detach;
556d08e973aSMaximilian Schneider 
557d08e973aSMaximilian Schneider 	dev = usbcan->canch[hf->channel];
558d08e973aSMaximilian Schneider 
559d08e973aSMaximilian Schneider 	netdev = dev->netdev;
560d08e973aSMaximilian Schneider 	stats = &netdev->stats;
561d08e973aSMaximilian Schneider 
562d08e973aSMaximilian Schneider 	if (!netif_device_present(netdev))
563d08e973aSMaximilian Schneider 		return;
564d08e973aSMaximilian Schneider 
565*5886e4d5SMarc Kleine-Budde 	if (!netif_running(netdev))
566*5886e4d5SMarc Kleine-Budde 		goto resubmit_urb;
567*5886e4d5SMarc Kleine-Budde 
568d08e973aSMaximilian Schneider 	if (hf->echo_id == -1) { /* normal rx */
56926949ac9SPeter Fink 		if (hf->flags & GS_CAN_FLAG_FD) {
57026949ac9SPeter Fink 			skb = alloc_canfd_skb(dev->netdev, &cfd);
57126949ac9SPeter Fink 			if (!skb)
57226949ac9SPeter Fink 				return;
57326949ac9SPeter Fink 
57426949ac9SPeter Fink 			cfd->can_id = le32_to_cpu(hf->can_id);
57526949ac9SPeter Fink 			cfd->len = can_fd_dlc2len(hf->can_dlc);
57626949ac9SPeter Fink 			if (hf->flags & GS_CAN_FLAG_BRS)
57726949ac9SPeter Fink 				cfd->flags |= CANFD_BRS;
57826949ac9SPeter Fink 			if (hf->flags & GS_CAN_FLAG_ESI)
57926949ac9SPeter Fink 				cfd->flags |= CANFD_ESI;
58026949ac9SPeter Fink 
58126949ac9SPeter Fink 			memcpy(cfd->data, hf->canfd->data, cfd->len);
58226949ac9SPeter Fink 		} else {
583d08e973aSMaximilian Schneider 			skb = alloc_can_skb(dev->netdev, &cf);
584d08e973aSMaximilian Schneider 			if (!skb)
585d08e973aSMaximilian Schneider 				return;
586d08e973aSMaximilian Schneider 
5874ba1cb39SMarc Kleine-Budde 			cf->can_id = le32_to_cpu(hf->can_id);
5884c01fc87SOliver Hartkopp 			can_frame_set_cc_len(cf, hf->can_dlc, dev->can.ctrlmode);
58926949ac9SPeter Fink 
590c359931dSPeter Fink 			memcpy(cf->data, hf->classic_can->data, 8);
591d08e973aSMaximilian Schneider 
592d08e973aSMaximilian Schneider 			/* ERROR frames tell us information about the controller */
5934ba1cb39SMarc Kleine-Budde 			if (le32_to_cpu(hf->can_id) & CAN_ERR_FLAG)
594d08e973aSMaximilian Schneider 				gs_update_state(dev, cf);
59526949ac9SPeter Fink 		}
596d08e973aSMaximilian Schneider 
59745dfa45fSJohn Whittington 		gs_usb_set_timestamp(dev, skb, hf);
59845dfa45fSJohn Whittington 
599d08e973aSMaximilian Schneider 		netdev->stats.rx_packets++;
600d08e973aSMaximilian Schneider 		netdev->stats.rx_bytes += hf->can_dlc;
601d08e973aSMaximilian Schneider 
602d08e973aSMaximilian Schneider 		netif_rx(skb);
603d08e973aSMaximilian Schneider 	} else { /* echo_id == hf->echo_id */
604d08e973aSMaximilian Schneider 		if (hf->echo_id >= GS_MAX_TX_URBS) {
605d08e973aSMaximilian Schneider 			netdev_err(netdev,
60610819466SMarc Kleine-Budde 				   "Unexpected out of range echo id %u\n",
607d08e973aSMaximilian Schneider 				   hf->echo_id);
608d08e973aSMaximilian Schneider 			goto resubmit_urb;
609d08e973aSMaximilian Schneider 		}
610d08e973aSMaximilian Schneider 
611d08e973aSMaximilian Schneider 		txc = gs_get_tx_context(dev, hf->echo_id);
612d08e973aSMaximilian Schneider 
613d08e973aSMaximilian Schneider 		/* bad devices send bad echo_ids. */
614d08e973aSMaximilian Schneider 		if (!txc) {
615d08e973aSMaximilian Schneider 			netdev_err(netdev,
61610819466SMarc Kleine-Budde 				   "Unexpected unused echo id %u\n",
617d08e973aSMaximilian Schneider 				   hf->echo_id);
618d08e973aSMaximilian Schneider 			goto resubmit_urb;
619d08e973aSMaximilian Schneider 		}
620d08e973aSMaximilian Schneider 
62145dfa45fSJohn Whittington 		skb = dev->can.echo_skb[hf->echo_id];
62245dfa45fSJohn Whittington 		gs_usb_set_timestamp(dev, skb, hf);
62345dfa45fSJohn Whittington 
624cc4b08c3SVincent Mailhol 		netdev->stats.tx_packets++;
625cc4b08c3SVincent Mailhol 		netdev->stats.tx_bytes += can_get_echo_skb(netdev, hf->echo_id,
626cc4b08c3SVincent Mailhol 							   NULL);
627d08e973aSMaximilian Schneider 
628d08e973aSMaximilian Schneider 		gs_free_tx_context(txc);
629d08e973aSMaximilian Schneider 
63097819f94SWolfgang Grandegger 		atomic_dec(&dev->active_tx_urbs);
63197819f94SWolfgang Grandegger 
632d08e973aSMaximilian Schneider 		netif_wake_queue(netdev);
633d08e973aSMaximilian Schneider 	}
634d08e973aSMaximilian Schneider 
635d08e973aSMaximilian Schneider 	if (hf->flags & GS_CAN_FLAG_OVERFLOW) {
636d08e973aSMaximilian Schneider 		skb = alloc_can_err_skb(netdev, &cf);
637d08e973aSMaximilian Schneider 		if (!skb)
638d08e973aSMaximilian Schneider 			goto resubmit_urb;
639d08e973aSMaximilian Schneider 
640d08e973aSMaximilian Schneider 		cf->can_id |= CAN_ERR_CRTL;
641c7b74967SOliver Hartkopp 		cf->len = CAN_ERR_DLC;
642d08e973aSMaximilian Schneider 		cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
643d08e973aSMaximilian Schneider 		stats->rx_over_errors++;
644d08e973aSMaximilian Schneider 		stats->rx_errors++;
645d08e973aSMaximilian Schneider 		netif_rx(skb);
646d08e973aSMaximilian Schneider 	}
647d08e973aSMaximilian Schneider 
648d08e973aSMaximilian Schneider  resubmit_urb:
649c1ee7269SMarc Kleine-Budde 	usb_fill_bulk_urb(urb, usbcan->udev,
65049c007b9SMarc Kleine-Budde 			  usb_rcvbulkpipe(usbcan->udev, GS_USB_ENDPOINT_IN),
651c359931dSPeter Fink 			  hf, dev->parent->hf_size_rx,
652c1ee7269SMarc Kleine-Budde 			  gs_usb_receive_bulk_callback, usbcan);
653d08e973aSMaximilian Schneider 
654d08e973aSMaximilian Schneider 	rc = usb_submit_urb(urb, GFP_ATOMIC);
655d08e973aSMaximilian Schneider 
656d08e973aSMaximilian Schneider 	/* USB failure take down all interfaces */
657d08e973aSMaximilian Schneider 	if (rc == -ENODEV) {
6584a8737ffSMarc Kleine-Budde  device_detach:
659d08e973aSMaximilian Schneider 		for (rc = 0; rc < GS_MAX_INTF; rc++) {
660d08e973aSMaximilian Schneider 			if (usbcan->canch[rc])
661d08e973aSMaximilian Schneider 				netif_device_detach(usbcan->canch[rc]->netdev);
662d08e973aSMaximilian Schneider 		}
663d08e973aSMaximilian Schneider 	}
664d08e973aSMaximilian Schneider }
665d08e973aSMaximilian Schneider 
666d08e973aSMaximilian Schneider static int gs_usb_set_bittiming(struct net_device *netdev)
667d08e973aSMaximilian Schneider {
668d08e973aSMaximilian Schneider 	struct gs_can *dev = netdev_priv(netdev);
669d08e973aSMaximilian Schneider 	struct can_bittiming *bt = &dev->can.bittiming;
6703814ed27SMarc Kleine-Budde 	struct gs_device_bittiming dbt = {
6713814ed27SMarc Kleine-Budde 		.prop_seg = cpu_to_le32(bt->prop_seg),
6723814ed27SMarc Kleine-Budde 		.phase_seg1 = cpu_to_le32(bt->phase_seg1),
6733814ed27SMarc Kleine-Budde 		.phase_seg2 = cpu_to_le32(bt->phase_seg2),
6743814ed27SMarc Kleine-Budde 		.sjw = cpu_to_le32(bt->sjw),
6753814ed27SMarc Kleine-Budde 		.brp = cpu_to_le32(bt->brp),
6763814ed27SMarc Kleine-Budde 	};
677d08e973aSMaximilian Schneider 
678d08e973aSMaximilian Schneider 	/* request bit timings */
67956c56a30SVincent Mailhol 	return usb_control_msg_send(dev->udev, 0, GS_USB_BREQ_BITTIMING,
680d08e973aSMaximilian Schneider 				    USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
6813814ed27SMarc Kleine-Budde 				    dev->channel, 0, &dbt, sizeof(dbt), 1000,
6823814ed27SMarc Kleine-Budde 				    GFP_KERNEL);
683d08e973aSMaximilian Schneider }
684d08e973aSMaximilian Schneider 
68526949ac9SPeter Fink static int gs_usb_set_data_bittiming(struct net_device *netdev)
68626949ac9SPeter Fink {
68726949ac9SPeter Fink 	struct gs_can *dev = netdev_priv(netdev);
68826949ac9SPeter Fink 	struct can_bittiming *bt = &dev->can.data_bittiming;
6893814ed27SMarc Kleine-Budde 	struct gs_device_bittiming dbt = {
6903814ed27SMarc Kleine-Budde 		.prop_seg = cpu_to_le32(bt->prop_seg),
6913814ed27SMarc Kleine-Budde 		.phase_seg1 = cpu_to_le32(bt->phase_seg1),
6923814ed27SMarc Kleine-Budde 		.phase_seg2 = cpu_to_le32(bt->phase_seg2),
6933814ed27SMarc Kleine-Budde 		.sjw = cpu_to_le32(bt->sjw),
6943814ed27SMarc Kleine-Budde 		.brp = cpu_to_le32(bt->brp),
6953814ed27SMarc Kleine-Budde 	};
69632cd9013SMarc Kleine-Budde 	u8 request = GS_USB_BREQ_DATA_BITTIMING;
69726949ac9SPeter Fink 
69832cd9013SMarc Kleine-Budde 	if (dev->feature & GS_CAN_FEATURE_QUIRK_BREQ_CANTACT_PRO)
69932cd9013SMarc Kleine-Budde 		request = GS_USB_BREQ_QUIRK_CANTACT_PRO_DATA_BITTIMING;
70032cd9013SMarc Kleine-Budde 
7013814ed27SMarc Kleine-Budde 	/* request data bit timings */
70256c56a30SVincent Mailhol 	return usb_control_msg_send(dev->udev, 0, request,
70326949ac9SPeter Fink 				    USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
7043814ed27SMarc Kleine-Budde 				    dev->channel, 0, &dbt, sizeof(dbt), 1000,
7053814ed27SMarc Kleine-Budde 				    GFP_KERNEL);
70626949ac9SPeter Fink }
70726949ac9SPeter Fink 
708d08e973aSMaximilian Schneider static void gs_usb_xmit_callback(struct urb *urb)
709d08e973aSMaximilian Schneider {
710d08e973aSMaximilian Schneider 	struct gs_tx_context *txc = urb->context;
711d08e973aSMaximilian Schneider 	struct gs_can *dev = txc->dev;
712d08e973aSMaximilian Schneider 	struct net_device *netdev = dev->netdev;
713d08e973aSMaximilian Schneider 
714d08e973aSMaximilian Schneider 	if (urb->status)
71510819466SMarc Kleine-Budde 		netdev_info(netdev, "usb xmit fail %u\n", txc->echo_id);
716d08e973aSMaximilian Schneider }
717d08e973aSMaximilian Schneider 
7189be95c91SMaximilian Schneider static netdev_tx_t gs_can_start_xmit(struct sk_buff *skb,
7199be95c91SMaximilian Schneider 				     struct net_device *netdev)
720d08e973aSMaximilian Schneider {
721d08e973aSMaximilian Schneider 	struct gs_can *dev = netdev_priv(netdev);
722d08e973aSMaximilian Schneider 	struct net_device_stats *stats = &dev->netdev->stats;
723d08e973aSMaximilian Schneider 	struct urb *urb;
724d08e973aSMaximilian Schneider 	struct gs_host_frame *hf;
725d08e973aSMaximilian Schneider 	struct can_frame *cf;
72626949ac9SPeter Fink 	struct canfd_frame *cfd;
727d08e973aSMaximilian Schneider 	int rc;
728d08e973aSMaximilian Schneider 	unsigned int idx;
729d08e973aSMaximilian Schneider 	struct gs_tx_context *txc;
730d08e973aSMaximilian Schneider 
731ae64438bSOliver Hartkopp 	if (can_dev_dropped_skb(netdev, skb))
732d08e973aSMaximilian Schneider 		return NETDEV_TX_OK;
733d08e973aSMaximilian Schneider 
734d08e973aSMaximilian Schneider 	/* find an empty context to keep track of transmission */
735d08e973aSMaximilian Schneider 	txc = gs_alloc_tx_context(dev);
736d08e973aSMaximilian Schneider 	if (!txc)
737d08e973aSMaximilian Schneider 		return NETDEV_TX_BUSY;
738d08e973aSMaximilian Schneider 
739d08e973aSMaximilian Schneider 	/* create a URB, and a buffer for it */
740d08e973aSMaximilian Schneider 	urb = usb_alloc_urb(0, GFP_ATOMIC);
7412a4a1e40SWolfram Sang 	if (!urb)
742d08e973aSMaximilian Schneider 		goto nomem_urb;
743d08e973aSMaximilian Schneider 
74462f102c0SVasanth Sadhasivan 	hf = kmalloc(dev->hf_size_tx, GFP_ATOMIC);
745d08e973aSMaximilian Schneider 	if (!hf) {
746d08e973aSMaximilian Schneider 		netdev_err(netdev, "No memory left for USB buffer\n");
747d08e973aSMaximilian Schneider 		goto nomem_hf;
748d08e973aSMaximilian Schneider 	}
749d08e973aSMaximilian Schneider 
750d08e973aSMaximilian Schneider 	idx = txc->echo_id;
751d08e973aSMaximilian Schneider 
752d08e973aSMaximilian Schneider 	if (idx >= GS_MAX_TX_URBS) {
75310819466SMarc Kleine-Budde 		netdev_err(netdev, "Invalid tx context %u\n", idx);
754d08e973aSMaximilian Schneider 		goto badidx;
755d08e973aSMaximilian Schneider 	}
756d08e973aSMaximilian Schneider 
757d08e973aSMaximilian Schneider 	hf->echo_id = idx;
758d08e973aSMaximilian Schneider 	hf->channel = dev->channel;
75989d58aebSBrian Silverman 	hf->flags = 0;
76089d58aebSBrian Silverman 	hf->reserved = 0;
761d08e973aSMaximilian Schneider 
76226949ac9SPeter Fink 	if (can_is_canfd_skb(skb)) {
76326949ac9SPeter Fink 		cfd = (struct canfd_frame *)skb->data;
76426949ac9SPeter Fink 
76526949ac9SPeter Fink 		hf->can_id = cpu_to_le32(cfd->can_id);
76626949ac9SPeter Fink 		hf->can_dlc = can_fd_len2dlc(cfd->len);
76726949ac9SPeter Fink 		hf->flags |= GS_CAN_FLAG_FD;
76826949ac9SPeter Fink 		if (cfd->flags & CANFD_BRS)
76926949ac9SPeter Fink 			hf->flags |= GS_CAN_FLAG_BRS;
77026949ac9SPeter Fink 		if (cfd->flags & CANFD_ESI)
77126949ac9SPeter Fink 			hf->flags |= GS_CAN_FLAG_ESI;
77226949ac9SPeter Fink 
77326949ac9SPeter Fink 		memcpy(hf->canfd->data, cfd->data, cfd->len);
77426949ac9SPeter Fink 	} else {
775d08e973aSMaximilian Schneider 		cf = (struct can_frame *)skb->data;
776d08e973aSMaximilian Schneider 
7774ba1cb39SMarc Kleine-Budde 		hf->can_id = cpu_to_le32(cf->can_id);
7784c01fc87SOliver Hartkopp 		hf->can_dlc = can_get_cc_dlc(cf, dev->can.ctrlmode);
7794c01fc87SOliver Hartkopp 
780c359931dSPeter Fink 		memcpy(hf->classic_can->data, cf->data, cf->len);
78126949ac9SPeter Fink 	}
782d08e973aSMaximilian Schneider 
783d08e973aSMaximilian Schneider 	usb_fill_bulk_urb(urb, dev->udev,
78449c007b9SMarc Kleine-Budde 			  usb_sndbulkpipe(dev->udev, GS_USB_ENDPOINT_OUT),
785c359931dSPeter Fink 			  hf, dev->hf_size_tx,
786c1ee7269SMarc Kleine-Budde 			  gs_usb_xmit_callback, txc);
787d08e973aSMaximilian Schneider 
78862f102c0SVasanth Sadhasivan 	urb->transfer_flags |= URB_FREE_BUFFER;
789d08e973aSMaximilian Schneider 	usb_anchor_urb(urb, &dev->tx_submitted);
790d08e973aSMaximilian Schneider 
7911dcb6e57SVincent Mailhol 	can_put_echo_skb(skb, netdev, idx, 0);
792d08e973aSMaximilian Schneider 
793d08e973aSMaximilian Schneider 	atomic_inc(&dev->active_tx_urbs);
794d08e973aSMaximilian Schneider 
795d08e973aSMaximilian Schneider 	rc = usb_submit_urb(urb, GFP_ATOMIC);
796d08e973aSMaximilian Schneider 	if (unlikely(rc)) {			/* usb send failed */
797d08e973aSMaximilian Schneider 		atomic_dec(&dev->active_tx_urbs);
798d08e973aSMaximilian Schneider 
799f318482aSMarc Kleine-Budde 		can_free_echo_skb(netdev, idx, NULL);
800d08e973aSMaximilian Schneider 		gs_free_tx_context(txc);
801d08e973aSMaximilian Schneider 
802d08e973aSMaximilian Schneider 		usb_unanchor_urb(urb);
803d08e973aSMaximilian Schneider 
804d08e973aSMaximilian Schneider 		if (rc == -ENODEV) {
805d08e973aSMaximilian Schneider 			netif_device_detach(netdev);
806d08e973aSMaximilian Schneider 		} else {
807d08e973aSMaximilian Schneider 			netdev_err(netdev, "usb_submit failed (err=%d)\n", rc);
808d08e973aSMaximilian Schneider 			stats->tx_dropped++;
809d08e973aSMaximilian Schneider 		}
810d08e973aSMaximilian Schneider 	} else {
811d08e973aSMaximilian Schneider 		/* Slow down tx path */
812d08e973aSMaximilian Schneider 		if (atomic_read(&dev->active_tx_urbs) >= GS_MAX_TX_URBS)
813d08e973aSMaximilian Schneider 			netif_stop_queue(netdev);
814d08e973aSMaximilian Schneider 	}
815d08e973aSMaximilian Schneider 
816d08e973aSMaximilian Schneider 	/* let usb core take care of this urb */
817d08e973aSMaximilian Schneider 	usb_free_urb(urb);
818d08e973aSMaximilian Schneider 
819d08e973aSMaximilian Schneider 	return NETDEV_TX_OK;
820d08e973aSMaximilian Schneider 
821d08e973aSMaximilian Schneider  badidx:
82262f102c0SVasanth Sadhasivan 	kfree(hf);
823d08e973aSMaximilian Schneider  nomem_hf:
824d08e973aSMaximilian Schneider 	usb_free_urb(urb);
825d08e973aSMaximilian Schneider 
826d08e973aSMaximilian Schneider  nomem_urb:
827d08e973aSMaximilian Schneider 	gs_free_tx_context(txc);
828d08e973aSMaximilian Schneider 	dev_kfree_skb(skb);
829d08e973aSMaximilian Schneider 	stats->tx_dropped++;
830d08e973aSMaximilian Schneider 	return NETDEV_TX_OK;
831d08e973aSMaximilian Schneider }
832d08e973aSMaximilian Schneider 
833d08e973aSMaximilian Schneider static int gs_can_open(struct net_device *netdev)
834d08e973aSMaximilian Schneider {
835d08e973aSMaximilian Schneider 	struct gs_can *dev = netdev_priv(netdev);
836d08e973aSMaximilian Schneider 	struct gs_usb *parent = dev->parent;
8373814ed27SMarc Kleine-Budde 	struct gs_device_mode dm = {
8383814ed27SMarc Kleine-Budde 		.mode = cpu_to_le32(GS_CAN_MODE_START),
8393814ed27SMarc Kleine-Budde 	};
840c359931dSPeter Fink 	struct gs_host_frame *hf;
8412603be9eSMarc Kleine-Budde 	struct urb *urb = NULL;
842d08e973aSMaximilian Schneider 	u32 ctrlmode;
8434ba1cb39SMarc Kleine-Budde 	u32 flags = 0;
8443814ed27SMarc Kleine-Budde 	int rc, i;
845d08e973aSMaximilian Schneider 
846d08e973aSMaximilian Schneider 	rc = open_candev(netdev);
847d08e973aSMaximilian Schneider 	if (rc)
848d08e973aSMaximilian Schneider 		return rc;
849d08e973aSMaximilian Schneider 
85026949ac9SPeter Fink 	ctrlmode = dev->can.ctrlmode;
85126949ac9SPeter Fink 	if (ctrlmode & CAN_CTRLMODE_FD) {
852eb9fa77aSPeter Fink 		if (dev->feature & GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX)
853eb9fa77aSPeter Fink 			dev->hf_size_tx = struct_size(hf, canfd_quirk, 1);
854eb9fa77aSPeter Fink 		else
85526949ac9SPeter Fink 			dev->hf_size_tx = struct_size(hf, canfd, 1);
85626949ac9SPeter Fink 	} else {
857eb9fa77aSPeter Fink 		if (dev->feature & GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX)
858eb9fa77aSPeter Fink 			dev->hf_size_tx = struct_size(hf, classic_can_quirk, 1);
859eb9fa77aSPeter Fink 		else
860c359931dSPeter Fink 			dev->hf_size_tx = struct_size(hf, classic_can, 1);
86126949ac9SPeter Fink 	}
862c359931dSPeter Fink 
863035b0fcfSVincent Mailhol 	if (!parent->active_channels) {
864*5886e4d5SMarc Kleine-Budde 		if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
865*5886e4d5SMarc Kleine-Budde 			gs_usb_timestamp_init(parent);
866*5886e4d5SMarc Kleine-Budde 
867d08e973aSMaximilian Schneider 		for (i = 0; i < GS_MAX_RX_URBS; i++) {
868d08e973aSMaximilian Schneider 			u8 *buf;
869d08e973aSMaximilian Schneider 
870d08e973aSMaximilian Schneider 			/* alloc rx urb */
871d08e973aSMaximilian Schneider 			urb = usb_alloc_urb(0, GFP_KERNEL);
8722603be9eSMarc Kleine-Budde 			if (!urb) {
8732603be9eSMarc Kleine-Budde 				rc = -ENOMEM;
8742603be9eSMarc Kleine-Budde 				goto out_usb_kill_anchored_urbs;
8752603be9eSMarc Kleine-Budde 			}
876d08e973aSMaximilian Schneider 
877d08e973aSMaximilian Schneider 			/* alloc rx buffer */
87862f102c0SVasanth Sadhasivan 			buf = kmalloc(dev->parent->hf_size_rx,
87962f102c0SVasanth Sadhasivan 				      GFP_KERNEL);
880d08e973aSMaximilian Schneider 			if (!buf) {
881d08e973aSMaximilian Schneider 				netdev_err(netdev,
882d08e973aSMaximilian Schneider 					   "No memory left for USB buffer\n");
8832603be9eSMarc Kleine-Budde 				rc = -ENOMEM;
8842603be9eSMarc Kleine-Budde 				goto out_usb_free_urb;
885d08e973aSMaximilian Schneider 			}
886d08e973aSMaximilian Schneider 
887d08e973aSMaximilian Schneider 			/* fill, anchor, and submit rx urb */
888d08e973aSMaximilian Schneider 			usb_fill_bulk_urb(urb,
889d08e973aSMaximilian Schneider 					  dev->udev,
890d08e973aSMaximilian Schneider 					  usb_rcvbulkpipe(dev->udev,
89149c007b9SMarc Kleine-Budde 							  GS_USB_ENDPOINT_IN),
892d08e973aSMaximilian Schneider 					  buf,
893c359931dSPeter Fink 					  dev->parent->hf_size_rx,
894c1ee7269SMarc Kleine-Budde 					  gs_usb_receive_bulk_callback, parent);
89562f102c0SVasanth Sadhasivan 			urb->transfer_flags |= URB_FREE_BUFFER;
896d08e973aSMaximilian Schneider 
897d08e973aSMaximilian Schneider 			usb_anchor_urb(urb, &parent->rx_submitted);
898d08e973aSMaximilian Schneider 
899d08e973aSMaximilian Schneider 			rc = usb_submit_urb(urb, GFP_KERNEL);
900d08e973aSMaximilian Schneider 			if (rc) {
901d08e973aSMaximilian Schneider 				if (rc == -ENODEV)
902d08e973aSMaximilian Schneider 					netif_device_detach(dev->netdev);
903d08e973aSMaximilian Schneider 
904d08e973aSMaximilian Schneider 				netdev_err(netdev,
905d0cd2aa8SMarc Kleine-Budde 					   "usb_submit failed (err=%d)\n", rc);
906d08e973aSMaximilian Schneider 
9072603be9eSMarc Kleine-Budde 				goto out_usb_unanchor_urb;
908d08e973aSMaximilian Schneider 			}
909d08e973aSMaximilian Schneider 
910d08e973aSMaximilian Schneider 			/* Drop reference,
911d08e973aSMaximilian Schneider 			 * USB core will take care of freeing it
912d08e973aSMaximilian Schneider 			 */
913d08e973aSMaximilian Schneider 			usb_free_urb(urb);
914d08e973aSMaximilian Schneider 		}
915d08e973aSMaximilian Schneider 	}
916d08e973aSMaximilian Schneider 
917d08e973aSMaximilian Schneider 	/* flags */
918d08e973aSMaximilian Schneider 	if (ctrlmode & CAN_CTRLMODE_LOOPBACK)
9194ba1cb39SMarc Kleine-Budde 		flags |= GS_CAN_MODE_LOOP_BACK;
920deb8534eSMarc Kleine-Budde 
921deb8534eSMarc Kleine-Budde 	if (ctrlmode & CAN_CTRLMODE_LISTENONLY)
9224ba1cb39SMarc Kleine-Budde 		flags |= GS_CAN_MODE_LISTEN_ONLY;
923d08e973aSMaximilian Schneider 
924f6adf410SMarc Kleine-Budde 	if (ctrlmode & CAN_CTRLMODE_3_SAMPLES)
925f6adf410SMarc Kleine-Budde 		flags |= GS_CAN_MODE_TRIPLE_SAMPLE;
926f6adf410SMarc Kleine-Budde 
927d08e973aSMaximilian Schneider 	if (ctrlmode & CAN_CTRLMODE_ONE_SHOT)
9284ba1cb39SMarc Kleine-Budde 		flags |= GS_CAN_MODE_ONE_SHOT;
929d08e973aSMaximilian Schneider 
9302f3cdad1SJeroen Hofstee 	if (ctrlmode & CAN_CTRLMODE_BERR_REPORTING)
9312f3cdad1SJeroen Hofstee 		flags |= GS_CAN_MODE_BERR_REPORTING;
9322f3cdad1SJeroen Hofstee 
933f6adf410SMarc Kleine-Budde 	if (ctrlmode & CAN_CTRLMODE_FD)
934f6adf410SMarc Kleine-Budde 		flags |= GS_CAN_MODE_FD;
935d08e973aSMaximilian Schneider 
93645dfa45fSJohn Whittington 	/* if hardware supports timestamps, enable it */
937*5886e4d5SMarc Kleine-Budde 	if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
93845dfa45fSJohn Whittington 		flags |= GS_CAN_MODE_HW_TIMESTAMP;
93945dfa45fSJohn Whittington 
940d08e973aSMaximilian Schneider 	/* finally start device */
9415440428bSMarc Kleine-Budde 	dev->can.state = CAN_STATE_ERROR_ACTIVE;
9423814ed27SMarc Kleine-Budde 	dm.flags = cpu_to_le32(flags);
94356c56a30SVincent Mailhol 	rc = usb_control_msg_send(dev->udev, 0, GS_USB_BREQ_MODE,
944c1ee7269SMarc Kleine-Budde 				  USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
9453814ed27SMarc Kleine-Budde 				  dev->channel, 0, &dm, sizeof(dm), 1000,
9463814ed27SMarc Kleine-Budde 				  GFP_KERNEL);
9473814ed27SMarc Kleine-Budde 	if (rc) {
948d08e973aSMaximilian Schneider 		netdev_err(netdev, "Couldn't start device (err=%d)\n", rc);
9495440428bSMarc Kleine-Budde 		dev->can.state = CAN_STATE_STOPPED;
9502603be9eSMarc Kleine-Budde 
9512603be9eSMarc Kleine-Budde 		goto out_usb_kill_anchored_urbs;
952d08e973aSMaximilian Schneider 	}
953d08e973aSMaximilian Schneider 
954035b0fcfSVincent Mailhol 	parent->active_channels++;
955d08e973aSMaximilian Schneider 	if (!(dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY))
956d08e973aSMaximilian Schneider 		netif_start_queue(netdev);
957d08e973aSMaximilian Schneider 
958d08e973aSMaximilian Schneider 	return 0;
9592603be9eSMarc Kleine-Budde 
9602603be9eSMarc Kleine-Budde out_usb_unanchor_urb:
9612603be9eSMarc Kleine-Budde 	usb_unanchor_urb(urb);
9622603be9eSMarc Kleine-Budde out_usb_free_urb:
9632603be9eSMarc Kleine-Budde 	usb_free_urb(urb);
9642603be9eSMarc Kleine-Budde out_usb_kill_anchored_urbs:
965*5886e4d5SMarc Kleine-Budde 	if (!parent->active_channels) {
9662603be9eSMarc Kleine-Budde 		usb_kill_anchored_urbs(&dev->tx_submitted);
9672603be9eSMarc Kleine-Budde 
968*5886e4d5SMarc Kleine-Budde 		if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
969*5886e4d5SMarc Kleine-Budde 			gs_usb_timestamp_stop(parent);
970*5886e4d5SMarc Kleine-Budde 	}
971*5886e4d5SMarc Kleine-Budde 
9722603be9eSMarc Kleine-Budde 	close_candev(netdev);
9732603be9eSMarc Kleine-Budde 
9742603be9eSMarc Kleine-Budde 	return rc;
975d08e973aSMaximilian Schneider }
976d08e973aSMaximilian Schneider 
9770c9f92a4SJeroen Hofstee static int gs_usb_get_state(const struct net_device *netdev,
9780c9f92a4SJeroen Hofstee 			    struct can_berr_counter *bec,
9790c9f92a4SJeroen Hofstee 			    enum can_state *state)
9800c9f92a4SJeroen Hofstee {
9810c9f92a4SJeroen Hofstee 	struct gs_can *dev = netdev_priv(netdev);
9820c9f92a4SJeroen Hofstee 	struct gs_device_state ds;
9830c9f92a4SJeroen Hofstee 	int rc;
9840c9f92a4SJeroen Hofstee 
98556c56a30SVincent Mailhol 	rc = usb_control_msg_recv(dev->udev, 0, GS_USB_BREQ_GET_STATE,
9860c9f92a4SJeroen Hofstee 				  USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
9870c9f92a4SJeroen Hofstee 				  dev->channel, 0,
9880c9f92a4SJeroen Hofstee 				  &ds, sizeof(ds),
9890c9f92a4SJeroen Hofstee 				  USB_CTRL_GET_TIMEOUT,
9900c9f92a4SJeroen Hofstee 				  GFP_KERNEL);
9910c9f92a4SJeroen Hofstee 	if (rc)
9920c9f92a4SJeroen Hofstee 		return rc;
9930c9f92a4SJeroen Hofstee 
9940c9f92a4SJeroen Hofstee 	if (le32_to_cpu(ds.state) >= CAN_STATE_MAX)
9950c9f92a4SJeroen Hofstee 		return -EOPNOTSUPP;
9960c9f92a4SJeroen Hofstee 
9970c9f92a4SJeroen Hofstee 	*state = le32_to_cpu(ds.state);
9980c9f92a4SJeroen Hofstee 	bec->txerr = le32_to_cpu(ds.txerr);
9990c9f92a4SJeroen Hofstee 	bec->rxerr = le32_to_cpu(ds.rxerr);
10000c9f92a4SJeroen Hofstee 
10010c9f92a4SJeroen Hofstee 	return 0;
10020c9f92a4SJeroen Hofstee }
10030c9f92a4SJeroen Hofstee 
10040c9f92a4SJeroen Hofstee static int gs_usb_can_get_berr_counter(const struct net_device *netdev,
10050c9f92a4SJeroen Hofstee 				       struct can_berr_counter *bec)
10060c9f92a4SJeroen Hofstee {
10070c9f92a4SJeroen Hofstee 	enum can_state state;
10080c9f92a4SJeroen Hofstee 
10090c9f92a4SJeroen Hofstee 	return gs_usb_get_state(netdev, bec, &state);
10100c9f92a4SJeroen Hofstee }
10110c9f92a4SJeroen Hofstee 
1012d08e973aSMaximilian Schneider static int gs_can_close(struct net_device *netdev)
1013d08e973aSMaximilian Schneider {
1014d08e973aSMaximilian Schneider 	int rc;
1015d08e973aSMaximilian Schneider 	struct gs_can *dev = netdev_priv(netdev);
1016d08e973aSMaximilian Schneider 	struct gs_usb *parent = dev->parent;
1017d08e973aSMaximilian Schneider 
1018d08e973aSMaximilian Schneider 	netif_stop_queue(netdev);
1019d08e973aSMaximilian Schneider 
1020d08e973aSMaximilian Schneider 	/* Stop polling */
1021035b0fcfSVincent Mailhol 	parent->active_channels--;
10222bda24efSRhett Aultman 	if (!parent->active_channels) {
1023d08e973aSMaximilian Schneider 		usb_kill_anchored_urbs(&parent->rx_submitted);
1024*5886e4d5SMarc Kleine-Budde 
1025*5886e4d5SMarc Kleine-Budde 		if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
1026*5886e4d5SMarc Kleine-Budde 			gs_usb_timestamp_stop(parent);
10272bda24efSRhett Aultman 	}
1028d08e973aSMaximilian Schneider 
1029d08e973aSMaximilian Schneider 	/* Stop sending URBs */
1030d08e973aSMaximilian Schneider 	usb_kill_anchored_urbs(&dev->tx_submitted);
1031d08e973aSMaximilian Schneider 	atomic_set(&dev->active_tx_urbs, 0);
1032d08e973aSMaximilian Schneider 
1033d08e973aSMaximilian Schneider 	/* reset the device */
1034dda8deb6SMarc Kleine-Budde 	rc = gs_cmd_reset(dev);
1035d08e973aSMaximilian Schneider 	if (rc < 0)
1036d08e973aSMaximilian Schneider 		netdev_warn(netdev, "Couldn't shutdown device (err=%d)", rc);
1037d08e973aSMaximilian Schneider 
1038d08e973aSMaximilian Schneider 	/* reset tx contexts */
1039d08e973aSMaximilian Schneider 	for (rc = 0; rc < GS_MAX_TX_URBS; rc++) {
1040d08e973aSMaximilian Schneider 		dev->tx_context[rc].dev = dev;
1041d08e973aSMaximilian Schneider 		dev->tx_context[rc].echo_id = GS_MAX_TX_URBS;
1042d08e973aSMaximilian Schneider 	}
1043d08e973aSMaximilian Schneider 
1044d08e973aSMaximilian Schneider 	/* close the netdev */
1045d08e973aSMaximilian Schneider 	close_candev(netdev);
1046d08e973aSMaximilian Schneider 
1047d08e973aSMaximilian Schneider 	return 0;
1048d08e973aSMaximilian Schneider }
1049d08e973aSMaximilian Schneider 
105045dfa45fSJohn Whittington static int gs_can_eth_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
105145dfa45fSJohn Whittington {
105245dfa45fSJohn Whittington 	const struct gs_can *dev = netdev_priv(netdev);
105345dfa45fSJohn Whittington 
105445dfa45fSJohn Whittington 	if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
105545dfa45fSJohn Whittington 		return can_eth_ioctl_hwts(netdev, ifr, cmd);
105645dfa45fSJohn Whittington 
105745dfa45fSJohn Whittington 	return -EOPNOTSUPP;
105845dfa45fSJohn Whittington }
105945dfa45fSJohn Whittington 
1060d08e973aSMaximilian Schneider static const struct net_device_ops gs_usb_netdev_ops = {
1061d08e973aSMaximilian Schneider 	.ndo_open = gs_can_open,
1062d08e973aSMaximilian Schneider 	.ndo_stop = gs_can_close,
1063d08e973aSMaximilian Schneider 	.ndo_start_xmit = gs_can_start_xmit,
106450212b42SMarc Kleine-Budde 	.ndo_change_mtu = can_change_mtu,
106545dfa45fSJohn Whittington 	.ndo_eth_ioctl = gs_can_eth_ioctl,
1066d08e973aSMaximilian Schneider };
1067d08e973aSMaximilian Schneider 
106805ca5270SMaximilian Schneider static int gs_usb_set_identify(struct net_device *netdev, bool do_identify)
106905ca5270SMaximilian Schneider {
107005ca5270SMaximilian Schneider 	struct gs_can *dev = netdev_priv(netdev);
10713814ed27SMarc Kleine-Budde 	struct gs_identify_mode imode;
1072b05c73bdSMaksim Salau 
107305ca5270SMaximilian Schneider 	if (do_identify)
10743814ed27SMarc Kleine-Budde 		imode.mode = cpu_to_le32(GS_CAN_IDENTIFY_ON);
107505ca5270SMaximilian Schneider 	else
10763814ed27SMarc Kleine-Budde 		imode.mode = cpu_to_le32(GS_CAN_IDENTIFY_OFF);
107705ca5270SMaximilian Schneider 
107856c56a30SVincent Mailhol 	return usb_control_msg_send(dev->udev, 0, GS_USB_BREQ_IDENTIFY,
1079c1ee7269SMarc Kleine-Budde 				    USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
10803814ed27SMarc Kleine-Budde 				    dev->channel, 0, &imode, sizeof(imode), 100,
10813814ed27SMarc Kleine-Budde 				    GFP_KERNEL);
108205ca5270SMaximilian Schneider }
108305ca5270SMaximilian Schneider 
108405ca5270SMaximilian Schneider /* blink LED's for finding the this interface */
10850f2211f1SMarc Kleine-Budde static int gs_usb_set_phys_id(struct net_device *netdev,
108605ca5270SMaximilian Schneider 			      enum ethtool_phys_id_state state)
108705ca5270SMaximilian Schneider {
10880f2211f1SMarc Kleine-Budde 	const struct gs_can *dev = netdev_priv(netdev);
108905ca5270SMaximilian Schneider 	int rc = 0;
109005ca5270SMaximilian Schneider 
10910f2211f1SMarc Kleine-Budde 	if (!(dev->feature & GS_CAN_FEATURE_IDENTIFY))
10920f2211f1SMarc Kleine-Budde 		return -EOPNOTSUPP;
10930f2211f1SMarc Kleine-Budde 
109405ca5270SMaximilian Schneider 	switch (state) {
109505ca5270SMaximilian Schneider 	case ETHTOOL_ID_ACTIVE:
10960f2211f1SMarc Kleine-Budde 		rc = gs_usb_set_identify(netdev, GS_CAN_IDENTIFY_ON);
109705ca5270SMaximilian Schneider 		break;
109805ca5270SMaximilian Schneider 	case ETHTOOL_ID_INACTIVE:
10990f2211f1SMarc Kleine-Budde 		rc = gs_usb_set_identify(netdev, GS_CAN_IDENTIFY_OFF);
110005ca5270SMaximilian Schneider 		break;
110105ca5270SMaximilian Schneider 	default:
110205ca5270SMaximilian Schneider 		break;
110305ca5270SMaximilian Schneider 	}
110405ca5270SMaximilian Schneider 
110505ca5270SMaximilian Schneider 	return rc;
110605ca5270SMaximilian Schneider }
110705ca5270SMaximilian Schneider 
110845dfa45fSJohn Whittington static int gs_usb_get_ts_info(struct net_device *netdev,
110945dfa45fSJohn Whittington 			      struct ethtool_ts_info *info)
111045dfa45fSJohn Whittington {
111145dfa45fSJohn Whittington 	struct gs_can *dev = netdev_priv(netdev);
111245dfa45fSJohn Whittington 
111345dfa45fSJohn Whittington 	/* report if device supports HW timestamps */
111445dfa45fSJohn Whittington 	if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
111545dfa45fSJohn Whittington 		return can_ethtool_op_get_ts_info_hwts(netdev, info);
111645dfa45fSJohn Whittington 
111745dfa45fSJohn Whittington 	return ethtool_op_get_ts_info(netdev, info);
111845dfa45fSJohn Whittington }
111945dfa45fSJohn Whittington 
112005ca5270SMaximilian Schneider static const struct ethtool_ops gs_usb_ethtool_ops = {
112105ca5270SMaximilian Schneider 	.set_phys_id = gs_usb_set_phys_id,
112245dfa45fSJohn Whittington 	.get_ts_info = gs_usb_get_ts_info,
112305ca5270SMaximilian Schneider };
112405ca5270SMaximilian Schneider 
1125906e0e68SMarc Kleine-Budde static int gs_usb_get_termination(struct net_device *netdev, u16 *term)
1126906e0e68SMarc Kleine-Budde {
1127906e0e68SMarc Kleine-Budde 	struct gs_can *dev = netdev_priv(netdev);
1128906e0e68SMarc Kleine-Budde 	struct gs_device_termination_state term_state;
1129906e0e68SMarc Kleine-Budde 	int rc;
1130906e0e68SMarc Kleine-Budde 
113156c56a30SVincent Mailhol 	rc = usb_control_msg_recv(dev->udev, 0, GS_USB_BREQ_GET_TERMINATION,
1132906e0e68SMarc Kleine-Budde 				  USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1133906e0e68SMarc Kleine-Budde 				  dev->channel, 0,
1134906e0e68SMarc Kleine-Budde 				  &term_state, sizeof(term_state), 1000,
1135906e0e68SMarc Kleine-Budde 				  GFP_KERNEL);
1136906e0e68SMarc Kleine-Budde 	if (rc)
1137906e0e68SMarc Kleine-Budde 		return rc;
1138906e0e68SMarc Kleine-Budde 
1139906e0e68SMarc Kleine-Budde 	if (term_state.state == cpu_to_le32(GS_CAN_TERMINATION_STATE_ON))
1140906e0e68SMarc Kleine-Budde 		*term = GS_USB_TERMINATION_ENABLED;
1141906e0e68SMarc Kleine-Budde 	else
1142906e0e68SMarc Kleine-Budde 		*term = GS_USB_TERMINATION_DISABLED;
1143906e0e68SMarc Kleine-Budde 
1144906e0e68SMarc Kleine-Budde 	return 0;
1145906e0e68SMarc Kleine-Budde }
1146906e0e68SMarc Kleine-Budde 
1147906e0e68SMarc Kleine-Budde static int gs_usb_set_termination(struct net_device *netdev, u16 term)
1148906e0e68SMarc Kleine-Budde {
1149906e0e68SMarc Kleine-Budde 	struct gs_can *dev = netdev_priv(netdev);
1150906e0e68SMarc Kleine-Budde 	struct gs_device_termination_state term_state;
1151906e0e68SMarc Kleine-Budde 
1152906e0e68SMarc Kleine-Budde 	if (term == GS_USB_TERMINATION_ENABLED)
1153906e0e68SMarc Kleine-Budde 		term_state.state = cpu_to_le32(GS_CAN_TERMINATION_STATE_ON);
1154906e0e68SMarc Kleine-Budde 	else
1155906e0e68SMarc Kleine-Budde 		term_state.state = cpu_to_le32(GS_CAN_TERMINATION_STATE_OFF);
1156906e0e68SMarc Kleine-Budde 
115756c56a30SVincent Mailhol 	return usb_control_msg_send(dev->udev, 0, GS_USB_BREQ_SET_TERMINATION,
1158906e0e68SMarc Kleine-Budde 				    USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1159906e0e68SMarc Kleine-Budde 				    dev->channel, 0,
1160906e0e68SMarc Kleine-Budde 				    &term_state, sizeof(term_state), 1000,
1161906e0e68SMarc Kleine-Budde 				    GFP_KERNEL);
1162906e0e68SMarc Kleine-Budde }
1163906e0e68SMarc Kleine-Budde 
1164906e0e68SMarc Kleine-Budde static const u16 gs_usb_termination_const[] = {
1165906e0e68SMarc Kleine-Budde 	GS_USB_TERMINATION_DISABLED,
1166906e0e68SMarc Kleine-Budde 	GS_USB_TERMINATION_ENABLED
1167906e0e68SMarc Kleine-Budde };
1168906e0e68SMarc Kleine-Budde 
116905ca5270SMaximilian Schneider static struct gs_can *gs_make_candev(unsigned int channel,
117005ca5270SMaximilian Schneider 				     struct usb_interface *intf,
117105ca5270SMaximilian Schneider 				     struct gs_device_config *dconf)
1172d08e973aSMaximilian Schneider {
1173d08e973aSMaximilian Schneider 	struct gs_can *dev;
1174d08e973aSMaximilian Schneider 	struct net_device *netdev;
1175d08e973aSMaximilian Schneider 	int rc;
11763814ed27SMarc Kleine-Budde 	struct gs_device_bt_const_extended bt_const_extended;
11773814ed27SMarc Kleine-Budde 	struct gs_device_bt_const bt_const;
11784ba1cb39SMarc Kleine-Budde 	u32 feature;
1179d08e973aSMaximilian Schneider 
1180d08e973aSMaximilian Schneider 	/* fetch bit timing constants */
11813814ed27SMarc Kleine-Budde 	rc = usb_control_msg_recv(interface_to_usbdev(intf), 0,
1182d08e973aSMaximilian Schneider 				  GS_USB_BREQ_BT_CONST,
1183d08e973aSMaximilian Schneider 				  USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
11843814ed27SMarc Kleine-Budde 				  channel, 0, &bt_const, sizeof(bt_const), 1000,
11853814ed27SMarc Kleine-Budde 				  GFP_KERNEL);
1186d08e973aSMaximilian Schneider 
11873814ed27SMarc Kleine-Budde 	if (rc) {
1188d08e973aSMaximilian Schneider 		dev_err(&intf->dev,
118968822f4eSMarc Kleine-Budde 			"Couldn't get bit timing const for channel %d (%pe)\n",
119068822f4eSMarc Kleine-Budde 			channel, ERR_PTR(rc));
1191d08e973aSMaximilian Schneider 		return ERR_PTR(rc);
1192d08e973aSMaximilian Schneider 	}
1193d08e973aSMaximilian Schneider 
1194d08e973aSMaximilian Schneider 	/* create netdev */
1195d08e973aSMaximilian Schneider 	netdev = alloc_candev(sizeof(struct gs_can), GS_MAX_TX_URBS);
1196d08e973aSMaximilian Schneider 	if (!netdev) {
1197d08e973aSMaximilian Schneider 		dev_err(&intf->dev, "Couldn't allocate candev\n");
1198d08e973aSMaximilian Schneider 		return ERR_PTR(-ENOMEM);
1199d08e973aSMaximilian Schneider 	}
1200d08e973aSMaximilian Schneider 
1201d08e973aSMaximilian Schneider 	dev = netdev_priv(netdev);
1202d08e973aSMaximilian Schneider 
1203d08e973aSMaximilian Schneider 	netdev->netdev_ops = &gs_usb_netdev_ops;
1204409c188cSVincent Mailhol 	netdev->ethtool_ops = &gs_usb_ethtool_ops;
1205d08e973aSMaximilian Schneider 
1206d08e973aSMaximilian Schneider 	netdev->flags |= IFF_ECHO; /* we support full roundtrip echo */
1207acff76faSMarc Kleine-Budde 	netdev->dev_id = channel;
1208d08e973aSMaximilian Schneider 
120988bfb9a7SMarc Kleine-Budde 	/* dev setup */
1210c250d5ebSVincent Mailhol 	strcpy(dev->bt_const.name, KBUILD_MODNAME);
12113814ed27SMarc Kleine-Budde 	dev->bt_const.tseg1_min = le32_to_cpu(bt_const.tseg1_min);
12123814ed27SMarc Kleine-Budde 	dev->bt_const.tseg1_max = le32_to_cpu(bt_const.tseg1_max);
12133814ed27SMarc Kleine-Budde 	dev->bt_const.tseg2_min = le32_to_cpu(bt_const.tseg2_min);
12143814ed27SMarc Kleine-Budde 	dev->bt_const.tseg2_max = le32_to_cpu(bt_const.tseg2_max);
12153814ed27SMarc Kleine-Budde 	dev->bt_const.sjw_max = le32_to_cpu(bt_const.sjw_max);
12163814ed27SMarc Kleine-Budde 	dev->bt_const.brp_min = le32_to_cpu(bt_const.brp_min);
12173814ed27SMarc Kleine-Budde 	dev->bt_const.brp_max = le32_to_cpu(bt_const.brp_max);
12183814ed27SMarc Kleine-Budde 	dev->bt_const.brp_inc = le32_to_cpu(bt_const.brp_inc);
1219d08e973aSMaximilian Schneider 
1220d08e973aSMaximilian Schneider 	dev->udev = interface_to_usbdev(intf);
1221d08e973aSMaximilian Schneider 	dev->netdev = netdev;
1222d08e973aSMaximilian Schneider 	dev->channel = channel;
1223d08e973aSMaximilian Schneider 
1224d08e973aSMaximilian Schneider 	init_usb_anchor(&dev->tx_submitted);
1225d08e973aSMaximilian Schneider 	atomic_set(&dev->active_tx_urbs, 0);
1226d08e973aSMaximilian Schneider 	spin_lock_init(&dev->tx_ctx_lock);
1227d08e973aSMaximilian Schneider 	for (rc = 0; rc < GS_MAX_TX_URBS; rc++) {
1228d08e973aSMaximilian Schneider 		dev->tx_context[rc].dev = dev;
1229d08e973aSMaximilian Schneider 		dev->tx_context[rc].echo_id = GS_MAX_TX_URBS;
1230d08e973aSMaximilian Schneider 	}
1231d08e973aSMaximilian Schneider 
123288bfb9a7SMarc Kleine-Budde 	/* can setup */
1233d08e973aSMaximilian Schneider 	dev->can.state = CAN_STATE_STOPPED;
12343814ed27SMarc Kleine-Budde 	dev->can.clock.freq = le32_to_cpu(bt_const.fclk_can);
1235d08e973aSMaximilian Schneider 	dev->can.bittiming_const = &dev->bt_const;
1236d08e973aSMaximilian Schneider 	dev->can.do_set_bittiming = gs_usb_set_bittiming;
1237d08e973aSMaximilian Schneider 
12384c01fc87SOliver Hartkopp 	dev->can.ctrlmode_supported = CAN_CTRLMODE_CC_LEN8_DLC;
1239d08e973aSMaximilian Schneider 
12403814ed27SMarc Kleine-Budde 	feature = le32_to_cpu(bt_const.feature);
1241eb9fa77aSPeter Fink 	dev->feature = FIELD_GET(GS_CAN_FEATURE_MASK, feature);
12424ba1cb39SMarc Kleine-Budde 	if (feature & GS_CAN_FEATURE_LISTEN_ONLY)
1243d08e973aSMaximilian Schneider 		dev->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY;
1244d08e973aSMaximilian Schneider 
12454ba1cb39SMarc Kleine-Budde 	if (feature & GS_CAN_FEATURE_LOOP_BACK)
1246d08e973aSMaximilian Schneider 		dev->can.ctrlmode_supported |= CAN_CTRLMODE_LOOPBACK;
1247d08e973aSMaximilian Schneider 
12484ba1cb39SMarc Kleine-Budde 	if (feature & GS_CAN_FEATURE_TRIPLE_SAMPLE)
1249d08e973aSMaximilian Schneider 		dev->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
1250d08e973aSMaximilian Schneider 
12514ba1cb39SMarc Kleine-Budde 	if (feature & GS_CAN_FEATURE_ONE_SHOT)
1252d08e973aSMaximilian Schneider 		dev->can.ctrlmode_supported |= CAN_CTRLMODE_ONE_SHOT;
1253d08e973aSMaximilian Schneider 
125426949ac9SPeter Fink 	if (feature & GS_CAN_FEATURE_FD) {
125526949ac9SPeter Fink 		dev->can.ctrlmode_supported |= CAN_CTRLMODE_FD;
12566679f4c5SPeter Fink 		/* The data bit timing will be overwritten, if
12576679f4c5SPeter Fink 		 * GS_CAN_FEATURE_BT_CONST_EXT is set.
12586679f4c5SPeter Fink 		 */
125926949ac9SPeter Fink 		dev->can.data_bittiming_const = &dev->bt_const;
126026949ac9SPeter Fink 		dev->can.do_set_data_bittiming = gs_usb_set_data_bittiming;
126126949ac9SPeter Fink 	}
126226949ac9SPeter Fink 
1263906e0e68SMarc Kleine-Budde 	if (feature & GS_CAN_FEATURE_TERMINATION) {
1264906e0e68SMarc Kleine-Budde 		rc = gs_usb_get_termination(netdev, &dev->can.termination);
1265906e0e68SMarc Kleine-Budde 		if (rc) {
1266906e0e68SMarc Kleine-Budde 			dev->feature &= ~GS_CAN_FEATURE_TERMINATION;
1267906e0e68SMarc Kleine-Budde 
1268906e0e68SMarc Kleine-Budde 			dev_info(&intf->dev,
1269906e0e68SMarc Kleine-Budde 				 "Disabling termination support for channel %d (%pe)\n",
1270906e0e68SMarc Kleine-Budde 				 channel, ERR_PTR(rc));
1271906e0e68SMarc Kleine-Budde 		} else {
1272906e0e68SMarc Kleine-Budde 			dev->can.termination_const = gs_usb_termination_const;
1273906e0e68SMarc Kleine-Budde 			dev->can.termination_const_cnt = ARRAY_SIZE(gs_usb_termination_const);
1274906e0e68SMarc Kleine-Budde 			dev->can.do_set_termination = gs_usb_set_termination;
1275906e0e68SMarc Kleine-Budde 		}
1276906e0e68SMarc Kleine-Budde 	}
1277906e0e68SMarc Kleine-Budde 
12782f3cdad1SJeroen Hofstee 	if (feature & GS_CAN_FEATURE_BERR_REPORTING)
12792f3cdad1SJeroen Hofstee 		dev->can.ctrlmode_supported |= CAN_CTRLMODE_BERR_REPORTING;
12802f3cdad1SJeroen Hofstee 
12810c9f92a4SJeroen Hofstee 	if (feature & GS_CAN_FEATURE_GET_STATE)
12820c9f92a4SJeroen Hofstee 		dev->can.do_get_berr_counter = gs_usb_can_get_berr_counter;
12830c9f92a4SJeroen Hofstee 
1284b00ca070SPeter Fink 	/* The CANtact Pro from LinkLayer Labs is based on the
1285b00ca070SPeter Fink 	 * LPC54616 µC, which is affected by the NXP LPC USB transfer
1286b00ca070SPeter Fink 	 * erratum. However, the current firmware (version 2) doesn't
1287b00ca070SPeter Fink 	 * set the GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX bit. Set the
1288b00ca070SPeter Fink 	 * feature GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX to workaround
1289b00ca070SPeter Fink 	 * this issue.
1290b00ca070SPeter Fink 	 *
1291b00ca070SPeter Fink 	 * For the GS_USB_BREQ_DATA_BITTIMING USB control message the
1292b00ca070SPeter Fink 	 * CANtact Pro firmware uses a request value, which is already
1293b00ca070SPeter Fink 	 * used by the candleLight firmware for a different purpose
1294b00ca070SPeter Fink 	 * (GS_USB_BREQ_GET_USER_ID). Set the feature
1295b00ca070SPeter Fink 	 * GS_CAN_FEATURE_QUIRK_BREQ_CANTACT_PRO to workaround this
1296b00ca070SPeter Fink 	 * issue.
1297b00ca070SPeter Fink 	 */
129849c007b9SMarc Kleine-Budde 	if (dev->udev->descriptor.idVendor == cpu_to_le16(USB_GS_USB_1_VENDOR_ID) &&
129949c007b9SMarc Kleine-Budde 	    dev->udev->descriptor.idProduct == cpu_to_le16(USB_GS_USB_1_PRODUCT_ID) &&
1300b00ca070SPeter Fink 	    dev->udev->manufacturer && dev->udev->product &&
1301b00ca070SPeter Fink 	    !strcmp(dev->udev->manufacturer, "LinkLayer Labs") &&
1302b00ca070SPeter Fink 	    !strcmp(dev->udev->product, "CANtact Pro") &&
1303b00ca070SPeter Fink 	    (le32_to_cpu(dconf->sw_version) <= 2))
1304b00ca070SPeter Fink 		dev->feature |= GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX |
1305b00ca070SPeter Fink 			GS_CAN_FEATURE_QUIRK_BREQ_CANTACT_PRO;
1306b00ca070SPeter Fink 
13070f2211f1SMarc Kleine-Budde 	/* GS_CAN_FEATURE_IDENTIFY is only supported for sw_version > 1 */
13080f2211f1SMarc Kleine-Budde 	if (!(le32_to_cpu(dconf->sw_version) > 1 &&
13090f2211f1SMarc Kleine-Budde 	      feature & GS_CAN_FEATURE_IDENTIFY))
13100f2211f1SMarc Kleine-Budde 		dev->feature &= ~GS_CAN_FEATURE_IDENTIFY;
131105ca5270SMaximilian Schneider 
13126679f4c5SPeter Fink 	/* fetch extended bit timing constants if device has feature
13136679f4c5SPeter Fink 	 * GS_CAN_FEATURE_FD and GS_CAN_FEATURE_BT_CONST_EXT
13146679f4c5SPeter Fink 	 */
13156679f4c5SPeter Fink 	if (feature & GS_CAN_FEATURE_FD &&
13166679f4c5SPeter Fink 	    feature & GS_CAN_FEATURE_BT_CONST_EXT) {
13173814ed27SMarc Kleine-Budde 		rc = usb_control_msg_recv(interface_to_usbdev(intf), 0,
13186679f4c5SPeter Fink 					  GS_USB_BREQ_BT_CONST_EXT,
13196679f4c5SPeter Fink 					  USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
13203814ed27SMarc Kleine-Budde 					  channel, 0, &bt_const_extended,
13213814ed27SMarc Kleine-Budde 					  sizeof(bt_const_extended),
13223814ed27SMarc Kleine-Budde 					  1000, GFP_KERNEL);
13233814ed27SMarc Kleine-Budde 		if (rc) {
13246679f4c5SPeter Fink 			dev_err(&intf->dev,
132568822f4eSMarc Kleine-Budde 				"Couldn't get extended bit timing const for channel %d (%pe)\n",
132668822f4eSMarc Kleine-Budde 				channel, ERR_PTR(rc));
132768822f4eSMarc Kleine-Budde 			goto out_free_candev;
13286679f4c5SPeter Fink 		}
13296679f4c5SPeter Fink 
1330c250d5ebSVincent Mailhol 		strcpy(dev->data_bt_const.name, KBUILD_MODNAME);
13313814ed27SMarc Kleine-Budde 		dev->data_bt_const.tseg1_min = le32_to_cpu(bt_const_extended.dtseg1_min);
13323814ed27SMarc Kleine-Budde 		dev->data_bt_const.tseg1_max = le32_to_cpu(bt_const_extended.dtseg1_max);
13333814ed27SMarc Kleine-Budde 		dev->data_bt_const.tseg2_min = le32_to_cpu(bt_const_extended.dtseg2_min);
13343814ed27SMarc Kleine-Budde 		dev->data_bt_const.tseg2_max = le32_to_cpu(bt_const_extended.dtseg2_max);
13353814ed27SMarc Kleine-Budde 		dev->data_bt_const.sjw_max = le32_to_cpu(bt_const_extended.dsjw_max);
13363814ed27SMarc Kleine-Budde 		dev->data_bt_const.brp_min = le32_to_cpu(bt_const_extended.dbrp_min);
13373814ed27SMarc Kleine-Budde 		dev->data_bt_const.brp_max = le32_to_cpu(bt_const_extended.dbrp_max);
13383814ed27SMarc Kleine-Budde 		dev->data_bt_const.brp_inc = le32_to_cpu(bt_const_extended.dbrp_inc);
13396679f4c5SPeter Fink 
13406679f4c5SPeter Fink 		dev->can.data_bittiming_const = &dev->data_bt_const;
13416679f4c5SPeter Fink 	}
13426679f4c5SPeter Fink 
1343e0d25759SMarc Kleine-Budde 	SET_NETDEV_DEV(netdev, &intf->dev);
1344e0d25759SMarc Kleine-Budde 
1345d08e973aSMaximilian Schneider 	rc = register_candev(dev->netdev);
1346d08e973aSMaximilian Schneider 	if (rc) {
134768822f4eSMarc Kleine-Budde 		dev_err(&intf->dev,
134868822f4eSMarc Kleine-Budde 			"Couldn't register candev for channel %d (%pe)\n",
134968822f4eSMarc Kleine-Budde 			channel, ERR_PTR(rc));
135068822f4eSMarc Kleine-Budde 		goto out_free_candev;
1351d08e973aSMaximilian Schneider 	}
1352d08e973aSMaximilian Schneider 
1353d08e973aSMaximilian Schneider 	return dev;
135468822f4eSMarc Kleine-Budde 
135568822f4eSMarc Kleine-Budde  out_free_candev:
135668822f4eSMarc Kleine-Budde 	free_candev(dev->netdev);
135768822f4eSMarc Kleine-Budde 	return ERR_PTR(rc);
1358d08e973aSMaximilian Schneider }
1359d08e973aSMaximilian Schneider 
1360d08e973aSMaximilian Schneider static void gs_destroy_candev(struct gs_can *dev)
1361d08e973aSMaximilian Schneider {
1362d08e973aSMaximilian Schneider 	unregister_candev(dev->netdev);
1363d08e973aSMaximilian Schneider 	usb_kill_anchored_urbs(&dev->tx_submitted);
1364e9a2d81bSMaximilain Schneider 	free_candev(dev->netdev);
1365d08e973aSMaximilian Schneider }
1366d08e973aSMaximilian Schneider 
136705ca5270SMaximilian Schneider static int gs_usb_probe(struct usb_interface *intf,
136805ca5270SMaximilian Schneider 			const struct usb_device_id *id)
1369d08e973aSMaximilian Schneider {
13705374d083SMarc Kleine-Budde 	struct usb_device *udev = interface_to_usbdev(intf);
1371c359931dSPeter Fink 	struct gs_host_frame *hf;
1372d08e973aSMaximilian Schneider 	struct gs_usb *dev;
13733814ed27SMarc Kleine-Budde 	struct gs_host_config hconf = {
13743814ed27SMarc Kleine-Budde 		.byte_order = cpu_to_le32(0x0000beef),
13753814ed27SMarc Kleine-Budde 	};
13763814ed27SMarc Kleine-Budde 	struct gs_device_config dconf;
1377d08e973aSMaximilian Schneider 	unsigned int icount, i;
13783814ed27SMarc Kleine-Budde 	int rc;
1379d08e973aSMaximilian Schneider 
1380d08e973aSMaximilian Schneider 	/* send host config */
13813814ed27SMarc Kleine-Budde 	rc = usb_control_msg_send(udev, 0,
1382d08e973aSMaximilian Schneider 				  GS_USB_BREQ_HOST_FORMAT,
1383d08e973aSMaximilian Schneider 				  USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1384c1ee7269SMarc Kleine-Budde 				  1, intf->cur_altsetting->desc.bInterfaceNumber,
13853814ed27SMarc Kleine-Budde 				  &hconf, sizeof(hconf), 1000,
13863814ed27SMarc Kleine-Budde 				  GFP_KERNEL);
13873814ed27SMarc Kleine-Budde 	if (rc) {
1388d0cd2aa8SMarc Kleine-Budde 		dev_err(&intf->dev, "Couldn't send data format (err=%d)\n", rc);
1389d08e973aSMaximilian Schneider 		return rc;
1390d08e973aSMaximilian Schneider 	}
1391d08e973aSMaximilian Schneider 
1392d08e973aSMaximilian Schneider 	/* read device config */
13933814ed27SMarc Kleine-Budde 	rc = usb_control_msg_recv(udev, 0,
1394d08e973aSMaximilian Schneider 				  GS_USB_BREQ_DEVICE_CONFIG,
1395d08e973aSMaximilian Schneider 				  USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1396c1ee7269SMarc Kleine-Budde 				  1, intf->cur_altsetting->desc.bInterfaceNumber,
13973814ed27SMarc Kleine-Budde 				  &dconf, sizeof(dconf), 1000,
13983814ed27SMarc Kleine-Budde 				  GFP_KERNEL);
13993814ed27SMarc Kleine-Budde 	if (rc) {
1400d08e973aSMaximilian Schneider 		dev_err(&intf->dev, "Couldn't get device config: (err=%d)\n",
1401d08e973aSMaximilian Schneider 			rc);
1402d08e973aSMaximilian Schneider 		return rc;
1403d08e973aSMaximilian Schneider 	}
1404d08e973aSMaximilian Schneider 
14053814ed27SMarc Kleine-Budde 	icount = dconf.icount + 1;
140610819466SMarc Kleine-Budde 	dev_info(&intf->dev, "Configuring for %u interfaces\n", icount);
1407d08e973aSMaximilian Schneider 
1408d08e973aSMaximilian Schneider 	if (icount > GS_MAX_INTF) {
1409d08e973aSMaximilian Schneider 		dev_err(&intf->dev,
141010819466SMarc Kleine-Budde 			"Driver cannot handle more that %u CAN interfaces\n",
1411d08e973aSMaximilian Schneider 			GS_MAX_INTF);
1412d08e973aSMaximilian Schneider 		return -EINVAL;
1413d08e973aSMaximilian Schneider 	}
1414d08e973aSMaximilian Schneider 
1415d08e973aSMaximilian Schneider 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
14163814ed27SMarc Kleine-Budde 	if (!dev)
141737920a74SColin Ian King 		return -ENOMEM;
1418c919a306SEthan Zonca 
1419d08e973aSMaximilian Schneider 	init_usb_anchor(&dev->rx_submitted);
1420d08e973aSMaximilian Schneider 
1421d08e973aSMaximilian Schneider 	usb_set_intfdata(intf, dev);
14225374d083SMarc Kleine-Budde 	dev->udev = udev;
1423d08e973aSMaximilian Schneider 
1424d08e973aSMaximilian Schneider 	for (i = 0; i < icount; i++) {
142545dfa45fSJohn Whittington 		unsigned int hf_size_rx = 0;
142645dfa45fSJohn Whittington 
14273814ed27SMarc Kleine-Budde 		dev->canch[i] = gs_make_candev(i, intf, &dconf);
1428d08e973aSMaximilian Schneider 		if (IS_ERR_OR_NULL(dev->canch[i])) {
1429e9a2d81bSMaximilain Schneider 			/* save error code to return later */
1430e9a2d81bSMaximilain Schneider 			rc = PTR_ERR(dev->canch[i]);
1431e9a2d81bSMaximilain Schneider 
1432d08e973aSMaximilian Schneider 			/* on failure destroy previously created candevs */
1433d08e973aSMaximilian Schneider 			icount = i;
1434e9a2d81bSMaximilain Schneider 			for (i = 0; i < icount; i++)
1435d08e973aSMaximilian Schneider 				gs_destroy_candev(dev->canch[i]);
1436e9a2d81bSMaximilain Schneider 
1437e9a2d81bSMaximilain Schneider 			usb_kill_anchored_urbs(&dev->rx_submitted);
1438d08e973aSMaximilian Schneider 			kfree(dev);
1439d08e973aSMaximilian Schneider 			return rc;
1440d08e973aSMaximilian Schneider 		}
1441d08e973aSMaximilian Schneider 		dev->canch[i]->parent = dev;
144226949ac9SPeter Fink 
144345dfa45fSJohn Whittington 		/* set RX packet size based on FD and if hardware
144445dfa45fSJohn Whittington                 * timestamps are supported.
144545dfa45fSJohn Whittington 		*/
144645dfa45fSJohn Whittington 		if (dev->canch[i]->can.ctrlmode_supported & CAN_CTRLMODE_FD) {
144745dfa45fSJohn Whittington 			if (dev->canch[i]->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
144845dfa45fSJohn Whittington 				hf_size_rx = struct_size(hf, canfd_ts, 1);
144945dfa45fSJohn Whittington 			else
145045dfa45fSJohn Whittington 				hf_size_rx = struct_size(hf, canfd, 1);
145145dfa45fSJohn Whittington 		} else {
145245dfa45fSJohn Whittington 			if (dev->canch[i]->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
145345dfa45fSJohn Whittington 				hf_size_rx = struct_size(hf, classic_can_ts, 1);
145445dfa45fSJohn Whittington 			else
145545dfa45fSJohn Whittington 				hf_size_rx = struct_size(hf, classic_can, 1);
145645dfa45fSJohn Whittington 		}
145745dfa45fSJohn Whittington 		dev->hf_size_rx = max(dev->hf_size_rx, hf_size_rx);
1458d08e973aSMaximilian Schneider 	}
1459d08e973aSMaximilian Schneider 
1460d08e973aSMaximilian Schneider 	return 0;
1461d08e973aSMaximilian Schneider }
1462d08e973aSMaximilian Schneider 
1463d08e973aSMaximilian Schneider static void gs_usb_disconnect(struct usb_interface *intf)
1464d08e973aSMaximilian Schneider {
1465d08e973aSMaximilian Schneider 	struct gs_usb *dev = usb_get_intfdata(intf);
1466b9d9b030SMarc Kleine-Budde 	unsigned int i;
1467b9d9b030SMarc Kleine-Budde 
1468d08e973aSMaximilian Schneider 	usb_set_intfdata(intf, NULL);
1469d08e973aSMaximilian Schneider 
1470d08e973aSMaximilian Schneider 	if (!dev) {
1471d08e973aSMaximilian Schneider 		dev_err(&intf->dev, "Disconnect (nodata)\n");
1472d08e973aSMaximilian Schneider 		return;
1473d08e973aSMaximilian Schneider 	}
1474d08e973aSMaximilian Schneider 
1475e9a2d81bSMaximilain Schneider 	for (i = 0; i < GS_MAX_INTF; i++)
1476e9a2d81bSMaximilain Schneider 		if (dev->canch[i])
1477e9a2d81bSMaximilain Schneider 			gs_destroy_candev(dev->canch[i]);
1478d08e973aSMaximilian Schneider 
1479d08e973aSMaximilian Schneider 	usb_kill_anchored_urbs(&dev->rx_submitted);
1480e9a2d81bSMaximilain Schneider 	kfree(dev);
1481d08e973aSMaximilian Schneider }
1482d08e973aSMaximilian Schneider 
1483d08e973aSMaximilian Schneider static const struct usb_device_id gs_usb_table[] = {
148449c007b9SMarc Kleine-Budde 	{ USB_DEVICE_INTERFACE_NUMBER(USB_GS_USB_1_VENDOR_ID,
148549c007b9SMarc Kleine-Budde 				      USB_GS_USB_1_PRODUCT_ID, 0) },
1486f155d9c0SMaximilian Schneider 	{ USB_DEVICE_INTERFACE_NUMBER(USB_CANDLELIGHT_VENDOR_ID,
1487f155d9c0SMaximilian Schneider 				      USB_CANDLELIGHT_PRODUCT_ID, 0) },
1488d03bb08eSPeter Fink 	{ USB_DEVICE_INTERFACE_NUMBER(USB_CES_CANEXT_FD_VENDOR_ID,
1489d03bb08eSPeter Fink 				      USB_CES_CANEXT_FD_PRODUCT_ID, 0) },
14900691a4b5SBen Evans 	{ USB_DEVICE_INTERFACE_NUMBER(USB_ABE_CANDEBUGGER_FD_VENDOR_ID,
14910691a4b5SBen Evans 				      USB_ABE_CANDEBUGGER_FD_PRODUCT_ID, 0) },
1492d08e973aSMaximilian Schneider 	{} /* Terminating entry */
1493d08e973aSMaximilian Schneider };
1494d08e973aSMaximilian Schneider 
1495d08e973aSMaximilian Schneider MODULE_DEVICE_TABLE(usb, gs_usb_table);
1496d08e973aSMaximilian Schneider 
1497d08e973aSMaximilian Schneider static struct usb_driver gs_usb_driver = {
1498c250d5ebSVincent Mailhol 	.name = KBUILD_MODNAME,
1499d08e973aSMaximilian Schneider 	.probe = gs_usb_probe,
1500d08e973aSMaximilian Schneider 	.disconnect = gs_usb_disconnect,
1501d08e973aSMaximilian Schneider 	.id_table = gs_usb_table,
1502d08e973aSMaximilian Schneider };
1503d08e973aSMaximilian Schneider 
1504d08e973aSMaximilian Schneider module_usb_driver(gs_usb_driver);
1505d08e973aSMaximilian Schneider 
1506d08e973aSMaximilian Schneider MODULE_AUTHOR("Maximilian Schneider <mws@schneidersoft.net>");
1507d08e973aSMaximilian Schneider MODULE_DESCRIPTION(
1508d08e973aSMaximilian Schneider "Socket CAN device driver for Geschwister Schneider Technologie-, "
1509f155d9c0SMaximilian Schneider "Entwicklungs- und Vertriebs UG. USB2.0 to CAN interfaces\n"
1510f155d9c0SMaximilian Schneider "and bytewerk.org candleLight USB CAN interfaces.");
1511d08e973aSMaximilian Schneider MODULE_LICENSE("GPL v2");
1512