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