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