1 // SPDX-License-Identifier: GPL-2.0-or-later
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
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>
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 */
32 STATUS_PARAMETER_ERROR,
36 #define CMD_PACKET_SIZE 64
37 #define CMD_TIMEOUT 100
38 #define CMD_REPLY_RETRY 5
40 #define CX82310_MTU 1514
44 struct work_struct reenable_work;
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
54 static int cx82310_cmd(struct usbnet *dev, enum cx82310_cmd cmd, bool reply,
55 u8 *wdata, int wlen, u8 *rdata, int rlen)
57 int actual_len, retries, ret;
58 struct usb_device *udev = dev->udev;
59 u8 *buf = kzalloc(CMD_PACKET_SIZE, GFP_KERNEL);
64 /* create command packet */
67 memcpy(buf + 4, wdata, min_t(int, wlen, CMD_PACKET_SIZE - 4));
69 /* send command packet */
70 ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, CMD_EP), buf,
71 CMD_PACKET_SIZE, &actual_len, CMD_TIMEOUT);
73 if (cmd != CMD_GET_LINK_STATUS)
74 netdev_err(dev->net, "send command %#x: error %d\n",
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,
86 if (cmd != CMD_GET_LINK_STATUS)
87 netdev_err(dev->net, "reply receive error %d\n",
94 if (actual_len == 0) {
95 netdev_err(dev->net, "no reply to command %#x\n", cmd);
100 netdev_err(dev->net, "got reply to command %#x, expected: %#x\n",
105 if (buf[1] != STATUS_SUCCESS) {
106 netdev_err(dev->net, "command %#x failed: %#x\n", cmd,
112 memcpy(rdata, buf + 4,
113 min_t(int, rlen, CMD_PACKET_SIZE - 4));
120 static int cx82310_enable_ethernet(struct usbnet *dev)
122 int ret = cx82310_cmd(dev, CMD_ETHERNET_MODE, true, "\x01", 1, NULL, 0);
125 netdev_err(dev->net, "unable to enable ethernet mode: %d\n",
130 static void cx82310_reenable_work(struct work_struct *work)
132 struct cx82310_priv *priv = container_of(work, struct cx82310_priv,
134 cx82310_enable_ethernet(priv->dev);
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 */
141 static int cx82310_bind(struct usbnet *dev, struct usb_interface *intf)
145 struct usb_device *udev = dev->udev;
148 struct cx82310_priv *priv;
151 /* avoid ADSL modems - continue only if iProduct is "USB NET CARD" */
152 if (usb_string(udev, udev->descriptor.iProduct, buf, sizeof(buf)) > 0
153 && strcmp(buf, "USB NET CARD")) {
154 dev_info(&udev->dev, "ignoring: probably an ADSL modem\n");
158 ret = usbnet_get_endpoints(dev, intf);
163 * this must not include ethernet header as the device can send partial
164 * packets with no header (and sometimes even empty URBs)
166 dev->net->hard_header_len = 0;
167 /* we can send at most 1514 bytes of data (+ 2-byte header) per URB */
168 dev->hard_mtu = CX82310_MTU + 2;
169 /* we can receive URBs up to 4KB from the device */
170 dev->rx_urb_size = 4096;
172 dev->partial_data = (unsigned long) kmalloc(dev->hard_mtu, GFP_KERNEL);
173 if (!dev->partial_data)
176 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
181 dev->driver_priv = priv;
182 INIT_WORK(&priv->reenable_work, cx82310_reenable_work);
185 /* wait for firmware to become ready (indicated by the link being up) */
187 ret = cx82310_cmd(dev, CMD_GET_LINK_STATUS, true, NULL, 0,
189 /* the command can time out during boot - it's not an error */
190 if (!ret && link[0] == 1 && link[2] == 1)
195 netdev_err(dev->net, "firmware not ready in time\n");
200 /* enable ethernet mode (?) */
201 ret = cx82310_enable_ethernet(dev);
205 /* get the MAC address */
206 ret = cx82310_cmd(dev, CMD_GET_MAC_ADDR, true, NULL, 0, addr, ETH_ALEN);
208 netdev_err(dev->net, "unable to read MAC address: %d\n", ret);
211 eth_hw_addr_set(dev->net, addr);
213 /* start (does not seem to have any effect?) */
214 ret = cx82310_cmd(dev, CMD_START, false, NULL, 0, NULL, 0);
220 kfree(dev->driver_priv);
222 kfree((void *)dev->partial_data);
226 static void cx82310_unbind(struct usbnet *dev, struct usb_interface *intf)
228 struct cx82310_priv *priv = dev->driver_priv;
230 kfree((void *)dev->partial_data);
231 cancel_work_sync(&priv->reenable_work);
232 kfree(dev->driver_priv);
236 * RX is NOT easy - we can receive multiple packets per skb, each having 2-byte
237 * packet length at the beginning.
238 * The last packet might be incomplete (when it crosses the 4KB URB size),
239 * continuing in the next skb (without any headers).
240 * If a packet has odd length, there is one extra byte at the end (before next
241 * packet or at the end of the URB).
243 static int cx82310_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
246 struct sk_buff *skb2;
247 struct cx82310_priv *priv = dev->driver_priv;
250 * If the last skb ended with an incomplete packet, this skb contains
251 * end of that packet at the beginning.
253 if (dev->partial_rem) {
254 len = dev->partial_len + dev->partial_rem;
255 skb2 = alloc_skb(len, GFP_ATOMIC);
259 memcpy(skb2->data, (void *)dev->partial_data,
261 memcpy(skb2->data + dev->partial_len, skb->data,
263 usbnet_skb_return(dev, skb2);
264 skb_pull(skb, (dev->partial_rem + 1) & ~1);
265 dev->partial_rem = 0;
270 /* a skb can contain multiple packets */
271 while (skb->len > 1) {
272 /* first two bytes are packet length */
273 len = skb->data[0] | (skb->data[1] << 8);
276 /* if last packet in the skb, let usbnet to process it */
277 if (len == skb->len || len + 1 == skb->len) {
283 netdev_info(dev->net, "router was rebooted, re-enabling ethernet mode");
284 schedule_work(&priv->reenable_work);
285 } else if (len > CX82310_MTU) {
286 netdev_err(dev->net, "RX packet too long: %d B\n", len);
290 /* incomplete packet, save it for the next skb */
291 if (len > skb->len) {
292 dev->partial_len = skb->len;
293 dev->partial_rem = len - skb->len;
294 memcpy((void *)dev->partial_data, skb->data,
296 skb_pull(skb, skb->len);
300 skb2 = alloc_skb(len, GFP_ATOMIC);
304 memcpy(skb2->data, skb->data, len);
305 /* process the packet */
306 usbnet_skb_return(dev, skb2);
308 skb_pull(skb, (len + 1) & ~1);
311 /* let usbnet process the last packet */
315 /* TX is easy, just add 2 bytes of length at the beginning */
316 static struct sk_buff *cx82310_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
321 if (skb_cow_head(skb, 2)) {
322 dev_kfree_skb_any(skb);
328 skb->data[1] = len >> 8;
334 static const struct driver_info cx82310_info = {
335 .description = "Conexant CX82310 USB ethernet",
337 .bind = cx82310_bind,
338 .unbind = cx82310_unbind,
339 .rx_fixup = cx82310_rx_fixup,
340 .tx_fixup = cx82310_tx_fixup,
343 #define USB_DEVICE_CLASS(vend, prod, cl, sc, pr) \
344 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \
345 USB_DEVICE_ID_MATCH_DEV_INFO, \
346 .idVendor = (vend), \
347 .idProduct = (prod), \
348 .bDeviceClass = (cl), \
349 .bDeviceSubClass = (sc), \
350 .bDeviceProtocol = (pr)
352 static const struct usb_device_id products[] = {
354 USB_DEVICE_CLASS(0x0572, 0xcb01, 0xff, 0, 0),
355 .driver_info = (unsigned long) &cx82310_info
359 MODULE_DEVICE_TABLE(usb, products);
361 static struct usb_driver cx82310_driver = {
362 .name = "cx82310_eth",
363 .id_table = products,
364 .probe = usbnet_probe,
365 .disconnect = usbnet_disconnect,
366 .suspend = usbnet_suspend,
367 .resume = usbnet_resume,
368 .disable_hub_initiated_lpm = 1,
371 module_usb_driver(cx82310_driver);
373 MODULE_AUTHOR("Ondrej Zary");
374 MODULE_DESCRIPTION("Conexant CX82310-based ADSL router USB ethernet driver");
375 MODULE_LICENSE("GPL");