• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 
3 /* Driver for ETAS GmbH ES58X USB CAN(-FD) Bus Interfaces.
4  *
5  * File es58x_core.c: Core logic to manage the network devices and the
6  * USB interface.
7  *
8  * Copyright (c) 2019 Robert Bosch Engineering and Business Solutions. All rights reserved.
9  * Copyright (c) 2020 ETAS K.K.. All rights reserved.
10  * Copyright (c) 2020, 2021 Vincent Mailhol <mailhol.vincent@wanadoo.fr>
11  */
12 
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/usb.h>
16 #include <linux/crc16.h>
17 #include <asm/unaligned.h>
18 
19 #include "es58x_core.h"
20 
21 #define DRV_VERSION "1.00"
22 MODULE_AUTHOR("Vincent Mailhol <mailhol.vincent@wanadoo.fr>");
23 MODULE_AUTHOR("Arunachalam Santhanam <arunachalam.santhanam@in.bosch.com>");
24 MODULE_DESCRIPTION("Socket CAN driver for ETAS ES58X USB adapters");
25 MODULE_VERSION(DRV_VERSION);
26 MODULE_LICENSE("GPL v2");
27 
28 #define ES58X_MODULE_NAME "etas_es58x"
29 #define ES58X_VENDOR_ID 0x108C
30 #define ES581_4_PRODUCT_ID 0x0159
31 #define ES582_1_PRODUCT_ID 0x0168
32 #define ES584_1_PRODUCT_ID 0x0169
33 
34 /* ES58X FD has some interface protocols unsupported by this driver. */
35 #define ES58X_FD_INTERFACE_PROTOCOL 0
36 
37 /* Table of devices which work with this driver. */
38 static const struct usb_device_id es58x_id_table[] = {
39 	{
40 		/* ETAS GmbH ES581.4 USB dual-channel CAN Bus Interface module. */
41 		USB_DEVICE(ES58X_VENDOR_ID, ES581_4_PRODUCT_ID),
42 		.driver_info = ES58X_DUAL_CHANNEL
43 	}, {
44 		/* ETAS GmbH ES582.1 USB dual-channel CAN FD Bus Interface module. */
45 		USB_DEVICE_INTERFACE_PROTOCOL(ES58X_VENDOR_ID, ES582_1_PRODUCT_ID,
46 					      ES58X_FD_INTERFACE_PROTOCOL),
47 		.driver_info = ES58X_DUAL_CHANNEL | ES58X_FD_FAMILY
48 	}, {
49 		/* ETAS GmbH ES584.1 USB single-channel CAN FD Bus Interface module. */
50 		USB_DEVICE_INTERFACE_PROTOCOL(ES58X_VENDOR_ID, ES584_1_PRODUCT_ID,
51 					      ES58X_FD_INTERFACE_PROTOCOL),
52 		.driver_info = ES58X_FD_FAMILY
53 	}, {
54 		/* Terminating entry */
55 	}
56 };
57 
58 MODULE_DEVICE_TABLE(usb, es58x_id_table);
59 
60 #define es58x_print_hex_dump(buf, len)					\
61 	print_hex_dump(KERN_DEBUG,					\
62 		       ES58X_MODULE_NAME " " __stringify(buf) ": ",	\
63 		       DUMP_PREFIX_NONE, 16, 1, buf, len, false)
64 
65 #define es58x_print_hex_dump_debug(buf, len)				 \
66 	print_hex_dump_debug(ES58X_MODULE_NAME " " __stringify(buf) ": ",\
67 			     DUMP_PREFIX_NONE, 16, 1, buf, len, false)
68 
69 /* The last two bytes of an ES58X command is a CRC16. The first two
70  * bytes (the start of frame) are skipped and the CRC calculation
71  * starts on the third byte.
72  */
73 #define ES58X_CRC_CALC_OFFSET sizeof_field(union es58x_urb_cmd, sof)
74 
75 /**
76  * es58x_calculate_crc() - Compute the crc16 of a given URB.
77  * @urb_cmd: The URB command for which we want to calculate the CRC.
78  * @urb_len: Length of @urb_cmd. Must be at least bigger than 4
79  *	(ES58X_CRC_CALC_OFFSET + sizeof(crc))
80  *
81  * Return: crc16 value.
82  */
es58x_calculate_crc(const union es58x_urb_cmd * urb_cmd,u16 urb_len)83 static u16 es58x_calculate_crc(const union es58x_urb_cmd *urb_cmd, u16 urb_len)
84 {
85 	u16 crc;
86 	ssize_t len = urb_len - ES58X_CRC_CALC_OFFSET - sizeof(crc);
87 
88 	crc = crc16(0, &urb_cmd->raw_cmd[ES58X_CRC_CALC_OFFSET], len);
89 	return crc;
90 }
91 
92 /**
93  * es58x_get_crc() - Get the CRC value of a given URB.
94  * @urb_cmd: The URB command for which we want to get the CRC.
95  * @urb_len: Length of @urb_cmd. Must be at least bigger than 4
96  *	(ES58X_CRC_CALC_OFFSET + sizeof(crc))
97  *
98  * Return: crc16 value.
99  */
es58x_get_crc(const union es58x_urb_cmd * urb_cmd,u16 urb_len)100 static u16 es58x_get_crc(const union es58x_urb_cmd *urb_cmd, u16 urb_len)
101 {
102 	u16 crc;
103 	const __le16 *crc_addr;
104 
105 	crc_addr = (__le16 *)&urb_cmd->raw_cmd[urb_len - sizeof(crc)];
106 	crc = get_unaligned_le16(crc_addr);
107 	return crc;
108 }
109 
110 /**
111  * es58x_set_crc() - Set the CRC value of a given URB.
112  * @urb_cmd: The URB command for which we want to get the CRC.
113  * @urb_len: Length of @urb_cmd. Must be at least bigger than 4
114  *	(ES58X_CRC_CALC_OFFSET + sizeof(crc))
115  */
es58x_set_crc(union es58x_urb_cmd * urb_cmd,u16 urb_len)116 static void es58x_set_crc(union es58x_urb_cmd *urb_cmd, u16 urb_len)
117 {
118 	u16 crc;
119 	__le16 *crc_addr;
120 
121 	crc = es58x_calculate_crc(urb_cmd, urb_len);
122 	crc_addr = (__le16 *)&urb_cmd->raw_cmd[urb_len - sizeof(crc)];
123 	put_unaligned_le16(crc, crc_addr);
124 }
125 
126 /**
127  * es58x_check_crc() - Validate the CRC value of a given URB.
128  * @es58x_dev: ES58X device.
129  * @urb_cmd: The URB command for which we want to check the CRC.
130  * @urb_len: Length of @urb_cmd. Must be at least bigger than 4
131  *	(ES58X_CRC_CALC_OFFSET + sizeof(crc))
132  *
133  * Return: zero on success, -EBADMSG if the CRC check fails.
134  */
es58x_check_crc(struct es58x_device * es58x_dev,const union es58x_urb_cmd * urb_cmd,u16 urb_len)135 static int es58x_check_crc(struct es58x_device *es58x_dev,
136 			   const union es58x_urb_cmd *urb_cmd, u16 urb_len)
137 {
138 	u16 calculated_crc = es58x_calculate_crc(urb_cmd, urb_len);
139 	u16 expected_crc = es58x_get_crc(urb_cmd, urb_len);
140 
141 	if (expected_crc != calculated_crc) {
142 		dev_err_ratelimited(es58x_dev->dev,
143 				    "%s: Bad CRC, urb_len: %d\n",
144 				    __func__, urb_len);
145 		return -EBADMSG;
146 	}
147 
148 	return 0;
149 }
150 
151 /**
152  * es58x_timestamp_to_ns() - Convert a timestamp value received from a
153  *	ES58X device to nanoseconds.
154  * @timestamp: Timestamp received from a ES58X device.
155  *
156  * The timestamp received from ES58X is expressed in multiples of 0.5
157  * micro seconds. This function converts it in to nanoseconds.
158  *
159  * Return: Timestamp value in nanoseconds.
160  */
es58x_timestamp_to_ns(u64 timestamp)161 static u64 es58x_timestamp_to_ns(u64 timestamp)
162 {
163 	const u64 es58x_timestamp_ns_mult_coef = 500ULL;
164 
165 	return es58x_timestamp_ns_mult_coef * timestamp;
166 }
167 
168 /**
169  * es58x_set_skb_timestamp() - Set the hardware timestamp of an skb.
170  * @netdev: CAN network device.
171  * @skb: socket buffer of a CAN message.
172  * @timestamp: Timestamp received from an ES58X device.
173  *
174  * Used for both received and echo messages.
175  */
es58x_set_skb_timestamp(struct net_device * netdev,struct sk_buff * skb,u64 timestamp)176 static void es58x_set_skb_timestamp(struct net_device *netdev,
177 				    struct sk_buff *skb, u64 timestamp)
178 {
179 	struct es58x_device *es58x_dev = es58x_priv(netdev)->es58x_dev;
180 	struct skb_shared_hwtstamps *hwts;
181 
182 	hwts = skb_hwtstamps(skb);
183 	/* Ignoring overflow (overflow on 64 bits timestamp with nano
184 	 * second precision would occur after more than 500 years).
185 	 */
186 	hwts->hwtstamp = ns_to_ktime(es58x_timestamp_to_ns(timestamp) +
187 				     es58x_dev->realtime_diff_ns);
188 }
189 
190 /**
191  * es58x_rx_timestamp() - Handle a received timestamp.
192  * @es58x_dev: ES58X device.
193  * @timestamp: Timestamp received from a ES58X device.
194  *
195  * Calculate the difference between the ES58X device and the kernel
196  * internal clocks. This difference will be later used as an offset to
197  * convert the timestamps of RX and echo messages to match the kernel
198  * system time (e.g. convert to UNIX time).
199  */
es58x_rx_timestamp(struct es58x_device * es58x_dev,u64 timestamp)200 void es58x_rx_timestamp(struct es58x_device *es58x_dev, u64 timestamp)
201 {
202 	u64 ktime_real_ns = ktime_get_real_ns();
203 	u64 device_timestamp = es58x_timestamp_to_ns(timestamp);
204 
205 	dev_dbg(es58x_dev->dev, "%s: request round-trip time: %llu ns\n",
206 		__func__, ktime_real_ns - es58x_dev->ktime_req_ns);
207 
208 	es58x_dev->realtime_diff_ns =
209 	    (es58x_dev->ktime_req_ns + ktime_real_ns) / 2 - device_timestamp;
210 	es58x_dev->ktime_req_ns = 0;
211 
212 	dev_dbg(es58x_dev->dev,
213 		"%s: Device timestamp: %llu, diff with kernel: %llu\n",
214 		__func__, device_timestamp, es58x_dev->realtime_diff_ns);
215 }
216 
217 /**
218  * es58x_set_realtime_diff_ns() - Calculate difference between the
219  *	clocks of the ES58X device and the kernel
220  * @es58x_dev: ES58X device.
221  *
222  * Request a timestamp from the ES58X device. Once the answer is
223  * received, the timestamp difference will be set by the callback
224  * function es58x_rx_timestamp().
225  *
226  * Return: zero on success, errno when any error occurs.
227  */
es58x_set_realtime_diff_ns(struct es58x_device * es58x_dev)228 static int es58x_set_realtime_diff_ns(struct es58x_device *es58x_dev)
229 {
230 	if (es58x_dev->ktime_req_ns) {
231 		dev_warn(es58x_dev->dev,
232 			 "%s: Previous request to set timestamp has not completed yet\n",
233 			 __func__);
234 		return -EBUSY;
235 	}
236 
237 	es58x_dev->ktime_req_ns = ktime_get_real_ns();
238 	return es58x_dev->ops->get_timestamp(es58x_dev);
239 }
240 
241 /**
242  * es58x_is_can_state_active() - Is the network device in an active
243  *	CAN state?
244  * @netdev: CAN network device.
245  *
246  * The device is considered active if it is able to send or receive
247  * CAN frames, that is to say if it is in any of
248  * CAN_STATE_ERROR_ACTIVE, CAN_STATE_ERROR_WARNING or
249  * CAN_STATE_ERROR_PASSIVE states.
250  *
251  * Caution: when recovering from a bus-off,
252  * net/core/dev.c#can_restart() will call
253  * net/core/dev.c#can_flush_echo_skb() without using any kind of
254  * locks. For this reason, it is critical to guarantee that no TX or
255  * echo operations (i.e. any access to priv->echo_skb[]) can be done
256  * while this function is returning false.
257  *
258  * Return: true if the device is active, else returns false.
259  */
es58x_is_can_state_active(struct net_device * netdev)260 static bool es58x_is_can_state_active(struct net_device *netdev)
261 {
262 	return es58x_priv(netdev)->can.state < CAN_STATE_BUS_OFF;
263 }
264 
265 /**
266  * es58x_is_echo_skb_threshold_reached() - Determine the limit of how
267  *	many skb slots can be taken before we should stop the network
268  *	queue.
269  * @priv: ES58X private parameters related to the network device.
270  *
271  * We need to save enough free skb slots in order to be able to do
272  * bulk send. This function can be used to determine when to wake or
273  * stop the network queue in regard to the number of skb slots already
274  * taken if the echo FIFO.
275  *
276  * Return: boolean.
277  */
es58x_is_echo_skb_threshold_reached(struct es58x_priv * priv)278 static bool es58x_is_echo_skb_threshold_reached(struct es58x_priv *priv)
279 {
280 	u32 num_echo_skb =  priv->tx_head - priv->tx_tail;
281 	u32 threshold = priv->can.echo_skb_max -
282 		priv->es58x_dev->param->tx_bulk_max + 1;
283 
284 	return num_echo_skb >= threshold;
285 }
286 
287 /**
288  * es58x_can_free_echo_skb_tail() - Remove the oldest echo skb of the
289  *	echo FIFO.
290  * @netdev: CAN network device.
291  *
292  * Naming convention: the tail is the beginning of the FIFO, i.e. the
293  * first skb to have entered the FIFO.
294  */
es58x_can_free_echo_skb_tail(struct net_device * netdev)295 static void es58x_can_free_echo_skb_tail(struct net_device *netdev)
296 {
297 	struct es58x_priv *priv = es58x_priv(netdev);
298 	u16 fifo_mask = priv->es58x_dev->param->fifo_mask;
299 	unsigned int frame_len = 0;
300 
301 	can_free_echo_skb(netdev, priv->tx_tail & fifo_mask, &frame_len);
302 	netdev_completed_queue(netdev, 1, frame_len);
303 
304 	priv->tx_tail++;
305 
306 	netdev->stats.tx_dropped++;
307 }
308 
309 /**
310  * es58x_can_get_echo_skb_recovery() - Try to re-sync the echo FIFO.
311  * @netdev: CAN network device.
312  * @rcv_packet_idx: Index
313  *
314  * This function should not be called under normal circumstances. In
315  * the unlikely case that one or several URB packages get dropped by
316  * the device, the index will get out of sync. Try to recover by
317  * dropping the echo skb packets with older indexes.
318  *
319  * Return: zero if recovery was successful, -EINVAL otherwise.
320  */
es58x_can_get_echo_skb_recovery(struct net_device * netdev,u32 rcv_packet_idx)321 static int es58x_can_get_echo_skb_recovery(struct net_device *netdev,
322 					   u32 rcv_packet_idx)
323 {
324 	struct es58x_priv *priv = es58x_priv(netdev);
325 	int ret = 0;
326 
327 	netdev->stats.tx_errors++;
328 
329 	if (net_ratelimit())
330 		netdev_warn(netdev,
331 			    "Bad echo packet index: %u. First index: %u, end index %u, num_echo_skb: %02u/%02u\n",
332 			    rcv_packet_idx, priv->tx_tail, priv->tx_head,
333 			    priv->tx_head - priv->tx_tail,
334 			    priv->can.echo_skb_max);
335 
336 	if ((s32)(rcv_packet_idx - priv->tx_tail) < 0) {
337 		if (net_ratelimit())
338 			netdev_warn(netdev,
339 				    "Received echo index is from the past. Ignoring it\n");
340 		ret = -EINVAL;
341 	} else if ((s32)(rcv_packet_idx - priv->tx_head) >= 0) {
342 		if (net_ratelimit())
343 			netdev_err(netdev,
344 				   "Received echo index is from the future. Ignoring it\n");
345 		ret = -EINVAL;
346 	} else {
347 		if (net_ratelimit())
348 			netdev_warn(netdev,
349 				    "Recovery: dropping %u echo skb from index %u to %u\n",
350 				    rcv_packet_idx - priv->tx_tail,
351 				    priv->tx_tail, rcv_packet_idx - 1);
352 		while (priv->tx_tail != rcv_packet_idx) {
353 			if (priv->tx_tail == priv->tx_head)
354 				return -EINVAL;
355 			es58x_can_free_echo_skb_tail(netdev);
356 		}
357 	}
358 	return ret;
359 }
360 
361 /**
362  * es58x_can_get_echo_skb() - Get the skb from the echo FIFO and loop
363  *	it back locally.
364  * @netdev: CAN network device.
365  * @rcv_packet_idx: Index of the first packet received from the device.
366  * @tstamps: Array of hardware timestamps received from a ES58X device.
367  * @pkts: Number of packets (and so, length of @tstamps).
368  *
369  * Callback function for when we receive a self reception
370  * acknowledgment.  Retrieves the skb from the echo FIFO, sets its
371  * hardware timestamp (the actual time it was sent) and loops it back
372  * locally.
373  *
374  * The device has to be active (i.e. network interface UP and not in
375  * bus off state or restarting).
376  *
377  * Packet indexes must be consecutive (i.e. index of first packet is
378  * @rcv_packet_idx, index of second packet is @rcv_packet_idx + 1 and
379  * index of last packet is @rcv_packet_idx + @pkts - 1).
380  *
381  * Return: zero on success.
382  */
es58x_can_get_echo_skb(struct net_device * netdev,u32 rcv_packet_idx,u64 * tstamps,unsigned int pkts)383 int es58x_can_get_echo_skb(struct net_device *netdev, u32 rcv_packet_idx,
384 			   u64 *tstamps, unsigned int pkts)
385 {
386 	struct es58x_priv *priv = es58x_priv(netdev);
387 	unsigned int rx_total_frame_len = 0;
388 	unsigned int num_echo_skb = priv->tx_head - priv->tx_tail;
389 	int i;
390 	u16 fifo_mask = priv->es58x_dev->param->fifo_mask;
391 
392 	if (!netif_running(netdev)) {
393 		if (net_ratelimit())
394 			netdev_info(netdev,
395 				    "%s: %s is down, dropping %d echo packets\n",
396 				    __func__, netdev->name, pkts);
397 		netdev->stats.tx_dropped += pkts;
398 		return 0;
399 	} else if (!es58x_is_can_state_active(netdev)) {
400 		if (net_ratelimit())
401 			netdev_dbg(netdev,
402 				   "Bus is off or device is restarting. Ignoring %u echo packets from index %u\n",
403 				   pkts, rcv_packet_idx);
404 		/* stats.tx_dropped will be (or was already)
405 		 * incremented by
406 		 * drivers/net/can/net/dev.c:can_flush_echo_skb().
407 		 */
408 		return 0;
409 	} else if (num_echo_skb == 0) {
410 		if (net_ratelimit())
411 			netdev_warn(netdev,
412 				    "Received %u echo packets from index: %u but echo skb queue is empty.\n",
413 				    pkts, rcv_packet_idx);
414 		netdev->stats.tx_dropped += pkts;
415 		return 0;
416 	}
417 
418 	if (priv->tx_tail != rcv_packet_idx) {
419 		if (es58x_can_get_echo_skb_recovery(netdev, rcv_packet_idx) < 0) {
420 			if (net_ratelimit())
421 				netdev_warn(netdev,
422 					    "Could not find echo skb for echo packet index: %u\n",
423 					    rcv_packet_idx);
424 			return 0;
425 		}
426 	}
427 	if (num_echo_skb < pkts) {
428 		int pkts_drop = pkts - num_echo_skb;
429 
430 		if (net_ratelimit())
431 			netdev_err(netdev,
432 				   "Received %u echo packets but have only %d echo skb. Dropping %d echo skb\n",
433 				   pkts, num_echo_skb, pkts_drop);
434 		netdev->stats.tx_dropped += pkts_drop;
435 		pkts -= pkts_drop;
436 	}
437 
438 	for (i = 0; i < pkts; i++) {
439 		unsigned int skb_idx = priv->tx_tail & fifo_mask;
440 		struct sk_buff *skb = priv->can.echo_skb[skb_idx];
441 		unsigned int frame_len = 0;
442 
443 		if (skb)
444 			es58x_set_skb_timestamp(netdev, skb, tstamps[i]);
445 
446 		netdev->stats.tx_bytes += can_get_echo_skb(netdev, skb_idx,
447 							   &frame_len);
448 		rx_total_frame_len += frame_len;
449 
450 		priv->tx_tail++;
451 	}
452 
453 	netdev_completed_queue(netdev, pkts, rx_total_frame_len);
454 	netdev->stats.tx_packets += pkts;
455 
456 	priv->err_passive_before_rtx_success = 0;
457 	if (!es58x_is_echo_skb_threshold_reached(priv))
458 		netif_wake_queue(netdev);
459 
460 	return 0;
461 }
462 
463 /**
464  * es58x_can_reset_echo_fifo() - Reset the echo FIFO.
465  * @netdev: CAN network device.
466  *
467  * The echo_skb array of struct can_priv will be flushed by
468  * drivers/net/can/dev.c:can_flush_echo_skb(). This function resets
469  * the parameters of the struct es58x_priv of our device and reset the
470  * queue (c.f. BQL).
471  */
es58x_can_reset_echo_fifo(struct net_device * netdev)472 static void es58x_can_reset_echo_fifo(struct net_device *netdev)
473 {
474 	struct es58x_priv *priv = es58x_priv(netdev);
475 
476 	priv->tx_tail = 0;
477 	priv->tx_head = 0;
478 	priv->tx_urb = NULL;
479 	priv->err_passive_before_rtx_success = 0;
480 	netdev_reset_queue(netdev);
481 }
482 
483 /**
484  * es58x_flush_pending_tx_msg() - Reset the buffer for transmission messages.
485  * @netdev: CAN network device.
486  *
487  * es58x_start_xmit() will queue up to tx_bulk_max messages in
488  * &tx_urb buffer and do a bulk send of all messages in one single URB
489  * (c.f. xmit_more flag). When the device recovers from a bus off
490  * state or when the device stops, the tx_urb buffer might still have
491  * pending messages in it and thus need to be flushed.
492  */
es58x_flush_pending_tx_msg(struct net_device * netdev)493 static void es58x_flush_pending_tx_msg(struct net_device *netdev)
494 {
495 	struct es58x_priv *priv = es58x_priv(netdev);
496 	struct es58x_device *es58x_dev = priv->es58x_dev;
497 
498 	if (priv->tx_urb) {
499 		netdev_warn(netdev, "%s: dropping %d TX messages\n",
500 			    __func__, priv->tx_can_msg_cnt);
501 		netdev->stats.tx_dropped += priv->tx_can_msg_cnt;
502 		while (priv->tx_can_msg_cnt > 0) {
503 			unsigned int frame_len = 0;
504 			u16 fifo_mask = priv->es58x_dev->param->fifo_mask;
505 
506 			priv->tx_head--;
507 			priv->tx_can_msg_cnt--;
508 			can_free_echo_skb(netdev, priv->tx_head & fifo_mask,
509 					  &frame_len);
510 			netdev_completed_queue(netdev, 1, frame_len);
511 		}
512 		usb_anchor_urb(priv->tx_urb, &priv->es58x_dev->tx_urbs_idle);
513 		atomic_inc(&es58x_dev->tx_urbs_idle_cnt);
514 		usb_free_urb(priv->tx_urb);
515 	}
516 	priv->tx_urb = NULL;
517 }
518 
519 /**
520  * es58x_tx_ack_msg() - Handle acknowledgment messages.
521  * @netdev: CAN network device.
522  * @tx_free_entries: Number of free entries in the device transmit FIFO.
523  * @rx_cmd_ret_u32: error code as returned by the ES58X device.
524  *
525  * ES58X sends an acknowledgment message after a transmission request
526  * is done. This is mandatory for the ES581.4 but is optional (and
527  * deactivated in this driver) for the ES58X_FD family.
528  *
529  * Under normal circumstances, this function should never throw an
530  * error message.
531  *
532  * Return: zero on success, errno when any error occurs.
533  */
es58x_tx_ack_msg(struct net_device * netdev,u16 tx_free_entries,enum es58x_ret_u32 rx_cmd_ret_u32)534 int es58x_tx_ack_msg(struct net_device *netdev, u16 tx_free_entries,
535 		     enum es58x_ret_u32 rx_cmd_ret_u32)
536 {
537 	struct es58x_priv *priv = es58x_priv(netdev);
538 
539 	if (tx_free_entries <= priv->es58x_dev->param->tx_bulk_max) {
540 		if (net_ratelimit())
541 			netdev_err(netdev,
542 				   "Only %d entries left in device queue, num_echo_skb: %d/%d\n",
543 				   tx_free_entries,
544 				   priv->tx_head - priv->tx_tail,
545 				   priv->can.echo_skb_max);
546 		netif_stop_queue(netdev);
547 	}
548 
549 	return es58x_rx_cmd_ret_u32(netdev, ES58X_RET_TYPE_TX_MSG,
550 				    rx_cmd_ret_u32);
551 }
552 
553 /**
554  * es58x_rx_can_msg() - Handle a received a CAN message.
555  * @netdev: CAN network device.
556  * @timestamp: Hardware time stamp (only relevant in rx branches).
557  * @data: CAN payload.
558  * @can_id: CAN ID.
559  * @es58x_flags: Please refer to enum es58x_flag.
560  * @dlc: Data Length Code (raw value).
561  *
562  * Fill up a CAN skb and post it.
563  *
564  * This function handles the case where the DLC of a classical CAN
565  * frame is greater than CAN_MAX_DLEN (c.f. the len8_dlc field of
566  * struct can_frame).
567  *
568  * Return: zero on success.
569  */
es58x_rx_can_msg(struct net_device * netdev,u64 timestamp,const u8 * data,canid_t can_id,enum es58x_flag es58x_flags,u8 dlc)570 int es58x_rx_can_msg(struct net_device *netdev, u64 timestamp, const u8 *data,
571 		     canid_t can_id, enum es58x_flag es58x_flags, u8 dlc)
572 {
573 	struct canfd_frame *cfd;
574 	struct can_frame *ccf;
575 	struct sk_buff *skb;
576 	u8 len;
577 	bool is_can_fd = !!(es58x_flags & ES58X_FLAG_FD_DATA);
578 
579 	if (dlc > CAN_MAX_RAW_DLC) {
580 		netdev_err(netdev,
581 			   "%s: DLC is %d but maximum should be %d\n",
582 			   __func__, dlc, CAN_MAX_RAW_DLC);
583 		return -EMSGSIZE;
584 	}
585 
586 	if (is_can_fd) {
587 		len = can_fd_dlc2len(dlc);
588 		skb = alloc_canfd_skb(netdev, &cfd);
589 	} else {
590 		len = can_cc_dlc2len(dlc);
591 		skb = alloc_can_skb(netdev, &ccf);
592 		cfd = (struct canfd_frame *)ccf;
593 	}
594 	if (!skb) {
595 		netdev->stats.rx_dropped++;
596 		return 0;
597 	}
598 
599 	cfd->can_id = can_id;
600 	if (es58x_flags & ES58X_FLAG_EFF)
601 		cfd->can_id |= CAN_EFF_FLAG;
602 	if (is_can_fd) {
603 		cfd->len = len;
604 		if (es58x_flags & ES58X_FLAG_FD_BRS)
605 			cfd->flags |= CANFD_BRS;
606 		if (es58x_flags & ES58X_FLAG_FD_ESI)
607 			cfd->flags |= CANFD_ESI;
608 	} else {
609 		can_frame_set_cc_len(ccf, dlc, es58x_priv(netdev)->can.ctrlmode);
610 		if (es58x_flags & ES58X_FLAG_RTR) {
611 			ccf->can_id |= CAN_RTR_FLAG;
612 			len = 0;
613 		}
614 	}
615 	memcpy(cfd->data, data, len);
616 	netdev->stats.rx_packets++;
617 	netdev->stats.rx_bytes += len;
618 
619 	es58x_set_skb_timestamp(netdev, skb, timestamp);
620 	netif_rx(skb);
621 
622 	es58x_priv(netdev)->err_passive_before_rtx_success = 0;
623 
624 	return 0;
625 }
626 
627 /**
628  * es58x_rx_err_msg() - Handle a received CAN event or error message.
629  * @netdev: CAN network device.
630  * @error: Error code.
631  * @event: Event code.
632  * @timestamp: Timestamp received from a ES58X device.
633  *
634  * Handle the errors and events received by the ES58X device, create
635  * a CAN error skb and post it.
636  *
637  * In some rare cases the devices might get stuck alternating between
638  * CAN_STATE_ERROR_PASSIVE and CAN_STATE_ERROR_WARNING. To prevent
639  * this behavior, we force a bus off state if the device goes in
640  * CAN_STATE_ERROR_WARNING for ES58X_MAX_CONSECUTIVE_WARN consecutive
641  * times with no successful transmission or reception in between.
642  *
643  * Once the device is in bus off state, the only way to restart it is
644  * through the drivers/net/can/dev.c:can_restart() function. The
645  * device is technically capable to recover by itself under certain
646  * circumstances, however, allowing self recovery would create
647  * complex race conditions with drivers/net/can/dev.c:can_restart()
648  * and thus was not implemented. To activate automatic restart, please
649  * set the restart-ms parameter (e.g. ip link set can0 type can
650  * restart-ms 100).
651  *
652  * If the bus is really instable, this function would try to send a
653  * lot of log messages. Those are rate limited (i.e. you will see
654  * messages such as "net_ratelimit: XXX callbacks suppressed" in
655  * dmesg).
656  *
657  * Return: zero on success, errno when any error occurs.
658  */
es58x_rx_err_msg(struct net_device * netdev,enum es58x_err error,enum es58x_event event,u64 timestamp)659 int es58x_rx_err_msg(struct net_device *netdev, enum es58x_err error,
660 		     enum es58x_event event, u64 timestamp)
661 {
662 	struct es58x_priv *priv = es58x_priv(netdev);
663 	struct can_priv *can = netdev_priv(netdev);
664 	struct can_device_stats *can_stats = &can->can_stats;
665 	struct can_frame *cf = NULL;
666 	struct sk_buff *skb;
667 	int ret = 0;
668 
669 	if (!netif_running(netdev)) {
670 		if (net_ratelimit())
671 			netdev_info(netdev, "%s: %s is down, dropping packet\n",
672 				    __func__, netdev->name);
673 		netdev->stats.rx_dropped++;
674 		return 0;
675 	}
676 
677 	if (error == ES58X_ERR_OK && event == ES58X_EVENT_OK) {
678 		netdev_err(netdev, "%s: Both error and event are zero\n",
679 			   __func__);
680 		return -EINVAL;
681 	}
682 
683 	skb = alloc_can_err_skb(netdev, &cf);
684 
685 	switch (error) {
686 	case ES58X_ERR_OK:	/* 0: No error */
687 		break;
688 
689 	case ES58X_ERR_PROT_STUFF:
690 		if (net_ratelimit())
691 			netdev_dbg(netdev, "Error BITSTUFF\n");
692 		if (cf)
693 			cf->data[2] |= CAN_ERR_PROT_STUFF;
694 		break;
695 
696 	case ES58X_ERR_PROT_FORM:
697 		if (net_ratelimit())
698 			netdev_dbg(netdev, "Error FORMAT\n");
699 		if (cf)
700 			cf->data[2] |= CAN_ERR_PROT_FORM;
701 		break;
702 
703 	case ES58X_ERR_ACK:
704 		if (net_ratelimit())
705 			netdev_dbg(netdev, "Error ACK\n");
706 		if (cf)
707 			cf->can_id |= CAN_ERR_ACK;
708 		break;
709 
710 	case ES58X_ERR_PROT_BIT:
711 		if (net_ratelimit())
712 			netdev_dbg(netdev, "Error BIT\n");
713 		if (cf)
714 			cf->data[2] |= CAN_ERR_PROT_BIT;
715 		break;
716 
717 	case ES58X_ERR_PROT_CRC:
718 		if (net_ratelimit())
719 			netdev_dbg(netdev, "Error CRC\n");
720 		if (cf)
721 			cf->data[3] |= CAN_ERR_PROT_LOC_CRC_SEQ;
722 		break;
723 
724 	case ES58X_ERR_PROT_BIT1:
725 		if (net_ratelimit())
726 			netdev_dbg(netdev,
727 				   "Error: expected a recessive bit but monitored a dominant one\n");
728 		if (cf)
729 			cf->data[2] |= CAN_ERR_PROT_BIT1;
730 		break;
731 
732 	case ES58X_ERR_PROT_BIT0:
733 		if (net_ratelimit())
734 			netdev_dbg(netdev,
735 				   "Error expected a dominant bit but monitored a recessive one\n");
736 		if (cf)
737 			cf->data[2] |= CAN_ERR_PROT_BIT0;
738 		break;
739 
740 	case ES58X_ERR_PROT_OVERLOAD:
741 		if (net_ratelimit())
742 			netdev_dbg(netdev, "Error OVERLOAD\n");
743 		if (cf)
744 			cf->data[2] |= CAN_ERR_PROT_OVERLOAD;
745 		break;
746 
747 	case ES58X_ERR_PROT_UNSPEC:
748 		if (net_ratelimit())
749 			netdev_dbg(netdev, "Unspecified error\n");
750 		if (cf)
751 			cf->can_id |= CAN_ERR_PROT;
752 		break;
753 
754 	default:
755 		if (net_ratelimit())
756 			netdev_err(netdev,
757 				   "%s: Unspecified error code 0x%04X\n",
758 				   __func__, (int)error);
759 		if (cf)
760 			cf->can_id |= CAN_ERR_PROT;
761 		break;
762 	}
763 
764 	switch (event) {
765 	case ES58X_EVENT_OK:	/* 0: No event */
766 		break;
767 
768 	case ES58X_EVENT_CRTL_ACTIVE:
769 		if (can->state == CAN_STATE_BUS_OFF) {
770 			netdev_err(netdev,
771 				   "%s: state transition: BUS OFF -> ACTIVE\n",
772 				   __func__);
773 		}
774 		if (net_ratelimit())
775 			netdev_dbg(netdev, "Event CAN BUS ACTIVE\n");
776 		if (cf)
777 			cf->data[1] |= CAN_ERR_CRTL_ACTIVE;
778 		can->state = CAN_STATE_ERROR_ACTIVE;
779 		break;
780 
781 	case ES58X_EVENT_CRTL_PASSIVE:
782 		if (net_ratelimit())
783 			netdev_dbg(netdev, "Event CAN BUS PASSIVE\n");
784 		/* Either TX or RX error count reached passive state
785 		 * but we do not know which. Setting both flags by
786 		 * default.
787 		 */
788 		if (cf) {
789 			cf->data[1] |= CAN_ERR_CRTL_RX_PASSIVE;
790 			cf->data[1] |= CAN_ERR_CRTL_TX_PASSIVE;
791 		}
792 		if (can->state < CAN_STATE_BUS_OFF)
793 			can->state = CAN_STATE_ERROR_PASSIVE;
794 		can_stats->error_passive++;
795 		if (priv->err_passive_before_rtx_success < U8_MAX)
796 			priv->err_passive_before_rtx_success++;
797 		break;
798 
799 	case ES58X_EVENT_CRTL_WARNING:
800 		if (net_ratelimit())
801 			netdev_dbg(netdev, "Event CAN BUS WARNING\n");
802 		/* Either TX or RX error count reached warning state
803 		 * but we do not know which. Setting both flags by
804 		 * default.
805 		 */
806 		if (cf) {
807 			cf->data[1] |= CAN_ERR_CRTL_RX_WARNING;
808 			cf->data[1] |= CAN_ERR_CRTL_TX_WARNING;
809 		}
810 		if (can->state < CAN_STATE_BUS_OFF)
811 			can->state = CAN_STATE_ERROR_WARNING;
812 		can_stats->error_warning++;
813 		break;
814 
815 	case ES58X_EVENT_BUSOFF:
816 		if (net_ratelimit())
817 			netdev_dbg(netdev, "Event CAN BUS OFF\n");
818 		if (cf)
819 			cf->can_id |= CAN_ERR_BUSOFF;
820 		can_stats->bus_off++;
821 		netif_stop_queue(netdev);
822 		if (can->state != CAN_STATE_BUS_OFF) {
823 			can->state = CAN_STATE_BUS_OFF;
824 			can_bus_off(netdev);
825 			ret = can->do_set_mode(netdev, CAN_MODE_STOP);
826 		}
827 		break;
828 
829 	case ES58X_EVENT_SINGLE_WIRE:
830 		if (net_ratelimit())
831 			netdev_warn(netdev,
832 				    "Lost connection on either CAN high or CAN low\n");
833 		/* Lost connection on either CAN high or CAN
834 		 * low. Setting both flags by default.
835 		 */
836 		if (cf) {
837 			cf->data[4] |= CAN_ERR_TRX_CANH_NO_WIRE;
838 			cf->data[4] |= CAN_ERR_TRX_CANL_NO_WIRE;
839 		}
840 		break;
841 
842 	default:
843 		if (net_ratelimit())
844 			netdev_err(netdev,
845 				   "%s: Unspecified event code 0x%04X\n",
846 				   __func__, (int)event);
847 		if (cf)
848 			cf->can_id |= CAN_ERR_CRTL;
849 		break;
850 	}
851 
852 	/* driver/net/can/dev.c:can_restart() takes in account error
853 	 * messages in the RX stats. Doing the same here for
854 	 * consistency.
855 	 */
856 	netdev->stats.rx_packets++;
857 	netdev->stats.rx_bytes += CAN_ERR_DLC;
858 
859 	if (cf) {
860 		if (cf->data[1])
861 			cf->can_id |= CAN_ERR_CRTL;
862 		if (cf->data[2] || cf->data[3]) {
863 			cf->can_id |= CAN_ERR_PROT;
864 			can_stats->bus_error++;
865 		}
866 		if (cf->data[4])
867 			cf->can_id |= CAN_ERR_TRX;
868 
869 		es58x_set_skb_timestamp(netdev, skb, timestamp);
870 		netif_rx(skb);
871 	}
872 
873 	if ((event & ES58X_EVENT_CRTL_PASSIVE) &&
874 	    priv->err_passive_before_rtx_success == ES58X_CONSECUTIVE_ERR_PASSIVE_MAX) {
875 		netdev_info(netdev,
876 			    "Got %d consecutive warning events with no successful RX or TX. Forcing bus-off\n",
877 			    priv->err_passive_before_rtx_success);
878 		return es58x_rx_err_msg(netdev, ES58X_ERR_OK,
879 					ES58X_EVENT_BUSOFF, timestamp);
880 	}
881 
882 	return ret;
883 }
884 
885 /**
886  * es58x_cmd_ret_desc() - Convert a command type to a string.
887  * @cmd_ret_type: Type of the command which triggered the return code.
888  *
889  * The final line (return "<unknown>") should not be reached. If this
890  * is the case, there is an implementation bug.
891  *
892  * Return: a readable description of the @cmd_ret_type.
893  */
es58x_cmd_ret_desc(enum es58x_ret_type cmd_ret_type)894 static const char *es58x_cmd_ret_desc(enum es58x_ret_type cmd_ret_type)
895 {
896 	switch (cmd_ret_type) {
897 	case ES58X_RET_TYPE_SET_BITTIMING:
898 		return "Set bittiming";
899 	case ES58X_RET_TYPE_ENABLE_CHANNEL:
900 		return "Enable channel";
901 	case ES58X_RET_TYPE_DISABLE_CHANNEL:
902 		return "Disable channel";
903 	case ES58X_RET_TYPE_TX_MSG:
904 		return "Transmit message";
905 	case ES58X_RET_TYPE_RESET_RX:
906 		return "Reset RX";
907 	case ES58X_RET_TYPE_RESET_TX:
908 		return "Reset TX";
909 	case ES58X_RET_TYPE_DEVICE_ERR:
910 		return "Device error";
911 	}
912 
913 	return "<unknown>";
914 };
915 
916 /**
917  * es58x_rx_cmd_ret_u8() - Handle the command's return code received
918  *	from the ES58X device.
919  * @dev: Device, only used for the dev_XXX() print functions.
920  * @cmd_ret_type: Type of the command which triggered the return code.
921  * @rx_cmd_ret_u8: Command error code as returned by the ES58X device.
922  *
923  * Handles the 8 bits command return code. Those are specific to the
924  * ES581.4 device. The return value will eventually be used by
925  * es58x_handle_urb_cmd() function which will take proper actions in
926  * case of critical issues such and memory errors or bad CRC values.
927  *
928  * In contrast with es58x_rx_cmd_ret_u32(), the network device is
929  * unknown.
930  *
931  * Return: zero on success, return errno when any error occurs.
932  */
es58x_rx_cmd_ret_u8(struct device * dev,enum es58x_ret_type cmd_ret_type,enum es58x_ret_u8 rx_cmd_ret_u8)933 int es58x_rx_cmd_ret_u8(struct device *dev,
934 			enum es58x_ret_type cmd_ret_type,
935 			enum es58x_ret_u8 rx_cmd_ret_u8)
936 {
937 	const char *ret_desc = es58x_cmd_ret_desc(cmd_ret_type);
938 
939 	switch (rx_cmd_ret_u8) {
940 	case ES58X_RET_U8_OK:
941 		dev_dbg_ratelimited(dev, "%s: OK\n", ret_desc);
942 		return 0;
943 
944 	case ES58X_RET_U8_ERR_UNSPECIFIED_FAILURE:
945 		dev_err(dev, "%s: unspecified failure\n", ret_desc);
946 		return -EBADMSG;
947 
948 	case ES58X_RET_U8_ERR_NO_MEM:
949 		dev_err(dev, "%s: device ran out of memory\n", ret_desc);
950 		return -ENOMEM;
951 
952 	case ES58X_RET_U8_ERR_BAD_CRC:
953 		dev_err(dev, "%s: CRC of previous command is incorrect\n",
954 			ret_desc);
955 		return -EIO;
956 
957 	default:
958 		dev_err(dev, "%s: returned unknown value: 0x%02X\n",
959 			ret_desc, rx_cmd_ret_u8);
960 		return -EBADMSG;
961 	}
962 }
963 
964 /**
965  * es58x_rx_cmd_ret_u32() - Handle the command return code received
966  *	from the ES58X device.
967  * @netdev: CAN network device.
968  * @cmd_ret_type: Type of the command which triggered the return code.
969  * @rx_cmd_ret_u32: error code as returned by the ES58X device.
970  *
971  * Handles the 32 bits command return code. The return value will
972  * eventually be used by es58x_handle_urb_cmd() function which will
973  * take proper actions in case of critical issues such and memory
974  * errors or bad CRC values.
975  *
976  * Return: zero on success, errno when any error occurs.
977  */
es58x_rx_cmd_ret_u32(struct net_device * netdev,enum es58x_ret_type cmd_ret_type,enum es58x_ret_u32 rx_cmd_ret_u32)978 int es58x_rx_cmd_ret_u32(struct net_device *netdev,
979 			 enum es58x_ret_type cmd_ret_type,
980 			 enum es58x_ret_u32 rx_cmd_ret_u32)
981 {
982 	struct es58x_priv *priv = es58x_priv(netdev);
983 	const struct es58x_operators *ops = priv->es58x_dev->ops;
984 	const char *ret_desc = es58x_cmd_ret_desc(cmd_ret_type);
985 
986 	switch (rx_cmd_ret_u32) {
987 	case ES58X_RET_U32_OK:
988 		switch (cmd_ret_type) {
989 		case ES58X_RET_TYPE_ENABLE_CHANNEL:
990 			es58x_can_reset_echo_fifo(netdev);
991 			priv->can.state = CAN_STATE_ERROR_ACTIVE;
992 			netif_wake_queue(netdev);
993 			netdev_info(netdev,
994 				    "%s: %s (Serial Number %s): CAN%d channel becomes ready\n",
995 				    ret_desc, priv->es58x_dev->udev->product,
996 				    priv->es58x_dev->udev->serial,
997 				    priv->channel_idx + 1);
998 			break;
999 
1000 		case ES58X_RET_TYPE_TX_MSG:
1001 			if (IS_ENABLED(CONFIG_VERBOSE_DEBUG) && net_ratelimit())
1002 				netdev_vdbg(netdev, "%s: OK\n", ret_desc);
1003 			break;
1004 
1005 		default:
1006 			netdev_dbg(netdev, "%s: OK\n", ret_desc);
1007 			break;
1008 		}
1009 		return 0;
1010 
1011 	case ES58X_RET_U32_ERR_UNSPECIFIED_FAILURE:
1012 		if (cmd_ret_type == ES58X_RET_TYPE_ENABLE_CHANNEL) {
1013 			int ret;
1014 
1015 			netdev_warn(netdev,
1016 				    "%s: channel is already opened, closing and re-opening it to reflect new configuration\n",
1017 				    ret_desc);
1018 			ret = ops->disable_channel(es58x_priv(netdev));
1019 			if (ret)
1020 				return ret;
1021 			return ops->enable_channel(es58x_priv(netdev));
1022 		}
1023 		if (cmd_ret_type == ES58X_RET_TYPE_DISABLE_CHANNEL) {
1024 			netdev_info(netdev,
1025 				    "%s: channel is already closed\n", ret_desc);
1026 			return 0;
1027 		}
1028 		netdev_err(netdev,
1029 			   "%s: unspecified failure\n", ret_desc);
1030 		return -EBADMSG;
1031 
1032 	case ES58X_RET_U32_ERR_NO_MEM:
1033 		netdev_err(netdev, "%s: device ran out of memory\n", ret_desc);
1034 		return -ENOMEM;
1035 
1036 	case ES58X_RET_U32_WARN_PARAM_ADJUSTED:
1037 		netdev_warn(netdev,
1038 			    "%s: some incompatible parameters have been adjusted\n",
1039 			    ret_desc);
1040 		return 0;
1041 
1042 	case ES58X_RET_U32_WARN_TX_MAYBE_REORDER:
1043 		netdev_warn(netdev,
1044 			    "%s: TX messages might have been reordered\n",
1045 			    ret_desc);
1046 		return 0;
1047 
1048 	case ES58X_RET_U32_ERR_TIMEDOUT:
1049 		netdev_err(netdev, "%s: command timed out\n", ret_desc);
1050 		return -ETIMEDOUT;
1051 
1052 	case ES58X_RET_U32_ERR_FIFO_FULL:
1053 		netdev_warn(netdev, "%s: fifo is full\n", ret_desc);
1054 		return 0;
1055 
1056 	case ES58X_RET_U32_ERR_BAD_CONFIG:
1057 		netdev_err(netdev, "%s: bad configuration\n", ret_desc);
1058 		return -EINVAL;
1059 
1060 	case ES58X_RET_U32_ERR_NO_RESOURCE:
1061 		netdev_err(netdev, "%s: no resource available\n", ret_desc);
1062 		return -EBUSY;
1063 
1064 	default:
1065 		netdev_err(netdev, "%s returned unknown value: 0x%08X\n",
1066 			   ret_desc, rx_cmd_ret_u32);
1067 		return -EBADMSG;
1068 	}
1069 }
1070 
1071 /**
1072  * es58x_increment_rx_errors() - Increment the network devices' error
1073  *	count.
1074  * @es58x_dev: ES58X device.
1075  *
1076  * If an error occurs on the early stages on receiving an URB command,
1077  * we might not be able to figure out on which network device the
1078  * error occurred. In such case, we arbitrarily increment the error
1079  * count of all the network devices attached to our ES58X device.
1080  */
es58x_increment_rx_errors(struct es58x_device * es58x_dev)1081 static void es58x_increment_rx_errors(struct es58x_device *es58x_dev)
1082 {
1083 	int i;
1084 
1085 	for (i = 0; i < es58x_dev->num_can_ch; i++)
1086 		if (es58x_dev->netdev[i])
1087 			es58x_dev->netdev[i]->stats.rx_errors++;
1088 }
1089 
1090 /**
1091  * es58x_handle_urb_cmd() - Handle the URB command
1092  * @es58x_dev: ES58X device.
1093  * @urb_cmd: The URB command received from the ES58X device, might not
1094  *	be aligned.
1095  *
1096  * Sends the URB command to the device specific function. Manages the
1097  * errors thrown back by those functions.
1098  */
es58x_handle_urb_cmd(struct es58x_device * es58x_dev,const union es58x_urb_cmd * urb_cmd)1099 static void es58x_handle_urb_cmd(struct es58x_device *es58x_dev,
1100 				 const union es58x_urb_cmd *urb_cmd)
1101 {
1102 	const struct es58x_operators *ops = es58x_dev->ops;
1103 	size_t cmd_len;
1104 	int i, ret;
1105 
1106 	ret = ops->handle_urb_cmd(es58x_dev, urb_cmd);
1107 	switch (ret) {
1108 	case 0:		/* OK */
1109 		return;
1110 
1111 	case -ENODEV:
1112 		dev_err_ratelimited(es58x_dev->dev, "Device is not ready\n");
1113 		break;
1114 
1115 	case -EINVAL:
1116 	case -EMSGSIZE:
1117 	case -EBADRQC:
1118 	case -EBADMSG:
1119 	case -ECHRNG:
1120 	case -ETIMEDOUT:
1121 		cmd_len = es58x_get_urb_cmd_len(es58x_dev,
1122 						ops->get_msg_len(urb_cmd));
1123 		dev_err(es58x_dev->dev,
1124 			"ops->handle_urb_cmd() returned error %pe",
1125 			ERR_PTR(ret));
1126 		es58x_print_hex_dump(urb_cmd, cmd_len);
1127 		break;
1128 
1129 	case -EFAULT:
1130 	case -ENOMEM:
1131 	case -EIO:
1132 	default:
1133 		dev_crit(es58x_dev->dev,
1134 			 "ops->handle_urb_cmd() returned error %pe, detaching all network devices\n",
1135 			 ERR_PTR(ret));
1136 		for (i = 0; i < es58x_dev->num_can_ch; i++)
1137 			if (es58x_dev->netdev[i])
1138 				netif_device_detach(es58x_dev->netdev[i]);
1139 		if (es58x_dev->ops->reset_device)
1140 			es58x_dev->ops->reset_device(es58x_dev);
1141 		break;
1142 	}
1143 
1144 	/* Because the urb command could not fully be parsed,
1145 	 * channel_id is not confirmed. Incrementing rx_errors count
1146 	 * of all channels.
1147 	 */
1148 	es58x_increment_rx_errors(es58x_dev);
1149 }
1150 
1151 /**
1152  * es58x_check_rx_urb() - Check the length and format of the URB command.
1153  * @es58x_dev: ES58X device.
1154  * @urb_cmd: The URB command received from the ES58X device, might not
1155  *	be aligned.
1156  * @urb_actual_len: The actual length of the URB command.
1157  *
1158  * Check if the first message of the received urb is valid, that is to
1159  * say that both the header and the length are coherent.
1160  *
1161  * Return:
1162  * the length of the first message of the URB on success.
1163  *
1164  * -ENODATA if the URB command is incomplete (in which case, the URB
1165  * command should be buffered and combined with the next URB to try to
1166  * reconstitute the URB command).
1167  *
1168  * -EOVERFLOW if the length is bigger than the maximum expected one.
1169  *
1170  * -EBADRQC if the start of frame does not match the expected value.
1171  */
es58x_check_rx_urb(struct es58x_device * es58x_dev,const union es58x_urb_cmd * urb_cmd,u32 urb_actual_len)1172 static signed int es58x_check_rx_urb(struct es58x_device *es58x_dev,
1173 				     const union es58x_urb_cmd *urb_cmd,
1174 				     u32 urb_actual_len)
1175 {
1176 	const struct device *dev = es58x_dev->dev;
1177 	const struct es58x_parameters *param = es58x_dev->param;
1178 	u16 sof, msg_len;
1179 	signed int urb_cmd_len, ret;
1180 
1181 	if (urb_actual_len < param->urb_cmd_header_len) {
1182 		dev_vdbg(dev,
1183 			 "%s: Received %d bytes [%*ph]: header incomplete\n",
1184 			 __func__, urb_actual_len, urb_actual_len,
1185 			 urb_cmd->raw_cmd);
1186 		return -ENODATA;
1187 	}
1188 
1189 	sof = get_unaligned_le16(&urb_cmd->sof);
1190 	if (sof != param->rx_start_of_frame) {
1191 		dev_err_ratelimited(es58x_dev->dev,
1192 				    "%s: Expected sequence 0x%04X for start of frame but got 0x%04X.\n",
1193 				    __func__, param->rx_start_of_frame, sof);
1194 		return -EBADRQC;
1195 	}
1196 
1197 	msg_len = es58x_dev->ops->get_msg_len(urb_cmd);
1198 	urb_cmd_len = es58x_get_urb_cmd_len(es58x_dev, msg_len);
1199 	if (urb_cmd_len > param->rx_urb_cmd_max_len) {
1200 		dev_err_ratelimited(es58x_dev->dev,
1201 				    "%s: Biggest expected size for rx urb_cmd is %u but receive a command of size %d\n",
1202 				    __func__,
1203 				    param->rx_urb_cmd_max_len, urb_cmd_len);
1204 		return -EOVERFLOW;
1205 	} else if (urb_actual_len < urb_cmd_len) {
1206 		dev_vdbg(dev, "%s: Received %02d/%02d bytes\n",
1207 			 __func__, urb_actual_len, urb_cmd_len);
1208 		return -ENODATA;
1209 	}
1210 
1211 	ret = es58x_check_crc(es58x_dev, urb_cmd, urb_cmd_len);
1212 	if (ret)
1213 		return ret;
1214 
1215 	return urb_cmd_len;
1216 }
1217 
1218 /**
1219  * es58x_copy_to_cmd_buf() - Copy an array to the URB command buffer.
1220  * @es58x_dev: ES58X device.
1221  * @raw_cmd: the buffer we want to copy.
1222  * @raw_cmd_len: length of @raw_cmd.
1223  *
1224  * Concatenates @raw_cmd_len bytes of @raw_cmd to the end of the URB
1225  * command buffer.
1226  *
1227  * Return: zero on success, -EMSGSIZE if not enough space is available
1228  * to do the copy.
1229  */
es58x_copy_to_cmd_buf(struct es58x_device * es58x_dev,u8 * raw_cmd,int raw_cmd_len)1230 static int es58x_copy_to_cmd_buf(struct es58x_device *es58x_dev,
1231 				 u8 *raw_cmd, int raw_cmd_len)
1232 {
1233 	if (es58x_dev->rx_cmd_buf_len + raw_cmd_len >
1234 	    es58x_dev->param->rx_urb_cmd_max_len)
1235 		return -EMSGSIZE;
1236 
1237 	memcpy(&es58x_dev->rx_cmd_buf.raw_cmd[es58x_dev->rx_cmd_buf_len],
1238 	       raw_cmd, raw_cmd_len);
1239 	es58x_dev->rx_cmd_buf_len += raw_cmd_len;
1240 
1241 	return 0;
1242 }
1243 
1244 /**
1245  * es58x_split_urb_try_recovery() - Try to recover bad URB sequences.
1246  * @es58x_dev: ES58X device.
1247  * @raw_cmd: pointer to the buffer we want to copy.
1248  * @raw_cmd_len: length of @raw_cmd.
1249  *
1250  * Under some rare conditions, we might get incorrect URBs from the
1251  * device. From our observations, one of the valid URB gets replaced
1252  * by one from the past. The full root cause is not identified.
1253  *
1254  * This function looks for the next start of frame in the urb buffer
1255  * in order to try to recover.
1256  *
1257  * Such behavior was not observed on the devices of the ES58X FD
1258  * family and only seems to impact the ES581.4.
1259  *
1260  * Return: the number of bytes dropped on success, -EBADMSG if recovery failed.
1261  */
es58x_split_urb_try_recovery(struct es58x_device * es58x_dev,u8 * raw_cmd,size_t raw_cmd_len)1262 static int es58x_split_urb_try_recovery(struct es58x_device *es58x_dev,
1263 					u8 *raw_cmd, size_t raw_cmd_len)
1264 {
1265 	union es58x_urb_cmd *urb_cmd;
1266 	signed int urb_cmd_len;
1267 	u16 sof;
1268 	int dropped_bytes = 0;
1269 
1270 	es58x_increment_rx_errors(es58x_dev);
1271 
1272 	while (raw_cmd_len > sizeof(sof)) {
1273 		urb_cmd = (union es58x_urb_cmd *)raw_cmd;
1274 		sof = get_unaligned_le16(&urb_cmd->sof);
1275 
1276 		if (sof == es58x_dev->param->rx_start_of_frame) {
1277 			urb_cmd_len = es58x_check_rx_urb(es58x_dev,
1278 							 urb_cmd, raw_cmd_len);
1279 			if ((urb_cmd_len == -ENODATA) || urb_cmd_len > 0) {
1280 				dev_info_ratelimited(es58x_dev->dev,
1281 						     "Recovery successful! Dropped %d bytes (urb_cmd_len: %d)\n",
1282 						     dropped_bytes,
1283 						     urb_cmd_len);
1284 				return dropped_bytes;
1285 			}
1286 		}
1287 		raw_cmd++;
1288 		raw_cmd_len--;
1289 		dropped_bytes++;
1290 	}
1291 
1292 	dev_warn_ratelimited(es58x_dev->dev, "%s: Recovery failed\n", __func__);
1293 	return -EBADMSG;
1294 }
1295 
1296 /**
1297  * es58x_handle_incomplete_cmd() - Reconstitute an URB command from
1298  *	different URB pieces.
1299  * @es58x_dev: ES58X device.
1300  * @urb: last urb buffer received.
1301  *
1302  * The device might split the URB commands in an arbitrary amount of
1303  * pieces. This function concatenates those in an URB buffer until a
1304  * full URB command is reconstituted and consume it.
1305  *
1306  * Return:
1307  * number of bytes consumed from @urb if successful.
1308  *
1309  * -ENODATA if the URB command is still incomplete.
1310  *
1311  * -EBADMSG if the URB command is incorrect.
1312  */
es58x_handle_incomplete_cmd(struct es58x_device * es58x_dev,struct urb * urb)1313 static signed int es58x_handle_incomplete_cmd(struct es58x_device *es58x_dev,
1314 					      struct urb *urb)
1315 {
1316 	size_t cpy_len;
1317 	signed int urb_cmd_len, tmp_cmd_buf_len, ret;
1318 
1319 	tmp_cmd_buf_len = es58x_dev->rx_cmd_buf_len;
1320 	cpy_len = min_t(int, es58x_dev->param->rx_urb_cmd_max_len -
1321 			es58x_dev->rx_cmd_buf_len, urb->actual_length);
1322 	ret = es58x_copy_to_cmd_buf(es58x_dev, urb->transfer_buffer, cpy_len);
1323 	if (ret < 0)
1324 		return ret;
1325 
1326 	urb_cmd_len = es58x_check_rx_urb(es58x_dev, &es58x_dev->rx_cmd_buf,
1327 					 es58x_dev->rx_cmd_buf_len);
1328 	if (urb_cmd_len == -ENODATA) {
1329 		return -ENODATA;
1330 	} else if (urb_cmd_len < 0) {
1331 		dev_err_ratelimited(es58x_dev->dev,
1332 				    "Could not reconstitute incomplete command from previous URB, dropping %d bytes\n",
1333 				    tmp_cmd_buf_len + urb->actual_length);
1334 		dev_err_ratelimited(es58x_dev->dev,
1335 				    "Error code: %pe, es58x_dev->rx_cmd_buf_len: %d, urb->actual_length: %u\n",
1336 				    ERR_PTR(urb_cmd_len),
1337 				    tmp_cmd_buf_len, urb->actual_length);
1338 		es58x_print_hex_dump(&es58x_dev->rx_cmd_buf, tmp_cmd_buf_len);
1339 		es58x_print_hex_dump(urb->transfer_buffer, urb->actual_length);
1340 		return urb->actual_length;
1341 	}
1342 
1343 	es58x_handle_urb_cmd(es58x_dev, &es58x_dev->rx_cmd_buf);
1344 	return urb_cmd_len - tmp_cmd_buf_len;	/* consumed length */
1345 }
1346 
1347 /**
1348  * es58x_split_urb() - Cut the received URB in individual URB commands.
1349  * @es58x_dev: ES58X device.
1350  * @urb: last urb buffer received.
1351  *
1352  * The device might send urb in bulk format (i.e. several URB commands
1353  * concatenated together). This function will split all the commands
1354  * contained in the urb.
1355  *
1356  * Return:
1357  * number of bytes consumed from @urb if successful.
1358  *
1359  * -ENODATA if the URB command is incomplete.
1360  *
1361  * -EBADMSG if the URB command is incorrect.
1362  */
es58x_split_urb(struct es58x_device * es58x_dev,struct urb * urb)1363 static signed int es58x_split_urb(struct es58x_device *es58x_dev,
1364 				  struct urb *urb)
1365 {
1366 	union es58x_urb_cmd *urb_cmd;
1367 	u8 *raw_cmd = urb->transfer_buffer;
1368 	s32 raw_cmd_len = urb->actual_length;
1369 	int ret;
1370 
1371 	if (es58x_dev->rx_cmd_buf_len != 0) {
1372 		ret = es58x_handle_incomplete_cmd(es58x_dev, urb);
1373 		if (ret != -ENODATA)
1374 			es58x_dev->rx_cmd_buf_len = 0;
1375 		if (ret < 0)
1376 			return ret;
1377 
1378 		raw_cmd += ret;
1379 		raw_cmd_len -= ret;
1380 	}
1381 
1382 	while (raw_cmd_len > 0) {
1383 		if (raw_cmd[0] == ES58X_HEARTBEAT) {
1384 			raw_cmd++;
1385 			raw_cmd_len--;
1386 			continue;
1387 		}
1388 		urb_cmd = (union es58x_urb_cmd *)raw_cmd;
1389 		ret = es58x_check_rx_urb(es58x_dev, urb_cmd, raw_cmd_len);
1390 		if (ret > 0) {
1391 			es58x_handle_urb_cmd(es58x_dev, urb_cmd);
1392 		} else if (ret == -ENODATA) {
1393 			es58x_copy_to_cmd_buf(es58x_dev, raw_cmd, raw_cmd_len);
1394 			return -ENODATA;
1395 		} else if (ret < 0) {
1396 			ret = es58x_split_urb_try_recovery(es58x_dev, raw_cmd,
1397 							   raw_cmd_len);
1398 			if (ret < 0)
1399 				return ret;
1400 		}
1401 		raw_cmd += ret;
1402 		raw_cmd_len -= ret;
1403 	}
1404 
1405 	return 0;
1406 }
1407 
1408 /**
1409  * es58x_read_bulk_callback() - Callback for reading data from device.
1410  * @urb: last urb buffer received.
1411  *
1412  * This function gets eventually called each time an URB is received
1413  * from the ES58X device.
1414  *
1415  * Checks urb status, calls read function and resubmits urb read
1416  * operation.
1417  */
es58x_read_bulk_callback(struct urb * urb)1418 static void es58x_read_bulk_callback(struct urb *urb)
1419 {
1420 	struct es58x_device *es58x_dev = urb->context;
1421 	const struct device *dev = es58x_dev->dev;
1422 	int i, ret;
1423 
1424 	switch (urb->status) {
1425 	case 0:		/* success */
1426 		break;
1427 
1428 	case -EOVERFLOW:
1429 		dev_err_ratelimited(dev, "%s: error %pe\n",
1430 				    __func__, ERR_PTR(urb->status));
1431 		es58x_print_hex_dump_debug(urb->transfer_buffer,
1432 					   urb->transfer_buffer_length);
1433 		goto resubmit_urb;
1434 
1435 	case -EPROTO:
1436 		dev_warn_ratelimited(dev, "%s: error %pe. Device unplugged?\n",
1437 				     __func__, ERR_PTR(urb->status));
1438 		goto free_urb;
1439 
1440 	case -ENOENT:
1441 	case -EPIPE:
1442 		dev_err_ratelimited(dev, "%s: error %pe\n",
1443 				    __func__, ERR_PTR(urb->status));
1444 		goto free_urb;
1445 
1446 	case -ESHUTDOWN:
1447 		dev_dbg_ratelimited(dev, "%s: error %pe\n",
1448 				    __func__, ERR_PTR(urb->status));
1449 		goto free_urb;
1450 
1451 	default:
1452 		dev_err_ratelimited(dev, "%s: error %pe\n",
1453 				    __func__, ERR_PTR(urb->status));
1454 		goto resubmit_urb;
1455 	}
1456 
1457 	ret = es58x_split_urb(es58x_dev, urb);
1458 	if ((ret != -ENODATA) && ret < 0) {
1459 		dev_err(es58x_dev->dev, "es58x_split_urb() returned error %pe",
1460 			ERR_PTR(ret));
1461 		es58x_print_hex_dump_debug(urb->transfer_buffer,
1462 					   urb->actual_length);
1463 
1464 		/* Because the urb command could not be parsed,
1465 		 * channel_id is not confirmed. Incrementing rx_errors
1466 		 * count of all channels.
1467 		 */
1468 		es58x_increment_rx_errors(es58x_dev);
1469 	}
1470 
1471  resubmit_urb:
1472 	usb_fill_bulk_urb(urb, es58x_dev->udev, es58x_dev->rx_pipe,
1473 			  urb->transfer_buffer, urb->transfer_buffer_length,
1474 			  es58x_read_bulk_callback, es58x_dev);
1475 
1476 	ret = usb_submit_urb(urb, GFP_ATOMIC);
1477 	if (ret == -ENODEV) {
1478 		for (i = 0; i < es58x_dev->num_can_ch; i++)
1479 			if (es58x_dev->netdev[i])
1480 				netif_device_detach(es58x_dev->netdev[i]);
1481 	} else if (ret)
1482 		dev_err_ratelimited(dev,
1483 				    "Failed resubmitting read bulk urb: %pe\n",
1484 				    ERR_PTR(ret));
1485 	return;
1486 
1487  free_urb:
1488 	usb_free_coherent(urb->dev, urb->transfer_buffer_length,
1489 			  urb->transfer_buffer, urb->transfer_dma);
1490 }
1491 
1492 /**
1493  * es58x_write_bulk_callback() - Callback after writing data to the device.
1494  * @urb: urb buffer which was previously submitted.
1495  *
1496  * This function gets eventually called each time an URB was sent to
1497  * the ES58X device.
1498  *
1499  * Puts the @urb back to the urbs idle anchor and tries to restart the
1500  * network queue.
1501  */
es58x_write_bulk_callback(struct urb * urb)1502 static void es58x_write_bulk_callback(struct urb *urb)
1503 {
1504 	struct net_device *netdev = urb->context;
1505 	struct es58x_device *es58x_dev = es58x_priv(netdev)->es58x_dev;
1506 
1507 	switch (urb->status) {
1508 	case 0:		/* success */
1509 		break;
1510 
1511 	case -EOVERFLOW:
1512 		if (net_ratelimit())
1513 			netdev_err(netdev, "%s: error %pe\n",
1514 				   __func__, ERR_PTR(urb->status));
1515 		es58x_print_hex_dump(urb->transfer_buffer,
1516 				     urb->transfer_buffer_length);
1517 		break;
1518 
1519 	case -ENOENT:
1520 		if (net_ratelimit())
1521 			netdev_dbg(netdev, "%s: error %pe\n",
1522 				   __func__, ERR_PTR(urb->status));
1523 		usb_free_coherent(urb->dev,
1524 				  es58x_dev->param->tx_urb_cmd_max_len,
1525 				  urb->transfer_buffer, urb->transfer_dma);
1526 		return;
1527 
1528 	default:
1529 		if (net_ratelimit())
1530 			netdev_info(netdev, "%s: error %pe\n",
1531 				    __func__, ERR_PTR(urb->status));
1532 		break;
1533 	}
1534 
1535 	usb_anchor_urb(urb, &es58x_dev->tx_urbs_idle);
1536 	atomic_inc(&es58x_dev->tx_urbs_idle_cnt);
1537 }
1538 
1539 /**
1540  * es58x_alloc_urb() - Allocate memory for an URB and its transfer
1541  *	buffer.
1542  * @es58x_dev: ES58X device.
1543  * @urb: URB to be allocated.
1544  * @buf: used to return DMA address of buffer.
1545  * @buf_len: requested buffer size.
1546  * @mem_flags: affect whether allocation may block.
1547  *
1548  * Allocates an URB and its @transfer_buffer and set its @transfer_dma
1549  * address.
1550  *
1551  * This function is used at start-up to allocate all RX URBs at once
1552  * and during run time for TX URBs.
1553  *
1554  * Return: zero on success, -ENOMEM if no memory is available.
1555  */
es58x_alloc_urb(struct es58x_device * es58x_dev,struct urb ** urb,u8 ** buf,size_t buf_len,gfp_t mem_flags)1556 static int es58x_alloc_urb(struct es58x_device *es58x_dev, struct urb **urb,
1557 			   u8 **buf, size_t buf_len, gfp_t mem_flags)
1558 {
1559 	*urb = usb_alloc_urb(0, mem_flags);
1560 	if (!*urb) {
1561 		dev_err(es58x_dev->dev, "No memory left for URBs\n");
1562 		return -ENOMEM;
1563 	}
1564 
1565 	*buf = usb_alloc_coherent(es58x_dev->udev, buf_len,
1566 				  mem_flags, &(*urb)->transfer_dma);
1567 	if (!*buf) {
1568 		dev_err(es58x_dev->dev, "No memory left for USB buffer\n");
1569 		usb_free_urb(*urb);
1570 		return -ENOMEM;
1571 	}
1572 
1573 	(*urb)->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1574 
1575 	return 0;
1576 }
1577 
1578 /**
1579  * es58x_get_tx_urb() - Get an URB for transmission.
1580  * @es58x_dev: ES58X device.
1581  *
1582  * Gets an URB from the idle urbs anchor or allocate a new one if the
1583  * anchor is empty.
1584  *
1585  * If there are more than ES58X_TX_URBS_MAX in the idle anchor, do
1586  * some garbage collection. The garbage collection is done here
1587  * instead of within es58x_write_bulk_callback() because
1588  * usb_free_coherent() should not be used in IRQ context:
1589  * c.f. WARN_ON(irqs_disabled()) in dma_free_attrs().
1590  *
1591  * Return: a pointer to an URB on success, NULL if no memory is
1592  * available.
1593  */
es58x_get_tx_urb(struct es58x_device * es58x_dev)1594 static struct urb *es58x_get_tx_urb(struct es58x_device *es58x_dev)
1595 {
1596 	atomic_t *idle_cnt = &es58x_dev->tx_urbs_idle_cnt;
1597 	struct urb *urb = usb_get_from_anchor(&es58x_dev->tx_urbs_idle);
1598 
1599 	if (!urb) {
1600 		size_t tx_buf_len;
1601 		u8 *buf;
1602 
1603 		tx_buf_len = es58x_dev->param->tx_urb_cmd_max_len;
1604 		if (es58x_alloc_urb(es58x_dev, &urb, &buf, tx_buf_len,
1605 				    GFP_ATOMIC))
1606 			return NULL;
1607 
1608 		usb_fill_bulk_urb(urb, es58x_dev->udev, es58x_dev->tx_pipe,
1609 				  buf, tx_buf_len, NULL, NULL);
1610 		return urb;
1611 	}
1612 
1613 	while (atomic_dec_return(idle_cnt) > ES58X_TX_URBS_MAX) {
1614 		/* Garbage collector */
1615 		struct urb *tmp = usb_get_from_anchor(&es58x_dev->tx_urbs_idle);
1616 
1617 		if (!tmp)
1618 			break;
1619 		usb_free_coherent(tmp->dev,
1620 				  es58x_dev->param->tx_urb_cmd_max_len,
1621 				  tmp->transfer_buffer, tmp->transfer_dma);
1622 		usb_free_urb(tmp);
1623 	}
1624 
1625 	return urb;
1626 }
1627 
1628 /**
1629  * es58x_submit_urb() - Send data to the device.
1630  * @es58x_dev: ES58X device.
1631  * @urb: URB to be sent.
1632  * @netdev: CAN network device.
1633  *
1634  * Return: zero on success, errno when any error occurs.
1635  */
es58x_submit_urb(struct es58x_device * es58x_dev,struct urb * urb,struct net_device * netdev)1636 static int es58x_submit_urb(struct es58x_device *es58x_dev, struct urb *urb,
1637 			    struct net_device *netdev)
1638 {
1639 	int ret;
1640 
1641 	es58x_set_crc(urb->transfer_buffer, urb->transfer_buffer_length);
1642 	usb_fill_bulk_urb(urb, es58x_dev->udev, es58x_dev->tx_pipe,
1643 			  urb->transfer_buffer, urb->transfer_buffer_length,
1644 			  es58x_write_bulk_callback, netdev);
1645 	usb_anchor_urb(urb, &es58x_dev->tx_urbs_busy);
1646 	ret = usb_submit_urb(urb, GFP_ATOMIC);
1647 	if (ret) {
1648 		netdev_err(netdev, "%s: USB send urb failure: %pe\n",
1649 			   __func__, ERR_PTR(ret));
1650 		usb_unanchor_urb(urb);
1651 		usb_free_coherent(urb->dev,
1652 				  es58x_dev->param->tx_urb_cmd_max_len,
1653 				  urb->transfer_buffer, urb->transfer_dma);
1654 	}
1655 	usb_free_urb(urb);
1656 
1657 	return ret;
1658 }
1659 
1660 /**
1661  * es58x_send_msg() - Prepare an URB and submit it.
1662  * @es58x_dev: ES58X device.
1663  * @cmd_type: Command type.
1664  * @cmd_id: Command ID.
1665  * @msg: ES58X message to be sent.
1666  * @msg_len: Length of @msg.
1667  * @channel_idx: Index of the network device.
1668  *
1669  * Creates an URB command from a given message, sets the header and the
1670  * CRC and then submits it.
1671  *
1672  * Return: zero on success, errno when any error occurs.
1673  */
es58x_send_msg(struct es58x_device * es58x_dev,u8 cmd_type,u8 cmd_id,const void * msg,u16 msg_len,int channel_idx)1674 int es58x_send_msg(struct es58x_device *es58x_dev, u8 cmd_type, u8 cmd_id,
1675 		   const void *msg, u16 msg_len, int channel_idx)
1676 {
1677 	struct net_device *netdev;
1678 	union es58x_urb_cmd *urb_cmd;
1679 	struct urb *urb;
1680 	int urb_cmd_len;
1681 
1682 	if (channel_idx == ES58X_CHANNEL_IDX_NA)
1683 		netdev = es58x_dev->netdev[0];	/* Default to first channel */
1684 	else
1685 		netdev = es58x_dev->netdev[channel_idx];
1686 
1687 	urb_cmd_len = es58x_get_urb_cmd_len(es58x_dev, msg_len);
1688 	if (urb_cmd_len > es58x_dev->param->tx_urb_cmd_max_len)
1689 		return -EOVERFLOW;
1690 
1691 	urb = es58x_get_tx_urb(es58x_dev);
1692 	if (!urb)
1693 		return -ENOMEM;
1694 
1695 	urb_cmd = urb->transfer_buffer;
1696 	es58x_dev->ops->fill_urb_header(urb_cmd, cmd_type, cmd_id,
1697 					channel_idx, msg_len);
1698 	memcpy(&urb_cmd->raw_cmd[es58x_dev->param->urb_cmd_header_len],
1699 	       msg, msg_len);
1700 	urb->transfer_buffer_length = urb_cmd_len;
1701 
1702 	return es58x_submit_urb(es58x_dev, urb, netdev);
1703 }
1704 
1705 /**
1706  * es58x_alloc_rx_urbs() - Allocate RX URBs.
1707  * @es58x_dev: ES58X device.
1708  *
1709  * Allocate URBs for reception and anchor them.
1710  *
1711  * Return: zero on success, errno when any error occurs.
1712  */
es58x_alloc_rx_urbs(struct es58x_device * es58x_dev)1713 static int es58x_alloc_rx_urbs(struct es58x_device *es58x_dev)
1714 {
1715 	const struct device *dev = es58x_dev->dev;
1716 	const struct es58x_parameters *param = es58x_dev->param;
1717 	size_t rx_buf_len = es58x_dev->rx_max_packet_size;
1718 	struct urb *urb;
1719 	u8 *buf;
1720 	int i;
1721 	int ret = -EINVAL;
1722 
1723 	for (i = 0; i < param->rx_urb_max; i++) {
1724 		ret = es58x_alloc_urb(es58x_dev, &urb, &buf, rx_buf_len,
1725 				      GFP_KERNEL);
1726 		if (ret)
1727 			break;
1728 
1729 		usb_fill_bulk_urb(urb, es58x_dev->udev, es58x_dev->rx_pipe,
1730 				  buf, rx_buf_len, es58x_read_bulk_callback,
1731 				  es58x_dev);
1732 		usb_anchor_urb(urb, &es58x_dev->rx_urbs);
1733 
1734 		ret = usb_submit_urb(urb, GFP_KERNEL);
1735 		if (ret) {
1736 			usb_unanchor_urb(urb);
1737 			usb_free_coherent(es58x_dev->udev, rx_buf_len,
1738 					  buf, urb->transfer_dma);
1739 			usb_free_urb(urb);
1740 			break;
1741 		}
1742 		usb_free_urb(urb);
1743 	}
1744 
1745 	if (i == 0) {
1746 		dev_err(dev, "%s: Could not setup any rx URBs\n", __func__);
1747 		return ret;
1748 	}
1749 	dev_dbg(dev, "%s: Allocated %d rx URBs each of size %zu\n",
1750 		__func__, i, rx_buf_len);
1751 
1752 	return ret;
1753 }
1754 
1755 /**
1756  * es58x_free_urbs() - Free all the TX and RX URBs.
1757  * @es58x_dev: ES58X device.
1758  */
es58x_free_urbs(struct es58x_device * es58x_dev)1759 static void es58x_free_urbs(struct es58x_device *es58x_dev)
1760 {
1761 	struct urb *urb;
1762 
1763 	if (!usb_wait_anchor_empty_timeout(&es58x_dev->tx_urbs_busy, 1000)) {
1764 		dev_err(es58x_dev->dev, "%s: Timeout, some TX urbs still remain\n",
1765 			__func__);
1766 		usb_kill_anchored_urbs(&es58x_dev->tx_urbs_busy);
1767 	}
1768 
1769 	while ((urb = usb_get_from_anchor(&es58x_dev->tx_urbs_idle)) != NULL) {
1770 		usb_free_coherent(urb->dev, es58x_dev->param->tx_urb_cmd_max_len,
1771 				  urb->transfer_buffer, urb->transfer_dma);
1772 		usb_free_urb(urb);
1773 		atomic_dec(&es58x_dev->tx_urbs_idle_cnt);
1774 	}
1775 	if (atomic_read(&es58x_dev->tx_urbs_idle_cnt))
1776 		dev_err(es58x_dev->dev,
1777 			"All idle urbs were freed but tx_urb_idle_cnt is %d\n",
1778 			atomic_read(&es58x_dev->tx_urbs_idle_cnt));
1779 
1780 	usb_kill_anchored_urbs(&es58x_dev->rx_urbs);
1781 }
1782 
1783 /**
1784  * es58x_open() - Enable the network device.
1785  * @netdev: CAN network device.
1786  *
1787  * Called when the network transitions to the up state. Allocate the
1788  * URB resources if needed and open the channel.
1789  *
1790  * Return: zero on success, errno when any error occurs.
1791  */
es58x_open(struct net_device * netdev)1792 static int es58x_open(struct net_device *netdev)
1793 {
1794 	struct es58x_device *es58x_dev = es58x_priv(netdev)->es58x_dev;
1795 	int ret;
1796 
1797 	if (!es58x_dev->opened_channel_cnt) {
1798 		ret = es58x_alloc_rx_urbs(es58x_dev);
1799 		if (ret)
1800 			return ret;
1801 
1802 		ret = es58x_set_realtime_diff_ns(es58x_dev);
1803 		if (ret)
1804 			goto free_urbs;
1805 	}
1806 
1807 	ret = open_candev(netdev);
1808 	if (ret)
1809 		goto free_urbs;
1810 
1811 	ret = es58x_dev->ops->enable_channel(es58x_priv(netdev));
1812 	if (ret)
1813 		goto free_urbs;
1814 
1815 	es58x_dev->opened_channel_cnt++;
1816 	netif_start_queue(netdev);
1817 
1818 	return ret;
1819 
1820  free_urbs:
1821 	if (!es58x_dev->opened_channel_cnt)
1822 		es58x_free_urbs(es58x_dev);
1823 	netdev_err(netdev, "%s: Could not open the network device: %pe\n",
1824 		   __func__, ERR_PTR(ret));
1825 
1826 	return ret;
1827 }
1828 
1829 /**
1830  * es58x_stop() - Disable the network device.
1831  * @netdev: CAN network device.
1832  *
1833  * Called when the network transitions to the down state. If all the
1834  * channels of the device are closed, free the URB resources which are
1835  * not needed anymore.
1836  *
1837  * Return: zero on success, errno when any error occurs.
1838  */
es58x_stop(struct net_device * netdev)1839 static int es58x_stop(struct net_device *netdev)
1840 {
1841 	struct es58x_priv *priv = es58x_priv(netdev);
1842 	struct es58x_device *es58x_dev = priv->es58x_dev;
1843 	int ret;
1844 
1845 	netif_stop_queue(netdev);
1846 	ret = es58x_dev->ops->disable_channel(priv);
1847 	if (ret)
1848 		return ret;
1849 
1850 	priv->can.state = CAN_STATE_STOPPED;
1851 	es58x_can_reset_echo_fifo(netdev);
1852 	close_candev(netdev);
1853 
1854 	es58x_flush_pending_tx_msg(netdev);
1855 
1856 	es58x_dev->opened_channel_cnt--;
1857 	if (!es58x_dev->opened_channel_cnt)
1858 		es58x_free_urbs(es58x_dev);
1859 
1860 	return 0;
1861 }
1862 
1863 /**
1864  * es58x_xmit_commit() - Send the bulk urb.
1865  * @netdev: CAN network device.
1866  *
1867  * Do the bulk send. This function should be called only once by bulk
1868  * transmission.
1869  *
1870  * Return: zero on success, errno when any error occurs.
1871  */
es58x_xmit_commit(struct net_device * netdev)1872 static int es58x_xmit_commit(struct net_device *netdev)
1873 {
1874 	struct es58x_priv *priv = es58x_priv(netdev);
1875 	int ret;
1876 
1877 	if (!es58x_is_can_state_active(netdev))
1878 		return -ENETDOWN;
1879 
1880 	if (es58x_is_echo_skb_threshold_reached(priv))
1881 		netif_stop_queue(netdev);
1882 
1883 	ret = es58x_submit_urb(priv->es58x_dev, priv->tx_urb, netdev);
1884 	if (ret == 0)
1885 		priv->tx_urb = NULL;
1886 
1887 	return ret;
1888 }
1889 
1890 /**
1891  * es58x_xmit_more() - Can we put more packets?
1892  * @priv: ES58X private parameters related to the network device.
1893  *
1894  * Return: true if we can put more, false if it is time to send.
1895  */
es58x_xmit_more(struct es58x_priv * priv)1896 static bool es58x_xmit_more(struct es58x_priv *priv)
1897 {
1898 	unsigned int free_slots =
1899 	    priv->can.echo_skb_max - (priv->tx_head - priv->tx_tail);
1900 
1901 	return netdev_xmit_more() && free_slots > 0 &&
1902 		priv->tx_can_msg_cnt < priv->es58x_dev->param->tx_bulk_max;
1903 }
1904 
1905 /**
1906  * es58x_start_xmit() - Transmit an skb.
1907  * @skb: socket buffer of a CAN message.
1908  * @netdev: CAN network device.
1909  *
1910  * Called when a packet needs to be transmitted.
1911  *
1912  * This function relies on Byte Queue Limits (BQL). The main benefit
1913  * is to increase the throughput by allowing bulk transfers
1914  * (c.f. xmit_more flag).
1915  *
1916  * Queues up to tx_bulk_max messages in &tx_urb buffer and does
1917  * a bulk send of all messages in one single URB.
1918  *
1919  * Return: NETDEV_TX_OK regardless of if we could transmit the @skb or
1920  *	had to drop it.
1921  */
es58x_start_xmit(struct sk_buff * skb,struct net_device * netdev)1922 static netdev_tx_t es58x_start_xmit(struct sk_buff *skb,
1923 				    struct net_device *netdev)
1924 {
1925 	struct es58x_priv *priv = es58x_priv(netdev);
1926 	struct es58x_device *es58x_dev = priv->es58x_dev;
1927 	unsigned int frame_len;
1928 	int ret;
1929 
1930 	if (can_dropped_invalid_skb(netdev, skb)) {
1931 		if (priv->tx_urb)
1932 			goto xmit_commit;
1933 		return NETDEV_TX_OK;
1934 	}
1935 
1936 	if (priv->tx_urb && priv->tx_can_msg_is_fd != can_is_canfd_skb(skb)) {
1937 		/* Can not do bulk send with mixed CAN and CAN FD frames. */
1938 		ret = es58x_xmit_commit(netdev);
1939 		if (ret)
1940 			goto drop_skb;
1941 	}
1942 
1943 	if (!priv->tx_urb) {
1944 		priv->tx_urb = es58x_get_tx_urb(es58x_dev);
1945 		if (!priv->tx_urb) {
1946 			ret = -ENOMEM;
1947 			goto drop_skb;
1948 		}
1949 		priv->tx_can_msg_cnt = 0;
1950 		priv->tx_can_msg_is_fd = can_is_canfd_skb(skb);
1951 	}
1952 
1953 	ret = es58x_dev->ops->tx_can_msg(priv, skb);
1954 	if (ret)
1955 		goto drop_skb;
1956 
1957 	frame_len = can_skb_get_frame_len(skb);
1958 	ret = can_put_echo_skb(skb, netdev,
1959 			       priv->tx_head & es58x_dev->param->fifo_mask,
1960 			       frame_len);
1961 	if (ret)
1962 		goto xmit_failure;
1963 	netdev_sent_queue(netdev, frame_len);
1964 
1965 	priv->tx_head++;
1966 	priv->tx_can_msg_cnt++;
1967 
1968  xmit_commit:
1969 	if (!es58x_xmit_more(priv)) {
1970 		ret = es58x_xmit_commit(netdev);
1971 		if (ret)
1972 			goto xmit_failure;
1973 	}
1974 
1975 	return NETDEV_TX_OK;
1976 
1977  drop_skb:
1978 	dev_kfree_skb(skb);
1979 	netdev->stats.tx_dropped++;
1980  xmit_failure:
1981 	netdev_warn(netdev, "%s: send message failure: %pe\n",
1982 		    __func__, ERR_PTR(ret));
1983 	netdev->stats.tx_errors++;
1984 	es58x_flush_pending_tx_msg(netdev);
1985 	return NETDEV_TX_OK;
1986 }
1987 
1988 static const struct net_device_ops es58x_netdev_ops = {
1989 	.ndo_open = es58x_open,
1990 	.ndo_stop = es58x_stop,
1991 	.ndo_start_xmit = es58x_start_xmit
1992 };
1993 
1994 /**
1995  * es58x_set_mode() - Change network device mode.
1996  * @netdev: CAN network device.
1997  * @mode: either %CAN_MODE_START, %CAN_MODE_STOP or %CAN_MODE_SLEEP
1998  *
1999  * Currently, this function is only used to stop and restart the
2000  * channel during a bus off event (c.f. es58x_rx_err_msg() and
2001  * drivers/net/can/dev.c:can_restart() which are the two only
2002  * callers).
2003  *
2004  * Return: zero on success, errno when any error occurs.
2005  */
es58x_set_mode(struct net_device * netdev,enum can_mode mode)2006 static int es58x_set_mode(struct net_device *netdev, enum can_mode mode)
2007 {
2008 	struct es58x_priv *priv = es58x_priv(netdev);
2009 
2010 	switch (mode) {
2011 	case CAN_MODE_START:
2012 		switch (priv->can.state) {
2013 		case CAN_STATE_BUS_OFF:
2014 			return priv->es58x_dev->ops->enable_channel(priv);
2015 
2016 		case CAN_STATE_STOPPED:
2017 			return es58x_open(netdev);
2018 
2019 		case CAN_STATE_ERROR_ACTIVE:
2020 		case CAN_STATE_ERROR_WARNING:
2021 		case CAN_STATE_ERROR_PASSIVE:
2022 		default:
2023 			return 0;
2024 		}
2025 
2026 	case CAN_MODE_STOP:
2027 		switch (priv->can.state) {
2028 		case CAN_STATE_STOPPED:
2029 			return 0;
2030 
2031 		case CAN_STATE_ERROR_ACTIVE:
2032 		case CAN_STATE_ERROR_WARNING:
2033 		case CAN_STATE_ERROR_PASSIVE:
2034 		case CAN_STATE_BUS_OFF:
2035 		default:
2036 			return priv->es58x_dev->ops->disable_channel(priv);
2037 		}
2038 
2039 	case CAN_MODE_SLEEP:
2040 	default:
2041 		return -EOPNOTSUPP;
2042 	}
2043 }
2044 
2045 /**
2046  * es58x_init_priv() - Initialize private parameters.
2047  * @es58x_dev: ES58X device.
2048  * @priv: ES58X private parameters related to the network device.
2049  * @channel_idx: Index of the network device.
2050  */
es58x_init_priv(struct es58x_device * es58x_dev,struct es58x_priv * priv,int channel_idx)2051 static void es58x_init_priv(struct es58x_device *es58x_dev,
2052 			    struct es58x_priv *priv, int channel_idx)
2053 {
2054 	const struct es58x_parameters *param = es58x_dev->param;
2055 	struct can_priv *can = &priv->can;
2056 
2057 	priv->es58x_dev = es58x_dev;
2058 	priv->channel_idx = channel_idx;
2059 	priv->tx_urb = NULL;
2060 	priv->tx_can_msg_cnt = 0;
2061 
2062 	can->bittiming_const = param->bittiming_const;
2063 	if (param->ctrlmode_supported & CAN_CTRLMODE_FD) {
2064 		can->data_bittiming_const = param->data_bittiming_const;
2065 		can->tdc_const = param->tdc_const;
2066 	}
2067 	can->bitrate_max = param->bitrate_max;
2068 	can->clock = param->clock;
2069 	can->state = CAN_STATE_STOPPED;
2070 	can->ctrlmode_supported = param->ctrlmode_supported;
2071 	can->do_set_mode = es58x_set_mode;
2072 }
2073 
2074 /**
2075  * es58x_init_netdev() - Initialize the network device.
2076  * @es58x_dev: ES58X device.
2077  * @channel_idx: Index of the network device.
2078  *
2079  * Return: zero on success, errno when any error occurs.
2080  */
es58x_init_netdev(struct es58x_device * es58x_dev,int channel_idx)2081 static int es58x_init_netdev(struct es58x_device *es58x_dev, int channel_idx)
2082 {
2083 	struct net_device *netdev;
2084 	struct device *dev = es58x_dev->dev;
2085 	int ret;
2086 
2087 	netdev = alloc_candev(sizeof(struct es58x_priv),
2088 			      es58x_dev->param->fifo_mask + 1);
2089 	if (!netdev) {
2090 		dev_err(dev, "Could not allocate candev\n");
2091 		return -ENOMEM;
2092 	}
2093 	SET_NETDEV_DEV(netdev, dev);
2094 	es58x_dev->netdev[channel_idx] = netdev;
2095 	es58x_init_priv(es58x_dev, es58x_priv(netdev), channel_idx);
2096 
2097 	netdev->netdev_ops = &es58x_netdev_ops;
2098 	netdev->flags |= IFF_ECHO;	/* We support local echo */
2099 
2100 	ret = register_candev(netdev);
2101 	if (ret) {
2102 		es58x_dev->netdev[channel_idx] = NULL;
2103 		free_candev(netdev);
2104 		return ret;
2105 	}
2106 
2107 	netdev_queue_set_dql_min_limit(netdev_get_tx_queue(netdev, 0),
2108 				       es58x_dev->param->dql_min_limit);
2109 
2110 	return ret;
2111 }
2112 
2113 /**
2114  * es58x_free_netdevs() - Release all network resources of the device.
2115  * @es58x_dev: ES58X device.
2116  */
es58x_free_netdevs(struct es58x_device * es58x_dev)2117 static void es58x_free_netdevs(struct es58x_device *es58x_dev)
2118 {
2119 	int i;
2120 
2121 	for (i = 0; i < es58x_dev->num_can_ch; i++) {
2122 		struct net_device *netdev = es58x_dev->netdev[i];
2123 
2124 		if (!netdev)
2125 			continue;
2126 		unregister_candev(netdev);
2127 		es58x_dev->netdev[i] = NULL;
2128 		free_candev(netdev);
2129 	}
2130 }
2131 
2132 /**
2133  * es58x_get_product_info() - Get the product information and print them.
2134  * @es58x_dev: ES58X device.
2135  *
2136  * Do a synchronous call to get the product information.
2137  *
2138  * Return: zero on success, errno when any error occurs.
2139  */
es58x_get_product_info(struct es58x_device * es58x_dev)2140 static int es58x_get_product_info(struct es58x_device *es58x_dev)
2141 {
2142 	struct usb_device *udev = es58x_dev->udev;
2143 	const int es58x_prod_info_idx = 6;
2144 	/* Empirical tests show a prod_info length of maximum 83,
2145 	 * below should be more than enough.
2146 	 */
2147 	const size_t prod_info_len = 127;
2148 	char *prod_info;
2149 	int ret;
2150 
2151 	prod_info = kmalloc(prod_info_len, GFP_KERNEL);
2152 	if (!prod_info)
2153 		return -ENOMEM;
2154 
2155 	ret = usb_string(udev, es58x_prod_info_idx, prod_info, prod_info_len);
2156 	if (ret < 0) {
2157 		dev_err(es58x_dev->dev,
2158 			"%s: Could not read the product info: %pe\n",
2159 			__func__, ERR_PTR(ret));
2160 		goto out_free;
2161 	}
2162 	if (ret >= prod_info_len - 1) {
2163 		dev_warn(es58x_dev->dev,
2164 			 "%s: Buffer is too small, result might be truncated\n",
2165 			 __func__);
2166 	}
2167 	dev_info(es58x_dev->dev, "Product info: %s\n", prod_info);
2168 
2169  out_free:
2170 	kfree(prod_info);
2171 	return ret < 0 ? ret : 0;
2172 }
2173 
2174 /**
2175  * es58x_init_es58x_dev() - Initialize the ES58X device.
2176  * @intf: USB interface.
2177  * @driver_info: Quirks of the device.
2178  *
2179  * Return: pointer to an ES58X device on success, error pointer when
2180  *	any error occurs.
2181  */
es58x_init_es58x_dev(struct usb_interface * intf,kernel_ulong_t driver_info)2182 static struct es58x_device *es58x_init_es58x_dev(struct usb_interface *intf,
2183 						 kernel_ulong_t driver_info)
2184 {
2185 	struct device *dev = &intf->dev;
2186 	struct es58x_device *es58x_dev;
2187 	const struct es58x_parameters *param;
2188 	const struct es58x_operators *ops;
2189 	struct usb_device *udev = interface_to_usbdev(intf);
2190 	struct usb_endpoint_descriptor *ep_in, *ep_out;
2191 	int ret;
2192 
2193 	dev_info(dev,
2194 		 "Starting %s %s (Serial Number %s) driver version %s\n",
2195 		 udev->manufacturer, udev->product, udev->serial, DRV_VERSION);
2196 
2197 	ret = usb_find_common_endpoints(intf->cur_altsetting, &ep_in, &ep_out,
2198 					NULL, NULL);
2199 	if (ret)
2200 		return ERR_PTR(ret);
2201 
2202 	if (driver_info & ES58X_FD_FAMILY) {
2203 		param = &es58x_fd_param;
2204 		ops = &es58x_fd_ops;
2205 	} else {
2206 		param = &es581_4_param;
2207 		ops = &es581_4_ops;
2208 	}
2209 
2210 	es58x_dev = devm_kzalloc(dev, es58x_sizeof_es58x_device(param),
2211 				 GFP_KERNEL);
2212 	if (!es58x_dev)
2213 		return ERR_PTR(-ENOMEM);
2214 
2215 	es58x_dev->param = param;
2216 	es58x_dev->ops = ops;
2217 	es58x_dev->dev = dev;
2218 	es58x_dev->udev = udev;
2219 
2220 	if (driver_info & ES58X_DUAL_CHANNEL)
2221 		es58x_dev->num_can_ch = 2;
2222 	else
2223 		es58x_dev->num_can_ch = 1;
2224 
2225 	init_usb_anchor(&es58x_dev->rx_urbs);
2226 	init_usb_anchor(&es58x_dev->tx_urbs_idle);
2227 	init_usb_anchor(&es58x_dev->tx_urbs_busy);
2228 	atomic_set(&es58x_dev->tx_urbs_idle_cnt, 0);
2229 	usb_set_intfdata(intf, es58x_dev);
2230 
2231 	es58x_dev->rx_pipe = usb_rcvbulkpipe(es58x_dev->udev,
2232 					     ep_in->bEndpointAddress);
2233 	es58x_dev->tx_pipe = usb_sndbulkpipe(es58x_dev->udev,
2234 					     ep_out->bEndpointAddress);
2235 	es58x_dev->rx_max_packet_size = le16_to_cpu(ep_in->wMaxPacketSize);
2236 
2237 	return es58x_dev;
2238 }
2239 
2240 /**
2241  * es58x_probe() - Initialize the USB device.
2242  * @intf: USB interface.
2243  * @id: USB device ID.
2244  *
2245  * Return: zero on success, -ENODEV if the interface is not supported
2246  * or errno when any other error occurs.
2247  */
es58x_probe(struct usb_interface * intf,const struct usb_device_id * id)2248 static int es58x_probe(struct usb_interface *intf,
2249 		       const struct usb_device_id *id)
2250 {
2251 	struct es58x_device *es58x_dev;
2252 	int ch_idx, ret;
2253 
2254 	es58x_dev = es58x_init_es58x_dev(intf, id->driver_info);
2255 	if (IS_ERR(es58x_dev))
2256 		return PTR_ERR(es58x_dev);
2257 
2258 	ret = es58x_get_product_info(es58x_dev);
2259 	if (ret)
2260 		return ret;
2261 
2262 	for (ch_idx = 0; ch_idx < es58x_dev->num_can_ch; ch_idx++) {
2263 		ret = es58x_init_netdev(es58x_dev, ch_idx);
2264 		if (ret) {
2265 			es58x_free_netdevs(es58x_dev);
2266 			return ret;
2267 		}
2268 	}
2269 
2270 	return ret;
2271 }
2272 
2273 /**
2274  * es58x_disconnect() - Disconnect the USB device.
2275  * @intf: USB interface
2276  *
2277  * Called by the usb core when driver is unloaded or device is
2278  * removed.
2279  */
es58x_disconnect(struct usb_interface * intf)2280 static void es58x_disconnect(struct usb_interface *intf)
2281 {
2282 	struct es58x_device *es58x_dev = usb_get_intfdata(intf);
2283 
2284 	dev_info(&intf->dev, "Disconnecting %s %s\n",
2285 		 es58x_dev->udev->manufacturer, es58x_dev->udev->product);
2286 
2287 	es58x_free_netdevs(es58x_dev);
2288 	es58x_free_urbs(es58x_dev);
2289 	usb_set_intfdata(intf, NULL);
2290 }
2291 
2292 static struct usb_driver es58x_driver = {
2293 	.name = ES58X_MODULE_NAME,
2294 	.probe = es58x_probe,
2295 	.disconnect = es58x_disconnect,
2296 	.id_table = es58x_id_table
2297 };
2298 
2299 module_usb_driver(es58x_driver);
2300