| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Driver for USB ethernet port of Conexant CX82310-based ADSL routers |
|---|
| 3 | 4 | * Copyright (C) 2010 by Ondrej Zary |
|---|
| 4 | 5 | * 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 | 6 | */ |
|---|
| 19 | 7 | |
|---|
| 20 | 8 | #include <linux/module.h> |
|---|
| .. | .. |
|---|
| 52 | 40 | #define CX82310_MTU 1514 |
|---|
| 53 | 41 | #define CMD_EP 0x01 |
|---|
| 54 | 42 | |
|---|
| 43 | +struct cx82310_priv { |
|---|
| 44 | + struct work_struct reenable_work; |
|---|
| 45 | + struct usbnet *dev; |
|---|
| 46 | +}; |
|---|
| 47 | + |
|---|
| 55 | 48 | /* |
|---|
| 56 | 49 | * execute control command |
|---|
| 57 | 50 | * - optionally send some data (command parameters) |
|---|
| .. | .. |
|---|
| 78 | 71 | CMD_PACKET_SIZE, &actual_len, CMD_TIMEOUT); |
|---|
| 79 | 72 | if (ret < 0) { |
|---|
| 80 | 73 | if (cmd != CMD_GET_LINK_STATUS) |
|---|
| 81 | | - dev_err(&dev->udev->dev, "send command %#x: error %d\n", |
|---|
| 82 | | - cmd, ret); |
|---|
| 74 | + netdev_err(dev->net, "send command %#x: error %d\n", |
|---|
| 75 | + cmd, ret); |
|---|
| 83 | 76 | goto end; |
|---|
| 84 | 77 | } |
|---|
| 85 | 78 | |
|---|
| .. | .. |
|---|
| 91 | 84 | CMD_TIMEOUT); |
|---|
| 92 | 85 | if (ret < 0) { |
|---|
| 93 | 86 | if (cmd != CMD_GET_LINK_STATUS) |
|---|
| 94 | | - dev_err(&dev->udev->dev, |
|---|
| 95 | | - "reply receive error %d\n", |
|---|
| 96 | | - ret); |
|---|
| 87 | + netdev_err(dev->net, "reply receive error %d\n", |
|---|
| 88 | + ret); |
|---|
| 97 | 89 | goto end; |
|---|
| 98 | 90 | } |
|---|
| 99 | 91 | if (actual_len > 0) |
|---|
| 100 | 92 | break; |
|---|
| 101 | 93 | } |
|---|
| 102 | 94 | if (actual_len == 0) { |
|---|
| 103 | | - dev_err(&dev->udev->dev, "no reply to command %#x\n", |
|---|
| 104 | | - cmd); |
|---|
| 95 | + netdev_err(dev->net, "no reply to command %#x\n", cmd); |
|---|
| 105 | 96 | ret = -EIO; |
|---|
| 106 | 97 | goto end; |
|---|
| 107 | 98 | } |
|---|
| 108 | 99 | if (buf[0] != cmd) { |
|---|
| 109 | | - dev_err(&dev->udev->dev, |
|---|
| 110 | | - "got reply to command %#x, expected: %#x\n", |
|---|
| 111 | | - buf[0], cmd); |
|---|
| 100 | + netdev_err(dev->net, "got reply to command %#x, expected: %#x\n", |
|---|
| 101 | + buf[0], cmd); |
|---|
| 112 | 102 | ret = -EIO; |
|---|
| 113 | 103 | goto end; |
|---|
| 114 | 104 | } |
|---|
| 115 | 105 | if (buf[1] != STATUS_SUCCESS) { |
|---|
| 116 | | - dev_err(&dev->udev->dev, "command %#x failed: %#x\n", |
|---|
| 117 | | - cmd, buf[1]); |
|---|
| 106 | + netdev_err(dev->net, "command %#x failed: %#x\n", cmd, |
|---|
| 107 | + buf[1]); |
|---|
| 118 | 108 | ret = -EIO; |
|---|
| 119 | 109 | goto end; |
|---|
| 120 | 110 | } |
|---|
| .. | .. |
|---|
| 125 | 115 | end: |
|---|
| 126 | 116 | kfree(buf); |
|---|
| 127 | 117 | return ret; |
|---|
| 118 | +} |
|---|
| 119 | + |
|---|
| 120 | +static int cx82310_enable_ethernet(struct usbnet *dev) |
|---|
| 121 | +{ |
|---|
| 122 | + int ret = cx82310_cmd(dev, CMD_ETHERNET_MODE, true, "\x01", 1, NULL, 0); |
|---|
| 123 | + |
|---|
| 124 | + if (ret) |
|---|
| 125 | + netdev_err(dev->net, "unable to enable ethernet mode: %d\n", |
|---|
| 126 | + ret); |
|---|
| 127 | + return ret; |
|---|
| 128 | +} |
|---|
| 129 | + |
|---|
| 130 | +static void cx82310_reenable_work(struct work_struct *work) |
|---|
| 131 | +{ |
|---|
| 132 | + struct cx82310_priv *priv = container_of(work, struct cx82310_priv, |
|---|
| 133 | + reenable_work); |
|---|
| 134 | + cx82310_enable_ethernet(priv->dev); |
|---|
| 128 | 135 | } |
|---|
| 129 | 136 | |
|---|
| 130 | 137 | #define partial_len data[0] /* length of partial packet data */ |
|---|
| .. | .. |
|---|
| 138 | 145 | struct usb_device *udev = dev->udev; |
|---|
| 139 | 146 | u8 link[3]; |
|---|
| 140 | 147 | int timeout = 50; |
|---|
| 148 | + struct cx82310_priv *priv; |
|---|
| 141 | 149 | |
|---|
| 142 | 150 | /* avoid ADSL modems - continue only if iProduct is "USB NET CARD" */ |
|---|
| 143 | 151 | if (usb_string(udev, udev->descriptor.iProduct, buf, sizeof(buf)) > 0 |
|---|
| .. | .. |
|---|
| 164 | 172 | if (!dev->partial_data) |
|---|
| 165 | 173 | return -ENOMEM; |
|---|
| 166 | 174 | |
|---|
| 175 | + priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
|---|
| 176 | + if (!priv) { |
|---|
| 177 | + ret = -ENOMEM; |
|---|
| 178 | + goto err_partial; |
|---|
| 179 | + } |
|---|
| 180 | + dev->driver_priv = priv; |
|---|
| 181 | + INIT_WORK(&priv->reenable_work, cx82310_reenable_work); |
|---|
| 182 | + priv->dev = dev; |
|---|
| 183 | + |
|---|
| 167 | 184 | /* wait for firmware to become ready (indicated by the link being up) */ |
|---|
| 168 | 185 | while (--timeout) { |
|---|
| 169 | 186 | ret = cx82310_cmd(dev, CMD_GET_LINK_STATUS, true, NULL, 0, |
|---|
| .. | .. |
|---|
| 174 | 191 | msleep(500); |
|---|
| 175 | 192 | } |
|---|
| 176 | 193 | if (!timeout) { |
|---|
| 177 | | - dev_err(&udev->dev, "firmware not ready in time\n"); |
|---|
| 194 | + netdev_err(dev->net, "firmware not ready in time\n"); |
|---|
| 178 | 195 | ret = -ETIMEDOUT; |
|---|
| 179 | 196 | goto err; |
|---|
| 180 | 197 | } |
|---|
| 181 | 198 | |
|---|
| 182 | 199 | /* 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); |
|---|
| 200 | + ret = cx82310_enable_ethernet(dev); |
|---|
| 201 | + if (ret) |
|---|
| 187 | 202 | goto err; |
|---|
| 188 | | - } |
|---|
| 189 | 203 | |
|---|
| 190 | 204 | /* get the MAC address */ |
|---|
| 191 | 205 | ret = cx82310_cmd(dev, CMD_GET_MAC_ADDR, true, NULL, 0, |
|---|
| 192 | 206 | dev->net->dev_addr, ETH_ALEN); |
|---|
| 193 | 207 | if (ret) { |
|---|
| 194 | | - dev_err(&udev->dev, "unable to read MAC address: %d\n", ret); |
|---|
| 208 | + netdev_err(dev->net, "unable to read MAC address: %d\n", ret); |
|---|
| 195 | 209 | goto err; |
|---|
| 196 | 210 | } |
|---|
| 197 | 211 | |
|---|
| .. | .. |
|---|
| 202 | 216 | |
|---|
| 203 | 217 | return 0; |
|---|
| 204 | 218 | err: |
|---|
| 219 | + kfree(dev->driver_priv); |
|---|
| 220 | +err_partial: |
|---|
| 205 | 221 | kfree((void *)dev->partial_data); |
|---|
| 206 | 222 | return ret; |
|---|
| 207 | 223 | } |
|---|
| 208 | 224 | |
|---|
| 209 | 225 | static void cx82310_unbind(struct usbnet *dev, struct usb_interface *intf) |
|---|
| 210 | 226 | { |
|---|
| 227 | + struct cx82310_priv *priv = dev->driver_priv; |
|---|
| 228 | + |
|---|
| 211 | 229 | kfree((void *)dev->partial_data); |
|---|
| 230 | + cancel_work_sync(&priv->reenable_work); |
|---|
| 231 | + kfree(dev->driver_priv); |
|---|
| 212 | 232 | } |
|---|
| 213 | 233 | |
|---|
| 214 | 234 | /* |
|---|
| .. | .. |
|---|
| 223 | 243 | { |
|---|
| 224 | 244 | int len; |
|---|
| 225 | 245 | struct sk_buff *skb2; |
|---|
| 246 | + struct cx82310_priv *priv = dev->driver_priv; |
|---|
| 226 | 247 | |
|---|
| 227 | 248 | /* |
|---|
| 228 | 249 | * If the last skb ended with an incomplete packet, this skb contains |
|---|
| .. | .. |
|---|
| 257 | 278 | break; |
|---|
| 258 | 279 | } |
|---|
| 259 | 280 | |
|---|
| 260 | | - if (len > CX82310_MTU) { |
|---|
| 261 | | - dev_err(&dev->udev->dev, "RX packet too long: %d B\n", |
|---|
| 262 | | - len); |
|---|
| 281 | + if (len == 0xffff) { |
|---|
| 282 | + netdev_info(dev->net, "router was rebooted, re-enabling ethernet mode"); |
|---|
| 283 | + schedule_work(&priv->reenable_work); |
|---|
| 284 | + } else if (len > CX82310_MTU) { |
|---|
| 285 | + netdev_err(dev->net, "RX packet too long: %d B\n", len); |
|---|
| 263 | 286 | return 0; |
|---|
| 264 | 287 | } |
|---|
| 265 | 288 | |
|---|