xref: /openbmc/linux/drivers/net/usb/kaweth.c (revision b04b4f78)
1 /****************************************************************
2  *
3  *     kaweth.c - driver for KL5KUSB101 based USB->Ethernet
4  *
5  *     (c) 2000 Interlan Communications
6  *     (c) 2000 Stephane Alnet
7  *     (C) 2001 Brad Hards
8  *     (C) 2002 Oliver Neukum
9  *
10  *     Original author: The Zapman <zapman@interlan.net>
11  *     Inspired by, and much credit goes to Michael Rothwell
12  *     <rothwell@interlan.net> for the test equipment, help, and patience
13  *     Based off of (and with thanks to) Petko Manolov's pegaus.c driver.
14  *     Also many thanks to Joel Silverman and Ed Surprenant at Kawasaki
15  *     for providing the firmware and driver resources.
16  *
17  *     This program is free software; you can redistribute it and/or
18  *     modify it under the terms of the GNU General Public License as
19  *     published by the Free Software Foundation; either version 2, or
20  *     (at your option) any later version.
21  *
22  *     This program is distributed in the hope that it will be useful,
23  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
24  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  *     GNU General Public License for more details.
26  *
27  *     You should have received a copy of the GNU General Public License
28  *     along with this program; if not, write to the Free Software Foundation,
29  *     Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30  *
31  ****************************************************************/
32 
33 /* TODO:
34  * Fix in_interrupt() problem
35  * Develop test procedures for USB net interfaces
36  * Run test procedures
37  * Fix bugs from previous two steps
38  * Snoop other OSs for any tricks we're not doing
39  * Reduce arbitrary timeouts
40  * Smart multicast support
41  * Temporary MAC change support
42  * Tunable SOFs parameter - ioctl()?
43  * Ethernet stats collection
44  * Code formatting improvements
45  */
46 
47 #include <linux/module.h>
48 #include <linux/slab.h>
49 #include <linux/string.h>
50 #include <linux/init.h>
51 #include <linux/delay.h>
52 #include <linux/netdevice.h>
53 #include <linux/etherdevice.h>
54 #include <linux/usb.h>
55 #include <linux/types.h>
56 #include <linux/ethtool.h>
57 #include <linux/dma-mapping.h>
58 #include <linux/wait.h>
59 #include <linux/firmware.h>
60 #include <asm/uaccess.h>
61 #include <asm/byteorder.h>
62 
63 #undef DEBUG
64 
65 #define KAWETH_MTU			1514
66 #define KAWETH_BUF_SIZE			1664
67 #define KAWETH_TX_TIMEOUT		(5 * HZ)
68 #define KAWETH_SCRATCH_SIZE		32
69 #define KAWETH_FIRMWARE_BUF_SIZE	4096
70 #define KAWETH_CONTROL_TIMEOUT		(30000)
71 
72 #define KAWETH_STATUS_BROKEN		0x0000001
73 #define KAWETH_STATUS_CLOSING		0x0000002
74 #define KAWETH_STATUS_SUSPENDING	0x0000004
75 
76 #define KAWETH_STATUS_BLOCKED (KAWETH_STATUS_CLOSING | KAWETH_STATUS_SUSPENDING)
77 
78 #define KAWETH_PACKET_FILTER_PROMISCUOUS	0x01
79 #define KAWETH_PACKET_FILTER_ALL_MULTICAST	0x02
80 #define KAWETH_PACKET_FILTER_DIRECTED		0x04
81 #define KAWETH_PACKET_FILTER_BROADCAST		0x08
82 #define KAWETH_PACKET_FILTER_MULTICAST		0x10
83 
84 /* Table 7 */
85 #define KAWETH_COMMAND_GET_ETHERNET_DESC	0x00
86 #define KAWETH_COMMAND_MULTICAST_FILTERS        0x01
87 #define KAWETH_COMMAND_SET_PACKET_FILTER	0x02
88 #define KAWETH_COMMAND_STATISTICS               0x03
89 #define KAWETH_COMMAND_SET_TEMP_MAC     	0x06
90 #define KAWETH_COMMAND_GET_TEMP_MAC             0x07
91 #define KAWETH_COMMAND_SET_URB_SIZE		0x08
92 #define KAWETH_COMMAND_SET_SOFS_WAIT		0x09
93 #define KAWETH_COMMAND_SCAN			0xFF
94 
95 #define KAWETH_SOFS_TO_WAIT			0x05
96 
97 #define INTBUFFERSIZE				4
98 
99 #define STATE_OFFSET				0
100 #define STATE_MASK				0x40
101 #define	STATE_SHIFT				5
102 
103 #define IS_BLOCKED(s) (s & KAWETH_STATUS_BLOCKED)
104 
105 
106 MODULE_AUTHOR("Michael Zappe <zapman@interlan.net>, Stephane Alnet <stephane@u-picardie.fr>, Brad Hards <bhards@bigpond.net.au> and Oliver Neukum <oliver@neukum.org>");
107 MODULE_DESCRIPTION("KL5USB101 USB Ethernet driver");
108 MODULE_LICENSE("GPL");
109 MODULE_FIRMWARE("kaweth/new_code.bin");
110 MODULE_FIRMWARE("kaweth/new_code_fix.bin");
111 MODULE_FIRMWARE("kaweth/trigger_code.bin");
112 MODULE_FIRMWARE("kaweth/trigger_code_fix.bin");
113 
114 static const char driver_name[] = "kaweth";
115 
116 static int kaweth_probe(
117 		struct usb_interface *intf,
118 		const struct usb_device_id *id	/* from id_table */
119 	);
120 static void kaweth_disconnect(struct usb_interface *intf);
121 static int kaweth_internal_control_msg(struct usb_device *usb_dev,
122 				       unsigned int pipe,
123 				       struct usb_ctrlrequest *cmd, void *data,
124 				       int len, int timeout);
125 static int kaweth_suspend(struct usb_interface *intf, pm_message_t message);
126 static int kaweth_resume(struct usb_interface *intf);
127 
128 /****************************************************************
129  *     usb_device_id
130  ****************************************************************/
131 static struct usb_device_id usb_klsi_table[] = {
132 	{ USB_DEVICE(0x03e8, 0x0008) }, /* AOX Endpoints USB Ethernet */
133 	{ USB_DEVICE(0x04bb, 0x0901) }, /* I-O DATA USB-ET/T */
134 	{ USB_DEVICE(0x0506, 0x03e8) }, /* 3Com 3C19250 */
135 	{ USB_DEVICE(0x0506, 0x11f8) }, /* 3Com 3C460 */
136 	{ USB_DEVICE(0x0557, 0x2002) }, /* ATEN USB Ethernet */
137 	{ USB_DEVICE(0x0557, 0x4000) }, /* D-Link DSB-650C */
138 	{ USB_DEVICE(0x0565, 0x0002) }, /* Peracom Enet */
139 	{ USB_DEVICE(0x0565, 0x0003) }, /* Optus@Home UEP1045A */
140 	{ USB_DEVICE(0x0565, 0x0005) }, /* Peracom Enet2 */
141 	{ USB_DEVICE(0x05e9, 0x0008) }, /* KLSI KL5KUSB101B */
142 	{ USB_DEVICE(0x05e9, 0x0009) }, /* KLSI KL5KUSB101B (Board change) */
143 	{ USB_DEVICE(0x066b, 0x2202) }, /* Linksys USB10T */
144 	{ USB_DEVICE(0x06e1, 0x0008) }, /* ADS USB-10BT */
145 	{ USB_DEVICE(0x06e1, 0x0009) }, /* ADS USB-10BT */
146 	{ USB_DEVICE(0x0707, 0x0100) }, /* SMC 2202USB */
147 	{ USB_DEVICE(0x07aa, 0x0001) }, /* Correga K.K. */
148 	{ USB_DEVICE(0x07b8, 0x4000) }, /* D-Link DU-E10 */
149 	{ USB_DEVICE(0x0846, 0x1001) }, /* NetGear EA-101 */
150 	{ USB_DEVICE(0x0846, 0x1002) }, /* NetGear EA-101 */
151 	{ USB_DEVICE(0x085a, 0x0008) }, /* PortGear Ethernet Adapter */
152 	{ USB_DEVICE(0x085a, 0x0009) }, /* PortGear Ethernet Adapter */
153 	{ USB_DEVICE(0x087d, 0x5704) }, /* Jaton USB Ethernet Device Adapter */
154 	{ USB_DEVICE(0x0951, 0x0008) }, /* Kingston Technology USB Ethernet Adapter */
155 	{ USB_DEVICE(0x095a, 0x3003) }, /* Portsmith Express Ethernet Adapter */
156 	{ USB_DEVICE(0x10bd, 0x1427) }, /* ASANTE USB To Ethernet Adapter */
157 	{ USB_DEVICE(0x1342, 0x0204) }, /* Mobility USB-Ethernet Adapter */
158 	{ USB_DEVICE(0x13d2, 0x0400) }, /* Shark Pocket Adapter */
159 	{ USB_DEVICE(0x1485, 0x0001) },	/* Silicom U2E */
160 	{ USB_DEVICE(0x1485, 0x0002) }, /* Psion Dacom Gold Port Ethernet */
161 	{ USB_DEVICE(0x1645, 0x0005) }, /* Entrega E45 */
162 	{ USB_DEVICE(0x1645, 0x0008) }, /* Entrega USB Ethernet Adapter */
163 	{ USB_DEVICE(0x1645, 0x8005) }, /* PortGear Ethernet Adapter */
164 	{ USB_DEVICE(0x1668, 0x0323) }, /* Actiontec USB Ethernet */
165 	{ USB_DEVICE(0x2001, 0x4000) }, /* D-link DSB-650C */
166 	{} /* Null terminator */
167 };
168 
169 MODULE_DEVICE_TABLE (usb, usb_klsi_table);
170 
171 /****************************************************************
172  *     kaweth_driver
173  ****************************************************************/
174 static struct usb_driver kaweth_driver = {
175 	.name =		driver_name,
176 	.probe =	kaweth_probe,
177 	.disconnect =	kaweth_disconnect,
178 	.suspend =	kaweth_suspend,
179 	.resume =	kaweth_resume,
180 	.id_table =     usb_klsi_table,
181 	.supports_autosuspend =	1,
182 };
183 
184 typedef __u8 eth_addr_t[6];
185 
186 /****************************************************************
187  *     usb_eth_dev
188  ****************************************************************/
189 struct usb_eth_dev {
190 	char *name;
191 	__u16 vendor;
192 	__u16 device;
193 	void *pdata;
194 };
195 
196 /****************************************************************
197  *     kaweth_ethernet_configuration
198  *     Refer Table 8
199  ****************************************************************/
200 struct kaweth_ethernet_configuration
201 {
202 	__u8 size;
203 	__u8 reserved1;
204 	__u8 reserved2;
205 	eth_addr_t hw_addr;
206 	__u32 statistics_mask;
207 	__le16 segment_size;
208 	__u16 max_multicast_filters;
209 	__u8 reserved3;
210 } __attribute__ ((packed));
211 
212 /****************************************************************
213  *     kaweth_device
214  ****************************************************************/
215 struct kaweth_device
216 {
217 	spinlock_t device_lock;
218 
219 	__u32 status;
220 	int end;
221 	int suspend_lowmem_rx;
222 	int suspend_lowmem_ctrl;
223 	int linkstate;
224 	int opened;
225 	struct delayed_work lowmem_work;
226 
227 	struct usb_device *dev;
228 	struct usb_interface *intf;
229 	struct net_device *net;
230 	wait_queue_head_t term_wait;
231 
232 	struct urb *rx_urb;
233 	struct urb *tx_urb;
234 	struct urb *irq_urb;
235 
236 	dma_addr_t intbufferhandle;
237 	__u8 *intbuffer;
238 	dma_addr_t rxbufferhandle;
239 	__u8 *rx_buf;
240 
241 
242 	struct sk_buff *tx_skb;
243 
244 	__u8 *firmware_buf;
245 	__u8 scratch[KAWETH_SCRATCH_SIZE];
246 	__u16 packet_filter_bitmap;
247 
248 	struct kaweth_ethernet_configuration configuration;
249 
250 	struct net_device_stats stats;
251 };
252 
253 /****************************************************************
254  *     kaweth_control
255  ****************************************************************/
256 static int kaweth_control(struct kaweth_device *kaweth,
257 			  unsigned int pipe,
258 			  __u8 request,
259 			  __u8 requesttype,
260 			  __u16 value,
261 			  __u16 index,
262 			  void *data,
263 			  __u16 size,
264 			  int timeout)
265 {
266 	struct usb_ctrlrequest *dr;
267 
268 	dbg("kaweth_control()");
269 
270 	if(in_interrupt()) {
271 		dbg("in_interrupt()");
272 		return -EBUSY;
273 	}
274 
275 	dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
276 
277 	if (!dr) {
278 		dbg("kmalloc() failed");
279 		return -ENOMEM;
280 	}
281 
282 	dr->bRequestType= requesttype;
283 	dr->bRequest = request;
284 	dr->wValue = cpu_to_le16(value);
285 	dr->wIndex = cpu_to_le16(index);
286 	dr->wLength = cpu_to_le16(size);
287 
288 	return kaweth_internal_control_msg(kaweth->dev,
289 					pipe,
290 					dr,
291 					data,
292 					size,
293 					timeout);
294 }
295 
296 /****************************************************************
297  *     kaweth_read_configuration
298  ****************************************************************/
299 static int kaweth_read_configuration(struct kaweth_device *kaweth)
300 {
301 	int retval;
302 
303 	dbg("Reading kaweth configuration");
304 
305 	retval = kaweth_control(kaweth,
306 				usb_rcvctrlpipe(kaweth->dev, 0),
307 				KAWETH_COMMAND_GET_ETHERNET_DESC,
308 				USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_DEVICE,
309 				0,
310 				0,
311 				(void *)&kaweth->configuration,
312 				sizeof(kaweth->configuration),
313 				KAWETH_CONTROL_TIMEOUT);
314 
315 	return retval;
316 }
317 
318 /****************************************************************
319  *     kaweth_set_urb_size
320  ****************************************************************/
321 static int kaweth_set_urb_size(struct kaweth_device *kaweth, __u16 urb_size)
322 {
323 	int retval;
324 
325 	dbg("Setting URB size to %d", (unsigned)urb_size);
326 
327 	retval = kaweth_control(kaweth,
328 				usb_sndctrlpipe(kaweth->dev, 0),
329 				KAWETH_COMMAND_SET_URB_SIZE,
330 				USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
331 				urb_size,
332 				0,
333 				(void *)&kaweth->scratch,
334 				0,
335 				KAWETH_CONTROL_TIMEOUT);
336 
337 	return retval;
338 }
339 
340 /****************************************************************
341  *     kaweth_set_sofs_wait
342  ****************************************************************/
343 static int kaweth_set_sofs_wait(struct kaweth_device *kaweth, __u16 sofs_wait)
344 {
345 	int retval;
346 
347 	dbg("Set SOFS wait to %d", (unsigned)sofs_wait);
348 
349 	retval = kaweth_control(kaweth,
350 				usb_sndctrlpipe(kaweth->dev, 0),
351 				KAWETH_COMMAND_SET_SOFS_WAIT,
352 				USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
353 				sofs_wait,
354 				0,
355 				(void *)&kaweth->scratch,
356 				0,
357 				KAWETH_CONTROL_TIMEOUT);
358 
359 	return retval;
360 }
361 
362 /****************************************************************
363  *     kaweth_set_receive_filter
364  ****************************************************************/
365 static int kaweth_set_receive_filter(struct kaweth_device *kaweth,
366 				     __u16 receive_filter)
367 {
368 	int retval;
369 
370 	dbg("Set receive filter to %d", (unsigned)receive_filter);
371 
372 	retval = kaweth_control(kaweth,
373 				usb_sndctrlpipe(kaweth->dev, 0),
374 				KAWETH_COMMAND_SET_PACKET_FILTER,
375 				USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
376 				receive_filter,
377 				0,
378 				(void *)&kaweth->scratch,
379 				0,
380 				KAWETH_CONTROL_TIMEOUT);
381 
382 	return retval;
383 }
384 
385 /****************************************************************
386  *     kaweth_download_firmware
387  ****************************************************************/
388 static int kaweth_download_firmware(struct kaweth_device *kaweth,
389 				    const char *fwname,
390 				    __u8 interrupt,
391 				    __u8 type)
392 {
393 	const struct firmware *fw;
394 	int data_len;
395 	int ret;
396 
397 	ret = request_firmware(&fw, fwname, &kaweth->dev->dev);
398 	if (ret) {
399 		err("Firmware request failed\n");
400 		return ret;
401 	}
402 
403 	if (fw->size > KAWETH_FIRMWARE_BUF_SIZE) {
404 		err("Firmware too big: %zu", fw->size);
405 		return -ENOSPC;
406 	}
407 	data_len = fw->size;
408 	memcpy(kaweth->firmware_buf, fw->data, fw->size);
409 
410 	release_firmware(fw);
411 
412 	kaweth->firmware_buf[2] = (data_len & 0xFF) - 7;
413 	kaweth->firmware_buf[3] = data_len >> 8;
414 	kaweth->firmware_buf[4] = type;
415 	kaweth->firmware_buf[5] = interrupt;
416 
417 	dbg("High: %i, Low:%i", kaweth->firmware_buf[3],
418 		   kaweth->firmware_buf[2]);
419 
420 	dbg("Downloading firmware at %p to kaweth device at %p",
421 	    fw->data, kaweth);
422 	dbg("Firmware length: %d", data_len);
423 
424 	return kaweth_control(kaweth,
425 		              usb_sndctrlpipe(kaweth->dev, 0),
426 			      KAWETH_COMMAND_SCAN,
427 			      USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
428 			      0,
429 			      0,
430 			      (void *)kaweth->firmware_buf,
431 			      data_len,
432 			      KAWETH_CONTROL_TIMEOUT);
433 }
434 
435 /****************************************************************
436  *     kaweth_trigger_firmware
437  ****************************************************************/
438 static int kaweth_trigger_firmware(struct kaweth_device *kaweth,
439 				   __u8 interrupt)
440 {
441 	kaweth->firmware_buf[0] = 0xB6;
442 	kaweth->firmware_buf[1] = 0xC3;
443 	kaweth->firmware_buf[2] = 0x01;
444 	kaweth->firmware_buf[3] = 0x00;
445 	kaweth->firmware_buf[4] = 0x06;
446 	kaweth->firmware_buf[5] = interrupt;
447 	kaweth->firmware_buf[6] = 0x00;
448 	kaweth->firmware_buf[7] = 0x00;
449 
450 	dbg("Triggering firmware");
451 
452 	return kaweth_control(kaweth,
453 			      usb_sndctrlpipe(kaweth->dev, 0),
454 			      KAWETH_COMMAND_SCAN,
455 			      USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
456 			      0,
457 			      0,
458 			      (void *)kaweth->firmware_buf,
459 			      8,
460 			      KAWETH_CONTROL_TIMEOUT);
461 }
462 
463 /****************************************************************
464  *     kaweth_reset
465  ****************************************************************/
466 static int kaweth_reset(struct kaweth_device *kaweth)
467 {
468 	int result;
469 
470 	dbg("kaweth_reset(%p)", kaweth);
471 	result = kaweth_control(kaweth,
472 				usb_sndctrlpipe(kaweth->dev, 0),
473 				USB_REQ_SET_CONFIGURATION,
474 				0,
475 				kaweth->dev->config[0].desc.bConfigurationValue,
476 				0,
477 				NULL,
478 				0,
479 				KAWETH_CONTROL_TIMEOUT);
480 
481 	mdelay(10);
482 
483 	dbg("kaweth_reset() returns %d.",result);
484 
485 	return result;
486 }
487 
488 static void kaweth_usb_receive(struct urb *);
489 static int kaweth_resubmit_rx_urb(struct kaweth_device *, gfp_t);
490 
491 /****************************************************************
492 	int_callback
493 *****************************************************************/
494 
495 static void kaweth_resubmit_int_urb(struct kaweth_device *kaweth, gfp_t mf)
496 {
497 	int status;
498 
499 	status = usb_submit_urb (kaweth->irq_urb, mf);
500 	if (unlikely(status == -ENOMEM)) {
501 		kaweth->suspend_lowmem_ctrl = 1;
502 		schedule_delayed_work(&kaweth->lowmem_work, HZ/4);
503 	} else {
504 		kaweth->suspend_lowmem_ctrl = 0;
505 	}
506 
507 	if (status)
508 		err ("can't resubmit intr, %s-%s, status %d",
509 				kaweth->dev->bus->bus_name,
510 				kaweth->dev->devpath, status);
511 }
512 
513 static void int_callback(struct urb *u)
514 {
515 	struct kaweth_device *kaweth = u->context;
516 	int act_state;
517 	int status = u->status;
518 
519 	switch (status) {
520 	case 0:			/* success */
521 		break;
522 	case -ECONNRESET:	/* unlink */
523 	case -ENOENT:
524 	case -ESHUTDOWN:
525 		return;
526 	/* -EPIPE:  should clear the halt */
527 	default:		/* error */
528 		goto resubmit;
529 	}
530 
531 	/* we check the link state to report changes */
532 	if (kaweth->linkstate != (act_state = ( kaweth->intbuffer[STATE_OFFSET] | STATE_MASK) >> STATE_SHIFT)) {
533 		if (act_state)
534 			netif_carrier_on(kaweth->net);
535 		else
536 			netif_carrier_off(kaweth->net);
537 
538 		kaweth->linkstate = act_state;
539 	}
540 resubmit:
541 	kaweth_resubmit_int_urb(kaweth, GFP_ATOMIC);
542 }
543 
544 static void kaweth_resubmit_tl(struct work_struct *work)
545 {
546 	struct kaweth_device *kaweth =
547 		container_of(work, struct kaweth_device, lowmem_work.work);
548 
549 	if (IS_BLOCKED(kaweth->status))
550 		return;
551 
552 	if (kaweth->suspend_lowmem_rx)
553 		kaweth_resubmit_rx_urb(kaweth, GFP_NOIO);
554 
555 	if (kaweth->suspend_lowmem_ctrl)
556 		kaweth_resubmit_int_urb(kaweth, GFP_NOIO);
557 }
558 
559 
560 /****************************************************************
561  *     kaweth_resubmit_rx_urb
562  ****************************************************************/
563 static int kaweth_resubmit_rx_urb(struct kaweth_device *kaweth,
564 						gfp_t mem_flags)
565 {
566 	int result;
567 
568 	usb_fill_bulk_urb(kaweth->rx_urb,
569 		      kaweth->dev,
570 		      usb_rcvbulkpipe(kaweth->dev, 1),
571 		      kaweth->rx_buf,
572 		      KAWETH_BUF_SIZE,
573 		      kaweth_usb_receive,
574 		      kaweth);
575 	kaweth->rx_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
576 	kaweth->rx_urb->transfer_dma = kaweth->rxbufferhandle;
577 
578 	if((result = usb_submit_urb(kaweth->rx_urb, mem_flags))) {
579 		if (result == -ENOMEM) {
580 			kaweth->suspend_lowmem_rx = 1;
581 			schedule_delayed_work(&kaweth->lowmem_work, HZ/4);
582 		}
583 		err("resubmitting rx_urb %d failed", result);
584 	} else {
585 		kaweth->suspend_lowmem_rx = 0;
586 	}
587 
588 	return result;
589 }
590 
591 static void kaweth_async_set_rx_mode(struct kaweth_device *kaweth);
592 
593 /****************************************************************
594  *     kaweth_usb_receive
595  ****************************************************************/
596 static void kaweth_usb_receive(struct urb *urb)
597 {
598 	struct kaweth_device *kaweth = urb->context;
599 	struct net_device *net = kaweth->net;
600 	int status = urb->status;
601 
602 	int count = urb->actual_length;
603 	int count2 = urb->transfer_buffer_length;
604 
605 	__u16 pkt_len = le16_to_cpup((__le16 *)kaweth->rx_buf);
606 
607 	struct sk_buff *skb;
608 
609 	if(unlikely(status == -ECONNRESET || status == -ESHUTDOWN))
610 	/* we are killed - set a flag and wake the disconnect handler */
611 	{
612 		kaweth->end = 1;
613 		wake_up(&kaweth->term_wait);
614 		return;
615 	}
616 
617 	spin_lock(&kaweth->device_lock);
618 	if (IS_BLOCKED(kaweth->status)) {
619 		spin_unlock(&kaweth->device_lock);
620 		return;
621 	}
622 	spin_unlock(&kaweth->device_lock);
623 
624 	if(status && status != -EREMOTEIO && count != 1) {
625 		err("%s RX status: %d count: %d packet_len: %d",
626                            net->name,
627 			   status,
628 			   count,
629 			   (int)pkt_len);
630 		kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
631                 return;
632 	}
633 
634 	if(kaweth->net && (count > 2)) {
635 		if(pkt_len > (count - 2)) {
636 			err("Packet length too long for USB frame (pkt_len: %x, count: %x)",pkt_len, count);
637 			err("Packet len & 2047: %x", pkt_len & 2047);
638 			err("Count 2: %x", count2);
639 		        kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
640                         return;
641                 }
642 
643 		if(!(skb = dev_alloc_skb(pkt_len+2))) {
644 		        kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
645                         return;
646 		}
647 
648 		skb_reserve(skb, 2);    /* Align IP on 16 byte boundaries */
649 
650 		skb_copy_to_linear_data(skb, kaweth->rx_buf + 2, pkt_len);
651 
652 		skb_put(skb, pkt_len);
653 
654 		skb->protocol = eth_type_trans(skb, net);
655 
656 		netif_rx(skb);
657 
658 		kaweth->stats.rx_packets++;
659 		kaweth->stats.rx_bytes += pkt_len;
660 	}
661 
662 	kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
663 }
664 
665 /****************************************************************
666  *     kaweth_open
667  ****************************************************************/
668 static int kaweth_open(struct net_device *net)
669 {
670 	struct kaweth_device *kaweth = netdev_priv(net);
671 	int res;
672 
673 	dbg("Opening network device.");
674 
675 	res = usb_autopm_get_interface(kaweth->intf);
676 	if (res) {
677 		err("Interface cannot be resumed.");
678 		return -EIO;
679 	}
680 	res = kaweth_resubmit_rx_urb(kaweth, GFP_KERNEL);
681 	if (res)
682 		goto err_out;
683 
684 	usb_fill_int_urb(
685 		kaweth->irq_urb,
686 		kaweth->dev,
687 		usb_rcvintpipe(kaweth->dev, 3),
688 		kaweth->intbuffer,
689 		INTBUFFERSIZE,
690 		int_callback,
691 		kaweth,
692 		250); /* overriding the descriptor */
693 	kaweth->irq_urb->transfer_dma = kaweth->intbufferhandle;
694 	kaweth->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
695 
696 	res = usb_submit_urb(kaweth->irq_urb, GFP_KERNEL);
697 	if (res) {
698 		usb_kill_urb(kaweth->rx_urb);
699 		goto err_out;
700 	}
701 	kaweth->opened = 1;
702 
703 	netif_start_queue(net);
704 
705 	kaweth_async_set_rx_mode(kaweth);
706 	return 0;
707 
708 err_out:
709 	usb_autopm_enable(kaweth->intf);
710 	return -EIO;
711 }
712 
713 /****************************************************************
714  *     kaweth_kill_urbs
715  ****************************************************************/
716 static void kaweth_kill_urbs(struct kaweth_device *kaweth)
717 {
718 	usb_kill_urb(kaweth->irq_urb);
719 	usb_kill_urb(kaweth->rx_urb);
720 	usb_kill_urb(kaweth->tx_urb);
721 
722 	cancel_delayed_work_sync(&kaweth->lowmem_work);
723 
724 	/* a scheduled work may have resubmitted,
725 	   we hit them again */
726 	usb_kill_urb(kaweth->irq_urb);
727 	usb_kill_urb(kaweth->rx_urb);
728 }
729 
730 /****************************************************************
731  *     kaweth_close
732  ****************************************************************/
733 static int kaweth_close(struct net_device *net)
734 {
735 	struct kaweth_device *kaweth = netdev_priv(net);
736 
737 	netif_stop_queue(net);
738 	kaweth->opened = 0;
739 
740 	kaweth->status |= KAWETH_STATUS_CLOSING;
741 
742 	kaweth_kill_urbs(kaweth);
743 
744 	kaweth->status &= ~KAWETH_STATUS_CLOSING;
745 
746 	usb_autopm_enable(kaweth->intf);
747 
748 	return 0;
749 }
750 
751 static void kaweth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
752 {
753 	struct kaweth_device *kaweth = netdev_priv(dev);
754 
755 	strlcpy(info->driver, driver_name, sizeof(info->driver));
756 	usb_make_path(kaweth->dev, info->bus_info, sizeof (info->bus_info));
757 }
758 
759 static u32 kaweth_get_link(struct net_device *dev)
760 {
761 	struct kaweth_device *kaweth = netdev_priv(dev);
762 
763 	return kaweth->linkstate;
764 }
765 
766 static struct ethtool_ops ops = {
767 	.get_drvinfo	= kaweth_get_drvinfo,
768 	.get_link	= kaweth_get_link
769 };
770 
771 /****************************************************************
772  *     kaweth_usb_transmit_complete
773  ****************************************************************/
774 static void kaweth_usb_transmit_complete(struct urb *urb)
775 {
776 	struct kaweth_device *kaweth = urb->context;
777 	struct sk_buff *skb = kaweth->tx_skb;
778 	int status = urb->status;
779 
780 	if (unlikely(status != 0))
781 		if (status != -ENOENT)
782 			dbg("%s: TX status %d.", kaweth->net->name, status);
783 
784 	netif_wake_queue(kaweth->net);
785 	dev_kfree_skb_irq(skb);
786 }
787 
788 /****************************************************************
789  *     kaweth_start_xmit
790  ****************************************************************/
791 static int kaweth_start_xmit(struct sk_buff *skb, struct net_device *net)
792 {
793 	struct kaweth_device *kaweth = netdev_priv(net);
794 	__le16 *private_header;
795 
796 	int res;
797 
798 	spin_lock_irq(&kaweth->device_lock);
799 
800 	kaweth_async_set_rx_mode(kaweth);
801 	netif_stop_queue(net);
802 	if (IS_BLOCKED(kaweth->status)) {
803 		goto skip;
804 	}
805 
806 	/* We now decide whether we can put our special header into the sk_buff */
807 	if (skb_cloned(skb) || skb_headroom(skb) < 2) {
808 		/* no such luck - we make our own */
809 		struct sk_buff *copied_skb;
810 		copied_skb = skb_copy_expand(skb, 2, 0, GFP_ATOMIC);
811 		dev_kfree_skb_irq(skb);
812 		skb = copied_skb;
813 		if (!copied_skb) {
814 			kaweth->stats.tx_errors++;
815 			netif_start_queue(net);
816 			spin_unlock_irq(&kaweth->device_lock);
817 			return 0;
818 		}
819 	}
820 
821 	private_header = (__le16 *)__skb_push(skb, 2);
822 	*private_header = cpu_to_le16(skb->len-2);
823 	kaweth->tx_skb = skb;
824 
825 	usb_fill_bulk_urb(kaweth->tx_urb,
826 		      kaweth->dev,
827 		      usb_sndbulkpipe(kaweth->dev, 2),
828 		      private_header,
829 		      skb->len,
830 		      kaweth_usb_transmit_complete,
831 		      kaweth);
832 	kaweth->end = 0;
833 
834 	if((res = usb_submit_urb(kaweth->tx_urb, GFP_ATOMIC)))
835 	{
836 		dev_warn(&net->dev, "kaweth failed tx_urb %d\n", res);
837 skip:
838 		kaweth->stats.tx_errors++;
839 
840 		netif_start_queue(net);
841 		dev_kfree_skb_irq(skb);
842 	}
843 	else
844 	{
845 		kaweth->stats.tx_packets++;
846 		kaweth->stats.tx_bytes += skb->len;
847 		net->trans_start = jiffies;
848 	}
849 
850 	spin_unlock_irq(&kaweth->device_lock);
851 
852 	return 0;
853 }
854 
855 /****************************************************************
856  *     kaweth_set_rx_mode
857  ****************************************************************/
858 static void kaweth_set_rx_mode(struct net_device *net)
859 {
860 	struct kaweth_device *kaweth = netdev_priv(net);
861 
862 	__u16 packet_filter_bitmap = KAWETH_PACKET_FILTER_DIRECTED |
863                                      KAWETH_PACKET_FILTER_BROADCAST |
864 		                     KAWETH_PACKET_FILTER_MULTICAST;
865 
866 	dbg("Setting Rx mode to %d", packet_filter_bitmap);
867 
868 	netif_stop_queue(net);
869 
870 	if (net->flags & IFF_PROMISC) {
871 		packet_filter_bitmap |= KAWETH_PACKET_FILTER_PROMISCUOUS;
872 	}
873 	else if ((net->mc_count) || (net->flags & IFF_ALLMULTI)) {
874 		packet_filter_bitmap |= KAWETH_PACKET_FILTER_ALL_MULTICAST;
875 	}
876 
877 	kaweth->packet_filter_bitmap = packet_filter_bitmap;
878 	netif_wake_queue(net);
879 }
880 
881 /****************************************************************
882  *     kaweth_async_set_rx_mode
883  ****************************************************************/
884 static void kaweth_async_set_rx_mode(struct kaweth_device *kaweth)
885 {
886 	__u16 packet_filter_bitmap = kaweth->packet_filter_bitmap;
887 	kaweth->packet_filter_bitmap = 0;
888 	if (packet_filter_bitmap == 0)
889 		return;
890 
891 	{
892 	int result;
893 	result = kaweth_control(kaweth,
894 				usb_sndctrlpipe(kaweth->dev, 0),
895 				KAWETH_COMMAND_SET_PACKET_FILTER,
896 				USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
897 				packet_filter_bitmap,
898 				0,
899 				(void *)&kaweth->scratch,
900 				0,
901 				KAWETH_CONTROL_TIMEOUT);
902 
903 	if(result < 0) {
904 		err("Failed to set Rx mode: %d", result);
905 	}
906 	else {
907 		dbg("Set Rx mode to %d", packet_filter_bitmap);
908 	}
909 	}
910 }
911 
912 /****************************************************************
913  *     kaweth_netdev_stats
914  ****************************************************************/
915 static struct net_device_stats *kaweth_netdev_stats(struct net_device *dev)
916 {
917 	struct kaweth_device *kaweth = netdev_priv(dev);
918 	return &kaweth->stats;
919 }
920 
921 /****************************************************************
922  *     kaweth_tx_timeout
923  ****************************************************************/
924 static void kaweth_tx_timeout(struct net_device *net)
925 {
926 	struct kaweth_device *kaweth = netdev_priv(net);
927 
928 	dev_warn(&net->dev, "%s: Tx timed out. Resetting.\n", net->name);
929 	kaweth->stats.tx_errors++;
930 	net->trans_start = jiffies;
931 
932 	usb_unlink_urb(kaweth->tx_urb);
933 }
934 
935 /****************************************************************
936  *     kaweth_suspend
937  ****************************************************************/
938 static int kaweth_suspend(struct usb_interface *intf, pm_message_t message)
939 {
940 	struct kaweth_device *kaweth = usb_get_intfdata(intf);
941 	unsigned long flags;
942 
943 	dbg("Suspending device");
944 	spin_lock_irqsave(&kaweth->device_lock, flags);
945 	kaweth->status |= KAWETH_STATUS_SUSPENDING;
946 	spin_unlock_irqrestore(&kaweth->device_lock, flags);
947 
948 	kaweth_kill_urbs(kaweth);
949 	return 0;
950 }
951 
952 /****************************************************************
953  *     kaweth_resume
954  ****************************************************************/
955 static int kaweth_resume(struct usb_interface *intf)
956 {
957 	struct kaweth_device *kaweth = usb_get_intfdata(intf);
958 	unsigned long flags;
959 
960 	dbg("Resuming device");
961 	spin_lock_irqsave(&kaweth->device_lock, flags);
962 	kaweth->status &= ~KAWETH_STATUS_SUSPENDING;
963 	spin_unlock_irqrestore(&kaweth->device_lock, flags);
964 
965 	if (!kaweth->opened)
966 		return 0;
967 	kaweth_resubmit_rx_urb(kaweth, GFP_NOIO);
968 	kaweth_resubmit_int_urb(kaweth, GFP_NOIO);
969 
970 	return 0;
971 }
972 
973 /****************************************************************
974  *     kaweth_probe
975  ****************************************************************/
976 
977 
978 static const struct net_device_ops kaweth_netdev_ops = {
979 	.ndo_open =			kaweth_open,
980 	.ndo_stop =			kaweth_close,
981 	.ndo_start_xmit =		kaweth_start_xmit,
982 	.ndo_tx_timeout =		kaweth_tx_timeout,
983 	.ndo_set_multicast_list =	kaweth_set_rx_mode,
984 	.ndo_get_stats =		kaweth_netdev_stats,
985 };
986 
987 static int kaweth_probe(
988 		struct usb_interface *intf,
989 		const struct usb_device_id *id      /* from id_table */
990 	)
991 {
992 	struct usb_device *dev = interface_to_usbdev(intf);
993 	struct kaweth_device *kaweth;
994 	struct net_device *netdev;
995 	const eth_addr_t bcast_addr = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
996 	int result = 0;
997 
998 	dbg("Kawasaki Device Probe (Device number:%d): 0x%4.4x:0x%4.4x:0x%4.4x",
999 		 dev->devnum,
1000 		 le16_to_cpu(dev->descriptor.idVendor),
1001 		 le16_to_cpu(dev->descriptor.idProduct),
1002 		 le16_to_cpu(dev->descriptor.bcdDevice));
1003 
1004 	dbg("Device at %p", dev);
1005 
1006 	dbg("Descriptor length: %x type: %x",
1007 		 (int)dev->descriptor.bLength,
1008 		 (int)dev->descriptor.bDescriptorType);
1009 
1010 	netdev = alloc_etherdev(sizeof(*kaweth));
1011 	if (!netdev)
1012 		return -ENOMEM;
1013 
1014 	kaweth = netdev_priv(netdev);
1015 	kaweth->dev = dev;
1016 	kaweth->net = netdev;
1017 
1018 	spin_lock_init(&kaweth->device_lock);
1019 	init_waitqueue_head(&kaweth->term_wait);
1020 
1021 	dbg("Resetting.");
1022 
1023 	kaweth_reset(kaweth);
1024 
1025 	/*
1026 	 * If high byte of bcdDevice is nonzero, firmware is already
1027 	 * downloaded. Don't try to do it again, or we'll hang the device.
1028 	 */
1029 
1030 	if (le16_to_cpu(dev->descriptor.bcdDevice) >> 8) {
1031 		dev_info(&intf->dev, "Firmware present in device.\n");
1032 	} else {
1033 		/* Download the firmware */
1034 		dev_info(&intf->dev, "Downloading firmware...\n");
1035 		kaweth->firmware_buf = (__u8 *)__get_free_page(GFP_KERNEL);
1036 		if ((result = kaweth_download_firmware(kaweth,
1037 						      "kaweth/new_code.bin",
1038 						      100,
1039 						      2)) < 0) {
1040 			err("Error downloading firmware (%d)", result);
1041 			goto err_fw;
1042 		}
1043 
1044 		if ((result = kaweth_download_firmware(kaweth,
1045 						      "kaweth/new_code_fix.bin",
1046 						      100,
1047 						      3)) < 0) {
1048 			err("Error downloading firmware fix (%d)", result);
1049 			goto err_fw;
1050 		}
1051 
1052 		if ((result = kaweth_download_firmware(kaweth,
1053 						      "kaweth/trigger_code.bin",
1054 						      126,
1055 						      2)) < 0) {
1056 			err("Error downloading trigger code (%d)", result);
1057 			goto err_fw;
1058 
1059 		}
1060 
1061 		if ((result = kaweth_download_firmware(kaweth,
1062 						      "kaweth/trigger_code_fix.bin",
1063 						      126,
1064 						      3)) < 0) {
1065 			err("Error downloading trigger code fix (%d)", result);
1066 			goto err_fw;
1067 		}
1068 
1069 
1070 		if ((result = kaweth_trigger_firmware(kaweth, 126)) < 0) {
1071 			err("Error triggering firmware (%d)", result);
1072 			goto err_fw;
1073 		}
1074 
1075 		/* Device will now disappear for a moment...  */
1076 		dev_info(&intf->dev, "Firmware loaded.  I'll be back...\n");
1077 err_fw:
1078 		free_page((unsigned long)kaweth->firmware_buf);
1079 		free_netdev(netdev);
1080 		return -EIO;
1081 	}
1082 
1083 	result = kaweth_read_configuration(kaweth);
1084 
1085 	if(result < 0) {
1086 		err("Error reading configuration (%d), no net device created", result);
1087 		goto err_free_netdev;
1088 	}
1089 
1090 	dev_info(&intf->dev, "Statistics collection: %x\n", kaweth->configuration.statistics_mask);
1091 	dev_info(&intf->dev, "Multicast filter limit: %x\n", kaweth->configuration.max_multicast_filters & ((1 << 15) - 1));
1092 	dev_info(&intf->dev, "MTU: %d\n", le16_to_cpu(kaweth->configuration.segment_size));
1093 	dev_info(&intf->dev, "Read MAC address %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x\n",
1094 		 (int)kaweth->configuration.hw_addr[0],
1095 		 (int)kaweth->configuration.hw_addr[1],
1096 		 (int)kaweth->configuration.hw_addr[2],
1097 		 (int)kaweth->configuration.hw_addr[3],
1098 		 (int)kaweth->configuration.hw_addr[4],
1099 		 (int)kaweth->configuration.hw_addr[5]);
1100 
1101 	if(!memcmp(&kaweth->configuration.hw_addr,
1102                    &bcast_addr,
1103 		   sizeof(bcast_addr))) {
1104 		err("Firmware not functioning properly, no net device created");
1105 		goto err_free_netdev;
1106 	}
1107 
1108 	if(kaweth_set_urb_size(kaweth, KAWETH_BUF_SIZE) < 0) {
1109 		dbg("Error setting URB size");
1110 		goto err_free_netdev;
1111 	}
1112 
1113 	if(kaweth_set_sofs_wait(kaweth, KAWETH_SOFS_TO_WAIT) < 0) {
1114 		err("Error setting SOFS wait");
1115 		goto err_free_netdev;
1116 	}
1117 
1118 	result = kaweth_set_receive_filter(kaweth,
1119                                            KAWETH_PACKET_FILTER_DIRECTED |
1120                                            KAWETH_PACKET_FILTER_BROADCAST |
1121                                            KAWETH_PACKET_FILTER_MULTICAST);
1122 
1123 	if(result < 0) {
1124 		err("Error setting receive filter");
1125 		goto err_free_netdev;
1126 	}
1127 
1128 	dbg("Initializing net device.");
1129 
1130 	kaweth->intf = intf;
1131 
1132 	kaweth->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
1133 	if (!kaweth->tx_urb)
1134 		goto err_free_netdev;
1135 	kaweth->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
1136 	if (!kaweth->rx_urb)
1137 		goto err_only_tx;
1138 	kaweth->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
1139 	if (!kaweth->irq_urb)
1140 		goto err_tx_and_rx;
1141 
1142 	kaweth->intbuffer = usb_buffer_alloc(	kaweth->dev,
1143 						INTBUFFERSIZE,
1144 						GFP_KERNEL,
1145 						&kaweth->intbufferhandle);
1146 	if (!kaweth->intbuffer)
1147 		goto err_tx_and_rx_and_irq;
1148 	kaweth->rx_buf = usb_buffer_alloc(	kaweth->dev,
1149 						KAWETH_BUF_SIZE,
1150 						GFP_KERNEL,
1151 						&kaweth->rxbufferhandle);
1152 	if (!kaweth->rx_buf)
1153 		goto err_all_but_rxbuf;
1154 
1155 	memcpy(netdev->broadcast, &bcast_addr, sizeof(bcast_addr));
1156 	memcpy(netdev->dev_addr, &kaweth->configuration.hw_addr,
1157                sizeof(kaweth->configuration.hw_addr));
1158 
1159 	netdev->netdev_ops = &kaweth_netdev_ops;
1160 	netdev->watchdog_timeo = KAWETH_TX_TIMEOUT;
1161 	netdev->mtu = le16_to_cpu(kaweth->configuration.segment_size);
1162 	SET_ETHTOOL_OPS(netdev, &ops);
1163 
1164 	/* kaweth is zeroed as part of alloc_netdev */
1165 	INIT_DELAYED_WORK(&kaweth->lowmem_work, kaweth_resubmit_tl);
1166 	usb_set_intfdata(intf, kaweth);
1167 
1168 #if 0
1169 // dma_supported() is deeply broken on almost all architectures
1170 	if (dma_supported (&intf->dev, 0xffffffffffffffffULL))
1171 		kaweth->net->features |= NETIF_F_HIGHDMA;
1172 #endif
1173 
1174 	SET_NETDEV_DEV(netdev, &intf->dev);
1175 	if (register_netdev(netdev) != 0) {
1176 		err("Error registering netdev.");
1177 		goto err_intfdata;
1178 	}
1179 
1180 	dev_info(&intf->dev, "kaweth interface created at %s\n",
1181 		 kaweth->net->name);
1182 
1183 	dbg("Kaweth probe returning.");
1184 
1185 	return 0;
1186 
1187 err_intfdata:
1188 	usb_set_intfdata(intf, NULL);
1189 	usb_buffer_free(kaweth->dev, KAWETH_BUF_SIZE, (void *)kaweth->rx_buf, kaweth->rxbufferhandle);
1190 err_all_but_rxbuf:
1191 	usb_buffer_free(kaweth->dev, INTBUFFERSIZE, (void *)kaweth->intbuffer, kaweth->intbufferhandle);
1192 err_tx_and_rx_and_irq:
1193 	usb_free_urb(kaweth->irq_urb);
1194 err_tx_and_rx:
1195 	usb_free_urb(kaweth->rx_urb);
1196 err_only_tx:
1197 	usb_free_urb(kaweth->tx_urb);
1198 err_free_netdev:
1199 	free_netdev(netdev);
1200 
1201 	return -EIO;
1202 }
1203 
1204 /****************************************************************
1205  *     kaweth_disconnect
1206  ****************************************************************/
1207 static void kaweth_disconnect(struct usb_interface *intf)
1208 {
1209 	struct kaweth_device *kaweth = usb_get_intfdata(intf);
1210 	struct net_device *netdev;
1211 
1212 	dev_info(&intf->dev, "Unregistering\n");
1213 
1214 	usb_set_intfdata(intf, NULL);
1215 	if (!kaweth) {
1216 		dev_warn(&intf->dev, "unregistering non-existant device\n");
1217 		return;
1218 	}
1219 	netdev = kaweth->net;
1220 
1221 	dbg("Unregistering net device");
1222 	unregister_netdev(netdev);
1223 
1224 	usb_free_urb(kaweth->rx_urb);
1225 	usb_free_urb(kaweth->tx_urb);
1226 	usb_free_urb(kaweth->irq_urb);
1227 
1228 	usb_buffer_free(kaweth->dev, KAWETH_BUF_SIZE, (void *)kaweth->rx_buf, kaweth->rxbufferhandle);
1229 	usb_buffer_free(kaweth->dev, INTBUFFERSIZE, (void *)kaweth->intbuffer, kaweth->intbufferhandle);
1230 
1231 	free_netdev(netdev);
1232 }
1233 
1234 
1235 // FIXME this completion stuff is a modified clone of
1236 // an OLD version of some stuff in usb.c ...
1237 struct usb_api_data {
1238 	wait_queue_head_t wqh;
1239 	int done;
1240 };
1241 
1242 /*-------------------------------------------------------------------*
1243  * completion handler for compatibility wrappers (sync control/bulk) *
1244  *-------------------------------------------------------------------*/
1245 static void usb_api_blocking_completion(struct urb *urb)
1246 {
1247         struct usb_api_data *awd = (struct usb_api_data *)urb->context;
1248 
1249 	awd->done=1;
1250 	wake_up(&awd->wqh);
1251 }
1252 
1253 /*-------------------------------------------------------------------*
1254  *                         COMPATIBILITY STUFF                       *
1255  *-------------------------------------------------------------------*/
1256 
1257 // Starts urb and waits for completion or timeout
1258 static int usb_start_wait_urb(struct urb *urb, int timeout, int* actual_length)
1259 {
1260 	struct usb_api_data awd;
1261         int status;
1262 
1263         init_waitqueue_head(&awd.wqh);
1264         awd.done = 0;
1265 
1266         urb->context = &awd;
1267         status = usb_submit_urb(urb, GFP_NOIO);
1268         if (status) {
1269                 // something went wrong
1270                 usb_free_urb(urb);
1271                 return status;
1272         }
1273 
1274 	if (!wait_event_timeout(awd.wqh, awd.done, timeout)) {
1275                 // timeout
1276                 dev_warn(&urb->dev->dev, "usb_control/bulk_msg: timeout\n");
1277                 usb_kill_urb(urb);  // remove urb safely
1278                 status = -ETIMEDOUT;
1279         }
1280 	else {
1281                 status = urb->status;
1282 	}
1283 
1284         if (actual_length) {
1285                 *actual_length = urb->actual_length;
1286 	}
1287 
1288         usb_free_urb(urb);
1289         return status;
1290 }
1291 
1292 /*-------------------------------------------------------------------*/
1293 // returns status (negative) or length (positive)
1294 static int kaweth_internal_control_msg(struct usb_device *usb_dev,
1295 				       unsigned int pipe,
1296 				       struct usb_ctrlrequest *cmd, void *data,
1297 				       int len, int timeout)
1298 {
1299         struct urb *urb;
1300         int retv;
1301         int length = 0; /* shut up GCC */
1302 
1303         urb = usb_alloc_urb(0, GFP_NOIO);
1304         if (!urb)
1305                 return -ENOMEM;
1306 
1307         usb_fill_control_urb(urb, usb_dev, pipe, (unsigned char*)cmd, data,
1308 			 len, usb_api_blocking_completion, NULL);
1309 
1310         retv = usb_start_wait_urb(urb, timeout, &length);
1311         if (retv < 0) {
1312                 return retv;
1313 	}
1314         else {
1315                 return length;
1316 	}
1317 }
1318 
1319 
1320 /****************************************************************
1321  *     kaweth_init
1322  ****************************************************************/
1323 static int __init kaweth_init(void)
1324 {
1325 	dbg("Driver loading");
1326 	return usb_register(&kaweth_driver);
1327 }
1328 
1329 /****************************************************************
1330  *     kaweth_exit
1331  ****************************************************************/
1332 static void __exit kaweth_exit(void)
1333 {
1334 	usb_deregister(&kaweth_driver);
1335 }
1336 
1337 module_init(kaweth_init);
1338 module_exit(kaweth_exit);
1339 
1340 
1341 
1342 
1343 
1344 
1345 
1346 
1347 
1348