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