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