Linux Audio

Check our new training course

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