Advertisement
prismctg

trendchip

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