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