xref: /openbmc/linux/drivers/net/can/usb/gs_usb.c (revision c359931d2545173c4328d828643035259a0ba936)
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) */
1004643e34eSMarc 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)
1324643e34eSMarc 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 
149*c359931dSPeter Fink struct classic_can {
150*c359931dSPeter Fink 	u8 data[8];
151*c359931dSPeter Fink } __packed;
152*c359931dSPeter Fink 
153d08e973aSMaximilian Schneider struct gs_host_frame {
154d08e973aSMaximilian Schneider 	u32 echo_id;
1554ba1cb39SMarc Kleine-Budde 	__le32 can_id;
156d08e973aSMaximilian Schneider 
157d08e973aSMaximilian Schneider 	u8 can_dlc;
158d08e973aSMaximilian Schneider 	u8 channel;
159d08e973aSMaximilian Schneider 	u8 flags;
160d08e973aSMaximilian Schneider 	u8 reserved;
161d08e973aSMaximilian Schneider 
162*c359931dSPeter Fink 	union {
163*c359931dSPeter Fink 		DECLARE_FLEX_ARRAY(struct classic_can, classic_can);
164*c359931dSPeter Fink 	};
165d08e973aSMaximilian Schneider } __packed;
166d08e973aSMaximilian Schneider /* The GS USB devices make use of the same flags and masks as in
167d08e973aSMaximilian Schneider  * linux/can.h and linux/can/error.h, and no additional mapping is necessary.
168d08e973aSMaximilian Schneider  */
169d08e973aSMaximilian Schneider 
170d08e973aSMaximilian Schneider /* Only send a max of GS_MAX_TX_URBS frames per channel at a time. */
171d08e973aSMaximilian Schneider #define GS_MAX_TX_URBS 10
172d08e973aSMaximilian Schneider /* Only launch a max of GS_MAX_RX_URBS usb requests at a time. */
173d08e973aSMaximilian Schneider #define GS_MAX_RX_URBS 30
174d08e973aSMaximilian Schneider /* Maximum number of interfaces the driver supports per device.
175e10ab8b3SMarc Kleine-Budde  * Current hardware only supports 3 interfaces. The future may vary.
176d08e973aSMaximilian Schneider  */
177e10ab8b3SMarc Kleine-Budde #define GS_MAX_INTF 3
178d08e973aSMaximilian Schneider 
179d08e973aSMaximilian Schneider struct gs_tx_context {
180d08e973aSMaximilian Schneider 	struct gs_can *dev;
181d08e973aSMaximilian Schneider 	unsigned int echo_id;
182d08e973aSMaximilian Schneider };
183d08e973aSMaximilian Schneider 
184d08e973aSMaximilian Schneider struct gs_can {
185d08e973aSMaximilian Schneider 	struct can_priv can; /* must be the first member */
186d08e973aSMaximilian Schneider 
187d08e973aSMaximilian Schneider 	struct gs_usb *parent;
188d08e973aSMaximilian Schneider 
189d08e973aSMaximilian Schneider 	struct net_device *netdev;
190d08e973aSMaximilian Schneider 	struct usb_device *udev;
191d08e973aSMaximilian Schneider 	struct usb_interface *iface;
192d08e973aSMaximilian Schneider 
193d08e973aSMaximilian Schneider 	struct can_bittiming_const bt_const;
194d08e973aSMaximilian Schneider 	unsigned int channel;	/* channel number */
195d08e973aSMaximilian Schneider 
196*c359931dSPeter Fink 	unsigned int hf_size_tx;
197*c359931dSPeter Fink 
198ae421e33SNik Nyby 	/* This lock prevents a race condition between xmit and receive. */
199d08e973aSMaximilian Schneider 	spinlock_t tx_ctx_lock;
200d08e973aSMaximilian Schneider 	struct gs_tx_context tx_context[GS_MAX_TX_URBS];
201d08e973aSMaximilian Schneider 
202d08e973aSMaximilian Schneider 	struct usb_anchor tx_submitted;
203d08e973aSMaximilian Schneider 	atomic_t active_tx_urbs;
204d08e973aSMaximilian Schneider };
205d08e973aSMaximilian Schneider 
206d08e973aSMaximilian Schneider /* usb interface struct */
207d08e973aSMaximilian Schneider struct gs_usb {
208d08e973aSMaximilian Schneider 	struct gs_can *canch[GS_MAX_INTF];
209d08e973aSMaximilian Schneider 	struct usb_anchor rx_submitted;
210d08e973aSMaximilian Schneider 	struct usb_device *udev;
211*c359931dSPeter Fink 	unsigned int hf_size_rx;
212035b0fcfSVincent Mailhol 	u8 active_channels;
213d08e973aSMaximilian Schneider };
214d08e973aSMaximilian Schneider 
215d08e973aSMaximilian Schneider /* 'allocate' a tx context.
216d08e973aSMaximilian Schneider  * returns a valid tx context or NULL if there is no space.
217d08e973aSMaximilian Schneider  */
218d08e973aSMaximilian Schneider static struct gs_tx_context *gs_alloc_tx_context(struct gs_can *dev)
219d08e973aSMaximilian Schneider {
220d08e973aSMaximilian Schneider 	int i = 0;
221d08e973aSMaximilian Schneider 	unsigned long flags;
222d08e973aSMaximilian Schneider 
223d08e973aSMaximilian Schneider 	spin_lock_irqsave(&dev->tx_ctx_lock, flags);
224d08e973aSMaximilian Schneider 
225d08e973aSMaximilian Schneider 	for (; i < GS_MAX_TX_URBS; i++) {
226d08e973aSMaximilian Schneider 		if (dev->tx_context[i].echo_id == GS_MAX_TX_URBS) {
227d08e973aSMaximilian Schneider 			dev->tx_context[i].echo_id = i;
228d08e973aSMaximilian Schneider 			spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
229d08e973aSMaximilian Schneider 			return &dev->tx_context[i];
230d08e973aSMaximilian Schneider 		}
231d08e973aSMaximilian Schneider 	}
232d08e973aSMaximilian Schneider 
233d08e973aSMaximilian Schneider 	spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
234d08e973aSMaximilian Schneider 	return NULL;
235d08e973aSMaximilian Schneider }
236d08e973aSMaximilian Schneider 
237d08e973aSMaximilian Schneider /* releases a tx context
238d08e973aSMaximilian Schneider  */
239d08e973aSMaximilian Schneider static void gs_free_tx_context(struct gs_tx_context *txc)
240d08e973aSMaximilian Schneider {
241d08e973aSMaximilian Schneider 	txc->echo_id = GS_MAX_TX_URBS;
242d08e973aSMaximilian Schneider }
243d08e973aSMaximilian Schneider 
244d08e973aSMaximilian Schneider /* Get a tx context by id.
245d08e973aSMaximilian Schneider  */
2469be95c91SMaximilian Schneider static struct gs_tx_context *gs_get_tx_context(struct gs_can *dev,
2479be95c91SMaximilian Schneider 					       unsigned int id)
248d08e973aSMaximilian Schneider {
249d08e973aSMaximilian Schneider 	unsigned long flags;
250d08e973aSMaximilian Schneider 
251d08e973aSMaximilian Schneider 	if (id < GS_MAX_TX_URBS) {
252d08e973aSMaximilian Schneider 		spin_lock_irqsave(&dev->tx_ctx_lock, flags);
253d08e973aSMaximilian Schneider 		if (dev->tx_context[id].echo_id == id) {
254d08e973aSMaximilian Schneider 			spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
255d08e973aSMaximilian Schneider 			return &dev->tx_context[id];
256d08e973aSMaximilian Schneider 		}
257d08e973aSMaximilian Schneider 		spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
258d08e973aSMaximilian Schneider 	}
259d08e973aSMaximilian Schneider 	return NULL;
260d08e973aSMaximilian Schneider }
261d08e973aSMaximilian Schneider 
262dda8deb6SMarc Kleine-Budde static int gs_cmd_reset(struct gs_can *gsdev)
263d08e973aSMaximilian Schneider {
264d08e973aSMaximilian Schneider 	struct gs_device_mode *dm;
265d08e973aSMaximilian Schneider 	struct usb_interface *intf = gsdev->iface;
266d08e973aSMaximilian Schneider 	int rc;
267d08e973aSMaximilian Schneider 
268d08e973aSMaximilian Schneider 	dm = kzalloc(sizeof(*dm), GFP_KERNEL);
269d08e973aSMaximilian Schneider 	if (!dm)
270d08e973aSMaximilian Schneider 		return -ENOMEM;
271d08e973aSMaximilian Schneider 
272d08e973aSMaximilian Schneider 	dm->mode = GS_CAN_MODE_RESET;
273d08e973aSMaximilian Schneider 
274d08e973aSMaximilian Schneider 	rc = usb_control_msg(interface_to_usbdev(intf),
275d08e973aSMaximilian Schneider 			     usb_sndctrlpipe(interface_to_usbdev(intf), 0),
276d08e973aSMaximilian Schneider 			     GS_USB_BREQ_MODE,
277d08e973aSMaximilian Schneider 			     USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
278c1ee7269SMarc Kleine-Budde 			     gsdev->channel, 0, dm, sizeof(*dm), 1000);
279d08e973aSMaximilian Schneider 
2805cda3ee5SMarc Kleine-Budde 	kfree(dm);
2815cda3ee5SMarc Kleine-Budde 
282d08e973aSMaximilian Schneider 	return rc;
283d08e973aSMaximilian Schneider }
284d08e973aSMaximilian Schneider 
285d08e973aSMaximilian Schneider static void gs_update_state(struct gs_can *dev, struct can_frame *cf)
286d08e973aSMaximilian Schneider {
287d08e973aSMaximilian Schneider 	struct can_device_stats *can_stats = &dev->can.can_stats;
288d08e973aSMaximilian Schneider 
289d08e973aSMaximilian Schneider 	if (cf->can_id & CAN_ERR_RESTARTED) {
290d08e973aSMaximilian Schneider 		dev->can.state = CAN_STATE_ERROR_ACTIVE;
291d08e973aSMaximilian Schneider 		can_stats->restarts++;
292d08e973aSMaximilian Schneider 	} else if (cf->can_id & CAN_ERR_BUSOFF) {
293d08e973aSMaximilian Schneider 		dev->can.state = CAN_STATE_BUS_OFF;
294d08e973aSMaximilian Schneider 		can_stats->bus_off++;
295d08e973aSMaximilian Schneider 	} else if (cf->can_id & CAN_ERR_CRTL) {
296d08e973aSMaximilian Schneider 		if ((cf->data[1] & CAN_ERR_CRTL_TX_WARNING) ||
297d08e973aSMaximilian Schneider 		    (cf->data[1] & CAN_ERR_CRTL_RX_WARNING)) {
298d08e973aSMaximilian Schneider 			dev->can.state = CAN_STATE_ERROR_WARNING;
299d08e973aSMaximilian Schneider 			can_stats->error_warning++;
300d08e973aSMaximilian Schneider 		} else if ((cf->data[1] & CAN_ERR_CRTL_TX_PASSIVE) ||
301d08e973aSMaximilian Schneider 			   (cf->data[1] & CAN_ERR_CRTL_RX_PASSIVE)) {
302d08e973aSMaximilian Schneider 			dev->can.state = CAN_STATE_ERROR_PASSIVE;
303d08e973aSMaximilian Schneider 			can_stats->error_passive++;
304d08e973aSMaximilian Schneider 		} else {
305d08e973aSMaximilian Schneider 			dev->can.state = CAN_STATE_ERROR_ACTIVE;
306d08e973aSMaximilian Schneider 		}
307d08e973aSMaximilian Schneider 	}
308d08e973aSMaximilian Schneider }
309d08e973aSMaximilian Schneider 
310ae421e33SNik Nyby static void gs_usb_receive_bulk_callback(struct urb *urb)
311d08e973aSMaximilian Schneider {
312d08e973aSMaximilian Schneider 	struct gs_usb *usbcan = urb->context;
313d08e973aSMaximilian Schneider 	struct gs_can *dev;
314d08e973aSMaximilian Schneider 	struct net_device *netdev;
315d08e973aSMaximilian Schneider 	int rc;
316d08e973aSMaximilian Schneider 	struct net_device_stats *stats;
317d08e973aSMaximilian Schneider 	struct gs_host_frame *hf = urb->transfer_buffer;
318d08e973aSMaximilian Schneider 	struct gs_tx_context *txc;
319d08e973aSMaximilian Schneider 	struct can_frame *cf;
320d08e973aSMaximilian Schneider 	struct sk_buff *skb;
321d08e973aSMaximilian Schneider 
322d08e973aSMaximilian Schneider 	BUG_ON(!usbcan);
323d08e973aSMaximilian Schneider 
324d08e973aSMaximilian Schneider 	switch (urb->status) {
325d08e973aSMaximilian Schneider 	case 0: /* success */
326d08e973aSMaximilian Schneider 		break;
327d08e973aSMaximilian Schneider 	case -ENOENT:
328d08e973aSMaximilian Schneider 	case -ESHUTDOWN:
329d08e973aSMaximilian Schneider 		return;
330d08e973aSMaximilian Schneider 	default:
331d08e973aSMaximilian Schneider 		/* do not resubmit aborted urbs. eg: when device goes down */
332d08e973aSMaximilian Schneider 		return;
333d08e973aSMaximilian Schneider 	}
334d08e973aSMaximilian Schneider 
335d08e973aSMaximilian Schneider 	/* device reports out of range channel id */
336d08e973aSMaximilian Schneider 	if (hf->channel >= GS_MAX_INTF)
3374a8737ffSMarc Kleine-Budde 		goto device_detach;
338d08e973aSMaximilian Schneider 
339d08e973aSMaximilian Schneider 	dev = usbcan->canch[hf->channel];
340d08e973aSMaximilian Schneider 
341d08e973aSMaximilian Schneider 	netdev = dev->netdev;
342d08e973aSMaximilian Schneider 	stats = &netdev->stats;
343d08e973aSMaximilian Schneider 
344d08e973aSMaximilian Schneider 	if (!netif_device_present(netdev))
345d08e973aSMaximilian Schneider 		return;
346d08e973aSMaximilian Schneider 
347d08e973aSMaximilian Schneider 	if (hf->echo_id == -1) { /* normal rx */
348d08e973aSMaximilian Schneider 		skb = alloc_can_skb(dev->netdev, &cf);
349d08e973aSMaximilian Schneider 		if (!skb)
350d08e973aSMaximilian Schneider 			return;
351d08e973aSMaximilian Schneider 
3524ba1cb39SMarc Kleine-Budde 		cf->can_id = le32_to_cpu(hf->can_id);
353d08e973aSMaximilian Schneider 
3544c01fc87SOliver Hartkopp 		can_frame_set_cc_len(cf, hf->can_dlc, dev->can.ctrlmode);
355*c359931dSPeter Fink 		memcpy(cf->data, hf->classic_can->data, 8);
356d08e973aSMaximilian Schneider 
357d08e973aSMaximilian Schneider 		/* ERROR frames tell us information about the controller */
3584ba1cb39SMarc Kleine-Budde 		if (le32_to_cpu(hf->can_id) & CAN_ERR_FLAG)
359d08e973aSMaximilian Schneider 			gs_update_state(dev, cf);
360d08e973aSMaximilian Schneider 
361d08e973aSMaximilian Schneider 		netdev->stats.rx_packets++;
362d08e973aSMaximilian Schneider 		netdev->stats.rx_bytes += hf->can_dlc;
363d08e973aSMaximilian Schneider 
364d08e973aSMaximilian Schneider 		netif_rx(skb);
365d08e973aSMaximilian Schneider 	} else { /* echo_id == hf->echo_id */
366d08e973aSMaximilian Schneider 		if (hf->echo_id >= GS_MAX_TX_URBS) {
367d08e973aSMaximilian Schneider 			netdev_err(netdev,
36810819466SMarc Kleine-Budde 				   "Unexpected out of range echo id %u\n",
369d08e973aSMaximilian Schneider 				   hf->echo_id);
370d08e973aSMaximilian Schneider 			goto resubmit_urb;
371d08e973aSMaximilian Schneider 		}
372d08e973aSMaximilian Schneider 
373d08e973aSMaximilian Schneider 		txc = gs_get_tx_context(dev, hf->echo_id);
374d08e973aSMaximilian Schneider 
375d08e973aSMaximilian Schneider 		/* bad devices send bad echo_ids. */
376d08e973aSMaximilian Schneider 		if (!txc) {
377d08e973aSMaximilian Schneider 			netdev_err(netdev,
37810819466SMarc Kleine-Budde 				   "Unexpected unused echo id %u\n",
379d08e973aSMaximilian Schneider 				   hf->echo_id);
380d08e973aSMaximilian Schneider 			goto resubmit_urb;
381d08e973aSMaximilian Schneider 		}
382d08e973aSMaximilian Schneider 
383cc4b08c3SVincent Mailhol 		netdev->stats.tx_packets++;
384cc4b08c3SVincent Mailhol 		netdev->stats.tx_bytes += can_get_echo_skb(netdev, hf->echo_id,
385cc4b08c3SVincent Mailhol 							   NULL);
386d08e973aSMaximilian Schneider 
387d08e973aSMaximilian Schneider 		gs_free_tx_context(txc);
388d08e973aSMaximilian Schneider 
38997819f94SWolfgang Grandegger 		atomic_dec(&dev->active_tx_urbs);
39097819f94SWolfgang Grandegger 
391d08e973aSMaximilian Schneider 		netif_wake_queue(netdev);
392d08e973aSMaximilian Schneider 	}
393d08e973aSMaximilian Schneider 
394d08e973aSMaximilian Schneider 	if (hf->flags & GS_CAN_FLAG_OVERFLOW) {
395d08e973aSMaximilian Schneider 		skb = alloc_can_err_skb(netdev, &cf);
396d08e973aSMaximilian Schneider 		if (!skb)
397d08e973aSMaximilian Schneider 			goto resubmit_urb;
398d08e973aSMaximilian Schneider 
399d08e973aSMaximilian Schneider 		cf->can_id |= CAN_ERR_CRTL;
400c7b74967SOliver Hartkopp 		cf->len = CAN_ERR_DLC;
401d08e973aSMaximilian Schneider 		cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
402d08e973aSMaximilian Schneider 		stats->rx_over_errors++;
403d08e973aSMaximilian Schneider 		stats->rx_errors++;
404d08e973aSMaximilian Schneider 		netif_rx(skb);
405d08e973aSMaximilian Schneider 	}
406d08e973aSMaximilian Schneider 
407d08e973aSMaximilian Schneider  resubmit_urb:
408c1ee7269SMarc Kleine-Budde 	usb_fill_bulk_urb(urb, usbcan->udev,
409d08e973aSMaximilian Schneider 			  usb_rcvbulkpipe(usbcan->udev, GSUSB_ENDPOINT_IN),
410*c359931dSPeter Fink 			  hf, dev->parent->hf_size_rx,
411c1ee7269SMarc Kleine-Budde 			  gs_usb_receive_bulk_callback, usbcan);
412d08e973aSMaximilian Schneider 
413d08e973aSMaximilian Schneider 	rc = usb_submit_urb(urb, GFP_ATOMIC);
414d08e973aSMaximilian Schneider 
415d08e973aSMaximilian Schneider 	/* USB failure take down all interfaces */
416d08e973aSMaximilian Schneider 	if (rc == -ENODEV) {
4174a8737ffSMarc Kleine-Budde  device_detach:
418d08e973aSMaximilian Schneider 		for (rc = 0; rc < GS_MAX_INTF; rc++) {
419d08e973aSMaximilian Schneider 			if (usbcan->canch[rc])
420d08e973aSMaximilian Schneider 				netif_device_detach(usbcan->canch[rc]->netdev);
421d08e973aSMaximilian Schneider 		}
422d08e973aSMaximilian Schneider 	}
423d08e973aSMaximilian Schneider }
424d08e973aSMaximilian Schneider 
425d08e973aSMaximilian Schneider static int gs_usb_set_bittiming(struct net_device *netdev)
426d08e973aSMaximilian Schneider {
427d08e973aSMaximilian Schneider 	struct gs_can *dev = netdev_priv(netdev);
428d08e973aSMaximilian Schneider 	struct can_bittiming *bt = &dev->can.bittiming;
429d08e973aSMaximilian Schneider 	struct usb_interface *intf = dev->iface;
430d08e973aSMaximilian Schneider 	int rc;
431d08e973aSMaximilian Schneider 	struct gs_device_bittiming *dbt;
432d08e973aSMaximilian Schneider 
433d08e973aSMaximilian Schneider 	dbt = kmalloc(sizeof(*dbt), GFP_KERNEL);
434d08e973aSMaximilian Schneider 	if (!dbt)
435d08e973aSMaximilian Schneider 		return -ENOMEM;
436d08e973aSMaximilian Schneider 
4374ba1cb39SMarc Kleine-Budde 	dbt->prop_seg = cpu_to_le32(bt->prop_seg);
4384ba1cb39SMarc Kleine-Budde 	dbt->phase_seg1 = cpu_to_le32(bt->phase_seg1);
4394ba1cb39SMarc Kleine-Budde 	dbt->phase_seg2 = cpu_to_le32(bt->phase_seg2);
4404ba1cb39SMarc Kleine-Budde 	dbt->sjw = cpu_to_le32(bt->sjw);
4414ba1cb39SMarc Kleine-Budde 	dbt->brp = cpu_to_le32(bt->brp);
442d08e973aSMaximilian Schneider 
443d08e973aSMaximilian Schneider 	/* request bit timings */
444d08e973aSMaximilian Schneider 	rc = usb_control_msg(interface_to_usbdev(intf),
445d08e973aSMaximilian Schneider 			     usb_sndctrlpipe(interface_to_usbdev(intf), 0),
446d08e973aSMaximilian Schneider 			     GS_USB_BREQ_BITTIMING,
447d08e973aSMaximilian Schneider 			     USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
448c1ee7269SMarc Kleine-Budde 			     dev->channel, 0, dbt, sizeof(*dbt), 1000);
449d08e973aSMaximilian Schneider 
450d08e973aSMaximilian Schneider 	kfree(dbt);
451d08e973aSMaximilian Schneider 
452d08e973aSMaximilian Schneider 	if (rc < 0)
453d08e973aSMaximilian Schneider 		dev_err(netdev->dev.parent, "Couldn't set bittimings (err=%d)",
454d08e973aSMaximilian Schneider 			rc);
455d08e973aSMaximilian Schneider 
456d5b42e66SWolfgang Grandegger 	return (rc > 0) ? 0 : rc;
457d08e973aSMaximilian Schneider }
458d08e973aSMaximilian Schneider 
459d08e973aSMaximilian Schneider static void gs_usb_xmit_callback(struct urb *urb)
460d08e973aSMaximilian Schneider {
461d08e973aSMaximilian Schneider 	struct gs_tx_context *txc = urb->context;
462d08e973aSMaximilian Schneider 	struct gs_can *dev = txc->dev;
463d08e973aSMaximilian Schneider 	struct net_device *netdev = dev->netdev;
464d08e973aSMaximilian Schneider 
465d08e973aSMaximilian Schneider 	if (urb->status)
46610819466SMarc Kleine-Budde 		netdev_info(netdev, "usb xmit fail %u\n", txc->echo_id);
467d08e973aSMaximilian Schneider 
468c1ee7269SMarc Kleine-Budde 	usb_free_coherent(urb->dev, urb->transfer_buffer_length,
469c1ee7269SMarc Kleine-Budde 			  urb->transfer_buffer, urb->transfer_dma);
470d08e973aSMaximilian Schneider }
471d08e973aSMaximilian Schneider 
4729be95c91SMaximilian Schneider static netdev_tx_t gs_can_start_xmit(struct sk_buff *skb,
4739be95c91SMaximilian Schneider 				     struct net_device *netdev)
474d08e973aSMaximilian Schneider {
475d08e973aSMaximilian Schneider 	struct gs_can *dev = netdev_priv(netdev);
476d08e973aSMaximilian Schneider 	struct net_device_stats *stats = &dev->netdev->stats;
477d08e973aSMaximilian Schneider 	struct urb *urb;
478d08e973aSMaximilian Schneider 	struct gs_host_frame *hf;
479d08e973aSMaximilian Schneider 	struct can_frame *cf;
480d08e973aSMaximilian Schneider 	int rc;
481d08e973aSMaximilian Schneider 	unsigned int idx;
482d08e973aSMaximilian Schneider 	struct gs_tx_context *txc;
483d08e973aSMaximilian Schneider 
484d08e973aSMaximilian Schneider 	if (can_dropped_invalid_skb(netdev, skb))
485d08e973aSMaximilian Schneider 		return NETDEV_TX_OK;
486d08e973aSMaximilian Schneider 
487d08e973aSMaximilian Schneider 	/* find an empty context to keep track of transmission */
488d08e973aSMaximilian Schneider 	txc = gs_alloc_tx_context(dev);
489d08e973aSMaximilian Schneider 	if (!txc)
490d08e973aSMaximilian Schneider 		return NETDEV_TX_BUSY;
491d08e973aSMaximilian Schneider 
492d08e973aSMaximilian Schneider 	/* create a URB, and a buffer for it */
493d08e973aSMaximilian Schneider 	urb = usb_alloc_urb(0, GFP_ATOMIC);
4942a4a1e40SWolfram Sang 	if (!urb)
495d08e973aSMaximilian Schneider 		goto nomem_urb;
496d08e973aSMaximilian Schneider 
497*c359931dSPeter Fink 	hf = usb_alloc_coherent(dev->udev, dev->hf_size_tx, GFP_ATOMIC,
498d08e973aSMaximilian Schneider 				&urb->transfer_dma);
499d08e973aSMaximilian Schneider 	if (!hf) {
500d08e973aSMaximilian Schneider 		netdev_err(netdev, "No memory left for USB buffer\n");
501d08e973aSMaximilian Schneider 		goto nomem_hf;
502d08e973aSMaximilian Schneider 	}
503d08e973aSMaximilian Schneider 
504d08e973aSMaximilian Schneider 	idx = txc->echo_id;
505d08e973aSMaximilian Schneider 
506d08e973aSMaximilian Schneider 	if (idx >= GS_MAX_TX_URBS) {
50710819466SMarc Kleine-Budde 		netdev_err(netdev, "Invalid tx context %u\n", idx);
508d08e973aSMaximilian Schneider 		goto badidx;
509d08e973aSMaximilian Schneider 	}
510d08e973aSMaximilian Schneider 
511d08e973aSMaximilian Schneider 	hf->echo_id = idx;
512d08e973aSMaximilian Schneider 	hf->channel = dev->channel;
51389d58aebSBrian Silverman 	hf->flags = 0;
51489d58aebSBrian Silverman 	hf->reserved = 0;
515d08e973aSMaximilian Schneider 
516d08e973aSMaximilian Schneider 	cf = (struct can_frame *)skb->data;
517d08e973aSMaximilian Schneider 
5184ba1cb39SMarc Kleine-Budde 	hf->can_id = cpu_to_le32(cf->can_id);
5194c01fc87SOliver Hartkopp 	hf->can_dlc = can_get_cc_dlc(cf, dev->can.ctrlmode);
5204c01fc87SOliver Hartkopp 
521*c359931dSPeter Fink 	memcpy(hf->classic_can->data, cf->data, cf->len);
522d08e973aSMaximilian Schneider 
523d08e973aSMaximilian Schneider 	usb_fill_bulk_urb(urb, dev->udev,
524d08e973aSMaximilian Schneider 			  usb_sndbulkpipe(dev->udev, GSUSB_ENDPOINT_OUT),
525*c359931dSPeter Fink 			  hf, dev->hf_size_tx,
526c1ee7269SMarc Kleine-Budde 			  gs_usb_xmit_callback, txc);
527d08e973aSMaximilian Schneider 
528d08e973aSMaximilian Schneider 	urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
529d08e973aSMaximilian Schneider 	usb_anchor_urb(urb, &dev->tx_submitted);
530d08e973aSMaximilian Schneider 
5311dcb6e57SVincent Mailhol 	can_put_echo_skb(skb, netdev, idx, 0);
532d08e973aSMaximilian Schneider 
533d08e973aSMaximilian Schneider 	atomic_inc(&dev->active_tx_urbs);
534d08e973aSMaximilian Schneider 
535d08e973aSMaximilian Schneider 	rc = usb_submit_urb(urb, GFP_ATOMIC);
536d08e973aSMaximilian Schneider 	if (unlikely(rc)) {			/* usb send failed */
537d08e973aSMaximilian Schneider 		atomic_dec(&dev->active_tx_urbs);
538d08e973aSMaximilian Schneider 
539f318482aSMarc Kleine-Budde 		can_free_echo_skb(netdev, idx, NULL);
540d08e973aSMaximilian Schneider 		gs_free_tx_context(txc);
541d08e973aSMaximilian Schneider 
542d08e973aSMaximilian Schneider 		usb_unanchor_urb(urb);
543*c359931dSPeter Fink 		usb_free_coherent(dev->udev, urb->transfer_buffer_length,
544*c359931dSPeter Fink 				  urb->transfer_buffer, urb->transfer_dma);
545d08e973aSMaximilian Schneider 
546d08e973aSMaximilian Schneider 		if (rc == -ENODEV) {
547d08e973aSMaximilian Schneider 			netif_device_detach(netdev);
548d08e973aSMaximilian Schneider 		} else {
549d08e973aSMaximilian Schneider 			netdev_err(netdev, "usb_submit failed (err=%d)\n", rc);
550d08e973aSMaximilian Schneider 			stats->tx_dropped++;
551d08e973aSMaximilian Schneider 		}
552d08e973aSMaximilian Schneider 	} else {
553d08e973aSMaximilian Schneider 		/* Slow down tx path */
554d08e973aSMaximilian Schneider 		if (atomic_read(&dev->active_tx_urbs) >= GS_MAX_TX_URBS)
555d08e973aSMaximilian Schneider 			netif_stop_queue(netdev);
556d08e973aSMaximilian Schneider 	}
557d08e973aSMaximilian Schneider 
558d08e973aSMaximilian Schneider 	/* let usb core take care of this urb */
559d08e973aSMaximilian Schneider 	usb_free_urb(urb);
560d08e973aSMaximilian Schneider 
561d08e973aSMaximilian Schneider 	return NETDEV_TX_OK;
562d08e973aSMaximilian Schneider 
563d08e973aSMaximilian Schneider  badidx:
564*c359931dSPeter Fink 	usb_free_coherent(dev->udev, urb->transfer_buffer_length,
565*c359931dSPeter Fink 			  urb->transfer_buffer, urb->transfer_dma);
566d08e973aSMaximilian Schneider  nomem_hf:
567d08e973aSMaximilian Schneider 	usb_free_urb(urb);
568d08e973aSMaximilian Schneider 
569d08e973aSMaximilian Schneider  nomem_urb:
570d08e973aSMaximilian Schneider 	gs_free_tx_context(txc);
571d08e973aSMaximilian Schneider 	dev_kfree_skb(skb);
572d08e973aSMaximilian Schneider 	stats->tx_dropped++;
573d08e973aSMaximilian Schneider 	return NETDEV_TX_OK;
574d08e973aSMaximilian Schneider }
575d08e973aSMaximilian Schneider 
576d08e973aSMaximilian Schneider static int gs_can_open(struct net_device *netdev)
577d08e973aSMaximilian Schneider {
578d08e973aSMaximilian Schneider 	struct gs_can *dev = netdev_priv(netdev);
579d08e973aSMaximilian Schneider 	struct gs_usb *parent = dev->parent;
580d08e973aSMaximilian Schneider 	int rc, i;
581d08e973aSMaximilian Schneider 	struct gs_device_mode *dm;
582*c359931dSPeter Fink 	struct gs_host_frame *hf;
583d08e973aSMaximilian Schneider 	u32 ctrlmode;
5844ba1cb39SMarc Kleine-Budde 	u32 flags = 0;
585d08e973aSMaximilian Schneider 
586d08e973aSMaximilian Schneider 	rc = open_candev(netdev);
587d08e973aSMaximilian Schneider 	if (rc)
588d08e973aSMaximilian Schneider 		return rc;
589d08e973aSMaximilian Schneider 
590*c359931dSPeter Fink 	dev->hf_size_tx = struct_size(hf, classic_can, 1);
591*c359931dSPeter Fink 
592035b0fcfSVincent Mailhol 	if (!parent->active_channels) {
593d08e973aSMaximilian Schneider 		for (i = 0; i < GS_MAX_RX_URBS; i++) {
594d08e973aSMaximilian Schneider 			struct urb *urb;
595d08e973aSMaximilian Schneider 			u8 *buf;
596d08e973aSMaximilian Schneider 
597d08e973aSMaximilian Schneider 			/* alloc rx urb */
598d08e973aSMaximilian Schneider 			urb = usb_alloc_urb(0, GFP_KERNEL);
5992a4a1e40SWolfram Sang 			if (!urb)
600d08e973aSMaximilian Schneider 				return -ENOMEM;
601d08e973aSMaximilian Schneider 
602d08e973aSMaximilian Schneider 			/* alloc rx buffer */
603d08e973aSMaximilian Schneider 			buf = usb_alloc_coherent(dev->udev,
604*c359931dSPeter Fink 						 dev->parent->hf_size_rx,
605d08e973aSMaximilian Schneider 						 GFP_KERNEL,
606d08e973aSMaximilian Schneider 						 &urb->transfer_dma);
607d08e973aSMaximilian Schneider 			if (!buf) {
608d08e973aSMaximilian Schneider 				netdev_err(netdev,
609d08e973aSMaximilian Schneider 					   "No memory left for USB buffer\n");
610d08e973aSMaximilian Schneider 				usb_free_urb(urb);
611d08e973aSMaximilian Schneider 				return -ENOMEM;
612d08e973aSMaximilian Schneider 			}
613d08e973aSMaximilian Schneider 
614d08e973aSMaximilian Schneider 			/* fill, anchor, and submit rx urb */
615d08e973aSMaximilian Schneider 			usb_fill_bulk_urb(urb,
616d08e973aSMaximilian Schneider 					  dev->udev,
617d08e973aSMaximilian Schneider 					  usb_rcvbulkpipe(dev->udev,
618d08e973aSMaximilian Schneider 							  GSUSB_ENDPOINT_IN),
619d08e973aSMaximilian Schneider 					  buf,
620*c359931dSPeter Fink 					  dev->parent->hf_size_rx,
621c1ee7269SMarc Kleine-Budde 					  gs_usb_receive_bulk_callback, parent);
622d08e973aSMaximilian Schneider 			urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
623d08e973aSMaximilian Schneider 
624d08e973aSMaximilian Schneider 			usb_anchor_urb(urb, &parent->rx_submitted);
625d08e973aSMaximilian Schneider 
626d08e973aSMaximilian Schneider 			rc = usb_submit_urb(urb, GFP_KERNEL);
627d08e973aSMaximilian Schneider 			if (rc) {
628d08e973aSMaximilian Schneider 				if (rc == -ENODEV)
629d08e973aSMaximilian Schneider 					netif_device_detach(dev->netdev);
630d08e973aSMaximilian Schneider 
631d08e973aSMaximilian Schneider 				netdev_err(netdev,
632d0cd2aa8SMarc Kleine-Budde 					   "usb_submit failed (err=%d)\n", rc);
633d08e973aSMaximilian Schneider 
634d08e973aSMaximilian Schneider 				usb_unanchor_urb(urb);
635fb5be6a7SNavid Emamdoost 				usb_free_urb(urb);
636d08e973aSMaximilian Schneider 				break;
637d08e973aSMaximilian Schneider 			}
638d08e973aSMaximilian Schneider 
639d08e973aSMaximilian Schneider 			/* Drop reference,
640d08e973aSMaximilian Schneider 			 * USB core will take care of freeing it
641d08e973aSMaximilian Schneider 			 */
642d08e973aSMaximilian Schneider 			usb_free_urb(urb);
643d08e973aSMaximilian Schneider 		}
644d08e973aSMaximilian Schneider 	}
645d08e973aSMaximilian Schneider 
646d08e973aSMaximilian Schneider 	dm = kmalloc(sizeof(*dm), GFP_KERNEL);
647d08e973aSMaximilian Schneider 	if (!dm)
648d08e973aSMaximilian Schneider 		return -ENOMEM;
649d08e973aSMaximilian Schneider 
650d08e973aSMaximilian Schneider 	/* flags */
651d08e973aSMaximilian Schneider 	ctrlmode = dev->can.ctrlmode;
652d08e973aSMaximilian Schneider 
653d08e973aSMaximilian Schneider 	if (ctrlmode & CAN_CTRLMODE_LOOPBACK)
6544ba1cb39SMarc Kleine-Budde 		flags |= GS_CAN_MODE_LOOP_BACK;
655d08e973aSMaximilian Schneider 	else if (ctrlmode & CAN_CTRLMODE_LISTENONLY)
6564ba1cb39SMarc Kleine-Budde 		flags |= GS_CAN_MODE_LISTEN_ONLY;
657d08e973aSMaximilian Schneider 
658d08e973aSMaximilian Schneider 	/* Controller is not allowed to retry TX
659d08e973aSMaximilian Schneider 	 * this mode is unavailable on atmels uc3c hardware
660d08e973aSMaximilian Schneider 	 */
661d08e973aSMaximilian Schneider 	if (ctrlmode & CAN_CTRLMODE_ONE_SHOT)
6624ba1cb39SMarc Kleine-Budde 		flags |= GS_CAN_MODE_ONE_SHOT;
663d08e973aSMaximilian Schneider 
664d08e973aSMaximilian Schneider 	if (ctrlmode & CAN_CTRLMODE_3_SAMPLES)
6654ba1cb39SMarc Kleine-Budde 		flags |= GS_CAN_MODE_TRIPLE_SAMPLE;
666d08e973aSMaximilian Schneider 
667d08e973aSMaximilian Schneider 	/* finally start device */
6684ba1cb39SMarc Kleine-Budde 	dm->mode = cpu_to_le32(GS_CAN_MODE_START);
6694ba1cb39SMarc Kleine-Budde 	dm->flags = cpu_to_le32(flags);
670d08e973aSMaximilian Schneider 	rc = usb_control_msg(interface_to_usbdev(dev->iface),
671d08e973aSMaximilian Schneider 			     usb_sndctrlpipe(interface_to_usbdev(dev->iface), 0),
672d08e973aSMaximilian Schneider 			     GS_USB_BREQ_MODE,
673c1ee7269SMarc Kleine-Budde 			     USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
674c1ee7269SMarc Kleine-Budde 			     dev->channel, 0, dm, sizeof(*dm), 1000);
675d08e973aSMaximilian Schneider 
676d08e973aSMaximilian Schneider 	if (rc < 0) {
677d08e973aSMaximilian Schneider 		netdev_err(netdev, "Couldn't start device (err=%d)\n", rc);
678d08e973aSMaximilian Schneider 		kfree(dm);
679d08e973aSMaximilian Schneider 		return rc;
680d08e973aSMaximilian Schneider 	}
681d08e973aSMaximilian Schneider 
682d08e973aSMaximilian Schneider 	kfree(dm);
683d08e973aSMaximilian Schneider 
684d08e973aSMaximilian Schneider 	dev->can.state = CAN_STATE_ERROR_ACTIVE;
685d08e973aSMaximilian Schneider 
686035b0fcfSVincent Mailhol 	parent->active_channels++;
687d08e973aSMaximilian Schneider 	if (!(dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY))
688d08e973aSMaximilian Schneider 		netif_start_queue(netdev);
689d08e973aSMaximilian Schneider 
690d08e973aSMaximilian Schneider 	return 0;
691d08e973aSMaximilian Schneider }
692d08e973aSMaximilian Schneider 
693d08e973aSMaximilian Schneider static int gs_can_close(struct net_device *netdev)
694d08e973aSMaximilian Schneider {
695d08e973aSMaximilian Schneider 	int rc;
696d08e973aSMaximilian Schneider 	struct gs_can *dev = netdev_priv(netdev);
697d08e973aSMaximilian Schneider 	struct gs_usb *parent = dev->parent;
698d08e973aSMaximilian Schneider 
699d08e973aSMaximilian Schneider 	netif_stop_queue(netdev);
700d08e973aSMaximilian Schneider 
701d08e973aSMaximilian Schneider 	/* Stop polling */
702035b0fcfSVincent Mailhol 	parent->active_channels--;
703035b0fcfSVincent Mailhol 	if (!parent->active_channels)
704d08e973aSMaximilian Schneider 		usb_kill_anchored_urbs(&parent->rx_submitted);
705d08e973aSMaximilian Schneider 
706d08e973aSMaximilian Schneider 	/* Stop sending URBs */
707d08e973aSMaximilian Schneider 	usb_kill_anchored_urbs(&dev->tx_submitted);
708d08e973aSMaximilian Schneider 	atomic_set(&dev->active_tx_urbs, 0);
709d08e973aSMaximilian Schneider 
710d08e973aSMaximilian Schneider 	/* reset the device */
711dda8deb6SMarc Kleine-Budde 	rc = gs_cmd_reset(dev);
712d08e973aSMaximilian Schneider 	if (rc < 0)
713d08e973aSMaximilian Schneider 		netdev_warn(netdev, "Couldn't shutdown device (err=%d)", rc);
714d08e973aSMaximilian Schneider 
715d08e973aSMaximilian Schneider 	/* reset tx contexts */
716d08e973aSMaximilian Schneider 	for (rc = 0; rc < GS_MAX_TX_URBS; rc++) {
717d08e973aSMaximilian Schneider 		dev->tx_context[rc].dev = dev;
718d08e973aSMaximilian Schneider 		dev->tx_context[rc].echo_id = GS_MAX_TX_URBS;
719d08e973aSMaximilian Schneider 	}
720d08e973aSMaximilian Schneider 
721d08e973aSMaximilian Schneider 	/* close the netdev */
722d08e973aSMaximilian Schneider 	close_candev(netdev);
723d08e973aSMaximilian Schneider 
724d08e973aSMaximilian Schneider 	return 0;
725d08e973aSMaximilian Schneider }
726d08e973aSMaximilian Schneider 
727d08e973aSMaximilian Schneider static const struct net_device_ops gs_usb_netdev_ops = {
728d08e973aSMaximilian Schneider 	.ndo_open = gs_can_open,
729d08e973aSMaximilian Schneider 	.ndo_stop = gs_can_close,
730d08e973aSMaximilian Schneider 	.ndo_start_xmit = gs_can_start_xmit,
73150212b42SMarc Kleine-Budde 	.ndo_change_mtu = can_change_mtu,
732d08e973aSMaximilian Schneider };
733d08e973aSMaximilian Schneider 
73405ca5270SMaximilian Schneider static int gs_usb_set_identify(struct net_device *netdev, bool do_identify)
73505ca5270SMaximilian Schneider {
73605ca5270SMaximilian Schneider 	struct gs_can *dev = netdev_priv(netdev);
737b05c73bdSMaksim Salau 	struct gs_identify_mode *imode;
73805ca5270SMaximilian Schneider 	int rc;
73905ca5270SMaximilian Schneider 
740b05c73bdSMaksim Salau 	imode = kmalloc(sizeof(*imode), GFP_KERNEL);
741b05c73bdSMaksim Salau 
742b05c73bdSMaksim Salau 	if (!imode)
743b05c73bdSMaksim Salau 		return -ENOMEM;
744b05c73bdSMaksim Salau 
74505ca5270SMaximilian Schneider 	if (do_identify)
7464ba1cb39SMarc Kleine-Budde 		imode->mode = cpu_to_le32(GS_CAN_IDENTIFY_ON);
74705ca5270SMaximilian Schneider 	else
7484ba1cb39SMarc Kleine-Budde 		imode->mode = cpu_to_le32(GS_CAN_IDENTIFY_OFF);
74905ca5270SMaximilian Schneider 
75005ca5270SMaximilian Schneider 	rc = usb_control_msg(interface_to_usbdev(dev->iface),
751c1ee7269SMarc Kleine-Budde 			     usb_sndctrlpipe(interface_to_usbdev(dev->iface), 0),
75205ca5270SMaximilian Schneider 			     GS_USB_BREQ_IDENTIFY,
753c1ee7269SMarc Kleine-Budde 			     USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
754c1ee7269SMarc Kleine-Budde 			     dev->channel, 0, imode, sizeof(*imode), 100);
75505ca5270SMaximilian Schneider 
756b05c73bdSMaksim Salau 	kfree(imode);
757b05c73bdSMaksim Salau 
75805ca5270SMaximilian Schneider 	return (rc > 0) ? 0 : rc;
75905ca5270SMaximilian Schneider }
76005ca5270SMaximilian Schneider 
76105ca5270SMaximilian Schneider /* blink LED's for finding the this interface */
76205ca5270SMaximilian Schneider static int gs_usb_set_phys_id(struct net_device *dev,
76305ca5270SMaximilian Schneider 			      enum ethtool_phys_id_state state)
76405ca5270SMaximilian Schneider {
76505ca5270SMaximilian Schneider 	int rc = 0;
76605ca5270SMaximilian Schneider 
76705ca5270SMaximilian Schneider 	switch (state) {
76805ca5270SMaximilian Schneider 	case ETHTOOL_ID_ACTIVE:
76905ca5270SMaximilian Schneider 		rc = gs_usb_set_identify(dev, GS_CAN_IDENTIFY_ON);
77005ca5270SMaximilian Schneider 		break;
77105ca5270SMaximilian Schneider 	case ETHTOOL_ID_INACTIVE:
77205ca5270SMaximilian Schneider 		rc = gs_usb_set_identify(dev, GS_CAN_IDENTIFY_OFF);
77305ca5270SMaximilian Schneider 		break;
77405ca5270SMaximilian Schneider 	default:
77505ca5270SMaximilian Schneider 		break;
77605ca5270SMaximilian Schneider 	}
77705ca5270SMaximilian Schneider 
77805ca5270SMaximilian Schneider 	return rc;
77905ca5270SMaximilian Schneider }
78005ca5270SMaximilian Schneider 
78105ca5270SMaximilian Schneider static const struct ethtool_ops gs_usb_ethtool_ops = {
78205ca5270SMaximilian Schneider 	.set_phys_id = gs_usb_set_phys_id,
78305ca5270SMaximilian Schneider };
78405ca5270SMaximilian Schneider 
78505ca5270SMaximilian Schneider static struct gs_can *gs_make_candev(unsigned int channel,
78605ca5270SMaximilian Schneider 				     struct usb_interface *intf,
78705ca5270SMaximilian Schneider 				     struct gs_device_config *dconf)
788d08e973aSMaximilian Schneider {
789d08e973aSMaximilian Schneider 	struct gs_can *dev;
790d08e973aSMaximilian Schneider 	struct net_device *netdev;
791d08e973aSMaximilian Schneider 	int rc;
792d08e973aSMaximilian Schneider 	struct gs_device_bt_const *bt_const;
7934ba1cb39SMarc Kleine-Budde 	u32 feature;
794d08e973aSMaximilian Schneider 
795d08e973aSMaximilian Schneider 	bt_const = kmalloc(sizeof(*bt_const), GFP_KERNEL);
796d08e973aSMaximilian Schneider 	if (!bt_const)
797d08e973aSMaximilian Schneider 		return ERR_PTR(-ENOMEM);
798d08e973aSMaximilian Schneider 
799d08e973aSMaximilian Schneider 	/* fetch bit timing constants */
800d08e973aSMaximilian Schneider 	rc = usb_control_msg(interface_to_usbdev(intf),
801d08e973aSMaximilian Schneider 			     usb_rcvctrlpipe(interface_to_usbdev(intf), 0),
802d08e973aSMaximilian Schneider 			     GS_USB_BREQ_BT_CONST,
803d08e973aSMaximilian Schneider 			     USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
804c1ee7269SMarc Kleine-Budde 			     channel, 0, bt_const, sizeof(*bt_const), 1000);
805d08e973aSMaximilian Schneider 
806d08e973aSMaximilian Schneider 	if (rc < 0) {
807d08e973aSMaximilian Schneider 		dev_err(&intf->dev,
808d08e973aSMaximilian Schneider 			"Couldn't get bit timing const for channel (err=%d)\n",
809d08e973aSMaximilian Schneider 			rc);
810d08e973aSMaximilian Schneider 		kfree(bt_const);
811d08e973aSMaximilian Schneider 		return ERR_PTR(rc);
812d08e973aSMaximilian Schneider 	}
813d08e973aSMaximilian Schneider 
814d08e973aSMaximilian Schneider 	/* create netdev */
815d08e973aSMaximilian Schneider 	netdev = alloc_candev(sizeof(struct gs_can), GS_MAX_TX_URBS);
816d08e973aSMaximilian Schneider 	if (!netdev) {
817d08e973aSMaximilian Schneider 		dev_err(&intf->dev, "Couldn't allocate candev\n");
818d08e973aSMaximilian Schneider 		kfree(bt_const);
819d08e973aSMaximilian Schneider 		return ERR_PTR(-ENOMEM);
820d08e973aSMaximilian Schneider 	}
821d08e973aSMaximilian Schneider 
822d08e973aSMaximilian Schneider 	dev = netdev_priv(netdev);
823d08e973aSMaximilian Schneider 
824d08e973aSMaximilian Schneider 	netdev->netdev_ops = &gs_usb_netdev_ops;
825d08e973aSMaximilian Schneider 
826d08e973aSMaximilian Schneider 	netdev->flags |= IFF_ECHO; /* we support full roundtrip echo */
827d08e973aSMaximilian Schneider 
82888bfb9a7SMarc Kleine-Budde 	/* dev setup */
829d08e973aSMaximilian Schneider 	strcpy(dev->bt_const.name, "gs_usb");
8304ba1cb39SMarc Kleine-Budde 	dev->bt_const.tseg1_min = le32_to_cpu(bt_const->tseg1_min);
8314ba1cb39SMarc Kleine-Budde 	dev->bt_const.tseg1_max = le32_to_cpu(bt_const->tseg1_max);
8324ba1cb39SMarc Kleine-Budde 	dev->bt_const.tseg2_min = le32_to_cpu(bt_const->tseg2_min);
8334ba1cb39SMarc Kleine-Budde 	dev->bt_const.tseg2_max = le32_to_cpu(bt_const->tseg2_max);
8344ba1cb39SMarc Kleine-Budde 	dev->bt_const.sjw_max = le32_to_cpu(bt_const->sjw_max);
8354ba1cb39SMarc Kleine-Budde 	dev->bt_const.brp_min = le32_to_cpu(bt_const->brp_min);
8364ba1cb39SMarc Kleine-Budde 	dev->bt_const.brp_max = le32_to_cpu(bt_const->brp_max);
8374ba1cb39SMarc Kleine-Budde 	dev->bt_const.brp_inc = le32_to_cpu(bt_const->brp_inc);
838d08e973aSMaximilian Schneider 
839d08e973aSMaximilian Schneider 	dev->udev = interface_to_usbdev(intf);
840d08e973aSMaximilian Schneider 	dev->iface = intf;
841d08e973aSMaximilian Schneider 	dev->netdev = netdev;
842d08e973aSMaximilian Schneider 	dev->channel = channel;
843d08e973aSMaximilian Schneider 
844d08e973aSMaximilian Schneider 	init_usb_anchor(&dev->tx_submitted);
845d08e973aSMaximilian Schneider 	atomic_set(&dev->active_tx_urbs, 0);
846d08e973aSMaximilian Schneider 	spin_lock_init(&dev->tx_ctx_lock);
847d08e973aSMaximilian Schneider 	for (rc = 0; rc < GS_MAX_TX_URBS; rc++) {
848d08e973aSMaximilian Schneider 		dev->tx_context[rc].dev = dev;
849d08e973aSMaximilian Schneider 		dev->tx_context[rc].echo_id = GS_MAX_TX_URBS;
850d08e973aSMaximilian Schneider 	}
851d08e973aSMaximilian Schneider 
85288bfb9a7SMarc Kleine-Budde 	/* can setup */
853d08e973aSMaximilian Schneider 	dev->can.state = CAN_STATE_STOPPED;
8544ba1cb39SMarc Kleine-Budde 	dev->can.clock.freq = le32_to_cpu(bt_const->fclk_can);
855d08e973aSMaximilian Schneider 	dev->can.bittiming_const = &dev->bt_const;
856d08e973aSMaximilian Schneider 	dev->can.do_set_bittiming = gs_usb_set_bittiming;
857d08e973aSMaximilian Schneider 
8584c01fc87SOliver Hartkopp 	dev->can.ctrlmode_supported = CAN_CTRLMODE_CC_LEN8_DLC;
859d08e973aSMaximilian Schneider 
8604ba1cb39SMarc Kleine-Budde 	feature = le32_to_cpu(bt_const->feature);
8614ba1cb39SMarc Kleine-Budde 	if (feature & GS_CAN_FEATURE_LISTEN_ONLY)
862d08e973aSMaximilian Schneider 		dev->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY;
863d08e973aSMaximilian Schneider 
8644ba1cb39SMarc Kleine-Budde 	if (feature & GS_CAN_FEATURE_LOOP_BACK)
865d08e973aSMaximilian Schneider 		dev->can.ctrlmode_supported |= CAN_CTRLMODE_LOOPBACK;
866d08e973aSMaximilian Schneider 
8674ba1cb39SMarc Kleine-Budde 	if (feature & GS_CAN_FEATURE_TRIPLE_SAMPLE)
868d08e973aSMaximilian Schneider 		dev->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
869d08e973aSMaximilian Schneider 
8704ba1cb39SMarc Kleine-Budde 	if (feature & GS_CAN_FEATURE_ONE_SHOT)
871d08e973aSMaximilian Schneider 		dev->can.ctrlmode_supported |= CAN_CTRLMODE_ONE_SHOT;
872d08e973aSMaximilian Schneider 
8734ba1cb39SMarc Kleine-Budde 	if (le32_to_cpu(dconf->sw_version) > 1)
8744ba1cb39SMarc Kleine-Budde 		if (feature & GS_CAN_FEATURE_IDENTIFY)
87505ca5270SMaximilian Schneider 			netdev->ethtool_ops = &gs_usb_ethtool_ops;
87605ca5270SMaximilian Schneider 
87705ca5270SMaximilian Schneider 	kfree(bt_const);
87805ca5270SMaximilian Schneider 
879e0d25759SMarc Kleine-Budde 	SET_NETDEV_DEV(netdev, &intf->dev);
880e0d25759SMarc Kleine-Budde 
881d08e973aSMaximilian Schneider 	rc = register_candev(dev->netdev);
882d08e973aSMaximilian Schneider 	if (rc) {
883d08e973aSMaximilian Schneider 		free_candev(dev->netdev);
884d08e973aSMaximilian Schneider 		dev_err(&intf->dev, "Couldn't register candev (err=%d)\n", rc);
885d08e973aSMaximilian Schneider 		return ERR_PTR(rc);
886d08e973aSMaximilian Schneider 	}
887d08e973aSMaximilian Schneider 
888d08e973aSMaximilian Schneider 	return dev;
889d08e973aSMaximilian Schneider }
890d08e973aSMaximilian Schneider 
891d08e973aSMaximilian Schneider static void gs_destroy_candev(struct gs_can *dev)
892d08e973aSMaximilian Schneider {
893d08e973aSMaximilian Schneider 	unregister_candev(dev->netdev);
894d08e973aSMaximilian Schneider 	usb_kill_anchored_urbs(&dev->tx_submitted);
895e9a2d81bSMaximilain Schneider 	free_candev(dev->netdev);
896d08e973aSMaximilian Schneider }
897d08e973aSMaximilian Schneider 
89805ca5270SMaximilian Schneider static int gs_usb_probe(struct usb_interface *intf,
89905ca5270SMaximilian Schneider 			const struct usb_device_id *id)
900d08e973aSMaximilian Schneider {
9015374d083SMarc Kleine-Budde 	struct usb_device *udev = interface_to_usbdev(intf);
902*c359931dSPeter Fink 	struct gs_host_frame *hf;
903d08e973aSMaximilian Schneider 	struct gs_usb *dev;
904d08e973aSMaximilian Schneider 	int rc = -ENOMEM;
905d08e973aSMaximilian Schneider 	unsigned int icount, i;
906c919a306SEthan Zonca 	struct gs_host_config *hconf;
907c919a306SEthan Zonca 	struct gs_device_config *dconf;
908c919a306SEthan Zonca 
909c919a306SEthan Zonca 	hconf = kmalloc(sizeof(*hconf), GFP_KERNEL);
910c919a306SEthan Zonca 	if (!hconf)
911c919a306SEthan Zonca 		return -ENOMEM;
912c919a306SEthan Zonca 
9134ba1cb39SMarc Kleine-Budde 	hconf->byte_order = cpu_to_le32(0x0000beef);
914d08e973aSMaximilian Schneider 
915d08e973aSMaximilian Schneider 	/* send host config */
9165374d083SMarc Kleine-Budde 	rc = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
917d08e973aSMaximilian Schneider 			     GS_USB_BREQ_HOST_FORMAT,
918d08e973aSMaximilian Schneider 			     USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
919c1ee7269SMarc Kleine-Budde 			     1, intf->cur_altsetting->desc.bInterfaceNumber,
920c1ee7269SMarc Kleine-Budde 			     hconf, sizeof(*hconf), 1000);
921d08e973aSMaximilian Schneider 
922c919a306SEthan Zonca 	kfree(hconf);
923c919a306SEthan Zonca 
924d08e973aSMaximilian Schneider 	if (rc < 0) {
925d0cd2aa8SMarc Kleine-Budde 		dev_err(&intf->dev, "Couldn't send data format (err=%d)\n", rc);
926d08e973aSMaximilian Schneider 		return rc;
927d08e973aSMaximilian Schneider 	}
928d08e973aSMaximilian Schneider 
929c919a306SEthan Zonca 	dconf = kmalloc(sizeof(*dconf), GFP_KERNEL);
930c919a306SEthan Zonca 	if (!dconf)
931c919a306SEthan Zonca 		return -ENOMEM;
932c919a306SEthan Zonca 
933d08e973aSMaximilian Schneider 	/* read device config */
9345374d083SMarc Kleine-Budde 	rc = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
935d08e973aSMaximilian Schneider 			     GS_USB_BREQ_DEVICE_CONFIG,
936d08e973aSMaximilian Schneider 			     USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
937c1ee7269SMarc Kleine-Budde 			     1, intf->cur_altsetting->desc.bInterfaceNumber,
938c1ee7269SMarc Kleine-Budde 			     dconf, sizeof(*dconf), 1000);
939d08e973aSMaximilian Schneider 	if (rc < 0) {
940d08e973aSMaximilian Schneider 		dev_err(&intf->dev, "Couldn't get device config: (err=%d)\n",
941d08e973aSMaximilian Schneider 			rc);
942c919a306SEthan Zonca 		kfree(dconf);
943d08e973aSMaximilian Schneider 		return rc;
944d08e973aSMaximilian Schneider 	}
945d08e973aSMaximilian Schneider 
946c919a306SEthan Zonca 	icount = dconf->icount + 1;
94710819466SMarc Kleine-Budde 	dev_info(&intf->dev, "Configuring for %u interfaces\n", icount);
948d08e973aSMaximilian Schneider 
949d08e973aSMaximilian Schneider 	if (icount > GS_MAX_INTF) {
950d08e973aSMaximilian Schneider 		dev_err(&intf->dev,
95110819466SMarc Kleine-Budde 			"Driver cannot handle more that %u CAN interfaces\n",
952d08e973aSMaximilian Schneider 			GS_MAX_INTF);
953c919a306SEthan Zonca 		kfree(dconf);
954d08e973aSMaximilian Schneider 		return -EINVAL;
955d08e973aSMaximilian Schneider 	}
956d08e973aSMaximilian Schneider 
957d08e973aSMaximilian Schneider 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
958c919a306SEthan Zonca 	if (!dev) {
959c919a306SEthan Zonca 		kfree(dconf);
96037920a74SColin Ian King 		return -ENOMEM;
961c919a306SEthan Zonca 	}
962c919a306SEthan Zonca 
963d08e973aSMaximilian Schneider 	init_usb_anchor(&dev->rx_submitted);
964*c359931dSPeter Fink 	dev->hf_size_rx = struct_size(hf, classic_can, 1);
965d08e973aSMaximilian Schneider 
966d08e973aSMaximilian Schneider 	usb_set_intfdata(intf, dev);
9675374d083SMarc Kleine-Budde 	dev->udev = udev;
968d08e973aSMaximilian Schneider 
969d08e973aSMaximilian Schneider 	for (i = 0; i < icount; i++) {
970c919a306SEthan Zonca 		dev->canch[i] = gs_make_candev(i, intf, dconf);
971d08e973aSMaximilian Schneider 		if (IS_ERR_OR_NULL(dev->canch[i])) {
972e9a2d81bSMaximilain Schneider 			/* save error code to return later */
973e9a2d81bSMaximilain Schneider 			rc = PTR_ERR(dev->canch[i]);
974e9a2d81bSMaximilain Schneider 
975d08e973aSMaximilian Schneider 			/* on failure destroy previously created candevs */
976d08e973aSMaximilian Schneider 			icount = i;
977e9a2d81bSMaximilain Schneider 			for (i = 0; i < icount; i++)
978d08e973aSMaximilian Schneider 				gs_destroy_candev(dev->canch[i]);
979e9a2d81bSMaximilain Schneider 
980e9a2d81bSMaximilain Schneider 			usb_kill_anchored_urbs(&dev->rx_submitted);
981c919a306SEthan Zonca 			kfree(dconf);
982d08e973aSMaximilian Schneider 			kfree(dev);
983d08e973aSMaximilian Schneider 			return rc;
984d08e973aSMaximilian Schneider 		}
985d08e973aSMaximilian Schneider 		dev->canch[i]->parent = dev;
986d08e973aSMaximilian Schneider 	}
987d08e973aSMaximilian Schneider 
988c919a306SEthan Zonca 	kfree(dconf);
989c919a306SEthan Zonca 
990d08e973aSMaximilian Schneider 	return 0;
991d08e973aSMaximilian Schneider }
992d08e973aSMaximilian Schneider 
993d08e973aSMaximilian Schneider static void gs_usb_disconnect(struct usb_interface *intf)
994d08e973aSMaximilian Schneider {
995d08e973aSMaximilian Schneider 	struct gs_usb *dev = usb_get_intfdata(intf);
996b9d9b030SMarc Kleine-Budde 	unsigned int i;
997b9d9b030SMarc Kleine-Budde 
998d08e973aSMaximilian Schneider 	usb_set_intfdata(intf, NULL);
999d08e973aSMaximilian Schneider 
1000d08e973aSMaximilian Schneider 	if (!dev) {
1001d08e973aSMaximilian Schneider 		dev_err(&intf->dev, "Disconnect (nodata)\n");
1002d08e973aSMaximilian Schneider 		return;
1003d08e973aSMaximilian Schneider 	}
1004d08e973aSMaximilian Schneider 
1005e9a2d81bSMaximilain Schneider 	for (i = 0; i < GS_MAX_INTF; i++)
1006e9a2d81bSMaximilain Schneider 		if (dev->canch[i])
1007e9a2d81bSMaximilain Schneider 			gs_destroy_candev(dev->canch[i]);
1008d08e973aSMaximilian Schneider 
1009d08e973aSMaximilian Schneider 	usb_kill_anchored_urbs(&dev->rx_submitted);
1010e9a2d81bSMaximilain Schneider 	kfree(dev);
1011d08e973aSMaximilian Schneider }
1012d08e973aSMaximilian Schneider 
1013d08e973aSMaximilian Schneider static const struct usb_device_id gs_usb_table[] = {
10142fe6c943SMaximilian Schneider 	{ USB_DEVICE_INTERFACE_NUMBER(USB_GSUSB_1_VENDOR_ID,
10152fe6c943SMaximilian Schneider 				      USB_GSUSB_1_PRODUCT_ID, 0) },
1016f155d9c0SMaximilian Schneider 	{ USB_DEVICE_INTERFACE_NUMBER(USB_CANDLELIGHT_VENDOR_ID,
1017f155d9c0SMaximilian Schneider 				      USB_CANDLELIGHT_PRODUCT_ID, 0) },
1018d08e973aSMaximilian Schneider 	{} /* Terminating entry */
1019d08e973aSMaximilian Schneider };
1020d08e973aSMaximilian Schneider 
1021d08e973aSMaximilian Schneider MODULE_DEVICE_TABLE(usb, gs_usb_table);
1022d08e973aSMaximilian Schneider 
1023d08e973aSMaximilian Schneider static struct usb_driver gs_usb_driver = {
1024d08e973aSMaximilian Schneider 	.name = "gs_usb",
1025d08e973aSMaximilian Schneider 	.probe = gs_usb_probe,
1026d08e973aSMaximilian Schneider 	.disconnect = gs_usb_disconnect,
1027d08e973aSMaximilian Schneider 	.id_table = gs_usb_table,
1028d08e973aSMaximilian Schneider };
1029d08e973aSMaximilian Schneider 
1030d08e973aSMaximilian Schneider module_usb_driver(gs_usb_driver);
1031d08e973aSMaximilian Schneider 
1032d08e973aSMaximilian Schneider MODULE_AUTHOR("Maximilian Schneider <mws@schneidersoft.net>");
1033d08e973aSMaximilian Schneider MODULE_DESCRIPTION(
1034d08e973aSMaximilian Schneider "Socket CAN device driver for Geschwister Schneider Technologie-, "
1035f155d9c0SMaximilian Schneider "Entwicklungs- und Vertriebs UG. USB2.0 to CAN interfaces\n"
1036f155d9c0SMaximilian Schneider "and bytewerk.org candleLight USB CAN interfaces.");
1037d08e973aSMaximilian Schneider MODULE_LICENSE("GPL v2");
1038