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