pandora: defconfig: update
[pandora-kernel.git] / 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 *netdev;
159         struct urb *rx_urb, *tx_urb, *intr_urb, *ctrl_urb;
160         struct sk_buff *tx_skb, *rx_skb;
161         struct sk_buff *rx_skb_pool[RX_SKB_POOL_SIZE];
162         spinlock_t rx_pool_lock;
163         struct usb_ctrlrequest dr;
164         int intr_interval;
165         __le16 rx_creg;
166         u8 *intr_buff;
167         u8 phy;
168 };
169
170 typedef struct rtl8150 rtl8150_t;
171
172 static const char driver_name [] = "rtl8150";
173
174 /*
175 **
176 **      device related part of the code
177 **
178 */
179 static int get_registers(rtl8150_t * dev, u16 indx, u16 size, void *data)
180 {
181         void *buf;
182         int ret;
183
184         buf = kmalloc(size, GFP_NOIO);
185         if (!buf)
186                 return -ENOMEM;
187
188         ret = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
189                               RTL8150_REQ_GET_REGS, RTL8150_REQT_READ,
190                               indx, 0, buf, size, 500);
191         if (ret > 0 && ret <= size)
192                 memcpy(data, buf, ret);
193         kfree(buf);
194         return ret;
195 }
196
197 static int set_registers(rtl8150_t * dev, u16 indx, u16 size, const void *data)
198 {
199         void *buf;
200         int ret;
201
202         buf = kmemdup(data, size, GFP_NOIO);
203         if (!buf)
204                 return -ENOMEM;
205
206         ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
207                               RTL8150_REQ_SET_REGS, RTL8150_REQT_WRITE,
208                               indx, 0, buf, size, 500);
209         kfree(buf);
210         return ret;
211 }
212
213 static void ctrl_callback(struct urb *urb)
214 {
215         rtl8150_t *dev;
216         int status = urb->status;
217
218         switch (status) {
219         case 0:
220                 break;
221         case -EINPROGRESS:
222                 break;
223         case -ENOENT:
224                 break;
225         default:
226                 if (printk_ratelimit())
227                         dev_warn(&urb->dev->dev, "ctrl urb status %d\n", status);
228         }
229         dev = urb->context;
230         clear_bit(RX_REG_SET, &dev->flags);
231 }
232
233 static int async_set_registers(rtl8150_t * dev, u16 indx, u16 size)
234 {
235         int ret;
236
237         if (test_bit(RX_REG_SET, &dev->flags))
238                 return -EAGAIN;
239
240         dev->dr.bRequestType = RTL8150_REQT_WRITE;
241         dev->dr.bRequest = RTL8150_REQ_SET_REGS;
242         dev->dr.wValue = cpu_to_le16(indx);
243         dev->dr.wIndex = 0;
244         dev->dr.wLength = cpu_to_le16(size);
245         dev->ctrl_urb->transfer_buffer_length = size;
246         usb_fill_control_urb(dev->ctrl_urb, dev->udev,
247                          usb_sndctrlpipe(dev->udev, 0), (char *) &dev->dr,
248                          &dev->rx_creg, size, ctrl_callback, dev);
249         if ((ret = usb_submit_urb(dev->ctrl_urb, GFP_ATOMIC))) {
250                 if (ret == -ENODEV)
251                         netif_device_detach(dev->netdev);
252                 err("control request submission failed: %d", ret);
253         } else
254                 set_bit(RX_REG_SET, &dev->flags);
255
256         return ret;
257 }
258
259 static int read_mii_word(rtl8150_t * dev, u8 phy, __u8 indx, u16 * reg)
260 {
261         int i;
262         u8 data[3], tmp;
263
264         data[0] = phy;
265         data[1] = data[2] = 0;
266         tmp = indx | PHY_READ | PHY_GO;
267         i = 0;
268
269         set_registers(dev, PHYADD, sizeof(data), data);
270         set_registers(dev, PHYCNT, 1, &tmp);
271         do {
272                 get_registers(dev, PHYCNT, 1, data);
273         } while ((data[0] & PHY_GO) && (i++ < MII_TIMEOUT));
274
275         if (i <= MII_TIMEOUT) {
276                 get_registers(dev, PHYDAT, 2, data);
277                 *reg = data[0] | (data[1] << 8);
278                 return 0;
279         } else
280                 return 1;
281 }
282
283 static int write_mii_word(rtl8150_t * dev, u8 phy, __u8 indx, u16 reg)
284 {
285         int i;
286         u8 data[3], tmp;
287
288         data[0] = phy;
289         data[1] = reg & 0xff;
290         data[2] = (reg >> 8) & 0xff;
291         tmp = indx | PHY_WRITE | PHY_GO;
292         i = 0;
293
294         set_registers(dev, PHYADD, sizeof(data), data);
295         set_registers(dev, PHYCNT, 1, &tmp);
296         do {
297                 get_registers(dev, PHYCNT, 1, data);
298         } while ((data[0] & PHY_GO) && (i++ < MII_TIMEOUT));
299
300         if (i <= MII_TIMEOUT)
301                 return 0;
302         else
303                 return 1;
304 }
305
306 static inline void set_ethernet_addr(rtl8150_t * dev)
307 {
308         u8 node_id[6];
309
310         get_registers(dev, IDR, sizeof(node_id), node_id);
311         memcpy(dev->netdev->dev_addr, node_id, sizeof(node_id));
312 }
313
314 static int rtl8150_set_mac_address(struct net_device *netdev, void *p)
315 {
316         struct sockaddr *addr = p;
317         rtl8150_t *dev = netdev_priv(netdev);
318
319         if (netif_running(netdev))
320                 return -EBUSY;
321
322         memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
323         dbg("%s: Setting MAC address to %pM\n", netdev->name, netdev->dev_addr);
324         /* Set the IDR registers. */
325         set_registers(dev, IDR, netdev->addr_len, netdev->dev_addr);
326 #ifdef EEPROM_WRITE
327         {
328         int i;
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 = urb->status;
428         int result;
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 (status) {
440         case 0:
441                 break;
442         case -ENOENT:
443                 return; /* the urb is in unlink state */
444         case -ETIME:
445                 if (printk_ratelimit())
446                         dev_warn(&urb->dev->dev, "may be reset is needed?..\n");
447                 goto goon;
448         default:
449                 if (printk_ratelimit())
450                         dev_warn(&urb->dev->dev, "Rx status %d\n", status);
451                 goto goon;
452         }
453
454         if (!dev->rx_skb)
455                 goto resched;
456         /* protect against short packets (tell me why we got some?!?) */
457         if (urb->actual_length < 4)
458                 goto goon;
459
460         res = urb->actual_length;
461         rx_stat = le16_to_cpu(*(__le16 *)(urb->transfer_buffer + res - 4));
462         pkt_len = res - 4;
463
464         skb_put(dev->rx_skb, pkt_len);
465         dev->rx_skb->protocol = eth_type_trans(dev->rx_skb, netdev);
466         netif_rx(dev->rx_skb);
467         netdev->stats.rx_packets++;
468         netdev->stats.rx_bytes += pkt_len;
469
470         spin_lock(&dev->rx_pool_lock);
471         skb = pull_skb(dev);
472         spin_unlock(&dev->rx_pool_lock);
473         if (!skb)
474                 goto resched;
475
476         dev->rx_skb = skb;
477 goon:
478         usb_fill_bulk_urb(dev->rx_urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
479                       dev->rx_skb->data, RTL8150_MTU, read_bulk_callback, dev);
480         result = usb_submit_urb(dev->rx_urb, GFP_ATOMIC);
481         if (result == -ENODEV)
482                 netif_device_detach(dev->netdev);
483         else if (result) {
484                 set_bit(RX_URB_FAIL, &dev->flags);
485                 goto resched;
486         } else {
487                 clear_bit(RX_URB_FAIL, &dev->flags);
488         }
489
490         return;
491 resched:
492         tasklet_schedule(&dev->tl);
493 }
494
495 static void write_bulk_callback(struct urb *urb)
496 {
497         rtl8150_t *dev;
498         int status = urb->status;
499
500         dev = urb->context;
501         if (!dev)
502                 return;
503         dev_kfree_skb_irq(dev->tx_skb);
504         if (!netif_device_present(dev->netdev))
505                 return;
506         if (status)
507                 dev_info(&urb->dev->dev, "%s: Tx status %d\n",
508                          dev->netdev->name, status);
509         dev->netdev->trans_start = jiffies;
510         netif_wake_queue(dev->netdev);
511 }
512
513 static void intr_callback(struct urb *urb)
514 {
515         rtl8150_t *dev;
516         __u8 *d;
517         int status = urb->status;
518         int res;
519
520         dev = urb->context;
521         if (!dev)
522                 return;
523         switch (status) {
524         case 0:                 /* success */
525                 break;
526         case -ECONNRESET:       /* unlink */
527         case -ENOENT:
528         case -ESHUTDOWN:
529                 return;
530         /* -EPIPE:  should clear the halt */
531         default:
532                 dev_info(&urb->dev->dev, "%s: intr status %d\n",
533                          dev->netdev->name, status);
534                 goto resubmit;
535         }
536
537         d = urb->transfer_buffer;
538         if (d[0] & TSR_ERRORS) {
539                 dev->netdev->stats.tx_errors++;
540                 if (d[INT_TSR] & (TSR_ECOL | TSR_JBR))
541                         dev->netdev->stats.tx_aborted_errors++;
542                 if (d[INT_TSR] & TSR_LCOL)
543                         dev->netdev->stats.tx_window_errors++;
544                 if (d[INT_TSR] & TSR_LOSS_CRS)
545                         dev->netdev->stats.tx_carrier_errors++;
546         }
547         /* Report link status changes to the network stack */
548         if ((d[INT_MSR] & MSR_LINK) == 0) {
549                 if (netif_carrier_ok(dev->netdev)) {
550                         netif_carrier_off(dev->netdev);
551                         dbg("%s: LINK LOST\n", __func__);
552                 }
553         } else {
554                 if (!netif_carrier_ok(dev->netdev)) {
555                         netif_carrier_on(dev->netdev);
556                         dbg("%s: LINK CAME BACK\n", __func__);
557                 }
558         }
559
560 resubmit:
561         res = usb_submit_urb (urb, GFP_ATOMIC);
562         if (res == -ENODEV)
563                 netif_device_detach(dev->netdev);
564         else if (res)
565                 err ("can't resubmit intr, %s-%s/input0, status %d",
566                                 dev->udev->bus->bus_name,
567                                 dev->udev->devpath, res);
568 }
569
570 static int rtl8150_suspend(struct usb_interface *intf, pm_message_t message)
571 {
572         rtl8150_t *dev = usb_get_intfdata(intf);
573
574         netif_device_detach(dev->netdev);
575
576         if (netif_running(dev->netdev)) {
577                 usb_kill_urb(dev->rx_urb);
578                 usb_kill_urb(dev->intr_urb);
579         }
580         return 0;
581 }
582
583 static int rtl8150_resume(struct usb_interface *intf)
584 {
585         rtl8150_t *dev = usb_get_intfdata(intf);
586
587         netif_device_attach(dev->netdev);
588         if (netif_running(dev->netdev)) {
589                 dev->rx_urb->status = 0;
590                 dev->rx_urb->actual_length = 0;
591                 read_bulk_callback(dev->rx_urb);
592
593                 dev->intr_urb->status = 0;
594                 dev->intr_urb->actual_length = 0;
595                 intr_callback(dev->intr_urb);
596         }
597         return 0;
598 }
599
600 /*
601 **
602 **      network related part of the code
603 **
604 */
605
606 static void fill_skb_pool(rtl8150_t *dev)
607 {
608         struct sk_buff *skb;
609         int i;
610
611         for (i = 0; i < RX_SKB_POOL_SIZE; i++) {
612                 if (dev->rx_skb_pool[i])
613                         continue;
614                 skb = dev_alloc_skb(RTL8150_MTU + 2);
615                 if (!skb) {
616                         return;
617                 }
618                 skb_reserve(skb, 2);
619                 dev->rx_skb_pool[i] = skb;
620         }
621 }
622
623 static void free_skb_pool(rtl8150_t *dev)
624 {
625         int i;
626
627         for (i = 0; i < RX_SKB_POOL_SIZE; i++)
628                 if (dev->rx_skb_pool[i])
629                         dev_kfree_skb(dev->rx_skb_pool[i]);
630 }
631
632 static void rx_fixup(unsigned long data)
633 {
634         struct rtl8150 *dev = (struct rtl8150 *)data;
635         struct sk_buff *skb;
636         int status;
637
638         spin_lock_irq(&dev->rx_pool_lock);
639         fill_skb_pool(dev);
640         spin_unlock_irq(&dev->rx_pool_lock);
641         if (test_bit(RX_URB_FAIL, &dev->flags))
642                 if (dev->rx_skb)
643                         goto try_again;
644         spin_lock_irq(&dev->rx_pool_lock);
645         skb = pull_skb(dev);
646         spin_unlock_irq(&dev->rx_pool_lock);
647         if (skb == NULL)
648                 goto tlsched;
649         dev->rx_skb = skb;
650         usb_fill_bulk_urb(dev->rx_urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
651                       dev->rx_skb->data, RTL8150_MTU, read_bulk_callback, dev);
652 try_again:
653         status = usb_submit_urb(dev->rx_urb, GFP_ATOMIC);
654         if (status == -ENODEV) {
655                 netif_device_detach(dev->netdev);
656         } else if (status) {
657                 set_bit(RX_URB_FAIL, &dev->flags);
658                 goto tlsched;
659         } else {
660                 clear_bit(RX_URB_FAIL, &dev->flags);
661         }
662
663         return;
664 tlsched:
665         tasklet_schedule(&dev->tl);
666 }
667
668 static int enable_net_traffic(rtl8150_t * dev)
669 {
670         u8 cr, tcr, rcr, msr;
671
672         if (!rtl8150_reset(dev)) {
673                 dev_warn(&dev->udev->dev, "device reset failed\n");
674         }
675         /* RCR bit7=1 attach Rx info at the end;  =0 HW CRC (which is broken) */
676         rcr = 0x9e;
677         dev->rx_creg = cpu_to_le16(rcr);
678         tcr = 0xd8;
679         cr = 0x0c;
680         if (!(rcr & 0x80))
681                 set_bit(RTL8150_HW_CRC, &dev->flags);
682         set_registers(dev, RCR, 1, &rcr);
683         set_registers(dev, TCR, 1, &tcr);
684         set_registers(dev, CR, 1, &cr);
685         get_registers(dev, MSR, 1, &msr);
686
687         return 0;
688 }
689
690 static void disable_net_traffic(rtl8150_t * dev)
691 {
692         u8 cr;
693
694         get_registers(dev, CR, 1, &cr);
695         cr &= 0xf3;
696         set_registers(dev, CR, 1, &cr);
697 }
698
699 static void rtl8150_tx_timeout(struct net_device *netdev)
700 {
701         rtl8150_t *dev = netdev_priv(netdev);
702         dev_warn(&netdev->dev, "Tx timeout.\n");
703         usb_unlink_urb(dev->tx_urb);
704         netdev->stats.tx_errors++;
705 }
706
707 static void rtl8150_set_multicast(struct net_device *netdev)
708 {
709         rtl8150_t *dev = netdev_priv(netdev);
710         netif_stop_queue(netdev);
711         if (netdev->flags & IFF_PROMISC) {
712                 dev->rx_creg |= cpu_to_le16(0x0001);
713                 dev_info(&netdev->dev, "%s: promiscuous mode\n", netdev->name);
714         } else if (!netdev_mc_empty(netdev) ||
715                    (netdev->flags & IFF_ALLMULTI)) {
716                 dev->rx_creg &= cpu_to_le16(0xfffe);
717                 dev->rx_creg |= cpu_to_le16(0x0002);
718                 dev_info(&netdev->dev, "%s: allmulti set\n", netdev->name);
719         } else {
720                 /* ~RX_MULTICAST, ~RX_PROMISCUOUS */
721                 dev->rx_creg &= cpu_to_le16(0x00fc);
722         }
723         async_set_registers(dev, RCR, 2);
724         netif_wake_queue(netdev);
725 }
726
727 static netdev_tx_t rtl8150_start_xmit(struct sk_buff *skb,
728                                             struct net_device *netdev)
729 {
730         rtl8150_t *dev = netdev_priv(netdev);
731         int count, res;
732
733         netif_stop_queue(netdev);
734         count = (skb->len < 60) ? 60 : skb->len;
735         count = (count & 0x3f) ? count : count + 1;
736         dev->tx_skb = skb;
737         usb_fill_bulk_urb(dev->tx_urb, dev->udev, usb_sndbulkpipe(dev->udev, 2),
738                       skb->data, count, write_bulk_callback, dev);
739         if ((res = usb_submit_urb(dev->tx_urb, GFP_ATOMIC))) {
740                 /* Can we get/handle EPIPE here? */
741                 if (res == -ENODEV)
742                         netif_device_detach(dev->netdev);
743                 else {
744                         dev_warn(&netdev->dev, "failed tx_urb %d\n", res);
745                         netdev->stats.tx_errors++;
746                         netif_start_queue(netdev);
747                 }
748         } else {
749                 netdev->stats.tx_packets++;
750                 netdev->stats.tx_bytes += skb->len;
751                 netdev->trans_start = jiffies;
752         }
753
754         return NETDEV_TX_OK;
755 }
756
757
758 static void set_carrier(struct net_device *netdev)
759 {
760         rtl8150_t *dev = netdev_priv(netdev);
761         short tmp;
762
763         get_registers(dev, CSCR, 2, &tmp);
764         if (tmp & CSCR_LINK_STATUS)
765                 netif_carrier_on(netdev);
766         else
767                 netif_carrier_off(netdev);
768 }
769
770 static int rtl8150_open(struct net_device *netdev)
771 {
772         rtl8150_t *dev = netdev_priv(netdev);
773         int res;
774
775         if (dev->rx_skb == NULL)
776                 dev->rx_skb = pull_skb(dev);
777         if (!dev->rx_skb)
778                 return -ENOMEM;
779
780         set_registers(dev, IDR, 6, netdev->dev_addr);
781
782         usb_fill_bulk_urb(dev->rx_urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
783                       dev->rx_skb->data, RTL8150_MTU, read_bulk_callback, dev);
784         if ((res = usb_submit_urb(dev->rx_urb, GFP_KERNEL))) {
785                 if (res == -ENODEV)
786                         netif_device_detach(dev->netdev);
787                 dev_warn(&netdev->dev, "rx_urb submit failed: %d\n", res);
788                 return res;
789         }
790         usb_fill_int_urb(dev->intr_urb, dev->udev, usb_rcvintpipe(dev->udev, 3),
791                      dev->intr_buff, INTBUFSIZE, intr_callback,
792                      dev, dev->intr_interval);
793         if ((res = usb_submit_urb(dev->intr_urb, GFP_KERNEL))) {
794                 if (res == -ENODEV)
795                         netif_device_detach(dev->netdev);
796                 dev_warn(&netdev->dev, "intr_urb submit failed: %d\n", res);
797                 usb_kill_urb(dev->rx_urb);
798                 return res;
799         }
800         enable_net_traffic(dev);
801         set_carrier(netdev);
802         netif_start_queue(netdev);
803
804         return res;
805 }
806
807 static int rtl8150_close(struct net_device *netdev)
808 {
809         rtl8150_t *dev = netdev_priv(netdev);
810         int res = 0;
811
812         netif_stop_queue(netdev);
813         if (!test_bit(RTL8150_UNPLUG, &dev->flags))
814                 disable_net_traffic(dev);
815         unlink_all_urbs(dev);
816
817         return res;
818 }
819
820 static void rtl8150_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *info)
821 {
822         rtl8150_t *dev = netdev_priv(netdev);
823
824         strncpy(info->driver, driver_name, ETHTOOL_BUSINFO_LEN);
825         strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
826         usb_make_path(dev->udev, info->bus_info, sizeof info->bus_info);
827 }
828
829 static int rtl8150_get_settings(struct net_device *netdev, struct ethtool_cmd *ecmd)
830 {
831         rtl8150_t *dev = netdev_priv(netdev);
832         short lpa, bmcr;
833
834         ecmd->supported = (SUPPORTED_10baseT_Half |
835                           SUPPORTED_10baseT_Full |
836                           SUPPORTED_100baseT_Half |
837                           SUPPORTED_100baseT_Full |
838                           SUPPORTED_Autoneg |
839                           SUPPORTED_TP | SUPPORTED_MII);
840         ecmd->port = PORT_TP;
841         ecmd->transceiver = XCVR_INTERNAL;
842         ecmd->phy_address = dev->phy;
843         get_registers(dev, BMCR, 2, &bmcr);
844         get_registers(dev, ANLP, 2, &lpa);
845         if (bmcr & BMCR_ANENABLE) {
846                 u32 speed = ((lpa & (LPA_100HALF | LPA_100FULL)) ?
847                              SPEED_100 : SPEED_10);
848                 ethtool_cmd_speed_set(ecmd, speed);
849                 ecmd->autoneg = AUTONEG_ENABLE;
850                 if (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                 ethtool_cmd_speed_set(ecmd, ((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 const 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 const struct net_device_ops rtl8150_netdev_ops = {
897         .ndo_open               = rtl8150_open,
898         .ndo_stop               = rtl8150_close,
899         .ndo_do_ioctl           = rtl8150_ioctl,
900         .ndo_start_xmit         = rtl8150_start_xmit,
901         .ndo_tx_timeout         = rtl8150_tx_timeout,
902         .ndo_set_rx_mode        = rtl8150_set_multicast,
903         .ndo_set_mac_address    = rtl8150_set_mac_address,
904
905         .ndo_change_mtu         = eth_change_mtu,
906         .ndo_validate_addr      = eth_validate_addr,
907 };
908
909 static int rtl8150_probe(struct usb_interface *intf,
910                          const struct usb_device_id *id)
911 {
912         struct usb_device *udev = interface_to_usbdev(intf);
913         rtl8150_t *dev;
914         struct net_device *netdev;
915
916         netdev = alloc_etherdev(sizeof(rtl8150_t));
917         if (!netdev) {
918                 err("Out of memory");
919                 return -ENOMEM;
920         }
921
922         dev = netdev_priv(netdev);
923
924         dev->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL);
925         if (!dev->intr_buff) {
926                 free_netdev(netdev);
927                 return -ENOMEM;
928         }
929
930         tasklet_init(&dev->tl, rx_fixup, (unsigned long)dev);
931         spin_lock_init(&dev->rx_pool_lock);
932
933         dev->udev = udev;
934         dev->netdev = netdev;
935         netdev->netdev_ops = &rtl8150_netdev_ops;
936         netdev->watchdog_timeo = RTL8150_TX_TIMEOUT;
937         SET_ETHTOOL_OPS(netdev, &ops);
938         dev->intr_interval = 100;       /* 100ms */
939
940         if (!alloc_all_urbs(dev)) {
941                 err("out of memory");
942                 goto out;
943         }
944         if (!rtl8150_reset(dev)) {
945                 err("couldn't reset the device");
946                 goto out1;
947         }
948         fill_skb_pool(dev);
949         set_ethernet_addr(dev);
950
951         usb_set_intfdata(intf, dev);
952         SET_NETDEV_DEV(netdev, &intf->dev);
953         if (register_netdev(netdev) != 0) {
954                 err("couldn't register the device");
955                 goto out2;
956         }
957
958         dev_info(&intf->dev, "%s: rtl8150 is detected\n", netdev->name);
959
960         return 0;
961
962 out2:
963         usb_set_intfdata(intf, NULL);
964         free_skb_pool(dev);
965 out1:
966         free_all_urbs(dev);
967 out:
968         kfree(dev->intr_buff);
969         free_netdev(netdev);
970         return -EIO;
971 }
972
973 static void rtl8150_disconnect(struct usb_interface *intf)
974 {
975         rtl8150_t *dev = usb_get_intfdata(intf);
976
977         usb_set_intfdata(intf, NULL);
978         if (dev) {
979                 set_bit(RTL8150_UNPLUG, &dev->flags);
980                 tasklet_kill(&dev->tl);
981                 unregister_netdev(dev->netdev);
982                 unlink_all_urbs(dev);
983                 free_all_urbs(dev);
984                 free_skb_pool(dev);
985                 if (dev->rx_skb)
986                         dev_kfree_skb(dev->rx_skb);
987                 kfree(dev->intr_buff);
988                 free_netdev(dev->netdev);
989         }
990 }
991
992 static struct usb_driver rtl8150_driver = {
993         .name           = driver_name,
994         .probe          = rtl8150_probe,
995         .disconnect     = rtl8150_disconnect,
996         .id_table       = rtl8150_table,
997         .suspend        = rtl8150_suspend,
998         .resume         = rtl8150_resume
999 };
1000
1001 static int __init usb_rtl8150_init(void)
1002 {
1003         printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1004                DRIVER_DESC "\n");
1005         return usb_register(&rtl8150_driver);
1006 }
1007
1008 static void __exit usb_rtl8150_exit(void)
1009 {
1010         usb_deregister(&rtl8150_driver);
1011 }
1012
1013 module_init(usb_rtl8150_init);
1014 module_exit(usb_rtl8150_exit);
1015
1016 MODULE_AUTHOR(DRIVER_AUTHOR);
1017 MODULE_DESCRIPTION(DRIVER_DESC);
1018 MODULE_LICENSE("GPL");