| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* |
| 3 |
* Copyright (c) 2002 Petko Manolov ([email protected]) |
| 4 |
*/ |
| 5 |
|
| 6 |
#include <linux/signal.h> |
| 7 |
#include <linux/slab.h> |
| 8 |
#include <linux/module.h> |
| 9 |
#include <linux/netdevice.h> |
| 10 |
#include <linux/etherdevice.h> |
| 11 |
#include <linux/mii.h> |
| 12 |
#include <linux/ethtool.h> |
| 13 |
#include <linux/usb.h> |
| 14 |
#include <linux/uaccess.h> |
| 15 |
|
| 16 |
#define DRIVER_AUTHOR "Petko Manolov <[email protected]>" |
| 17 |
#define DRIVER_DESC "rtl8150 based usb-ethernet driver" |
| 18 |
|
| 19 |
#define IDR 0x0120 |
| 20 |
#define MAR 0x0126 |
| 21 |
#define CR 0x012e |
| 22 |
#define TCR 0x012f |
| 23 |
#define RCR 0x0130 |
| 24 |
#define TSR 0x0132 |
| 25 |
#define RSR 0x0133 |
| 26 |
#define CON0 0x0135 |
| 27 |
#define CON1 0x0136 |
| 28 |
#define MSR 0x0137 |
| 29 |
#define PHYADD 0x0138 |
| 30 |
#define PHYDAT 0x0139 |
| 31 |
#define PHYCNT 0x013b |
| 32 |
#define GPPC 0x013d |
| 33 |
#define BMCR 0x0140 |
| 34 |
#define BMSR 0x0142 |
| 35 |
#define ANAR 0x0144 |
| 36 |
#define ANLP 0x0146 |
| 37 |
#define AER 0x0148 |
| 38 |
#define CSCR 0x014C /* This one has the link status */ |
| 39 |
#define CSCR_LINK_STATUS (1 << 3) |
| 40 |
|
| 41 |
#define IDR_EEPROM 0x1202 |
| 42 |
|
| 43 |
#define PHY_READ 0 |
| 44 |
#define PHY_WRITE 0x20 |
| 45 |
#define PHY_GO 0x40 |
| 46 |
|
| 47 |
#define MII_TIMEOUT 10 |
| 48 |
#define INTBUFSIZE 8 |
| 49 |
|
| 50 |
#define RTL8150_REQT_READ 0xc0 |
| 51 |
#define RTL8150_REQT_WRITE 0x40 |
| 52 |
#define RTL8150_REQ_GET_REGS 0x05 |
| 53 |
#define RTL8150_REQ_SET_REGS 0x05 |
| 54 |
|
| 55 |
|
| 56 |
/* Transmit status register errors */ |
| 57 |
#define TSR_ECOL (1<<5) |
| 58 |
#define TSR_LCOL (1<<4) |
| 59 |
#define TSR_LOSS_CRS (1<<3) |
| 60 |
#define TSR_JBR (1<<2) |
| 61 |
#define TSR_ERRORS (TSR_ECOL | TSR_LCOL | TSR_LOSS_CRS | TSR_JBR) |
| 62 |
/* Receive status register errors */ |
| 63 |
#define RSR_CRC (1<<2) |
| 64 |
#define RSR_FAE (1<<1) |
| 65 |
#define RSR_ERRORS (RSR_CRC | RSR_FAE) |
| 66 |
|
| 67 |
/* Media status register definitions */ |
| 68 |
#define MSR_DUPLEX (1<<4) |
| 69 |
#define MSR_SPEED (1<<3) |
| 70 |
#define MSR_LINK (1<<2) |
| 71 |
|
| 72 |
/* USB endpoints */ |
| 73 |
enum rtl8150_usb_ep { |
| 74 |
RTL8150_USB_EP_CONTROL = 0, |
| 75 |
RTL8150_USB_EP_BULK_IN = 1, |
| 76 |
RTL8150_USB_EP_BULK_OUT = 2, |
| 77 |
RTL8150_USB_EP_INT_IN = 3, |
| 78 |
}; |
| 79 |
|
| 80 |
/* Interrupt pipe data */ |
| 81 |
#define INT_TSR 0x00 |
| 82 |
#define INT_RSR 0x01 |
| 83 |
#define INT_MSR 0x02 |
| 84 |
#define INT_WAKSR 0x03 |
| 85 |
#define INT_TXOK_CNT 0x04 |
| 86 |
#define INT_RXLOST_CNT 0x05 |
| 87 |
#define INT_CRERR_CNT 0x06 |
| 88 |
#define INT_COL_CNT 0x07 |
| 89 |
|
| 90 |
|
| 91 |
#define RTL8150_MTU 1540 |
| 92 |
#define RTL8150_TX_TIMEOUT (HZ) |
| 93 |
#define RX_SKB_POOL_SIZE 4 |
| 94 |
|
| 95 |
/* rtl8150 flags */ |
| 96 |
#define RTL8150_HW_CRC 0 |
| 97 |
#define RX_REG_SET 1 |
| 98 |
#define RTL8150_UNPLUG 2 |
| 99 |
#define RX_URB_FAIL 3 |
| 100 |
|
| 101 |
/* Define these values to match your device */ |
| 102 |
#define VENDOR_ID_REALTEK 0x0bda |
| 103 |
#define VENDOR_ID_MELCO 0x0411 |
| 104 |
#define VENDOR_ID_MICRONET 0x3980 |
| 105 |
#define VENDOR_ID_LONGSHINE 0x07b8 |
| 106 |
#define VENDOR_ID_OQO 0x1557 |
| 107 |
#define VENDOR_ID_ZYXEL 0x0586 |
| 108 |
|
| 109 |
#define PRODUCT_ID_RTL8150 0x8150 |
| 110 |
#define PRODUCT_ID_LUAKTX 0x0012 |
| 111 |
#define PRODUCT_ID_LCS8138TX 0x401a |
| 112 |
#define PRODUCT_ID_SP128AR 0x0003 |
| 113 |
#define PRODUCT_ID_PRESTIGE 0x401a |
| 114 |
|
| 115 |
#undef EEPROM_WRITE |
| 116 |
|
| 117 |
/* table of devices that work with this driver */ |
| 118 |
static const struct usb_device_id rtl8150_table[] = { |
| 119 |
{USB_DEVICE(VENDOR_ID_REALTEK, PRODUCT_ID_RTL8150)}, |
| 120 |
{USB_DEVICE(VENDOR_ID_MELCO, PRODUCT_ID_LUAKTX)}, |
| 121 |
{USB_DEVICE(VENDOR_ID_MICRONET, PRODUCT_ID_SP128AR)}, |
| 122 |
{USB_DEVICE(VENDOR_ID_LONGSHINE, PRODUCT_ID_LCS8138TX)}, |
| 123 |
{USB_DEVICE(VENDOR_ID_OQO, PRODUCT_ID_RTL8150)}, |
| 124 |
{USB_DEVICE(VENDOR_ID_ZYXEL, PRODUCT_ID_PRESTIGE)}, |
| 125 |
{} |
| 126 |
}; |
| 127 |
|
| 128 |
MODULE_DEVICE_TABLE(usb, rtl8150_table); |
| 129 |
|
| 130 |
struct rtl8150 { |
| 131 |
unsigned long flags; |
| 132 |
struct usb_device *udev; |
| 133 |
struct tasklet_struct tl; |
| 134 |
struct net_device *netdev; |
| 135 |
struct urb *rx_urb, *tx_urb, *intr_urb; |
| 136 |
struct sk_buff *tx_skb, *rx_skb; |
| 137 |
struct sk_buff *rx_skb_pool[RX_SKB_POOL_SIZE]; |
| 138 |
spinlock_t rx_pool_lock; |
| 139 |
struct usb_ctrlrequest dr; |
| 140 |
int intr_interval; |
| 141 |
u8 *intr_buff; |
| 142 |
u8 phy; |
| 143 |
}; |
| 144 |
|
| 145 |
typedef struct rtl8150 rtl8150_t; |
| 146 |
|
| 147 |
struct async_req { |
| 148 |
struct usb_ctrlrequest dr; |
| 149 |
u16 rx_creg; |
| 150 |
}; |
| 151 |
|
| 152 |
static const char driver_name [] = "rtl8150"; |
| 153 |
|
| 154 |
/* |
| 155 |
** |
| 156 |
** device related part of the code |
| 157 |
** |
| 158 |
*/ |
| 159 |
static int get_registers(rtl8150_t * dev, u16 indx, u16 size, void *data) |
| 160 |
{ |
| 161 |
return usb_control_msg_recv(dev->udev, 0, RTL8150_REQ_GET_REGS, |
| 162 |
RTL8150_REQT_READ, indx, 0, data, size, |
| 163 |
1000, GFP_NOIO); |
| 164 |
} |
| 165 |
|
| 166 |
static int set_registers(rtl8150_t * dev, u16 indx, u16 size, const void *data) |
| 167 |
{ |
| 168 |
return usb_control_msg_send(dev->udev, 0, RTL8150_REQ_SET_REGS, |
| 169 |
RTL8150_REQT_WRITE, indx, 0, data, size, |
| 170 |
1000, GFP_NOIO); |
| 171 |
} |
| 172 |
|
| 173 |
static void async_set_reg_cb(struct urb *urb) |
| 174 |
{ |
| 175 |
struct async_req *req = (struct async_req *)urb->context; |
| 176 |
int status = urb->status; |
| 177 |
|
| 178 |
if (status < 0) |
| 179 |
dev_dbg(&urb->dev->dev, "%s failed with %d", __func__, status); |
| 180 |
kfree(req); |
| 181 |
usb_free_urb(urb); |
| 182 |
} |
| 183 |
|
| 184 |
static int async_set_registers(rtl8150_t *dev, u16 indx, u16 size, u16 reg) |
| 185 |
{ |
| 186 |
int res = -ENOMEM; |
| 187 |
struct urb *async_urb; |
| 188 |
struct async_req *req; |
| 189 |
|
| 190 |
req = kmalloc_obj(struct async_req, GFP_ATOMIC); |
| 191 |
if (req == NULL) |
| 192 |
return res; |
| 193 |
async_urb = usb_alloc_urb(0, GFP_ATOMIC); |
| 194 |
if (async_urb == NULL) { |
| 195 |
kfree(req); |
| 196 |
return res; |
| 197 |
} |
| 198 |
req->rx_creg = cpu_to_le16(reg); |
| 199 |
req->dr.bRequestType = RTL8150_REQT_WRITE; |
| 200 |
req->dr.bRequest = RTL8150_REQ_SET_REGS; |
| 201 |
req->dr.wIndex = 0; |
| 202 |
req->dr.wValue = cpu_to_le16(indx); |
| 203 |
req->dr.wLength = cpu_to_le16(size); |
| 204 |
usb_fill_control_urb(async_urb, dev->udev, |
| 205 |
usb_sndctrlpipe(dev->udev, 0), (void *)&req->dr, |
| 206 |
&req->rx_creg, size, async_set_reg_cb, req); |
| 207 |
res = usb_submit_urb(async_urb, GFP_ATOMIC); |
| 208 |
if (res) { |
| 209 |
if (res == -ENODEV) |
| 210 |
netif_device_detach(dev->netdev); |
| 211 |
dev_err(&dev->udev->dev, "%s failed with %d\n", __func__, res); |
| 212 |
kfree(req); |
| 213 |
usb_free_urb(async_urb); |
| 214 |
} |
| 215 |
return res; |
| 216 |
} |
| 217 |
|
| 218 |
static int read_mii_word(rtl8150_t * dev, u8 phy, __u8 indx, u16 * reg) |
| 219 |
{ |
| 220 |
int i; |
| 221 |
u8 data[3], tmp; |
| 222 |
|
| 223 |
data[0] = phy; |
| 224 |
data[1] = data[2] = 0; |
| 225 |
tmp = indx | PHY_READ | PHY_GO; |
| 226 |
i = 0; |
| 227 |
|
| 228 |
set_registers(dev, PHYADD, sizeof(data), data); |
| 229 |
set_registers(dev, PHYCNT, 1, &tmp); |
| 230 |
do { |
| 231 |
get_registers(dev, PHYCNT, 1, data); |
| 232 |
} while ((data[0] & PHY_GO) && (i++ < MII_TIMEOUT)); |
| 233 |
|
| 234 |
if (i <= MII_TIMEOUT) { |
| 235 |
get_registers(dev, PHYDAT, 2, data); |
| 236 |
*reg = data[0] | (data[1] << 8); |
| 237 |
return 0; |
| 238 |
} else |
| 239 |
return 1; |
| 240 |
} |
| 241 |
|
| 242 |
static int write_mii_word(rtl8150_t * dev, u8 phy, __u8 indx, u16 reg) |
| 243 |
{ |
| 244 |
int i; |
| 245 |
u8 data[3], tmp; |
| 246 |
|
| 247 |
data[0] = phy; |
| 248 |
data[1] = reg & 0xff; |
| 249 |
data[2] = (reg >> 8) & 0xff; |
| 250 |
tmp = indx | PHY_WRITE | PHY_GO; |
| 251 |
i = 0; |
| 252 |
|
| 253 |
set_registers(dev, PHYADD, sizeof(data), data); |
| 254 |
set_registers(dev, PHYCNT, 1, &tmp); |
| 255 |
do { |
| 256 |
get_registers(dev, PHYCNT, 1, data); |
| 257 |
} while ((data[0] & PHY_GO) && (i++ < MII_TIMEOUT)); |
| 258 |
|
| 259 |
if (i <= MII_TIMEOUT) |
| 260 |
return 0; |
| 261 |
else |
| 262 |
return 1; |
| 263 |
} |
| 264 |
|
| 265 |
static void set_ethernet_addr(rtl8150_t *dev) |
| 266 |
{ |
| 267 |
u8 node_id[ETH_ALEN]; |
| 268 |
int ret; |
| 269 |
|
| 270 |
ret = get_registers(dev, IDR, sizeof(node_id), node_id); |
| 271 |
|
| 272 |
if (!ret) { |
| 273 |
eth_hw_addr_set(dev->netdev, node_id); |
| 274 |
} else { |
| 275 |
eth_hw_addr_random(dev->netdev); |
| 276 |
netdev_notice(dev->netdev, "Assigned a random MAC address: %pM\n", |
| 277 |
dev->netdev->dev_addr); |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
static int rtl8150_set_mac_address(struct net_device *netdev, void *p) |
| 282 |
{ |
| 283 |
struct sockaddr *addr = p; |
| 284 |
rtl8150_t *dev = netdev_priv(netdev); |
| 285 |
|
| 286 |
if (netif_running(netdev)) |
| 287 |
return -EBUSY; |
| 288 |
|
| 289 |
eth_hw_addr_set(netdev, addr->sa_data); |
| 290 |
netdev_dbg(netdev, "Setting MAC address to %pM\n", netdev->dev_addr); |
| 291 |
/* Set the IDR registers. */ |
| 292 |
set_registers(dev, IDR, netdev->addr_len, netdev->dev_addr); |
| 293 |
#ifdef EEPROM_WRITE |
| 294 |
{ |
| 295 |
int i; |
| 296 |
u8 cr; |
| 297 |
/* Get the CR contents. */ |
| 298 |
get_registers(dev, CR, 1, &cr); |
| 299 |
/* Set the WEPROM bit (eeprom write enable). */ |
| 300 |
cr |= 0x20; |
| 301 |
set_registers(dev, CR, 1, &cr); |
| 302 |
/* Write the MAC address into eeprom. Eeprom writes must be word-sized, |
| 303 |
so we need to split them up. */ |
| 304 |
for (i = 0; i * 2 < netdev->addr_len; i++) { |
| 305 |
set_registers(dev, IDR_EEPROM + (i * 2), 2, |
| 306 |
netdev->dev_addr + (i * 2)); |
| 307 |
} |
| 308 |
/* Clear the WEPROM bit (preventing accidental eeprom writes). */ |
| 309 |
cr &= 0xdf; |
| 310 |
set_registers(dev, CR, 1, &cr); |
| 311 |
} |
| 312 |
#endif |
| 313 |
return 0; |
| 314 |
} |
| 315 |
|
| 316 |
static int rtl8150_reset(rtl8150_t * dev) |
| 317 |
{ |
| 318 |
u8 data = 0x10; |
| 319 |
int i = HZ; |
| 320 |
|
| 321 |
set_registers(dev, CR, 1, &data); |
| 322 |
do { |
| 323 |
get_registers(dev, CR, 1, &data); |
| 324 |
} while ((data & 0x10) && --i); |
| 325 |
|
| 326 |
return (i > 0) ? 1 : 0; |
| 327 |
} |
| 328 |
|
| 329 |
static int alloc_all_urbs(rtl8150_t * dev) |
| 330 |
{ |
| 331 |
dev->rx_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 332 |
if (!dev->rx_urb) |
| 333 |
return 0; |
| 334 |
dev->tx_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 335 |
if (!dev->tx_urb) { |
| 336 |
usb_free_urb(dev->rx_urb); |
| 337 |
return 0; |
| 338 |
} |
| 339 |
dev->intr_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 340 |
if (!dev->intr_urb) { |
| 341 |
usb_free_urb(dev->rx_urb); |
| 342 |
usb_free_urb(dev->tx_urb); |
| 343 |
return 0; |
| 344 |
} |
| 345 |
|
| 346 |
return 1; |
| 347 |
} |
| 348 |
|
| 349 |
static void free_all_urbs(rtl8150_t * dev) |
| 350 |
{ |
| 351 |
usb_free_urb(dev->rx_urb); |
| 352 |
usb_free_urb(dev->tx_urb); |
| 353 |
usb_free_urb(dev->intr_urb); |
| 354 |
} |
| 355 |
|
| 356 |
static void unlink_all_urbs(rtl8150_t * dev) |
| 357 |
{ |
| 358 |
usb_kill_urb(dev->rx_urb); |
| 359 |
usb_kill_urb(dev->tx_urb); |
| 360 |
usb_kill_urb(dev->intr_urb); |
| 361 |
} |
| 362 |
|
| 363 |
static inline struct sk_buff *pull_skb(rtl8150_t *dev) |
| 364 |
{ |
| 365 |
struct sk_buff *skb; |
| 366 |
int i; |
| 367 |
|
| 368 |
for (i = 0; i < RX_SKB_POOL_SIZE; i++) { |
| 369 |
if (dev->rx_skb_pool[i]) { |
| 370 |
skb = dev->rx_skb_pool[i]; |
| 371 |
dev->rx_skb_pool[i] = NULL; |
| 372 |
return skb; |
| 373 |
} |
| 374 |
} |
| 375 |
return NULL; |
| 376 |
} |
| 377 |
|
| 378 |
static void read_bulk_callback(struct urb *urb) |
| 379 |
{ |
| 380 |
rtl8150_t *dev; |
| 381 |
unsigned pkt_len, res; |
| 382 |
struct sk_buff *skb; |
| 383 |
struct net_device *netdev; |
| 384 |
int status = urb->status; |
| 385 |
int result; |
| 386 |
unsigned long flags; |
| 387 |
|
| 388 |
dev = urb->context; |
| 389 |
if (!dev) |
| 390 |
return; |
| 391 |
if (test_bit(RTL8150_UNPLUG, &dev->flags)) |
| 392 |
return; |
| 393 |
netdev = dev->netdev; |
| 394 |
if (!netif_device_present(netdev)) |
| 395 |
return; |
| 396 |
|
| 397 |
switch (status) { |
| 398 |
case 0: |
| 399 |
break; |
| 400 |
case -ENOENT: |
| 401 |
return; /* the urb is in unlink state */ |
| 402 |
case -ETIME: |
| 403 |
if (printk_ratelimit()) |
| 404 |
dev_warn(&urb->dev->dev, "may be reset is needed?..\n"); |
| 405 |
goto goon; |
| 406 |
default: |
| 407 |
if (printk_ratelimit()) |
| 408 |
dev_warn(&urb->dev->dev, "Rx status %d\n", status); |
| 409 |
goto goon; |
| 410 |
} |
| 411 |
|
| 412 |
if (!dev->rx_skb) |
| 413 |
goto resched; |
| 414 |
/* protect against short packets (tell me why we got some?!?) */ |
| 415 |
if (urb->actual_length < 4) |
| 416 |
goto goon; |
| 417 |
|
| 418 |
res = urb->actual_length; |
| 419 |
pkt_len = res - 4; |
| 420 |
|
| 421 |
skb_put(dev->rx_skb, pkt_len); |
| 422 |
dev->rx_skb->protocol = eth_type_trans(dev->rx_skb, netdev); |
| 423 |
netif_rx(dev->rx_skb); |
| 424 |
netdev->stats.rx_packets++; |
| 425 |
netdev->stats.rx_bytes += pkt_len; |
| 426 |
|
| 427 |
spin_lock_irqsave(&dev->rx_pool_lock, flags); |
| 428 |
skb = pull_skb(dev); |
| 429 |
spin_unlock_irqrestore(&dev->rx_pool_lock, flags); |
| 430 |
if (!skb) |
| 431 |
goto resched; |
| 432 |
|
| 433 |
dev->rx_skb = skb; |
| 434 |
goon: |
| 435 |
usb_fill_bulk_urb(dev->rx_urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1), |
| 436 |
dev->rx_skb->data, RTL8150_MTU, read_bulk_callback, dev); |
| 437 |
result = usb_submit_urb(dev->rx_urb, GFP_ATOMIC); |
| 438 |
if (result == -ENODEV) |
| 439 |
netif_device_detach(dev->netdev); |
| 440 |
else if (result) { |
| 441 |
set_bit(RX_URB_FAIL, &dev->flags); |
| 442 |
goto resched; |
| 443 |
} else { |
| 444 |
clear_bit(RX_URB_FAIL, &dev->flags); |
| 445 |
} |
| 446 |
|
| 447 |
return; |
| 448 |
resched: |
| 449 |
tasklet_schedule(&dev->tl); |
| 450 |
} |
| 451 |
|
| 452 |
static void write_bulk_callback(struct urb *urb) |
| 453 |
{ |
| 454 |
rtl8150_t *dev; |
| 455 |
int status = urb->status; |
| 456 |
|
| 457 |
dev = urb->context; |
| 458 |
if (!dev) |
| 459 |
return; |
| 460 |
dev_kfree_skb_irq(dev->tx_skb); |
| 461 |
if (!netif_device_present(dev->netdev)) |
| 462 |
return; |
| 463 |
if (status) |
| 464 |
dev_info(&urb->dev->dev, "%s: Tx status %d\n", |
| 465 |
dev->netdev->name, status); |
| 466 |
netif_trans_update(dev->netdev); |
| 467 |
netif_wake_queue(dev->netdev); |
| 468 |
} |
| 469 |
|
| 470 |
static void intr_callback(struct urb *urb) |
| 471 |
{ |
| 472 |
rtl8150_t *dev; |
| 473 |
__u8 *d; |
| 474 |
int status = urb->status; |
| 475 |
int res; |
| 476 |
|
| 477 |
dev = urb->context; |
| 478 |
if (!dev) |
| 479 |
return; |
| 480 |
switch (status) { |
| 481 |
case 0: /* success */ |
| 482 |
break; |
| 483 |
case -ECONNRESET: /* unlink */ |
| 484 |
case -ENOENT: |
| 485 |
case -ESHUTDOWN: |
| 486 |
return; |
| 487 |
/* -EPIPE: should clear the halt */ |
| 488 |
default: |
| 489 |
dev_info(&urb->dev->dev, "%s: intr status %d\n", |
| 490 |
dev->netdev->name, status); |
| 491 |
goto resubmit; |
| 492 |
} |
| 493 |
|
| 494 |
d = urb->transfer_buffer; |
| 495 |
if (d[0] & TSR_ERRORS) { |
| 496 |
dev->netdev->stats.tx_errors++; |
| 497 |
if (d[INT_TSR] & (TSR_ECOL | TSR_JBR)) |
| 498 |
dev->netdev->stats.tx_aborted_errors++; |
| 499 |
if (d[INT_TSR] & TSR_LCOL) |
| 500 |
dev->netdev->stats.tx_window_errors++; |
| 501 |
if (d[INT_TSR] & TSR_LOSS_CRS) |
| 502 |
dev->netdev->stats.tx_carrier_errors++; |
| 503 |
} |
| 504 |
/* Report link status changes to the network stack */ |
| 505 |
if ((d[INT_MSR] & MSR_LINK) == 0) { |
| 506 |
if (netif_carrier_ok(dev->netdev)) { |
| 507 |
netif_carrier_off(dev->netdev); |
| 508 |
netdev_dbg(dev->netdev, "%s: LINK LOST\n", __func__); |
| 509 |
} |
| 510 |
} else { |
| 511 |
if (!netif_carrier_ok(dev->netdev)) { |
| 512 |
netif_carrier_on(dev->netdev); |
| 513 |
netdev_dbg(dev->netdev, "%s: LINK CAME BACK\n", __func__); |
| 514 |
} |
| 515 |
} |
| 516 |
|
| 517 |
resubmit: |
| 518 |
res = usb_submit_urb (urb, GFP_ATOMIC); |
| 519 |
if (res == -ENODEV) |
| 520 |
netif_device_detach(dev->netdev); |
| 521 |
else if (res) |
| 522 |
dev_err(&dev->udev->dev, |
| 523 |
"can't resubmit intr, %s-%s/input0, status %d\n", |
| 524 |
dev->udev->bus->bus_name, dev->udev->devpath, res); |
| 525 |
} |
| 526 |
|
| 527 |
static int rtl8150_suspend(struct usb_interface *intf, pm_message_t message) |
| 528 |
{ |
| 529 |
rtl8150_t *dev = usb_get_intfdata(intf); |
| 530 |
|
| 531 |
netif_device_detach(dev->netdev); |
| 532 |
|
| 533 |
if (netif_running(dev->netdev)) { |
| 534 |
usb_kill_urb(dev->rx_urb); |
| 535 |
usb_kill_urb(dev->intr_urb); |
| 536 |
} |
| 537 |
return 0; |
| 538 |
} |
| 539 |
|
| 540 |
static int rtl8150_resume(struct usb_interface *intf) |
| 541 |
{ |
| 542 |
rtl8150_t *dev = usb_get_intfdata(intf); |
| 543 |
|
| 544 |
netif_device_attach(dev->netdev); |
| 545 |
if (netif_running(dev->netdev)) { |
| 546 |
dev->rx_urb->status = 0; |
| 547 |
dev->rx_urb->actual_length = 0; |
| 548 |
read_bulk_callback(dev->rx_urb); |
| 549 |
|
| 550 |
dev->intr_urb->status = 0; |
| 551 |
dev->intr_urb->actual_length = 0; |
| 552 |
intr_callback(dev->intr_urb); |
| 553 |
} |
| 554 |
return 0; |
| 555 |
} |
| 556 |
|
| 557 |
/* |
| 558 |
** |
| 559 |
** network related part of the code |
| 560 |
** |
| 561 |
*/ |
| 562 |
|
| 563 |
static void fill_skb_pool(rtl8150_t *dev) |
| 564 |
{ |
| 565 |
struct sk_buff *skb; |
| 566 |
int i; |
| 567 |
|
| 568 |
for (i = 0; i < RX_SKB_POOL_SIZE; i++) { |
| 569 |
if (dev->rx_skb_pool[i]) |
| 570 |
continue; |
| 571 |
skb = dev_alloc_skb(RTL8150_MTU + 2); |
| 572 |
if (!skb) { |
| 573 |
return; |
| 574 |
} |
| 575 |
skb_reserve(skb, 2); |
| 576 |
dev->rx_skb_pool[i] = skb; |
| 577 |
} |
| 578 |
} |
| 579 |
|
| 580 |
static void free_skb_pool(rtl8150_t *dev) |
| 581 |
{ |
| 582 |
int i; |
| 583 |
|
| 584 |
for (i = 0; i < RX_SKB_POOL_SIZE; i++) |
| 585 |
dev_kfree_skb(dev->rx_skb_pool[i]); |
| 586 |
} |
| 587 |
|
| 588 |
static void rx_fixup(struct tasklet_struct *t) |
| 589 |
{ |
| 590 |
struct rtl8150 *dev = from_tasklet(dev, t, tl); |
| 591 |
struct sk_buff *skb; |
| 592 |
int status; |
| 593 |
|
| 594 |
spin_lock_irq(&dev->rx_pool_lock); |
| 595 |
fill_skb_pool(dev); |
| 596 |
spin_unlock_irq(&dev->rx_pool_lock); |
| 597 |
if (test_bit(RX_URB_FAIL, &dev->flags)) |
| 598 |
if (dev->rx_skb) |
| 599 |
goto try_again; |
| 600 |
spin_lock_irq(&dev->rx_pool_lock); |
| 601 |
skb = pull_skb(dev); |
| 602 |
spin_unlock_irq(&dev->rx_pool_lock); |
| 603 |
if (skb == NULL) |
| 604 |
goto tlsched; |
| 605 |
dev->rx_skb = skb; |
| 606 |
usb_fill_bulk_urb(dev->rx_urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1), |
| 607 |
dev->rx_skb->data, RTL8150_MTU, read_bulk_callback, dev); |
| 608 |
try_again: |
| 609 |
status = usb_submit_urb(dev->rx_urb, GFP_ATOMIC); |
| 610 |
if (status == -ENODEV) { |
| 611 |
netif_device_detach(dev->netdev); |
| 612 |
} else if (status) { |
| 613 |
set_bit(RX_URB_FAIL, &dev->flags); |
| 614 |
goto tlsched; |
| 615 |
} else { |
| 616 |
clear_bit(RX_URB_FAIL, &dev->flags); |
| 617 |
} |
| 618 |
|
| 619 |
return; |
| 620 |
tlsched: |
| 621 |
tasklet_schedule(&dev->tl); |
| 622 |
} |
| 623 |
|
| 624 |
static int enable_net_traffic(rtl8150_t * dev) |
| 625 |
{ |
| 626 |
u8 cr, tcr, rcr, msr; |
| 627 |
|
| 628 |
if (!rtl8150_reset(dev)) { |
| 629 |
dev_warn(&dev->udev->dev, "device reset failed\n"); |
| 630 |
} |
| 631 |
/* RCR bit7=1 attach Rx info at the end; =0 HW CRC (which is broken) */ |
| 632 |
rcr = 0x9e; |
| 633 |
tcr = 0xd8; |
| 634 |
cr = 0x0c; |
| 635 |
if (!(rcr & 0x80)) |
| 636 |
set_bit(RTL8150_HW_CRC, &dev->flags); |
| 637 |
set_registers(dev, RCR, 1, &rcr); |
| 638 |
set_registers(dev, TCR, 1, &tcr); |
| 639 |
set_registers(dev, CR, 1, &cr); |
| 640 |
get_registers(dev, MSR, 1, &msr); |
| 641 |
|
| 642 |
return 0; |
| 643 |
} |
| 644 |
|
| 645 |
static void disable_net_traffic(rtl8150_t * dev) |
| 646 |
{ |
| 647 |
u8 cr; |
| 648 |
|
| 649 |
get_registers(dev, CR, 1, &cr); |
| 650 |
cr &= 0xf3; |
| 651 |
set_registers(dev, CR, 1, &cr); |
| 652 |
} |
| 653 |
|
| 654 |
static void rtl8150_tx_timeout(struct net_device *netdev, unsigned int txqueue) |
| 655 |
{ |
| 656 |
rtl8150_t *dev = netdev_priv(netdev); |
| 657 |
dev_warn(&netdev->dev, "Tx timeout.\n"); |
| 658 |
usb_unlink_urb(dev->tx_urb); |
| 659 |
netdev->stats.tx_errors++; |
| 660 |
} |
| 661 |
|
| 662 |
static void rtl8150_set_multicast(struct net_device *netdev) |
| 663 |
{ |
| 664 |
rtl8150_t *dev = netdev_priv(netdev); |
| 665 |
u16 rx_creg = 0x9e; |
| 666 |
|
| 667 |
if (netdev->flags & IFF_PROMISC) { |
| 668 |
rx_creg |= 0x0001; |
| 669 |
dev_info(&netdev->dev, "%s: promiscuous mode\n", netdev->name); |
| 670 |
} else if (!netdev_mc_empty(netdev) || |
| 671 |
(netdev->flags & IFF_ALLMULTI)) { |
| 672 |
rx_creg &= 0xfffe; |
| 673 |
rx_creg |= 0x0002; |
| 674 |
dev_dbg(&netdev->dev, "%s: allmulti set\n", netdev->name); |
| 675 |
} else { |
| 676 |
/* ~RX_MULTICAST, ~RX_PROMISCUOUS */ |
| 677 |
rx_creg &= 0x00fc; |
| 678 |
} |
| 679 |
async_set_registers(dev, RCR, sizeof(rx_creg), rx_creg); |
| 680 |
} |
| 681 |
|
| 682 |
static netdev_tx_t rtl8150_start_xmit(struct sk_buff *skb, |
| 683 |
struct net_device *netdev) |
| 684 |
{ |
| 685 |
rtl8150_t *dev = netdev_priv(netdev); |
| 686 |
unsigned int skb_len; |
| 687 |
int count, res; |
| 688 |
|
| 689 |
/* pad the frame and ensure terminating USB packet, datasheet 9.2.3 */ |
| 690 |
count = max(skb->len, ETH_ZLEN); |
| 691 |
if (count % 64 == 0) |
| 692 |
count++; |
| 693 |
if (skb_padto(skb, count)) { |
| 694 |
netdev->stats.tx_dropped++; |
| 695 |
return NETDEV_TX_OK; |
| 696 |
} |
| 697 |
|
| 698 |
skb_len = skb->len; |
| 699 |
|
| 700 |
netif_stop_queue(netdev); |
| 701 |
dev->tx_skb = skb; |
| 702 |
usb_fill_bulk_urb(dev->tx_urb, dev->udev, usb_sndbulkpipe(dev->udev, 2), |
| 703 |
skb->data, count, write_bulk_callback, dev); |
| 704 |
if ((res = usb_submit_urb(dev->tx_urb, GFP_ATOMIC))) { |
| 705 |
/* Can we get/handle EPIPE here? */ |
| 706 |
if (res == -ENODEV) |
| 707 |
netif_device_detach(dev->netdev); |
| 708 |
else { |
| 709 |
dev_warn(&netdev->dev, "failed tx_urb %d\n", res); |
| 710 |
netdev->stats.tx_errors++; |
| 711 |
netif_start_queue(netdev); |
| 712 |
} |
| 713 |
/* |
| 714 |
* The URB was not submitted, so write_bulk_callback() will |
| 715 |
* never run to free dev->tx_skb. Drop the skb here and |
| 716 |
* clear tx_skb to avoid leaving a stale pointer. |
| 717 |
*/ |
| 718 |
dev->tx_skb = NULL; |
| 719 |
dev_kfree_skb_any(skb); |
| 720 |
} else { |
| 721 |
netdev->stats.tx_packets++; |
| 722 |
netdev->stats.tx_bytes += skb_len; |
| 723 |
netif_trans_update(netdev); |
| 724 |
} |
| 725 |
|
| 726 |
return NETDEV_TX_OK; |
| 727 |
} |
| 728 |
|
| 729 |
|
| 730 |
static void set_carrier(struct net_device *netdev) |
| 731 |
{ |
| 732 |
rtl8150_t *dev = netdev_priv(netdev); |
| 733 |
short tmp; |
| 734 |
|
| 735 |
get_registers(dev, CSCR, 2, &tmp); |
| 736 |
if (tmp & CSCR_LINK_STATUS) |
| 737 |
netif_carrier_on(netdev); |
| 738 |
else |
| 739 |
netif_carrier_off(netdev); |
| 740 |
} |
| 741 |
|
| 742 |
static int rtl8150_open(struct net_device *netdev) |
| 743 |
{ |
| 744 |
rtl8150_t *dev = netdev_priv(netdev); |
| 745 |
int res; |
| 746 |
|
| 747 |
if (dev->rx_skb == NULL) |
| 748 |
dev->rx_skb = pull_skb(dev); |
| 749 |
if (!dev->rx_skb) |
| 750 |
return -ENOMEM; |
| 751 |
|
| 752 |
set_registers(dev, IDR, 6, netdev->dev_addr); |
| 753 |
|
| 754 |
usb_fill_bulk_urb(dev->rx_urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1), |
| 755 |
dev->rx_skb->data, RTL8150_MTU, read_bulk_callback, dev); |
| 756 |
if ((res = usb_submit_urb(dev->rx_urb, GFP_KERNEL))) { |
| 757 |
if (res == -ENODEV) |
| 758 |
netif_device_detach(dev->netdev); |
| 759 |
dev_warn(&netdev->dev, "rx_urb submit failed: %d\n", res); |
| 760 |
return res; |
| 761 |
} |
| 762 |
usb_fill_int_urb(dev->intr_urb, dev->udev, usb_rcvintpipe(dev->udev, 3), |
| 763 |
dev->intr_buff, INTBUFSIZE, intr_callback, |
| 764 |
dev, dev->intr_interval); |
| 765 |
if ((res = usb_submit_urb(dev->intr_urb, GFP_KERNEL))) { |
| 766 |
if (res == -ENODEV) |
| 767 |
netif_device_detach(dev->netdev); |
| 768 |
dev_warn(&netdev->dev, "intr_urb submit failed: %d\n", res); |
| 769 |
usb_kill_urb(dev->rx_urb); |
| 770 |
return res; |
| 771 |
} |
| 772 |
enable_net_traffic(dev); |
| 773 |
set_carrier(netdev); |
| 774 |
netif_start_queue(netdev); |
| 775 |
|
| 776 |
return res; |
| 777 |
} |
| 778 |
|
| 779 |
static int rtl8150_close(struct net_device *netdev) |
| 780 |
{ |
| 781 |
rtl8150_t *dev = netdev_priv(netdev); |
| 782 |
|
| 783 |
netif_stop_queue(netdev); |
| 784 |
if (!test_bit(RTL8150_UNPLUG, &dev->flags)) |
| 785 |
disable_net_traffic(dev); |
| 786 |
unlink_all_urbs(dev); |
| 787 |
|
| 788 |
return 0; |
| 789 |
} |
| 790 |
|
| 791 |
static void rtl8150_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *info) |
| 792 |
{ |
| 793 |
rtl8150_t *dev = netdev_priv(netdev); |
| 794 |
|
| 795 |
strscpy(info->driver, driver_name, sizeof(info->driver)); |
| 796 |
usb_make_path(dev->udev, info->bus_info, sizeof(info->bus_info)); |
| 797 |
} |
| 798 |
|
| 799 |
static int rtl8150_get_link_ksettings(struct net_device *netdev, |
| 800 |
struct ethtool_link_ksettings *ecmd) |
| 801 |
{ |
| 802 |
rtl8150_t *dev = netdev_priv(netdev); |
| 803 |
short lpa = 0; |
| 804 |
short bmcr = 0; |
| 805 |
u32 supported; |
| 806 |
|
| 807 |
supported = (SUPPORTED_10baseT_Half | |
| 808 |
SUPPORTED_10baseT_Full | |
| 809 |
SUPPORTED_100baseT_Half | |
| 810 |
SUPPORTED_100baseT_Full | |
| 811 |
SUPPORTED_Autoneg | |
| 812 |
SUPPORTED_TP | SUPPORTED_MII); |
| 813 |
ecmd->base.port = PORT_TP; |
| 814 |
ecmd->base.phy_address = dev->phy; |
| 815 |
get_registers(dev, BMCR, 2, &bmcr); |
| 816 |
get_registers(dev, ANLP, 2, &lpa); |
| 817 |
if (bmcr & BMCR_ANENABLE) { |
| 818 |
u32 speed = ((lpa & (LPA_100HALF | LPA_100FULL)) ? |
| 819 |
SPEED_100 : SPEED_10); |
| 820 |
ecmd->base.speed = speed; |
| 821 |
ecmd->base.autoneg = AUTONEG_ENABLE; |
| 822 |
if (speed == SPEED_100) |
| 823 |
ecmd->base.duplex = (lpa & LPA_100FULL) ? |
| 824 |
DUPLEX_FULL : DUPLEX_HALF; |
| 825 |
else |
| 826 |
ecmd->base.duplex = (lpa & LPA_10FULL) ? |
| 827 |
DUPLEX_FULL : DUPLEX_HALF; |
| 828 |
} else { |
| 829 |
ecmd->base.autoneg = AUTONEG_DISABLE; |
| 830 |
ecmd->base.speed = ((bmcr & BMCR_SPEED100) ? |
| 831 |
SPEED_100 : SPEED_10); |
| 832 |
ecmd->base.duplex = (bmcr & BMCR_FULLDPLX) ? |
| 833 |
DUPLEX_FULL : DUPLEX_HALF; |
| 834 |
} |
| 835 |
|
| 836 |
ethtool_convert_legacy_u32_to_link_mode(ecmd->link_modes.supported, |
| 837 |
supported); |
| 838 |
|
| 839 |
return 0; |
| 840 |
} |
| 841 |
|
| 842 |
static const struct ethtool_ops ops = { |
| 843 |
.get_drvinfo = rtl8150_get_drvinfo, |
| 844 |
.get_link = ethtool_op_get_link, |
| 845 |
.get_link_ksettings = rtl8150_get_link_ksettings, |
| 846 |
}; |
| 847 |
|
| 848 |
static int rtl8150_siocdevprivate(struct net_device *netdev, struct ifreq *rq, |
| 849 |
void __user *udata, int cmd) |
| 850 |
{ |
| 851 |
rtl8150_t *dev = netdev_priv(netdev); |
| 852 |
u16 *data = (u16 *) & rq->ifr_ifru; |
| 853 |
int res = 0; |
| 854 |
|
| 855 |
switch (cmd) { |
| 856 |
case SIOCDEVPRIVATE: |
| 857 |
data[0] = dev->phy; |
| 858 |
fallthrough; |
| 859 |
case SIOCDEVPRIVATE + 1: |
| 860 |
read_mii_word(dev, dev->phy, (data[1] & 0x1f), &data[3]); |
| 861 |
break; |
| 862 |
case SIOCDEVPRIVATE + 2: |
| 863 |
if (!capable(CAP_NET_ADMIN)) |
| 864 |
return -EPERM; |
| 865 |
write_mii_word(dev, dev->phy, (data[1] & 0x1f), data[2]); |
| 866 |
break; |
| 867 |
default: |
| 868 |
res = -EOPNOTSUPP; |
| 869 |
} |
| 870 |
|
| 871 |
return res; |
| 872 |
} |
| 873 |
|
| 874 |
static const struct net_device_ops rtl8150_netdev_ops = { |
| 875 |
.ndo_open = rtl8150_open, |
| 876 |
.ndo_stop = rtl8150_close, |
| 877 |
.ndo_siocdevprivate = rtl8150_siocdevprivate, |
| 878 |
.ndo_start_xmit = rtl8150_start_xmit, |
| 879 |
.ndo_tx_timeout = rtl8150_tx_timeout, |
| 880 |
.ndo_set_rx_mode = rtl8150_set_multicast, |
| 881 |
.ndo_set_mac_address = rtl8150_set_mac_address, |
| 882 |
|
| 883 |
.ndo_validate_addr = eth_validate_addr, |
| 884 |
}; |
| 885 |
|
| 886 |
static int rtl8150_probe(struct usb_interface *intf, |
| 887 |
const struct usb_device_id *id) |
| 888 |
{ |
| 889 |
struct usb_device *udev = interface_to_usbdev(intf); |
| 890 |
rtl8150_t *dev; |
| 891 |
struct net_device *netdev; |
| 892 |
static const u8 bulk_ep_addr[] = { |
| 893 |
RTL8150_USB_EP_BULK_IN | USB_DIR_IN, |
| 894 |
RTL8150_USB_EP_BULK_OUT | USB_DIR_OUT, |
| 895 |
0}; |
| 896 |
static const u8 int_ep_addr[] = { |
| 897 |
RTL8150_USB_EP_INT_IN | USB_DIR_IN, |
| 898 |
0}; |
| 899 |
|
| 900 |
netdev = alloc_etherdev(sizeof(rtl8150_t)); |
| 901 |
if (!netdev) |
| 902 |
return -ENOMEM; |
| 903 |
|
| 904 |
dev = netdev_priv(netdev); |
| 905 |
|
| 906 |
dev->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL); |
| 907 |
if (!dev->intr_buff) { |
| 908 |
free_netdev(netdev); |
| 909 |
return -ENOMEM; |
| 910 |
} |
| 911 |
|
| 912 |
/* Verify that all required endpoints are present */ |
| 913 |
if (!usb_check_bulk_endpoints(intf, bulk_ep_addr) || |
| 914 |
!usb_check_int_endpoints(intf, int_ep_addr)) { |
| 915 |
dev_err(&intf->dev, "couldn't find required endpoints\n"); |
| 916 |
goto out; |
| 917 |
} |
| 918 |
|
| 919 |
tasklet_setup(&dev->tl, rx_fixup); |
| 920 |
spin_lock_init(&dev->rx_pool_lock); |
| 921 |
|
| 922 |
dev->udev = udev; |
| 923 |
dev->netdev = netdev; |
| 924 |
netdev->netdev_ops = &rtl8150_netdev_ops; |
| 925 |
netdev->watchdog_timeo = RTL8150_TX_TIMEOUT; |
| 926 |
netdev->ethtool_ops = &ops; |
| 927 |
dev->intr_interval = 100; /* 100ms */ |
| 928 |
|
| 929 |
if (!alloc_all_urbs(dev)) { |
| 930 |
dev_err(&intf->dev, "out of memory\n"); |
| 931 |
goto out; |
| 932 |
} |
| 933 |
if (!rtl8150_reset(dev)) { |
| 934 |
dev_err(&intf->dev, "couldn't reset the device\n"); |
| 935 |
goto out1; |
| 936 |
} |
| 937 |
fill_skb_pool(dev); |
| 938 |
set_ethernet_addr(dev); |
| 939 |
|
| 940 |
usb_set_intfdata(intf, dev); |
| 941 |
SET_NETDEV_DEV(netdev, &intf->dev); |
| 942 |
if (register_netdev(netdev) != 0) { |
| 943 |
dev_err(&intf->dev, "couldn't register the device\n"); |
| 944 |
goto out2; |
| 945 |
} |
| 946 |
|
| 947 |
dev_info(&intf->dev, "%s: rtl8150 is detected\n", netdev->name); |
| 948 |
|
| 949 |
return 0; |
| 950 |
|
| 951 |
out2: |
| 952 |
usb_set_intfdata(intf, NULL); |
| 953 |
free_skb_pool(dev); |
| 954 |
out1: |
| 955 |
free_all_urbs(dev); |
| 956 |
out: |
| 957 |
kfree(dev->intr_buff); |
| 958 |
free_netdev(netdev); |
| 959 |
return -EIO; |
| 960 |
} |
| 961 |
|
| 962 |
static void rtl8150_disconnect(struct usb_interface *intf) |
| 963 |
{ |
| 964 |
rtl8150_t *dev = usb_get_intfdata(intf); |
| 965 |
|
| 966 |
usb_set_intfdata(intf, NULL); |
| 967 |
if (dev) { |
| 968 |
set_bit(RTL8150_UNPLUG, &dev->flags); |
| 969 |
tasklet_kill(&dev->tl); |
| 970 |
unregister_netdev(dev->netdev); |
| 971 |
unlink_all_urbs(dev); |
| 972 |
free_all_urbs(dev); |
| 973 |
free_skb_pool(dev); |
| 974 |
dev_kfree_skb(dev->rx_skb); |
| 975 |
kfree(dev->intr_buff); |
| 976 |
free_netdev(dev->netdev); |
| 977 |
} |
| 978 |
} |
| 979 |
|
| 980 |
static struct usb_driver rtl8150_driver = { |
| 981 |
.name = driver_name, |
| 982 |
.probe = rtl8150_probe, |
| 983 |
.disconnect = rtl8150_disconnect, |
| 984 |
.id_table = rtl8150_table, |
| 985 |
.suspend = rtl8150_suspend, |
| 986 |
.resume = rtl8150_resume, |
| 987 |
.disable_hub_initiated_lpm = 1, |
| 988 |
}; |
| 989 |
|
| 990 |
module_usb_driver(rtl8150_driver); |
| 991 |
|
| 992 |
MODULE_AUTHOR(DRIVER_AUTHOR); |
| 993 |
MODULE_DESCRIPTION(DRIVER_DESC); |
| 994 |
MODULE_LICENSE("GPL"); |