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