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