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