blob: 0710b4ca925208afe7f749b16df8874e95882e58 [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 const char driver_name [] = "rtl8150";
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174/*
175**
176** device related part of the code
177**
178*/
179static int get_registers(rtl8150_t * dev, u16 indx, u16 size, void *data)
180{
181 return usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
182 RTL8150_REQ_GET_REGS, RTL8150_REQT_READ,
183 indx, 0, data, size, 500);
184}
185
186static int set_registers(rtl8150_t * dev, u16 indx, u16 size, void *data)
187{
188 return usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
189 RTL8150_REQ_SET_REGS, RTL8150_REQT_WRITE,
190 indx, 0, data, size, 500);
191}
192
David Howells7d12e782006-10-05 14:55:46 +0100193static void ctrl_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
195 rtl8150_t *dev;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800196 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Oliver Neukumc94cb312008-12-18 23:00:59 -0800198 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 case 0:
200 break;
201 case -EINPROGRESS:
202 break;
203 case -ENOENT:
204 break;
205 default:
André Goddard Rosa342a4372009-05-29 22:13:58 -0700206 if (printk_ratelimit())
207 dev_warn(&urb->dev->dev, "ctrl urb status %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 }
209 dev = urb->context;
210 clear_bit(RX_REG_SET, &dev->flags);
211}
212
213static int async_set_registers(rtl8150_t * dev, u16 indx, u16 size)
214{
215 int ret;
216
217 if (test_bit(RX_REG_SET, &dev->flags))
218 return -EAGAIN;
219
220 dev->dr.bRequestType = RTL8150_REQT_WRITE;
221 dev->dr.bRequest = RTL8150_REQ_SET_REGS;
222 dev->dr.wValue = cpu_to_le16(indx);
223 dev->dr.wIndex = 0;
224 dev->dr.wLength = cpu_to_le16(size);
225 dev->ctrl_urb->transfer_buffer_length = size;
226 usb_fill_control_urb(dev->ctrl_urb, dev->udev,
227 usb_sndctrlpipe(dev->udev, 0), (char *) &dev->dr,
228 &dev->rx_creg, size, ctrl_callback, dev);
Peter Chubb23219c12006-07-25 20:39:14 +1000229 if ((ret = usb_submit_urb(dev->ctrl_urb, GFP_ATOMIC))) {
230 if (ret == -ENODEV)
231 netif_device_detach(dev->netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 err("control request submission failed: %d", ret);
Peter Chubb23219c12006-07-25 20:39:14 +1000233 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 set_bit(RX_REG_SET, &dev->flags);
235
236 return ret;
237}
238
239static int read_mii_word(rtl8150_t * dev, u8 phy, __u8 indx, u16 * reg)
240{
241 int i;
242 u8 data[3], tmp;
243
244 data[0] = phy;
245 data[1] = data[2] = 0;
246 tmp = indx | PHY_READ | PHY_GO;
247 i = 0;
248
249 set_registers(dev, PHYADD, sizeof(data), data);
250 set_registers(dev, PHYCNT, 1, &tmp);
251 do {
252 get_registers(dev, PHYCNT, 1, data);
253 } while ((data[0] & PHY_GO) && (i++ < MII_TIMEOUT));
254
roel kluinc064efc2009-12-27 11:22:08 +0000255 if (i <= MII_TIMEOUT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 get_registers(dev, PHYDAT, 2, data);
257 *reg = data[0] | (data[1] << 8);
258 return 0;
259 } else
260 return 1;
261}
262
263static int write_mii_word(rtl8150_t * dev, u8 phy, __u8 indx, u16 reg)
264{
265 int i;
266 u8 data[3], tmp;
267
268 data[0] = phy;
Al Viro886ae1f2007-02-04 03:02:17 +0000269 data[1] = reg & 0xff;
270 data[2] = (reg >> 8) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 tmp = indx | PHY_WRITE | PHY_GO;
272 i = 0;
273
274 set_registers(dev, PHYADD, sizeof(data), data);
275 set_registers(dev, PHYCNT, 1, &tmp);
276 do {
277 get_registers(dev, PHYCNT, 1, data);
278 } while ((data[0] & PHY_GO) && (i++ < MII_TIMEOUT));
279
roel kluinc064efc2009-12-27 11:22:08 +0000280 if (i <= MII_TIMEOUT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 return 0;
282 else
283 return 1;
284}
285
286static inline void set_ethernet_addr(rtl8150_t * dev)
287{
288 u8 node_id[6];
289
290 get_registers(dev, IDR, sizeof(node_id), node_id);
291 memcpy(dev->netdev->dev_addr, node_id, sizeof(node_id));
292}
293
294static int rtl8150_set_mac_address(struct net_device *netdev, void *p)
295{
296 struct sockaddr *addr = p;
297 rtl8150_t *dev = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299 if (netif_running(netdev))
300 return -EBUSY;
301
302 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
H Hartley Sweetend649a282009-12-29 20:03:28 -0800303 dbg("%s: Setting MAC address to %pM\n", netdev->name, netdev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 /* Set the IDR registers. */
Julia Lawall60579122009-12-13 05:47:04 +0000305 set_registers(dev, IDR, netdev->addr_len, netdev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306#ifdef EEPROM_WRITE
307 {
H Hartley Sweetend649a282009-12-29 20:03:28 -0800308 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 u8 cr;
310 /* Get the CR contents. */
311 get_registers(dev, CR, 1, &cr);
312 /* Set the WEPROM bit (eeprom write enable). */
313 cr |= 0x20;
314 set_registers(dev, CR, 1, &cr);
315 /* Write the MAC address into eeprom. Eeprom writes must be word-sized,
316 so we need to split them up. */
317 for (i = 0; i * 2 < netdev->addr_len; i++) {
françois romieu141b9e62011-09-30 00:38:29 +0000318 set_registers(dev, IDR_EEPROM + (i * 2), 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 netdev->dev_addr + (i * 2));
320 }
321 /* Clear the WEPROM bit (preventing accidental eeprom writes). */
322 cr &= 0xdf;
323 set_registers(dev, CR, 1, &cr);
324 }
325#endif
326 return 0;
327}
328
329static int rtl8150_reset(rtl8150_t * dev)
330{
331 u8 data = 0x10;
332 int i = HZ;
333
334 set_registers(dev, CR, 1, &data);
335 do {
336 get_registers(dev, CR, 1, &data);
337 } while ((data & 0x10) && --i);
338
339 return (i > 0) ? 1 : 0;
340}
341
342static int alloc_all_urbs(rtl8150_t * dev)
343{
344 dev->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
345 if (!dev->rx_urb)
346 return 0;
347 dev->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
348 if (!dev->tx_urb) {
349 usb_free_urb(dev->rx_urb);
350 return 0;
351 }
352 dev->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
353 if (!dev->intr_urb) {
354 usb_free_urb(dev->rx_urb);
355 usb_free_urb(dev->tx_urb);
356 return 0;
357 }
358 dev->ctrl_urb = usb_alloc_urb(0, GFP_KERNEL);
Robert P. J. Day7fdba2f2008-03-09 20:44:52 -0400359 if (!dev->ctrl_urb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 usb_free_urb(dev->rx_urb);
361 usb_free_urb(dev->tx_urb);
362 usb_free_urb(dev->intr_urb);
363 return 0;
364 }
365
366 return 1;
367}
368
369static void free_all_urbs(rtl8150_t * dev)
370{
371 usb_free_urb(dev->rx_urb);
372 usb_free_urb(dev->tx_urb);
373 usb_free_urb(dev->intr_urb);
374 usb_free_urb(dev->ctrl_urb);
375}
376
377static void unlink_all_urbs(rtl8150_t * dev)
378{
379 usb_kill_urb(dev->rx_urb);
380 usb_kill_urb(dev->tx_urb);
381 usb_kill_urb(dev->intr_urb);
382 usb_kill_urb(dev->ctrl_urb);
383}
384
385static inline struct sk_buff *pull_skb(rtl8150_t *dev)
386{
387 struct sk_buff *skb;
388 int i;
389
390 for (i = 0; i < RX_SKB_POOL_SIZE; i++) {
391 if (dev->rx_skb_pool[i]) {
392 skb = dev->rx_skb_pool[i];
393 dev->rx_skb_pool[i] = NULL;
394 return skb;
395 }
396 }
397 return NULL;
398}
399
David Howells7d12e782006-10-05 14:55:46 +0100400static void read_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
402 rtl8150_t *dev;
403 unsigned pkt_len, res;
404 struct sk_buff *skb;
405 struct net_device *netdev;
406 u16 rx_stat;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800407 int status = urb->status;
408 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 dev = urb->context;
411 if (!dev)
412 return;
413 if (test_bit(RTL8150_UNPLUG, &dev->flags))
414 return;
415 netdev = dev->netdev;
416 if (!netif_device_present(netdev))
417 return;
418
Oliver Neukumc94cb312008-12-18 23:00:59 -0800419 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 case 0:
421 break;
422 case -ENOENT:
423 return; /* the urb is in unlink state */
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700424 case -ETIME:
André Goddard Rosa342a4372009-05-29 22:13:58 -0700425 if (printk_ratelimit())
426 dev_warn(&urb->dev->dev, "may be reset is needed?..\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 goto goon;
428 default:
André Goddard Rosa342a4372009-05-29 22:13:58 -0700429 if (printk_ratelimit())
430 dev_warn(&urb->dev->dev, "Rx status %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 goto goon;
432 }
433
434 if (!dev->rx_skb)
435 goto resched;
436 /* protect against short packets (tell me why we got some?!?) */
437 if (urb->actual_length < 4)
438 goto goon;
439
440 res = urb->actual_length;
441 rx_stat = le16_to_cpu(*(__le16 *)(urb->transfer_buffer + res - 4));
442 pkt_len = res - 4;
443
444 skb_put(dev->rx_skb, pkt_len);
445 dev->rx_skb->protocol = eth_type_trans(dev->rx_skb, netdev);
446 netif_rx(dev->rx_skb);
Stephen Hemmingerb7e41e22009-03-20 19:35:50 +0000447 netdev->stats.rx_packets++;
448 netdev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 spin_lock(&dev->rx_pool_lock);
451 skb = pull_skb(dev);
452 spin_unlock(&dev->rx_pool_lock);
453 if (!skb)
454 goto resched;
455
456 dev->rx_skb = skb;
457goon:
458 usb_fill_bulk_urb(dev->rx_urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
459 dev->rx_skb->data, RTL8150_MTU, read_bulk_callback, dev);
Oliver Neukumc94cb312008-12-18 23:00:59 -0800460 result = usb_submit_urb(dev->rx_urb, GFP_ATOMIC);
461 if (result == -ENODEV)
Peter Chubb23219c12006-07-25 20:39:14 +1000462 netif_device_detach(dev->netdev);
Oliver Neukumc94cb312008-12-18 23:00:59 -0800463 else if (result) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 set_bit(RX_URB_FAIL, &dev->flags);
465 goto resched;
466 } else {
467 clear_bit(RX_URB_FAIL, &dev->flags);
468 }
469
470 return;
471resched:
472 tasklet_schedule(&dev->tl);
473}
474
David Howells7d12e782006-10-05 14:55:46 +0100475static void write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
477 rtl8150_t *dev;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800478 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480 dev = urb->context;
481 if (!dev)
482 return;
483 dev_kfree_skb_irq(dev->tx_skb);
484 if (!netif_device_present(dev->netdev))
485 return;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800486 if (status)
Greg Kroah-Hartman880c9c62008-08-18 13:21:04 -0700487 dev_info(&urb->dev->dev, "%s: Tx status %d\n",
Oliver Neukumc94cb312008-12-18 23:00:59 -0800488 dev->netdev->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 dev->netdev->trans_start = jiffies;
490 netif_wake_queue(dev->netdev);
491}
492
David Howells7d12e782006-10-05 14:55:46 +0100493static void intr_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
495 rtl8150_t *dev;
496 __u8 *d;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800497 int status = urb->status;
498 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 dev = urb->context;
501 if (!dev)
502 return;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800503 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 case 0: /* success */
505 break;
506 case -ECONNRESET: /* unlink */
507 case -ENOENT:
508 case -ESHUTDOWN:
509 return;
510 /* -EPIPE: should clear the halt */
511 default:
Greg Kroah-Hartman880c9c62008-08-18 13:21:04 -0700512 dev_info(&urb->dev->dev, "%s: intr status %d\n",
Oliver Neukumc94cb312008-12-18 23:00:59 -0800513 dev->netdev->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 goto resubmit;
515 }
516
517 d = urb->transfer_buffer;
518 if (d[0] & TSR_ERRORS) {
Stephen Hemmingerb7e41e22009-03-20 19:35:50 +0000519 dev->netdev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if (d[INT_TSR] & (TSR_ECOL | TSR_JBR))
Stephen Hemmingerb7e41e22009-03-20 19:35:50 +0000521 dev->netdev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (d[INT_TSR] & TSR_LCOL)
Stephen Hemmingerb7e41e22009-03-20 19:35:50 +0000523 dev->netdev->stats.tx_window_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 if (d[INT_TSR] & TSR_LOSS_CRS)
Stephen Hemmingerb7e41e22009-03-20 19:35:50 +0000525 dev->netdev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 }
527 /* Report link status changes to the network stack */
528 if ((d[INT_MSR] & MSR_LINK) == 0) {
529 if (netif_carrier_ok(dev->netdev)) {
530 netif_carrier_off(dev->netdev);
531 dbg("%s: LINK LOST\n", __func__);
532 }
533 } else {
534 if (!netif_carrier_ok(dev->netdev)) {
535 netif_carrier_on(dev->netdev);
536 dbg("%s: LINK CAME BACK\n", __func__);
537 }
538 }
539
540resubmit:
Oliver Neukumc94cb312008-12-18 23:00:59 -0800541 res = usb_submit_urb (urb, GFP_ATOMIC);
542 if (res == -ENODEV)
Peter Chubb23219c12006-07-25 20:39:14 +1000543 netif_device_detach(dev->netdev);
Oliver Neukumc94cb312008-12-18 23:00:59 -0800544 else if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 err ("can't resubmit intr, %s-%s/input0, status %d",
546 dev->udev->bus->bus_name,
Oliver Neukumc94cb312008-12-18 23:00:59 -0800547 dev->udev->devpath, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549
Peter Chubb23219c12006-07-25 20:39:14 +1000550static int rtl8150_suspend(struct usb_interface *intf, pm_message_t message)
551{
552 rtl8150_t *dev = usb_get_intfdata(intf);
553
554 netif_device_detach(dev->netdev);
555
556 if (netif_running(dev->netdev)) {
557 usb_kill_urb(dev->rx_urb);
558 usb_kill_urb(dev->intr_urb);
559 }
560 return 0;
561}
562
563static int rtl8150_resume(struct usb_interface *intf)
564{
565 rtl8150_t *dev = usb_get_intfdata(intf);
566
567 netif_device_attach(dev->netdev);
568 if (netif_running(dev->netdev)) {
569 dev->rx_urb->status = 0;
570 dev->rx_urb->actual_length = 0;
David Howells7d12e782006-10-05 14:55:46 +0100571 read_bulk_callback(dev->rx_urb);
Peter Chubb23219c12006-07-25 20:39:14 +1000572
573 dev->intr_urb->status = 0;
574 dev->intr_urb->actual_length = 0;
David Howells7d12e782006-10-05 14:55:46 +0100575 intr_callback(dev->intr_urb);
Peter Chubb23219c12006-07-25 20:39:14 +1000576 }
577 return 0;
578}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580/*
581**
582** network related part of the code
583**
584*/
585
586static void fill_skb_pool(rtl8150_t *dev)
587{
588 struct sk_buff *skb;
589 int i;
590
591 for (i = 0; i < RX_SKB_POOL_SIZE; i++) {
592 if (dev->rx_skb_pool[i])
593 continue;
594 skb = dev_alloc_skb(RTL8150_MTU + 2);
595 if (!skb) {
596 return;
597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 skb_reserve(skb, 2);
599 dev->rx_skb_pool[i] = skb;
600 }
601}
602
603static void free_skb_pool(rtl8150_t *dev)
604{
605 int i;
606
607 for (i = 0; i < RX_SKB_POOL_SIZE; i++)
608 if (dev->rx_skb_pool[i])
609 dev_kfree_skb(dev->rx_skb_pool[i]);
610}
611
françois romieu141b9e62011-09-30 00:38:29 +0000612static void rx_fixup(unsigned long data)
613{
614 struct rtl8150 *dev = (struct rtl8150 *)data;
615 struct sk_buff *skb;
616 int status;
617
618 spin_lock_irq(&dev->rx_pool_lock);
619 fill_skb_pool(dev);
620 spin_unlock_irq(&dev->rx_pool_lock);
621 if (test_bit(RX_URB_FAIL, &dev->flags))
622 if (dev->rx_skb)
623 goto try_again;
624 spin_lock_irq(&dev->rx_pool_lock);
625 skb = pull_skb(dev);
626 spin_unlock_irq(&dev->rx_pool_lock);
627 if (skb == NULL)
628 goto tlsched;
629 dev->rx_skb = skb;
630 usb_fill_bulk_urb(dev->rx_urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
631 dev->rx_skb->data, RTL8150_MTU, read_bulk_callback, dev);
632try_again:
633 status = usb_submit_urb(dev->rx_urb, GFP_ATOMIC);
634 if (status == -ENODEV) {
635 netif_device_detach(dev->netdev);
636 } else if (status) {
637 set_bit(RX_URB_FAIL, &dev->flags);
638 goto tlsched;
639 } else {
640 clear_bit(RX_URB_FAIL, &dev->flags);
641 }
642
643 return;
644tlsched:
645 tasklet_schedule(&dev->tl);
646}
647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648static int enable_net_traffic(rtl8150_t * dev)
649{
650 u8 cr, tcr, rcr, msr;
651
652 if (!rtl8150_reset(dev)) {
Greg Kroah-Hartman4dc89942008-08-14 09:37:34 -0700653 dev_warn(&dev->udev->dev, "device reset failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 }
655 /* RCR bit7=1 attach Rx info at the end; =0 HW CRC (which is broken) */
656 rcr = 0x9e;
657 dev->rx_creg = cpu_to_le16(rcr);
658 tcr = 0xd8;
659 cr = 0x0c;
660 if (!(rcr & 0x80))
661 set_bit(RTL8150_HW_CRC, &dev->flags);
662 set_registers(dev, RCR, 1, &rcr);
663 set_registers(dev, TCR, 1, &tcr);
664 set_registers(dev, CR, 1, &cr);
665 get_registers(dev, MSR, 1, &msr);
666
667 return 0;
668}
669
670static void disable_net_traffic(rtl8150_t * dev)
671{
672 u8 cr;
673
674 get_registers(dev, CR, 1, &cr);
675 cr &= 0xf3;
676 set_registers(dev, CR, 1, &cr);
677}
678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679static void rtl8150_tx_timeout(struct net_device *netdev)
680{
681 rtl8150_t *dev = netdev_priv(netdev);
Greg Kroah-Hartman4dc89942008-08-14 09:37:34 -0700682 dev_warn(&netdev->dev, "Tx timeout.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 usb_unlink_urb(dev->tx_urb);
Stephen Hemmingerb7e41e22009-03-20 19:35:50 +0000684 netdev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
686
687static void rtl8150_set_multicast(struct net_device *netdev)
688{
689 rtl8150_t *dev = netdev_priv(netdev);
690 netif_stop_queue(netdev);
691 if (netdev->flags & IFF_PROMISC) {
692 dev->rx_creg |= cpu_to_le16(0x0001);
Greg Kroah-Hartman880c9c62008-08-18 13:21:04 -0700693 dev_info(&netdev->dev, "%s: promiscuous mode\n", netdev->name);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000694 } else if (!netdev_mc_empty(netdev) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 (netdev->flags & IFF_ALLMULTI)) {
696 dev->rx_creg &= cpu_to_le16(0xfffe);
697 dev->rx_creg |= cpu_to_le16(0x0002);
Greg Kroah-Hartman880c9c62008-08-18 13:21:04 -0700698 dev_info(&netdev->dev, "%s: allmulti set\n", netdev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 } else {
700 /* ~RX_MULTICAST, ~RX_PROMISCUOUS */
701 dev->rx_creg &= cpu_to_le16(0x00fc);
702 }
703 async_set_registers(dev, RCR, 2);
704 netif_wake_queue(netdev);
705}
706
Stephen Hemminger25a79c42009-08-31 19:50:45 +0000707static netdev_tx_t rtl8150_start_xmit(struct sk_buff *skb,
708 struct net_device *netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
710 rtl8150_t *dev = netdev_priv(netdev);
711 int count, res;
712
713 netif_stop_queue(netdev);
714 count = (skb->len < 60) ? 60 : skb->len;
715 count = (count & 0x3f) ? count : count + 1;
716 dev->tx_skb = skb;
717 usb_fill_bulk_urb(dev->tx_urb, dev->udev, usb_sndbulkpipe(dev->udev, 2),
718 skb->data, count, write_bulk_callback, dev);
719 if ((res = usb_submit_urb(dev->tx_urb, GFP_ATOMIC))) {
Peter Chubb23219c12006-07-25 20:39:14 +1000720 /* Can we get/handle EPIPE here? */
721 if (res == -ENODEV)
722 netif_device_detach(dev->netdev);
723 else {
Greg Kroah-Hartman4dc89942008-08-14 09:37:34 -0700724 dev_warn(&netdev->dev, "failed tx_urb %d\n", res);
Stephen Hemmingerb7e41e22009-03-20 19:35:50 +0000725 netdev->stats.tx_errors++;
Peter Chubb23219c12006-07-25 20:39:14 +1000726 netif_start_queue(netdev);
727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 } else {
Stephen Hemmingerb7e41e22009-03-20 19:35:50 +0000729 netdev->stats.tx_packets++;
730 netdev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 netdev->trans_start = jiffies;
732 }
733
Patrick McHardy6ed10652009-06-23 06:03:08 +0000734 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735}
736
737
738static void set_carrier(struct net_device *netdev)
739{
740 rtl8150_t *dev = netdev_priv(netdev);
741 short tmp;
742
743 get_registers(dev, CSCR, 2, &tmp);
744 if (tmp & CSCR_LINK_STATUS)
745 netif_carrier_on(netdev);
746 else
747 netif_carrier_off(netdev);
748}
749
750static int rtl8150_open(struct net_device *netdev)
751{
752 rtl8150_t *dev = netdev_priv(netdev);
753 int res;
754
755 if (dev->rx_skb == NULL)
756 dev->rx_skb = pull_skb(dev);
757 if (!dev->rx_skb)
758 return -ENOMEM;
759
760 set_registers(dev, IDR, 6, netdev->dev_addr);
françois romieu141b9e62011-09-30 00:38:29 +0000761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 usb_fill_bulk_urb(dev->rx_urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
763 dev->rx_skb->data, RTL8150_MTU, read_bulk_callback, dev);
Peter Chubb23219c12006-07-25 20:39:14 +1000764 if ((res = usb_submit_urb(dev->rx_urb, GFP_KERNEL))) {
765 if (res == -ENODEV)
766 netif_device_detach(dev->netdev);
Greg Kroah-Hartman4dc89942008-08-14 09:37:34 -0700767 dev_warn(&netdev->dev, "rx_urb submit failed: %d\n", res);
Peter Chubb23219c12006-07-25 20:39:14 +1000768 return res;
769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 usb_fill_int_urb(dev->intr_urb, dev->udev, usb_rcvintpipe(dev->udev, 3),
771 dev->intr_buff, INTBUFSIZE, intr_callback,
772 dev, dev->intr_interval);
Peter Chubb23219c12006-07-25 20:39:14 +1000773 if ((res = usb_submit_urb(dev->intr_urb, GFP_KERNEL))) {
774 if (res == -ENODEV)
775 netif_device_detach(dev->netdev);
Greg Kroah-Hartman4dc89942008-08-14 09:37:34 -0700776 dev_warn(&netdev->dev, "intr_urb submit failed: %d\n", res);
Peter Chubb23219c12006-07-25 20:39:14 +1000777 usb_kill_urb(dev->rx_urb);
778 return res;
779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 enable_net_traffic(dev);
781 set_carrier(netdev);
Peter Chubb23219c12006-07-25 20:39:14 +1000782 netif_start_queue(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784 return res;
785}
786
787static int rtl8150_close(struct net_device *netdev)
788{
789 rtl8150_t *dev = netdev_priv(netdev);
790 int res = 0;
791
792 netif_stop_queue(netdev);
793 if (!test_bit(RTL8150_UNPLUG, &dev->flags))
794 disable_net_traffic(dev);
795 unlink_all_urbs(dev);
796
797 return res;
798}
799
800static void rtl8150_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *info)
801{
802 rtl8150_t *dev = netdev_priv(netdev);
803
804 strncpy(info->driver, driver_name, ETHTOOL_BUSINFO_LEN);
805 strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
806 usb_make_path(dev->udev, info->bus_info, sizeof info->bus_info);
807}
808
809static int rtl8150_get_settings(struct net_device *netdev, struct ethtool_cmd *ecmd)
810{
811 rtl8150_t *dev = netdev_priv(netdev);
812 short lpa, bmcr;
813
814 ecmd->supported = (SUPPORTED_10baseT_Half |
815 SUPPORTED_10baseT_Full |
816 SUPPORTED_100baseT_Half |
817 SUPPORTED_100baseT_Full |
818 SUPPORTED_Autoneg |
819 SUPPORTED_TP | SUPPORTED_MII);
820 ecmd->port = PORT_TP;
821 ecmd->transceiver = XCVR_INTERNAL;
822 ecmd->phy_address = dev->phy;
823 get_registers(dev, BMCR, 2, &bmcr);
824 get_registers(dev, ANLP, 2, &lpa);
825 if (bmcr & BMCR_ANENABLE) {
David Decotigny70739492011-04-27 18:32:40 +0000826 u32 speed = ((lpa & (LPA_100HALF | LPA_100FULL)) ?
827 SPEED_100 : SPEED_10);
828 ethtool_cmd_speed_set(ecmd, speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 ecmd->autoneg = AUTONEG_ENABLE;
David Decotigny70739492011-04-27 18:32:40 +0000830 if (speed == SPEED_100)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 ecmd->duplex = (lpa & LPA_100FULL) ?
832 DUPLEX_FULL : DUPLEX_HALF;
833 else
834 ecmd->duplex = (lpa & LPA_10FULL) ?
835 DUPLEX_FULL : DUPLEX_HALF;
836 } else {
837 ecmd->autoneg = AUTONEG_DISABLE;
David Decotigny70739492011-04-27 18:32:40 +0000838 ethtool_cmd_speed_set(ecmd, ((bmcr & BMCR_SPEED100) ?
839 SPEED_100 : SPEED_10));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 ecmd->duplex = (bmcr & BMCR_FULLDPLX) ?
841 DUPLEX_FULL : DUPLEX_HALF;
842 }
843 return 0;
844}
845
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700846static const struct ethtool_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 .get_drvinfo = rtl8150_get_drvinfo,
848 .get_settings = rtl8150_get_settings,
849 .get_link = ethtool_op_get_link
850};
851
852static int rtl8150_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
853{
854 rtl8150_t *dev = netdev_priv(netdev);
855 u16 *data = (u16 *) & rq->ifr_ifru;
856 int res = 0;
857
858 switch (cmd) {
859 case SIOCDEVPRIVATE:
860 data[0] = dev->phy;
861 case SIOCDEVPRIVATE + 1:
862 read_mii_word(dev, dev->phy, (data[1] & 0x1f), &data[3]);
863 break;
864 case SIOCDEVPRIVATE + 2:
865 if (!capable(CAP_NET_ADMIN))
866 return -EPERM;
867 write_mii_word(dev, dev->phy, (data[1] & 0x1f), data[2]);
868 break;
869 default:
870 res = -EOPNOTSUPP;
871 }
872
873 return res;
874}
875
Stephen Hemmingerd79f7ef2009-03-20 19:35:51 +0000876static const struct net_device_ops rtl8150_netdev_ops = {
877 .ndo_open = rtl8150_open,
878 .ndo_stop = rtl8150_close,
879 .ndo_do_ioctl = rtl8150_ioctl,
880 .ndo_start_xmit = rtl8150_start_xmit,
françois romieu141b9e62011-09-30 00:38:29 +0000881 .ndo_tx_timeout = rtl8150_tx_timeout,
Jiri Pirkoafc4b132011-08-16 06:29:01 +0000882 .ndo_set_rx_mode = rtl8150_set_multicast,
Stephen Hemmingerd79f7ef2009-03-20 19:35:51 +0000883 .ndo_set_mac_address = rtl8150_set_mac_address,
884
885 .ndo_change_mtu = eth_change_mtu,
886 .ndo_validate_addr = eth_validate_addr,
887};
888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889static int rtl8150_probe(struct usb_interface *intf,
890 const struct usb_device_id *id)
891{
892 struct usb_device *udev = interface_to_usbdev(intf);
893 rtl8150_t *dev;
894 struct net_device *netdev;
895
896 netdev = alloc_etherdev(sizeof(rtl8150_t));
897 if (!netdev) {
898 err("Out of memory");
899 return -ENOMEM;
900 }
901
902 dev = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 dev->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL);
905 if (!dev->intr_buff) {
906 free_netdev(netdev);
907 return -ENOMEM;
908 }
909
910 tasklet_init(&dev->tl, rx_fixup, (unsigned long)dev);
911 spin_lock_init(&dev->rx_pool_lock);
françois romieu141b9e62011-09-30 00:38:29 +0000912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 dev->udev = udev;
914 dev->netdev = netdev;
Stephen Hemmingerd79f7ef2009-03-20 19:35:51 +0000915 netdev->netdev_ops = &rtl8150_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 netdev->watchdog_timeo = RTL8150_TX_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 SET_ETHTOOL_OPS(netdev, &ops);
918 dev->intr_interval = 100; /* 100ms */
919
920 if (!alloc_all_urbs(dev)) {
921 err("out of memory");
922 goto out;
923 }
924 if (!rtl8150_reset(dev)) {
925 err("couldn't reset the device");
926 goto out1;
927 }
928 fill_skb_pool(dev);
929 set_ethernet_addr(dev);
françois romieu141b9e62011-09-30 00:38:29 +0000930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 usb_set_intfdata(intf, dev);
932 SET_NETDEV_DEV(netdev, &intf->dev);
933 if (register_netdev(netdev) != 0) {
934 err("couldn't register the device");
935 goto out2;
936 }
Petko Manolov09abfa82006-03-15 16:29:38 +0200937
Greg Kroah-Hartman880c9c62008-08-18 13:21:04 -0700938 dev_info(&intf->dev, "%s: rtl8150 is detected\n", netdev->name);
Petko Manolov09abfa82006-03-15 16:29:38 +0200939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return 0;
941
942out2:
943 usb_set_intfdata(intf, NULL);
944 free_skb_pool(dev);
945out1:
946 free_all_urbs(dev);
947out:
948 kfree(dev->intr_buff);
949 free_netdev(netdev);
950 return -EIO;
951}
952
953static void rtl8150_disconnect(struct usb_interface *intf)
954{
955 rtl8150_t *dev = usb_get_intfdata(intf);
956
957 usb_set_intfdata(intf, NULL);
958 if (dev) {
959 set_bit(RTL8150_UNPLUG, &dev->flags);
Andrew Mortoneff674a2006-08-14 23:11:09 -0700960 tasklet_kill(&dev->tl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 unregister_netdev(dev->netdev);
962 unlink_all_urbs(dev);
963 free_all_urbs(dev);
964 free_skb_pool(dev);
965 if (dev->rx_skb)
966 dev_kfree_skb(dev->rx_skb);
967 kfree(dev->intr_buff);
968 free_netdev(dev->netdev);
969 }
970}
971
françois romieu141b9e62011-09-30 00:38:29 +0000972static struct usb_driver rtl8150_driver = {
973 .name = driver_name,
974 .probe = rtl8150_probe,
975 .disconnect = rtl8150_disconnect,
976 .id_table = rtl8150_table,
977 .suspend = rtl8150_suspend,
978 .resume = rtl8150_resume
979};
980
Greg Kroah-Hartmand632eb12011-11-18 09:44:20 -0800981module_usb_driver(rtl8150_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983MODULE_AUTHOR(DRIVER_AUTHOR);
984MODULE_DESCRIPTION(DRIVER_DESC);
985MODULE_LICENSE("GPL");