xref: /openbmc/linux/drivers/net/can/usb/gs_usb.c (revision eb9fa77a4211cd74d761c46627ac343916fb854e)
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 
12*eb9fa77aSPeter Fink #include <linux/bitfield.h>
13cc69837fSJakub Kicinski #include <linux/ethtool.h>
14d08e973aSMaximilian Schneider #include <linux/init.h>
15d08e973aSMaximilian Schneider #include <linux/module.h>
16d08e973aSMaximilian Schneider #include <linux/netdevice.h>
17f6bb2510SMarc Kleine-Budde #include <linux/signal.h>
18d08e973aSMaximilian Schneider #include <linux/usb.h>
19d08e973aSMaximilian Schneider 
20d08e973aSMaximilian Schneider #include <linux/can.h>
21d08e973aSMaximilian Schneider #include <linux/can/dev.h>
22d08e973aSMaximilian Schneider #include <linux/can/error.h>
23d08e973aSMaximilian Schneider 
24d08e973aSMaximilian Schneider /* Device specific constants */
25d08e973aSMaximilian Schneider #define USB_GSUSB_1_VENDOR_ID 0x1d50
26d08e973aSMaximilian Schneider #define USB_GSUSB_1_PRODUCT_ID 0x606f
27d08e973aSMaximilian Schneider 
28f155d9c0SMaximilian Schneider #define USB_CANDLELIGHT_VENDOR_ID 0x1209
29f155d9c0SMaximilian Schneider #define USB_CANDLELIGHT_PRODUCT_ID 0x2323
30f155d9c0SMaximilian Schneider 
31d08e973aSMaximilian Schneider #define GSUSB_ENDPOINT_IN 1
32d08e973aSMaximilian Schneider #define GSUSB_ENDPOINT_OUT 2
33d08e973aSMaximilian Schneider 
34d08e973aSMaximilian Schneider /* Device specific constants */
35d08e973aSMaximilian Schneider enum gs_usb_breq {
36d08e973aSMaximilian Schneider 	GS_USB_BREQ_HOST_FORMAT = 0,
37d08e973aSMaximilian Schneider 	GS_USB_BREQ_BITTIMING,
38d08e973aSMaximilian Schneider 	GS_USB_BREQ_MODE,
39d08e973aSMaximilian Schneider 	GS_USB_BREQ_BERR,
40d08e973aSMaximilian Schneider 	GS_USB_BREQ_BT_CONST,
4105ca5270SMaximilian Schneider 	GS_USB_BREQ_DEVICE_CONFIG,
4205ca5270SMaximilian Schneider 	GS_USB_BREQ_TIMESTAMP,
4305ca5270SMaximilian Schneider 	GS_USB_BREQ_IDENTIFY,
44e0902cadSMarc Kleine-Budde 	GS_USB_BREQ_GET_USER_ID,
45e0902cadSMarc Kleine-Budde 	GS_USB_BREQ_SET_USER_ID,
4626949ac9SPeter Fink 	GS_USB_BREQ_DATA_BITTIMING,
47d08e973aSMaximilian Schneider };
48d08e973aSMaximilian Schneider 
49d08e973aSMaximilian Schneider enum gs_can_mode {
50d08e973aSMaximilian Schneider 	/* reset a channel. turns it off */
51d08e973aSMaximilian Schneider 	GS_CAN_MODE_RESET = 0,
52d08e973aSMaximilian Schneider 	/* starts a channel */
53d08e973aSMaximilian Schneider 	GS_CAN_MODE_START
54d08e973aSMaximilian Schneider };
55d08e973aSMaximilian Schneider 
56d08e973aSMaximilian Schneider enum gs_can_state {
57d08e973aSMaximilian Schneider 	GS_CAN_STATE_ERROR_ACTIVE = 0,
58d08e973aSMaximilian Schneider 	GS_CAN_STATE_ERROR_WARNING,
59d08e973aSMaximilian Schneider 	GS_CAN_STATE_ERROR_PASSIVE,
60d08e973aSMaximilian Schneider 	GS_CAN_STATE_BUS_OFF,
61d08e973aSMaximilian Schneider 	GS_CAN_STATE_STOPPED,
62d08e973aSMaximilian Schneider 	GS_CAN_STATE_SLEEPING
63d08e973aSMaximilian Schneider };
64d08e973aSMaximilian Schneider 
6505ca5270SMaximilian Schneider enum gs_can_identify_mode {
6605ca5270SMaximilian Schneider 	GS_CAN_IDENTIFY_OFF = 0,
6705ca5270SMaximilian Schneider 	GS_CAN_IDENTIFY_ON
6805ca5270SMaximilian Schneider };
6905ca5270SMaximilian Schneider 
70d08e973aSMaximilian Schneider /* data types passed between host and device */
714ba1cb39SMarc Kleine-Budde 
724ba1cb39SMarc Kleine-Budde /* The firmware on the original USB2CAN by Geschwister Schneider
734ba1cb39SMarc Kleine-Budde  * Technologie Entwicklungs- und Vertriebs UG exchanges all data
744ba1cb39SMarc Kleine-Budde  * between the host and the device in host byte order. This is done
754ba1cb39SMarc Kleine-Budde  * with the struct gs_host_config::byte_order member, which is sent
764ba1cb39SMarc Kleine-Budde  * first to indicate the desired byte order.
774ba1cb39SMarc Kleine-Budde  *
784ba1cb39SMarc Kleine-Budde  * The widely used open source firmware candleLight doesn't support
794ba1cb39SMarc Kleine-Budde  * this feature and exchanges the data in little endian byte order.
80d08e973aSMaximilian Schneider  */
814ba1cb39SMarc Kleine-Budde struct gs_host_config {
824ba1cb39SMarc Kleine-Budde 	__le32 byte_order;
834ba1cb39SMarc Kleine-Budde } __packed;
84d08e973aSMaximilian Schneider 
85d08e973aSMaximilian Schneider struct gs_device_config {
86d08e973aSMaximilian Schneider 	u8 reserved1;
87d08e973aSMaximilian Schneider 	u8 reserved2;
88d08e973aSMaximilian Schneider 	u8 reserved3;
89d08e973aSMaximilian Schneider 	u8 icount;
904ba1cb39SMarc Kleine-Budde 	__le32 sw_version;
914ba1cb39SMarc Kleine-Budde 	__le32 hw_version;
92d08e973aSMaximilian Schneider } __packed;
93d08e973aSMaximilian Schneider 
94d08e973aSMaximilian Schneider #define GS_CAN_MODE_NORMAL 0
959be95c91SMaximilian Schneider #define GS_CAN_MODE_LISTEN_ONLY BIT(0)
969be95c91SMaximilian Schneider #define GS_CAN_MODE_LOOP_BACK BIT(1)
979be95c91SMaximilian Schneider #define GS_CAN_MODE_TRIPLE_SAMPLE BIT(2)
989be95c91SMaximilian Schneider #define GS_CAN_MODE_ONE_SHOT BIT(3)
99d42d2111SMarc Kleine-Budde #define GS_CAN_MODE_HW_TIMESTAMP BIT(4)
10015564f82SMarc Kleine-Budde /* GS_CAN_FEATURE_IDENTIFY BIT(5) */
101e0902cadSMarc Kleine-Budde /* GS_CAN_FEATURE_USER_ID BIT(6) */
1024643e34eSMarc Kleine-Budde #define GS_CAN_MODE_PAD_PKTS_TO_MAX_PKT_SIZE BIT(7)
10326949ac9SPeter Fink #define GS_CAN_MODE_FD BIT(8)
104*eb9fa77aSPeter Fink /* GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX BIT(9) */
105d08e973aSMaximilian Schneider 
106d08e973aSMaximilian Schneider struct gs_device_mode {
1074ba1cb39SMarc Kleine-Budde 	__le32 mode;
1084ba1cb39SMarc Kleine-Budde 	__le32 flags;
109d08e973aSMaximilian Schneider } __packed;
110d08e973aSMaximilian Schneider 
111d08e973aSMaximilian Schneider struct gs_device_state {
1124ba1cb39SMarc Kleine-Budde 	__le32 state;
1134ba1cb39SMarc Kleine-Budde 	__le32 rxerr;
1144ba1cb39SMarc Kleine-Budde 	__le32 txerr;
115d08e973aSMaximilian Schneider } __packed;
116d08e973aSMaximilian Schneider 
117d08e973aSMaximilian Schneider struct gs_device_bittiming {
1184ba1cb39SMarc Kleine-Budde 	__le32 prop_seg;
1194ba1cb39SMarc Kleine-Budde 	__le32 phase_seg1;
1204ba1cb39SMarc Kleine-Budde 	__le32 phase_seg2;
1214ba1cb39SMarc Kleine-Budde 	__le32 sjw;
1224ba1cb39SMarc Kleine-Budde 	__le32 brp;
123d08e973aSMaximilian Schneider } __packed;
124d08e973aSMaximilian Schneider 
12505ca5270SMaximilian Schneider struct gs_identify_mode {
1264ba1cb39SMarc Kleine-Budde 	__le32 mode;
12705ca5270SMaximilian Schneider } __packed;
12805ca5270SMaximilian Schneider 
1299be95c91SMaximilian Schneider #define GS_CAN_FEATURE_LISTEN_ONLY BIT(0)
1309be95c91SMaximilian Schneider #define GS_CAN_FEATURE_LOOP_BACK BIT(1)
1319be95c91SMaximilian Schneider #define GS_CAN_FEATURE_TRIPLE_SAMPLE BIT(2)
1329be95c91SMaximilian Schneider #define GS_CAN_FEATURE_ONE_SHOT BIT(3)
13305ca5270SMaximilian Schneider #define GS_CAN_FEATURE_HW_TIMESTAMP BIT(4)
13405ca5270SMaximilian Schneider #define GS_CAN_FEATURE_IDENTIFY BIT(5)
135e0902cadSMarc Kleine-Budde #define GS_CAN_FEATURE_USER_ID BIT(6)
1364643e34eSMarc Kleine-Budde #define GS_CAN_FEATURE_PAD_PKTS_TO_MAX_PKT_SIZE BIT(7)
13726949ac9SPeter Fink #define GS_CAN_FEATURE_FD BIT(8)
138*eb9fa77aSPeter Fink #define GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX BIT(9)
139*eb9fa77aSPeter Fink #define GS_CAN_FEATURE_MASK GENMASK(9, 0)
140d08e973aSMaximilian Schneider 
141d08e973aSMaximilian Schneider struct gs_device_bt_const {
1424ba1cb39SMarc Kleine-Budde 	__le32 feature;
1434ba1cb39SMarc Kleine-Budde 	__le32 fclk_can;
1444ba1cb39SMarc Kleine-Budde 	__le32 tseg1_min;
1454ba1cb39SMarc Kleine-Budde 	__le32 tseg1_max;
1464ba1cb39SMarc Kleine-Budde 	__le32 tseg2_min;
1474ba1cb39SMarc Kleine-Budde 	__le32 tseg2_max;
1484ba1cb39SMarc Kleine-Budde 	__le32 sjw_max;
1494ba1cb39SMarc Kleine-Budde 	__le32 brp_min;
1504ba1cb39SMarc Kleine-Budde 	__le32 brp_max;
1514ba1cb39SMarc Kleine-Budde 	__le32 brp_inc;
152d08e973aSMaximilian Schneider } __packed;
153d08e973aSMaximilian Schneider 
1544b8f03e3SMarc Kleine-Budde #define GS_CAN_FLAG_OVERFLOW BIT(0)
15526949ac9SPeter Fink #define GS_CAN_FLAG_FD BIT(1)
15626949ac9SPeter Fink #define GS_CAN_FLAG_BRS BIT(2)
15726949ac9SPeter Fink #define GS_CAN_FLAG_ESI BIT(3)
158d08e973aSMaximilian Schneider 
159c359931dSPeter Fink struct classic_can {
160c359931dSPeter Fink 	u8 data[8];
161c359931dSPeter Fink } __packed;
162c359931dSPeter Fink 
163*eb9fa77aSPeter Fink struct classic_can_quirk {
164*eb9fa77aSPeter Fink 	u8 data[8];
165*eb9fa77aSPeter Fink 	u8 quirk;
166*eb9fa77aSPeter Fink } __packed;
167*eb9fa77aSPeter Fink 
16826949ac9SPeter Fink struct canfd {
16926949ac9SPeter Fink 	u8 data[64];
17026949ac9SPeter Fink } __packed;
17126949ac9SPeter Fink 
172*eb9fa77aSPeter Fink struct canfd_quirk {
173*eb9fa77aSPeter Fink 	u8 data[64];
174*eb9fa77aSPeter Fink 	u8 quirk;
175*eb9fa77aSPeter Fink } __packed;
176*eb9fa77aSPeter Fink 
177d08e973aSMaximilian Schneider struct gs_host_frame {
178d08e973aSMaximilian Schneider 	u32 echo_id;
1794ba1cb39SMarc Kleine-Budde 	__le32 can_id;
180d08e973aSMaximilian Schneider 
181d08e973aSMaximilian Schneider 	u8 can_dlc;
182d08e973aSMaximilian Schneider 	u8 channel;
183d08e973aSMaximilian Schneider 	u8 flags;
184d08e973aSMaximilian Schneider 	u8 reserved;
185d08e973aSMaximilian Schneider 
186c359931dSPeter Fink 	union {
187c359931dSPeter Fink 		DECLARE_FLEX_ARRAY(struct classic_can, classic_can);
188*eb9fa77aSPeter Fink 		DECLARE_FLEX_ARRAY(struct classic_can_quirk, classic_can_quirk);
18926949ac9SPeter Fink 		DECLARE_FLEX_ARRAY(struct canfd, canfd);
190*eb9fa77aSPeter Fink 		DECLARE_FLEX_ARRAY(struct canfd_quirk, canfd_quirk);
191c359931dSPeter Fink 	};
192d08e973aSMaximilian Schneider } __packed;
193d08e973aSMaximilian Schneider /* The GS USB devices make use of the same flags and masks as in
194d08e973aSMaximilian Schneider  * linux/can.h and linux/can/error.h, and no additional mapping is necessary.
195d08e973aSMaximilian Schneider  */
196d08e973aSMaximilian Schneider 
197d08e973aSMaximilian Schneider /* Only send a max of GS_MAX_TX_URBS frames per channel at a time. */
198d08e973aSMaximilian Schneider #define GS_MAX_TX_URBS 10
199d08e973aSMaximilian Schneider /* Only launch a max of GS_MAX_RX_URBS usb requests at a time. */
200d08e973aSMaximilian Schneider #define GS_MAX_RX_URBS 30
201d08e973aSMaximilian Schneider /* Maximum number of interfaces the driver supports per device.
202e10ab8b3SMarc Kleine-Budde  * Current hardware only supports 3 interfaces. The future may vary.
203d08e973aSMaximilian Schneider  */
204e10ab8b3SMarc Kleine-Budde #define GS_MAX_INTF 3
205d08e973aSMaximilian Schneider 
206d08e973aSMaximilian Schneider struct gs_tx_context {
207d08e973aSMaximilian Schneider 	struct gs_can *dev;
208d08e973aSMaximilian Schneider 	unsigned int echo_id;
209d08e973aSMaximilian Schneider };
210d08e973aSMaximilian Schneider 
211d08e973aSMaximilian Schneider struct gs_can {
212d08e973aSMaximilian Schneider 	struct can_priv can; /* must be the first member */
213d08e973aSMaximilian Schneider 
214d08e973aSMaximilian Schneider 	struct gs_usb *parent;
215d08e973aSMaximilian Schneider 
216d08e973aSMaximilian Schneider 	struct net_device *netdev;
217d08e973aSMaximilian Schneider 	struct usb_device *udev;
218d08e973aSMaximilian Schneider 	struct usb_interface *iface;
219d08e973aSMaximilian Schneider 
220d08e973aSMaximilian Schneider 	struct can_bittiming_const bt_const;
221d08e973aSMaximilian Schneider 	unsigned int channel;	/* channel number */
222d08e973aSMaximilian Schneider 
223*eb9fa77aSPeter Fink 	u32 feature;
224c359931dSPeter Fink 	unsigned int hf_size_tx;
225c359931dSPeter Fink 
226ae421e33SNik Nyby 	/* This lock prevents a race condition between xmit and receive. */
227d08e973aSMaximilian Schneider 	spinlock_t tx_ctx_lock;
228d08e973aSMaximilian Schneider 	struct gs_tx_context tx_context[GS_MAX_TX_URBS];
229d08e973aSMaximilian Schneider 
230d08e973aSMaximilian Schneider 	struct usb_anchor tx_submitted;
231d08e973aSMaximilian Schneider 	atomic_t active_tx_urbs;
232d08e973aSMaximilian Schneider };
233d08e973aSMaximilian Schneider 
234d08e973aSMaximilian Schneider /* usb interface struct */
235d08e973aSMaximilian Schneider struct gs_usb {
236d08e973aSMaximilian Schneider 	struct gs_can *canch[GS_MAX_INTF];
237d08e973aSMaximilian Schneider 	struct usb_anchor rx_submitted;
238d08e973aSMaximilian Schneider 	struct usb_device *udev;
239c359931dSPeter Fink 	unsigned int hf_size_rx;
240035b0fcfSVincent Mailhol 	u8 active_channels;
241d08e973aSMaximilian Schneider };
242d08e973aSMaximilian Schneider 
243d08e973aSMaximilian Schneider /* 'allocate' a tx context.
244d08e973aSMaximilian Schneider  * returns a valid tx context or NULL if there is no space.
245d08e973aSMaximilian Schneider  */
246d08e973aSMaximilian Schneider static struct gs_tx_context *gs_alloc_tx_context(struct gs_can *dev)
247d08e973aSMaximilian Schneider {
248d08e973aSMaximilian Schneider 	int i = 0;
249d08e973aSMaximilian Schneider 	unsigned long flags;
250d08e973aSMaximilian Schneider 
251d08e973aSMaximilian Schneider 	spin_lock_irqsave(&dev->tx_ctx_lock, flags);
252d08e973aSMaximilian Schneider 
253d08e973aSMaximilian Schneider 	for (; i < GS_MAX_TX_URBS; i++) {
254d08e973aSMaximilian Schneider 		if (dev->tx_context[i].echo_id == GS_MAX_TX_URBS) {
255d08e973aSMaximilian Schneider 			dev->tx_context[i].echo_id = i;
256d08e973aSMaximilian Schneider 			spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
257d08e973aSMaximilian Schneider 			return &dev->tx_context[i];
258d08e973aSMaximilian Schneider 		}
259d08e973aSMaximilian Schneider 	}
260d08e973aSMaximilian Schneider 
261d08e973aSMaximilian Schneider 	spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
262d08e973aSMaximilian Schneider 	return NULL;
263d08e973aSMaximilian Schneider }
264d08e973aSMaximilian Schneider 
265d08e973aSMaximilian Schneider /* releases a tx context
266d08e973aSMaximilian Schneider  */
267d08e973aSMaximilian Schneider static void gs_free_tx_context(struct gs_tx_context *txc)
268d08e973aSMaximilian Schneider {
269d08e973aSMaximilian Schneider 	txc->echo_id = GS_MAX_TX_URBS;
270d08e973aSMaximilian Schneider }
271d08e973aSMaximilian Schneider 
272d08e973aSMaximilian Schneider /* Get a tx context by id.
273d08e973aSMaximilian Schneider  */
2749be95c91SMaximilian Schneider static struct gs_tx_context *gs_get_tx_context(struct gs_can *dev,
2759be95c91SMaximilian Schneider 					       unsigned int id)
276d08e973aSMaximilian Schneider {
277d08e973aSMaximilian Schneider 	unsigned long flags;
278d08e973aSMaximilian Schneider 
279d08e973aSMaximilian Schneider 	if (id < GS_MAX_TX_URBS) {
280d08e973aSMaximilian Schneider 		spin_lock_irqsave(&dev->tx_ctx_lock, flags);
281d08e973aSMaximilian Schneider 		if (dev->tx_context[id].echo_id == id) {
282d08e973aSMaximilian Schneider 			spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
283d08e973aSMaximilian Schneider 			return &dev->tx_context[id];
284d08e973aSMaximilian Schneider 		}
285d08e973aSMaximilian Schneider 		spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
286d08e973aSMaximilian Schneider 	}
287d08e973aSMaximilian Schneider 	return NULL;
288d08e973aSMaximilian Schneider }
289d08e973aSMaximilian Schneider 
290dda8deb6SMarc Kleine-Budde static int gs_cmd_reset(struct gs_can *gsdev)
291d08e973aSMaximilian Schneider {
292d08e973aSMaximilian Schneider 	struct gs_device_mode *dm;
293d08e973aSMaximilian Schneider 	struct usb_interface *intf = gsdev->iface;
294d08e973aSMaximilian Schneider 	int rc;
295d08e973aSMaximilian Schneider 
296d08e973aSMaximilian Schneider 	dm = kzalloc(sizeof(*dm), GFP_KERNEL);
297d08e973aSMaximilian Schneider 	if (!dm)
298d08e973aSMaximilian Schneider 		return -ENOMEM;
299d08e973aSMaximilian Schneider 
300d08e973aSMaximilian Schneider 	dm->mode = GS_CAN_MODE_RESET;
301d08e973aSMaximilian Schneider 
302d08e973aSMaximilian Schneider 	rc = usb_control_msg(interface_to_usbdev(intf),
303d08e973aSMaximilian Schneider 			     usb_sndctrlpipe(interface_to_usbdev(intf), 0),
304d08e973aSMaximilian Schneider 			     GS_USB_BREQ_MODE,
305d08e973aSMaximilian Schneider 			     USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
306c1ee7269SMarc Kleine-Budde 			     gsdev->channel, 0, dm, sizeof(*dm), 1000);
307d08e973aSMaximilian Schneider 
3085cda3ee5SMarc Kleine-Budde 	kfree(dm);
3095cda3ee5SMarc Kleine-Budde 
310d08e973aSMaximilian Schneider 	return rc;
311d08e973aSMaximilian Schneider }
312d08e973aSMaximilian Schneider 
313d08e973aSMaximilian Schneider static void gs_update_state(struct gs_can *dev, struct can_frame *cf)
314d08e973aSMaximilian Schneider {
315d08e973aSMaximilian Schneider 	struct can_device_stats *can_stats = &dev->can.can_stats;
316d08e973aSMaximilian Schneider 
317d08e973aSMaximilian Schneider 	if (cf->can_id & CAN_ERR_RESTARTED) {
318d08e973aSMaximilian Schneider 		dev->can.state = CAN_STATE_ERROR_ACTIVE;
319d08e973aSMaximilian Schneider 		can_stats->restarts++;
320d08e973aSMaximilian Schneider 	} else if (cf->can_id & CAN_ERR_BUSOFF) {
321d08e973aSMaximilian Schneider 		dev->can.state = CAN_STATE_BUS_OFF;
322d08e973aSMaximilian Schneider 		can_stats->bus_off++;
323d08e973aSMaximilian Schneider 	} else if (cf->can_id & CAN_ERR_CRTL) {
324d08e973aSMaximilian Schneider 		if ((cf->data[1] & CAN_ERR_CRTL_TX_WARNING) ||
325d08e973aSMaximilian Schneider 		    (cf->data[1] & CAN_ERR_CRTL_RX_WARNING)) {
326d08e973aSMaximilian Schneider 			dev->can.state = CAN_STATE_ERROR_WARNING;
327d08e973aSMaximilian Schneider 			can_stats->error_warning++;
328d08e973aSMaximilian Schneider 		} else if ((cf->data[1] & CAN_ERR_CRTL_TX_PASSIVE) ||
329d08e973aSMaximilian Schneider 			   (cf->data[1] & CAN_ERR_CRTL_RX_PASSIVE)) {
330d08e973aSMaximilian Schneider 			dev->can.state = CAN_STATE_ERROR_PASSIVE;
331d08e973aSMaximilian Schneider 			can_stats->error_passive++;
332d08e973aSMaximilian Schneider 		} else {
333d08e973aSMaximilian Schneider 			dev->can.state = CAN_STATE_ERROR_ACTIVE;
334d08e973aSMaximilian Schneider 		}
335d08e973aSMaximilian Schneider 	}
336d08e973aSMaximilian Schneider }
337d08e973aSMaximilian Schneider 
338ae421e33SNik Nyby static void gs_usb_receive_bulk_callback(struct urb *urb)
339d08e973aSMaximilian Schneider {
340d08e973aSMaximilian Schneider 	struct gs_usb *usbcan = urb->context;
341d08e973aSMaximilian Schneider 	struct gs_can *dev;
342d08e973aSMaximilian Schneider 	struct net_device *netdev;
343d08e973aSMaximilian Schneider 	int rc;
344d08e973aSMaximilian Schneider 	struct net_device_stats *stats;
345d08e973aSMaximilian Schneider 	struct gs_host_frame *hf = urb->transfer_buffer;
346d08e973aSMaximilian Schneider 	struct gs_tx_context *txc;
347d08e973aSMaximilian Schneider 	struct can_frame *cf;
34826949ac9SPeter Fink 	struct canfd_frame *cfd;
349d08e973aSMaximilian Schneider 	struct sk_buff *skb;
350d08e973aSMaximilian Schneider 
351d08e973aSMaximilian Schneider 	BUG_ON(!usbcan);
352d08e973aSMaximilian Schneider 
353d08e973aSMaximilian Schneider 	switch (urb->status) {
354d08e973aSMaximilian Schneider 	case 0: /* success */
355d08e973aSMaximilian Schneider 		break;
356d08e973aSMaximilian Schneider 	case -ENOENT:
357d08e973aSMaximilian Schneider 	case -ESHUTDOWN:
358d08e973aSMaximilian Schneider 		return;
359d08e973aSMaximilian Schneider 	default:
360d08e973aSMaximilian Schneider 		/* do not resubmit aborted urbs. eg: when device goes down */
361d08e973aSMaximilian Schneider 		return;
362d08e973aSMaximilian Schneider 	}
363d08e973aSMaximilian Schneider 
364d08e973aSMaximilian Schneider 	/* device reports out of range channel id */
365d08e973aSMaximilian Schneider 	if (hf->channel >= GS_MAX_INTF)
3664a8737ffSMarc Kleine-Budde 		goto device_detach;
367d08e973aSMaximilian Schneider 
368d08e973aSMaximilian Schneider 	dev = usbcan->canch[hf->channel];
369d08e973aSMaximilian Schneider 
370d08e973aSMaximilian Schneider 	netdev = dev->netdev;
371d08e973aSMaximilian Schneider 	stats = &netdev->stats;
372d08e973aSMaximilian Schneider 
373d08e973aSMaximilian Schneider 	if (!netif_device_present(netdev))
374d08e973aSMaximilian Schneider 		return;
375d08e973aSMaximilian Schneider 
376d08e973aSMaximilian Schneider 	if (hf->echo_id == -1) { /* normal rx */
37726949ac9SPeter Fink 		if (hf->flags & GS_CAN_FLAG_FD) {
37826949ac9SPeter Fink 			skb = alloc_canfd_skb(dev->netdev, &cfd);
37926949ac9SPeter Fink 			if (!skb)
38026949ac9SPeter Fink 				return;
38126949ac9SPeter Fink 
38226949ac9SPeter Fink 			cfd->can_id = le32_to_cpu(hf->can_id);
38326949ac9SPeter Fink 			cfd->len = can_fd_dlc2len(hf->can_dlc);
38426949ac9SPeter Fink 			if (hf->flags & GS_CAN_FLAG_BRS)
38526949ac9SPeter Fink 				cfd->flags |= CANFD_BRS;
38626949ac9SPeter Fink 			if (hf->flags & GS_CAN_FLAG_ESI)
38726949ac9SPeter Fink 				cfd->flags |= CANFD_ESI;
38826949ac9SPeter Fink 
38926949ac9SPeter Fink 			memcpy(cfd->data, hf->canfd->data, cfd->len);
39026949ac9SPeter Fink 		} else {
391d08e973aSMaximilian Schneider 			skb = alloc_can_skb(dev->netdev, &cf);
392d08e973aSMaximilian Schneider 			if (!skb)
393d08e973aSMaximilian Schneider 				return;
394d08e973aSMaximilian Schneider 
3954ba1cb39SMarc Kleine-Budde 			cf->can_id = le32_to_cpu(hf->can_id);
3964c01fc87SOliver Hartkopp 			can_frame_set_cc_len(cf, hf->can_dlc, dev->can.ctrlmode);
39726949ac9SPeter Fink 
398c359931dSPeter Fink 			memcpy(cf->data, hf->classic_can->data, 8);
399d08e973aSMaximilian Schneider 
400d08e973aSMaximilian Schneider 			/* ERROR frames tell us information about the controller */
4014ba1cb39SMarc Kleine-Budde 			if (le32_to_cpu(hf->can_id) & CAN_ERR_FLAG)
402d08e973aSMaximilian Schneider 				gs_update_state(dev, cf);
40326949ac9SPeter Fink 		}
404d08e973aSMaximilian Schneider 
405d08e973aSMaximilian Schneider 		netdev->stats.rx_packets++;
406d08e973aSMaximilian Schneider 		netdev->stats.rx_bytes += hf->can_dlc;
407d08e973aSMaximilian Schneider 
408d08e973aSMaximilian Schneider 		netif_rx(skb);
409d08e973aSMaximilian Schneider 	} else { /* echo_id == hf->echo_id */
410d08e973aSMaximilian Schneider 		if (hf->echo_id >= GS_MAX_TX_URBS) {
411d08e973aSMaximilian Schneider 			netdev_err(netdev,
41210819466SMarc Kleine-Budde 				   "Unexpected out of range echo id %u\n",
413d08e973aSMaximilian Schneider 				   hf->echo_id);
414d08e973aSMaximilian Schneider 			goto resubmit_urb;
415d08e973aSMaximilian Schneider 		}
416d08e973aSMaximilian Schneider 
417d08e973aSMaximilian Schneider 		txc = gs_get_tx_context(dev, hf->echo_id);
418d08e973aSMaximilian Schneider 
419d08e973aSMaximilian Schneider 		/* bad devices send bad echo_ids. */
420d08e973aSMaximilian Schneider 		if (!txc) {
421d08e973aSMaximilian Schneider 			netdev_err(netdev,
42210819466SMarc Kleine-Budde 				   "Unexpected unused echo id %u\n",
423d08e973aSMaximilian Schneider 				   hf->echo_id);
424d08e973aSMaximilian Schneider 			goto resubmit_urb;
425d08e973aSMaximilian Schneider 		}
426d08e973aSMaximilian Schneider 
427cc4b08c3SVincent Mailhol 		netdev->stats.tx_packets++;
428cc4b08c3SVincent Mailhol 		netdev->stats.tx_bytes += can_get_echo_skb(netdev, hf->echo_id,
429cc4b08c3SVincent Mailhol 							   NULL);
430d08e973aSMaximilian Schneider 
431d08e973aSMaximilian Schneider 		gs_free_tx_context(txc);
432d08e973aSMaximilian Schneider 
43397819f94SWolfgang Grandegger 		atomic_dec(&dev->active_tx_urbs);
43497819f94SWolfgang Grandegger 
435d08e973aSMaximilian Schneider 		netif_wake_queue(netdev);
436d08e973aSMaximilian Schneider 	}
437d08e973aSMaximilian Schneider 
438d08e973aSMaximilian Schneider 	if (hf->flags & GS_CAN_FLAG_OVERFLOW) {
439d08e973aSMaximilian Schneider 		skb = alloc_can_err_skb(netdev, &cf);
440d08e973aSMaximilian Schneider 		if (!skb)
441d08e973aSMaximilian Schneider 			goto resubmit_urb;
442d08e973aSMaximilian Schneider 
443d08e973aSMaximilian Schneider 		cf->can_id |= CAN_ERR_CRTL;
444c7b74967SOliver Hartkopp 		cf->len = CAN_ERR_DLC;
445d08e973aSMaximilian Schneider 		cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
446d08e973aSMaximilian Schneider 		stats->rx_over_errors++;
447d08e973aSMaximilian Schneider 		stats->rx_errors++;
448d08e973aSMaximilian Schneider 		netif_rx(skb);
449d08e973aSMaximilian Schneider 	}
450d08e973aSMaximilian Schneider 
451d08e973aSMaximilian Schneider  resubmit_urb:
452c1ee7269SMarc Kleine-Budde 	usb_fill_bulk_urb(urb, usbcan->udev,
453d08e973aSMaximilian Schneider 			  usb_rcvbulkpipe(usbcan->udev, GSUSB_ENDPOINT_IN),
454c359931dSPeter Fink 			  hf, dev->parent->hf_size_rx,
455c1ee7269SMarc Kleine-Budde 			  gs_usb_receive_bulk_callback, usbcan);
456d08e973aSMaximilian Schneider 
457d08e973aSMaximilian Schneider 	rc = usb_submit_urb(urb, GFP_ATOMIC);
458d08e973aSMaximilian Schneider 
459d08e973aSMaximilian Schneider 	/* USB failure take down all interfaces */
460d08e973aSMaximilian Schneider 	if (rc == -ENODEV) {
4614a8737ffSMarc Kleine-Budde  device_detach:
462d08e973aSMaximilian Schneider 		for (rc = 0; rc < GS_MAX_INTF; rc++) {
463d08e973aSMaximilian Schneider 			if (usbcan->canch[rc])
464d08e973aSMaximilian Schneider 				netif_device_detach(usbcan->canch[rc]->netdev);
465d08e973aSMaximilian Schneider 		}
466d08e973aSMaximilian Schneider 	}
467d08e973aSMaximilian Schneider }
468d08e973aSMaximilian Schneider 
469d08e973aSMaximilian Schneider static int gs_usb_set_bittiming(struct net_device *netdev)
470d08e973aSMaximilian Schneider {
471d08e973aSMaximilian Schneider 	struct gs_can *dev = netdev_priv(netdev);
472d08e973aSMaximilian Schneider 	struct can_bittiming *bt = &dev->can.bittiming;
473d08e973aSMaximilian Schneider 	struct usb_interface *intf = dev->iface;
474d08e973aSMaximilian Schneider 	int rc;
475d08e973aSMaximilian Schneider 	struct gs_device_bittiming *dbt;
476d08e973aSMaximilian Schneider 
477d08e973aSMaximilian Schneider 	dbt = kmalloc(sizeof(*dbt), GFP_KERNEL);
478d08e973aSMaximilian Schneider 	if (!dbt)
479d08e973aSMaximilian Schneider 		return -ENOMEM;
480d08e973aSMaximilian Schneider 
4814ba1cb39SMarc Kleine-Budde 	dbt->prop_seg = cpu_to_le32(bt->prop_seg);
4824ba1cb39SMarc Kleine-Budde 	dbt->phase_seg1 = cpu_to_le32(bt->phase_seg1);
4834ba1cb39SMarc Kleine-Budde 	dbt->phase_seg2 = cpu_to_le32(bt->phase_seg2);
4844ba1cb39SMarc Kleine-Budde 	dbt->sjw = cpu_to_le32(bt->sjw);
4854ba1cb39SMarc Kleine-Budde 	dbt->brp = cpu_to_le32(bt->brp);
486d08e973aSMaximilian Schneider 
487d08e973aSMaximilian Schneider 	/* request bit timings */
488d08e973aSMaximilian Schneider 	rc = usb_control_msg(interface_to_usbdev(intf),
489d08e973aSMaximilian Schneider 			     usb_sndctrlpipe(interface_to_usbdev(intf), 0),
490d08e973aSMaximilian Schneider 			     GS_USB_BREQ_BITTIMING,
491d08e973aSMaximilian Schneider 			     USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
492c1ee7269SMarc Kleine-Budde 			     dev->channel, 0, dbt, sizeof(*dbt), 1000);
493d08e973aSMaximilian Schneider 
494d08e973aSMaximilian Schneider 	kfree(dbt);
495d08e973aSMaximilian Schneider 
496d08e973aSMaximilian Schneider 	if (rc < 0)
497d08e973aSMaximilian Schneider 		dev_err(netdev->dev.parent, "Couldn't set bittimings (err=%d)",
498d08e973aSMaximilian Schneider 			rc);
499d08e973aSMaximilian Schneider 
500d5b42e66SWolfgang Grandegger 	return (rc > 0) ? 0 : rc;
501d08e973aSMaximilian Schneider }
502d08e973aSMaximilian Schneider 
50326949ac9SPeter Fink static int gs_usb_set_data_bittiming(struct net_device *netdev)
50426949ac9SPeter Fink {
50526949ac9SPeter Fink 	struct gs_can *dev = netdev_priv(netdev);
50626949ac9SPeter Fink 	struct can_bittiming *bt = &dev->can.data_bittiming;
50726949ac9SPeter Fink 	struct usb_interface *intf = dev->iface;
50826949ac9SPeter Fink 	struct gs_device_bittiming *dbt;
50926949ac9SPeter Fink 	int rc;
51026949ac9SPeter Fink 
51126949ac9SPeter Fink 	dbt = kmalloc(sizeof(*dbt), GFP_KERNEL);
51226949ac9SPeter Fink 	if (!dbt)
51326949ac9SPeter Fink 		return -ENOMEM;
51426949ac9SPeter Fink 
51526949ac9SPeter Fink 	dbt->prop_seg = cpu_to_le32(bt->prop_seg);
51626949ac9SPeter Fink 	dbt->phase_seg1 = cpu_to_le32(bt->phase_seg1);
51726949ac9SPeter Fink 	dbt->phase_seg2 = cpu_to_le32(bt->phase_seg2);
51826949ac9SPeter Fink 	dbt->sjw = cpu_to_le32(bt->sjw);
51926949ac9SPeter Fink 	dbt->brp = cpu_to_le32(bt->brp);
52026949ac9SPeter Fink 
52126949ac9SPeter Fink 	/* request bit timings */
52226949ac9SPeter Fink 	rc = usb_control_msg(interface_to_usbdev(intf),
52326949ac9SPeter Fink 			     usb_sndctrlpipe(interface_to_usbdev(intf), 0),
52426949ac9SPeter Fink 			     GS_USB_BREQ_DATA_BITTIMING,
52526949ac9SPeter Fink 			     USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
52626949ac9SPeter Fink 			     dev->channel, 0, dbt, sizeof(*dbt), 1000);
52726949ac9SPeter Fink 
52826949ac9SPeter Fink 	kfree(dbt);
52926949ac9SPeter Fink 
53026949ac9SPeter Fink 	if (rc < 0)
53126949ac9SPeter Fink 		dev_err(netdev->dev.parent,
53226949ac9SPeter Fink 			"Couldn't set data bittimings (err=%d)", rc);
53326949ac9SPeter Fink 
53426949ac9SPeter Fink 	return (rc > 0) ? 0 : rc;
53526949ac9SPeter Fink }
53626949ac9SPeter Fink 
537d08e973aSMaximilian Schneider static void gs_usb_xmit_callback(struct urb *urb)
538d08e973aSMaximilian Schneider {
539d08e973aSMaximilian Schneider 	struct gs_tx_context *txc = urb->context;
540d08e973aSMaximilian Schneider 	struct gs_can *dev = txc->dev;
541d08e973aSMaximilian Schneider 	struct net_device *netdev = dev->netdev;
542d08e973aSMaximilian Schneider 
543d08e973aSMaximilian Schneider 	if (urb->status)
54410819466SMarc Kleine-Budde 		netdev_info(netdev, "usb xmit fail %u\n", txc->echo_id);
545d08e973aSMaximilian Schneider 
546c1ee7269SMarc Kleine-Budde 	usb_free_coherent(urb->dev, urb->transfer_buffer_length,
547c1ee7269SMarc Kleine-Budde 			  urb->transfer_buffer, urb->transfer_dma);
548d08e973aSMaximilian Schneider }
549d08e973aSMaximilian Schneider 
5509be95c91SMaximilian Schneider static netdev_tx_t gs_can_start_xmit(struct sk_buff *skb,
5519be95c91SMaximilian Schneider 				     struct net_device *netdev)
552d08e973aSMaximilian Schneider {
553d08e973aSMaximilian Schneider 	struct gs_can *dev = netdev_priv(netdev);
554d08e973aSMaximilian Schneider 	struct net_device_stats *stats = &dev->netdev->stats;
555d08e973aSMaximilian Schneider 	struct urb *urb;
556d08e973aSMaximilian Schneider 	struct gs_host_frame *hf;
557d08e973aSMaximilian Schneider 	struct can_frame *cf;
55826949ac9SPeter Fink 	struct canfd_frame *cfd;
559d08e973aSMaximilian Schneider 	int rc;
560d08e973aSMaximilian Schneider 	unsigned int idx;
561d08e973aSMaximilian Schneider 	struct gs_tx_context *txc;
562d08e973aSMaximilian Schneider 
563d08e973aSMaximilian Schneider 	if (can_dropped_invalid_skb(netdev, skb))
564d08e973aSMaximilian Schneider 		return NETDEV_TX_OK;
565d08e973aSMaximilian Schneider 
566d08e973aSMaximilian Schneider 	/* find an empty context to keep track of transmission */
567d08e973aSMaximilian Schneider 	txc = gs_alloc_tx_context(dev);
568d08e973aSMaximilian Schneider 	if (!txc)
569d08e973aSMaximilian Schneider 		return NETDEV_TX_BUSY;
570d08e973aSMaximilian Schneider 
571d08e973aSMaximilian Schneider 	/* create a URB, and a buffer for it */
572d08e973aSMaximilian Schneider 	urb = usb_alloc_urb(0, GFP_ATOMIC);
5732a4a1e40SWolfram Sang 	if (!urb)
574d08e973aSMaximilian Schneider 		goto nomem_urb;
575d08e973aSMaximilian Schneider 
576c359931dSPeter Fink 	hf = usb_alloc_coherent(dev->udev, dev->hf_size_tx, GFP_ATOMIC,
577d08e973aSMaximilian Schneider 				&urb->transfer_dma);
578d08e973aSMaximilian Schneider 	if (!hf) {
579d08e973aSMaximilian Schneider 		netdev_err(netdev, "No memory left for USB buffer\n");
580d08e973aSMaximilian Schneider 		goto nomem_hf;
581d08e973aSMaximilian Schneider 	}
582d08e973aSMaximilian Schneider 
583d08e973aSMaximilian Schneider 	idx = txc->echo_id;
584d08e973aSMaximilian Schneider 
585d08e973aSMaximilian Schneider 	if (idx >= GS_MAX_TX_URBS) {
58610819466SMarc Kleine-Budde 		netdev_err(netdev, "Invalid tx context %u\n", idx);
587d08e973aSMaximilian Schneider 		goto badidx;
588d08e973aSMaximilian Schneider 	}
589d08e973aSMaximilian Schneider 
590d08e973aSMaximilian Schneider 	hf->echo_id = idx;
591d08e973aSMaximilian Schneider 	hf->channel = dev->channel;
59289d58aebSBrian Silverman 	hf->flags = 0;
59389d58aebSBrian Silverman 	hf->reserved = 0;
594d08e973aSMaximilian Schneider 
59526949ac9SPeter Fink 	if (can_is_canfd_skb(skb)) {
59626949ac9SPeter Fink 		cfd = (struct canfd_frame *)skb->data;
59726949ac9SPeter Fink 
59826949ac9SPeter Fink 		hf->can_id = cpu_to_le32(cfd->can_id);
59926949ac9SPeter Fink 		hf->can_dlc = can_fd_len2dlc(cfd->len);
60026949ac9SPeter Fink 		hf->flags |= GS_CAN_FLAG_FD;
60126949ac9SPeter Fink 		if (cfd->flags & CANFD_BRS)
60226949ac9SPeter Fink 			hf->flags |= GS_CAN_FLAG_BRS;
60326949ac9SPeter Fink 		if (cfd->flags & CANFD_ESI)
60426949ac9SPeter Fink 			hf->flags |= GS_CAN_FLAG_ESI;
60526949ac9SPeter Fink 
60626949ac9SPeter Fink 		memcpy(hf->canfd->data, cfd->data, cfd->len);
60726949ac9SPeter Fink 	} else {
608d08e973aSMaximilian Schneider 		cf = (struct can_frame *)skb->data;
609d08e973aSMaximilian Schneider 
6104ba1cb39SMarc Kleine-Budde 		hf->can_id = cpu_to_le32(cf->can_id);
6114c01fc87SOliver Hartkopp 		hf->can_dlc = can_get_cc_dlc(cf, dev->can.ctrlmode);
6124c01fc87SOliver Hartkopp 
613c359931dSPeter Fink 		memcpy(hf->classic_can->data, cf->data, cf->len);
61426949ac9SPeter Fink 	}
615d08e973aSMaximilian Schneider 
616d08e973aSMaximilian Schneider 	usb_fill_bulk_urb(urb, dev->udev,
617d08e973aSMaximilian Schneider 			  usb_sndbulkpipe(dev->udev, GSUSB_ENDPOINT_OUT),
618c359931dSPeter Fink 			  hf, dev->hf_size_tx,
619c1ee7269SMarc Kleine-Budde 			  gs_usb_xmit_callback, txc);
620d08e973aSMaximilian Schneider 
621d08e973aSMaximilian Schneider 	urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
622d08e973aSMaximilian Schneider 	usb_anchor_urb(urb, &dev->tx_submitted);
623d08e973aSMaximilian Schneider 
6241dcb6e57SVincent Mailhol 	can_put_echo_skb(skb, netdev, idx, 0);
625d08e973aSMaximilian Schneider 
626d08e973aSMaximilian Schneider 	atomic_inc(&dev->active_tx_urbs);
627d08e973aSMaximilian Schneider 
628d08e973aSMaximilian Schneider 	rc = usb_submit_urb(urb, GFP_ATOMIC);
629d08e973aSMaximilian Schneider 	if (unlikely(rc)) {			/* usb send failed */
630d08e973aSMaximilian Schneider 		atomic_dec(&dev->active_tx_urbs);
631d08e973aSMaximilian Schneider 
632f318482aSMarc Kleine-Budde 		can_free_echo_skb(netdev, idx, NULL);
633d08e973aSMaximilian Schneider 		gs_free_tx_context(txc);
634d08e973aSMaximilian Schneider 
635d08e973aSMaximilian Schneider 		usb_unanchor_urb(urb);
636c359931dSPeter Fink 		usb_free_coherent(dev->udev, urb->transfer_buffer_length,
637c359931dSPeter Fink 				  urb->transfer_buffer, urb->transfer_dma);
638d08e973aSMaximilian Schneider 
639d08e973aSMaximilian Schneider 		if (rc == -ENODEV) {
640d08e973aSMaximilian Schneider 			netif_device_detach(netdev);
641d08e973aSMaximilian Schneider 		} else {
642d08e973aSMaximilian Schneider 			netdev_err(netdev, "usb_submit failed (err=%d)\n", rc);
643d08e973aSMaximilian Schneider 			stats->tx_dropped++;
644d08e973aSMaximilian Schneider 		}
645d08e973aSMaximilian Schneider 	} else {
646d08e973aSMaximilian Schneider 		/* Slow down tx path */
647d08e973aSMaximilian Schneider 		if (atomic_read(&dev->active_tx_urbs) >= GS_MAX_TX_URBS)
648d08e973aSMaximilian Schneider 			netif_stop_queue(netdev);
649d08e973aSMaximilian Schneider 	}
650d08e973aSMaximilian Schneider 
651d08e973aSMaximilian Schneider 	/* let usb core take care of this urb */
652d08e973aSMaximilian Schneider 	usb_free_urb(urb);
653d08e973aSMaximilian Schneider 
654d08e973aSMaximilian Schneider 	return NETDEV_TX_OK;
655d08e973aSMaximilian Schneider 
656d08e973aSMaximilian Schneider  badidx:
657c359931dSPeter Fink 	usb_free_coherent(dev->udev, urb->transfer_buffer_length,
658c359931dSPeter Fink 			  urb->transfer_buffer, urb->transfer_dma);
659d08e973aSMaximilian Schneider  nomem_hf:
660d08e973aSMaximilian Schneider 	usb_free_urb(urb);
661d08e973aSMaximilian Schneider 
662d08e973aSMaximilian Schneider  nomem_urb:
663d08e973aSMaximilian Schneider 	gs_free_tx_context(txc);
664d08e973aSMaximilian Schneider 	dev_kfree_skb(skb);
665d08e973aSMaximilian Schneider 	stats->tx_dropped++;
666d08e973aSMaximilian Schneider 	return NETDEV_TX_OK;
667d08e973aSMaximilian Schneider }
668d08e973aSMaximilian Schneider 
669d08e973aSMaximilian Schneider static int gs_can_open(struct net_device *netdev)
670d08e973aSMaximilian Schneider {
671d08e973aSMaximilian Schneider 	struct gs_can *dev = netdev_priv(netdev);
672d08e973aSMaximilian Schneider 	struct gs_usb *parent = dev->parent;
673d08e973aSMaximilian Schneider 	int rc, i;
674d08e973aSMaximilian Schneider 	struct gs_device_mode *dm;
675c359931dSPeter Fink 	struct gs_host_frame *hf;
676d08e973aSMaximilian Schneider 	u32 ctrlmode;
6774ba1cb39SMarc Kleine-Budde 	u32 flags = 0;
678d08e973aSMaximilian Schneider 
679d08e973aSMaximilian Schneider 	rc = open_candev(netdev);
680d08e973aSMaximilian Schneider 	if (rc)
681d08e973aSMaximilian Schneider 		return rc;
682d08e973aSMaximilian Schneider 
68326949ac9SPeter Fink 	ctrlmode = dev->can.ctrlmode;
68426949ac9SPeter Fink 	if (ctrlmode & CAN_CTRLMODE_FD) {
68526949ac9SPeter Fink 		flags |= GS_CAN_MODE_FD;
686*eb9fa77aSPeter Fink 
687*eb9fa77aSPeter Fink 		if (dev->feature & GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX)
688*eb9fa77aSPeter Fink 			dev->hf_size_tx = struct_size(hf, canfd_quirk, 1);
689*eb9fa77aSPeter Fink 		else
69026949ac9SPeter Fink 			dev->hf_size_tx = struct_size(hf, canfd, 1);
69126949ac9SPeter Fink 	} else {
692*eb9fa77aSPeter Fink 		if (dev->feature & GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX)
693*eb9fa77aSPeter Fink 			dev->hf_size_tx = struct_size(hf, classic_can_quirk, 1);
694*eb9fa77aSPeter Fink 		else
695c359931dSPeter Fink 			dev->hf_size_tx = struct_size(hf, classic_can, 1);
69626949ac9SPeter Fink 	}
697c359931dSPeter Fink 
698035b0fcfSVincent Mailhol 	if (!parent->active_channels) {
699d08e973aSMaximilian Schneider 		for (i = 0; i < GS_MAX_RX_URBS; i++) {
700d08e973aSMaximilian Schneider 			struct urb *urb;
701d08e973aSMaximilian Schneider 			u8 *buf;
702d08e973aSMaximilian Schneider 
703d08e973aSMaximilian Schneider 			/* alloc rx urb */
704d08e973aSMaximilian Schneider 			urb = usb_alloc_urb(0, GFP_KERNEL);
7052a4a1e40SWolfram Sang 			if (!urb)
706d08e973aSMaximilian Schneider 				return -ENOMEM;
707d08e973aSMaximilian Schneider 
708d08e973aSMaximilian Schneider 			/* alloc rx buffer */
709d08e973aSMaximilian Schneider 			buf = usb_alloc_coherent(dev->udev,
710c359931dSPeter Fink 						 dev->parent->hf_size_rx,
711d08e973aSMaximilian Schneider 						 GFP_KERNEL,
712d08e973aSMaximilian Schneider 						 &urb->transfer_dma);
713d08e973aSMaximilian Schneider 			if (!buf) {
714d08e973aSMaximilian Schneider 				netdev_err(netdev,
715d08e973aSMaximilian Schneider 					   "No memory left for USB buffer\n");
716d08e973aSMaximilian Schneider 				usb_free_urb(urb);
717d08e973aSMaximilian Schneider 				return -ENOMEM;
718d08e973aSMaximilian Schneider 			}
719d08e973aSMaximilian Schneider 
720d08e973aSMaximilian Schneider 			/* fill, anchor, and submit rx urb */
721d08e973aSMaximilian Schneider 			usb_fill_bulk_urb(urb,
722d08e973aSMaximilian Schneider 					  dev->udev,
723d08e973aSMaximilian Schneider 					  usb_rcvbulkpipe(dev->udev,
724d08e973aSMaximilian Schneider 							  GSUSB_ENDPOINT_IN),
725d08e973aSMaximilian Schneider 					  buf,
726c359931dSPeter Fink 					  dev->parent->hf_size_rx,
727c1ee7269SMarc Kleine-Budde 					  gs_usb_receive_bulk_callback, parent);
728d08e973aSMaximilian Schneider 			urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
729d08e973aSMaximilian Schneider 
730d08e973aSMaximilian Schneider 			usb_anchor_urb(urb, &parent->rx_submitted);
731d08e973aSMaximilian Schneider 
732d08e973aSMaximilian Schneider 			rc = usb_submit_urb(urb, GFP_KERNEL);
733d08e973aSMaximilian Schneider 			if (rc) {
734d08e973aSMaximilian Schneider 				if (rc == -ENODEV)
735d08e973aSMaximilian Schneider 					netif_device_detach(dev->netdev);
736d08e973aSMaximilian Schneider 
737d08e973aSMaximilian Schneider 				netdev_err(netdev,
738d0cd2aa8SMarc Kleine-Budde 					   "usb_submit failed (err=%d)\n", rc);
739d08e973aSMaximilian Schneider 
740d08e973aSMaximilian Schneider 				usb_unanchor_urb(urb);
741fb5be6a7SNavid Emamdoost 				usb_free_urb(urb);
742d08e973aSMaximilian Schneider 				break;
743d08e973aSMaximilian Schneider 			}
744d08e973aSMaximilian Schneider 
745d08e973aSMaximilian Schneider 			/* Drop reference,
746d08e973aSMaximilian Schneider 			 * USB core will take care of freeing it
747d08e973aSMaximilian Schneider 			 */
748d08e973aSMaximilian Schneider 			usb_free_urb(urb);
749d08e973aSMaximilian Schneider 		}
750d08e973aSMaximilian Schneider 	}
751d08e973aSMaximilian Schneider 
752d08e973aSMaximilian Schneider 	dm = kmalloc(sizeof(*dm), GFP_KERNEL);
753d08e973aSMaximilian Schneider 	if (!dm)
754d08e973aSMaximilian Schneider 		return -ENOMEM;
755d08e973aSMaximilian Schneider 
756d08e973aSMaximilian Schneider 	/* flags */
757d08e973aSMaximilian Schneider 	if (ctrlmode & CAN_CTRLMODE_LOOPBACK)
7584ba1cb39SMarc Kleine-Budde 		flags |= GS_CAN_MODE_LOOP_BACK;
759d08e973aSMaximilian Schneider 	else if (ctrlmode & CAN_CTRLMODE_LISTENONLY)
7604ba1cb39SMarc Kleine-Budde 		flags |= GS_CAN_MODE_LISTEN_ONLY;
761d08e973aSMaximilian Schneider 
762d08e973aSMaximilian Schneider 	/* Controller is not allowed to retry TX
763d08e973aSMaximilian Schneider 	 * this mode is unavailable on atmels uc3c hardware
764d08e973aSMaximilian Schneider 	 */
765d08e973aSMaximilian Schneider 	if (ctrlmode & CAN_CTRLMODE_ONE_SHOT)
7664ba1cb39SMarc Kleine-Budde 		flags |= GS_CAN_MODE_ONE_SHOT;
767d08e973aSMaximilian Schneider 
768d08e973aSMaximilian Schneider 	if (ctrlmode & CAN_CTRLMODE_3_SAMPLES)
7694ba1cb39SMarc Kleine-Budde 		flags |= GS_CAN_MODE_TRIPLE_SAMPLE;
770d08e973aSMaximilian Schneider 
771d08e973aSMaximilian Schneider 	/* finally start device */
7724ba1cb39SMarc Kleine-Budde 	dm->mode = cpu_to_le32(GS_CAN_MODE_START);
7734ba1cb39SMarc Kleine-Budde 	dm->flags = cpu_to_le32(flags);
774d08e973aSMaximilian Schneider 	rc = usb_control_msg(interface_to_usbdev(dev->iface),
775d08e973aSMaximilian Schneider 			     usb_sndctrlpipe(interface_to_usbdev(dev->iface), 0),
776d08e973aSMaximilian Schneider 			     GS_USB_BREQ_MODE,
777c1ee7269SMarc Kleine-Budde 			     USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
778c1ee7269SMarc Kleine-Budde 			     dev->channel, 0, dm, sizeof(*dm), 1000);
779d08e973aSMaximilian Schneider 
780d08e973aSMaximilian Schneider 	if (rc < 0) {
781d08e973aSMaximilian Schneider 		netdev_err(netdev, "Couldn't start device (err=%d)\n", rc);
782d08e973aSMaximilian Schneider 		kfree(dm);
783d08e973aSMaximilian Schneider 		return rc;
784d08e973aSMaximilian Schneider 	}
785d08e973aSMaximilian Schneider 
786d08e973aSMaximilian Schneider 	kfree(dm);
787d08e973aSMaximilian Schneider 
788d08e973aSMaximilian Schneider 	dev->can.state = CAN_STATE_ERROR_ACTIVE;
789d08e973aSMaximilian Schneider 
790035b0fcfSVincent Mailhol 	parent->active_channels++;
791d08e973aSMaximilian Schneider 	if (!(dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY))
792d08e973aSMaximilian Schneider 		netif_start_queue(netdev);
793d08e973aSMaximilian Schneider 
794d08e973aSMaximilian Schneider 	return 0;
795d08e973aSMaximilian Schneider }
796d08e973aSMaximilian Schneider 
797d08e973aSMaximilian Schneider static int gs_can_close(struct net_device *netdev)
798d08e973aSMaximilian Schneider {
799d08e973aSMaximilian Schneider 	int rc;
800d08e973aSMaximilian Schneider 	struct gs_can *dev = netdev_priv(netdev);
801d08e973aSMaximilian Schneider 	struct gs_usb *parent = dev->parent;
802d08e973aSMaximilian Schneider 
803d08e973aSMaximilian Schneider 	netif_stop_queue(netdev);
804d08e973aSMaximilian Schneider 
805d08e973aSMaximilian Schneider 	/* Stop polling */
806035b0fcfSVincent Mailhol 	parent->active_channels--;
807035b0fcfSVincent Mailhol 	if (!parent->active_channels)
808d08e973aSMaximilian Schneider 		usb_kill_anchored_urbs(&parent->rx_submitted);
809d08e973aSMaximilian Schneider 
810d08e973aSMaximilian Schneider 	/* Stop sending URBs */
811d08e973aSMaximilian Schneider 	usb_kill_anchored_urbs(&dev->tx_submitted);
812d08e973aSMaximilian Schneider 	atomic_set(&dev->active_tx_urbs, 0);
813d08e973aSMaximilian Schneider 
814d08e973aSMaximilian Schneider 	/* reset the device */
815dda8deb6SMarc Kleine-Budde 	rc = gs_cmd_reset(dev);
816d08e973aSMaximilian Schneider 	if (rc < 0)
817d08e973aSMaximilian Schneider 		netdev_warn(netdev, "Couldn't shutdown device (err=%d)", rc);
818d08e973aSMaximilian Schneider 
819d08e973aSMaximilian Schneider 	/* reset tx contexts */
820d08e973aSMaximilian Schneider 	for (rc = 0; rc < GS_MAX_TX_URBS; rc++) {
821d08e973aSMaximilian Schneider 		dev->tx_context[rc].dev = dev;
822d08e973aSMaximilian Schneider 		dev->tx_context[rc].echo_id = GS_MAX_TX_URBS;
823d08e973aSMaximilian Schneider 	}
824d08e973aSMaximilian Schneider 
825d08e973aSMaximilian Schneider 	/* close the netdev */
826d08e973aSMaximilian Schneider 	close_candev(netdev);
827d08e973aSMaximilian Schneider 
828d08e973aSMaximilian Schneider 	return 0;
829d08e973aSMaximilian Schneider }
830d08e973aSMaximilian Schneider 
831d08e973aSMaximilian Schneider static const struct net_device_ops gs_usb_netdev_ops = {
832d08e973aSMaximilian Schneider 	.ndo_open = gs_can_open,
833d08e973aSMaximilian Schneider 	.ndo_stop = gs_can_close,
834d08e973aSMaximilian Schneider 	.ndo_start_xmit = gs_can_start_xmit,
83550212b42SMarc Kleine-Budde 	.ndo_change_mtu = can_change_mtu,
836d08e973aSMaximilian Schneider };
837d08e973aSMaximilian Schneider 
83805ca5270SMaximilian Schneider static int gs_usb_set_identify(struct net_device *netdev, bool do_identify)
83905ca5270SMaximilian Schneider {
84005ca5270SMaximilian Schneider 	struct gs_can *dev = netdev_priv(netdev);
841b05c73bdSMaksim Salau 	struct gs_identify_mode *imode;
84205ca5270SMaximilian Schneider 	int rc;
84305ca5270SMaximilian Schneider 
844b05c73bdSMaksim Salau 	imode = kmalloc(sizeof(*imode), GFP_KERNEL);
845b05c73bdSMaksim Salau 
846b05c73bdSMaksim Salau 	if (!imode)
847b05c73bdSMaksim Salau 		return -ENOMEM;
848b05c73bdSMaksim Salau 
84905ca5270SMaximilian Schneider 	if (do_identify)
8504ba1cb39SMarc Kleine-Budde 		imode->mode = cpu_to_le32(GS_CAN_IDENTIFY_ON);
85105ca5270SMaximilian Schneider 	else
8524ba1cb39SMarc Kleine-Budde 		imode->mode = cpu_to_le32(GS_CAN_IDENTIFY_OFF);
85305ca5270SMaximilian Schneider 
85405ca5270SMaximilian Schneider 	rc = usb_control_msg(interface_to_usbdev(dev->iface),
855c1ee7269SMarc Kleine-Budde 			     usb_sndctrlpipe(interface_to_usbdev(dev->iface), 0),
85605ca5270SMaximilian Schneider 			     GS_USB_BREQ_IDENTIFY,
857c1ee7269SMarc Kleine-Budde 			     USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
858c1ee7269SMarc Kleine-Budde 			     dev->channel, 0, imode, sizeof(*imode), 100);
85905ca5270SMaximilian Schneider 
860b05c73bdSMaksim Salau 	kfree(imode);
861b05c73bdSMaksim Salau 
86205ca5270SMaximilian Schneider 	return (rc > 0) ? 0 : rc;
86305ca5270SMaximilian Schneider }
86405ca5270SMaximilian Schneider 
86505ca5270SMaximilian Schneider /* blink LED's for finding the this interface */
86605ca5270SMaximilian Schneider static int gs_usb_set_phys_id(struct net_device *dev,
86705ca5270SMaximilian Schneider 			      enum ethtool_phys_id_state state)
86805ca5270SMaximilian Schneider {
86905ca5270SMaximilian Schneider 	int rc = 0;
87005ca5270SMaximilian Schneider 
87105ca5270SMaximilian Schneider 	switch (state) {
87205ca5270SMaximilian Schneider 	case ETHTOOL_ID_ACTIVE:
87305ca5270SMaximilian Schneider 		rc = gs_usb_set_identify(dev, GS_CAN_IDENTIFY_ON);
87405ca5270SMaximilian Schneider 		break;
87505ca5270SMaximilian Schneider 	case ETHTOOL_ID_INACTIVE:
87605ca5270SMaximilian Schneider 		rc = gs_usb_set_identify(dev, GS_CAN_IDENTIFY_OFF);
87705ca5270SMaximilian Schneider 		break;
87805ca5270SMaximilian Schneider 	default:
87905ca5270SMaximilian Schneider 		break;
88005ca5270SMaximilian Schneider 	}
88105ca5270SMaximilian Schneider 
88205ca5270SMaximilian Schneider 	return rc;
88305ca5270SMaximilian Schneider }
88405ca5270SMaximilian Schneider 
88505ca5270SMaximilian Schneider static const struct ethtool_ops gs_usb_ethtool_ops = {
88605ca5270SMaximilian Schneider 	.set_phys_id = gs_usb_set_phys_id,
88705ca5270SMaximilian Schneider };
88805ca5270SMaximilian Schneider 
88905ca5270SMaximilian Schneider static struct gs_can *gs_make_candev(unsigned int channel,
89005ca5270SMaximilian Schneider 				     struct usb_interface *intf,
89105ca5270SMaximilian Schneider 				     struct gs_device_config *dconf)
892d08e973aSMaximilian Schneider {
893d08e973aSMaximilian Schneider 	struct gs_can *dev;
894d08e973aSMaximilian Schneider 	struct net_device *netdev;
895d08e973aSMaximilian Schneider 	int rc;
896d08e973aSMaximilian Schneider 	struct gs_device_bt_const *bt_const;
8974ba1cb39SMarc Kleine-Budde 	u32 feature;
898d08e973aSMaximilian Schneider 
899d08e973aSMaximilian Schneider 	bt_const = kmalloc(sizeof(*bt_const), GFP_KERNEL);
900d08e973aSMaximilian Schneider 	if (!bt_const)
901d08e973aSMaximilian Schneider 		return ERR_PTR(-ENOMEM);
902d08e973aSMaximilian Schneider 
903d08e973aSMaximilian Schneider 	/* fetch bit timing constants */
904d08e973aSMaximilian Schneider 	rc = usb_control_msg(interface_to_usbdev(intf),
905d08e973aSMaximilian Schneider 			     usb_rcvctrlpipe(interface_to_usbdev(intf), 0),
906d08e973aSMaximilian Schneider 			     GS_USB_BREQ_BT_CONST,
907d08e973aSMaximilian Schneider 			     USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
908c1ee7269SMarc Kleine-Budde 			     channel, 0, bt_const, sizeof(*bt_const), 1000);
909d08e973aSMaximilian Schneider 
910d08e973aSMaximilian Schneider 	if (rc < 0) {
911d08e973aSMaximilian Schneider 		dev_err(&intf->dev,
912d08e973aSMaximilian Schneider 			"Couldn't get bit timing const for channel (err=%d)\n",
913d08e973aSMaximilian Schneider 			rc);
914d08e973aSMaximilian Schneider 		kfree(bt_const);
915d08e973aSMaximilian Schneider 		return ERR_PTR(rc);
916d08e973aSMaximilian Schneider 	}
917d08e973aSMaximilian Schneider 
918d08e973aSMaximilian Schneider 	/* create netdev */
919d08e973aSMaximilian Schneider 	netdev = alloc_candev(sizeof(struct gs_can), GS_MAX_TX_URBS);
920d08e973aSMaximilian Schneider 	if (!netdev) {
921d08e973aSMaximilian Schneider 		dev_err(&intf->dev, "Couldn't allocate candev\n");
922d08e973aSMaximilian Schneider 		kfree(bt_const);
923d08e973aSMaximilian Schneider 		return ERR_PTR(-ENOMEM);
924d08e973aSMaximilian Schneider 	}
925d08e973aSMaximilian Schneider 
926d08e973aSMaximilian Schneider 	dev = netdev_priv(netdev);
927d08e973aSMaximilian Schneider 
928d08e973aSMaximilian Schneider 	netdev->netdev_ops = &gs_usb_netdev_ops;
929d08e973aSMaximilian Schneider 
930d08e973aSMaximilian Schneider 	netdev->flags |= IFF_ECHO; /* we support full roundtrip echo */
931d08e973aSMaximilian Schneider 
93288bfb9a7SMarc Kleine-Budde 	/* dev setup */
933d08e973aSMaximilian Schneider 	strcpy(dev->bt_const.name, "gs_usb");
9344ba1cb39SMarc Kleine-Budde 	dev->bt_const.tseg1_min = le32_to_cpu(bt_const->tseg1_min);
9354ba1cb39SMarc Kleine-Budde 	dev->bt_const.tseg1_max = le32_to_cpu(bt_const->tseg1_max);
9364ba1cb39SMarc Kleine-Budde 	dev->bt_const.tseg2_min = le32_to_cpu(bt_const->tseg2_min);
9374ba1cb39SMarc Kleine-Budde 	dev->bt_const.tseg2_max = le32_to_cpu(bt_const->tseg2_max);
9384ba1cb39SMarc Kleine-Budde 	dev->bt_const.sjw_max = le32_to_cpu(bt_const->sjw_max);
9394ba1cb39SMarc Kleine-Budde 	dev->bt_const.brp_min = le32_to_cpu(bt_const->brp_min);
9404ba1cb39SMarc Kleine-Budde 	dev->bt_const.brp_max = le32_to_cpu(bt_const->brp_max);
9414ba1cb39SMarc Kleine-Budde 	dev->bt_const.brp_inc = le32_to_cpu(bt_const->brp_inc);
942d08e973aSMaximilian Schneider 
943d08e973aSMaximilian Schneider 	dev->udev = interface_to_usbdev(intf);
944d08e973aSMaximilian Schneider 	dev->iface = intf;
945d08e973aSMaximilian Schneider 	dev->netdev = netdev;
946d08e973aSMaximilian Schneider 	dev->channel = channel;
947d08e973aSMaximilian Schneider 
948d08e973aSMaximilian Schneider 	init_usb_anchor(&dev->tx_submitted);
949d08e973aSMaximilian Schneider 	atomic_set(&dev->active_tx_urbs, 0);
950d08e973aSMaximilian Schneider 	spin_lock_init(&dev->tx_ctx_lock);
951d08e973aSMaximilian Schneider 	for (rc = 0; rc < GS_MAX_TX_URBS; rc++) {
952d08e973aSMaximilian Schneider 		dev->tx_context[rc].dev = dev;
953d08e973aSMaximilian Schneider 		dev->tx_context[rc].echo_id = GS_MAX_TX_URBS;
954d08e973aSMaximilian Schneider 	}
955d08e973aSMaximilian Schneider 
95688bfb9a7SMarc Kleine-Budde 	/* can setup */
957d08e973aSMaximilian Schneider 	dev->can.state = CAN_STATE_STOPPED;
9584ba1cb39SMarc Kleine-Budde 	dev->can.clock.freq = le32_to_cpu(bt_const->fclk_can);
959d08e973aSMaximilian Schneider 	dev->can.bittiming_const = &dev->bt_const;
960d08e973aSMaximilian Schneider 	dev->can.do_set_bittiming = gs_usb_set_bittiming;
961d08e973aSMaximilian Schneider 
9624c01fc87SOliver Hartkopp 	dev->can.ctrlmode_supported = CAN_CTRLMODE_CC_LEN8_DLC;
963d08e973aSMaximilian Schneider 
9644ba1cb39SMarc Kleine-Budde 	feature = le32_to_cpu(bt_const->feature);
965*eb9fa77aSPeter Fink 	dev->feature = FIELD_GET(GS_CAN_FEATURE_MASK, feature);
9664ba1cb39SMarc Kleine-Budde 	if (feature & GS_CAN_FEATURE_LISTEN_ONLY)
967d08e973aSMaximilian Schneider 		dev->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY;
968d08e973aSMaximilian Schneider 
9694ba1cb39SMarc Kleine-Budde 	if (feature & GS_CAN_FEATURE_LOOP_BACK)
970d08e973aSMaximilian Schneider 		dev->can.ctrlmode_supported |= CAN_CTRLMODE_LOOPBACK;
971d08e973aSMaximilian Schneider 
9724ba1cb39SMarc Kleine-Budde 	if (feature & GS_CAN_FEATURE_TRIPLE_SAMPLE)
973d08e973aSMaximilian Schneider 		dev->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
974d08e973aSMaximilian Schneider 
9754ba1cb39SMarc Kleine-Budde 	if (feature & GS_CAN_FEATURE_ONE_SHOT)
976d08e973aSMaximilian Schneider 		dev->can.ctrlmode_supported |= CAN_CTRLMODE_ONE_SHOT;
977d08e973aSMaximilian Schneider 
97826949ac9SPeter Fink 	if (feature & GS_CAN_FEATURE_FD) {
97926949ac9SPeter Fink 		dev->can.ctrlmode_supported |= CAN_CTRLMODE_FD;
98026949ac9SPeter Fink 		dev->can.data_bittiming_const = &dev->bt_const;
98126949ac9SPeter Fink 		dev->can.do_set_data_bittiming = gs_usb_set_data_bittiming;
98226949ac9SPeter Fink 	}
98326949ac9SPeter Fink 
9844ba1cb39SMarc Kleine-Budde 	if (le32_to_cpu(dconf->sw_version) > 1)
9854ba1cb39SMarc Kleine-Budde 		if (feature & GS_CAN_FEATURE_IDENTIFY)
98605ca5270SMaximilian Schneider 			netdev->ethtool_ops = &gs_usb_ethtool_ops;
98705ca5270SMaximilian Schneider 
98805ca5270SMaximilian Schneider 	kfree(bt_const);
98905ca5270SMaximilian Schneider 
990e0d25759SMarc Kleine-Budde 	SET_NETDEV_DEV(netdev, &intf->dev);
991e0d25759SMarc Kleine-Budde 
992d08e973aSMaximilian Schneider 	rc = register_candev(dev->netdev);
993d08e973aSMaximilian Schneider 	if (rc) {
994d08e973aSMaximilian Schneider 		free_candev(dev->netdev);
995d08e973aSMaximilian Schneider 		dev_err(&intf->dev, "Couldn't register candev (err=%d)\n", rc);
996d08e973aSMaximilian Schneider 		return ERR_PTR(rc);
997d08e973aSMaximilian Schneider 	}
998d08e973aSMaximilian Schneider 
999d08e973aSMaximilian Schneider 	return dev;
1000d08e973aSMaximilian Schneider }
1001d08e973aSMaximilian Schneider 
1002d08e973aSMaximilian Schneider static void gs_destroy_candev(struct gs_can *dev)
1003d08e973aSMaximilian Schneider {
1004d08e973aSMaximilian Schneider 	unregister_candev(dev->netdev);
1005d08e973aSMaximilian Schneider 	usb_kill_anchored_urbs(&dev->tx_submitted);
1006e9a2d81bSMaximilain Schneider 	free_candev(dev->netdev);
1007d08e973aSMaximilian Schneider }
1008d08e973aSMaximilian Schneider 
100905ca5270SMaximilian Schneider static int gs_usb_probe(struct usb_interface *intf,
101005ca5270SMaximilian Schneider 			const struct usb_device_id *id)
1011d08e973aSMaximilian Schneider {
10125374d083SMarc Kleine-Budde 	struct usb_device *udev = interface_to_usbdev(intf);
1013c359931dSPeter Fink 	struct gs_host_frame *hf;
1014d08e973aSMaximilian Schneider 	struct gs_usb *dev;
1015d08e973aSMaximilian Schneider 	int rc = -ENOMEM;
1016d08e973aSMaximilian Schneider 	unsigned int icount, i;
1017c919a306SEthan Zonca 	struct gs_host_config *hconf;
1018c919a306SEthan Zonca 	struct gs_device_config *dconf;
1019c919a306SEthan Zonca 
1020c919a306SEthan Zonca 	hconf = kmalloc(sizeof(*hconf), GFP_KERNEL);
1021c919a306SEthan Zonca 	if (!hconf)
1022c919a306SEthan Zonca 		return -ENOMEM;
1023c919a306SEthan Zonca 
10244ba1cb39SMarc Kleine-Budde 	hconf->byte_order = cpu_to_le32(0x0000beef);
1025d08e973aSMaximilian Schneider 
1026d08e973aSMaximilian Schneider 	/* send host config */
10275374d083SMarc Kleine-Budde 	rc = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
1028d08e973aSMaximilian Schneider 			     GS_USB_BREQ_HOST_FORMAT,
1029d08e973aSMaximilian Schneider 			     USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1030c1ee7269SMarc Kleine-Budde 			     1, intf->cur_altsetting->desc.bInterfaceNumber,
1031c1ee7269SMarc Kleine-Budde 			     hconf, sizeof(*hconf), 1000);
1032d08e973aSMaximilian Schneider 
1033c919a306SEthan Zonca 	kfree(hconf);
1034c919a306SEthan Zonca 
1035d08e973aSMaximilian Schneider 	if (rc < 0) {
1036d0cd2aa8SMarc Kleine-Budde 		dev_err(&intf->dev, "Couldn't send data format (err=%d)\n", rc);
1037d08e973aSMaximilian Schneider 		return rc;
1038d08e973aSMaximilian Schneider 	}
1039d08e973aSMaximilian Schneider 
1040c919a306SEthan Zonca 	dconf = kmalloc(sizeof(*dconf), GFP_KERNEL);
1041c919a306SEthan Zonca 	if (!dconf)
1042c919a306SEthan Zonca 		return -ENOMEM;
1043c919a306SEthan Zonca 
1044d08e973aSMaximilian Schneider 	/* read device config */
10455374d083SMarc Kleine-Budde 	rc = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
1046d08e973aSMaximilian Schneider 			     GS_USB_BREQ_DEVICE_CONFIG,
1047d08e973aSMaximilian Schneider 			     USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1048c1ee7269SMarc Kleine-Budde 			     1, intf->cur_altsetting->desc.bInterfaceNumber,
1049c1ee7269SMarc Kleine-Budde 			     dconf, sizeof(*dconf), 1000);
1050d08e973aSMaximilian Schneider 	if (rc < 0) {
1051d08e973aSMaximilian Schneider 		dev_err(&intf->dev, "Couldn't get device config: (err=%d)\n",
1052d08e973aSMaximilian Schneider 			rc);
1053c919a306SEthan Zonca 		kfree(dconf);
1054d08e973aSMaximilian Schneider 		return rc;
1055d08e973aSMaximilian Schneider 	}
1056d08e973aSMaximilian Schneider 
1057c919a306SEthan Zonca 	icount = dconf->icount + 1;
105810819466SMarc Kleine-Budde 	dev_info(&intf->dev, "Configuring for %u interfaces\n", icount);
1059d08e973aSMaximilian Schneider 
1060d08e973aSMaximilian Schneider 	if (icount > GS_MAX_INTF) {
1061d08e973aSMaximilian Schneider 		dev_err(&intf->dev,
106210819466SMarc Kleine-Budde 			"Driver cannot handle more that %u CAN interfaces\n",
1063d08e973aSMaximilian Schneider 			GS_MAX_INTF);
1064c919a306SEthan Zonca 		kfree(dconf);
1065d08e973aSMaximilian Schneider 		return -EINVAL;
1066d08e973aSMaximilian Schneider 	}
1067d08e973aSMaximilian Schneider 
1068d08e973aSMaximilian Schneider 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1069c919a306SEthan Zonca 	if (!dev) {
1070c919a306SEthan Zonca 		kfree(dconf);
107137920a74SColin Ian King 		return -ENOMEM;
1072c919a306SEthan Zonca 	}
1073c919a306SEthan Zonca 
1074d08e973aSMaximilian Schneider 	init_usb_anchor(&dev->rx_submitted);
107526949ac9SPeter Fink 	/* default to classic CAN, switch to CAN-FD if at least one of
107626949ac9SPeter Fink 	 * our channels support CAN-FD.
107726949ac9SPeter Fink 	 */
1078c359931dSPeter Fink 	dev->hf_size_rx = struct_size(hf, classic_can, 1);
1079d08e973aSMaximilian Schneider 
1080d08e973aSMaximilian Schneider 	usb_set_intfdata(intf, dev);
10815374d083SMarc Kleine-Budde 	dev->udev = udev;
1082d08e973aSMaximilian Schneider 
1083d08e973aSMaximilian Schneider 	for (i = 0; i < icount; i++) {
1084c919a306SEthan Zonca 		dev->canch[i] = gs_make_candev(i, intf, dconf);
1085d08e973aSMaximilian Schneider 		if (IS_ERR_OR_NULL(dev->canch[i])) {
1086e9a2d81bSMaximilain Schneider 			/* save error code to return later */
1087e9a2d81bSMaximilain Schneider 			rc = PTR_ERR(dev->canch[i]);
1088e9a2d81bSMaximilain Schneider 
1089d08e973aSMaximilian Schneider 			/* on failure destroy previously created candevs */
1090d08e973aSMaximilian Schneider 			icount = i;
1091e9a2d81bSMaximilain Schneider 			for (i = 0; i < icount; i++)
1092d08e973aSMaximilian Schneider 				gs_destroy_candev(dev->canch[i]);
1093e9a2d81bSMaximilain Schneider 
1094e9a2d81bSMaximilain Schneider 			usb_kill_anchored_urbs(&dev->rx_submitted);
1095c919a306SEthan Zonca 			kfree(dconf);
1096d08e973aSMaximilian Schneider 			kfree(dev);
1097d08e973aSMaximilian Schneider 			return rc;
1098d08e973aSMaximilian Schneider 		}
1099d08e973aSMaximilian Schneider 		dev->canch[i]->parent = dev;
110026949ac9SPeter Fink 
110126949ac9SPeter Fink 		if (dev->canch[i]->can.ctrlmode_supported & CAN_CTRLMODE_FD)
110226949ac9SPeter Fink 			dev->hf_size_rx = struct_size(hf, canfd, 1);
1103d08e973aSMaximilian Schneider 	}
1104d08e973aSMaximilian Schneider 
1105c919a306SEthan Zonca 	kfree(dconf);
1106c919a306SEthan Zonca 
1107d08e973aSMaximilian Schneider 	return 0;
1108d08e973aSMaximilian Schneider }
1109d08e973aSMaximilian Schneider 
1110d08e973aSMaximilian Schneider static void gs_usb_disconnect(struct usb_interface *intf)
1111d08e973aSMaximilian Schneider {
1112d08e973aSMaximilian Schneider 	struct gs_usb *dev = usb_get_intfdata(intf);
1113b9d9b030SMarc Kleine-Budde 	unsigned int i;
1114b9d9b030SMarc Kleine-Budde 
1115d08e973aSMaximilian Schneider 	usb_set_intfdata(intf, NULL);
1116d08e973aSMaximilian Schneider 
1117d08e973aSMaximilian Schneider 	if (!dev) {
1118d08e973aSMaximilian Schneider 		dev_err(&intf->dev, "Disconnect (nodata)\n");
1119d08e973aSMaximilian Schneider 		return;
1120d08e973aSMaximilian Schneider 	}
1121d08e973aSMaximilian Schneider 
1122e9a2d81bSMaximilain Schneider 	for (i = 0; i < GS_MAX_INTF; i++)
1123e9a2d81bSMaximilain Schneider 		if (dev->canch[i])
1124e9a2d81bSMaximilain Schneider 			gs_destroy_candev(dev->canch[i]);
1125d08e973aSMaximilian Schneider 
1126d08e973aSMaximilian Schneider 	usb_kill_anchored_urbs(&dev->rx_submitted);
1127e9a2d81bSMaximilain Schneider 	kfree(dev);
1128d08e973aSMaximilian Schneider }
1129d08e973aSMaximilian Schneider 
1130d08e973aSMaximilian Schneider static const struct usb_device_id gs_usb_table[] = {
11312fe6c943SMaximilian Schneider 	{ USB_DEVICE_INTERFACE_NUMBER(USB_GSUSB_1_VENDOR_ID,
11322fe6c943SMaximilian Schneider 				      USB_GSUSB_1_PRODUCT_ID, 0) },
1133f155d9c0SMaximilian Schneider 	{ USB_DEVICE_INTERFACE_NUMBER(USB_CANDLELIGHT_VENDOR_ID,
1134f155d9c0SMaximilian Schneider 				      USB_CANDLELIGHT_PRODUCT_ID, 0) },
1135d08e973aSMaximilian Schneider 	{} /* Terminating entry */
1136d08e973aSMaximilian Schneider };
1137d08e973aSMaximilian Schneider 
1138d08e973aSMaximilian Schneider MODULE_DEVICE_TABLE(usb, gs_usb_table);
1139d08e973aSMaximilian Schneider 
1140d08e973aSMaximilian Schneider static struct usb_driver gs_usb_driver = {
1141d08e973aSMaximilian Schneider 	.name = "gs_usb",
1142d08e973aSMaximilian Schneider 	.probe = gs_usb_probe,
1143d08e973aSMaximilian Schneider 	.disconnect = gs_usb_disconnect,
1144d08e973aSMaximilian Schneider 	.id_table = gs_usb_table,
1145d08e973aSMaximilian Schneider };
1146d08e973aSMaximilian Schneider 
1147d08e973aSMaximilian Schneider module_usb_driver(gs_usb_driver);
1148d08e973aSMaximilian Schneider 
1149d08e973aSMaximilian Schneider MODULE_AUTHOR("Maximilian Schneider <mws@schneidersoft.net>");
1150d08e973aSMaximilian Schneider MODULE_DESCRIPTION(
1151d08e973aSMaximilian Schneider "Socket CAN device driver for Geschwister Schneider Technologie-, "
1152f155d9c0SMaximilian Schneider "Entwicklungs- und Vertriebs UG. USB2.0 to CAN interfaces\n"
1153f155d9c0SMaximilian Schneider "and bytewerk.org candleLight USB CAN interfaces.");
1154d08e973aSMaximilian Schneider MODULE_LICENSE("GPL v2");
1155