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.
9 #include <linux/sched.h>
10 #include <linux/init.h>
11 #include <linux/signal.h>
12 #include <linux/slab.h>
13 #include <linux/module.h>
14 #include <linux/netdevice.h>
15 #include <linux/etherdevice.h>
16 #include <linux/mii.h>
17 #include <linux/ethtool.h>
18 #include <linux/usb.h>
19 #include <asm/uaccess.h>
21 /* Version Information */
22 #define DRIVER_VERSION "v0.6.2 (2004/08/27)"
23 #define DRIVER_AUTHOR "Petko Manolov <petkan@users.sourceforge.net>"
24 #define DRIVER_DESC "rtl8150 based usb-ethernet driver"
45 #define CSCR 0x014C /* This one has the link status */
46 #define CSCR_LINK_STATUS (1 << 3)
48 #define IDR_EEPROM 0x1202
51 #define PHY_WRITE 0x20
54 #define MII_TIMEOUT 10
57 #define RTL8150_REQT_READ 0xc0
58 #define RTL8150_REQT_WRITE 0x40
59 #define RTL8150_REQ_GET_REGS 0x05
60 #define RTL8150_REQ_SET_REGS 0x05
63 /* Transmit status register errors */
64 #define TSR_ECOL (1<<5)
65 #define TSR_LCOL (1<<4)
66 #define TSR_LOSS_CRS (1<<3)
67 #define TSR_JBR (1<<2)
68 #define TSR_ERRORS (TSR_ECOL | TSR_LCOL | TSR_LOSS_CRS | TSR_JBR)
69 /* Receive status register errors */
70 #define RSR_CRC (1<<2)
71 #define RSR_FAE (1<<1)
72 #define RSR_ERRORS (RSR_CRC | RSR_FAE)
74 /* Media status register definitions */
75 #define MSR_DUPLEX (1<<4)
76 #define MSR_SPEED (1<<3)
77 #define MSR_LINK (1<<2)
79 /* Interrupt pipe data */
83 #define INT_WAKSR 0x03
84 #define INT_TXOK_CNT 0x04
85 #define INT_RXLOST_CNT 0x05
86 #define INT_CRERR_CNT 0x06
87 #define INT_COL_CNT 0x07
89 /* Transmit status register errors */
90 #define TSR_ECOL (1<<5)
91 #define TSR_LCOL (1<<4)
92 #define TSR_LOSS_CRS (1<<3)
93 #define TSR_JBR (1<<2)
94 #define TSR_ERRORS (TSR_ECOL | TSR_LCOL | TSR_LOSS_CRS | TSR_JBR)
95 /* Receive status register errors */
96 #define RSR_CRC (1<<2)
97 #define RSR_FAE (1<<1)
98 #define RSR_ERRORS (RSR_CRC | RSR_FAE)
100 /* Media status register definitions */
101 #define MSR_DUPLEX (1<<4)
102 #define MSR_SPEED (1<<3)
103 #define MSR_LINK (1<<2)
105 /* Interrupt pipe data */
109 #define INT_WAKSR 0x03
110 #define INT_TXOK_CNT 0x04
111 #define INT_RXLOST_CNT 0x05
112 #define INT_CRERR_CNT 0x06
113 #define INT_COL_CNT 0x07
116 #define RTL8150_MTU 1540
117 #define RTL8150_TX_TIMEOUT (HZ)
118 #define RX_SKB_POOL_SIZE 4
121 #define RTL8150_HW_CRC 0
123 #define RTL8150_UNPLUG 2
124 #define RX_URB_FAIL 3
126 /* Define these values to match your device */
127 #define VENDOR_ID_REALTEK 0x0bda
128 #define VENDOR_ID_MELCO 0x0411
129 #define VENDOR_ID_MICRONET 0x3980
130 #define VENDOR_ID_LONGSHINE 0x07b8
131 #define VENDOR_ID_ZYXEL 0x0586
133 #define PRODUCT_ID_RTL8150 0x8150
134 #define PRODUCT_ID_LUAKTX 0x0012
135 #define PRODUCT_ID_LCS8138TX 0x401a
136 #define PRODUCT_ID_SP128AR 0x0003
137 #define PRODUCT_ID_PRESTIGE 0x401a
141 /* table of devices that work with this driver */
142 static struct usb_device_id rtl8150_table
[] = {
143 {USB_DEVICE(VENDOR_ID_REALTEK
, PRODUCT_ID_RTL8150
)},
144 {USB_DEVICE(VENDOR_ID_MELCO
, PRODUCT_ID_LUAKTX
)},
145 {USB_DEVICE(VENDOR_ID_MICRONET
, PRODUCT_ID_SP128AR
)},
146 {USB_DEVICE(VENDOR_ID_LONGSHINE
, PRODUCT_ID_LCS8138TX
)},
147 {USB_DEVICE(VENDOR_ID_ZYXEL
, PRODUCT_ID_PRESTIGE
)},
151 MODULE_DEVICE_TABLE(usb
, rtl8150_table
);
155 struct usb_device
*udev
;
156 struct tasklet_struct tl
;
157 struct net_device_stats stats
;
158 struct net_device
*netdev
;
159 struct urb
*rx_urb
, *tx_urb
, *intr_urb
, *ctrl_urb
;
160 struct sk_buff
*tx_skb
, *rx_skb
;
161 struct sk_buff
*rx_skb_pool
[RX_SKB_POOL_SIZE
];
162 spinlock_t rx_pool_lock
;
163 struct usb_ctrlrequest dr
;
170 typedef struct rtl8150 rtl8150_t
;
172 static void fill_skb_pool(rtl8150_t
*);
173 static void free_skb_pool(rtl8150_t
*);
174 static inline struct sk_buff
*pull_skb(rtl8150_t
*);
175 static void rtl8150_disconnect(struct usb_interface
*intf
);
176 static int rtl8150_probe(struct usb_interface
*intf
,
177 const struct usb_device_id
*id
);
178 static int rtl8150_suspend(struct usb_interface
*intf
, pm_message_t message
);
179 static int rtl8150_resume(struct usb_interface
*intf
);
181 static const char driver_name
[] = "rtl8150";
183 static struct usb_driver rtl8150_driver
= {
185 .probe
= rtl8150_probe
,
186 .disconnect
= rtl8150_disconnect
,
187 .id_table
= rtl8150_table
,
188 .suspend
= rtl8150_suspend
,
189 .resume
= rtl8150_resume
194 ** device related part of the code
197 static int get_registers(rtl8150_t
* dev
, u16 indx
, u16 size
, void *data
)
199 return usb_control_msg(dev
->udev
, usb_rcvctrlpipe(dev
->udev
, 0),
200 RTL8150_REQ_GET_REGS
, RTL8150_REQT_READ
,
201 indx
, 0, data
, size
, 500);
204 static int set_registers(rtl8150_t
* dev
, u16 indx
, u16 size
, void *data
)
206 return usb_control_msg(dev
->udev
, usb_sndctrlpipe(dev
->udev
, 0),
207 RTL8150_REQ_SET_REGS
, RTL8150_REQT_WRITE
,
208 indx
, 0, data
, size
, 500);
211 static void ctrl_callback(struct urb
*urb
, struct pt_regs
*regs
)
215 switch (urb
->status
) {
223 warn("ctrl urb status %d", urb
->status
);
226 clear_bit(RX_REG_SET
, &dev
->flags
);
229 static int async_set_registers(rtl8150_t
* dev
, u16 indx
, u16 size
)
233 if (test_bit(RX_REG_SET
, &dev
->flags
))
236 dev
->dr
.bRequestType
= RTL8150_REQT_WRITE
;
237 dev
->dr
.bRequest
= RTL8150_REQ_SET_REGS
;
238 dev
->dr
.wValue
= cpu_to_le16(indx
);
240 dev
->dr
.wLength
= cpu_to_le16(size
);
241 dev
->ctrl_urb
->transfer_buffer_length
= size
;
242 usb_fill_control_urb(dev
->ctrl_urb
, dev
->udev
,
243 usb_sndctrlpipe(dev
->udev
, 0), (char *) &dev
->dr
,
244 &dev
->rx_creg
, size
, ctrl_callback
, dev
);
245 if ((ret
= usb_submit_urb(dev
->ctrl_urb
, GFP_ATOMIC
))) {
247 netif_device_detach(dev
->netdev
);
248 err("control request submission failed: %d", ret
);
250 set_bit(RX_REG_SET
, &dev
->flags
);
255 static int read_mii_word(rtl8150_t
* dev
, u8 phy
, __u8 indx
, u16
* reg
)
261 data
[1] = data
[2] = 0;
262 tmp
= indx
| PHY_READ
| PHY_GO
;
265 set_registers(dev
, PHYADD
, sizeof(data
), data
);
266 set_registers(dev
, PHYCNT
, 1, &tmp
);
268 get_registers(dev
, PHYCNT
, 1, data
);
269 } while ((data
[0] & PHY_GO
) && (i
++ < MII_TIMEOUT
));
271 if (i
< MII_TIMEOUT
) {
272 get_registers(dev
, PHYDAT
, 2, data
);
273 *reg
= data
[0] | (data
[1] << 8);
279 static int write_mii_word(rtl8150_t
* dev
, u8 phy
, __u8 indx
, u16 reg
)
285 *(data
+ 1) = cpu_to_le16p(®
);
286 tmp
= indx
| PHY_WRITE
| PHY_GO
;
289 set_registers(dev
, PHYADD
, sizeof(data
), data
);
290 set_registers(dev
, PHYCNT
, 1, &tmp
);
292 get_registers(dev
, PHYCNT
, 1, data
);
293 } while ((data
[0] & PHY_GO
) && (i
++ < MII_TIMEOUT
));
301 static inline void set_ethernet_addr(rtl8150_t
* dev
)
305 get_registers(dev
, IDR
, sizeof(node_id
), node_id
);
306 memcpy(dev
->netdev
->dev_addr
, node_id
, sizeof(node_id
));
309 static int rtl8150_set_mac_address(struct net_device
*netdev
, void *p
)
311 struct sockaddr
*addr
= p
;
312 rtl8150_t
*dev
= netdev_priv(netdev
);
315 if (netif_running(netdev
))
318 memcpy(netdev
->dev_addr
, addr
->sa_data
, netdev
->addr_len
);
319 dbg("%s: Setting MAC address to ", netdev
->name
);
320 for (i
= 0; i
< 5; i
++)
321 dbg("%02X:", netdev
->dev_addr
[i
]);
322 dbg("%02X\n", netdev
->dev_addr
[i
]);
323 /* Set the IDR registers. */
324 set_registers(dev
, IDR
, sizeof(netdev
->dev_addr
), netdev
->dev_addr
);
328 /* Get the CR contents. */
329 get_registers(dev
, CR
, 1, &cr
);
330 /* Set the WEPROM bit (eeprom write enable). */
332 set_registers(dev
, CR
, 1, &cr
);
333 /* Write the MAC address into eeprom. Eeprom writes must be word-sized,
334 so we need to split them up. */
335 for (i
= 0; i
* 2 < netdev
->addr_len
; i
++) {
336 set_registers(dev
, IDR_EEPROM
+ (i
* 2), 2,
337 netdev
->dev_addr
+ (i
* 2));
339 /* Clear the WEPROM bit (preventing accidental eeprom writes). */
341 set_registers(dev
, CR
, 1, &cr
);
347 static int rtl8150_reset(rtl8150_t
* dev
)
352 set_registers(dev
, CR
, 1, &data
);
354 get_registers(dev
, CR
, 1, &data
);
355 } while ((data
& 0x10) && --i
);
357 return (i
> 0) ? 1 : 0;
360 static int alloc_all_urbs(rtl8150_t
* dev
)
362 dev
->rx_urb
= usb_alloc_urb(0, GFP_KERNEL
);
365 dev
->tx_urb
= usb_alloc_urb(0, GFP_KERNEL
);
367 usb_free_urb(dev
->rx_urb
);
370 dev
->intr_urb
= usb_alloc_urb(0, GFP_KERNEL
);
371 if (!dev
->intr_urb
) {
372 usb_free_urb(dev
->rx_urb
);
373 usb_free_urb(dev
->tx_urb
);
376 dev
->ctrl_urb
= usb_alloc_urb(0, GFP_KERNEL
);
377 if (!dev
->intr_urb
) {
378 usb_free_urb(dev
->rx_urb
);
379 usb_free_urb(dev
->tx_urb
);
380 usb_free_urb(dev
->intr_urb
);
387 static void free_all_urbs(rtl8150_t
* dev
)
389 usb_free_urb(dev
->rx_urb
);
390 usb_free_urb(dev
->tx_urb
);
391 usb_free_urb(dev
->intr_urb
);
392 usb_free_urb(dev
->ctrl_urb
);
395 static void unlink_all_urbs(rtl8150_t
* dev
)
397 usb_kill_urb(dev
->rx_urb
);
398 usb_kill_urb(dev
->tx_urb
);
399 usb_kill_urb(dev
->intr_urb
);
400 usb_kill_urb(dev
->ctrl_urb
);
403 static inline struct sk_buff
*pull_skb(rtl8150_t
*dev
)
408 for (i
= 0; i
< RX_SKB_POOL_SIZE
; i
++) {
409 if (dev
->rx_skb_pool
[i
]) {
410 skb
= dev
->rx_skb_pool
[i
];
411 dev
->rx_skb_pool
[i
] = NULL
;
418 static void read_bulk_callback(struct urb
*urb
, struct pt_regs
*regs
)
421 unsigned pkt_len
, res
;
423 struct net_device
*netdev
;
430 if (test_bit(RTL8150_UNPLUG
, &dev
->flags
))
432 netdev
= dev
->netdev
;
433 if (!netif_device_present(netdev
))
436 switch (urb
->status
) {
440 return; /* the urb is in unlink state */
442 warn("may be reset is needed?..");
445 warn("Rx status %d", urb
->status
);
451 /* protect against short packets (tell me why we got some?!?) */
452 if (urb
->actual_length
< 4)
455 res
= urb
->actual_length
;
456 rx_stat
= le16_to_cpu(*(__le16
*)(urb
->transfer_buffer
+ res
- 4));
459 skb_put(dev
->rx_skb
, pkt_len
);
460 dev
->rx_skb
->protocol
= eth_type_trans(dev
->rx_skb
, netdev
);
461 netif_rx(dev
->rx_skb
);
462 dev
->stats
.rx_packets
++;
463 dev
->stats
.rx_bytes
+= pkt_len
;
465 spin_lock(&dev
->rx_pool_lock
);
467 spin_unlock(&dev
->rx_pool_lock
);
473 usb_fill_bulk_urb(dev
->rx_urb
, dev
->udev
, usb_rcvbulkpipe(dev
->udev
, 1),
474 dev
->rx_skb
->data
, RTL8150_MTU
, read_bulk_callback
, dev
);
475 status
= usb_submit_urb(dev
->rx_urb
, GFP_ATOMIC
);
476 if (status
== -ENODEV
)
477 netif_device_detach(dev
->netdev
);
479 set_bit(RX_URB_FAIL
, &dev
->flags
);
482 clear_bit(RX_URB_FAIL
, &dev
->flags
);
487 tasklet_schedule(&dev
->tl
);
490 static void rx_fixup(unsigned long data
)
496 dev
= (rtl8150_t
*)data
;
498 spin_lock_irq(&dev
->rx_pool_lock
);
500 spin_unlock_irq(&dev
->rx_pool_lock
);
501 if (test_bit(RX_URB_FAIL
, &dev
->flags
))
504 spin_lock_irq(&dev
->rx_pool_lock
);
506 spin_unlock_irq(&dev
->rx_pool_lock
);
510 usb_fill_bulk_urb(dev
->rx_urb
, dev
->udev
, usb_rcvbulkpipe(dev
->udev
, 1),
511 dev
->rx_skb
->data
, RTL8150_MTU
, read_bulk_callback
, dev
);
513 status
= usb_submit_urb(dev
->rx_urb
, GFP_ATOMIC
);
514 if (status
== -ENODEV
) {
515 netif_device_detach(dev
->netdev
);
517 set_bit(RX_URB_FAIL
, &dev
->flags
);
520 clear_bit(RX_URB_FAIL
, &dev
->flags
);
525 tasklet_schedule(&dev
->tl
);
528 static void write_bulk_callback(struct urb
*urb
, struct pt_regs
*regs
)
535 dev_kfree_skb_irq(dev
->tx_skb
);
536 if (!netif_device_present(dev
->netdev
))
539 info("%s: Tx status %d", dev
->netdev
->name
, urb
->status
);
540 dev
->netdev
->trans_start
= jiffies
;
541 netif_wake_queue(dev
->netdev
);
544 static void intr_callback(struct urb
*urb
, struct pt_regs
*regs
)
553 switch (urb
->status
) {
554 case 0: /* success */
556 case -ECONNRESET
: /* unlink */
560 /* -EPIPE: should clear the halt */
562 info("%s: intr status %d", dev
->netdev
->name
, urb
->status
);
566 d
= urb
->transfer_buffer
;
567 if (d
[0] & TSR_ERRORS
) {
568 dev
->stats
.tx_errors
++;
569 if (d
[INT_TSR
] & (TSR_ECOL
| TSR_JBR
))
570 dev
->stats
.tx_aborted_errors
++;
571 if (d
[INT_TSR
] & TSR_LCOL
)
572 dev
->stats
.tx_window_errors
++;
573 if (d
[INT_TSR
] & TSR_LOSS_CRS
)
574 dev
->stats
.tx_carrier_errors
++;
576 /* Report link status changes to the network stack */
577 if ((d
[INT_MSR
] & MSR_LINK
) == 0) {
578 if (netif_carrier_ok(dev
->netdev
)) {
579 netif_carrier_off(dev
->netdev
);
580 dbg("%s: LINK LOST\n", __func__
);
583 if (!netif_carrier_ok(dev
->netdev
)) {
584 netif_carrier_on(dev
->netdev
);
585 dbg("%s: LINK CAME BACK\n", __func__
);
590 status
= usb_submit_urb (urb
, SLAB_ATOMIC
);
591 if (status
== -ENODEV
)
592 netif_device_detach(dev
->netdev
);
594 err ("can't resubmit intr, %s-%s/input0, status %d",
595 dev
->udev
->bus
->bus_name
,
596 dev
->udev
->devpath
, status
);
599 static int rtl8150_suspend(struct usb_interface
*intf
, pm_message_t message
)
601 rtl8150_t
*dev
= usb_get_intfdata(intf
);
603 netif_device_detach(dev
->netdev
);
605 if (netif_running(dev
->netdev
)) {
606 usb_kill_urb(dev
->rx_urb
);
607 usb_kill_urb(dev
->intr_urb
);
612 static int rtl8150_resume(struct usb_interface
*intf
)
614 rtl8150_t
*dev
= usb_get_intfdata(intf
);
616 netif_device_attach(dev
->netdev
);
617 if (netif_running(dev
->netdev
)) {
618 dev
->rx_urb
->status
= 0;
619 dev
->rx_urb
->actual_length
= 0;
620 read_bulk_callback(dev
->rx_urb
, NULL
);
622 dev
->intr_urb
->status
= 0;
623 dev
->intr_urb
->actual_length
= 0;
624 intr_callback(dev
->intr_urb
, NULL
);
631 ** network related part of the code
635 static void fill_skb_pool(rtl8150_t
*dev
)
640 for (i
= 0; i
< RX_SKB_POOL_SIZE
; i
++) {
641 if (dev
->rx_skb_pool
[i
])
643 skb
= dev_alloc_skb(RTL8150_MTU
+ 2);
647 skb
->dev
= dev
->netdev
;
649 dev
->rx_skb_pool
[i
] = skb
;
653 static void free_skb_pool(rtl8150_t
*dev
)
657 for (i
= 0; i
< RX_SKB_POOL_SIZE
; i
++)
658 if (dev
->rx_skb_pool
[i
])
659 dev_kfree_skb(dev
->rx_skb_pool
[i
]);
662 static int enable_net_traffic(rtl8150_t
* dev
)
664 u8 cr
, tcr
, rcr
, msr
;
666 if (!rtl8150_reset(dev
)) {
667 warn("%s - device reset failed", __FUNCTION__
);
669 /* RCR bit7=1 attach Rx info at the end; =0 HW CRC (which is broken) */
671 dev
->rx_creg
= cpu_to_le16(rcr
);
675 set_bit(RTL8150_HW_CRC
, &dev
->flags
);
676 set_registers(dev
, RCR
, 1, &rcr
);
677 set_registers(dev
, TCR
, 1, &tcr
);
678 set_registers(dev
, CR
, 1, &cr
);
679 get_registers(dev
, MSR
, 1, &msr
);
684 static void disable_net_traffic(rtl8150_t
* dev
)
688 get_registers(dev
, CR
, 1, &cr
);
690 set_registers(dev
, CR
, 1, &cr
);
693 static struct net_device_stats
*rtl8150_netdev_stats(struct net_device
*dev
)
695 return &((rtl8150_t
*)netdev_priv(dev
))->stats
;
698 static void rtl8150_tx_timeout(struct net_device
*netdev
)
700 rtl8150_t
*dev
= netdev_priv(netdev
);
701 warn("%s: Tx timeout.", netdev
->name
);
702 usb_unlink_urb(dev
->tx_urb
);
703 dev
->stats
.tx_errors
++;
706 static void rtl8150_set_multicast(struct net_device
*netdev
)
708 rtl8150_t
*dev
= netdev_priv(netdev
);
709 netif_stop_queue(netdev
);
710 if (netdev
->flags
& IFF_PROMISC
) {
711 dev
->rx_creg
|= cpu_to_le16(0x0001);
712 info("%s: promiscuous mode", netdev
->name
);
713 } else if (netdev
->mc_count
||
714 (netdev
->flags
& IFF_ALLMULTI
)) {
715 dev
->rx_creg
&= cpu_to_le16(0xfffe);
716 dev
->rx_creg
|= cpu_to_le16(0x0002);
717 info("%s: allmulti set", netdev
->name
);
719 /* ~RX_MULTICAST, ~RX_PROMISCUOUS */
720 dev
->rx_creg
&= cpu_to_le16(0x00fc);
722 async_set_registers(dev
, RCR
, 2);
723 netif_wake_queue(netdev
);
726 static int rtl8150_start_xmit(struct sk_buff
*skb
, struct net_device
*netdev
)
728 rtl8150_t
*dev
= netdev_priv(netdev
);
731 netif_stop_queue(netdev
);
732 count
= (skb
->len
< 60) ? 60 : skb
->len
;
733 count
= (count
& 0x3f) ? count
: count
+ 1;
735 usb_fill_bulk_urb(dev
->tx_urb
, dev
->udev
, usb_sndbulkpipe(dev
->udev
, 2),
736 skb
->data
, count
, write_bulk_callback
, dev
);
737 if ((res
= usb_submit_urb(dev
->tx_urb
, GFP_ATOMIC
))) {
738 /* Can we get/handle EPIPE here? */
740 netif_device_detach(dev
->netdev
);
742 warn("failed tx_urb %d\n", res
);
743 dev
->stats
.tx_errors
++;
744 netif_start_queue(netdev
);
747 dev
->stats
.tx_packets
++;
748 dev
->stats
.tx_bytes
+= skb
->len
;
749 netdev
->trans_start
= jiffies
;
756 static void set_carrier(struct net_device
*netdev
)
758 rtl8150_t
*dev
= netdev_priv(netdev
);
761 get_registers(dev
, CSCR
, 2, &tmp
);
762 if (tmp
& CSCR_LINK_STATUS
)
763 netif_carrier_on(netdev
);
765 netif_carrier_off(netdev
);
768 static int rtl8150_open(struct net_device
*netdev
)
770 rtl8150_t
*dev
= netdev_priv(netdev
);
773 if (dev
->rx_skb
== NULL
)
774 dev
->rx_skb
= pull_skb(dev
);
778 set_registers(dev
, IDR
, 6, netdev
->dev_addr
);
780 usb_fill_bulk_urb(dev
->rx_urb
, dev
->udev
, usb_rcvbulkpipe(dev
->udev
, 1),
781 dev
->rx_skb
->data
, RTL8150_MTU
, read_bulk_callback
, dev
);
782 if ((res
= usb_submit_urb(dev
->rx_urb
, GFP_KERNEL
))) {
784 netif_device_detach(dev
->netdev
);
785 warn("%s: rx_urb submit failed: %d", __FUNCTION__
, res
);
788 usb_fill_int_urb(dev
->intr_urb
, dev
->udev
, usb_rcvintpipe(dev
->udev
, 3),
789 dev
->intr_buff
, INTBUFSIZE
, intr_callback
,
790 dev
, dev
->intr_interval
);
791 if ((res
= usb_submit_urb(dev
->intr_urb
, GFP_KERNEL
))) {
793 netif_device_detach(dev
->netdev
);
794 warn("%s: intr_urb submit failed: %d", __FUNCTION__
, res
);
795 usb_kill_urb(dev
->rx_urb
);
798 enable_net_traffic(dev
);
800 netif_start_queue(netdev
);
805 static int rtl8150_close(struct net_device
*netdev
)
807 rtl8150_t
*dev
= netdev_priv(netdev
);
810 netif_stop_queue(netdev
);
811 if (!test_bit(RTL8150_UNPLUG
, &dev
->flags
))
812 disable_net_traffic(dev
);
813 unlink_all_urbs(dev
);
818 static void rtl8150_get_drvinfo(struct net_device
*netdev
, struct ethtool_drvinfo
*info
)
820 rtl8150_t
*dev
= netdev_priv(netdev
);
822 strncpy(info
->driver
, driver_name
, ETHTOOL_BUSINFO_LEN
);
823 strncpy(info
->version
, DRIVER_VERSION
, ETHTOOL_BUSINFO_LEN
);
824 usb_make_path(dev
->udev
, info
->bus_info
, sizeof info
->bus_info
);
827 static int rtl8150_get_settings(struct net_device
*netdev
, struct ethtool_cmd
*ecmd
)
829 rtl8150_t
*dev
= netdev_priv(netdev
);
832 ecmd
->supported
= (SUPPORTED_10baseT_Half
|
833 SUPPORTED_10baseT_Full
|
834 SUPPORTED_100baseT_Half
|
835 SUPPORTED_100baseT_Full
|
837 SUPPORTED_TP
| SUPPORTED_MII
);
838 ecmd
->port
= PORT_TP
;
839 ecmd
->transceiver
= XCVR_INTERNAL
;
840 ecmd
->phy_address
= dev
->phy
;
841 get_registers(dev
, BMCR
, 2, &bmcr
);
842 get_registers(dev
, ANLP
, 2, &lpa
);
843 if (bmcr
& BMCR_ANENABLE
) {
844 ecmd
->autoneg
= AUTONEG_ENABLE
;
845 ecmd
->speed
= (lpa
& (LPA_100HALF
| LPA_100FULL
)) ?
846 SPEED_100
: SPEED_10
;
847 if (ecmd
->speed
== SPEED_100
)
848 ecmd
->duplex
= (lpa
& LPA_100FULL
) ?
849 DUPLEX_FULL
: DUPLEX_HALF
;
851 ecmd
->duplex
= (lpa
& LPA_10FULL
) ?
852 DUPLEX_FULL
: DUPLEX_HALF
;
854 ecmd
->autoneg
= AUTONEG_DISABLE
;
855 ecmd
->speed
= (bmcr
& BMCR_SPEED100
) ?
856 SPEED_100
: SPEED_10
;
857 ecmd
->duplex
= (bmcr
& BMCR_FULLDPLX
) ?
858 DUPLEX_FULL
: DUPLEX_HALF
;
863 static struct ethtool_ops ops
= {
864 .get_drvinfo
= rtl8150_get_drvinfo
,
865 .get_settings
= rtl8150_get_settings
,
866 .get_link
= ethtool_op_get_link
869 static int rtl8150_ioctl(struct net_device
*netdev
, struct ifreq
*rq
, int cmd
)
871 rtl8150_t
*dev
= netdev_priv(netdev
);
872 u16
*data
= (u16
*) & rq
->ifr_ifru
;
878 case SIOCDEVPRIVATE
+ 1:
879 read_mii_word(dev
, dev
->phy
, (data
[1] & 0x1f), &data
[3]);
881 case SIOCDEVPRIVATE
+ 2:
882 if (!capable(CAP_NET_ADMIN
))
884 write_mii_word(dev
, dev
->phy
, (data
[1] & 0x1f), data
[2]);
893 static int rtl8150_probe(struct usb_interface
*intf
,
894 const struct usb_device_id
*id
)
896 struct usb_device
*udev
= interface_to_usbdev(intf
);
898 struct net_device
*netdev
;
900 netdev
= alloc_etherdev(sizeof(rtl8150_t
));
902 err("Out of memory");
906 dev
= netdev_priv(netdev
);
907 memset(dev
, 0, sizeof(rtl8150_t
));
909 dev
->intr_buff
= kmalloc(INTBUFSIZE
, GFP_KERNEL
);
910 if (!dev
->intr_buff
) {
915 tasklet_init(&dev
->tl
, rx_fixup
, (unsigned long)dev
);
916 spin_lock_init(&dev
->rx_pool_lock
);
919 dev
->netdev
= netdev
;
920 SET_MODULE_OWNER(netdev
);
921 netdev
->open
= rtl8150_open
;
922 netdev
->stop
= rtl8150_close
;
923 netdev
->do_ioctl
= rtl8150_ioctl
;
924 netdev
->watchdog_timeo
= RTL8150_TX_TIMEOUT
;
925 netdev
->tx_timeout
= rtl8150_tx_timeout
;
926 netdev
->hard_start_xmit
= rtl8150_start_xmit
;
927 netdev
->set_multicast_list
= rtl8150_set_multicast
;
928 netdev
->set_mac_address
= rtl8150_set_mac_address
;
929 netdev
->get_stats
= rtl8150_netdev_stats
;
930 netdev
->mtu
= RTL8150_MTU
;
931 SET_ETHTOOL_OPS(netdev
, &ops
);
932 dev
->intr_interval
= 100; /* 100ms */
934 if (!alloc_all_urbs(dev
)) {
935 err("out of memory");
938 if (!rtl8150_reset(dev
)) {
939 err("couldn't reset the device");
943 set_ethernet_addr(dev
);
945 usb_set_intfdata(intf
, dev
);
946 SET_NETDEV_DEV(netdev
, &intf
->dev
);
947 if (register_netdev(netdev
) != 0) {
948 err("couldn't register the device");
952 info("%s: rtl8150 is detected", netdev
->name
);
957 usb_set_intfdata(intf
, NULL
);
962 kfree(dev
->intr_buff
);
967 static void rtl8150_disconnect(struct usb_interface
*intf
)
969 rtl8150_t
*dev
= usb_get_intfdata(intf
);
971 usb_set_intfdata(intf
, NULL
);
973 set_bit(RTL8150_UNPLUG
, &dev
->flags
);
974 tasklet_disable(&dev
->tl
);
975 tasklet_kill(&dev
->tl
);
976 unregister_netdev(dev
->netdev
);
977 unlink_all_urbs(dev
);
981 dev_kfree_skb(dev
->rx_skb
);
982 kfree(dev
->intr_buff
);
983 free_netdev(dev
->netdev
);
987 static int __init
usb_rtl8150_init(void)
989 info(DRIVER_DESC
" " DRIVER_VERSION
);
990 return usb_register(&rtl8150_driver
);
993 static void __exit
usb_rtl8150_exit(void)
995 usb_deregister(&rtl8150_driver
);
998 module_init(usb_rtl8150_init
);
999 module_exit(usb_rtl8150_exit
);
1001 MODULE_AUTHOR(DRIVER_AUTHOR
);
1002 MODULE_DESCRIPTION(DRIVER_DESC
);
1003 MODULE_LICENSE("GPL");