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/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"
44 #define CSCR 0x014C /* This one has the link status */
45 #define CSCR_LINK_STATUS (1 << 3)
47 #define IDR_EEPROM 0x1202
50 #define PHY_WRITE 0x20
53 #define MII_TIMEOUT 10
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 */
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
88 /* Transmit status register errors */
89 #define TSR_ECOL (1<<5)
90 #define TSR_LCOL (1<<4)
91 #define TSR_LOSS_CRS (1<<3)
92 #define TSR_JBR (1<<2)
93 #define TSR_ERRORS (TSR_ECOL | TSR_LCOL | TSR_LOSS_CRS | TSR_JBR)
94 /* Receive status register errors */
95 #define RSR_CRC (1<<2)
96 #define RSR_FAE (1<<1)
97 #define RSR_ERRORS (RSR_CRC | RSR_FAE)
99 /* Media status register definitions */
100 #define MSR_DUPLEX (1<<4)
101 #define MSR_SPEED (1<<3)
102 #define MSR_LINK (1<<2)
104 /* Interrupt pipe data */
108 #define INT_WAKSR 0x03
109 #define INT_TXOK_CNT 0x04
110 #define INT_RXLOST_CNT 0x05
111 #define INT_CRERR_CNT 0x06
112 #define INT_COL_CNT 0x07
115 #define RTL8150_MTU 1540
116 #define RTL8150_TX_TIMEOUT (HZ)
117 #define RX_SKB_POOL_SIZE 4
120 #define RTL8150_HW_CRC 0
122 #define RTL8150_UNPLUG 2
123 #define RX_URB_FAIL 3
125 /* Define these values to match your device */
126 #define VENDOR_ID_REALTEK 0x0bda
127 #define VENDOR_ID_MELCO 0x0411
128 #define VENDOR_ID_MICRONET 0x3980
129 #define VENDOR_ID_LONGSHINE 0x07b8
130 #define VENDOR_ID_OQO 0x1557
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_OQO
, PRODUCT_ID_RTL8150
)},
148 {USB_DEVICE(VENDOR_ID_ZYXEL
, PRODUCT_ID_PRESTIGE
)},
152 MODULE_DEVICE_TABLE(usb
, rtl8150_table
);
156 struct usb_device
*udev
;
157 struct tasklet_struct tl
;
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
)
214 int status
= urb
->status
;
224 if (printk_ratelimit())
225 dev_warn(&urb
->dev
->dev
, "ctrl urb status %d\n", status
);
228 clear_bit(RX_REG_SET
, &dev
->flags
);
231 static int async_set_registers(rtl8150_t
* dev
, u16 indx
, u16 size
)
235 if (test_bit(RX_REG_SET
, &dev
->flags
))
238 dev
->dr
.bRequestType
= RTL8150_REQT_WRITE
;
239 dev
->dr
.bRequest
= RTL8150_REQ_SET_REGS
;
240 dev
->dr
.wValue
= cpu_to_le16(indx
);
242 dev
->dr
.wLength
= cpu_to_le16(size
);
243 dev
->ctrl_urb
->transfer_buffer_length
= size
;
244 usb_fill_control_urb(dev
->ctrl_urb
, dev
->udev
,
245 usb_sndctrlpipe(dev
->udev
, 0), (char *) &dev
->dr
,
246 &dev
->rx_creg
, size
, ctrl_callback
, dev
);
247 if ((ret
= usb_submit_urb(dev
->ctrl_urb
, GFP_ATOMIC
))) {
249 netif_device_detach(dev
->netdev
);
250 err("control request submission failed: %d", ret
);
252 set_bit(RX_REG_SET
, &dev
->flags
);
257 static int read_mii_word(rtl8150_t
* dev
, u8 phy
, __u8 indx
, u16
* reg
)
263 data
[1] = data
[2] = 0;
264 tmp
= indx
| PHY_READ
| PHY_GO
;
267 set_registers(dev
, PHYADD
, sizeof(data
), data
);
268 set_registers(dev
, PHYCNT
, 1, &tmp
);
270 get_registers(dev
, PHYCNT
, 1, data
);
271 } while ((data
[0] & PHY_GO
) && (i
++ < MII_TIMEOUT
));
273 if (i
<= MII_TIMEOUT
) {
274 get_registers(dev
, PHYDAT
, 2, data
);
275 *reg
= data
[0] | (data
[1] << 8);
281 static int write_mii_word(rtl8150_t
* dev
, u8 phy
, __u8 indx
, u16 reg
)
287 data
[1] = reg
& 0xff;
288 data
[2] = (reg
>> 8) & 0xff;
289 tmp
= indx
| PHY_WRITE
| PHY_GO
;
292 set_registers(dev
, PHYADD
, sizeof(data
), data
);
293 set_registers(dev
, PHYCNT
, 1, &tmp
);
295 get_registers(dev
, PHYCNT
, 1, data
);
296 } while ((data
[0] & PHY_GO
) && (i
++ < MII_TIMEOUT
));
298 if (i
<= MII_TIMEOUT
)
304 static inline void set_ethernet_addr(rtl8150_t
* dev
)
308 get_registers(dev
, IDR
, sizeof(node_id
), node_id
);
309 memcpy(dev
->netdev
->dev_addr
, node_id
, sizeof(node_id
));
312 static int rtl8150_set_mac_address(struct net_device
*netdev
, void *p
)
314 struct sockaddr
*addr
= p
;
315 rtl8150_t
*dev
= netdev_priv(netdev
);
317 if (netif_running(netdev
))
320 memcpy(netdev
->dev_addr
, addr
->sa_data
, netdev
->addr_len
);
321 dbg("%s: Setting MAC address to %pM\n", netdev
->name
, netdev
->dev_addr
);
322 /* Set the IDR registers. */
323 set_registers(dev
, IDR
, netdev
->addr_len
, 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
->ctrl_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
)
421 unsigned pkt_len
, res
;
423 struct net_device
*netdev
;
425 int status
= urb
->status
;
431 if (test_bit(RTL8150_UNPLUG
, &dev
->flags
))
433 netdev
= dev
->netdev
;
434 if (!netif_device_present(netdev
))
441 return; /* the urb is in unlink state */
443 if (printk_ratelimit())
444 dev_warn(&urb
->dev
->dev
, "may be reset is needed?..\n");
447 if (printk_ratelimit())
448 dev_warn(&urb
->dev
->dev
, "Rx status %d\n", status
);
454 /* protect against short packets (tell me why we got some?!?) */
455 if (urb
->actual_length
< 4)
458 res
= urb
->actual_length
;
459 rx_stat
= le16_to_cpu(*(__le16
*)(urb
->transfer_buffer
+ res
- 4));
462 skb_put(dev
->rx_skb
, pkt_len
);
463 dev
->rx_skb
->protocol
= eth_type_trans(dev
->rx_skb
, netdev
);
464 netif_rx(dev
->rx_skb
);
465 netdev
->stats
.rx_packets
++;
466 netdev
->stats
.rx_bytes
+= pkt_len
;
468 spin_lock(&dev
->rx_pool_lock
);
470 spin_unlock(&dev
->rx_pool_lock
);
476 usb_fill_bulk_urb(dev
->rx_urb
, dev
->udev
, usb_rcvbulkpipe(dev
->udev
, 1),
477 dev
->rx_skb
->data
, RTL8150_MTU
, read_bulk_callback
, dev
);
478 result
= usb_submit_urb(dev
->rx_urb
, GFP_ATOMIC
);
479 if (result
== -ENODEV
)
480 netif_device_detach(dev
->netdev
);
482 set_bit(RX_URB_FAIL
, &dev
->flags
);
485 clear_bit(RX_URB_FAIL
, &dev
->flags
);
490 tasklet_schedule(&dev
->tl
);
493 static void rx_fixup(unsigned long data
)
499 dev
= (rtl8150_t
*)data
;
501 spin_lock_irq(&dev
->rx_pool_lock
);
503 spin_unlock_irq(&dev
->rx_pool_lock
);
504 if (test_bit(RX_URB_FAIL
, &dev
->flags
))
507 spin_lock_irq(&dev
->rx_pool_lock
);
509 spin_unlock_irq(&dev
->rx_pool_lock
);
513 usb_fill_bulk_urb(dev
->rx_urb
, dev
->udev
, usb_rcvbulkpipe(dev
->udev
, 1),
514 dev
->rx_skb
->data
, RTL8150_MTU
, read_bulk_callback
, dev
);
516 status
= usb_submit_urb(dev
->rx_urb
, GFP_ATOMIC
);
517 if (status
== -ENODEV
) {
518 netif_device_detach(dev
->netdev
);
520 set_bit(RX_URB_FAIL
, &dev
->flags
);
523 clear_bit(RX_URB_FAIL
, &dev
->flags
);
528 tasklet_schedule(&dev
->tl
);
531 static void write_bulk_callback(struct urb
*urb
)
534 int status
= urb
->status
;
539 dev_kfree_skb_irq(dev
->tx_skb
);
540 if (!netif_device_present(dev
->netdev
))
543 dev_info(&urb
->dev
->dev
, "%s: Tx status %d\n",
544 dev
->netdev
->name
, status
);
545 dev
->netdev
->trans_start
= jiffies
;
546 netif_wake_queue(dev
->netdev
);
549 static void intr_callback(struct urb
*urb
)
553 int status
= urb
->status
;
560 case 0: /* success */
562 case -ECONNRESET
: /* unlink */
566 /* -EPIPE: should clear the halt */
568 dev_info(&urb
->dev
->dev
, "%s: intr status %d\n",
569 dev
->netdev
->name
, status
);
573 d
= urb
->transfer_buffer
;
574 if (d
[0] & TSR_ERRORS
) {
575 dev
->netdev
->stats
.tx_errors
++;
576 if (d
[INT_TSR
] & (TSR_ECOL
| TSR_JBR
))
577 dev
->netdev
->stats
.tx_aborted_errors
++;
578 if (d
[INT_TSR
] & TSR_LCOL
)
579 dev
->netdev
->stats
.tx_window_errors
++;
580 if (d
[INT_TSR
] & TSR_LOSS_CRS
)
581 dev
->netdev
->stats
.tx_carrier_errors
++;
583 /* Report link status changes to the network stack */
584 if ((d
[INT_MSR
] & MSR_LINK
) == 0) {
585 if (netif_carrier_ok(dev
->netdev
)) {
586 netif_carrier_off(dev
->netdev
);
587 dbg("%s: LINK LOST\n", __func__
);
590 if (!netif_carrier_ok(dev
->netdev
)) {
591 netif_carrier_on(dev
->netdev
);
592 dbg("%s: LINK CAME BACK\n", __func__
);
597 res
= usb_submit_urb (urb
, GFP_ATOMIC
);
599 netif_device_detach(dev
->netdev
);
601 err ("can't resubmit intr, %s-%s/input0, status %d",
602 dev
->udev
->bus
->bus_name
,
603 dev
->udev
->devpath
, res
);
606 static int rtl8150_suspend(struct usb_interface
*intf
, pm_message_t message
)
608 rtl8150_t
*dev
= usb_get_intfdata(intf
);
610 netif_device_detach(dev
->netdev
);
612 if (netif_running(dev
->netdev
)) {
613 usb_kill_urb(dev
->rx_urb
);
614 usb_kill_urb(dev
->intr_urb
);
619 static int rtl8150_resume(struct usb_interface
*intf
)
621 rtl8150_t
*dev
= usb_get_intfdata(intf
);
623 netif_device_attach(dev
->netdev
);
624 if (netif_running(dev
->netdev
)) {
625 dev
->rx_urb
->status
= 0;
626 dev
->rx_urb
->actual_length
= 0;
627 read_bulk_callback(dev
->rx_urb
);
629 dev
->intr_urb
->status
= 0;
630 dev
->intr_urb
->actual_length
= 0;
631 intr_callback(dev
->intr_urb
);
638 ** network related part of the code
642 static void fill_skb_pool(rtl8150_t
*dev
)
647 for (i
= 0; i
< RX_SKB_POOL_SIZE
; i
++) {
648 if (dev
->rx_skb_pool
[i
])
650 skb
= dev_alloc_skb(RTL8150_MTU
+ 2);
655 dev
->rx_skb_pool
[i
] = skb
;
659 static void free_skb_pool(rtl8150_t
*dev
)
663 for (i
= 0; i
< RX_SKB_POOL_SIZE
; i
++)
664 if (dev
->rx_skb_pool
[i
])
665 dev_kfree_skb(dev
->rx_skb_pool
[i
]);
668 static int enable_net_traffic(rtl8150_t
* dev
)
670 u8 cr
, tcr
, rcr
, msr
;
672 if (!rtl8150_reset(dev
)) {
673 dev_warn(&dev
->udev
->dev
, "device reset failed\n");
675 /* RCR bit7=1 attach Rx info at the end; =0 HW CRC (which is broken) */
677 dev
->rx_creg
= cpu_to_le16(rcr
);
681 set_bit(RTL8150_HW_CRC
, &dev
->flags
);
682 set_registers(dev
, RCR
, 1, &rcr
);
683 set_registers(dev
, TCR
, 1, &tcr
);
684 set_registers(dev
, CR
, 1, &cr
);
685 get_registers(dev
, MSR
, 1, &msr
);
690 static void disable_net_traffic(rtl8150_t
* dev
)
694 get_registers(dev
, CR
, 1, &cr
);
696 set_registers(dev
, CR
, 1, &cr
);
699 static void rtl8150_tx_timeout(struct net_device
*netdev
)
701 rtl8150_t
*dev
= netdev_priv(netdev
);
702 dev_warn(&netdev
->dev
, "Tx timeout.\n");
703 usb_unlink_urb(dev
->tx_urb
);
704 netdev
->stats
.tx_errors
++;
707 static void rtl8150_set_multicast(struct net_device
*netdev
)
709 rtl8150_t
*dev
= netdev_priv(netdev
);
710 netif_stop_queue(netdev
);
711 if (netdev
->flags
& IFF_PROMISC
) {
712 dev
->rx_creg
|= cpu_to_le16(0x0001);
713 dev_info(&netdev
->dev
, "%s: promiscuous mode\n", netdev
->name
);
714 } else if (!netdev_mc_empty(netdev
) ||
715 (netdev
->flags
& IFF_ALLMULTI
)) {
716 dev
->rx_creg
&= cpu_to_le16(0xfffe);
717 dev
->rx_creg
|= cpu_to_le16(0x0002);
718 dev_info(&netdev
->dev
, "%s: allmulti set\n", netdev
->name
);
720 /* ~RX_MULTICAST, ~RX_PROMISCUOUS */
721 dev
->rx_creg
&= cpu_to_le16(0x00fc);
723 async_set_registers(dev
, RCR
, 2);
724 netif_wake_queue(netdev
);
727 static netdev_tx_t
rtl8150_start_xmit(struct sk_buff
*skb
,
728 struct net_device
*netdev
)
730 rtl8150_t
*dev
= netdev_priv(netdev
);
733 netif_stop_queue(netdev
);
734 count
= (skb
->len
< 60) ? 60 : skb
->len
;
735 count
= (count
& 0x3f) ? count
: count
+ 1;
737 usb_fill_bulk_urb(dev
->tx_urb
, dev
->udev
, usb_sndbulkpipe(dev
->udev
, 2),
738 skb
->data
, count
, write_bulk_callback
, dev
);
739 if ((res
= usb_submit_urb(dev
->tx_urb
, GFP_ATOMIC
))) {
740 /* Can we get/handle EPIPE here? */
742 netif_device_detach(dev
->netdev
);
744 dev_warn(&netdev
->dev
, "failed tx_urb %d\n", res
);
745 netdev
->stats
.tx_errors
++;
746 netif_start_queue(netdev
);
749 netdev
->stats
.tx_packets
++;
750 netdev
->stats
.tx_bytes
+= skb
->len
;
751 netdev
->trans_start
= jiffies
;
758 static void set_carrier(struct net_device
*netdev
)
760 rtl8150_t
*dev
= netdev_priv(netdev
);
763 get_registers(dev
, CSCR
, 2, &tmp
);
764 if (tmp
& CSCR_LINK_STATUS
)
765 netif_carrier_on(netdev
);
767 netif_carrier_off(netdev
);
770 static int rtl8150_open(struct net_device
*netdev
)
772 rtl8150_t
*dev
= netdev_priv(netdev
);
775 if (dev
->rx_skb
== NULL
)
776 dev
->rx_skb
= pull_skb(dev
);
780 set_registers(dev
, IDR
, 6, netdev
->dev_addr
);
782 usb_fill_bulk_urb(dev
->rx_urb
, dev
->udev
, usb_rcvbulkpipe(dev
->udev
, 1),
783 dev
->rx_skb
->data
, RTL8150_MTU
, read_bulk_callback
, dev
);
784 if ((res
= usb_submit_urb(dev
->rx_urb
, GFP_KERNEL
))) {
786 netif_device_detach(dev
->netdev
);
787 dev_warn(&netdev
->dev
, "rx_urb submit failed: %d\n", res
);
790 usb_fill_int_urb(dev
->intr_urb
, dev
->udev
, usb_rcvintpipe(dev
->udev
, 3),
791 dev
->intr_buff
, INTBUFSIZE
, intr_callback
,
792 dev
, dev
->intr_interval
);
793 if ((res
= usb_submit_urb(dev
->intr_urb
, GFP_KERNEL
))) {
795 netif_device_detach(dev
->netdev
);
796 dev_warn(&netdev
->dev
, "intr_urb submit failed: %d\n", res
);
797 usb_kill_urb(dev
->rx_urb
);
800 enable_net_traffic(dev
);
802 netif_start_queue(netdev
);
807 static int rtl8150_close(struct net_device
*netdev
)
809 rtl8150_t
*dev
= netdev_priv(netdev
);
812 netif_stop_queue(netdev
);
813 if (!test_bit(RTL8150_UNPLUG
, &dev
->flags
))
814 disable_net_traffic(dev
);
815 unlink_all_urbs(dev
);
820 static void rtl8150_get_drvinfo(struct net_device
*netdev
, struct ethtool_drvinfo
*info
)
822 rtl8150_t
*dev
= netdev_priv(netdev
);
824 strncpy(info
->driver
, driver_name
, ETHTOOL_BUSINFO_LEN
);
825 strncpy(info
->version
, DRIVER_VERSION
, ETHTOOL_BUSINFO_LEN
);
826 usb_make_path(dev
->udev
, info
->bus_info
, sizeof info
->bus_info
);
829 static int rtl8150_get_settings(struct net_device
*netdev
, struct ethtool_cmd
*ecmd
)
831 rtl8150_t
*dev
= netdev_priv(netdev
);
834 ecmd
->supported
= (SUPPORTED_10baseT_Half
|
835 SUPPORTED_10baseT_Full
|
836 SUPPORTED_100baseT_Half
|
837 SUPPORTED_100baseT_Full
|
839 SUPPORTED_TP
| SUPPORTED_MII
);
840 ecmd
->port
= PORT_TP
;
841 ecmd
->transceiver
= XCVR_INTERNAL
;
842 ecmd
->phy_address
= dev
->phy
;
843 get_registers(dev
, BMCR
, 2, &bmcr
);
844 get_registers(dev
, ANLP
, 2, &lpa
);
845 if (bmcr
& BMCR_ANENABLE
) {
846 ecmd
->autoneg
= AUTONEG_ENABLE
;
847 ecmd
->speed
= (lpa
& (LPA_100HALF
| LPA_100FULL
)) ?
848 SPEED_100
: SPEED_10
;
849 if (ecmd
->speed
== SPEED_100
)
850 ecmd
->duplex
= (lpa
& LPA_100FULL
) ?
851 DUPLEX_FULL
: DUPLEX_HALF
;
853 ecmd
->duplex
= (lpa
& LPA_10FULL
) ?
854 DUPLEX_FULL
: DUPLEX_HALF
;
856 ecmd
->autoneg
= AUTONEG_DISABLE
;
857 ecmd
->speed
= (bmcr
& BMCR_SPEED100
) ?
858 SPEED_100
: SPEED_10
;
859 ecmd
->duplex
= (bmcr
& BMCR_FULLDPLX
) ?
860 DUPLEX_FULL
: DUPLEX_HALF
;
865 static const struct ethtool_ops ops
= {
866 .get_drvinfo
= rtl8150_get_drvinfo
,
867 .get_settings
= rtl8150_get_settings
,
868 .get_link
= ethtool_op_get_link
871 static int rtl8150_ioctl(struct net_device
*netdev
, struct ifreq
*rq
, int cmd
)
873 rtl8150_t
*dev
= netdev_priv(netdev
);
874 u16
*data
= (u16
*) & rq
->ifr_ifru
;
880 case SIOCDEVPRIVATE
+ 1:
881 read_mii_word(dev
, dev
->phy
, (data
[1] & 0x1f), &data
[3]);
883 case SIOCDEVPRIVATE
+ 2:
884 if (!capable(CAP_NET_ADMIN
))
886 write_mii_word(dev
, dev
->phy
, (data
[1] & 0x1f), data
[2]);
895 static const struct net_device_ops rtl8150_netdev_ops
= {
896 .ndo_open
= rtl8150_open
,
897 .ndo_stop
= rtl8150_close
,
898 .ndo_do_ioctl
= rtl8150_ioctl
,
899 .ndo_start_xmit
= rtl8150_start_xmit
,
900 .ndo_tx_timeout
= rtl8150_tx_timeout
,
901 .ndo_set_multicast_list
= rtl8150_set_multicast
,
902 .ndo_set_mac_address
= rtl8150_set_mac_address
,
904 .ndo_change_mtu
= eth_change_mtu
,
905 .ndo_validate_addr
= eth_validate_addr
,
908 static int rtl8150_probe(struct usb_interface
*intf
,
909 const struct usb_device_id
*id
)
911 struct usb_device
*udev
= interface_to_usbdev(intf
);
913 struct net_device
*netdev
;
915 netdev
= alloc_etherdev(sizeof(rtl8150_t
));
917 err("Out of memory");
921 dev
= netdev_priv(netdev
);
923 dev
->intr_buff
= kmalloc(INTBUFSIZE
, GFP_KERNEL
);
924 if (!dev
->intr_buff
) {
929 tasklet_init(&dev
->tl
, rx_fixup
, (unsigned long)dev
);
930 spin_lock_init(&dev
->rx_pool_lock
);
933 dev
->netdev
= netdev
;
934 netdev
->netdev_ops
= &rtl8150_netdev_ops
;
935 netdev
->watchdog_timeo
= RTL8150_TX_TIMEOUT
;
936 SET_ETHTOOL_OPS(netdev
, &ops
);
937 dev
->intr_interval
= 100; /* 100ms */
939 if (!alloc_all_urbs(dev
)) {
940 err("out of memory");
943 if (!rtl8150_reset(dev
)) {
944 err("couldn't reset the device");
948 set_ethernet_addr(dev
);
950 usb_set_intfdata(intf
, dev
);
951 SET_NETDEV_DEV(netdev
, &intf
->dev
);
952 if (register_netdev(netdev
) != 0) {
953 err("couldn't register the device");
957 dev_info(&intf
->dev
, "%s: rtl8150 is detected\n", netdev
->name
);
962 usb_set_intfdata(intf
, NULL
);
967 kfree(dev
->intr_buff
);
972 static void rtl8150_disconnect(struct usb_interface
*intf
)
974 rtl8150_t
*dev
= usb_get_intfdata(intf
);
976 usb_set_intfdata(intf
, NULL
);
978 set_bit(RTL8150_UNPLUG
, &dev
->flags
);
979 tasklet_disable(&dev
->tl
);
980 tasklet_kill(&dev
->tl
);
981 unregister_netdev(dev
->netdev
);
982 unlink_all_urbs(dev
);
986 dev_kfree_skb(dev
->rx_skb
);
987 kfree(dev
->intr_buff
);
988 free_netdev(dev
->netdev
);
992 static int __init
usb_rtl8150_init(void)
994 printk(KERN_INFO KBUILD_MODNAME
": " DRIVER_VERSION
":"
996 return usb_register(&rtl8150_driver
);
999 static void __exit
usb_rtl8150_exit(void)
1001 usb_deregister(&rtl8150_driver
);
1004 module_init(usb_rtl8150_init
);
1005 module_exit(usb_rtl8150_exit
);
1007 MODULE_AUTHOR(DRIVER_AUTHOR
);
1008 MODULE_DESCRIPTION(DRIVER_DESC
);
1009 MODULE_LICENSE("GPL");