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