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