Linux 3.11-rc3
[cris-mirror.git] / drivers / net / usb / rtl8150.c
blob6cbdac67f3a0d3f899b0a3ca783d427b2e1c9704
1 /*
2 * Copyright (c) 2002 Petko Manolov (petkan@users.sourceforge.net)
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 */
9 #include <linux/init.h>
10 #include <linux/signal.h>
11 #include <linux/slab.h>
12 #include <linux/module.h>
13 #include <linux/netdevice.h>
14 #include <linux/etherdevice.h>
15 #include <linux/mii.h>
16 #include <linux/ethtool.h>
17 #include <linux/usb.h>
18 #include <asm/uaccess.h>
20 /* Version Information */
21 #define DRIVER_VERSION "v0.6.2 (2004/08/27)"
22 #define DRIVER_AUTHOR "Petko Manolov <petkan@users.sourceforge.net>"
23 #define DRIVER_DESC "rtl8150 based usb-ethernet driver"
25 #define IDR 0x0120
26 #define MAR 0x0126
27 #define CR 0x012e
28 #define TCR 0x012f
29 #define RCR 0x0130
30 #define TSR 0x0132
31 #define RSR 0x0133
32 #define CON0 0x0135
33 #define CON1 0x0136
34 #define MSR 0x0137
35 #define PHYADD 0x0138
36 #define PHYDAT 0x0139
37 #define PHYCNT 0x013b
38 #define GPPC 0x013d
39 #define BMCR 0x0140
40 #define BMSR 0x0142
41 #define ANAR 0x0144
42 #define ANLP 0x0146
43 #define AER 0x0148
44 #define CSCR 0x014C /* This one has the link status */
45 #define CSCR_LINK_STATUS (1 << 3)
47 #define IDR_EEPROM 0x1202
49 #define PHY_READ 0
50 #define PHY_WRITE 0x20
51 #define PHY_GO 0x40
53 #define MII_TIMEOUT 10
54 #define INTBUFSIZE 8
56 #define RTL8150_REQT_READ 0xc0
57 #define RTL8150_REQT_WRITE 0x40
58 #define RTL8150_REQ_GET_REGS 0x05
59 #define RTL8150_REQ_SET_REGS 0x05
62 /* Transmit status register errors */
63 #define TSR_ECOL (1<<5)
64 #define TSR_LCOL (1<<4)
65 #define TSR_LOSS_CRS (1<<3)
66 #define TSR_JBR (1<<2)
67 #define TSR_ERRORS (TSR_ECOL | TSR_LCOL | TSR_LOSS_CRS | TSR_JBR)
68 /* Receive status register errors */
69 #define RSR_CRC (1<<2)
70 #define RSR_FAE (1<<1)
71 #define RSR_ERRORS (RSR_CRC | RSR_FAE)
73 /* Media status register definitions */
74 #define MSR_DUPLEX (1<<4)
75 #define MSR_SPEED (1<<3)
76 #define MSR_LINK (1<<2)
78 /* Interrupt pipe data */
79 #define INT_TSR 0x00
80 #define INT_RSR 0x01
81 #define INT_MSR 0x02
82 #define INT_WAKSR 0x03
83 #define INT_TXOK_CNT 0x04
84 #define INT_RXLOST_CNT 0x05
85 #define INT_CRERR_CNT 0x06
86 #define INT_COL_CNT 0x07
89 #define RTL8150_MTU 1540
90 #define RTL8150_TX_TIMEOUT (HZ)
91 #define RX_SKB_POOL_SIZE 4
93 /* rtl8150 flags */
94 #define RTL8150_HW_CRC 0
95 #define RX_REG_SET 1
96 #define RTL8150_UNPLUG 2
97 #define RX_URB_FAIL 3
99 /* Define these values to match your device */
100 #define VENDOR_ID_REALTEK 0x0bda
101 #define VENDOR_ID_MELCO 0x0411
102 #define VENDOR_ID_MICRONET 0x3980
103 #define VENDOR_ID_LONGSHINE 0x07b8
104 #define VENDOR_ID_OQO 0x1557
105 #define VENDOR_ID_ZYXEL 0x0586
107 #define PRODUCT_ID_RTL8150 0x8150
108 #define PRODUCT_ID_LUAKTX 0x0012
109 #define PRODUCT_ID_LCS8138TX 0x401a
110 #define PRODUCT_ID_SP128AR 0x0003
111 #define PRODUCT_ID_PRESTIGE 0x401a
113 #undef EEPROM_WRITE
115 /* table of devices that work with this driver */
116 static struct usb_device_id rtl8150_table[] = {
117 {USB_DEVICE(VENDOR_ID_REALTEK, PRODUCT_ID_RTL8150)},
118 {USB_DEVICE(VENDOR_ID_MELCO, PRODUCT_ID_LUAKTX)},
119 {USB_DEVICE(VENDOR_ID_MICRONET, PRODUCT_ID_SP128AR)},
120 {USB_DEVICE(VENDOR_ID_LONGSHINE, PRODUCT_ID_LCS8138TX)},
121 {USB_DEVICE(VENDOR_ID_OQO, PRODUCT_ID_RTL8150)},
122 {USB_DEVICE(VENDOR_ID_ZYXEL, PRODUCT_ID_PRESTIGE)},
126 MODULE_DEVICE_TABLE(usb, rtl8150_table);
128 struct rtl8150 {
129 unsigned long flags;
130 struct usb_device *udev;
131 struct tasklet_struct tl;
132 struct net_device *netdev;
133 struct urb *rx_urb, *tx_urb, *intr_urb;
134 struct sk_buff *tx_skb, *rx_skb;
135 struct sk_buff *rx_skb_pool[RX_SKB_POOL_SIZE];
136 spinlock_t rx_pool_lock;
137 struct usb_ctrlrequest dr;
138 int intr_interval;
139 u8 *intr_buff;
140 u8 phy;
143 typedef struct rtl8150 rtl8150_t;
145 struct async_req {
146 struct usb_ctrlrequest dr;
147 u16 rx_creg;
150 static const char driver_name [] = "rtl8150";
154 ** device related part of the code
157 static int get_registers(rtl8150_t * dev, u16 indx, u16 size, void *data)
159 return usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
160 RTL8150_REQ_GET_REGS, RTL8150_REQT_READ,
161 indx, 0, data, size, 500);
164 static int set_registers(rtl8150_t * dev, u16 indx, u16 size, void *data)
166 return usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
167 RTL8150_REQ_SET_REGS, RTL8150_REQT_WRITE,
168 indx, 0, data, size, 500);
171 static void async_set_reg_cb(struct urb *urb)
173 struct async_req *req = (struct async_req *)urb->context;
174 int status = urb->status;
176 if (status < 0)
177 dev_dbg(&urb->dev->dev, "%s failed with %d", __func__, status);
178 kfree(req);
179 usb_free_urb(urb);
182 static int async_set_registers(rtl8150_t *dev, u16 indx, u16 size, u16 reg)
184 int res = -ENOMEM;
185 struct urb *async_urb;
186 struct async_req *req;
188 req = kmalloc(sizeof(struct async_req), GFP_ATOMIC);
189 if (req == NULL)
190 return res;
191 async_urb = usb_alloc_urb(0, GFP_ATOMIC);
192 if (async_urb == NULL) {
193 kfree(req);
194 return res;
196 req->rx_creg = cpu_to_le16(reg);
197 req->dr.bRequestType = RTL8150_REQT_WRITE;
198 req->dr.bRequest = RTL8150_REQ_SET_REGS;
199 req->dr.wIndex = 0;
200 req->dr.wValue = cpu_to_le16(indx);
201 req->dr.wLength = cpu_to_le16(size);
202 usb_fill_control_urb(async_urb, dev->udev,
203 usb_sndctrlpipe(dev->udev, 0), (void *)&req->dr,
204 &req->rx_creg, size, async_set_reg_cb, req);
205 res = usb_submit_urb(async_urb, GFP_ATOMIC);
206 if (res) {
207 if (res == -ENODEV)
208 netif_device_detach(dev->netdev);
209 dev_err(&dev->udev->dev, "%s failed with %d\n", __func__, res);
211 return res;
214 static int read_mii_word(rtl8150_t * dev, u8 phy, __u8 indx, u16 * reg)
216 int i;
217 u8 data[3], tmp;
219 data[0] = phy;
220 data[1] = data[2] = 0;
221 tmp = indx | PHY_READ | PHY_GO;
222 i = 0;
224 set_registers(dev, PHYADD, sizeof(data), data);
225 set_registers(dev, PHYCNT, 1, &tmp);
226 do {
227 get_registers(dev, PHYCNT, 1, data);
228 } while ((data[0] & PHY_GO) && (i++ < MII_TIMEOUT));
230 if (i <= MII_TIMEOUT) {
231 get_registers(dev, PHYDAT, 2, data);
232 *reg = data[0] | (data[1] << 8);
233 return 0;
234 } else
235 return 1;
238 static int write_mii_word(rtl8150_t * dev, u8 phy, __u8 indx, u16 reg)
240 int i;
241 u8 data[3], tmp;
243 data[0] = phy;
244 data[1] = reg & 0xff;
245 data[2] = (reg >> 8) & 0xff;
246 tmp = indx | PHY_WRITE | PHY_GO;
247 i = 0;
249 set_registers(dev, PHYADD, sizeof(data), data);
250 set_registers(dev, PHYCNT, 1, &tmp);
251 do {
252 get_registers(dev, PHYCNT, 1, data);
253 } while ((data[0] & PHY_GO) && (i++ < MII_TIMEOUT));
255 if (i <= MII_TIMEOUT)
256 return 0;
257 else
258 return 1;
261 static inline void set_ethernet_addr(rtl8150_t * dev)
263 u8 node_id[6];
265 get_registers(dev, IDR, sizeof(node_id), node_id);
266 memcpy(dev->netdev->dev_addr, node_id, sizeof(node_id));
269 static int rtl8150_set_mac_address(struct net_device *netdev, void *p)
271 struct sockaddr *addr = p;
272 rtl8150_t *dev = netdev_priv(netdev);
274 if (netif_running(netdev))
275 return -EBUSY;
277 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
278 netdev_dbg(netdev, "Setting MAC address to %pM\n", netdev->dev_addr);
279 /* Set the IDR registers. */
280 set_registers(dev, IDR, netdev->addr_len, netdev->dev_addr);
281 #ifdef EEPROM_WRITE
283 int i;
284 u8 cr;
285 /* Get the CR contents. */
286 get_registers(dev, CR, 1, &cr);
287 /* Set the WEPROM bit (eeprom write enable). */
288 cr |= 0x20;
289 set_registers(dev, CR, 1, &cr);
290 /* Write the MAC address into eeprom. Eeprom writes must be word-sized,
291 so we need to split them up. */
292 for (i = 0; i * 2 < netdev->addr_len; i++) {
293 set_registers(dev, IDR_EEPROM + (i * 2), 2,
294 netdev->dev_addr + (i * 2));
296 /* Clear the WEPROM bit (preventing accidental eeprom writes). */
297 cr &= 0xdf;
298 set_registers(dev, CR, 1, &cr);
300 #endif
301 return 0;
304 static int rtl8150_reset(rtl8150_t * dev)
306 u8 data = 0x10;
307 int i = HZ;
309 set_registers(dev, CR, 1, &data);
310 do {
311 get_registers(dev, CR, 1, &data);
312 } while ((data & 0x10) && --i);
314 return (i > 0) ? 1 : 0;
317 static int alloc_all_urbs(rtl8150_t * dev)
319 dev->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
320 if (!dev->rx_urb)
321 return 0;
322 dev->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
323 if (!dev->tx_urb) {
324 usb_free_urb(dev->rx_urb);
325 return 0;
327 dev->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
328 if (!dev->intr_urb) {
329 usb_free_urb(dev->rx_urb);
330 usb_free_urb(dev->tx_urb);
331 return 0;
334 return 1;
337 static void free_all_urbs(rtl8150_t * dev)
339 usb_free_urb(dev->rx_urb);
340 usb_free_urb(dev->tx_urb);
341 usb_free_urb(dev->intr_urb);
344 static void unlink_all_urbs(rtl8150_t * dev)
346 usb_kill_urb(dev->rx_urb);
347 usb_kill_urb(dev->tx_urb);
348 usb_kill_urb(dev->intr_urb);
351 static inline struct sk_buff *pull_skb(rtl8150_t *dev)
353 struct sk_buff *skb;
354 int i;
356 for (i = 0; i < RX_SKB_POOL_SIZE; i++) {
357 if (dev->rx_skb_pool[i]) {
358 skb = dev->rx_skb_pool[i];
359 dev->rx_skb_pool[i] = NULL;
360 return skb;
363 return NULL;
366 static void read_bulk_callback(struct urb *urb)
368 rtl8150_t *dev;
369 unsigned pkt_len, res;
370 struct sk_buff *skb;
371 struct net_device *netdev;
372 u16 rx_stat;
373 int status = urb->status;
374 int result;
376 dev = urb->context;
377 if (!dev)
378 return;
379 if (test_bit(RTL8150_UNPLUG, &dev->flags))
380 return;
381 netdev = dev->netdev;
382 if (!netif_device_present(netdev))
383 return;
385 switch (status) {
386 case 0:
387 break;
388 case -ENOENT:
389 return; /* the urb is in unlink state */
390 case -ETIME:
391 if (printk_ratelimit())
392 dev_warn(&urb->dev->dev, "may be reset is needed?..\n");
393 goto goon;
394 default:
395 if (printk_ratelimit())
396 dev_warn(&urb->dev->dev, "Rx status %d\n", status);
397 goto goon;
400 if (!dev->rx_skb)
401 goto resched;
402 /* protect against short packets (tell me why we got some?!?) */
403 if (urb->actual_length < 4)
404 goto goon;
406 res = urb->actual_length;
407 rx_stat = le16_to_cpu(*(__le16 *)(urb->transfer_buffer + res - 4));
408 pkt_len = res - 4;
410 skb_put(dev->rx_skb, pkt_len);
411 dev->rx_skb->protocol = eth_type_trans(dev->rx_skb, netdev);
412 netif_rx(dev->rx_skb);
413 netdev->stats.rx_packets++;
414 netdev->stats.rx_bytes += pkt_len;
416 spin_lock(&dev->rx_pool_lock);
417 skb = pull_skb(dev);
418 spin_unlock(&dev->rx_pool_lock);
419 if (!skb)
420 goto resched;
422 dev->rx_skb = skb;
423 goon:
424 usb_fill_bulk_urb(dev->rx_urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
425 dev->rx_skb->data, RTL8150_MTU, read_bulk_callback, dev);
426 result = usb_submit_urb(dev->rx_urb, GFP_ATOMIC);
427 if (result == -ENODEV)
428 netif_device_detach(dev->netdev);
429 else if (result) {
430 set_bit(RX_URB_FAIL, &dev->flags);
431 goto resched;
432 } else {
433 clear_bit(RX_URB_FAIL, &dev->flags);
436 return;
437 resched:
438 tasklet_schedule(&dev->tl);
441 static void write_bulk_callback(struct urb *urb)
443 rtl8150_t *dev;
444 int status = urb->status;
446 dev = urb->context;
447 if (!dev)
448 return;
449 dev_kfree_skb_irq(dev->tx_skb);
450 if (!netif_device_present(dev->netdev))
451 return;
452 if (status)
453 dev_info(&urb->dev->dev, "%s: Tx status %d\n",
454 dev->netdev->name, status);
455 dev->netdev->trans_start = jiffies;
456 netif_wake_queue(dev->netdev);
459 static void intr_callback(struct urb *urb)
461 rtl8150_t *dev;
462 __u8 *d;
463 int status = urb->status;
464 int res;
466 dev = urb->context;
467 if (!dev)
468 return;
469 switch (status) {
470 case 0: /* success */
471 break;
472 case -ECONNRESET: /* unlink */
473 case -ENOENT:
474 case -ESHUTDOWN:
475 return;
476 /* -EPIPE: should clear the halt */
477 default:
478 dev_info(&urb->dev->dev, "%s: intr status %d\n",
479 dev->netdev->name, status);
480 goto resubmit;
483 d = urb->transfer_buffer;
484 if (d[0] & TSR_ERRORS) {
485 dev->netdev->stats.tx_errors++;
486 if (d[INT_TSR] & (TSR_ECOL | TSR_JBR))
487 dev->netdev->stats.tx_aborted_errors++;
488 if (d[INT_TSR] & TSR_LCOL)
489 dev->netdev->stats.tx_window_errors++;
490 if (d[INT_TSR] & TSR_LOSS_CRS)
491 dev->netdev->stats.tx_carrier_errors++;
493 /* Report link status changes to the network stack */
494 if ((d[INT_MSR] & MSR_LINK) == 0) {
495 if (netif_carrier_ok(dev->netdev)) {
496 netif_carrier_off(dev->netdev);
497 netdev_dbg(dev->netdev, "%s: LINK LOST\n", __func__);
499 } else {
500 if (!netif_carrier_ok(dev->netdev)) {
501 netif_carrier_on(dev->netdev);
502 netdev_dbg(dev->netdev, "%s: LINK CAME BACK\n", __func__);
506 resubmit:
507 res = usb_submit_urb (urb, GFP_ATOMIC);
508 if (res == -ENODEV)
509 netif_device_detach(dev->netdev);
510 else if (res)
511 dev_err(&dev->udev->dev,
512 "can't resubmit intr, %s-%s/input0, status %d\n",
513 dev->udev->bus->bus_name, dev->udev->devpath, res);
516 static int rtl8150_suspend(struct usb_interface *intf, pm_message_t message)
518 rtl8150_t *dev = usb_get_intfdata(intf);
520 netif_device_detach(dev->netdev);
522 if (netif_running(dev->netdev)) {
523 usb_kill_urb(dev->rx_urb);
524 usb_kill_urb(dev->intr_urb);
526 return 0;
529 static int rtl8150_resume(struct usb_interface *intf)
531 rtl8150_t *dev = usb_get_intfdata(intf);
533 netif_device_attach(dev->netdev);
534 if (netif_running(dev->netdev)) {
535 dev->rx_urb->status = 0;
536 dev->rx_urb->actual_length = 0;
537 read_bulk_callback(dev->rx_urb);
539 dev->intr_urb->status = 0;
540 dev->intr_urb->actual_length = 0;
541 intr_callback(dev->intr_urb);
543 return 0;
548 ** network related part of the code
552 static void fill_skb_pool(rtl8150_t *dev)
554 struct sk_buff *skb;
555 int i;
557 for (i = 0; i < RX_SKB_POOL_SIZE; i++) {
558 if (dev->rx_skb_pool[i])
559 continue;
560 skb = dev_alloc_skb(RTL8150_MTU + 2);
561 if (!skb) {
562 return;
564 skb_reserve(skb, 2);
565 dev->rx_skb_pool[i] = skb;
569 static void free_skb_pool(rtl8150_t *dev)
571 int i;
573 for (i = 0; i < RX_SKB_POOL_SIZE; i++)
574 if (dev->rx_skb_pool[i])
575 dev_kfree_skb(dev->rx_skb_pool[i]);
578 static void rx_fixup(unsigned long data)
580 struct rtl8150 *dev = (struct rtl8150 *)data;
581 struct sk_buff *skb;
582 int status;
584 spin_lock_irq(&dev->rx_pool_lock);
585 fill_skb_pool(dev);
586 spin_unlock_irq(&dev->rx_pool_lock);
587 if (test_bit(RX_URB_FAIL, &dev->flags))
588 if (dev->rx_skb)
589 goto try_again;
590 spin_lock_irq(&dev->rx_pool_lock);
591 skb = pull_skb(dev);
592 spin_unlock_irq(&dev->rx_pool_lock);
593 if (skb == NULL)
594 goto tlsched;
595 dev->rx_skb = skb;
596 usb_fill_bulk_urb(dev->rx_urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
597 dev->rx_skb->data, RTL8150_MTU, read_bulk_callback, dev);
598 try_again:
599 status = usb_submit_urb(dev->rx_urb, GFP_ATOMIC);
600 if (status == -ENODEV) {
601 netif_device_detach(dev->netdev);
602 } else if (status) {
603 set_bit(RX_URB_FAIL, &dev->flags);
604 goto tlsched;
605 } else {
606 clear_bit(RX_URB_FAIL, &dev->flags);
609 return;
610 tlsched:
611 tasklet_schedule(&dev->tl);
614 static int enable_net_traffic(rtl8150_t * dev)
616 u8 cr, tcr, rcr, msr;
618 if (!rtl8150_reset(dev)) {
619 dev_warn(&dev->udev->dev, "device reset failed\n");
621 /* RCR bit7=1 attach Rx info at the end; =0 HW CRC (which is broken) */
622 rcr = 0x9e;
623 tcr = 0xd8;
624 cr = 0x0c;
625 if (!(rcr & 0x80))
626 set_bit(RTL8150_HW_CRC, &dev->flags);
627 set_registers(dev, RCR, 1, &rcr);
628 set_registers(dev, TCR, 1, &tcr);
629 set_registers(dev, CR, 1, &cr);
630 get_registers(dev, MSR, 1, &msr);
632 return 0;
635 static void disable_net_traffic(rtl8150_t * dev)
637 u8 cr;
639 get_registers(dev, CR, 1, &cr);
640 cr &= 0xf3;
641 set_registers(dev, CR, 1, &cr);
644 static void rtl8150_tx_timeout(struct net_device *netdev)
646 rtl8150_t *dev = netdev_priv(netdev);
647 dev_warn(&netdev->dev, "Tx timeout.\n");
648 usb_unlink_urb(dev->tx_urb);
649 netdev->stats.tx_errors++;
652 static void rtl8150_set_multicast(struct net_device *netdev)
654 rtl8150_t *dev = netdev_priv(netdev);
655 u16 rx_creg = 0x9e;
657 netif_stop_queue(netdev);
658 if (netdev->flags & IFF_PROMISC) {
659 rx_creg |= 0x0001;
660 dev_info(&netdev->dev, "%s: promiscuous mode\n", netdev->name);
661 } else if (!netdev_mc_empty(netdev) ||
662 (netdev->flags & IFF_ALLMULTI)) {
663 rx_creg &= 0xfffe;
664 rx_creg |= 0x0002;
665 dev_info(&netdev->dev, "%s: allmulti set\n", netdev->name);
666 } else {
667 /* ~RX_MULTICAST, ~RX_PROMISCUOUS */
668 rx_creg &= 0x00fc;
670 async_set_registers(dev, RCR, sizeof(rx_creg), rx_creg);
671 netif_wake_queue(netdev);
674 static netdev_tx_t rtl8150_start_xmit(struct sk_buff *skb,
675 struct net_device *netdev)
677 rtl8150_t *dev = netdev_priv(netdev);
678 int count, res;
680 netif_stop_queue(netdev);
681 count = (skb->len < 60) ? 60 : skb->len;
682 count = (count & 0x3f) ? count : count + 1;
683 dev->tx_skb = skb;
684 usb_fill_bulk_urb(dev->tx_urb, dev->udev, usb_sndbulkpipe(dev->udev, 2),
685 skb->data, count, write_bulk_callback, dev);
686 if ((res = usb_submit_urb(dev->tx_urb, GFP_ATOMIC))) {
687 /* Can we get/handle EPIPE here? */
688 if (res == -ENODEV)
689 netif_device_detach(dev->netdev);
690 else {
691 dev_warn(&netdev->dev, "failed tx_urb %d\n", res);
692 netdev->stats.tx_errors++;
693 netif_start_queue(netdev);
695 } else {
696 netdev->stats.tx_packets++;
697 netdev->stats.tx_bytes += skb->len;
698 netdev->trans_start = jiffies;
701 return NETDEV_TX_OK;
705 static void set_carrier(struct net_device *netdev)
707 rtl8150_t *dev = netdev_priv(netdev);
708 short tmp;
710 get_registers(dev, CSCR, 2, &tmp);
711 if (tmp & CSCR_LINK_STATUS)
712 netif_carrier_on(netdev);
713 else
714 netif_carrier_off(netdev);
717 static int rtl8150_open(struct net_device *netdev)
719 rtl8150_t *dev = netdev_priv(netdev);
720 int res;
722 if (dev->rx_skb == NULL)
723 dev->rx_skb = pull_skb(dev);
724 if (!dev->rx_skb)
725 return -ENOMEM;
727 set_registers(dev, IDR, 6, netdev->dev_addr);
729 usb_fill_bulk_urb(dev->rx_urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
730 dev->rx_skb->data, RTL8150_MTU, read_bulk_callback, dev);
731 if ((res = usb_submit_urb(dev->rx_urb, GFP_KERNEL))) {
732 if (res == -ENODEV)
733 netif_device_detach(dev->netdev);
734 dev_warn(&netdev->dev, "rx_urb submit failed: %d\n", res);
735 return res;
737 usb_fill_int_urb(dev->intr_urb, dev->udev, usb_rcvintpipe(dev->udev, 3),
738 dev->intr_buff, INTBUFSIZE, intr_callback,
739 dev, dev->intr_interval);
740 if ((res = usb_submit_urb(dev->intr_urb, GFP_KERNEL))) {
741 if (res == -ENODEV)
742 netif_device_detach(dev->netdev);
743 dev_warn(&netdev->dev, "intr_urb submit failed: %d\n", res);
744 usb_kill_urb(dev->rx_urb);
745 return res;
747 enable_net_traffic(dev);
748 set_carrier(netdev);
749 netif_start_queue(netdev);
751 return res;
754 static int rtl8150_close(struct net_device *netdev)
756 rtl8150_t *dev = netdev_priv(netdev);
757 int res = 0;
759 netif_stop_queue(netdev);
760 if (!test_bit(RTL8150_UNPLUG, &dev->flags))
761 disable_net_traffic(dev);
762 unlink_all_urbs(dev);
764 return res;
767 static void rtl8150_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *info)
769 rtl8150_t *dev = netdev_priv(netdev);
771 strlcpy(info->driver, driver_name, sizeof(info->driver));
772 strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
773 usb_make_path(dev->udev, info->bus_info, sizeof(info->bus_info));
776 static int rtl8150_get_settings(struct net_device *netdev, struct ethtool_cmd *ecmd)
778 rtl8150_t *dev = netdev_priv(netdev);
779 short lpa, bmcr;
781 ecmd->supported = (SUPPORTED_10baseT_Half |
782 SUPPORTED_10baseT_Full |
783 SUPPORTED_100baseT_Half |
784 SUPPORTED_100baseT_Full |
785 SUPPORTED_Autoneg |
786 SUPPORTED_TP | SUPPORTED_MII);
787 ecmd->port = PORT_TP;
788 ecmd->transceiver = XCVR_INTERNAL;
789 ecmd->phy_address = dev->phy;
790 get_registers(dev, BMCR, 2, &bmcr);
791 get_registers(dev, ANLP, 2, &lpa);
792 if (bmcr & BMCR_ANENABLE) {
793 u32 speed = ((lpa & (LPA_100HALF | LPA_100FULL)) ?
794 SPEED_100 : SPEED_10);
795 ethtool_cmd_speed_set(ecmd, speed);
796 ecmd->autoneg = AUTONEG_ENABLE;
797 if (speed == SPEED_100)
798 ecmd->duplex = (lpa & LPA_100FULL) ?
799 DUPLEX_FULL : DUPLEX_HALF;
800 else
801 ecmd->duplex = (lpa & LPA_10FULL) ?
802 DUPLEX_FULL : DUPLEX_HALF;
803 } else {
804 ecmd->autoneg = AUTONEG_DISABLE;
805 ethtool_cmd_speed_set(ecmd, ((bmcr & BMCR_SPEED100) ?
806 SPEED_100 : SPEED_10));
807 ecmd->duplex = (bmcr & BMCR_FULLDPLX) ?
808 DUPLEX_FULL : DUPLEX_HALF;
810 return 0;
813 static const struct ethtool_ops ops = {
814 .get_drvinfo = rtl8150_get_drvinfo,
815 .get_settings = rtl8150_get_settings,
816 .get_link = ethtool_op_get_link
819 static int rtl8150_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
821 rtl8150_t *dev = netdev_priv(netdev);
822 u16 *data = (u16 *) & rq->ifr_ifru;
823 int res = 0;
825 switch (cmd) {
826 case SIOCDEVPRIVATE:
827 data[0] = dev->phy;
828 case SIOCDEVPRIVATE + 1:
829 read_mii_word(dev, dev->phy, (data[1] & 0x1f), &data[3]);
830 break;
831 case SIOCDEVPRIVATE + 2:
832 if (!capable(CAP_NET_ADMIN))
833 return -EPERM;
834 write_mii_word(dev, dev->phy, (data[1] & 0x1f), data[2]);
835 break;
836 default:
837 res = -EOPNOTSUPP;
840 return res;
843 static const struct net_device_ops rtl8150_netdev_ops = {
844 .ndo_open = rtl8150_open,
845 .ndo_stop = rtl8150_close,
846 .ndo_do_ioctl = rtl8150_ioctl,
847 .ndo_start_xmit = rtl8150_start_xmit,
848 .ndo_tx_timeout = rtl8150_tx_timeout,
849 .ndo_set_rx_mode = rtl8150_set_multicast,
850 .ndo_set_mac_address = rtl8150_set_mac_address,
852 .ndo_change_mtu = eth_change_mtu,
853 .ndo_validate_addr = eth_validate_addr,
856 static int rtl8150_probe(struct usb_interface *intf,
857 const struct usb_device_id *id)
859 struct usb_device *udev = interface_to_usbdev(intf);
860 rtl8150_t *dev;
861 struct net_device *netdev;
863 netdev = alloc_etherdev(sizeof(rtl8150_t));
864 if (!netdev)
865 return -ENOMEM;
867 dev = netdev_priv(netdev);
869 dev->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL);
870 if (!dev->intr_buff) {
871 free_netdev(netdev);
872 return -ENOMEM;
875 tasklet_init(&dev->tl, rx_fixup, (unsigned long)dev);
876 spin_lock_init(&dev->rx_pool_lock);
878 dev->udev = udev;
879 dev->netdev = netdev;
880 netdev->netdev_ops = &rtl8150_netdev_ops;
881 netdev->watchdog_timeo = RTL8150_TX_TIMEOUT;
882 SET_ETHTOOL_OPS(netdev, &ops);
883 dev->intr_interval = 100; /* 100ms */
885 if (!alloc_all_urbs(dev)) {
886 dev_err(&intf->dev, "out of memory\n");
887 goto out;
889 if (!rtl8150_reset(dev)) {
890 dev_err(&intf->dev, "couldn't reset the device\n");
891 goto out1;
893 fill_skb_pool(dev);
894 set_ethernet_addr(dev);
896 usb_set_intfdata(intf, dev);
897 SET_NETDEV_DEV(netdev, &intf->dev);
898 if (register_netdev(netdev) != 0) {
899 dev_err(&intf->dev, "couldn't register the device\n");
900 goto out2;
903 dev_info(&intf->dev, "%s: rtl8150 is detected\n", netdev->name);
905 return 0;
907 out2:
908 usb_set_intfdata(intf, NULL);
909 free_skb_pool(dev);
910 out1:
911 free_all_urbs(dev);
912 out:
913 kfree(dev->intr_buff);
914 free_netdev(netdev);
915 return -EIO;
918 static void rtl8150_disconnect(struct usb_interface *intf)
920 rtl8150_t *dev = usb_get_intfdata(intf);
922 usb_set_intfdata(intf, NULL);
923 if (dev) {
924 set_bit(RTL8150_UNPLUG, &dev->flags);
925 tasklet_kill(&dev->tl);
926 unregister_netdev(dev->netdev);
927 unlink_all_urbs(dev);
928 free_all_urbs(dev);
929 free_skb_pool(dev);
930 if (dev->rx_skb)
931 dev_kfree_skb(dev->rx_skb);
932 kfree(dev->intr_buff);
933 free_netdev(dev->netdev);
937 static struct usb_driver rtl8150_driver = {
938 .name = driver_name,
939 .probe = rtl8150_probe,
940 .disconnect = rtl8150_disconnect,
941 .id_table = rtl8150_table,
942 .suspend = rtl8150_suspend,
943 .resume = rtl8150_resume,
944 .disable_hub_initiated_lpm = 1,
947 module_usb_driver(rtl8150_driver);
949 MODULE_AUTHOR(DRIVER_AUTHOR);
950 MODULE_DESCRIPTION(DRIVER_DESC);
951 MODULE_LICENSE("GPL");