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