Merge branches 'topic/asoc', 'topic/misc-fixes' and 'topic/hda' into for-linus
[linux-2.6] / drivers / net / usb / 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/init.h>
10 #include <linux/signal.h>
11 #include <linux/slab.h>
12 #include <linux/module.h>
13 #include <linux/netdevice.h>
14 #include <linux/etherdevice.h>
15 #include <linux/mii.h>
16 #include <linux/ethtool.h>
17 #include <linux/usb.h>
18 #include <asm/uaccess.h>
19
20 /* Version Information */
21 #define DRIVER_VERSION "v0.6.2 (2004/08/27)"
22 #define DRIVER_AUTHOR "Petko Manolov <petkan@users.sourceforge.net>"
23 #define DRIVER_DESC "rtl8150 based usb-ethernet driver"
24
25 #define IDR                     0x0120
26 #define MAR                     0x0126
27 #define CR                      0x012e
28 #define TCR                     0x012f
29 #define RCR                     0x0130
30 #define TSR                     0x0132
31 #define RSR                     0x0133
32 #define CON0                    0x0135
33 #define CON1                    0x0136
34 #define MSR                     0x0137
35 #define PHYADD                  0x0138
36 #define PHYDAT                  0x0139
37 #define PHYCNT                  0x013b
38 #define GPPC                    0x013d
39 #define BMCR                    0x0140
40 #define BMSR                    0x0142
41 #define ANAR                    0x0144
42 #define ANLP                    0x0146
43 #define AER                     0x0148
44 #define CSCR                    0x014C  /* This one has the link status */
45 #define CSCR_LINK_STATUS        (1 << 3)
46
47 #define IDR_EEPROM              0x1202
48
49 #define PHY_READ                0
50 #define PHY_WRITE               0x20
51 #define PHY_GO                  0x40
52
53 #define MII_TIMEOUT             10
54 #define INTBUFSIZE              8
55
56 #define RTL8150_REQT_READ       0xc0
57 #define RTL8150_REQT_WRITE      0x40
58 #define RTL8150_REQ_GET_REGS    0x05
59 #define RTL8150_REQ_SET_REGS    0x05
60
61
62 /* Transmit status register errors */
63 #define TSR_ECOL                (1<<5)
64 #define TSR_LCOL                (1<<4)
65 #define TSR_LOSS_CRS            (1<<3)
66 #define TSR_JBR                 (1<<2)
67 #define TSR_ERRORS              (TSR_ECOL | TSR_LCOL | TSR_LOSS_CRS | TSR_JBR)
68 /* Receive status register errors */
69 #define RSR_CRC                 (1<<2)
70 #define RSR_FAE                 (1<<1)
71 #define RSR_ERRORS              (RSR_CRC | RSR_FAE)
72
73 /* Media status register definitions */
74 #define MSR_DUPLEX              (1<<4)
75 #define MSR_SPEED               (1<<3)
76 #define MSR_LINK                (1<<2)
77
78 /* Interrupt pipe data */
79 #define INT_TSR                 0x00
80 #define INT_RSR                 0x01
81 #define INT_MSR                 0x02
82 #define INT_WAKSR               0x03
83 #define INT_TXOK_CNT            0x04
84 #define INT_RXLOST_CNT          0x05
85 #define INT_CRERR_CNT           0x06
86 #define INT_COL_CNT             0x07
87
88 /* Transmit status register errors */
89 #define TSR_ECOL                (1<<5)
90 #define TSR_LCOL                (1<<4)
91 #define TSR_LOSS_CRS            (1<<3)
92 #define TSR_JBR                 (1<<2)
93 #define TSR_ERRORS              (TSR_ECOL | TSR_LCOL | TSR_LOSS_CRS | TSR_JBR)
94 /* Receive status register errors */
95 #define RSR_CRC                 (1<<2)
96 #define RSR_FAE                 (1<<1)
97 #define RSR_ERRORS              (RSR_CRC | RSR_FAE)
98
99 /* Media status register definitions */
100 #define MSR_DUPLEX              (1<<4)
101 #define MSR_SPEED               (1<<3)
102 #define MSR_LINK                (1<<2)
103
104 /* Interrupt pipe data */
105 #define INT_TSR                 0x00
106 #define INT_RSR                 0x01
107 #define INT_MSR                 0x02
108 #define INT_WAKSR               0x03
109 #define INT_TXOK_CNT            0x04
110 #define INT_RXLOST_CNT          0x05
111 #define INT_CRERR_CNT           0x06
112 #define INT_COL_CNT             0x07
113
114
115 #define RTL8150_MTU             1540
116 #define RTL8150_TX_TIMEOUT      (HZ)
117 #define RX_SKB_POOL_SIZE        4
118
119 /* rtl8150 flags */
120 #define RTL8150_HW_CRC          0
121 #define RX_REG_SET              1
122 #define RTL8150_UNPLUG          2
123 #define RX_URB_FAIL             3
124
125 /* Define these values to match your device */
126 #define VENDOR_ID_REALTEK               0x0bda
127 #define VENDOR_ID_MELCO                 0x0411
128 #define VENDOR_ID_MICRONET              0x3980
129 #define VENDOR_ID_LONGSHINE             0x07b8
130 #define VENDOR_ID_OQO                   0x1557
131 #define VENDOR_ID_ZYXEL                 0x0586
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 #define PRODUCT_ID_PRESTIGE             0x401a
138
139 #undef  EEPROM_WRITE
140
141 /* table of devices that work with this driver */
142 static struct usb_device_id rtl8150_table[] = {
143         {USB_DEVICE(VENDOR_ID_REALTEK, PRODUCT_ID_RTL8150)},
144         {USB_DEVICE(VENDOR_ID_MELCO, PRODUCT_ID_LUAKTX)},
145         {USB_DEVICE(VENDOR_ID_MICRONET, PRODUCT_ID_SP128AR)},
146         {USB_DEVICE(VENDOR_ID_LONGSHINE, PRODUCT_ID_LCS8138TX)},
147         {USB_DEVICE(VENDOR_ID_OQO, PRODUCT_ID_RTL8150)},
148         {USB_DEVICE(VENDOR_ID_ZYXEL, PRODUCT_ID_PRESTIGE)},
149         {}
150 };
151
152 MODULE_DEVICE_TABLE(usb, rtl8150_table);
153
154 struct rtl8150 {
155         unsigned long flags;
156         struct usb_device *udev;
157         struct tasklet_struct tl;
158         struct net_device_stats stats;
159         struct net_device *netdev;
160         struct urb *rx_urb, *tx_urb, *intr_urb, *ctrl_urb;
161         struct sk_buff *tx_skb, *rx_skb;
162         struct sk_buff *rx_skb_pool[RX_SKB_POOL_SIZE];
163         spinlock_t rx_pool_lock;
164         struct usb_ctrlrequest dr;
165         int intr_interval;
166         __le16 rx_creg;
167         u8 *intr_buff;
168         u8 phy;
169 };
170
171 typedef struct rtl8150 rtl8150_t;
172
173 static void fill_skb_pool(rtl8150_t *);
174 static void free_skb_pool(rtl8150_t *);
175 static inline struct sk_buff *pull_skb(rtl8150_t *);
176 static void rtl8150_disconnect(struct usb_interface *intf);
177 static int rtl8150_probe(struct usb_interface *intf,
178                            const struct usb_device_id *id);
179 static int rtl8150_suspend(struct usb_interface *intf, pm_message_t message);
180 static int rtl8150_resume(struct usb_interface *intf);
181
182 static const char driver_name [] = "rtl8150";
183
184 static struct usb_driver rtl8150_driver = {
185         .name =         driver_name,
186         .probe =        rtl8150_probe,
187         .disconnect =   rtl8150_disconnect,
188         .id_table =     rtl8150_table,
189         .suspend =      rtl8150_suspend,
190         .resume =       rtl8150_resume
191 };
192
193 /*
194 **
195 **      device related part of the code
196 **
197 */
198 static int get_registers(rtl8150_t * dev, u16 indx, u16 size, void *data)
199 {
200         return usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
201                                RTL8150_REQ_GET_REGS, RTL8150_REQT_READ,
202                                indx, 0, data, size, 500);
203 }
204
205 static int set_registers(rtl8150_t * dev, u16 indx, u16 size, void *data)
206 {
207         return usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
208                                RTL8150_REQ_SET_REGS, RTL8150_REQT_WRITE,
209                                indx, 0, data, size, 500);
210 }
211
212 static void ctrl_callback(struct urb *urb)
213 {
214         rtl8150_t *dev;
215
216         switch (urb->status) {
217         case 0:
218                 break;
219         case -EINPROGRESS:
220                 break;
221         case -ENOENT:
222                 break;
223         default:
224                 dev_warn(&urb->dev->dev, "ctrl urb status %d\n", urb->status);
225         }
226         dev = urb->context;
227         clear_bit(RX_REG_SET, &dev->flags);
228 }
229
230 static int async_set_registers(rtl8150_t * dev, u16 indx, u16 size)
231 {
232         int ret;
233
234         if (test_bit(RX_REG_SET, &dev->flags))
235                 return -EAGAIN;
236
237         dev->dr.bRequestType = RTL8150_REQT_WRITE;
238         dev->dr.bRequest = RTL8150_REQ_SET_REGS;
239         dev->dr.wValue = cpu_to_le16(indx);
240         dev->dr.wIndex = 0;
241         dev->dr.wLength = cpu_to_le16(size);
242         dev->ctrl_urb->transfer_buffer_length = size;
243         usb_fill_control_urb(dev->ctrl_urb, dev->udev,
244                          usb_sndctrlpipe(dev->udev, 0), (char *) &dev->dr,
245                          &dev->rx_creg, size, ctrl_callback, dev);
246         if ((ret = usb_submit_urb(dev->ctrl_urb, GFP_ATOMIC))) {
247                 if (ret == -ENODEV)
248                         netif_device_detach(dev->netdev);
249                 err("control request submission failed: %d", ret);
250         } else
251                 set_bit(RX_REG_SET, &dev->flags);
252
253         return ret;
254 }
255
256 static int read_mii_word(rtl8150_t * dev, u8 phy, __u8 indx, u16 * reg)
257 {
258         int i;
259         u8 data[3], tmp;
260
261         data[0] = phy;
262         data[1] = data[2] = 0;
263         tmp = indx | PHY_READ | PHY_GO;
264         i = 0;
265
266         set_registers(dev, PHYADD, sizeof(data), data);
267         set_registers(dev, PHYCNT, 1, &tmp);
268         do {
269                 get_registers(dev, PHYCNT, 1, data);
270         } while ((data[0] & PHY_GO) && (i++ < MII_TIMEOUT));
271
272         if (i < MII_TIMEOUT) {
273                 get_registers(dev, PHYDAT, 2, data);
274                 *reg = data[0] | (data[1] << 8);
275                 return 0;
276         } else
277                 return 1;
278 }
279
280 static int write_mii_word(rtl8150_t * dev, u8 phy, __u8 indx, u16 reg)
281 {
282         int i;
283         u8 data[3], tmp;
284
285         data[0] = phy;
286         data[1] = reg & 0xff;
287         data[2] = (reg >> 8) & 0xff;
288         tmp = indx | PHY_WRITE | PHY_GO;
289         i = 0;
290
291         set_registers(dev, PHYADD, sizeof(data), data);
292         set_registers(dev, PHYCNT, 1, &tmp);
293         do {
294                 get_registers(dev, PHYCNT, 1, data);
295         } while ((data[0] & PHY_GO) && (i++ < MII_TIMEOUT));
296
297         if (i < MII_TIMEOUT)
298                 return 0;
299         else
300                 return 1;
301 }
302
303 static inline void set_ethernet_addr(rtl8150_t * dev)
304 {
305         u8 node_id[6];
306
307         get_registers(dev, IDR, sizeof(node_id), node_id);
308         memcpy(dev->netdev->dev_addr, node_id, sizeof(node_id));
309 }
310
311 static int rtl8150_set_mac_address(struct net_device *netdev, void *p)
312 {
313         struct sockaddr *addr = p;
314         rtl8150_t *dev = netdev_priv(netdev);
315         int i;
316
317         if (netif_running(netdev))
318                 return -EBUSY;
319
320         memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
321         dbg("%s: Setting MAC address to ", netdev->name);
322         for (i = 0; i < 5; i++)
323                 dbg("%02X:", netdev->dev_addr[i]);
324         dbg("%02X\n", netdev->dev_addr[i]);
325         /* Set the IDR registers. */
326         set_registers(dev, IDR, sizeof(netdev->dev_addr), netdev->dev_addr);
327 #ifdef EEPROM_WRITE
328         {
329         u8 cr;
330         /* Get the CR contents. */
331         get_registers(dev, CR, 1, &cr);
332         /* Set the WEPROM bit (eeprom write enable). */
333         cr |= 0x20;
334         set_registers(dev, CR, 1, &cr);
335         /* Write the MAC address into eeprom. Eeprom writes must be word-sized,
336            so we need to split them up. */
337         for (i = 0; i * 2 < netdev->addr_len; i++) {
338                 set_registers(dev, IDR_EEPROM + (i * 2), 2, 
339                 netdev->dev_addr + (i * 2));
340         }
341         /* Clear the WEPROM bit (preventing accidental eeprom writes). */
342         cr &= 0xdf;
343         set_registers(dev, CR, 1, &cr);
344         }
345 #endif
346         return 0;
347 }
348
349 static int rtl8150_reset(rtl8150_t * dev)
350 {
351         u8 data = 0x10;
352         int i = HZ;
353
354         set_registers(dev, CR, 1, &data);
355         do {
356                 get_registers(dev, CR, 1, &data);
357         } while ((data & 0x10) && --i);
358
359         return (i > 0) ? 1 : 0;
360 }
361
362 static int alloc_all_urbs(rtl8150_t * dev)
363 {
364         dev->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
365         if (!dev->rx_urb)
366                 return 0;
367         dev->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
368         if (!dev->tx_urb) {
369                 usb_free_urb(dev->rx_urb);
370                 return 0;
371         }
372         dev->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
373         if (!dev->intr_urb) {
374                 usb_free_urb(dev->rx_urb);
375                 usb_free_urb(dev->tx_urb);
376                 return 0;
377         }
378         dev->ctrl_urb = usb_alloc_urb(0, GFP_KERNEL);
379         if (!dev->ctrl_urb) {
380                 usb_free_urb(dev->rx_urb);
381                 usb_free_urb(dev->tx_urb);
382                 usb_free_urb(dev->intr_urb);
383                 return 0;
384         }
385
386         return 1;
387 }
388
389 static void free_all_urbs(rtl8150_t * dev)
390 {
391         usb_free_urb(dev->rx_urb);
392         usb_free_urb(dev->tx_urb);
393         usb_free_urb(dev->intr_urb);
394         usb_free_urb(dev->ctrl_urb);
395 }
396
397 static void unlink_all_urbs(rtl8150_t * dev)
398 {
399         usb_kill_urb(dev->rx_urb);
400         usb_kill_urb(dev->tx_urb);
401         usb_kill_urb(dev->intr_urb);
402         usb_kill_urb(dev->ctrl_urb);
403 }
404
405 static inline struct sk_buff *pull_skb(rtl8150_t *dev)
406 {
407         struct sk_buff *skb;
408         int i;
409
410         for (i = 0; i < RX_SKB_POOL_SIZE; i++) {
411                 if (dev->rx_skb_pool[i]) {
412                         skb = dev->rx_skb_pool[i];
413                         dev->rx_skb_pool[i] = NULL;
414                         return skb;
415                 }
416         }
417         return NULL;
418 }
419
420 static void read_bulk_callback(struct urb *urb)
421 {
422         rtl8150_t *dev;
423         unsigned pkt_len, res;
424         struct sk_buff *skb;
425         struct net_device *netdev;
426         u16 rx_stat;
427         int status;
428
429         dev = urb->context;
430         if (!dev)
431                 return;
432         if (test_bit(RTL8150_UNPLUG, &dev->flags))
433                 return;
434         netdev = dev->netdev;
435         if (!netif_device_present(netdev))
436                 return;
437
438         switch (urb->status) {
439         case 0:
440                 break;
441         case -ENOENT:
442                 return; /* the urb is in unlink state */
443         case -ETIME:
444                 dev_warn(&urb->dev->dev, "may be reset is needed?..\n");
445                 goto goon;
446         default:
447                 dev_warn(&urb->dev->dev, "Rx status %d\n", urb->status);
448                 goto goon;
449         }
450
451         if (!dev->rx_skb)
452                 goto resched;
453         /* protect against short packets (tell me why we got some?!?) */
454         if (urb->actual_length < 4)
455                 goto goon;
456
457         res = urb->actual_length;
458         rx_stat = le16_to_cpu(*(__le16 *)(urb->transfer_buffer + res - 4));
459         pkt_len = res - 4;
460
461         skb_put(dev->rx_skb, pkt_len);
462         dev->rx_skb->protocol = eth_type_trans(dev->rx_skb, netdev);
463         netif_rx(dev->rx_skb);
464         dev->stats.rx_packets++;
465         dev->stats.rx_bytes += pkt_len;
466
467         spin_lock(&dev->rx_pool_lock);
468         skb = pull_skb(dev);
469         spin_unlock(&dev->rx_pool_lock);
470         if (!skb)
471                 goto resched;
472
473         dev->rx_skb = skb;
474 goon:
475         usb_fill_bulk_urb(dev->rx_urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
476                       dev->rx_skb->data, RTL8150_MTU, read_bulk_callback, dev);
477         status = usb_submit_urb(dev->rx_urb, GFP_ATOMIC);
478         if (status == -ENODEV)
479                 netif_device_detach(dev->netdev);
480         else if (status) {
481                 set_bit(RX_URB_FAIL, &dev->flags);
482                 goto resched;
483         } else {
484                 clear_bit(RX_URB_FAIL, &dev->flags);
485         }
486
487         return;
488 resched:
489         tasklet_schedule(&dev->tl);
490 }
491
492 static void rx_fixup(unsigned long data)
493 {
494         rtl8150_t *dev;
495         struct sk_buff *skb;
496         int status;
497
498         dev = (rtl8150_t *)data;
499
500         spin_lock_irq(&dev->rx_pool_lock);
501         fill_skb_pool(dev);
502         spin_unlock_irq(&dev->rx_pool_lock);
503         if (test_bit(RX_URB_FAIL, &dev->flags))
504                 if (dev->rx_skb)
505                         goto try_again;
506         spin_lock_irq(&dev->rx_pool_lock);
507         skb = pull_skb(dev);
508         spin_unlock_irq(&dev->rx_pool_lock);
509         if (skb == NULL)
510                 goto tlsched;
511         dev->rx_skb = skb;
512         usb_fill_bulk_urb(dev->rx_urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
513                       dev->rx_skb->data, RTL8150_MTU, read_bulk_callback, dev);
514 try_again:
515         status = usb_submit_urb(dev->rx_urb, GFP_ATOMIC);
516         if (status == -ENODEV) {
517                 netif_device_detach(dev->netdev);
518         } else if (status) {
519                 set_bit(RX_URB_FAIL, &dev->flags);
520                 goto tlsched;
521         } else {
522                 clear_bit(RX_URB_FAIL, &dev->flags);
523         }
524
525         return;
526 tlsched:
527         tasklet_schedule(&dev->tl);
528 }
529
530 static void write_bulk_callback(struct urb *urb)
531 {
532         rtl8150_t *dev;
533
534         dev = urb->context;
535         if (!dev)
536                 return;
537         dev_kfree_skb_irq(dev->tx_skb);
538         if (!netif_device_present(dev->netdev))
539                 return;
540         if (urb->status)
541                 dev_info(&urb->dev->dev, "%s: Tx status %d\n",
542                          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                 dev_info(&urb->dev->dev, "%s: intr status %d\n",
566                          dev->netdev->name, urb->status);
567                 goto resubmit;
568         }
569
570         d = urb->transfer_buffer;
571         if (d[0] & TSR_ERRORS) {
572                 dev->stats.tx_errors++;
573                 if (d[INT_TSR] & (TSR_ECOL | TSR_JBR))
574                         dev->stats.tx_aborted_errors++;
575                 if (d[INT_TSR] & TSR_LCOL)
576                         dev->stats.tx_window_errors++;
577                 if (d[INT_TSR] & TSR_LOSS_CRS)
578                         dev->stats.tx_carrier_errors++;
579         }
580         /* Report link status changes to the network stack */
581         if ((d[INT_MSR] & MSR_LINK) == 0) {
582                 if (netif_carrier_ok(dev->netdev)) {
583                         netif_carrier_off(dev->netdev);
584                         dbg("%s: LINK LOST\n", __func__);
585                 }
586         } else {
587                 if (!netif_carrier_ok(dev->netdev)) {
588                         netif_carrier_on(dev->netdev);
589                         dbg("%s: LINK CAME BACK\n", __func__);
590                 }
591         }
592
593 resubmit:
594         status = usb_submit_urb (urb, GFP_ATOMIC);
595         if (status == -ENODEV)
596                 netif_device_detach(dev->netdev);
597         else if (status)
598                 err ("can't resubmit intr, %s-%s/input0, status %d",
599                                 dev->udev->bus->bus_name,
600                                 dev->udev->devpath, status);
601 }
602
603 static int rtl8150_suspend(struct usb_interface *intf, pm_message_t message)
604 {
605         rtl8150_t *dev = usb_get_intfdata(intf);
606
607         netif_device_detach(dev->netdev);
608
609         if (netif_running(dev->netdev)) {
610                 usb_kill_urb(dev->rx_urb);
611                 usb_kill_urb(dev->intr_urb);
612         }
613         return 0;
614 }
615
616 static int rtl8150_resume(struct usb_interface *intf)
617 {
618         rtl8150_t *dev = usb_get_intfdata(intf);
619
620         netif_device_attach(dev->netdev);
621         if (netif_running(dev->netdev)) {
622                 dev->rx_urb->status = 0;
623                 dev->rx_urb->actual_length = 0;
624                 read_bulk_callback(dev->rx_urb);
625
626                 dev->intr_urb->status = 0;
627                 dev->intr_urb->actual_length = 0;
628                 intr_callback(dev->intr_urb);
629         }
630         return 0;
631 }
632
633 /*
634 **
635 **      network related part of the code
636 **
637 */
638
639 static void fill_skb_pool(rtl8150_t *dev)
640 {
641         struct sk_buff *skb;
642         int i;
643
644         for (i = 0; i < RX_SKB_POOL_SIZE; i++) {
645                 if (dev->rx_skb_pool[i])
646                         continue;
647                 skb = dev_alloc_skb(RTL8150_MTU + 2);
648                 if (!skb) {
649                         return;
650                 }
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                 dev_warn(&dev->udev->dev, "device reset failed\n");
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         dev_warn(&netdev->dev, "Tx timeout.\n");
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                 dev_info(&netdev->dev, "%s: promiscuous mode\n", 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                 dev_info(&netdev->dev, "%s: allmulti set\n", 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                         dev_warn(&netdev->dev, "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                 dev_warn(&netdev->dev, "rx_urb submit failed: %d\n", 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                 dev_warn(&netdev->dev, "intr_urb submit failed: %d\n", 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
911         dev->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL);
912         if (!dev->intr_buff) {
913                 free_netdev(netdev);
914                 return -ENOMEM;
915         }
916
917         tasklet_init(&dev->tl, rx_fixup, (unsigned long)dev);
918         spin_lock_init(&dev->rx_pool_lock);
919         
920         dev->udev = udev;
921         dev->netdev = netdev;
922         netdev->open = rtl8150_open;
923         netdev->stop = rtl8150_close;
924         netdev->do_ioctl = rtl8150_ioctl;
925         netdev->watchdog_timeo = RTL8150_TX_TIMEOUT;
926         netdev->tx_timeout = rtl8150_tx_timeout;
927         netdev->hard_start_xmit = rtl8150_start_xmit;
928         netdev->set_multicast_list = rtl8150_set_multicast;
929         netdev->set_mac_address = rtl8150_set_mac_address;
930         netdev->get_stats = rtl8150_netdev_stats;
931         SET_ETHTOOL_OPS(netdev, &ops);
932         dev->intr_interval = 100;       /* 100ms */
933
934         if (!alloc_all_urbs(dev)) {
935                 err("out of memory");
936                 goto out;
937         }
938         if (!rtl8150_reset(dev)) {
939                 err("couldn't reset the device");
940                 goto out1;
941         }
942         fill_skb_pool(dev);
943         set_ethernet_addr(dev);
944         
945         usb_set_intfdata(intf, dev);
946         SET_NETDEV_DEV(netdev, &intf->dev);
947         if (register_netdev(netdev) != 0) {
948                 err("couldn't register the device");
949                 goto out2;
950         }
951
952         dev_info(&intf->dev, "%s: rtl8150 is detected\n", netdev->name);
953
954         return 0;
955
956 out2:
957         usb_set_intfdata(intf, NULL);
958         free_skb_pool(dev);
959 out1:
960         free_all_urbs(dev);
961 out:
962         kfree(dev->intr_buff);
963         free_netdev(netdev);
964         return -EIO;
965 }
966
967 static void rtl8150_disconnect(struct usb_interface *intf)
968 {
969         rtl8150_t *dev = usb_get_intfdata(intf);
970
971         usb_set_intfdata(intf, NULL);
972         if (dev) {
973                 set_bit(RTL8150_UNPLUG, &dev->flags);
974                 tasklet_disable(&dev->tl);
975                 tasklet_kill(&dev->tl);
976                 unregister_netdev(dev->netdev);
977                 unlink_all_urbs(dev);
978                 free_all_urbs(dev);
979                 free_skb_pool(dev);
980                 if (dev->rx_skb)
981                         dev_kfree_skb(dev->rx_skb);
982                 kfree(dev->intr_buff);
983                 free_netdev(dev->netdev);
984         }
985 }
986
987 static int __init usb_rtl8150_init(void)
988 {
989         printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
990                DRIVER_DESC "\n");
991         return usb_register(&rtl8150_driver);
992 }
993
994 static void __exit usb_rtl8150_exit(void)
995 {
996         usb_deregister(&rtl8150_driver);
997 }
998
999 module_init(usb_rtl8150_init);
1000 module_exit(usb_rtl8150_exit);
1001
1002 MODULE_AUTHOR(DRIVER_AUTHOR);
1003 MODULE_DESCRIPTION(DRIVER_DESC);
1004 MODULE_LICENSE("GPL");