blob: 0e2c92e0e5323e03b57b442e43db78f4f0739e8a [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89#define RTL8150_MTU 1540
90#define RTL8150_TX_TIMEOUT (HZ)
91#define RX_SKB_POOL_SIZE 4
92
93/* rtl8150 flags */
94#define RTL8150_HW_CRC 0
95#define RX_REG_SET 1
96#define RTL8150_UNPLUG 2
97#define RX_URB_FAIL 3
98
99/* Define these values to match your device */
Petko Manolov58592712006-12-04 14:27:36 +0200100#define VENDOR_ID_REALTEK 0x0bda
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#define VENDOR_ID_MELCO 0x0411
Petko Manolov58592712006-12-04 14:27:36 +0200102#define VENDOR_ID_MICRONET 0x3980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103#define VENDOR_ID_LONGSHINE 0x07b8
Petko Manolov58592712006-12-04 14:27:36 +0200104#define VENDOR_ID_OQO 0x1557
Dan Streetmanb6c27992006-07-05 19:17:27 -0400105#define VENDOR_ID_ZYXEL 0x0586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107#define PRODUCT_ID_RTL8150 0x8150
108#define PRODUCT_ID_LUAKTX 0x0012
109#define PRODUCT_ID_LCS8138TX 0x401a
110#define PRODUCT_ID_SP128AR 0x0003
Dan Streetmanb6c27992006-07-05 19:17:27 -0400111#define PRODUCT_ID_PRESTIGE 0x401a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113#undef EEPROM_WRITE
114
115/* table of devices that work with this driver */
116static struct usb_device_id rtl8150_table[] = {
117 {USB_DEVICE(VENDOR_ID_REALTEK, PRODUCT_ID_RTL8150)},
118 {USB_DEVICE(VENDOR_ID_MELCO, PRODUCT_ID_LUAKTX)},
119 {USB_DEVICE(VENDOR_ID_MICRONET, PRODUCT_ID_SP128AR)},
120 {USB_DEVICE(VENDOR_ID_LONGSHINE, PRODUCT_ID_LCS8138TX)},
Petko Manolov58592712006-12-04 14:27:36 +0200121 {USB_DEVICE(VENDOR_ID_OQO, PRODUCT_ID_RTL8150)},
Dan Streetmanb6c27992006-07-05 19:17:27 -0400122 {USB_DEVICE(VENDOR_ID_ZYXEL, PRODUCT_ID_PRESTIGE)},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 {}
124};
125
126MODULE_DEVICE_TABLE(usb, rtl8150_table);
127
128struct rtl8150 {
129 unsigned long flags;
130 struct usb_device *udev;
131 struct tasklet_struct tl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 struct net_device *netdev;
133 struct urb *rx_urb, *tx_urb, *intr_urb, *ctrl_urb;
134 struct sk_buff *tx_skb, *rx_skb;
135 struct sk_buff *rx_skb_pool[RX_SKB_POOL_SIZE];
136 spinlock_t rx_pool_lock;
137 struct usb_ctrlrequest dr;
138 int intr_interval;
139 __le16 rx_creg;
140 u8 *intr_buff;
141 u8 phy;
142};
143
144typedef struct rtl8150 rtl8150_t;
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146static const char driver_name [] = "rtl8150";
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148/*
149**
150** device related part of the code
151**
152*/
153static int get_registers(rtl8150_t * dev, u16 indx, u16 size, void *data)
154{
155 return usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
156 RTL8150_REQ_GET_REGS, RTL8150_REQT_READ,
157 indx, 0, data, size, 500);
158}
159
160static int set_registers(rtl8150_t * dev, u16 indx, u16 size, void *data)
161{
162 return usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
163 RTL8150_REQ_SET_REGS, RTL8150_REQT_WRITE,
164 indx, 0, data, size, 500);
165}
166
David Howells7d12e782006-10-05 14:55:46 +0100167static void ctrl_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
169 rtl8150_t *dev;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800170 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Oliver Neukumc94cb312008-12-18 23:00:59 -0800172 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 case 0:
174 break;
175 case -EINPROGRESS:
176 break;
177 case -ENOENT:
178 break;
179 default:
André Goddard Rosa342a4372009-05-29 22:13:58 -0700180 if (printk_ratelimit())
181 dev_warn(&urb->dev->dev, "ctrl urb status %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 }
183 dev = urb->context;
184 clear_bit(RX_REG_SET, &dev->flags);
185}
186
187static int async_set_registers(rtl8150_t * dev, u16 indx, u16 size)
188{
189 int ret;
190
191 if (test_bit(RX_REG_SET, &dev->flags))
192 return -EAGAIN;
193
194 dev->dr.bRequestType = RTL8150_REQT_WRITE;
195 dev->dr.bRequest = RTL8150_REQ_SET_REGS;
196 dev->dr.wValue = cpu_to_le16(indx);
197 dev->dr.wIndex = 0;
198 dev->dr.wLength = cpu_to_le16(size);
199 dev->ctrl_urb->transfer_buffer_length = size;
200 usb_fill_control_urb(dev->ctrl_urb, dev->udev,
201 usb_sndctrlpipe(dev->udev, 0), (char *) &dev->dr,
202 &dev->rx_creg, size, ctrl_callback, dev);
Peter Chubb23219c12006-07-25 20:39:14 +1000203 if ((ret = usb_submit_urb(dev->ctrl_urb, GFP_ATOMIC))) {
204 if (ret == -ENODEV)
205 netif_device_detach(dev->netdev);
Greg Kroah-Hartman21f52432012-04-25 12:37:49 -0700206 dev_err(&dev->udev->dev,
207 "control request submission failed: %d\n", ret);
Peter Chubb23219c12006-07-25 20:39:14 +1000208 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 set_bit(RX_REG_SET, &dev->flags);
210
211 return ret;
212}
213
214static int read_mii_word(rtl8150_t * dev, u8 phy, __u8 indx, u16 * reg)
215{
216 int i;
217 u8 data[3], tmp;
218
219 data[0] = phy;
220 data[1] = data[2] = 0;
221 tmp = indx | PHY_READ | PHY_GO;
222 i = 0;
223
224 set_registers(dev, PHYADD, sizeof(data), data);
225 set_registers(dev, PHYCNT, 1, &tmp);
226 do {
227 get_registers(dev, PHYCNT, 1, data);
228 } while ((data[0] & PHY_GO) && (i++ < MII_TIMEOUT));
229
roel kluinc064efc2009-12-27 11:22:08 +0000230 if (i <= MII_TIMEOUT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 get_registers(dev, PHYDAT, 2, data);
232 *reg = data[0] | (data[1] << 8);
233 return 0;
234 } else
235 return 1;
236}
237
238static int write_mii_word(rtl8150_t * dev, u8 phy, __u8 indx, u16 reg)
239{
240 int i;
241 u8 data[3], tmp;
242
243 data[0] = phy;
Al Viro886ae1f2007-02-04 03:02:17 +0000244 data[1] = reg & 0xff;
245 data[2] = (reg >> 8) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 tmp = indx | PHY_WRITE | 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 return 0;
257 else
258 return 1;
259}
260
261static inline void set_ethernet_addr(rtl8150_t * dev)
262{
263 u8 node_id[6];
264
265 get_registers(dev, IDR, sizeof(node_id), node_id);
266 memcpy(dev->netdev->dev_addr, node_id, sizeof(node_id));
267}
268
269static int rtl8150_set_mac_address(struct net_device *netdev, void *p)
270{
271 struct sockaddr *addr = p;
272 rtl8150_t *dev = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 if (netif_running(netdev))
275 return -EBUSY;
276
277 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
H Hartley Sweetend649a282009-12-29 20:03:28 -0800278 dbg("%s: Setting MAC address to %pM\n", netdev->name, netdev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 /* Set the IDR registers. */
Julia Lawall60579122009-12-13 05:47:04 +0000280 set_registers(dev, IDR, netdev->addr_len, netdev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281#ifdef EEPROM_WRITE
282 {
H Hartley Sweetend649a282009-12-29 20:03:28 -0800283 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 u8 cr;
285 /* Get the CR contents. */
286 get_registers(dev, CR, 1, &cr);
287 /* Set the WEPROM bit (eeprom write enable). */
288 cr |= 0x20;
289 set_registers(dev, CR, 1, &cr);
290 /* Write the MAC address into eeprom. Eeprom writes must be word-sized,
291 so we need to split them up. */
292 for (i = 0; i * 2 < netdev->addr_len; i++) {
françois romieu141b9e62011-09-30 00:38:29 +0000293 set_registers(dev, IDR_EEPROM + (i * 2), 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 netdev->dev_addr + (i * 2));
295 }
296 /* Clear the WEPROM bit (preventing accidental eeprom writes). */
297 cr &= 0xdf;
298 set_registers(dev, CR, 1, &cr);
299 }
300#endif
301 return 0;
302}
303
304static int rtl8150_reset(rtl8150_t * dev)
305{
306 u8 data = 0x10;
307 int i = HZ;
308
309 set_registers(dev, CR, 1, &data);
310 do {
311 get_registers(dev, CR, 1, &data);
312 } while ((data & 0x10) && --i);
313
314 return (i > 0) ? 1 : 0;
315}
316
317static int alloc_all_urbs(rtl8150_t * dev)
318{
319 dev->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
320 if (!dev->rx_urb)
321 return 0;
322 dev->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
323 if (!dev->tx_urb) {
324 usb_free_urb(dev->rx_urb);
325 return 0;
326 }
327 dev->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
328 if (!dev->intr_urb) {
329 usb_free_urb(dev->rx_urb);
330 usb_free_urb(dev->tx_urb);
331 return 0;
332 }
333 dev->ctrl_urb = usb_alloc_urb(0, GFP_KERNEL);
Robert P. J. Day7fdba2f2008-03-09 20:44:52 -0400334 if (!dev->ctrl_urb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 usb_free_urb(dev->rx_urb);
336 usb_free_urb(dev->tx_urb);
337 usb_free_urb(dev->intr_urb);
338 return 0;
339 }
340
341 return 1;
342}
343
344static void free_all_urbs(rtl8150_t * dev)
345{
346 usb_free_urb(dev->rx_urb);
347 usb_free_urb(dev->tx_urb);
348 usb_free_urb(dev->intr_urb);
349 usb_free_urb(dev->ctrl_urb);
350}
351
352static void unlink_all_urbs(rtl8150_t * dev)
353{
354 usb_kill_urb(dev->rx_urb);
355 usb_kill_urb(dev->tx_urb);
356 usb_kill_urb(dev->intr_urb);
357 usb_kill_urb(dev->ctrl_urb);
358}
359
360static inline struct sk_buff *pull_skb(rtl8150_t *dev)
361{
362 struct sk_buff *skb;
363 int i;
364
365 for (i = 0; i < RX_SKB_POOL_SIZE; i++) {
366 if (dev->rx_skb_pool[i]) {
367 skb = dev->rx_skb_pool[i];
368 dev->rx_skb_pool[i] = NULL;
369 return skb;
370 }
371 }
372 return NULL;
373}
374
David Howells7d12e782006-10-05 14:55:46 +0100375static void read_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
377 rtl8150_t *dev;
378 unsigned pkt_len, res;
379 struct sk_buff *skb;
380 struct net_device *netdev;
381 u16 rx_stat;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800382 int status = urb->status;
383 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 dev = urb->context;
386 if (!dev)
387 return;
388 if (test_bit(RTL8150_UNPLUG, &dev->flags))
389 return;
390 netdev = dev->netdev;
391 if (!netif_device_present(netdev))
392 return;
393
Oliver Neukumc94cb312008-12-18 23:00:59 -0800394 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 case 0:
396 break;
397 case -ENOENT:
398 return; /* the urb is in unlink state */
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700399 case -ETIME:
André Goddard Rosa342a4372009-05-29 22:13:58 -0700400 if (printk_ratelimit())
401 dev_warn(&urb->dev->dev, "may be reset is needed?..\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 goto goon;
403 default:
André Goddard Rosa342a4372009-05-29 22:13:58 -0700404 if (printk_ratelimit())
405 dev_warn(&urb->dev->dev, "Rx status %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 goto goon;
407 }
408
409 if (!dev->rx_skb)
410 goto resched;
411 /* protect against short packets (tell me why we got some?!?) */
412 if (urb->actual_length < 4)
413 goto goon;
414
415 res = urb->actual_length;
416 rx_stat = le16_to_cpu(*(__le16 *)(urb->transfer_buffer + res - 4));
417 pkt_len = res - 4;
418
419 skb_put(dev->rx_skb, pkt_len);
420 dev->rx_skb->protocol = eth_type_trans(dev->rx_skb, netdev);
421 netif_rx(dev->rx_skb);
Stephen Hemmingerb7e41e22009-03-20 19:35:50 +0000422 netdev->stats.rx_packets++;
423 netdev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 spin_lock(&dev->rx_pool_lock);
426 skb = pull_skb(dev);
427 spin_unlock(&dev->rx_pool_lock);
428 if (!skb)
429 goto resched;
430
431 dev->rx_skb = skb;
432goon:
433 usb_fill_bulk_urb(dev->rx_urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
434 dev->rx_skb->data, RTL8150_MTU, read_bulk_callback, dev);
Oliver Neukumc94cb312008-12-18 23:00:59 -0800435 result = usb_submit_urb(dev->rx_urb, GFP_ATOMIC);
436 if (result == -ENODEV)
Peter Chubb23219c12006-07-25 20:39:14 +1000437 netif_device_detach(dev->netdev);
Oliver Neukumc94cb312008-12-18 23:00:59 -0800438 else if (result) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 set_bit(RX_URB_FAIL, &dev->flags);
440 goto resched;
441 } else {
442 clear_bit(RX_URB_FAIL, &dev->flags);
443 }
444
445 return;
446resched:
447 tasklet_schedule(&dev->tl);
448}
449
David Howells7d12e782006-10-05 14:55:46 +0100450static void write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
452 rtl8150_t *dev;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800453 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 dev = urb->context;
456 if (!dev)
457 return;
458 dev_kfree_skb_irq(dev->tx_skb);
459 if (!netif_device_present(dev->netdev))
460 return;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800461 if (status)
Greg Kroah-Hartman880c9c62008-08-18 13:21:04 -0700462 dev_info(&urb->dev->dev, "%s: Tx status %d\n",
Oliver Neukumc94cb312008-12-18 23:00:59 -0800463 dev->netdev->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 dev->netdev->trans_start = jiffies;
465 netif_wake_queue(dev->netdev);
466}
467
David Howells7d12e782006-10-05 14:55:46 +0100468static void intr_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
470 rtl8150_t *dev;
471 __u8 *d;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800472 int status = urb->status;
473 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475 dev = urb->context;
476 if (!dev)
477 return;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800478 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 case 0: /* success */
480 break;
481 case -ECONNRESET: /* unlink */
482 case -ENOENT:
483 case -ESHUTDOWN:
484 return;
485 /* -EPIPE: should clear the halt */
486 default:
Greg Kroah-Hartman880c9c62008-08-18 13:21:04 -0700487 dev_info(&urb->dev->dev, "%s: intr status %d\n",
Oliver Neukumc94cb312008-12-18 23:00:59 -0800488 dev->netdev->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 goto resubmit;
490 }
491
492 d = urb->transfer_buffer;
493 if (d[0] & TSR_ERRORS) {
Stephen Hemmingerb7e41e22009-03-20 19:35:50 +0000494 dev->netdev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if (d[INT_TSR] & (TSR_ECOL | TSR_JBR))
Stephen Hemmingerb7e41e22009-03-20 19:35:50 +0000496 dev->netdev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 if (d[INT_TSR] & TSR_LCOL)
Stephen Hemmingerb7e41e22009-03-20 19:35:50 +0000498 dev->netdev->stats.tx_window_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 if (d[INT_TSR] & TSR_LOSS_CRS)
Stephen Hemmingerb7e41e22009-03-20 19:35:50 +0000500 dev->netdev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 }
502 /* Report link status changes to the network stack */
503 if ((d[INT_MSR] & MSR_LINK) == 0) {
504 if (netif_carrier_ok(dev->netdev)) {
505 netif_carrier_off(dev->netdev);
506 dbg("%s: LINK LOST\n", __func__);
507 }
508 } else {
509 if (!netif_carrier_ok(dev->netdev)) {
510 netif_carrier_on(dev->netdev);
511 dbg("%s: LINK CAME BACK\n", __func__);
512 }
513 }
514
515resubmit:
Oliver Neukumc94cb312008-12-18 23:00:59 -0800516 res = usb_submit_urb (urb, GFP_ATOMIC);
517 if (res == -ENODEV)
Peter Chubb23219c12006-07-25 20:39:14 +1000518 netif_device_detach(dev->netdev);
Oliver Neukumc94cb312008-12-18 23:00:59 -0800519 else if (res)
Greg Kroah-Hartman21f52432012-04-25 12:37:49 -0700520 dev_err(&dev->udev->dev,
521 "can't resubmit intr, %s-%s/input0, status %d\n",
522 dev->udev->bus->bus_name, dev->udev->devpath, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523}
524
Peter Chubb23219c12006-07-25 20:39:14 +1000525static int rtl8150_suspend(struct usb_interface *intf, pm_message_t message)
526{
527 rtl8150_t *dev = usb_get_intfdata(intf);
528
529 netif_device_detach(dev->netdev);
530
531 if (netif_running(dev->netdev)) {
532 usb_kill_urb(dev->rx_urb);
533 usb_kill_urb(dev->intr_urb);
534 }
535 return 0;
536}
537
538static int rtl8150_resume(struct usb_interface *intf)
539{
540 rtl8150_t *dev = usb_get_intfdata(intf);
541
542 netif_device_attach(dev->netdev);
543 if (netif_running(dev->netdev)) {
544 dev->rx_urb->status = 0;
545 dev->rx_urb->actual_length = 0;
David Howells7d12e782006-10-05 14:55:46 +0100546 read_bulk_callback(dev->rx_urb);
Peter Chubb23219c12006-07-25 20:39:14 +1000547
548 dev->intr_urb->status = 0;
549 dev->intr_urb->actual_length = 0;
David Howells7d12e782006-10-05 14:55:46 +0100550 intr_callback(dev->intr_urb);
Peter Chubb23219c12006-07-25 20:39:14 +1000551 }
552 return 0;
553}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555/*
556**
557** network related part of the code
558**
559*/
560
561static void fill_skb_pool(rtl8150_t *dev)
562{
563 struct sk_buff *skb;
564 int i;
565
566 for (i = 0; i < RX_SKB_POOL_SIZE; i++) {
567 if (dev->rx_skb_pool[i])
568 continue;
569 skb = dev_alloc_skb(RTL8150_MTU + 2);
570 if (!skb) {
571 return;
572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 skb_reserve(skb, 2);
574 dev->rx_skb_pool[i] = skb;
575 }
576}
577
578static void free_skb_pool(rtl8150_t *dev)
579{
580 int i;
581
582 for (i = 0; i < RX_SKB_POOL_SIZE; i++)
583 if (dev->rx_skb_pool[i])
584 dev_kfree_skb(dev->rx_skb_pool[i]);
585}
586
françois romieu141b9e62011-09-30 00:38:29 +0000587static void rx_fixup(unsigned long data)
588{
589 struct rtl8150 *dev = (struct rtl8150 *)data;
590 struct sk_buff *skb;
591 int status;
592
593 spin_lock_irq(&dev->rx_pool_lock);
594 fill_skb_pool(dev);
595 spin_unlock_irq(&dev->rx_pool_lock);
596 if (test_bit(RX_URB_FAIL, &dev->flags))
597 if (dev->rx_skb)
598 goto try_again;
599 spin_lock_irq(&dev->rx_pool_lock);
600 skb = pull_skb(dev);
601 spin_unlock_irq(&dev->rx_pool_lock);
602 if (skb == NULL)
603 goto tlsched;
604 dev->rx_skb = skb;
605 usb_fill_bulk_urb(dev->rx_urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
606 dev->rx_skb->data, RTL8150_MTU, read_bulk_callback, dev);
607try_again:
608 status = usb_submit_urb(dev->rx_urb, GFP_ATOMIC);
609 if (status == -ENODEV) {
610 netif_device_detach(dev->netdev);
611 } else if (status) {
612 set_bit(RX_URB_FAIL, &dev->flags);
613 goto tlsched;
614 } else {
615 clear_bit(RX_URB_FAIL, &dev->flags);
616 }
617
618 return;
619tlsched:
620 tasklet_schedule(&dev->tl);
621}
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623static int enable_net_traffic(rtl8150_t * dev)
624{
625 u8 cr, tcr, rcr, msr;
626
627 if (!rtl8150_reset(dev)) {
Greg Kroah-Hartman4dc89942008-08-14 09:37:34 -0700628 dev_warn(&dev->udev->dev, "device reset failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 }
630 /* RCR bit7=1 attach Rx info at the end; =0 HW CRC (which is broken) */
631 rcr = 0x9e;
632 dev->rx_creg = cpu_to_le16(rcr);
633 tcr = 0xd8;
634 cr = 0x0c;
635 if (!(rcr & 0x80))
636 set_bit(RTL8150_HW_CRC, &dev->flags);
637 set_registers(dev, RCR, 1, &rcr);
638 set_registers(dev, TCR, 1, &tcr);
639 set_registers(dev, CR, 1, &cr);
640 get_registers(dev, MSR, 1, &msr);
641
642 return 0;
643}
644
645static void disable_net_traffic(rtl8150_t * dev)
646{
647 u8 cr;
648
649 get_registers(dev, CR, 1, &cr);
650 cr &= 0xf3;
651 set_registers(dev, CR, 1, &cr);
652}
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654static void rtl8150_tx_timeout(struct net_device *netdev)
655{
656 rtl8150_t *dev = netdev_priv(netdev);
Greg Kroah-Hartman4dc89942008-08-14 09:37:34 -0700657 dev_warn(&netdev->dev, "Tx timeout.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 usb_unlink_urb(dev->tx_urb);
Stephen Hemmingerb7e41e22009-03-20 19:35:50 +0000659 netdev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660}
661
662static void rtl8150_set_multicast(struct net_device *netdev)
663{
664 rtl8150_t *dev = netdev_priv(netdev);
665 netif_stop_queue(netdev);
666 if (netdev->flags & IFF_PROMISC) {
667 dev->rx_creg |= cpu_to_le16(0x0001);
Greg Kroah-Hartman880c9c62008-08-18 13:21:04 -0700668 dev_info(&netdev->dev, "%s: promiscuous mode\n", netdev->name);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000669 } else if (!netdev_mc_empty(netdev) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 (netdev->flags & IFF_ALLMULTI)) {
671 dev->rx_creg &= cpu_to_le16(0xfffe);
672 dev->rx_creg |= cpu_to_le16(0x0002);
Greg Kroah-Hartman880c9c62008-08-18 13:21:04 -0700673 dev_info(&netdev->dev, "%s: allmulti set\n", netdev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 } else {
675 /* ~RX_MULTICAST, ~RX_PROMISCUOUS */
676 dev->rx_creg &= cpu_to_le16(0x00fc);
677 }
678 async_set_registers(dev, RCR, 2);
679 netif_wake_queue(netdev);
680}
681
Stephen Hemminger25a79c42009-08-31 19:50:45 +0000682static netdev_tx_t rtl8150_start_xmit(struct sk_buff *skb,
683 struct net_device *netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
685 rtl8150_t *dev = netdev_priv(netdev);
686 int count, res;
687
688 netif_stop_queue(netdev);
689 count = (skb->len < 60) ? 60 : skb->len;
690 count = (count & 0x3f) ? count : count + 1;
691 dev->tx_skb = skb;
692 usb_fill_bulk_urb(dev->tx_urb, dev->udev, usb_sndbulkpipe(dev->udev, 2),
693 skb->data, count, write_bulk_callback, dev);
694 if ((res = usb_submit_urb(dev->tx_urb, GFP_ATOMIC))) {
Peter Chubb23219c12006-07-25 20:39:14 +1000695 /* Can we get/handle EPIPE here? */
696 if (res == -ENODEV)
697 netif_device_detach(dev->netdev);
698 else {
Greg Kroah-Hartman4dc89942008-08-14 09:37:34 -0700699 dev_warn(&netdev->dev, "failed tx_urb %d\n", res);
Stephen Hemmingerb7e41e22009-03-20 19:35:50 +0000700 netdev->stats.tx_errors++;
Peter Chubb23219c12006-07-25 20:39:14 +1000701 netif_start_queue(netdev);
702 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 } else {
Stephen Hemmingerb7e41e22009-03-20 19:35:50 +0000704 netdev->stats.tx_packets++;
705 netdev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 netdev->trans_start = jiffies;
707 }
708
Patrick McHardy6ed10652009-06-23 06:03:08 +0000709 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
711
712
713static void set_carrier(struct net_device *netdev)
714{
715 rtl8150_t *dev = netdev_priv(netdev);
716 short tmp;
717
718 get_registers(dev, CSCR, 2, &tmp);
719 if (tmp & CSCR_LINK_STATUS)
720 netif_carrier_on(netdev);
721 else
722 netif_carrier_off(netdev);
723}
724
725static int rtl8150_open(struct net_device *netdev)
726{
727 rtl8150_t *dev = netdev_priv(netdev);
728 int res;
729
730 if (dev->rx_skb == NULL)
731 dev->rx_skb = pull_skb(dev);
732 if (!dev->rx_skb)
733 return -ENOMEM;
734
735 set_registers(dev, IDR, 6, netdev->dev_addr);
françois romieu141b9e62011-09-30 00:38:29 +0000736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 usb_fill_bulk_urb(dev->rx_urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
738 dev->rx_skb->data, RTL8150_MTU, read_bulk_callback, dev);
Peter Chubb23219c12006-07-25 20:39:14 +1000739 if ((res = usb_submit_urb(dev->rx_urb, GFP_KERNEL))) {
740 if (res == -ENODEV)
741 netif_device_detach(dev->netdev);
Greg Kroah-Hartman4dc89942008-08-14 09:37:34 -0700742 dev_warn(&netdev->dev, "rx_urb submit failed: %d\n", res);
Peter Chubb23219c12006-07-25 20:39:14 +1000743 return res;
744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 usb_fill_int_urb(dev->intr_urb, dev->udev, usb_rcvintpipe(dev->udev, 3),
746 dev->intr_buff, INTBUFSIZE, intr_callback,
747 dev, dev->intr_interval);
Peter Chubb23219c12006-07-25 20:39:14 +1000748 if ((res = usb_submit_urb(dev->intr_urb, GFP_KERNEL))) {
749 if (res == -ENODEV)
750 netif_device_detach(dev->netdev);
Greg Kroah-Hartman4dc89942008-08-14 09:37:34 -0700751 dev_warn(&netdev->dev, "intr_urb submit failed: %d\n", res);
Peter Chubb23219c12006-07-25 20:39:14 +1000752 usb_kill_urb(dev->rx_urb);
753 return res;
754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 enable_net_traffic(dev);
756 set_carrier(netdev);
Peter Chubb23219c12006-07-25 20:39:14 +1000757 netif_start_queue(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 return res;
760}
761
762static int rtl8150_close(struct net_device *netdev)
763{
764 rtl8150_t *dev = netdev_priv(netdev);
765 int res = 0;
766
767 netif_stop_queue(netdev);
768 if (!test_bit(RTL8150_UNPLUG, &dev->flags))
769 disable_net_traffic(dev);
770 unlink_all_urbs(dev);
771
772 return res;
773}
774
775static void rtl8150_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *info)
776{
777 rtl8150_t *dev = netdev_priv(netdev);
778
779 strncpy(info->driver, driver_name, ETHTOOL_BUSINFO_LEN);
780 strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
781 usb_make_path(dev->udev, info->bus_info, sizeof info->bus_info);
782}
783
784static int rtl8150_get_settings(struct net_device *netdev, struct ethtool_cmd *ecmd)
785{
786 rtl8150_t *dev = netdev_priv(netdev);
787 short lpa, bmcr;
788
789 ecmd->supported = (SUPPORTED_10baseT_Half |
790 SUPPORTED_10baseT_Full |
791 SUPPORTED_100baseT_Half |
792 SUPPORTED_100baseT_Full |
793 SUPPORTED_Autoneg |
794 SUPPORTED_TP | SUPPORTED_MII);
795 ecmd->port = PORT_TP;
796 ecmd->transceiver = XCVR_INTERNAL;
797 ecmd->phy_address = dev->phy;
798 get_registers(dev, BMCR, 2, &bmcr);
799 get_registers(dev, ANLP, 2, &lpa);
800 if (bmcr & BMCR_ANENABLE) {
David Decotigny70739492011-04-27 18:32:40 +0000801 u32 speed = ((lpa & (LPA_100HALF | LPA_100FULL)) ?
802 SPEED_100 : SPEED_10);
803 ethtool_cmd_speed_set(ecmd, speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 ecmd->autoneg = AUTONEG_ENABLE;
David Decotigny70739492011-04-27 18:32:40 +0000805 if (speed == SPEED_100)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 ecmd->duplex = (lpa & LPA_100FULL) ?
807 DUPLEX_FULL : DUPLEX_HALF;
808 else
809 ecmd->duplex = (lpa & LPA_10FULL) ?
810 DUPLEX_FULL : DUPLEX_HALF;
811 } else {
812 ecmd->autoneg = AUTONEG_DISABLE;
David Decotigny70739492011-04-27 18:32:40 +0000813 ethtool_cmd_speed_set(ecmd, ((bmcr & BMCR_SPEED100) ?
814 SPEED_100 : SPEED_10));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 ecmd->duplex = (bmcr & BMCR_FULLDPLX) ?
816 DUPLEX_FULL : DUPLEX_HALF;
817 }
818 return 0;
819}
820
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700821static const struct ethtool_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 .get_drvinfo = rtl8150_get_drvinfo,
823 .get_settings = rtl8150_get_settings,
824 .get_link = ethtool_op_get_link
825};
826
827static int rtl8150_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
828{
829 rtl8150_t *dev = netdev_priv(netdev);
830 u16 *data = (u16 *) & rq->ifr_ifru;
831 int res = 0;
832
833 switch (cmd) {
834 case SIOCDEVPRIVATE:
835 data[0] = dev->phy;
836 case SIOCDEVPRIVATE + 1:
837 read_mii_word(dev, dev->phy, (data[1] & 0x1f), &data[3]);
838 break;
839 case SIOCDEVPRIVATE + 2:
840 if (!capable(CAP_NET_ADMIN))
841 return -EPERM;
842 write_mii_word(dev, dev->phy, (data[1] & 0x1f), data[2]);
843 break;
844 default:
845 res = -EOPNOTSUPP;
846 }
847
848 return res;
849}
850
Stephen Hemmingerd79f7ef2009-03-20 19:35:51 +0000851static const struct net_device_ops rtl8150_netdev_ops = {
852 .ndo_open = rtl8150_open,
853 .ndo_stop = rtl8150_close,
854 .ndo_do_ioctl = rtl8150_ioctl,
855 .ndo_start_xmit = rtl8150_start_xmit,
françois romieu141b9e62011-09-30 00:38:29 +0000856 .ndo_tx_timeout = rtl8150_tx_timeout,
Jiri Pirkoafc4b132011-08-16 06:29:01 +0000857 .ndo_set_rx_mode = rtl8150_set_multicast,
Stephen Hemmingerd79f7ef2009-03-20 19:35:51 +0000858 .ndo_set_mac_address = rtl8150_set_mac_address,
859
860 .ndo_change_mtu = eth_change_mtu,
861 .ndo_validate_addr = eth_validate_addr,
862};
863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864static int rtl8150_probe(struct usb_interface *intf,
865 const struct usb_device_id *id)
866{
867 struct usb_device *udev = interface_to_usbdev(intf);
868 rtl8150_t *dev;
869 struct net_device *netdev;
870
871 netdev = alloc_etherdev(sizeof(rtl8150_t));
Joe Perches41de8d42012-01-29 13:47:52 +0000872 if (!netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875 dev = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877 dev->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL);
878 if (!dev->intr_buff) {
879 free_netdev(netdev);
880 return -ENOMEM;
881 }
882
883 tasklet_init(&dev->tl, rx_fixup, (unsigned long)dev);
884 spin_lock_init(&dev->rx_pool_lock);
françois romieu141b9e62011-09-30 00:38:29 +0000885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 dev->udev = udev;
887 dev->netdev = netdev;
Stephen Hemmingerd79f7ef2009-03-20 19:35:51 +0000888 netdev->netdev_ops = &rtl8150_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 netdev->watchdog_timeo = RTL8150_TX_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 SET_ETHTOOL_OPS(netdev, &ops);
891 dev->intr_interval = 100; /* 100ms */
892
893 if (!alloc_all_urbs(dev)) {
Greg Kroah-Hartman21f52432012-04-25 12:37:49 -0700894 dev_err(&intf->dev, "out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 goto out;
896 }
897 if (!rtl8150_reset(dev)) {
Greg Kroah-Hartman21f52432012-04-25 12:37:49 -0700898 dev_err(&intf->dev, "couldn't reset the device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 goto out1;
900 }
901 fill_skb_pool(dev);
902 set_ethernet_addr(dev);
françois romieu141b9e62011-09-30 00:38:29 +0000903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 usb_set_intfdata(intf, dev);
905 SET_NETDEV_DEV(netdev, &intf->dev);
906 if (register_netdev(netdev) != 0) {
Greg Kroah-Hartman21f52432012-04-25 12:37:49 -0700907 dev_err(&intf->dev, "couldn't register the device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 goto out2;
909 }
Petko Manolov09abfa82006-03-15 16:29:38 +0200910
Greg Kroah-Hartman880c9c62008-08-18 13:21:04 -0700911 dev_info(&intf->dev, "%s: rtl8150 is detected\n", netdev->name);
Petko Manolov09abfa82006-03-15 16:29:38 +0200912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 return 0;
914
915out2:
916 usb_set_intfdata(intf, NULL);
917 free_skb_pool(dev);
918out1:
919 free_all_urbs(dev);
920out:
921 kfree(dev->intr_buff);
922 free_netdev(netdev);
923 return -EIO;
924}
925
926static void rtl8150_disconnect(struct usb_interface *intf)
927{
928 rtl8150_t *dev = usb_get_intfdata(intf);
929
930 usb_set_intfdata(intf, NULL);
931 if (dev) {
932 set_bit(RTL8150_UNPLUG, &dev->flags);
Andrew Mortoneff674a2006-08-14 23:11:09 -0700933 tasklet_kill(&dev->tl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 unregister_netdev(dev->netdev);
935 unlink_all_urbs(dev);
936 free_all_urbs(dev);
937 free_skb_pool(dev);
938 if (dev->rx_skb)
939 dev_kfree_skb(dev->rx_skb);
940 kfree(dev->intr_buff);
941 free_netdev(dev->netdev);
942 }
943}
944
françois romieu141b9e62011-09-30 00:38:29 +0000945static struct usb_driver rtl8150_driver = {
946 .name = driver_name,
947 .probe = rtl8150_probe,
948 .disconnect = rtl8150_disconnect,
949 .id_table = rtl8150_table,
950 .suspend = rtl8150_suspend,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -0700951 .resume = rtl8150_resume,
952 .disable_hub_initiated_lpm = 1,
françois romieu141b9e62011-09-30 00:38:29 +0000953};
954
Greg Kroah-Hartmand632eb12011-11-18 09:44:20 -0800955module_usb_driver(rtl8150_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
957MODULE_AUTHOR(DRIVER_AUTHOR);
958MODULE_DESCRIPTION(DRIVER_DESC);
959MODULE_LICENSE("GPL");