GNU Linux-libre 4.9-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         if(in_interrupt()) {
266                 netdev_dbg(kaweth->net, "in_interrupt()\n");
267                 return -EBUSY;
268         }
269
270         dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
271         if (!dr)
272                 return -ENOMEM;
273
274         dr->bRequestType = requesttype;
275         dr->bRequest = request;
276         dr->wValue = cpu_to_le16(value);
277         dr->wIndex = cpu_to_le16(index);
278         dr->wLength = cpu_to_le16(size);
279
280         retval = kaweth_internal_control_msg(kaweth->dev,
281                                              pipe,
282                                              dr,
283                                              data,
284                                              size,
285                                              timeout);
286
287         kfree(dr);
288         return retval;
289 }
290
291 /****************************************************************
292  *     kaweth_read_configuration
293  ****************************************************************/
294 static int kaweth_read_configuration(struct kaweth_device *kaweth)
295 {
296         int retval;
297
298         retval = kaweth_control(kaweth,
299                                 usb_rcvctrlpipe(kaweth->dev, 0),
300                                 KAWETH_COMMAND_GET_ETHERNET_DESC,
301                                 USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_DEVICE,
302                                 0,
303                                 0,
304                                 (void *)&kaweth->configuration,
305                                 sizeof(kaweth->configuration),
306                                 KAWETH_CONTROL_TIMEOUT);
307
308         return retval;
309 }
310
311 /****************************************************************
312  *     kaweth_set_urb_size
313  ****************************************************************/
314 static int kaweth_set_urb_size(struct kaweth_device *kaweth, __u16 urb_size)
315 {
316         int retval;
317
318         netdev_dbg(kaweth->net, "Setting URB size to %d\n", (unsigned)urb_size);
319
320         retval = kaweth_control(kaweth,
321                                 usb_sndctrlpipe(kaweth->dev, 0),
322                                 KAWETH_COMMAND_SET_URB_SIZE,
323                                 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
324                                 urb_size,
325                                 0,
326                                 (void *)&kaweth->scratch,
327                                 0,
328                                 KAWETH_CONTROL_TIMEOUT);
329
330         return retval;
331 }
332
333 /****************************************************************
334  *     kaweth_set_sofs_wait
335  ****************************************************************/
336 static int kaweth_set_sofs_wait(struct kaweth_device *kaweth, __u16 sofs_wait)
337 {
338         int retval;
339
340         netdev_dbg(kaweth->net, "Set SOFS wait to %d\n", (unsigned)sofs_wait);
341
342         retval = kaweth_control(kaweth,
343                                 usb_sndctrlpipe(kaweth->dev, 0),
344                                 KAWETH_COMMAND_SET_SOFS_WAIT,
345                                 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
346                                 sofs_wait,
347                                 0,
348                                 (void *)&kaweth->scratch,
349                                 0,
350                                 KAWETH_CONTROL_TIMEOUT);
351
352         return retval;
353 }
354
355 /****************************************************************
356  *     kaweth_set_receive_filter
357  ****************************************************************/
358 static int kaweth_set_receive_filter(struct kaweth_device *kaweth,
359                                      __u16 receive_filter)
360 {
361         int retval;
362
363         netdev_dbg(kaweth->net, "Set receive filter to %d\n",
364                    (unsigned)receive_filter);
365
366         retval = kaweth_control(kaweth,
367                                 usb_sndctrlpipe(kaweth->dev, 0),
368                                 KAWETH_COMMAND_SET_PACKET_FILTER,
369                                 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
370                                 receive_filter,
371                                 0,
372                                 (void *)&kaweth->scratch,
373                                 0,
374                                 KAWETH_CONTROL_TIMEOUT);
375
376         return retval;
377 }
378
379 /****************************************************************
380  *     kaweth_download_firmware
381  ****************************************************************/
382 static int kaweth_download_firmware(struct kaweth_device *kaweth,
383                                     const char *fwname,
384                                     __u8 interrupt,
385                                     __u8 type)
386 {
387         const struct firmware *fw;
388         int data_len;
389         int ret;
390
391         ret = reject_firmware(&fw, fwname, &kaweth->dev->dev);
392         if (ret) {
393                 dev_err(&kaweth->intf->dev, "Firmware request failed\n");
394                 return ret;
395         }
396
397         if (fw->size > KAWETH_FIRMWARE_BUF_SIZE) {
398                 dev_err(&kaweth->intf->dev, "Firmware too big: %zu\n",
399                         fw->size);
400                 release_firmware(fw);
401                 return -ENOSPC;
402         }
403         data_len = fw->size;
404         memcpy(kaweth->firmware_buf, fw->data, fw->size);
405
406         release_firmware(fw);
407
408         kaweth->firmware_buf[2] = (data_len & 0xFF) - 7;
409         kaweth->firmware_buf[3] = data_len >> 8;
410         kaweth->firmware_buf[4] = type;
411         kaweth->firmware_buf[5] = interrupt;
412
413         netdev_dbg(kaweth->net, "High: %i, Low:%i\n", kaweth->firmware_buf[3],
414                    kaweth->firmware_buf[2]);
415
416         netdev_dbg(kaweth->net,
417                    "Downloading firmware at %p to kaweth device at %p\n",
418                    kaweth->firmware_buf, kaweth);
419         netdev_dbg(kaweth->net, "Firmware length: %d\n", data_len);
420
421         return kaweth_control(kaweth,
422                               usb_sndctrlpipe(kaweth->dev, 0),
423                               KAWETH_COMMAND_SCAN,
424                               USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
425                               0,
426                               0,
427                               (void *)kaweth->firmware_buf,
428                               data_len,
429                               KAWETH_CONTROL_TIMEOUT);
430 }
431
432 /****************************************************************
433  *     kaweth_trigger_firmware
434  ****************************************************************/
435 static int kaweth_trigger_firmware(struct kaweth_device *kaweth,
436                                    __u8 interrupt)
437 {
438         kaweth->firmware_buf[0] = 0xB6;
439         kaweth->firmware_buf[1] = 0xC3;
440         kaweth->firmware_buf[2] = 0x01;
441         kaweth->firmware_buf[3] = 0x00;
442         kaweth->firmware_buf[4] = 0x06;
443         kaweth->firmware_buf[5] = interrupt;
444         kaweth->firmware_buf[6] = 0x00;
445         kaweth->firmware_buf[7] = 0x00;
446
447         return kaweth_control(kaweth,
448                               usb_sndctrlpipe(kaweth->dev, 0),
449                               KAWETH_COMMAND_SCAN,
450                               USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
451                               0,
452                               0,
453                               (void *)kaweth->firmware_buf,
454                               8,
455                               KAWETH_CONTROL_TIMEOUT);
456 }
457
458 /****************************************************************
459  *     kaweth_reset
460  ****************************************************************/
461 static int kaweth_reset(struct kaweth_device *kaweth)
462 {
463         int result;
464
465         result = usb_reset_configuration(kaweth->dev);
466         mdelay(10);
467
468         netdev_dbg(kaweth->net, "kaweth_reset() returns %d.\n", result);
469
470         return result;
471 }
472
473 static void kaweth_usb_receive(struct urb *);
474 static int kaweth_resubmit_rx_urb(struct kaweth_device *, gfp_t);
475
476 /****************************************************************
477         int_callback
478 *****************************************************************/
479
480 static void kaweth_resubmit_int_urb(struct kaweth_device *kaweth, gfp_t mf)
481 {
482         int status;
483
484         status = usb_submit_urb (kaweth->irq_urb, mf);
485         if (unlikely(status == -ENOMEM)) {
486                 kaweth->suspend_lowmem_ctrl = 1;
487                 schedule_delayed_work(&kaweth->lowmem_work, HZ/4);
488         } else {
489                 kaweth->suspend_lowmem_ctrl = 0;
490         }
491
492         if (status)
493                 dev_err(&kaweth->intf->dev,
494                         "can't resubmit intr, %s-%s, status %d\n",
495                         kaweth->dev->bus->bus_name,
496                         kaweth->dev->devpath, status);
497 }
498
499 static void int_callback(struct urb *u)
500 {
501         struct kaweth_device *kaweth = u->context;
502         int act_state;
503         int status = u->status;
504
505         switch (status) {
506         case 0:                 /* success */
507                 break;
508         case -ECONNRESET:       /* unlink */
509         case -ENOENT:
510         case -ESHUTDOWN:
511                 return;
512         /* -EPIPE:  should clear the halt */
513         default:                /* error */
514                 goto resubmit;
515         }
516
517         /* we check the link state to report changes */
518         if (kaweth->linkstate != (act_state = ( kaweth->intbuffer[STATE_OFFSET] | STATE_MASK) >> STATE_SHIFT)) {
519                 if (act_state)
520                         netif_carrier_on(kaweth->net);
521                 else
522                         netif_carrier_off(kaweth->net);
523
524                 kaweth->linkstate = act_state;
525         }
526 resubmit:
527         kaweth_resubmit_int_urb(kaweth, GFP_ATOMIC);
528 }
529
530 static void kaweth_resubmit_tl(struct work_struct *work)
531 {
532         struct kaweth_device *kaweth =
533                 container_of(work, struct kaweth_device, lowmem_work.work);
534
535         if (IS_BLOCKED(kaweth->status))
536                 return;
537
538         if (kaweth->suspend_lowmem_rx)
539                 kaweth_resubmit_rx_urb(kaweth, GFP_NOIO);
540
541         if (kaweth->suspend_lowmem_ctrl)
542                 kaweth_resubmit_int_urb(kaweth, GFP_NOIO);
543 }
544
545
546 /****************************************************************
547  *     kaweth_resubmit_rx_urb
548  ****************************************************************/
549 static int kaweth_resubmit_rx_urb(struct kaweth_device *kaweth,
550                                                 gfp_t mem_flags)
551 {
552         int result;
553
554         usb_fill_bulk_urb(kaweth->rx_urb,
555                       kaweth->dev,
556                       usb_rcvbulkpipe(kaweth->dev, 1),
557                       kaweth->rx_buf,
558                       KAWETH_BUF_SIZE,
559                       kaweth_usb_receive,
560                       kaweth);
561         kaweth->rx_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
562         kaweth->rx_urb->transfer_dma = kaweth->rxbufferhandle;
563
564         if((result = usb_submit_urb(kaweth->rx_urb, mem_flags))) {
565                 if (result == -ENOMEM) {
566                         kaweth->suspend_lowmem_rx = 1;
567                         schedule_delayed_work(&kaweth->lowmem_work, HZ/4);
568                 }
569                 dev_err(&kaweth->intf->dev, "resubmitting rx_urb %d failed\n",
570                         result);
571         } else {
572                 kaweth->suspend_lowmem_rx = 0;
573         }
574
575         return result;
576 }
577
578 static void kaweth_async_set_rx_mode(struct kaweth_device *kaweth);
579
580 /****************************************************************
581  *     kaweth_usb_receive
582  ****************************************************************/
583 static void kaweth_usb_receive(struct urb *urb)
584 {
585         struct device *dev = &urb->dev->dev;
586         struct kaweth_device *kaweth = urb->context;
587         struct net_device *net = kaweth->net;
588         int status = urb->status;
589
590         int count = urb->actual_length;
591         int count2 = urb->transfer_buffer_length;
592
593         __u16 pkt_len = le16_to_cpup((__le16 *)kaweth->rx_buf);
594
595         struct sk_buff *skb;
596
597         if (unlikely(status == -EPIPE)) {
598                 kaweth->stats.rx_errors++;
599                 kaweth->end = 1;
600                 wake_up(&kaweth->term_wait);
601                 dev_dbg(dev, "Status was -EPIPE.\n");
602                 return;
603         }
604         if (unlikely(status == -ECONNRESET || status == -ESHUTDOWN)) {
605                 /* we are killed - set a flag and wake the disconnect handler */
606                 kaweth->end = 1;
607                 wake_up(&kaweth->term_wait);
608                 dev_dbg(dev, "Status was -ECONNRESET or -ESHUTDOWN.\n");
609                 return;
610         }
611         if (unlikely(status == -EPROTO || status == -ETIME ||
612                      status == -EILSEQ)) {
613                 kaweth->stats.rx_errors++;
614                 dev_dbg(dev, "Status was -EPROTO, -ETIME, or -EILSEQ.\n");
615                 return;
616         }
617         if (unlikely(status == -EOVERFLOW)) {
618                 kaweth->stats.rx_errors++;
619                 dev_dbg(dev, "Status was -EOVERFLOW.\n");
620         }
621         spin_lock(&kaweth->device_lock);
622         if (IS_BLOCKED(kaweth->status)) {
623                 spin_unlock(&kaweth->device_lock);
624                 return;
625         }
626         spin_unlock(&kaweth->device_lock);
627
628         if(status && status != -EREMOTEIO && count != 1) {
629                 dev_err(&kaweth->intf->dev,
630                         "%s RX status: %d count: %d packet_len: %d\n",
631                         net->name, status, count, (int)pkt_len);
632                 kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
633                 return;
634         }
635
636         if(kaweth->net && (count > 2)) {
637                 if(pkt_len > (count - 2)) {
638                         dev_err(&kaweth->intf->dev,
639                                 "Packet length too long for USB frame (pkt_len: %x, count: %x)\n",
640                                 pkt_len, count);
641                         dev_err(&kaweth->intf->dev, "Packet len & 2047: %x\n",
642                                 pkt_len & 2047);
643                         dev_err(&kaweth->intf->dev, "Count 2: %x\n", count2);
644                         kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
645                         return;
646                 }
647
648                 if(!(skb = dev_alloc_skb(pkt_len+2))) {
649                         kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
650                         return;
651                 }
652
653                 skb_reserve(skb, 2);    /* Align IP on 16 byte boundaries */
654
655                 skb_copy_to_linear_data(skb, kaweth->rx_buf + 2, pkt_len);
656
657                 skb_put(skb, pkt_len);
658
659                 skb->protocol = eth_type_trans(skb, net);
660
661                 netif_rx(skb);
662
663                 kaweth->stats.rx_packets++;
664                 kaweth->stats.rx_bytes += pkt_len;
665         }
666
667         kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
668 }
669
670 /****************************************************************
671  *     kaweth_open
672  ****************************************************************/
673 static int kaweth_open(struct net_device *net)
674 {
675         struct kaweth_device *kaweth = netdev_priv(net);
676         int res;
677
678         res = usb_autopm_get_interface(kaweth->intf);
679         if (res) {
680                 dev_err(&kaweth->intf->dev, "Interface cannot be resumed.\n");
681                 return -EIO;
682         }
683         res = kaweth_resubmit_rx_urb(kaweth, GFP_KERNEL);
684         if (res)
685                 goto err_out;
686
687         usb_fill_int_urb(
688                 kaweth->irq_urb,
689                 kaweth->dev,
690                 usb_rcvintpipe(kaweth->dev, 3),
691                 kaweth->intbuffer,
692                 INTBUFFERSIZE,
693                 int_callback,
694                 kaweth,
695                 250); /* overriding the descriptor */
696         kaweth->irq_urb->transfer_dma = kaweth->intbufferhandle;
697         kaweth->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
698
699         res = usb_submit_urb(kaweth->irq_urb, GFP_KERNEL);
700         if (res) {
701                 usb_kill_urb(kaweth->rx_urb);
702                 goto err_out;
703         }
704         kaweth->opened = 1;
705
706         netif_start_queue(net);
707
708         kaweth_async_set_rx_mode(kaweth);
709         return 0;
710
711 err_out:
712         usb_autopm_put_interface(kaweth->intf);
713         return -EIO;
714 }
715
716 /****************************************************************
717  *     kaweth_kill_urbs
718  ****************************************************************/
719 static void kaweth_kill_urbs(struct kaweth_device *kaweth)
720 {
721         usb_kill_urb(kaweth->irq_urb);
722         usb_kill_urb(kaweth->rx_urb);
723         usb_kill_urb(kaweth->tx_urb);
724
725         cancel_delayed_work_sync(&kaweth->lowmem_work);
726
727         /* a scheduled work may have resubmitted,
728            we hit them again */
729         usb_kill_urb(kaweth->irq_urb);
730         usb_kill_urb(kaweth->rx_urb);
731 }
732
733 /****************************************************************
734  *     kaweth_close
735  ****************************************************************/
736 static int kaweth_close(struct net_device *net)
737 {
738         struct kaweth_device *kaweth = netdev_priv(net);
739
740         netif_stop_queue(net);
741         kaweth->opened = 0;
742
743         kaweth->status |= KAWETH_STATUS_CLOSING;
744
745         kaweth_kill_urbs(kaweth);
746
747         kaweth->status &= ~KAWETH_STATUS_CLOSING;
748
749         usb_autopm_put_interface(kaweth->intf);
750
751         return 0;
752 }
753
754 static u32 kaweth_get_link(struct net_device *dev)
755 {
756         struct kaweth_device *kaweth = netdev_priv(dev);
757
758         return kaweth->linkstate;
759 }
760
761 static const struct ethtool_ops ops = {
762         .get_link       = kaweth_get_link
763 };
764
765 /****************************************************************
766  *     kaweth_usb_transmit_complete
767  ****************************************************************/
768 static void kaweth_usb_transmit_complete(struct urb *urb)
769 {
770         struct kaweth_device *kaweth = urb->context;
771         struct sk_buff *skb = kaweth->tx_skb;
772         int status = urb->status;
773
774         if (unlikely(status != 0))
775                 if (status != -ENOENT)
776                         dev_dbg(&urb->dev->dev, "%s: TX status %d.\n",
777                                 kaweth->net->name, status);
778
779         netif_wake_queue(kaweth->net);
780         dev_kfree_skb_irq(skb);
781 }
782
783 /****************************************************************
784  *     kaweth_start_xmit
785  ****************************************************************/
786 static netdev_tx_t kaweth_start_xmit(struct sk_buff *skb,
787                                            struct net_device *net)
788 {
789         struct kaweth_device *kaweth = netdev_priv(net);
790         __le16 *private_header;
791
792         int res;
793
794         spin_lock_irq(&kaweth->device_lock);
795
796         kaweth_async_set_rx_mode(kaweth);
797         netif_stop_queue(net);
798         if (IS_BLOCKED(kaweth->status)) {
799                 goto skip;
800         }
801
802         /* We now decide whether we can put our special header into the sk_buff */
803         if (skb_cloned(skb) || skb_headroom(skb) < 2) {
804                 /* no such luck - we make our own */
805                 struct sk_buff *copied_skb;
806                 copied_skb = skb_copy_expand(skb, 2, 0, GFP_ATOMIC);
807                 dev_kfree_skb_irq(skb);
808                 skb = copied_skb;
809                 if (!copied_skb) {
810                         kaweth->stats.tx_errors++;
811                         netif_start_queue(net);
812                         spin_unlock_irq(&kaweth->device_lock);
813                         return NETDEV_TX_OK;
814                 }
815         }
816
817         private_header = (__le16 *)__skb_push(skb, 2);
818         *private_header = cpu_to_le16(skb->len-2);
819         kaweth->tx_skb = skb;
820
821         usb_fill_bulk_urb(kaweth->tx_urb,
822                       kaweth->dev,
823                       usb_sndbulkpipe(kaweth->dev, 2),
824                       private_header,
825                       skb->len,
826                       kaweth_usb_transmit_complete,
827                       kaweth);
828         kaweth->end = 0;
829
830         if((res = usb_submit_urb(kaweth->tx_urb, GFP_ATOMIC)))
831         {
832                 dev_warn(&net->dev, "kaweth failed tx_urb %d\n", res);
833 skip:
834                 kaweth->stats.tx_errors++;
835
836                 netif_start_queue(net);
837                 dev_kfree_skb_irq(skb);
838         }
839         else
840         {
841                 kaweth->stats.tx_packets++;
842                 kaweth->stats.tx_bytes += skb->len;
843         }
844
845         spin_unlock_irq(&kaweth->device_lock);
846
847         return NETDEV_TX_OK;
848 }
849
850 /****************************************************************
851  *     kaweth_set_rx_mode
852  ****************************************************************/
853 static void kaweth_set_rx_mode(struct net_device *net)
854 {
855         struct kaweth_device *kaweth = netdev_priv(net);
856
857         __u16 packet_filter_bitmap = KAWETH_PACKET_FILTER_DIRECTED |
858                                      KAWETH_PACKET_FILTER_BROADCAST |
859                                      KAWETH_PACKET_FILTER_MULTICAST;
860
861         netdev_dbg(net, "Setting Rx mode to %d\n", packet_filter_bitmap);
862
863         netif_stop_queue(net);
864
865         if (net->flags & IFF_PROMISC) {
866                 packet_filter_bitmap |= KAWETH_PACKET_FILTER_PROMISCUOUS;
867         }
868         else if (!netdev_mc_empty(net) || (net->flags & IFF_ALLMULTI)) {
869                 packet_filter_bitmap |= KAWETH_PACKET_FILTER_ALL_MULTICAST;
870         }
871
872         kaweth->packet_filter_bitmap = packet_filter_bitmap;
873         netif_wake_queue(net);
874 }
875
876 /****************************************************************
877  *     kaweth_async_set_rx_mode
878  ****************************************************************/
879 static void kaweth_async_set_rx_mode(struct kaweth_device *kaweth)
880 {
881         int result;
882         __u16 packet_filter_bitmap = kaweth->packet_filter_bitmap;
883
884         kaweth->packet_filter_bitmap = 0;
885         if (packet_filter_bitmap == 0)
886                 return;
887
888         if (in_interrupt())
889                 return;
890
891         result = kaweth_control(kaweth,
892                                 usb_sndctrlpipe(kaweth->dev, 0),
893                                 KAWETH_COMMAND_SET_PACKET_FILTER,
894                                 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
895                                 packet_filter_bitmap,
896                                 0,
897                                 (void *)&kaweth->scratch,
898                                 0,
899                                 KAWETH_CONTROL_TIMEOUT);
900
901         if(result < 0) {
902                 dev_err(&kaweth->intf->dev, "Failed to set Rx mode: %d\n",
903                         result);
904         }
905         else {
906                 netdev_dbg(kaweth->net, "Set Rx mode to %d\n",
907                            packet_filter_bitmap);
908         }
909 }
910
911 /****************************************************************
912  *     kaweth_netdev_stats
913  ****************************************************************/
914 static struct net_device_stats *kaweth_netdev_stats(struct net_device *dev)
915 {
916         struct kaweth_device *kaweth = netdev_priv(dev);
917         return &kaweth->stats;
918 }
919
920 /****************************************************************
921  *     kaweth_tx_timeout
922  ****************************************************************/
923 static void kaweth_tx_timeout(struct net_device *net)
924 {
925         struct kaweth_device *kaweth = netdev_priv(net);
926
927         dev_warn(&net->dev, "%s: Tx timed out. Resetting.\n", net->name);
928         kaweth->stats.tx_errors++;
929         netif_trans_update(net);
930
931         usb_unlink_urb(kaweth->tx_urb);
932 }
933
934 /****************************************************************
935  *     kaweth_suspend
936  ****************************************************************/
937 static int kaweth_suspend(struct usb_interface *intf, pm_message_t message)
938 {
939         struct kaweth_device *kaweth = usb_get_intfdata(intf);
940         unsigned long flags;
941
942         spin_lock_irqsave(&kaweth->device_lock, flags);
943         kaweth->status |= KAWETH_STATUS_SUSPENDING;
944         spin_unlock_irqrestore(&kaweth->device_lock, flags);
945
946         kaweth_kill_urbs(kaweth);
947         return 0;
948 }
949
950 /****************************************************************
951  *     kaweth_resume
952  ****************************************************************/
953 static int kaweth_resume(struct usb_interface *intf)
954 {
955         struct kaweth_device *kaweth = usb_get_intfdata(intf);
956         unsigned long flags;
957
958         spin_lock_irqsave(&kaweth->device_lock, flags);
959         kaweth->status &= ~KAWETH_STATUS_SUSPENDING;
960         spin_unlock_irqrestore(&kaweth->device_lock, flags);
961
962         if (!kaweth->opened)
963                 return 0;
964         kaweth_resubmit_rx_urb(kaweth, GFP_NOIO);
965         kaweth_resubmit_int_urb(kaweth, GFP_NOIO);
966
967         return 0;
968 }
969
970 /****************************************************************
971  *     kaweth_probe
972  ****************************************************************/
973
974
975 static const struct net_device_ops kaweth_netdev_ops = {
976         .ndo_open =                     kaweth_open,
977         .ndo_stop =                     kaweth_close,
978         .ndo_start_xmit =               kaweth_start_xmit,
979         .ndo_tx_timeout =               kaweth_tx_timeout,
980         .ndo_set_rx_mode =              kaweth_set_rx_mode,
981         .ndo_get_stats =                kaweth_netdev_stats,
982         .ndo_change_mtu =               eth_change_mtu,
983         .ndo_set_mac_address =          eth_mac_addr,
984         .ndo_validate_addr =            eth_validate_addr,
985 };
986
987 static int kaweth_probe(
988                 struct usb_interface *intf,
989                 const struct usb_device_id *id      /* from id_table */
990         )
991 {
992         struct device *dev = &intf->dev;
993         struct usb_device *udev = 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         int rv = -EIO;
999
1000         dev_dbg(dev,
1001                 "Kawasaki Device Probe (Device number:%d): 0x%4.4x:0x%4.4x:0x%4.4x\n",
1002                 udev->devnum, le16_to_cpu(udev->descriptor.idVendor),
1003                 le16_to_cpu(udev->descriptor.idProduct),
1004                 le16_to_cpu(udev->descriptor.bcdDevice));
1005
1006         dev_dbg(dev, "Device at %p\n", udev);
1007
1008         dev_dbg(dev, "Descriptor length: %x type: %x\n",
1009                 (int)udev->descriptor.bLength,
1010                 (int)udev->descriptor.bDescriptorType);
1011
1012         netdev = alloc_etherdev(sizeof(*kaweth));
1013         if (!netdev)
1014                 return -ENOMEM;
1015
1016         kaweth = netdev_priv(netdev);
1017         kaweth->dev = udev;
1018         kaweth->net = netdev;
1019         kaweth->intf = intf;
1020
1021         spin_lock_init(&kaweth->device_lock);
1022         init_waitqueue_head(&kaweth->term_wait);
1023
1024         dev_dbg(dev, "Resetting.\n");
1025
1026         kaweth_reset(kaweth);
1027
1028         /*
1029          * If high byte of bcdDevice is nonzero, firmware is already
1030          * downloaded. Don't try to do it again, or we'll hang the device.
1031          */
1032
1033         if (le16_to_cpu(udev->descriptor.bcdDevice) >> 8) {
1034                 dev_info(dev, "Firmware present in device.\n");
1035         } else {
1036                 /* Download the firmware */
1037                 dev_info(dev, "Downloading firmware...\n");
1038                 kaweth->firmware_buf = (__u8 *)__get_free_page(GFP_KERNEL);
1039                 if (!kaweth->firmware_buf) {
1040                         rv = -ENOMEM;
1041                         goto err_free_netdev;
1042                 }
1043                 if ((result = kaweth_download_firmware(kaweth,
1044                                                       "/*(DEBLOBBED)*/",
1045                                                       100,
1046                                                       2)) < 0) {
1047                         dev_err(dev, "Error downloading firmware (%d)\n",
1048                                 result);
1049                         goto err_fw;
1050                 }
1051
1052                 if ((result = kaweth_download_firmware(kaweth,
1053                                                       "/*(DEBLOBBED)*/",
1054                                                       100,
1055                                                       3)) < 0) {
1056                         dev_err(dev, "Error downloading firmware fix (%d)\n",
1057                                 result);
1058                         goto err_fw;
1059                 }
1060
1061                 if ((result = kaweth_download_firmware(kaweth,
1062                                                       "/*(DEBLOBBED)*/",
1063                                                       126,
1064                                                       2)) < 0) {
1065                         dev_err(dev, "Error downloading trigger code (%d)\n",
1066                                 result);
1067                         goto err_fw;
1068
1069                 }
1070
1071                 if ((result = kaweth_download_firmware(kaweth,
1072                                                       "/*(DEBLOBBED)*/",
1073                                                       126,
1074                                                       3)) < 0) {
1075                         dev_err(dev, "Error downloading trigger code fix (%d)\n", result);
1076                         goto err_fw;
1077                 }
1078
1079
1080                 if ((result = kaweth_trigger_firmware(kaweth, 126)) < 0) {
1081                         dev_err(dev, "Error triggering firmware (%d)\n", result);
1082                         goto err_fw;
1083                 }
1084
1085                 /* Device will now disappear for a moment...  */
1086                 dev_info(dev, "Firmware loaded.  I'll be back...\n");
1087 err_fw:
1088                 free_page((unsigned long)kaweth->firmware_buf);
1089                 free_netdev(netdev);
1090                 return -EIO;
1091         }
1092
1093         result = kaweth_read_configuration(kaweth);
1094
1095         if(result < 0) {
1096                 dev_err(dev, "Error reading configuration (%d), no net device created\n", result);
1097                 goto err_free_netdev;
1098         }
1099
1100         dev_info(dev, "Statistics collection: %x\n", kaweth->configuration.statistics_mask);
1101         dev_info(dev, "Multicast filter limit: %x\n", kaweth->configuration.max_multicast_filters & ((1 << 15) - 1));
1102         dev_info(dev, "MTU: %d\n", le16_to_cpu(kaweth->configuration.segment_size));
1103         dev_info(dev, "Read MAC address %pM\n", kaweth->configuration.hw_addr);
1104
1105         if(!memcmp(&kaweth->configuration.hw_addr,
1106                    &bcast_addr,
1107                    sizeof(bcast_addr))) {
1108                 dev_err(dev, "Firmware not functioning properly, no net device created\n");
1109                 goto err_free_netdev;
1110         }
1111
1112         if(kaweth_set_urb_size(kaweth, KAWETH_BUF_SIZE) < 0) {
1113                 dev_dbg(dev, "Error setting URB size\n");
1114                 goto err_free_netdev;
1115         }
1116
1117         if(kaweth_set_sofs_wait(kaweth, KAWETH_SOFS_TO_WAIT) < 0) {
1118                 dev_err(dev, "Error setting SOFS wait\n");
1119                 goto err_free_netdev;
1120         }
1121
1122         result = kaweth_set_receive_filter(kaweth,
1123                                            KAWETH_PACKET_FILTER_DIRECTED |
1124                                            KAWETH_PACKET_FILTER_BROADCAST |
1125                                            KAWETH_PACKET_FILTER_MULTICAST);
1126
1127         if(result < 0) {
1128                 dev_err(dev, "Error setting receive filter\n");
1129                 goto err_free_netdev;
1130         }
1131
1132         dev_dbg(dev, "Initializing net device.\n");
1133
1134         kaweth->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
1135         if (!kaweth->tx_urb)
1136                 goto err_free_netdev;
1137         kaweth->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
1138         if (!kaweth->rx_urb)
1139                 goto err_only_tx;
1140         kaweth->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
1141         if (!kaweth->irq_urb)
1142                 goto err_tx_and_rx;
1143
1144         kaweth->intbuffer = usb_alloc_coherent( kaweth->dev,
1145                                                 INTBUFFERSIZE,
1146                                                 GFP_KERNEL,
1147                                                 &kaweth->intbufferhandle);
1148         if (!kaweth->intbuffer)
1149                 goto err_tx_and_rx_and_irq;
1150         kaweth->rx_buf = usb_alloc_coherent(    kaweth->dev,
1151                                                 KAWETH_BUF_SIZE,
1152                                                 GFP_KERNEL,
1153                                                 &kaweth->rxbufferhandle);
1154         if (!kaweth->rx_buf)
1155                 goto err_all_but_rxbuf;
1156
1157         memcpy(netdev->broadcast, &bcast_addr, sizeof(bcast_addr));
1158         memcpy(netdev->dev_addr, &kaweth->configuration.hw_addr,
1159                sizeof(kaweth->configuration.hw_addr));
1160
1161         netdev->netdev_ops = &kaweth_netdev_ops;
1162         netdev->watchdog_timeo = KAWETH_TX_TIMEOUT;
1163         netdev->mtu = le16_to_cpu(kaweth->configuration.segment_size);
1164         netdev->ethtool_ops = &ops;
1165
1166         /* kaweth is zeroed as part of alloc_netdev */
1167         INIT_DELAYED_WORK(&kaweth->lowmem_work, kaweth_resubmit_tl);
1168         usb_set_intfdata(intf, kaweth);
1169
1170         SET_NETDEV_DEV(netdev, dev);
1171         if (register_netdev(netdev) != 0) {
1172                 dev_err(dev, "Error registering netdev.\n");
1173                 goto err_intfdata;
1174         }
1175
1176         dev_info(dev, "kaweth interface created at %s\n",
1177                  kaweth->net->name);
1178
1179         return 0;
1180
1181 err_intfdata:
1182         usb_set_intfdata(intf, NULL);
1183         usb_free_coherent(kaweth->dev, KAWETH_BUF_SIZE, (void *)kaweth->rx_buf, kaweth->rxbufferhandle);
1184 err_all_but_rxbuf:
1185         usb_free_coherent(kaweth->dev, INTBUFFERSIZE, (void *)kaweth->intbuffer, kaweth->intbufferhandle);
1186 err_tx_and_rx_and_irq:
1187         usb_free_urb(kaweth->irq_urb);
1188 err_tx_and_rx:
1189         usb_free_urb(kaweth->rx_urb);
1190 err_only_tx:
1191         usb_free_urb(kaweth->tx_urb);
1192 err_free_netdev:
1193         free_netdev(netdev);
1194
1195         return rv;
1196 }
1197
1198 /****************************************************************
1199  *     kaweth_disconnect
1200  ****************************************************************/
1201 static void kaweth_disconnect(struct usb_interface *intf)
1202 {
1203         struct kaweth_device *kaweth = usb_get_intfdata(intf);
1204         struct net_device *netdev;
1205
1206         usb_set_intfdata(intf, NULL);
1207         if (!kaweth) {
1208                 dev_warn(&intf->dev, "unregistering non-existent device\n");
1209                 return;
1210         }
1211         netdev = kaweth->net;
1212
1213         netdev_dbg(kaweth->net, "Unregistering net device\n");
1214         unregister_netdev(netdev);
1215
1216         usb_free_urb(kaweth->rx_urb);
1217         usb_free_urb(kaweth->tx_urb);
1218         usb_free_urb(kaweth->irq_urb);
1219
1220         usb_free_coherent(kaweth->dev, KAWETH_BUF_SIZE, (void *)kaweth->rx_buf, kaweth->rxbufferhandle);
1221         usb_free_coherent(kaweth->dev, INTBUFFERSIZE, (void *)kaweth->intbuffer, kaweth->intbufferhandle);
1222
1223         free_netdev(netdev);
1224 }
1225
1226
1227 // FIXME this completion stuff is a modified clone of
1228 // an OLD version of some stuff in usb.c ...
1229 struct usb_api_data {
1230         wait_queue_head_t wqh;
1231         int done;
1232 };
1233
1234 /*-------------------------------------------------------------------*
1235  * completion handler for compatibility wrappers (sync control/bulk) *
1236  *-------------------------------------------------------------------*/
1237 static void usb_api_blocking_completion(struct urb *urb)
1238 {
1239         struct usb_api_data *awd = (struct usb_api_data *)urb->context;
1240
1241         awd->done=1;
1242         wake_up(&awd->wqh);
1243 }
1244
1245 /*-------------------------------------------------------------------*
1246  *                         COMPATIBILITY STUFF                       *
1247  *-------------------------------------------------------------------*/
1248
1249 // Starts urb and waits for completion or timeout
1250 static int usb_start_wait_urb(struct urb *urb, int timeout, int* actual_length)
1251 {
1252         struct usb_api_data awd;
1253         int status;
1254
1255         init_waitqueue_head(&awd.wqh);
1256         awd.done = 0;
1257
1258         urb->context = &awd;
1259         status = usb_submit_urb(urb, GFP_ATOMIC);
1260         if (status) {
1261                 // something went wrong
1262                 usb_free_urb(urb);
1263                 return status;
1264         }
1265
1266         if (!wait_event_timeout(awd.wqh, awd.done, timeout)) {
1267                 // timeout
1268                 dev_warn(&urb->dev->dev, "usb_control/bulk_msg: timeout\n");
1269                 usb_kill_urb(urb);  // remove urb safely
1270                 status = -ETIMEDOUT;
1271         }
1272         else {
1273                 status = urb->status;
1274         }
1275
1276         if (actual_length) {
1277                 *actual_length = urb->actual_length;
1278         }
1279
1280         usb_free_urb(urb);
1281         return status;
1282 }
1283
1284 /*-------------------------------------------------------------------*/
1285 // returns status (negative) or length (positive)
1286 static int kaweth_internal_control_msg(struct usb_device *usb_dev,
1287                                        unsigned int pipe,
1288                                        struct usb_ctrlrequest *cmd, void *data,
1289                                        int len, int timeout)
1290 {
1291         struct urb *urb;
1292         int retv;
1293         int length = 0; /* shut up GCC */
1294
1295         urb = usb_alloc_urb(0, GFP_ATOMIC);
1296         if (!urb)
1297                 return -ENOMEM;
1298
1299         usb_fill_control_urb(urb, usb_dev, pipe, (unsigned char*)cmd, data,
1300                          len, usb_api_blocking_completion, NULL);
1301
1302         retv = usb_start_wait_urb(urb, timeout, &length);
1303         if (retv < 0) {
1304                 return retv;
1305         }
1306         else {
1307                 return length;
1308         }
1309 }
1310
1311 module_usb_driver(kaweth_driver);