v2.4.10.4 -> v2.4.10.5
[linux-flexiantxendom0-3.2.10.git] / drivers / usb / CDCEther.c
1 // Portions of this file taken from 
2 // Petko Manolov - Petkan (petkan@dce.bg)
3 // from his driver pegasus.c
4
5 /*
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20
21
22 #include <linux/sched.h>
23 #include <linux/slab.h>
24 #include <linux/init.h>
25 #include <linux/delay.h>
26 #include <linux/netdevice.h>
27 #include <linux/etherdevice.h>
28 #include <linux/usb.h>
29 #include <linux/module.h>
30 #include "CDCEther.h"
31
32 static const char *version = __FILE__ ": v0.98.5 22 Sep 2001 Brad Hards and another";
33
34 /* Take any CDC device, and sort it out in probe() */
35 static struct usb_device_id CDCEther_ids[] = {
36         { USB_DEVICE_INFO(USB_CLASS_COMM, 0, 0) },
37         { } /* Terminating null entry */
38 };
39
40 /* 
41  * module parameter that provides an alternate upper limit on the 
42  * number of multicast filters we use, with a default to use all
43  * the filters available to us. Note that the actual number used
44  * is the lesser of this parameter and the number returned in the
45  * descriptor for the particular device. See Table 41 of the CDC
46  * spec for more info on the descriptor limit.
47  */
48 static int multicast_filter_limit = 32767;
49
50
51 //////////////////////////////////////////////////////////////////////////////
52 // Callback routines from USB device /////////////////////////////////////////
53 //////////////////////////////////////////////////////////////////////////////
54
55 static void read_bulk_callback( struct urb *urb )
56 {
57         ether_dev_t *ether_dev = urb->context;
58         struct net_device *net;
59         int count = urb->actual_length, res;
60         struct sk_buff  *skb;
61
62         // Sanity check 
63         if ( !ether_dev || !(ether_dev->flags & CDC_ETHER_RUNNING) ) {
64                 dbg("BULK IN callback but driver is not active!");
65                 return;
66         }
67
68         net = ether_dev->net;
69         if ( !netif_device_present(net) ) {
70                 // Somebody killed our network interface...
71                 return;
72         }
73
74         if ( ether_dev->flags & CDC_ETHER_RX_BUSY ) {
75                 // Are we already trying to receive a frame???
76                 ether_dev->stats.rx_errors++;
77                 dbg("ether_dev Rx busy");
78                 return;
79         }
80
81         // We are busy, leave us alone!
82         ether_dev->flags |= CDC_ETHER_RX_BUSY;
83
84         switch ( urb->status ) {
85                 case USB_ST_NOERROR:
86                         break;
87                 case USB_ST_NORESPONSE:
88                         dbg( "no repsonse in BULK IN" );
89                         ether_dev->flags &= ~CDC_ETHER_RX_BUSY;
90                         break;
91                 default:
92                         dbg( "%s: RX status %d", net->name, urb->status );
93                         goto goon;
94         }
95
96         // Check to make sure we got some data...
97         if ( !count ) {
98                 // We got no data!!!
99                 goto goon;
100         }
101
102         // Tell the kernel we want some memory
103         if ( !(skb = dev_alloc_skb(count)) ) {
104                 // We got no receive buffer.
105                 goto goon;
106         }
107
108         // Here's where it came from
109         skb->dev = net;
110         
111         // Now we copy it over
112         eth_copy_and_sum(skb, ether_dev->rx_buff, count, 0);
113         
114         // Not sure
115         skb_put(skb, count);
116         // Not sure here either
117         skb->protocol = eth_type_trans(skb, net);
118         
119         // Ship it off to the kernel
120         netif_rx(skb);
121         
122         // update out statistics
123         ether_dev->stats.rx_packets++;
124         ether_dev->stats.rx_bytes += count;
125
126 goon:
127         // Prep the USB to wait for another frame
128         FILL_BULK_URB( &ether_dev->rx_urb, ether_dev->usb,
129                         usb_rcvbulkpipe(ether_dev->usb, ether_dev->data_ep_in),
130                         ether_dev->rx_buff, ether_dev->wMaxSegmentSize, 
131                         read_bulk_callback, ether_dev );
132                         
133         // Give this to the USB subsystem so it can tell us 
134         // when more data arrives.
135         if ( (res = usb_submit_urb(&ether_dev->rx_urb)) ) {
136                 warn( __FUNCTION__ " failed submint rx_urb %d", res);
137         }
138         
139         // We are no longer busy, show us the frames!!!
140         ether_dev->flags &= ~CDC_ETHER_RX_BUSY;
141 }
142
143 static void write_bulk_callback( struct urb *urb )
144 {
145         ether_dev_t *ether_dev = urb->context;
146
147         // Sanity check
148         if ( !ether_dev || !(ether_dev->flags & CDC_ETHER_RUNNING) ) {
149                 // We are insane!!!
150                 err( "write_bulk_callback: device not running" );
151                 return;
152         }
153
154         // Do we still have a valid kernel network device?
155         if ( !netif_device_present(ether_dev->net) ) {
156                 // Someone killed our network interface.
157                 err( "write_bulk_callback: net device not present" );
158                 return;
159         }
160
161         // Hmm...  What on Earth could have happened???
162         if ( urb->status ) {
163                 info("%s: TX status %d", ether_dev->net->name, urb->status);
164         }
165
166         // Update the network interface and tell it we are
167         // ready for another frame
168         ether_dev->net->trans_start = jiffies;
169         netif_wake_queue( ether_dev->net );
170 }
171
172 //static void intr_callback( struct urb *urb )
173 //{
174 //      ether_dev_t *ether_dev = urb->context;
175 //      struct net_device *net;
176 //      __u8    *d;
177 //
178 //      if ( !ether_dev )
179 //              return;
180 //              
181 //      switch ( urb->status ) {
182 //              case USB_ST_NOERROR:
183 //                      break;
184 //              case USB_ST_URB_KILLED:
185 //                      return;
186 //              default:
187 //                      info("intr status %d", urb->status);
188 //      }
189 //
190 //      d = urb->transfer_buffer;
191 //      net = ether_dev->net;
192 //      if ( d[0] & 0xfc ) {
193 //              ether_dev->stats.tx_errors++;
194 //              if ( d[0] & TX_UNDERRUN )
195 //                      ether_dev->stats.tx_fifo_errors++;
196 //              if ( d[0] & (EXCESSIVE_COL | JABBER_TIMEOUT) )
197 //                      ether_dev->stats.tx_aborted_errors++;
198 //              if ( d[0] & LATE_COL )
199 //                      ether_dev->stats.tx_window_errors++;
200 //              if ( d[0] & (NO_CARRIER | LOSS_CARRIER) )
201 //                      ether_dev->stats.tx_carrier_errors++;
202 //      }
203 //}
204
205 //////////////////////////////////////////////////////////////////////////////
206 // Routines for turning net traffic on and off on the USB side ///////////////
207 //////////////////////////////////////////////////////////////////////////////
208
209 static inline int enable_net_traffic( ether_dev_t *ether_dev )
210 {
211         struct usb_device *usb = ether_dev->usb;
212
213         // Here would be the time to set the data interface to the configuration where
214         // it has two endpoints that use a protocol we can understand.
215
216         if (usb_set_interface( usb, 
217                                 ether_dev->data_bInterfaceNumber, 
218                                 ether_dev->data_bAlternateSetting_with_traffic ) )  {
219                 err("usb_set_interface() failed" );
220                 err("Attempted to set interface %d", ether_dev->data_bInterfaceNumber);
221                 err("To alternate setting       %d", ether_dev->data_bAlternateSetting_with_traffic);
222                 return -1;
223         }
224         return 0;
225 }
226
227 static inline void disable_net_traffic( ether_dev_t *ether_dev )
228 {
229         // The thing to do is to set the data interface to the alternate setting that has
230         // no endpoints.  This is what the spec suggests.
231
232         if (ether_dev->data_interface_altset_num_without_traffic >= 0 ) {
233                 if (usb_set_interface( ether_dev->usb, 
234                                         ether_dev->data_bInterfaceNumber, 
235                                         ether_dev->data_bAlternateSetting_without_traffic ) )   {
236                         err("usb_set_interface() failed");
237                 }
238         } else {
239                 // Some devices just may not support this...
240                 warn("No way to disable net traffic");
241         }
242 }
243
244 //////////////////////////////////////////////////////////////////////////////
245 // Callback routines for kernel Ethernet Device //////////////////////////////
246 //////////////////////////////////////////////////////////////////////////////
247
248 static void CDCEther_tx_timeout( struct net_device *net )
249 {
250         ether_dev_t *ether_dev = net->priv;
251
252         // Sanity check
253         if ( !ether_dev ) {
254                 // Seems to be a case of insanity here
255                 return;
256         }
257
258         // Tell syslog we are hosed.
259         warn("%s: Tx timed out.", net->name);
260         
261         // Tear the waiting frame off the list
262         ether_dev->tx_urb.transfer_flags |= USB_ASYNC_UNLINK;
263         usb_unlink_urb( &ether_dev->tx_urb );
264         
265         // Update statistics
266         ether_dev->stats.tx_errors++;
267 }
268
269 static int CDCEther_start_xmit( struct sk_buff *skb, struct net_device *net )
270 {
271         ether_dev_t     *ether_dev = net->priv;
272         int     count;
273         int     res;
274
275         // If we are told to transmit an ethernet frame that fits EXACTLY 
276         // into an integer number of USB packets, we force it to send one 
277         // more byte so the device will get a runt USB packet signalling the 
278         // end of the ethernet frame
279         if ( (skb->len) ^ (ether_dev->data_ep_out_size) ) {
280                 // It was not an exact multiple
281                 // no need to add anything extra
282                 count = skb->len;
283         } else {
284                 // Add one to make it NOT an exact multiple
285                 count = skb->len + 1;
286         }
287
288         // Tell the kernel, "No more frames 'til we are done
289         // with this one.'
290         netif_stop_queue( net );
291
292         // Copy it from kernel memory to OUR memory
293         memcpy(ether_dev->tx_buff, skb->data, skb->len);
294
295         // Fill in the URB for shipping it out.
296         FILL_BULK_URB( &ether_dev->tx_urb, ether_dev->usb,
297                         usb_sndbulkpipe(ether_dev->usb, ether_dev->data_ep_out),
298                         ether_dev->tx_buff, ether_dev->wMaxSegmentSize, 
299                         write_bulk_callback, ether_dev );
300
301         // Tell the URB how much it will be transporting today
302         ether_dev->tx_urb.transfer_buffer_length = count;
303         
304         // Send the URB on its merry way.
305         if ((res = usb_submit_urb(&ether_dev->tx_urb)))  {
306                 // Hmm...  It didn't go. Tell someone...
307                 warn("failed tx_urb %d", res);
308                 // update some stats...
309                 ether_dev->stats.tx_errors++;
310                 // and tell the kernel to give us another.
311                 // Maybe we'll get it right next time.
312                 netif_start_queue( net );
313         } else {
314                 // Okay, it went out.
315                 // Update statistics
316                 ether_dev->stats.tx_packets++;
317                 ether_dev->stats.tx_bytes += skb->len;
318                 // And tell the kernel when the last transmit occurred.
319                 net->trans_start = jiffies;
320         }
321
322         // We are done with the kernel's memory
323         dev_kfree_skb(skb);
324
325         // We are done here.
326         return 0;
327 }
328
329 static struct net_device_stats *CDCEther_netdev_stats( struct net_device *net )
330 {
331         // Easy enough!
332         return &((ether_dev_t *)net->priv)->stats;
333 }
334
335 static int CDCEther_open(struct net_device *net)
336 {
337         ether_dev_t *ether_dev = (ether_dev_t *)net->priv;
338         int     res;
339
340         // We are finally getting used!
341         MOD_INC_USE_COUNT;
342
343         // Turn on the USB and let the packets flow!!!
344         if ( (res = enable_net_traffic( ether_dev )) ) {
345                 err( __FUNCTION__ "can't enable_net_traffic() - %d", res );
346                 MOD_DEC_USE_COUNT;
347                 return -EIO;
348         }
349
350         // Prep a receive URB
351         FILL_BULK_URB( &ether_dev->rx_urb, ether_dev->usb,
352                         usb_rcvbulkpipe(ether_dev->usb, ether_dev->data_ep_in),
353                         ether_dev->rx_buff, ether_dev->wMaxSegmentSize, 
354                         read_bulk_callback, ether_dev );
355
356         // Put it out there so the device can send us stuff
357         if ( (res = usb_submit_urb(&ether_dev->rx_urb)) )
358         {
359                 // Hmm...  Okay...
360                 warn( __FUNCTION__ " failed rx_urb %d", res );
361         }
362
363         // Tell the kernel we are ready to start receiving from it
364         netif_start_queue( net );
365         
366         // We are up and running.
367         ether_dev->flags |= CDC_ETHER_RUNNING;
368
369         // Let's get ready to move frames!!!
370         return 0;
371 }
372
373 static int CDCEther_close( struct net_device *net )
374 {
375         ether_dev_t     *ether_dev = net->priv;
376
377         // We are no longer running.
378         ether_dev->flags &= ~CDC_ETHER_RUNNING;
379         
380         // Tell the kernel to stop sending us stuff
381         netif_stop_queue( net );
382         
383         // If we are not already unplugged, turn off USB
384         // traffic
385         if ( !(ether_dev->flags & CDC_ETHER_UNPLUG) ) {
386                 disable_net_traffic( ether_dev );
387         }
388
389         // We don't need the URBs anymore.
390         usb_unlink_urb( &ether_dev->rx_urb );
391         usb_unlink_urb( &ether_dev->tx_urb );
392         usb_unlink_urb( &ether_dev->intr_urb );
393         
394         // We are not being used now.
395         MOD_DEC_USE_COUNT;
396
397         // That's it.  I'm done.
398         return 0;
399 }
400
401 static int CDCEther_ioctl( struct net_device *net, struct ifreq *rq, int cmd )
402 {
403         //__u16 *data = (__u16 *)&rq->ifr_data;
404         //ether_dev_t   *ether_dev = net->priv;
405
406         // No support here yet.
407         // Do we need support???
408         switch(cmd) {
409                 case SIOCDEVPRIVATE:
410                         return -EOPNOTSUPP;
411                 case SIOCDEVPRIVATE+1:
412                         return -EOPNOTSUPP;
413                 case SIOCDEVPRIVATE+2:
414                         //return 0;
415                         return -EOPNOTSUPP;
416                 default:
417                         return -EOPNOTSUPP;
418         }
419 }
420
421 static void CDC_SetEthernetPacketFilter (ether_dev_t *ether_dev)
422 {
423         usb_control_msg(ether_dev->usb,
424                         usb_sndctrlpipe(ether_dev->usb, 0),
425                         SET_ETHERNET_PACKET_FILTER, /* request */
426                         USB_TYPE_CLASS | USB_DIR_OUT | USB_RECIP_INTERFACE, /* request type */
427                         cpu_to_le16(ether_dev->mode_flags), /* value */
428                         cpu_to_le16((u16)ether_dev->comm_interface), /* index */
429                         NULL,
430                         0, /* size */
431                         HZ); /* timeout */
432 }       
433
434
435 static void CDCEther_set_multicast( struct net_device *net )
436 {
437         ether_dev_t *ether_dev = net->priv;
438         int i;
439         __u8 *buff;
440         
441
442         // Tell the kernel to stop sending us frames while we get this
443         // all set up.
444         netif_stop_queue(net);
445
446       /* Note: do not reorder, GCC is clever about common statements. */
447         if (net->flags & IFF_PROMISC) {
448                 /* Unconditionally log net taps. */
449                 info( "%s: Promiscuous mode enabled", net->name);
450                 ether_dev->mode_flags = MODE_FLAG_PROMISCUOUS |
451                         MODE_FLAG_ALL_MULTICAST |
452                         MODE_FLAG_DIRECTED |
453                         MODE_FLAG_BROADCAST |
454                         MODE_FLAG_MULTICAST;
455         } else if (net->mc_count > ether_dev->wNumberMCFilters) {
456                 /* Too many to filter perfectly -- accept all multicasts. */
457                 info("%s: set too many MC filters, using allmulti", net->name);
458                 ether_dev->mode_flags = MODE_FLAG_ALL_MULTICAST |
459                         MODE_FLAG_DIRECTED |
460                         MODE_FLAG_BROADCAST |
461                         MODE_FLAG_MULTICAST;
462         } else if (net->flags & IFF_ALLMULTI) {
463                 /* Filter in software */
464                 info("%s: using allmulti", net->name);
465                 ether_dev->mode_flags = MODE_FLAG_ALL_MULTICAST |
466                         MODE_FLAG_DIRECTED |
467                         MODE_FLAG_BROADCAST |
468                         MODE_FLAG_MULTICAST;
469         } else {
470                 /* do multicast filtering in hardware */
471                 struct dev_mc_list *mclist;
472                 info("%s: set multicast filters", net->name);
473                 ether_dev->mode_flags = MODE_FLAG_ALL_MULTICAST |
474                         MODE_FLAG_DIRECTED |
475                         MODE_FLAG_BROADCAST |
476                         MODE_FLAG_MULTICAST;
477                 buff = kmalloc(6 * net->mc_count, in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
478                 for (i = 0, mclist = net->mc_list;
479                      mclist && i < net->mc_count;
480                      i++, mclist = mclist->next) {
481                         memcpy(&mclist->dmi_addr, &buff[i * 6], 6);
482                 }
483                 usb_control_msg(ether_dev->usb,
484                                 usb_sndctrlpipe(ether_dev->usb, 0),
485                                 SET_ETHERNET_MULTICAST_FILTER, /* request */
486                                 USB_TYPE_CLASS | USB_DIR_OUT | USB_RECIP_INTERFACE, /* request type */
487                                 cpu_to_le16(net->mc_count), /* value */
488                                 cpu_to_le16((u16)ether_dev->comm_interface), /* index */
489                                 buff,
490                                 (6* net->mc_count), /* size */
491                                 HZ); /* timeout */
492                 kfree(buff);
493         }
494  
495         CDC_SetEthernetPacketFilter(ether_dev);
496         
497         // Tell the kernel to start giving frames to us again.
498         netif_wake_queue(net);
499 }
500
501 //////////////////////////////////////////////////////////////////////////////
502 // Routines used to parse out the Functional Descriptors /////////////////////
503 //////////////////////////////////////////////////////////////////////////////
504
505 static int parse_header_functional_descriptor( int *bFunctionLength, 
506                                                int bDescriptorType, 
507                                                int bDescriptorSubtype,
508                                                unsigned char *data,
509                                                ether_dev_t *ether_dev,
510                                                int *requirements )
511 {
512         // Check to make sure we haven't seen one of these already.
513         if ( (~*requirements) & REQ_HDR_FUNC_DESCR ) {
514                 err( "Multiple Header Functional Descriptors found." );
515                 return -1;
516         }
517         
518         // Is it the right size???
519         if (*bFunctionLength != 5) {
520                 info( "Invalid length in Header Functional Descriptor" );
521                 // This is a hack to get around a particular device (NO NAMES)
522                 // It has this function length set to the length of the
523                 // whole class-specific descriptor
524                 *bFunctionLength = 5;
525         }
526         
527         // Nothing extremely useful here.
528         // We'll keep it for posterity
529         ether_dev->bcdCDC = data[0] + (data[1] << 8);
530         dbg( "Found Header descriptor, CDC version %x", ether_dev->bcdCDC);
531
532         // We've seen one of these
533         *requirements &= ~REQ_HDR_FUNC_DESCR;
534         
535         // It's all good.
536         return 0;
537 }
538
539 static int parse_union_functional_descriptor( int *bFunctionLength, 
540                                               int bDescriptorType, 
541                                               int bDescriptorSubtype,
542                                               unsigned char *data,
543                                               ether_dev_t *ether_dev,
544                                               int *requirements )
545 {
546         // Check to make sure we haven't seen one of these already.
547         if ( (~*requirements) & REQ_UNION_FUNC_DESCR ) {
548                 err( "Multiple Union Functional Descriptors found." );
549                 return -1;
550         }
551
552         // Is it the right size?
553         if (*bFunctionLength != 5) {
554                 // It is NOT the size we expected.
555                 err( "Unsupported length in Union Functional Descriptor" );
556                 return -1;
557         }
558         
559         // Sanity check of sorts
560         if (ether_dev->comm_interface != data[0]) {
561                 // This tells us that we are chasing the wrong comm
562                 // interface or we are crazy or something else weird.
563                 if (ether_dev->comm_interface == data[1]) {
564                         info( "Probably broken Union descriptor, fudging data interface" );
565                         // We'll need this in a few microseconds, 
566                         // so guess here, and hope for the best
567                         ether_dev->data_interface = data[0];
568                 } else {
569                         err( "Union Functional Descriptor is broken beyond repair" );
570                         return -1;
571                 }
572         } else{ // Descriptor is OK
573                 // We'll need this in a few microseconds!
574                 ether_dev->data_interface = data[1];
575         }
576
577         // We've seen one of these now.
578         *requirements &= ~REQ_UNION_FUNC_DESCR;
579         
580         // Done
581         return 0;
582 }
583
584 static int parse_ethernet_functional_descriptor( int *bFunctionLength, 
585                                                  int bDescriptorType, 
586                                                  int bDescriptorSubtype,
587                                                  unsigned char *data,
588                                                  ether_dev_t *ether_dev,
589                                                  int *requirements )
590 {
591         // Check to make sure we haven't seen one of these already.
592         if ( (~*requirements) & REQ_ETH_FUNC_DESCR ) {
593                 err( "Multiple Ethernet Functional Descriptors found." );
594                 return -1;
595         }
596         
597         // Is it the right size?
598         if (*bFunctionLength != 13) {
599                 err( "Invalid length in Ethernet Networking Functional Descriptor" );
600                 return -1;
601         }
602         
603         // Lots of goodies from this one.  They are all important.
604         ether_dev->iMACAddress = data[0];
605         ether_dev->bmEthernetStatistics = data[1] + (data[2] << 8) + (data[3] << 16) + (data[4] << 24);
606         ether_dev->wMaxSegmentSize = data[5] + (data[6] << 8);
607         ether_dev->wNumberMCFilters = (data[7] + (data[8] << 8)) & 0x00007FFF;
608         if (ether_dev->wNumberMCFilters > multicast_filter_limit) {
609                 ether_dev->wNumberMCFilters = multicast_filter_limit;
610                 }       
611         ether_dev->bNumberPowerFilters = data[9];
612         
613         // We've seen one of these now.
614         *requirements &= ~REQ_ETH_FUNC_DESCR;
615         
616         // That's all she wrote.
617         return 0;
618 }
619
620 static int parse_protocol_unit_functional_descriptor( int *bFunctionLength, 
621                                                       int bDescriptorType, 
622                                                       int bDescriptorSubtype,
623                                                       unsigned char *data,
624                                                       ether_dev_t *ether_dev,
625                                                       int *requirements )
626 {
627         // There should only be one type if we are sane
628         if (bDescriptorType != CS_INTERFACE) {
629                 info( "Invalid bDescriptorType found." );
630                 return -1;
631         }
632
633         // The Subtype tells the tale.
634         switch (bDescriptorSubtype){
635                 case 0x00:      // Header Functional Descriptor
636                         return parse_header_functional_descriptor( bFunctionLength,
637                                                                    bDescriptorType,
638                                                                    bDescriptorSubtype,
639                                                                    data,
640                                                                    ether_dev,
641                                                                    requirements );
642                         break;
643                 case 0x06:      // Union Functional Descriptor
644                         return parse_union_functional_descriptor( bFunctionLength,
645                                                                   bDescriptorType,
646                                                                   bDescriptorSubtype,
647                                                                   data,
648                                                                   ether_dev,
649                                                                   requirements );
650                         break;
651                 case 0x0F:      // Ethernet Networking Functional Descriptor
652                         return parse_ethernet_functional_descriptor( bFunctionLength,
653                                                                      bDescriptorType,
654                                                                      bDescriptorSubtype,
655                                                                      data,
656                                                                      ether_dev,
657                                                                      requirements );
658                         break;
659                 default:        // We don't support this at this time...
660                         // However that doesn't necessarily indicate an error.
661                         dbg( "Unexpected header type %x:", bDescriptorSubtype );
662                         return 0;
663         }
664         // How did we get here???
665         return -1;
666 }
667
668 static int parse_ethernet_class_information( unsigned char *data, int length, ether_dev_t *ether_dev )
669 {
670         int loc = 0;
671         int rc;
672         int bFunctionLength;
673         int bDescriptorType;
674         int bDescriptorSubtype;
675         int requirements = REQUIREMENTS_TOTAL;
676
677         // As long as there is something here, we will try to parse it
678         while (loc < length) {
679                 // Length
680                 bFunctionLength = data[loc];
681                 loc++;
682                 
683                 // Type
684                 bDescriptorType = data[loc];
685                 loc++;
686                 
687                 // Subtype
688                 bDescriptorSubtype = data[loc];
689                 loc++;
690                 
691                 // ship this off to be processed elsewhere.
692                 rc = parse_protocol_unit_functional_descriptor( &bFunctionLength, 
693                                                                 bDescriptorType, 
694                                                                 bDescriptorSubtype, 
695                                                                 &data[loc],
696                                                                 ether_dev,
697                                                                 &requirements );
698                 // Did it process okay?
699                 if (rc) {
700                         // Something was hosed somewhere.
701                         // No need to continue;
702                         err("Bad descriptor parsing: %x", rc );
703                         return -1;
704                 }
705                 // We have already taken three bytes.
706                 loc += (bFunctionLength - 3);
707         }
708         // Check to see if we got everything we need.
709         if (requirements) {
710                 // We missed some of the requirements...
711                 err( "Not all required functional descriptors present 0x%08X", requirements );
712                 return -1;
713         }
714         // We got everything.
715         return 0;
716 }
717
718 //////////////////////////////////////////////////////////////////////////////
719 // Routine to check for the existence of the Functional Descriptors //////////
720 //////////////////////////////////////////////////////////////////////////////
721
722 static int find_and_parse_ethernet_class_information( struct usb_device *device, ether_dev_t *ether_dev )
723 {
724         struct usb_config_descriptor *conf = NULL;
725         struct usb_interface *comm_intf_group = NULL;
726         struct usb_interface_descriptor *comm_intf = NULL;
727         int rc = -1;
728         // The assumption here is that find_ethernet_comm_interface
729         // and find_valid_configuration 
730         // have already filled in the information about where to find
731         // the a valid commication interface.
732
733         conf = &( device->config[ether_dev->configuration_num] );
734         comm_intf_group = &( conf->interface[ether_dev->comm_interface] );
735         comm_intf = &( comm_intf_group->altsetting[ether_dev->comm_interface_altset_num] );
736         // Let's check and see if it has the extra information we need...
737
738         if (comm_intf->extralen > 0) {
739                 // This is where the information is SUPPOSED to be.
740                 rc = parse_ethernet_class_information( comm_intf->extra, comm_intf->extralen, ether_dev );
741         } else if (conf->extralen > 0) {
742                 // This is a hack.  The spec says it should be at the interface 
743                 // location checked above.  However I have seen it here also.
744                 // This is the same device that requires the functional descriptor hack above
745                 warn( "Ethernet information found at device configuration.  This is broken." );
746                 rc = parse_ethernet_class_information( conf->extra, conf->extralen, ether_dev );
747         } else  {
748                 // I don't know where else to look.
749                 warn( "No ethernet information found." );
750                 rc = -1;
751         }
752         return rc;
753 }
754
755 //////////////////////////////////////////////////////////////////////////////
756 // Routines to verify the data interface /////////////////////////////////////
757 //////////////////////////////////////////////////////////////////////////////
758
759 static int get_data_interface_endpoints( struct usb_device *device, ether_dev_t *ether_dev )
760 {
761         struct usb_config_descriptor *conf = NULL;
762         struct usb_interface *data_intf_group = NULL;
763         struct usb_interface_descriptor *data_intf = NULL;
764         
765         // Walk through and get to the data interface we are checking.
766         conf = &( device->config[ether_dev->configuration_num] );
767         data_intf_group = &( conf->interface[ether_dev->data_interface] );
768         data_intf = &( data_intf_group->altsetting[ether_dev->data_interface_altset_num_with_traffic] );
769
770         // Start out assuming we won't find anything we can use
771         ether_dev->data_ep_in = 0;
772         ether_dev->data_ep_out = 0;
773         
774         // If these are not BULK endpoints, we don't want them
775         if ( data_intf->endpoint[0].bmAttributes != 0x02 ) {
776                 return -1;
777         } if ( data_intf->endpoint[1].bmAttributes != 0x02 ) {
778                 return -1;
779         }
780
781         // Check the first endpoint to see if it is IN or OUT
782         if ( data_intf->endpoint[0].bEndpointAddress & 0x80 ) {
783                 // This endpoint is IN
784                 ether_dev->data_ep_in = data_intf->endpoint[0].bEndpointAddress & 0x7F;
785         } else {
786                 // This endpoint is OUT
787                 ether_dev->data_ep_out = data_intf->endpoint[0].bEndpointAddress & 0x7F;
788                 ether_dev->data_ep_out_size = data_intf->endpoint[0].wMaxPacketSize;
789         }
790
791         // Check the second endpoint to see if it is IN or OUT
792         if ( data_intf->endpoint[1].bEndpointAddress & 0x80 ) {
793                 // This endpoint is IN
794                 ether_dev->data_ep_in = data_intf->endpoint[1].bEndpointAddress & 0x7F;
795         } else  {
796                 // This endpoint is OUT
797                 ether_dev->data_ep_out = data_intf->endpoint[1].bEndpointAddress & 0x7F;
798                 ether_dev->data_ep_out_size = data_intf->endpoint[1].wMaxPacketSize;
799         }
800         
801         // Now make sure we got both an IN and an OUT
802         if (ether_dev->data_ep_in && ether_dev->data_ep_out) {
803                 // We did get both, we are in good shape...
804                 info( "detected BULK OUT packets of size %d", ether_dev->data_ep_out_size );
805                 return 0;
806         }
807         return -1;
808 }
809
810 static int verify_ethernet_data_interface( struct usb_device *device, ether_dev_t *ether_dev )
811 {
812         struct usb_config_descriptor *conf = NULL;
813         struct usb_interface *data_intf_group = NULL;
814         struct usb_interface_descriptor *data_intf = NULL;
815         int rc = -1;
816         int status;
817         int altset_num;
818
819         // The assumption here is that parse_ethernet_class_information()
820         // and find_valid_configuration() 
821         // have already filled in the information about where to find
822         // a data interface
823         conf = &( device->config[ether_dev->configuration_num] );
824         data_intf_group = &( conf->interface[ether_dev->data_interface] );
825
826         // start out assuming we won't find what we are looking for.
827         ether_dev->data_interface_altset_num_with_traffic = -1;
828         ether_dev->data_bAlternateSetting_with_traffic = -1;
829         ether_dev->data_interface_altset_num_without_traffic = -1;
830         ether_dev->data_bAlternateSetting_without_traffic = -1;
831
832         // Walk through every possible setting for this interface until
833         // we find what makes us happy.
834         for ( altset_num = 0; altset_num < data_intf_group->num_altsetting; altset_num++ ) {
835                 data_intf = &( data_intf_group->altsetting[altset_num] );
836
837                 // Is this a data interface we like?
838                 if ( ( data_intf->bInterfaceClass == 0x0A )
839                    && ( data_intf->bInterfaceSubClass == 0x00 )
840                    && ( data_intf->bInterfaceProtocol == 0x00 ) ) {
841                         if ( data_intf->bNumEndpoints == 2 ) {
842                                 // We are required to have one of these.
843                                 // An interface with 2 endpoints to send Ethernet traffic back and forth
844                                 // It actually may be possible that the device might only
845                                 // communicate in a vendor specific manner.
846                                 // That would not be very nice.
847                                 // We can add that one later.
848                                 ether_dev->data_bInterfaceNumber = data_intf->bInterfaceNumber;
849                                 ether_dev->data_interface_altset_num_with_traffic = altset_num;
850                                 ether_dev->data_bAlternateSetting_with_traffic = data_intf->bAlternateSetting;
851                                 status = get_data_interface_endpoints( device, ether_dev );
852                                 if (!status) {
853                                         rc = 0;
854                                 }
855                         }
856                         if ( data_intf->bNumEndpoints == 0 ) {
857                                 // According to the spec we are SUPPOSED to have one of these
858                                 // In fact the device is supposed to come up in this state.
859                                 // However, I have seen a device that did not have such an interface.
860                                 // So it must be just optional for our driver...
861                                 ether_dev->data_bInterfaceNumber = data_intf->bInterfaceNumber;
862                                 ether_dev->data_interface_altset_num_without_traffic = altset_num;
863                                 ether_dev->data_bAlternateSetting_without_traffic = data_intf->bAlternateSetting;
864                         }
865                 }
866         }
867         return rc;
868 }
869
870 //////////////////////////////////////////////////////////////////////////////
871 // Routine to find a communication interface /////////////////////////////////
872 //////////////////////////////////////////////////////////////////////////////
873
874 static int find_ethernet_comm_interface( struct usb_device *device, ether_dev_t *ether_dev )
875 {
876         struct usb_config_descriptor *conf = NULL;
877         struct usb_interface *comm_intf_group = NULL;
878         struct usb_interface_descriptor *comm_intf = NULL;
879         int intf_num;
880         int altset_num;
881         int rc;
882
883         conf = &( device->config[ether_dev->configuration_num] );
884
885         // We need to check and see if any of these interfaces are something we want.
886         // Walk through each interface one at a time
887         for ( intf_num = 0; intf_num < conf->bNumInterfaces; intf_num++ ) {
888                 comm_intf_group = &( conf->interface[intf_num] );
889                 // Now for each of those interfaces, check every possible
890                 // alternate setting.
891                 for ( altset_num = 0; altset_num < comm_intf_group->num_altsetting; altset_num++ ) {
892                         comm_intf = &( comm_intf_group->altsetting[altset_num] );
893
894                         // Is this a communication class of interface of the
895                         // ethernet subclass variety.
896                         if ( ( comm_intf->bInterfaceClass == 0x02 )
897                            && ( comm_intf->bInterfaceSubClass == 0x06 )
898                            && ( comm_intf->bInterfaceProtocol == 0x00 ) ) {
899                                 if ( comm_intf->bNumEndpoints == 1 ) {
900                                         // Good, we found one, we will try this one
901                                         // Fill in the structure...
902                                         ether_dev->comm_interface = intf_num;
903                                         ether_dev->comm_bInterfaceNumber = comm_intf->bInterfaceNumber;
904                                         ether_dev->comm_interface_altset_num = altset_num;
905                                         ether_dev->comm_bAlternateSetting = comm_intf->bAlternateSetting;
906
907                                         // Look for the Ethernet Functional Descriptors
908                                         rc = find_and_parse_ethernet_class_information( device, ether_dev );
909                                         if (rc) {
910                                                 // Nope this was no good after all.
911                                                 continue;
912                                         }
913
914                                         // Check that we really can talk to the data
915                                         // interface 
916                                         // This includes # of endpoints, protocols,
917                                         // etc.
918                                         rc = verify_ethernet_data_interface( device, ether_dev );
919                                         if (rc) {
920                                                 // We got something we didn't like
921                                                 continue;
922                                         }
923                                         // This communication interface seems to give us everything
924                                         // we require.  We have all the ethernet info we need.
925                                         // Let's get out of here and go home right now.
926                                         return 0;
927                                 } else {
928                                         // bNumEndPoints != 1
929                                         // We found an interface that had the wrong number of 
930                                         // endpoints but would have otherwise been okay
931                                 } // end bNumEndpoints check.
932                         } // end interface specifics check.
933                 } // end for altset_num
934         } // end for intf_num
935         return -1;
936 }
937
938 //////////////////////////////////////////////////////////////////////////////
939 // Routine to go through all configurations and find one that ////////////////
940 // is an Ethernet Networking Device //////////////////////////////////////////
941 //////////////////////////////////////////////////////////////////////////////
942
943 static int find_valid_configuration( struct usb_device *device, ether_dev_t *ether_dev )
944 {
945         struct usb_config_descriptor *conf = NULL;
946         int conf_num;
947         int rc;
948
949         // We will try each and every possible configuration
950         for ( conf_num = 0; conf_num < device->descriptor.bNumConfigurations; conf_num++ ) {
951                 conf = &( device->config[conf_num] );
952
953                 // Our first requirement : 2 interfaces
954                 if ( conf->bNumInterfaces != 2 ) {
955                         // I currently don't know how to handle devices with any number of interfaces
956                         // other than 2.
957                         continue;
958                 }
959
960                 // This one passed our first check, fill in some 
961                 // useful data
962                 ether_dev->configuration_num = conf_num;
963                 ether_dev->bConfigurationValue = conf->bConfigurationValue;
964
965                 // Now run it through the ringers and see what comes
966                 // out the other side.
967                 rc = find_ethernet_comm_interface( device, ether_dev );
968
969                 // Check if we found an ethernet Communcation Device
970                 if ( !rc ) {
971                         // We found one.
972                         return 0;
973                 }
974         }
975         // None of the configurations suited us.
976         return -1;
977 }
978
979 //////////////////////////////////////////////////////////////////////////////
980 // Routine that checks a given configuration to see if any driver ////////////
981 // has claimed any of the devices interfaces /////////////////////////////////
982 //////////////////////////////////////////////////////////////////////////////
983
984 static int check_for_claimed_interfaces( struct usb_config_descriptor *config )
985 {
986         struct usb_interface *comm_intf_group;
987         int intf_num;
988
989         // Go through all the interfaces and make sure none are 
990         // claimed by anybody else.
991         for ( intf_num = 0; intf_num < config->bNumInterfaces; intf_num++ ) {
992                 comm_intf_group = &( config->interface[intf_num] );
993                 if ( usb_interface_claimed( comm_intf_group ) ) {
994                         // Somebody has beat us to this guy.
995                         // We can't change the configuration out from underneath of whoever
996                         // is using this device, so we will go ahead and give up.
997                         return -1;
998                 }
999         }
1000         // We made it all the way through.
1001         // I guess no one has claimed any of these interfaces.
1002         return 0;
1003 }
1004
1005 //////////////////////////////////////////////////////////////////////////////
1006 // Routines to ask for and set the kernel network interface's MAC address ////
1007 // Used by driver's probe routine ////////////////////////////////////////////
1008 //////////////////////////////////////////////////////////////////////////////
1009
1010 static inline unsigned char hex2dec( unsigned char digit )
1011 {
1012         // Is there a standard way to do this???
1013         // I have written this code TOO MANY times.
1014         if ( (digit >= '0') && (digit <= '9') ) {
1015                 return (digit - '0');
1016         }
1017         if ( (digit >= 'a') && (digit <= 'f') ) {
1018                 return (digit - 'a' + 10);
1019         }
1020         if ( (digit >= 'A') && (digit <= 'F') ) {
1021                 return (digit - 'A' + 10);
1022         }
1023         return 0;
1024 }
1025
1026 static void set_ethernet_addr( ether_dev_t *ether_dev )
1027 {
1028         unsigned char   mac_addr[6];
1029         int             i;
1030         int             len;
1031         unsigned char   buffer[13];
1032
1033         // Let's assume we don't get anything...
1034         mac_addr[0] = 0x00;
1035         mac_addr[1] = 0x00;
1036         mac_addr[2] = 0x00;
1037         mac_addr[3] = 0x00;
1038         mac_addr[4] = 0x00;
1039         mac_addr[5] = 0x00;
1040
1041         // Let's ask the device...
1042         len = usb_string(ether_dev->usb, ether_dev->iMACAddress, buffer, 13);
1043
1044         // Sanity check!
1045         if (len != 12)  {
1046                 // You gotta love failing sanity checks
1047                 err("Attempting to get MAC address returned %d bytes", len);
1048                 return;
1049         }
1050
1051         // Fill in the mac_addr
1052         for (i = 0; i < 6; i++) {
1053                 mac_addr[i] = ( hex2dec( buffer[2 * i] ) << 4 ) + hex2dec( buffer[2 * i + 1] );
1054         }
1055
1056         // Now copy it over to the kernel's network driver.
1057         memcpy( ether_dev->net->dev_addr, mac_addr, sizeof(mac_addr) );
1058 }
1059
1060 //////////////////////////////////////////////////////////////////////////////
1061 // Routine to print to syslog information about the driver ///////////////////
1062 // Used by driver's probe routine ////////////////////////////////////////////
1063 //////////////////////////////////////////////////////////////////////////////
1064
1065 void log_device_info(ether_dev_t *ether_dev)
1066 {
1067         int len;
1068         int string_num;
1069         unsigned char manu[256];
1070         unsigned char prod[256];
1071         unsigned char sern[256];
1072         unsigned char *mac_addr;
1073
1074         // Default empty strings in case we don't find a real one
1075         manu[0] = 0x00;
1076         prod[0] = 0x00;
1077         sern[0] = 0x00;
1078
1079         // Try to get the device Manufacturer
1080         string_num = ether_dev->usb->descriptor.iManufacturer;
1081         if (string_num) {
1082                 // Put it into its buffer
1083                 len = usb_string(ether_dev->usb, string_num, manu, 255);
1084                 // Just to be safe
1085                 manu[len] = 0x00;
1086         }
1087
1088         // Try to get the device Product Name
1089         string_num = ether_dev->usb->descriptor.iProduct;
1090         if (string_num) {
1091                 // Put it into its buffer
1092                 len = usb_string(ether_dev->usb, string_num, prod, 255);
1093                 // Just to be safe
1094                 prod[len] = 0x00;
1095         }
1096
1097         // Try to get the device Serial Number
1098         string_num = ether_dev->usb->descriptor.iSerialNumber;
1099         if (string_num) {
1100                 // Put it into its buffer
1101                 len = usb_string(ether_dev->usb, string_num, sern, 255);
1102                 // Just to be safe
1103                 sern[len] = 0x00;
1104         }
1105
1106         // This makes it easier for us to print
1107         mac_addr = ether_dev->net->dev_addr;
1108
1109         // Now send everything we found to the syslog
1110         info( "%s: %s %s %s %02X:%02X:%02X:%02X:%02X:%02X", 
1111               ether_dev->net->name, manu, prod, sern, mac_addr[0], 
1112               mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], 
1113               mac_addr[5] );
1114 }
1115
1116 /* Forward declaration */
1117 static struct usb_driver CDCEther_driver ;
1118
1119 //////////////////////////////////////////////////////////////////////////////
1120 // Module's probe routine ////////////////////////////////////////////////////
1121 // claims interfaces if they are for an Ethernet CDC /////////////////////////
1122 //////////////////////////////////////////////////////////////////////////////
1123
1124 static void * CDCEther_probe( struct usb_device *usb, unsigned int ifnum,
1125                              const struct usb_device_id *id)
1126 {
1127         struct net_device       *net;
1128         ether_dev_t             *ether_dev;
1129         int                     rc;
1130
1131         // First we should check the active configuration to see if 
1132         // any other driver has claimed any of the interfaces.
1133         if ( check_for_claimed_interfaces( usb->actconfig ) ) {
1134                 // Someone has already put there grubby paws on this device.
1135                 // We don't want it now...
1136                 return NULL;
1137         }
1138
1139         // We might be finding a device we can use.
1140         // We all go ahead and allocate our storage space.
1141         // We need to because we have to start filling in the data that
1142         // we are going to need later.
1143         if(!(ether_dev = kmalloc(sizeof(ether_dev_t), GFP_KERNEL))) {
1144                 err("out of memory allocating device structure");
1145                 return NULL;
1146         }
1147
1148         // Zero everything out.
1149         memset(ether_dev, 0, sizeof(ether_dev_t));
1150
1151         // Let's see if we can find a configuration we can use.
1152         rc = find_valid_configuration( usb, ether_dev );
1153         if (rc) {
1154                 // Nope we couldn't find one we liked.
1155                 // This device was not meant for us to control.
1156                 kfree( ether_dev );
1157                 return  NULL;
1158         }
1159
1160         // Now that we FOUND a configuration. let's try to make the 
1161         // device go into it.
1162         if ( usb_set_configuration( usb, ether_dev->bConfigurationValue ) ) {
1163                 err("usb_set_configuration() failed");
1164                 kfree( ether_dev );
1165                 return NULL;
1166         }
1167
1168         // Now set the communication interface up as required.
1169         if (usb_set_interface(usb, ether_dev->comm_bInterfaceNumber, ether_dev->comm_bAlternateSetting)) {
1170                 err("usb_set_interface() failed");
1171                 kfree( ether_dev );
1172                 return NULL;
1173         }
1174
1175         // Only turn traffic on right now if we must...
1176         if (ether_dev->data_interface_altset_num_without_traffic >= 0)  {
1177                 // We found an alternate setting for the data
1178                 // interface that allows us to turn off traffic.
1179                 // We should use it.
1180                 if (usb_set_interface( usb, 
1181                                        ether_dev->data_bInterfaceNumber, 
1182                                        ether_dev->data_bAlternateSetting_without_traffic)) {
1183                         err("usb_set_interface() failed");
1184                         kfree( ether_dev );
1185                         return NULL;
1186                 }
1187         } else  {
1188                 // We didn't find an alternate setting for the data
1189                 // interface that would let us turn off traffic.
1190                 // Oh well, let's go ahead and do what we must...
1191                 if (usb_set_interface( usb, 
1192                                        ether_dev->data_bInterfaceNumber, 
1193                                        ether_dev->data_bAlternateSetting_with_traffic)) {
1194                         err("usb_set_interface() failed");
1195                         kfree( ether_dev );
1196                         return NULL;
1197                 }
1198         }
1199
1200         // Now we need to get a kernel Ethernet interface.
1201         net = init_etherdev( NULL, 0 );
1202         if ( !net ) {
1203                 // Hmm...  The kernel is not sharing today...
1204                 // Fine, we didn't want it anyway...
1205                 err( "Unable to initialize ethernet device" );
1206                 kfree( ether_dev );
1207                 return  NULL;
1208         }
1209
1210         // Now that we have an ethernet device, let's set it up
1211         // (And I don't mean "set [it] up the bomb".)
1212         net->priv = ether_dev;
1213         net->open = CDCEther_open;
1214         net->stop = CDCEther_close;
1215         net->watchdog_timeo = CDC_ETHER_TX_TIMEOUT;
1216         net->tx_timeout = CDCEther_tx_timeout;   // TX timeout function
1217         net->do_ioctl = CDCEther_ioctl;
1218         net->hard_start_xmit = CDCEther_start_xmit;
1219         net->set_multicast_list = CDCEther_set_multicast;
1220         net->get_stats = CDCEther_netdev_stats;
1221         net->mtu = ether_dev->wMaxSegmentSize - 14;
1222
1223         // We'll keep track of this information for later...
1224         ether_dev->usb = usb;
1225         ether_dev->net = net;
1226         
1227         // and don't forget the MAC address.
1228         set_ethernet_addr( ether_dev );
1229
1230         // Send a message to syslog about what we are handling
1231         log_device_info( ether_dev );
1232
1233         // I claim this interface to be a CDC Ethernet Networking device
1234         usb_driver_claim_interface( &CDCEther_driver, 
1235                                     &(usb->config[ether_dev->configuration_num].interface[ether_dev->comm_interface]), 
1236                                     ether_dev );
1237         // I claim this interface to be a CDC Ethernet Networking device
1238         usb_driver_claim_interface( &CDCEther_driver, 
1239                                     &(usb->config[ether_dev->configuration_num].interface[ether_dev->data_interface]), 
1240                                     ether_dev );
1241
1242         // Does this REALLY do anything???
1243         usb_inc_dev_use( usb );
1244
1245         // TODO - last minute HACK
1246         ether_dev->comm_ep_in = 5;
1247
1248         // Okay, we are finally done...
1249         return NULL;
1250 }
1251
1252
1253 //////////////////////////////////////////////////////////////////////////////
1254 // Module's disconnect routine ///////////////////////////////////////////////
1255 // Called when the driver is unloaded or the device is unplugged /////////////
1256 // (Whichever happens first assuming the driver suceeded at its probe) ///////
1257 //////////////////////////////////////////////////////////////////////////////
1258
1259 static void CDCEther_disconnect( struct usb_device *usb, void *ptr )
1260 {
1261         ether_dev_t *ether_dev = ptr;
1262
1263         // Sanity check!!!
1264         if ( !ether_dev || !ether_dev->usb ) {
1265                 // We failed.  We are insane!!!
1266                 warn("unregistering non-existant device");
1267                 return;
1268         }
1269
1270         // Make sure we fail the sanity check if we try this again.
1271         ether_dev->usb = NULL;
1272         
1273         // It is possible that this function is called before
1274         // the "close" function.
1275         // This tells the close function we are already disconnected
1276         ether_dev->flags |= CDC_ETHER_UNPLUG;
1277         
1278         // We don't need the network device any more
1279         unregister_netdev( ether_dev->net );
1280         
1281         // For sanity checks
1282         ether_dev->net = NULL;
1283
1284         // I ask again, does this do anything???
1285         usb_dec_dev_use( usb );
1286
1287         // We are done with this interface
1288         usb_driver_release_interface( &CDCEther_driver, 
1289                                       &(usb->config[ether_dev->configuration_num].interface[ether_dev->comm_interface]) );
1290
1291         // We are done with this interface too
1292         usb_driver_release_interface( &CDCEther_driver, 
1293                                       &(usb->config[ether_dev->configuration_num].interface[ether_dev->data_interface]) );
1294
1295         // No more tied up kernel memory
1296         kfree( ether_dev );
1297         
1298         // This does no good, but it looks nice!
1299         ether_dev = NULL;
1300 }
1301
1302 //////////////////////////////////////////////////////////////////////////////
1303 // Driver info ///////////////////////////////////////////////////////////////
1304 //////////////////////////////////////////////////////////////////////////////
1305
1306 static struct usb_driver CDCEther_driver = {
1307         name:           "CDCEther",
1308         probe:          CDCEther_probe,
1309         disconnect:     CDCEther_disconnect,
1310         id_table:       CDCEther_ids,
1311 };
1312
1313 //////////////////////////////////////////////////////////////////////////////
1314 // init and exit routines called when driver is installed and uninstalled ////
1315 //////////////////////////////////////////////////////////////////////////////
1316
1317 int __init CDCEther_init(void)
1318 {
1319         info( "%s", version );
1320         return usb_register( &CDCEther_driver );
1321 }
1322
1323 void __exit CDCEther_exit(void)
1324 {
1325         usb_deregister( &CDCEther_driver );
1326 }
1327
1328 //////////////////////////////////////////////////////////////////////////////
1329 // Module info ///////////////////////////////////////////////////////////////
1330 //////////////////////////////////////////////////////////////////////////////
1331
1332 module_init( CDCEther_init );
1333 module_exit( CDCEther_exit );
1334
1335 MODULE_AUTHOR("Brad Hards and another");
1336 MODULE_DESCRIPTION("USB CDC Ethernet driver");
1337 MODULE_LICENSE("GPL");
1338
1339 MODULE_PARM (multicast_filter_limit, "i");
1340 MODULE_PARM_DESC (multicast_filter_limit, "CDCEther maximum number of filtered multicast addresses");
1341
1342 MODULE_DEVICE_TABLE (usb, CDCEther_ids);
1343
1344 //////////////////////////////////////////////////////////////////////////////
1345 // End of file ///////////////////////////////////////////////////////////////
1346 //////////////////////////////////////////////////////////////////////////////