xref: /openbmc/linux/drivers/net/can/usb/gs_usb.c (revision 278002edb19bce2c628fafb0af936e77000f3a5b)
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
824bc41b4SMarc Kleine-Budde  * Copyright (c) 2023 Pengutronix, Marc Kleine-Budde <kernel@pengutronix.de>
9d08e973aSMaximilian Schneider  *
10d08e973aSMaximilian Schneider  * Many thanks to all socketcan devs!
11d08e973aSMaximilian Schneider  */
12d08e973aSMaximilian Schneider 
13eb9fa77aSPeter Fink #include <linux/bitfield.h>
1445dfa45fSJohn Whittington #include <linux/clocksource.h>
15cc69837fSJakub Kicinski #include <linux/ethtool.h>
16d08e973aSMaximilian Schneider #include <linux/init.h>
17d08e973aSMaximilian Schneider #include <linux/module.h>
18d08e973aSMaximilian Schneider #include <linux/netdevice.h>
19f6bb2510SMarc Kleine-Budde #include <linux/signal.h>
2045dfa45fSJohn Whittington #include <linux/timecounter.h>
2145dfa45fSJohn Whittington #include <linux/units.h>
22d08e973aSMaximilian Schneider #include <linux/usb.h>
2345dfa45fSJohn Whittington #include <linux/workqueue.h>
24d08e973aSMaximilian Schneider 
25d08e973aSMaximilian Schneider #include <linux/can.h>
26d08e973aSMaximilian Schneider #include <linux/can/dev.h>
27d08e973aSMaximilian Schneider #include <linux/can/error.h>
2824bc41b4SMarc Kleine-Budde #include <linux/can/rx-offload.h>
29d08e973aSMaximilian Schneider 
30d08e973aSMaximilian Schneider /* Device specific constants */
3149c007b9SMarc Kleine-Budde #define USB_GS_USB_1_VENDOR_ID 0x1d50
3249c007b9SMarc Kleine-Budde #define USB_GS_USB_1_PRODUCT_ID 0x606f
33d08e973aSMaximilian Schneider 
34f155d9c0SMaximilian Schneider #define USB_CANDLELIGHT_VENDOR_ID 0x1209
35f155d9c0SMaximilian Schneider #define USB_CANDLELIGHT_PRODUCT_ID 0x2323
36f155d9c0SMaximilian Schneider 
37d03bb08eSPeter Fink #define USB_CES_CANEXT_FD_VENDOR_ID 0x1cd2
38d03bb08eSPeter Fink #define USB_CES_CANEXT_FD_PRODUCT_ID 0x606f
39d03bb08eSPeter Fink 
400691a4b5SBen Evans #define USB_ABE_CANDEBUGGER_FD_VENDOR_ID 0x16d0
410691a4b5SBen Evans #define USB_ABE_CANDEBUGGER_FD_PRODUCT_ID 0x10b8
420691a4b5SBen Evans 
43e2282706SMarc Kleine-Budde #define USB_XYLANTA_SAINT3_VENDOR_ID 0x16d0
44e2282706SMarc Kleine-Budde #define USB_XYLANTA_SAINT3_PRODUCT_ID 0x0f30
45e2282706SMarc Kleine-Budde 
4645dfa45fSJohn Whittington /* Timestamp 32 bit timer runs at 1 MHz (1 µs tick). Worker accounts
4745dfa45fSJohn Whittington  * for timer overflow (will be after ~71 minutes)
4845dfa45fSJohn Whittington  */
4945dfa45fSJohn Whittington #define GS_USB_TIMESTAMP_TIMER_HZ (1 * HZ_PER_MHZ)
5045dfa45fSJohn Whittington #define GS_USB_TIMESTAMP_WORK_DELAY_SEC 1800
5145dfa45fSJohn Whittington static_assert(GS_USB_TIMESTAMP_WORK_DELAY_SEC <
5245dfa45fSJohn Whittington 	      CYCLECOUNTER_MASK(32) / GS_USB_TIMESTAMP_TIMER_HZ / 2);
5345dfa45fSJohn Whittington 
54d08e973aSMaximilian Schneider /* Device specific constants */
55d08e973aSMaximilian Schneider enum gs_usb_breq {
56d08e973aSMaximilian Schneider 	GS_USB_BREQ_HOST_FORMAT = 0,
57d08e973aSMaximilian Schneider 	GS_USB_BREQ_BITTIMING,
58d08e973aSMaximilian Schneider 	GS_USB_BREQ_MODE,
59d08e973aSMaximilian Schneider 	GS_USB_BREQ_BERR,
60d08e973aSMaximilian Schneider 	GS_USB_BREQ_BT_CONST,
6105ca5270SMaximilian Schneider 	GS_USB_BREQ_DEVICE_CONFIG,
6205ca5270SMaximilian Schneider 	GS_USB_BREQ_TIMESTAMP,
6305ca5270SMaximilian Schneider 	GS_USB_BREQ_IDENTIFY,
64e0902cadSMarc Kleine-Budde 	GS_USB_BREQ_GET_USER_ID,
6532cd9013SMarc Kleine-Budde 	GS_USB_BREQ_QUIRK_CANTACT_PRO_DATA_BITTIMING = GS_USB_BREQ_GET_USER_ID,
66e0902cadSMarc Kleine-Budde 	GS_USB_BREQ_SET_USER_ID,
6726949ac9SPeter Fink 	GS_USB_BREQ_DATA_BITTIMING,
686679f4c5SPeter Fink 	GS_USB_BREQ_BT_CONST_EXT,
69906e0e68SMarc Kleine-Budde 	GS_USB_BREQ_SET_TERMINATION,
70906e0e68SMarc Kleine-Budde 	GS_USB_BREQ_GET_TERMINATION,
7140e1997dSJeroen Hofstee 	GS_USB_BREQ_GET_STATE,
72d08e973aSMaximilian Schneider };
73d08e973aSMaximilian Schneider 
74d08e973aSMaximilian Schneider enum gs_can_mode {
75d08e973aSMaximilian Schneider 	/* reset a channel. turns it off */
76d08e973aSMaximilian Schneider 	GS_CAN_MODE_RESET = 0,
77d08e973aSMaximilian Schneider 	/* starts a channel */
78d08e973aSMaximilian Schneider 	GS_CAN_MODE_START
79d08e973aSMaximilian Schneider };
80d08e973aSMaximilian Schneider 
81d08e973aSMaximilian Schneider enum gs_can_state {
82d08e973aSMaximilian Schneider 	GS_CAN_STATE_ERROR_ACTIVE = 0,
83d08e973aSMaximilian Schneider 	GS_CAN_STATE_ERROR_WARNING,
84d08e973aSMaximilian Schneider 	GS_CAN_STATE_ERROR_PASSIVE,
85d08e973aSMaximilian Schneider 	GS_CAN_STATE_BUS_OFF,
86d08e973aSMaximilian Schneider 	GS_CAN_STATE_STOPPED,
87d08e973aSMaximilian Schneider 	GS_CAN_STATE_SLEEPING
88d08e973aSMaximilian Schneider };
89d08e973aSMaximilian Schneider 
9005ca5270SMaximilian Schneider enum gs_can_identify_mode {
9105ca5270SMaximilian Schneider 	GS_CAN_IDENTIFY_OFF = 0,
9205ca5270SMaximilian Schneider 	GS_CAN_IDENTIFY_ON
9305ca5270SMaximilian Schneider };
9405ca5270SMaximilian Schneider 
95906e0e68SMarc Kleine-Budde enum gs_can_termination_state {
96906e0e68SMarc Kleine-Budde 	GS_CAN_TERMINATION_STATE_OFF = 0,
97906e0e68SMarc Kleine-Budde 	GS_CAN_TERMINATION_STATE_ON
98906e0e68SMarc Kleine-Budde };
99906e0e68SMarc Kleine-Budde 
100906e0e68SMarc Kleine-Budde #define GS_USB_TERMINATION_DISABLED CAN_TERMINATION_DISABLED
101906e0e68SMarc Kleine-Budde #define GS_USB_TERMINATION_ENABLED 120
102906e0e68SMarc Kleine-Budde 
103d08e973aSMaximilian Schneider /* data types passed between host and device */
1044ba1cb39SMarc Kleine-Budde 
1054ba1cb39SMarc Kleine-Budde /* The firmware on the original USB2CAN by Geschwister Schneider
1064ba1cb39SMarc Kleine-Budde  * Technologie Entwicklungs- und Vertriebs UG exchanges all data
1074ba1cb39SMarc Kleine-Budde  * between the host and the device in host byte order. This is done
1084ba1cb39SMarc Kleine-Budde  * with the struct gs_host_config::byte_order member, which is sent
1094ba1cb39SMarc Kleine-Budde  * first to indicate the desired byte order.
1104ba1cb39SMarc Kleine-Budde  *
1114ba1cb39SMarc Kleine-Budde  * The widely used open source firmware candleLight doesn't support
1124ba1cb39SMarc Kleine-Budde  * this feature and exchanges the data in little endian byte order.
113d08e973aSMaximilian Schneider  */
1144ba1cb39SMarc Kleine-Budde struct gs_host_config {
1154ba1cb39SMarc Kleine-Budde 	__le32 byte_order;
1164ba1cb39SMarc Kleine-Budde } __packed;
117d08e973aSMaximilian Schneider 
118d08e973aSMaximilian Schneider struct gs_device_config {
119d08e973aSMaximilian Schneider 	u8 reserved1;
120d08e973aSMaximilian Schneider 	u8 reserved2;
121d08e973aSMaximilian Schneider 	u8 reserved3;
122d08e973aSMaximilian Schneider 	u8 icount;
1234ba1cb39SMarc Kleine-Budde 	__le32 sw_version;
1244ba1cb39SMarc Kleine-Budde 	__le32 hw_version;
125d08e973aSMaximilian Schneider } __packed;
126d08e973aSMaximilian Schneider 
127d08e973aSMaximilian Schneider #define GS_CAN_MODE_NORMAL 0
1289be95c91SMaximilian Schneider #define GS_CAN_MODE_LISTEN_ONLY BIT(0)
1299be95c91SMaximilian Schneider #define GS_CAN_MODE_LOOP_BACK BIT(1)
1309be95c91SMaximilian Schneider #define GS_CAN_MODE_TRIPLE_SAMPLE BIT(2)
1319be95c91SMaximilian Schneider #define GS_CAN_MODE_ONE_SHOT BIT(3)
132d42d2111SMarc Kleine-Budde #define GS_CAN_MODE_HW_TIMESTAMP BIT(4)
13315564f82SMarc Kleine-Budde /* GS_CAN_FEATURE_IDENTIFY BIT(5) */
134e0902cadSMarc Kleine-Budde /* GS_CAN_FEATURE_USER_ID BIT(6) */
1354643e34eSMarc Kleine-Budde #define GS_CAN_MODE_PAD_PKTS_TO_MAX_PKT_SIZE BIT(7)
13626949ac9SPeter Fink #define GS_CAN_MODE_FD BIT(8)
137eb9fa77aSPeter Fink /* GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX BIT(9) */
1386679f4c5SPeter Fink /* GS_CAN_FEATURE_BT_CONST_EXT BIT(10) */
139906e0e68SMarc Kleine-Budde /* GS_CAN_FEATURE_TERMINATION BIT(11) */
1401f183526SJeroen Hofstee #define GS_CAN_MODE_BERR_REPORTING BIT(12)
14140e1997dSJeroen Hofstee /* GS_CAN_FEATURE_GET_STATE BIT(13) */
142d08e973aSMaximilian Schneider 
143d08e973aSMaximilian Schneider struct gs_device_mode {
1444ba1cb39SMarc Kleine-Budde 	__le32 mode;
1454ba1cb39SMarc Kleine-Budde 	__le32 flags;
146d08e973aSMaximilian Schneider } __packed;
147d08e973aSMaximilian Schneider 
148d08e973aSMaximilian Schneider struct gs_device_state {
1494ba1cb39SMarc Kleine-Budde 	__le32 state;
1504ba1cb39SMarc Kleine-Budde 	__le32 rxerr;
1514ba1cb39SMarc Kleine-Budde 	__le32 txerr;
152d08e973aSMaximilian Schneider } __packed;
153d08e973aSMaximilian Schneider 
154d08e973aSMaximilian Schneider struct gs_device_bittiming {
1554ba1cb39SMarc Kleine-Budde 	__le32 prop_seg;
1564ba1cb39SMarc Kleine-Budde 	__le32 phase_seg1;
1574ba1cb39SMarc Kleine-Budde 	__le32 phase_seg2;
1584ba1cb39SMarc Kleine-Budde 	__le32 sjw;
1594ba1cb39SMarc Kleine-Budde 	__le32 brp;
160d08e973aSMaximilian Schneider } __packed;
161d08e973aSMaximilian Schneider 
16205ca5270SMaximilian Schneider struct gs_identify_mode {
1634ba1cb39SMarc Kleine-Budde 	__le32 mode;
16405ca5270SMaximilian Schneider } __packed;
16505ca5270SMaximilian Schneider 
166906e0e68SMarc Kleine-Budde struct gs_device_termination_state {
167906e0e68SMarc Kleine-Budde 	__le32 state;
168906e0e68SMarc Kleine-Budde } __packed;
169906e0e68SMarc Kleine-Budde 
1709be95c91SMaximilian Schneider #define GS_CAN_FEATURE_LISTEN_ONLY BIT(0)
1719be95c91SMaximilian Schneider #define GS_CAN_FEATURE_LOOP_BACK BIT(1)
1729be95c91SMaximilian Schneider #define GS_CAN_FEATURE_TRIPLE_SAMPLE BIT(2)
1739be95c91SMaximilian Schneider #define GS_CAN_FEATURE_ONE_SHOT BIT(3)
17405ca5270SMaximilian Schneider #define GS_CAN_FEATURE_HW_TIMESTAMP BIT(4)
17505ca5270SMaximilian Schneider #define GS_CAN_FEATURE_IDENTIFY BIT(5)
176e0902cadSMarc Kleine-Budde #define GS_CAN_FEATURE_USER_ID BIT(6)
1774643e34eSMarc Kleine-Budde #define GS_CAN_FEATURE_PAD_PKTS_TO_MAX_PKT_SIZE BIT(7)
17826949ac9SPeter Fink #define GS_CAN_FEATURE_FD BIT(8)
179eb9fa77aSPeter Fink #define GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX BIT(9)
1806679f4c5SPeter Fink #define GS_CAN_FEATURE_BT_CONST_EXT BIT(10)
181906e0e68SMarc Kleine-Budde #define GS_CAN_FEATURE_TERMINATION BIT(11)
1821f183526SJeroen Hofstee #define GS_CAN_FEATURE_BERR_REPORTING BIT(12)
18340e1997dSJeroen Hofstee #define GS_CAN_FEATURE_GET_STATE BIT(13)
18440e1997dSJeroen Hofstee #define GS_CAN_FEATURE_MASK GENMASK(13, 0)
185d08e973aSMaximilian Schneider 
18632cd9013SMarc Kleine-Budde /* internal quirks - keep in GS_CAN_FEATURE space for now */
18732cd9013SMarc Kleine-Budde 
18832cd9013SMarc Kleine-Budde /* CANtact Pro original firmware:
18932cd9013SMarc Kleine-Budde  * BREQ DATA_BITTIMING overlaps with GET_USER_ID
19032cd9013SMarc Kleine-Budde  */
19132cd9013SMarc Kleine-Budde #define GS_CAN_FEATURE_QUIRK_BREQ_CANTACT_PRO BIT(31)
19232cd9013SMarc Kleine-Budde 
193d08e973aSMaximilian Schneider struct gs_device_bt_const {
1944ba1cb39SMarc Kleine-Budde 	__le32 feature;
1954ba1cb39SMarc Kleine-Budde 	__le32 fclk_can;
1964ba1cb39SMarc Kleine-Budde 	__le32 tseg1_min;
1974ba1cb39SMarc Kleine-Budde 	__le32 tseg1_max;
1984ba1cb39SMarc Kleine-Budde 	__le32 tseg2_min;
1994ba1cb39SMarc Kleine-Budde 	__le32 tseg2_max;
2004ba1cb39SMarc Kleine-Budde 	__le32 sjw_max;
2014ba1cb39SMarc Kleine-Budde 	__le32 brp_min;
2024ba1cb39SMarc Kleine-Budde 	__le32 brp_max;
2034ba1cb39SMarc Kleine-Budde 	__le32 brp_inc;
204d08e973aSMaximilian Schneider } __packed;
205d08e973aSMaximilian Schneider 
2066679f4c5SPeter Fink struct gs_device_bt_const_extended {
2076679f4c5SPeter Fink 	__le32 feature;
2086679f4c5SPeter Fink 	__le32 fclk_can;
2096679f4c5SPeter Fink 	__le32 tseg1_min;
2106679f4c5SPeter Fink 	__le32 tseg1_max;
2116679f4c5SPeter Fink 	__le32 tseg2_min;
2126679f4c5SPeter Fink 	__le32 tseg2_max;
2136679f4c5SPeter Fink 	__le32 sjw_max;
2146679f4c5SPeter Fink 	__le32 brp_min;
2156679f4c5SPeter Fink 	__le32 brp_max;
2166679f4c5SPeter Fink 	__le32 brp_inc;
2176679f4c5SPeter Fink 
2186679f4c5SPeter Fink 	__le32 dtseg1_min;
2196679f4c5SPeter Fink 	__le32 dtseg1_max;
2206679f4c5SPeter Fink 	__le32 dtseg2_min;
2216679f4c5SPeter Fink 	__le32 dtseg2_max;
2226679f4c5SPeter Fink 	__le32 dsjw_max;
2236679f4c5SPeter Fink 	__le32 dbrp_min;
2246679f4c5SPeter Fink 	__le32 dbrp_max;
2256679f4c5SPeter Fink 	__le32 dbrp_inc;
2266679f4c5SPeter Fink } __packed;
2276679f4c5SPeter Fink 
2284b8f03e3SMarc Kleine-Budde #define GS_CAN_FLAG_OVERFLOW BIT(0)
22926949ac9SPeter Fink #define GS_CAN_FLAG_FD BIT(1)
23026949ac9SPeter Fink #define GS_CAN_FLAG_BRS BIT(2)
23126949ac9SPeter Fink #define GS_CAN_FLAG_ESI BIT(3)
232d08e973aSMaximilian Schneider 
233c359931dSPeter Fink struct classic_can {
234c359931dSPeter Fink 	u8 data[8];
235c359931dSPeter Fink } __packed;
236c359931dSPeter Fink 
23745dfa45fSJohn Whittington struct classic_can_ts {
23845dfa45fSJohn Whittington 	u8 data[8];
23945dfa45fSJohn Whittington 	__le32 timestamp_us;
24045dfa45fSJohn Whittington } __packed;
24145dfa45fSJohn Whittington 
242eb9fa77aSPeter Fink struct classic_can_quirk {
243eb9fa77aSPeter Fink 	u8 data[8];
244eb9fa77aSPeter Fink 	u8 quirk;
245eb9fa77aSPeter Fink } __packed;
246eb9fa77aSPeter Fink 
24726949ac9SPeter Fink struct canfd {
24826949ac9SPeter Fink 	u8 data[64];
24926949ac9SPeter Fink } __packed;
25026949ac9SPeter Fink 
25145dfa45fSJohn Whittington struct canfd_ts {
25245dfa45fSJohn Whittington 	u8 data[64];
25345dfa45fSJohn Whittington 	__le32 timestamp_us;
25445dfa45fSJohn Whittington } __packed;
25545dfa45fSJohn Whittington 
256eb9fa77aSPeter Fink struct canfd_quirk {
257eb9fa77aSPeter Fink 	u8 data[64];
258eb9fa77aSPeter Fink 	u8 quirk;
259eb9fa77aSPeter Fink } __packed;
260eb9fa77aSPeter Fink 
261d08e973aSMaximilian Schneider struct gs_host_frame {
262d08e973aSMaximilian Schneider 	u32 echo_id;
2634ba1cb39SMarc Kleine-Budde 	__le32 can_id;
264d08e973aSMaximilian Schneider 
265d08e973aSMaximilian Schneider 	u8 can_dlc;
266d08e973aSMaximilian Schneider 	u8 channel;
267d08e973aSMaximilian Schneider 	u8 flags;
268d08e973aSMaximilian Schneider 	u8 reserved;
269d08e973aSMaximilian Schneider 
270c359931dSPeter Fink 	union {
271c359931dSPeter Fink 		DECLARE_FLEX_ARRAY(struct classic_can, classic_can);
27245dfa45fSJohn Whittington 		DECLARE_FLEX_ARRAY(struct classic_can_ts, classic_can_ts);
273eb9fa77aSPeter Fink 		DECLARE_FLEX_ARRAY(struct classic_can_quirk, classic_can_quirk);
27426949ac9SPeter Fink 		DECLARE_FLEX_ARRAY(struct canfd, canfd);
27545dfa45fSJohn Whittington 		DECLARE_FLEX_ARRAY(struct canfd_ts, canfd_ts);
276eb9fa77aSPeter Fink 		DECLARE_FLEX_ARRAY(struct canfd_quirk, canfd_quirk);
277c359931dSPeter Fink 	};
278d08e973aSMaximilian Schneider } __packed;
279d08e973aSMaximilian Schneider /* The GS USB devices make use of the same flags and masks as in
280d08e973aSMaximilian Schneider  * linux/can.h and linux/can/error.h, and no additional mapping is necessary.
281d08e973aSMaximilian Schneider  */
282d08e973aSMaximilian Schneider 
283d08e973aSMaximilian Schneider /* Only send a max of GS_MAX_TX_URBS frames per channel at a time. */
284d08e973aSMaximilian Schneider #define GS_MAX_TX_URBS 10
285d08e973aSMaximilian Schneider /* Only launch a max of GS_MAX_RX_URBS usb requests at a time. */
286d08e973aSMaximilian Schneider #define GS_MAX_RX_URBS 30
28724bc41b4SMarc Kleine-Budde #define GS_NAPI_WEIGHT 32
28824bc41b4SMarc Kleine-Budde 
289d08e973aSMaximilian Schneider /* Maximum number of interfaces the driver supports per device.
290e10ab8b3SMarc Kleine-Budde  * Current hardware only supports 3 interfaces. The future may vary.
291d08e973aSMaximilian Schneider  */
292e10ab8b3SMarc Kleine-Budde #define GS_MAX_INTF 3
293d08e973aSMaximilian Schneider 
294d08e973aSMaximilian Schneider struct gs_tx_context {
295d08e973aSMaximilian Schneider 	struct gs_can *dev;
296d08e973aSMaximilian Schneider 	unsigned int echo_id;
297d08e973aSMaximilian Schneider };
298d08e973aSMaximilian Schneider 
299d08e973aSMaximilian Schneider struct gs_can {
300d08e973aSMaximilian Schneider 	struct can_priv can; /* must be the first member */
301d08e973aSMaximilian Schneider 
30224bc41b4SMarc Kleine-Budde 	struct can_rx_offload offload;
303d08e973aSMaximilian Schneider 	struct gs_usb *parent;
304d08e973aSMaximilian Schneider 
305d08e973aSMaximilian Schneider 	struct net_device *netdev;
306d08e973aSMaximilian Schneider 	struct usb_device *udev;
307d08e973aSMaximilian Schneider 
3086679f4c5SPeter Fink 	struct can_bittiming_const bt_const, data_bt_const;
309d08e973aSMaximilian Schneider 	unsigned int channel;	/* channel number */
310d08e973aSMaximilian Schneider 
311eb9fa77aSPeter Fink 	u32 feature;
312c359931dSPeter Fink 	unsigned int hf_size_tx;
313c359931dSPeter Fink 
314ae421e33SNik Nyby 	/* This lock prevents a race condition between xmit and receive. */
315d08e973aSMaximilian Schneider 	spinlock_t tx_ctx_lock;
316d08e973aSMaximilian Schneider 	struct gs_tx_context tx_context[GS_MAX_TX_URBS];
317d08e973aSMaximilian Schneider 
318d08e973aSMaximilian Schneider 	struct usb_anchor tx_submitted;
319d08e973aSMaximilian Schneider 	atomic_t active_tx_urbs;
320d08e973aSMaximilian Schneider };
321d08e973aSMaximilian Schneider 
322d08e973aSMaximilian Schneider /* usb interface struct */
323d08e973aSMaximilian Schneider struct gs_usb {
324d08e973aSMaximilian Schneider 	struct gs_can *canch[GS_MAX_INTF];
325d08e973aSMaximilian Schneider 	struct usb_anchor rx_submitted;
326d08e973aSMaximilian Schneider 	struct usb_device *udev;
3275886e4d5SMarc Kleine-Budde 
3285886e4d5SMarc Kleine-Budde 	/* time counter for hardware timestamps */
3295886e4d5SMarc Kleine-Budde 	struct cyclecounter cc;
3305886e4d5SMarc Kleine-Budde 	struct timecounter tc;
3315886e4d5SMarc Kleine-Budde 	spinlock_t tc_lock; /* spinlock to guard access tc->cycle_last */
3325886e4d5SMarc Kleine-Budde 	struct delayed_work timestamp;
3335886e4d5SMarc Kleine-Budde 
334c359931dSPeter Fink 	unsigned int hf_size_rx;
335035b0fcfSVincent Mailhol 	u8 active_channels;
336*1cdd3563SAlexander Kozhinov 
337*1cdd3563SAlexander Kozhinov 	unsigned int pipe_in;
338*1cdd3563SAlexander Kozhinov 	unsigned int pipe_out;
339d08e973aSMaximilian Schneider };
340d08e973aSMaximilian Schneider 
341d08e973aSMaximilian Schneider /* 'allocate' a tx context.
342d08e973aSMaximilian Schneider  * returns a valid tx context or NULL if there is no space.
343d08e973aSMaximilian Schneider  */
gs_alloc_tx_context(struct gs_can * dev)344d08e973aSMaximilian Schneider static struct gs_tx_context *gs_alloc_tx_context(struct gs_can *dev)
345d08e973aSMaximilian Schneider {
346d08e973aSMaximilian Schneider 	int i = 0;
347d08e973aSMaximilian Schneider 	unsigned long flags;
348d08e973aSMaximilian Schneider 
349d08e973aSMaximilian Schneider 	spin_lock_irqsave(&dev->tx_ctx_lock, flags);
350d08e973aSMaximilian Schneider 
351d08e973aSMaximilian Schneider 	for (; i < GS_MAX_TX_URBS; i++) {
352d08e973aSMaximilian Schneider 		if (dev->tx_context[i].echo_id == GS_MAX_TX_URBS) {
353d08e973aSMaximilian Schneider 			dev->tx_context[i].echo_id = i;
354d08e973aSMaximilian Schneider 			spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
355d08e973aSMaximilian Schneider 			return &dev->tx_context[i];
356d08e973aSMaximilian Schneider 		}
357d08e973aSMaximilian Schneider 	}
358d08e973aSMaximilian Schneider 
359d08e973aSMaximilian Schneider 	spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
360d08e973aSMaximilian Schneider 	return NULL;
361d08e973aSMaximilian Schneider }
362d08e973aSMaximilian Schneider 
363d08e973aSMaximilian Schneider /* releases a tx context
364d08e973aSMaximilian Schneider  */
gs_free_tx_context(struct gs_tx_context * txc)365d08e973aSMaximilian Schneider static void gs_free_tx_context(struct gs_tx_context *txc)
366d08e973aSMaximilian Schneider {
367d08e973aSMaximilian Schneider 	txc->echo_id = GS_MAX_TX_URBS;
368d08e973aSMaximilian Schneider }
369d08e973aSMaximilian Schneider 
370d08e973aSMaximilian Schneider /* Get a tx context by id.
371d08e973aSMaximilian Schneider  */
gs_get_tx_context(struct gs_can * dev,unsigned int id)3729be95c91SMaximilian Schneider static struct gs_tx_context *gs_get_tx_context(struct gs_can *dev,
3739be95c91SMaximilian Schneider 					       unsigned int id)
374d08e973aSMaximilian Schneider {
375d08e973aSMaximilian Schneider 	unsigned long flags;
376d08e973aSMaximilian Schneider 
377d08e973aSMaximilian Schneider 	if (id < GS_MAX_TX_URBS) {
378d08e973aSMaximilian Schneider 		spin_lock_irqsave(&dev->tx_ctx_lock, flags);
379d08e973aSMaximilian Schneider 		if (dev->tx_context[id].echo_id == id) {
380d08e973aSMaximilian Schneider 			spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
381d08e973aSMaximilian Schneider 			return &dev->tx_context[id];
382d08e973aSMaximilian Schneider 		}
383d08e973aSMaximilian Schneider 		spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
384d08e973aSMaximilian Schneider 	}
385d08e973aSMaximilian Schneider 	return NULL;
386d08e973aSMaximilian Schneider }
387d08e973aSMaximilian Schneider 
gs_cmd_reset(struct gs_can * dev)38800246751SMarc Kleine-Budde static int gs_cmd_reset(struct gs_can *dev)
389d08e973aSMaximilian Schneider {
3903814ed27SMarc Kleine-Budde 	struct gs_device_mode dm = {
3913814ed27SMarc Kleine-Budde 		.mode = GS_CAN_MODE_RESET,
3923814ed27SMarc Kleine-Budde 	};
393d08e973aSMaximilian Schneider 
39456c56a30SVincent Mailhol 	return usb_control_msg_send(dev->udev, 0, GS_USB_BREQ_MODE,
395d08e973aSMaximilian Schneider 				    USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
3963814ed27SMarc Kleine-Budde 				    dev->channel, 0, &dm, sizeof(dm), 1000,
3973814ed27SMarc Kleine-Budde 				    GFP_KERNEL);
398d08e973aSMaximilian Schneider }
399d08e973aSMaximilian Schneider 
gs_usb_get_timestamp(const struct gs_usb * parent,u32 * timestamp_p)4005886e4d5SMarc Kleine-Budde static inline int gs_usb_get_timestamp(const struct gs_usb *parent,
40145dfa45fSJohn Whittington 				       u32 *timestamp_p)
40245dfa45fSJohn Whittington {
40345dfa45fSJohn Whittington 	__le32 timestamp;
40445dfa45fSJohn Whittington 	int rc;
40545dfa45fSJohn Whittington 
4065886e4d5SMarc Kleine-Budde 	rc = usb_control_msg_recv(parent->udev, 0, GS_USB_BREQ_TIMESTAMP,
40745dfa45fSJohn Whittington 				  USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
4085886e4d5SMarc Kleine-Budde 				  0, 0,
40945dfa45fSJohn Whittington 				  &timestamp, sizeof(timestamp),
41045dfa45fSJohn Whittington 				  USB_CTRL_GET_TIMEOUT,
41145dfa45fSJohn Whittington 				  GFP_KERNEL);
41245dfa45fSJohn Whittington 	if (rc)
41345dfa45fSJohn Whittington 		return rc;
41445dfa45fSJohn Whittington 
41545dfa45fSJohn Whittington 	*timestamp_p = le32_to_cpu(timestamp);
41645dfa45fSJohn Whittington 
41745dfa45fSJohn Whittington 	return 0;
41845dfa45fSJohn Whittington }
41945dfa45fSJohn Whittington 
gs_usb_timestamp_read(const struct cyclecounter * cc)42029a8c9ecSMarc Kleine-Budde static u64 gs_usb_timestamp_read(const struct cyclecounter *cc) __must_hold(&dev->tc_lock)
42145dfa45fSJohn Whittington {
4225886e4d5SMarc Kleine-Budde 	struct gs_usb *parent = container_of(cc, struct gs_usb, cc);
42345dfa45fSJohn Whittington 	u32 timestamp = 0;
42445dfa45fSJohn Whittington 	int err;
42545dfa45fSJohn Whittington 
4265886e4d5SMarc Kleine-Budde 	lockdep_assert_held(&parent->tc_lock);
42729a8c9ecSMarc Kleine-Budde 
42829a8c9ecSMarc Kleine-Budde 	/* drop lock for synchronous USB transfer */
4295886e4d5SMarc Kleine-Budde 	spin_unlock_bh(&parent->tc_lock);
4305886e4d5SMarc Kleine-Budde 	err = gs_usb_get_timestamp(parent, &timestamp);
4315886e4d5SMarc Kleine-Budde 	spin_lock_bh(&parent->tc_lock);
43245dfa45fSJohn Whittington 	if (err)
4335886e4d5SMarc Kleine-Budde 		dev_err(&parent->udev->dev,
43445dfa45fSJohn Whittington 			"Error %d while reading timestamp. HW timestamps may be inaccurate.",
43545dfa45fSJohn Whittington 			err);
43645dfa45fSJohn Whittington 
43745dfa45fSJohn Whittington 	return timestamp;
43845dfa45fSJohn Whittington }
43945dfa45fSJohn Whittington 
gs_usb_timestamp_work(struct work_struct * work)44045dfa45fSJohn Whittington static void gs_usb_timestamp_work(struct work_struct *work)
44145dfa45fSJohn Whittington {
44245dfa45fSJohn Whittington 	struct delayed_work *delayed_work = to_delayed_work(work);
4435886e4d5SMarc Kleine-Budde 	struct gs_usb *parent;
44445dfa45fSJohn Whittington 
4455886e4d5SMarc Kleine-Budde 	parent = container_of(delayed_work, struct gs_usb, timestamp);
4465886e4d5SMarc Kleine-Budde 	spin_lock_bh(&parent->tc_lock);
4475886e4d5SMarc Kleine-Budde 	timecounter_read(&parent->tc);
4485886e4d5SMarc Kleine-Budde 	spin_unlock_bh(&parent->tc_lock);
44945dfa45fSJohn Whittington 
4505886e4d5SMarc Kleine-Budde 	schedule_delayed_work(&parent->timestamp,
45145dfa45fSJohn Whittington 			      GS_USB_TIMESTAMP_WORK_DELAY_SEC * HZ);
45245dfa45fSJohn Whittington }
45345dfa45fSJohn Whittington 
gs_usb_skb_set_timestamp(struct gs_can * dev,struct sk_buff * skb,u32 timestamp)45429a8c9ecSMarc Kleine-Budde static void gs_usb_skb_set_timestamp(struct gs_can *dev,
45545dfa45fSJohn Whittington 				     struct sk_buff *skb, u32 timestamp)
45645dfa45fSJohn Whittington {
45745dfa45fSJohn Whittington 	struct skb_shared_hwtstamps *hwtstamps = skb_hwtstamps(skb);
4585886e4d5SMarc Kleine-Budde 	struct gs_usb *parent = dev->parent;
45945dfa45fSJohn Whittington 	u64 ns;
46045dfa45fSJohn Whittington 
4615886e4d5SMarc Kleine-Budde 	spin_lock_bh(&parent->tc_lock);
4625886e4d5SMarc Kleine-Budde 	ns = timecounter_cyc2time(&parent->tc, timestamp);
4635886e4d5SMarc Kleine-Budde 	spin_unlock_bh(&parent->tc_lock);
46429a8c9ecSMarc Kleine-Budde 
46545dfa45fSJohn Whittington 	hwtstamps->hwtstamp = ns_to_ktime(ns);
46645dfa45fSJohn Whittington }
46745dfa45fSJohn Whittington 
gs_usb_timestamp_init(struct gs_usb * parent)4685886e4d5SMarc Kleine-Budde static void gs_usb_timestamp_init(struct gs_usb *parent)
46945dfa45fSJohn Whittington {
4705886e4d5SMarc Kleine-Budde 	struct cyclecounter *cc = &parent->cc;
47145dfa45fSJohn Whittington 
47245dfa45fSJohn Whittington 	cc->read = gs_usb_timestamp_read;
47345dfa45fSJohn Whittington 	cc->mask = CYCLECOUNTER_MASK(32);
47445dfa45fSJohn Whittington 	cc->shift = 32 - bits_per(NSEC_PER_SEC / GS_USB_TIMESTAMP_TIMER_HZ);
47545dfa45fSJohn Whittington 	cc->mult = clocksource_hz2mult(GS_USB_TIMESTAMP_TIMER_HZ, cc->shift);
47645dfa45fSJohn Whittington 
4775886e4d5SMarc Kleine-Budde 	spin_lock_init(&parent->tc_lock);
4785886e4d5SMarc Kleine-Budde 	spin_lock_bh(&parent->tc_lock);
4795886e4d5SMarc Kleine-Budde 	timecounter_init(&parent->tc, &parent->cc, ktime_get_real_ns());
4805886e4d5SMarc Kleine-Budde 	spin_unlock_bh(&parent->tc_lock);
48145dfa45fSJohn Whittington 
4825886e4d5SMarc Kleine-Budde 	INIT_DELAYED_WORK(&parent->timestamp, gs_usb_timestamp_work);
4835886e4d5SMarc Kleine-Budde 	schedule_delayed_work(&parent->timestamp,
48445dfa45fSJohn Whittington 			      GS_USB_TIMESTAMP_WORK_DELAY_SEC * HZ);
48545dfa45fSJohn Whittington }
48645dfa45fSJohn Whittington 
gs_usb_timestamp_stop(struct gs_usb * parent)4875886e4d5SMarc Kleine-Budde static void gs_usb_timestamp_stop(struct gs_usb *parent)
48845dfa45fSJohn Whittington {
4895886e4d5SMarc Kleine-Budde 	cancel_delayed_work_sync(&parent->timestamp);
49045dfa45fSJohn Whittington }
49145dfa45fSJohn Whittington 
gs_update_state(struct gs_can * dev,struct can_frame * cf)492d08e973aSMaximilian Schneider static void gs_update_state(struct gs_can *dev, struct can_frame *cf)
493d08e973aSMaximilian Schneider {
494d08e973aSMaximilian Schneider 	struct can_device_stats *can_stats = &dev->can.can_stats;
495d08e973aSMaximilian Schneider 
496d08e973aSMaximilian Schneider 	if (cf->can_id & CAN_ERR_RESTARTED) {
497d08e973aSMaximilian Schneider 		dev->can.state = CAN_STATE_ERROR_ACTIVE;
498d08e973aSMaximilian Schneider 		can_stats->restarts++;
499d08e973aSMaximilian Schneider 	} else if (cf->can_id & CAN_ERR_BUSOFF) {
500d08e973aSMaximilian Schneider 		dev->can.state = CAN_STATE_BUS_OFF;
501d08e973aSMaximilian Schneider 		can_stats->bus_off++;
502d08e973aSMaximilian Schneider 	} else if (cf->can_id & CAN_ERR_CRTL) {
503d08e973aSMaximilian Schneider 		if ((cf->data[1] & CAN_ERR_CRTL_TX_WARNING) ||
504d08e973aSMaximilian Schneider 		    (cf->data[1] & CAN_ERR_CRTL_RX_WARNING)) {
505d08e973aSMaximilian Schneider 			dev->can.state = CAN_STATE_ERROR_WARNING;
506d08e973aSMaximilian Schneider 			can_stats->error_warning++;
507d08e973aSMaximilian Schneider 		} else if ((cf->data[1] & CAN_ERR_CRTL_TX_PASSIVE) ||
508d08e973aSMaximilian Schneider 			   (cf->data[1] & CAN_ERR_CRTL_RX_PASSIVE)) {
509d08e973aSMaximilian Schneider 			dev->can.state = CAN_STATE_ERROR_PASSIVE;
510d08e973aSMaximilian Schneider 			can_stats->error_passive++;
511d08e973aSMaximilian Schneider 		} else {
512d08e973aSMaximilian Schneider 			dev->can.state = CAN_STATE_ERROR_ACTIVE;
513d08e973aSMaximilian Schneider 		}
514d08e973aSMaximilian Schneider 	}
515d08e973aSMaximilian Schneider }
516d08e973aSMaximilian Schneider 
gs_usb_set_timestamp(struct gs_can * dev,struct sk_buff * skb,const struct gs_host_frame * hf)51724bc41b4SMarc Kleine-Budde static u32 gs_usb_set_timestamp(struct gs_can *dev, struct sk_buff *skb,
51845dfa45fSJohn Whittington 				const struct gs_host_frame *hf)
51945dfa45fSJohn Whittington {
52045dfa45fSJohn Whittington 	u32 timestamp;
52145dfa45fSJohn Whittington 
52245dfa45fSJohn Whittington 	if (hf->flags & GS_CAN_FLAG_FD)
52345dfa45fSJohn Whittington 		timestamp = le32_to_cpu(hf->canfd_ts->timestamp_us);
52445dfa45fSJohn Whittington 	else
52545dfa45fSJohn Whittington 		timestamp = le32_to_cpu(hf->classic_can_ts->timestamp_us);
52645dfa45fSJohn Whittington 
52724bc41b4SMarc Kleine-Budde 	if (skb)
52845dfa45fSJohn Whittington 		gs_usb_skb_set_timestamp(dev, skb, timestamp);
52924bc41b4SMarc Kleine-Budde 
53024bc41b4SMarc Kleine-Budde 	return timestamp;
53124bc41b4SMarc Kleine-Budde }
53224bc41b4SMarc Kleine-Budde 
gs_usb_rx_offload(struct gs_can * dev,struct sk_buff * skb,const struct gs_host_frame * hf)53324bc41b4SMarc Kleine-Budde static void gs_usb_rx_offload(struct gs_can *dev, struct sk_buff *skb,
53424bc41b4SMarc Kleine-Budde 			      const struct gs_host_frame *hf)
53524bc41b4SMarc Kleine-Budde {
53624bc41b4SMarc Kleine-Budde 	struct can_rx_offload *offload = &dev->offload;
53724bc41b4SMarc Kleine-Budde 	int rc;
53824bc41b4SMarc Kleine-Budde 
53924bc41b4SMarc Kleine-Budde 	if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP) {
54024bc41b4SMarc Kleine-Budde 		const u32 ts = gs_usb_set_timestamp(dev, skb, hf);
54124bc41b4SMarc Kleine-Budde 
54224bc41b4SMarc Kleine-Budde 		rc = can_rx_offload_queue_timestamp(offload, skb, ts);
54324bc41b4SMarc Kleine-Budde 	} else {
54424bc41b4SMarc Kleine-Budde 		rc = can_rx_offload_queue_tail(offload, skb);
54524bc41b4SMarc Kleine-Budde 	}
54624bc41b4SMarc Kleine-Budde 
54724bc41b4SMarc Kleine-Budde 	if (rc)
54824bc41b4SMarc Kleine-Budde 		dev->netdev->stats.rx_fifo_errors++;
54924bc41b4SMarc Kleine-Budde }
55024bc41b4SMarc Kleine-Budde 
55124bc41b4SMarc Kleine-Budde static unsigned int
gs_usb_get_echo_skb(struct gs_can * dev,struct sk_buff * skb,const struct gs_host_frame * hf)55224bc41b4SMarc Kleine-Budde gs_usb_get_echo_skb(struct gs_can *dev, struct sk_buff *skb,
55324bc41b4SMarc Kleine-Budde 		    const struct gs_host_frame *hf)
55424bc41b4SMarc Kleine-Budde {
55524bc41b4SMarc Kleine-Budde 	struct can_rx_offload *offload = &dev->offload;
55624bc41b4SMarc Kleine-Budde 	const u32 echo_id = hf->echo_id;
55724bc41b4SMarc Kleine-Budde 	unsigned int len;
55824bc41b4SMarc Kleine-Budde 
55924bc41b4SMarc Kleine-Budde 	if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP) {
56024bc41b4SMarc Kleine-Budde 		const u32 ts = gs_usb_set_timestamp(dev, skb, hf);
56124bc41b4SMarc Kleine-Budde 
56224bc41b4SMarc Kleine-Budde 		len = can_rx_offload_get_echo_skb_queue_timestamp(offload, echo_id,
56324bc41b4SMarc Kleine-Budde 								  ts, NULL);
56424bc41b4SMarc Kleine-Budde 	} else {
56524bc41b4SMarc Kleine-Budde 		len = can_rx_offload_get_echo_skb_queue_tail(offload, echo_id,
56624bc41b4SMarc Kleine-Budde 							     NULL);
56724bc41b4SMarc Kleine-Budde 	}
56824bc41b4SMarc Kleine-Budde 
56924bc41b4SMarc Kleine-Budde 	return len;
57045dfa45fSJohn Whittington }
57145dfa45fSJohn Whittington 
gs_usb_receive_bulk_callback(struct urb * urb)572ae421e33SNik Nyby static void gs_usb_receive_bulk_callback(struct urb *urb)
573d08e973aSMaximilian Schneider {
574b6980ad3SMarc Kleine-Budde 	struct gs_usb *parent = urb->context;
575d08e973aSMaximilian Schneider 	struct gs_can *dev;
576d08e973aSMaximilian Schneider 	struct net_device *netdev;
577d08e973aSMaximilian Schneider 	int rc;
578d08e973aSMaximilian Schneider 	struct net_device_stats *stats;
579d08e973aSMaximilian Schneider 	struct gs_host_frame *hf = urb->transfer_buffer;
580d08e973aSMaximilian Schneider 	struct gs_tx_context *txc;
581d08e973aSMaximilian Schneider 	struct can_frame *cf;
58226949ac9SPeter Fink 	struct canfd_frame *cfd;
583d08e973aSMaximilian Schneider 	struct sk_buff *skb;
584d08e973aSMaximilian Schneider 
585b6980ad3SMarc Kleine-Budde 	BUG_ON(!parent);
586d08e973aSMaximilian Schneider 
587d08e973aSMaximilian Schneider 	switch (urb->status) {
588d08e973aSMaximilian Schneider 	case 0: /* success */
589d08e973aSMaximilian Schneider 		break;
590d08e973aSMaximilian Schneider 	case -ENOENT:
591d08e973aSMaximilian Schneider 	case -ESHUTDOWN:
592d08e973aSMaximilian Schneider 		return;
593d08e973aSMaximilian Schneider 	default:
594d08e973aSMaximilian Schneider 		/* do not resubmit aborted urbs. eg: when device goes down */
595d08e973aSMaximilian Schneider 		return;
596d08e973aSMaximilian Schneider 	}
597d08e973aSMaximilian Schneider 
598d08e973aSMaximilian Schneider 	/* device reports out of range channel id */
599d08e973aSMaximilian Schneider 	if (hf->channel >= GS_MAX_INTF)
6004a8737ffSMarc Kleine-Budde 		goto device_detach;
601d08e973aSMaximilian Schneider 
602b6980ad3SMarc Kleine-Budde 	dev = parent->canch[hf->channel];
603d08e973aSMaximilian Schneider 
604d08e973aSMaximilian Schneider 	netdev = dev->netdev;
605d08e973aSMaximilian Schneider 	stats = &netdev->stats;
606d08e973aSMaximilian Schneider 
607d08e973aSMaximilian Schneider 	if (!netif_device_present(netdev))
608d08e973aSMaximilian Schneider 		return;
609d08e973aSMaximilian Schneider 
6105886e4d5SMarc Kleine-Budde 	if (!netif_running(netdev))
6115886e4d5SMarc Kleine-Budde 		goto resubmit_urb;
6125886e4d5SMarc Kleine-Budde 
613d08e973aSMaximilian Schneider 	if (hf->echo_id == -1) { /* normal rx */
61426949ac9SPeter Fink 		if (hf->flags & GS_CAN_FLAG_FD) {
615fcb880efSMarc Kleine-Budde 			skb = alloc_canfd_skb(netdev, &cfd);
61626949ac9SPeter Fink 			if (!skb)
61726949ac9SPeter Fink 				return;
61826949ac9SPeter Fink 
61926949ac9SPeter Fink 			cfd->can_id = le32_to_cpu(hf->can_id);
62026949ac9SPeter Fink 			cfd->len = can_fd_dlc2len(hf->can_dlc);
62126949ac9SPeter Fink 			if (hf->flags & GS_CAN_FLAG_BRS)
62226949ac9SPeter Fink 				cfd->flags |= CANFD_BRS;
62326949ac9SPeter Fink 			if (hf->flags & GS_CAN_FLAG_ESI)
62426949ac9SPeter Fink 				cfd->flags |= CANFD_ESI;
62526949ac9SPeter Fink 
62626949ac9SPeter Fink 			memcpy(cfd->data, hf->canfd->data, cfd->len);
62726949ac9SPeter Fink 		} else {
628fcb880efSMarc Kleine-Budde 			skb = alloc_can_skb(netdev, &cf);
629d08e973aSMaximilian Schneider 			if (!skb)
630d08e973aSMaximilian Schneider 				return;
631d08e973aSMaximilian Schneider 
6324ba1cb39SMarc Kleine-Budde 			cf->can_id = le32_to_cpu(hf->can_id);
6334c01fc87SOliver Hartkopp 			can_frame_set_cc_len(cf, hf->can_dlc, dev->can.ctrlmode);
63426949ac9SPeter Fink 
635c359931dSPeter Fink 			memcpy(cf->data, hf->classic_can->data, 8);
636d08e973aSMaximilian Schneider 
637d08e973aSMaximilian Schneider 			/* ERROR frames tell us information about the controller */
6384ba1cb39SMarc Kleine-Budde 			if (le32_to_cpu(hf->can_id) & CAN_ERR_FLAG)
639d08e973aSMaximilian Schneider 				gs_update_state(dev, cf);
64026949ac9SPeter Fink 		}
641d08e973aSMaximilian Schneider 
64224bc41b4SMarc Kleine-Budde 		gs_usb_rx_offload(dev, skb, hf);
643d08e973aSMaximilian Schneider 	} else { /* echo_id == hf->echo_id */
644d08e973aSMaximilian Schneider 		if (hf->echo_id >= GS_MAX_TX_URBS) {
645d08e973aSMaximilian Schneider 			netdev_err(netdev,
64610819466SMarc Kleine-Budde 				   "Unexpected out of range echo id %u\n",
647d08e973aSMaximilian Schneider 				   hf->echo_id);
648d08e973aSMaximilian Schneider 			goto resubmit_urb;
649d08e973aSMaximilian Schneider 		}
650d08e973aSMaximilian Schneider 
651d08e973aSMaximilian Schneider 		txc = gs_get_tx_context(dev, hf->echo_id);
652d08e973aSMaximilian Schneider 
653d08e973aSMaximilian Schneider 		/* bad devices send bad echo_ids. */
654d08e973aSMaximilian Schneider 		if (!txc) {
655d08e973aSMaximilian Schneider 			netdev_err(netdev,
65610819466SMarc Kleine-Budde 				   "Unexpected unused echo id %u\n",
657d08e973aSMaximilian Schneider 				   hf->echo_id);
658d08e973aSMaximilian Schneider 			goto resubmit_urb;
659d08e973aSMaximilian Schneider 		}
660d08e973aSMaximilian Schneider 
66145dfa45fSJohn Whittington 		skb = dev->can.echo_skb[hf->echo_id];
66255ad95d9SMarc Kleine-Budde 		stats->tx_packets++;
66324bc41b4SMarc Kleine-Budde 		stats->tx_bytes += gs_usb_get_echo_skb(dev, skb, hf);
664d08e973aSMaximilian Schneider 		gs_free_tx_context(txc);
665d08e973aSMaximilian Schneider 
66697819f94SWolfgang Grandegger 		atomic_dec(&dev->active_tx_urbs);
66797819f94SWolfgang Grandegger 
668d08e973aSMaximilian Schneider 		netif_wake_queue(netdev);
669d08e973aSMaximilian Schneider 	}
670d08e973aSMaximilian Schneider 
671d08e973aSMaximilian Schneider 	if (hf->flags & GS_CAN_FLAG_OVERFLOW) {
6726c8bc15fSMarc Kleine-Budde 		stats->rx_over_errors++;
6736c8bc15fSMarc Kleine-Budde 		stats->rx_errors++;
6746c8bc15fSMarc Kleine-Budde 
675d08e973aSMaximilian Schneider 		skb = alloc_can_err_skb(netdev, &cf);
676d08e973aSMaximilian Schneider 		if (!skb)
677d08e973aSMaximilian Schneider 			goto resubmit_urb;
678d08e973aSMaximilian Schneider 
679d08e973aSMaximilian Schneider 		cf->can_id |= CAN_ERR_CRTL;
680c7b74967SOliver Hartkopp 		cf->len = CAN_ERR_DLC;
681d08e973aSMaximilian Schneider 		cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
68224bc41b4SMarc Kleine-Budde 
68324bc41b4SMarc Kleine-Budde 		gs_usb_rx_offload(dev, skb, hf);
684d08e973aSMaximilian Schneider 	}
685d08e973aSMaximilian Schneider 
68624bc41b4SMarc Kleine-Budde 	can_rx_offload_irq_finish(&dev->offload);
68724bc41b4SMarc Kleine-Budde 
688d08e973aSMaximilian Schneider resubmit_urb:
689b6980ad3SMarc Kleine-Budde 	usb_fill_bulk_urb(urb, parent->udev,
690*1cdd3563SAlexander Kozhinov 			  parent->pipe_in,
691c359931dSPeter Fink 			  hf, dev->parent->hf_size_rx,
692b6980ad3SMarc Kleine-Budde 			  gs_usb_receive_bulk_callback, parent);
693d08e973aSMaximilian Schneider 
694d08e973aSMaximilian Schneider 	rc = usb_submit_urb(urb, GFP_ATOMIC);
695d08e973aSMaximilian Schneider 
696d08e973aSMaximilian Schneider 	/* USB failure take down all interfaces */
697d08e973aSMaximilian Schneider 	if (rc == -ENODEV) {
6984a8737ffSMarc Kleine-Budde device_detach:
699d08e973aSMaximilian Schneider 		for (rc = 0; rc < GS_MAX_INTF; rc++) {
700b6980ad3SMarc Kleine-Budde 			if (parent->canch[rc])
701b6980ad3SMarc Kleine-Budde 				netif_device_detach(parent->canch[rc]->netdev);
702d08e973aSMaximilian Schneider 		}
703d08e973aSMaximilian Schneider 	}
704d08e973aSMaximilian Schneider }
705d08e973aSMaximilian Schneider 
gs_usb_set_bittiming(struct net_device * netdev)706d08e973aSMaximilian Schneider static int gs_usb_set_bittiming(struct net_device *netdev)
707d08e973aSMaximilian Schneider {
708d08e973aSMaximilian Schneider 	struct gs_can *dev = netdev_priv(netdev);
709d08e973aSMaximilian Schneider 	struct can_bittiming *bt = &dev->can.bittiming;
7103814ed27SMarc Kleine-Budde 	struct gs_device_bittiming dbt = {
7113814ed27SMarc Kleine-Budde 		.prop_seg = cpu_to_le32(bt->prop_seg),
7123814ed27SMarc Kleine-Budde 		.phase_seg1 = cpu_to_le32(bt->phase_seg1),
7133814ed27SMarc Kleine-Budde 		.phase_seg2 = cpu_to_le32(bt->phase_seg2),
7143814ed27SMarc Kleine-Budde 		.sjw = cpu_to_le32(bt->sjw),
7153814ed27SMarc Kleine-Budde 		.brp = cpu_to_le32(bt->brp),
7163814ed27SMarc Kleine-Budde 	};
717d08e973aSMaximilian Schneider 
718d08e973aSMaximilian Schneider 	/* request bit timings */
71956c56a30SVincent Mailhol 	return usb_control_msg_send(dev->udev, 0, GS_USB_BREQ_BITTIMING,
720d08e973aSMaximilian Schneider 				    USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
7213814ed27SMarc Kleine-Budde 				    dev->channel, 0, &dbt, sizeof(dbt), 1000,
7223814ed27SMarc Kleine-Budde 				    GFP_KERNEL);
723d08e973aSMaximilian Schneider }
724d08e973aSMaximilian Schneider 
gs_usb_set_data_bittiming(struct net_device * netdev)72526949ac9SPeter Fink static int gs_usb_set_data_bittiming(struct net_device *netdev)
72626949ac9SPeter Fink {
72726949ac9SPeter Fink 	struct gs_can *dev = netdev_priv(netdev);
72826949ac9SPeter Fink 	struct can_bittiming *bt = &dev->can.data_bittiming;
7293814ed27SMarc Kleine-Budde 	struct gs_device_bittiming dbt = {
7303814ed27SMarc Kleine-Budde 		.prop_seg = cpu_to_le32(bt->prop_seg),
7313814ed27SMarc Kleine-Budde 		.phase_seg1 = cpu_to_le32(bt->phase_seg1),
7323814ed27SMarc Kleine-Budde 		.phase_seg2 = cpu_to_le32(bt->phase_seg2),
7333814ed27SMarc Kleine-Budde 		.sjw = cpu_to_le32(bt->sjw),
7343814ed27SMarc Kleine-Budde 		.brp = cpu_to_le32(bt->brp),
7353814ed27SMarc Kleine-Budde 	};
73632cd9013SMarc Kleine-Budde 	u8 request = GS_USB_BREQ_DATA_BITTIMING;
73726949ac9SPeter Fink 
73832cd9013SMarc Kleine-Budde 	if (dev->feature & GS_CAN_FEATURE_QUIRK_BREQ_CANTACT_PRO)
73932cd9013SMarc Kleine-Budde 		request = GS_USB_BREQ_QUIRK_CANTACT_PRO_DATA_BITTIMING;
74032cd9013SMarc Kleine-Budde 
7413814ed27SMarc Kleine-Budde 	/* request data bit timings */
74256c56a30SVincent Mailhol 	return usb_control_msg_send(dev->udev, 0, request,
74326949ac9SPeter Fink 				    USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
7443814ed27SMarc Kleine-Budde 				    dev->channel, 0, &dbt, sizeof(dbt), 1000,
7453814ed27SMarc Kleine-Budde 				    GFP_KERNEL);
74626949ac9SPeter Fink }
74726949ac9SPeter Fink 
gs_usb_xmit_callback(struct urb * urb)748d08e973aSMaximilian Schneider static void gs_usb_xmit_callback(struct urb *urb)
749d08e973aSMaximilian Schneider {
750d08e973aSMaximilian Schneider 	struct gs_tx_context *txc = urb->context;
751d08e973aSMaximilian Schneider 	struct gs_can *dev = txc->dev;
752d08e973aSMaximilian Schneider 	struct net_device *netdev = dev->netdev;
753d08e973aSMaximilian Schneider 
754d08e973aSMaximilian Schneider 	if (urb->status)
75510819466SMarc Kleine-Budde 		netdev_info(netdev, "usb xmit fail %u\n", txc->echo_id);
756d08e973aSMaximilian Schneider }
757d08e973aSMaximilian Schneider 
gs_can_start_xmit(struct sk_buff * skb,struct net_device * netdev)7589be95c91SMaximilian Schneider static netdev_tx_t gs_can_start_xmit(struct sk_buff *skb,
7599be95c91SMaximilian Schneider 				     struct net_device *netdev)
760d08e973aSMaximilian Schneider {
761d08e973aSMaximilian Schneider 	struct gs_can *dev = netdev_priv(netdev);
762d08e973aSMaximilian Schneider 	struct net_device_stats *stats = &dev->netdev->stats;
763d08e973aSMaximilian Schneider 	struct urb *urb;
764d08e973aSMaximilian Schneider 	struct gs_host_frame *hf;
765d08e973aSMaximilian Schneider 	struct can_frame *cf;
76626949ac9SPeter Fink 	struct canfd_frame *cfd;
767d08e973aSMaximilian Schneider 	int rc;
768d08e973aSMaximilian Schneider 	unsigned int idx;
769d08e973aSMaximilian Schneider 	struct gs_tx_context *txc;
770d08e973aSMaximilian Schneider 
771ae64438bSOliver Hartkopp 	if (can_dev_dropped_skb(netdev, skb))
772d08e973aSMaximilian Schneider 		return NETDEV_TX_OK;
773d08e973aSMaximilian Schneider 
774d08e973aSMaximilian Schneider 	/* find an empty context to keep track of transmission */
775d08e973aSMaximilian Schneider 	txc = gs_alloc_tx_context(dev);
776d08e973aSMaximilian Schneider 	if (!txc)
777d08e973aSMaximilian Schneider 		return NETDEV_TX_BUSY;
778d08e973aSMaximilian Schneider 
779d08e973aSMaximilian Schneider 	/* create a URB, and a buffer for it */
780d08e973aSMaximilian Schneider 	urb = usb_alloc_urb(0, GFP_ATOMIC);
7812a4a1e40SWolfram Sang 	if (!urb)
782d08e973aSMaximilian Schneider 		goto nomem_urb;
783d08e973aSMaximilian Schneider 
78462f102c0SVasanth Sadhasivan 	hf = kmalloc(dev->hf_size_tx, GFP_ATOMIC);
7851494ffe4SMarc Kleine-Budde 	if (!hf)
786d08e973aSMaximilian Schneider 		goto nomem_hf;
787d08e973aSMaximilian Schneider 
788d08e973aSMaximilian Schneider 	idx = txc->echo_id;
789d08e973aSMaximilian Schneider 
790d08e973aSMaximilian Schneider 	if (idx >= GS_MAX_TX_URBS) {
79110819466SMarc Kleine-Budde 		netdev_err(netdev, "Invalid tx context %u\n", idx);
792d08e973aSMaximilian Schneider 		goto badidx;
793d08e973aSMaximilian Schneider 	}
794d08e973aSMaximilian Schneider 
795d08e973aSMaximilian Schneider 	hf->echo_id = idx;
796d08e973aSMaximilian Schneider 	hf->channel = dev->channel;
79789d58aebSBrian Silverman 	hf->flags = 0;
79889d58aebSBrian Silverman 	hf->reserved = 0;
799d08e973aSMaximilian Schneider 
80026949ac9SPeter Fink 	if (can_is_canfd_skb(skb)) {
80126949ac9SPeter Fink 		cfd = (struct canfd_frame *)skb->data;
80226949ac9SPeter Fink 
80326949ac9SPeter Fink 		hf->can_id = cpu_to_le32(cfd->can_id);
80426949ac9SPeter Fink 		hf->can_dlc = can_fd_len2dlc(cfd->len);
80526949ac9SPeter Fink 		hf->flags |= GS_CAN_FLAG_FD;
80626949ac9SPeter Fink 		if (cfd->flags & CANFD_BRS)
80726949ac9SPeter Fink 			hf->flags |= GS_CAN_FLAG_BRS;
80826949ac9SPeter Fink 		if (cfd->flags & CANFD_ESI)
80926949ac9SPeter Fink 			hf->flags |= GS_CAN_FLAG_ESI;
81026949ac9SPeter Fink 
81126949ac9SPeter Fink 		memcpy(hf->canfd->data, cfd->data, cfd->len);
81226949ac9SPeter Fink 	} else {
813d08e973aSMaximilian Schneider 		cf = (struct can_frame *)skb->data;
814d08e973aSMaximilian Schneider 
8154ba1cb39SMarc Kleine-Budde 		hf->can_id = cpu_to_le32(cf->can_id);
8164c01fc87SOliver Hartkopp 		hf->can_dlc = can_get_cc_dlc(cf, dev->can.ctrlmode);
8174c01fc87SOliver Hartkopp 
818c359931dSPeter Fink 		memcpy(hf->classic_can->data, cf->data, cf->len);
81926949ac9SPeter Fink 	}
820d08e973aSMaximilian Schneider 
821d08e973aSMaximilian Schneider 	usb_fill_bulk_urb(urb, dev->udev,
822*1cdd3563SAlexander Kozhinov 			  dev->parent->pipe_out,
823c359931dSPeter Fink 			  hf, dev->hf_size_tx,
824c1ee7269SMarc Kleine-Budde 			  gs_usb_xmit_callback, txc);
825d08e973aSMaximilian Schneider 
82662f102c0SVasanth Sadhasivan 	urb->transfer_flags |= URB_FREE_BUFFER;
827d08e973aSMaximilian Schneider 	usb_anchor_urb(urb, &dev->tx_submitted);
828d08e973aSMaximilian Schneider 
8291dcb6e57SVincent Mailhol 	can_put_echo_skb(skb, netdev, idx, 0);
830d08e973aSMaximilian Schneider 
831d08e973aSMaximilian Schneider 	atomic_inc(&dev->active_tx_urbs);
832d08e973aSMaximilian Schneider 
833d08e973aSMaximilian Schneider 	rc = usb_submit_urb(urb, GFP_ATOMIC);
834d08e973aSMaximilian Schneider 	if (unlikely(rc)) {			/* usb send failed */
835d08e973aSMaximilian Schneider 		atomic_dec(&dev->active_tx_urbs);
836d08e973aSMaximilian Schneider 
837f318482aSMarc Kleine-Budde 		can_free_echo_skb(netdev, idx, NULL);
838d08e973aSMaximilian Schneider 		gs_free_tx_context(txc);
839d08e973aSMaximilian Schneider 
840d08e973aSMaximilian Schneider 		usb_unanchor_urb(urb);
841d08e973aSMaximilian Schneider 
842d08e973aSMaximilian Schneider 		if (rc == -ENODEV) {
843d08e973aSMaximilian Schneider 			netif_device_detach(netdev);
844d08e973aSMaximilian Schneider 		} else {
845d08e973aSMaximilian Schneider 			netdev_err(netdev, "usb_submit failed (err=%d)\n", rc);
846d08e973aSMaximilian Schneider 			stats->tx_dropped++;
847d08e973aSMaximilian Schneider 		}
848d08e973aSMaximilian Schneider 	} else {
849d08e973aSMaximilian Schneider 		/* Slow down tx path */
850d08e973aSMaximilian Schneider 		if (atomic_read(&dev->active_tx_urbs) >= GS_MAX_TX_URBS)
851d08e973aSMaximilian Schneider 			netif_stop_queue(netdev);
852d08e973aSMaximilian Schneider 	}
853d08e973aSMaximilian Schneider 
854d08e973aSMaximilian Schneider 	/* let usb core take care of this urb */
855d08e973aSMaximilian Schneider 	usb_free_urb(urb);
856d08e973aSMaximilian Schneider 
857d08e973aSMaximilian Schneider 	return NETDEV_TX_OK;
858d08e973aSMaximilian Schneider 
859d08e973aSMaximilian Schneider badidx:
86062f102c0SVasanth Sadhasivan 	kfree(hf);
861d08e973aSMaximilian Schneider nomem_hf:
862d08e973aSMaximilian Schneider 	usb_free_urb(urb);
863d08e973aSMaximilian Schneider 
864d08e973aSMaximilian Schneider nomem_urb:
865d08e973aSMaximilian Schneider 	gs_free_tx_context(txc);
866d08e973aSMaximilian Schneider 	dev_kfree_skb(skb);
867d08e973aSMaximilian Schneider 	stats->tx_dropped++;
868d08e973aSMaximilian Schneider 	return NETDEV_TX_OK;
869d08e973aSMaximilian Schneider }
870d08e973aSMaximilian Schneider 
gs_can_open(struct net_device * netdev)871d08e973aSMaximilian Schneider static int gs_can_open(struct net_device *netdev)
872d08e973aSMaximilian Schneider {
873d08e973aSMaximilian Schneider 	struct gs_can *dev = netdev_priv(netdev);
874d08e973aSMaximilian Schneider 	struct gs_usb *parent = dev->parent;
8753814ed27SMarc Kleine-Budde 	struct gs_device_mode dm = {
8763814ed27SMarc Kleine-Budde 		.mode = cpu_to_le32(GS_CAN_MODE_START),
8773814ed27SMarc Kleine-Budde 	};
878c359931dSPeter Fink 	struct gs_host_frame *hf;
8792603be9eSMarc Kleine-Budde 	struct urb *urb = NULL;
880d08e973aSMaximilian Schneider 	u32 ctrlmode;
8814ba1cb39SMarc Kleine-Budde 	u32 flags = 0;
8823814ed27SMarc Kleine-Budde 	int rc, i;
883d08e973aSMaximilian Schneider 
884d08e973aSMaximilian Schneider 	rc = open_candev(netdev);
885d08e973aSMaximilian Schneider 	if (rc)
886d08e973aSMaximilian Schneider 		return rc;
887d08e973aSMaximilian Schneider 
88826949ac9SPeter Fink 	ctrlmode = dev->can.ctrlmode;
88926949ac9SPeter Fink 	if (ctrlmode & CAN_CTRLMODE_FD) {
890eb9fa77aSPeter Fink 		if (dev->feature & GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX)
891eb9fa77aSPeter Fink 			dev->hf_size_tx = struct_size(hf, canfd_quirk, 1);
892eb9fa77aSPeter Fink 		else
89326949ac9SPeter Fink 			dev->hf_size_tx = struct_size(hf, canfd, 1);
89426949ac9SPeter Fink 	} else {
895eb9fa77aSPeter Fink 		if (dev->feature & GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX)
896eb9fa77aSPeter Fink 			dev->hf_size_tx = struct_size(hf, classic_can_quirk, 1);
897eb9fa77aSPeter Fink 		else
898c359931dSPeter Fink 			dev->hf_size_tx = struct_size(hf, classic_can, 1);
89926949ac9SPeter Fink 	}
900c359931dSPeter Fink 
90124bc41b4SMarc Kleine-Budde 	can_rx_offload_enable(&dev->offload);
90224bc41b4SMarc Kleine-Budde 
903035b0fcfSVincent Mailhol 	if (!parent->active_channels) {
9045886e4d5SMarc Kleine-Budde 		if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
9055886e4d5SMarc Kleine-Budde 			gs_usb_timestamp_init(parent);
9065886e4d5SMarc Kleine-Budde 
907d08e973aSMaximilian Schneider 		for (i = 0; i < GS_MAX_RX_URBS; i++) {
908d08e973aSMaximilian Schneider 			u8 *buf;
909d08e973aSMaximilian Schneider 
910d08e973aSMaximilian Schneider 			/* alloc rx urb */
911d08e973aSMaximilian Schneider 			urb = usb_alloc_urb(0, GFP_KERNEL);
9122603be9eSMarc Kleine-Budde 			if (!urb) {
9132603be9eSMarc Kleine-Budde 				rc = -ENOMEM;
9142603be9eSMarc Kleine-Budde 				goto out_usb_kill_anchored_urbs;
9152603be9eSMarc Kleine-Budde 			}
916d08e973aSMaximilian Schneider 
917d08e973aSMaximilian Schneider 			/* alloc rx buffer */
91862f102c0SVasanth Sadhasivan 			buf = kmalloc(dev->parent->hf_size_rx,
91962f102c0SVasanth Sadhasivan 				      GFP_KERNEL);
920d08e973aSMaximilian Schneider 			if (!buf) {
9212603be9eSMarc Kleine-Budde 				rc = -ENOMEM;
9222603be9eSMarc Kleine-Budde 				goto out_usb_free_urb;
923d08e973aSMaximilian Schneider 			}
924d08e973aSMaximilian Schneider 
925d08e973aSMaximilian Schneider 			/* fill, anchor, and submit rx urb */
926d08e973aSMaximilian Schneider 			usb_fill_bulk_urb(urb,
927d08e973aSMaximilian Schneider 					  dev->udev,
928*1cdd3563SAlexander Kozhinov 					  dev->parent->pipe_in,
929d08e973aSMaximilian Schneider 					  buf,
930c359931dSPeter Fink 					  dev->parent->hf_size_rx,
931c1ee7269SMarc Kleine-Budde 					  gs_usb_receive_bulk_callback, parent);
93262f102c0SVasanth Sadhasivan 			urb->transfer_flags |= URB_FREE_BUFFER;
933d08e973aSMaximilian Schneider 
934d08e973aSMaximilian Schneider 			usb_anchor_urb(urb, &parent->rx_submitted);
935d08e973aSMaximilian Schneider 
936d08e973aSMaximilian Schneider 			rc = usb_submit_urb(urb, GFP_KERNEL);
937d08e973aSMaximilian Schneider 			if (rc) {
938d08e973aSMaximilian Schneider 				if (rc == -ENODEV)
939d08e973aSMaximilian Schneider 					netif_device_detach(dev->netdev);
940d08e973aSMaximilian Schneider 
941d08e973aSMaximilian Schneider 				netdev_err(netdev,
9421494ffe4SMarc Kleine-Budde 					   "usb_submit_urb() failed, error %pe\n",
9431494ffe4SMarc Kleine-Budde 					   ERR_PTR(rc));
944d08e973aSMaximilian Schneider 
9452603be9eSMarc Kleine-Budde 				goto out_usb_unanchor_urb;
946d08e973aSMaximilian Schneider 			}
947d08e973aSMaximilian Schneider 
948d08e973aSMaximilian Schneider 			/* Drop reference,
949d08e973aSMaximilian Schneider 			 * USB core will take care of freeing it
950d08e973aSMaximilian Schneider 			 */
951d08e973aSMaximilian Schneider 			usb_free_urb(urb);
952d08e973aSMaximilian Schneider 		}
953d08e973aSMaximilian Schneider 	}
954d08e973aSMaximilian Schneider 
955d08e973aSMaximilian Schneider 	/* flags */
956d08e973aSMaximilian Schneider 	if (ctrlmode & CAN_CTRLMODE_LOOPBACK)
9574ba1cb39SMarc Kleine-Budde 		flags |= GS_CAN_MODE_LOOP_BACK;
958deb8534eSMarc Kleine-Budde 
959deb8534eSMarc Kleine-Budde 	if (ctrlmode & CAN_CTRLMODE_LISTENONLY)
9604ba1cb39SMarc Kleine-Budde 		flags |= GS_CAN_MODE_LISTEN_ONLY;
961d08e973aSMaximilian Schneider 
962f6adf410SMarc Kleine-Budde 	if (ctrlmode & CAN_CTRLMODE_3_SAMPLES)
963f6adf410SMarc Kleine-Budde 		flags |= GS_CAN_MODE_TRIPLE_SAMPLE;
964f6adf410SMarc Kleine-Budde 
965d08e973aSMaximilian Schneider 	if (ctrlmode & CAN_CTRLMODE_ONE_SHOT)
9664ba1cb39SMarc Kleine-Budde 		flags |= GS_CAN_MODE_ONE_SHOT;
967d08e973aSMaximilian Schneider 
9682f3cdad1SJeroen Hofstee 	if (ctrlmode & CAN_CTRLMODE_BERR_REPORTING)
9692f3cdad1SJeroen Hofstee 		flags |= GS_CAN_MODE_BERR_REPORTING;
9702f3cdad1SJeroen Hofstee 
971f6adf410SMarc Kleine-Budde 	if (ctrlmode & CAN_CTRLMODE_FD)
972f6adf410SMarc Kleine-Budde 		flags |= GS_CAN_MODE_FD;
973d08e973aSMaximilian Schneider 
97445dfa45fSJohn Whittington 	/* if hardware supports timestamps, enable it */
9755886e4d5SMarc Kleine-Budde 	if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
97645dfa45fSJohn Whittington 		flags |= GS_CAN_MODE_HW_TIMESTAMP;
97745dfa45fSJohn Whittington 
978d08e973aSMaximilian Schneider 	/* finally start device */
9795440428bSMarc Kleine-Budde 	dev->can.state = CAN_STATE_ERROR_ACTIVE;
9803814ed27SMarc Kleine-Budde 	dm.flags = cpu_to_le32(flags);
98156c56a30SVincent Mailhol 	rc = usb_control_msg_send(dev->udev, 0, GS_USB_BREQ_MODE,
982c1ee7269SMarc Kleine-Budde 				  USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
9833814ed27SMarc Kleine-Budde 				  dev->channel, 0, &dm, sizeof(dm), 1000,
9843814ed27SMarc Kleine-Budde 				  GFP_KERNEL);
9853814ed27SMarc Kleine-Budde 	if (rc) {
986d08e973aSMaximilian Schneider 		netdev_err(netdev, "Couldn't start device (err=%d)\n", rc);
9875440428bSMarc Kleine-Budde 		dev->can.state = CAN_STATE_STOPPED;
9882603be9eSMarc Kleine-Budde 
9892603be9eSMarc Kleine-Budde 		goto out_usb_kill_anchored_urbs;
990d08e973aSMaximilian Schneider 	}
991d08e973aSMaximilian Schneider 
992035b0fcfSVincent Mailhol 	parent->active_channels++;
993d08e973aSMaximilian Schneider 	if (!(dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY))
994d08e973aSMaximilian Schneider 		netif_start_queue(netdev);
995d08e973aSMaximilian Schneider 
996d08e973aSMaximilian Schneider 	return 0;
9972603be9eSMarc Kleine-Budde 
9982603be9eSMarc Kleine-Budde out_usb_unanchor_urb:
9992603be9eSMarc Kleine-Budde 	usb_unanchor_urb(urb);
10002603be9eSMarc Kleine-Budde out_usb_free_urb:
10012603be9eSMarc Kleine-Budde 	usb_free_urb(urb);
10022603be9eSMarc Kleine-Budde out_usb_kill_anchored_urbs:
10035886e4d5SMarc Kleine-Budde 	if (!parent->active_channels) {
10042603be9eSMarc Kleine-Budde 		usb_kill_anchored_urbs(&dev->tx_submitted);
10052603be9eSMarc Kleine-Budde 
10065886e4d5SMarc Kleine-Budde 		if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
10075886e4d5SMarc Kleine-Budde 			gs_usb_timestamp_stop(parent);
10085886e4d5SMarc Kleine-Budde 	}
10095886e4d5SMarc Kleine-Budde 
101024bc41b4SMarc Kleine-Budde 	can_rx_offload_disable(&dev->offload);
10112603be9eSMarc Kleine-Budde 	close_candev(netdev);
10122603be9eSMarc Kleine-Budde 
10132603be9eSMarc Kleine-Budde 	return rc;
1014d08e973aSMaximilian Schneider }
1015d08e973aSMaximilian Schneider 
gs_usb_get_state(const struct net_device * netdev,struct can_berr_counter * bec,enum can_state * state)10160c9f92a4SJeroen Hofstee static int gs_usb_get_state(const struct net_device *netdev,
10170c9f92a4SJeroen Hofstee 			    struct can_berr_counter *bec,
10180c9f92a4SJeroen Hofstee 			    enum can_state *state)
10190c9f92a4SJeroen Hofstee {
10200c9f92a4SJeroen Hofstee 	struct gs_can *dev = netdev_priv(netdev);
10210c9f92a4SJeroen Hofstee 	struct gs_device_state ds;
10220c9f92a4SJeroen Hofstee 	int rc;
10230c9f92a4SJeroen Hofstee 
102456c56a30SVincent Mailhol 	rc = usb_control_msg_recv(dev->udev, 0, GS_USB_BREQ_GET_STATE,
10250c9f92a4SJeroen Hofstee 				  USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
10260c9f92a4SJeroen Hofstee 				  dev->channel, 0,
10270c9f92a4SJeroen Hofstee 				  &ds, sizeof(ds),
10280c9f92a4SJeroen Hofstee 				  USB_CTRL_GET_TIMEOUT,
10290c9f92a4SJeroen Hofstee 				  GFP_KERNEL);
10300c9f92a4SJeroen Hofstee 	if (rc)
10310c9f92a4SJeroen Hofstee 		return rc;
10320c9f92a4SJeroen Hofstee 
10330c9f92a4SJeroen Hofstee 	if (le32_to_cpu(ds.state) >= CAN_STATE_MAX)
10340c9f92a4SJeroen Hofstee 		return -EOPNOTSUPP;
10350c9f92a4SJeroen Hofstee 
10360c9f92a4SJeroen Hofstee 	*state = le32_to_cpu(ds.state);
10370c9f92a4SJeroen Hofstee 	bec->txerr = le32_to_cpu(ds.txerr);
10380c9f92a4SJeroen Hofstee 	bec->rxerr = le32_to_cpu(ds.rxerr);
10390c9f92a4SJeroen Hofstee 
10400c9f92a4SJeroen Hofstee 	return 0;
10410c9f92a4SJeroen Hofstee }
10420c9f92a4SJeroen Hofstee 
gs_usb_can_get_berr_counter(const struct net_device * netdev,struct can_berr_counter * bec)10430c9f92a4SJeroen Hofstee static int gs_usb_can_get_berr_counter(const struct net_device *netdev,
10440c9f92a4SJeroen Hofstee 				       struct can_berr_counter *bec)
10450c9f92a4SJeroen Hofstee {
10460c9f92a4SJeroen Hofstee 	enum can_state state;
10470c9f92a4SJeroen Hofstee 
10480c9f92a4SJeroen Hofstee 	return gs_usb_get_state(netdev, bec, &state);
10490c9f92a4SJeroen Hofstee }
10500c9f92a4SJeroen Hofstee 
gs_can_close(struct net_device * netdev)1051d08e973aSMaximilian Schneider static int gs_can_close(struct net_device *netdev)
1052d08e973aSMaximilian Schneider {
1053d08e973aSMaximilian Schneider 	int rc;
1054d08e973aSMaximilian Schneider 	struct gs_can *dev = netdev_priv(netdev);
1055d08e973aSMaximilian Schneider 	struct gs_usb *parent = dev->parent;
1056d08e973aSMaximilian Schneider 
1057d08e973aSMaximilian Schneider 	netif_stop_queue(netdev);
1058d08e973aSMaximilian Schneider 
1059d08e973aSMaximilian Schneider 	/* Stop polling */
1060035b0fcfSVincent Mailhol 	parent->active_channels--;
10612bda24efSRhett Aultman 	if (!parent->active_channels) {
1062d08e973aSMaximilian Schneider 		usb_kill_anchored_urbs(&parent->rx_submitted);
10635886e4d5SMarc Kleine-Budde 
10645886e4d5SMarc Kleine-Budde 		if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
10655886e4d5SMarc Kleine-Budde 			gs_usb_timestamp_stop(parent);
10662bda24efSRhett Aultman 	}
1067d08e973aSMaximilian Schneider 
1068d08e973aSMaximilian Schneider 	/* Stop sending URBs */
1069d08e973aSMaximilian Schneider 	usb_kill_anchored_urbs(&dev->tx_submitted);
1070d08e973aSMaximilian Schneider 	atomic_set(&dev->active_tx_urbs, 0);
1071d08e973aSMaximilian Schneider 
1072f8a2da6eSMarc Kleine-Budde 	dev->can.state = CAN_STATE_STOPPED;
1073f8a2da6eSMarc Kleine-Budde 
1074d08e973aSMaximilian Schneider 	/* reset the device */
10755c6c313aSMarc Kleine-Budde 	gs_cmd_reset(dev);
1076d08e973aSMaximilian Schneider 
1077d08e973aSMaximilian Schneider 	/* reset tx contexts */
1078d08e973aSMaximilian Schneider 	for (rc = 0; rc < GS_MAX_TX_URBS; rc++) {
1079d08e973aSMaximilian Schneider 		dev->tx_context[rc].dev = dev;
1080d08e973aSMaximilian Schneider 		dev->tx_context[rc].echo_id = GS_MAX_TX_URBS;
1081d08e973aSMaximilian Schneider 	}
1082d08e973aSMaximilian Schneider 
108324bc41b4SMarc Kleine-Budde 	can_rx_offload_disable(&dev->offload);
108424bc41b4SMarc Kleine-Budde 
1085d08e973aSMaximilian Schneider 	/* close the netdev */
1086d08e973aSMaximilian Schneider 	close_candev(netdev);
1087d08e973aSMaximilian Schneider 
1088d08e973aSMaximilian Schneider 	return 0;
1089d08e973aSMaximilian Schneider }
1090d08e973aSMaximilian Schneider 
gs_can_eth_ioctl(struct net_device * netdev,struct ifreq * ifr,int cmd)109145dfa45fSJohn Whittington static int gs_can_eth_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
109245dfa45fSJohn Whittington {
109345dfa45fSJohn Whittington 	const struct gs_can *dev = netdev_priv(netdev);
109445dfa45fSJohn Whittington 
109545dfa45fSJohn Whittington 	if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
109645dfa45fSJohn Whittington 		return can_eth_ioctl_hwts(netdev, ifr, cmd);
109745dfa45fSJohn Whittington 
109845dfa45fSJohn Whittington 	return -EOPNOTSUPP;
109945dfa45fSJohn Whittington }
110045dfa45fSJohn Whittington 
1101d08e973aSMaximilian Schneider static const struct net_device_ops gs_usb_netdev_ops = {
1102d08e973aSMaximilian Schneider 	.ndo_open = gs_can_open,
1103d08e973aSMaximilian Schneider 	.ndo_stop = gs_can_close,
1104d08e973aSMaximilian Schneider 	.ndo_start_xmit = gs_can_start_xmit,
110550212b42SMarc Kleine-Budde 	.ndo_change_mtu = can_change_mtu,
110645dfa45fSJohn Whittington 	.ndo_eth_ioctl = gs_can_eth_ioctl,
1107d08e973aSMaximilian Schneider };
1108d08e973aSMaximilian Schneider 
gs_usb_set_identify(struct net_device * netdev,bool do_identify)110905ca5270SMaximilian Schneider static int gs_usb_set_identify(struct net_device *netdev, bool do_identify)
111005ca5270SMaximilian Schneider {
111105ca5270SMaximilian Schneider 	struct gs_can *dev = netdev_priv(netdev);
11123814ed27SMarc Kleine-Budde 	struct gs_identify_mode imode;
1113b05c73bdSMaksim Salau 
111405ca5270SMaximilian Schneider 	if (do_identify)
11153814ed27SMarc Kleine-Budde 		imode.mode = cpu_to_le32(GS_CAN_IDENTIFY_ON);
111605ca5270SMaximilian Schneider 	else
11173814ed27SMarc Kleine-Budde 		imode.mode = cpu_to_le32(GS_CAN_IDENTIFY_OFF);
111805ca5270SMaximilian Schneider 
111956c56a30SVincent Mailhol 	return usb_control_msg_send(dev->udev, 0, GS_USB_BREQ_IDENTIFY,
1120c1ee7269SMarc Kleine-Budde 				    USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
11213814ed27SMarc Kleine-Budde 				    dev->channel, 0, &imode, sizeof(imode), 100,
11223814ed27SMarc Kleine-Budde 				    GFP_KERNEL);
112305ca5270SMaximilian Schneider }
112405ca5270SMaximilian Schneider 
112505ca5270SMaximilian Schneider /* blink LED's for finding the this interface */
gs_usb_set_phys_id(struct net_device * netdev,enum ethtool_phys_id_state state)11260f2211f1SMarc Kleine-Budde static int gs_usb_set_phys_id(struct net_device *netdev,
112705ca5270SMaximilian Schneider 			      enum ethtool_phys_id_state state)
112805ca5270SMaximilian Schneider {
11290f2211f1SMarc Kleine-Budde 	const struct gs_can *dev = netdev_priv(netdev);
113005ca5270SMaximilian Schneider 	int rc = 0;
113105ca5270SMaximilian Schneider 
11320f2211f1SMarc Kleine-Budde 	if (!(dev->feature & GS_CAN_FEATURE_IDENTIFY))
11330f2211f1SMarc Kleine-Budde 		return -EOPNOTSUPP;
11340f2211f1SMarc Kleine-Budde 
113505ca5270SMaximilian Schneider 	switch (state) {
113605ca5270SMaximilian Schneider 	case ETHTOOL_ID_ACTIVE:
11370f2211f1SMarc Kleine-Budde 		rc = gs_usb_set_identify(netdev, GS_CAN_IDENTIFY_ON);
113805ca5270SMaximilian Schneider 		break;
113905ca5270SMaximilian Schneider 	case ETHTOOL_ID_INACTIVE:
11400f2211f1SMarc Kleine-Budde 		rc = gs_usb_set_identify(netdev, GS_CAN_IDENTIFY_OFF);
114105ca5270SMaximilian Schneider 		break;
114205ca5270SMaximilian Schneider 	default:
114305ca5270SMaximilian Schneider 		break;
114405ca5270SMaximilian Schneider 	}
114505ca5270SMaximilian Schneider 
114605ca5270SMaximilian Schneider 	return rc;
114705ca5270SMaximilian Schneider }
114805ca5270SMaximilian Schneider 
gs_usb_get_ts_info(struct net_device * netdev,struct ethtool_ts_info * info)114945dfa45fSJohn Whittington static int gs_usb_get_ts_info(struct net_device *netdev,
115045dfa45fSJohn Whittington 			      struct ethtool_ts_info *info)
115145dfa45fSJohn Whittington {
115245dfa45fSJohn Whittington 	struct gs_can *dev = netdev_priv(netdev);
115345dfa45fSJohn Whittington 
115445dfa45fSJohn Whittington 	/* report if device supports HW timestamps */
115545dfa45fSJohn Whittington 	if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
115645dfa45fSJohn Whittington 		return can_ethtool_op_get_ts_info_hwts(netdev, info);
115745dfa45fSJohn Whittington 
115845dfa45fSJohn Whittington 	return ethtool_op_get_ts_info(netdev, info);
115945dfa45fSJohn Whittington }
116045dfa45fSJohn Whittington 
116105ca5270SMaximilian Schneider static const struct ethtool_ops gs_usb_ethtool_ops = {
116205ca5270SMaximilian Schneider 	.set_phys_id = gs_usb_set_phys_id,
116345dfa45fSJohn Whittington 	.get_ts_info = gs_usb_get_ts_info,
116405ca5270SMaximilian Schneider };
116505ca5270SMaximilian Schneider 
gs_usb_get_termination(struct net_device * netdev,u16 * term)1166906e0e68SMarc Kleine-Budde static int gs_usb_get_termination(struct net_device *netdev, u16 *term)
1167906e0e68SMarc Kleine-Budde {
1168906e0e68SMarc Kleine-Budde 	struct gs_can *dev = netdev_priv(netdev);
1169906e0e68SMarc Kleine-Budde 	struct gs_device_termination_state term_state;
1170906e0e68SMarc Kleine-Budde 	int rc;
1171906e0e68SMarc Kleine-Budde 
117256c56a30SVincent Mailhol 	rc = usb_control_msg_recv(dev->udev, 0, GS_USB_BREQ_GET_TERMINATION,
1173906e0e68SMarc Kleine-Budde 				  USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1174906e0e68SMarc Kleine-Budde 				  dev->channel, 0,
1175906e0e68SMarc Kleine-Budde 				  &term_state, sizeof(term_state), 1000,
1176906e0e68SMarc Kleine-Budde 				  GFP_KERNEL);
1177906e0e68SMarc Kleine-Budde 	if (rc)
1178906e0e68SMarc Kleine-Budde 		return rc;
1179906e0e68SMarc Kleine-Budde 
1180906e0e68SMarc Kleine-Budde 	if (term_state.state == cpu_to_le32(GS_CAN_TERMINATION_STATE_ON))
1181906e0e68SMarc Kleine-Budde 		*term = GS_USB_TERMINATION_ENABLED;
1182906e0e68SMarc Kleine-Budde 	else
1183906e0e68SMarc Kleine-Budde 		*term = GS_USB_TERMINATION_DISABLED;
1184906e0e68SMarc Kleine-Budde 
1185906e0e68SMarc Kleine-Budde 	return 0;
1186906e0e68SMarc Kleine-Budde }
1187906e0e68SMarc Kleine-Budde 
gs_usb_set_termination(struct net_device * netdev,u16 term)1188906e0e68SMarc Kleine-Budde static int gs_usb_set_termination(struct net_device *netdev, u16 term)
1189906e0e68SMarc Kleine-Budde {
1190906e0e68SMarc Kleine-Budde 	struct gs_can *dev = netdev_priv(netdev);
1191906e0e68SMarc Kleine-Budde 	struct gs_device_termination_state term_state;
1192906e0e68SMarc Kleine-Budde 
1193906e0e68SMarc Kleine-Budde 	if (term == GS_USB_TERMINATION_ENABLED)
1194906e0e68SMarc Kleine-Budde 		term_state.state = cpu_to_le32(GS_CAN_TERMINATION_STATE_ON);
1195906e0e68SMarc Kleine-Budde 	else
1196906e0e68SMarc Kleine-Budde 		term_state.state = cpu_to_le32(GS_CAN_TERMINATION_STATE_OFF);
1197906e0e68SMarc Kleine-Budde 
119856c56a30SVincent Mailhol 	return usb_control_msg_send(dev->udev, 0, GS_USB_BREQ_SET_TERMINATION,
1199906e0e68SMarc Kleine-Budde 				    USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1200906e0e68SMarc Kleine-Budde 				    dev->channel, 0,
1201906e0e68SMarc Kleine-Budde 				    &term_state, sizeof(term_state), 1000,
1202906e0e68SMarc Kleine-Budde 				    GFP_KERNEL);
1203906e0e68SMarc Kleine-Budde }
1204906e0e68SMarc Kleine-Budde 
1205906e0e68SMarc Kleine-Budde static const u16 gs_usb_termination_const[] = {
1206906e0e68SMarc Kleine-Budde 	GS_USB_TERMINATION_DISABLED,
1207906e0e68SMarc Kleine-Budde 	GS_USB_TERMINATION_ENABLED
1208906e0e68SMarc Kleine-Budde };
1209906e0e68SMarc Kleine-Budde 
gs_make_candev(unsigned int channel,struct usb_interface * intf,struct gs_device_config * dconf)121005ca5270SMaximilian Schneider static struct gs_can *gs_make_candev(unsigned int channel,
121105ca5270SMaximilian Schneider 				     struct usb_interface *intf,
121205ca5270SMaximilian Schneider 				     struct gs_device_config *dconf)
1213d08e973aSMaximilian Schneider {
1214d08e973aSMaximilian Schneider 	struct gs_can *dev;
1215d08e973aSMaximilian Schneider 	struct net_device *netdev;
1216d08e973aSMaximilian Schneider 	int rc;
12173814ed27SMarc Kleine-Budde 	struct gs_device_bt_const_extended bt_const_extended;
12183814ed27SMarc Kleine-Budde 	struct gs_device_bt_const bt_const;
12194ba1cb39SMarc Kleine-Budde 	u32 feature;
1220d08e973aSMaximilian Schneider 
1221d08e973aSMaximilian Schneider 	/* fetch bit timing constants */
12223814ed27SMarc Kleine-Budde 	rc = usb_control_msg_recv(interface_to_usbdev(intf), 0,
1223d08e973aSMaximilian Schneider 				  GS_USB_BREQ_BT_CONST,
1224d08e973aSMaximilian Schneider 				  USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
12253814ed27SMarc Kleine-Budde 				  channel, 0, &bt_const, sizeof(bt_const), 1000,
12263814ed27SMarc Kleine-Budde 				  GFP_KERNEL);
1227d08e973aSMaximilian Schneider 
12283814ed27SMarc Kleine-Budde 	if (rc) {
1229d08e973aSMaximilian Schneider 		dev_err(&intf->dev,
123068822f4eSMarc Kleine-Budde 			"Couldn't get bit timing const for channel %d (%pe)\n",
123168822f4eSMarc Kleine-Budde 			channel, ERR_PTR(rc));
1232d08e973aSMaximilian Schneider 		return ERR_PTR(rc);
1233d08e973aSMaximilian Schneider 	}
1234d08e973aSMaximilian Schneider 
1235d08e973aSMaximilian Schneider 	/* create netdev */
1236d08e973aSMaximilian Schneider 	netdev = alloc_candev(sizeof(struct gs_can), GS_MAX_TX_URBS);
1237d08e973aSMaximilian Schneider 	if (!netdev) {
1238d08e973aSMaximilian Schneider 		dev_err(&intf->dev, "Couldn't allocate candev\n");
1239d08e973aSMaximilian Schneider 		return ERR_PTR(-ENOMEM);
1240d08e973aSMaximilian Schneider 	}
1241d08e973aSMaximilian Schneider 
1242d08e973aSMaximilian Schneider 	dev = netdev_priv(netdev);
1243d08e973aSMaximilian Schneider 
1244d08e973aSMaximilian Schneider 	netdev->netdev_ops = &gs_usb_netdev_ops;
1245409c188cSVincent Mailhol 	netdev->ethtool_ops = &gs_usb_ethtool_ops;
1246d08e973aSMaximilian Schneider 
1247d08e973aSMaximilian Schneider 	netdev->flags |= IFF_ECHO; /* we support full roundtrip echo */
1248acff76faSMarc Kleine-Budde 	netdev->dev_id = channel;
1249d08e973aSMaximilian Schneider 
125088bfb9a7SMarc Kleine-Budde 	/* dev setup */
1251c250d5ebSVincent Mailhol 	strcpy(dev->bt_const.name, KBUILD_MODNAME);
12523814ed27SMarc Kleine-Budde 	dev->bt_const.tseg1_min = le32_to_cpu(bt_const.tseg1_min);
12533814ed27SMarc Kleine-Budde 	dev->bt_const.tseg1_max = le32_to_cpu(bt_const.tseg1_max);
12543814ed27SMarc Kleine-Budde 	dev->bt_const.tseg2_min = le32_to_cpu(bt_const.tseg2_min);
12553814ed27SMarc Kleine-Budde 	dev->bt_const.tseg2_max = le32_to_cpu(bt_const.tseg2_max);
12563814ed27SMarc Kleine-Budde 	dev->bt_const.sjw_max = le32_to_cpu(bt_const.sjw_max);
12573814ed27SMarc Kleine-Budde 	dev->bt_const.brp_min = le32_to_cpu(bt_const.brp_min);
12583814ed27SMarc Kleine-Budde 	dev->bt_const.brp_max = le32_to_cpu(bt_const.brp_max);
12593814ed27SMarc Kleine-Budde 	dev->bt_const.brp_inc = le32_to_cpu(bt_const.brp_inc);
1260d08e973aSMaximilian Schneider 
1261d08e973aSMaximilian Schneider 	dev->udev = interface_to_usbdev(intf);
1262d08e973aSMaximilian Schneider 	dev->netdev = netdev;
1263d08e973aSMaximilian Schneider 	dev->channel = channel;
1264d08e973aSMaximilian Schneider 
1265d08e973aSMaximilian Schneider 	init_usb_anchor(&dev->tx_submitted);
1266d08e973aSMaximilian Schneider 	atomic_set(&dev->active_tx_urbs, 0);
1267d08e973aSMaximilian Schneider 	spin_lock_init(&dev->tx_ctx_lock);
1268d08e973aSMaximilian Schneider 	for (rc = 0; rc < GS_MAX_TX_URBS; rc++) {
1269d08e973aSMaximilian Schneider 		dev->tx_context[rc].dev = dev;
1270d08e973aSMaximilian Schneider 		dev->tx_context[rc].echo_id = GS_MAX_TX_URBS;
1271d08e973aSMaximilian Schneider 	}
1272d08e973aSMaximilian Schneider 
127388bfb9a7SMarc Kleine-Budde 	/* can setup */
1274d08e973aSMaximilian Schneider 	dev->can.state = CAN_STATE_STOPPED;
12753814ed27SMarc Kleine-Budde 	dev->can.clock.freq = le32_to_cpu(bt_const.fclk_can);
1276d08e973aSMaximilian Schneider 	dev->can.bittiming_const = &dev->bt_const;
1277d08e973aSMaximilian Schneider 	dev->can.do_set_bittiming = gs_usb_set_bittiming;
1278d08e973aSMaximilian Schneider 
12794c01fc87SOliver Hartkopp 	dev->can.ctrlmode_supported = CAN_CTRLMODE_CC_LEN8_DLC;
1280d08e973aSMaximilian Schneider 
12813814ed27SMarc Kleine-Budde 	feature = le32_to_cpu(bt_const.feature);
1282eb9fa77aSPeter Fink 	dev->feature = FIELD_GET(GS_CAN_FEATURE_MASK, feature);
12834ba1cb39SMarc Kleine-Budde 	if (feature & GS_CAN_FEATURE_LISTEN_ONLY)
1284d08e973aSMaximilian Schneider 		dev->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY;
1285d08e973aSMaximilian Schneider 
12864ba1cb39SMarc Kleine-Budde 	if (feature & GS_CAN_FEATURE_LOOP_BACK)
1287d08e973aSMaximilian Schneider 		dev->can.ctrlmode_supported |= CAN_CTRLMODE_LOOPBACK;
1288d08e973aSMaximilian Schneider 
12894ba1cb39SMarc Kleine-Budde 	if (feature & GS_CAN_FEATURE_TRIPLE_SAMPLE)
1290d08e973aSMaximilian Schneider 		dev->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
1291d08e973aSMaximilian Schneider 
12924ba1cb39SMarc Kleine-Budde 	if (feature & GS_CAN_FEATURE_ONE_SHOT)
1293d08e973aSMaximilian Schneider 		dev->can.ctrlmode_supported |= CAN_CTRLMODE_ONE_SHOT;
1294d08e973aSMaximilian Schneider 
129526949ac9SPeter Fink 	if (feature & GS_CAN_FEATURE_FD) {
129626949ac9SPeter Fink 		dev->can.ctrlmode_supported |= CAN_CTRLMODE_FD;
12976679f4c5SPeter Fink 		/* The data bit timing will be overwritten, if
12986679f4c5SPeter Fink 		 * GS_CAN_FEATURE_BT_CONST_EXT is set.
12996679f4c5SPeter Fink 		 */
130026949ac9SPeter Fink 		dev->can.data_bittiming_const = &dev->bt_const;
130126949ac9SPeter Fink 		dev->can.do_set_data_bittiming = gs_usb_set_data_bittiming;
130226949ac9SPeter Fink 	}
130326949ac9SPeter Fink 
1304906e0e68SMarc Kleine-Budde 	if (feature & GS_CAN_FEATURE_TERMINATION) {
1305906e0e68SMarc Kleine-Budde 		rc = gs_usb_get_termination(netdev, &dev->can.termination);
1306906e0e68SMarc Kleine-Budde 		if (rc) {
1307906e0e68SMarc Kleine-Budde 			dev->feature &= ~GS_CAN_FEATURE_TERMINATION;
1308906e0e68SMarc Kleine-Budde 
1309906e0e68SMarc Kleine-Budde 			dev_info(&intf->dev,
1310906e0e68SMarc Kleine-Budde 				 "Disabling termination support for channel %d (%pe)\n",
1311906e0e68SMarc Kleine-Budde 				 channel, ERR_PTR(rc));
1312906e0e68SMarc Kleine-Budde 		} else {
1313906e0e68SMarc Kleine-Budde 			dev->can.termination_const = gs_usb_termination_const;
1314906e0e68SMarc Kleine-Budde 			dev->can.termination_const_cnt = ARRAY_SIZE(gs_usb_termination_const);
1315906e0e68SMarc Kleine-Budde 			dev->can.do_set_termination = gs_usb_set_termination;
1316906e0e68SMarc Kleine-Budde 		}
1317906e0e68SMarc Kleine-Budde 	}
1318906e0e68SMarc Kleine-Budde 
13192f3cdad1SJeroen Hofstee 	if (feature & GS_CAN_FEATURE_BERR_REPORTING)
13202f3cdad1SJeroen Hofstee 		dev->can.ctrlmode_supported |= CAN_CTRLMODE_BERR_REPORTING;
13212f3cdad1SJeroen Hofstee 
13220c9f92a4SJeroen Hofstee 	if (feature & GS_CAN_FEATURE_GET_STATE)
13230c9f92a4SJeroen Hofstee 		dev->can.do_get_berr_counter = gs_usb_can_get_berr_counter;
13240c9f92a4SJeroen Hofstee 
1325b00ca070SPeter Fink 	/* The CANtact Pro from LinkLayer Labs is based on the
1326b00ca070SPeter Fink 	 * LPC54616 µC, which is affected by the NXP LPC USB transfer
1327b00ca070SPeter Fink 	 * erratum. However, the current firmware (version 2) doesn't
1328b00ca070SPeter Fink 	 * set the GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX bit. Set the
1329b00ca070SPeter Fink 	 * feature GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX to workaround
1330b00ca070SPeter Fink 	 * this issue.
1331b00ca070SPeter Fink 	 *
1332b00ca070SPeter Fink 	 * For the GS_USB_BREQ_DATA_BITTIMING USB control message the
1333b00ca070SPeter Fink 	 * CANtact Pro firmware uses a request value, which is already
1334b00ca070SPeter Fink 	 * used by the candleLight firmware for a different purpose
1335b00ca070SPeter Fink 	 * (GS_USB_BREQ_GET_USER_ID). Set the feature
1336b00ca070SPeter Fink 	 * GS_CAN_FEATURE_QUIRK_BREQ_CANTACT_PRO to workaround this
1337b00ca070SPeter Fink 	 * issue.
1338b00ca070SPeter Fink 	 */
133949c007b9SMarc Kleine-Budde 	if (dev->udev->descriptor.idVendor == cpu_to_le16(USB_GS_USB_1_VENDOR_ID) &&
134049c007b9SMarc Kleine-Budde 	    dev->udev->descriptor.idProduct == cpu_to_le16(USB_GS_USB_1_PRODUCT_ID) &&
1341b00ca070SPeter Fink 	    dev->udev->manufacturer && dev->udev->product &&
1342b00ca070SPeter Fink 	    !strcmp(dev->udev->manufacturer, "LinkLayer Labs") &&
1343b00ca070SPeter Fink 	    !strcmp(dev->udev->product, "CANtact Pro") &&
1344b00ca070SPeter Fink 	    (le32_to_cpu(dconf->sw_version) <= 2))
1345b00ca070SPeter Fink 		dev->feature |= GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX |
1346b00ca070SPeter Fink 			GS_CAN_FEATURE_QUIRK_BREQ_CANTACT_PRO;
1347b00ca070SPeter Fink 
13480f2211f1SMarc Kleine-Budde 	/* GS_CAN_FEATURE_IDENTIFY is only supported for sw_version > 1 */
13490f2211f1SMarc Kleine-Budde 	if (!(le32_to_cpu(dconf->sw_version) > 1 &&
13500f2211f1SMarc Kleine-Budde 	      feature & GS_CAN_FEATURE_IDENTIFY))
13510f2211f1SMarc Kleine-Budde 		dev->feature &= ~GS_CAN_FEATURE_IDENTIFY;
135205ca5270SMaximilian Schneider 
13536679f4c5SPeter Fink 	/* fetch extended bit timing constants if device has feature
13546679f4c5SPeter Fink 	 * GS_CAN_FEATURE_FD and GS_CAN_FEATURE_BT_CONST_EXT
13556679f4c5SPeter Fink 	 */
13566679f4c5SPeter Fink 	if (feature & GS_CAN_FEATURE_FD &&
13576679f4c5SPeter Fink 	    feature & GS_CAN_FEATURE_BT_CONST_EXT) {
13583814ed27SMarc Kleine-Budde 		rc = usb_control_msg_recv(interface_to_usbdev(intf), 0,
13596679f4c5SPeter Fink 					  GS_USB_BREQ_BT_CONST_EXT,
13606679f4c5SPeter Fink 					  USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
13613814ed27SMarc Kleine-Budde 					  channel, 0, &bt_const_extended,
13623814ed27SMarc Kleine-Budde 					  sizeof(bt_const_extended),
13633814ed27SMarc Kleine-Budde 					  1000, GFP_KERNEL);
13643814ed27SMarc Kleine-Budde 		if (rc) {
13656679f4c5SPeter Fink 			dev_err(&intf->dev,
136668822f4eSMarc Kleine-Budde 				"Couldn't get extended bit timing const for channel %d (%pe)\n",
136768822f4eSMarc Kleine-Budde 				channel, ERR_PTR(rc));
136868822f4eSMarc Kleine-Budde 			goto out_free_candev;
13696679f4c5SPeter Fink 		}
13706679f4c5SPeter Fink 
1371c250d5ebSVincent Mailhol 		strcpy(dev->data_bt_const.name, KBUILD_MODNAME);
13723814ed27SMarc Kleine-Budde 		dev->data_bt_const.tseg1_min = le32_to_cpu(bt_const_extended.dtseg1_min);
13733814ed27SMarc Kleine-Budde 		dev->data_bt_const.tseg1_max = le32_to_cpu(bt_const_extended.dtseg1_max);
13743814ed27SMarc Kleine-Budde 		dev->data_bt_const.tseg2_min = le32_to_cpu(bt_const_extended.dtseg2_min);
13753814ed27SMarc Kleine-Budde 		dev->data_bt_const.tseg2_max = le32_to_cpu(bt_const_extended.dtseg2_max);
13763814ed27SMarc Kleine-Budde 		dev->data_bt_const.sjw_max = le32_to_cpu(bt_const_extended.dsjw_max);
13773814ed27SMarc Kleine-Budde 		dev->data_bt_const.brp_min = le32_to_cpu(bt_const_extended.dbrp_min);
13783814ed27SMarc Kleine-Budde 		dev->data_bt_const.brp_max = le32_to_cpu(bt_const_extended.dbrp_max);
13793814ed27SMarc Kleine-Budde 		dev->data_bt_const.brp_inc = le32_to_cpu(bt_const_extended.dbrp_inc);
13806679f4c5SPeter Fink 
13816679f4c5SPeter Fink 		dev->can.data_bittiming_const = &dev->data_bt_const;
13826679f4c5SPeter Fink 	}
13836679f4c5SPeter Fink 
138424bc41b4SMarc Kleine-Budde 	can_rx_offload_add_manual(netdev, &dev->offload, GS_NAPI_WEIGHT);
1385e0d25759SMarc Kleine-Budde 	SET_NETDEV_DEV(netdev, &intf->dev);
1386e0d25759SMarc Kleine-Budde 
1387d08e973aSMaximilian Schneider 	rc = register_candev(dev->netdev);
1388d08e973aSMaximilian Schneider 	if (rc) {
138968822f4eSMarc Kleine-Budde 		dev_err(&intf->dev,
139068822f4eSMarc Kleine-Budde 			"Couldn't register candev for channel %d (%pe)\n",
139168822f4eSMarc Kleine-Budde 			channel, ERR_PTR(rc));
139224bc41b4SMarc Kleine-Budde 		goto out_can_rx_offload_del;
1393d08e973aSMaximilian Schneider 	}
1394d08e973aSMaximilian Schneider 
1395d08e973aSMaximilian Schneider 	return dev;
139668822f4eSMarc Kleine-Budde 
139724bc41b4SMarc Kleine-Budde out_can_rx_offload_del:
139824bc41b4SMarc Kleine-Budde 	can_rx_offload_del(&dev->offload);
139968822f4eSMarc Kleine-Budde out_free_candev:
140068822f4eSMarc Kleine-Budde 	free_candev(dev->netdev);
140168822f4eSMarc Kleine-Budde 	return ERR_PTR(rc);
1402d08e973aSMaximilian Schneider }
1403d08e973aSMaximilian Schneider 
gs_destroy_candev(struct gs_can * dev)1404d08e973aSMaximilian Schneider static void gs_destroy_candev(struct gs_can *dev)
1405d08e973aSMaximilian Schneider {
1406d08e973aSMaximilian Schneider 	unregister_candev(dev->netdev);
140724bc41b4SMarc Kleine-Budde 	can_rx_offload_del(&dev->offload);
1408e9a2d81bSMaximilain Schneider 	free_candev(dev->netdev);
1409d08e973aSMaximilian Schneider }
1410d08e973aSMaximilian Schneider 
gs_usb_probe(struct usb_interface * intf,const struct usb_device_id * id)141105ca5270SMaximilian Schneider static int gs_usb_probe(struct usb_interface *intf,
141205ca5270SMaximilian Schneider 			const struct usb_device_id *id)
1413d08e973aSMaximilian Schneider {
14145374d083SMarc Kleine-Budde 	struct usb_device *udev = interface_to_usbdev(intf);
1415*1cdd3563SAlexander Kozhinov 	struct usb_endpoint_descriptor *ep_in, *ep_out;
1416c359931dSPeter Fink 	struct gs_host_frame *hf;
1417b6980ad3SMarc Kleine-Budde 	struct gs_usb *parent;
14183814ed27SMarc Kleine-Budde 	struct gs_host_config hconf = {
14193814ed27SMarc Kleine-Budde 		.byte_order = cpu_to_le32(0x0000beef),
14203814ed27SMarc Kleine-Budde 	};
14213814ed27SMarc Kleine-Budde 	struct gs_device_config dconf;
1422d08e973aSMaximilian Schneider 	unsigned int icount, i;
14233814ed27SMarc Kleine-Budde 	int rc;
1424d08e973aSMaximilian Schneider 
1425*1cdd3563SAlexander Kozhinov 	rc = usb_find_common_endpoints(intf->cur_altsetting,
1426*1cdd3563SAlexander Kozhinov 				       &ep_in, &ep_out, NULL, NULL);
1427*1cdd3563SAlexander Kozhinov 	if (rc) {
1428*1cdd3563SAlexander Kozhinov 		dev_err(&intf->dev, "Required endpoints not found\n");
1429*1cdd3563SAlexander Kozhinov 		return rc;
1430*1cdd3563SAlexander Kozhinov 	}
1431*1cdd3563SAlexander Kozhinov 
1432d08e973aSMaximilian Schneider 	/* send host config */
14333814ed27SMarc Kleine-Budde 	rc = usb_control_msg_send(udev, 0,
1434d08e973aSMaximilian Schneider 				  GS_USB_BREQ_HOST_FORMAT,
1435d08e973aSMaximilian Schneider 				  USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1436c1ee7269SMarc Kleine-Budde 				  1, intf->cur_altsetting->desc.bInterfaceNumber,
14373814ed27SMarc Kleine-Budde 				  &hconf, sizeof(hconf), 1000,
14383814ed27SMarc Kleine-Budde 				  GFP_KERNEL);
14393814ed27SMarc Kleine-Budde 	if (rc) {
1440d0cd2aa8SMarc Kleine-Budde 		dev_err(&intf->dev, "Couldn't send data format (err=%d)\n", rc);
1441d08e973aSMaximilian Schneider 		return rc;
1442d08e973aSMaximilian Schneider 	}
1443d08e973aSMaximilian Schneider 
1444d08e973aSMaximilian Schneider 	/* read device config */
14453814ed27SMarc Kleine-Budde 	rc = usb_control_msg_recv(udev, 0,
1446d08e973aSMaximilian Schneider 				  GS_USB_BREQ_DEVICE_CONFIG,
1447d08e973aSMaximilian Schneider 				  USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1448c1ee7269SMarc Kleine-Budde 				  1, intf->cur_altsetting->desc.bInterfaceNumber,
14493814ed27SMarc Kleine-Budde 				  &dconf, sizeof(dconf), 1000,
14503814ed27SMarc Kleine-Budde 				  GFP_KERNEL);
14513814ed27SMarc Kleine-Budde 	if (rc) {
1452d08e973aSMaximilian Schneider 		dev_err(&intf->dev, "Couldn't get device config: (err=%d)\n",
1453d08e973aSMaximilian Schneider 			rc);
1454d08e973aSMaximilian Schneider 		return rc;
1455d08e973aSMaximilian Schneider 	}
1456d08e973aSMaximilian Schneider 
14573814ed27SMarc Kleine-Budde 	icount = dconf.icount + 1;
145810819466SMarc Kleine-Budde 	dev_info(&intf->dev, "Configuring for %u interfaces\n", icount);
1459d08e973aSMaximilian Schneider 
1460d08e973aSMaximilian Schneider 	if (icount > GS_MAX_INTF) {
1461d08e973aSMaximilian Schneider 		dev_err(&intf->dev,
146210819466SMarc Kleine-Budde 			"Driver cannot handle more that %u CAN interfaces\n",
1463d08e973aSMaximilian Schneider 			GS_MAX_INTF);
1464d08e973aSMaximilian Schneider 		return -EINVAL;
1465d08e973aSMaximilian Schneider 	}
1466d08e973aSMaximilian Schneider 
1467b6980ad3SMarc Kleine-Budde 	parent = kzalloc(sizeof(*parent), GFP_KERNEL);
1468b6980ad3SMarc Kleine-Budde 	if (!parent)
146937920a74SColin Ian King 		return -ENOMEM;
1470c919a306SEthan Zonca 
1471b6980ad3SMarc Kleine-Budde 	init_usb_anchor(&parent->rx_submitted);
1472d08e973aSMaximilian Schneider 
1473b6980ad3SMarc Kleine-Budde 	usb_set_intfdata(intf, parent);
1474b6980ad3SMarc Kleine-Budde 	parent->udev = udev;
1475d08e973aSMaximilian Schneider 
1476*1cdd3563SAlexander Kozhinov 	/* store the detected endpoints */
1477*1cdd3563SAlexander Kozhinov 	parent->pipe_in = usb_rcvbulkpipe(parent->udev, ep_in->bEndpointAddress);
1478*1cdd3563SAlexander Kozhinov 	parent->pipe_out = usb_sndbulkpipe(parent->udev, ep_out->bEndpointAddress);
1479*1cdd3563SAlexander Kozhinov 
1480d08e973aSMaximilian Schneider 	for (i = 0; i < icount; i++) {
148145dfa45fSJohn Whittington 		unsigned int hf_size_rx = 0;
148245dfa45fSJohn Whittington 
1483b6980ad3SMarc Kleine-Budde 		parent->canch[i] = gs_make_candev(i, intf, &dconf);
1484b6980ad3SMarc Kleine-Budde 		if (IS_ERR_OR_NULL(parent->canch[i])) {
1485e9a2d81bSMaximilain Schneider 			/* save error code to return later */
1486b6980ad3SMarc Kleine-Budde 			rc = PTR_ERR(parent->canch[i]);
1487e9a2d81bSMaximilain Schneider 
1488d08e973aSMaximilian Schneider 			/* on failure destroy previously created candevs */
1489d08e973aSMaximilian Schneider 			icount = i;
1490e9a2d81bSMaximilain Schneider 			for (i = 0; i < icount; i++)
1491b6980ad3SMarc Kleine-Budde 				gs_destroy_candev(parent->canch[i]);
1492e9a2d81bSMaximilain Schneider 
1493b6980ad3SMarc Kleine-Budde 			usb_kill_anchored_urbs(&parent->rx_submitted);
1494b6980ad3SMarc Kleine-Budde 			kfree(parent);
1495d08e973aSMaximilian Schneider 			return rc;
1496d08e973aSMaximilian Schneider 		}
1497b6980ad3SMarc Kleine-Budde 		parent->canch[i]->parent = parent;
149826949ac9SPeter Fink 
149945dfa45fSJohn Whittington 		/* set RX packet size based on FD and if hardware
150045dfa45fSJohn Whittington 		 * timestamps are supported.
150145dfa45fSJohn Whittington 		 */
1502b6980ad3SMarc Kleine-Budde 		if (parent->canch[i]->can.ctrlmode_supported & CAN_CTRLMODE_FD) {
1503b6980ad3SMarc Kleine-Budde 			if (parent->canch[i]->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
150445dfa45fSJohn Whittington 				hf_size_rx = struct_size(hf, canfd_ts, 1);
150545dfa45fSJohn Whittington 			else
150645dfa45fSJohn Whittington 				hf_size_rx = struct_size(hf, canfd, 1);
150745dfa45fSJohn Whittington 		} else {
1508b6980ad3SMarc Kleine-Budde 			if (parent->canch[i]->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
150945dfa45fSJohn Whittington 				hf_size_rx = struct_size(hf, classic_can_ts, 1);
151045dfa45fSJohn Whittington 			else
151145dfa45fSJohn Whittington 				hf_size_rx = struct_size(hf, classic_can, 1);
151245dfa45fSJohn Whittington 		}
1513b6980ad3SMarc Kleine-Budde 		parent->hf_size_rx = max(parent->hf_size_rx, hf_size_rx);
1514d08e973aSMaximilian Schneider 	}
1515d08e973aSMaximilian Schneider 
1516d08e973aSMaximilian Schneider 	return 0;
1517d08e973aSMaximilian Schneider }
1518d08e973aSMaximilian Schneider 
gs_usb_disconnect(struct usb_interface * intf)1519d08e973aSMaximilian Schneider static void gs_usb_disconnect(struct usb_interface *intf)
1520d08e973aSMaximilian Schneider {
1521b6980ad3SMarc Kleine-Budde 	struct gs_usb *parent = usb_get_intfdata(intf);
1522b9d9b030SMarc Kleine-Budde 	unsigned int i;
1523b9d9b030SMarc Kleine-Budde 
1524d08e973aSMaximilian Schneider 	usb_set_intfdata(intf, NULL);
1525d08e973aSMaximilian Schneider 
1526b6980ad3SMarc Kleine-Budde 	if (!parent) {
1527d08e973aSMaximilian Schneider 		dev_err(&intf->dev, "Disconnect (nodata)\n");
1528d08e973aSMaximilian Schneider 		return;
1529d08e973aSMaximilian Schneider 	}
1530d08e973aSMaximilian Schneider 
1531e9a2d81bSMaximilain Schneider 	for (i = 0; i < GS_MAX_INTF; i++)
1532b6980ad3SMarc Kleine-Budde 		if (parent->canch[i])
1533b6980ad3SMarc Kleine-Budde 			gs_destroy_candev(parent->canch[i]);
1534d08e973aSMaximilian Schneider 
1535b6980ad3SMarc Kleine-Budde 	kfree(parent);
1536d08e973aSMaximilian Schneider }
1537d08e973aSMaximilian Schneider 
1538d08e973aSMaximilian Schneider static const struct usb_device_id gs_usb_table[] = {
153949c007b9SMarc Kleine-Budde 	{ USB_DEVICE_INTERFACE_NUMBER(USB_GS_USB_1_VENDOR_ID,
154049c007b9SMarc Kleine-Budde 				      USB_GS_USB_1_PRODUCT_ID, 0) },
1541f155d9c0SMaximilian Schneider 	{ USB_DEVICE_INTERFACE_NUMBER(USB_CANDLELIGHT_VENDOR_ID,
1542f155d9c0SMaximilian Schneider 				      USB_CANDLELIGHT_PRODUCT_ID, 0) },
1543d03bb08eSPeter Fink 	{ USB_DEVICE_INTERFACE_NUMBER(USB_CES_CANEXT_FD_VENDOR_ID,
1544d03bb08eSPeter Fink 				      USB_CES_CANEXT_FD_PRODUCT_ID, 0) },
15450691a4b5SBen Evans 	{ USB_DEVICE_INTERFACE_NUMBER(USB_ABE_CANDEBUGGER_FD_VENDOR_ID,
15460691a4b5SBen Evans 				      USB_ABE_CANDEBUGGER_FD_PRODUCT_ID, 0) },
1547e2282706SMarc Kleine-Budde 	{ USB_DEVICE_INTERFACE_NUMBER(USB_XYLANTA_SAINT3_VENDOR_ID,
1548e2282706SMarc Kleine-Budde 				      USB_XYLANTA_SAINT3_PRODUCT_ID, 0) },
1549d08e973aSMaximilian Schneider 	{} /* Terminating entry */
1550d08e973aSMaximilian Schneider };
1551d08e973aSMaximilian Schneider 
1552d08e973aSMaximilian Schneider MODULE_DEVICE_TABLE(usb, gs_usb_table);
1553d08e973aSMaximilian Schneider 
1554d08e973aSMaximilian Schneider static struct usb_driver gs_usb_driver = {
1555c250d5ebSVincent Mailhol 	.name = KBUILD_MODNAME,
1556d08e973aSMaximilian Schneider 	.probe = gs_usb_probe,
1557d08e973aSMaximilian Schneider 	.disconnect = gs_usb_disconnect,
1558d08e973aSMaximilian Schneider 	.id_table = gs_usb_table,
1559d08e973aSMaximilian Schneider };
1560d08e973aSMaximilian Schneider 
1561d08e973aSMaximilian Schneider module_usb_driver(gs_usb_driver);
1562d08e973aSMaximilian Schneider 
1563d08e973aSMaximilian Schneider MODULE_AUTHOR("Maximilian Schneider <mws@schneidersoft.net>");
1564d08e973aSMaximilian Schneider MODULE_DESCRIPTION(
1565d08e973aSMaximilian Schneider "Socket CAN device driver for Geschwister Schneider Technologie-, "
1566f155d9c0SMaximilian Schneider "Entwicklungs- und Vertriebs UG. USB2.0 to CAN interfaces\n"
1567f155d9c0SMaximilian Schneider "and bytewerk.org candleLight USB CAN interfaces.");
1568d08e973aSMaximilian Schneider MODULE_LICENSE("GPL v2");
1569