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
21 #include <linux/types.h>
22 #include <linux/jiffies.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 "cdc-ether.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
, struct pt_regs
*regs
)
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
) {
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 usb_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
);
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
, GFP_ATOMIC
)) ) {
136 warn("%s failed submint rx_urb %d", __FUNCTION__
, 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
, struct pt_regs
*regs
)
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 ) {
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
|= URB_ASYNC_UNLINK
;
263 usb_unlink_urb( ether_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
;
274 // Tell the kernel, "No more frames 'til we are done
276 netif_stop_queue( net
);
278 // Copy it from kernel memory to OUR memory
279 memcpy(ether_dev
->tx_buff
, skb
->data
, skb
->len
);
281 // Fill in the URB for shipping it out.
282 usb_fill_bulk_urb( ether_dev
->tx_urb
, ether_dev
->usb
,
283 usb_sndbulkpipe(ether_dev
->usb
, ether_dev
->data_ep_out
),
284 ether_dev
->tx_buff
, ether_dev
->wMaxSegmentSize
,
285 write_bulk_callback
, ether_dev
);
287 // Tell the URB how much it will be transporting today
288 ether_dev
->tx_urb
->transfer_buffer_length
= skb
->len
;
290 /* Deal with the zero length problem, I hope */
291 ether_dev
->tx_urb
->transfer_flags
|= URB_ZERO_PACKET
;
293 // Send the URB on its merry way.
294 if ((res
= usb_submit_urb(ether_dev
->tx_urb
, GFP_ATOMIC
))) {
295 // Hmm... It didn't go. Tell someone...
296 warn("failed tx_urb %d", res
);
297 // update some stats...
298 ether_dev
->stats
.tx_errors
++;
299 // and tell the kernel to give us another.
300 // Maybe we'll get it right next time.
301 netif_start_queue( net
);
303 // Okay, it went out.
305 ether_dev
->stats
.tx_packets
++;
306 ether_dev
->stats
.tx_bytes
+= skb
->len
;
307 // And tell the kernel when the last transmit occurred.
308 net
->trans_start
= jiffies
;
311 // We are done with the kernel's memory
318 static struct net_device_stats
*CDCEther_netdev_stats( struct net_device
*net
)
321 return &((ether_dev_t
*)net
->priv
)->stats
;
324 static int CDCEther_open(struct net_device
*net
)
326 ether_dev_t
*ether_dev
= (ether_dev_t
*)net
->priv
;
329 // Turn on the USB and let the packets flow!!!
330 if ( (res
= enable_net_traffic( ether_dev
)) ) {
331 err("%s can't enable_net_traffic() - %d", __FUNCTION__
, res
);
335 // Prep a receive URB
336 usb_fill_bulk_urb( ether_dev
->rx_urb
, ether_dev
->usb
,
337 usb_rcvbulkpipe(ether_dev
->usb
, ether_dev
->data_ep_in
),
338 ether_dev
->rx_buff
, ether_dev
->wMaxSegmentSize
,
339 read_bulk_callback
, ether_dev
);
341 // Put it out there so the device can send us stuff
342 if ( (res
= usb_submit_urb(ether_dev
->rx_urb
, GFP_KERNEL
)) )
345 warn("%s failed rx_urb %d", __FUNCTION__
, res
);
348 // Tell the kernel we are ready to start receiving from it
349 netif_start_queue( net
);
351 // We are up and running.
352 ether_dev
->flags
|= CDC_ETHER_RUNNING
;
354 // Let's get ready to move frames!!!
358 static int CDCEther_close( struct net_device
*net
)
360 ether_dev_t
*ether_dev
= net
->priv
;
362 // We are no longer running.
363 ether_dev
->flags
&= ~CDC_ETHER_RUNNING
;
365 // Tell the kernel to stop sending us stuff
366 netif_stop_queue( net
);
368 // If we are not already unplugged, turn off USB
370 if ( !(ether_dev
->flags
& CDC_ETHER_UNPLUG
) ) {
371 disable_net_traffic( ether_dev
);
374 // We don't need the URBs anymore.
375 usb_unlink_urb( ether_dev
->rx_urb
);
376 usb_unlink_urb( ether_dev
->tx_urb
);
377 usb_unlink_urb( ether_dev
->intr_urb
);
379 // That's it. I'm done.
383 static int CDCEther_ioctl( struct net_device
*net
, struct ifreq
*rq
, int cmd
)
385 //__u16 *data = (__u16 *)&rq->ifr_data;
386 //ether_dev_t *ether_dev = net->priv;
388 // No support here yet.
389 // Do we need support???
393 case SIOCDEVPRIVATE
+1:
395 case SIOCDEVPRIVATE
+2:
404 static void CDC_SetEthernetPacketFilter (ether_dev_t
*ether_dev
)
406 usb_control_msg(ether_dev
->usb
,
407 usb_sndctrlpipe(ether_dev
->usb
, 0),
408 SET_ETHERNET_PACKET_FILTER
, /* request */
409 USB_TYPE_CLASS
| USB_DIR_OUT
| USB_RECIP_INTERFACE
, /* request type */
410 cpu_to_le16(ether_dev
->mode_flags
), /* value */
411 cpu_to_le16((u16
)ether_dev
->comm_interface
), /* index */
419 static void CDCEther_set_multicast( struct net_device
*net
)
421 ether_dev_t
*ether_dev
= net
->priv
;
426 // Tell the kernel to stop sending us frames while we get this
428 // netif_stop_queue(net);
430 // FIXME: We hold xmit_lock. If you want to do the queue stuff you need
431 // to enable it from a completion handler
433 /* Note: do not reorder, GCC is clever about common statements. */
434 if (net
->flags
& IFF_PROMISC
) {
435 /* Unconditionally log net taps. */
436 info( "%s: Promiscuous mode enabled", net
->name
);
437 ether_dev
->mode_flags
= MODE_FLAG_PROMISCUOUS
|
438 MODE_FLAG_ALL_MULTICAST
|
440 MODE_FLAG_BROADCAST
|
442 } else if (net
->mc_count
> ether_dev
->wNumberMCFilters
) {
443 /* Too many to filter perfectly -- accept all multicasts. */
444 info("%s: set too many MC filters, using allmulti", net
->name
);
445 ether_dev
->mode_flags
= MODE_FLAG_ALL_MULTICAST
|
447 MODE_FLAG_BROADCAST
|
449 } else if (net
->flags
& IFF_ALLMULTI
) {
450 /* Filter in software */
451 info("%s: using allmulti", net
->name
);
452 ether_dev
->mode_flags
= MODE_FLAG_ALL_MULTICAST
|
454 MODE_FLAG_BROADCAST
|
457 /* do multicast filtering in hardware */
458 struct dev_mc_list
*mclist
;
459 info("%s: set multicast filters", net
->name
);
460 ether_dev
->mode_flags
= MODE_FLAG_ALL_MULTICAST
|
462 MODE_FLAG_BROADCAST
|
464 buff
= kmalloc(6 * net
->mc_count
, GFP_ATOMIC
);
465 for (i
= 0, mclist
= net
->mc_list
;
466 mclist
&& i
< net
->mc_count
;
467 i
++, mclist
= mclist
->next
) {
468 memcpy(&mclist
->dmi_addr
, &buff
[i
* 6], 6);
471 usb_control_msg(ether_dev
->usb
,
472 // FIXME: We hold a spinlock. You must not use a synchronous API
473 usb_sndctrlpipe(ether_dev
->usb
, 0),
474 SET_ETHERNET_MULTICAST_FILTER
, /* request */
475 USB_TYPE_CLASS
| USB_DIR_OUT
| USB_RECIP_INTERFACE
, /* request type */
476 cpu_to_le16(net
->mc_count
), /* value */
477 cpu_to_le16((u16
)ether_dev
->comm_interface
), /* index */
479 (6* net
->mc_count
), /* size */
486 CDC_SetEthernetPacketFilter(ether_dev
);
488 // Tell the kernel to start giving frames to us again.
489 // netif_wake_queue(net);
492 //////////////////////////////////////////////////////////////////////////////
493 // Routines used to parse out the Functional Descriptors /////////////////////
494 //////////////////////////////////////////////////////////////////////////////
496 static int parse_header_functional_descriptor( int *bFunctionLength
,
498 int bDescriptorSubtype
,
500 ether_dev_t
*ether_dev
,
503 // Check to make sure we haven't seen one of these already.
504 if ( (~*requirements
) & REQ_HDR_FUNC_DESCR
) {
505 err( "Multiple Header Functional Descriptors found." );
509 // Is it the right size???
510 if (*bFunctionLength
!= 5) {
511 info( "Invalid length in Header Functional Descriptor" );
512 // This is a hack to get around a particular device (NO NAMES)
513 // It has this function length set to the length of the
514 // whole class-specific descriptor
515 *bFunctionLength
= 5;
518 // Nothing extremely useful here.
519 // We'll keep it for posterity
520 ether_dev
->bcdCDC
= data
[0] + (data
[1] << 8);
521 dbg( "Found Header descriptor, CDC version %x", ether_dev
->bcdCDC
);
523 // We've seen one of these
524 *requirements
&= ~REQ_HDR_FUNC_DESCR
;
530 static int parse_union_functional_descriptor( int *bFunctionLength
,
532 int bDescriptorSubtype
,
534 ether_dev_t
*ether_dev
,
537 // Check to make sure we haven't seen one of these already.
538 if ( (~*requirements
) & REQ_UNION_FUNC_DESCR
) {
539 err( "Multiple Union Functional Descriptors found." );
543 // Is it the right size?
544 if (*bFunctionLength
!= 5) {
545 // It is NOT the size we expected.
546 err( "Unsupported length in Union Functional Descriptor" );
550 // Sanity check of sorts
551 if (ether_dev
->comm_interface
!= data
[0]) {
552 // This tells us that we are chasing the wrong comm
553 // interface or we are crazy or something else weird.
554 if (ether_dev
->comm_interface
== data
[1]) {
555 info( "Probably broken Union descriptor, fudging data interface" );
556 // We'll need this in a few microseconds,
557 // so guess here, and hope for the best
558 ether_dev
->data_interface
= data
[0];
560 err( "Union Functional Descriptor is broken beyond repair" );
563 } else{ // Descriptor is OK
564 // We'll need this in a few microseconds!
565 ether_dev
->data_interface
= data
[1];
568 // We've seen one of these now.
569 *requirements
&= ~REQ_UNION_FUNC_DESCR
;
575 static int parse_ethernet_functional_descriptor( int *bFunctionLength
,
577 int bDescriptorSubtype
,
579 ether_dev_t
*ether_dev
,
582 // Check to make sure we haven't seen one of these already.
583 if ( (~*requirements
) & REQ_ETH_FUNC_DESCR
) {
584 err( "Multiple Ethernet Functional Descriptors found." );
588 // Is it the right size?
589 if (*bFunctionLength
!= 13) {
590 err( "Invalid length in Ethernet Networking Functional Descriptor" );
594 // Lots of goodies from this one. They are all important.
595 ether_dev
->iMACAddress
= data
[0];
596 ether_dev
->bmEthernetStatistics
= data
[1] + (data
[2] << 8) + (data
[3] << 16) + (data
[4] << 24);
597 ether_dev
->wMaxSegmentSize
= data
[5] + (data
[6] << 8);
598 ether_dev
->wNumberMCFilters
= (data
[7] + (data
[8] << 8)) & 0x00007FFF;
599 if (ether_dev
->wNumberMCFilters
> multicast_filter_limit
) {
600 ether_dev
->wNumberMCFilters
= multicast_filter_limit
;
602 ether_dev
->bNumberPowerFilters
= data
[9];
604 // We've seen one of these now.
605 *requirements
&= ~REQ_ETH_FUNC_DESCR
;
607 // That's all she wrote.
611 static int parse_protocol_unit_functional_descriptor( int *bFunctionLength
,
613 int bDescriptorSubtype
,
615 ether_dev_t
*ether_dev
,
618 // There should only be one type if we are sane
619 if (bDescriptorType
!= CS_INTERFACE
) {
620 info( "Invalid bDescriptorType found." );
624 // The Subtype tells the tale.
625 switch (bDescriptorSubtype
){
626 case 0x00: // Header Functional Descriptor
627 return parse_header_functional_descriptor( bFunctionLength
,
634 case 0x06: // Union Functional Descriptor
635 return parse_union_functional_descriptor( bFunctionLength
,
642 case 0x0F: // Ethernet Networking Functional Descriptor
643 return parse_ethernet_functional_descriptor( bFunctionLength
,
650 default: // We don't support this at this time...
651 // However that doesn't necessarily indicate an error.
652 dbg( "Unexpected header type %x:", bDescriptorSubtype
);
655 // How did we get here???
659 static int parse_ethernet_class_information( unsigned char *data
, int length
, ether_dev_t
*ether_dev
)
665 int bDescriptorSubtype
;
666 int requirements
= REQUIREMENTS_TOTAL
;
668 // As long as there is something here, we will try to parse it
669 while (loc
< length
) {
671 bFunctionLength
= data
[loc
];
675 bDescriptorType
= data
[loc
];
679 bDescriptorSubtype
= data
[loc
];
682 // ship this off to be processed elsewhere.
683 rc
= parse_protocol_unit_functional_descriptor( &bFunctionLength
,
689 // Did it process okay?
691 // Something was hosed somewhere.
692 // No need to continue;
693 err("Bad descriptor parsing: %x", rc
);
696 // We have already taken three bytes.
697 loc
+= (bFunctionLength
- 3);
699 // Check to see if we got everything we need.
701 // We missed some of the requirements...
702 err( "Not all required functional descriptors present 0x%08X", requirements
);
705 // We got everything.
709 //////////////////////////////////////////////////////////////////////////////
710 // Routine to check for the existence of the Functional Descriptors //////////
711 //////////////////////////////////////////////////////////////////////////////
713 static int find_and_parse_ethernet_class_information( struct usb_device
*device
, ether_dev_t
*ether_dev
)
715 struct usb_host_config
*conf
= NULL
;
716 struct usb_interface
*comm_intf_group
= NULL
;
717 struct usb_host_interface
*comm_intf
= NULL
;
719 // The assumption here is that find_ethernet_comm_interface
720 // and find_valid_configuration
721 // have already filled in the information about where to find
722 // the a valid commication interface.
724 conf
= &( device
->config
[ether_dev
->configuration_num
] );
725 comm_intf_group
= &( conf
->interface
[ether_dev
->comm_interface
] );
726 comm_intf
= &( comm_intf_group
->altsetting
[ether_dev
->comm_interface_altset_num
] );
727 // Let's check and see if it has the extra information we need...
729 if (comm_intf
->extralen
> 0) {
730 // This is where the information is SUPPOSED to be.
731 rc
= parse_ethernet_class_information( comm_intf
->extra
, comm_intf
->extralen
, ether_dev
);
732 } else if (conf
->extralen
> 0) {
733 // This is a hack. The spec says it should be at the interface
734 // location checked above. However I have seen it here also.
735 // This is the same device that requires the functional descriptor hack above
736 warn( "Ethernet information found at device configuration. This is broken." );
737 rc
= parse_ethernet_class_information( conf
->extra
, conf
->extralen
, ether_dev
);
739 // I don't know where else to look.
740 warn( "No ethernet information found." );
746 //////////////////////////////////////////////////////////////////////////////
747 // Routines to verify the data interface /////////////////////////////////////
748 //////////////////////////////////////////////////////////////////////////////
750 static int get_data_interface_endpoints( struct usb_device
*device
, ether_dev_t
*ether_dev
)
752 struct usb_host_config
*conf
= NULL
;
753 struct usb_interface
*data_intf_group
= NULL
;
754 struct usb_host_interface
*data_intf
= NULL
;
756 // Walk through and get to the data interface we are checking.
757 conf
= &( device
->config
[ether_dev
->configuration_num
] );
758 data_intf_group
= &( conf
->interface
[ether_dev
->data_interface
] );
759 data_intf
= &( data_intf_group
->altsetting
[ether_dev
->data_interface_altset_num_with_traffic
] );
761 // Start out assuming we won't find anything we can use
762 ether_dev
->data_ep_in
= 0;
763 ether_dev
->data_ep_out
= 0;
765 // If these are not BULK endpoints, we don't want them
766 if ( data_intf
->endpoint
[0].desc
.bmAttributes
!= 0x02 ) {
768 } if ( data_intf
->endpoint
[1].desc
.bmAttributes
!= 0x02 ) {
772 // Check the first endpoint to see if it is IN or OUT
773 if ( data_intf
->endpoint
[0].desc
.bEndpointAddress
& 0x80 ) {
774 // This endpoint is IN
775 ether_dev
->data_ep_in
= data_intf
->endpoint
[0].desc
.bEndpointAddress
& 0x7F;
777 // This endpoint is OUT
778 ether_dev
->data_ep_out
= data_intf
->endpoint
[0].desc
.bEndpointAddress
& 0x7F;
779 ether_dev
->data_ep_out_size
= data_intf
->endpoint
[0].desc
.wMaxPacketSize
;
782 // Check the second endpoint to see if it is IN or OUT
783 if ( data_intf
->endpoint
[1].desc
.bEndpointAddress
& 0x80 ) {
784 // This endpoint is IN
785 ether_dev
->data_ep_in
= data_intf
->endpoint
[1].desc
.bEndpointAddress
& 0x7F;
787 // This endpoint is OUT
788 ether_dev
->data_ep_out
= data_intf
->endpoint
[1].desc
.bEndpointAddress
& 0x7F;
789 ether_dev
->data_ep_out_size
= data_intf
->endpoint
[1].desc
.wMaxPacketSize
;
792 // Now make sure we got both an IN and an OUT
793 if (ether_dev
->data_ep_in
&& ether_dev
->data_ep_out
) {
794 // We did get both, we are in good shape...
795 info( "detected BULK OUT packets of size %d", ether_dev
->data_ep_out_size
);
801 static int verify_ethernet_data_interface( struct usb_device
*device
, ether_dev_t
*ether_dev
)
803 struct usb_host_config
*conf
= NULL
;
804 struct usb_interface
*data_intf_group
= NULL
;
805 struct usb_interface_descriptor
*data_intf
= NULL
;
810 // The assumption here is that parse_ethernet_class_information()
811 // and find_valid_configuration()
812 // have already filled in the information about where to find
814 conf
= &( device
->config
[ether_dev
->configuration_num
] );
815 data_intf_group
= &( conf
->interface
[ether_dev
->data_interface
] );
817 // start out assuming we won't find what we are looking for.
818 ether_dev
->data_interface_altset_num_with_traffic
= -1;
819 ether_dev
->data_bAlternateSetting_with_traffic
= -1;
820 ether_dev
->data_interface_altset_num_without_traffic
= -1;
821 ether_dev
->data_bAlternateSetting_without_traffic
= -1;
823 // Walk through every possible setting for this interface until
824 // we find what makes us happy.
825 for ( altset_num
= 0; altset_num
< data_intf_group
->num_altsetting
; altset_num
++ ) {
826 data_intf
= &( data_intf_group
->altsetting
[altset_num
].desc
);
828 // Is this a data interface we like?
829 if ( ( data_intf
->bInterfaceClass
== 0x0A )
830 && ( data_intf
->bInterfaceSubClass
== 0x00 )
831 && ( data_intf
->bInterfaceProtocol
== 0x00 ) ) {
832 if ( data_intf
->bNumEndpoints
== 2 ) {
833 // We are required to have one of these.
834 // An interface with 2 endpoints to send Ethernet traffic back and forth
835 // It actually may be possible that the device might only
836 // communicate in a vendor specific manner.
837 // That would not be very nice.
838 // We can add that one later.
839 ether_dev
->data_bInterfaceNumber
= data_intf
->bInterfaceNumber
;
840 ether_dev
->data_interface_altset_num_with_traffic
= altset_num
;
841 ether_dev
->data_bAlternateSetting_with_traffic
= data_intf
->bAlternateSetting
;
842 status
= get_data_interface_endpoints( device
, ether_dev
);
847 if ( data_intf
->bNumEndpoints
== 0 ) {
848 // According to the spec we are SUPPOSED to have one of these
849 // In fact the device is supposed to come up in this state.
850 // However, I have seen a device that did not have such an interface.
851 // So it must be just optional for our driver...
852 ether_dev
->data_bInterfaceNumber
= data_intf
->bInterfaceNumber
;
853 ether_dev
->data_interface_altset_num_without_traffic
= altset_num
;
854 ether_dev
->data_bAlternateSetting_without_traffic
= data_intf
->bAlternateSetting
;
861 //////////////////////////////////////////////////////////////////////////////
862 // Routine to find a communication interface /////////////////////////////////
863 //////////////////////////////////////////////////////////////////////////////
865 static int find_ethernet_comm_interface( struct usb_device
*device
, ether_dev_t
*ether_dev
)
867 struct usb_host_config
*conf
= NULL
;
868 struct usb_interface
*comm_intf_group
= NULL
;
869 struct usb_interface_descriptor
*comm_intf
= NULL
;
874 conf
= &( device
->config
[ether_dev
->configuration_num
] );
876 // We need to check and see if any of these interfaces are something we want.
877 // Walk through each interface one at a time
878 for ( intf_num
= 0; intf_num
< conf
->desc
.bNumInterfaces
; intf_num
++ ) {
879 comm_intf_group
= &( conf
->interface
[intf_num
] );
880 // Now for each of those interfaces, check every possible
881 // alternate setting.
882 for ( altset_num
= 0; altset_num
< comm_intf_group
->num_altsetting
; altset_num
++ ) {
883 comm_intf
= &( comm_intf_group
->altsetting
[altset_num
].desc
);
885 // Is this a communication class of interface of the
886 // ethernet subclass variety.
887 if ( ( comm_intf
->bInterfaceClass
== 0x02 )
888 && ( comm_intf
->bInterfaceSubClass
== 0x06 )
889 && ( comm_intf
->bInterfaceProtocol
== 0x00 ) ) {
890 if ( comm_intf
->bNumEndpoints
== 1 ) {
891 // Good, we found one, we will try this one
892 // Fill in the structure...
893 ether_dev
->comm_interface
= intf_num
;
894 ether_dev
->comm_bInterfaceNumber
= comm_intf
->bInterfaceNumber
;
895 ether_dev
->comm_interface_altset_num
= altset_num
;
896 ether_dev
->comm_bAlternateSetting
= comm_intf
->bAlternateSetting
;
898 // Look for the Ethernet Functional Descriptors
899 rc
= find_and_parse_ethernet_class_information( device
, ether_dev
);
901 // Nope this was no good after all.
905 // Check that we really can talk to the data
907 // This includes # of endpoints, protocols,
909 rc
= verify_ethernet_data_interface( device
, ether_dev
);
911 // We got something we didn't like
914 // This communication interface seems to give us everything
915 // we require. We have all the ethernet info we need.
916 // Let's get out of here and go home right now.
919 // bNumEndPoints != 1
920 // We found an interface that had the wrong number of
921 // endpoints but would have otherwise been okay
922 } // end bNumEndpoints check.
923 } // end interface specifics check.
924 } // end for altset_num
925 } // end for intf_num
929 //////////////////////////////////////////////////////////////////////////////
930 // Routine to go through all configurations and find one that ////////////////
931 // is an Ethernet Networking Device //////////////////////////////////////////
932 //////////////////////////////////////////////////////////////////////////////
934 static int find_valid_configuration( struct usb_device
*device
, ether_dev_t
*ether_dev
)
936 struct usb_host_config
*conf
= NULL
;
940 // We will try each and every possible configuration
941 for ( conf_num
= 0; conf_num
< device
->descriptor
.bNumConfigurations
; conf_num
++ ) {
942 conf
= &( device
->config
[conf_num
] );
944 // Our first requirement : 2 interfaces
945 if ( conf
->desc
.bNumInterfaces
!= 2 ) {
946 // I currently don't know how to handle devices with any number of interfaces
951 // This one passed our first check, fill in some
953 ether_dev
->configuration_num
= conf_num
;
954 ether_dev
->bConfigurationValue
= conf
->desc
.bConfigurationValue
;
956 // Now run it through the ringers and see what comes
957 // out the other side.
958 rc
= find_ethernet_comm_interface( device
, ether_dev
);
960 // Check if we found an ethernet Communcation Device
966 // None of the configurations suited us.
970 //////////////////////////////////////////////////////////////////////////////
971 // Routine that checks a given configuration to see if any driver ////////////
972 // has claimed any of the devices interfaces /////////////////////////////////
973 //////////////////////////////////////////////////////////////////////////////
975 static int check_for_claimed_interfaces( struct usb_host_config
*config
)
977 struct usb_interface
*comm_intf_group
;
980 // Go through all the interfaces and make sure none are
981 // claimed by anybody else.
982 for ( intf_num
= 0; intf_num
< config
->desc
.bNumInterfaces
; intf_num
++ ) {
983 comm_intf_group
= &( config
->interface
[intf_num
] );
984 if ( usb_interface_claimed( comm_intf_group
) ) {
985 // Somebody has beat us to this guy.
986 // We can't change the configuration out from underneath of whoever
987 // is using this device, so we will go ahead and give up.
991 // We made it all the way through.
992 // I guess no one has claimed any of these interfaces.
996 //////////////////////////////////////////////////////////////////////////////
997 // Routines to ask for and set the kernel network interface's MAC address ////
998 // Used by driver's probe routine ////////////////////////////////////////////
999 //////////////////////////////////////////////////////////////////////////////
1001 static inline unsigned char hex2dec( unsigned char digit
)
1003 // Is there a standard way to do this???
1004 // I have written this code TOO MANY times.
1005 if ( (digit
>= '0') && (digit
<= '9') ) {
1006 return (digit
- '0');
1008 if ( (digit
>= 'a') && (digit
<= 'f') ) {
1009 return (digit
- 'a' + 10);
1011 if ( (digit
>= 'A') && (digit
<= 'F') ) {
1012 return (digit
- 'A' + 10);
1017 static void set_ethernet_addr( ether_dev_t
*ether_dev
)
1019 unsigned char mac_addr
[6];
1022 unsigned char buffer
[13];
1024 // Let's assume we don't get anything...
1032 // Let's ask the device...
1033 len
= usb_string(ether_dev
->usb
, ether_dev
->iMACAddress
, buffer
, 13);
1037 // You gotta love failing sanity checks
1038 err("Attempting to get MAC address returned %d bytes", len
);
1042 // Fill in the mac_addr
1043 for (i
= 0; i
< 6; i
++) {
1044 mac_addr
[i
] = ( hex2dec( buffer
[2 * i
] ) << 4 ) + hex2dec( buffer
[2 * i
+ 1] );
1047 // Now copy it over to the kernel's network driver.
1048 memcpy( ether_dev
->net
->dev_addr
, mac_addr
, sizeof(mac_addr
) );
1051 //////////////////////////////////////////////////////////////////////////////
1052 // Routine to print to syslog information about the driver ///////////////////
1053 // Used by driver's probe routine ////////////////////////////////////////////
1054 //////////////////////////////////////////////////////////////////////////////
1056 static void log_device_info(ether_dev_t
*ether_dev
)
1060 unsigned char *manu
= NULL
;
1061 unsigned char *prod
= NULL
;
1062 unsigned char *sern
= NULL
;
1063 unsigned char *mac_addr
;
1065 manu
= kmalloc(256, GFP_KERNEL
);
1066 prod
= kmalloc(256, GFP_KERNEL
);
1067 sern
= kmalloc(256, GFP_KERNEL
);
1068 if (!manu
|| !prod
|| !sern
) {
1069 dbg("no mem for log_device_info");
1073 // Default empty strings in case we don't find a real one
1078 // Try to get the device Manufacturer
1079 string_num
= ether_dev
->usb
->descriptor
.iManufacturer
;
1081 // Put it into its buffer
1082 len
= usb_string(ether_dev
->usb
, string_num
, manu
, 255);
1087 // Try to get the device Product Name
1088 string_num
= ether_dev
->usb
->descriptor
.iProduct
;
1090 // Put it into its buffer
1091 len
= usb_string(ether_dev
->usb
, string_num
, prod
, 255);
1096 // Try to get the device Serial Number
1097 string_num
= ether_dev
->usb
->descriptor
.iSerialNumber
;
1099 // Put it into its buffer
1100 len
= usb_string(ether_dev
->usb
, string_num
, sern
, 255);
1105 // This makes it easier for us to print
1106 mac_addr
= ether_dev
->net
->dev_addr
;
1108 // Now send everything we found to the syslog
1109 info( "%s: %s %s %s %02X:%02X:%02X:%02X:%02X:%02X",
1110 ether_dev
->net
->name
, manu
, prod
, sern
, mac_addr
[0],
1111 mac_addr
[1], mac_addr
[2], mac_addr
[3], mac_addr
[4],
1119 /* Forward declaration */
1120 static struct usb_driver CDCEther_driver
;
1122 //////////////////////////////////////////////////////////////////////////////
1123 // Module's probe routine ////////////////////////////////////////////////////
1124 // claims interfaces if they are for an Ethernet CDC /////////////////////////
1125 //////////////////////////////////////////////////////////////////////////////
1127 static int CDCEther_probe( struct usb_interface
*intf
,
1128 const struct usb_device_id
*id
)
1130 struct usb_device
*usb
= interface_to_usbdev(intf
);
1131 struct net_device
*net
;
1132 ether_dev_t
*ether_dev
;
1135 // First we should check the active configuration to see if
1136 // any other driver has claimed any of the interfaces.
1137 if ( check_for_claimed_interfaces( usb
->actconfig
) ) {
1138 // Someone has already put there grubby paws on this device.
1139 // We don't want it now...
1143 // We might be finding a device we can use.
1144 // We all go ahead and allocate our storage space.
1145 // We need to because we have to start filling in the data that
1146 // we are going to need later.
1147 if(!(ether_dev
= kmalloc(sizeof(ether_dev_t
), GFP_KERNEL
))) {
1148 err("out of memory allocating device structure");
1152 // Zero everything out.
1153 memset(ether_dev
, 0, sizeof(ether_dev_t
));
1155 ether_dev
->rx_urb
= usb_alloc_urb(0, GFP_KERNEL
);
1156 if (!ether_dev
->rx_urb
) {
1160 ether_dev
->tx_urb
= usb_alloc_urb(0, GFP_KERNEL
);
1161 if (!ether_dev
->tx_urb
) {
1162 usb_free_urb(ether_dev
->rx_urb
);
1166 ether_dev
->intr_urb
= usb_alloc_urb(0, GFP_KERNEL
);
1167 if (!ether_dev
->intr_urb
) {
1168 usb_free_urb(ether_dev
->tx_urb
);
1169 usb_free_urb(ether_dev
->rx_urb
);
1174 // Let's see if we can find a configuration we can use.
1175 rc
= find_valid_configuration( usb
, ether_dev
);
1177 // Nope we couldn't find one we liked.
1178 // This device was not meant for us to control.
1182 // Now that we FOUND a configuration. let's try to make the
1183 // device go into it.
1184 if ( usb_set_configuration( usb
, ether_dev
->bConfigurationValue
) ) {
1185 err("usb_set_configuration() failed");
1189 // Now set the communication interface up as required.
1190 if (usb_set_interface(usb
, ether_dev
->comm_bInterfaceNumber
, ether_dev
->comm_bAlternateSetting
)) {
1191 err("usb_set_interface() failed");
1195 // Only turn traffic on right now if we must...
1196 if (ether_dev
->data_interface_altset_num_without_traffic
>= 0) {
1197 // We found an alternate setting for the data
1198 // interface that allows us to turn off traffic.
1199 // We should use it.
1200 if (usb_set_interface( usb
,
1201 ether_dev
->data_bInterfaceNumber
,
1202 ether_dev
->data_bAlternateSetting_without_traffic
)) {
1203 err("usb_set_interface() failed");
1207 // We didn't find an alternate setting for the data
1208 // interface that would let us turn off traffic.
1209 // Oh well, let's go ahead and do what we must...
1210 if (usb_set_interface( usb
,
1211 ether_dev
->data_bInterfaceNumber
,
1212 ether_dev
->data_bAlternateSetting_with_traffic
)) {
1213 err("usb_set_interface() failed");
1218 // Now we need to get a kernel Ethernet interface.
1219 net
= alloc_etherdev(0);
1221 // Hmm... The kernel is not sharing today...
1222 // Fine, we didn't want it anyway...
1223 err( "Unable to initialize ethernet device" );
1227 // Now that we have an ethernet device, let's set it up
1228 // (And I don't mean "set [it] up the bomb".)
1229 net
->priv
= ether_dev
;
1230 SET_MODULE_OWNER(net
);
1231 net
->open
= CDCEther_open
;
1232 net
->stop
= CDCEther_close
;
1233 net
->watchdog_timeo
= CDC_ETHER_TX_TIMEOUT
;
1234 net
->tx_timeout
= CDCEther_tx_timeout
; // TX timeout function
1235 net
->do_ioctl
= CDCEther_ioctl
;
1236 net
->hard_start_xmit
= CDCEther_start_xmit
;
1237 net
->set_multicast_list
= CDCEther_set_multicast
;
1238 net
->get_stats
= CDCEther_netdev_stats
;
1239 net
->mtu
= ether_dev
->wMaxSegmentSize
- 14;
1241 // We'll keep track of this information for later...
1242 ether_dev
->usb
= usb
;
1243 ether_dev
->net
= net
;
1245 // and don't forget the MAC address.
1246 set_ethernet_addr( ether_dev
);
1248 // Send a message to syslog about what we are handling
1249 log_device_info( ether_dev
);
1251 // I claim this interface to be a CDC Ethernet Networking device
1252 usb_driver_claim_interface( &CDCEther_driver
,
1253 &(usb
->config
[ether_dev
->configuration_num
].interface
[ether_dev
->comm_interface
]),
1255 // I claim this interface to be a CDC Ethernet Networking device
1256 usb_driver_claim_interface( &CDCEther_driver
,
1257 &(usb
->config
[ether_dev
->configuration_num
].interface
[ether_dev
->data_interface
]),
1260 // Does this REALLY do anything???
1263 // TODO - last minute HACK
1264 ether_dev
->comm_ep_in
= 5;
1266 if (register_netdev(net
) != 0) {
1271 /* FIXME!!! This driver needs to be fixed to work with the new USB interface logic
1272 * this is not the correct thing to be doing here, we need to set the interface
1273 * driver specific data field.
1275 // Okay, we are finally done...
1280 usb_driver_release_interface( &CDCEther_driver
,
1281 &(usb
->config
[ether_dev
->configuration_num
].interface
[ether_dev
->comm_interface
]) );
1283 usb_driver_release_interface( &CDCEther_driver
,
1284 &(usb
->config
[ether_dev
->configuration_num
].interface
[ether_dev
->data_interface
]) );
1285 // bailing out with our tail between our knees
1287 usb_free_urb(ether_dev
->tx_urb
);
1288 usb_free_urb(ether_dev
->rx_urb
);
1289 usb_free_urb(ether_dev
->intr_urb
);
1295 //////////////////////////////////////////////////////////////////////////////
1296 // Module's disconnect routine ///////////////////////////////////////////////
1297 // Called when the driver is unloaded or the device is unplugged /////////////
1298 // (Whichever happens first assuming the driver suceeded at its probe) ///////
1299 //////////////////////////////////////////////////////////////////////////////
1301 static void CDCEther_disconnect( struct usb_interface
*intf
)
1303 ether_dev_t
*ether_dev
= usb_get_intfdata(intf
);
1304 struct usb_device
*usb
;
1306 usb_set_intfdata(intf
, NULL
);
1309 if ( !ether_dev
|| !ether_dev
->usb
) {
1310 // We failed. We are insane!!!
1311 warn("unregistering non-existant device");
1315 // Make sure we fail the sanity check if we try this again.
1316 ether_dev
->usb
= NULL
;
1318 usb
= interface_to_usbdev(intf
);
1320 // It is possible that this function is called before
1321 // the "close" function.
1322 // This tells the close function we are already disconnected
1323 ether_dev
->flags
|= CDC_ETHER_UNPLUG
;
1325 // We don't need the network device any more
1326 unregister_netdev( ether_dev
->net
);
1328 // For sanity checks
1329 ether_dev
->net
= NULL
;
1331 // I ask again, does this do anything???
1334 // We are done with this interface
1335 usb_driver_release_interface( &CDCEther_driver
,
1336 &(usb
->config
[ether_dev
->configuration_num
].interface
[ether_dev
->comm_interface
]) );
1338 // We are done with this interface too
1339 usb_driver_release_interface( &CDCEther_driver
,
1340 &(usb
->config
[ether_dev
->configuration_num
].interface
[ether_dev
->data_interface
]) );
1342 // No more tied up kernel memory
1343 usb_free_urb(ether_dev
->intr_urb
);
1344 usb_free_urb(ether_dev
->rx_urb
);
1345 usb_free_urb(ether_dev
->rx_urb
);
1348 // This does no good, but it looks nice!
1352 //////////////////////////////////////////////////////////////////////////////
1353 // Driver info ///////////////////////////////////////////////////////////////
1354 //////////////////////////////////////////////////////////////////////////////
1356 static struct usb_driver CDCEther_driver
= {
1357 .owner
= THIS_MODULE
,
1359 .probe
= CDCEther_probe
,
1360 .disconnect
= CDCEther_disconnect
,
1361 .id_table
= CDCEther_ids
,
1364 //////////////////////////////////////////////////////////////////////////////
1365 // init and exit routines called when driver is installed and uninstalled ////
1366 //////////////////////////////////////////////////////////////////////////////
1368 int __init
CDCEther_init(void)
1370 info( "%s", version
);
1371 return usb_register( &CDCEther_driver
);
1374 void __exit
CDCEther_exit(void)
1376 usb_deregister( &CDCEther_driver
);
1379 //////////////////////////////////////////////////////////////////////////////
1380 // Module info ///////////////////////////////////////////////////////////////
1381 //////////////////////////////////////////////////////////////////////////////
1383 module_init( CDCEther_init
);
1384 module_exit( CDCEther_exit
);
1386 MODULE_AUTHOR("Brad Hards and another");
1387 MODULE_DESCRIPTION("USB CDC Ethernet driver");
1388 MODULE_LICENSE("GPL");
1390 MODULE_PARM (multicast_filter_limit
, "i");
1391 MODULE_PARM_DESC (multicast_filter_limit
, "CDCEther maximum number of filtered multicast addresses");
1393 MODULE_DEVICE_TABLE (usb
, CDCEther_ids
);
1395 //////////////////////////////////////////////////////////////////////////////
1396 // End of file ///////////////////////////////////////////////////////////////
1397 //////////////////////////////////////////////////////////////////////////////