blob: d9f84f22fbc7cc33d242c65a839d172878dc80cf [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/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 */
Petko Manolov58592712006-12-04 14:27:36 +0200126#define VENDOR_ID_REALTEK 0x0bda
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127#define VENDOR_ID_MELCO 0x0411
Petko Manolov58592712006-12-04 14:27:36 +0200128#define VENDOR_ID_MICRONET 0x3980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129#define VENDOR_ID_LONGSHINE 0x07b8
Petko Manolov58592712006-12-04 14:27:36 +0200130#define VENDOR_ID_OQO 0x1557
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)},
Petko Manolov58592712006-12-04 14:27:36 +0200147 {USB_DEVICE(VENDOR_ID_OQO, PRODUCT_ID_RTL8150)},
Dan Streetmanb6c27992006-07-05 19:17:27 -0400148 {USB_DEVICE(VENDOR_ID_ZYXEL, PRODUCT_ID_PRESTIGE)},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 {}
150};
151
152MODULE_DEVICE_TABLE(usb, rtl8150_table);
153
154struct rtl8150 {
155 unsigned long flags;
156 struct usb_device *udev;
157 struct tasklet_struct tl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 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;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800214 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Oliver Neukumc94cb312008-12-18 23:00:59 -0800216 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 case 0:
218 break;
219 case -EINPROGRESS:
220 break;
221 case -ENOENT:
222 break;
223 default:
André Goddard Rosa342a4372009-05-29 22:13:58 -0700224 if (printk_ratelimit())
225 dev_warn(&urb->dev->dev, "ctrl urb status %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 }
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;
Al Viro886ae1f2007-02-04 03:02:17 +0000287 data[1] = reg & 0xff;
288 data[2] = (reg >> 8) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 tmp = indx | PHY_WRITE | PHY_GO;
290 i = 0;
291
292 set_registers(dev, PHYADD, sizeof(data), data);
293 set_registers(dev, PHYCNT, 1, &tmp);
294 do {
295 get_registers(dev, PHYCNT, 1, data);
296 } while ((data[0] & PHY_GO) && (i++ < MII_TIMEOUT));
297
298 if (i < MII_TIMEOUT)
299 return 0;
300 else
301 return 1;
302}
303
304static inline void set_ethernet_addr(rtl8150_t * dev)
305{
306 u8 node_id[6];
307
308 get_registers(dev, IDR, sizeof(node_id), node_id);
309 memcpy(dev->netdev->dev_addr, node_id, sizeof(node_id));
310}
311
312static int rtl8150_set_mac_address(struct net_device *netdev, void *p)
313{
314 struct sockaddr *addr = p;
315 rtl8150_t *dev = netdev_priv(netdev);
316 int i;
317
318 if (netif_running(netdev))
319 return -EBUSY;
320
321 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
322 dbg("%s: Setting MAC address to ", netdev->name);
323 for (i = 0; i < 5; i++)
324 dbg("%02X:", netdev->dev_addr[i]);
325 dbg("%02X\n", netdev->dev_addr[i]);
326 /* Set the IDR registers. */
327 set_registers(dev, IDR, sizeof(netdev->dev_addr), netdev->dev_addr);
328#ifdef EEPROM_WRITE
329 {
330 u8 cr;
331 /* Get the CR contents. */
332 get_registers(dev, CR, 1, &cr);
333 /* Set the WEPROM bit (eeprom write enable). */
334 cr |= 0x20;
335 set_registers(dev, CR, 1, &cr);
336 /* Write the MAC address into eeprom. Eeprom writes must be word-sized,
337 so we need to split them up. */
338 for (i = 0; i * 2 < netdev->addr_len; i++) {
339 set_registers(dev, IDR_EEPROM + (i * 2), 2,
340 netdev->dev_addr + (i * 2));
341 }
342 /* Clear the WEPROM bit (preventing accidental eeprom writes). */
343 cr &= 0xdf;
344 set_registers(dev, CR, 1, &cr);
345 }
346#endif
347 return 0;
348}
349
350static int rtl8150_reset(rtl8150_t * dev)
351{
352 u8 data = 0x10;
353 int i = HZ;
354
355 set_registers(dev, CR, 1, &data);
356 do {
357 get_registers(dev, CR, 1, &data);
358 } while ((data & 0x10) && --i);
359
360 return (i > 0) ? 1 : 0;
361}
362
363static int alloc_all_urbs(rtl8150_t * dev)
364{
365 dev->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
366 if (!dev->rx_urb)
367 return 0;
368 dev->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
369 if (!dev->tx_urb) {
370 usb_free_urb(dev->rx_urb);
371 return 0;
372 }
373 dev->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
374 if (!dev->intr_urb) {
375 usb_free_urb(dev->rx_urb);
376 usb_free_urb(dev->tx_urb);
377 return 0;
378 }
379 dev->ctrl_urb = usb_alloc_urb(0, GFP_KERNEL);
Robert P. J. Day7fdba2f2008-03-09 20:44:52 -0400380 if (!dev->ctrl_urb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 usb_free_urb(dev->rx_urb);
382 usb_free_urb(dev->tx_urb);
383 usb_free_urb(dev->intr_urb);
384 return 0;
385 }
386
387 return 1;
388}
389
390static void free_all_urbs(rtl8150_t * dev)
391{
392 usb_free_urb(dev->rx_urb);
393 usb_free_urb(dev->tx_urb);
394 usb_free_urb(dev->intr_urb);
395 usb_free_urb(dev->ctrl_urb);
396}
397
398static void unlink_all_urbs(rtl8150_t * dev)
399{
400 usb_kill_urb(dev->rx_urb);
401 usb_kill_urb(dev->tx_urb);
402 usb_kill_urb(dev->intr_urb);
403 usb_kill_urb(dev->ctrl_urb);
404}
405
406static inline struct sk_buff *pull_skb(rtl8150_t *dev)
407{
408 struct sk_buff *skb;
409 int i;
410
411 for (i = 0; i < RX_SKB_POOL_SIZE; i++) {
412 if (dev->rx_skb_pool[i]) {
413 skb = dev->rx_skb_pool[i];
414 dev->rx_skb_pool[i] = NULL;
415 return skb;
416 }
417 }
418 return NULL;
419}
420
David Howells7d12e782006-10-05 14:55:46 +0100421static void read_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
423 rtl8150_t *dev;
424 unsigned pkt_len, res;
425 struct sk_buff *skb;
426 struct net_device *netdev;
427 u16 rx_stat;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800428 int status = urb->status;
429 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 dev = urb->context;
432 if (!dev)
433 return;
434 if (test_bit(RTL8150_UNPLUG, &dev->flags))
435 return;
436 netdev = dev->netdev;
437 if (!netif_device_present(netdev))
438 return;
439
Oliver Neukumc94cb312008-12-18 23:00:59 -0800440 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 case 0:
442 break;
443 case -ENOENT:
444 return; /* the urb is in unlink state */
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700445 case -ETIME:
André Goddard Rosa342a4372009-05-29 22:13:58 -0700446 if (printk_ratelimit())
447 dev_warn(&urb->dev->dev, "may be reset is needed?..\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 goto goon;
449 default:
André Goddard Rosa342a4372009-05-29 22:13:58 -0700450 if (printk_ratelimit())
451 dev_warn(&urb->dev->dev, "Rx status %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 goto goon;
453 }
454
455 if (!dev->rx_skb)
456 goto resched;
457 /* protect against short packets (tell me why we got some?!?) */
458 if (urb->actual_length < 4)
459 goto goon;
460
461 res = urb->actual_length;
462 rx_stat = le16_to_cpu(*(__le16 *)(urb->transfer_buffer + res - 4));
463 pkt_len = res - 4;
464
465 skb_put(dev->rx_skb, pkt_len);
466 dev->rx_skb->protocol = eth_type_trans(dev->rx_skb, netdev);
467 netif_rx(dev->rx_skb);
Stephen Hemmingerb7e41e22009-03-20 19:35:50 +0000468 netdev->stats.rx_packets++;
469 netdev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 spin_lock(&dev->rx_pool_lock);
472 skb = pull_skb(dev);
473 spin_unlock(&dev->rx_pool_lock);
474 if (!skb)
475 goto resched;
476
477 dev->rx_skb = skb;
478goon:
479 usb_fill_bulk_urb(dev->rx_urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
480 dev->rx_skb->data, RTL8150_MTU, read_bulk_callback, dev);
Oliver Neukumc94cb312008-12-18 23:00:59 -0800481 result = usb_submit_urb(dev->rx_urb, GFP_ATOMIC);
482 if (result == -ENODEV)
Peter Chubb23219c12006-07-25 20:39:14 +1000483 netif_device_detach(dev->netdev);
Oliver Neukumc94cb312008-12-18 23:00:59 -0800484 else if (result) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 set_bit(RX_URB_FAIL, &dev->flags);
486 goto resched;
487 } else {
488 clear_bit(RX_URB_FAIL, &dev->flags);
489 }
490
491 return;
492resched:
493 tasklet_schedule(&dev->tl);
494}
495
496static void rx_fixup(unsigned long data)
497{
498 rtl8150_t *dev;
499 struct sk_buff *skb;
Peter Chubb23219c12006-07-25 20:39:14 +1000500 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 dev = (rtl8150_t *)data;
503
504 spin_lock_irq(&dev->rx_pool_lock);
505 fill_skb_pool(dev);
506 spin_unlock_irq(&dev->rx_pool_lock);
507 if (test_bit(RX_URB_FAIL, &dev->flags))
508 if (dev->rx_skb)
509 goto try_again;
510 spin_lock_irq(&dev->rx_pool_lock);
511 skb = pull_skb(dev);
512 spin_unlock_irq(&dev->rx_pool_lock);
513 if (skb == NULL)
514 goto tlsched;
515 dev->rx_skb = skb;
516 usb_fill_bulk_urb(dev->rx_urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
517 dev->rx_skb->data, RTL8150_MTU, read_bulk_callback, dev);
518try_again:
Peter Chubb23219c12006-07-25 20:39:14 +1000519 status = usb_submit_urb(dev->rx_urb, GFP_ATOMIC);
520 if (status == -ENODEV) {
521 netif_device_detach(dev->netdev);
522 } else if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 set_bit(RX_URB_FAIL, &dev->flags);
524 goto tlsched;
Peter Chubb23219c12006-07-25 20:39:14 +1000525 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 clear_bit(RX_URB_FAIL, &dev->flags);
527 }
528
529 return;
530tlsched:
531 tasklet_schedule(&dev->tl);
532}
533
David Howells7d12e782006-10-05 14:55:46 +0100534static void write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
536 rtl8150_t *dev;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800537 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539 dev = urb->context;
540 if (!dev)
541 return;
542 dev_kfree_skb_irq(dev->tx_skb);
543 if (!netif_device_present(dev->netdev))
544 return;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800545 if (status)
Greg Kroah-Hartman880c9c62008-08-18 13:21:04 -0700546 dev_info(&urb->dev->dev, "%s: Tx status %d\n",
Oliver Neukumc94cb312008-12-18 23:00:59 -0800547 dev->netdev->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 dev->netdev->trans_start = jiffies;
549 netif_wake_queue(dev->netdev);
550}
551
David Howells7d12e782006-10-05 14:55:46 +0100552static void intr_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
554 rtl8150_t *dev;
555 __u8 *d;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800556 int status = urb->status;
557 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 dev = urb->context;
560 if (!dev)
561 return;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800562 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 case 0: /* success */
564 break;
565 case -ECONNRESET: /* unlink */
566 case -ENOENT:
567 case -ESHUTDOWN:
568 return;
569 /* -EPIPE: should clear the halt */
570 default:
Greg Kroah-Hartman880c9c62008-08-18 13:21:04 -0700571 dev_info(&urb->dev->dev, "%s: intr status %d\n",
Oliver Neukumc94cb312008-12-18 23:00:59 -0800572 dev->netdev->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 goto resubmit;
574 }
575
576 d = urb->transfer_buffer;
577 if (d[0] & TSR_ERRORS) {
Stephen Hemmingerb7e41e22009-03-20 19:35:50 +0000578 dev->netdev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if (d[INT_TSR] & (TSR_ECOL | TSR_JBR))
Stephen Hemmingerb7e41e22009-03-20 19:35:50 +0000580 dev->netdev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 if (d[INT_TSR] & TSR_LCOL)
Stephen Hemmingerb7e41e22009-03-20 19:35:50 +0000582 dev->netdev->stats.tx_window_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 if (d[INT_TSR] & TSR_LOSS_CRS)
Stephen Hemmingerb7e41e22009-03-20 19:35:50 +0000584 dev->netdev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 }
586 /* Report link status changes to the network stack */
587 if ((d[INT_MSR] & MSR_LINK) == 0) {
588 if (netif_carrier_ok(dev->netdev)) {
589 netif_carrier_off(dev->netdev);
590 dbg("%s: LINK LOST\n", __func__);
591 }
592 } else {
593 if (!netif_carrier_ok(dev->netdev)) {
594 netif_carrier_on(dev->netdev);
595 dbg("%s: LINK CAME BACK\n", __func__);
596 }
597 }
598
599resubmit:
Oliver Neukumc94cb312008-12-18 23:00:59 -0800600 res = usb_submit_urb (urb, GFP_ATOMIC);
601 if (res == -ENODEV)
Peter Chubb23219c12006-07-25 20:39:14 +1000602 netif_device_detach(dev->netdev);
Oliver Neukumc94cb312008-12-18 23:00:59 -0800603 else if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 err ("can't resubmit intr, %s-%s/input0, status %d",
605 dev->udev->bus->bus_name,
Oliver Neukumc94cb312008-12-18 23:00:59 -0800606 dev->udev->devpath, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607}
608
Peter Chubb23219c12006-07-25 20:39:14 +1000609static int rtl8150_suspend(struct usb_interface *intf, pm_message_t message)
610{
611 rtl8150_t *dev = usb_get_intfdata(intf);
612
613 netif_device_detach(dev->netdev);
614
615 if (netif_running(dev->netdev)) {
616 usb_kill_urb(dev->rx_urb);
617 usb_kill_urb(dev->intr_urb);
618 }
619 return 0;
620}
621
622static int rtl8150_resume(struct usb_interface *intf)
623{
624 rtl8150_t *dev = usb_get_intfdata(intf);
625
626 netif_device_attach(dev->netdev);
627 if (netif_running(dev->netdev)) {
628 dev->rx_urb->status = 0;
629 dev->rx_urb->actual_length = 0;
David Howells7d12e782006-10-05 14:55:46 +0100630 read_bulk_callback(dev->rx_urb);
Peter Chubb23219c12006-07-25 20:39:14 +1000631
632 dev->intr_urb->status = 0;
633 dev->intr_urb->actual_length = 0;
David Howells7d12e782006-10-05 14:55:46 +0100634 intr_callback(dev->intr_urb);
Peter Chubb23219c12006-07-25 20:39:14 +1000635 }
636 return 0;
637}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639/*
640**
641** network related part of the code
642**
643*/
644
645static void fill_skb_pool(rtl8150_t *dev)
646{
647 struct sk_buff *skb;
648 int i;
649
650 for (i = 0; i < RX_SKB_POOL_SIZE; i++) {
651 if (dev->rx_skb_pool[i])
652 continue;
653 skb = dev_alloc_skb(RTL8150_MTU + 2);
654 if (!skb) {
655 return;
656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 skb_reserve(skb, 2);
658 dev->rx_skb_pool[i] = skb;
659 }
660}
661
662static void free_skb_pool(rtl8150_t *dev)
663{
664 int i;
665
666 for (i = 0; i < RX_SKB_POOL_SIZE; i++)
667 if (dev->rx_skb_pool[i])
668 dev_kfree_skb(dev->rx_skb_pool[i]);
669}
670
671static int enable_net_traffic(rtl8150_t * dev)
672{
673 u8 cr, tcr, rcr, msr;
674
675 if (!rtl8150_reset(dev)) {
Greg Kroah-Hartman4dc89942008-08-14 09:37:34 -0700676 dev_warn(&dev->udev->dev, "device reset failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 }
678 /* RCR bit7=1 attach Rx info at the end; =0 HW CRC (which is broken) */
679 rcr = 0x9e;
680 dev->rx_creg = cpu_to_le16(rcr);
681 tcr = 0xd8;
682 cr = 0x0c;
683 if (!(rcr & 0x80))
684 set_bit(RTL8150_HW_CRC, &dev->flags);
685 set_registers(dev, RCR, 1, &rcr);
686 set_registers(dev, TCR, 1, &tcr);
687 set_registers(dev, CR, 1, &cr);
688 get_registers(dev, MSR, 1, &msr);
689
690 return 0;
691}
692
693static void disable_net_traffic(rtl8150_t * dev)
694{
695 u8 cr;
696
697 get_registers(dev, CR, 1, &cr);
698 cr &= 0xf3;
699 set_registers(dev, CR, 1, &cr);
700}
701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702static void rtl8150_tx_timeout(struct net_device *netdev)
703{
704 rtl8150_t *dev = netdev_priv(netdev);
Greg Kroah-Hartman4dc89942008-08-14 09:37:34 -0700705 dev_warn(&netdev->dev, "Tx timeout.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 usb_unlink_urb(dev->tx_urb);
Stephen Hemmingerb7e41e22009-03-20 19:35:50 +0000707 netdev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
709
710static void rtl8150_set_multicast(struct net_device *netdev)
711{
712 rtl8150_t *dev = netdev_priv(netdev);
713 netif_stop_queue(netdev);
714 if (netdev->flags & IFF_PROMISC) {
715 dev->rx_creg |= cpu_to_le16(0x0001);
Greg Kroah-Hartman880c9c62008-08-18 13:21:04 -0700716 dev_info(&netdev->dev, "%s: promiscuous mode\n", netdev->name);
YOSHIFUJI Hideakiae0a97b2005-05-25 16:07:04 +0900717 } else if (netdev->mc_count ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 (netdev->flags & IFF_ALLMULTI)) {
719 dev->rx_creg &= cpu_to_le16(0xfffe);
720 dev->rx_creg |= cpu_to_le16(0x0002);
Greg Kroah-Hartman880c9c62008-08-18 13:21:04 -0700721 dev_info(&netdev->dev, "%s: allmulti set\n", netdev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 } else {
723 /* ~RX_MULTICAST, ~RX_PROMISCUOUS */
724 dev->rx_creg &= cpu_to_le16(0x00fc);
725 }
726 async_set_registers(dev, RCR, 2);
727 netif_wake_queue(netdev);
728}
729
Stephen Hemminger25a79c42009-08-31 19:50:45 +0000730static netdev_tx_t rtl8150_start_xmit(struct sk_buff *skb,
731 struct net_device *netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
733 rtl8150_t *dev = netdev_priv(netdev);
734 int count, res;
735
736 netif_stop_queue(netdev);
737 count = (skb->len < 60) ? 60 : skb->len;
738 count = (count & 0x3f) ? count : count + 1;
739 dev->tx_skb = skb;
740 usb_fill_bulk_urb(dev->tx_urb, dev->udev, usb_sndbulkpipe(dev->udev, 2),
741 skb->data, count, write_bulk_callback, dev);
742 if ((res = usb_submit_urb(dev->tx_urb, GFP_ATOMIC))) {
Peter Chubb23219c12006-07-25 20:39:14 +1000743 /* Can we get/handle EPIPE here? */
744 if (res == -ENODEV)
745 netif_device_detach(dev->netdev);
746 else {
Greg Kroah-Hartman4dc89942008-08-14 09:37:34 -0700747 dev_warn(&netdev->dev, "failed tx_urb %d\n", res);
Stephen Hemmingerb7e41e22009-03-20 19:35:50 +0000748 netdev->stats.tx_errors++;
Peter Chubb23219c12006-07-25 20:39:14 +1000749 netif_start_queue(netdev);
750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 } else {
Stephen Hemmingerb7e41e22009-03-20 19:35:50 +0000752 netdev->stats.tx_packets++;
753 netdev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 netdev->trans_start = jiffies;
755 }
756
Patrick McHardy6ed10652009-06-23 06:03:08 +0000757 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758}
759
760
761static void set_carrier(struct net_device *netdev)
762{
763 rtl8150_t *dev = netdev_priv(netdev);
764 short tmp;
765
766 get_registers(dev, CSCR, 2, &tmp);
767 if (tmp & CSCR_LINK_STATUS)
768 netif_carrier_on(netdev);
769 else
770 netif_carrier_off(netdev);
771}
772
773static int rtl8150_open(struct net_device *netdev)
774{
775 rtl8150_t *dev = netdev_priv(netdev);
776 int res;
777
778 if (dev->rx_skb == NULL)
779 dev->rx_skb = pull_skb(dev);
780 if (!dev->rx_skb)
781 return -ENOMEM;
782
783 set_registers(dev, IDR, 6, netdev->dev_addr);
784
785 usb_fill_bulk_urb(dev->rx_urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
786 dev->rx_skb->data, RTL8150_MTU, read_bulk_callback, dev);
Peter Chubb23219c12006-07-25 20:39:14 +1000787 if ((res = usb_submit_urb(dev->rx_urb, GFP_KERNEL))) {
788 if (res == -ENODEV)
789 netif_device_detach(dev->netdev);
Greg Kroah-Hartman4dc89942008-08-14 09:37:34 -0700790 dev_warn(&netdev->dev, "rx_urb submit failed: %d\n", res);
Peter Chubb23219c12006-07-25 20:39:14 +1000791 return res;
792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 usb_fill_int_urb(dev->intr_urb, dev->udev, usb_rcvintpipe(dev->udev, 3),
794 dev->intr_buff, INTBUFSIZE, intr_callback,
795 dev, dev->intr_interval);
Peter Chubb23219c12006-07-25 20:39:14 +1000796 if ((res = usb_submit_urb(dev->intr_urb, GFP_KERNEL))) {
797 if (res == -ENODEV)
798 netif_device_detach(dev->netdev);
Greg Kroah-Hartman4dc89942008-08-14 09:37:34 -0700799 dev_warn(&netdev->dev, "intr_urb submit failed: %d\n", res);
Peter Chubb23219c12006-07-25 20:39:14 +1000800 usb_kill_urb(dev->rx_urb);
801 return res;
802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 enable_net_traffic(dev);
804 set_carrier(netdev);
Peter Chubb23219c12006-07-25 20:39:14 +1000805 netif_start_queue(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807 return res;
808}
809
810static int rtl8150_close(struct net_device *netdev)
811{
812 rtl8150_t *dev = netdev_priv(netdev);
813 int res = 0;
814
815 netif_stop_queue(netdev);
816 if (!test_bit(RTL8150_UNPLUG, &dev->flags))
817 disable_net_traffic(dev);
818 unlink_all_urbs(dev);
819
820 return res;
821}
822
823static void rtl8150_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *info)
824{
825 rtl8150_t *dev = netdev_priv(netdev);
826
827 strncpy(info->driver, driver_name, ETHTOOL_BUSINFO_LEN);
828 strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
829 usb_make_path(dev->udev, info->bus_info, sizeof info->bus_info);
830}
831
832static int rtl8150_get_settings(struct net_device *netdev, struct ethtool_cmd *ecmd)
833{
834 rtl8150_t *dev = netdev_priv(netdev);
835 short lpa, bmcr;
836
837 ecmd->supported = (SUPPORTED_10baseT_Half |
838 SUPPORTED_10baseT_Full |
839 SUPPORTED_100baseT_Half |
840 SUPPORTED_100baseT_Full |
841 SUPPORTED_Autoneg |
842 SUPPORTED_TP | SUPPORTED_MII);
843 ecmd->port = PORT_TP;
844 ecmd->transceiver = XCVR_INTERNAL;
845 ecmd->phy_address = dev->phy;
846 get_registers(dev, BMCR, 2, &bmcr);
847 get_registers(dev, ANLP, 2, &lpa);
848 if (bmcr & BMCR_ANENABLE) {
849 ecmd->autoneg = AUTONEG_ENABLE;
850 ecmd->speed = (lpa & (LPA_100HALF | LPA_100FULL)) ?
851 SPEED_100 : SPEED_10;
852 if (ecmd->speed == SPEED_100)
853 ecmd->duplex = (lpa & LPA_100FULL) ?
854 DUPLEX_FULL : DUPLEX_HALF;
855 else
856 ecmd->duplex = (lpa & LPA_10FULL) ?
857 DUPLEX_FULL : DUPLEX_HALF;
858 } else {
859 ecmd->autoneg = AUTONEG_DISABLE;
860 ecmd->speed = (bmcr & BMCR_SPEED100) ?
861 SPEED_100 : SPEED_10;
862 ecmd->duplex = (bmcr & BMCR_FULLDPLX) ?
863 DUPLEX_FULL : DUPLEX_HALF;
864 }
865 return 0;
866}
867
868static struct ethtool_ops ops = {
869 .get_drvinfo = rtl8150_get_drvinfo,
870 .get_settings = rtl8150_get_settings,
871 .get_link = ethtool_op_get_link
872};
873
874static int rtl8150_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
875{
876 rtl8150_t *dev = netdev_priv(netdev);
877 u16 *data = (u16 *) & rq->ifr_ifru;
878 int res = 0;
879
880 switch (cmd) {
881 case SIOCDEVPRIVATE:
882 data[0] = dev->phy;
883 case SIOCDEVPRIVATE + 1:
884 read_mii_word(dev, dev->phy, (data[1] & 0x1f), &data[3]);
885 break;
886 case SIOCDEVPRIVATE + 2:
887 if (!capable(CAP_NET_ADMIN))
888 return -EPERM;
889 write_mii_word(dev, dev->phy, (data[1] & 0x1f), data[2]);
890 break;
891 default:
892 res = -EOPNOTSUPP;
893 }
894
895 return res;
896}
897
Stephen Hemmingerd79f7ef2009-03-20 19:35:51 +0000898static const struct net_device_ops rtl8150_netdev_ops = {
899 .ndo_open = rtl8150_open,
900 .ndo_stop = rtl8150_close,
901 .ndo_do_ioctl = rtl8150_ioctl,
902 .ndo_start_xmit = rtl8150_start_xmit,
903 .ndo_tx_timeout = rtl8150_tx_timeout,
904 .ndo_set_multicast_list = rtl8150_set_multicast,
905 .ndo_set_mac_address = rtl8150_set_mac_address,
906
907 .ndo_change_mtu = eth_change_mtu,
908 .ndo_validate_addr = eth_validate_addr,
909};
910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911static int rtl8150_probe(struct usb_interface *intf,
912 const struct usb_device_id *id)
913{
914 struct usb_device *udev = interface_to_usbdev(intf);
915 rtl8150_t *dev;
916 struct net_device *netdev;
917
918 netdev = alloc_etherdev(sizeof(rtl8150_t));
919 if (!netdev) {
920 err("Out of memory");
921 return -ENOMEM;
922 }
923
924 dev = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926 dev->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL);
927 if (!dev->intr_buff) {
928 free_netdev(netdev);
929 return -ENOMEM;
930 }
931
932 tasklet_init(&dev->tl, rx_fixup, (unsigned long)dev);
933 spin_lock_init(&dev->rx_pool_lock);
934
935 dev->udev = udev;
936 dev->netdev = netdev;
Stephen Hemmingerd79f7ef2009-03-20 19:35:51 +0000937 netdev->netdev_ops = &rtl8150_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 netdev->watchdog_timeo = RTL8150_TX_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 SET_ETHTOOL_OPS(netdev, &ops);
940 dev->intr_interval = 100; /* 100ms */
941
942 if (!alloc_all_urbs(dev)) {
943 err("out of memory");
944 goto out;
945 }
946 if (!rtl8150_reset(dev)) {
947 err("couldn't reset the device");
948 goto out1;
949 }
950 fill_skb_pool(dev);
951 set_ethernet_addr(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953 usb_set_intfdata(intf, dev);
954 SET_NETDEV_DEV(netdev, &intf->dev);
955 if (register_netdev(netdev) != 0) {
956 err("couldn't register the device");
957 goto out2;
958 }
Petko Manolov09abfa82006-03-15 16:29:38 +0200959
Greg Kroah-Hartman880c9c62008-08-18 13:21:04 -0700960 dev_info(&intf->dev, "%s: rtl8150 is detected\n", netdev->name);
Petko Manolov09abfa82006-03-15 16:29:38 +0200961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 return 0;
963
964out2:
965 usb_set_intfdata(intf, NULL);
966 free_skb_pool(dev);
967out1:
968 free_all_urbs(dev);
969out:
970 kfree(dev->intr_buff);
971 free_netdev(netdev);
972 return -EIO;
973}
974
975static void rtl8150_disconnect(struct usb_interface *intf)
976{
977 rtl8150_t *dev = usb_get_intfdata(intf);
978
979 usb_set_intfdata(intf, NULL);
980 if (dev) {
981 set_bit(RTL8150_UNPLUG, &dev->flags);
Greg Kroah-Hartman8da608c2005-06-20 21:15:16 -0700982 tasklet_disable(&dev->tl);
Andrew Mortoneff674a2006-08-14 23:11:09 -0700983 tasklet_kill(&dev->tl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 unregister_netdev(dev->netdev);
985 unlink_all_urbs(dev);
986 free_all_urbs(dev);
987 free_skb_pool(dev);
988 if (dev->rx_skb)
989 dev_kfree_skb(dev->rx_skb);
990 kfree(dev->intr_buff);
991 free_netdev(dev->netdev);
992 }
993}
994
995static int __init usb_rtl8150_init(void)
996{
Greg Kroah-Hartman880c9c62008-08-18 13:21:04 -0700997 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
998 DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 return usb_register(&rtl8150_driver);
1000}
1001
1002static void __exit usb_rtl8150_exit(void)
1003{
1004 usb_deregister(&rtl8150_driver);
1005}
1006
1007module_init(usb_rtl8150_init);
1008module_exit(usb_rtl8150_exit);
1009
1010MODULE_AUTHOR(DRIVER_AUTHOR);
1011MODULE_DESCRIPTION(DRIVER_DESC);
1012MODULE_LICENSE("GPL");