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