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