xref: /openbmc/linux/drivers/net/usb/cx82310_eth.c (revision 835fd614)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Driver for USB ethernet port of Conexant CX82310-based ADSL routers
4  * Copyright (C) 2010 by Ondrej Zary
5  * some parts inspired by the cxacru driver
6  */
7 
8 #include <linux/module.h>
9 #include <linux/netdevice.h>
10 #include <linux/etherdevice.h>
11 #include <linux/ethtool.h>
12 #include <linux/workqueue.h>
13 #include <linux/mii.h>
14 #include <linux/usb.h>
15 #include <linux/usb/usbnet.h>
16 
17 enum cx82310_cmd {
18 	CMD_START		= 0x84,	/* no effect? */
19 	CMD_STOP		= 0x85,	/* no effect? */
20 	CMD_GET_STATUS		= 0x90,	/* returns nothing? */
21 	CMD_GET_MAC_ADDR	= 0x91,	/* read MAC address */
22 	CMD_GET_LINK_STATUS	= 0x92,	/* not useful, link is always up */
23 	CMD_ETHERNET_MODE	= 0x99,	/* unknown, needed during init */
24 };
25 
26 enum cx82310_status {
27 	STATUS_UNDEFINED,
28 	STATUS_SUCCESS,
29 	STATUS_ERROR,
30 	STATUS_UNSUPPORTED,
31 	STATUS_UNIMPLEMENTED,
32 	STATUS_PARAMETER_ERROR,
33 	STATUS_DBG_LOOPBACK,
34 };
35 
36 #define CMD_PACKET_SIZE	64
37 #define CMD_TIMEOUT	100
38 #define CMD_REPLY_RETRY 5
39 
40 #define CX82310_MTU	1514
41 #define CMD_EP		0x01
42 
43 struct cx82310_priv {
44 	struct work_struct reenable_work;
45 	struct usbnet *dev;
46 };
47 
48 /*
49  * execute control command
50  *  - optionally send some data (command parameters)
51  *  - optionally wait for the reply
52  *  - optionally read some data from the reply
53  */
54 static int cx82310_cmd(struct usbnet *dev, enum cx82310_cmd cmd, bool reply,
55 		       u8 *wdata, int wlen, u8 *rdata, int rlen)
56 {
57 	int actual_len, retries, ret;
58 	struct usb_device *udev = dev->udev;
59 	u8 *buf = kzalloc(CMD_PACKET_SIZE, GFP_KERNEL);
60 
61 	if (!buf)
62 		return -ENOMEM;
63 
64 	/* create command packet */
65 	buf[0] = cmd;
66 	if (wdata)
67 		memcpy(buf + 4, wdata, min_t(int, wlen, CMD_PACKET_SIZE - 4));
68 
69 	/* send command packet */
70 	ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, CMD_EP), buf,
71 			   CMD_PACKET_SIZE, &actual_len, CMD_TIMEOUT);
72 	if (ret < 0) {
73 		if (cmd != CMD_GET_LINK_STATUS)
74 			netdev_err(dev->net, "send command %#x: error %d\n",
75 				   cmd, ret);
76 		goto end;
77 	}
78 
79 	if (reply) {
80 		/* wait for reply, retry if it's empty */
81 		for (retries = 0; retries < CMD_REPLY_RETRY; retries++) {
82 			ret = usb_bulk_msg(udev, usb_rcvbulkpipe(udev, CMD_EP),
83 					   buf, CMD_PACKET_SIZE, &actual_len,
84 					   CMD_TIMEOUT);
85 			if (ret < 0) {
86 				if (cmd != CMD_GET_LINK_STATUS)
87 					netdev_err(dev->net, "reply receive error %d\n",
88 						   ret);
89 				goto end;
90 			}
91 			if (actual_len > 0)
92 				break;
93 		}
94 		if (actual_len == 0) {
95 			netdev_err(dev->net, "no reply to command %#x\n", cmd);
96 			ret = -EIO;
97 			goto end;
98 		}
99 		if (buf[0] != cmd) {
100 			netdev_err(dev->net, "got reply to command %#x, expected: %#x\n",
101 				   buf[0], cmd);
102 			ret = -EIO;
103 			goto end;
104 		}
105 		if (buf[1] != STATUS_SUCCESS) {
106 			netdev_err(dev->net, "command %#x failed: %#x\n", cmd,
107 				   buf[1]);
108 			ret = -EIO;
109 			goto end;
110 		}
111 		if (rdata)
112 			memcpy(rdata, buf + 4,
113 			       min_t(int, rlen, CMD_PACKET_SIZE - 4));
114 	}
115 end:
116 	kfree(buf);
117 	return ret;
118 }
119 
120 static int cx82310_enable_ethernet(struct usbnet *dev)
121 {
122 	int ret = cx82310_cmd(dev, CMD_ETHERNET_MODE, true, "\x01", 1, NULL, 0);
123 
124 	if (ret)
125 		netdev_err(dev->net, "unable to enable ethernet mode: %d\n",
126 			   ret);
127 	return ret;
128 }
129 
130 static void cx82310_reenable_work(struct work_struct *work)
131 {
132 	struct cx82310_priv *priv = container_of(work, struct cx82310_priv,
133 						 reenable_work);
134 	cx82310_enable_ethernet(priv->dev);
135 }
136 
137 #define partial_len	data[0]		/* length of partial packet data */
138 #define partial_rem	data[1]		/* remaining (missing) data length */
139 #define partial_data	data[2]		/* partial packet data */
140 
141 static int cx82310_bind(struct usbnet *dev, struct usb_interface *intf)
142 {
143 	int ret;
144 	char buf[15];
145 	struct usb_device *udev = dev->udev;
146 	u8 link[3];
147 	int timeout = 50;
148 	struct cx82310_priv *priv;
149 
150 	/* avoid ADSL modems - continue only if iProduct is "USB NET CARD" */
151 	if (usb_string(udev, udev->descriptor.iProduct, buf, sizeof(buf)) > 0
152 	    && strcmp(buf, "USB NET CARD")) {
153 		dev_info(&udev->dev, "ignoring: probably an ADSL modem\n");
154 		return -ENODEV;
155 	}
156 
157 	ret = usbnet_get_endpoints(dev, intf);
158 	if (ret)
159 		return ret;
160 
161 	/*
162 	 * this must not include ethernet header as the device can send partial
163 	 * packets with no header (and sometimes even empty URBs)
164 	 */
165 	dev->net->hard_header_len = 0;
166 	/* we can send at most 1514 bytes of data (+ 2-byte header) per URB */
167 	dev->hard_mtu = CX82310_MTU + 2;
168 	/* we can receive URBs up to 4KB from the device */
169 	dev->rx_urb_size = 4096;
170 
171 	dev->partial_data = (unsigned long) kmalloc(dev->hard_mtu, GFP_KERNEL);
172 	if (!dev->partial_data)
173 		return -ENOMEM;
174 
175 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
176 	if (!priv) {
177 		ret = -ENOMEM;
178 		goto err_partial;
179 	}
180 	dev->driver_priv = priv;
181 	INIT_WORK(&priv->reenable_work, cx82310_reenable_work);
182 	priv->dev = dev;
183 
184 	/* wait for firmware to become ready (indicated by the link being up) */
185 	while (--timeout) {
186 		ret = cx82310_cmd(dev, CMD_GET_LINK_STATUS, true, NULL, 0,
187 				  link, sizeof(link));
188 		/* the command can time out during boot - it's not an error */
189 		if (!ret && link[0] == 1 && link[2] == 1)
190 			break;
191 		msleep(500);
192 	}
193 	if (!timeout) {
194 		netdev_err(dev->net, "firmware not ready in time\n");
195 		ret = -ETIMEDOUT;
196 		goto err;
197 	}
198 
199 	/* enable ethernet mode (?) */
200 	if (cx82310_enable_ethernet(dev))
201 		goto err;
202 
203 	/* get the MAC address */
204 	ret = cx82310_cmd(dev, CMD_GET_MAC_ADDR, true, NULL, 0,
205 			  dev->net->dev_addr, ETH_ALEN);
206 	if (ret) {
207 		netdev_err(dev->net, "unable to read MAC address: %d\n", ret);
208 		goto err;
209 	}
210 
211 	/* start (does not seem to have any effect?) */
212 	ret = cx82310_cmd(dev, CMD_START, false, NULL, 0, NULL, 0);
213 	if (ret)
214 		goto err;
215 
216 	return 0;
217 err:
218 	kfree(dev->driver_priv);
219 err_partial:
220 	kfree((void *)dev->partial_data);
221 	return ret;
222 }
223 
224 static void cx82310_unbind(struct usbnet *dev, struct usb_interface *intf)
225 {
226 	struct cx82310_priv *priv = dev->driver_priv;
227 
228 	kfree((void *)dev->partial_data);
229 	cancel_work_sync(&priv->reenable_work);
230 	kfree(dev->driver_priv);
231 }
232 
233 /*
234  * RX is NOT easy - we can receive multiple packets per skb, each having 2-byte
235  * packet length at the beginning.
236  * The last packet might be incomplete (when it crosses the 4KB URB size),
237  * continuing in the next skb (without any headers).
238  * If a packet has odd length, there is one extra byte at the end (before next
239  * packet or at the end of the URB).
240  */
241 static int cx82310_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
242 {
243 	int len;
244 	struct sk_buff *skb2;
245 	struct cx82310_priv *priv = dev->driver_priv;
246 
247 	/*
248 	 * If the last skb ended with an incomplete packet, this skb contains
249 	 * end of that packet at the beginning.
250 	 */
251 	if (dev->partial_rem) {
252 		len = dev->partial_len + dev->partial_rem;
253 		skb2 = alloc_skb(len, GFP_ATOMIC);
254 		if (!skb2)
255 			return 0;
256 		skb_put(skb2, len);
257 		memcpy(skb2->data, (void *)dev->partial_data,
258 		       dev->partial_len);
259 		memcpy(skb2->data + dev->partial_len, skb->data,
260 		       dev->partial_rem);
261 		usbnet_skb_return(dev, skb2);
262 		skb_pull(skb, (dev->partial_rem + 1) & ~1);
263 		dev->partial_rem = 0;
264 		if (skb->len < 2)
265 			return 1;
266 	}
267 
268 	/* a skb can contain multiple packets */
269 	while (skb->len > 1) {
270 		/* first two bytes are packet length */
271 		len = skb->data[0] | (skb->data[1] << 8);
272 		skb_pull(skb, 2);
273 
274 		/* if last packet in the skb, let usbnet to process it */
275 		if (len == skb->len || len + 1 == skb->len) {
276 			skb_trim(skb, len);
277 			break;
278 		}
279 
280 		if (len == 0xffff) {
281 			netdev_info(dev->net, "router was rebooted, re-enabling ethernet mode");
282 			schedule_work(&priv->reenable_work);
283 		} else if (len > CX82310_MTU) {
284 			netdev_err(dev->net, "RX packet too long: %d B\n", len);
285 			return 0;
286 		}
287 
288 		/* incomplete packet, save it for the next skb */
289 		if (len > skb->len) {
290 			dev->partial_len = skb->len;
291 			dev->partial_rem = len - skb->len;
292 			memcpy((void *)dev->partial_data, skb->data,
293 			       dev->partial_len);
294 			skb_pull(skb, skb->len);
295 			break;
296 		}
297 
298 		skb2 = alloc_skb(len, GFP_ATOMIC);
299 		if (!skb2)
300 			return 0;
301 		skb_put(skb2, len);
302 		memcpy(skb2->data, skb->data, len);
303 		/* process the packet */
304 		usbnet_skb_return(dev, skb2);
305 
306 		skb_pull(skb, (len + 1) & ~1);
307 	}
308 
309 	/* let usbnet process the last packet */
310 	return 1;
311 }
312 
313 /* TX is easy, just add 2 bytes of length at the beginning */
314 static struct sk_buff *cx82310_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
315 				       gfp_t flags)
316 {
317 	int len = skb->len;
318 
319 	if (skb_cow_head(skb, 2)) {
320 		dev_kfree_skb_any(skb);
321 		return NULL;
322 	}
323 	skb_push(skb, 2);
324 
325 	skb->data[0] = len;
326 	skb->data[1] = len >> 8;
327 
328 	return skb;
329 }
330 
331 
332 static const struct driver_info	cx82310_info = {
333 	.description	= "Conexant CX82310 USB ethernet",
334 	.flags		= FLAG_ETHER,
335 	.bind		= cx82310_bind,
336 	.unbind		= cx82310_unbind,
337 	.rx_fixup	= cx82310_rx_fixup,
338 	.tx_fixup	= cx82310_tx_fixup,
339 };
340 
341 #define USB_DEVICE_CLASS(vend, prod, cl, sc, pr) \
342 	.match_flags = USB_DEVICE_ID_MATCH_DEVICE | \
343 		       USB_DEVICE_ID_MATCH_DEV_INFO, \
344 	.idVendor = (vend), \
345 	.idProduct = (prod), \
346 	.bDeviceClass = (cl), \
347 	.bDeviceSubClass = (sc), \
348 	.bDeviceProtocol = (pr)
349 
350 static const struct usb_device_id products[] = {
351 	{
352 		USB_DEVICE_CLASS(0x0572, 0xcb01, 0xff, 0, 0),
353 		.driver_info = (unsigned long) &cx82310_info
354 	},
355 	{ },
356 };
357 MODULE_DEVICE_TABLE(usb, products);
358 
359 static struct usb_driver cx82310_driver = {
360 	.name		= "cx82310_eth",
361 	.id_table	= products,
362 	.probe		= usbnet_probe,
363 	.disconnect	= usbnet_disconnect,
364 	.suspend	= usbnet_suspend,
365 	.resume		= usbnet_resume,
366 	.disable_hub_initiated_lpm = 1,
367 };
368 
369 module_usb_driver(cx82310_driver);
370 
371 MODULE_AUTHOR("Ondrej Zary");
372 MODULE_DESCRIPTION("Conexant CX82310-based ADSL router USB ethernet driver");
373 MODULE_LICENSE("GPL");
374