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