v2.4.9.9 -> v2.4.9.10
[opensuse:kernel.git] / drivers / usb / kaweth.c
1 /****************************************************************
2  *
3  *     kaweth.c - driver for KL5KUSB101 based USB->Ethernet
4  *
5  *     (c) 2000 Interlan Communications
6  *     (c) 2000 Stephane Alnet
7  *     (C) 2001 Brad Hards
8  *
9  *     Original author: The Zapman <zapman@interlan.net>
10  *     Inspired by, and much credit goes to Michael Rothwell 
11  *     <rothwell@interlan.net> for the test equipment, help, and patience
12  *     Based off of (and with thanks to) Petko Manolov's pegaus.c driver.
13  *     Also many thanks to Joel Silverman and Ed Surprenant at Kawasaki 
14  *     for providing the firmware and driver resources.
15  *
16  *     This program is free software; you can redistribute it and/or
17  *     modify it under the terms of the GNU General Public License as
18  *     published by the Free Software Foundation; either version 2, or 
19  *     (at your option) any later version.
20  *
21  *     This program is distributed in the hope that it will be useful,
22  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
23  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  *     GNU General Public License for more details.
25  *
26  *     You should have received a copy of the GNU General Public License
27  *     along with this program; if not, write to the Free Software Foundation,
28  *     Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
29  *
30  ****************************************************************/
31
32 /* TODO:
33  * Fix in_interrupt() problem
34  * Develop test procedures for USB net interfaces
35  * Run test procedures
36  * Fix bugs from previous two steps
37  * Snoop other OSs for any tricks we're not doing
38  * SMP locking
39  * Reduce arbitrary timeouts 
40  * Smart multicast support 
41  * Temporary MAC change support
42  * Tunable SOFs parameter - ioctl()?
43  * Ethernet stats collection
44  * Code formatting improvements
45  */
46
47 #include <linux/module.h>
48 #include <linux/sched.h>
49 #include <linux/slab.h>
50 #include <linux/string.h>
51 #include <linux/init.h>
52 #include <linux/delay.h>
53 #include <linux/netdevice.h>
54 #include <linux/etherdevice.h>
55 #include <linux/usb.h>
56 #include <linux/types.h>
57 #include <asm/semaphore.h>
58
59 #define DEBUG
60
61 #ifdef DEBUG
62 #define kaweth_dbg(format, arg...) printk(KERN_DEBUG __FILE__ ": " format "\n" ,##arg)
63 #else
64 #define kaweth_dbg(format, arg...) do {} while (0)
65 #endif
66 #define kaweth_err(format, arg...) printk(KERN_ERR __FILE__ ": " format "\n" ,##arg)
67 #define kaweth_info(format, arg...) printk(KERN_INFO __FILE__ ": " format "\n" , ##arg)
68 #define kaweth_warn(format, arg...) printk(KERN_WARNING __FILE__ ": " format "\n" , ##arg)
69
70
71 #include "kawethfw.h"
72
73 #define KAWETH_MTU                      1514
74 #define KAWETH_BUF_SIZE                 1664
75 #define KAWETH_TX_TIMEOUT               (5 * HZ)
76 #define KAWETH_FIRMWARE_BUF_SIZE        4096
77 #define KAWETH_CONTROL_TIMEOUT          (30 * HZ)
78
79 #define KAWETH_STATUS_BROKEN            0x0000001
80 #define KAWETH_STATUS_CLOSING           0x0000002
81
82 #define KAWETH_PACKET_FILTER_PROMISCUOUS        0x01
83 #define KAWETH_PACKET_FILTER_ALL_MULTICAST      0x02
84 #define KAWETH_PACKET_FILTER_DIRECTED           0x04
85 #define KAWETH_PACKET_FILTER_BROADCAST          0x08
86 #define KAWETH_PACKET_FILTER_MULTICAST          0x10
87
88 /* Table 7 */
89 #define KAWETH_COMMAND_GET_ETHERNET_DESC        0x00
90 #define KAWETH_COMMAND_MULTICAST_FILTERS        0x01
91 #define KAWETH_COMMAND_SET_PACKET_FILTER        0x02
92 #define KAWETH_COMMAND_STATISTICS               0x03
93 #define KAWETH_COMMAND_SET_TEMP_MAC             0x06
94 #define KAWETH_COMMAND_GET_TEMP_MAC             0x07
95 #define KAWETH_COMMAND_SET_URB_SIZE             0x08
96 #define KAWETH_COMMAND_SET_SOFS_WAIT            0x09
97 #define KAWETH_COMMAND_SCAN                     0xFF
98
99 #define KAWETH_SOFS_TO_WAIT                     0x05
100
101
102 MODULE_AUTHOR("Michael Zappe <zapman@interlan.net>, Stephane Alnet <stephane@u-picardie.fr> and Brad Hards <bhards@bigpond.net.au>");
103 MODULE_DESCRIPTION("KL5USB101 USB Ethernet driver");
104 MODULE_LICENSE("GPL");
105
106 static void *kaweth_probe(
107             struct usb_device *dev,             /* the device */
108             unsigned ifnum,                     /* what interface */
109             const struct usb_device_id *id      /* from id_table */
110         );
111 static void kaweth_disconnect(struct usb_device *dev, void *ptr);
112 int kaweth_internal_control_msg(struct usb_device *usb_dev, unsigned int pipe,
113                                 devrequest *cmd, void *data, int len,
114                                 int timeout);
115
116 /****************************************************************
117  *     usb_device_id
118  ****************************************************************/
119 static struct usb_device_id usb_klsi_table[] = {
120         { USB_DEVICE(0x03e8, 0x0008) }, /* AOX Endpoints USB Ethernet */ 
121         { USB_DEVICE(0x04bb, 0x0901) }, /* I-O DATA USB-ET/T */
122         { USB_DEVICE(0x0506, 0x03e8) }, /* 3Com 3C19250 */ 
123         { USB_DEVICE(0x0557, 0x2002) }, /* ATEN USB Ethernet */ 
124         { USB_DEVICE(0x0557, 0x4000) }, /* D-Link DSB-650C */ 
125         { USB_DEVICE(0x0565, 0x0002) }, /* Peracom Enet */
126         { USB_DEVICE(0x0565, 0x0005) }, /* Peracom Enet2 */ 
127         { USB_DEVICE(0x05e9, 0x0008) }, /* KLSI KL5KUSB101B */
128         { USB_DEVICE(0x05e9, 0x0009) }, /* KLSI KL5KUSB101B (Board change) */
129         { USB_DEVICE(0x066b, 0x2202) }, /* Linksys USB10T */ 
130         { USB_DEVICE(0x06e1, 0x0008) }, /* ADS USB-10BT */ 
131         { USB_DEVICE(0x06e1, 0x0009) }, /* ADS USB-10BT */ 
132         { USB_DEVICE(0x0707, 0x0100) }, /* SMC 2202USB */ 
133         { USB_DEVICE(0x07aa, 0x0001) }, /* Correga K.K. */ 
134         { USB_DEVICE(0x07b8, 0x4000) }, /* D-Link DU-E10 */
135         { USB_DEVICE(0x0846, 0x1001) }, /* NetGear EA-101 */
136         { USB_DEVICE(0x0846, 0x1002) }, /* NetGear EA-101 */
137         { USB_DEVICE(0x085a, 0x0008) }, /* PortGear Ethernet Adapter */
138         { USB_DEVICE(0x085a, 0x0009) }, /* PortGear Ethernet Adapter */
139         { USB_DEVICE(0x087d, 0x5704) }, /* Jaton USB Ethernet Device Adapter */
140         { USB_DEVICE(0x0951, 0x0008) }, /* Kingston Technology USB Ethernet Adapter */
141         { USB_DEVICE(0x095a, 0x3003) }, /* Portsmith Express Ethernet Adapter */
142         { USB_DEVICE(0x10bd, 0x1427) }, /* ASANTE USB To Ethernet Adapter */
143         { USB_DEVICE(0x1342, 0x0204) }, /* Mobility USB-Ethernet Adapter */
144         { USB_DEVICE(0x13d2, 0x0400) }, /* Shark Pocket Adapter */
145         { USB_DEVICE(0x1645, 0x0005) }, /* Entrega E45 */ 
146         { USB_DEVICE(0x1645, 0x0008) }, /* Entrega USB Ethernet Adapter */ 
147         { USB_DEVICE(0x1645, 0x8005) }, /* PortGear Ethernet Adapter */ 
148         { USB_DEVICE(0x2001, 0x4000) }, /* D-link DSB-650C */
149         {} /* Null terminator */
150 };
151
152 MODULE_DEVICE_TABLE (usb, usb_klsi_table);
153
154 /****************************************************************
155  *     kaweth_driver
156  ****************************************************************/
157 static struct usb_driver kaweth_driver = {
158         name:           "kaweth",
159         probe:          kaweth_probe,
160         disconnect:     kaweth_disconnect,
161         id_table:       usb_klsi_table,
162 };
163
164 typedef __u8 eth_addr_t[6];
165
166 /****************************************************************
167  *     usb_eth_dev
168  ****************************************************************/
169 struct usb_eth_dev {
170         char *name;
171         __u16 vendor;
172         __u16 device;
173         void *pdata;
174 };
175
176 /****************************************************************
177  *     kaweth_ethernet_configuration
178  *     Refer Table 8
179  ****************************************************************/
180 struct kaweth_ethernet_configuration
181 {
182         __u8 size;
183         __u8 reserved1;
184         __u8 reserved2;
185         eth_addr_t hw_addr;
186         __u32 statistics_mask;
187         __u16 segment_size;
188         __u16 max_multicast_filters;
189         __u8 reserved3;
190 } __attribute__ ((packed));
191
192 /****************************************************************
193  *     kaweth_device
194  ****************************************************************/
195 struct kaweth_device
196 {
197         spinlock_t device_lock;
198
199         __u32 status;
200
201         struct usb_device *dev;
202         struct net_device *net;
203         wait_queue_head_t control_wait;
204
205         struct urb *rx_urb;
206         struct urb *tx_urb;
207         
208         __u8 firmware_buf[KAWETH_FIRMWARE_BUF_SIZE];
209         __u8 tx_buf[KAWETH_BUF_SIZE];
210         __u8 rx_buf[KAWETH_BUF_SIZE];
211         __u16 packet_filter_bitmap;
212
213         struct kaweth_ethernet_configuration configuration;
214
215         struct net_device_stats stats;
216 } __attribute__ ((packed));
217
218
219 /****************************************************************
220  *     kaweth_control
221  ****************************************************************/
222 static int kaweth_control(struct kaweth_device *kaweth,
223                           unsigned int pipe, 
224                           __u8 request, 
225                           __u8 requesttype, 
226                           __u16 value, 
227                           __u16 index,
228                           void *data, 
229                           __u16 size, 
230                           int timeout)
231 {
232         devrequest *dr;
233
234         kaweth_dbg("kaweth_control()");
235
236         if(in_interrupt()) {
237                 kaweth_dbg("in_interrupt()");
238                 return -EBUSY;
239         }
240
241         dr = kmalloc(sizeof(devrequest), 
242                      in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
243
244         if(!dr)
245         {
246                 kaweth_dbg("kmalloc() failed");
247                 return -ENOMEM;
248         }
249         
250         dr->requesttype = requesttype;
251         dr->request = request;
252         dr->value = cpu_to_le16p(&value);
253         dr->index = cpu_to_le16p(&index);
254         dr->length = cpu_to_le16p(&size);
255
256         return kaweth_internal_control_msg(kaweth->dev,
257                                         pipe,
258                                         dr,
259                                         data,
260                                         size,
261                                         timeout);
262 }
263
264 /****************************************************************
265  *     kaweth_read_configuration
266  ****************************************************************/
267 static int kaweth_read_configuration(struct kaweth_device *kaweth)
268 {
269         int retval;
270
271         kaweth_dbg("Reading kaweth configuration");
272
273         retval = kaweth_control(kaweth,
274                                 usb_rcvctrlpipe(kaweth->dev, 0),
275                                 KAWETH_COMMAND_GET_ETHERNET_DESC,
276                                 USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_DEVICE,
277                                 0,
278                                 0,
279                                 (void *)&kaweth->configuration,
280                                 sizeof(kaweth->configuration),
281                                 KAWETH_CONTROL_TIMEOUT);
282
283         return retval;
284 }
285
286 /****************************************************************
287  *     kaweth_set_urb_size
288  ****************************************************************/
289 static int kaweth_set_urb_size(struct kaweth_device *kaweth, __u16 urb_size)
290 {
291         int retval;
292
293         kaweth_dbg("Setting URB size to %d", (unsigned)urb_size);
294
295         retval = kaweth_control(kaweth,
296                                 usb_sndctrlpipe(kaweth->dev, 0),
297                                 KAWETH_COMMAND_SET_URB_SIZE,
298                                 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
299                                 urb_size,
300                                 0,
301                                 (void *)&kaweth->firmware_buf,
302                                 0,
303                                 KAWETH_CONTROL_TIMEOUT);
304
305         return retval;
306 }
307
308 /****************************************************************
309  *     kaweth_set_sofs_wait
310  ****************************************************************/
311 static int kaweth_set_sofs_wait(struct kaweth_device *kaweth, __u16 sofs_wait)
312 {
313         int retval;
314
315         kaweth_dbg("Set SOFS wait to %d", (unsigned)sofs_wait);
316
317         retval = kaweth_control(kaweth,
318                                 usb_sndctrlpipe(kaweth->dev, 0),
319                                 KAWETH_COMMAND_SET_SOFS_WAIT,
320                                 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
321                                 sofs_wait,
322                                 0,
323                                 (void *)&kaweth->firmware_buf,
324                                 0,
325                                 KAWETH_CONTROL_TIMEOUT);
326
327         return retval;
328 }
329
330 /****************************************************************
331  *     kaweth_set_receive_filter
332  ****************************************************************/
333 static int kaweth_set_receive_filter(struct kaweth_device *kaweth,
334                                      __u16 receive_filter)
335 {
336         int retval;
337
338         kaweth_dbg("Set receive filter to %d", (unsigned)receive_filter);
339
340         retval = kaweth_control(kaweth,
341                                 usb_sndctrlpipe(kaweth->dev, 0),
342                                 KAWETH_COMMAND_SET_PACKET_FILTER,
343                                 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
344                                 receive_filter,
345                                 0,
346                                 (void *)&kaweth->firmware_buf,
347                                 0,
348                                 KAWETH_CONTROL_TIMEOUT);
349
350         return retval;
351 }
352
353 /****************************************************************
354  *     kaweth_download_firmware
355  ****************************************************************/
356 static int kaweth_download_firmware(struct kaweth_device *kaweth, 
357                                     __u8 *data, 
358                                     __u16 data_len,
359                                     __u8 interrupt,
360                                     __u8 type)
361 {       
362         if(data_len > KAWETH_FIRMWARE_BUF_SIZE) {
363                 kaweth_err("Firmware too big: %d", data_len);
364                 return -ENOSPC;
365         }
366         
367         memcpy(kaweth->firmware_buf, data, data_len);
368         
369         kaweth->firmware_buf[2] = (data_len & 0xFF) - 7;
370         kaweth->firmware_buf[3] = data_len >> 8;
371         kaweth->firmware_buf[4] = type;
372         kaweth->firmware_buf[5] = interrupt;
373
374         kaweth_dbg("High: %i, Low:%i", kaweth->firmware_buf[3],
375                    kaweth->firmware_buf[2]);
376
377         kaweth_dbg("Downloading firmware at %x to kaweth device at %x", 
378             (int)data, 
379             (int)kaweth);
380         kaweth_dbg("Firmware length: %d", data_len);
381
382         return kaweth_control(kaweth,
383                               usb_sndctrlpipe(kaweth->dev, 0),
384                               KAWETH_COMMAND_SCAN,
385                               USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
386                               0,
387                               0,
388                               (void *)&kaweth->firmware_buf,
389                               data_len,
390                               KAWETH_CONTROL_TIMEOUT);
391 }
392
393 /****************************************************************
394  *     kaweth_trigger_firmware
395  ****************************************************************/
396 static int kaweth_trigger_firmware(struct kaweth_device *kaweth,
397                                    __u8 interrupt)
398 {
399         kaweth->firmware_buf[0] = 0xB6;
400         kaweth->firmware_buf[1] = 0xC3;
401         kaweth->firmware_buf[2] = 0x01;
402         kaweth->firmware_buf[3] = 0x00;
403         kaweth->firmware_buf[4] = 0x06;
404         kaweth->firmware_buf[5] = interrupt;
405         kaweth->firmware_buf[6] = 0x00;
406         kaweth->firmware_buf[7] = 0x00;
407         
408         kaweth_dbg("Triggering firmware");
409
410         return kaweth_control(kaweth,
411                               usb_sndctrlpipe(kaweth->dev, 0),
412                               KAWETH_COMMAND_SCAN,
413                               USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
414                               0,
415                               0,
416                               (void *)&kaweth->firmware_buf,
417                               8,
418                               KAWETH_CONTROL_TIMEOUT);
419 }
420
421 /****************************************************************
422  *     kaweth_reset
423  ****************************************************************/
424 static int kaweth_reset(struct kaweth_device *kaweth)
425 {
426         int result;
427
428         kaweth_dbg("kaweth_reset(%p)", kaweth);
429         result = kaweth_control(kaweth,
430                                 usb_sndctrlpipe(kaweth->dev, 0),
431                                 USB_REQ_SET_CONFIGURATION, 
432                                 0, 
433                                 kaweth->dev->config[0].bConfigurationValue,
434                                 0, 
435                                 NULL, 
436                                 0, 
437                                 KAWETH_CONTROL_TIMEOUT);
438
439         udelay(10000);
440
441         kaweth_dbg("kaweth_reset() returns %d.",result);
442
443         return result;
444 }
445
446 static void kaweth_usb_receive(struct urb *);
447
448 /****************************************************************
449  *     kaweth_resubmit_rx_urb
450  ****************************************************************/
451 static inline void kaweth_resubmit_rx_urb(struct kaweth_device *kaweth)
452 {
453         int result;
454
455         memset(kaweth->rx_urb, 0, sizeof(*kaweth->rx_urb));
456
457         FILL_BULK_URB(kaweth->rx_urb,
458                       kaweth->dev,
459                       usb_rcvbulkpipe(kaweth->dev, 1),
460                       kaweth->rx_buf,
461                       KAWETH_BUF_SIZE,
462                       kaweth_usb_receive,
463                       kaweth);
464
465         if((result = usb_submit_urb(kaweth->rx_urb))) {
466                 kaweth_err("resubmitting rx_urb %d failed", result);
467         }
468 }
469
470 static void kaweth_async_set_rx_mode(struct kaweth_device *kaweth);
471
472 /****************************************************************
473  *     kaweth_usb_receive
474  ****************************************************************/
475 static void kaweth_usb_receive(struct urb *urb)
476 {
477         struct kaweth_device *kaweth = urb->context;
478         struct net_device *net = kaweth->net;
479         
480         int count = urb->actual_length;
481         int count2 = urb->transfer_buffer_length;
482                         
483         __u16 pkt_len = le16_to_cpup(kaweth->rx_buf);
484
485         struct sk_buff *skb;
486
487         if(kaweth->status & KAWETH_STATUS_CLOSING) {
488                 return;
489         }
490         
491         if(urb->status && urb->status != -EREMOTEIO && count != 1) { 
492                 kaweth_err("%s RX status: %d count: %d packet_len: %d",
493                            net->name, 
494                            urb->status,
495                            count, 
496                            (int)pkt_len);
497                 kaweth_resubmit_rx_urb(kaweth);
498                 return;
499         }
500
501         if(kaweth->net && (count > 2)) {
502                 if(pkt_len > (count - 2)) {
503                         kaweth_err("Packet length too long for USB frame (pkt_len: %x, count: %x)",pkt_len, count);
504                         kaweth_err("Packet len & 2047: %x", pkt_len & 2047);
505                         kaweth_err("Count 2: %x", count2);
506                         kaweth_resubmit_rx_urb(kaweth);
507                         return;
508                 }
509                 
510                 if(!(skb = dev_alloc_skb(pkt_len+2))) {
511                         kaweth_resubmit_rx_urb(kaweth);
512                         return;
513                 }
514
515                 skb->dev = net;
516
517                 eth_copy_and_sum(skb, kaweth->rx_buf + 2, pkt_len, 0);
518                 
519                 skb_put(skb, pkt_len);
520
521                 skb->protocol = eth_type_trans(skb, net);
522                 
523                 netif_rx(skb);
524                 
525                 kaweth->stats.rx_packets++;
526                 kaweth->stats.rx_bytes += pkt_len;
527         }
528
529         kaweth_resubmit_rx_urb(kaweth);
530 }
531
532 /****************************************************************
533  *     kaweth_open
534  ****************************************************************/
535 static int kaweth_open(struct net_device *net)
536 {
537         struct kaweth_device *kaweth = (struct kaweth_device *)net->priv;
538
539         kaweth_dbg("Dev usage: %d", kaweth->dev->refcnt.counter);
540
541         kaweth_dbg("Opening network device.");
542
543         kaweth_resubmit_rx_urb(kaweth);
544
545         netif_start_queue(net);
546
547         MOD_INC_USE_COUNT;
548
549         kaweth_async_set_rx_mode(kaweth);
550         return 0;
551 }
552
553 /****************************************************************
554  *     kaweth_close
555  ****************************************************************/
556 static int kaweth_close(struct net_device *net)
557 {
558         struct kaweth_device *kaweth = net->priv;
559
560         netif_stop_queue(net);
561
562         kaweth->status |= KAWETH_STATUS_CLOSING;
563
564         usb_unlink_urb(kaweth->rx_urb);
565
566         kaweth->status &= ~KAWETH_STATUS_CLOSING;
567
568         MOD_DEC_USE_COUNT;
569
570         printk("Dev usage: %d", kaweth->dev->refcnt.counter);
571
572         return 0;
573 }
574
575 /****************************************************************
576  *     kaweth_ioctl
577  ****************************************************************/
578 static int kaweth_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
579 {
580         return -EOPNOTSUPP;
581 }
582
583 /****************************************************************
584  *     kaweth_usb_transmit_complete
585  ****************************************************************/
586 static void kaweth_usb_transmit_complete(struct urb *urb)
587 {
588         struct kaweth_device *kaweth = urb->context;
589
590         spin_lock(&kaweth->device_lock);
591
592         if (urb->status)
593                 kaweth_dbg("%s: TX status %d.", kaweth->net->name, urb->status);
594
595         netif_wake_queue(kaweth->net);
596
597         spin_unlock(&kaweth->device_lock);
598 }
599
600 /****************************************************************
601  *     kaweth_start_xmit
602  ****************************************************************/
603 static int kaweth_start_xmit(struct sk_buff *skb, struct net_device *net)
604 {
605         struct kaweth_device *kaweth = net->priv;
606         int count = skb->len;
607         
608         int res;
609
610         spin_lock(&kaweth->device_lock);
611
612         kaweth_async_set_rx_mode(kaweth);
613         netif_stop_queue(net);
614
615         *((__u16 *)kaweth->tx_buf) = cpu_to_le16(skb->len);
616
617         memcpy(kaweth->tx_buf + 2, skb->data, skb->len);
618
619         memset(kaweth->tx_urb, 0, sizeof(*kaweth->tx_urb));
620
621         FILL_BULK_URB(kaweth->tx_urb,
622                       kaweth->dev,
623                       usb_sndbulkpipe(kaweth->dev, 2),
624                       kaweth->tx_buf,
625                       count + 2,
626                       kaweth_usb_transmit_complete,
627                       kaweth);
628
629         if((res = usb_submit_urb(kaweth->tx_urb)))
630         {
631                 kaweth_warn("kaweth failed tx_urb %d", res);
632                 kaweth->stats.tx_errors++;
633                 
634                 netif_start_queue(net);
635         } 
636         else 
637         {
638                 kaweth->stats.tx_packets++;
639                 kaweth->stats.tx_bytes += skb->len;
640                 net->trans_start = jiffies;
641         }
642
643         dev_kfree_skb(skb);
644
645         spin_unlock(&kaweth->device_lock);
646
647         return 0;
648 }
649
650 /****************************************************************
651  *     kaweth_set_rx_mode
652  ****************************************************************/
653 static void kaweth_set_rx_mode(struct net_device *net)
654 {
655         struct kaweth_device *kaweth = net->priv;
656         
657         __u16 packet_filter_bitmap = KAWETH_PACKET_FILTER_DIRECTED |
658                                      KAWETH_PACKET_FILTER_BROADCAST |
659                                      KAWETH_PACKET_FILTER_MULTICAST;
660
661         kaweth_dbg("Setting Rx mode to %d", packet_filter_bitmap);
662
663         netif_stop_queue(net);
664
665         if (net->flags & IFF_PROMISC) {
666                 packet_filter_bitmap |= KAWETH_PACKET_FILTER_PROMISCUOUS;
667         } 
668         else if ((net->mc_count) || (net->flags & IFF_ALLMULTI)) {
669                 packet_filter_bitmap |= KAWETH_PACKET_FILTER_ALL_MULTICAST;
670         }
671
672         kaweth->packet_filter_bitmap = packet_filter_bitmap;
673         netif_wake_queue(net);
674 }
675
676 /****************************************************************
677  *     kaweth_async_set_rx_mode
678  ****************************************************************/
679 static void kaweth_async_set_rx_mode(struct kaweth_device *kaweth)
680 {
681         __u16 packet_filter_bitmap = kaweth->packet_filter_bitmap;
682         kaweth->packet_filter_bitmap = 0;       
683         if(packet_filter_bitmap == 0) return;
684
685         {
686         int result;
687         result = kaweth_control(kaweth,
688                                 usb_sndctrlpipe(kaweth->dev, 0),
689                                 KAWETH_COMMAND_SET_PACKET_FILTER,
690                                 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
691                                 packet_filter_bitmap,
692                                 0,
693                                 (void *)&kaweth->firmware_buf,
694                                 0,
695                                 KAWETH_CONTROL_TIMEOUT);
696
697         if(result < 0) {
698                 kaweth_err("Failed to set Rx mode: %d", result);
699         }
700         else {
701                 kaweth_dbg("Set Rx mode to %d", packet_filter_bitmap);
702         }
703         }
704 }
705
706 /****************************************************************
707  *     kaweth_netdev_stats
708  ****************************************************************/
709 static struct net_device_stats *kaweth_netdev_stats(struct net_device *dev)
710 {
711         return &((struct kaweth_device *)dev->priv)->stats;
712 }
713
714 /****************************************************************
715  *     kaweth_tx_timeout
716  ****************************************************************/
717 static void kaweth_tx_timeout(struct net_device *net)
718 {
719         struct kaweth_device *kaweth = net->priv;
720
721         kaweth_warn("%s: Tx timed out. Resetting.", net->name);
722         kaweth->stats.tx_errors++;
723         net->trans_start = jiffies;
724
725         usb_unlink_urb(kaweth->tx_urb);
726 }
727
728 /****************************************************************
729  *     kaweth_probe
730  ****************************************************************/
731 static void *kaweth_probe(
732             struct usb_device *dev,             /* the device */
733             unsigned ifnum,                      /* what interface */
734             const struct usb_device_id *id      /* from id_table */
735         )
736 {
737         struct kaweth_device *kaweth;
738         const eth_addr_t bcast_addr = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
739         int result = 0;
740
741         kaweth_dbg("Kawasaki Device Probe (Device number:%d): 0x%4.4x:0x%4.4x:0x%4.4x",
742                  dev->devnum, 
743                  (int)dev->descriptor.idVendor, 
744                  (int)dev->descriptor.idProduct,
745                  (int)dev->descriptor.bcdDevice);
746
747         kaweth_dbg("Device at %p", dev);
748
749         kaweth_dbg("Descriptor length: %x type: %x", 
750                  (int)dev->descriptor.bLength,
751                  (int)dev->descriptor.bDescriptorType);
752
753         if(!(kaweth = kmalloc(sizeof(struct kaweth_device), GFP_KERNEL))) {
754                 kaweth_dbg("out of memory allocating device structure\n");
755                 return NULL;
756         }
757
758         memset(kaweth, 0, sizeof(struct kaweth_device));
759
760         kaweth->dev = dev;
761         kaweth->status = 0;
762         kaweth->net = NULL;
763         kaweth->device_lock = SPIN_LOCK_UNLOCKED;
764                 
765         kaweth_dbg("Resetting.");
766
767         kaweth_reset(kaweth);
768
769         /*
770          * If high byte of bcdDevice is nonzero, firmware is already
771          * downloaded. Don't try to do it again, or we'll hang the device.
772          */
773
774         if (dev->descriptor.bcdDevice >> 8) {
775                 kaweth_info("Firmware present in device.");
776         } else {
777                 /* Download the firmware */
778                 kaweth_info("Downloading firmware...");
779                 if ((result = kaweth_download_firmware(kaweth, 
780                                                       kaweth_new_code, 
781                                                       len_kaweth_new_code, 
782                                                       100, 
783                                                       2)) < 0) {
784                         kaweth_err("Error downloading firmware (%d)", result);
785                         kfree(kaweth);
786                         return NULL;
787                 }
788
789                 if ((result = kaweth_download_firmware(kaweth, 
790                                                       kaweth_new_code_fix, 
791                                                       len_kaweth_new_code_fix, 
792                                                       100, 
793                                                       3)) < 0) {
794                         kaweth_err("Error downloading firmware fix (%d)", result);
795                         kfree(kaweth);
796                         return NULL;
797                 }
798
799                 if ((result = kaweth_download_firmware(kaweth,
800                                                       kaweth_trigger_code,
801                                                       len_kaweth_trigger_code,
802                                                       126,
803                                                       2)) < 0) {
804                         kaweth_err("Error downloading trigger code (%d)", result);
805                         kfree(kaweth);
806                         return NULL;
807                 }
808
809                 if ((result = kaweth_download_firmware(kaweth,
810                                                       kaweth_trigger_code_fix,
811                                                       len_kaweth_trigger_code_fix,
812                                                       126,
813                                                       3)) < 0) {
814                         kaweth_err("Error downloading trigger code fix (%d)", result);
815                         kfree(kaweth);
816                         return NULL;
817                 }
818
819
820                 if ((result = kaweth_trigger_firmware(kaweth, 126)) < 0) {
821                         kaweth_err("Error triggering firmware (%d)", result);
822                         kfree(kaweth);
823                         return NULL;
824                 }
825
826                 /* Device will now disappear for a moment...  */
827                 kaweth_info("Firmware loaded.  I'll be back...");
828                 return NULL;
829         }
830
831         result = kaweth_read_configuration(kaweth);
832
833         if(result < 0) {
834                 kaweth_err("Error reading configuration (%d), no net device created", result);
835                 kfree(kaweth);
836                 return NULL;
837         }
838
839         kaweth_info("Statistics collection: %x", kaweth->configuration.statistics_mask);
840         kaweth_info("Multicast filter limit: %x", kaweth->configuration.max_multicast_filters & ((1 << 15) - 1));
841         kaweth_info("MTU: %d", le16_to_cpu(kaweth->configuration.segment_size));
842         kaweth_info("Read MAC address %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
843                  (int)kaweth->configuration.hw_addr[0],
844                  (int)kaweth->configuration.hw_addr[1],
845                  (int)kaweth->configuration.hw_addr[2],
846                  (int)kaweth->configuration.hw_addr[3],
847                  (int)kaweth->configuration.hw_addr[4],
848                  (int)kaweth->configuration.hw_addr[5]);
849
850         if(!memcmp(&kaweth->configuration.hw_addr,
851                    &bcast_addr, 
852                    sizeof(bcast_addr))) {
853                 kaweth_err("Firmware not functioning properly, no net device created");
854                 kfree(kaweth);
855                 return NULL;
856         }
857
858         if(kaweth_set_urb_size(kaweth, KAWETH_BUF_SIZE) < 0) {
859                 kaweth_dbg("Error setting URB size");
860                 return kaweth;
861         }
862         
863         if(kaweth_set_sofs_wait(kaweth, KAWETH_SOFS_TO_WAIT) < 0) {
864                 kaweth_err("Error setting SOFS wait");
865                 return kaweth;
866         }
867
868         result = kaweth_set_receive_filter(kaweth, 
869                                            KAWETH_PACKET_FILTER_DIRECTED |
870                                            KAWETH_PACKET_FILTER_BROADCAST |
871                                            KAWETH_PACKET_FILTER_MULTICAST);
872
873         if(result < 0) {
874                 kaweth_err("Error setting receive filter");
875                 return kaweth;
876         }
877         
878         kaweth_dbg("Initializing net device.");
879
880         kaweth->tx_urb = usb_alloc_urb(0);
881         kaweth->rx_urb = usb_alloc_urb(0);
882
883         kaweth->net = init_etherdev(0, 0);
884         if (!kaweth->net) {
885                 kaweth_err("Error calling init_etherdev.");
886                 return kaweth;
887         }
888
889         memcpy(kaweth->net->broadcast, &bcast_addr, sizeof(bcast_addr));
890         memcpy(kaweth->net->dev_addr, 
891                &kaweth->configuration.hw_addr,
892                sizeof(kaweth->configuration.hw_addr));
893          
894         kaweth->net->priv = kaweth;
895         kaweth->net->open = kaweth_open;
896         kaweth->net->stop = kaweth_close;
897
898         kaweth->net->watchdog_timeo = KAWETH_TX_TIMEOUT;
899         kaweth->net->tx_timeout = kaweth_tx_timeout;
900         
901         kaweth->net->do_ioctl = kaweth_ioctl;
902         kaweth->net->hard_start_xmit = kaweth_start_xmit;
903         kaweth->net->set_multicast_list = kaweth_set_rx_mode;
904         kaweth->net->get_stats = kaweth_netdev_stats;
905         kaweth->net->mtu = le16_to_cpu(kaweth->configuration.segment_size);
906
907         memset(&kaweth->stats, 0, sizeof(kaweth->stats));
908
909         kaweth_info("kaweth interface created at %s", kaweth->net->name);
910                                                                 
911         kaweth_dbg("Kaweth probe returning.");
912
913         return kaweth;
914 }
915
916 /****************************************************************
917  *     kaweth_disconnect
918  ****************************************************************/
919 static void kaweth_disconnect(struct usb_device *dev, void *ptr)
920 {
921         struct kaweth_device *kaweth = ptr;
922
923         kaweth_info("Unregistering");
924
925         if (!kaweth) {
926                 kaweth_warn("unregistering non-existant device");
927                 return;
928         }
929
930         if(kaweth->net) {
931                 if(kaweth->net->flags & IFF_UP) {
932                         kaweth_dbg("Closing net device");
933                         dev_close(kaweth->net);
934                 }
935
936                 kaweth_dbg("Unregistering net device");
937                 unregister_netdev(kaweth->net);
938         }
939
940         usb_free_urb(kaweth->rx_urb);
941         usb_free_urb(kaweth->tx_urb);
942
943         kfree(kaweth);
944 }
945
946
947 /*-------------------------------------------------------------------*
948  * completion handler for compatibility wrappers (sync control/bulk) *
949  *-------------------------------------------------------------------*/
950 static void usb_api_blocking_completion(urb_t *urb)
951 {
952         api_wrapper_data *awd = (api_wrapper_data *)urb->context;
953
954         if (waitqueue_active(awd->wakeup)) {
955                 wake_up(awd->wakeup);
956         }
957
958 }
959
960 /*-------------------------------------------------------------------*
961  *                         COMPATIBILITY STUFF                       *
962  *-------------------------------------------------------------------*/
963
964 // Starts urb and waits for completion or timeout
965 static int usb_start_wait_urb(urb_t *urb, int timeout, int* actual_length)
966 {
967         DECLARE_WAITQUEUE(wait, current);
968         DECLARE_WAIT_QUEUE_HEAD(wqh);
969         api_wrapper_data awd;
970         int status;
971
972         awd.wakeup = &wqh;
973         init_waitqueue_head(&wqh);
974         current->state = TASK_INTERRUPTIBLE;
975         add_wait_queue(&wqh, &wait);
976         urb->context = &awd;
977         status = usb_submit_urb(urb);
978         if (status) {
979                 // something went wrong
980                 usb_free_urb(urb);
981                 current->state = TASK_RUNNING;
982                 remove_wait_queue(&wqh, &wait);
983                 return status;
984         }
985
986         if (urb->status == -EINPROGRESS) {
987                 while (timeout && urb->status == -EINPROGRESS)
988                         status = timeout = schedule_timeout(timeout);
989         } 
990         else {
991                 status = 1;
992         }
993
994         current->state = TASK_RUNNING;
995         remove_wait_queue(&wqh, &wait);
996
997         if (!status) {
998                 // timeout
999                 kaweth_warn("usb_control/bulk_msg: timeout");
1000                 usb_unlink_urb(urb);  // remove urb safely
1001                 status = -ETIMEDOUT;
1002         }
1003         else {
1004                 status = urb->status;
1005         }
1006
1007         if (actual_length) {
1008                 *actual_length = urb->actual_length;
1009         }
1010
1011         usb_free_urb(urb);
1012         return status;
1013 }
1014
1015 /*-------------------------------------------------------------------*/
1016 // returns status (negative) or length (positive)
1017 int kaweth_internal_control_msg(struct usb_device *usb_dev, unsigned int pipe,
1018                             devrequest *cmd,  void *data, int len, int timeout)
1019 {
1020         urb_t *urb;
1021         int retv;
1022         int length;
1023
1024         urb = usb_alloc_urb(0);
1025         if (!urb)
1026                 return -ENOMEM;
1027
1028         FILL_CONTROL_URB(urb, usb_dev, pipe, (unsigned char*)cmd, data,
1029                          len, (usb_complete_t)usb_api_blocking_completion,0);
1030
1031         retv = usb_start_wait_urb(urb, timeout, &length);
1032         if (retv < 0) {
1033                 return retv;
1034         }
1035         else {
1036                 return length;
1037         }
1038 }
1039
1040
1041 /****************************************************************
1042  *     kaweth_init
1043  ****************************************************************/
1044 int __init kaweth_init(void)
1045 {
1046         kaweth_dbg("Driver loading");
1047         return usb_register(&kaweth_driver);
1048 }
1049
1050 /****************************************************************
1051  *     kaweth_exit
1052  ****************************************************************/
1053 void __exit kaweth_exit(void)
1054 {
1055         usb_deregister(&kaweth_driver);
1056 }
1057
1058 module_init(kaweth_init);
1059 module_exit(kaweth_exit);
1060
1061
1062
1063
1064
1065
1066
1067