Linux 3.4.102
[linux/fpc-iii.git] / drivers / net / usb / rtl8150.c
blobd363b31053da13e667dc9793026041353ef1c3bb
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, *ctrl_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 __le16 rx_creg;
140 u8 *intr_buff;
141 u8 phy;
144 typedef struct rtl8150 rtl8150_t;
146 static const char driver_name [] = "rtl8150";
150 ** device related part of the code
153 static int get_registers(rtl8150_t * dev, u16 indx, u16 size, void *data)
155 return usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
156 RTL8150_REQ_GET_REGS, RTL8150_REQT_READ,
157 indx, 0, data, size, 500);
160 static int set_registers(rtl8150_t * dev, u16 indx, u16 size, void *data)
162 return usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
163 RTL8150_REQ_SET_REGS, RTL8150_REQT_WRITE,
164 indx, 0, data, size, 500);
167 static void ctrl_callback(struct urb *urb)
169 rtl8150_t *dev;
170 int status = urb->status;
172 switch (status) {
173 case 0:
174 break;
175 case -EINPROGRESS:
176 break;
177 case -ENOENT:
178 break;
179 default:
180 if (printk_ratelimit())
181 dev_warn(&urb->dev->dev, "ctrl urb status %d\n", status);
183 dev = urb->context;
184 clear_bit(RX_REG_SET, &dev->flags);
187 static int async_set_registers(rtl8150_t * dev, u16 indx, u16 size)
189 int ret;
191 if (test_bit(RX_REG_SET, &dev->flags))
192 return -EAGAIN;
194 dev->dr.bRequestType = RTL8150_REQT_WRITE;
195 dev->dr.bRequest = RTL8150_REQ_SET_REGS;
196 dev->dr.wValue = cpu_to_le16(indx);
197 dev->dr.wIndex = 0;
198 dev->dr.wLength = cpu_to_le16(size);
199 dev->ctrl_urb->transfer_buffer_length = size;
200 usb_fill_control_urb(dev->ctrl_urb, dev->udev,
201 usb_sndctrlpipe(dev->udev, 0), (char *) &dev->dr,
202 &dev->rx_creg, size, ctrl_callback, dev);
203 if ((ret = usb_submit_urb(dev->ctrl_urb, GFP_ATOMIC))) {
204 if (ret == -ENODEV)
205 netif_device_detach(dev->netdev);
206 err("control request submission failed: %d", ret);
207 } else
208 set_bit(RX_REG_SET, &dev->flags);
210 return ret;
213 static int read_mii_word(rtl8150_t * dev, u8 phy, __u8 indx, u16 * reg)
215 int i;
216 u8 data[3], tmp;
218 data[0] = phy;
219 data[1] = data[2] = 0;
220 tmp = indx | PHY_READ | PHY_GO;
221 i = 0;
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));
229 if (i <= MII_TIMEOUT) {
230 get_registers(dev, PHYDAT, 2, data);
231 *reg = data[0] | (data[1] << 8);
232 return 0;
233 } else
234 return 1;
237 static int write_mii_word(rtl8150_t * dev, u8 phy, __u8 indx, u16 reg)
239 int i;
240 u8 data[3], tmp;
242 data[0] = phy;
243 data[1] = reg & 0xff;
244 data[2] = (reg >> 8) & 0xff;
245 tmp = indx | PHY_WRITE | PHY_GO;
246 i = 0;
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));
254 if (i <= MII_TIMEOUT)
255 return 0;
256 else
257 return 1;
260 static inline void set_ethernet_addr(rtl8150_t * dev)
262 u8 node_id[6];
264 get_registers(dev, IDR, sizeof(node_id), node_id);
265 memcpy(dev->netdev->dev_addr, node_id, sizeof(node_id));
268 static int rtl8150_set_mac_address(struct net_device *netdev, void *p)
270 struct sockaddr *addr = p;
271 rtl8150_t *dev = netdev_priv(netdev);
273 if (netif_running(netdev))
274 return -EBUSY;
276 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
277 dbg("%s: Setting MAC address to %pM\n", netdev->name, netdev->dev_addr);
278 /* Set the IDR registers. */
279 set_registers(dev, IDR, netdev->addr_len, netdev->dev_addr);
280 #ifdef EEPROM_WRITE
282 int i;
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++) {
292 set_registers(dev, IDR_EEPROM + (i * 2), 2,
293 netdev->dev_addr + (i * 2));
295 /* Clear the WEPROM bit (preventing accidental eeprom writes). */
296 cr &= 0xdf;
297 set_registers(dev, CR, 1, &cr);
299 #endif
300 return 0;
303 static int rtl8150_reset(rtl8150_t * dev)
305 u8 data = 0x10;
306 int i = HZ;
308 set_registers(dev, CR, 1, &data);
309 do {
310 get_registers(dev, CR, 1, &data);
311 } while ((data & 0x10) && --i);
313 return (i > 0) ? 1 : 0;
316 static int alloc_all_urbs(rtl8150_t * dev)
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;
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;
332 dev->ctrl_urb = usb_alloc_urb(0, GFP_KERNEL);
333 if (!dev->ctrl_urb) {
334 usb_free_urb(dev->rx_urb);
335 usb_free_urb(dev->tx_urb);
336 usb_free_urb(dev->intr_urb);
337 return 0;
340 return 1;
343 static void free_all_urbs(rtl8150_t * dev)
345 usb_free_urb(dev->rx_urb);
346 usb_free_urb(dev->tx_urb);
347 usb_free_urb(dev->intr_urb);
348 usb_free_urb(dev->ctrl_urb);
351 static void unlink_all_urbs(rtl8150_t * dev)
353 usb_kill_urb(dev->rx_urb);
354 usb_kill_urb(dev->tx_urb);
355 usb_kill_urb(dev->intr_urb);
356 usb_kill_urb(dev->ctrl_urb);
359 static inline struct sk_buff *pull_skb(rtl8150_t *dev)
361 struct sk_buff *skb;
362 int i;
364 for (i = 0; i < RX_SKB_POOL_SIZE; i++) {
365 if (dev->rx_skb_pool[i]) {
366 skb = dev->rx_skb_pool[i];
367 dev->rx_skb_pool[i] = NULL;
368 return skb;
371 return NULL;
374 static void read_bulk_callback(struct urb *urb)
376 rtl8150_t *dev;
377 unsigned pkt_len, res;
378 struct sk_buff *skb;
379 struct net_device *netdev;
380 u16 rx_stat;
381 int status = urb->status;
382 int result;
384 dev = urb->context;
385 if (!dev)
386 return;
387 if (test_bit(RTL8150_UNPLUG, &dev->flags))
388 return;
389 netdev = dev->netdev;
390 if (!netif_device_present(netdev))
391 return;
393 switch (status) {
394 case 0:
395 break;
396 case -ENOENT:
397 return; /* the urb is in unlink state */
398 case -ETIME:
399 if (printk_ratelimit())
400 dev_warn(&urb->dev->dev, "may be reset is needed?..\n");
401 goto goon;
402 default:
403 if (printk_ratelimit())
404 dev_warn(&urb->dev->dev, "Rx status %d\n", status);
405 goto goon;
408 if (!dev->rx_skb)
409 goto resched;
410 /* protect against short packets (tell me why we got some?!?) */
411 if (urb->actual_length < 4)
412 goto goon;
414 res = urb->actual_length;
415 rx_stat = le16_to_cpu(*(__le16 *)(urb->transfer_buffer + res - 4));
416 pkt_len = res - 4;
418 skb_put(dev->rx_skb, pkt_len);
419 dev->rx_skb->protocol = eth_type_trans(dev->rx_skb, netdev);
420 netif_rx(dev->rx_skb);
421 netdev->stats.rx_packets++;
422 netdev->stats.rx_bytes += pkt_len;
424 spin_lock(&dev->rx_pool_lock);
425 skb = pull_skb(dev);
426 spin_unlock(&dev->rx_pool_lock);
427 if (!skb)
428 goto resched;
430 dev->rx_skb = skb;
431 goon:
432 usb_fill_bulk_urb(dev->rx_urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
433 dev->rx_skb->data, RTL8150_MTU, read_bulk_callback, dev);
434 result = usb_submit_urb(dev->rx_urb, GFP_ATOMIC);
435 if (result == -ENODEV)
436 netif_device_detach(dev->netdev);
437 else if (result) {
438 set_bit(RX_URB_FAIL, &dev->flags);
439 goto resched;
440 } else {
441 clear_bit(RX_URB_FAIL, &dev->flags);
444 return;
445 resched:
446 tasklet_schedule(&dev->tl);
449 static void write_bulk_callback(struct urb *urb)
451 rtl8150_t *dev;
452 int status = urb->status;
454 dev = urb->context;
455 if (!dev)
456 return;
457 dev_kfree_skb_irq(dev->tx_skb);
458 if (!netif_device_present(dev->netdev))
459 return;
460 if (status)
461 dev_info(&urb->dev->dev, "%s: Tx status %d\n",
462 dev->netdev->name, status);
463 dev->netdev->trans_start = jiffies;
464 netif_wake_queue(dev->netdev);
467 static void intr_callback(struct urb *urb)
469 rtl8150_t *dev;
470 __u8 *d;
471 int status = urb->status;
472 int res;
474 dev = urb->context;
475 if (!dev)
476 return;
477 switch (status) {
478 case 0: /* success */
479 break;
480 case -ECONNRESET: /* unlink */
481 case -ENOENT:
482 case -ESHUTDOWN:
483 return;
484 /* -EPIPE: should clear the halt */
485 default:
486 dev_info(&urb->dev->dev, "%s: intr status %d\n",
487 dev->netdev->name, status);
488 goto resubmit;
491 d = urb->transfer_buffer;
492 if (d[0] & TSR_ERRORS) {
493 dev->netdev->stats.tx_errors++;
494 if (d[INT_TSR] & (TSR_ECOL | TSR_JBR))
495 dev->netdev->stats.tx_aborted_errors++;
496 if (d[INT_TSR] & TSR_LCOL)
497 dev->netdev->stats.tx_window_errors++;
498 if (d[INT_TSR] & TSR_LOSS_CRS)
499 dev->netdev->stats.tx_carrier_errors++;
501 /* Report link status changes to the network stack */
502 if ((d[INT_MSR] & MSR_LINK) == 0) {
503 if (netif_carrier_ok(dev->netdev)) {
504 netif_carrier_off(dev->netdev);
505 dbg("%s: LINK LOST\n", __func__);
507 } else {
508 if (!netif_carrier_ok(dev->netdev)) {
509 netif_carrier_on(dev->netdev);
510 dbg("%s: LINK CAME BACK\n", __func__);
514 resubmit:
515 res = usb_submit_urb (urb, GFP_ATOMIC);
516 if (res == -ENODEV)
517 netif_device_detach(dev->netdev);
518 else if (res)
519 err ("can't resubmit intr, %s-%s/input0, status %d",
520 dev->udev->bus->bus_name,
521 dev->udev->devpath, res);
524 static int rtl8150_suspend(struct usb_interface *intf, pm_message_t message)
526 rtl8150_t *dev = usb_get_intfdata(intf);
528 netif_device_detach(dev->netdev);
530 if (netif_running(dev->netdev)) {
531 usb_kill_urb(dev->rx_urb);
532 usb_kill_urb(dev->intr_urb);
534 return 0;
537 static int rtl8150_resume(struct usb_interface *intf)
539 rtl8150_t *dev = usb_get_intfdata(intf);
541 netif_device_attach(dev->netdev);
542 if (netif_running(dev->netdev)) {
543 dev->rx_urb->status = 0;
544 dev->rx_urb->actual_length = 0;
545 read_bulk_callback(dev->rx_urb);
547 dev->intr_urb->status = 0;
548 dev->intr_urb->actual_length = 0;
549 intr_callback(dev->intr_urb);
551 return 0;
556 ** network related part of the code
560 static void fill_skb_pool(rtl8150_t *dev)
562 struct sk_buff *skb;
563 int i;
565 for (i = 0; i < RX_SKB_POOL_SIZE; i++) {
566 if (dev->rx_skb_pool[i])
567 continue;
568 skb = dev_alloc_skb(RTL8150_MTU + 2);
569 if (!skb) {
570 return;
572 skb_reserve(skb, 2);
573 dev->rx_skb_pool[i] = skb;
577 static void free_skb_pool(rtl8150_t *dev)
579 int i;
581 for (i = 0; i < RX_SKB_POOL_SIZE; i++)
582 if (dev->rx_skb_pool[i])
583 dev_kfree_skb(dev->rx_skb_pool[i]);
586 static void rx_fixup(unsigned long data)
588 struct rtl8150 *dev = (struct rtl8150 *)data;
589 struct sk_buff *skb;
590 int status;
592 spin_lock_irq(&dev->rx_pool_lock);
593 fill_skb_pool(dev);
594 spin_unlock_irq(&dev->rx_pool_lock);
595 if (test_bit(RX_URB_FAIL, &dev->flags))
596 if (dev->rx_skb)
597 goto try_again;
598 spin_lock_irq(&dev->rx_pool_lock);
599 skb = pull_skb(dev);
600 spin_unlock_irq(&dev->rx_pool_lock);
601 if (skb == NULL)
602 goto tlsched;
603 dev->rx_skb = skb;
604 usb_fill_bulk_urb(dev->rx_urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
605 dev->rx_skb->data, RTL8150_MTU, read_bulk_callback, dev);
606 try_again:
607 status = usb_submit_urb(dev->rx_urb, GFP_ATOMIC);
608 if (status == -ENODEV) {
609 netif_device_detach(dev->netdev);
610 } else if (status) {
611 set_bit(RX_URB_FAIL, &dev->flags);
612 goto tlsched;
613 } else {
614 clear_bit(RX_URB_FAIL, &dev->flags);
617 return;
618 tlsched:
619 tasklet_schedule(&dev->tl);
622 static int enable_net_traffic(rtl8150_t * dev)
624 u8 cr, tcr, rcr, msr;
626 if (!rtl8150_reset(dev)) {
627 dev_warn(&dev->udev->dev, "device reset failed\n");
629 /* RCR bit7=1 attach Rx info at the end; =0 HW CRC (which is broken) */
630 rcr = 0x9e;
631 dev->rx_creg = cpu_to_le16(rcr);
632 tcr = 0xd8;
633 cr = 0x0c;
634 if (!(rcr & 0x80))
635 set_bit(RTL8150_HW_CRC, &dev->flags);
636 set_registers(dev, RCR, 1, &rcr);
637 set_registers(dev, TCR, 1, &tcr);
638 set_registers(dev, CR, 1, &cr);
639 get_registers(dev, MSR, 1, &msr);
641 return 0;
644 static void disable_net_traffic(rtl8150_t * dev)
646 u8 cr;
648 get_registers(dev, CR, 1, &cr);
649 cr &= 0xf3;
650 set_registers(dev, CR, 1, &cr);
653 static void rtl8150_tx_timeout(struct net_device *netdev)
655 rtl8150_t *dev = netdev_priv(netdev);
656 dev_warn(&netdev->dev, "Tx timeout.\n");
657 usb_unlink_urb(dev->tx_urb);
658 netdev->stats.tx_errors++;
661 static void rtl8150_set_multicast(struct net_device *netdev)
663 rtl8150_t *dev = netdev_priv(netdev);
664 netif_stop_queue(netdev);
665 if (netdev->flags & IFF_PROMISC) {
666 dev->rx_creg |= cpu_to_le16(0x0001);
667 dev_info(&netdev->dev, "%s: promiscuous mode\n", netdev->name);
668 } else if (!netdev_mc_empty(netdev) ||
669 (netdev->flags & IFF_ALLMULTI)) {
670 dev->rx_creg &= cpu_to_le16(0xfffe);
671 dev->rx_creg |= cpu_to_le16(0x0002);
672 dev_info(&netdev->dev, "%s: allmulti set\n", netdev->name);
673 } else {
674 /* ~RX_MULTICAST, ~RX_PROMISCUOUS */
675 dev->rx_creg &= cpu_to_le16(0x00fc);
677 async_set_registers(dev, RCR, 2);
678 netif_wake_queue(netdev);
681 static netdev_tx_t rtl8150_start_xmit(struct sk_buff *skb,
682 struct net_device *netdev)
684 rtl8150_t *dev = netdev_priv(netdev);
685 int count, res;
687 netif_stop_queue(netdev);
688 count = (skb->len < 60) ? 60 : skb->len;
689 count = (count & 0x3f) ? count : count + 1;
690 dev->tx_skb = skb;
691 usb_fill_bulk_urb(dev->tx_urb, dev->udev, usb_sndbulkpipe(dev->udev, 2),
692 skb->data, count, write_bulk_callback, dev);
693 if ((res = usb_submit_urb(dev->tx_urb, GFP_ATOMIC))) {
694 /* Can we get/handle EPIPE here? */
695 if (res == -ENODEV)
696 netif_device_detach(dev->netdev);
697 else {
698 dev_warn(&netdev->dev, "failed tx_urb %d\n", res);
699 netdev->stats.tx_errors++;
700 netif_start_queue(netdev);
702 } else {
703 netdev->stats.tx_packets++;
704 netdev->stats.tx_bytes += skb->len;
705 netdev->trans_start = jiffies;
708 return NETDEV_TX_OK;
712 static void set_carrier(struct net_device *netdev)
714 rtl8150_t *dev = netdev_priv(netdev);
715 short tmp;
717 get_registers(dev, CSCR, 2, &tmp);
718 if (tmp & CSCR_LINK_STATUS)
719 netif_carrier_on(netdev);
720 else
721 netif_carrier_off(netdev);
724 static int rtl8150_open(struct net_device *netdev)
726 rtl8150_t *dev = netdev_priv(netdev);
727 int res;
729 if (dev->rx_skb == NULL)
730 dev->rx_skb = pull_skb(dev);
731 if (!dev->rx_skb)
732 return -ENOMEM;
734 set_registers(dev, IDR, 6, netdev->dev_addr);
736 usb_fill_bulk_urb(dev->rx_urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
737 dev->rx_skb->data, RTL8150_MTU, read_bulk_callback, dev);
738 if ((res = usb_submit_urb(dev->rx_urb, GFP_KERNEL))) {
739 if (res == -ENODEV)
740 netif_device_detach(dev->netdev);
741 dev_warn(&netdev->dev, "rx_urb submit failed: %d\n", res);
742 return res;
744 usb_fill_int_urb(dev->intr_urb, dev->udev, usb_rcvintpipe(dev->udev, 3),
745 dev->intr_buff, INTBUFSIZE, intr_callback,
746 dev, dev->intr_interval);
747 if ((res = usb_submit_urb(dev->intr_urb, GFP_KERNEL))) {
748 if (res == -ENODEV)
749 netif_device_detach(dev->netdev);
750 dev_warn(&netdev->dev, "intr_urb submit failed: %d\n", res);
751 usb_kill_urb(dev->rx_urb);
752 return res;
754 enable_net_traffic(dev);
755 set_carrier(netdev);
756 netif_start_queue(netdev);
758 return res;
761 static int rtl8150_close(struct net_device *netdev)
763 rtl8150_t *dev = netdev_priv(netdev);
764 int res = 0;
766 netif_stop_queue(netdev);
767 if (!test_bit(RTL8150_UNPLUG, &dev->flags))
768 disable_net_traffic(dev);
769 unlink_all_urbs(dev);
771 return res;
774 static void rtl8150_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *info)
776 rtl8150_t *dev = netdev_priv(netdev);
778 strncpy(info->driver, driver_name, ETHTOOL_BUSINFO_LEN);
779 strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
780 usb_make_path(dev->udev, info->bus_info, sizeof info->bus_info);
783 static int rtl8150_get_settings(struct net_device *netdev, struct ethtool_cmd *ecmd)
785 rtl8150_t *dev = netdev_priv(netdev);
786 short lpa, bmcr;
788 ecmd->supported = (SUPPORTED_10baseT_Half |
789 SUPPORTED_10baseT_Full |
790 SUPPORTED_100baseT_Half |
791 SUPPORTED_100baseT_Full |
792 SUPPORTED_Autoneg |
793 SUPPORTED_TP | SUPPORTED_MII);
794 ecmd->port = PORT_TP;
795 ecmd->transceiver = XCVR_INTERNAL;
796 ecmd->phy_address = dev->phy;
797 get_registers(dev, BMCR, 2, &bmcr);
798 get_registers(dev, ANLP, 2, &lpa);
799 if (bmcr & BMCR_ANENABLE) {
800 u32 speed = ((lpa & (LPA_100HALF | LPA_100FULL)) ?
801 SPEED_100 : SPEED_10);
802 ethtool_cmd_speed_set(ecmd, speed);
803 ecmd->autoneg = AUTONEG_ENABLE;
804 if (speed == SPEED_100)
805 ecmd->duplex = (lpa & LPA_100FULL) ?
806 DUPLEX_FULL : DUPLEX_HALF;
807 else
808 ecmd->duplex = (lpa & LPA_10FULL) ?
809 DUPLEX_FULL : DUPLEX_HALF;
810 } else {
811 ecmd->autoneg = AUTONEG_DISABLE;
812 ethtool_cmd_speed_set(ecmd, ((bmcr & BMCR_SPEED100) ?
813 SPEED_100 : SPEED_10));
814 ecmd->duplex = (bmcr & BMCR_FULLDPLX) ?
815 DUPLEX_FULL : DUPLEX_HALF;
817 return 0;
820 static const struct ethtool_ops ops = {
821 .get_drvinfo = rtl8150_get_drvinfo,
822 .get_settings = rtl8150_get_settings,
823 .get_link = ethtool_op_get_link
826 static int rtl8150_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
828 rtl8150_t *dev = netdev_priv(netdev);
829 u16 *data = (u16 *) & rq->ifr_ifru;
830 int res = 0;
832 switch (cmd) {
833 case SIOCDEVPRIVATE:
834 data[0] = dev->phy;
835 case SIOCDEVPRIVATE + 1:
836 read_mii_word(dev, dev->phy, (data[1] & 0x1f), &data[3]);
837 break;
838 case SIOCDEVPRIVATE + 2:
839 if (!capable(CAP_NET_ADMIN))
840 return -EPERM;
841 write_mii_word(dev, dev->phy, (data[1] & 0x1f), data[2]);
842 break;
843 default:
844 res = -EOPNOTSUPP;
847 return res;
850 static const struct net_device_ops rtl8150_netdev_ops = {
851 .ndo_open = rtl8150_open,
852 .ndo_stop = rtl8150_close,
853 .ndo_do_ioctl = rtl8150_ioctl,
854 .ndo_start_xmit = rtl8150_start_xmit,
855 .ndo_tx_timeout = rtl8150_tx_timeout,
856 .ndo_set_rx_mode = rtl8150_set_multicast,
857 .ndo_set_mac_address = rtl8150_set_mac_address,
859 .ndo_change_mtu = eth_change_mtu,
860 .ndo_validate_addr = eth_validate_addr,
863 static int rtl8150_probe(struct usb_interface *intf,
864 const struct usb_device_id *id)
866 struct usb_device *udev = interface_to_usbdev(intf);
867 rtl8150_t *dev;
868 struct net_device *netdev;
870 netdev = alloc_etherdev(sizeof(rtl8150_t));
871 if (!netdev)
872 return -ENOMEM;
874 dev = netdev_priv(netdev);
876 dev->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL);
877 if (!dev->intr_buff) {
878 free_netdev(netdev);
879 return -ENOMEM;
882 tasklet_init(&dev->tl, rx_fixup, (unsigned long)dev);
883 spin_lock_init(&dev->rx_pool_lock);
885 dev->udev = udev;
886 dev->netdev = netdev;
887 netdev->netdev_ops = &rtl8150_netdev_ops;
888 netdev->watchdog_timeo = RTL8150_TX_TIMEOUT;
889 SET_ETHTOOL_OPS(netdev, &ops);
890 dev->intr_interval = 100; /* 100ms */
892 if (!alloc_all_urbs(dev)) {
893 err("out of memory");
894 goto out;
896 if (!rtl8150_reset(dev)) {
897 err("couldn't reset the device");
898 goto out1;
900 fill_skb_pool(dev);
901 set_ethernet_addr(dev);
903 usb_set_intfdata(intf, dev);
904 SET_NETDEV_DEV(netdev, &intf->dev);
905 if (register_netdev(netdev) != 0) {
906 err("couldn't register the device");
907 goto out2;
910 dev_info(&intf->dev, "%s: rtl8150 is detected\n", netdev->name);
912 return 0;
914 out2:
915 usb_set_intfdata(intf, NULL);
916 free_skb_pool(dev);
917 out1:
918 free_all_urbs(dev);
919 out:
920 kfree(dev->intr_buff);
921 free_netdev(netdev);
922 return -EIO;
925 static void rtl8150_disconnect(struct usb_interface *intf)
927 rtl8150_t *dev = usb_get_intfdata(intf);
929 usb_set_intfdata(intf, NULL);
930 if (dev) {
931 set_bit(RTL8150_UNPLUG, &dev->flags);
932 tasklet_kill(&dev->tl);
933 unregister_netdev(dev->netdev);
934 unlink_all_urbs(dev);
935 free_all_urbs(dev);
936 free_skb_pool(dev);
937 if (dev->rx_skb)
938 dev_kfree_skb(dev->rx_skb);
939 kfree(dev->intr_buff);
940 free_netdev(dev->netdev);
944 static struct usb_driver rtl8150_driver = {
945 .name = driver_name,
946 .probe = rtl8150_probe,
947 .disconnect = rtl8150_disconnect,
948 .id_table = rtl8150_table,
949 .suspend = rtl8150_suspend,
950 .resume = rtl8150_resume
953 module_usb_driver(rtl8150_driver);
955 MODULE_AUTHOR(DRIVER_AUTHOR);
956 MODULE_DESCRIPTION(DRIVER_DESC);
957 MODULE_LICENSE("GPL");