GNU Linux-libre 4.4.294-gnu1
[releases.git] / drivers / net / usb / kaweth.c
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/delay.h>
49 #include <linux/netdevice.h>
50 #include <linux/etherdevice.h>
51 #include <linux/usb.h>
52 #include <linux/types.h>
53 #include <linux/ethtool.h>
54 #include <linux/dma-mapping.h>
55 #include <linux/wait.h>
56 #include <linux/firmware.h>
57 #include <asm/uaccess.h>
58 #include <asm/byteorder.h>
59
60 #undef DEBUG
61
62 #define KAWETH_MTU                      1514
63 #define KAWETH_BUF_SIZE                 1664
64 #define KAWETH_TX_TIMEOUT               (5 * HZ)
65 #define KAWETH_SCRATCH_SIZE             32
66 #define KAWETH_FIRMWARE_BUF_SIZE        4096
67 #define KAWETH_CONTROL_TIMEOUT          (30000)
68
69 #define KAWETH_STATUS_BROKEN            0x0000001
70 #define KAWETH_STATUS_CLOSING           0x0000002
71 #define KAWETH_STATUS_SUSPENDING        0x0000004
72
73 #define KAWETH_STATUS_BLOCKED (KAWETH_STATUS_CLOSING | KAWETH_STATUS_SUSPENDING)
74
75 #define KAWETH_PACKET_FILTER_PROMISCUOUS        0x01
76 #define KAWETH_PACKET_FILTER_ALL_MULTICAST      0x02
77 #define KAWETH_PACKET_FILTER_DIRECTED           0x04
78 #define KAWETH_PACKET_FILTER_BROADCAST          0x08
79 #define KAWETH_PACKET_FILTER_MULTICAST          0x10
80
81 /* Table 7 */
82 #define KAWETH_COMMAND_GET_ETHERNET_DESC        0x00
83 #define KAWETH_COMMAND_MULTICAST_FILTERS        0x01
84 #define KAWETH_COMMAND_SET_PACKET_FILTER        0x02
85 #define KAWETH_COMMAND_STATISTICS               0x03
86 #define KAWETH_COMMAND_SET_TEMP_MAC             0x06
87 #define KAWETH_COMMAND_GET_TEMP_MAC             0x07
88 #define KAWETH_COMMAND_SET_URB_SIZE             0x08
89 #define KAWETH_COMMAND_SET_SOFS_WAIT            0x09
90 #define KAWETH_COMMAND_SCAN                     0xFF
91
92 #define KAWETH_SOFS_TO_WAIT                     0x05
93
94 #define INTBUFFERSIZE                           4
95
96 #define STATE_OFFSET                            0
97 #define STATE_MASK                              0x40
98 #define STATE_SHIFT                             5
99
100 #define IS_BLOCKED(s) (s & KAWETH_STATUS_BLOCKED)
101
102
103 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>");
104 MODULE_DESCRIPTION("KL5USB101 USB Ethernet driver");
105 MODULE_LICENSE("GPL");
106 /*(DEBLOBBED)*/
107
108 static const char driver_name[] = "kaweth";
109
110 static int kaweth_probe(
111                 struct usb_interface *intf,
112                 const struct usb_device_id *id  /* from id_table */
113         );
114 static void kaweth_disconnect(struct usb_interface *intf);
115 static int kaweth_internal_control_msg(struct usb_device *usb_dev,
116                                        unsigned int pipe,
117                                        struct usb_ctrlrequest *cmd, void *data,
118                                        int len, int timeout);
119 static int kaweth_suspend(struct usb_interface *intf, pm_message_t message);
120 static int kaweth_resume(struct usb_interface *intf);
121
122 /****************************************************************
123  *     usb_device_id
124  ****************************************************************/
125 static struct usb_device_id usb_klsi_table[] = {
126         { USB_DEVICE(0x03e8, 0x0008) }, /* AOX Endpoints USB Ethernet */
127         { USB_DEVICE(0x04bb, 0x0901) }, /* I-O DATA USB-ET/T */
128         { USB_DEVICE(0x0506, 0x03e8) }, /* 3Com 3C19250 */
129         { USB_DEVICE(0x0506, 0x11f8) }, /* 3Com 3C460 */
130         { USB_DEVICE(0x0557, 0x2002) }, /* ATEN USB Ethernet */
131         { USB_DEVICE(0x0557, 0x4000) }, /* D-Link DSB-650C */
132         { USB_DEVICE(0x0565, 0x0002) }, /* Peracom Enet */
133         { USB_DEVICE(0x0565, 0x0003) }, /* Optus@Home UEP1045A */
134         { USB_DEVICE(0x0565, 0x0005) }, /* Peracom Enet2 */
135         { USB_DEVICE(0x05e9, 0x0008) }, /* KLSI KL5KUSB101B */
136         { USB_DEVICE(0x05e9, 0x0009) }, /* KLSI KL5KUSB101B (Board change) */
137         { USB_DEVICE(0x066b, 0x2202) }, /* Linksys USB10T */
138         { USB_DEVICE(0x06e1, 0x0008) }, /* ADS USB-10BT */
139         { USB_DEVICE(0x06e1, 0x0009) }, /* ADS USB-10BT */
140         { USB_DEVICE(0x0707, 0x0100) }, /* SMC 2202USB */
141         { USB_DEVICE(0x07aa, 0x0001) }, /* Correga K.K. */
142         { USB_DEVICE(0x07b8, 0x4000) }, /* D-Link DU-E10 */
143         { USB_DEVICE(0x07c9, 0xb010) }, /* Allied Telesyn AT-USB10 USB Ethernet Adapter */
144         { USB_DEVICE(0x0846, 0x1001) }, /* NetGear EA-101 */
145         { USB_DEVICE(0x0846, 0x1002) }, /* NetGear EA-101 */
146         { USB_DEVICE(0x085a, 0x0008) }, /* PortGear Ethernet Adapter */
147         { USB_DEVICE(0x085a, 0x0009) }, /* PortGear Ethernet Adapter */
148         { USB_DEVICE(0x087d, 0x5704) }, /* Jaton USB Ethernet Device Adapter */
149         { USB_DEVICE(0x0951, 0x0008) }, /* Kingston Technology USB Ethernet Adapter */
150         { USB_DEVICE(0x095a, 0x3003) }, /* Portsmith Express Ethernet Adapter */
151         { USB_DEVICE(0x10bd, 0x1427) }, /* ASANTE USB To Ethernet Adapter */
152         { USB_DEVICE(0x1342, 0x0204) }, /* Mobility USB-Ethernet Adapter */
153         { USB_DEVICE(0x13d2, 0x0400) }, /* Shark Pocket Adapter */
154         { USB_DEVICE(0x1485, 0x0001) }, /* Silicom U2E */
155         { USB_DEVICE(0x1485, 0x0002) }, /* Psion Dacom Gold Port Ethernet */
156         { USB_DEVICE(0x1645, 0x0005) }, /* Entrega E45 */
157         { USB_DEVICE(0x1645, 0x0008) }, /* Entrega USB Ethernet Adapter */
158         { USB_DEVICE(0x1645, 0x8005) }, /* PortGear Ethernet Adapter */
159         { USB_DEVICE(0x1668, 0x0323) }, /* Actiontec USB Ethernet */
160         { USB_DEVICE(0x2001, 0x4000) }, /* D-link DSB-650C */
161         {} /* Null terminator */
162 };
163
164 MODULE_DEVICE_TABLE (usb, usb_klsi_table);
165
166 /****************************************************************
167  *     kaweth_driver
168  ****************************************************************/
169 static struct usb_driver kaweth_driver = {
170         .name =         driver_name,
171         .probe =        kaweth_probe,
172         .disconnect =   kaweth_disconnect,
173         .suspend =      kaweth_suspend,
174         .resume =       kaweth_resume,
175         .id_table =     usb_klsi_table,
176         .supports_autosuspend = 1,
177         .disable_hub_initiated_lpm = 1,
178 };
179
180 typedef __u8 eth_addr_t[6];
181
182 /****************************************************************
183  *     usb_eth_dev
184  ****************************************************************/
185 struct usb_eth_dev {
186         char *name;
187         __u16 vendor;
188         __u16 device;
189         void *pdata;
190 };
191
192 /****************************************************************
193  *     kaweth_ethernet_configuration
194  *     Refer Table 8
195  ****************************************************************/
196 struct kaweth_ethernet_configuration
197 {
198         __u8 size;
199         __u8 reserved1;
200         __u8 reserved2;
201         eth_addr_t hw_addr;
202         __u32 statistics_mask;
203         __le16 segment_size;
204         __u16 max_multicast_filters;
205         __u8 reserved3;
206 } __packed;
207
208 /****************************************************************
209  *     kaweth_device
210  ****************************************************************/
211 struct kaweth_device
212 {
213         spinlock_t device_lock;
214
215         __u32 status;
216         int end;
217         int suspend_lowmem_rx;
218         int suspend_lowmem_ctrl;
219         int linkstate;
220         int opened;
221         struct delayed_work lowmem_work;
222
223         struct usb_device *dev;
224         struct usb_interface *intf;
225         struct net_device *net;
226         wait_queue_head_t term_wait;
227
228         struct urb *rx_urb;
229         struct urb *tx_urb;
230         struct urb *irq_urb;
231
232         dma_addr_t intbufferhandle;
233         __u8 *intbuffer;
234         dma_addr_t rxbufferhandle;
235         __u8 *rx_buf;
236
237         
238         struct sk_buff *tx_skb;
239
240         __u8 *firmware_buf;
241         __u8 scratch[KAWETH_SCRATCH_SIZE];
242         __u16 packet_filter_bitmap;
243
244         struct kaweth_ethernet_configuration configuration;
245
246         struct net_device_stats stats;
247 };
248
249 /****************************************************************
250  *     kaweth_control
251  ****************************************************************/
252 static int kaweth_control(struct kaweth_device *kaweth,
253                           unsigned int pipe,
254                           __u8 request,
255                           __u8 requesttype,
256                           __u16 value,
257                           __u16 index,
258                           void *data,
259                           __u16 size,
260                           int timeout)
261 {
262         struct usb_ctrlrequest *dr;
263         int retval;
264
265         netdev_dbg(kaweth->net, "kaweth_control()\n");
266
267         if(in_interrupt()) {
268                 netdev_dbg(kaweth->net, "in_interrupt()\n");
269                 return -EBUSY;
270         }
271
272         dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
273         if (!dr)
274                 return -ENOMEM;
275
276         dr->bRequestType = requesttype;
277         dr->bRequest = request;
278         dr->wValue = cpu_to_le16(value);
279         dr->wIndex = cpu_to_le16(index);
280         dr->wLength = cpu_to_le16(size);
281
282         retval = kaweth_internal_control_msg(kaweth->dev,
283                                              pipe,
284                                              dr,
285                                              data,
286                                              size,
287                                              timeout);
288
289         kfree(dr);
290         return retval;
291 }
292
293 /****************************************************************
294  *     kaweth_read_configuration
295  ****************************************************************/
296 static int kaweth_read_configuration(struct kaweth_device *kaweth)
297 {
298         int retval;
299
300         netdev_dbg(kaweth->net, "Reading kaweth configuration\n");
301
302         retval = kaweth_control(kaweth,
303                                 usb_rcvctrlpipe(kaweth->dev, 0),
304                                 KAWETH_COMMAND_GET_ETHERNET_DESC,
305                                 USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_DEVICE,
306                                 0,
307                                 0,
308                                 (void *)&kaweth->configuration,
309                                 sizeof(kaweth->configuration),
310                                 KAWETH_CONTROL_TIMEOUT);
311
312         return retval;
313 }
314
315 /****************************************************************
316  *     kaweth_set_urb_size
317  ****************************************************************/
318 static int kaweth_set_urb_size(struct kaweth_device *kaweth, __u16 urb_size)
319 {
320         int retval;
321
322         netdev_dbg(kaweth->net, "Setting URB size to %d\n", (unsigned)urb_size);
323
324         retval = kaweth_control(kaweth,
325                                 usb_sndctrlpipe(kaweth->dev, 0),
326                                 KAWETH_COMMAND_SET_URB_SIZE,
327                                 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
328                                 urb_size,
329                                 0,
330                                 (void *)&kaweth->scratch,
331                                 0,
332                                 KAWETH_CONTROL_TIMEOUT);
333
334         return retval;
335 }
336
337 /****************************************************************
338  *     kaweth_set_sofs_wait
339  ****************************************************************/
340 static int kaweth_set_sofs_wait(struct kaweth_device *kaweth, __u16 sofs_wait)
341 {
342         int retval;
343
344         netdev_dbg(kaweth->net, "Set SOFS wait to %d\n", (unsigned)sofs_wait);
345
346         retval = kaweth_control(kaweth,
347                                 usb_sndctrlpipe(kaweth->dev, 0),
348                                 KAWETH_COMMAND_SET_SOFS_WAIT,
349                                 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
350                                 sofs_wait,
351                                 0,
352                                 (void *)&kaweth->scratch,
353                                 0,
354                                 KAWETH_CONTROL_TIMEOUT);
355
356         return retval;
357 }
358
359 /****************************************************************
360  *     kaweth_set_receive_filter
361  ****************************************************************/
362 static int kaweth_set_receive_filter(struct kaweth_device *kaweth,
363                                      __u16 receive_filter)
364 {
365         int retval;
366
367         netdev_dbg(kaweth->net, "Set receive filter to %d\n",
368                    (unsigned)receive_filter);
369
370         retval = kaweth_control(kaweth,
371                                 usb_sndctrlpipe(kaweth->dev, 0),
372                                 KAWETH_COMMAND_SET_PACKET_FILTER,
373                                 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
374                                 receive_filter,
375                                 0,
376                                 (void *)&kaweth->scratch,
377                                 0,
378                                 KAWETH_CONTROL_TIMEOUT);
379
380         return retval;
381 }
382
383 /****************************************************************
384  *     kaweth_download_firmware
385  ****************************************************************/
386 static int kaweth_download_firmware(struct kaweth_device *kaweth,
387                                     const char *fwname,
388                                     __u8 interrupt,
389                                     __u8 type)
390 {
391         const struct firmware *fw;
392         int data_len;
393         int ret;
394
395         ret = reject_firmware(&fw, fwname, &kaweth->dev->dev);
396         if (ret) {
397                 dev_err(&kaweth->intf->dev, "Firmware request failed\n");
398                 return ret;
399         }
400
401         if (fw->size > KAWETH_FIRMWARE_BUF_SIZE) {
402                 dev_err(&kaweth->intf->dev, "Firmware too big: %zu\n",
403                         fw->size);
404                 release_firmware(fw);
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         netdev_dbg(kaweth->net, "High: %i, Low:%i\n", kaweth->firmware_buf[3],
418                    kaweth->firmware_buf[2]);
419
420         netdev_dbg(kaweth->net,
421                    "Downloading firmware at %p to kaweth device at %p\n",
422                    kaweth->firmware_buf, kaweth);
423         netdev_dbg(kaweth->net, "Firmware length: %d\n", 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  ****************************************************************/
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         netdev_dbg(kaweth->net, "Triggering firmware\n");
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  ****************************************************************/
467 static int kaweth_reset(struct kaweth_device *kaweth)
468 {
469         int result;
470
471         netdev_dbg(kaweth->net, "kaweth_reset(%p)\n", kaweth);
472         result = usb_reset_configuration(kaweth->dev);
473         mdelay(10);
474
475         netdev_dbg(kaweth->net, "kaweth_reset() returns %d.\n", result);
476
477         return result;
478 }
479
480 static void kaweth_usb_receive(struct urb *);
481 static int kaweth_resubmit_rx_urb(struct kaweth_device *, gfp_t);
482
483 /****************************************************************
484         int_callback
485 *****************************************************************/
486
487 static void kaweth_resubmit_int_urb(struct kaweth_device *kaweth, gfp_t mf)
488 {
489         int status;
490
491         status = usb_submit_urb (kaweth->irq_urb, mf);
492         if (unlikely(status == -ENOMEM)) {
493                 kaweth->suspend_lowmem_ctrl = 1;
494                 schedule_delayed_work(&kaweth->lowmem_work, HZ/4);
495         } else {
496                 kaweth->suspend_lowmem_ctrl = 0;
497         }
498
499         if (status)
500                 dev_err(&kaweth->intf->dev,
501                         "can't resubmit intr, %s-%s, status %d\n",
502                         kaweth->dev->bus->bus_name,
503                         kaweth->dev->devpath, status);
504 }
505
506 static void int_callback(struct urb *u)
507 {
508         struct kaweth_device *kaweth = u->context;
509         int act_state;
510         int status = u->status;
511
512         switch (status) {
513         case 0:                 /* success */
514                 break;
515         case -ECONNRESET:       /* unlink */
516         case -ENOENT:
517         case -ESHUTDOWN:
518                 return;
519         /* -EPIPE:  should clear the halt */
520         default:                /* error */
521                 goto resubmit;
522         }
523
524         /* we check the link state to report changes */
525         if (kaweth->linkstate != (act_state = ( kaweth->intbuffer[STATE_OFFSET] | STATE_MASK) >> STATE_SHIFT)) {
526                 if (act_state)
527                         netif_carrier_on(kaweth->net);
528                 else
529                         netif_carrier_off(kaweth->net);
530
531                 kaweth->linkstate = act_state;
532         }
533 resubmit:
534         kaweth_resubmit_int_urb(kaweth, GFP_ATOMIC);
535 }
536
537 static void kaweth_resubmit_tl(struct work_struct *work)
538 {
539         struct kaweth_device *kaweth =
540                 container_of(work, struct kaweth_device, lowmem_work.work);
541
542         if (IS_BLOCKED(kaweth->status))
543                 return;
544
545         if (kaweth->suspend_lowmem_rx)
546                 kaweth_resubmit_rx_urb(kaweth, GFP_NOIO);
547
548         if (kaweth->suspend_lowmem_ctrl)
549                 kaweth_resubmit_int_urb(kaweth, GFP_NOIO);
550 }
551
552
553 /****************************************************************
554  *     kaweth_resubmit_rx_urb
555  ****************************************************************/
556 static int kaweth_resubmit_rx_urb(struct kaweth_device *kaweth,
557                                                 gfp_t mem_flags)
558 {
559         int result;
560
561         usb_fill_bulk_urb(kaweth->rx_urb,
562                       kaweth->dev,
563                       usb_rcvbulkpipe(kaweth->dev, 1),
564                       kaweth->rx_buf,
565                       KAWETH_BUF_SIZE,
566                       kaweth_usb_receive,
567                       kaweth);
568         kaweth->rx_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
569         kaweth->rx_urb->transfer_dma = kaweth->rxbufferhandle;
570
571         if((result = usb_submit_urb(kaweth->rx_urb, mem_flags))) {
572                 if (result == -ENOMEM) {
573                         kaweth->suspend_lowmem_rx = 1;
574                         schedule_delayed_work(&kaweth->lowmem_work, HZ/4);
575                 }
576                 dev_err(&kaweth->intf->dev, "resubmitting rx_urb %d failed\n",
577                         result);
578         } else {
579                 kaweth->suspend_lowmem_rx = 0;
580         }
581
582         return result;
583 }
584
585 static void kaweth_async_set_rx_mode(struct kaweth_device *kaweth);
586
587 /****************************************************************
588  *     kaweth_usb_receive
589  ****************************************************************/
590 static void kaweth_usb_receive(struct urb *urb)
591 {
592         struct device *dev = &urb->dev->dev;
593         struct kaweth_device *kaweth = urb->context;
594         struct net_device *net = kaweth->net;
595         int status = urb->status;
596
597         int count = urb->actual_length;
598         int count2 = urb->transfer_buffer_length;
599
600         __u16 pkt_len = le16_to_cpup((__le16 *)kaweth->rx_buf);
601
602         struct sk_buff *skb;
603
604         if (unlikely(status == -EPIPE)) {
605                 kaweth->stats.rx_errors++;
606                 kaweth->end = 1;
607                 wake_up(&kaweth->term_wait);
608                 dev_dbg(dev, "Status was -EPIPE.\n");
609                 return;
610         }
611         if (unlikely(status == -ECONNRESET || status == -ESHUTDOWN)) {
612                 /* we are killed - set a flag and wake the disconnect handler */
613                 kaweth->end = 1;
614                 wake_up(&kaweth->term_wait);
615                 dev_dbg(dev, "Status was -ECONNRESET or -ESHUTDOWN.\n");
616                 return;
617         }
618         if (unlikely(status == -EPROTO || status == -ETIME ||
619                      status == -EILSEQ)) {
620                 kaweth->stats.rx_errors++;
621                 dev_dbg(dev, "Status was -EPROTO, -ETIME, or -EILSEQ.\n");
622                 return;
623         }
624         if (unlikely(status == -EOVERFLOW)) {
625                 kaweth->stats.rx_errors++;
626                 dev_dbg(dev, "Status was -EOVERFLOW.\n");
627         }
628         spin_lock(&kaweth->device_lock);
629         if (IS_BLOCKED(kaweth->status)) {
630                 spin_unlock(&kaweth->device_lock);
631                 return;
632         }
633         spin_unlock(&kaweth->device_lock);
634
635         if(status && status != -EREMOTEIO && count != 1) {
636                 dev_err(&kaweth->intf->dev,
637                         "%s RX status: %d count: %d packet_len: %d\n",
638                         net->name, status, count, (int)pkt_len);
639                 kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
640                 return;
641         }
642
643         if(kaweth->net && (count > 2)) {
644                 if(pkt_len > (count - 2)) {
645                         dev_err(&kaweth->intf->dev,
646                                 "Packet length too long for USB frame (pkt_len: %x, count: %x)\n",
647                                 pkt_len, count);
648                         dev_err(&kaweth->intf->dev, "Packet len & 2047: %x\n",
649                                 pkt_len & 2047);
650                         dev_err(&kaweth->intf->dev, "Count 2: %x\n", count2);
651                         kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
652                         return;
653                 }
654
655                 if(!(skb = dev_alloc_skb(pkt_len+2))) {
656                         kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
657                         return;
658                 }
659
660                 skb_reserve(skb, 2);    /* Align IP on 16 byte boundaries */
661
662                 skb_copy_to_linear_data(skb, kaweth->rx_buf + 2, pkt_len);
663
664                 skb_put(skb, pkt_len);
665
666                 skb->protocol = eth_type_trans(skb, net);
667
668                 netif_rx(skb);
669
670                 kaweth->stats.rx_packets++;
671                 kaweth->stats.rx_bytes += pkt_len;
672         }
673
674         kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
675 }
676
677 /****************************************************************
678  *     kaweth_open
679  ****************************************************************/
680 static int kaweth_open(struct net_device *net)
681 {
682         struct kaweth_device *kaweth = netdev_priv(net);
683         int res;
684
685         netdev_dbg(kaweth->net, "Opening network device.\n");
686
687         res = usb_autopm_get_interface(kaweth->intf);
688         if (res) {
689                 dev_err(&kaweth->intf->dev, "Interface cannot be resumed.\n");
690                 return -EIO;
691         }
692         res = kaweth_resubmit_rx_urb(kaweth, GFP_KERNEL);
693         if (res)
694                 goto err_out;
695
696         usb_fill_int_urb(
697                 kaweth->irq_urb,
698                 kaweth->dev,
699                 usb_rcvintpipe(kaweth->dev, 3),
700                 kaweth->intbuffer,
701                 INTBUFFERSIZE,
702                 int_callback,
703                 kaweth,
704                 250); /* overriding the descriptor */
705         kaweth->irq_urb->transfer_dma = kaweth->intbufferhandle;
706         kaweth->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
707
708         res = usb_submit_urb(kaweth->irq_urb, GFP_KERNEL);
709         if (res) {
710                 usb_kill_urb(kaweth->rx_urb);
711                 goto err_out;
712         }
713         kaweth->opened = 1;
714
715         netif_start_queue(net);
716
717         kaweth_async_set_rx_mode(kaweth);
718         return 0;
719
720 err_out:
721         usb_autopm_put_interface(kaweth->intf);
722         return -EIO;
723 }
724
725 /****************************************************************
726  *     kaweth_kill_urbs
727  ****************************************************************/
728 static void kaweth_kill_urbs(struct kaweth_device *kaweth)
729 {
730         usb_kill_urb(kaweth->irq_urb);
731         usb_kill_urb(kaweth->rx_urb);
732         usb_kill_urb(kaweth->tx_urb);
733
734         cancel_delayed_work_sync(&kaweth->lowmem_work);
735
736         /* a scheduled work may have resubmitted,
737            we hit them again */
738         usb_kill_urb(kaweth->irq_urb);
739         usb_kill_urb(kaweth->rx_urb);
740 }
741
742 /****************************************************************
743  *     kaweth_close
744  ****************************************************************/
745 static int kaweth_close(struct net_device *net)
746 {
747         struct kaweth_device *kaweth = netdev_priv(net);
748
749         netif_stop_queue(net);
750         kaweth->opened = 0;
751
752         kaweth->status |= KAWETH_STATUS_CLOSING;
753
754         kaweth_kill_urbs(kaweth);
755
756         kaweth->status &= ~KAWETH_STATUS_CLOSING;
757
758         usb_autopm_put_interface(kaweth->intf);
759
760         return 0;
761 }
762
763 static u32 kaweth_get_link(struct net_device *dev)
764 {
765         struct kaweth_device *kaweth = netdev_priv(dev);
766
767         return kaweth->linkstate;
768 }
769
770 static const struct ethtool_ops ops = {
771         .get_link       = kaweth_get_link
772 };
773
774 /****************************************************************
775  *     kaweth_usb_transmit_complete
776  ****************************************************************/
777 static void kaweth_usb_transmit_complete(struct urb *urb)
778 {
779         struct kaweth_device *kaweth = urb->context;
780         struct sk_buff *skb = kaweth->tx_skb;
781         int status = urb->status;
782
783         if (unlikely(status != 0))
784                 if (status != -ENOENT)
785                         dev_dbg(&urb->dev->dev, "%s: TX status %d.\n",
786                                 kaweth->net->name, status);
787
788         netif_wake_queue(kaweth->net);
789         dev_kfree_skb_irq(skb);
790 }
791
792 /****************************************************************
793  *     kaweth_start_xmit
794  ****************************************************************/
795 static netdev_tx_t kaweth_start_xmit(struct sk_buff *skb,
796                                            struct net_device *net)
797 {
798         struct kaweth_device *kaweth = netdev_priv(net);
799         __le16 *private_header;
800
801         int res;
802
803         spin_lock_irq(&kaweth->device_lock);
804
805         kaweth_async_set_rx_mode(kaweth);
806         netif_stop_queue(net);
807         if (IS_BLOCKED(kaweth->status)) {
808                 goto skip;
809         }
810
811         /* We now decide whether we can put our special header into the sk_buff */
812         if (skb_cow_head(skb, 2)) {
813                 kaweth->stats.tx_errors++;
814                 netif_start_queue(net);
815                 spin_unlock_irq(&kaweth->device_lock);
816                 dev_kfree_skb_any(skb);
817                 return NETDEV_TX_OK;
818         }
819
820         private_header = (__le16 *)__skb_push(skb, 2);
821         *private_header = cpu_to_le16(skb->len-2);
822         kaweth->tx_skb = skb;
823
824         usb_fill_bulk_urb(kaweth->tx_urb,
825                       kaweth->dev,
826                       usb_sndbulkpipe(kaweth->dev, 2),
827                       private_header,
828                       skb->len,
829                       kaweth_usb_transmit_complete,
830                       kaweth);
831         kaweth->end = 0;
832
833         if((res = usb_submit_urb(kaweth->tx_urb, GFP_ATOMIC)))
834         {
835                 dev_warn(&net->dev, "kaweth failed tx_urb %d\n", res);
836 skip:
837                 kaweth->stats.tx_errors++;
838
839                 netif_start_queue(net);
840                 dev_kfree_skb_irq(skb);
841         }
842         else
843         {
844                 kaweth->stats.tx_packets++;
845                 kaweth->stats.tx_bytes += skb->len;
846         }
847
848         spin_unlock_irq(&kaweth->device_lock);
849
850         return NETDEV_TX_OK;
851 }
852
853 /****************************************************************
854  *     kaweth_set_rx_mode
855  ****************************************************************/
856 static void kaweth_set_rx_mode(struct net_device *net)
857 {
858         struct kaweth_device *kaweth = netdev_priv(net);
859
860         __u16 packet_filter_bitmap = KAWETH_PACKET_FILTER_DIRECTED |
861                                      KAWETH_PACKET_FILTER_BROADCAST |
862                                      KAWETH_PACKET_FILTER_MULTICAST;
863
864         netdev_dbg(net, "Setting Rx mode to %d\n", packet_filter_bitmap);
865
866         netif_stop_queue(net);
867
868         if (net->flags & IFF_PROMISC) {
869                 packet_filter_bitmap |= KAWETH_PACKET_FILTER_PROMISCUOUS;
870         }
871         else if (!netdev_mc_empty(net) || (net->flags & IFF_ALLMULTI)) {
872                 packet_filter_bitmap |= KAWETH_PACKET_FILTER_ALL_MULTICAST;
873         }
874
875         kaweth->packet_filter_bitmap = packet_filter_bitmap;
876         netif_wake_queue(net);
877 }
878
879 /****************************************************************
880  *     kaweth_async_set_rx_mode
881  ****************************************************************/
882 static void kaweth_async_set_rx_mode(struct kaweth_device *kaweth)
883 {
884         int result;
885         __u16 packet_filter_bitmap = kaweth->packet_filter_bitmap;
886
887         kaweth->packet_filter_bitmap = 0;
888         if (packet_filter_bitmap == 0)
889                 return;
890
891         if (in_interrupt())
892                 return;
893
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                 dev_err(&kaweth->intf->dev, "Failed to set Rx mode: %d\n",
906                         result);
907         }
908         else {
909                 netdev_dbg(kaweth->net, "Set Rx mode to %d\n",
910                            packet_filter_bitmap);
911         }
912 }
913
914 /****************************************************************
915  *     kaweth_netdev_stats
916  ****************************************************************/
917 static struct net_device_stats *kaweth_netdev_stats(struct net_device *dev)
918 {
919         struct kaweth_device *kaweth = netdev_priv(dev);
920         return &kaweth->stats;
921 }
922
923 /****************************************************************
924  *     kaweth_tx_timeout
925  ****************************************************************/
926 static void kaweth_tx_timeout(struct net_device *net)
927 {
928         struct kaweth_device *kaweth = netdev_priv(net);
929
930         dev_warn(&net->dev, "%s: Tx timed out. Resetting.\n", net->name);
931         kaweth->stats.tx_errors++;
932         net->trans_start = jiffies;
933
934         usb_unlink_urb(kaweth->tx_urb);
935 }
936
937 /****************************************************************
938  *     kaweth_suspend
939  ****************************************************************/
940 static int kaweth_suspend(struct usb_interface *intf, pm_message_t message)
941 {
942         struct kaweth_device *kaweth = usb_get_intfdata(intf);
943         unsigned long flags;
944
945         dev_dbg(&intf->dev, "Suspending device\n");
946         spin_lock_irqsave(&kaweth->device_lock, flags);
947         kaweth->status |= KAWETH_STATUS_SUSPENDING;
948         spin_unlock_irqrestore(&kaweth->device_lock, flags);
949
950         kaweth_kill_urbs(kaweth);
951         return 0;
952 }
953
954 /****************************************************************
955  *     kaweth_resume
956  ****************************************************************/
957 static int kaweth_resume(struct usb_interface *intf)
958 {
959         struct kaweth_device *kaweth = usb_get_intfdata(intf);
960         unsigned long flags;
961
962         dev_dbg(&intf->dev, "Resuming device\n");
963         spin_lock_irqsave(&kaweth->device_lock, flags);
964         kaweth->status &= ~KAWETH_STATUS_SUSPENDING;
965         spin_unlock_irqrestore(&kaweth->device_lock, flags);
966
967         if (!kaweth->opened)
968                 return 0;
969         kaweth_resubmit_rx_urb(kaweth, GFP_NOIO);
970         kaweth_resubmit_int_urb(kaweth, GFP_NOIO);
971
972         return 0;
973 }
974
975 /****************************************************************
976  *     kaweth_probe
977  ****************************************************************/
978
979
980 static const struct net_device_ops kaweth_netdev_ops = {
981         .ndo_open =                     kaweth_open,
982         .ndo_stop =                     kaweth_close,
983         .ndo_start_xmit =               kaweth_start_xmit,
984         .ndo_tx_timeout =               kaweth_tx_timeout,
985         .ndo_set_rx_mode =              kaweth_set_rx_mode,
986         .ndo_get_stats =                kaweth_netdev_stats,
987         .ndo_change_mtu =               eth_change_mtu,
988         .ndo_set_mac_address =          eth_mac_addr,
989         .ndo_validate_addr =            eth_validate_addr,
990 };
991
992 static int kaweth_probe(
993                 struct usb_interface *intf,
994                 const struct usb_device_id *id      /* from id_table */
995         )
996 {
997         struct device *dev = &intf->dev;
998         struct usb_device *udev = interface_to_usbdev(intf);
999         struct kaweth_device *kaweth;
1000         struct net_device *netdev;
1001         const eth_addr_t bcast_addr = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
1002         int result = 0;
1003         int rv = -EIO;
1004
1005         dev_dbg(dev,
1006                 "Kawasaki Device Probe (Device number:%d): 0x%4.4x:0x%4.4x:0x%4.4x\n",
1007                 udev->devnum, le16_to_cpu(udev->descriptor.idVendor),
1008                 le16_to_cpu(udev->descriptor.idProduct),
1009                 le16_to_cpu(udev->descriptor.bcdDevice));
1010
1011         dev_dbg(dev, "Device at %p\n", udev);
1012
1013         dev_dbg(dev, "Descriptor length: %x type: %x\n",
1014                 (int)udev->descriptor.bLength,
1015                 (int)udev->descriptor.bDescriptorType);
1016
1017         netdev = alloc_etherdev(sizeof(*kaweth));
1018         if (!netdev)
1019                 return -ENOMEM;
1020
1021         kaweth = netdev_priv(netdev);
1022         kaweth->dev = udev;
1023         kaweth->net = netdev;
1024         kaweth->intf = intf;
1025
1026         spin_lock_init(&kaweth->device_lock);
1027         init_waitqueue_head(&kaweth->term_wait);
1028
1029         dev_dbg(dev, "Resetting.\n");
1030
1031         kaweth_reset(kaweth);
1032
1033         /*
1034          * If high byte of bcdDevice is nonzero, firmware is already
1035          * downloaded. Don't try to do it again, or we'll hang the device.
1036          */
1037
1038         if (le16_to_cpu(udev->descriptor.bcdDevice) >> 8) {
1039                 dev_info(dev, "Firmware present in device.\n");
1040         } else {
1041                 /* Download the firmware */
1042                 dev_info(dev, "Downloading firmware...\n");
1043                 kaweth->firmware_buf = (__u8 *)__get_free_page(GFP_KERNEL);
1044                 if (!kaweth->firmware_buf) {
1045                         rv = -ENOMEM;
1046                         goto err_free_netdev;
1047                 }
1048                 if ((result = kaweth_download_firmware(kaweth,
1049                                                       "/*(DEBLOBBED)*/",
1050                                                       100,
1051                                                       2)) < 0) {
1052                         dev_err(dev, "Error downloading firmware (%d)\n",
1053                                 result);
1054                         goto err_fw;
1055                 }
1056
1057                 if ((result = kaweth_download_firmware(kaweth,
1058                                                       "/*(DEBLOBBED)*/",
1059                                                       100,
1060                                                       3)) < 0) {
1061                         dev_err(dev, "Error downloading firmware fix (%d)\n",
1062                                 result);
1063                         goto err_fw;
1064                 }
1065
1066                 if ((result = kaweth_download_firmware(kaweth,
1067                                                       "/*(DEBLOBBED)*/",
1068                                                       126,
1069                                                       2)) < 0) {
1070                         dev_err(dev, "Error downloading trigger code (%d)\n",
1071                                 result);
1072                         goto err_fw;
1073
1074                 }
1075
1076                 if ((result = kaweth_download_firmware(kaweth,
1077                                                       "/*(DEBLOBBED)*/",
1078                                                       126,
1079                                                       3)) < 0) {
1080                         dev_err(dev, "Error downloading trigger code fix (%d)\n", result);
1081                         goto err_fw;
1082                 }
1083
1084
1085                 if ((result = kaweth_trigger_firmware(kaweth, 126)) < 0) {
1086                         dev_err(dev, "Error triggering firmware (%d)\n", result);
1087                         goto err_fw;
1088                 }
1089
1090                 /* Device will now disappear for a moment...  */
1091                 dev_info(dev, "Firmware loaded.  I'll be back...\n");
1092 err_fw:
1093                 free_page((unsigned long)kaweth->firmware_buf);
1094                 free_netdev(netdev);
1095                 return -EIO;
1096         }
1097
1098         result = kaweth_read_configuration(kaweth);
1099
1100         if(result < 0) {
1101                 dev_err(dev, "Error reading configuration (%d), no net device created\n", result);
1102                 goto err_free_netdev;
1103         }
1104
1105         dev_info(dev, "Statistics collection: %x\n", kaweth->configuration.statistics_mask);
1106         dev_info(dev, "Multicast filter limit: %x\n", kaweth->configuration.max_multicast_filters & ((1 << 15) - 1));
1107         dev_info(dev, "MTU: %d\n", le16_to_cpu(kaweth->configuration.segment_size));
1108         dev_info(dev, "Read MAC address %pM\n", kaweth->configuration.hw_addr);
1109
1110         if(!memcmp(&kaweth->configuration.hw_addr,
1111                    &bcast_addr,
1112                    sizeof(bcast_addr))) {
1113                 dev_err(dev, "Firmware not functioning properly, no net device created\n");
1114                 goto err_free_netdev;
1115         }
1116
1117         if(kaweth_set_urb_size(kaweth, KAWETH_BUF_SIZE) < 0) {
1118                 dev_dbg(dev, "Error setting URB size\n");
1119                 goto err_free_netdev;
1120         }
1121
1122         if(kaweth_set_sofs_wait(kaweth, KAWETH_SOFS_TO_WAIT) < 0) {
1123                 dev_err(dev, "Error setting SOFS wait\n");
1124                 goto err_free_netdev;
1125         }
1126
1127         result = kaweth_set_receive_filter(kaweth,
1128                                            KAWETH_PACKET_FILTER_DIRECTED |
1129                                            KAWETH_PACKET_FILTER_BROADCAST |
1130                                            KAWETH_PACKET_FILTER_MULTICAST);
1131
1132         if(result < 0) {
1133                 dev_err(dev, "Error setting receive filter\n");
1134                 goto err_free_netdev;
1135         }
1136
1137         dev_dbg(dev, "Initializing net device.\n");
1138
1139         kaweth->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
1140         if (!kaweth->tx_urb)
1141                 goto err_free_netdev;
1142         kaweth->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
1143         if (!kaweth->rx_urb)
1144                 goto err_only_tx;
1145         kaweth->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
1146         if (!kaweth->irq_urb)
1147                 goto err_tx_and_rx;
1148
1149         kaweth->intbuffer = usb_alloc_coherent( kaweth->dev,
1150                                                 INTBUFFERSIZE,
1151                                                 GFP_KERNEL,
1152                                                 &kaweth->intbufferhandle);
1153         if (!kaweth->intbuffer)
1154                 goto err_tx_and_rx_and_irq;
1155         kaweth->rx_buf = usb_alloc_coherent(    kaweth->dev,
1156                                                 KAWETH_BUF_SIZE,
1157                                                 GFP_KERNEL,
1158                                                 &kaweth->rxbufferhandle);
1159         if (!kaweth->rx_buf)
1160                 goto err_all_but_rxbuf;
1161
1162         memcpy(netdev->broadcast, &bcast_addr, sizeof(bcast_addr));
1163         memcpy(netdev->dev_addr, &kaweth->configuration.hw_addr,
1164                sizeof(kaweth->configuration.hw_addr));
1165
1166         netdev->netdev_ops = &kaweth_netdev_ops;
1167         netdev->watchdog_timeo = KAWETH_TX_TIMEOUT;
1168         netdev->mtu = le16_to_cpu(kaweth->configuration.segment_size);
1169         netdev->ethtool_ops = &ops;
1170
1171         /* kaweth is zeroed as part of alloc_netdev */
1172         INIT_DELAYED_WORK(&kaweth->lowmem_work, kaweth_resubmit_tl);
1173         usb_set_intfdata(intf, kaweth);
1174
1175         SET_NETDEV_DEV(netdev, dev);
1176         if (register_netdev(netdev) != 0) {
1177                 dev_err(dev, "Error registering netdev.\n");
1178                 goto err_intfdata;
1179         }
1180
1181         dev_info(dev, "kaweth interface created at %s\n",
1182                  kaweth->net->name);
1183
1184         dev_dbg(dev, "Kaweth probe returning.\n");
1185
1186         return 0;
1187
1188 err_intfdata:
1189         usb_set_intfdata(intf, NULL);
1190         usb_free_coherent(kaweth->dev, KAWETH_BUF_SIZE, (void *)kaweth->rx_buf, kaweth->rxbufferhandle);
1191 err_all_but_rxbuf:
1192         usb_free_coherent(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 rv;
1203 }
1204
1205 /****************************************************************
1206  *     kaweth_disconnect
1207  ****************************************************************/
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-existent device\n");
1218                 return;
1219         }
1220         netdev = kaweth->net;
1221
1222         netdev_dbg(kaweth->net, "Unregistering net device\n");
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_free_coherent(kaweth->dev, KAWETH_BUF_SIZE, (void *)kaweth->rx_buf, kaweth->rxbufferhandle);
1230         usb_free_coherent(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  *-------------------------------------------------------------------*/
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
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_ATOMIC);
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)
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_ATOMIC);
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 module_usb_driver(kaweth_driver);