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