1 /*
2  * USB Host-to-Host Links
3  * Copyright (C) 2000-2002 by David Brownell <dbrownell@users.sourceforge.net>
4  * Copyright (C) 2002 Pavel Machek <pavel@ucw.cz>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 
21 /*
22  * This is used for "USB networking", connecting USB hosts as peers.
23  *
24  * It can be used with USB "network cables", for IP-over-USB communications;
25  * Ethernet speeds without the Ethernet.  USB devices (including some PDAs)
26  * can support such links directly, replacing device-specific protocols
27  * with Internet standard ones.
28  *
29  * The links can be bridged using the Ethernet bridging (net/bridge)
30  * support as appropriate.  Devices currently supported include:
31  *
32  *	- AnchorChip 2720
33  *	- Belkin, eTEK (interops with Win32 drivers)
34  *	- EPSON USB clients
35  *	- GeneSys GL620USB-A
36  *	- NetChip 1080 (interoperates with NetChip Win32 drivers)
37  *	- Prolific PL-2301/2302 (replaces "plusb" driver)
38  *	- PXA-250 or SA-1100 Linux PDAs like iPAQ, Yopy, and Zaurus
39  *
40  * USB devices can implement their side of this protocol at the cost
41  * of two bulk endpoints; it's not restricted to "cable" applications.
42  * See the SA1110, Zaurus, or EPSON device/client support in this driver;
43  * slave/target drivers such as "usb-eth" (on most SA-1100 PDAs) are
44  * used inside USB slave/target devices.
45  *
46  *
47  * Status:
48  *
49  * - AN2720 ... not widely available, but reportedly works well
50  *
51  * - Belkin/eTEK ... no known issues
52  *
53  * - Both GeneSys and PL-230x use interrupt transfers for driver-to-driver
54  *   handshaking; it'd be worth implementing those as "carrier detect".
55  *   Prefer generic hooks, not minidriver-specific hacks.
56  *
57  * - For Netchip, should use keventd to poll via control requests to detect
58  *   hardware level "carrier detect".
59  *
60  * - PL-230x ... the initialization protocol doesn't seem to match chip data
61  *   sheets, sometimes it's not needed and sometimes it hangs.  Prolific has
62  *   not responded to repeated support/information requests.
63  *
64  * - SA-1100 PDAs ... the standard ARM Linux SA-1100 support works nicely,
65  *   as found in www.handhelds.org and other kernels.  The Sharp/Lineo
66  *   kernels use different drivers, which also talk to this code.
67  *
68  * Interop with more Win32 drivers may be a good thing.
69  *
70  * Seems like reporting "peer connected" (carrier present) events may end
71  * up going through the netlink event system, not hotplug ... so new links
72  * would likely be handled with a link monitoring thread in some daemon.
73  *
74  * There are reports that bridging gives lower-than-usual throughput.
75  *
76  * Need smarter hotplug policy scripts ... ones that know how to arrange
77  * bridging with "brctl", and can handle static and dynamic ("pump") setups.
78  * Use those eventual "peer connected" events, and zeroconf.
79  *
80  *
81  * CHANGELOG:
82  *
83  * 13-sep-2000	experimental, new
84  * 10-oct-2000	usb_device_id table created.
85  * 28-oct-2000	misc fixes; mostly, discard more TTL-mangled rx packets.
86  * 01-nov-2000	usb_device_id table and probing api update by
87  *		Adam J. Richter <adam@yggdrasil.com>.
88  * 18-dec-2000	(db) tx watchdog, "net1080" renaming to "usbnet", device_info
89  *		and prolific support, isolate net1080-specific bits, cleanup.
90  *		fix unlink_urbs oops in D3 PM resume code path.
91  *
92  * 02-feb-2001	(db) fix tx skb sharing, packet length, match_flags, ...
93  * 08-feb-2001	stubbed in "linuxdev", maybe the SA-1100 folk can use it;
94  *		AnchorChips 2720 support (from spec) for testing;
95  *		fix bit-ordering problem with ethernet multicast addr
96  * 19-feb-2001  Support for clearing halt conditions. SA1100 UDC support
97  *		updates. Oleg Drokin (green@iXcelerator.com)
98  * 25-mar-2001	More SA-1100 updates, including workaround for ip problem
99  *		expecting cleared skb->cb and framing change to match latest
100  *		handhelds.org version (Oleg).  Enable device IDs from the
101  *		Win32 Belkin driver; other cleanups (db).
102  * 16-jul-2001	Bugfixes for uhci oops-on-unplug, Belkin support, various
103  *		cleanups for problems not yet seen in the field. (db)
104  * 17-oct-2001	Handle "Advance USBNET" product, like Belkin/eTEK devices,
105  *		from Ioannis Mavroukakis <i.mavroukakis@btinternet.com>;
106  *		rx unlinks somehow weren't async; minor cleanup.
107  * 03-nov-2001	Merged GeneSys driver; original code from Jiun-Jie Huang
108  *		<huangjj@genesyslogic.com.tw>, updated by Stanislav Brabec
109  *		<utx@penguin.cz>.  Made framing options (NetChip/GeneSys)
110  *		tie mostly to (sub)driver info.  Workaround some PL-2302
111  *		chips that seem to reject SET_INTERFACE requests.
112  *
113  * 06-apr-2002	Added ethtool support, based on a patch from Brad Hards.
114  *		Level of diagnostics is more configurable; they use device
115  *		location (usb_device->devpath) instead of address (2.5).
116  *		For tx_fixup, memflags can't be NOIO.
117  * 07-may-2002	Generalize/cleanup keventd support, handling rx stalls (mostly
118  *		for USB 2.0 TTs) and memory shortages (potential) too. (db)
119  *		Use "locally assigned" IEEE802 address space. (Brad Hards)
120  * 18-oct-2002	Support for Zaurus (Pavel Machek), related cleanup (db).
121  * 15-dec-2002	Partial sync with 2.5 code: cleanups and stubbed PXA-250
122  * 		support (db), fix for framing issues on Z, net1080, and
123  * 		gl620a (Toby Milne)
124  *
125  *-------------------------------------------------------------------------*/
126 
127 #include <linux/config.h>
128 #include <linux/module.h>
129 #include <linux/kmod.h>
130 #include <linux/sched.h>
131 #include <linux/init.h>
132 #include <linux/netdevice.h>
133 #include <linux/etherdevice.h>
134 #include <linux/random.h>
135 #include <linux/ethtool.h>
136 #include <linux/mii.h>
137 #include <linux/tqueue.h>
138 #include <asm/uaccess.h>
139 #include <asm/unaligned.h>
140 
141 // #define	DEBUG			// error path messages, extra info
142 // #define	VERBOSE			// more; success messages
143 // #define	REALLY_QUEUE
144 
145 #if !defined (DEBUG) && defined (CONFIG_USB_DEBUG)
146 #   define DEBUG
147 #endif
148 #include <linux/usb.h>
149 
150 /* in 2.5 these standard usb ops take mem_flags */
151 #define ALLOC_URB(n,flags)	usb_alloc_urb(n)
152 #define SUBMIT_URB(u,flags)	usb_submit_urb(u)
153 
154 /* and these got renamed (may move to usb.h) */
155 #define usb_get_dev		usb_inc_dev_use
156 #define usb_put_dev		usb_dec_dev_use
157 
158 
159 /* minidrivers _could_ be individually configured */
160 #define	CONFIG_USB_AN2720
161 #define	CONFIG_USB_AX8817X
162 #define	CONFIG_USB_BELKIN
163 #define	CONFIG_USB_EPSON2888
164 #define	CONFIG_USB_GENESYS
165 #define	CONFIG_USB_NET1080
166 #define	CONFIG_USB_PL2301
167 #define	CONFIG_USB_ARMLINUX
168 #define	CONFIG_USB_ZAURUS
169 
170 
171 #define DRIVER_VERSION		"18-Oct-2002"
172 
173 /*-------------------------------------------------------------------------*/
174 
175 /*
176  * Nineteen USB 1.1 max size bulk transactions per frame (ms), max.
177  * Several dozen bytes of IPv4 data can fit in two such transactions.
178  * One maximum size Ethernet packet takes twenty four of them.
179  * For high speed, each frame comfortably fits almost 36 max size
180  * Ethernet packets (so queues should be bigger).
181  */
182 #ifdef REALLY_QUEUE
183 #define	RX_QLEN		4
184 #define	TX_QLEN		4
185 #else
186 #define	RX_QLEN		1
187 #define	TX_QLEN		1
188 #endif
189 
190 // packets are always ethernet inside
191 // ... except they can be bigger (limit of 64K with NetChip framing)
192 #define MIN_PACKET	sizeof(struct ethhdr)
193 #define MAX_PACKET	32768
194 
195 // reawaken network queue this soon after stopping; else watchdog barks
196 #define TX_TIMEOUT_JIFFIES	(5*HZ)
197 
198 // for vendor-specific control operations
199 #define	CONTROL_TIMEOUT_MS	(500)			/* msec */
200 #define CONTROL_TIMEOUT_JIFFIES ((CONTROL_TIMEOUT_MS * HZ)/1000)
201 
202 // between wakeups
203 #define UNLINK_TIMEOUT_JIFFIES ((3  /*ms*/ * HZ)/1000)
204 
205 /*-------------------------------------------------------------------------*/
206 
207 // list of all devices we manage
208 static DECLARE_MUTEX (usbnet_mutex);
209 static LIST_HEAD (usbnet_list);
210 
211 // randomly generated ethernet address
212 static u8	node_id [ETH_ALEN];
213 
214 // state we keep for each device we handle
215 struct usbnet {
216 	// housekeeping
217 	struct usb_device	*udev;
218 	struct driver_info	*driver_info;
219 	struct semaphore	mutex;
220 	struct list_head	dev_list;
221 	wait_queue_head_t	*wait;
222 
223 	// i/o info: pipes etc
224 	unsigned		in, out;
225 	unsigned		maxpacket;
226 	//struct timer_list	delay;
227 
228 	// protocol/interface state
229 	struct net_device	net;
230 	struct net_device_stats	stats;
231 	int			msg_level;
232 	struct mii_if_info	mii;
233 
234 #ifdef CONFIG_USB_NET1080
235 	u16			packet_id;
236 #endif
237 
238 	// various kinds of pending driver work
239 	struct sk_buff_head	rxq;
240 	struct sk_buff_head	txq;
241 	struct sk_buff_head	done;
242 	struct tasklet_struct	bh;
243 
244 	struct tq_struct	kevent;
245 	unsigned long		flags;
246 #		define EVENT_TX_HALT	0
247 #		define EVENT_RX_HALT	1
248 #		define EVENT_RX_MEMORY	2
249 };
250 
251 // device-specific info used by the driver
252 struct driver_info {
253 	char		*description;
254 
255 	int		flags;
256 #define FLAG_FRAMING_NC	0x0001		/* guard against device dropouts */
257 #define FLAG_FRAMING_GL	0x0002		/* genelink batches packets */
258 #define FLAG_FRAMING_Z	0x0004		/* zaurus adds a trailer */
259 #define FLAG_NO_SETINT	0x0010		/* device can't set_interface() */
260 #define FLAG_ETHER	0x0020		/* maybe use "eth%d" names */
261 
262 	/* init device ... can sleep, or cause probe() failure */
263 	int	(*bind)(struct usbnet *, struct usb_device *);
264 
265 	/* reset device ... can sleep */
266 	int	(*reset)(struct usbnet *);
267 
268 	/* see if peer is connected ... can sleep */
269 	int	(*check_connect)(struct usbnet *);
270 
271 	/* fixup rx packet (strip framing) */
272 	int	(*rx_fixup)(struct usbnet *dev, struct sk_buff *skb);
273 
274 	/* fixup tx packet (add framing) */
275 	struct sk_buff	*(*tx_fixup)(struct usbnet *dev,
276 				struct sk_buff *skb, int flags);
277 
278 	// FIXME -- also an interrupt mechanism
279 	// useful for at least PL2301/2302 and GL620USB-A
280 
281 	/* for new devices, use the descriptor-reading code instead */
282 	int		in;		/* rx endpoint */
283 	int		out;		/* tx endpoint */
284 	int		epsize;
285 
286 	unsigned long	data;		/* Misc driver specific data */
287 };
288 
289 // we record the state for each of our queued skbs
290 enum skb_state {
291 	illegal = 0,
292 	tx_start, tx_done,
293 	rx_start, rx_done, rx_cleanup
294 };
295 
296 struct skb_data {	// skb->cb is one of these
297 	struct urb		*urb;
298 	struct usbnet		*dev;
299 	enum skb_state		state;
300 	size_t			length;
301 };
302 
303 static const char driver_name [] = "usbnet";
304 
305 /* use ethtool to change the level for any given device */
306 static int msg_level = 1;
307 MODULE_PARM (msg_level, "i");
308 MODULE_PARM_DESC (msg_level, "Initial message level (default = 1)");
309 
310 
311 #define	mutex_lock(x)	down(x)
312 #define	mutex_unlock(x)	up(x)
313 
314 #define	RUN_CONTEXT (in_irq () ? "in_irq" \
315 			: (in_interrupt () ? "in_interrupt" : "can sleep"))
316 
317 static struct ethtool_ops usbnet_ethtool_ops;
318 
319 /* mostly for PDA style devices, which are always present */
always_connected(struct usbnet * dev)320 static int always_connected (struct usbnet *dev)
321 {
322 	return 0;
323 }
324 
325 /*-------------------------------------------------------------------------*/
326 
327 /* handles CDC Ethernet and many other network "bulk data" interfaces */
328 static int
get_endpoints(struct usbnet * dev,struct usb_interface * intf)329 get_endpoints (struct usbnet *dev, struct usb_interface *intf)
330 {
331 	int				tmp;
332 	struct usb_interface_descriptor	*alt;
333 	struct usb_endpoint_descriptor	*in, *out;
334 
335 	for (tmp = 0; tmp < intf->max_altsetting; tmp++) {
336 		unsigned	ep;
337 
338 		in = out = 0;
339 		alt = intf->altsetting + tmp;
340 
341 		/* take the first altsetting with in-bulk + out-bulk;
342 		 * ignore other endpoints and altsetttings.
343 		 */
344 		for (ep = 0; ep < alt->bNumEndpoints; ep++) {
345 			struct usb_endpoint_descriptor	*e;
346 
347 			e = alt->endpoint + ep;
348 			if (e->bmAttributes != USB_ENDPOINT_XFER_BULK)
349 				continue;
350 			if (e->bEndpointAddress & USB_DIR_IN) {
351 				if (!in)
352 					in = e;
353 			} else {
354 				if (!out)
355 					out = e;
356 			}
357 			if (in && out)
358 				goto found;
359 		}
360 	}
361 	return -EINVAL;
362 
363 found:
364 	if (alt->bAlternateSetting != 0
365 			|| !(dev->driver_info->flags & FLAG_NO_SETINT)) {
366 		tmp = usb_set_interface (dev->udev, alt->bInterfaceNumber,
367 				alt->bAlternateSetting);
368 		if (tmp < 0)
369 			return tmp;
370 	}
371 
372 	dev->in = usb_rcvbulkpipe (dev->udev,
373 			in->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
374 	dev->out = usb_sndbulkpipe (dev->udev,
375 			out->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
376 	dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
377 	return 0;
378 }
379 
380 /*-------------------------------------------------------------------------*/
381 
382 #ifdef DEBUG
383 #define devdbg(usbnet, fmt, arg...) \
384 	printk(KERN_DEBUG "%s: " fmt "\n" , (usbnet)->net.name , ## arg)
385 #else
386 #define devdbg(usbnet, fmt, arg...) do {} while(0)
387 #endif
388 
389 #define devinfo(usbnet, fmt, arg...) \
390 	do { if ((usbnet)->msg_level >= 1) \
391 	printk(KERN_INFO "%s: " fmt "\n" , (usbnet)->net.name , ## arg); \
392 	} while (0)
393 
394 
395 #ifdef	CONFIG_USB_AN2720
396 
397 /*-------------------------------------------------------------------------
398  *
399  * AnchorChips 2720 driver ... http://www.cypress.com
400  *
401  * This doesn't seem to have a way to detect whether the peer is
402  * connected, or need any reset handshaking.  It's got pretty big
403  * internal buffers (handles most of a frame's worth of data).
404  * Chip data sheets don't describe any vendor control messages.
405  *
406  *-------------------------------------------------------------------------*/
407 
408 static const struct driver_info	an2720_info = {
409 	.description =	"AnchorChips/Cypress 2720",
410 	// no reset available!
411 	// no check_connect available!
412 
413 	.in = 2, .out = 2,		// direction distinguishes these
414 	.epsize =64,
415 };
416 
417 #endif	/* CONFIG_USB_AN2720 */
418 
419 
420 #ifdef CONFIG_USB_AX8817X
421 /* ASIX AX8817X based USB 2.0 Ethernet Devices */
422 
423 #define HAVE_HARDWARE
424 #define NEED_MII
425 
426 #include <linux/crc32.h>
427 
428 #define AX_CMD_SET_SW_MII		0x06
429 #define AX_CMD_READ_MII_REG		0x07
430 #define AX_CMD_WRITE_MII_REG		0x08
431 #define AX_CMD_SET_HW_MII		0x0a
432 #define AX_CMD_WRITE_RX_CTL		0x10
433 #define AX_CMD_READ_IPG012		0x11
434 #define AX_CMD_WRITE_IPG0		0x12
435 #define AX_CMD_WRITE_IPG1		0x13
436 #define AX_CMD_WRITE_IPG2		0x14
437 #define AX_CMD_WRITE_MULTI_FILTER	0x16
438 #define AX_CMD_READ_NODE_ID		0x17
439 #define AX_CMD_READ_PHY_ID		0x19
440 #define AX_CMD_WRITE_MEDIUM_MODE	0x1b
441 #define AX_CMD_WRITE_GPIOS		0x1f
442 
443 #define AX_MCAST_FILTER_SIZE		8
444 #define AX_MAX_MCAST			64
445 
ax8817x_read_cmd(struct usbnet * dev,u8 cmd,u16 value,u16 index,u16 size,void * data)446 static int ax8817x_read_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
447 			    u16 size, void *data)
448 {
449 	return usb_control_msg(
450 		dev->udev,
451 		usb_rcvctrlpipe(dev->udev, 0),
452 		cmd,
453 		USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
454 		value,
455 		index,
456 		data,
457 		size,
458 		CONTROL_TIMEOUT_JIFFIES);
459 }
460 
ax8817x_write_cmd(struct usbnet * dev,u8 cmd,u16 value,u16 index,u16 size,void * data)461 static int ax8817x_write_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
462 			     u16 size, void *data)
463 {
464 	return usb_control_msg(
465 		dev->udev,
466 		usb_sndctrlpipe(dev->udev, 0),
467 		cmd,
468 		USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
469 		value,
470 		index,
471 		data,
472 		size,
473 		CONTROL_TIMEOUT_JIFFIES);
474 }
475 
ax8817x_async_cmd_callback(struct urb * urb)476 static void ax8817x_async_cmd_callback(struct urb *urb)
477 {
478 	struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
479 
480 	if (urb->status < 0)
481 		printk(KERN_DEBUG "ax8817x_async_cmd_callback() failed with %d",
482 			urb->status);
483 
484 	kfree(req);
485 	usb_free_urb(urb);
486 }
487 
ax8817x_write_cmd_async(struct usbnet * dev,u8 cmd,u16 value,u16 index,u16 size,void * data)488 static void ax8817x_write_cmd_async(struct usbnet *dev, u8 cmd, u16 value, u16 index,
489 				    u16 size, void *data)
490 {
491 	struct usb_ctrlrequest *req;
492 	int status;
493 	struct urb *urb;
494 
495 	if ((urb = ALLOC_URB(0, GFP_ATOMIC)) == NULL) {
496 		devdbg(dev, "Error allocating URB in write_cmd_async!");
497 		return;
498 	}
499 
500 	if ((req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC)) == NULL) {
501 		devdbg(dev, "Failed to allocate memory for control request");
502 		usb_free_urb(urb);
503 		return;
504 	}
505 
506 	req->bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE;
507 	req->bRequest = cmd;
508 	req->wValue = cpu_to_le16(value);
509 	req->wIndex = cpu_to_le16(index);
510 	req->wLength = cpu_to_le16(size);
511 
512 	usb_fill_control_urb(urb, dev->udev,
513 			     usb_sndctrlpipe(dev->udev, 0),
514 			     (void *)req, data, size,
515 			     ax8817x_async_cmd_callback, req);
516 
517 	if((status = SUBMIT_URB(urb, GFP_ATOMIC)) < 0)
518 		devdbg(dev, "Error submitting the control message: status=%d", status);
519 }
520 
ax8817x_set_multicast(struct net_device * net)521 static void ax8817x_set_multicast(struct net_device *net)
522 {
523 	struct usbnet *dev = (struct usbnet *) net->priv;
524 	u8 rx_ctl = 0x8c;
525 
526 	if (net->flags & IFF_PROMISC) {
527 		rx_ctl |= 0x01;
528 	} else if (net->flags & IFF_ALLMULTI
529 		   || net->mc_count > AX_MAX_MCAST) {
530 		rx_ctl |= 0x02;
531 	} else if (net->mc_count == 0) {
532 		/* just broadcast and directed */
533 	} else {
534 		struct dev_mc_list *mc_list = net->mc_list;
535 		u8 *multi_filter;
536 		u32 crc_bits;
537 		int i;
538 
539 		multi_filter = kmalloc(AX_MCAST_FILTER_SIZE, GFP_ATOMIC);
540 		if (multi_filter == NULL) {
541 			/* Oops, couldn't allocate a buffer for setting the multicast
542 			   filter. Try all multi mode. */
543 			rx_ctl |= 0x02;
544 		} else {
545 			memset(multi_filter, 0, AX_MCAST_FILTER_SIZE);
546 
547 			/* Build the multicast hash filter. */
548 			for (i = 0; i < net->mc_count; i++) {
549 				crc_bits =
550 				    ether_crc(ETH_ALEN,
551 					      mc_list->dmi_addr) >> 26;
552 				multi_filter[crc_bits >> 3] |=
553 				    1 << (crc_bits & 7);
554 				mc_list = mc_list->next;
555 			}
556 
557 			ax8817x_write_cmd_async(dev, AX_CMD_WRITE_MULTI_FILTER, 0, 0,
558 					   AX_MCAST_FILTER_SIZE, multi_filter);
559 
560 			rx_ctl |= 0x10;
561 		}
562 	}
563 
564 	ax8817x_write_cmd_async(dev, AX_CMD_WRITE_RX_CTL, rx_ctl, 0, 0, NULL);
565 }
566 
ax8817x_mdio_read(struct net_device * netdev,int phy_id,int loc)567 static int ax8817x_mdio_read(struct net_device *netdev, int phy_id, int loc)
568 {
569 	struct usbnet *dev = netdev->priv;
570 	u16 res;
571 	u8 buf[4];
572 
573 	ax8817x_write_cmd(dev, AX_CMD_SET_SW_MII, 0, 0, 0, &buf);
574 	ax8817x_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id, (__u16)loc, 2, (u16 *)&res);
575 	ax8817x_write_cmd(dev, AX_CMD_SET_HW_MII, 0, 0, 0, &buf);
576 
577 	return res & 0xffff;
578 }
579 
ax8817x_mdio_write(struct net_device * netdev,int phy_id,int loc,int val)580 static void ax8817x_mdio_write(struct net_device *netdev, int phy_id, int loc, int val)
581 {
582 	struct usbnet *dev = netdev->priv;
583 	u16 res = val;
584 	u8 buf[4];
585 
586 	ax8817x_write_cmd(dev, AX_CMD_SET_SW_MII, 0, 0, 0, &buf);
587 	ax8817x_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id, (__u16)loc, 2, (u16 *)&res);
588 	ax8817x_write_cmd(dev, AX_CMD_SET_HW_MII, 0, 0, 0, &buf);
589 }
590 
ax8817x_bind(struct usbnet * dev,struct usb_device * intf)591 static int ax8817x_bind(struct usbnet *dev, struct usb_device *intf)
592 {
593 	int ret;
594 	u8 buf[6];
595 	u16 *buf16 = (u16 *) buf;
596 	int i;
597 	unsigned long gpio_bits = dev->driver_info->data;
598 
599 	dev->in = usb_rcvbulkpipe(dev->udev, 3);
600 	dev->out = usb_sndbulkpipe(dev->udev, 2);
601 
602 	/* Toggle the GPIOs in a manufacturer/model specific way */
603 	for (i = 2; i >= 0; i--) {
604 		if ((ret = ax8817x_write_cmd(dev, AX_CMD_WRITE_GPIOS,
605 				       (gpio_bits >> (i * 8)) & 0xff, 0, 0,
606 				       buf)) < 0)
607 			return ret;
608 		wait_ms(5);
609         }
610 
611 	if ((ret = ax8817x_write_cmd(dev, AX_CMD_WRITE_RX_CTL, 0x80, 0, 0, buf)) < 0) {
612 		dbg("send AX_CMD_WRITE_RX_CTL failed: %d", ret);
613 		return ret;
614 	}
615 
616 	/* Get the MAC address */
617 	memset(buf, 0, ETH_ALEN);
618 	if ((ret = ax8817x_read_cmd(dev, AX_CMD_READ_NODE_ID, 0, 0, 6, buf)) < 0) {
619 		dbg("read AX_CMD_READ_NODE_ID failed: %d", ret);
620 		return ret;
621 	}
622 	memcpy(dev->net.dev_addr, buf, ETH_ALEN);
623 
624 	/* Get IPG values */
625 	if ((ret = ax8817x_read_cmd(dev, AX_CMD_READ_IPG012, 0, 0, 3, buf)) < 0) {
626 		dbg("Error reading IPG values: %d", ret);
627 		return ret;
628 	}
629 
630 	for(i = 0;i < 3;i++) {
631 		ax8817x_write_cmd(dev, AX_CMD_WRITE_IPG0 + i, 0, 0, 1, &buf[i]);
632 	}
633 
634 	/* Get the PHY id */
635 	if ((ret = ax8817x_read_cmd(dev, AX_CMD_READ_PHY_ID, 0, 0, 2, buf)) < 0) {
636 		dbg("error on read AX_CMD_READ_PHY_ID: %02x", ret);
637 		return ret;
638 	} else if (ret < 2) {
639 		/* this should always return 2 bytes */
640 		dbg("AX_CMD_READ_PHY_ID returned less than 2 bytes: ret=%02x", ret);
641 		return -EIO;
642 	}
643 
644 	/* Initialize MII structure */
645 	dev->mii.dev = &dev->net;
646 	dev->mii.mdio_read = ax8817x_mdio_read;
647 	dev->mii.mdio_write = ax8817x_mdio_write;
648 	dev->mii.phy_id_mask = 0x3f;
649 	dev->mii.reg_num_mask = 0x1f;
650 	dev->mii.phy_id = buf[1];
651 
652 	if ((ret = ax8817x_write_cmd(dev, AX_CMD_SET_SW_MII, 0, 0, 0, &buf)) < 0) {
653 		dbg("Failed to go to software MII mode: %02x", ret);
654 		return ret;
655 	}
656 
657 	*buf16 = cpu_to_le16(BMCR_RESET);
658 	if ((ret = ax8817x_write_cmd(dev, AX_CMD_WRITE_MII_REG,
659 				     dev->mii.phy_id, MII_BMCR, 2, buf16)) < 0) {
660 		dbg("Failed to write MII reg - MII_BMCR: %02x", ret);
661 		return ret;
662 	}
663 
664 	/* Advertise that we can do full-duplex pause */
665 	*buf16 = cpu_to_le16(ADVERTISE_ALL | ADVERTISE_CSMA | 0x0400);
666 	if ((ret = ax8817x_write_cmd(dev, AX_CMD_WRITE_MII_REG,
667 			   	     dev->mii.phy_id, MII_ADVERTISE,
668 				     2, buf16)) < 0) {
669 		dbg("Failed to write MII_REG advertisement: %02x", ret);
670 		return ret;
671 	}
672 
673 	*buf16 = cpu_to_le16(BMCR_ANENABLE | BMCR_ANRESTART);
674 	if ((ret = ax8817x_write_cmd(dev, AX_CMD_WRITE_MII_REG,
675 			  	     dev->mii.phy_id, MII_BMCR,
676 				     2, buf16)) < 0) {
677 		dbg("Failed to write MII reg autonegotiate: %02x", ret);
678 		return ret;
679 	}
680 
681 	if ((ret = ax8817x_write_cmd(dev, AX_CMD_SET_HW_MII, 0, 0, 0, &buf)) < 0) {
682 		dbg("Failed to set hardware MII: %02x", ret);
683 		return ret;
684 	}
685 
686 	dev->net.set_multicast_list = ax8817x_set_multicast;
687 
688 	return 0;
689 }
690 
691 static const struct driver_info ax8817x_info = {
692 	.description = "ASIX AX8817x USB 2.0 Ethernet",
693 	.bind = ax8817x_bind,
694 	.flags =  FLAG_ETHER,
695 	.data = 0x00130103,
696 };
697 
698 static const struct driver_info dlink_dub_e100_info = {
699 	.description = "DLink DUB-E100 USB Ethernet",
700 	.bind = ax8817x_bind,
701 	.flags =  FLAG_ETHER,
702 	.data = 0x009f9d9f,
703 };
704 
705 static const struct driver_info netgear_fa120_info = {
706 	.description = "Netgear FA-120 USB Ethernet",
707 	.bind = ax8817x_bind,
708 	.flags =  FLAG_ETHER,
709 	.data = 0x00130103,
710 };
711 
712 static const struct driver_info hawking_uf200_info = {
713 	.description = "Hawking UF200 USB Ethernet",
714 	.bind = ax8817x_bind,
715 	.flags =  FLAG_ETHER,
716 	.data = 0x001f1d1f,
717 };
718 #endif /* CONFIG_USB_AX8817X */
719 
720 
721 #ifdef	CONFIG_USB_BELKIN
722 
723 /*-------------------------------------------------------------------------
724  *
725  * Belkin F5U104 ... two NetChip 2280 devices + Atmel microcontroller
726  *
727  * ... also two eTEK designs, including one sold as "Advance USBNET"
728  *
729  *-------------------------------------------------------------------------*/
730 
731 static const struct driver_info	belkin_info = {
732 	.description =	"Belkin, eTEK, or compatible",
733 };
734 
735 #endif	/* CONFIG_USB_BELKIN */
736 
737 
738 
739 #ifdef	CONFIG_USB_EPSON2888
740 
741 /*-------------------------------------------------------------------------
742  *
743  * EPSON USB clients
744  *
745  * This is the same idea as Linux PDAs (below) except the firmware in the
746  * device might not be Tux-powered.  Epson provides reference firmware that
747  * implements this interface.  Product developers can reuse or modify that
748  * code, such as by using their own product and vendor codes.
749  *
750  *-------------------------------------------------------------------------*/
751 
752 static const struct driver_info	epson2888_info = {
753 	.description =	"Epson USB Device",
754 	.check_connect = always_connected,
755 
756 	.in = 4, .out = 3,
757 	.epsize = 64,
758 };
759 
760 #endif	/* CONFIG_USB_EPSON2888 */
761 
762 
763 #ifdef CONFIG_USB_GENESYS
764 
765 /*-------------------------------------------------------------------------
766  *
767  * GeneSys GL620USB-A (www.genesyslogic.com.tw)
768  *
769  * ... should partially interop with the Win32 driver for this hardware
770  * The GeneSys docs imply there's some NDIS issue motivating this framing.
771  *
772  * Some info from GeneSys:
773  *  - GL620USB-A is full duplex; GL620USB is only half duplex for bulk.
774  *    (Some cables, like the BAFO-100c, use the half duplex version.)
775  *  - For the full duplex model, the low bit of the version code says
776  *    which side is which ("left/right").
777  *  - For the half duplex type, a control/interrupt handshake settles
778  *    the transfer direction.  (That's disabled here, partially coded.)
779  *    A control URB would block until other side writes an interrupt.
780  *
781  *-------------------------------------------------------------------------*/
782 
783 // control msg write command
784 #define GENELINK_CONNECT_WRITE			0xF0
785 // interrupt pipe index
786 #define GENELINK_INTERRUPT_PIPE			0x03
787 // interrupt read buffer size
788 #define INTERRUPT_BUFSIZE			0x08
789 // interrupt pipe interval value
790 #define GENELINK_INTERRUPT_INTERVAL		0x10
791 // max transmit packet number per transmit
792 #define GL_MAX_TRANSMIT_PACKETS			32
793 // max packet length
794 #define GL_MAX_PACKET_LEN			1514
795 // max receive buffer size
796 #define GL_RCV_BUF_SIZE		\
797 	(((GL_MAX_PACKET_LEN + 4) * GL_MAX_TRANSMIT_PACKETS) + 4)
798 
799 struct gl_packet {
800 	u32		packet_length;
801 	char		packet_data [1];
802 };
803 
804 struct gl_header {
805 	u32			packet_count;
806 	struct gl_packet	packets;
807 };
808 
809 #ifdef	GENLINK_ACK
810 
811 // FIXME:  this code is incomplete, not debugged; it doesn't
812 // handle interrupts correctly.  interrupts should be generic
813 // code like all other device I/O, anyway.
814 
815 struct gl_priv {
816 	struct urb	*irq_urb;
817 	char		irq_buf [INTERRUPT_BUFSIZE];
818 };
819 
gl_control_write(struct usbnet * dev,u8 request,u16 value)820 static inline int gl_control_write (struct usbnet *dev, u8 request, u16 value)
821 {
822 	int retval;
823 
824 	retval = usb_control_msg (dev->udev,
825 		      usb_sndctrlpipe (dev->udev, 0),
826 		      request,
827 		      USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
828 		      value,
829 		      0,			// index
830 		      0,			// data buffer
831 		      0,			// size
832 		      CONTROL_TIMEOUT_JIFFIES);
833 	return retval;
834 }
835 
gl_interrupt_complete(struct urb * urb)836 static void gl_interrupt_complete (struct urb *urb)
837 {
838 	int status = urb->status;
839 
840 	switch (status) {
841 	case 0:
842 		/* success */
843 		break;
844 	case -ECONNRESET:
845 	case -ENOENT:
846 	case -ESHUTDOWN:
847 		/* this urb is terminated, clean up */
848 		dbg("%s - urb shutting down with status: %d",
849 				__FUNCTION__, status);
850 		return;
851 	default:
852 		dbg("%s - nonzero urb status received: %d",
853 				__FUNCTION__, urb->status);
854 	}
855 
856 	// NOTE:  2.4 still has automagic resubmit, so this would be
857 	// wrong ... but this code has never worked, is always disabled.
858 	status = usb_submit_urb (urb, GFP_ATOMIC);
859 	if (status)
860 		err ("%s - usb_submit_urb failed with result %d",
861 		     __FUNCTION__, status);
862 }
863 
gl_interrupt_read(struct usbnet * dev)864 static int gl_interrupt_read (struct usbnet *dev)
865 {
866 	struct gl_priv	*priv = dev->priv_data;
867 	int		retval;
868 
869 	// issue usb interrupt read
870 	if (priv && priv->irq_urb) {
871 		// submit urb
872 		if ((retval = SUBMIT_URB (priv->irq_urb, GFP_KERNEL)) != 0)
873 			dbg ("gl_interrupt_read: submit fail - %X...", retval);
874 		else
875 			dbg ("gl_interrupt_read: submit success...");
876 	}
877 
878 	return 0;
879 }
880 
881 // check whether another side is connected
genelink_check_connect(struct usbnet * dev)882 static int genelink_check_connect (struct usbnet *dev)
883 {
884 	int			retval;
885 
886 	dbg ("genelink_check_connect...");
887 
888 	// detect whether another side is connected
889 	if ((retval = gl_control_write (dev, GENELINK_CONNECT_WRITE, 0)) != 0) {
890 		dbg ("%s: genelink_check_connect write fail - %X",
891 			dev->net.name, retval);
892 		return retval;
893 	}
894 
895 	// usb interrupt read to ack another side
896 	if ((retval = gl_interrupt_read (dev)) != 0) {
897 		dbg ("%s: genelink_check_connect read fail - %X",
898 			dev->net.name, retval);
899 		return retval;
900 	}
901 
902 	dbg ("%s: genelink_check_connect read success", dev->net.name);
903 	return 0;
904 }
905 
906 // allocate and initialize the private data for genelink
genelink_init(struct usbnet * dev)907 static int genelink_init (struct usbnet *dev)
908 {
909 	struct gl_priv *priv;
910 
911 	// allocate the private data structure
912 	if ((priv = kmalloc (sizeof *priv, GFP_KERNEL)) == 0) {
913 		dbg ("%s: cannot allocate private data per device",
914 			dev->net.name);
915 		return -ENOMEM;
916 	}
917 
918 	// allocate irq urb
919 	if ((priv->irq_urb = ALLOC_URB (0, GFP_KERNEL)) == 0) {
920 		dbg ("%s: cannot allocate private irq urb per device",
921 			dev->net.name);
922 		kfree (priv);
923 		return -ENOMEM;
924 	}
925 
926 	// fill irq urb
927 	usb_fill_int_urb (priv->irq_urb, dev->udev,
928 		usb_rcvintpipe (dev->udev, GENELINK_INTERRUPT_PIPE),
929 		priv->irq_buf, INTERRUPT_BUFSIZE,
930 		gl_interrupt_complete, 0,
931 		GENELINK_INTERRUPT_INTERVAL);
932 
933 	// set private data pointer
934 	dev->priv_data = priv;
935 
936 	return 0;
937 }
938 
939 // release the private data
genelink_free(struct usbnet * dev)940 static int genelink_free (struct usbnet *dev)
941 {
942 	struct gl_priv	*priv = dev->priv_data;
943 
944 	if (!priv)
945 		return 0;
946 
947 // FIXME:  can't cancel here; it's synchronous, and
948 // should have happened earlier in any case (interrupt
949 // handling needs to be generic)
950 
951 	// cancel irq urb first
952 	usb_unlink_urb (priv->irq_urb);
953 
954 	// free irq urb
955 	usb_free_urb (priv->irq_urb);
956 
957 	// free the private data structure
958 	kfree (priv);
959 
960 	return 0;
961 }
962 
963 #endif
964 
genelink_rx_fixup(struct usbnet * dev,struct sk_buff * skb)965 static int genelink_rx_fixup (struct usbnet *dev, struct sk_buff *skb)
966 {
967 	struct gl_header	*header;
968 	struct gl_packet	*packet;
969 	struct sk_buff		*gl_skb;
970 	int			status;
971 	u32			size;
972 
973 	header = (struct gl_header *) skb->data;
974 
975 	// get the packet count of the received skb
976 	le32_to_cpus (&header->packet_count);
977 	if ((header->packet_count > GL_MAX_TRANSMIT_PACKETS)
978 			|| (header->packet_count < 0)) {
979 		dbg ("genelink: illegal received packet count %d",
980 			header->packet_count);
981 		return 0;
982 	}
983 
984 	// set the current packet pointer to the first packet
985 	packet = &header->packets;
986 
987 	// decrement the length for the packet count size 4 bytes
988 	skb_pull (skb, 4);
989 
990 	while (header->packet_count > 1) {
991 		// get the packet length
992 		size = packet->packet_length;
993 
994 		// this may be a broken packet
995 		if (size > GL_MAX_PACKET_LEN) {
996 			dbg ("genelink: illegal rx length %d", size);
997 			return 0;
998 		}
999 
1000 		// allocate the skb for the individual packet
1001 		gl_skb = alloc_skb (size, GFP_ATOMIC);
1002 		if (gl_skb) {
1003 
1004 			// copy the packet data to the new skb
1005 			memcpy (gl_skb->data, packet->packet_data, size);
1006 
1007 			// set skb data size
1008 			gl_skb->len = size;
1009 			gl_skb->dev = &dev->net;
1010 
1011 			// determine the packet's protocol ID
1012 			gl_skb->protocol = eth_type_trans (gl_skb, &dev->net);
1013 
1014 			// update the status
1015 			dev->stats.rx_packets++;
1016 			dev->stats.rx_bytes += size;
1017 
1018 			// notify os of the received packet
1019 			status = netif_rx (gl_skb);
1020 		}
1021 
1022 		// advance to the next packet
1023 		packet = (struct gl_packet *)
1024 			&packet->packet_data [size];
1025 		header->packet_count--;
1026 
1027 		// shift the data pointer to the next gl_packet
1028 		skb_pull (skb, size + 4);
1029 	}
1030 
1031 	// skip the packet length field 4 bytes
1032 	skb_pull (skb, 4);
1033 
1034 	if (skb->len > GL_MAX_PACKET_LEN) {
1035 		dbg ("genelink: illegal rx length %d", skb->len);
1036 		return 0;
1037 	}
1038 	return 1;
1039 }
1040 
1041 static struct sk_buff *
genelink_tx_fixup(struct usbnet * dev,struct sk_buff * skb,int flags)1042 genelink_tx_fixup (struct usbnet *dev, struct sk_buff *skb, int flags)
1043 {
1044 	int 	padlen;
1045 	int	length = skb->len;
1046 	int	headroom = skb_headroom (skb);
1047 	int	tailroom = skb_tailroom (skb);
1048 	u32	*packet_count;
1049 	u32	*packet_len;
1050 
1051 	// FIXME:  magic numbers, bleech
1052 	padlen = ((skb->len + (4 + 4*1)) % 64) ? 0 : 1;
1053 
1054 	if ((!skb_cloned (skb))
1055 			&& ((headroom + tailroom) >= (padlen + (4 + 4*1)))) {
1056 		if ((headroom < (4 + 4*1)) || (tailroom < padlen)) {
1057 			skb->data = memmove (skb->head + (4 + 4*1),
1058 					     skb->data, skb->len);
1059 			skb->tail = skb->data + skb->len;
1060 		}
1061 	} else {
1062 		struct sk_buff	*skb2;
1063 		skb2 = skb_copy_expand (skb, (4 + 4*1) , padlen, flags);
1064 		dev_kfree_skb_any (skb);
1065 		skb = skb2;
1066 	}
1067 
1068 	// attach the packet count to the header
1069 	packet_count = (u32 *) skb_push (skb, (4 + 4*1));
1070 	packet_len = packet_count + 1;
1071 
1072 	// FIXME little endian?
1073 	*packet_count = 1;
1074 	*packet_len = length;
1075 
1076 	// add padding byte
1077 	if ((skb->len % dev->maxpacket) == 0)
1078 		skb_put (skb, 1);
1079 
1080 	return skb;
1081 }
1082 
1083 static const struct driver_info	genelink_info = {
1084 	.description =	"Genesys GeneLink",
1085 	.flags =	FLAG_FRAMING_GL | FLAG_NO_SETINT,
1086 	.rx_fixup =	genelink_rx_fixup,
1087 	.tx_fixup =	genelink_tx_fixup,
1088 
1089 	.in = 1, .out = 2,
1090 	.epsize =64,
1091 
1092 #ifdef	GENELINK_ACK
1093 	.check_connect =genelink_check_connect,
1094 #endif
1095 };
1096 
1097 #endif /* CONFIG_USB_GENESYS */
1098 
1099 
1100 
1101 #ifdef	CONFIG_USB_NET1080
1102 
1103 /*-------------------------------------------------------------------------
1104  *
1105  * Netchip 1080 driver ... http://www.netchip.com
1106  * Used in LapLink cables
1107  *
1108  *-------------------------------------------------------------------------*/
1109 
1110 /*
1111  * NetChip framing of ethernet packets, supporting additional error
1112  * checks for links that may drop bulk packets from inside messages.
1113  * Odd USB length == always short read for last usb packet.
1114  *	- nc_header
1115  *	- Ethernet header (14 bytes)
1116  *	- payload
1117  *	- (optional padding byte, if needed so length becomes odd)
1118  *	- nc_trailer
1119  *
1120  * This framing is to be avoided for non-NetChip devices.
1121  */
1122 
1123 struct nc_header {		// packed:
1124 	u16	hdr_len;		// sizeof nc_header (LE, all)
1125 	u16	packet_len;		// payload size (including ethhdr)
1126 	u16	packet_id;		// detects dropped packets
1127 #define MIN_HEADER	6
1128 
1129 	// all else is optional, and must start with:
1130 	// u16	vendorId;		// from usb-if
1131 	// u16	productId;
1132 } __attribute__((__packed__));
1133 
1134 #define	PAD_BYTE	((unsigned char)0xAC)
1135 
1136 struct nc_trailer {
1137 	u16	packet_id;
1138 } __attribute__((__packed__));
1139 
1140 // packets may use FLAG_FRAMING_NC and optional pad
1141 #define FRAMED_SIZE(mtu) (sizeof (struct nc_header) \
1142 				+ sizeof (struct ethhdr) \
1143 				+ (mtu) \
1144 				+ 1 \
1145 				+ sizeof (struct nc_trailer))
1146 
1147 #define MIN_FRAMED	FRAMED_SIZE(0)
1148 
1149 
1150 /*
1151  * Zero means no timeout; else, how long a 64 byte bulk packet may be queued
1152  * before the hardware drops it.  If that's done, the driver will need to
1153  * frame network packets to guard against the dropped USB packets.  The win32
1154  * driver sets this for both sides of the link.
1155  */
1156 #define	NC_READ_TTL_MS	((u8)255)	// ms
1157 
1158 /*
1159  * We ignore most registers and EEPROM contents.
1160  */
1161 #define	REG_USBCTL	((u8)0x04)
1162 #define REG_TTL		((u8)0x10)
1163 #define REG_STATUS	((u8)0x11)
1164 
1165 /*
1166  * Vendor specific requests to read/write data
1167  */
1168 #define	REQUEST_REGISTER	((u8)0x10)
1169 #define	REQUEST_EEPROM		((u8)0x11)
1170 
1171 static int
nc_vendor_read(struct usbnet * dev,u8 req,u8 regnum,u16 * retval_ptr)1172 nc_vendor_read (struct usbnet *dev, u8 req, u8 regnum, u16 *retval_ptr)
1173 {
1174 	int status = usb_control_msg (dev->udev,
1175 		usb_rcvctrlpipe (dev->udev, 0),
1176 		req,
1177 		USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1178 		0, regnum,
1179 		retval_ptr, sizeof *retval_ptr,
1180 		CONTROL_TIMEOUT_JIFFIES);
1181 	if (status > 0)
1182 		status = 0;
1183 	if (!status)
1184 		le16_to_cpus (retval_ptr);
1185 	return status;
1186 }
1187 
1188 static inline int
nc_register_read(struct usbnet * dev,u8 regnum,u16 * retval_ptr)1189 nc_register_read (struct usbnet *dev, u8 regnum, u16 *retval_ptr)
1190 {
1191 	return nc_vendor_read (dev, REQUEST_REGISTER, regnum, retval_ptr);
1192 }
1193 
1194 // no retval ... can become async, usable in_interrupt()
1195 static void
nc_vendor_write(struct usbnet * dev,u8 req,u8 regnum,u16 value)1196 nc_vendor_write (struct usbnet *dev, u8 req, u8 regnum, u16 value)
1197 {
1198 	usb_control_msg (dev->udev,
1199 		usb_sndctrlpipe (dev->udev, 0),
1200 		req,
1201 		USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1202 		value, regnum,
1203 		0, 0,			// data is in setup packet
1204 		CONTROL_TIMEOUT_JIFFIES);
1205 }
1206 
1207 static inline void
nc_register_write(struct usbnet * dev,u8 regnum,u16 value)1208 nc_register_write (struct usbnet *dev, u8 regnum, u16 value)
1209 {
1210 	nc_vendor_write (dev, REQUEST_REGISTER, regnum, value);
1211 }
1212 
1213 
1214 #if 0
1215 static void nc_dump_registers (struct usbnet *dev)
1216 {
1217 	u8	reg;
1218 	u16	*vp = kmalloc (sizeof (u16));
1219 
1220 	if (!vp) {
1221 		dbg ("no memory?");
1222 		return;
1223 	}
1224 
1225 	dbg ("%s registers:", dev->net.name);
1226 	for (reg = 0; reg < 0x20; reg++) {
1227 		int retval;
1228 
1229 		// reading some registers is trouble
1230 		if (reg >= 0x08 && reg <= 0xf)
1231 			continue;
1232 		if (reg >= 0x12 && reg <= 0x1e)
1233 			continue;
1234 
1235 		retval = nc_register_read (dev, reg, vp);
1236 		if (retval < 0)
1237 			dbg ("%s reg [0x%x] ==> error %d",
1238 				dev->net.name, reg, retval);
1239 		else
1240 			dbg ("%s reg [0x%x] = 0x%x",
1241 				dev->net.name, reg, *vp);
1242 	}
1243 	kfree (vp);
1244 }
1245 #endif
1246 
1247 
1248 /*-------------------------------------------------------------------------*/
1249 
1250 /*
1251  * Control register
1252  */
1253 
1254 #define	USBCTL_WRITABLE_MASK	0x1f0f
1255 // bits 15-13 reserved, r/o
1256 #define	USBCTL_ENABLE_LANG	(1 << 12)
1257 #define	USBCTL_ENABLE_MFGR	(1 << 11)
1258 #define	USBCTL_ENABLE_PROD	(1 << 10)
1259 #define	USBCTL_ENABLE_SERIAL	(1 << 9)
1260 #define	USBCTL_ENABLE_DEFAULTS	(1 << 8)
1261 // bits 7-4 reserved, r/o
1262 #define	USBCTL_FLUSH_OTHER	(1 << 3)
1263 #define	USBCTL_FLUSH_THIS	(1 << 2)
1264 #define	USBCTL_DISCONN_OTHER	(1 << 1)
1265 #define	USBCTL_DISCONN_THIS	(1 << 0)
1266 
nc_dump_usbctl(struct usbnet * dev,u16 usbctl)1267 static inline void nc_dump_usbctl (struct usbnet *dev, u16 usbctl)
1268 {
1269 #ifdef DEBUG
1270 	devdbg (dev, "net1080 %s-%s usbctl 0x%x:%s%s%s%s%s;"
1271 			" this%s%s;"
1272 			" other%s%s; r/o 0x%x",
1273 		dev->udev->bus->bus_name, dev->udev->devpath,
1274 		usbctl,
1275 		(usbctl & USBCTL_ENABLE_LANG) ? " lang" : "",
1276 		(usbctl & USBCTL_ENABLE_MFGR) ? " mfgr" : "",
1277 		(usbctl & USBCTL_ENABLE_PROD) ? " prod" : "",
1278 		(usbctl & USBCTL_ENABLE_SERIAL) ? " serial" : "",
1279 		(usbctl & USBCTL_ENABLE_DEFAULTS) ? " defaults" : "",
1280 
1281 		(usbctl & USBCTL_FLUSH_OTHER) ? " FLUSH" : "",
1282 		(usbctl & USBCTL_DISCONN_OTHER) ? " DIS" : "",
1283 		(usbctl & USBCTL_FLUSH_THIS) ? " FLUSH" : "",
1284 		(usbctl & USBCTL_DISCONN_THIS) ? " DIS" : "",
1285 		usbctl & ~USBCTL_WRITABLE_MASK
1286 		);
1287 #endif
1288 }
1289 
1290 /*-------------------------------------------------------------------------*/
1291 
1292 /*
1293  * Status register
1294  */
1295 
1296 #define	STATUS_PORT_A		(1 << 15)
1297 
1298 #define	STATUS_CONN_OTHER	(1 << 14)
1299 #define	STATUS_SUSPEND_OTHER	(1 << 13)
1300 #define	STATUS_MAILBOX_OTHER	(1 << 12)
1301 #define	STATUS_PACKETS_OTHER(n)	(((n) >> 8) & 0x03)
1302 
1303 #define	STATUS_CONN_THIS	(1 << 6)
1304 #define	STATUS_SUSPEND_THIS	(1 << 5)
1305 #define	STATUS_MAILBOX_THIS	(1 << 4)
1306 #define	STATUS_PACKETS_THIS(n)	(((n) >> 0) & 0x03)
1307 
1308 #define	STATUS_UNSPEC_MASK	0x0c8c
1309 #define	STATUS_NOISE_MASK 	((u16)~(0x0303|STATUS_UNSPEC_MASK))
1310 
1311 
nc_dump_status(struct usbnet * dev,u16 status)1312 static inline void nc_dump_status (struct usbnet *dev, u16 status)
1313 {
1314 #ifdef DEBUG
1315 	devdbg (dev, "net1080 %s-%s status 0x%x:"
1316 			" this (%c) PKT=%d%s%s%s;"
1317 			" other PKT=%d%s%s%s; unspec 0x%x",
1318 		dev->udev->bus->bus_name, dev->udev->devpath,
1319 		status,
1320 
1321 		// XXX the packet counts don't seem right
1322 		// (1 at reset, not 0); maybe UNSPEC too
1323 
1324 		(status & STATUS_PORT_A) ? 'A' : 'B',
1325 		STATUS_PACKETS_THIS (status),
1326 		(status & STATUS_CONN_THIS) ? " CON" : "",
1327 		(status & STATUS_SUSPEND_THIS) ? " SUS" : "",
1328 		(status & STATUS_MAILBOX_THIS) ? " MBOX" : "",
1329 
1330 		STATUS_PACKETS_OTHER (status),
1331 		(status & STATUS_CONN_OTHER) ? " CON" : "",
1332 		(status & STATUS_SUSPEND_OTHER) ? " SUS" : "",
1333 		(status & STATUS_MAILBOX_OTHER) ? " MBOX" : "",
1334 
1335 		status & STATUS_UNSPEC_MASK
1336 		);
1337 #endif
1338 }
1339 
1340 /*-------------------------------------------------------------------------*/
1341 
1342 /*
1343  * TTL register
1344  */
1345 
1346 #define	TTL_THIS(ttl)	(0x00ff & ttl)
1347 #define	TTL_OTHER(ttl)	(0x00ff & (ttl >> 8))
1348 #define MK_TTL(this,other)	((u16)(((other)<<8)|(0x00ff&(this))))
1349 
nc_dump_ttl(struct usbnet * dev,u16 ttl)1350 static inline void nc_dump_ttl (struct usbnet *dev, u16 ttl)
1351 {
1352 #ifdef DEBUG
1353 	devdbg (dev, "net1080 %s-%s ttl 0x%x this = %d, other = %d",
1354 		dev->udev->bus->bus_name, dev->udev->devpath,
1355 		ttl,
1356 
1357 		TTL_THIS (ttl),
1358 		TTL_OTHER (ttl)
1359 		);
1360 #endif
1361 }
1362 
1363 /*-------------------------------------------------------------------------*/
1364 
net1080_reset(struct usbnet * dev)1365 static int net1080_reset (struct usbnet *dev)
1366 {
1367 	u16		usbctl, status, ttl;
1368 	u16		*vp = kmalloc (sizeof (u16), GFP_KERNEL);
1369 	int		retval;
1370 
1371 	if (!vp)
1372 		return -ENOMEM;
1373 
1374 	// nc_dump_registers (dev);
1375 
1376 	if ((retval = nc_register_read (dev, REG_STATUS, vp)) < 0) {
1377 		dbg ("can't read %s-%s status: %d",
1378 			dev->udev->bus->bus_name, dev->udev->devpath, retval);
1379 		goto done;
1380 	}
1381 	status = *vp;
1382 	// nc_dump_status (dev, status);
1383 
1384 	if ((retval = nc_register_read (dev, REG_USBCTL, vp)) < 0) {
1385 		dbg ("can't read USBCTL, %d", retval);
1386 		goto done;
1387 	}
1388 	usbctl = *vp;
1389 	// nc_dump_usbctl (dev, usbctl);
1390 
1391 	nc_register_write (dev, REG_USBCTL,
1392 			USBCTL_FLUSH_THIS | USBCTL_FLUSH_OTHER);
1393 
1394 	if ((retval = nc_register_read (dev, REG_TTL, vp)) < 0) {
1395 		dbg ("can't read TTL, %d", retval);
1396 		goto done;
1397 	}
1398 	ttl = *vp;
1399 	// nc_dump_ttl (dev, ttl);
1400 
1401 	nc_register_write (dev, REG_TTL,
1402 			MK_TTL (NC_READ_TTL_MS, TTL_OTHER (ttl)) );
1403 	dbg ("%s: assigned TTL, %d ms", dev->net.name, NC_READ_TTL_MS);
1404 
1405 	if (dev->msg_level >= 2)
1406 		devinfo (dev, "port %c, peer %sconnected",
1407 			(status & STATUS_PORT_A) ? 'A' : 'B',
1408 			(status & STATUS_CONN_OTHER) ? "" : "dis"
1409 			);
1410 	retval = 0;
1411 
1412 done:
1413 	kfree (vp);
1414 	return retval;
1415 }
1416 
net1080_check_connect(struct usbnet * dev)1417 static int net1080_check_connect (struct usbnet *dev)
1418 {
1419 	int			retval;
1420 	u16			status;
1421 	u16			*vp = kmalloc (sizeof (u16), GFP_KERNEL);
1422 
1423 	if (!vp)
1424 		return -ENOMEM;
1425 	retval = nc_register_read (dev, REG_STATUS, vp);
1426 	status = *vp;
1427 	kfree (vp);
1428 	if (retval != 0) {
1429 		dbg ("%s net1080_check_conn read - %d", dev->net.name, retval);
1430 		return retval;
1431 	}
1432 	if ((status & STATUS_CONN_OTHER) != STATUS_CONN_OTHER)
1433 		return -ENOLINK;
1434 	return 0;
1435 }
1436 
net1080_rx_fixup(struct usbnet * dev,struct sk_buff * skb)1437 static int net1080_rx_fixup (struct usbnet *dev, struct sk_buff *skb)
1438 {
1439 	struct nc_header	*header;
1440 	struct nc_trailer	*trailer;
1441 
1442 	if (!(skb->len & 0x01)
1443 			|| MIN_FRAMED > skb->len
1444 			|| skb->len > FRAMED_SIZE (dev->net.mtu)) {
1445 		dev->stats.rx_frame_errors++;
1446 		dbg ("rx framesize %d range %d..%d mtu %d", skb->len,
1447 			(int)MIN_FRAMED, (int)FRAMED_SIZE (dev->net.mtu),
1448 			dev->net.mtu);
1449 		return 0;
1450 	}
1451 
1452 	header = (struct nc_header *) skb->data;
1453 	le16_to_cpus (&header->hdr_len);
1454 	le16_to_cpus (&header->packet_len);
1455 	if (FRAMED_SIZE (header->packet_len) > MAX_PACKET) {
1456 		dev->stats.rx_frame_errors++;
1457 		dbg ("packet too big, %d", header->packet_len);
1458 		return 0;
1459 	} else if (header->hdr_len < MIN_HEADER) {
1460 		dev->stats.rx_frame_errors++;
1461 		dbg ("header too short, %d", header->hdr_len);
1462 		return 0;
1463 	} else if (header->hdr_len > MIN_HEADER) {
1464 		// out of band data for us?
1465 		dbg ("header OOB, %d bytes",
1466 			header->hdr_len - MIN_HEADER);
1467 		// switch (vendor/product ids) { ... }
1468 	}
1469 	skb_pull (skb, header->hdr_len);
1470 
1471 	trailer = (struct nc_trailer *)
1472 		(skb->data + skb->len - sizeof *trailer);
1473 	skb_trim (skb, skb->len - sizeof *trailer);
1474 
1475 	if ((header->packet_len & 0x01) == 0) {
1476 		if (skb->data [header->packet_len] != PAD_BYTE) {
1477 			dev->stats.rx_frame_errors++;
1478 			dbg ("bad pad");
1479 			return 0;
1480 		}
1481 		skb_trim (skb, skb->len - 1);
1482 	}
1483 	if (skb->len != header->packet_len) {
1484 		dev->stats.rx_frame_errors++;
1485 		dbg ("bad packet len %d (expected %d)",
1486 			skb->len, header->packet_len);
1487 		return 0;
1488 	}
1489 	if (header->packet_id != get_unaligned (&trailer->packet_id)) {
1490 		dev->stats.rx_fifo_errors++;
1491 		dbg ("(2+ dropped) rx packet_id mismatch 0x%x 0x%x",
1492 			header->packet_id, trailer->packet_id);
1493 		return 0;
1494 	}
1495 #if 0
1496 	devdbg (dev, "frame <rx h %d p %d id %d", header->hdr_len,
1497 		header->packet_len, header->packet_id);
1498 #endif
1499 	return 1;
1500 }
1501 
1502 static struct sk_buff *
net1080_tx_fixup(struct usbnet * dev,struct sk_buff * skb,int flags)1503 net1080_tx_fixup (struct usbnet *dev, struct sk_buff *skb, int flags)
1504 {
1505 	int			padlen;
1506 	struct sk_buff		*skb2;
1507 
1508 	padlen = ((skb->len + sizeof (struct nc_header)
1509 			+ sizeof (struct nc_trailer)) & 0x01) ? 0 : 1;
1510 	if (!skb_cloned (skb)) {
1511 		int	headroom = skb_headroom (skb);
1512 		int	tailroom = skb_tailroom (skb);
1513 
1514 		if ((padlen + sizeof (struct nc_trailer)) <= tailroom
1515 			    && sizeof (struct nc_header) <= headroom)
1516 			return skb;
1517 
1518 		if ((sizeof (struct nc_header) + padlen
1519 					+ sizeof (struct nc_trailer)) <
1520 				(headroom + tailroom)) {
1521 			skb->data = memmove (skb->head
1522 						+ sizeof (struct nc_header),
1523 					    skb->data, skb->len);
1524 			skb->tail = skb->data + skb->len;
1525 			return skb;
1526 		}
1527 	}
1528 	skb2 = skb_copy_expand (skb,
1529 				sizeof (struct nc_header),
1530 				sizeof (struct nc_trailer) + padlen,
1531 				flags);
1532 	dev_kfree_skb_any (skb);
1533 	return skb2;
1534 }
1535 
1536 static const struct driver_info	net1080_info = {
1537 	.description =	"NetChip TurboCONNECT",
1538 	.flags =	FLAG_FRAMING_NC,
1539 	.reset =	net1080_reset,
1540 	.check_connect =net1080_check_connect,
1541 	.rx_fixup =	net1080_rx_fixup,
1542 	.tx_fixup =	net1080_tx_fixup,
1543 };
1544 
1545 #endif /* CONFIG_USB_NET1080 */
1546 
1547 
1548 
1549 #ifdef CONFIG_USB_PL2301
1550 
1551 /*-------------------------------------------------------------------------
1552  *
1553  * Prolific PL-2301/PL-2302 driver ... http://www.prolifictech.com
1554  *
1555  *-------------------------------------------------------------------------*/
1556 
1557 /*
1558  * Bits 0-4 can be used for software handshaking; they're set from
1559  * one end, cleared from the other, "read" with the interrupt byte.
1560  */
1561 #define	PL_S_EN		(1<<7)		/* (feature only) suspend enable */
1562 /* reserved bit -- rx ready (6) ? */
1563 #define	PL_TX_READY	(1<<5)		/* (interrupt only) transmit ready */
1564 #define	PL_RESET_OUT	(1<<4)		/* reset output pipe */
1565 #define	PL_RESET_IN	(1<<3)		/* reset input pipe */
1566 #define	PL_TX_C		(1<<2)		/* transmission complete */
1567 #define	PL_TX_REQ	(1<<1)		/* transmission received */
1568 #define	PL_PEER_E	(1<<0)		/* peer exists */
1569 
1570 static inline int
pl_vendor_req(struct usbnet * dev,u8 req,u8 val,u8 index)1571 pl_vendor_req (struct usbnet *dev, u8 req, u8 val, u8 index)
1572 {
1573 	return usb_control_msg (dev->udev,
1574 		usb_rcvctrlpipe (dev->udev, 0),
1575 		req,
1576 		USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1577 		val, index,
1578 		0, 0,
1579 		CONTROL_TIMEOUT_JIFFIES);
1580 }
1581 
1582 static inline int
pl_clear_QuickLink_features(struct usbnet * dev,int val)1583 pl_clear_QuickLink_features (struct usbnet *dev, int val)
1584 {
1585 	return pl_vendor_req (dev, 1, (u8) val, 0);
1586 }
1587 
1588 static inline int
pl_set_QuickLink_features(struct usbnet * dev,int val)1589 pl_set_QuickLink_features (struct usbnet *dev, int val)
1590 {
1591 	return pl_vendor_req (dev, 3, (u8) val, 0);
1592 }
1593 
1594 /*-------------------------------------------------------------------------*/
1595 
pl_reset(struct usbnet * dev)1596 static int pl_reset (struct usbnet *dev)
1597 {
1598 	return pl_set_QuickLink_features (dev,
1599 		PL_S_EN|PL_RESET_OUT|PL_RESET_IN|PL_PEER_E);
1600 }
1601 
1602 static const struct driver_info	prolific_info = {
1603 	.description =	"Prolific PL-2301/PL-2302",
1604 	.flags =	FLAG_NO_SETINT,
1605 		/* some PL-2302 versions seem to fail usb_set_interface() */
1606 	.reset =	pl_reset,
1607 };
1608 
1609 #endif /* CONFIG_USB_PL2301 */
1610 
1611 
1612 
1613 #ifdef	CONFIG_USB_ARMLINUX
1614 
1615 /*-------------------------------------------------------------------------
1616  *
1617  * Standard ARM kernels include a "usb-eth" driver, or a newer
1618  * "ethernet gadget" driver for basic USB connectivity.  The vendor
1619  * and product code may also be used for other non-CDC Linux devices,
1620  * if they all maintain protocol compatibility.
1621  *
1622  * That means lots of hardware could match here, possibly using
1623  * different endpoint numbers (and bcdVersion ids).  so we rely on
1624  * endpoint descriptors to sort that out for us.
1625  *
1626  * (Current Zaurus models need a different driver; see later.)
1627  *
1628  *-------------------------------------------------------------------------*/
1629 
1630 static const struct driver_info	linuxdev_info = {
1631 	.description =	"Linux Device",
1632 	.check_connect = always_connected,
1633 };
1634 
1635 static const struct driver_info	yopy_info = {
1636 	.description =	"Yopy",
1637 	.check_connect = always_connected,
1638 };
1639 
1640 static const struct driver_info	blob_info = {
1641 	.description =	"Boot Loader OBject",
1642 	.check_connect = always_connected,
1643 };
1644 
1645 #endif	/* CONFIG_USB_ARMLINUX */
1646 
1647 
1648 #ifdef CONFIG_USB_ZAURUS
1649 
1650 #include <linux/crc32.h>
1651 
1652 /*-------------------------------------------------------------------------
1653  *
1654  * Zaurus PDAs are also ARM based, but currently use different drivers
1655  * (and framing) for USB slave/gadget controllers than the case above.
1656  *
1657  * For the current version of that driver, the main way that framing is
1658  * nonstandard (also from perspective of the CDC ethernet model!) is a
1659  * crc32, added to help detect when some sa1100 usb-to-memory DMA errata
1660  * haven't been fully worked around.
1661  *
1662  *-------------------------------------------------------------------------*/
1663 
1664 static struct sk_buff *
zaurus_tx_fixup(struct usbnet * dev,struct sk_buff * skb,int flags)1665 zaurus_tx_fixup (struct usbnet *dev, struct sk_buff *skb, int flags)
1666 {
1667 	int			padlen;
1668 	struct sk_buff		*skb2;
1669 
1670 	padlen = 2;
1671 	if (!skb_cloned (skb)) {
1672 		int	tailroom = skb_tailroom (skb);
1673 		if ((padlen + 4) <= tailroom)
1674 			goto done;
1675 	}
1676 	skb2 = skb_copy_expand (skb, 0, 4 + padlen, flags);
1677 	dev_kfree_skb_any (skb);
1678 	skb = skb2;
1679 	if (skb) {
1680 		u32		fcs;
1681 done:
1682 		fcs = crc32_le (~0, skb->data, skb->len);
1683 		fcs = ~fcs;
1684 
1685 		*skb_put (skb, 1) = fcs       & 0xff;
1686 		*skb_put (skb, 1) = (fcs>> 8) & 0xff;
1687 		*skb_put (skb, 1) = (fcs>>16) & 0xff;
1688 		*skb_put (skb, 1) = (fcs>>24) & 0xff;
1689 	}
1690 	return skb;
1691 }
1692 
1693 /* SA-1100 based */
1694 static const struct driver_info	zaurus_sl5x00_info = {
1695 	.description =	"Sharp Zaurus SL-5x00",
1696 	.flags =	FLAG_FRAMING_Z,
1697 	.check_connect = always_connected,
1698 	.tx_fixup = 	zaurus_tx_fixup,
1699 
1700 	.in = 2, .out = 1,
1701 	.epsize = 64,
1702 };
1703 
1704 /* PXA-2xx based */
1705 static const struct driver_info zaurus_pxa_info = {
1706 	.description =	"Sharp Zaurus, PXA-2xx based",
1707 	.flags =	FLAG_FRAMING_Z,
1708 	.check_connect = always_connected,
1709 	.tx_fixup = 	zaurus_tx_fixup,
1710 
1711 	.in = 1, .out = 2,
1712 	.epsize = 64,
1713 };
1714 
1715 #endif
1716 
1717 
1718 /*-------------------------------------------------------------------------
1719  *
1720  * Network Device Driver (peer link to "Host Device", from USB host)
1721  *
1722  *-------------------------------------------------------------------------*/
1723 
usbnet_change_mtu(struct net_device * net,int new_mtu)1724 static int usbnet_change_mtu (struct net_device *net, int new_mtu)
1725 {
1726 	struct usbnet	*dev = (struct usbnet *) net->priv;
1727 
1728 	if (new_mtu <= MIN_PACKET || new_mtu > MAX_PACKET)
1729 		return -EINVAL;
1730 #ifdef	CONFIG_USB_NET1080
1731 	if (((dev->driver_info->flags) & FLAG_FRAMING_NC)) {
1732 		if (FRAMED_SIZE (new_mtu) > MAX_PACKET)
1733 			return -EINVAL;
1734 	}
1735 #endif
1736 #ifdef	CONFIG_USB_GENESYS
1737 	if (((dev->driver_info->flags) & FLAG_FRAMING_GL)
1738 			&& new_mtu > GL_MAX_PACKET_LEN)
1739 		return -EINVAL;
1740 #endif
1741 	// no second zero-length packet read wanted after mtu-sized packets
1742 	if (((new_mtu + sizeof (struct ethhdr)) % dev->maxpacket) == 0)
1743 		return -EDOM;
1744 	net->mtu = new_mtu;
1745 	return 0;
1746 }
1747 
1748 /*-------------------------------------------------------------------------*/
1749 
usbnet_get_stats(struct net_device * net)1750 static struct net_device_stats *usbnet_get_stats (struct net_device *net)
1751 {
1752 	return &((struct usbnet *) net->priv)->stats;
1753 }
1754 
1755 /*-------------------------------------------------------------------------*/
1756 
1757 /* urb completions may be in_irq; avoid doing real work then. */
1758 
defer_bh(struct usbnet * dev,struct sk_buff * skb)1759 static void defer_bh (struct usbnet *dev, struct sk_buff *skb)
1760 {
1761 	struct sk_buff_head	*list = skb->list;
1762 	unsigned long		flags;
1763 
1764 	spin_lock_irqsave (&list->lock, flags);
1765 	__skb_unlink (skb, list);
1766 	spin_unlock (&list->lock);
1767 	spin_lock (&dev->done.lock);
1768 	__skb_queue_tail (&dev->done, skb);
1769 	if (dev->done.qlen == 1)
1770 		tasklet_schedule (&dev->bh);
1771 	spin_unlock_irqrestore (&dev->done.lock, flags);
1772 }
1773 
1774 /* some work can't be done in tasklets, so we use keventd
1775  *
1776  * NOTE:  annoying asymmetry:  if it's active, schedule_task() fails,
1777  * but tasklet_schedule() doesn't.  hope the failure is rare.
1778  */
defer_kevent(struct usbnet * dev,int work)1779 static void defer_kevent (struct usbnet *dev, int work)
1780 {
1781 	set_bit (work, &dev->flags);
1782 	if (!schedule_task (&dev->kevent))
1783 		err ("%s: kevent %d may have been dropped",
1784 			dev->net.name, work);
1785 	else
1786 		dbg ("%s: kevent %d scheduled", dev->net.name, work);
1787 }
1788 
1789 /*-------------------------------------------------------------------------*/
1790 
1791 static void rx_complete (struct urb *urb);
1792 
rx_submit(struct usbnet * dev,struct urb * urb,int flags)1793 static void rx_submit (struct usbnet *dev, struct urb *urb, int flags)
1794 {
1795 	struct sk_buff		*skb;
1796 	struct skb_data		*entry;
1797 	int			retval = 0;
1798 	unsigned long		lockflags;
1799 	size_t			size;
1800 
1801 #ifdef CONFIG_USB_NET1080
1802 	if (dev->driver_info->flags & FLAG_FRAMING_NC)
1803 		size = FRAMED_SIZE (dev->net.mtu);
1804 	else
1805 #endif
1806 #ifdef CONFIG_USB_GENESYS
1807 	if (dev->driver_info->flags & FLAG_FRAMING_GL)
1808 		size = GL_RCV_BUF_SIZE;
1809 	else
1810 #endif
1811 #ifdef CONFIG_USB_ZAURUS
1812 	if (dev->driver_info->flags & FLAG_FRAMING_Z)
1813 		size = 6 + (sizeof (struct ethhdr) + dev->net.mtu);
1814 	else
1815 #endif
1816 		size = (sizeof (struct ethhdr) + dev->net.mtu);
1817 
1818 	if ((skb = alloc_skb (size, flags)) == 0) {
1819 		dbg ("no rx skb");
1820 		defer_kevent (dev, EVENT_RX_MEMORY);
1821 		usb_free_urb (urb);
1822 		return;
1823 	}
1824 
1825 	entry = (struct skb_data *) skb->cb;
1826 	entry->urb = urb;
1827 	entry->dev = dev;
1828 	entry->state = rx_start;
1829 	entry->length = 0;
1830 
1831 	usb_fill_bulk_urb (urb, dev->udev, dev->in,
1832 		skb->data, size, rx_complete, skb);
1833 	urb->transfer_flags |= USB_ASYNC_UNLINK;
1834 
1835 	spin_lock_irqsave (&dev->rxq.lock, lockflags);
1836 
1837 	if (netif_running (&dev->net)
1838 			&& !test_bit (EVENT_RX_HALT, &dev->flags)) {
1839 		switch (retval = SUBMIT_URB (urb, GFP_ATOMIC)){
1840 		case -EPIPE:
1841 			defer_kevent (dev, EVENT_RX_HALT);
1842 			break;
1843 		case -ENOMEM:
1844 			defer_kevent (dev, EVENT_RX_MEMORY);
1845 			break;
1846 		default:
1847 			dbg ("%s rx submit, %d", dev->net.name, retval);
1848 			tasklet_schedule (&dev->bh);
1849 			break;
1850 		case 0:
1851 			__skb_queue_tail (&dev->rxq, skb);
1852 		}
1853 	} else {
1854 		dbg ("rx: stopped");
1855 		retval = -ENOLINK;
1856 	}
1857 	spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
1858 	if (retval) {
1859 		dev_kfree_skb_any (skb);
1860 		usb_free_urb (urb);
1861 	}
1862 }
1863 
1864 
1865 /*-------------------------------------------------------------------------*/
1866 
rx_process(struct usbnet * dev,struct sk_buff * skb)1867 static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
1868 {
1869 	if (dev->driver_info->rx_fixup
1870 			&& !dev->driver_info->rx_fixup (dev, skb))
1871 		goto error;
1872 	// else network stack removes extra byte if we forced a short packet
1873 
1874 	if (skb->len) {
1875 		int	status;
1876 
1877 		skb->dev = &dev->net;
1878 		skb->protocol = eth_type_trans (skb, &dev->net);
1879 		dev->stats.rx_packets++;
1880 		dev->stats.rx_bytes += skb->len;
1881 
1882 #ifdef	VERBOSE
1883 		devdbg (dev, "< rx, len %d, type 0x%x",
1884 			skb->len + sizeof (struct ethhdr), skb->protocol);
1885 #endif
1886 		memset (skb->cb, 0, sizeof (struct skb_data));
1887 		status = netif_rx (skb);
1888 		if (status != NET_RX_SUCCESS)
1889 			devdbg (dev, "netif_rx status %d", status);
1890 	} else {
1891 		dbg ("drop");
1892 error:
1893 		dev->stats.rx_errors++;
1894 		skb_queue_tail (&dev->done, skb);
1895 	}
1896 }
1897 
1898 /*-------------------------------------------------------------------------*/
1899 
rx_complete(struct urb * urb)1900 static void rx_complete (struct urb *urb)
1901 {
1902 	struct sk_buff		*skb = (struct sk_buff *) urb->context;
1903 	struct skb_data		*entry = (struct skb_data *) skb->cb;
1904 	struct usbnet		*dev = entry->dev;
1905 	int			urb_status = urb->status;
1906 
1907 	skb_put (skb, urb->actual_length);
1908 	entry->state = rx_done;
1909 	entry->urb = 0;
1910 
1911 	switch (urb_status) {
1912 	    // success
1913 	    case 0:
1914 		if (MIN_PACKET > skb->len || skb->len > MAX_PACKET) {
1915 			entry->state = rx_cleanup;
1916 			dev->stats.rx_errors++;
1917 			dev->stats.rx_length_errors++;
1918 			dbg ("rx length %d", skb->len);
1919 		}
1920 		break;
1921 
1922 	    // stalls need manual reset. this is rare ... except that
1923 	    // when going through USB 2.0 TTs, unplug appears this way.
1924 	    // we avoid the highspeed version of the ETIMEOUT/EILSEQ
1925 	    // storm, recovering as needed.
1926 	    case -EPIPE:
1927 		defer_kevent (dev, EVENT_RX_HALT);
1928 		// FALLTHROUGH
1929 
1930 	    // software-driven interface shutdown
1931 	    case -ECONNRESET:		// according to API spec
1932 	    case -ECONNABORTED:		// some (now fixed?) UHCI bugs
1933 		dbg ("%s rx shutdown, code %d", dev->net.name, urb_status);
1934 		entry->state = rx_cleanup;
1935 		// do urb frees only in the tasklet (UHCI has oopsed ...)
1936 		entry->urb = urb;
1937 		urb = 0;
1938 		break;
1939 
1940 	    // data overrun ... flush fifo?
1941 	    case -EOVERFLOW:
1942 		dev->stats.rx_over_errors++;
1943 		// FALLTHROUGH
1944 
1945 	    default:
1946 		// on unplug we get ETIMEDOUT (ohci) or EILSEQ (uhci)
1947 		// until khubd sees its interrupt and disconnects us.
1948 		// that can easily be hundreds of passes through here.
1949 		entry->state = rx_cleanup;
1950 		dev->stats.rx_errors++;
1951 		dbg ("%s rx: status %d", dev->net.name, urb_status);
1952 		break;
1953 	}
1954 
1955 	defer_bh (dev, skb);
1956 
1957 	if (urb) {
1958 		if (netif_running (&dev->net)
1959 				&& !test_bit (EVENT_RX_HALT, &dev->flags)) {
1960 			rx_submit (dev, urb, GFP_ATOMIC);
1961 			return;
1962 		}
1963 		usb_free_urb (urb);
1964 	}
1965 #ifdef	VERBOSE
1966 	dbg ("no read resubmitted");
1967 #endif /* VERBOSE */
1968 }
1969 
1970 /*-------------------------------------------------------------------------*/
1971 
1972 // unlink pending rx/tx; completion handlers do all other cleanup
1973 
unlink_urbs(struct sk_buff_head * q)1974 static int unlink_urbs (struct sk_buff_head *q)
1975 {
1976 	unsigned long		flags;
1977 	struct sk_buff		*skb, *skbnext;
1978 	int			count = 0;
1979 
1980 	spin_lock_irqsave (&q->lock, flags);
1981 	for (skb = q->next; skb != (struct sk_buff *) q; skb = skbnext) {
1982 		struct skb_data		*entry;
1983 		struct urb		*urb;
1984 		int			retval;
1985 
1986 		entry = (struct skb_data *) skb->cb;
1987 		urb = entry->urb;
1988 		skbnext = skb->next;
1989 
1990 		// during some PM-driven resume scenarios,
1991 		// these (async) unlinks complete immediately
1992 		retval = usb_unlink_urb (urb);
1993 		if (retval != -EINPROGRESS && retval != 0)
1994 			dbg ("unlink urb err, %d", retval);
1995 		else
1996 			count++;
1997 	}
1998 	spin_unlock_irqrestore (&q->lock, flags);
1999 	return count;
2000 }
2001 
2002 
2003 /*-------------------------------------------------------------------------*/
2004 
2005 // precondition: never called in_interrupt
2006 
usbnet_stop(struct net_device * net)2007 static int usbnet_stop (struct net_device *net)
2008 {
2009 	struct usbnet		*dev = (struct usbnet *) net->priv;
2010 	int			temp;
2011 	DECLARE_WAIT_QUEUE_HEAD (unlink_wakeup);
2012 	DECLARE_WAITQUEUE (wait, current);
2013 
2014 	mutex_lock (&dev->mutex);
2015 	netif_stop_queue (net);
2016 
2017 	if (dev->msg_level >= 2)
2018 		devinfo (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld",
2019 			dev->stats.rx_packets, dev->stats.tx_packets,
2020 			dev->stats.rx_errors, dev->stats.tx_errors
2021 			);
2022 
2023 	// ensure there are no more active urbs
2024 	add_wait_queue (&unlink_wakeup, &wait);
2025 	dev->wait = &unlink_wakeup;
2026 	temp = unlink_urbs (&dev->txq) + unlink_urbs (&dev->rxq);
2027 
2028 	// maybe wait for deletions to finish.
2029 	while (skb_queue_len (&dev->rxq)
2030 			&& skb_queue_len (&dev->txq)
2031 			&& skb_queue_len (&dev->done)) {
2032 		set_current_state (TASK_UNINTERRUPTIBLE);
2033 		schedule_timeout (UNLINK_TIMEOUT_JIFFIES);
2034 		dbg ("waited for %d urb completions", temp);
2035 	}
2036 	dev->wait = 0;
2037 	remove_wait_queue (&unlink_wakeup, &wait);
2038 
2039 	mutex_unlock (&dev->mutex);
2040 	return 0;
2041 }
2042 
2043 /*-------------------------------------------------------------------------*/
2044 
2045 // posts reads, and enables write queing
2046 
2047 // precondition: never called in_interrupt
2048 
usbnet_open(struct net_device * net)2049 static int usbnet_open (struct net_device *net)
2050 {
2051 	struct usbnet		*dev = (struct usbnet *) net->priv;
2052 	int			retval = 0;
2053 	struct driver_info	*info = dev->driver_info;
2054 
2055 	mutex_lock (&dev->mutex);
2056 
2057 	// put into "known safe" state
2058 	if (info->reset && (retval = info->reset (dev)) < 0) {
2059 		devinfo (dev, "open reset fail (%d) usbnet usb-%s-%s, %s",
2060 			retval,
2061 			dev->udev->bus->bus_name, dev->udev->devpath,
2062 			info->description);
2063 		goto done;
2064 	}
2065 
2066 	// insist peer be connected
2067 	if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
2068 		devdbg (dev, "can't open; %d", retval);
2069 		goto done;
2070 	}
2071 
2072 	netif_start_queue (net);
2073 	if (dev->msg_level >= 2)
2074 		devinfo (dev, "open: enable queueing "
2075 				"(rx %d, tx %d) mtu %d %s framing",
2076 			RX_QLEN, TX_QLEN, dev->net.mtu,
2077 			(info->flags & (FLAG_FRAMING_NC | FLAG_FRAMING_GL))
2078 			    ? ((info->flags & FLAG_FRAMING_NC)
2079 				? "NetChip"
2080 				: "GeneSys")
2081 			    : "raw"
2082 			);
2083 
2084 	// delay posting reads until we're fully open
2085 	tasklet_schedule (&dev->bh);
2086 done:
2087 	mutex_unlock (&dev->mutex);
2088 	return retval;
2089 }
2090 
2091 /*-------------------------------------------------------------------------*/
2092 
usbnet_get_drvinfo(struct net_device * net,struct ethtool_drvinfo * info)2093 static void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
2094 {
2095 	struct usbnet *dev = net->priv;
2096 
2097 	strncpy (info->driver, driver_name, sizeof info->driver);
2098 	strncpy (info->version, DRIVER_VERSION, sizeof info->version);
2099 	strncpy (info->fw_version, dev->driver_info->description,
2100 		sizeof info->fw_version);
2101 	usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
2102 }
2103 
usbnet_get_link(struct net_device * net)2104 static u32 usbnet_get_link (struct net_device *net)
2105 {
2106 	struct usbnet *dev = net->priv;
2107 
2108 	/* If a check_connect is defined, return it's results */
2109 	if (dev->driver_info->check_connect)
2110 		return dev->driver_info->check_connect (dev) == 0;
2111 
2112 	/* Otherwise, we're up to avoid breaking scripts */
2113 	return 1;
2114 }
2115 
usbnet_get_msglevel(struct net_device * net)2116 static u32 usbnet_get_msglevel (struct net_device *net)
2117 {
2118 	struct usbnet *dev = net->priv;
2119 
2120 	return dev->msg_level;
2121 }
2122 
usbnet_set_msglevel(struct net_device * net,u32 level)2123 static void usbnet_set_msglevel (struct net_device *net, u32 level)
2124 {
2125 	struct usbnet *dev = net->priv;
2126 
2127 	dev->msg_level = level;
2128 }
2129 
usbnet_ioctl(struct net_device * net,struct ifreq * rq,int cmd)2130 static int usbnet_ioctl (struct net_device *net, struct ifreq *rq, int cmd)
2131 {
2132 #ifdef NEED_MII
2133 	{
2134 	struct usbnet *dev = (struct usbnet *)net->priv;
2135 
2136 	if (dev->mii.mdio_read != NULL && dev->mii.mdio_write != NULL)
2137 		return generic_mii_ioctl(&dev->mii,
2138 				(struct mii_ioctl_data *) &rq->ifr_data,
2139 				cmd, NULL);
2140 	}
2141 #endif
2142 	return -EOPNOTSUPP;
2143 }
2144 
2145 /*-------------------------------------------------------------------------*/
2146 
2147 /* work that cannot be done in interrupt context uses keventd.
2148  *
2149  * NOTE:  "uhci" and "usb-uhci" may have trouble with this since they don't
2150  * queue control transfers to individual devices, and other threads could
2151  * trigger control requests concurrently.  hope that's rare.
2152  */
2153 static void
kevent(void * data)2154 kevent (void *data)
2155 {
2156 	struct usbnet		*dev = data;
2157 	int			status;
2158 
2159 	/* usb_clear_halt() needs a thread context */
2160 	if (test_bit (EVENT_TX_HALT, &dev->flags)) {
2161 		unlink_urbs (&dev->txq);
2162 		status = usb_clear_halt (dev->udev, dev->out);
2163 		if (status < 0)
2164 			err ("%s: can't clear tx halt, status %d",
2165 				dev->net.name, status);
2166 		else {
2167 			clear_bit (EVENT_TX_HALT, &dev->flags);
2168 			netif_wake_queue (&dev->net);
2169 		}
2170 	}
2171 	if (test_bit (EVENT_RX_HALT, &dev->flags)) {
2172 		unlink_urbs (&dev->rxq);
2173 		status = usb_clear_halt (dev->udev, dev->in);
2174 		if (status < 0)
2175 			err ("%s: can't clear rx halt, status %d",
2176 				dev->net.name, status);
2177 		else {
2178 			clear_bit (EVENT_RX_HALT, &dev->flags);
2179 			tasklet_schedule (&dev->bh);
2180 		}
2181 	}
2182 
2183 	/* tasklet could resubmit itself forever if memory is tight */
2184 	if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
2185 		struct urb	*urb = 0;
2186 
2187 		if (netif_running (&dev->net))
2188 			urb = ALLOC_URB (0, GFP_KERNEL);
2189 		else
2190 			clear_bit (EVENT_RX_MEMORY, &dev->flags);
2191 		if (urb != 0) {
2192 			clear_bit (EVENT_RX_MEMORY, &dev->flags);
2193 			rx_submit (dev, urb, GFP_KERNEL);
2194 			tasklet_schedule (&dev->bh);
2195 		}
2196 	}
2197 
2198 	if (dev->flags)
2199 		dbg ("%s: kevent done, flags = 0x%lx",
2200 			dev->net.name, dev->flags);
2201 }
2202 
2203 /*-------------------------------------------------------------------------*/
2204 
tx_complete(struct urb * urb)2205 static void tx_complete (struct urb *urb)
2206 {
2207 	struct sk_buff		*skb = (struct sk_buff *) urb->context;
2208 	struct skb_data		*entry = (struct skb_data *) skb->cb;
2209 	struct usbnet		*dev = entry->dev;
2210 
2211 	if (urb->status == -EPIPE)
2212 		defer_kevent (dev, EVENT_TX_HALT);
2213 	urb->dev = 0;
2214 	entry->state = tx_done;
2215 	defer_bh (dev, skb);
2216 }
2217 
2218 /*-------------------------------------------------------------------------*/
2219 
usbnet_tx_timeout(struct net_device * net)2220 static void usbnet_tx_timeout (struct net_device *net)
2221 {
2222 	struct usbnet		*dev = (struct usbnet *) net->priv;
2223 
2224 	unlink_urbs (&dev->txq);
2225 	tasklet_schedule (&dev->bh);
2226 
2227 	// FIXME: device recovery -- reset?
2228 }
2229 
2230 /*-------------------------------------------------------------------------*/
2231 
usbnet_start_xmit(struct sk_buff * skb,struct net_device * net)2232 static int usbnet_start_xmit (struct sk_buff *skb, struct net_device *net)
2233 {
2234 	struct usbnet		*dev = (struct usbnet *) net->priv;
2235 	int			length;
2236 	int			retval = NET_XMIT_SUCCESS;
2237 	struct urb		*urb = 0;
2238 	struct skb_data		*entry;
2239 	struct driver_info	*info = dev->driver_info;
2240 	unsigned long		flags;
2241 #ifdef	CONFIG_USB_NET1080
2242 	struct nc_header	*header = 0;
2243 	struct nc_trailer	*trailer = 0;
2244 #endif	/* CONFIG_USB_NET1080 */
2245 
2246 	// some devices want funky USB-level framing, for
2247 	// win32 driver (usually) and/or hardware quirks
2248 	if (info->tx_fixup) {
2249 		skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
2250 		if (!skb) {
2251 			dbg ("can't tx_fixup skb");
2252 			goto drop;
2253 		}
2254 	}
2255 	length = skb->len;
2256 
2257 	if (!(urb = ALLOC_URB (0, GFP_ATOMIC))) {
2258 		dbg ("no urb");
2259 		goto drop;
2260 	}
2261 
2262 	entry = (struct skb_data *) skb->cb;
2263 	entry->urb = urb;
2264 	entry->dev = dev;
2265 	entry->state = tx_start;
2266 	entry->length = length;
2267 
2268 	// FIXME: reorganize a bit, so that fixup() fills out NetChip
2269 	// framing too. (Packet ID update needs the spinlock...)
2270 	// [ BETTER:  we already own net->xmit_lock, that's enough ]
2271 
2272 #ifdef	CONFIG_USB_NET1080
2273 	if (info->flags & FLAG_FRAMING_NC) {
2274 		header = (struct nc_header *) skb_push (skb, sizeof *header);
2275 		header->hdr_len = cpu_to_le16 (sizeof (*header));
2276 		header->packet_len = cpu_to_le16 (length);
2277 		if (!((skb->len + sizeof *trailer) & 0x01))
2278 			*skb_put (skb, 1) = PAD_BYTE;
2279 		trailer = (struct nc_trailer *) skb_put (skb, sizeof *trailer);
2280 	} else
2281 #endif	/* CONFIG_USB_NET1080 */
2282 
2283 	/* don't assume the hardware handles USB_ZERO_PACKET */
2284 	if ((length % dev->maxpacket) == 0)
2285 		skb->len++;
2286 
2287 	usb_fill_bulk_urb (urb, dev->udev, dev->out,
2288 			skb->data, skb->len, tx_complete, skb);
2289 	urb->transfer_flags |= USB_ASYNC_UNLINK;
2290 
2291 	spin_lock_irqsave (&dev->txq.lock, flags);
2292 
2293 #ifdef	CONFIG_USB_NET1080
2294 	if (info->flags & FLAG_FRAMING_NC) {
2295 		header->packet_id = cpu_to_le16 (dev->packet_id++);
2296 		put_unaligned (header->packet_id, &trailer->packet_id);
2297 #if 0
2298 		devdbg (dev, "frame >tx h %d p %d id %d",
2299 			header->hdr_len, header->packet_len,
2300 			header->packet_id);
2301 #endif
2302 	}
2303 #endif	/* CONFIG_USB_NET1080 */
2304 
2305 	switch ((retval = SUBMIT_URB (urb, GFP_ATOMIC))) {
2306 	case -EPIPE:
2307 		netif_stop_queue (net);
2308 		defer_kevent (dev, EVENT_TX_HALT);
2309 		break;
2310 	default:
2311 		dbg ("%s tx: submit urb err %d", net->name, retval);
2312 		break;
2313 	case 0:
2314 		net->trans_start = jiffies;
2315 		__skb_queue_tail (&dev->txq, skb);
2316 		if (dev->txq.qlen >= TX_QLEN)
2317 			netif_stop_queue (net);
2318 	}
2319 	spin_unlock_irqrestore (&dev->txq.lock, flags);
2320 
2321 	if (retval) {
2322 		devdbg (dev, "drop, code %d", retval);
2323 drop:
2324 		retval = NET_XMIT_SUCCESS;
2325 		dev->stats.tx_dropped++;
2326 		if (skb)
2327 			dev_kfree_skb_any (skb);
2328 		usb_free_urb (urb);
2329 #ifdef	VERBOSE
2330 	} else {
2331 		devdbg (dev, "> tx, len %d, type 0x%x",
2332 			length, skb->protocol);
2333 #endif
2334 	}
2335 	return retval;
2336 }
2337 
2338 
2339 /*-------------------------------------------------------------------------*/
2340 
2341 // tasklet ... work that avoided running in_irq()
2342 
usbnet_bh(unsigned long param)2343 static void usbnet_bh (unsigned long param)
2344 {
2345 	struct usbnet		*dev = (struct usbnet *) param;
2346 	struct sk_buff		*skb;
2347 	struct skb_data		*entry;
2348 
2349 	while ((skb = skb_dequeue (&dev->done))) {
2350 		entry = (struct skb_data *) skb->cb;
2351 		switch (entry->state) {
2352 		    case rx_done:
2353 			entry->state = rx_cleanup;
2354 			rx_process (dev, skb);
2355 			continue;
2356 		    case tx_done:
2357 			if (entry->urb->status) {
2358 				// can this statistic become more specific?
2359 				dev->stats.tx_errors++;
2360 				dbg ("%s tx: err %d", dev->net.name,
2361 					entry->urb->status);
2362 			} else {
2363 				dev->stats.tx_packets++;
2364 				dev->stats.tx_bytes += entry->length;
2365 			}
2366 			// FALLTHROUGH:
2367 		    case rx_cleanup:
2368 			usb_free_urb (entry->urb);
2369 			dev_kfree_skb (skb);
2370 			continue;
2371 		    default:
2372 			dbg ("%s: bogus skb state %d",
2373 				dev->net.name, entry->state);
2374 		}
2375 	}
2376 
2377 	// waiting for all pending urbs to complete?
2378 	if (dev->wait) {
2379 		if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
2380 			wake_up (dev->wait);
2381 		}
2382 
2383 	// or are we maybe short a few urbs?
2384 	} else if (netif_running (&dev->net)
2385 			&& !test_bit (EVENT_RX_HALT, &dev->flags)) {
2386 		int	temp = dev->rxq.qlen;
2387 
2388 		if (temp < RX_QLEN) {
2389 			struct urb	*urb;
2390 			int		i;
2391 			for (i = 0; i < 3 && dev->rxq.qlen < RX_QLEN; i++) {
2392 				if ((urb = ALLOC_URB (0, GFP_ATOMIC)) != 0)
2393 					rx_submit (dev, urb, GFP_ATOMIC);
2394 			}
2395 			if (temp != dev->rxq.qlen)
2396 				devdbg (dev, "rxqlen %d --> %d",
2397 						temp, dev->rxq.qlen);
2398 			if (dev->rxq.qlen < RX_QLEN)
2399 				tasklet_schedule (&dev->bh);
2400 		}
2401 		if (dev->txq.qlen < TX_QLEN)
2402 			netif_wake_queue (&dev->net);
2403 	}
2404 }
2405 
2406 
2407 
2408 /*-------------------------------------------------------------------------
2409  *
2410  * USB Device Driver support
2411  *
2412  *-------------------------------------------------------------------------*/
2413 
2414 // precondition: never called in_interrupt
2415 
usbnet_disconnect(struct usb_device * udev,void * ptr)2416 static void usbnet_disconnect (struct usb_device *udev, void *ptr)
2417 {
2418 	struct usbnet	*dev = (struct usbnet *) ptr;
2419 
2420 	devinfo (dev, "unregister usbnet usb-%s-%s, %s",
2421 		udev->bus->bus_name, udev->devpath,
2422 		dev->driver_info->description);
2423 
2424 	unregister_netdev (&dev->net);
2425 
2426 	mutex_lock (&usbnet_mutex);
2427 	mutex_lock (&dev->mutex);
2428 	list_del (&dev->dev_list);
2429 	mutex_unlock (&usbnet_mutex);
2430 
2431 	// assuming we used keventd, it must quiesce too
2432 	flush_scheduled_tasks ();
2433 
2434 	kfree (dev);
2435 	usb_put_dev (udev);
2436 }
2437 
2438 
2439 /*-------------------------------------------------------------------------*/
2440 
2441 // precondition: never called in_interrupt
2442 
2443 static void *
usbnet_probe(struct usb_device * udev,unsigned ifnum,const struct usb_device_id * prod)2444 usbnet_probe (struct usb_device *udev, unsigned ifnum,
2445 			const struct usb_device_id *prod)
2446 {
2447 	struct usbnet			*dev;
2448 	struct net_device 		*net;
2449 	struct driver_info		*info;
2450 	int				altnum = 0;
2451 	int				status;
2452 
2453 	info = (struct driver_info *) prod->driver_info;
2454 
2455 #ifdef CONFIG_USB_ZAURUS
2456 	if (info == &zaurus_sl5x00_info) {
2457 		int	status;
2458 
2459 		/* old ROMs have more than one config
2460 		 * so we have to make sure config="1" (?)
2461 		 */
2462 		status = usb_set_configuration (udev, 1);
2463 		if (status < 0) {
2464 			err ("set_config failed, %d", status);
2465 			return 0;
2466 		}
2467 		altnum = 1;
2468 	}
2469 #endif
2470 
2471 	// more sanity (unless the device is broken)
2472 	if (!(info->flags & FLAG_NO_SETINT)) {
2473 		if (usb_set_interface (udev, ifnum, altnum) < 0) {
2474 			err ("set_interface failed");
2475 			return 0;
2476 		}
2477 	}
2478 
2479 	// set up our own records
2480 	if (!(dev = kmalloc (sizeof *dev, GFP_KERNEL))) {
2481 		dbg ("can't kmalloc dev");
2482 		return 0;
2483 	}
2484 	memset (dev, 0, sizeof *dev);
2485 
2486 	init_MUTEX_LOCKED (&dev->mutex);
2487 	usb_get_dev (udev);
2488 	dev->udev = udev;
2489 	dev->driver_info = info;
2490 	dev->msg_level = msg_level;
2491 	INIT_LIST_HEAD (&dev->dev_list);
2492 	skb_queue_head_init (&dev->rxq);
2493 	skb_queue_head_init (&dev->txq);
2494 	skb_queue_head_init (&dev->done);
2495 	dev->bh.func = usbnet_bh;
2496 	dev->bh.data = (unsigned long) dev;
2497 	INIT_TQUEUE (&dev->kevent, kevent, dev);
2498 
2499 	// set up network interface records
2500 	net = &dev->net;
2501 	SET_MODULE_OWNER (net);
2502 	net->priv = dev;
2503 	strcpy (net->name, "usb%d");
2504 	memcpy (net->dev_addr, node_id, sizeof node_id);
2505 
2506 	// point-to-point link ... we always use Ethernet headers
2507 	// supports win32 interop and the bridge driver.
2508 	ether_setup (net);
2509 
2510 	net->change_mtu = usbnet_change_mtu;
2511 	net->get_stats = usbnet_get_stats;
2512 	net->hard_start_xmit = usbnet_start_xmit;
2513 	net->open = usbnet_open;
2514 	net->stop = usbnet_stop;
2515 	net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
2516 	net->tx_timeout = usbnet_tx_timeout;
2517 	net->do_ioctl = usbnet_ioctl;
2518 	net->ethtool_ops = &usbnet_ethtool_ops;
2519 
2520 	// allow device-specific bind/init procedures
2521 	// NOTE net->name still not usable ...
2522 	if (info->bind) {
2523 		status = info->bind (dev, udev);
2524 		// heuristic:  "usb%d" for links we know are two-host,
2525 		// else "eth%d" when there's reasonable doubt.  userspace
2526 		// can rename the link if it knows better.
2527 		if ((dev->driver_info->flags & FLAG_ETHER) != 0
2528 				&& (net->dev_addr [0] & 0x02) == 0)
2529 			strcpy (net->name, "eth%d");
2530 	} else if (!info->in || info->out)
2531 		status = get_endpoints (dev, udev->actconfig->interface + ifnum);
2532 	else {
2533 		dev->in = usb_rcvbulkpipe (udev, info->in);
2534 		dev->out = usb_sndbulkpipe (udev, info->out);
2535 	}
2536 
2537 	dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
2538 
2539 	register_netdev (&dev->net);
2540 	devinfo (dev, "register usbnet usb-%s-%s, %s",
2541 		udev->bus->bus_name, udev->devpath,
2542 		dev->driver_info->description);
2543 
2544 	// ok, it's ready to go.
2545 	mutex_lock (&usbnet_mutex);
2546 	list_add (&dev->dev_list, &usbnet_list);
2547 	mutex_unlock (&dev->mutex);
2548 
2549 	// start as if the link is up
2550 	netif_device_attach (&dev->net);
2551 
2552 	mutex_unlock (&usbnet_mutex);
2553 	return dev;
2554 }
2555 
2556 
2557 /*-------------------------------------------------------------------------*/
2558 
2559 /*
2560  * chip vendor names won't normally be on the cables, and
2561  * may not be on the device.
2562  */
2563 
2564 static const struct usb_device_id	products [] = {
2565 
2566 #ifdef	CONFIG_USB_AN2720
2567 {
2568 	USB_DEVICE (0x0547, 0x2720),	// AnchorChips defaults
2569 	.driver_info =	(unsigned long) &an2720_info,
2570 }, {
2571 	USB_DEVICE (0x0547, 0x2727),	// Xircom PGUNET
2572 	.driver_info =	(unsigned long) &an2720_info,
2573 },
2574 #endif
2575 
2576 #ifdef CONFIG_USB_AX8817X
2577 {
2578 	// Linksys USB200M
2579 	USB_DEVICE (0x077b, 0x2226),
2580 	.driver_info =	(unsigned long) &ax8817x_info,
2581 }, {
2582 	// Netgear FA120
2583 	USB_DEVICE (0x0846, 0x1040),
2584 	.driver_info =	(unsigned long) &netgear_fa120_info,
2585 }, {
2586 	// DLink DUB-E100
2587 	USB_DEVICE (0x2001, 0x1a00),
2588 	.driver_info =	(unsigned long) &dlink_dub_e100_info,
2589 }, {
2590 	// Intellinet, ST Lab USB Ethernet
2591 	USB_DEVICE (0x0b95, 0x1720),
2592 	.driver_info =	(unsigned long) &ax8817x_info,
2593 }, {
2594 	// Hawking UF200, TrendNet TU2-ET100
2595 	USB_DEVICE (0x07b8, 0x420a),
2596 	.driver_info =	(unsigned long) &hawking_uf200_info,
2597 }, {
2598 	// ATEN UC210T
2599 	USB_DEVICE (0x0557, 0x2009),
2600 	.driver_info =  (unsigned long) &ax8817x_info,
2601 },
2602 #endif
2603 
2604 #ifdef	CONFIG_USB_BELKIN
2605 {
2606 	USB_DEVICE (0x050d, 0x0004),	// Belkin
2607 	.driver_info =	(unsigned long) &belkin_info,
2608 }, {
2609 	USB_DEVICE (0x056c, 0x8100),	// eTEK
2610 	.driver_info =	(unsigned long) &belkin_info,
2611 }, {
2612 	USB_DEVICE (0x0525, 0x9901),	// Advance USBNET (eTEK)
2613 	.driver_info =	(unsigned long) &belkin_info,
2614 },
2615 #endif
2616 
2617 #ifdef	CONFIG_USB_EPSON2888
2618 {
2619 	USB_DEVICE (0x0525, 0x2888),	// EPSON USB client
2620 	.driver_info	= (unsigned long) &epson2888_info,
2621 },
2622 #endif
2623 
2624 #ifdef	CONFIG_USB_GENESYS
2625 {
2626 	USB_DEVICE (0x05e3, 0x0502),	// GL620USB-A
2627 	.driver_info =	(unsigned long) &genelink_info,
2628 },
2629 	/* NOT: USB_DEVICE (0x05e3, 0x0501),	// GL620USB
2630 	 * that's half duplex, not currently supported
2631 	 */
2632 #endif
2633 
2634 #ifdef	CONFIG_USB_NET1080
2635 {
2636 	USB_DEVICE (0x0525, 0x1080),	// NetChip ref design
2637 	.driver_info =	(unsigned long) &net1080_info,
2638 }, {
2639 	USB_DEVICE (0x06D0, 0x0622),	// Laplink Gold
2640 	.driver_info =	(unsigned long) &net1080_info,
2641 },
2642 #endif
2643 
2644 #ifdef CONFIG_USB_PL2301
2645 {
2646 	USB_DEVICE (0x067b, 0x0000),	// PL-2301
2647 	.driver_info =	(unsigned long) &prolific_info,
2648 }, {
2649 	USB_DEVICE (0x067b, 0x0001),	// PL-2302
2650 	.driver_info =	(unsigned long) &prolific_info,
2651 },
2652 #endif
2653 
2654 #ifdef	CONFIG_USB_ARMLINUX
2655 /*
2656  * SA-1100 using standard ARM Linux kernels, or compatible.
2657  * Often used when talking to Linux PDAs (iPaq, Yopy, etc).
2658  * The sa-1100 "usb-eth" driver handles the basic framing.
2659  * ARMv4.
2660  *
2661  * PXA2xx using usb "gadget" driver, or older "usb-eth" much like
2662  * the sa1100 one. (But PXA hardware uses different endpoints.)
2663  * ARMv5TE.
2664  */
2665 {
2666 	// 1183 = 0x049F, both used as hex values?
2667 	// Compaq "Itsy" vendor/product id
2668 	// version numbers vary, along with endpoint usage
2669 	// but otherwise they're protocol-compatible
2670 	USB_DEVICE (0x049F, 0x505A),
2671 	.driver_info =	(unsigned long) &linuxdev_info,
2672 }, {
2673 	USB_DEVICE (0x0E7E, 0x1001),	// G.Mate "Yopy"
2674 	.driver_info =	(unsigned long) &yopy_info,
2675 }, {
2676 	USB_DEVICE (0x8086, 0x07d3),	// "blob" bootloader
2677 	.driver_info =	(unsigned long) &blob_info,
2678 },
2679 #endif
2680 
2681 #ifdef	CONFIG_USB_ZAURUS
2682 /*
2683  * SA-1100 based Sharp Zaurus ("collie"), or compatible.
2684  * Same idea as above, but different framing.
2685  */
2686 {
2687 	.match_flags	=   USB_DEVICE_ID_MATCH_INT_INFO
2688 			  | USB_DEVICE_ID_MATCH_DEVICE,
2689 	.idVendor		= 0x04DD,
2690 	.idProduct		= 0x8004,
2691 	.bInterfaceClass	= 0x0a,
2692 	.bInterfaceSubClass	= 0x00,
2693 	.bInterfaceProtocol	= 0x00,
2694 	.driver_info =  (unsigned long) &zaurus_sl5x00_info,
2695 }, {
2696 	.match_flags	=   USB_DEVICE_ID_MATCH_INT_INFO
2697 			  | USB_DEVICE_ID_MATCH_DEVICE,
2698 	.idVendor		= 0x04DD,
2699 	.idProduct		= 0x8005, /* A-300 */
2700 	.bInterfaceClass	= 0x02,
2701 	.bInterfaceSubClass	= 0x0a,
2702 	.bInterfaceProtocol	= 0x00,
2703 	.driver_info =  (unsigned long) &zaurus_pxa_info,
2704 }, {
2705 	.match_flags	=   USB_DEVICE_ID_MATCH_INT_INFO
2706 			  | USB_DEVICE_ID_MATCH_DEVICE,
2707 	.idVendor		= 0x04DD,
2708 	.idProduct		= 0x8006, /* B-500/SL-5600 */
2709 	.bInterfaceClass	= 0x02,
2710 	.bInterfaceSubClass	= 0x0a,
2711 	.bInterfaceProtocol	= 0x00,
2712 	.driver_info =  (unsigned long) &zaurus_pxa_info,
2713 }, {
2714 	.match_flags	=   USB_DEVICE_ID_MATCH_INT_INFO
2715 			  | USB_DEVICE_ID_MATCH_DEVICE,
2716 	.idVendor		= 0x04DD,
2717 	.idProduct		= 0x8007, /* C-700 */
2718 	.bInterfaceClass	= 0x02,
2719 	.bInterfaceSubClass	= 0x0a,
2720 	.bInterfaceProtocol	= 0x00,
2721 	.driver_info =  (unsigned long) &zaurus_pxa_info,
2722 }, {
2723 	.match_flags	=   USB_DEVICE_ID_MATCH_INT_INFO
2724 			  | USB_DEVICE_ID_MATCH_DEVICE,
2725 	.idVendor		= 0x04DD,
2726 	.idProduct		= 0x9031, /* C-750 C-760 */
2727 	.bInterfaceClass	= 0x02,
2728 	.bInterfaceSubClass	= 0x0a,
2729 	.bInterfaceProtocol	= 0x00,
2730 	.driver_info =	(unsigned long) &zaurus_pxa_info,
2731 }, {
2732 	.match_flags	=   USB_DEVICE_ID_MATCH_INT_INFO
2733 			  | USB_DEVICE_ID_MATCH_DEVICE,
2734 	.idVendor		= 0x04DD,
2735 	.idProduct		= 0x9032, /* SL-6000 */
2736 	.bInterfaceClass	= 0x02,
2737 	.bInterfaceSubClass	= 0x0a,
2738 	.bInterfaceProtocol	= 0x00,
2739 	.driver_info =	(unsigned long) &zaurus_pxa_info,
2740 },
2741 #endif
2742 
2743 	{ },		// END
2744 };
2745 MODULE_DEVICE_TABLE (usb, products);
2746 
2747 static struct usb_driver usbnet_driver = {
2748 	.name =		driver_name,
2749 	.id_table =	products,
2750 	.probe =	usbnet_probe,
2751 	.disconnect =	usbnet_disconnect,
2752 };
2753 
2754 /* Default ethtool_ops assigned.  Devices can override in their bind() routine */
2755 static struct ethtool_ops usbnet_ethtool_ops = {
2756 	.get_drvinfo		= usbnet_get_drvinfo,
2757 	.get_link		= usbnet_get_link,
2758 	.get_msglevel		= usbnet_get_msglevel,
2759 	.set_msglevel		= usbnet_set_msglevel,
2760 };
2761 /*-------------------------------------------------------------------------*/
2762 
usbnet_init(void)2763 static int __init usbnet_init (void)
2764 {
2765 	// compiler should optimize this out
2766 	if (sizeof (((struct sk_buff *)0)->cb) < sizeof (struct skb_data))
2767 		BUG ();
2768 
2769 	get_random_bytes (node_id, sizeof node_id);
2770 	node_id [0] &= 0xfe;	// clear multicast bit
2771 	node_id [0] |= 0x02;    // set local assignment bit (IEEE802)
2772 
2773  	if (usb_register (&usbnet_driver) < 0)
2774  		return -1;
2775 
2776 	return 0;
2777 }
2778 module_init (usbnet_init);
2779 
usbnet_exit(void)2780 static void __exit usbnet_exit (void)
2781 {
2782  	usb_deregister (&usbnet_driver);
2783 }
2784 module_exit (usbnet_exit);
2785 
2786 EXPORT_NO_SYMBOLS;
2787 MODULE_AUTHOR ("David Brownell <dbrownell@users.sourceforge.net>");
2788 MODULE_DESCRIPTION ("USB Host-to-Host Link Drivers (numerous vendors)");
2789 MODULE_LICENSE ("GPL");
2790