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 * execute control command
45 * - optionally send some data (command parameters)
46 * - optionally wait for the reply
47 * - optionally read some data from the reply
49 static int cx82310_cmd(struct usbnet
*dev
, enum cx82310_cmd cmd
, bool reply
,
50 u8
*wdata
, int wlen
, u8
*rdata
, int rlen
)
52 int actual_len
, retries
, ret
;
53 struct usb_device
*udev
= dev
->udev
;
54 u8
*buf
= kzalloc(CMD_PACKET_SIZE
, GFP_KERNEL
);
59 /* create command packet */
62 memcpy(buf
+ 4, wdata
, min_t(int, wlen
, CMD_PACKET_SIZE
- 4));
64 /* send command packet */
65 ret
= usb_bulk_msg(udev
, usb_sndbulkpipe(udev
, CMD_EP
), buf
,
66 CMD_PACKET_SIZE
, &actual_len
, CMD_TIMEOUT
);
68 if (cmd
!= CMD_GET_LINK_STATUS
)
69 dev_err(&dev
->udev
->dev
, "send command %#x: error %d\n",
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
,
81 if (cmd
!= CMD_GET_LINK_STATUS
)
82 dev_err(&dev
->udev
->dev
,
83 "reply receive error %d\n",
90 if (actual_len
== 0) {
91 dev_err(&dev
->udev
->dev
, "no reply to command %#x\n",
97 dev_err(&dev
->udev
->dev
,
98 "got reply to command %#x, expected: %#x\n",
103 if (buf
[1] != STATUS_SUCCESS
) {
104 dev_err(&dev
->udev
->dev
, "command %#x failed: %#x\n",
110 memcpy(rdata
, buf
+ 4,
111 min_t(int, rlen
, CMD_PACKET_SIZE
- 4));
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 */
122 static int cx82310_bind(struct usbnet
*dev
, struct usb_interface
*intf
)
126 struct usb_device
*udev
= dev
->udev
;
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");
137 ret
= usbnet_get_endpoints(dev
, intf
);
142 * this must not include ethernet header as the device can send partial
143 * packets with no header (and sometimes even empty URBs)
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;
151 dev
->partial_data
= (unsigned long) kmalloc(dev
->hard_mtu
, GFP_KERNEL
);
152 if (!dev
->partial_data
)
155 /* wait for firmware to become ready (indicated by the link being up) */
157 ret
= cx82310_cmd(dev
, CMD_GET_LINK_STATUS
, true, NULL
, 0,
159 /* the command can time out during boot - it's not an error */
160 if (!ret
&& link
[0] == 1 && link
[2] == 1)
165 dev_err(&udev
->dev
, "firmware not ready in time\n");
170 /* enable ethernet mode (?) */
171 ret
= cx82310_cmd(dev
, CMD_ETHERNET_MODE
, true, "\x01", 1, NULL
, 0);
173 dev_err(&udev
->dev
, "unable to enable ethernet mode: %d\n",
178 /* get the MAC address */
179 ret
= cx82310_cmd(dev
, CMD_GET_MAC_ADDR
, true, NULL
, 0,
180 dev
->net
->dev_addr
, ETH_ALEN
);
182 dev_err(&udev
->dev
, "unable to read MAC address: %d\n", ret
);
186 /* start (does not seem to have any effect?) */
187 ret
= cx82310_cmd(dev
, CMD_START
, false, NULL
, 0, NULL
, 0);
193 kfree((void *)dev
->partial_data
);
197 static void cx82310_unbind(struct usbnet
*dev
, struct usb_interface
*intf
)
199 kfree((void *)dev
->partial_data
);
203 * RX is NOT easy - we can receive multiple packets per skb, each having 2-byte
204 * packet length at the beginning.
205 * The last packet might be incomplete (when it crosses the 4KB URB size),
206 * continuing in the next skb (without any headers).
207 * If a packet has odd length, there is one extra byte at the end (before next
208 * packet or at the end of the URB).
210 static int cx82310_rx_fixup(struct usbnet
*dev
, struct sk_buff
*skb
)
213 struct sk_buff
*skb2
;
216 * If the last skb ended with an incomplete packet, this skb contains
217 * end of that packet at the beginning.
219 if (dev
->partial_rem
) {
220 len
= dev
->partial_len
+ dev
->partial_rem
;
221 skb2
= alloc_skb(len
, GFP_ATOMIC
);
225 memcpy(skb2
->data
, (void *)dev
->partial_data
,
227 memcpy(skb2
->data
+ dev
->partial_len
, skb
->data
,
229 usbnet_skb_return(dev
, skb2
);
230 skb_pull(skb
, (dev
->partial_rem
+ 1) & ~1);
231 dev
->partial_rem
= 0;
236 /* a skb can contain multiple packets */
237 while (skb
->len
> 1) {
238 /* first two bytes are packet length */
239 len
= skb
->data
[0] | (skb
->data
[1] << 8);
242 /* if last packet in the skb, let usbnet to process it */
243 if (len
== skb
->len
|| len
+ 1 == skb
->len
) {
248 if (len
> CX82310_MTU
) {
249 dev_err(&dev
->udev
->dev
, "RX packet too long: %d B\n",
254 /* incomplete packet, save it for the next skb */
255 if (len
> skb
->len
) {
256 dev
->partial_len
= skb
->len
;
257 dev
->partial_rem
= len
- skb
->len
;
258 memcpy((void *)dev
->partial_data
, skb
->data
,
260 skb_pull(skb
, skb
->len
);
264 skb2
= alloc_skb(len
, GFP_ATOMIC
);
268 memcpy(skb2
->data
, skb
->data
, len
);
269 /* process the packet */
270 usbnet_skb_return(dev
, skb2
);
272 skb_pull(skb
, (len
+ 1) & ~1);
275 /* let usbnet process the last packet */
279 /* TX is easy, just add 2 bytes of length at the beginning */
280 static struct sk_buff
*cx82310_tx_fixup(struct usbnet
*dev
, struct sk_buff
*skb
,
285 if (skb_cow_head(skb
, 2)) {
286 dev_kfree_skb_any(skb
);
292 skb
->data
[1] = len
>> 8;
298 static const struct driver_info cx82310_info
= {
299 .description
= "Conexant CX82310 USB ethernet",
301 .bind
= cx82310_bind
,
302 .unbind
= cx82310_unbind
,
303 .rx_fixup
= cx82310_rx_fixup
,
304 .tx_fixup
= cx82310_tx_fixup
,
307 #define USB_DEVICE_CLASS(vend, prod, cl, sc, pr) \
308 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \
309 USB_DEVICE_ID_MATCH_DEV_INFO, \
310 .idVendor = (vend), \
311 .idProduct = (prod), \
312 .bDeviceClass = (cl), \
313 .bDeviceSubClass = (sc), \
314 .bDeviceProtocol = (pr)
316 static const struct usb_device_id products
[] = {
318 USB_DEVICE_CLASS(0x0572, 0xcb01, 0xff, 0, 0),
319 .driver_info
= (unsigned long) &cx82310_info
323 MODULE_DEVICE_TABLE(usb
, products
);
325 static struct usb_driver cx82310_driver
= {
326 .name
= "cx82310_eth",
327 .id_table
= products
,
328 .probe
= usbnet_probe
,
329 .disconnect
= usbnet_disconnect
,
330 .suspend
= usbnet_suspend
,
331 .resume
= usbnet_resume
,
332 .disable_hub_initiated_lpm
= 1,
335 module_usb_driver(cx82310_driver
);
337 MODULE_AUTHOR("Ondrej Zary");
338 MODULE_DESCRIPTION("Conexant CX82310-based ADSL router USB ethernet driver");
339 MODULE_LICENSE("GPL");