Merge master.kernel.org:/pub/scm/linux/kernel/git/davej/agpgart
[pandora-kernel.git] / 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 void fill_skb_pool(rtl8150_t *);
171 static void free_skb_pool(rtl8150_t *);
172 static inline struct sk_buff *pull_skb(rtl8150_t *);
173 static void rtl8150_disconnect(struct usb_interface *intf);
174 static int rtl8150_probe(struct usb_interface *intf,
175                            const struct usb_device_id *id);
176
177 static const char driver_name [] = "rtl8150";
178
179 static struct usb_driver rtl8150_driver = {
180         .owner =        THIS_MODULE,
181         .name =         driver_name,
182         .probe =        rtl8150_probe,
183         .disconnect =   rtl8150_disconnect,
184         .id_table =     rtl8150_table,
185 };
186
187 /*
188 **
189 **      device related part of the code
190 **
191 */
192 static int get_registers(rtl8150_t * dev, u16 indx, u16 size, void *data)
193 {
194         return usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
195                                RTL8150_REQ_GET_REGS, RTL8150_REQT_READ,
196                                indx, 0, data, size, 500);
197 }
198
199 static int set_registers(rtl8150_t * dev, u16 indx, u16 size, void *data)
200 {
201         return usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
202                                RTL8150_REQ_SET_REGS, RTL8150_REQT_WRITE,
203                                indx, 0, data, size, 500);
204 }
205
206 static void ctrl_callback(struct urb *urb, struct pt_regs *regs)
207 {
208         rtl8150_t *dev;
209
210         switch (urb->status) {
211         case 0:
212                 break;
213         case -EINPROGRESS:
214                 break;
215         case -ENOENT:
216                 break;
217         default:
218                 warn("ctrl urb status %d", urb->status);
219         }
220         dev = urb->context;
221         clear_bit(RX_REG_SET, &dev->flags);
222 }
223
224 static int async_set_registers(rtl8150_t * dev, u16 indx, u16 size)
225 {
226         int ret;
227
228         if (test_bit(RX_REG_SET, &dev->flags))
229                 return -EAGAIN;
230
231         dev->dr.bRequestType = RTL8150_REQT_WRITE;
232         dev->dr.bRequest = RTL8150_REQ_SET_REGS;
233         dev->dr.wValue = cpu_to_le16(indx);
234         dev->dr.wIndex = 0;
235         dev->dr.wLength = cpu_to_le16(size);
236         dev->ctrl_urb->transfer_buffer_length = size;
237         usb_fill_control_urb(dev->ctrl_urb, dev->udev,
238                          usb_sndctrlpipe(dev->udev, 0), (char *) &dev->dr,
239                          &dev->rx_creg, size, ctrl_callback, dev);
240         if ((ret = usb_submit_urb(dev->ctrl_urb, GFP_ATOMIC)))
241                 err("control request submission failed: %d", ret);
242         else
243                 set_bit(RX_REG_SET, &dev->flags);
244
245         return ret;
246 }
247
248 static int read_mii_word(rtl8150_t * dev, u8 phy, __u8 indx, u16 * reg)
249 {
250         int i;
251         u8 data[3], tmp;
252
253         data[0] = phy;
254         data[1] = data[2] = 0;
255         tmp = indx | PHY_READ | PHY_GO;
256         i = 0;
257
258         set_registers(dev, PHYADD, sizeof(data), data);
259         set_registers(dev, PHYCNT, 1, &tmp);
260         do {
261                 get_registers(dev, PHYCNT, 1, data);
262         } while ((data[0] & PHY_GO) && (i++ < MII_TIMEOUT));
263
264         if (i < MII_TIMEOUT) {
265                 get_registers(dev, PHYDAT, 2, data);
266                 *reg = data[0] | (data[1] << 8);
267                 return 0;
268         } else
269                 return 1;
270 }
271
272 static int write_mii_word(rtl8150_t * dev, u8 phy, __u8 indx, u16 reg)
273 {
274         int i;
275         u8 data[3], tmp;
276
277         data[0] = phy;
278         *(data + 1) = cpu_to_le16p(&reg);
279         tmp = indx | PHY_WRITE | PHY_GO;
280         i = 0;
281
282         set_registers(dev, PHYADD, sizeof(data), data);
283         set_registers(dev, PHYCNT, 1, &tmp);
284         do {
285                 get_registers(dev, PHYCNT, 1, data);
286         } while ((data[0] & PHY_GO) && (i++ < MII_TIMEOUT));
287
288         if (i < MII_TIMEOUT)
289                 return 0;
290         else
291                 return 1;
292 }
293
294 static inline void set_ethernet_addr(rtl8150_t * dev)
295 {
296         u8 node_id[6];
297
298         get_registers(dev, IDR, sizeof(node_id), node_id);
299         memcpy(dev->netdev->dev_addr, node_id, sizeof(node_id));
300 }
301
302 static int rtl8150_set_mac_address(struct net_device *netdev, void *p)
303 {
304         struct sockaddr *addr = p;
305         rtl8150_t *dev = netdev_priv(netdev);
306         int i;
307
308         if (netif_running(netdev))
309                 return -EBUSY;
310
311         memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
312         dbg("%s: Setting MAC address to ", netdev->name);
313         for (i = 0; i < 5; i++)
314                 dbg("%02X:", netdev->dev_addr[i]);
315         dbg("%02X\n", netdev->dev_addr[i]);
316         /* Set the IDR registers. */
317         set_registers(dev, IDR, sizeof(netdev->dev_addr), netdev->dev_addr);
318 #ifdef EEPROM_WRITE
319         {
320         u8 cr;
321         /* Get the CR contents. */
322         get_registers(dev, CR, 1, &cr);
323         /* Set the WEPROM bit (eeprom write enable). */
324         cr |= 0x20;
325         set_registers(dev, CR, 1, &cr);
326         /* Write the MAC address into eeprom. Eeprom writes must be word-sized,
327            so we need to split them up. */
328         for (i = 0; i * 2 < netdev->addr_len; i++) {
329                 set_registers(dev, IDR_EEPROM + (i * 2), 2, 
330                 netdev->dev_addr + (i * 2));
331         }
332         /* Clear the WEPROM bit (preventing accidental eeprom writes). */
333         cr &= 0xdf;
334         set_registers(dev, CR, 1, &cr);
335         }
336 #endif
337         return 0;
338 }
339
340 static int rtl8150_reset(rtl8150_t * dev)
341 {
342         u8 data = 0x10;
343         int i = HZ;
344
345         set_registers(dev, CR, 1, &data);
346         do {
347                 get_registers(dev, CR, 1, &data);
348         } while ((data & 0x10) && --i);
349
350         return (i > 0) ? 1 : 0;
351 }
352
353 static int alloc_all_urbs(rtl8150_t * dev)
354 {
355         dev->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
356         if (!dev->rx_urb)
357                 return 0;
358         dev->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
359         if (!dev->tx_urb) {
360                 usb_free_urb(dev->rx_urb);
361                 return 0;
362         }
363         dev->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
364         if (!dev->intr_urb) {
365                 usb_free_urb(dev->rx_urb);
366                 usb_free_urb(dev->tx_urb);
367                 return 0;
368         }
369         dev->ctrl_urb = usb_alloc_urb(0, GFP_KERNEL);
370         if (!dev->intr_urb) {
371                 usb_free_urb(dev->rx_urb);
372                 usb_free_urb(dev->tx_urb);
373                 usb_free_urb(dev->intr_urb);
374                 return 0;
375         }
376
377         return 1;
378 }
379
380 static void free_all_urbs(rtl8150_t * dev)
381 {
382         usb_free_urb(dev->rx_urb);
383         usb_free_urb(dev->tx_urb);
384         usb_free_urb(dev->intr_urb);
385         usb_free_urb(dev->ctrl_urb);
386 }
387
388 static void unlink_all_urbs(rtl8150_t * dev)
389 {
390         usb_kill_urb(dev->rx_urb);
391         usb_kill_urb(dev->tx_urb);
392         usb_kill_urb(dev->intr_urb);
393         usb_kill_urb(dev->ctrl_urb);
394 }
395
396 static inline struct sk_buff *pull_skb(rtl8150_t *dev)
397 {
398         struct sk_buff *skb;
399         int i;
400
401         for (i = 0; i < RX_SKB_POOL_SIZE; i++) {
402                 if (dev->rx_skb_pool[i]) {
403                         skb = dev->rx_skb_pool[i];
404                         dev->rx_skb_pool[i] = NULL;
405                         return skb;
406                 }
407         }
408         return NULL;
409 }
410
411 static void read_bulk_callback(struct urb *urb, struct pt_regs *regs)
412 {
413         rtl8150_t *dev;
414         unsigned pkt_len, res;
415         struct sk_buff *skb;
416         struct net_device *netdev;
417         u16 rx_stat;
418
419         dev = urb->context;
420         if (!dev)
421                 return;
422         if (test_bit(RTL8150_UNPLUG, &dev->flags))
423                 return;
424         netdev = dev->netdev;
425         if (!netif_device_present(netdev))
426                 return;
427
428         switch (urb->status) {
429         case 0:
430                 break;
431         case -ENOENT:
432                 return; /* the urb is in unlink state */
433         case -ETIMEDOUT:
434                 warn("may be reset is needed?..");
435                 goto goon;
436         default:
437                 warn("Rx status %d", urb->status);
438                 goto goon;
439         }
440
441         if (!dev->rx_skb)
442                 goto resched;
443         /* protect against short packets (tell me why we got some?!?) */
444         if (urb->actual_length < 4)
445                 goto goon;
446
447         res = urb->actual_length;
448         rx_stat = le16_to_cpu(*(__le16 *)(urb->transfer_buffer + res - 4));
449         pkt_len = res - 4;
450
451         skb_put(dev->rx_skb, pkt_len);
452         dev->rx_skb->protocol = eth_type_trans(dev->rx_skb, netdev);
453         netif_rx(dev->rx_skb);
454         dev->stats.rx_packets++;
455         dev->stats.rx_bytes += pkt_len;
456
457         spin_lock(&dev->rx_pool_lock);
458         skb = pull_skb(dev);
459         spin_unlock(&dev->rx_pool_lock);
460         if (!skb)
461                 goto resched;
462
463         dev->rx_skb = skb;
464 goon:
465         usb_fill_bulk_urb(dev->rx_urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
466                       dev->rx_skb->data, RTL8150_MTU, read_bulk_callback, dev);
467         if (usb_submit_urb(dev->rx_urb, GFP_ATOMIC)) {
468                 set_bit(RX_URB_FAIL, &dev->flags);
469                 goto resched;
470         } else {
471                 clear_bit(RX_URB_FAIL, &dev->flags);
472         }
473
474         return;
475 resched:
476         tasklet_schedule(&dev->tl);
477 }
478
479 static void rx_fixup(unsigned long data)
480 {
481         rtl8150_t *dev;
482         struct sk_buff *skb;
483
484         dev = (rtl8150_t *)data;
485
486         spin_lock_irq(&dev->rx_pool_lock);
487         fill_skb_pool(dev);
488         spin_unlock_irq(&dev->rx_pool_lock);
489         if (test_bit(RX_URB_FAIL, &dev->flags))
490                 if (dev->rx_skb)
491                         goto try_again;
492         spin_lock_irq(&dev->rx_pool_lock);
493         skb = pull_skb(dev);
494         spin_unlock_irq(&dev->rx_pool_lock);
495         if (skb == NULL)
496                 goto tlsched;
497         dev->rx_skb = skb;
498         usb_fill_bulk_urb(dev->rx_urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
499                       dev->rx_skb->data, RTL8150_MTU, read_bulk_callback, dev);
500 try_again:
501         if (usb_submit_urb(dev->rx_urb, GFP_ATOMIC)) {
502                 set_bit(RX_URB_FAIL, &dev->flags);
503                 goto tlsched;
504          } else {
505                 clear_bit(RX_URB_FAIL, &dev->flags);
506         }
507
508         return;
509 tlsched:
510         tasklet_schedule(&dev->tl);
511 }
512
513 static void write_bulk_callback(struct urb *urb, struct pt_regs *regs)
514 {
515         rtl8150_t *dev;
516
517         dev = urb->context;
518         if (!dev)
519                 return;
520         dev_kfree_skb_irq(dev->tx_skb);
521         if (!netif_device_present(dev->netdev))
522                 return;
523         if (urb->status)
524                 info("%s: Tx status %d", dev->netdev->name, urb->status);
525         dev->netdev->trans_start = jiffies;
526         netif_wake_queue(dev->netdev);
527 }
528
529 static void intr_callback(struct urb *urb, struct pt_regs *regs)
530 {
531         rtl8150_t *dev;
532         __u8 *d;
533         int status;
534
535         dev = urb->context;
536         if (!dev)
537                 return;
538         switch (urb->status) {
539         case 0:                 /* success */
540                 break;
541         case -ECONNRESET:       /* unlink */
542         case -ENOENT:
543         case -ESHUTDOWN:
544                 return;
545         /* -EPIPE:  should clear the halt */
546         default:
547                 info("%s: intr status %d", dev->netdev->name, urb->status);
548                 goto resubmit;
549         }
550
551         d = urb->transfer_buffer;
552         if (d[0] & TSR_ERRORS) {
553                 dev->stats.tx_errors++;
554                 if (d[INT_TSR] & (TSR_ECOL | TSR_JBR))
555                         dev->stats.tx_aborted_errors++;
556                 if (d[INT_TSR] & TSR_LCOL)
557                         dev->stats.tx_window_errors++;
558                 if (d[INT_TSR] & TSR_LOSS_CRS)
559                         dev->stats.tx_carrier_errors++;
560         }
561         /* Report link status changes to the network stack */
562         if ((d[INT_MSR] & MSR_LINK) == 0) {
563                 if (netif_carrier_ok(dev->netdev)) {
564                         netif_carrier_off(dev->netdev);
565                         dbg("%s: LINK LOST\n", __func__);
566                 }
567         } else {
568                 if (!netif_carrier_ok(dev->netdev)) {
569                         netif_carrier_on(dev->netdev);
570                         dbg("%s: LINK CAME BACK\n", __func__);
571                 }
572         }
573
574 resubmit:
575         status = usb_submit_urb (urb, SLAB_ATOMIC);
576         if (status)
577                 err ("can't resubmit intr, %s-%s/input0, status %d",
578                                 dev->udev->bus->bus_name,
579                                 dev->udev->devpath, status);
580 }
581
582
583 /*
584 **
585 **      network related part of the code
586 **
587 */
588
589 static void fill_skb_pool(rtl8150_t *dev)
590 {
591         struct sk_buff *skb;
592         int i;
593
594         for (i = 0; i < RX_SKB_POOL_SIZE; i++) {
595                 if (dev->rx_skb_pool[i])
596                         continue;
597                 skb = dev_alloc_skb(RTL8150_MTU + 2);
598                 if (!skb) {
599                         return;
600                 }
601                 skb->dev = dev->netdev;
602                 skb_reserve(skb, 2);
603                 dev->rx_skb_pool[i] = skb;
604         }
605 }
606
607 static void free_skb_pool(rtl8150_t *dev)
608 {
609         int i;
610
611         for (i = 0; i < RX_SKB_POOL_SIZE; i++)
612                 if (dev->rx_skb_pool[i])
613                         dev_kfree_skb(dev->rx_skb_pool[i]);
614 }
615
616 static int enable_net_traffic(rtl8150_t * dev)
617 {
618         u8 cr, tcr, rcr, msr;
619
620         if (!rtl8150_reset(dev)) {
621                 warn("%s - device reset failed", __FUNCTION__);
622         }
623         /* RCR bit7=1 attach Rx info at the end;  =0 HW CRC (which is broken) */
624         rcr = 0x9e;
625         dev->rx_creg = cpu_to_le16(rcr);
626         tcr = 0xd8;
627         cr = 0x0c;
628         if (!(rcr & 0x80))
629                 set_bit(RTL8150_HW_CRC, &dev->flags);
630         set_registers(dev, RCR, 1, &rcr);
631         set_registers(dev, TCR, 1, &tcr);
632         set_registers(dev, CR, 1, &cr);
633         get_registers(dev, MSR, 1, &msr);
634
635         return 0;
636 }
637
638 static void disable_net_traffic(rtl8150_t * dev)
639 {
640         u8 cr;
641
642         get_registers(dev, CR, 1, &cr);
643         cr &= 0xf3;
644         set_registers(dev, CR, 1, &cr);
645 }
646
647 static struct net_device_stats *rtl8150_netdev_stats(struct net_device *dev)
648 {
649         return &((rtl8150_t *)netdev_priv(dev))->stats;
650 }
651
652 static void rtl8150_tx_timeout(struct net_device *netdev)
653 {
654         rtl8150_t *dev = netdev_priv(netdev);
655         warn("%s: Tx timeout.", netdev->name);
656         usb_unlink_urb(dev->tx_urb);
657         dev->stats.tx_errors++;
658 }
659
660 static void rtl8150_set_multicast(struct net_device *netdev)
661 {
662         rtl8150_t *dev = netdev_priv(netdev);
663         netif_stop_queue(netdev);
664         if (netdev->flags & IFF_PROMISC) {
665                 dev->rx_creg |= cpu_to_le16(0x0001);
666                 info("%s: promiscuous mode", netdev->name);
667         } else if (netdev->mc_count ||
668                    (netdev->flags & IFF_ALLMULTI)) {
669                 dev->rx_creg &= cpu_to_le16(0xfffe);
670                 dev->rx_creg |= cpu_to_le16(0x0002);
671                 info("%s: allmulti set", netdev->name);
672         } else {
673                 /* ~RX_MULTICAST, ~RX_PROMISCUOUS */
674                 dev->rx_creg &= cpu_to_le16(0x00fc);
675         }
676         async_set_registers(dev, RCR, 2);
677         netif_wake_queue(netdev);
678 }
679
680 static int rtl8150_start_xmit(struct sk_buff *skb, struct net_device *netdev)
681 {
682         rtl8150_t *dev = netdev_priv(netdev);
683         int count, res;
684
685         netif_stop_queue(netdev);
686         count = (skb->len < 60) ? 60 : skb->len;
687         count = (count & 0x3f) ? count : count + 1;
688         dev->tx_skb = skb;
689         usb_fill_bulk_urb(dev->tx_urb, dev->udev, usb_sndbulkpipe(dev->udev, 2),
690                       skb->data, count, write_bulk_callback, dev);
691         if ((res = usb_submit_urb(dev->tx_urb, GFP_ATOMIC))) {
692                 warn("failed tx_urb %d\n", res);
693                 dev->stats.tx_errors++;
694                 netif_start_queue(netdev);
695         } else {
696                 dev->stats.tx_packets++;
697                 dev->stats.tx_bytes += skb->len;
698                 netdev->trans_start = jiffies;
699         }
700
701         return 0;
702 }
703
704
705 static void set_carrier(struct net_device *netdev)
706 {
707         rtl8150_t *dev = netdev_priv(netdev);
708         short tmp;
709
710         get_registers(dev, CSCR, 2, &tmp);
711         if (tmp & CSCR_LINK_STATUS)
712                 netif_carrier_on(netdev);
713         else
714                 netif_carrier_off(netdev);
715 }
716
717 static int rtl8150_open(struct net_device *netdev)
718 {
719         rtl8150_t *dev = netdev_priv(netdev);
720         int res;
721
722         if (dev->rx_skb == NULL)
723                 dev->rx_skb = pull_skb(dev);
724         if (!dev->rx_skb)
725                 return -ENOMEM;
726
727         set_registers(dev, IDR, 6, netdev->dev_addr);
728         
729         usb_fill_bulk_urb(dev->rx_urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
730                       dev->rx_skb->data, RTL8150_MTU, read_bulk_callback, dev);
731         if ((res = usb_submit_urb(dev->rx_urb, GFP_KERNEL)))
732                 warn("%s: rx_urb submit failed: %d", __FUNCTION__, res);
733         usb_fill_int_urb(dev->intr_urb, dev->udev, usb_rcvintpipe(dev->udev, 3),
734                      dev->intr_buff, INTBUFSIZE, intr_callback,
735                      dev, dev->intr_interval);
736         if ((res = usb_submit_urb(dev->intr_urb, GFP_KERNEL)))
737                 warn("%s: intr_urb submit failed: %d", __FUNCTION__, res);
738         netif_start_queue(netdev);
739         enable_net_traffic(dev);
740         set_carrier(netdev);
741
742         return res;
743 }
744
745 static int rtl8150_close(struct net_device *netdev)
746 {
747         rtl8150_t *dev = netdev_priv(netdev);
748         int res = 0;
749
750         netif_stop_queue(netdev);
751         if (!test_bit(RTL8150_UNPLUG, &dev->flags))
752                 disable_net_traffic(dev);
753         unlink_all_urbs(dev);
754
755         return res;
756 }
757
758 static void rtl8150_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *info)
759 {
760         rtl8150_t *dev = netdev_priv(netdev);
761
762         strncpy(info->driver, driver_name, ETHTOOL_BUSINFO_LEN);
763         strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
764         usb_make_path(dev->udev, info->bus_info, sizeof info->bus_info);
765 }
766
767 static int rtl8150_get_settings(struct net_device *netdev, struct ethtool_cmd *ecmd)
768 {
769         rtl8150_t *dev = netdev_priv(netdev);
770         short lpa, bmcr;
771
772         ecmd->supported = (SUPPORTED_10baseT_Half |
773                           SUPPORTED_10baseT_Full |
774                           SUPPORTED_100baseT_Half |
775                           SUPPORTED_100baseT_Full |
776                           SUPPORTED_Autoneg |
777                           SUPPORTED_TP | SUPPORTED_MII);
778         ecmd->port = PORT_TP;
779         ecmd->transceiver = XCVR_INTERNAL;
780         ecmd->phy_address = dev->phy;
781         get_registers(dev, BMCR, 2, &bmcr);
782         get_registers(dev, ANLP, 2, &lpa);
783         if (bmcr & BMCR_ANENABLE) {
784                 ecmd->autoneg = AUTONEG_ENABLE;
785                 ecmd->speed = (lpa & (LPA_100HALF | LPA_100FULL)) ?
786                              SPEED_100 : SPEED_10;
787                 if (ecmd->speed == SPEED_100)
788                         ecmd->duplex = (lpa & LPA_100FULL) ?
789                             DUPLEX_FULL : DUPLEX_HALF;
790                 else
791                         ecmd->duplex = (lpa & LPA_10FULL) ?
792                             DUPLEX_FULL : DUPLEX_HALF;
793         } else {
794                 ecmd->autoneg = AUTONEG_DISABLE;
795                 ecmd->speed = (bmcr & BMCR_SPEED100) ?
796                     SPEED_100 : SPEED_10;
797                 ecmd->duplex = (bmcr & BMCR_FULLDPLX) ?
798                     DUPLEX_FULL : DUPLEX_HALF;
799         }
800         return 0;
801 }
802
803 static struct ethtool_ops ops = {
804         .get_drvinfo = rtl8150_get_drvinfo,
805         .get_settings = rtl8150_get_settings,
806         .get_link = ethtool_op_get_link
807 };
808
809 static int rtl8150_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
810 {
811         rtl8150_t *dev = netdev_priv(netdev);
812         u16 *data = (u16 *) & rq->ifr_ifru;
813         int res = 0;
814
815         switch (cmd) {
816         case SIOCDEVPRIVATE:
817                 data[0] = dev->phy;
818         case SIOCDEVPRIVATE + 1:
819                 read_mii_word(dev, dev->phy, (data[1] & 0x1f), &data[3]);
820                 break;
821         case SIOCDEVPRIVATE + 2:
822                 if (!capable(CAP_NET_ADMIN))
823                         return -EPERM;
824                 write_mii_word(dev, dev->phy, (data[1] & 0x1f), data[2]);
825                 break;
826         default:
827                 res = -EOPNOTSUPP;
828         }
829
830         return res;
831 }
832
833 static int rtl8150_probe(struct usb_interface *intf,
834                          const struct usb_device_id *id)
835 {
836         struct usb_device *udev = interface_to_usbdev(intf);
837         rtl8150_t *dev;
838         struct net_device *netdev;
839
840         netdev = alloc_etherdev(sizeof(rtl8150_t));
841         if (!netdev) {
842                 err("Out of memory");
843                 return -ENOMEM;
844         }
845
846         dev = netdev_priv(netdev);
847         memset(dev, 0, sizeof(rtl8150_t));
848
849         dev->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL);
850         if (!dev->intr_buff) {
851                 free_netdev(netdev);
852                 return -ENOMEM;
853         }
854
855         tasklet_init(&dev->tl, rx_fixup, (unsigned long)dev);
856         spin_lock_init(&dev->rx_pool_lock);
857         
858         dev->udev = udev;
859         dev->netdev = netdev;
860         SET_MODULE_OWNER(netdev);
861         netdev->open = rtl8150_open;
862         netdev->stop = rtl8150_close;
863         netdev->do_ioctl = rtl8150_ioctl;
864         netdev->watchdog_timeo = RTL8150_TX_TIMEOUT;
865         netdev->tx_timeout = rtl8150_tx_timeout;
866         netdev->hard_start_xmit = rtl8150_start_xmit;
867         netdev->set_multicast_list = rtl8150_set_multicast;
868         netdev->set_mac_address = rtl8150_set_mac_address;
869         netdev->get_stats = rtl8150_netdev_stats;
870         netdev->mtu = RTL8150_MTU;
871         SET_ETHTOOL_OPS(netdev, &ops);
872         dev->intr_interval = 100;       /* 100ms */
873
874         if (!alloc_all_urbs(dev)) {
875                 err("out of memory");
876                 goto out;
877         }
878         if (!rtl8150_reset(dev)) {
879                 err("couldn't reset the device");
880                 goto out1;
881         }
882         fill_skb_pool(dev);
883         set_ethernet_addr(dev);
884         info("%s: rtl8150 is detected", netdev->name);
885         
886         usb_set_intfdata(intf, dev);
887         SET_NETDEV_DEV(netdev, &intf->dev);
888         if (register_netdev(netdev) != 0) {
889                 err("couldn't register the device");
890                 goto out2;
891         }
892         return 0;
893
894 out2:
895         usb_set_intfdata(intf, NULL);
896         free_skb_pool(dev);
897 out1:
898         free_all_urbs(dev);
899 out:
900         kfree(dev->intr_buff);
901         free_netdev(netdev);
902         return -EIO;
903 }
904
905 static void rtl8150_disconnect(struct usb_interface *intf)
906 {
907         rtl8150_t *dev = usb_get_intfdata(intf);
908
909         usb_set_intfdata(intf, NULL);
910         if (dev) {
911                 set_bit(RTL8150_UNPLUG, &dev->flags);
912                 tasklet_disable(&dev->tl);
913                 unregister_netdev(dev->netdev);
914                 unlink_all_urbs(dev);
915                 free_all_urbs(dev);
916                 free_skb_pool(dev);
917                 if (dev->rx_skb)
918                         dev_kfree_skb(dev->rx_skb);
919                 kfree(dev->intr_buff);
920                 free_netdev(dev->netdev);
921         }
922 }
923
924 static int __init usb_rtl8150_init(void)
925 {
926         info(DRIVER_DESC " " DRIVER_VERSION);
927         return usb_register(&rtl8150_driver);
928 }
929
930 static void __exit usb_rtl8150_exit(void)
931 {
932         usb_deregister(&rtl8150_driver);
933 }
934
935 module_init(usb_rtl8150_init);
936 module_exit(usb_rtl8150_exit);
937
938 MODULE_AUTHOR(DRIVER_AUTHOR);
939 MODULE_DESCRIPTION(DRIVER_DESC);
940 MODULE_LICENSE("GPL");