1 // Portions of this file taken from
2 // Petko Manolov - Petkan (petkan@dce.bg)
3 // from his driver pegasus.c
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.
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.
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
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>
32 static const char *version = __FILE__ ": v0.98.5 22 Sep 2001 Brad Hards and another";
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 */
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.
48 static int multicast_filter_limit = 32767;
51 //////////////////////////////////////////////////////////////////////////////
52 // Callback routines from USB device /////////////////////////////////////////
53 //////////////////////////////////////////////////////////////////////////////
55 static void read_bulk_callback( struct urb *urb )
57 ether_dev_t *ether_dev = urb->context;
58 struct net_device *net;
59 int count = urb->actual_length, res;
63 if ( !ether_dev || !(ether_dev->flags & CDC_ETHER_RUNNING) ) {
64 dbg("BULK IN callback but driver is not active!");
69 if ( !netif_device_present(net) ) {
70 // Somebody killed our network interface...
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");
81 // We are busy, leave us alone!
82 ether_dev->flags |= CDC_ETHER_RX_BUSY;
84 switch ( urb->status ) {
87 case USB_ST_NORESPONSE:
88 dbg( "no repsonse in BULK IN" );
89 ether_dev->flags &= ~CDC_ETHER_RX_BUSY;
92 dbg( "%s: RX status %d", net->name, urb->status );
96 // Check to make sure we got some data...
102 // Tell the kernel we want some memory
103 if ( !(skb = dev_alloc_skb(count)) ) {
104 // We got no receive buffer.
108 // Here's where it came from
111 // Now we copy it over
112 eth_copy_and_sum(skb, ether_dev->rx_buff, count, 0);
116 // Not sure here either
117 skb->protocol = eth_type_trans(skb, net);
119 // Ship it off to the kernel
122 // update out statistics
123 ether_dev->stats.rx_packets++;
124 ether_dev->stats.rx_bytes += count;
127 // Prep the USB to wait for another frame
128 FILL_BULK_URB( ðer_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 );
133 // Give this to the USB subsystem so it can tell us
134 // when more data arrives.
135 if ( (res = usb_submit_urb(ðer_dev->rx_urb)) ) {
136 warn( __FUNCTION__ " failed submint rx_urb %d", res);
139 // We are no longer busy, show us the frames!!!
140 ether_dev->flags &= ~CDC_ETHER_RX_BUSY;
143 static void write_bulk_callback( struct urb *urb )
145 ether_dev_t *ether_dev = urb->context;
148 if ( !ether_dev || !(ether_dev->flags & CDC_ETHER_RUNNING) ) {
150 err( "write_bulk_callback: device not running" );
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" );
161 // Hmm... What on Earth could have happened???
163 info("%s: TX status %d", ether_dev->net->name, urb->status);
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 );
172 //static void intr_callback( struct urb *urb )
174 // ether_dev_t *ether_dev = urb->context;
175 // struct net_device *net;
181 // switch ( urb->status ) {
182 // case USB_ST_NOERROR:
184 // case USB_ST_URB_KILLED:
187 // info("intr status %d", urb->status);
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++;
205 //////////////////////////////////////////////////////////////////////////////
206 // Routines for turning net traffic on and off on the USB side ///////////////
207 //////////////////////////////////////////////////////////////////////////////
209 static inline int enable_net_traffic( ether_dev_t *ether_dev )
211 struct usb_device *usb = ether_dev->usb;
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.
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);
227 static inline void disable_net_traffic( ether_dev_t *ether_dev )
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.
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");
239 // Some devices just may not support this...
240 warn("No way to disable net traffic");
244 //////////////////////////////////////////////////////////////////////////////
245 // Callback routines for kernel Ethernet Device //////////////////////////////
246 //////////////////////////////////////////////////////////////////////////////
248 static void CDCEther_tx_timeout( struct net_device *net )
250 ether_dev_t *ether_dev = net->priv;
254 // Seems to be a case of insanity here
258 // Tell syslog we are hosed.
259 warn("%s: Tx timed out.", net->name);
261 // Tear the waiting frame off the list
262 ether_dev->tx_urb.transfer_flags |= USB_ASYNC_UNLINK;
263 usb_unlink_urb( ðer_dev->tx_urb );
266 ether_dev->stats.tx_errors++;
269 static int CDCEther_start_xmit( struct sk_buff *skb, struct net_device *net )
271 ether_dev_t *ether_dev = net->priv;
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
284 // Add one to make it NOT an exact multiple
285 count = skb->len + 1;
288 // Tell the kernel, "No more frames 'til we are done
290 netif_stop_queue( net );
292 // Copy it from kernel memory to OUR memory
293 memcpy(ether_dev->tx_buff, skb->data, skb->len);
295 // Fill in the URB for shipping it out.
296 FILL_BULK_URB( ðer_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 );
301 // Tell the URB how much it will be transporting today
302 ether_dev->tx_urb.transfer_buffer_length = count;
304 // Send the URB on its merry way.
305 if ((res = usb_submit_urb(ðer_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 );
314 // Okay, it went out.
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;
322 // We are done with the kernel's memory
329 static struct net_device_stats *CDCEther_netdev_stats( struct net_device *net )
332 return &((ether_dev_t *)net->priv)->stats;
335 static int CDCEther_open(struct net_device *net)
337 ether_dev_t *ether_dev = (ether_dev_t *)net->priv;
340 // We are finally getting used!
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 );
350 // Prep a receive URB
351 FILL_BULK_URB( ðer_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 );
356 // Put it out there so the device can send us stuff
357 if ( (res = usb_submit_urb(ðer_dev->rx_urb)) )
360 warn( __FUNCTION__ " failed rx_urb %d", res );
363 // Tell the kernel we are ready to start receiving from it
364 netif_start_queue( net );
366 // We are up and running.
367 ether_dev->flags |= CDC_ETHER_RUNNING;
369 // Let's get ready to move frames!!!
373 static int CDCEther_close( struct net_device *net )
375 ether_dev_t *ether_dev = net->priv;
377 // We are no longer running.
378 ether_dev->flags &= ~CDC_ETHER_RUNNING;
380 // Tell the kernel to stop sending us stuff
381 netif_stop_queue( net );
383 // If we are not already unplugged, turn off USB
385 if ( !(ether_dev->flags & CDC_ETHER_UNPLUG) ) {
386 disable_net_traffic( ether_dev );
389 // We don't need the URBs anymore.
390 usb_unlink_urb( ðer_dev->rx_urb );
391 usb_unlink_urb( ðer_dev->tx_urb );
392 usb_unlink_urb( ðer_dev->intr_urb );
394 // We are not being used now.
397 // That's it. I'm done.
401 static int CDCEther_ioctl( struct net_device *net, struct ifreq *rq, int cmd )
403 //__u16 *data = (__u16 *)&rq->ifr_data;
404 //ether_dev_t *ether_dev = net->priv;
406 // No support here yet.
407 // Do we need support???
411 case SIOCDEVPRIVATE+1:
413 case SIOCDEVPRIVATE+2:
421 static void CDC_SetEthernetPacketFilter (ether_dev_t *ether_dev)
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 */
435 static void CDCEther_set_multicast( struct net_device *net )
437 ether_dev_t *ether_dev = net->priv;
442 // Tell the kernel to stop sending us frames while we get this
444 netif_stop_queue(net);
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 |
453 MODE_FLAG_BROADCAST |
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 |
460 MODE_FLAG_BROADCAST |
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 |
467 MODE_FLAG_BROADCAST |
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 |
475 MODE_FLAG_BROADCAST |
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);
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 */
490 (6* net->mc_count), /* size */
495 CDC_SetEthernetPacketFilter(ether_dev);
497 // Tell the kernel to start giving frames to us again.
498 netif_wake_queue(net);
501 //////////////////////////////////////////////////////////////////////////////
502 // Routines used to parse out the Functional Descriptors /////////////////////
503 //////////////////////////////////////////////////////////////////////////////
505 static int parse_header_functional_descriptor( int *bFunctionLength,
507 int bDescriptorSubtype,
509 ether_dev_t *ether_dev,
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." );
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;
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);
532 // We've seen one of these
533 *requirements &= ~REQ_HDR_FUNC_DESCR;
539 static int parse_union_functional_descriptor( int *bFunctionLength,
541 int bDescriptorSubtype,
543 ether_dev_t *ether_dev,
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." );
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" );
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];
569 err( "Union Functional Descriptor is broken beyond repair" );
572 } else{ // Descriptor is OK
573 // We'll need this in a few microseconds!
574 ether_dev->data_interface = data[1];
577 // We've seen one of these now.
578 *requirements &= ~REQ_UNION_FUNC_DESCR;
584 static int parse_ethernet_functional_descriptor( int *bFunctionLength,
586 int bDescriptorSubtype,
588 ether_dev_t *ether_dev,
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." );
597 // Is it the right size?
598 if (*bFunctionLength != 13) {
599 err( "Invalid length in Ethernet Networking Functional Descriptor" );
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;
611 ether_dev->bNumberPowerFilters = data[9];
613 // We've seen one of these now.
614 *requirements &= ~REQ_ETH_FUNC_DESCR;
616 // That's all she wrote.
620 static int parse_protocol_unit_functional_descriptor( int *bFunctionLength,
622 int bDescriptorSubtype,
624 ether_dev_t *ether_dev,
627 // There should only be one type if we are sane
628 if (bDescriptorType != CS_INTERFACE) {
629 info( "Invalid bDescriptorType found." );
633 // The Subtype tells the tale.
634 switch (bDescriptorSubtype){
635 case 0x00: // Header Functional Descriptor
636 return parse_header_functional_descriptor( bFunctionLength,
643 case 0x06: // Union Functional Descriptor
644 return parse_union_functional_descriptor( bFunctionLength,
651 case 0x0F: // Ethernet Networking Functional Descriptor
652 return parse_ethernet_functional_descriptor( bFunctionLength,
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 );
664 // How did we get here???
668 static int parse_ethernet_class_information( unsigned char *data, int length, ether_dev_t *ether_dev )
674 int bDescriptorSubtype;
675 int requirements = REQUIREMENTS_TOTAL;
677 // As long as there is something here, we will try to parse it
678 while (loc < length) {
680 bFunctionLength = data[loc];
684 bDescriptorType = data[loc];
688 bDescriptorSubtype = data[loc];
691 // ship this off to be processed elsewhere.
692 rc = parse_protocol_unit_functional_descriptor( &bFunctionLength,
698 // Did it process okay?
700 // Something was hosed somewhere.
701 // No need to continue;
702 err("Bad descriptor parsing: %x", rc );
705 // We have already taken three bytes.
706 loc += (bFunctionLength - 3);
708 // Check to see if we got everything we need.
710 // We missed some of the requirements...
711 err( "Not all required functional descriptors present 0x%08X", requirements );
714 // We got everything.
718 //////////////////////////////////////////////////////////////////////////////
719 // Routine to check for the existence of the Functional Descriptors //////////
720 //////////////////////////////////////////////////////////////////////////////
722 static int find_and_parse_ethernet_class_information( struct usb_device *device, ether_dev_t *ether_dev )
724 struct usb_config_descriptor *conf = NULL;
725 struct usb_interface *comm_intf_group = NULL;
726 struct usb_interface_descriptor *comm_intf = NULL;
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.
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...
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 );
748 // I don't know where else to look.
749 warn( "No ethernet information found." );
755 //////////////////////////////////////////////////////////////////////////////
756 // Routines to verify the data interface /////////////////////////////////////
757 //////////////////////////////////////////////////////////////////////////////
759 static int get_data_interface_endpoints( struct usb_device *device, ether_dev_t *ether_dev )
761 struct usb_config_descriptor *conf = NULL;
762 struct usb_interface *data_intf_group = NULL;
763 struct usb_interface_descriptor *data_intf = NULL;
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] );
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;
774 // If these are not BULK endpoints, we don't want them
775 if ( data_intf->endpoint[0].bmAttributes != 0x02 ) {
777 } if ( data_intf->endpoint[1].bmAttributes != 0x02 ) {
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;
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;
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;
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;
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 );
810 static int verify_ethernet_data_interface( struct usb_device *device, ether_dev_t *ether_dev )
812 struct usb_config_descriptor *conf = NULL;
813 struct usb_interface *data_intf_group = NULL;
814 struct usb_interface_descriptor *data_intf = NULL;
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
823 conf = &( device->config[ether_dev->configuration_num] );
824 data_intf_group = &( conf->interface[ether_dev->data_interface] );
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;
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] );
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 );
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;
870 //////////////////////////////////////////////////////////////////////////////
871 // Routine to find a communication interface /////////////////////////////////
872 //////////////////////////////////////////////////////////////////////////////
874 static int find_ethernet_comm_interface( struct usb_device *device, ether_dev_t *ether_dev )
876 struct usb_config_descriptor *conf = NULL;
877 struct usb_interface *comm_intf_group = NULL;
878 struct usb_interface_descriptor *comm_intf = NULL;
883 conf = &( device->config[ether_dev->configuration_num] );
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] );
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;
907 // Look for the Ethernet Functional Descriptors
908 rc = find_and_parse_ethernet_class_information( device, ether_dev );
910 // Nope this was no good after all.
914 // Check that we really can talk to the data
916 // This includes # of endpoints, protocols,
918 rc = verify_ethernet_data_interface( device, ether_dev );
920 // We got something we didn't like
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.
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
938 //////////////////////////////////////////////////////////////////////////////
939 // Routine to go through all configurations and find one that ////////////////
940 // is an Ethernet Networking Device //////////////////////////////////////////
941 //////////////////////////////////////////////////////////////////////////////
943 static int find_valid_configuration( struct usb_device *device, ether_dev_t *ether_dev )
945 struct usb_config_descriptor *conf = NULL;
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] );
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
960 // This one passed our first check, fill in some
962 ether_dev->configuration_num = conf_num;
963 ether_dev->bConfigurationValue = conf->bConfigurationValue;
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 );
969 // Check if we found an ethernet Communcation Device
975 // None of the configurations suited us.
979 //////////////////////////////////////////////////////////////////////////////
980 // Routine that checks a given configuration to see if any driver ////////////
981 // has claimed any of the devices interfaces /////////////////////////////////
982 //////////////////////////////////////////////////////////////////////////////
984 static int check_for_claimed_interfaces( struct usb_config_descriptor *config )
986 struct usb_interface *comm_intf_group;
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.
1000 // We made it all the way through.
1001 // I guess no one has claimed any of these interfaces.
1005 //////////////////////////////////////////////////////////////////////////////
1006 // Routines to ask for and set the kernel network interface's MAC address ////
1007 // Used by driver's probe routine ////////////////////////////////////////////
1008 //////////////////////////////////////////////////////////////////////////////
1010 static inline unsigned char hex2dec( unsigned char digit )
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');
1017 if ( (digit >= 'a') && (digit <= 'f') ) {
1018 return (digit - 'a' + 10);
1020 if ( (digit >= 'A') && (digit <= 'F') ) {
1021 return (digit - 'A' + 10);
1026 static void set_ethernet_addr( ether_dev_t *ether_dev )
1028 unsigned char mac_addr[6];
1031 unsigned char buffer[13];
1033 // Let's assume we don't get anything...
1041 // Let's ask the device...
1042 len = usb_string(ether_dev->usb, ether_dev->iMACAddress, buffer, 13);
1046 // You gotta love failing sanity checks
1047 err("Attempting to get MAC address returned %d bytes", len);
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] );
1056 // Now copy it over to the kernel's network driver.
1057 memcpy( ether_dev->net->dev_addr, mac_addr, sizeof(mac_addr) );
1060 //////////////////////////////////////////////////////////////////////////////
1061 // Routine to print to syslog information about the driver ///////////////////
1062 // Used by driver's probe routine ////////////////////////////////////////////
1063 //////////////////////////////////////////////////////////////////////////////
1065 void log_device_info(ether_dev_t *ether_dev)
1069 unsigned char manu[256];
1070 unsigned char prod[256];
1071 unsigned char sern[256];
1072 unsigned char *mac_addr;
1074 // Default empty strings in case we don't find a real one
1079 // Try to get the device Manufacturer
1080 string_num = ether_dev->usb->descriptor.iManufacturer;
1082 // Put it into its buffer
1083 len = usb_string(ether_dev->usb, string_num, manu, 255);
1088 // Try to get the device Product Name
1089 string_num = ether_dev->usb->descriptor.iProduct;
1091 // Put it into its buffer
1092 len = usb_string(ether_dev->usb, string_num, prod, 255);
1097 // Try to get the device Serial Number
1098 string_num = ether_dev->usb->descriptor.iSerialNumber;
1100 // Put it into its buffer
1101 len = usb_string(ether_dev->usb, string_num, sern, 255);
1106 // This makes it easier for us to print
1107 mac_addr = ether_dev->net->dev_addr;
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],
1116 /* Forward declaration */
1117 static struct usb_driver CDCEther_driver ;
1119 //////////////////////////////////////////////////////////////////////////////
1120 // Module's probe routine ////////////////////////////////////////////////////
1121 // claims interfaces if they are for an Ethernet CDC /////////////////////////
1122 //////////////////////////////////////////////////////////////////////////////
1124 static void * CDCEther_probe( struct usb_device *usb, unsigned int ifnum,
1125 const struct usb_device_id *id)
1127 struct net_device *net;
1128 ether_dev_t *ether_dev;
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...
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");
1148 // Zero everything out.
1149 memset(ether_dev, 0, sizeof(ether_dev_t));
1151 // Let's see if we can find a configuration we can use.
1152 rc = find_valid_configuration( usb, ether_dev );
1154 // Nope we couldn't find one we liked.
1155 // This device was not meant for us to control.
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");
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");
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");
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");
1200 // Now we need to get a kernel Ethernet interface.
1201 net = init_etherdev( NULL, 0 );
1203 // Hmm... The kernel is not sharing today...
1204 // Fine, we didn't want it anyway...
1205 err( "Unable to initialize ethernet device" );
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;
1223 // We'll keep track of this information for later...
1224 ether_dev->usb = usb;
1225 ether_dev->net = net;
1227 // and don't forget the MAC address.
1228 set_ethernet_addr( ether_dev );
1230 // Send a message to syslog about what we are handling
1231 log_device_info( ether_dev );
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]),
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]),
1242 // Does this REALLY do anything???
1243 usb_inc_dev_use( usb );
1245 // TODO - last minute HACK
1246 ether_dev->comm_ep_in = 5;
1248 // Okay, we are finally done...
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 //////////////////////////////////////////////////////////////////////////////
1259 static void CDCEther_disconnect( struct usb_device *usb, void *ptr )
1261 ether_dev_t *ether_dev = ptr;
1264 if ( !ether_dev || !ether_dev->usb ) {
1265 // We failed. We are insane!!!
1266 warn("unregistering non-existant device");
1270 // Make sure we fail the sanity check if we try this again.
1271 ether_dev->usb = NULL;
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;
1278 // We don't need the network device any more
1279 unregister_netdev( ether_dev->net );
1281 // For sanity checks
1282 ether_dev->net = NULL;
1284 // I ask again, does this do anything???
1285 usb_dec_dev_use( usb );
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]) );
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]) );
1295 // No more tied up kernel memory
1298 // This does no good, but it looks nice!
1302 //////////////////////////////////////////////////////////////////////////////
1303 // Driver info ///////////////////////////////////////////////////////////////
1304 //////////////////////////////////////////////////////////////////////////////
1306 static struct usb_driver CDCEther_driver = {
1308 probe: CDCEther_probe,
1309 disconnect: CDCEther_disconnect,
1310 id_table: CDCEther_ids,
1313 //////////////////////////////////////////////////////////////////////////////
1314 // init and exit routines called when driver is installed and uninstalled ////
1315 //////////////////////////////////////////////////////////////////////////////
1317 int __init CDCEther_init(void)
1319 info( "%s", version );
1320 return usb_register( &CDCEther_driver );
1323 void __exit CDCEther_exit(void)
1325 usb_deregister( &CDCEther_driver );
1328 //////////////////////////////////////////////////////////////////////////////
1329 // Module info ///////////////////////////////////////////////////////////////
1330 //////////////////////////////////////////////////////////////////////////////
1332 module_init( CDCEther_init );
1333 module_exit( CDCEther_exit );
1335 MODULE_AUTHOR("Brad Hards and another");
1336 MODULE_DESCRIPTION("USB CDC Ethernet driver");
1337 MODULE_LICENSE("GPL");
1339 MODULE_PARM (multicast_filter_limit, "i");
1340 MODULE_PARM_DESC (multicast_filter_limit, "CDCEther maximum number of filtered multicast addresses");
1342 MODULE_DEVICE_TABLE (usb, CDCEther_ids);
1344 //////////////////////////////////////////////////////////////////////////////
1345 // End of file ///////////////////////////////////////////////////////////////
1346 //////////////////////////////////////////////////////////////////////////////