]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/net/usb/rtl8150.c
a491d3a95393e4c533918c572ab7ba9256fa48ec
[mirror_ubuntu-artful-kernel.git] / drivers / net / usb / rtl8150.c
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
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 */
100 #define VENDOR_ID_REALTEK 0x0bda
101 #define VENDOR_ID_MELCO 0x0411
102 #define VENDOR_ID_MICRONET 0x3980
103 #define VENDOR_ID_LONGSHINE 0x07b8
104 #define VENDOR_ID_OQO 0x1557
105 #define VENDOR_ID_ZYXEL 0x0586
106
107 #define PRODUCT_ID_RTL8150 0x8150
108 #define PRODUCT_ID_LUAKTX 0x0012
109 #define PRODUCT_ID_LCS8138TX 0x401a
110 #define PRODUCT_ID_SP128AR 0x0003
111 #define PRODUCT_ID_PRESTIGE 0x401a
112
113 #undef EEPROM_WRITE
114
115 /* table of devices that work with this driver */
116 static 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)},
121 {USB_DEVICE(VENDOR_ID_OQO, PRODUCT_ID_RTL8150)},
122 {USB_DEVICE(VENDOR_ID_ZYXEL, PRODUCT_ID_PRESTIGE)},
123 {}
124 };
125
126 MODULE_DEVICE_TABLE(usb, rtl8150_table);
127
128 struct rtl8150 {
129 unsigned long flags;
130 struct usb_device *udev;
131 struct tasklet_struct tl;
132 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
144 typedef struct rtl8150 rtl8150_t;
145
146 static const char driver_name [] = "rtl8150";
147
148 /*
149 **
150 ** device related part of the code
151 **
152 */
153 static 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
160 static 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
167 static void ctrl_callback(struct urb *urb)
168 {
169 rtl8150_t *dev;
170 int status = urb->status;
171
172 switch (status) {
173 case 0:
174 break;
175 case -EINPROGRESS:
176 break;
177 case -ENOENT:
178 break;
179 default:
180 if (printk_ratelimit())
181 dev_warn(&urb->dev->dev, "ctrl urb status %d\n", status);
182 }
183 dev = urb->context;
184 clear_bit(RX_REG_SET, &dev->flags);
185 }
186
187 static 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);
203 if ((ret = usb_submit_urb(dev->ctrl_urb, GFP_ATOMIC))) {
204 if (ret == -ENODEV)
205 netif_device_detach(dev->netdev);
206 dev_err(&dev->udev->dev,
207 "control request submission failed: %d\n", ret);
208 } else
209 set_bit(RX_REG_SET, &dev->flags);
210
211 return ret;
212 }
213
214 static 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
230 if (i <= MII_TIMEOUT) {
231 get_registers(dev, PHYDAT, 2, data);
232 *reg = data[0] | (data[1] << 8);
233 return 0;
234 } else
235 return 1;
236 }
237
238 static 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;
244 data[1] = reg & 0xff;
245 data[2] = (reg >> 8) & 0xff;
246 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
255 if (i <= MII_TIMEOUT)
256 return 0;
257 else
258 return 1;
259 }
260
261 static 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
269 static int rtl8150_set_mac_address(struct net_device *netdev, void *p)
270 {
271 struct sockaddr *addr = p;
272 rtl8150_t *dev = netdev_priv(netdev);
273
274 if (netif_running(netdev))
275 return -EBUSY;
276
277 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
278 netdev_dbg(netdev, "Setting MAC address to %pM\n", netdev->dev_addr);
279 /* Set the IDR registers. */
280 set_registers(dev, IDR, netdev->addr_len, netdev->dev_addr);
281 #ifdef EEPROM_WRITE
282 {
283 int i;
284 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++) {
293 set_registers(dev, IDR_EEPROM + (i * 2), 2,
294 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
304 static 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
317 static 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);
334 if (!dev->ctrl_urb) {
335 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
344 static 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
352 static 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
360 static 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
375 static void read_bulk_callback(struct urb *urb)
376 {
377 rtl8150_t *dev;
378 unsigned pkt_len, res;
379 struct sk_buff *skb;
380 struct net_device *netdev;
381 u16 rx_stat;
382 int status = urb->status;
383 int result;
384
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
394 switch (status) {
395 case 0:
396 break;
397 case -ENOENT:
398 return; /* the urb is in unlink state */
399 case -ETIME:
400 if (printk_ratelimit())
401 dev_warn(&urb->dev->dev, "may be reset is needed?..\n");
402 goto goon;
403 default:
404 if (printk_ratelimit())
405 dev_warn(&urb->dev->dev, "Rx status %d\n", status);
406 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);
422 netdev->stats.rx_packets++;
423 netdev->stats.rx_bytes += pkt_len;
424
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;
432 goon:
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);
435 result = usb_submit_urb(dev->rx_urb, GFP_ATOMIC);
436 if (result == -ENODEV)
437 netif_device_detach(dev->netdev);
438 else if (result) {
439 set_bit(RX_URB_FAIL, &dev->flags);
440 goto resched;
441 } else {
442 clear_bit(RX_URB_FAIL, &dev->flags);
443 }
444
445 return;
446 resched:
447 tasklet_schedule(&dev->tl);
448 }
449
450 static void write_bulk_callback(struct urb *urb)
451 {
452 rtl8150_t *dev;
453 int status = urb->status;
454
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;
461 if (status)
462 dev_info(&urb->dev->dev, "%s: Tx status %d\n",
463 dev->netdev->name, status);
464 dev->netdev->trans_start = jiffies;
465 netif_wake_queue(dev->netdev);
466 }
467
468 static void intr_callback(struct urb *urb)
469 {
470 rtl8150_t *dev;
471 __u8 *d;
472 int status = urb->status;
473 int res;
474
475 dev = urb->context;
476 if (!dev)
477 return;
478 switch (status) {
479 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:
487 dev_info(&urb->dev->dev, "%s: intr status %d\n",
488 dev->netdev->name, status);
489 goto resubmit;
490 }
491
492 d = urb->transfer_buffer;
493 if (d[0] & TSR_ERRORS) {
494 dev->netdev->stats.tx_errors++;
495 if (d[INT_TSR] & (TSR_ECOL | TSR_JBR))
496 dev->netdev->stats.tx_aborted_errors++;
497 if (d[INT_TSR] & TSR_LCOL)
498 dev->netdev->stats.tx_window_errors++;
499 if (d[INT_TSR] & TSR_LOSS_CRS)
500 dev->netdev->stats.tx_carrier_errors++;
501 }
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 netdev_dbg(dev->netdev, "%s: LINK LOST\n", __func__);
507 }
508 } else {
509 if (!netif_carrier_ok(dev->netdev)) {
510 netif_carrier_on(dev->netdev);
511 netdev_dbg(dev->netdev, "%s: LINK CAME BACK\n", __func__);
512 }
513 }
514
515 resubmit:
516 res = usb_submit_urb (urb, GFP_ATOMIC);
517 if (res == -ENODEV)
518 netif_device_detach(dev->netdev);
519 else if (res)
520 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);
523 }
524
525 static 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
538 static 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;
546 read_bulk_callback(dev->rx_urb);
547
548 dev->intr_urb->status = 0;
549 dev->intr_urb->actual_length = 0;
550 intr_callback(dev->intr_urb);
551 }
552 return 0;
553 }
554
555 /*
556 **
557 ** network related part of the code
558 **
559 */
560
561 static 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 }
573 skb_reserve(skb, 2);
574 dev->rx_skb_pool[i] = skb;
575 }
576 }
577
578 static 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
587 static 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);
607 try_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;
619 tlsched:
620 tasklet_schedule(&dev->tl);
621 }
622
623 static int enable_net_traffic(rtl8150_t * dev)
624 {
625 u8 cr, tcr, rcr, msr;
626
627 if (!rtl8150_reset(dev)) {
628 dev_warn(&dev->udev->dev, "device reset failed\n");
629 }
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
645 static 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
654 static void rtl8150_tx_timeout(struct net_device *netdev)
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
662 static 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);
668 dev_info(&netdev->dev, "%s: promiscuous mode\n", netdev->name);
669 } else if (!netdev_mc_empty(netdev) ||
670 (netdev->flags & IFF_ALLMULTI)) {
671 dev->rx_creg &= cpu_to_le16(0xfffe);
672 dev->rx_creg |= cpu_to_le16(0x0002);
673 dev_info(&netdev->dev, "%s: allmulti set\n", netdev->name);
674 } 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
682 static netdev_tx_t rtl8150_start_xmit(struct sk_buff *skb,
683 struct net_device *netdev)
684 {
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))) {
695 /* Can we get/handle EPIPE here? */
696 if (res == -ENODEV)
697 netif_device_detach(dev->netdev);
698 else {
699 dev_warn(&netdev->dev, "failed tx_urb %d\n", res);
700 netdev->stats.tx_errors++;
701 netif_start_queue(netdev);
702 }
703 } else {
704 netdev->stats.tx_packets++;
705 netdev->stats.tx_bytes += skb->len;
706 netdev->trans_start = jiffies;
707 }
708
709 return NETDEV_TX_OK;
710 }
711
712
713 static 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
725 static 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);
736
737 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);
739 if ((res = usb_submit_urb(dev->rx_urb, GFP_KERNEL))) {
740 if (res == -ENODEV)
741 netif_device_detach(dev->netdev);
742 dev_warn(&netdev->dev, "rx_urb submit failed: %d\n", res);
743 return res;
744 }
745 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);
748 if ((res = usb_submit_urb(dev->intr_urb, GFP_KERNEL))) {
749 if (res == -ENODEV)
750 netif_device_detach(dev->netdev);
751 dev_warn(&netdev->dev, "intr_urb submit failed: %d\n", res);
752 usb_kill_urb(dev->rx_urb);
753 return res;
754 }
755 enable_net_traffic(dev);
756 set_carrier(netdev);
757 netif_start_queue(netdev);
758
759 return res;
760 }
761
762 static 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
775 static void rtl8150_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *info)
776 {
777 rtl8150_t *dev = netdev_priv(netdev);
778
779 strlcpy(info->driver, driver_name, sizeof(info->driver));
780 strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
781 usb_make_path(dev->udev, info->bus_info, sizeof(info->bus_info));
782 }
783
784 static 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) {
801 u32 speed = ((lpa & (LPA_100HALF | LPA_100FULL)) ?
802 SPEED_100 : SPEED_10);
803 ethtool_cmd_speed_set(ecmd, speed);
804 ecmd->autoneg = AUTONEG_ENABLE;
805 if (speed == SPEED_100)
806 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;
813 ethtool_cmd_speed_set(ecmd, ((bmcr & BMCR_SPEED100) ?
814 SPEED_100 : SPEED_10));
815 ecmd->duplex = (bmcr & BMCR_FULLDPLX) ?
816 DUPLEX_FULL : DUPLEX_HALF;
817 }
818 return 0;
819 }
820
821 static const struct ethtool_ops ops = {
822 .get_drvinfo = rtl8150_get_drvinfo,
823 .get_settings = rtl8150_get_settings,
824 .get_link = ethtool_op_get_link
825 };
826
827 static 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
851 static 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,
856 .ndo_tx_timeout = rtl8150_tx_timeout,
857 .ndo_set_rx_mode = rtl8150_set_multicast,
858 .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
864 static 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));
872 if (!netdev)
873 return -ENOMEM;
874
875 dev = netdev_priv(netdev);
876
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);
885
886 dev->udev = udev;
887 dev->netdev = netdev;
888 netdev->netdev_ops = &rtl8150_netdev_ops;
889 netdev->watchdog_timeo = RTL8150_TX_TIMEOUT;
890 SET_ETHTOOL_OPS(netdev, &ops);
891 dev->intr_interval = 100; /* 100ms */
892
893 if (!alloc_all_urbs(dev)) {
894 dev_err(&intf->dev, "out of memory\n");
895 goto out;
896 }
897 if (!rtl8150_reset(dev)) {
898 dev_err(&intf->dev, "couldn't reset the device\n");
899 goto out1;
900 }
901 fill_skb_pool(dev);
902 set_ethernet_addr(dev);
903
904 usb_set_intfdata(intf, dev);
905 SET_NETDEV_DEV(netdev, &intf->dev);
906 if (register_netdev(netdev) != 0) {
907 dev_err(&intf->dev, "couldn't register the device\n");
908 goto out2;
909 }
910
911 dev_info(&intf->dev, "%s: rtl8150 is detected\n", netdev->name);
912
913 return 0;
914
915 out2:
916 usb_set_intfdata(intf, NULL);
917 free_skb_pool(dev);
918 out1:
919 free_all_urbs(dev);
920 out:
921 kfree(dev->intr_buff);
922 free_netdev(netdev);
923 return -EIO;
924 }
925
926 static 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);
933 tasklet_kill(&dev->tl);
934 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
945 static 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,
951 .resume = rtl8150_resume,
952 .disable_hub_initiated_lpm = 1,
953 };
954
955 module_usb_driver(rtl8150_driver);
956
957 MODULE_AUTHOR(DRIVER_AUTHOR);
958 MODULE_DESCRIPTION(DRIVER_DESC);
959 MODULE_LICENSE("GPL");