xref: /openbmc/linux/drivers/net/can/usb/gs_usb.c (revision 4643e34eccfc17f97cb18425835c7087afa8d9fb)
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 
12cc69837fSJakub Kicinski #include <linux/ethtool.h>
13d08e973aSMaximilian Schneider #include <linux/init.h>
14d08e973aSMaximilian Schneider #include <linux/module.h>
15d08e973aSMaximilian Schneider #include <linux/netdevice.h>
16f6bb2510SMarc Kleine-Budde #include <linux/signal.h>
17d08e973aSMaximilian Schneider #include <linux/usb.h>
18d08e973aSMaximilian Schneider 
19d08e973aSMaximilian Schneider #include <linux/can.h>
20d08e973aSMaximilian Schneider #include <linux/can/dev.h>
21d08e973aSMaximilian Schneider #include <linux/can/error.h>
22d08e973aSMaximilian Schneider 
23d08e973aSMaximilian Schneider /* Device specific constants */
24d08e973aSMaximilian Schneider #define USB_GSUSB_1_VENDOR_ID 0x1d50
25d08e973aSMaximilian Schneider #define USB_GSUSB_1_PRODUCT_ID 0x606f
26d08e973aSMaximilian Schneider 
27f155d9c0SMaximilian Schneider #define USB_CANDLELIGHT_VENDOR_ID 0x1209
28f155d9c0SMaximilian Schneider #define USB_CANDLELIGHT_PRODUCT_ID 0x2323
29f155d9c0SMaximilian Schneider 
30d08e973aSMaximilian Schneider #define GSUSB_ENDPOINT_IN 1
31d08e973aSMaximilian Schneider #define GSUSB_ENDPOINT_OUT 2
32d08e973aSMaximilian Schneider 
33d08e973aSMaximilian Schneider /* Device specific constants */
34d08e973aSMaximilian Schneider enum gs_usb_breq {
35d08e973aSMaximilian Schneider 	GS_USB_BREQ_HOST_FORMAT = 0,
36d08e973aSMaximilian Schneider 	GS_USB_BREQ_BITTIMING,
37d08e973aSMaximilian Schneider 	GS_USB_BREQ_MODE,
38d08e973aSMaximilian Schneider 	GS_USB_BREQ_BERR,
39d08e973aSMaximilian Schneider 	GS_USB_BREQ_BT_CONST,
4005ca5270SMaximilian Schneider 	GS_USB_BREQ_DEVICE_CONFIG,
4105ca5270SMaximilian Schneider 	GS_USB_BREQ_TIMESTAMP,
4205ca5270SMaximilian Schneider 	GS_USB_BREQ_IDENTIFY,
43e0902cadSMarc Kleine-Budde 	GS_USB_BREQ_GET_USER_ID,
44e0902cadSMarc Kleine-Budde 	GS_USB_BREQ_SET_USER_ID,
45d08e973aSMaximilian Schneider };
46d08e973aSMaximilian Schneider 
47d08e973aSMaximilian Schneider enum gs_can_mode {
48d08e973aSMaximilian Schneider 	/* reset a channel. turns it off */
49d08e973aSMaximilian Schneider 	GS_CAN_MODE_RESET = 0,
50d08e973aSMaximilian Schneider 	/* starts a channel */
51d08e973aSMaximilian Schneider 	GS_CAN_MODE_START
52d08e973aSMaximilian Schneider };
53d08e973aSMaximilian Schneider 
54d08e973aSMaximilian Schneider enum gs_can_state {
55d08e973aSMaximilian Schneider 	GS_CAN_STATE_ERROR_ACTIVE = 0,
56d08e973aSMaximilian Schneider 	GS_CAN_STATE_ERROR_WARNING,
57d08e973aSMaximilian Schneider 	GS_CAN_STATE_ERROR_PASSIVE,
58d08e973aSMaximilian Schneider 	GS_CAN_STATE_BUS_OFF,
59d08e973aSMaximilian Schneider 	GS_CAN_STATE_STOPPED,
60d08e973aSMaximilian Schneider 	GS_CAN_STATE_SLEEPING
61d08e973aSMaximilian Schneider };
62d08e973aSMaximilian Schneider 
6305ca5270SMaximilian Schneider enum gs_can_identify_mode {
6405ca5270SMaximilian Schneider 	GS_CAN_IDENTIFY_OFF = 0,
6505ca5270SMaximilian Schneider 	GS_CAN_IDENTIFY_ON
6605ca5270SMaximilian Schneider };
6705ca5270SMaximilian Schneider 
68d08e973aSMaximilian Schneider /* data types passed between host and device */
694ba1cb39SMarc Kleine-Budde 
704ba1cb39SMarc Kleine-Budde /* The firmware on the original USB2CAN by Geschwister Schneider
714ba1cb39SMarc Kleine-Budde  * Technologie Entwicklungs- und Vertriebs UG exchanges all data
724ba1cb39SMarc Kleine-Budde  * between the host and the device in host byte order. This is done
734ba1cb39SMarc Kleine-Budde  * with the struct gs_host_config::byte_order member, which is sent
744ba1cb39SMarc Kleine-Budde  * first to indicate the desired byte order.
754ba1cb39SMarc Kleine-Budde  *
764ba1cb39SMarc Kleine-Budde  * The widely used open source firmware candleLight doesn't support
774ba1cb39SMarc Kleine-Budde  * this feature and exchanges the data in little endian byte order.
78d08e973aSMaximilian Schneider  */
794ba1cb39SMarc Kleine-Budde struct gs_host_config {
804ba1cb39SMarc Kleine-Budde 	__le32 byte_order;
814ba1cb39SMarc Kleine-Budde } __packed;
82d08e973aSMaximilian Schneider 
83d08e973aSMaximilian Schneider struct gs_device_config {
84d08e973aSMaximilian Schneider 	u8 reserved1;
85d08e973aSMaximilian Schneider 	u8 reserved2;
86d08e973aSMaximilian Schneider 	u8 reserved3;
87d08e973aSMaximilian Schneider 	u8 icount;
884ba1cb39SMarc Kleine-Budde 	__le32 sw_version;
894ba1cb39SMarc Kleine-Budde 	__le32 hw_version;
90d08e973aSMaximilian Schneider } __packed;
91d08e973aSMaximilian Schneider 
92d08e973aSMaximilian Schneider #define GS_CAN_MODE_NORMAL 0
939be95c91SMaximilian Schneider #define GS_CAN_MODE_LISTEN_ONLY BIT(0)
949be95c91SMaximilian Schneider #define GS_CAN_MODE_LOOP_BACK BIT(1)
959be95c91SMaximilian Schneider #define GS_CAN_MODE_TRIPLE_SAMPLE BIT(2)
969be95c91SMaximilian Schneider #define GS_CAN_MODE_ONE_SHOT BIT(3)
97d42d2111SMarc Kleine-Budde #define GS_CAN_MODE_HW_TIMESTAMP BIT(4)
9815564f82SMarc Kleine-Budde /* GS_CAN_FEATURE_IDENTIFY BIT(5) */
99e0902cadSMarc Kleine-Budde /* GS_CAN_FEATURE_USER_ID BIT(6) */
100*4643e34eSMarc Kleine-Budde #define GS_CAN_MODE_PAD_PKTS_TO_MAX_PKT_SIZE BIT(7)
101d08e973aSMaximilian Schneider 
102d08e973aSMaximilian Schneider struct gs_device_mode {
1034ba1cb39SMarc Kleine-Budde 	__le32 mode;
1044ba1cb39SMarc Kleine-Budde 	__le32 flags;
105d08e973aSMaximilian Schneider } __packed;
106d08e973aSMaximilian Schneider 
107d08e973aSMaximilian Schneider struct gs_device_state {
1084ba1cb39SMarc Kleine-Budde 	__le32 state;
1094ba1cb39SMarc Kleine-Budde 	__le32 rxerr;
1104ba1cb39SMarc Kleine-Budde 	__le32 txerr;
111d08e973aSMaximilian Schneider } __packed;
112d08e973aSMaximilian Schneider 
113d08e973aSMaximilian Schneider struct gs_device_bittiming {
1144ba1cb39SMarc Kleine-Budde 	__le32 prop_seg;
1154ba1cb39SMarc Kleine-Budde 	__le32 phase_seg1;
1164ba1cb39SMarc Kleine-Budde 	__le32 phase_seg2;
1174ba1cb39SMarc Kleine-Budde 	__le32 sjw;
1184ba1cb39SMarc Kleine-Budde 	__le32 brp;
119d08e973aSMaximilian Schneider } __packed;
120d08e973aSMaximilian Schneider 
12105ca5270SMaximilian Schneider struct gs_identify_mode {
1224ba1cb39SMarc Kleine-Budde 	__le32 mode;
12305ca5270SMaximilian Schneider } __packed;
12405ca5270SMaximilian Schneider 
1259be95c91SMaximilian Schneider #define GS_CAN_FEATURE_LISTEN_ONLY BIT(0)
1269be95c91SMaximilian Schneider #define GS_CAN_FEATURE_LOOP_BACK BIT(1)
1279be95c91SMaximilian Schneider #define GS_CAN_FEATURE_TRIPLE_SAMPLE BIT(2)
1289be95c91SMaximilian Schneider #define GS_CAN_FEATURE_ONE_SHOT BIT(3)
12905ca5270SMaximilian Schneider #define GS_CAN_FEATURE_HW_TIMESTAMP BIT(4)
13005ca5270SMaximilian Schneider #define GS_CAN_FEATURE_IDENTIFY BIT(5)
131e0902cadSMarc Kleine-Budde #define GS_CAN_FEATURE_USER_ID BIT(6)
132*4643e34eSMarc Kleine-Budde #define GS_CAN_FEATURE_PAD_PKTS_TO_MAX_PKT_SIZE BIT(7)
133d08e973aSMaximilian Schneider 
134d08e973aSMaximilian Schneider struct gs_device_bt_const {
1354ba1cb39SMarc Kleine-Budde 	__le32 feature;
1364ba1cb39SMarc Kleine-Budde 	__le32 fclk_can;
1374ba1cb39SMarc Kleine-Budde 	__le32 tseg1_min;
1384ba1cb39SMarc Kleine-Budde 	__le32 tseg1_max;
1394ba1cb39SMarc Kleine-Budde 	__le32 tseg2_min;
1404ba1cb39SMarc Kleine-Budde 	__le32 tseg2_max;
1414ba1cb39SMarc Kleine-Budde 	__le32 sjw_max;
1424ba1cb39SMarc Kleine-Budde 	__le32 brp_min;
1434ba1cb39SMarc Kleine-Budde 	__le32 brp_max;
1444ba1cb39SMarc Kleine-Budde 	__le32 brp_inc;
145d08e973aSMaximilian Schneider } __packed;
146d08e973aSMaximilian Schneider 
1474b8f03e3SMarc Kleine-Budde #define GS_CAN_FLAG_OVERFLOW BIT(0)
148d08e973aSMaximilian Schneider 
149d08e973aSMaximilian Schneider struct gs_host_frame {
150d08e973aSMaximilian Schneider 	u32 echo_id;
1514ba1cb39SMarc Kleine-Budde 	__le32 can_id;
152d08e973aSMaximilian Schneider 
153d08e973aSMaximilian Schneider 	u8 can_dlc;
154d08e973aSMaximilian Schneider 	u8 channel;
155d08e973aSMaximilian Schneider 	u8 flags;
156d08e973aSMaximilian Schneider 	u8 reserved;
157d08e973aSMaximilian Schneider 
158d08e973aSMaximilian Schneider 	u8 data[8];
159d08e973aSMaximilian Schneider } __packed;
160d08e973aSMaximilian Schneider /* The GS USB devices make use of the same flags and masks as in
161d08e973aSMaximilian Schneider  * linux/can.h and linux/can/error.h, and no additional mapping is necessary.
162d08e973aSMaximilian Schneider  */
163d08e973aSMaximilian Schneider 
164d08e973aSMaximilian Schneider /* Only send a max of GS_MAX_TX_URBS frames per channel at a time. */
165d08e973aSMaximilian Schneider #define GS_MAX_TX_URBS 10
166d08e973aSMaximilian Schneider /* Only launch a max of GS_MAX_RX_URBS usb requests at a time. */
167d08e973aSMaximilian Schneider #define GS_MAX_RX_URBS 30
168d08e973aSMaximilian Schneider /* Maximum number of interfaces the driver supports per device.
169d08e973aSMaximilian Schneider  * Current hardware only supports 2 interfaces. The future may vary.
170d08e973aSMaximilian Schneider  */
171d08e973aSMaximilian Schneider #define GS_MAX_INTF 2
172d08e973aSMaximilian Schneider 
173d08e973aSMaximilian Schneider struct gs_tx_context {
174d08e973aSMaximilian Schneider 	struct gs_can *dev;
175d08e973aSMaximilian Schneider 	unsigned int echo_id;
176d08e973aSMaximilian Schneider };
177d08e973aSMaximilian Schneider 
178d08e973aSMaximilian Schneider struct gs_can {
179d08e973aSMaximilian Schneider 	struct can_priv can; /* must be the first member */
180d08e973aSMaximilian Schneider 
181d08e973aSMaximilian Schneider 	struct gs_usb *parent;
182d08e973aSMaximilian Schneider 
183d08e973aSMaximilian Schneider 	struct net_device *netdev;
184d08e973aSMaximilian Schneider 	struct usb_device *udev;
185d08e973aSMaximilian Schneider 	struct usb_interface *iface;
186d08e973aSMaximilian Schneider 
187d08e973aSMaximilian Schneider 	struct can_bittiming_const bt_const;
188d08e973aSMaximilian Schneider 	unsigned int channel;	/* channel number */
189d08e973aSMaximilian Schneider 
190ae421e33SNik Nyby 	/* This lock prevents a race condition between xmit and receive. */
191d08e973aSMaximilian Schneider 	spinlock_t tx_ctx_lock;
192d08e973aSMaximilian Schneider 	struct gs_tx_context tx_context[GS_MAX_TX_URBS];
193d08e973aSMaximilian Schneider 
194d08e973aSMaximilian Schneider 	struct usb_anchor tx_submitted;
195d08e973aSMaximilian Schneider 	atomic_t active_tx_urbs;
196d08e973aSMaximilian Schneider };
197d08e973aSMaximilian Schneider 
198d08e973aSMaximilian Schneider /* usb interface struct */
199d08e973aSMaximilian Schneider struct gs_usb {
200d08e973aSMaximilian Schneider 	struct gs_can *canch[GS_MAX_INTF];
201d08e973aSMaximilian Schneider 	struct usb_anchor rx_submitted;
202d08e973aSMaximilian Schneider 	struct usb_device *udev;
203035b0fcfSVincent Mailhol 	u8 active_channels;
204d08e973aSMaximilian Schneider };
205d08e973aSMaximilian Schneider 
206d08e973aSMaximilian Schneider /* 'allocate' a tx context.
207d08e973aSMaximilian Schneider  * returns a valid tx context or NULL if there is no space.
208d08e973aSMaximilian Schneider  */
209d08e973aSMaximilian Schneider static struct gs_tx_context *gs_alloc_tx_context(struct gs_can *dev)
210d08e973aSMaximilian Schneider {
211d08e973aSMaximilian Schneider 	int i = 0;
212d08e973aSMaximilian Schneider 	unsigned long flags;
213d08e973aSMaximilian Schneider 
214d08e973aSMaximilian Schneider 	spin_lock_irqsave(&dev->tx_ctx_lock, flags);
215d08e973aSMaximilian Schneider 
216d08e973aSMaximilian Schneider 	for (; i < GS_MAX_TX_URBS; i++) {
217d08e973aSMaximilian Schneider 		if (dev->tx_context[i].echo_id == GS_MAX_TX_URBS) {
218d08e973aSMaximilian Schneider 			dev->tx_context[i].echo_id = i;
219d08e973aSMaximilian Schneider 			spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
220d08e973aSMaximilian Schneider 			return &dev->tx_context[i];
221d08e973aSMaximilian Schneider 		}
222d08e973aSMaximilian Schneider 	}
223d08e973aSMaximilian Schneider 
224d08e973aSMaximilian Schneider 	spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
225d08e973aSMaximilian Schneider 	return NULL;
226d08e973aSMaximilian Schneider }
227d08e973aSMaximilian Schneider 
228d08e973aSMaximilian Schneider /* releases a tx context
229d08e973aSMaximilian Schneider  */
230d08e973aSMaximilian Schneider static void gs_free_tx_context(struct gs_tx_context *txc)
231d08e973aSMaximilian Schneider {
232d08e973aSMaximilian Schneider 	txc->echo_id = GS_MAX_TX_URBS;
233d08e973aSMaximilian Schneider }
234d08e973aSMaximilian Schneider 
235d08e973aSMaximilian Schneider /* Get a tx context by id.
236d08e973aSMaximilian Schneider  */
2379be95c91SMaximilian Schneider static struct gs_tx_context *gs_get_tx_context(struct gs_can *dev,
2389be95c91SMaximilian Schneider 					       unsigned int id)
239d08e973aSMaximilian Schneider {
240d08e973aSMaximilian Schneider 	unsigned long flags;
241d08e973aSMaximilian Schneider 
242d08e973aSMaximilian Schneider 	if (id < GS_MAX_TX_URBS) {
243d08e973aSMaximilian Schneider 		spin_lock_irqsave(&dev->tx_ctx_lock, flags);
244d08e973aSMaximilian Schneider 		if (dev->tx_context[id].echo_id == id) {
245d08e973aSMaximilian Schneider 			spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
246d08e973aSMaximilian Schneider 			return &dev->tx_context[id];
247d08e973aSMaximilian Schneider 		}
248d08e973aSMaximilian Schneider 		spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
249d08e973aSMaximilian Schneider 	}
250d08e973aSMaximilian Schneider 	return NULL;
251d08e973aSMaximilian Schneider }
252d08e973aSMaximilian Schneider 
253dda8deb6SMarc Kleine-Budde static int gs_cmd_reset(struct gs_can *gsdev)
254d08e973aSMaximilian Schneider {
255d08e973aSMaximilian Schneider 	struct gs_device_mode *dm;
256d08e973aSMaximilian Schneider 	struct usb_interface *intf = gsdev->iface;
257d08e973aSMaximilian Schneider 	int rc;
258d08e973aSMaximilian Schneider 
259d08e973aSMaximilian Schneider 	dm = kzalloc(sizeof(*dm), GFP_KERNEL);
260d08e973aSMaximilian Schneider 	if (!dm)
261d08e973aSMaximilian Schneider 		return -ENOMEM;
262d08e973aSMaximilian Schneider 
263d08e973aSMaximilian Schneider 	dm->mode = GS_CAN_MODE_RESET;
264d08e973aSMaximilian Schneider 
265d08e973aSMaximilian Schneider 	rc = usb_control_msg(interface_to_usbdev(intf),
266d08e973aSMaximilian Schneider 			     usb_sndctrlpipe(interface_to_usbdev(intf), 0),
267d08e973aSMaximilian Schneider 			     GS_USB_BREQ_MODE,
268d08e973aSMaximilian Schneider 			     USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
269c1ee7269SMarc Kleine-Budde 			     gsdev->channel, 0, dm, sizeof(*dm), 1000);
270d08e973aSMaximilian Schneider 
2715cda3ee5SMarc Kleine-Budde 	kfree(dm);
2725cda3ee5SMarc Kleine-Budde 
273d08e973aSMaximilian Schneider 	return rc;
274d08e973aSMaximilian Schneider }
275d08e973aSMaximilian Schneider 
276d08e973aSMaximilian Schneider static void gs_update_state(struct gs_can *dev, struct can_frame *cf)
277d08e973aSMaximilian Schneider {
278d08e973aSMaximilian Schneider 	struct can_device_stats *can_stats = &dev->can.can_stats;
279d08e973aSMaximilian Schneider 
280d08e973aSMaximilian Schneider 	if (cf->can_id & CAN_ERR_RESTARTED) {
281d08e973aSMaximilian Schneider 		dev->can.state = CAN_STATE_ERROR_ACTIVE;
282d08e973aSMaximilian Schneider 		can_stats->restarts++;
283d08e973aSMaximilian Schneider 	} else if (cf->can_id & CAN_ERR_BUSOFF) {
284d08e973aSMaximilian Schneider 		dev->can.state = CAN_STATE_BUS_OFF;
285d08e973aSMaximilian Schneider 		can_stats->bus_off++;
286d08e973aSMaximilian Schneider 	} else if (cf->can_id & CAN_ERR_CRTL) {
287d08e973aSMaximilian Schneider 		if ((cf->data[1] & CAN_ERR_CRTL_TX_WARNING) ||
288d08e973aSMaximilian Schneider 		    (cf->data[1] & CAN_ERR_CRTL_RX_WARNING)) {
289d08e973aSMaximilian Schneider 			dev->can.state = CAN_STATE_ERROR_WARNING;
290d08e973aSMaximilian Schneider 			can_stats->error_warning++;
291d08e973aSMaximilian Schneider 		} else if ((cf->data[1] & CAN_ERR_CRTL_TX_PASSIVE) ||
292d08e973aSMaximilian Schneider 			   (cf->data[1] & CAN_ERR_CRTL_RX_PASSIVE)) {
293d08e973aSMaximilian Schneider 			dev->can.state = CAN_STATE_ERROR_PASSIVE;
294d08e973aSMaximilian Schneider 			can_stats->error_passive++;
295d08e973aSMaximilian Schneider 		} else {
296d08e973aSMaximilian Schneider 			dev->can.state = CAN_STATE_ERROR_ACTIVE;
297d08e973aSMaximilian Schneider 		}
298d08e973aSMaximilian Schneider 	}
299d08e973aSMaximilian Schneider }
300d08e973aSMaximilian Schneider 
301ae421e33SNik Nyby static void gs_usb_receive_bulk_callback(struct urb *urb)
302d08e973aSMaximilian Schneider {
303d08e973aSMaximilian Schneider 	struct gs_usb *usbcan = urb->context;
304d08e973aSMaximilian Schneider 	struct gs_can *dev;
305d08e973aSMaximilian Schneider 	struct net_device *netdev;
306d08e973aSMaximilian Schneider 	int rc;
307d08e973aSMaximilian Schneider 	struct net_device_stats *stats;
308d08e973aSMaximilian Schneider 	struct gs_host_frame *hf = urb->transfer_buffer;
309d08e973aSMaximilian Schneider 	struct gs_tx_context *txc;
310d08e973aSMaximilian Schneider 	struct can_frame *cf;
311d08e973aSMaximilian Schneider 	struct sk_buff *skb;
312d08e973aSMaximilian Schneider 
313d08e973aSMaximilian Schneider 	BUG_ON(!usbcan);
314d08e973aSMaximilian Schneider 
315d08e973aSMaximilian Schneider 	switch (urb->status) {
316d08e973aSMaximilian Schneider 	case 0: /* success */
317d08e973aSMaximilian Schneider 		break;
318d08e973aSMaximilian Schneider 	case -ENOENT:
319d08e973aSMaximilian Schneider 	case -ESHUTDOWN:
320d08e973aSMaximilian Schneider 		return;
321d08e973aSMaximilian Schneider 	default:
322d08e973aSMaximilian Schneider 		/* do not resubmit aborted urbs. eg: when device goes down */
323d08e973aSMaximilian Schneider 		return;
324d08e973aSMaximilian Schneider 	}
325d08e973aSMaximilian Schneider 
326d08e973aSMaximilian Schneider 	/* device reports out of range channel id */
327d08e973aSMaximilian Schneider 	if (hf->channel >= GS_MAX_INTF)
3284a8737ffSMarc Kleine-Budde 		goto device_detach;
329d08e973aSMaximilian Schneider 
330d08e973aSMaximilian Schneider 	dev = usbcan->canch[hf->channel];
331d08e973aSMaximilian Schneider 
332d08e973aSMaximilian Schneider 	netdev = dev->netdev;
333d08e973aSMaximilian Schneider 	stats = &netdev->stats;
334d08e973aSMaximilian Schneider 
335d08e973aSMaximilian Schneider 	if (!netif_device_present(netdev))
336d08e973aSMaximilian Schneider 		return;
337d08e973aSMaximilian Schneider 
338d08e973aSMaximilian Schneider 	if (hf->echo_id == -1) { /* normal rx */
339d08e973aSMaximilian Schneider 		skb = alloc_can_skb(dev->netdev, &cf);
340d08e973aSMaximilian Schneider 		if (!skb)
341d08e973aSMaximilian Schneider 			return;
342d08e973aSMaximilian Schneider 
3434ba1cb39SMarc Kleine-Budde 		cf->can_id = le32_to_cpu(hf->can_id);
344d08e973aSMaximilian Schneider 
3454c01fc87SOliver Hartkopp 		can_frame_set_cc_len(cf, hf->can_dlc, dev->can.ctrlmode);
346d08e973aSMaximilian Schneider 		memcpy(cf->data, hf->data, 8);
347d08e973aSMaximilian Schneider 
348d08e973aSMaximilian Schneider 		/* ERROR frames tell us information about the controller */
3494ba1cb39SMarc Kleine-Budde 		if (le32_to_cpu(hf->can_id) & CAN_ERR_FLAG)
350d08e973aSMaximilian Schneider 			gs_update_state(dev, cf);
351d08e973aSMaximilian Schneider 
352d08e973aSMaximilian Schneider 		netdev->stats.rx_packets++;
353d08e973aSMaximilian Schneider 		netdev->stats.rx_bytes += hf->can_dlc;
354d08e973aSMaximilian Schneider 
355d08e973aSMaximilian Schneider 		netif_rx(skb);
356d08e973aSMaximilian Schneider 	} else { /* echo_id == hf->echo_id */
357d08e973aSMaximilian Schneider 		if (hf->echo_id >= GS_MAX_TX_URBS) {
358d08e973aSMaximilian Schneider 			netdev_err(netdev,
35910819466SMarc Kleine-Budde 				   "Unexpected out of range echo id %u\n",
360d08e973aSMaximilian Schneider 				   hf->echo_id);
361d08e973aSMaximilian Schneider 			goto resubmit_urb;
362d08e973aSMaximilian Schneider 		}
363d08e973aSMaximilian Schneider 
364d08e973aSMaximilian Schneider 		txc = gs_get_tx_context(dev, hf->echo_id);
365d08e973aSMaximilian Schneider 
366d08e973aSMaximilian Schneider 		/* bad devices send bad echo_ids. */
367d08e973aSMaximilian Schneider 		if (!txc) {
368d08e973aSMaximilian Schneider 			netdev_err(netdev,
36910819466SMarc Kleine-Budde 				   "Unexpected unused echo id %u\n",
370d08e973aSMaximilian Schneider 				   hf->echo_id);
371d08e973aSMaximilian Schneider 			goto resubmit_urb;
372d08e973aSMaximilian Schneider 		}
373d08e973aSMaximilian Schneider 
374cc4b08c3SVincent Mailhol 		netdev->stats.tx_packets++;
375cc4b08c3SVincent Mailhol 		netdev->stats.tx_bytes += can_get_echo_skb(netdev, hf->echo_id,
376cc4b08c3SVincent Mailhol 							   NULL);
377d08e973aSMaximilian Schneider 
378d08e973aSMaximilian Schneider 		gs_free_tx_context(txc);
379d08e973aSMaximilian Schneider 
38097819f94SWolfgang Grandegger 		atomic_dec(&dev->active_tx_urbs);
38197819f94SWolfgang Grandegger 
382d08e973aSMaximilian Schneider 		netif_wake_queue(netdev);
383d08e973aSMaximilian Schneider 	}
384d08e973aSMaximilian Schneider 
385d08e973aSMaximilian Schneider 	if (hf->flags & GS_CAN_FLAG_OVERFLOW) {
386d08e973aSMaximilian Schneider 		skb = alloc_can_err_skb(netdev, &cf);
387d08e973aSMaximilian Schneider 		if (!skb)
388d08e973aSMaximilian Schneider 			goto resubmit_urb;
389d08e973aSMaximilian Schneider 
390d08e973aSMaximilian Schneider 		cf->can_id |= CAN_ERR_CRTL;
391c7b74967SOliver Hartkopp 		cf->len = CAN_ERR_DLC;
392d08e973aSMaximilian Schneider 		cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
393d08e973aSMaximilian Schneider 		stats->rx_over_errors++;
394d08e973aSMaximilian Schneider 		stats->rx_errors++;
395d08e973aSMaximilian Schneider 		netif_rx(skb);
396d08e973aSMaximilian Schneider 	}
397d08e973aSMaximilian Schneider 
398d08e973aSMaximilian Schneider  resubmit_urb:
399c1ee7269SMarc Kleine-Budde 	usb_fill_bulk_urb(urb, usbcan->udev,
400d08e973aSMaximilian Schneider 			  usb_rcvbulkpipe(usbcan->udev, GSUSB_ENDPOINT_IN),
401c1ee7269SMarc Kleine-Budde 			  hf, sizeof(struct gs_host_frame),
402c1ee7269SMarc Kleine-Budde 			  gs_usb_receive_bulk_callback, usbcan);
403d08e973aSMaximilian Schneider 
404d08e973aSMaximilian Schneider 	rc = usb_submit_urb(urb, GFP_ATOMIC);
405d08e973aSMaximilian Schneider 
406d08e973aSMaximilian Schneider 	/* USB failure take down all interfaces */
407d08e973aSMaximilian Schneider 	if (rc == -ENODEV) {
4084a8737ffSMarc Kleine-Budde  device_detach:
409d08e973aSMaximilian Schneider 		for (rc = 0; rc < GS_MAX_INTF; rc++) {
410d08e973aSMaximilian Schneider 			if (usbcan->canch[rc])
411d08e973aSMaximilian Schneider 				netif_device_detach(usbcan->canch[rc]->netdev);
412d08e973aSMaximilian Schneider 		}
413d08e973aSMaximilian Schneider 	}
414d08e973aSMaximilian Schneider }
415d08e973aSMaximilian Schneider 
416d08e973aSMaximilian Schneider static int gs_usb_set_bittiming(struct net_device *netdev)
417d08e973aSMaximilian Schneider {
418d08e973aSMaximilian Schneider 	struct gs_can *dev = netdev_priv(netdev);
419d08e973aSMaximilian Schneider 	struct can_bittiming *bt = &dev->can.bittiming;
420d08e973aSMaximilian Schneider 	struct usb_interface *intf = dev->iface;
421d08e973aSMaximilian Schneider 	int rc;
422d08e973aSMaximilian Schneider 	struct gs_device_bittiming *dbt;
423d08e973aSMaximilian Schneider 
424d08e973aSMaximilian Schneider 	dbt = kmalloc(sizeof(*dbt), GFP_KERNEL);
425d08e973aSMaximilian Schneider 	if (!dbt)
426d08e973aSMaximilian Schneider 		return -ENOMEM;
427d08e973aSMaximilian Schneider 
4284ba1cb39SMarc Kleine-Budde 	dbt->prop_seg = cpu_to_le32(bt->prop_seg);
4294ba1cb39SMarc Kleine-Budde 	dbt->phase_seg1 = cpu_to_le32(bt->phase_seg1);
4304ba1cb39SMarc Kleine-Budde 	dbt->phase_seg2 = cpu_to_le32(bt->phase_seg2);
4314ba1cb39SMarc Kleine-Budde 	dbt->sjw = cpu_to_le32(bt->sjw);
4324ba1cb39SMarc Kleine-Budde 	dbt->brp = cpu_to_le32(bt->brp);
433d08e973aSMaximilian Schneider 
434d08e973aSMaximilian Schneider 	/* request bit timings */
435d08e973aSMaximilian Schneider 	rc = usb_control_msg(interface_to_usbdev(intf),
436d08e973aSMaximilian Schneider 			     usb_sndctrlpipe(interface_to_usbdev(intf), 0),
437d08e973aSMaximilian Schneider 			     GS_USB_BREQ_BITTIMING,
438d08e973aSMaximilian Schneider 			     USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
439c1ee7269SMarc Kleine-Budde 			     dev->channel, 0, dbt, sizeof(*dbt), 1000);
440d08e973aSMaximilian Schneider 
441d08e973aSMaximilian Schneider 	kfree(dbt);
442d08e973aSMaximilian Schneider 
443d08e973aSMaximilian Schneider 	if (rc < 0)
444d08e973aSMaximilian Schneider 		dev_err(netdev->dev.parent, "Couldn't set bittimings (err=%d)",
445d08e973aSMaximilian Schneider 			rc);
446d08e973aSMaximilian Schneider 
447d5b42e66SWolfgang Grandegger 	return (rc > 0) ? 0 : rc;
448d08e973aSMaximilian Schneider }
449d08e973aSMaximilian Schneider 
450d08e973aSMaximilian Schneider static void gs_usb_xmit_callback(struct urb *urb)
451d08e973aSMaximilian Schneider {
452d08e973aSMaximilian Schneider 	struct gs_tx_context *txc = urb->context;
453d08e973aSMaximilian Schneider 	struct gs_can *dev = txc->dev;
454d08e973aSMaximilian Schneider 	struct net_device *netdev = dev->netdev;
455d08e973aSMaximilian Schneider 
456d08e973aSMaximilian Schneider 	if (urb->status)
45710819466SMarc Kleine-Budde 		netdev_info(netdev, "usb xmit fail %u\n", txc->echo_id);
458d08e973aSMaximilian Schneider 
459c1ee7269SMarc Kleine-Budde 	usb_free_coherent(urb->dev, urb->transfer_buffer_length,
460c1ee7269SMarc Kleine-Budde 			  urb->transfer_buffer, urb->transfer_dma);
461d08e973aSMaximilian Schneider }
462d08e973aSMaximilian Schneider 
4639be95c91SMaximilian Schneider static netdev_tx_t gs_can_start_xmit(struct sk_buff *skb,
4649be95c91SMaximilian Schneider 				     struct net_device *netdev)
465d08e973aSMaximilian Schneider {
466d08e973aSMaximilian Schneider 	struct gs_can *dev = netdev_priv(netdev);
467d08e973aSMaximilian Schneider 	struct net_device_stats *stats = &dev->netdev->stats;
468d08e973aSMaximilian Schneider 	struct urb *urb;
469d08e973aSMaximilian Schneider 	struct gs_host_frame *hf;
470d08e973aSMaximilian Schneider 	struct can_frame *cf;
471d08e973aSMaximilian Schneider 	int rc;
472d08e973aSMaximilian Schneider 	unsigned int idx;
473d08e973aSMaximilian Schneider 	struct gs_tx_context *txc;
474d08e973aSMaximilian Schneider 
475d08e973aSMaximilian Schneider 	if (can_dropped_invalid_skb(netdev, skb))
476d08e973aSMaximilian Schneider 		return NETDEV_TX_OK;
477d08e973aSMaximilian Schneider 
478d08e973aSMaximilian Schneider 	/* find an empty context to keep track of transmission */
479d08e973aSMaximilian Schneider 	txc = gs_alloc_tx_context(dev);
480d08e973aSMaximilian Schneider 	if (!txc)
481d08e973aSMaximilian Schneider 		return NETDEV_TX_BUSY;
482d08e973aSMaximilian Schneider 
483d08e973aSMaximilian Schneider 	/* create a URB, and a buffer for it */
484d08e973aSMaximilian Schneider 	urb = usb_alloc_urb(0, GFP_ATOMIC);
4852a4a1e40SWolfram Sang 	if (!urb)
486d08e973aSMaximilian Schneider 		goto nomem_urb;
487d08e973aSMaximilian Schneider 
488d08e973aSMaximilian Schneider 	hf = usb_alloc_coherent(dev->udev, sizeof(*hf), GFP_ATOMIC,
489d08e973aSMaximilian Schneider 				&urb->transfer_dma);
490d08e973aSMaximilian Schneider 	if (!hf) {
491d08e973aSMaximilian Schneider 		netdev_err(netdev, "No memory left for USB buffer\n");
492d08e973aSMaximilian Schneider 		goto nomem_hf;
493d08e973aSMaximilian Schneider 	}
494d08e973aSMaximilian Schneider 
495d08e973aSMaximilian Schneider 	idx = txc->echo_id;
496d08e973aSMaximilian Schneider 
497d08e973aSMaximilian Schneider 	if (idx >= GS_MAX_TX_URBS) {
49810819466SMarc Kleine-Budde 		netdev_err(netdev, "Invalid tx context %u\n", idx);
499d08e973aSMaximilian Schneider 		goto badidx;
500d08e973aSMaximilian Schneider 	}
501d08e973aSMaximilian Schneider 
502d08e973aSMaximilian Schneider 	hf->echo_id = idx;
503d08e973aSMaximilian Schneider 	hf->channel = dev->channel;
50489d58aebSBrian Silverman 	hf->flags = 0;
50589d58aebSBrian Silverman 	hf->reserved = 0;
506d08e973aSMaximilian Schneider 
507d08e973aSMaximilian Schneider 	cf = (struct can_frame *)skb->data;
508d08e973aSMaximilian Schneider 
5094ba1cb39SMarc Kleine-Budde 	hf->can_id = cpu_to_le32(cf->can_id);
5104c01fc87SOliver Hartkopp 	hf->can_dlc = can_get_cc_dlc(cf, dev->can.ctrlmode);
5114c01fc87SOliver Hartkopp 
512c7b74967SOliver Hartkopp 	memcpy(hf->data, cf->data, cf->len);
513d08e973aSMaximilian Schneider 
514d08e973aSMaximilian Schneider 	usb_fill_bulk_urb(urb, dev->udev,
515d08e973aSMaximilian Schneider 			  usb_sndbulkpipe(dev->udev, GSUSB_ENDPOINT_OUT),
516c1ee7269SMarc Kleine-Budde 			  hf, sizeof(*hf),
517c1ee7269SMarc Kleine-Budde 			  gs_usb_xmit_callback, txc);
518d08e973aSMaximilian Schneider 
519d08e973aSMaximilian Schneider 	urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
520d08e973aSMaximilian Schneider 	usb_anchor_urb(urb, &dev->tx_submitted);
521d08e973aSMaximilian Schneider 
5221dcb6e57SVincent Mailhol 	can_put_echo_skb(skb, netdev, idx, 0);
523d08e973aSMaximilian Schneider 
524d08e973aSMaximilian Schneider 	atomic_inc(&dev->active_tx_urbs);
525d08e973aSMaximilian Schneider 
526d08e973aSMaximilian Schneider 	rc = usb_submit_urb(urb, GFP_ATOMIC);
527d08e973aSMaximilian Schneider 	if (unlikely(rc)) {			/* usb send failed */
528d08e973aSMaximilian Schneider 		atomic_dec(&dev->active_tx_urbs);
529d08e973aSMaximilian Schneider 
530f318482aSMarc Kleine-Budde 		can_free_echo_skb(netdev, idx, NULL);
531d08e973aSMaximilian Schneider 		gs_free_tx_context(txc);
532d08e973aSMaximilian Schneider 
533d08e973aSMaximilian Schneider 		usb_unanchor_urb(urb);
534d08e973aSMaximilian Schneider 		usb_free_coherent(dev->udev,
535c1ee7269SMarc Kleine-Budde 				  sizeof(*hf), hf, urb->transfer_dma);
536d08e973aSMaximilian Schneider 
537d08e973aSMaximilian Schneider 		if (rc == -ENODEV) {
538d08e973aSMaximilian Schneider 			netif_device_detach(netdev);
539d08e973aSMaximilian Schneider 		} else {
540d08e973aSMaximilian Schneider 			netdev_err(netdev, "usb_submit failed (err=%d)\n", rc);
541d08e973aSMaximilian Schneider 			stats->tx_dropped++;
542d08e973aSMaximilian Schneider 		}
543d08e973aSMaximilian Schneider 	} else {
544d08e973aSMaximilian Schneider 		/* Slow down tx path */
545d08e973aSMaximilian Schneider 		if (atomic_read(&dev->active_tx_urbs) >= GS_MAX_TX_URBS)
546d08e973aSMaximilian Schneider 			netif_stop_queue(netdev);
547d08e973aSMaximilian Schneider 	}
548d08e973aSMaximilian Schneider 
549d08e973aSMaximilian Schneider 	/* let usb core take care of this urb */
550d08e973aSMaximilian Schneider 	usb_free_urb(urb);
551d08e973aSMaximilian Schneider 
552d08e973aSMaximilian Schneider 	return NETDEV_TX_OK;
553d08e973aSMaximilian Schneider 
554d08e973aSMaximilian Schneider  badidx:
555c1ee7269SMarc Kleine-Budde 	usb_free_coherent(dev->udev, sizeof(*hf), hf, urb->transfer_dma);
556d08e973aSMaximilian Schneider  nomem_hf:
557d08e973aSMaximilian Schneider 	usb_free_urb(urb);
558d08e973aSMaximilian Schneider 
559d08e973aSMaximilian Schneider  nomem_urb:
560d08e973aSMaximilian Schneider 	gs_free_tx_context(txc);
561d08e973aSMaximilian Schneider 	dev_kfree_skb(skb);
562d08e973aSMaximilian Schneider 	stats->tx_dropped++;
563d08e973aSMaximilian Schneider 	return NETDEV_TX_OK;
564d08e973aSMaximilian Schneider }
565d08e973aSMaximilian Schneider 
566d08e973aSMaximilian Schneider static int gs_can_open(struct net_device *netdev)
567d08e973aSMaximilian Schneider {
568d08e973aSMaximilian Schneider 	struct gs_can *dev = netdev_priv(netdev);
569d08e973aSMaximilian Schneider 	struct gs_usb *parent = dev->parent;
570d08e973aSMaximilian Schneider 	int rc, i;
571d08e973aSMaximilian Schneider 	struct gs_device_mode *dm;
572d08e973aSMaximilian Schneider 	u32 ctrlmode;
5734ba1cb39SMarc Kleine-Budde 	u32 flags = 0;
574d08e973aSMaximilian Schneider 
575d08e973aSMaximilian Schneider 	rc = open_candev(netdev);
576d08e973aSMaximilian Schneider 	if (rc)
577d08e973aSMaximilian Schneider 		return rc;
578d08e973aSMaximilian Schneider 
579035b0fcfSVincent Mailhol 	if (!parent->active_channels) {
580d08e973aSMaximilian Schneider 		for (i = 0; i < GS_MAX_RX_URBS; i++) {
581d08e973aSMaximilian Schneider 			struct urb *urb;
582d08e973aSMaximilian Schneider 			u8 *buf;
583d08e973aSMaximilian Schneider 
584d08e973aSMaximilian Schneider 			/* alloc rx urb */
585d08e973aSMaximilian Schneider 			urb = usb_alloc_urb(0, GFP_KERNEL);
5862a4a1e40SWolfram Sang 			if (!urb)
587d08e973aSMaximilian Schneider 				return -ENOMEM;
588d08e973aSMaximilian Schneider 
589d08e973aSMaximilian Schneider 			/* alloc rx buffer */
590d08e973aSMaximilian Schneider 			buf = usb_alloc_coherent(dev->udev,
591d08e973aSMaximilian Schneider 						 sizeof(struct gs_host_frame),
592d08e973aSMaximilian Schneider 						 GFP_KERNEL,
593d08e973aSMaximilian Schneider 						 &urb->transfer_dma);
594d08e973aSMaximilian Schneider 			if (!buf) {
595d08e973aSMaximilian Schneider 				netdev_err(netdev,
596d08e973aSMaximilian Schneider 					   "No memory left for USB buffer\n");
597d08e973aSMaximilian Schneider 				usb_free_urb(urb);
598d08e973aSMaximilian Schneider 				return -ENOMEM;
599d08e973aSMaximilian Schneider 			}
600d08e973aSMaximilian Schneider 
601d08e973aSMaximilian Schneider 			/* fill, anchor, and submit rx urb */
602d08e973aSMaximilian Schneider 			usb_fill_bulk_urb(urb,
603d08e973aSMaximilian Schneider 					  dev->udev,
604d08e973aSMaximilian Schneider 					  usb_rcvbulkpipe(dev->udev,
605d08e973aSMaximilian Schneider 							  GSUSB_ENDPOINT_IN),
606d08e973aSMaximilian Schneider 					  buf,
607d08e973aSMaximilian Schneider 					  sizeof(struct gs_host_frame),
608c1ee7269SMarc Kleine-Budde 					  gs_usb_receive_bulk_callback, parent);
609d08e973aSMaximilian Schneider 			urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
610d08e973aSMaximilian Schneider 
611d08e973aSMaximilian Schneider 			usb_anchor_urb(urb, &parent->rx_submitted);
612d08e973aSMaximilian Schneider 
613d08e973aSMaximilian Schneider 			rc = usb_submit_urb(urb, GFP_KERNEL);
614d08e973aSMaximilian Schneider 			if (rc) {
615d08e973aSMaximilian Schneider 				if (rc == -ENODEV)
616d08e973aSMaximilian Schneider 					netif_device_detach(dev->netdev);
617d08e973aSMaximilian Schneider 
618d08e973aSMaximilian Schneider 				netdev_err(netdev,
619d0cd2aa8SMarc Kleine-Budde 					   "usb_submit failed (err=%d)\n", rc);
620d08e973aSMaximilian Schneider 
621d08e973aSMaximilian Schneider 				usb_unanchor_urb(urb);
622fb5be6a7SNavid Emamdoost 				usb_free_urb(urb);
623d08e973aSMaximilian Schneider 				break;
624d08e973aSMaximilian Schneider 			}
625d08e973aSMaximilian Schneider 
626d08e973aSMaximilian Schneider 			/* Drop reference,
627d08e973aSMaximilian Schneider 			 * USB core will take care of freeing it
628d08e973aSMaximilian Schneider 			 */
629d08e973aSMaximilian Schneider 			usb_free_urb(urb);
630d08e973aSMaximilian Schneider 		}
631d08e973aSMaximilian Schneider 	}
632d08e973aSMaximilian Schneider 
633d08e973aSMaximilian Schneider 	dm = kmalloc(sizeof(*dm), GFP_KERNEL);
634d08e973aSMaximilian Schneider 	if (!dm)
635d08e973aSMaximilian Schneider 		return -ENOMEM;
636d08e973aSMaximilian Schneider 
637d08e973aSMaximilian Schneider 	/* flags */
638d08e973aSMaximilian Schneider 	ctrlmode = dev->can.ctrlmode;
639d08e973aSMaximilian Schneider 
640d08e973aSMaximilian Schneider 	if (ctrlmode & CAN_CTRLMODE_LOOPBACK)
6414ba1cb39SMarc Kleine-Budde 		flags |= GS_CAN_MODE_LOOP_BACK;
642d08e973aSMaximilian Schneider 	else if (ctrlmode & CAN_CTRLMODE_LISTENONLY)
6434ba1cb39SMarc Kleine-Budde 		flags |= GS_CAN_MODE_LISTEN_ONLY;
644d08e973aSMaximilian Schneider 
645d08e973aSMaximilian Schneider 	/* Controller is not allowed to retry TX
646d08e973aSMaximilian Schneider 	 * this mode is unavailable on atmels uc3c hardware
647d08e973aSMaximilian Schneider 	 */
648d08e973aSMaximilian Schneider 	if (ctrlmode & CAN_CTRLMODE_ONE_SHOT)
6494ba1cb39SMarc Kleine-Budde 		flags |= GS_CAN_MODE_ONE_SHOT;
650d08e973aSMaximilian Schneider 
651d08e973aSMaximilian Schneider 	if (ctrlmode & CAN_CTRLMODE_3_SAMPLES)
6524ba1cb39SMarc Kleine-Budde 		flags |= GS_CAN_MODE_TRIPLE_SAMPLE;
653d08e973aSMaximilian Schneider 
654d08e973aSMaximilian Schneider 	/* finally start device */
6554ba1cb39SMarc Kleine-Budde 	dm->mode = cpu_to_le32(GS_CAN_MODE_START);
6564ba1cb39SMarc Kleine-Budde 	dm->flags = cpu_to_le32(flags);
657d08e973aSMaximilian Schneider 	rc = usb_control_msg(interface_to_usbdev(dev->iface),
658d08e973aSMaximilian Schneider 			     usb_sndctrlpipe(interface_to_usbdev(dev->iface), 0),
659d08e973aSMaximilian Schneider 			     GS_USB_BREQ_MODE,
660c1ee7269SMarc Kleine-Budde 			     USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
661c1ee7269SMarc Kleine-Budde 			     dev->channel, 0, dm, sizeof(*dm), 1000);
662d08e973aSMaximilian Schneider 
663d08e973aSMaximilian Schneider 	if (rc < 0) {
664d08e973aSMaximilian Schneider 		netdev_err(netdev, "Couldn't start device (err=%d)\n", rc);
665d08e973aSMaximilian Schneider 		kfree(dm);
666d08e973aSMaximilian Schneider 		return rc;
667d08e973aSMaximilian Schneider 	}
668d08e973aSMaximilian Schneider 
669d08e973aSMaximilian Schneider 	kfree(dm);
670d08e973aSMaximilian Schneider 
671d08e973aSMaximilian Schneider 	dev->can.state = CAN_STATE_ERROR_ACTIVE;
672d08e973aSMaximilian Schneider 
673035b0fcfSVincent Mailhol 	parent->active_channels++;
674d08e973aSMaximilian Schneider 	if (!(dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY))
675d08e973aSMaximilian Schneider 		netif_start_queue(netdev);
676d08e973aSMaximilian Schneider 
677d08e973aSMaximilian Schneider 	return 0;
678d08e973aSMaximilian Schneider }
679d08e973aSMaximilian Schneider 
680d08e973aSMaximilian Schneider static int gs_can_close(struct net_device *netdev)
681d08e973aSMaximilian Schneider {
682d08e973aSMaximilian Schneider 	int rc;
683d08e973aSMaximilian Schneider 	struct gs_can *dev = netdev_priv(netdev);
684d08e973aSMaximilian Schneider 	struct gs_usb *parent = dev->parent;
685d08e973aSMaximilian Schneider 
686d08e973aSMaximilian Schneider 	netif_stop_queue(netdev);
687d08e973aSMaximilian Schneider 
688d08e973aSMaximilian Schneider 	/* Stop polling */
689035b0fcfSVincent Mailhol 	parent->active_channels--;
690035b0fcfSVincent Mailhol 	if (!parent->active_channels)
691d08e973aSMaximilian Schneider 		usb_kill_anchored_urbs(&parent->rx_submitted);
692d08e973aSMaximilian Schneider 
693d08e973aSMaximilian Schneider 	/* Stop sending URBs */
694d08e973aSMaximilian Schneider 	usb_kill_anchored_urbs(&dev->tx_submitted);
695d08e973aSMaximilian Schneider 	atomic_set(&dev->active_tx_urbs, 0);
696d08e973aSMaximilian Schneider 
697d08e973aSMaximilian Schneider 	/* reset the device */
698dda8deb6SMarc Kleine-Budde 	rc = gs_cmd_reset(dev);
699d08e973aSMaximilian Schneider 	if (rc < 0)
700d08e973aSMaximilian Schneider 		netdev_warn(netdev, "Couldn't shutdown device (err=%d)", rc);
701d08e973aSMaximilian Schneider 
702d08e973aSMaximilian Schneider 	/* reset tx contexts */
703d08e973aSMaximilian Schneider 	for (rc = 0; rc < GS_MAX_TX_URBS; rc++) {
704d08e973aSMaximilian Schneider 		dev->tx_context[rc].dev = dev;
705d08e973aSMaximilian Schneider 		dev->tx_context[rc].echo_id = GS_MAX_TX_URBS;
706d08e973aSMaximilian Schneider 	}
707d08e973aSMaximilian Schneider 
708d08e973aSMaximilian Schneider 	/* close the netdev */
709d08e973aSMaximilian Schneider 	close_candev(netdev);
710d08e973aSMaximilian Schneider 
711d08e973aSMaximilian Schneider 	return 0;
712d08e973aSMaximilian Schneider }
713d08e973aSMaximilian Schneider 
714d08e973aSMaximilian Schneider static const struct net_device_ops gs_usb_netdev_ops = {
715d08e973aSMaximilian Schneider 	.ndo_open = gs_can_open,
716d08e973aSMaximilian Schneider 	.ndo_stop = gs_can_close,
717d08e973aSMaximilian Schneider 	.ndo_start_xmit = gs_can_start_xmit,
71850212b42SMarc Kleine-Budde 	.ndo_change_mtu = can_change_mtu,
719d08e973aSMaximilian Schneider };
720d08e973aSMaximilian Schneider 
72105ca5270SMaximilian Schneider static int gs_usb_set_identify(struct net_device *netdev, bool do_identify)
72205ca5270SMaximilian Schneider {
72305ca5270SMaximilian Schneider 	struct gs_can *dev = netdev_priv(netdev);
724b05c73bdSMaksim Salau 	struct gs_identify_mode *imode;
72505ca5270SMaximilian Schneider 	int rc;
72605ca5270SMaximilian Schneider 
727b05c73bdSMaksim Salau 	imode = kmalloc(sizeof(*imode), GFP_KERNEL);
728b05c73bdSMaksim Salau 
729b05c73bdSMaksim Salau 	if (!imode)
730b05c73bdSMaksim Salau 		return -ENOMEM;
731b05c73bdSMaksim Salau 
73205ca5270SMaximilian Schneider 	if (do_identify)
7334ba1cb39SMarc Kleine-Budde 		imode->mode = cpu_to_le32(GS_CAN_IDENTIFY_ON);
73405ca5270SMaximilian Schneider 	else
7354ba1cb39SMarc Kleine-Budde 		imode->mode = cpu_to_le32(GS_CAN_IDENTIFY_OFF);
73605ca5270SMaximilian Schneider 
73705ca5270SMaximilian Schneider 	rc = usb_control_msg(interface_to_usbdev(dev->iface),
738c1ee7269SMarc Kleine-Budde 			     usb_sndctrlpipe(interface_to_usbdev(dev->iface), 0),
73905ca5270SMaximilian Schneider 			     GS_USB_BREQ_IDENTIFY,
740c1ee7269SMarc Kleine-Budde 			     USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
741c1ee7269SMarc Kleine-Budde 			     dev->channel, 0, imode, sizeof(*imode), 100);
74205ca5270SMaximilian Schneider 
743b05c73bdSMaksim Salau 	kfree(imode);
744b05c73bdSMaksim Salau 
74505ca5270SMaximilian Schneider 	return (rc > 0) ? 0 : rc;
74605ca5270SMaximilian Schneider }
74705ca5270SMaximilian Schneider 
74805ca5270SMaximilian Schneider /* blink LED's for finding the this interface */
74905ca5270SMaximilian Schneider static int gs_usb_set_phys_id(struct net_device *dev,
75005ca5270SMaximilian Schneider 			      enum ethtool_phys_id_state state)
75105ca5270SMaximilian Schneider {
75205ca5270SMaximilian Schneider 	int rc = 0;
75305ca5270SMaximilian Schneider 
75405ca5270SMaximilian Schneider 	switch (state) {
75505ca5270SMaximilian Schneider 	case ETHTOOL_ID_ACTIVE:
75605ca5270SMaximilian Schneider 		rc = gs_usb_set_identify(dev, GS_CAN_IDENTIFY_ON);
75705ca5270SMaximilian Schneider 		break;
75805ca5270SMaximilian Schneider 	case ETHTOOL_ID_INACTIVE:
75905ca5270SMaximilian Schneider 		rc = gs_usb_set_identify(dev, GS_CAN_IDENTIFY_OFF);
76005ca5270SMaximilian Schneider 		break;
76105ca5270SMaximilian Schneider 	default:
76205ca5270SMaximilian Schneider 		break;
76305ca5270SMaximilian Schneider 	}
76405ca5270SMaximilian Schneider 
76505ca5270SMaximilian Schneider 	return rc;
76605ca5270SMaximilian Schneider }
76705ca5270SMaximilian Schneider 
76805ca5270SMaximilian Schneider static const struct ethtool_ops gs_usb_ethtool_ops = {
76905ca5270SMaximilian Schneider 	.set_phys_id = gs_usb_set_phys_id,
77005ca5270SMaximilian Schneider };
77105ca5270SMaximilian Schneider 
77205ca5270SMaximilian Schneider static struct gs_can *gs_make_candev(unsigned int channel,
77305ca5270SMaximilian Schneider 				     struct usb_interface *intf,
77405ca5270SMaximilian Schneider 				     struct gs_device_config *dconf)
775d08e973aSMaximilian Schneider {
776d08e973aSMaximilian Schneider 	struct gs_can *dev;
777d08e973aSMaximilian Schneider 	struct net_device *netdev;
778d08e973aSMaximilian Schneider 	int rc;
779d08e973aSMaximilian Schneider 	struct gs_device_bt_const *bt_const;
7804ba1cb39SMarc Kleine-Budde 	u32 feature;
781d08e973aSMaximilian Schneider 
782d08e973aSMaximilian Schneider 	bt_const = kmalloc(sizeof(*bt_const), GFP_KERNEL);
783d08e973aSMaximilian Schneider 	if (!bt_const)
784d08e973aSMaximilian Schneider 		return ERR_PTR(-ENOMEM);
785d08e973aSMaximilian Schneider 
786d08e973aSMaximilian Schneider 	/* fetch bit timing constants */
787d08e973aSMaximilian Schneider 	rc = usb_control_msg(interface_to_usbdev(intf),
788d08e973aSMaximilian Schneider 			     usb_rcvctrlpipe(interface_to_usbdev(intf), 0),
789d08e973aSMaximilian Schneider 			     GS_USB_BREQ_BT_CONST,
790d08e973aSMaximilian Schneider 			     USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
791c1ee7269SMarc Kleine-Budde 			     channel, 0, bt_const, sizeof(*bt_const), 1000);
792d08e973aSMaximilian Schneider 
793d08e973aSMaximilian Schneider 	if (rc < 0) {
794d08e973aSMaximilian Schneider 		dev_err(&intf->dev,
795d08e973aSMaximilian Schneider 			"Couldn't get bit timing const for channel (err=%d)\n",
796d08e973aSMaximilian Schneider 			rc);
797d08e973aSMaximilian Schneider 		kfree(bt_const);
798d08e973aSMaximilian Schneider 		return ERR_PTR(rc);
799d08e973aSMaximilian Schneider 	}
800d08e973aSMaximilian Schneider 
801d08e973aSMaximilian Schneider 	/* create netdev */
802d08e973aSMaximilian Schneider 	netdev = alloc_candev(sizeof(struct gs_can), GS_MAX_TX_URBS);
803d08e973aSMaximilian Schneider 	if (!netdev) {
804d08e973aSMaximilian Schneider 		dev_err(&intf->dev, "Couldn't allocate candev\n");
805d08e973aSMaximilian Schneider 		kfree(bt_const);
806d08e973aSMaximilian Schneider 		return ERR_PTR(-ENOMEM);
807d08e973aSMaximilian Schneider 	}
808d08e973aSMaximilian Schneider 
809d08e973aSMaximilian Schneider 	dev = netdev_priv(netdev);
810d08e973aSMaximilian Schneider 
811d08e973aSMaximilian Schneider 	netdev->netdev_ops = &gs_usb_netdev_ops;
812d08e973aSMaximilian Schneider 
813d08e973aSMaximilian Schneider 	netdev->flags |= IFF_ECHO; /* we support full roundtrip echo */
814d08e973aSMaximilian Schneider 
81588bfb9a7SMarc Kleine-Budde 	/* dev setup */
816d08e973aSMaximilian Schneider 	strcpy(dev->bt_const.name, "gs_usb");
8174ba1cb39SMarc Kleine-Budde 	dev->bt_const.tseg1_min = le32_to_cpu(bt_const->tseg1_min);
8184ba1cb39SMarc Kleine-Budde 	dev->bt_const.tseg1_max = le32_to_cpu(bt_const->tseg1_max);
8194ba1cb39SMarc Kleine-Budde 	dev->bt_const.tseg2_min = le32_to_cpu(bt_const->tseg2_min);
8204ba1cb39SMarc Kleine-Budde 	dev->bt_const.tseg2_max = le32_to_cpu(bt_const->tseg2_max);
8214ba1cb39SMarc Kleine-Budde 	dev->bt_const.sjw_max = le32_to_cpu(bt_const->sjw_max);
8224ba1cb39SMarc Kleine-Budde 	dev->bt_const.brp_min = le32_to_cpu(bt_const->brp_min);
8234ba1cb39SMarc Kleine-Budde 	dev->bt_const.brp_max = le32_to_cpu(bt_const->brp_max);
8244ba1cb39SMarc Kleine-Budde 	dev->bt_const.brp_inc = le32_to_cpu(bt_const->brp_inc);
825d08e973aSMaximilian Schneider 
826d08e973aSMaximilian Schneider 	dev->udev = interface_to_usbdev(intf);
827d08e973aSMaximilian Schneider 	dev->iface = intf;
828d08e973aSMaximilian Schneider 	dev->netdev = netdev;
829d08e973aSMaximilian Schneider 	dev->channel = channel;
830d08e973aSMaximilian Schneider 
831d08e973aSMaximilian Schneider 	init_usb_anchor(&dev->tx_submitted);
832d08e973aSMaximilian Schneider 	atomic_set(&dev->active_tx_urbs, 0);
833d08e973aSMaximilian Schneider 	spin_lock_init(&dev->tx_ctx_lock);
834d08e973aSMaximilian Schneider 	for (rc = 0; rc < GS_MAX_TX_URBS; rc++) {
835d08e973aSMaximilian Schneider 		dev->tx_context[rc].dev = dev;
836d08e973aSMaximilian Schneider 		dev->tx_context[rc].echo_id = GS_MAX_TX_URBS;
837d08e973aSMaximilian Schneider 	}
838d08e973aSMaximilian Schneider 
83988bfb9a7SMarc Kleine-Budde 	/* can setup */
840d08e973aSMaximilian Schneider 	dev->can.state = CAN_STATE_STOPPED;
8414ba1cb39SMarc Kleine-Budde 	dev->can.clock.freq = le32_to_cpu(bt_const->fclk_can);
842d08e973aSMaximilian Schneider 	dev->can.bittiming_const = &dev->bt_const;
843d08e973aSMaximilian Schneider 	dev->can.do_set_bittiming = gs_usb_set_bittiming;
844d08e973aSMaximilian Schneider 
8454c01fc87SOliver Hartkopp 	dev->can.ctrlmode_supported = CAN_CTRLMODE_CC_LEN8_DLC;
846d08e973aSMaximilian Schneider 
8474ba1cb39SMarc Kleine-Budde 	feature = le32_to_cpu(bt_const->feature);
8484ba1cb39SMarc Kleine-Budde 	if (feature & GS_CAN_FEATURE_LISTEN_ONLY)
849d08e973aSMaximilian Schneider 		dev->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY;
850d08e973aSMaximilian Schneider 
8514ba1cb39SMarc Kleine-Budde 	if (feature & GS_CAN_FEATURE_LOOP_BACK)
852d08e973aSMaximilian Schneider 		dev->can.ctrlmode_supported |= CAN_CTRLMODE_LOOPBACK;
853d08e973aSMaximilian Schneider 
8544ba1cb39SMarc Kleine-Budde 	if (feature & GS_CAN_FEATURE_TRIPLE_SAMPLE)
855d08e973aSMaximilian Schneider 		dev->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
856d08e973aSMaximilian Schneider 
8574ba1cb39SMarc Kleine-Budde 	if (feature & GS_CAN_FEATURE_ONE_SHOT)
858d08e973aSMaximilian Schneider 		dev->can.ctrlmode_supported |= CAN_CTRLMODE_ONE_SHOT;
859d08e973aSMaximilian Schneider 
8604ba1cb39SMarc Kleine-Budde 	if (le32_to_cpu(dconf->sw_version) > 1)
8614ba1cb39SMarc Kleine-Budde 		if (feature & GS_CAN_FEATURE_IDENTIFY)
86205ca5270SMaximilian Schneider 			netdev->ethtool_ops = &gs_usb_ethtool_ops;
86305ca5270SMaximilian Schneider 
86405ca5270SMaximilian Schneider 	kfree(bt_const);
86505ca5270SMaximilian Schneider 
866e0d25759SMarc Kleine-Budde 	SET_NETDEV_DEV(netdev, &intf->dev);
867e0d25759SMarc Kleine-Budde 
868d08e973aSMaximilian Schneider 	rc = register_candev(dev->netdev);
869d08e973aSMaximilian Schneider 	if (rc) {
870d08e973aSMaximilian Schneider 		free_candev(dev->netdev);
871d08e973aSMaximilian Schneider 		dev_err(&intf->dev, "Couldn't register candev (err=%d)\n", rc);
872d08e973aSMaximilian Schneider 		return ERR_PTR(rc);
873d08e973aSMaximilian Schneider 	}
874d08e973aSMaximilian Schneider 
875d08e973aSMaximilian Schneider 	return dev;
876d08e973aSMaximilian Schneider }
877d08e973aSMaximilian Schneider 
878d08e973aSMaximilian Schneider static void gs_destroy_candev(struct gs_can *dev)
879d08e973aSMaximilian Schneider {
880d08e973aSMaximilian Schneider 	unregister_candev(dev->netdev);
881d08e973aSMaximilian Schneider 	usb_kill_anchored_urbs(&dev->tx_submitted);
882e9a2d81bSMaximilain Schneider 	free_candev(dev->netdev);
883d08e973aSMaximilian Schneider }
884d08e973aSMaximilian Schneider 
88505ca5270SMaximilian Schneider static int gs_usb_probe(struct usb_interface *intf,
88605ca5270SMaximilian Schneider 			const struct usb_device_id *id)
887d08e973aSMaximilian Schneider {
888d08e973aSMaximilian Schneider 	struct gs_usb *dev;
889d08e973aSMaximilian Schneider 	int rc = -ENOMEM;
890d08e973aSMaximilian Schneider 	unsigned int icount, i;
891c919a306SEthan Zonca 	struct gs_host_config *hconf;
892c919a306SEthan Zonca 	struct gs_device_config *dconf;
893c919a306SEthan Zonca 
894c919a306SEthan Zonca 	hconf = kmalloc(sizeof(*hconf), GFP_KERNEL);
895c919a306SEthan Zonca 	if (!hconf)
896c919a306SEthan Zonca 		return -ENOMEM;
897c919a306SEthan Zonca 
8984ba1cb39SMarc Kleine-Budde 	hconf->byte_order = cpu_to_le32(0x0000beef);
899d08e973aSMaximilian Schneider 
900d08e973aSMaximilian Schneider 	/* send host config */
901d08e973aSMaximilian Schneider 	rc = usb_control_msg(interface_to_usbdev(intf),
902d08e973aSMaximilian Schneider 			     usb_sndctrlpipe(interface_to_usbdev(intf), 0),
903d08e973aSMaximilian Schneider 			     GS_USB_BREQ_HOST_FORMAT,
904d08e973aSMaximilian Schneider 			     USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
905c1ee7269SMarc Kleine-Budde 			     1, intf->cur_altsetting->desc.bInterfaceNumber,
906c1ee7269SMarc Kleine-Budde 			     hconf, sizeof(*hconf), 1000);
907d08e973aSMaximilian Schneider 
908c919a306SEthan Zonca 	kfree(hconf);
909c919a306SEthan Zonca 
910d08e973aSMaximilian Schneider 	if (rc < 0) {
911d0cd2aa8SMarc Kleine-Budde 		dev_err(&intf->dev, "Couldn't send data format (err=%d)\n", rc);
912d08e973aSMaximilian Schneider 		return rc;
913d08e973aSMaximilian Schneider 	}
914d08e973aSMaximilian Schneider 
915c919a306SEthan Zonca 	dconf = kmalloc(sizeof(*dconf), GFP_KERNEL);
916c919a306SEthan Zonca 	if (!dconf)
917c919a306SEthan Zonca 		return -ENOMEM;
918c919a306SEthan Zonca 
919d08e973aSMaximilian Schneider 	/* read device config */
920d08e973aSMaximilian Schneider 	rc = usb_control_msg(interface_to_usbdev(intf),
921d08e973aSMaximilian Schneider 			     usb_rcvctrlpipe(interface_to_usbdev(intf), 0),
922d08e973aSMaximilian Schneider 			     GS_USB_BREQ_DEVICE_CONFIG,
923d08e973aSMaximilian Schneider 			     USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
924c1ee7269SMarc Kleine-Budde 			     1, intf->cur_altsetting->desc.bInterfaceNumber,
925c1ee7269SMarc Kleine-Budde 			     dconf, sizeof(*dconf), 1000);
926d08e973aSMaximilian Schneider 	if (rc < 0) {
927d08e973aSMaximilian Schneider 		dev_err(&intf->dev, "Couldn't get device config: (err=%d)\n",
928d08e973aSMaximilian Schneider 			rc);
929c919a306SEthan Zonca 		kfree(dconf);
930d08e973aSMaximilian Schneider 		return rc;
931d08e973aSMaximilian Schneider 	}
932d08e973aSMaximilian Schneider 
933c919a306SEthan Zonca 	icount = dconf->icount + 1;
93410819466SMarc Kleine-Budde 	dev_info(&intf->dev, "Configuring for %u interfaces\n", icount);
935d08e973aSMaximilian Schneider 
936d08e973aSMaximilian Schneider 	if (icount > GS_MAX_INTF) {
937d08e973aSMaximilian Schneider 		dev_err(&intf->dev,
93810819466SMarc Kleine-Budde 			"Driver cannot handle more that %u CAN interfaces\n",
939d08e973aSMaximilian Schneider 			GS_MAX_INTF);
940c919a306SEthan Zonca 		kfree(dconf);
941d08e973aSMaximilian Schneider 		return -EINVAL;
942d08e973aSMaximilian Schneider 	}
943d08e973aSMaximilian Schneider 
944d08e973aSMaximilian Schneider 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
945c919a306SEthan Zonca 	if (!dev) {
946c919a306SEthan Zonca 		kfree(dconf);
94737920a74SColin Ian King 		return -ENOMEM;
948c919a306SEthan Zonca 	}
949c919a306SEthan Zonca 
950d08e973aSMaximilian Schneider 	init_usb_anchor(&dev->rx_submitted);
951d08e973aSMaximilian Schneider 
952d08e973aSMaximilian Schneider 	usb_set_intfdata(intf, dev);
953d08e973aSMaximilian Schneider 	dev->udev = interface_to_usbdev(intf);
954d08e973aSMaximilian Schneider 
955d08e973aSMaximilian Schneider 	for (i = 0; i < icount; i++) {
956c919a306SEthan Zonca 		dev->canch[i] = gs_make_candev(i, intf, dconf);
957d08e973aSMaximilian Schneider 		if (IS_ERR_OR_NULL(dev->canch[i])) {
958e9a2d81bSMaximilain Schneider 			/* save error code to return later */
959e9a2d81bSMaximilain Schneider 			rc = PTR_ERR(dev->canch[i]);
960e9a2d81bSMaximilain Schneider 
961d08e973aSMaximilian Schneider 			/* on failure destroy previously created candevs */
962d08e973aSMaximilian Schneider 			icount = i;
963e9a2d81bSMaximilain Schneider 			for (i = 0; i < icount; i++)
964d08e973aSMaximilian Schneider 				gs_destroy_candev(dev->canch[i]);
965e9a2d81bSMaximilain Schneider 
966e9a2d81bSMaximilain Schneider 			usb_kill_anchored_urbs(&dev->rx_submitted);
967c919a306SEthan Zonca 			kfree(dconf);
968d08e973aSMaximilian Schneider 			kfree(dev);
969d08e973aSMaximilian Schneider 			return rc;
970d08e973aSMaximilian Schneider 		}
971d08e973aSMaximilian Schneider 		dev->canch[i]->parent = dev;
972d08e973aSMaximilian Schneider 	}
973d08e973aSMaximilian Schneider 
974c919a306SEthan Zonca 	kfree(dconf);
975c919a306SEthan Zonca 
976d08e973aSMaximilian Schneider 	return 0;
977d08e973aSMaximilian Schneider }
978d08e973aSMaximilian Schneider 
979d08e973aSMaximilian Schneider static void gs_usb_disconnect(struct usb_interface *intf)
980d08e973aSMaximilian Schneider {
981d08e973aSMaximilian Schneider 	struct gs_usb *dev = usb_get_intfdata(intf);
982b9d9b030SMarc Kleine-Budde 	unsigned int i;
983b9d9b030SMarc Kleine-Budde 
984d08e973aSMaximilian Schneider 	usb_set_intfdata(intf, NULL);
985d08e973aSMaximilian Schneider 
986d08e973aSMaximilian Schneider 	if (!dev) {
987d08e973aSMaximilian Schneider 		dev_err(&intf->dev, "Disconnect (nodata)\n");
988d08e973aSMaximilian Schneider 		return;
989d08e973aSMaximilian Schneider 	}
990d08e973aSMaximilian Schneider 
991e9a2d81bSMaximilain Schneider 	for (i = 0; i < GS_MAX_INTF; i++)
992e9a2d81bSMaximilain Schneider 		if (dev->canch[i])
993e9a2d81bSMaximilain Schneider 			gs_destroy_candev(dev->canch[i]);
994d08e973aSMaximilian Schneider 
995d08e973aSMaximilian Schneider 	usb_kill_anchored_urbs(&dev->rx_submitted);
996e9a2d81bSMaximilain Schneider 	kfree(dev);
997d08e973aSMaximilian Schneider }
998d08e973aSMaximilian Schneider 
999d08e973aSMaximilian Schneider static const struct usb_device_id gs_usb_table[] = {
10002fe6c943SMaximilian Schneider 	{ USB_DEVICE_INTERFACE_NUMBER(USB_GSUSB_1_VENDOR_ID,
10012fe6c943SMaximilian Schneider 				      USB_GSUSB_1_PRODUCT_ID, 0) },
1002f155d9c0SMaximilian Schneider 	{ USB_DEVICE_INTERFACE_NUMBER(USB_CANDLELIGHT_VENDOR_ID,
1003f155d9c0SMaximilian Schneider 				      USB_CANDLELIGHT_PRODUCT_ID, 0) },
1004d08e973aSMaximilian Schneider 	{} /* Terminating entry */
1005d08e973aSMaximilian Schneider };
1006d08e973aSMaximilian Schneider 
1007d08e973aSMaximilian Schneider MODULE_DEVICE_TABLE(usb, gs_usb_table);
1008d08e973aSMaximilian Schneider 
1009d08e973aSMaximilian Schneider static struct usb_driver gs_usb_driver = {
1010d08e973aSMaximilian Schneider 	.name = "gs_usb",
1011d08e973aSMaximilian Schneider 	.probe = gs_usb_probe,
1012d08e973aSMaximilian Schneider 	.disconnect = gs_usb_disconnect,
1013d08e973aSMaximilian Schneider 	.id_table = gs_usb_table,
1014d08e973aSMaximilian Schneider };
1015d08e973aSMaximilian Schneider 
1016d08e973aSMaximilian Schneider module_usb_driver(gs_usb_driver);
1017d08e973aSMaximilian Schneider 
1018d08e973aSMaximilian Schneider MODULE_AUTHOR("Maximilian Schneider <mws@schneidersoft.net>");
1019d08e973aSMaximilian Schneider MODULE_DESCRIPTION(
1020d08e973aSMaximilian Schneider "Socket CAN device driver for Geschwister Schneider Technologie-, "
1021f155d9c0SMaximilian Schneider "Entwicklungs- und Vertriebs UG. USB2.0 to CAN interfaces\n"
1022f155d9c0SMaximilian Schneider "and bytewerk.org candleLight USB CAN interfaces.");
1023d08e973aSMaximilian Schneider MODULE_LICENSE("GPL v2");
1024