Merge master.kernel.org:/pub/scm/linux/kernel/git/gregkh/usb-2.6
[linux-2.6] / 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 work_struct 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(void *d)
534 {
535         struct kaweth_device *kaweth = (struct kaweth_device *)d;
536
537         if (IS_BLOCKED(kaweth->status))
538                 return;
539
540         if (kaweth->suspend_lowmem_rx)
541                 kaweth_resubmit_rx_urb(kaweth, GFP_NOIO);
542
543         if (kaweth->suspend_lowmem_ctrl)
544                 kaweth_resubmit_int_urb(kaweth, GFP_NOIO);
545 }
546
547
548 /****************************************************************
549  *     kaweth_resubmit_rx_urb
550  ****************************************************************/
551 static int kaweth_resubmit_rx_urb(struct kaweth_device *kaweth,
552                                                 gfp_t mem_flags)
553 {
554         int result;
555
556         usb_fill_bulk_urb(kaweth->rx_urb,
557                       kaweth->dev,
558                       usb_rcvbulkpipe(kaweth->dev, 1),
559                       kaweth->rx_buf,
560                       KAWETH_BUF_SIZE,
561                       kaweth_usb_receive,
562                       kaweth);
563         kaweth->rx_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
564         kaweth->rx_urb->transfer_dma = kaweth->rxbufferhandle;
565
566         if((result = usb_submit_urb(kaweth->rx_urb, mem_flags))) {
567                 if (result == -ENOMEM) {
568                         kaweth->suspend_lowmem_rx = 1;
569                         schedule_delayed_work(&kaweth->lowmem_work, HZ/4);
570                 }
571                 err("resubmitting rx_urb %d failed", result);
572         } else {
573                 kaweth->suspend_lowmem_rx = 0;
574         }
575
576         return result;
577 }
578
579 static void kaweth_async_set_rx_mode(struct kaweth_device *kaweth);
580
581 /****************************************************************
582  *     kaweth_usb_receive
583  ****************************************************************/
584 static void kaweth_usb_receive(struct urb *urb)
585 {
586         struct kaweth_device *kaweth = urb->context;
587         struct net_device *net = kaweth->net;
588
589         int count = urb->actual_length;
590         int count2 = urb->transfer_buffer_length;
591
592         __u16 pkt_len = le16_to_cpup((__le16 *)kaweth->rx_buf);
593
594         struct sk_buff *skb;
595
596         if(unlikely(urb->status == -ECONNRESET || urb->status == -ESHUTDOWN))
597         /* we are killed - set a flag and wake the disconnect handler */
598         {
599                 kaweth->end = 1;
600                 wake_up(&kaweth->term_wait);
601                 return;
602         }
603
604         spin_lock(&kaweth->device_lock);
605         if (IS_BLOCKED(kaweth->status)) {
606                 spin_unlock(&kaweth->device_lock);
607                 return;
608         }
609         spin_unlock(&kaweth->device_lock);
610
611         if(urb->status && urb->status != -EREMOTEIO && count != 1) {
612                 err("%s RX status: %d count: %d packet_len: %d",
613                            net->name,
614                            urb->status,
615                            count,
616                            (int)pkt_len);
617                 kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
618                 return;
619         }
620
621         if(kaweth->net && (count > 2)) {
622                 if(pkt_len > (count - 2)) {
623                         err("Packet length too long for USB frame (pkt_len: %x, count: %x)",pkt_len, count);
624                         err("Packet len & 2047: %x", pkt_len & 2047);
625                         err("Count 2: %x", count2);
626                         kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
627                         return;
628                 }
629
630                 if(!(skb = dev_alloc_skb(pkt_len+2))) {
631                         kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
632                         return;
633                 }
634
635                 skb_reserve(skb, 2);    /* Align IP on 16 byte boundaries */
636
637                 skb->dev = net;
638
639                 eth_copy_and_sum(skb, kaweth->rx_buf + 2, pkt_len, 0);
640
641                 skb_put(skb, pkt_len);
642
643                 skb->protocol = eth_type_trans(skb, net);
644
645                 netif_rx(skb);
646
647                 kaweth->stats.rx_packets++;
648                 kaweth->stats.rx_bytes += pkt_len;
649         }
650
651         kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
652 }
653
654 /****************************************************************
655  *     kaweth_open
656  ****************************************************************/
657 static int kaweth_open(struct net_device *net)
658 {
659         struct kaweth_device *kaweth = netdev_priv(net);
660         int res;
661
662         dbg("Opening network device.");
663
664         res = kaweth_resubmit_rx_urb(kaweth, GFP_KERNEL);
665         if (res)
666                 return -EIO;
667
668         usb_fill_int_urb(
669                 kaweth->irq_urb,
670                 kaweth->dev,
671                 usb_rcvintpipe(kaweth->dev, 3),
672                 kaweth->intbuffer,
673                 INTBUFFERSIZE,
674                 int_callback,
675                 kaweth,
676                 250); /* overriding the descriptor */
677         kaweth->irq_urb->transfer_dma = kaweth->intbufferhandle;
678         kaweth->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
679
680         res = usb_submit_urb(kaweth->irq_urb, GFP_KERNEL);
681         if (res) {
682                 usb_kill_urb(kaweth->rx_urb);
683                 return -EIO;
684         }
685         kaweth->opened = 1;
686
687         netif_start_queue(net);
688
689         kaweth_async_set_rx_mode(kaweth);
690         return 0;
691 }
692
693 /****************************************************************
694  *     kaweth_close
695  ****************************************************************/
696 static void kaweth_kill_urbs(struct kaweth_device *kaweth)
697 {
698         usb_kill_urb(kaweth->irq_urb);
699         usb_kill_urb(kaweth->rx_urb);
700         usb_kill_urb(kaweth->tx_urb);
701
702         flush_scheduled_work();
703
704         /* a scheduled work may have resubmitted,
705            we hit them again */
706         usb_kill_urb(kaweth->irq_urb);
707         usb_kill_urb(kaweth->rx_urb);
708 }
709
710 /****************************************************************
711  *     kaweth_close
712  ****************************************************************/
713 static int kaweth_close(struct net_device *net)
714 {
715         struct kaweth_device *kaweth = netdev_priv(net);
716
717         netif_stop_queue(net);
718         kaweth->opened = 0;
719
720         kaweth->status |= KAWETH_STATUS_CLOSING;
721
722         kaweth_kill_urbs(kaweth);
723
724         kaweth->status &= ~KAWETH_STATUS_CLOSING;
725
726         return 0;
727 }
728
729 static void kaweth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
730 {
731
732         strlcpy(info->driver, driver_name, sizeof(info->driver));
733 }
734
735 static struct ethtool_ops ops = {
736         .get_drvinfo = kaweth_get_drvinfo
737 };
738
739 /****************************************************************
740  *     kaweth_usb_transmit_complete
741  ****************************************************************/
742 static void kaweth_usb_transmit_complete(struct urb *urb)
743 {
744         struct kaweth_device *kaweth = urb->context;
745         struct sk_buff *skb = kaweth->tx_skb;
746
747         if (unlikely(urb->status != 0))
748                 if (urb->status != -ENOENT)
749                         dbg("%s: TX status %d.", kaweth->net->name, urb->status);
750
751         netif_wake_queue(kaweth->net);
752         dev_kfree_skb_irq(skb);
753 }
754
755 /****************************************************************
756  *     kaweth_start_xmit
757  ****************************************************************/
758 static int kaweth_start_xmit(struct sk_buff *skb, struct net_device *net)
759 {
760         struct kaweth_device *kaweth = netdev_priv(net);
761         __le16 *private_header;
762
763         int res;
764
765         spin_lock(&kaweth->device_lock);
766
767         kaweth_async_set_rx_mode(kaweth);
768         netif_stop_queue(net);
769         if (IS_BLOCKED(kaweth->status)) {
770                 goto skip;
771         }
772
773         /* We now decide whether we can put our special header into the sk_buff */
774         if (skb_cloned(skb) || skb_headroom(skb) < 2) {
775                 /* no such luck - we make our own */
776                 struct sk_buff *copied_skb;
777                 copied_skb = skb_copy_expand(skb, 2, 0, GFP_ATOMIC);
778                 dev_kfree_skb_irq(skb);
779                 skb = copied_skb;
780                 if (!copied_skb) {
781                         kaweth->stats.tx_errors++;
782                         netif_start_queue(net);
783                         spin_unlock(&kaweth->device_lock);
784                         return 0;
785                 }
786         }
787
788         private_header = (__le16 *)__skb_push(skb, 2);
789         *private_header = cpu_to_le16(skb->len-2);
790         kaweth->tx_skb = skb;
791
792         usb_fill_bulk_urb(kaweth->tx_urb,
793                       kaweth->dev,
794                       usb_sndbulkpipe(kaweth->dev, 2),
795                       private_header,
796                       skb->len,
797                       kaweth_usb_transmit_complete,
798                       kaweth);
799         kaweth->end = 0;
800
801         if((res = usb_submit_urb(kaweth->tx_urb, GFP_ATOMIC)))
802         {
803                 warn("kaweth failed tx_urb %d", res);
804 skip:
805                 kaweth->stats.tx_errors++;
806
807                 netif_start_queue(net);
808                 dev_kfree_skb_irq(skb);
809         }
810         else
811         {
812                 kaweth->stats.tx_packets++;
813                 kaweth->stats.tx_bytes += skb->len;
814                 net->trans_start = jiffies;
815         }
816
817         spin_unlock(&kaweth->device_lock);
818
819         return 0;
820 }
821
822 /****************************************************************
823  *     kaweth_set_rx_mode
824  ****************************************************************/
825 static void kaweth_set_rx_mode(struct net_device *net)
826 {
827         struct kaweth_device *kaweth = netdev_priv(net);
828
829         __u16 packet_filter_bitmap = KAWETH_PACKET_FILTER_DIRECTED |
830                                      KAWETH_PACKET_FILTER_BROADCAST |
831                                      KAWETH_PACKET_FILTER_MULTICAST;
832
833         dbg("Setting Rx mode to %d", packet_filter_bitmap);
834
835         netif_stop_queue(net);
836
837         if (net->flags & IFF_PROMISC) {
838                 packet_filter_bitmap |= KAWETH_PACKET_FILTER_PROMISCUOUS;
839         }
840         else if ((net->mc_count) || (net->flags & IFF_ALLMULTI)) {
841                 packet_filter_bitmap |= KAWETH_PACKET_FILTER_ALL_MULTICAST;
842         }
843
844         kaweth->packet_filter_bitmap = packet_filter_bitmap;
845         netif_wake_queue(net);
846 }
847
848 /****************************************************************
849  *     kaweth_async_set_rx_mode
850  ****************************************************************/
851 static void kaweth_async_set_rx_mode(struct kaweth_device *kaweth)
852 {
853         __u16 packet_filter_bitmap = kaweth->packet_filter_bitmap;
854         kaweth->packet_filter_bitmap = 0;
855         if (packet_filter_bitmap == 0)
856                 return;
857
858         {
859         int result;
860         result = kaweth_control(kaweth,
861                                 usb_sndctrlpipe(kaweth->dev, 0),
862                                 KAWETH_COMMAND_SET_PACKET_FILTER,
863                                 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
864                                 packet_filter_bitmap,
865                                 0,
866                                 (void *)&kaweth->scratch,
867                                 0,
868                                 KAWETH_CONTROL_TIMEOUT);
869
870         if(result < 0) {
871                 err("Failed to set Rx mode: %d", result);
872         }
873         else {
874                 dbg("Set Rx mode to %d", packet_filter_bitmap);
875         }
876         }
877 }
878
879 /****************************************************************
880  *     kaweth_netdev_stats
881  ****************************************************************/
882 static struct net_device_stats *kaweth_netdev_stats(struct net_device *dev)
883 {
884         struct kaweth_device *kaweth = netdev_priv(dev);
885         return &kaweth->stats;
886 }
887
888 /****************************************************************
889  *     kaweth_tx_timeout
890  ****************************************************************/
891 static void kaweth_tx_timeout(struct net_device *net)
892 {
893         struct kaweth_device *kaweth = netdev_priv(net);
894
895         warn("%s: Tx timed out. Resetting.", net->name);
896         kaweth->stats.tx_errors++;
897         net->trans_start = jiffies;
898
899         usb_unlink_urb(kaweth->tx_urb);
900 }
901
902 /****************************************************************
903  *     kaweth_suspend
904  ****************************************************************/
905 static int kaweth_suspend(struct usb_interface *intf, pm_message_t message)
906 {
907         struct kaweth_device *kaweth = usb_get_intfdata(intf);
908         unsigned long flags;
909
910         spin_lock_irqsave(&kaweth->device_lock, flags);
911         kaweth->status |= KAWETH_STATUS_SUSPENDING;
912         spin_unlock_irqrestore(&kaweth->device_lock, flags);
913
914         kaweth_kill_urbs(kaweth);
915         return 0;
916 }
917
918 /****************************************************************
919  *     kaweth_resume
920  ****************************************************************/
921 static int kaweth_resume(struct usb_interface *intf)
922 {
923         struct kaweth_device *kaweth = usb_get_intfdata(intf);
924         unsigned long flags;
925
926         spin_lock_irqsave(&kaweth->device_lock, flags);
927         kaweth->status &= ~KAWETH_STATUS_SUSPENDING;
928         spin_unlock_irqrestore(&kaweth->device_lock, flags);
929
930         if (!kaweth->opened)
931                 return 0;
932         kaweth_resubmit_rx_urb(kaweth, GFP_NOIO);
933         kaweth_resubmit_int_urb(kaweth, GFP_NOIO);
934
935         return 0;
936 }
937
938 /****************************************************************
939  *     kaweth_probe
940  ****************************************************************/
941 static int kaweth_probe(
942                 struct usb_interface *intf,
943                 const struct usb_device_id *id      /* from id_table */
944         )
945 {
946         struct usb_device *dev = interface_to_usbdev(intf);
947         struct kaweth_device *kaweth;
948         struct net_device *netdev;
949         const eth_addr_t bcast_addr = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
950         int result = 0;
951
952         dbg("Kawasaki Device Probe (Device number:%d): 0x%4.4x:0x%4.4x:0x%4.4x",
953                  dev->devnum,
954                  le16_to_cpu(dev->descriptor.idVendor),
955                  le16_to_cpu(dev->descriptor.idProduct),
956                  le16_to_cpu(dev->descriptor.bcdDevice));
957
958         dbg("Device at %p", dev);
959
960         dbg("Descriptor length: %x type: %x",
961                  (int)dev->descriptor.bLength,
962                  (int)dev->descriptor.bDescriptorType);
963
964         netdev = alloc_etherdev(sizeof(*kaweth));
965         if (!netdev)
966                 return -ENOMEM;
967
968         kaweth = netdev_priv(netdev);
969         kaweth->dev = dev;
970         kaweth->net = netdev;
971
972         spin_lock_init(&kaweth->device_lock);
973         init_waitqueue_head(&kaweth->term_wait);
974
975         dbg("Resetting.");
976
977         kaweth_reset(kaweth);
978
979         /*
980          * If high byte of bcdDevice is nonzero, firmware is already
981          * downloaded. Don't try to do it again, or we'll hang the device.
982          */
983
984         if (le16_to_cpu(dev->descriptor.bcdDevice) >> 8) {
985                 info("Firmware present in device.");
986         } else {
987                 /* Download the firmware */
988                 info("Downloading firmware...");
989                 kaweth->firmware_buf = (__u8 *)__get_free_page(GFP_KERNEL);
990                 if ((result = kaweth_download_firmware(kaweth,
991                                                       kaweth_new_code,
992                                                       len_kaweth_new_code,
993                                                       100,
994                                                       2)) < 0) {
995                         err("Error downloading firmware (%d)", result);
996                         goto err_fw;
997                 }
998
999                 if ((result = kaweth_download_firmware(kaweth,
1000                                                       kaweth_new_code_fix,
1001                                                       len_kaweth_new_code_fix,
1002                                                       100,
1003                                                       3)) < 0) {
1004                         err("Error downloading firmware fix (%d)", result);
1005                         goto err_fw;
1006                 }
1007
1008                 if ((result = kaweth_download_firmware(kaweth,
1009                                                       kaweth_trigger_code,
1010                                                       len_kaweth_trigger_code,
1011                                                       126,
1012                                                       2)) < 0) {
1013                         err("Error downloading trigger code (%d)", result);
1014                         goto err_fw;
1015
1016                 }
1017
1018                 if ((result = kaweth_download_firmware(kaweth,
1019                                                       kaweth_trigger_code_fix,
1020                                                       len_kaweth_trigger_code_fix,
1021                                                       126,
1022                                                       3)) < 0) {
1023                         err("Error downloading trigger code fix (%d)", result);
1024                         goto err_fw;
1025                 }
1026
1027
1028                 if ((result = kaweth_trigger_firmware(kaweth, 126)) < 0) {
1029                         err("Error triggering firmware (%d)", result);
1030                         goto err_fw;
1031                 }
1032
1033                 /* Device will now disappear for a moment...  */
1034                 info("Firmware loaded.  I'll be back...");
1035 err_fw:
1036                 free_page((unsigned long)kaweth->firmware_buf);
1037                 free_netdev(netdev);
1038                 return -EIO;
1039         }
1040
1041         result = kaweth_read_configuration(kaweth);
1042
1043         if(result < 0) {
1044                 err("Error reading configuration (%d), no net device created", result);
1045                 goto err_free_netdev;
1046         }
1047
1048         info("Statistics collection: %x", kaweth->configuration.statistics_mask);
1049         info("Multicast filter limit: %x", kaweth->configuration.max_multicast_filters & ((1 << 15) - 1));
1050         info("MTU: %d", le16_to_cpu(kaweth->configuration.segment_size));
1051         info("Read MAC address %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
1052                  (int)kaweth->configuration.hw_addr[0],
1053                  (int)kaweth->configuration.hw_addr[1],
1054                  (int)kaweth->configuration.hw_addr[2],
1055                  (int)kaweth->configuration.hw_addr[3],
1056                  (int)kaweth->configuration.hw_addr[4],
1057                  (int)kaweth->configuration.hw_addr[5]);
1058
1059         if(!memcmp(&kaweth->configuration.hw_addr,
1060                    &bcast_addr,
1061                    sizeof(bcast_addr))) {
1062                 err("Firmware not functioning properly, no net device created");
1063                 goto err_free_netdev;
1064         }
1065
1066         if(kaweth_set_urb_size(kaweth, KAWETH_BUF_SIZE) < 0) {
1067                 dbg("Error setting URB size");
1068                 goto err_free_netdev;
1069         }
1070
1071         if(kaweth_set_sofs_wait(kaweth, KAWETH_SOFS_TO_WAIT) < 0) {
1072                 err("Error setting SOFS wait");
1073                 goto err_free_netdev;
1074         }
1075
1076         result = kaweth_set_receive_filter(kaweth,
1077                                            KAWETH_PACKET_FILTER_DIRECTED |
1078                                            KAWETH_PACKET_FILTER_BROADCAST |
1079                                            KAWETH_PACKET_FILTER_MULTICAST);
1080
1081         if(result < 0) {
1082                 err("Error setting receive filter");
1083                 goto err_free_netdev;
1084         }
1085
1086         dbg("Initializing net device.");
1087
1088         kaweth->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
1089         if (!kaweth->tx_urb)
1090                 goto err_free_netdev;
1091         kaweth->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
1092         if (!kaweth->rx_urb)
1093                 goto err_only_tx;
1094         kaweth->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
1095         if (!kaweth->irq_urb)
1096                 goto err_tx_and_rx;
1097
1098         kaweth->intbuffer = usb_buffer_alloc(   kaweth->dev,
1099                                                 INTBUFFERSIZE,
1100                                                 GFP_KERNEL,
1101                                                 &kaweth->intbufferhandle);
1102         if (!kaweth->intbuffer)
1103                 goto err_tx_and_rx_and_irq;
1104         kaweth->rx_buf = usb_buffer_alloc(      kaweth->dev,
1105                                                 KAWETH_BUF_SIZE,
1106                                                 GFP_KERNEL,
1107                                                 &kaweth->rxbufferhandle);
1108         if (!kaweth->rx_buf)
1109                 goto err_all_but_rxbuf;
1110
1111         memcpy(netdev->broadcast, &bcast_addr, sizeof(bcast_addr));
1112         memcpy(netdev->dev_addr, &kaweth->configuration.hw_addr,
1113                sizeof(kaweth->configuration.hw_addr));
1114
1115         netdev->open = kaweth_open;
1116         netdev->stop = kaweth_close;
1117
1118         netdev->watchdog_timeo = KAWETH_TX_TIMEOUT;
1119         netdev->tx_timeout = kaweth_tx_timeout;
1120
1121         netdev->hard_start_xmit = kaweth_start_xmit;
1122         netdev->set_multicast_list = kaweth_set_rx_mode;
1123         netdev->get_stats = kaweth_netdev_stats;
1124         netdev->mtu = le16_to_cpu(kaweth->configuration.segment_size);
1125         SET_ETHTOOL_OPS(netdev, &ops);
1126
1127         /* kaweth is zeroed as part of alloc_netdev */
1128
1129         INIT_WORK(&kaweth->lowmem_work, kaweth_resubmit_tl, (void *)kaweth);
1130
1131         SET_MODULE_OWNER(netdev);
1132
1133         usb_set_intfdata(intf, kaweth);
1134
1135 #if 0
1136 // dma_supported() is deeply broken on almost all architectures
1137         if (dma_supported (&intf->dev, 0xffffffffffffffffULL))
1138                 kaweth->net->features |= NETIF_F_HIGHDMA;
1139 #endif
1140
1141         SET_NETDEV_DEV(netdev, &intf->dev);
1142         if (register_netdev(netdev) != 0) {
1143                 err("Error registering netdev.");
1144                 goto err_intfdata;
1145         }
1146
1147         info("kaweth interface created at %s", kaweth->net->name);
1148
1149         dbg("Kaweth probe returning.");
1150
1151         return 0;
1152
1153 err_intfdata:
1154         usb_set_intfdata(intf, NULL);
1155         usb_buffer_free(kaweth->dev, KAWETH_BUF_SIZE, (void *)kaweth->rx_buf, kaweth->rxbufferhandle);
1156 err_all_but_rxbuf:
1157         usb_buffer_free(kaweth->dev, INTBUFFERSIZE, (void *)kaweth->intbuffer, kaweth->intbufferhandle);
1158 err_tx_and_rx_and_irq:
1159         usb_free_urb(kaweth->irq_urb);
1160 err_tx_and_rx:
1161         usb_free_urb(kaweth->rx_urb);
1162 err_only_tx:
1163         usb_free_urb(kaweth->tx_urb);
1164 err_free_netdev:
1165         free_netdev(netdev);
1166
1167         return -EIO;
1168 }
1169
1170 /****************************************************************
1171  *     kaweth_disconnect
1172  ****************************************************************/
1173 static void kaweth_disconnect(struct usb_interface *intf)
1174 {
1175         struct kaweth_device *kaweth = usb_get_intfdata(intf);
1176         struct net_device *netdev;
1177
1178         info("Unregistering");
1179
1180         usb_set_intfdata(intf, NULL);
1181         if (!kaweth) {
1182                 warn("unregistering non-existant device");
1183                 return;
1184         }
1185         netdev = kaweth->net;
1186
1187         dbg("Unregistering net device");
1188         unregister_netdev(netdev);
1189
1190         usb_free_urb(kaweth->rx_urb);
1191         usb_free_urb(kaweth->tx_urb);
1192         usb_free_urb(kaweth->irq_urb);
1193
1194         usb_buffer_free(kaweth->dev, KAWETH_BUF_SIZE, (void *)kaweth->rx_buf, kaweth->rxbufferhandle);
1195         usb_buffer_free(kaweth->dev, INTBUFFERSIZE, (void *)kaweth->intbuffer, kaweth->intbufferhandle);
1196
1197         free_netdev(netdev);
1198 }
1199
1200
1201 // FIXME this completion stuff is a modified clone of
1202 // an OLD version of some stuff in usb.c ...
1203 struct usb_api_data {
1204         wait_queue_head_t wqh;
1205         int done;
1206 };
1207
1208 /*-------------------------------------------------------------------*
1209  * completion handler for compatibility wrappers (sync control/bulk) *
1210  *-------------------------------------------------------------------*/
1211 static void usb_api_blocking_completion(struct urb *urb)
1212 {
1213         struct usb_api_data *awd = (struct usb_api_data *)urb->context;
1214
1215         awd->done=1;
1216         wake_up(&awd->wqh);
1217 }
1218
1219 /*-------------------------------------------------------------------*
1220  *                         COMPATIBILITY STUFF                       *
1221  *-------------------------------------------------------------------*/
1222
1223 // Starts urb and waits for completion or timeout
1224 static int usb_start_wait_urb(struct urb *urb, int timeout, int* actual_length)
1225 {
1226         struct usb_api_data awd;
1227         int status;
1228
1229         init_waitqueue_head(&awd.wqh);
1230         awd.done = 0;
1231
1232         urb->context = &awd;
1233         status = usb_submit_urb(urb, GFP_NOIO);
1234         if (status) {
1235                 // something went wrong
1236                 usb_free_urb(urb);
1237                 return status;
1238         }
1239
1240         if (!wait_event_timeout(awd.wqh, awd.done, timeout)) {
1241                 // timeout
1242                 warn("usb_control/bulk_msg: timeout");
1243                 usb_kill_urb(urb);  // remove urb safely
1244                 status = -ETIMEDOUT;
1245         }
1246         else {
1247                 status = urb->status;
1248         }
1249
1250         if (actual_length) {
1251                 *actual_length = urb->actual_length;
1252         }
1253
1254         usb_free_urb(urb);
1255         return status;
1256 }
1257
1258 /*-------------------------------------------------------------------*/
1259 // returns status (negative) or length (positive)
1260 static int kaweth_internal_control_msg(struct usb_device *usb_dev,
1261                                        unsigned int pipe,
1262                                        struct usb_ctrlrequest *cmd, void *data,
1263                                        int len, int timeout)
1264 {
1265         struct urb *urb;
1266         int retv;
1267         int length;
1268
1269         urb = usb_alloc_urb(0, GFP_NOIO);
1270         if (!urb)
1271                 return -ENOMEM;
1272
1273         usb_fill_control_urb(urb, usb_dev, pipe, (unsigned char*)cmd, data,
1274                          len, usb_api_blocking_completion, NULL);
1275
1276         retv = usb_start_wait_urb(urb, timeout, &length);
1277         if (retv < 0) {
1278                 return retv;
1279         }
1280         else {
1281                 return length;
1282         }
1283 }
1284
1285
1286 /****************************************************************
1287  *     kaweth_init
1288  ****************************************************************/
1289 static int __init kaweth_init(void)
1290 {
1291         dbg("Driver loading");
1292         return usb_register(&kaweth_driver);
1293 }
1294
1295 /****************************************************************
1296  *     kaweth_exit
1297  ****************************************************************/
1298 static void __exit kaweth_exit(void)
1299 {
1300         usb_deregister(&kaweth_driver);
1301 }
1302
1303 module_init(kaweth_init);
1304 module_exit(kaweth_exit);
1305
1306
1307
1308
1309
1310
1311
1312
1313