blob: e0eecda78ec12cac845fac5831742643db94688f [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 */
Petko Manolov58592712006-12-04 14:27:36 +0200127#define VENDOR_ID_REALTEK 0x0bda
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128#define VENDOR_ID_MELCO 0x0411
Petko Manolov58592712006-12-04 14:27:36 +0200129#define VENDOR_ID_MICRONET 0x3980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130#define VENDOR_ID_LONGSHINE 0x07b8
Petko Manolov58592712006-12-04 14:27:36 +0200131#define VENDOR_ID_OQO 0x1557
Dan Streetmanb6c27992006-07-05 19:17:27 -0400132#define VENDOR_ID_ZYXEL 0x0586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134#define PRODUCT_ID_RTL8150 0x8150
135#define PRODUCT_ID_LUAKTX 0x0012
136#define PRODUCT_ID_LCS8138TX 0x401a
137#define PRODUCT_ID_SP128AR 0x0003
Dan Streetmanb6c27992006-07-05 19:17:27 -0400138#define PRODUCT_ID_PRESTIGE 0x401a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140#undef EEPROM_WRITE
141
142/* table of devices that work with this driver */
143static struct usb_device_id rtl8150_table[] = {
144 {USB_DEVICE(VENDOR_ID_REALTEK, PRODUCT_ID_RTL8150)},
145 {USB_DEVICE(VENDOR_ID_MELCO, PRODUCT_ID_LUAKTX)},
146 {USB_DEVICE(VENDOR_ID_MICRONET, PRODUCT_ID_SP128AR)},
147 {USB_DEVICE(VENDOR_ID_LONGSHINE, PRODUCT_ID_LCS8138TX)},
Petko Manolov58592712006-12-04 14:27:36 +0200148 {USB_DEVICE(VENDOR_ID_OQO, PRODUCT_ID_RTL8150)},
Dan Streetmanb6c27992006-07-05 19:17:27 -0400149 {USB_DEVICE(VENDOR_ID_ZYXEL, PRODUCT_ID_PRESTIGE)},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 {}
151};
152
153MODULE_DEVICE_TABLE(usb, rtl8150_table);
154
155struct rtl8150 {
156 unsigned long flags;
157 struct usb_device *udev;
158 struct tasklet_struct tl;
159 struct net_device_stats stats;
160 struct net_device *netdev;
161 struct urb *rx_urb, *tx_urb, *intr_urb, *ctrl_urb;
162 struct sk_buff *tx_skb, *rx_skb;
163 struct sk_buff *rx_skb_pool[RX_SKB_POOL_SIZE];
164 spinlock_t rx_pool_lock;
165 struct usb_ctrlrequest dr;
166 int intr_interval;
167 __le16 rx_creg;
168 u8 *intr_buff;
169 u8 phy;
170};
171
172typedef struct rtl8150 rtl8150_t;
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174static void fill_skb_pool(rtl8150_t *);
175static void free_skb_pool(rtl8150_t *);
176static inline struct sk_buff *pull_skb(rtl8150_t *);
177static void rtl8150_disconnect(struct usb_interface *intf);
178static int rtl8150_probe(struct usb_interface *intf,
179 const struct usb_device_id *id);
Peter Chubb23219c12006-07-25 20:39:14 +1000180static int rtl8150_suspend(struct usb_interface *intf, pm_message_t message);
181static int rtl8150_resume(struct usb_interface *intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183static const char driver_name [] = "rtl8150";
184
185static struct usb_driver rtl8150_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 .name = driver_name,
187 .probe = rtl8150_probe,
188 .disconnect = rtl8150_disconnect,
189 .id_table = rtl8150_table,
Peter Chubb23219c12006-07-25 20:39:14 +1000190 .suspend = rtl8150_suspend,
191 .resume = rtl8150_resume
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192};
193
194/*
195**
196** device related part of the code
197**
198*/
199static int get_registers(rtl8150_t * dev, u16 indx, u16 size, void *data)
200{
201 return usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
202 RTL8150_REQ_GET_REGS, RTL8150_REQT_READ,
203 indx, 0, data, size, 500);
204}
205
206static int set_registers(rtl8150_t * dev, u16 indx, u16 size, void *data)
207{
208 return usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
209 RTL8150_REQ_SET_REGS, RTL8150_REQT_WRITE,
210 indx, 0, data, size, 500);
211}
212
David Howells7d12e782006-10-05 14:55:46 +0100213static void ctrl_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
215 rtl8150_t *dev;
216
217 switch (urb->status) {
218 case 0:
219 break;
220 case -EINPROGRESS:
221 break;
222 case -ENOENT:
223 break;
224 default:
225 warn("ctrl urb status %d", urb->status);
226 }
227 dev = urb->context;
228 clear_bit(RX_REG_SET, &dev->flags);
229}
230
231static int async_set_registers(rtl8150_t * dev, u16 indx, u16 size)
232{
233 int ret;
234
235 if (test_bit(RX_REG_SET, &dev->flags))
236 return -EAGAIN;
237
238 dev->dr.bRequestType = RTL8150_REQT_WRITE;
239 dev->dr.bRequest = RTL8150_REQ_SET_REGS;
240 dev->dr.wValue = cpu_to_le16(indx);
241 dev->dr.wIndex = 0;
242 dev->dr.wLength = cpu_to_le16(size);
243 dev->ctrl_urb->transfer_buffer_length = size;
244 usb_fill_control_urb(dev->ctrl_urb, dev->udev,
245 usb_sndctrlpipe(dev->udev, 0), (char *) &dev->dr,
246 &dev->rx_creg, size, ctrl_callback, dev);
Peter Chubb23219c12006-07-25 20:39:14 +1000247 if ((ret = usb_submit_urb(dev->ctrl_urb, GFP_ATOMIC))) {
248 if (ret == -ENODEV)
249 netif_device_detach(dev->netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 err("control request submission failed: %d", ret);
Peter Chubb23219c12006-07-25 20:39:14 +1000251 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 set_bit(RX_REG_SET, &dev->flags);
253
254 return ret;
255}
256
257static int read_mii_word(rtl8150_t * dev, u8 phy, __u8 indx, u16 * reg)
258{
259 int i;
260 u8 data[3], tmp;
261
262 data[0] = phy;
263 data[1] = data[2] = 0;
264 tmp = indx | PHY_READ | PHY_GO;
265 i = 0;
266
267 set_registers(dev, PHYADD, sizeof(data), data);
268 set_registers(dev, PHYCNT, 1, &tmp);
269 do {
270 get_registers(dev, PHYCNT, 1, data);
271 } while ((data[0] & PHY_GO) && (i++ < MII_TIMEOUT));
272
273 if (i < MII_TIMEOUT) {
274 get_registers(dev, PHYDAT, 2, data);
275 *reg = data[0] | (data[1] << 8);
276 return 0;
277 } else
278 return 1;
279}
280
281static int write_mii_word(rtl8150_t * dev, u8 phy, __u8 indx, u16 reg)
282{
283 int i;
284 u8 data[3], tmp;
285
286 data[0] = phy;
287 *(data + 1) = cpu_to_le16p(&reg);
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
303static 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
311static 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
349static 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
362static 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->intr_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
389static 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
397static 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
405static 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
David Howells7d12e782006-10-05 14:55:46 +0100420static void read_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
422 rtl8150_t *dev;
423 unsigned pkt_len, res;
424 struct sk_buff *skb;
425 struct net_device *netdev;
426 u16 rx_stat;
Peter Chubb23219c12006-07-25 20:39:14 +1000427 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
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 */
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700443 case -ETIME:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 warn("may be reset is needed?..");
445 goto goon;
446 default:
447 warn("Rx status %d", 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;
474goon:
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);
Peter Chubb23219c12006-07-25 20:39:14 +1000477 status = usb_submit_urb(dev->rx_urb, GFP_ATOMIC);
478 if (status == -ENODEV)
479 netif_device_detach(dev->netdev);
480 else if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 set_bit(RX_URB_FAIL, &dev->flags);
482 goto resched;
483 } else {
484 clear_bit(RX_URB_FAIL, &dev->flags);
485 }
486
487 return;
488resched:
489 tasklet_schedule(&dev->tl);
490}
491
492static void rx_fixup(unsigned long data)
493{
494 rtl8150_t *dev;
495 struct sk_buff *skb;
Peter Chubb23219c12006-07-25 20:39:14 +1000496 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
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);
514try_again:
Peter Chubb23219c12006-07-25 20:39:14 +1000515 status = usb_submit_urb(dev->rx_urb, GFP_ATOMIC);
516 if (status == -ENODEV) {
517 netif_device_detach(dev->netdev);
518 } else if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 set_bit(RX_URB_FAIL, &dev->flags);
520 goto tlsched;
Peter Chubb23219c12006-07-25 20:39:14 +1000521 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 clear_bit(RX_URB_FAIL, &dev->flags);
523 }
524
525 return;
526tlsched:
527 tasklet_schedule(&dev->tl);
528}
529
David Howells7d12e782006-10-05 14:55:46 +0100530static void write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
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 info("%s: Tx status %d", dev->netdev->name, urb->status);
542 dev->netdev->trans_start = jiffies;
543 netif_wake_queue(dev->netdev);
544}
545
David Howells7d12e782006-10-05 14:55:46 +0100546static void intr_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
548 rtl8150_t *dev;
549 __u8 *d;
550 int status;
551
552 dev = urb->context;
553 if (!dev)
554 return;
555 switch (urb->status) {
556 case 0: /* success */
557 break;
558 case -ECONNRESET: /* unlink */
559 case -ENOENT:
560 case -ESHUTDOWN:
561 return;
562 /* -EPIPE: should clear the halt */
563 default:
564 info("%s: intr status %d", dev->netdev->name, urb->status);
565 goto resubmit;
566 }
567
568 d = urb->transfer_buffer;
569 if (d[0] & TSR_ERRORS) {
570 dev->stats.tx_errors++;
571 if (d[INT_TSR] & (TSR_ECOL | TSR_JBR))
572 dev->stats.tx_aborted_errors++;
573 if (d[INT_TSR] & TSR_LCOL)
574 dev->stats.tx_window_errors++;
575 if (d[INT_TSR] & TSR_LOSS_CRS)
576 dev->stats.tx_carrier_errors++;
577 }
578 /* Report link status changes to the network stack */
579 if ((d[INT_MSR] & MSR_LINK) == 0) {
580 if (netif_carrier_ok(dev->netdev)) {
581 netif_carrier_off(dev->netdev);
582 dbg("%s: LINK LOST\n", __func__);
583 }
584 } else {
585 if (!netif_carrier_ok(dev->netdev)) {
586 netif_carrier_on(dev->netdev);
587 dbg("%s: LINK CAME BACK\n", __func__);
588 }
589 }
590
591resubmit:
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800592 status = usb_submit_urb (urb, GFP_ATOMIC);
Peter Chubb23219c12006-07-25 20:39:14 +1000593 if (status == -ENODEV)
594 netif_device_detach(dev->netdev);
595 else if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 err ("can't resubmit intr, %s-%s/input0, status %d",
597 dev->udev->bus->bus_name,
598 dev->udev->devpath, status);
599}
600
Peter Chubb23219c12006-07-25 20:39:14 +1000601static int rtl8150_suspend(struct usb_interface *intf, pm_message_t message)
602{
603 rtl8150_t *dev = usb_get_intfdata(intf);
604
605 netif_device_detach(dev->netdev);
606
607 if (netif_running(dev->netdev)) {
608 usb_kill_urb(dev->rx_urb);
609 usb_kill_urb(dev->intr_urb);
610 }
611 return 0;
612}
613
614static int rtl8150_resume(struct usb_interface *intf)
615{
616 rtl8150_t *dev = usb_get_intfdata(intf);
617
618 netif_device_attach(dev->netdev);
619 if (netif_running(dev->netdev)) {
620 dev->rx_urb->status = 0;
621 dev->rx_urb->actual_length = 0;
David Howells7d12e782006-10-05 14:55:46 +0100622 read_bulk_callback(dev->rx_urb);
Peter Chubb23219c12006-07-25 20:39:14 +1000623
624 dev->intr_urb->status = 0;
625 dev->intr_urb->actual_length = 0;
David Howells7d12e782006-10-05 14:55:46 +0100626 intr_callback(dev->intr_urb);
Peter Chubb23219c12006-07-25 20:39:14 +1000627 }
628 return 0;
629}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631/*
632**
633** network related part of the code
634**
635*/
636
637static void fill_skb_pool(rtl8150_t *dev)
638{
639 struct sk_buff *skb;
640 int i;
641
642 for (i = 0; i < RX_SKB_POOL_SIZE; i++) {
643 if (dev->rx_skb_pool[i])
644 continue;
645 skb = dev_alloc_skb(RTL8150_MTU + 2);
646 if (!skb) {
647 return;
648 }
649 skb->dev = dev->netdev;
650 skb_reserve(skb, 2);
651 dev->rx_skb_pool[i] = skb;
652 }
653}
654
655static void free_skb_pool(rtl8150_t *dev)
656{
657 int i;
658
659 for (i = 0; i < RX_SKB_POOL_SIZE; i++)
660 if (dev->rx_skb_pool[i])
661 dev_kfree_skb(dev->rx_skb_pool[i]);
662}
663
664static int enable_net_traffic(rtl8150_t * dev)
665{
666 u8 cr, tcr, rcr, msr;
667
668 if (!rtl8150_reset(dev)) {
669 warn("%s - device reset failed", __FUNCTION__);
670 }
671 /* RCR bit7=1 attach Rx info at the end; =0 HW CRC (which is broken) */
672 rcr = 0x9e;
673 dev->rx_creg = cpu_to_le16(rcr);
674 tcr = 0xd8;
675 cr = 0x0c;
676 if (!(rcr & 0x80))
677 set_bit(RTL8150_HW_CRC, &dev->flags);
678 set_registers(dev, RCR, 1, &rcr);
679 set_registers(dev, TCR, 1, &tcr);
680 set_registers(dev, CR, 1, &cr);
681 get_registers(dev, MSR, 1, &msr);
682
683 return 0;
684}
685
686static void disable_net_traffic(rtl8150_t * dev)
687{
688 u8 cr;
689
690 get_registers(dev, CR, 1, &cr);
691 cr &= 0xf3;
692 set_registers(dev, CR, 1, &cr);
693}
694
695static struct net_device_stats *rtl8150_netdev_stats(struct net_device *dev)
696{
697 return &((rtl8150_t *)netdev_priv(dev))->stats;
698}
699
700static void rtl8150_tx_timeout(struct net_device *netdev)
701{
702 rtl8150_t *dev = netdev_priv(netdev);
703 warn("%s: Tx timeout.", netdev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 usb_unlink_urb(dev->tx_urb);
705 dev->stats.tx_errors++;
706}
707
708static void rtl8150_set_multicast(struct net_device *netdev)
709{
710 rtl8150_t *dev = netdev_priv(netdev);
711 netif_stop_queue(netdev);
712 if (netdev->flags & IFF_PROMISC) {
713 dev->rx_creg |= cpu_to_le16(0x0001);
714 info("%s: promiscuous mode", netdev->name);
YOSHIFUJI Hideakiae0a97b2005-05-25 16:07:04 +0900715 } else if (netdev->mc_count ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 (netdev->flags & IFF_ALLMULTI)) {
717 dev->rx_creg &= cpu_to_le16(0xfffe);
718 dev->rx_creg |= cpu_to_le16(0x0002);
719 info("%s: allmulti set", netdev->name);
720 } else {
721 /* ~RX_MULTICAST, ~RX_PROMISCUOUS */
722 dev->rx_creg &= cpu_to_le16(0x00fc);
723 }
724 async_set_registers(dev, RCR, 2);
725 netif_wake_queue(netdev);
726}
727
728static int rtl8150_start_xmit(struct sk_buff *skb, 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))) {
Peter Chubb23219c12006-07-25 20:39:14 +1000740 /* Can we get/handle EPIPE here? */
741 if (res == -ENODEV)
742 netif_device_detach(dev->netdev);
743 else {
744 warn("failed tx_urb %d\n", res);
745 dev->stats.tx_errors++;
746 netif_start_queue(netdev);
747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 } else {
749 dev->stats.tx_packets++;
750 dev->stats.tx_bytes += skb->len;
751 netdev->trans_start = jiffies;
752 }
753
754 return 0;
755}
756
757
758static 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
770static 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);
Peter Chubb23219c12006-07-25 20:39:14 +1000784 if ((res = usb_submit_urb(dev->rx_urb, GFP_KERNEL))) {
785 if (res == -ENODEV)
786 netif_device_detach(dev->netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 warn("%s: rx_urb submit failed: %d", __FUNCTION__, res);
Peter Chubb23219c12006-07-25 20:39:14 +1000788 return res;
789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 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);
Peter Chubb23219c12006-07-25 20:39:14 +1000793 if ((res = usb_submit_urb(dev->intr_urb, GFP_KERNEL))) {
794 if (res == -ENODEV)
795 netif_device_detach(dev->netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 warn("%s: intr_urb submit failed: %d", __FUNCTION__, res);
Peter Chubb23219c12006-07-25 20:39:14 +1000797 usb_kill_urb(dev->rx_urb);
798 return res;
799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 enable_net_traffic(dev);
801 set_carrier(netdev);
Peter Chubb23219c12006-07-25 20:39:14 +1000802 netif_start_queue(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 return res;
805}
806
807static 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
820static 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
829static 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 ecmd->autoneg = AUTONEG_ENABLE;
847 ecmd->speed = (lpa & (LPA_100HALF | LPA_100FULL)) ?
848 SPEED_100 : SPEED_10;
849 if (ecmd->speed == SPEED_100)
850 ecmd->duplex = (lpa & LPA_100FULL) ?
851 DUPLEX_FULL : DUPLEX_HALF;
852 else
853 ecmd->duplex = (lpa & LPA_10FULL) ?
854 DUPLEX_FULL : DUPLEX_HALF;
855 } else {
856 ecmd->autoneg = AUTONEG_DISABLE;
857 ecmd->speed = (bmcr & BMCR_SPEED100) ?
858 SPEED_100 : SPEED_10;
859 ecmd->duplex = (bmcr & BMCR_FULLDPLX) ?
860 DUPLEX_FULL : DUPLEX_HALF;
861 }
862 return 0;
863}
864
865static struct ethtool_ops ops = {
866 .get_drvinfo = rtl8150_get_drvinfo,
867 .get_settings = rtl8150_get_settings,
868 .get_link = ethtool_op_get_link
869};
870
871static int rtl8150_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
872{
873 rtl8150_t *dev = netdev_priv(netdev);
874 u16 *data = (u16 *) & rq->ifr_ifru;
875 int res = 0;
876
877 switch (cmd) {
878 case SIOCDEVPRIVATE:
879 data[0] = dev->phy;
880 case SIOCDEVPRIVATE + 1:
881 read_mii_word(dev, dev->phy, (data[1] & 0x1f), &data[3]);
882 break;
883 case SIOCDEVPRIVATE + 2:
884 if (!capable(CAP_NET_ADMIN))
885 return -EPERM;
886 write_mii_word(dev, dev->phy, (data[1] & 0x1f), data[2]);
887 break;
888 default:
889 res = -EOPNOTSUPP;
890 }
891
892 return res;
893}
894
895static int rtl8150_probe(struct usb_interface *intf,
896 const struct usb_device_id *id)
897{
898 struct usb_device *udev = interface_to_usbdev(intf);
899 rtl8150_t *dev;
900 struct net_device *netdev;
901
902 netdev = alloc_etherdev(sizeof(rtl8150_t));
903 if (!netdev) {
904 err("Out of memory");
905 return -ENOMEM;
906 }
907
908 dev = netdev_priv(netdev);
909 memset(dev, 0, sizeof(rtl8150_t));
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 SET_MODULE_OWNER(netdev);
923 netdev->open = rtl8150_open;
924 netdev->stop = rtl8150_close;
925 netdev->do_ioctl = rtl8150_ioctl;
926 netdev->watchdog_timeo = RTL8150_TX_TIMEOUT;
927 netdev->tx_timeout = rtl8150_tx_timeout;
928 netdev->hard_start_xmit = rtl8150_start_xmit;
929 netdev->set_multicast_list = rtl8150_set_multicast;
930 netdev->set_mac_address = rtl8150_set_mac_address;
931 netdev->get_stats = rtl8150_netdev_stats;
932 netdev->mtu = RTL8150_MTU;
933 SET_ETHTOOL_OPS(netdev, &ops);
934 dev->intr_interval = 100; /* 100ms */
935
936 if (!alloc_all_urbs(dev)) {
937 err("out of memory");
938 goto out;
939 }
940 if (!rtl8150_reset(dev)) {
941 err("couldn't reset the device");
942 goto out1;
943 }
944 fill_skb_pool(dev);
945 set_ethernet_addr(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947 usb_set_intfdata(intf, dev);
948 SET_NETDEV_DEV(netdev, &intf->dev);
949 if (register_netdev(netdev) != 0) {
950 err("couldn't register the device");
951 goto out2;
952 }
Petko Manolov09abfa82006-03-15 16:29:38 +0200953
954 info("%s: rtl8150 is detected", netdev->name);
955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 return 0;
957
958out2:
959 usb_set_intfdata(intf, NULL);
960 free_skb_pool(dev);
961out1:
962 free_all_urbs(dev);
963out:
964 kfree(dev->intr_buff);
965 free_netdev(netdev);
966 return -EIO;
967}
968
969static void rtl8150_disconnect(struct usb_interface *intf)
970{
971 rtl8150_t *dev = usb_get_intfdata(intf);
972
973 usb_set_intfdata(intf, NULL);
974 if (dev) {
975 set_bit(RTL8150_UNPLUG, &dev->flags);
Greg Kroah-Hartman8da608c2005-06-20 21:15:16 -0700976 tasklet_disable(&dev->tl);
Andrew Mortoneff674a2006-08-14 23:11:09 -0700977 tasklet_kill(&dev->tl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 unregister_netdev(dev->netdev);
979 unlink_all_urbs(dev);
980 free_all_urbs(dev);
981 free_skb_pool(dev);
982 if (dev->rx_skb)
983 dev_kfree_skb(dev->rx_skb);
984 kfree(dev->intr_buff);
985 free_netdev(dev->netdev);
986 }
987}
988
989static int __init usb_rtl8150_init(void)
990{
991 info(DRIVER_DESC " " DRIVER_VERSION);
992 return usb_register(&rtl8150_driver);
993}
994
995static void __exit usb_rtl8150_exit(void)
996{
997 usb_deregister(&rtl8150_driver);
998}
999
1000module_init(usb_rtl8150_init);
1001module_exit(usb_rtl8150_exit);
1002
1003MODULE_AUTHOR(DRIVER_AUTHOR);
1004MODULE_DESCRIPTION(DRIVER_DESC);
1005MODULE_LICENSE("GPL");