Loading...
1/*
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
9#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 */
126#define VENDOR_ID_REALTEK 0x0bda
127#define VENDOR_ID_MELCO 0x0411
128#define VENDOR_ID_MICRONET 0x3980
129#define VENDOR_ID_LONGSHINE 0x07b8
130#define VENDOR_ID_OQO 0x1557
131#define VENDOR_ID_ZYXEL 0x0586
132
133#define PRODUCT_ID_RTL8150 0x8150
134#define PRODUCT_ID_LUAKTX 0x0012
135#define PRODUCT_ID_LCS8138TX 0x401a
136#define PRODUCT_ID_SP128AR 0x0003
137#define PRODUCT_ID_PRESTIGE 0x401a
138
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)},
147 {USB_DEVICE(VENDOR_ID_OQO, PRODUCT_ID_RTL8150)},
148 {USB_DEVICE(VENDOR_ID_ZYXEL, PRODUCT_ID_PRESTIGE)},
149 {}
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;
158 struct net_device *netdev;
159 struct urb *rx_urb, *tx_urb, *intr_urb, *ctrl_urb;
160 struct sk_buff *tx_skb, *rx_skb;
161 struct sk_buff *rx_skb_pool[RX_SKB_POOL_SIZE];
162 spinlock_t rx_pool_lock;
163 struct usb_ctrlrequest dr;
164 int intr_interval;
165 __le16 rx_creg;
166 u8 *intr_buff;
167 u8 phy;
168};
169
170typedef struct rtl8150 rtl8150_t;
171
172static 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);
178static int rtl8150_suspend(struct usb_interface *intf, pm_message_t message);
179static int rtl8150_resume(struct usb_interface *intf);
180
181static const char driver_name [] = "rtl8150";
182
183static struct usb_driver rtl8150_driver = {
184 .name = driver_name,
185 .probe = rtl8150_probe,
186 .disconnect = rtl8150_disconnect,
187 .id_table = rtl8150_table,
188 .suspend = rtl8150_suspend,
189 .resume = rtl8150_resume
190};
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
211static void ctrl_callback(struct urb *urb)
212{
213 rtl8150_t *dev;
214 int status = urb->status;
215
216 switch (status) {
217 case 0:
218 break;
219 case -EINPROGRESS:
220 break;
221 case -ENOENT:
222 break;
223 default:
224 if (printk_ratelimit())
225 dev_warn(&urb->dev->dev, "ctrl urb status %d\n", status);
226 }
227 dev = urb->context;
228 clear_bit(RX_REG_SET, &dev->flags);
229}
230
231static int async_set_registers(rtl8150_t * dev, u16 indx, u16 size)
232{
233 int ret;
234
235 if (test_bit(RX_REG_SET, &dev->flags))
236 return -EAGAIN;
237
238 dev->dr.bRequestType = RTL8150_REQT_WRITE;
239 dev->dr.bRequest = RTL8150_REQ_SET_REGS;
240 dev->dr.wValue = cpu_to_le16(indx);
241 dev->dr.wIndex = 0;
242 dev->dr.wLength = cpu_to_le16(size);
243 dev->ctrl_urb->transfer_buffer_length = size;
244 usb_fill_control_urb(dev->ctrl_urb, dev->udev,
245 usb_sndctrlpipe(dev->udev, 0), (char *) &dev->dr,
246 &dev->rx_creg, size, ctrl_callback, dev);
247 if ((ret = usb_submit_urb(dev->ctrl_urb, GFP_ATOMIC))) {
248 if (ret == -ENODEV)
249 netif_device_detach(dev->netdev);
250 err("control request submission failed: %d", ret);
251 } else
252 set_bit(RX_REG_SET, &dev->flags);
253
254 return ret;
255}
256
257static int read_mii_word(rtl8150_t * dev, u8 phy, __u8 indx, u16 * reg)
258{
259 int i;
260 u8 data[3], tmp;
261
262 data[0] = phy;
263 data[1] = data[2] = 0;
264 tmp = indx | PHY_READ | PHY_GO;
265 i = 0;
266
267 set_registers(dev, PHYADD, sizeof(data), data);
268 set_registers(dev, PHYCNT, 1, &tmp);
269 do {
270 get_registers(dev, PHYCNT, 1, data);
271 } while ((data[0] & PHY_GO) && (i++ < MII_TIMEOUT));
272
273 if (i <= MII_TIMEOUT) {
274 get_registers(dev, PHYDAT, 2, data);
275 *reg = data[0] | (data[1] << 8);
276 return 0;
277 } else
278 return 1;
279}
280
281static int write_mii_word(rtl8150_t * dev, u8 phy, __u8 indx, u16 reg)
282{
283 int i;
284 u8 data[3], tmp;
285
286 data[0] = phy;
287 data[1] = reg & 0xff;
288 data[2] = (reg >> 8) & 0xff;
289 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
317 if (netif_running(netdev))
318 return -EBUSY;
319
320 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
321 dbg("%s: Setting MAC address to %pM\n", netdev->name, netdev->dev_addr);
322 /* Set the IDR registers. */
323 set_registers(dev, IDR, netdev->addr_len, netdev->dev_addr);
324#ifdef EEPROM_WRITE
325 {
326 int i;
327 u8 cr;
328 /* Get the CR contents. */
329 get_registers(dev, CR, 1, &cr);
330 /* Set the WEPROM bit (eeprom write enable). */
331 cr |= 0x20;
332 set_registers(dev, CR, 1, &cr);
333 /* Write the MAC address into eeprom. Eeprom writes must be word-sized,
334 so we need to split them up. */
335 for (i = 0; i * 2 < netdev->addr_len; i++) {
336 set_registers(dev, IDR_EEPROM + (i * 2), 2,
337 netdev->dev_addr + (i * 2));
338 }
339 /* Clear the WEPROM bit (preventing accidental eeprom writes). */
340 cr &= 0xdf;
341 set_registers(dev, CR, 1, &cr);
342 }
343#endif
344 return 0;
345}
346
347static int rtl8150_reset(rtl8150_t * dev)
348{
349 u8 data = 0x10;
350 int i = HZ;
351
352 set_registers(dev, CR, 1, &data);
353 do {
354 get_registers(dev, CR, 1, &data);
355 } while ((data & 0x10) && --i);
356
357 return (i > 0) ? 1 : 0;
358}
359
360static int alloc_all_urbs(rtl8150_t * dev)
361{
362 dev->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
363 if (!dev->rx_urb)
364 return 0;
365 dev->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
366 if (!dev->tx_urb) {
367 usb_free_urb(dev->rx_urb);
368 return 0;
369 }
370 dev->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
371 if (!dev->intr_urb) {
372 usb_free_urb(dev->rx_urb);
373 usb_free_urb(dev->tx_urb);
374 return 0;
375 }
376 dev->ctrl_urb = usb_alloc_urb(0, GFP_KERNEL);
377 if (!dev->ctrl_urb) {
378 usb_free_urb(dev->rx_urb);
379 usb_free_urb(dev->tx_urb);
380 usb_free_urb(dev->intr_urb);
381 return 0;
382 }
383
384 return 1;
385}
386
387static void free_all_urbs(rtl8150_t * dev)
388{
389 usb_free_urb(dev->rx_urb);
390 usb_free_urb(dev->tx_urb);
391 usb_free_urb(dev->intr_urb);
392 usb_free_urb(dev->ctrl_urb);
393}
394
395static void unlink_all_urbs(rtl8150_t * dev)
396{
397 usb_kill_urb(dev->rx_urb);
398 usb_kill_urb(dev->tx_urb);
399 usb_kill_urb(dev->intr_urb);
400 usb_kill_urb(dev->ctrl_urb);
401}
402
403static inline struct sk_buff *pull_skb(rtl8150_t *dev)
404{
405 struct sk_buff *skb;
406 int i;
407
408 for (i = 0; i < RX_SKB_POOL_SIZE; i++) {
409 if (dev->rx_skb_pool[i]) {
410 skb = dev->rx_skb_pool[i];
411 dev->rx_skb_pool[i] = NULL;
412 return skb;
413 }
414 }
415 return NULL;
416}
417
418static void read_bulk_callback(struct urb *urb)
419{
420 rtl8150_t *dev;
421 unsigned pkt_len, res;
422 struct sk_buff *skb;
423 struct net_device *netdev;
424 u16 rx_stat;
425 int status = urb->status;
426 int result;
427
428 dev = urb->context;
429 if (!dev)
430 return;
431 if (test_bit(RTL8150_UNPLUG, &dev->flags))
432 return;
433 netdev = dev->netdev;
434 if (!netif_device_present(netdev))
435 return;
436
437 switch (status) {
438 case 0:
439 break;
440 case -ENOENT:
441 return; /* the urb is in unlink state */
442 case -ETIME:
443 if (printk_ratelimit())
444 dev_warn(&urb->dev->dev, "may be reset is needed?..\n");
445 goto goon;
446 default:
447 if (printk_ratelimit())
448 dev_warn(&urb->dev->dev, "Rx status %d\n", status);
449 goto goon;
450 }
451
452 if (!dev->rx_skb)
453 goto resched;
454 /* protect against short packets (tell me why we got some?!?) */
455 if (urb->actual_length < 4)
456 goto goon;
457
458 res = urb->actual_length;
459 rx_stat = le16_to_cpu(*(__le16 *)(urb->transfer_buffer + res - 4));
460 pkt_len = res - 4;
461
462 skb_put(dev->rx_skb, pkt_len);
463 dev->rx_skb->protocol = eth_type_trans(dev->rx_skb, netdev);
464 netif_rx(dev->rx_skb);
465 netdev->stats.rx_packets++;
466 netdev->stats.rx_bytes += pkt_len;
467
468 spin_lock(&dev->rx_pool_lock);
469 skb = pull_skb(dev);
470 spin_unlock(&dev->rx_pool_lock);
471 if (!skb)
472 goto resched;
473
474 dev->rx_skb = skb;
475goon:
476 usb_fill_bulk_urb(dev->rx_urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
477 dev->rx_skb->data, RTL8150_MTU, read_bulk_callback, dev);
478 result = usb_submit_urb(dev->rx_urb, GFP_ATOMIC);
479 if (result == -ENODEV)
480 netif_device_detach(dev->netdev);
481 else if (result) {
482 set_bit(RX_URB_FAIL, &dev->flags);
483 goto resched;
484 } else {
485 clear_bit(RX_URB_FAIL, &dev->flags);
486 }
487
488 return;
489resched:
490 tasklet_schedule(&dev->tl);
491}
492
493static void rx_fixup(unsigned long data)
494{
495 rtl8150_t *dev;
496 struct sk_buff *skb;
497 int status;
498
499 dev = (rtl8150_t *)data;
500
501 spin_lock_irq(&dev->rx_pool_lock);
502 fill_skb_pool(dev);
503 spin_unlock_irq(&dev->rx_pool_lock);
504 if (test_bit(RX_URB_FAIL, &dev->flags))
505 if (dev->rx_skb)
506 goto try_again;
507 spin_lock_irq(&dev->rx_pool_lock);
508 skb = pull_skb(dev);
509 spin_unlock_irq(&dev->rx_pool_lock);
510 if (skb == NULL)
511 goto tlsched;
512 dev->rx_skb = skb;
513 usb_fill_bulk_urb(dev->rx_urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
514 dev->rx_skb->data, RTL8150_MTU, read_bulk_callback, dev);
515try_again:
516 status = usb_submit_urb(dev->rx_urb, GFP_ATOMIC);
517 if (status == -ENODEV) {
518 netif_device_detach(dev->netdev);
519 } else if (status) {
520 set_bit(RX_URB_FAIL, &dev->flags);
521 goto tlsched;
522 } else {
523 clear_bit(RX_URB_FAIL, &dev->flags);
524 }
525
526 return;
527tlsched:
528 tasklet_schedule(&dev->tl);
529}
530
531static void write_bulk_callback(struct urb *urb)
532{
533 rtl8150_t *dev;
534 int status = urb->status;
535
536 dev = urb->context;
537 if (!dev)
538 return;
539 dev_kfree_skb_irq(dev->tx_skb);
540 if (!netif_device_present(dev->netdev))
541 return;
542 if (status)
543 dev_info(&urb->dev->dev, "%s: Tx status %d\n",
544 dev->netdev->name, status);
545 dev->netdev->trans_start = jiffies;
546 netif_wake_queue(dev->netdev);
547}
548
549static void intr_callback(struct urb *urb)
550{
551 rtl8150_t *dev;
552 __u8 *d;
553 int status = urb->status;
554 int res;
555
556 dev = urb->context;
557 if (!dev)
558 return;
559 switch (status) {
560 case 0: /* success */
561 break;
562 case -ECONNRESET: /* unlink */
563 case -ENOENT:
564 case -ESHUTDOWN:
565 return;
566 /* -EPIPE: should clear the halt */
567 default:
568 dev_info(&urb->dev->dev, "%s: intr status %d\n",
569 dev->netdev->name, status);
570 goto resubmit;
571 }
572
573 d = urb->transfer_buffer;
574 if (d[0] & TSR_ERRORS) {
575 dev->netdev->stats.tx_errors++;
576 if (d[INT_TSR] & (TSR_ECOL | TSR_JBR))
577 dev->netdev->stats.tx_aborted_errors++;
578 if (d[INT_TSR] & TSR_LCOL)
579 dev->netdev->stats.tx_window_errors++;
580 if (d[INT_TSR] & TSR_LOSS_CRS)
581 dev->netdev->stats.tx_carrier_errors++;
582 }
583 /* Report link status changes to the network stack */
584 if ((d[INT_MSR] & MSR_LINK) == 0) {
585 if (netif_carrier_ok(dev->netdev)) {
586 netif_carrier_off(dev->netdev);
587 dbg("%s: LINK LOST\n", __func__);
588 }
589 } else {
590 if (!netif_carrier_ok(dev->netdev)) {
591 netif_carrier_on(dev->netdev);
592 dbg("%s: LINK CAME BACK\n", __func__);
593 }
594 }
595
596resubmit:
597 res = usb_submit_urb (urb, GFP_ATOMIC);
598 if (res == -ENODEV)
599 netif_device_detach(dev->netdev);
600 else if (res)
601 err ("can't resubmit intr, %s-%s/input0, status %d",
602 dev->udev->bus->bus_name,
603 dev->udev->devpath, res);
604}
605
606static int rtl8150_suspend(struct usb_interface *intf, pm_message_t message)
607{
608 rtl8150_t *dev = usb_get_intfdata(intf);
609
610 netif_device_detach(dev->netdev);
611
612 if (netif_running(dev->netdev)) {
613 usb_kill_urb(dev->rx_urb);
614 usb_kill_urb(dev->intr_urb);
615 }
616 return 0;
617}
618
619static int rtl8150_resume(struct usb_interface *intf)
620{
621 rtl8150_t *dev = usb_get_intfdata(intf);
622
623 netif_device_attach(dev->netdev);
624 if (netif_running(dev->netdev)) {
625 dev->rx_urb->status = 0;
626 dev->rx_urb->actual_length = 0;
627 read_bulk_callback(dev->rx_urb);
628
629 dev->intr_urb->status = 0;
630 dev->intr_urb->actual_length = 0;
631 intr_callback(dev->intr_urb);
632 }
633 return 0;
634}
635
636/*
637**
638** network related part of the code
639**
640*/
641
642static void fill_skb_pool(rtl8150_t *dev)
643{
644 struct sk_buff *skb;
645 int i;
646
647 for (i = 0; i < RX_SKB_POOL_SIZE; i++) {
648 if (dev->rx_skb_pool[i])
649 continue;
650 skb = dev_alloc_skb(RTL8150_MTU + 2);
651 if (!skb) {
652 return;
653 }
654 skb_reserve(skb, 2);
655 dev->rx_skb_pool[i] = skb;
656 }
657}
658
659static void free_skb_pool(rtl8150_t *dev)
660{
661 int i;
662
663 for (i = 0; i < RX_SKB_POOL_SIZE; i++)
664 if (dev->rx_skb_pool[i])
665 dev_kfree_skb(dev->rx_skb_pool[i]);
666}
667
668static int enable_net_traffic(rtl8150_t * dev)
669{
670 u8 cr, tcr, rcr, msr;
671
672 if (!rtl8150_reset(dev)) {
673 dev_warn(&dev->udev->dev, "device reset failed\n");
674 }
675 /* RCR bit7=1 attach Rx info at the end; =0 HW CRC (which is broken) */
676 rcr = 0x9e;
677 dev->rx_creg = cpu_to_le16(rcr);
678 tcr = 0xd8;
679 cr = 0x0c;
680 if (!(rcr & 0x80))
681 set_bit(RTL8150_HW_CRC, &dev->flags);
682 set_registers(dev, RCR, 1, &rcr);
683 set_registers(dev, TCR, 1, &tcr);
684 set_registers(dev, CR, 1, &cr);
685 get_registers(dev, MSR, 1, &msr);
686
687 return 0;
688}
689
690static void disable_net_traffic(rtl8150_t * dev)
691{
692 u8 cr;
693
694 get_registers(dev, CR, 1, &cr);
695 cr &= 0xf3;
696 set_registers(dev, CR, 1, &cr);
697}
698
699static void rtl8150_tx_timeout(struct net_device *netdev)
700{
701 rtl8150_t *dev = netdev_priv(netdev);
702 dev_warn(&netdev->dev, "Tx timeout.\n");
703 usb_unlink_urb(dev->tx_urb);
704 netdev->stats.tx_errors++;
705}
706
707static void rtl8150_set_multicast(struct net_device *netdev)
708{
709 rtl8150_t *dev = netdev_priv(netdev);
710 netif_stop_queue(netdev);
711 if (netdev->flags & IFF_PROMISC) {
712 dev->rx_creg |= cpu_to_le16(0x0001);
713 dev_info(&netdev->dev, "%s: promiscuous mode\n", netdev->name);
714 } else if (!netdev_mc_empty(netdev) ||
715 (netdev->flags & IFF_ALLMULTI)) {
716 dev->rx_creg &= cpu_to_le16(0xfffe);
717 dev->rx_creg |= cpu_to_le16(0x0002);
718 dev_info(&netdev->dev, "%s: allmulti set\n", netdev->name);
719 } else {
720 /* ~RX_MULTICAST, ~RX_PROMISCUOUS */
721 dev->rx_creg &= cpu_to_le16(0x00fc);
722 }
723 async_set_registers(dev, RCR, 2);
724 netif_wake_queue(netdev);
725}
726
727static netdev_tx_t rtl8150_start_xmit(struct sk_buff *skb,
728 struct net_device *netdev)
729{
730 rtl8150_t *dev = netdev_priv(netdev);
731 int count, res;
732
733 netif_stop_queue(netdev);
734 count = (skb->len < 60) ? 60 : skb->len;
735 count = (count & 0x3f) ? count : count + 1;
736 dev->tx_skb = skb;
737 usb_fill_bulk_urb(dev->tx_urb, dev->udev, usb_sndbulkpipe(dev->udev, 2),
738 skb->data, count, write_bulk_callback, dev);
739 if ((res = usb_submit_urb(dev->tx_urb, GFP_ATOMIC))) {
740 /* Can we get/handle EPIPE here? */
741 if (res == -ENODEV)
742 netif_device_detach(dev->netdev);
743 else {
744 dev_warn(&netdev->dev, "failed tx_urb %d\n", res);
745 netdev->stats.tx_errors++;
746 netif_start_queue(netdev);
747 }
748 } else {
749 netdev->stats.tx_packets++;
750 netdev->stats.tx_bytes += skb->len;
751 netdev->trans_start = jiffies;
752 }
753
754 return NETDEV_TX_OK;
755}
756
757
758static void set_carrier(struct net_device *netdev)
759{
760 rtl8150_t *dev = netdev_priv(netdev);
761 short tmp;
762
763 get_registers(dev, CSCR, 2, &tmp);
764 if (tmp & CSCR_LINK_STATUS)
765 netif_carrier_on(netdev);
766 else
767 netif_carrier_off(netdev);
768}
769
770static int rtl8150_open(struct net_device *netdev)
771{
772 rtl8150_t *dev = netdev_priv(netdev);
773 int res;
774
775 if (dev->rx_skb == NULL)
776 dev->rx_skb = pull_skb(dev);
777 if (!dev->rx_skb)
778 return -ENOMEM;
779
780 set_registers(dev, IDR, 6, netdev->dev_addr);
781
782 usb_fill_bulk_urb(dev->rx_urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
783 dev->rx_skb->data, RTL8150_MTU, read_bulk_callback, dev);
784 if ((res = usb_submit_urb(dev->rx_urb, GFP_KERNEL))) {
785 if (res == -ENODEV)
786 netif_device_detach(dev->netdev);
787 dev_warn(&netdev->dev, "rx_urb submit failed: %d\n", res);
788 return res;
789 }
790 usb_fill_int_urb(dev->intr_urb, dev->udev, usb_rcvintpipe(dev->udev, 3),
791 dev->intr_buff, INTBUFSIZE, intr_callback,
792 dev, dev->intr_interval);
793 if ((res = usb_submit_urb(dev->intr_urb, GFP_KERNEL))) {
794 if (res == -ENODEV)
795 netif_device_detach(dev->netdev);
796 dev_warn(&netdev->dev, "intr_urb submit failed: %d\n", res);
797 usb_kill_urb(dev->rx_urb);
798 return res;
799 }
800 enable_net_traffic(dev);
801 set_carrier(netdev);
802 netif_start_queue(netdev);
803
804 return res;
805}
806
807static int rtl8150_close(struct net_device *netdev)
808{
809 rtl8150_t *dev = netdev_priv(netdev);
810 int res = 0;
811
812 netif_stop_queue(netdev);
813 if (!test_bit(RTL8150_UNPLUG, &dev->flags))
814 disable_net_traffic(dev);
815 unlink_all_urbs(dev);
816
817 return res;
818}
819
820static void rtl8150_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *info)
821{
822 rtl8150_t *dev = netdev_priv(netdev);
823
824 strncpy(info->driver, driver_name, ETHTOOL_BUSINFO_LEN);
825 strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
826 usb_make_path(dev->udev, info->bus_info, sizeof info->bus_info);
827}
828
829static int rtl8150_get_settings(struct net_device *netdev, struct ethtool_cmd *ecmd)
830{
831 rtl8150_t *dev = netdev_priv(netdev);
832 short lpa, bmcr;
833
834 ecmd->supported = (SUPPORTED_10baseT_Half |
835 SUPPORTED_10baseT_Full |
836 SUPPORTED_100baseT_Half |
837 SUPPORTED_100baseT_Full |
838 SUPPORTED_Autoneg |
839 SUPPORTED_TP | SUPPORTED_MII);
840 ecmd->port = PORT_TP;
841 ecmd->transceiver = XCVR_INTERNAL;
842 ecmd->phy_address = dev->phy;
843 get_registers(dev, BMCR, 2, &bmcr);
844 get_registers(dev, ANLP, 2, &lpa);
845 if (bmcr & BMCR_ANENABLE) {
846 u32 speed = ((lpa & (LPA_100HALF | LPA_100FULL)) ?
847 SPEED_100 : SPEED_10);
848 ethtool_cmd_speed_set(ecmd, speed);
849 ecmd->autoneg = AUTONEG_ENABLE;
850 if (speed == SPEED_100)
851 ecmd->duplex = (lpa & LPA_100FULL) ?
852 DUPLEX_FULL : DUPLEX_HALF;
853 else
854 ecmd->duplex = (lpa & LPA_10FULL) ?
855 DUPLEX_FULL : DUPLEX_HALF;
856 } else {
857 ecmd->autoneg = AUTONEG_DISABLE;
858 ethtool_cmd_speed_set(ecmd, ((bmcr & BMCR_SPEED100) ?
859 SPEED_100 : SPEED_10));
860 ecmd->duplex = (bmcr & BMCR_FULLDPLX) ?
861 DUPLEX_FULL : DUPLEX_HALF;
862 }
863 return 0;
864}
865
866static const struct ethtool_ops ops = {
867 .get_drvinfo = rtl8150_get_drvinfo,
868 .get_settings = rtl8150_get_settings,
869 .get_link = ethtool_op_get_link
870};
871
872static int rtl8150_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
873{
874 rtl8150_t *dev = netdev_priv(netdev);
875 u16 *data = (u16 *) & rq->ifr_ifru;
876 int res = 0;
877
878 switch (cmd) {
879 case SIOCDEVPRIVATE:
880 data[0] = dev->phy;
881 case SIOCDEVPRIVATE + 1:
882 read_mii_word(dev, dev->phy, (data[1] & 0x1f), &data[3]);
883 break;
884 case SIOCDEVPRIVATE + 2:
885 if (!capable(CAP_NET_ADMIN))
886 return -EPERM;
887 write_mii_word(dev, dev->phy, (data[1] & 0x1f), data[2]);
888 break;
889 default:
890 res = -EOPNOTSUPP;
891 }
892
893 return res;
894}
895
896static const struct net_device_ops rtl8150_netdev_ops = {
897 .ndo_open = rtl8150_open,
898 .ndo_stop = rtl8150_close,
899 .ndo_do_ioctl = rtl8150_ioctl,
900 .ndo_start_xmit = rtl8150_start_xmit,
901 .ndo_tx_timeout = rtl8150_tx_timeout,
902 .ndo_set_multicast_list = rtl8150_set_multicast,
903 .ndo_set_mac_address = rtl8150_set_mac_address,
904
905 .ndo_change_mtu = eth_change_mtu,
906 .ndo_validate_addr = eth_validate_addr,
907};
908
909static int rtl8150_probe(struct usb_interface *intf,
910 const struct usb_device_id *id)
911{
912 struct usb_device *udev = interface_to_usbdev(intf);
913 rtl8150_t *dev;
914 struct net_device *netdev;
915
916 netdev = alloc_etherdev(sizeof(rtl8150_t));
917 if (!netdev) {
918 err("Out of memory");
919 return -ENOMEM;
920 }
921
922 dev = netdev_priv(netdev);
923
924 dev->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL);
925 if (!dev->intr_buff) {
926 free_netdev(netdev);
927 return -ENOMEM;
928 }
929
930 tasklet_init(&dev->tl, rx_fixup, (unsigned long)dev);
931 spin_lock_init(&dev->rx_pool_lock);
932
933 dev->udev = udev;
934 dev->netdev = netdev;
935 netdev->netdev_ops = &rtl8150_netdev_ops;
936 netdev->watchdog_timeo = RTL8150_TX_TIMEOUT;
937 SET_ETHTOOL_OPS(netdev, &ops);
938 dev->intr_interval = 100; /* 100ms */
939
940 if (!alloc_all_urbs(dev)) {
941 err("out of memory");
942 goto out;
943 }
944 if (!rtl8150_reset(dev)) {
945 err("couldn't reset the device");
946 goto out1;
947 }
948 fill_skb_pool(dev);
949 set_ethernet_addr(dev);
950
951 usb_set_intfdata(intf, dev);
952 SET_NETDEV_DEV(netdev, &intf->dev);
953 if (register_netdev(netdev) != 0) {
954 err("couldn't register the device");
955 goto out2;
956 }
957
958 dev_info(&intf->dev, "%s: rtl8150 is detected\n", netdev->name);
959
960 return 0;
961
962out2:
963 usb_set_intfdata(intf, NULL);
964 free_skb_pool(dev);
965out1:
966 free_all_urbs(dev);
967out:
968 kfree(dev->intr_buff);
969 free_netdev(netdev);
970 return -EIO;
971}
972
973static void rtl8150_disconnect(struct usb_interface *intf)
974{
975 rtl8150_t *dev = usb_get_intfdata(intf);
976
977 usb_set_intfdata(intf, NULL);
978 if (dev) {
979 set_bit(RTL8150_UNPLUG, &dev->flags);
980 tasklet_kill(&dev->tl);
981 unregister_netdev(dev->netdev);
982 unlink_all_urbs(dev);
983 free_all_urbs(dev);
984 free_skb_pool(dev);
985 if (dev->rx_skb)
986 dev_kfree_skb(dev->rx_skb);
987 kfree(dev->intr_buff);
988 free_netdev(dev->netdev);
989 }
990}
991
992static int __init usb_rtl8150_init(void)
993{
994 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
995 DRIVER_DESC "\n");
996 return usb_register(&rtl8150_driver);
997}
998
999static void __exit usb_rtl8150_exit(void)
1000{
1001 usb_deregister(&rtl8150_driver);
1002}
1003
1004module_init(usb_rtl8150_init);
1005module_exit(usb_rtl8150_exit);
1006
1007MODULE_AUTHOR(DRIVER_AUTHOR);
1008MODULE_DESCRIPTION(DRIVER_DESC);
1009MODULE_LICENSE("GPL");
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (c) 2002 Petko Manolov (petkan@users.sourceforge.net)
4 */
5
6#include <linux/signal.h>
7#include <linux/slab.h>
8#include <linux/module.h>
9#include <linux/netdevice.h>
10#include <linux/etherdevice.h>
11#include <linux/mii.h>
12#include <linux/ethtool.h>
13#include <linux/usb.h>
14#include <linux/uaccess.h>
15
16/* Version Information */
17#define DRIVER_VERSION "v0.6.2 (2004/08/27)"
18#define DRIVER_AUTHOR "Petko Manolov <petkan@users.sourceforge.net>"
19#define DRIVER_DESC "rtl8150 based usb-ethernet driver"
20
21#define IDR 0x0120
22#define MAR 0x0126
23#define CR 0x012e
24#define TCR 0x012f
25#define RCR 0x0130
26#define TSR 0x0132
27#define RSR 0x0133
28#define CON0 0x0135
29#define CON1 0x0136
30#define MSR 0x0137
31#define PHYADD 0x0138
32#define PHYDAT 0x0139
33#define PHYCNT 0x013b
34#define GPPC 0x013d
35#define BMCR 0x0140
36#define BMSR 0x0142
37#define ANAR 0x0144
38#define ANLP 0x0146
39#define AER 0x0148
40#define CSCR 0x014C /* This one has the link status */
41#define CSCR_LINK_STATUS (1 << 3)
42
43#define IDR_EEPROM 0x1202
44
45#define PHY_READ 0
46#define PHY_WRITE 0x20
47#define PHY_GO 0x40
48
49#define MII_TIMEOUT 10
50#define INTBUFSIZE 8
51
52#define RTL8150_REQT_READ 0xc0
53#define RTL8150_REQT_WRITE 0x40
54#define RTL8150_REQ_GET_REGS 0x05
55#define RTL8150_REQ_SET_REGS 0x05
56
57
58/* Transmit status register errors */
59#define TSR_ECOL (1<<5)
60#define TSR_LCOL (1<<4)
61#define TSR_LOSS_CRS (1<<3)
62#define TSR_JBR (1<<2)
63#define TSR_ERRORS (TSR_ECOL | TSR_LCOL | TSR_LOSS_CRS | TSR_JBR)
64/* Receive status register errors */
65#define RSR_CRC (1<<2)
66#define RSR_FAE (1<<1)
67#define RSR_ERRORS (RSR_CRC | RSR_FAE)
68
69/* Media status register definitions */
70#define MSR_DUPLEX (1<<4)
71#define MSR_SPEED (1<<3)
72#define MSR_LINK (1<<2)
73
74/* USB endpoints */
75enum rtl8150_usb_ep {
76 RTL8150_USB_EP_CONTROL = 0,
77 RTL8150_USB_EP_BULK_IN = 1,
78 RTL8150_USB_EP_BULK_OUT = 2,
79 RTL8150_USB_EP_INT_IN = 3,
80};
81
82/* Interrupt pipe data */
83#define INT_TSR 0x00
84#define INT_RSR 0x01
85#define INT_MSR 0x02
86#define INT_WAKSR 0x03
87#define INT_TXOK_CNT 0x04
88#define INT_RXLOST_CNT 0x05
89#define INT_CRERR_CNT 0x06
90#define INT_COL_CNT 0x07
91
92
93#define RTL8150_MTU 1540
94#define RTL8150_TX_TIMEOUT (HZ)
95#define RX_SKB_POOL_SIZE 4
96
97/* rtl8150 flags */
98#define RTL8150_HW_CRC 0
99#define RX_REG_SET 1
100#define RTL8150_UNPLUG 2
101#define RX_URB_FAIL 3
102
103/* Define these values to match your device */
104#define VENDOR_ID_REALTEK 0x0bda
105#define VENDOR_ID_MELCO 0x0411
106#define VENDOR_ID_MICRONET 0x3980
107#define VENDOR_ID_LONGSHINE 0x07b8
108#define VENDOR_ID_OQO 0x1557
109#define VENDOR_ID_ZYXEL 0x0586
110
111#define PRODUCT_ID_RTL8150 0x8150
112#define PRODUCT_ID_LUAKTX 0x0012
113#define PRODUCT_ID_LCS8138TX 0x401a
114#define PRODUCT_ID_SP128AR 0x0003
115#define PRODUCT_ID_PRESTIGE 0x401a
116
117#undef EEPROM_WRITE
118
119/* table of devices that work with this driver */
120static const struct usb_device_id rtl8150_table[] = {
121 {USB_DEVICE(VENDOR_ID_REALTEK, PRODUCT_ID_RTL8150)},
122 {USB_DEVICE(VENDOR_ID_MELCO, PRODUCT_ID_LUAKTX)},
123 {USB_DEVICE(VENDOR_ID_MICRONET, PRODUCT_ID_SP128AR)},
124 {USB_DEVICE(VENDOR_ID_LONGSHINE, PRODUCT_ID_LCS8138TX)},
125 {USB_DEVICE(VENDOR_ID_OQO, PRODUCT_ID_RTL8150)},
126 {USB_DEVICE(VENDOR_ID_ZYXEL, PRODUCT_ID_PRESTIGE)},
127 {}
128};
129
130MODULE_DEVICE_TABLE(usb, rtl8150_table);
131
132struct rtl8150 {
133 unsigned long flags;
134 struct usb_device *udev;
135 struct tasklet_struct tl;
136 struct net_device *netdev;
137 struct urb *rx_urb, *tx_urb, *intr_urb;
138 struct sk_buff *tx_skb, *rx_skb;
139 struct sk_buff *rx_skb_pool[RX_SKB_POOL_SIZE];
140 spinlock_t rx_pool_lock;
141 struct usb_ctrlrequest dr;
142 int intr_interval;
143 u8 *intr_buff;
144 u8 phy;
145};
146
147typedef struct rtl8150 rtl8150_t;
148
149struct async_req {
150 struct usb_ctrlrequest dr;
151 u16 rx_creg;
152};
153
154static const char driver_name [] = "rtl8150";
155
156/*
157**
158** device related part of the code
159**
160*/
161static int get_registers(rtl8150_t * dev, u16 indx, u16 size, void *data)
162{
163 return usb_control_msg_recv(dev->udev, 0, RTL8150_REQ_GET_REGS,
164 RTL8150_REQT_READ, indx, 0, data, size,
165 1000, GFP_NOIO);
166}
167
168static int set_registers(rtl8150_t * dev, u16 indx, u16 size, const void *data)
169{
170 return usb_control_msg_send(dev->udev, 0, RTL8150_REQ_SET_REGS,
171 RTL8150_REQT_WRITE, indx, 0, data, size,
172 1000, GFP_NOIO);
173}
174
175static void async_set_reg_cb(struct urb *urb)
176{
177 struct async_req *req = (struct async_req *)urb->context;
178 int status = urb->status;
179
180 if (status < 0)
181 dev_dbg(&urb->dev->dev, "%s failed with %d", __func__, status);
182 kfree(req);
183 usb_free_urb(urb);
184}
185
186static int async_set_registers(rtl8150_t *dev, u16 indx, u16 size, u16 reg)
187{
188 int res = -ENOMEM;
189 struct urb *async_urb;
190 struct async_req *req;
191
192 req = kmalloc(sizeof(struct async_req), GFP_ATOMIC);
193 if (req == NULL)
194 return res;
195 async_urb = usb_alloc_urb(0, GFP_ATOMIC);
196 if (async_urb == NULL) {
197 kfree(req);
198 return res;
199 }
200 req->rx_creg = cpu_to_le16(reg);
201 req->dr.bRequestType = RTL8150_REQT_WRITE;
202 req->dr.bRequest = RTL8150_REQ_SET_REGS;
203 req->dr.wIndex = 0;
204 req->dr.wValue = cpu_to_le16(indx);
205 req->dr.wLength = cpu_to_le16(size);
206 usb_fill_control_urb(async_urb, dev->udev,
207 usb_sndctrlpipe(dev->udev, 0), (void *)&req->dr,
208 &req->rx_creg, size, async_set_reg_cb, req);
209 res = usb_submit_urb(async_urb, GFP_ATOMIC);
210 if (res) {
211 if (res == -ENODEV)
212 netif_device_detach(dev->netdev);
213 dev_err(&dev->udev->dev, "%s failed with %d\n", __func__, res);
214 }
215 return res;
216}
217
218static int read_mii_word(rtl8150_t * dev, u8 phy, __u8 indx, u16 * reg)
219{
220 int i;
221 u8 data[3], tmp;
222
223 data[0] = phy;
224 data[1] = data[2] = 0;
225 tmp = indx | PHY_READ | PHY_GO;
226 i = 0;
227
228 set_registers(dev, PHYADD, sizeof(data), data);
229 set_registers(dev, PHYCNT, 1, &tmp);
230 do {
231 get_registers(dev, PHYCNT, 1, data);
232 } while ((data[0] & PHY_GO) && (i++ < MII_TIMEOUT));
233
234 if (i <= MII_TIMEOUT) {
235 get_registers(dev, PHYDAT, 2, data);
236 *reg = data[0] | (data[1] << 8);
237 return 0;
238 } else
239 return 1;
240}
241
242static int write_mii_word(rtl8150_t * dev, u8 phy, __u8 indx, u16 reg)
243{
244 int i;
245 u8 data[3], tmp;
246
247 data[0] = phy;
248 data[1] = reg & 0xff;
249 data[2] = (reg >> 8) & 0xff;
250 tmp = indx | PHY_WRITE | PHY_GO;
251 i = 0;
252
253 set_registers(dev, PHYADD, sizeof(data), data);
254 set_registers(dev, PHYCNT, 1, &tmp);
255 do {
256 get_registers(dev, PHYCNT, 1, data);
257 } while ((data[0] & PHY_GO) && (i++ < MII_TIMEOUT));
258
259 if (i <= MII_TIMEOUT)
260 return 0;
261 else
262 return 1;
263}
264
265static void set_ethernet_addr(rtl8150_t *dev)
266{
267 u8 node_id[ETH_ALEN];
268 int ret;
269
270 ret = get_registers(dev, IDR, sizeof(node_id), node_id);
271
272 if (!ret) {
273 eth_hw_addr_set(dev->netdev, node_id);
274 } else {
275 eth_hw_addr_random(dev->netdev);
276 netdev_notice(dev->netdev, "Assigned a random MAC address: %pM\n",
277 dev->netdev->dev_addr);
278 }
279}
280
281static int rtl8150_set_mac_address(struct net_device *netdev, void *p)
282{
283 struct sockaddr *addr = p;
284 rtl8150_t *dev = netdev_priv(netdev);
285
286 if (netif_running(netdev))
287 return -EBUSY;
288
289 eth_hw_addr_set(netdev, addr->sa_data);
290 netdev_dbg(netdev, "Setting MAC address to %pM\n", netdev->dev_addr);
291 /* Set the IDR registers. */
292 set_registers(dev, IDR, netdev->addr_len, netdev->dev_addr);
293#ifdef EEPROM_WRITE
294 {
295 int i;
296 u8 cr;
297 /* Get the CR contents. */
298 get_registers(dev, CR, 1, &cr);
299 /* Set the WEPROM bit (eeprom write enable). */
300 cr |= 0x20;
301 set_registers(dev, CR, 1, &cr);
302 /* Write the MAC address into eeprom. Eeprom writes must be word-sized,
303 so we need to split them up. */
304 for (i = 0; i * 2 < netdev->addr_len; i++) {
305 set_registers(dev, IDR_EEPROM + (i * 2), 2,
306 netdev->dev_addr + (i * 2));
307 }
308 /* Clear the WEPROM bit (preventing accidental eeprom writes). */
309 cr &= 0xdf;
310 set_registers(dev, CR, 1, &cr);
311 }
312#endif
313 return 0;
314}
315
316static int rtl8150_reset(rtl8150_t * dev)
317{
318 u8 data = 0x10;
319 int i = HZ;
320
321 set_registers(dev, CR, 1, &data);
322 do {
323 get_registers(dev, CR, 1, &data);
324 } while ((data & 0x10) && --i);
325
326 return (i > 0) ? 1 : 0;
327}
328
329static int alloc_all_urbs(rtl8150_t * dev)
330{
331 dev->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
332 if (!dev->rx_urb)
333 return 0;
334 dev->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
335 if (!dev->tx_urb) {
336 usb_free_urb(dev->rx_urb);
337 return 0;
338 }
339 dev->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
340 if (!dev->intr_urb) {
341 usb_free_urb(dev->rx_urb);
342 usb_free_urb(dev->tx_urb);
343 return 0;
344 }
345
346 return 1;
347}
348
349static void free_all_urbs(rtl8150_t * dev)
350{
351 usb_free_urb(dev->rx_urb);
352 usb_free_urb(dev->tx_urb);
353 usb_free_urb(dev->intr_urb);
354}
355
356static void unlink_all_urbs(rtl8150_t * dev)
357{
358 usb_kill_urb(dev->rx_urb);
359 usb_kill_urb(dev->tx_urb);
360 usb_kill_urb(dev->intr_urb);
361}
362
363static inline struct sk_buff *pull_skb(rtl8150_t *dev)
364{
365 struct sk_buff *skb;
366 int i;
367
368 for (i = 0; i < RX_SKB_POOL_SIZE; i++) {
369 if (dev->rx_skb_pool[i]) {
370 skb = dev->rx_skb_pool[i];
371 dev->rx_skb_pool[i] = NULL;
372 return skb;
373 }
374 }
375 return NULL;
376}
377
378static void read_bulk_callback(struct urb *urb)
379{
380 rtl8150_t *dev;
381 unsigned pkt_len, res;
382 struct sk_buff *skb;
383 struct net_device *netdev;
384 int status = urb->status;
385 int result;
386 unsigned long flags;
387
388 dev = urb->context;
389 if (!dev)
390 return;
391 if (test_bit(RTL8150_UNPLUG, &dev->flags))
392 return;
393 netdev = dev->netdev;
394 if (!netif_device_present(netdev))
395 return;
396
397 switch (status) {
398 case 0:
399 break;
400 case -ENOENT:
401 return; /* the urb is in unlink state */
402 case -ETIME:
403 if (printk_ratelimit())
404 dev_warn(&urb->dev->dev, "may be reset is needed?..\n");
405 goto goon;
406 default:
407 if (printk_ratelimit())
408 dev_warn(&urb->dev->dev, "Rx status %d\n", status);
409 goto goon;
410 }
411
412 if (!dev->rx_skb)
413 goto resched;
414 /* protect against short packets (tell me why we got some?!?) */
415 if (urb->actual_length < 4)
416 goto goon;
417
418 res = urb->actual_length;
419 pkt_len = res - 4;
420
421 skb_put(dev->rx_skb, pkt_len);
422 dev->rx_skb->protocol = eth_type_trans(dev->rx_skb, netdev);
423 netif_rx(dev->rx_skb);
424 netdev->stats.rx_packets++;
425 netdev->stats.rx_bytes += pkt_len;
426
427 spin_lock_irqsave(&dev->rx_pool_lock, flags);
428 skb = pull_skb(dev);
429 spin_unlock_irqrestore(&dev->rx_pool_lock, flags);
430 if (!skb)
431 goto resched;
432
433 dev->rx_skb = skb;
434goon:
435 usb_fill_bulk_urb(dev->rx_urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
436 dev->rx_skb->data, RTL8150_MTU, read_bulk_callback, dev);
437 result = usb_submit_urb(dev->rx_urb, GFP_ATOMIC);
438 if (result == -ENODEV)
439 netif_device_detach(dev->netdev);
440 else if (result) {
441 set_bit(RX_URB_FAIL, &dev->flags);
442 goto resched;
443 } else {
444 clear_bit(RX_URB_FAIL, &dev->flags);
445 }
446
447 return;
448resched:
449 tasklet_schedule(&dev->tl);
450}
451
452static void write_bulk_callback(struct urb *urb)
453{
454 rtl8150_t *dev;
455 int status = urb->status;
456
457 dev = urb->context;
458 if (!dev)
459 return;
460 dev_kfree_skb_irq(dev->tx_skb);
461 if (!netif_device_present(dev->netdev))
462 return;
463 if (status)
464 dev_info(&urb->dev->dev, "%s: Tx status %d\n",
465 dev->netdev->name, status);
466 netif_trans_update(dev->netdev);
467 netif_wake_queue(dev->netdev);
468}
469
470static void intr_callback(struct urb *urb)
471{
472 rtl8150_t *dev;
473 __u8 *d;
474 int status = urb->status;
475 int res;
476
477 dev = urb->context;
478 if (!dev)
479 return;
480 switch (status) {
481 case 0: /* success */
482 break;
483 case -ECONNRESET: /* unlink */
484 case -ENOENT:
485 case -ESHUTDOWN:
486 return;
487 /* -EPIPE: should clear the halt */
488 default:
489 dev_info(&urb->dev->dev, "%s: intr status %d\n",
490 dev->netdev->name, status);
491 goto resubmit;
492 }
493
494 d = urb->transfer_buffer;
495 if (d[0] & TSR_ERRORS) {
496 dev->netdev->stats.tx_errors++;
497 if (d[INT_TSR] & (TSR_ECOL | TSR_JBR))
498 dev->netdev->stats.tx_aborted_errors++;
499 if (d[INT_TSR] & TSR_LCOL)
500 dev->netdev->stats.tx_window_errors++;
501 if (d[INT_TSR] & TSR_LOSS_CRS)
502 dev->netdev->stats.tx_carrier_errors++;
503 }
504 /* Report link status changes to the network stack */
505 if ((d[INT_MSR] & MSR_LINK) == 0) {
506 if (netif_carrier_ok(dev->netdev)) {
507 netif_carrier_off(dev->netdev);
508 netdev_dbg(dev->netdev, "%s: LINK LOST\n", __func__);
509 }
510 } else {
511 if (!netif_carrier_ok(dev->netdev)) {
512 netif_carrier_on(dev->netdev);
513 netdev_dbg(dev->netdev, "%s: LINK CAME BACK\n", __func__);
514 }
515 }
516
517resubmit:
518 res = usb_submit_urb (urb, GFP_ATOMIC);
519 if (res == -ENODEV)
520 netif_device_detach(dev->netdev);
521 else if (res)
522 dev_err(&dev->udev->dev,
523 "can't resubmit intr, %s-%s/input0, status %d\n",
524 dev->udev->bus->bus_name, dev->udev->devpath, res);
525}
526
527static int rtl8150_suspend(struct usb_interface *intf, pm_message_t message)
528{
529 rtl8150_t *dev = usb_get_intfdata(intf);
530
531 netif_device_detach(dev->netdev);
532
533 if (netif_running(dev->netdev)) {
534 usb_kill_urb(dev->rx_urb);
535 usb_kill_urb(dev->intr_urb);
536 }
537 return 0;
538}
539
540static int rtl8150_resume(struct usb_interface *intf)
541{
542 rtl8150_t *dev = usb_get_intfdata(intf);
543
544 netif_device_attach(dev->netdev);
545 if (netif_running(dev->netdev)) {
546 dev->rx_urb->status = 0;
547 dev->rx_urb->actual_length = 0;
548 read_bulk_callback(dev->rx_urb);
549
550 dev->intr_urb->status = 0;
551 dev->intr_urb->actual_length = 0;
552 intr_callback(dev->intr_urb);
553 }
554 return 0;
555}
556
557/*
558**
559** network related part of the code
560**
561*/
562
563static void fill_skb_pool(rtl8150_t *dev)
564{
565 struct sk_buff *skb;
566 int i;
567
568 for (i = 0; i < RX_SKB_POOL_SIZE; i++) {
569 if (dev->rx_skb_pool[i])
570 continue;
571 skb = dev_alloc_skb(RTL8150_MTU + 2);
572 if (!skb) {
573 return;
574 }
575 skb_reserve(skb, 2);
576 dev->rx_skb_pool[i] = skb;
577 }
578}
579
580static void free_skb_pool(rtl8150_t *dev)
581{
582 int i;
583
584 for (i = 0; i < RX_SKB_POOL_SIZE; i++)
585 dev_kfree_skb(dev->rx_skb_pool[i]);
586}
587
588static void rx_fixup(struct tasklet_struct *t)
589{
590 struct rtl8150 *dev = from_tasklet(dev, t, tl);
591 struct sk_buff *skb;
592 int status;
593
594 spin_lock_irq(&dev->rx_pool_lock);
595 fill_skb_pool(dev);
596 spin_unlock_irq(&dev->rx_pool_lock);
597 if (test_bit(RX_URB_FAIL, &dev->flags))
598 if (dev->rx_skb)
599 goto try_again;
600 spin_lock_irq(&dev->rx_pool_lock);
601 skb = pull_skb(dev);
602 spin_unlock_irq(&dev->rx_pool_lock);
603 if (skb == NULL)
604 goto tlsched;
605 dev->rx_skb = skb;
606 usb_fill_bulk_urb(dev->rx_urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
607 dev->rx_skb->data, RTL8150_MTU, read_bulk_callback, dev);
608try_again:
609 status = usb_submit_urb(dev->rx_urb, GFP_ATOMIC);
610 if (status == -ENODEV) {
611 netif_device_detach(dev->netdev);
612 } else if (status) {
613 set_bit(RX_URB_FAIL, &dev->flags);
614 goto tlsched;
615 } else {
616 clear_bit(RX_URB_FAIL, &dev->flags);
617 }
618
619 return;
620tlsched:
621 tasklet_schedule(&dev->tl);
622}
623
624static int enable_net_traffic(rtl8150_t * dev)
625{
626 u8 cr, tcr, rcr, msr;
627
628 if (!rtl8150_reset(dev)) {
629 dev_warn(&dev->udev->dev, "device reset failed\n");
630 }
631 /* RCR bit7=1 attach Rx info at the end; =0 HW CRC (which is broken) */
632 rcr = 0x9e;
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
654static void rtl8150_tx_timeout(struct net_device *netdev, unsigned int txqueue)
655{
656 rtl8150_t *dev = netdev_priv(netdev);
657 dev_warn(&netdev->dev, "Tx timeout.\n");
658 usb_unlink_urb(dev->tx_urb);
659 netdev->stats.tx_errors++;
660}
661
662static void rtl8150_set_multicast(struct net_device *netdev)
663{
664 rtl8150_t *dev = netdev_priv(netdev);
665 u16 rx_creg = 0x9e;
666
667 netif_stop_queue(netdev);
668 if (netdev->flags & IFF_PROMISC) {
669 rx_creg |= 0x0001;
670 dev_info(&netdev->dev, "%s: promiscuous mode\n", netdev->name);
671 } else if (!netdev_mc_empty(netdev) ||
672 (netdev->flags & IFF_ALLMULTI)) {
673 rx_creg &= 0xfffe;
674 rx_creg |= 0x0002;
675 dev_dbg(&netdev->dev, "%s: allmulti set\n", netdev->name);
676 } else {
677 /* ~RX_MULTICAST, ~RX_PROMISCUOUS */
678 rx_creg &= 0x00fc;
679 }
680 async_set_registers(dev, RCR, sizeof(rx_creg), rx_creg);
681 netif_wake_queue(netdev);
682}
683
684static netdev_tx_t rtl8150_start_xmit(struct sk_buff *skb,
685 struct net_device *netdev)
686{
687 rtl8150_t *dev = netdev_priv(netdev);
688 int count, res;
689
690 netif_stop_queue(netdev);
691 count = (skb->len < 60) ? 60 : skb->len;
692 count = (count & 0x3f) ? count : count + 1;
693 dev->tx_skb = skb;
694 usb_fill_bulk_urb(dev->tx_urb, dev->udev, usb_sndbulkpipe(dev->udev, 2),
695 skb->data, count, write_bulk_callback, dev);
696 if ((res = usb_submit_urb(dev->tx_urb, GFP_ATOMIC))) {
697 /* Can we get/handle EPIPE here? */
698 if (res == -ENODEV)
699 netif_device_detach(dev->netdev);
700 else {
701 dev_warn(&netdev->dev, "failed tx_urb %d\n", res);
702 netdev->stats.tx_errors++;
703 netif_start_queue(netdev);
704 }
705 } else {
706 netdev->stats.tx_packets++;
707 netdev->stats.tx_bytes += skb->len;
708 netif_trans_update(netdev);
709 }
710
711 return NETDEV_TX_OK;
712}
713
714
715static void set_carrier(struct net_device *netdev)
716{
717 rtl8150_t *dev = netdev_priv(netdev);
718 short tmp;
719
720 get_registers(dev, CSCR, 2, &tmp);
721 if (tmp & CSCR_LINK_STATUS)
722 netif_carrier_on(netdev);
723 else
724 netif_carrier_off(netdev);
725}
726
727static int rtl8150_open(struct net_device *netdev)
728{
729 rtl8150_t *dev = netdev_priv(netdev);
730 int res;
731
732 if (dev->rx_skb == NULL)
733 dev->rx_skb = pull_skb(dev);
734 if (!dev->rx_skb)
735 return -ENOMEM;
736
737 set_registers(dev, IDR, 6, netdev->dev_addr);
738
739 usb_fill_bulk_urb(dev->rx_urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
740 dev->rx_skb->data, RTL8150_MTU, read_bulk_callback, dev);
741 if ((res = usb_submit_urb(dev->rx_urb, GFP_KERNEL))) {
742 if (res == -ENODEV)
743 netif_device_detach(dev->netdev);
744 dev_warn(&netdev->dev, "rx_urb submit failed: %d\n", res);
745 return res;
746 }
747 usb_fill_int_urb(dev->intr_urb, dev->udev, usb_rcvintpipe(dev->udev, 3),
748 dev->intr_buff, INTBUFSIZE, intr_callback,
749 dev, dev->intr_interval);
750 if ((res = usb_submit_urb(dev->intr_urb, GFP_KERNEL))) {
751 if (res == -ENODEV)
752 netif_device_detach(dev->netdev);
753 dev_warn(&netdev->dev, "intr_urb submit failed: %d\n", res);
754 usb_kill_urb(dev->rx_urb);
755 return res;
756 }
757 enable_net_traffic(dev);
758 set_carrier(netdev);
759 netif_start_queue(netdev);
760
761 return res;
762}
763
764static int rtl8150_close(struct net_device *netdev)
765{
766 rtl8150_t *dev = netdev_priv(netdev);
767
768 netif_stop_queue(netdev);
769 if (!test_bit(RTL8150_UNPLUG, &dev->flags))
770 disable_net_traffic(dev);
771 unlink_all_urbs(dev);
772
773 return 0;
774}
775
776static void rtl8150_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *info)
777{
778 rtl8150_t *dev = netdev_priv(netdev);
779
780 strscpy(info->driver, driver_name, sizeof(info->driver));
781 strscpy(info->version, DRIVER_VERSION, sizeof(info->version));
782 usb_make_path(dev->udev, info->bus_info, sizeof(info->bus_info));
783}
784
785static int rtl8150_get_link_ksettings(struct net_device *netdev,
786 struct ethtool_link_ksettings *ecmd)
787{
788 rtl8150_t *dev = netdev_priv(netdev);
789 short lpa = 0;
790 short bmcr = 0;
791 u32 supported;
792
793 supported = (SUPPORTED_10baseT_Half |
794 SUPPORTED_10baseT_Full |
795 SUPPORTED_100baseT_Half |
796 SUPPORTED_100baseT_Full |
797 SUPPORTED_Autoneg |
798 SUPPORTED_TP | SUPPORTED_MII);
799 ecmd->base.port = PORT_TP;
800 ecmd->base.phy_address = dev->phy;
801 get_registers(dev, BMCR, 2, &bmcr);
802 get_registers(dev, ANLP, 2, &lpa);
803 if (bmcr & BMCR_ANENABLE) {
804 u32 speed = ((lpa & (LPA_100HALF | LPA_100FULL)) ?
805 SPEED_100 : SPEED_10);
806 ecmd->base.speed = speed;
807 ecmd->base.autoneg = AUTONEG_ENABLE;
808 if (speed == SPEED_100)
809 ecmd->base.duplex = (lpa & LPA_100FULL) ?
810 DUPLEX_FULL : DUPLEX_HALF;
811 else
812 ecmd->base.duplex = (lpa & LPA_10FULL) ?
813 DUPLEX_FULL : DUPLEX_HALF;
814 } else {
815 ecmd->base.autoneg = AUTONEG_DISABLE;
816 ecmd->base.speed = ((bmcr & BMCR_SPEED100) ?
817 SPEED_100 : SPEED_10);
818 ecmd->base.duplex = (bmcr & BMCR_FULLDPLX) ?
819 DUPLEX_FULL : DUPLEX_HALF;
820 }
821
822 ethtool_convert_legacy_u32_to_link_mode(ecmd->link_modes.supported,
823 supported);
824
825 return 0;
826}
827
828static const struct ethtool_ops ops = {
829 .get_drvinfo = rtl8150_get_drvinfo,
830 .get_link = ethtool_op_get_link,
831 .get_link_ksettings = rtl8150_get_link_ksettings,
832};
833
834static int rtl8150_siocdevprivate(struct net_device *netdev, struct ifreq *rq,
835 void __user *udata, int cmd)
836{
837 rtl8150_t *dev = netdev_priv(netdev);
838 u16 *data = (u16 *) & rq->ifr_ifru;
839 int res = 0;
840
841 switch (cmd) {
842 case SIOCDEVPRIVATE:
843 data[0] = dev->phy;
844 fallthrough;
845 case SIOCDEVPRIVATE + 1:
846 read_mii_word(dev, dev->phy, (data[1] & 0x1f), &data[3]);
847 break;
848 case SIOCDEVPRIVATE + 2:
849 if (!capable(CAP_NET_ADMIN))
850 return -EPERM;
851 write_mii_word(dev, dev->phy, (data[1] & 0x1f), data[2]);
852 break;
853 default:
854 res = -EOPNOTSUPP;
855 }
856
857 return res;
858}
859
860static const struct net_device_ops rtl8150_netdev_ops = {
861 .ndo_open = rtl8150_open,
862 .ndo_stop = rtl8150_close,
863 .ndo_siocdevprivate = rtl8150_siocdevprivate,
864 .ndo_start_xmit = rtl8150_start_xmit,
865 .ndo_tx_timeout = rtl8150_tx_timeout,
866 .ndo_set_rx_mode = rtl8150_set_multicast,
867 .ndo_set_mac_address = rtl8150_set_mac_address,
868
869 .ndo_validate_addr = eth_validate_addr,
870};
871
872static int rtl8150_probe(struct usb_interface *intf,
873 const struct usb_device_id *id)
874{
875 struct usb_device *udev = interface_to_usbdev(intf);
876 rtl8150_t *dev;
877 struct net_device *netdev;
878 static const u8 bulk_ep_addr[] = {
879 RTL8150_USB_EP_BULK_IN | USB_DIR_IN,
880 RTL8150_USB_EP_BULK_OUT | USB_DIR_OUT,
881 0};
882 static const u8 int_ep_addr[] = {
883 RTL8150_USB_EP_INT_IN | USB_DIR_IN,
884 0};
885
886 netdev = alloc_etherdev(sizeof(rtl8150_t));
887 if (!netdev)
888 return -ENOMEM;
889
890 dev = netdev_priv(netdev);
891
892 dev->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL);
893 if (!dev->intr_buff) {
894 free_netdev(netdev);
895 return -ENOMEM;
896 }
897
898 /* Verify that all required endpoints are present */
899 if (!usb_check_bulk_endpoints(intf, bulk_ep_addr) ||
900 !usb_check_int_endpoints(intf, int_ep_addr)) {
901 dev_err(&intf->dev, "couldn't find required endpoints\n");
902 goto out;
903 }
904
905 tasklet_setup(&dev->tl, rx_fixup);
906 spin_lock_init(&dev->rx_pool_lock);
907
908 dev->udev = udev;
909 dev->netdev = netdev;
910 netdev->netdev_ops = &rtl8150_netdev_ops;
911 netdev->watchdog_timeo = RTL8150_TX_TIMEOUT;
912 netdev->ethtool_ops = &ops;
913 dev->intr_interval = 100; /* 100ms */
914
915 if (!alloc_all_urbs(dev)) {
916 dev_err(&intf->dev, "out of memory\n");
917 goto out;
918 }
919 if (!rtl8150_reset(dev)) {
920 dev_err(&intf->dev, "couldn't reset the device\n");
921 goto out1;
922 }
923 fill_skb_pool(dev);
924 set_ethernet_addr(dev);
925
926 usb_set_intfdata(intf, dev);
927 SET_NETDEV_DEV(netdev, &intf->dev);
928 if (register_netdev(netdev) != 0) {
929 dev_err(&intf->dev, "couldn't register the device\n");
930 goto out2;
931 }
932
933 dev_info(&intf->dev, "%s: rtl8150 is detected\n", netdev->name);
934
935 return 0;
936
937out2:
938 usb_set_intfdata(intf, NULL);
939 free_skb_pool(dev);
940out1:
941 free_all_urbs(dev);
942out:
943 kfree(dev->intr_buff);
944 free_netdev(netdev);
945 return -EIO;
946}
947
948static void rtl8150_disconnect(struct usb_interface *intf)
949{
950 rtl8150_t *dev = usb_get_intfdata(intf);
951
952 usb_set_intfdata(intf, NULL);
953 if (dev) {
954 set_bit(RTL8150_UNPLUG, &dev->flags);
955 tasklet_kill(&dev->tl);
956 unregister_netdev(dev->netdev);
957 unlink_all_urbs(dev);
958 free_all_urbs(dev);
959 free_skb_pool(dev);
960 dev_kfree_skb(dev->rx_skb);
961 kfree(dev->intr_buff);
962 free_netdev(dev->netdev);
963 }
964}
965
966static struct usb_driver rtl8150_driver = {
967 .name = driver_name,
968 .probe = rtl8150_probe,
969 .disconnect = rtl8150_disconnect,
970 .id_table = rtl8150_table,
971 .suspend = rtl8150_suspend,
972 .resume = rtl8150_resume,
973 .disable_hub_initiated_lpm = 1,
974};
975
976module_usb_driver(rtl8150_driver);
977
978MODULE_AUTHOR(DRIVER_AUTHOR);
979MODULE_DESCRIPTION(DRIVER_DESC);
980MODULE_LICENSE("GPL");