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