iPXE
ecm.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 #include <stdint.h>
00027 #include <errno.h>
00028 #include <ipxe/netdevice.h>
00029 #include <ipxe/ethernet.h>
00030 #include <ipxe/if_ether.h>
00031 #include <ipxe/base16.h>
00032 #include <ipxe/profile.h>
00033 #include <ipxe/usb.h>
00034 #include "ecm.h"
00035 
00036 /** @file
00037  *
00038  * CDC-ECM USB Ethernet driver
00039  *
00040  */
00041 
00042 /** Interrupt completion profiler */
00043 static struct profiler ecm_intr_profiler __profiler =
00044         { .name = "ecm.intr" };
00045 
00046 /** Bulk IN completion profiler */
00047 static struct profiler ecm_in_profiler __profiler =
00048         { .name = "ecm.in" };
00049 
00050 /** Bulk OUT profiler */
00051 static struct profiler ecm_out_profiler __profiler =
00052         { .name = "ecm.out" };
00053 
00054 /******************************************************************************
00055  *
00056  * Ethernet functional descriptor
00057  *
00058  ******************************************************************************
00059  */
00060 
00061 /**
00062  * Locate Ethernet functional descriptor
00063  *
00064  * @v config            Configuration descriptor
00065  * @v interface         Interface descriptor
00066  * @ret desc            Descriptor, or NULL if not found
00067  */
00068 struct ecm_ethernet_descriptor *
00069 ecm_ethernet_descriptor ( struct usb_configuration_descriptor *config,
00070                           struct usb_interface_descriptor *interface ) {
00071         struct ecm_ethernet_descriptor *desc;
00072 
00073         for_each_interface_descriptor ( desc, config, interface ) {
00074                 if ( ( desc->header.type == USB_CS_INTERFACE_DESCRIPTOR ) &&
00075                      ( desc->subtype == CDC_SUBTYPE_ETHERNET ) )
00076                         return desc;
00077         }
00078         return NULL;
00079 }
00080 
00081 /**
00082  * Get hardware MAC address
00083  *
00084  * @v usb               USB device
00085  * @v desc              Ethernet functional descriptor
00086  * @v hw_addr           Hardware address to fill in
00087  * @ret rc              Return status code
00088  */
00089 int ecm_fetch_mac ( struct usb_device *usb,
00090                     struct ecm_ethernet_descriptor *desc, uint8_t *hw_addr ) {
00091         char buf[ base16_encoded_len ( ETH_ALEN ) + 1 /* NUL */ ];
00092         int len;
00093         int rc;
00094 
00095         /* Fetch MAC address string */
00096         len = usb_get_string_descriptor ( usb, desc->mac, 0, buf,
00097                                           sizeof ( buf ) );
00098         if ( len < 0 ) {
00099                 rc = len;
00100                 return rc;
00101         }
00102 
00103         /* Sanity check */
00104         if ( len != ( ( int ) ( sizeof ( buf ) - 1 /* NUL */ ) ) ) {
00105                 DBGC ( usb, "USB %s has invalid ECM MAC \"%s\"\n",
00106                        usb->name, buf );
00107                 return -EINVAL;
00108         }
00109 
00110         /* Decode MAC address */
00111         len = base16_decode ( buf, hw_addr, ETH_ALEN );
00112         if ( len < 0 ) {
00113                 rc = len;
00114                 DBGC ( usb, "USB %s could not decode ECM MAC \"%s\": %s\n",
00115                        usb->name, buf, strerror ( rc ) );
00116                 return rc;
00117         }
00118 
00119         return 0;
00120 }
00121 
00122 /******************************************************************************
00123  *
00124  * CDC-ECM communications interface
00125  *
00126  ******************************************************************************
00127  */
00128 
00129 /**
00130  * Complete interrupt transfer
00131  *
00132  * @v ep                USB endpoint
00133  * @v iobuf             I/O buffer
00134  * @v rc                Completion status code
00135  */
00136 static void ecm_intr_complete ( struct usb_endpoint *ep,
00137                                 struct io_buffer *iobuf, int rc ) {
00138         struct ecm_device *ecm = container_of ( ep, struct ecm_device,
00139                                                 usbnet.intr );
00140         struct net_device *netdev = ecm->netdev;
00141         struct usb_setup_packet *message;
00142         size_t len = iob_len ( iobuf );
00143 
00144         /* Profile completions */
00145         profile_start ( &ecm_intr_profiler );
00146 
00147         /* Ignore packets cancelled when the endpoint closes */
00148         if ( ! ep->open )
00149                 goto ignore;
00150 
00151         /* Drop packets with errors */
00152         if ( rc != 0 ) {
00153                 DBGC ( ecm, "ECM %p interrupt failed: %s\n",
00154                        ecm, strerror ( rc ) );
00155                 DBGC_HDA ( ecm, 0, iobuf->data, iob_len ( iobuf ) );
00156                 goto error;
00157         }
00158 
00159         /* Extract message header */
00160         if ( len < sizeof ( *message ) ) {
00161                 DBGC ( ecm, "ECM %p underlength interrupt:\n", ecm );
00162                 DBGC_HDA ( ecm, 0, iobuf->data, iob_len ( iobuf ) );
00163                 rc = -EINVAL;
00164                 goto error;
00165         }
00166         message = iobuf->data;
00167 
00168         /* Parse message header */
00169         switch ( message->request ) {
00170 
00171         case cpu_to_le16 ( CDC_NETWORK_CONNECTION ) :
00172                 if ( message->value && ! netdev_link_ok ( netdev ) ) {
00173                         DBGC ( ecm, "ECM %p link up\n", ecm );
00174                         netdev_link_up ( netdev );
00175                 } else if ( netdev_link_ok ( netdev ) && ! message->value ) {
00176                         DBGC ( ecm, "ECM %p link down\n", ecm );
00177                         netdev_link_down ( netdev );
00178                 }
00179                 break;
00180 
00181         case cpu_to_le16 ( CDC_CONNECTION_SPEED_CHANGE ) :
00182                 /* Ignore */
00183                 break;
00184 
00185         default:
00186                 DBGC ( ecm, "ECM %p unrecognised interrupt:\n", ecm );
00187                 DBGC_HDA ( ecm, 0, iobuf->data, iob_len ( iobuf ) );
00188                 rc = -ENOTSUP;
00189                 goto error;
00190         }
00191 
00192         /* Free I/O buffer */
00193         free_iob ( iobuf );
00194         profile_stop ( &ecm_intr_profiler );
00195 
00196         return;
00197 
00198  error:
00199         netdev_rx_err ( netdev, iob_disown ( iobuf ), rc );
00200  ignore:
00201         free_iob ( iobuf );
00202         return;
00203 }
00204 
00205 /** Interrupt endpoint operations */
00206 static struct usb_endpoint_driver_operations ecm_intr_operations = {
00207         .complete = ecm_intr_complete,
00208 };
00209 
00210 /******************************************************************************
00211  *
00212  * CDC-ECM data interface
00213  *
00214  ******************************************************************************
00215  */
00216 
00217 /**
00218  * Complete bulk IN transfer
00219  *
00220  * @v ep                USB endpoint
00221  * @v iobuf             I/O buffer
00222  * @v rc                Completion status code
00223  */
00224 static void ecm_in_complete ( struct usb_endpoint *ep, struct io_buffer *iobuf,
00225                               int rc ) {
00226         struct ecm_device *ecm = container_of ( ep, struct ecm_device,
00227                                                 usbnet.in );
00228         struct net_device *netdev = ecm->netdev;
00229 
00230         /* Profile receive completions */
00231         profile_start ( &ecm_in_profiler );
00232 
00233         /* Ignore packets cancelled when the endpoint closes */
00234         if ( ! ep->open )
00235                 goto ignore;
00236 
00237         /* Record USB errors against the network device */
00238         if ( rc != 0 ) {
00239                 DBGC ( ecm, "ECM %p bulk IN failed: %s\n",
00240                        ecm, strerror ( rc ) );
00241                 goto error;
00242         }
00243 
00244         /* Hand off to network stack */
00245         netdev_rx ( netdev, iob_disown ( iobuf ) );
00246 
00247         profile_stop ( &ecm_in_profiler );
00248         return;
00249 
00250  error:
00251         netdev_rx_err ( netdev, iob_disown ( iobuf ), rc );
00252  ignore:
00253         free_iob ( iobuf );
00254 }
00255 
00256 /** Bulk IN endpoint operations */
00257 static struct usb_endpoint_driver_operations ecm_in_operations = {
00258         .complete = ecm_in_complete,
00259 };
00260 
00261 /**
00262  * Transmit packet
00263  *
00264  * @v ecm               CDC-ECM device
00265  * @v iobuf             I/O buffer
00266  * @ret rc              Return status code
00267  */
00268 static int ecm_out_transmit ( struct ecm_device *ecm,
00269                               struct io_buffer *iobuf ) {
00270         int rc;
00271 
00272         /* Profile transmissions */
00273         profile_start ( &ecm_out_profiler );
00274 
00275         /* Enqueue I/O buffer */
00276         if ( ( rc = usb_stream ( &ecm->usbnet.out, iobuf, 1 ) ) != 0 )
00277                 return rc;
00278 
00279         profile_stop ( &ecm_out_profiler );
00280         return 0;
00281 }
00282 
00283 /**
00284  * Complete bulk OUT transfer
00285  *
00286  * @v ep                USB endpoint
00287  * @v iobuf             I/O buffer
00288  * @v rc                Completion status code
00289  */
00290 static void ecm_out_complete ( struct usb_endpoint *ep, struct io_buffer *iobuf,
00291                                int rc ) {
00292         struct ecm_device *ecm = container_of ( ep, struct ecm_device,
00293                                                 usbnet.out );
00294         struct net_device *netdev = ecm->netdev;
00295 
00296         /* Report TX completion */
00297         netdev_tx_complete_err ( netdev, iobuf, rc );
00298 }
00299 
00300 /** Bulk OUT endpoint operations */
00301 static struct usb_endpoint_driver_operations ecm_out_operations = {
00302         .complete = ecm_out_complete,
00303 };
00304 
00305 /******************************************************************************
00306  *
00307  * Network device interface
00308  *
00309  ******************************************************************************
00310  */
00311 
00312 /**
00313  * Open network device
00314  *
00315  * @v netdev            Network device
00316  * @ret rc              Return status code
00317  */
00318 static int ecm_open ( struct net_device *netdev ) {
00319         struct ecm_device *ecm = netdev->priv;
00320         struct usb_device *usb = ecm->usb;
00321         unsigned int filter;
00322         int rc;
00323 
00324         /* Open USB network device */
00325         if ( ( rc = usbnet_open ( &ecm->usbnet ) ) != 0 ) {
00326                 DBGC ( ecm, "ECM %p could not open: %s\n",
00327                        ecm, strerror ( rc ) );
00328                 goto err_open;
00329         }
00330 
00331         /* Set packet filter */
00332         filter = ( ECM_PACKET_TYPE_PROMISCUOUS |
00333                    ECM_PACKET_TYPE_ALL_MULTICAST |
00334                    ECM_PACKET_TYPE_DIRECTED |
00335                    ECM_PACKET_TYPE_BROADCAST );
00336         if ( ( rc = usb_control ( usb, ECM_SET_ETHERNET_PACKET_FILTER,
00337                                   filter, ecm->usbnet.comms, NULL, 0 ) ) != 0 ){
00338                 DBGC ( ecm, "ECM %p could not set packet filter: %s\n",
00339                        ecm, strerror ( rc ) );
00340                 goto err_set_filter;
00341         }
00342 
00343         return 0;
00344 
00345  err_set_filter:
00346         usbnet_close ( &ecm->usbnet );
00347  err_open:
00348         return rc;
00349 }
00350 
00351 /**
00352  * Close network device
00353  *
00354  * @v netdev            Network device
00355  */
00356 static void ecm_close ( struct net_device *netdev ) {
00357         struct ecm_device *ecm = netdev->priv;
00358 
00359         /* Close USB network device */
00360         usbnet_close ( &ecm->usbnet );
00361 }
00362 
00363 /**
00364  * Transmit packet
00365  *
00366  * @v netdev            Network device
00367  * @v iobuf             I/O buffer
00368  * @ret rc              Return status code
00369  */
00370 static int ecm_transmit ( struct net_device *netdev,
00371                           struct io_buffer *iobuf ) {
00372         struct ecm_device *ecm = netdev->priv;
00373         int rc;
00374 
00375         /* Transmit packet */
00376         if ( ( rc = ecm_out_transmit ( ecm, iobuf ) ) != 0 )
00377                 return rc;
00378 
00379         return 0;
00380 }
00381 
00382 /**
00383  * Poll for completed and received packets
00384  *
00385  * @v netdev            Network device
00386  */
00387 static void ecm_poll ( struct net_device *netdev ) {
00388         struct ecm_device *ecm = netdev->priv;
00389         int rc;
00390 
00391         /* Poll USB bus */
00392         usb_poll ( ecm->bus );
00393 
00394         /* Refill endpoints */
00395         if ( ( rc = usbnet_refill ( &ecm->usbnet ) ) != 0 )
00396                 netdev_rx_err ( netdev, NULL, rc );
00397 }
00398 
00399 /** CDC-ECM network device operations */
00400 static struct net_device_operations ecm_operations = {
00401         .open           = ecm_open,
00402         .close          = ecm_close,
00403         .transmit       = ecm_transmit,
00404         .poll           = ecm_poll,
00405 };
00406 
00407 /******************************************************************************
00408  *
00409  * USB interface
00410  *
00411  ******************************************************************************
00412  */
00413 
00414 /**
00415  * Probe device
00416  *
00417  * @v func              USB function
00418  * @v config            Configuration descriptor
00419  * @ret rc              Return status code
00420  */
00421 static int ecm_probe ( struct usb_function *func,
00422                        struct usb_configuration_descriptor *config ) {
00423         struct usb_device *usb = func->usb;
00424         struct net_device *netdev;
00425         struct ecm_device *ecm;
00426         struct usb_interface_descriptor *comms;
00427         struct ecm_ethernet_descriptor *ethernet;
00428         int rc;
00429 
00430         /* Allocate and initialise structure */
00431         netdev = alloc_etherdev ( sizeof ( *ecm ) );
00432         if ( ! netdev ) {
00433                 rc = -ENOMEM;
00434                 goto err_alloc;
00435         }
00436         netdev_init ( netdev, &ecm_operations );
00437         netdev->dev = &func->dev;
00438         ecm = netdev->priv;
00439         memset ( ecm, 0, sizeof ( *ecm ) );
00440         ecm->usb = usb;
00441         ecm->bus = usb->port->hub->bus;
00442         ecm->netdev = netdev;
00443         usbnet_init ( &ecm->usbnet, func, &ecm_intr_operations,
00444                       &ecm_in_operations, &ecm_out_operations );
00445         usb_refill_init ( &ecm->usbnet.intr, 0, 0, ECM_INTR_MAX_FILL );
00446         usb_refill_init ( &ecm->usbnet.in, 0, ECM_IN_MTU, ECM_IN_MAX_FILL );
00447         DBGC ( ecm, "ECM %p on %s\n", ecm, func->name );
00448 
00449         /* Describe USB network device */
00450         if ( ( rc = usbnet_describe ( &ecm->usbnet, config ) ) != 0 ) {
00451                 DBGC ( ecm, "ECM %p could not describe: %s\n",
00452                        ecm, strerror ( rc ) );
00453                 goto err_describe;
00454         }
00455 
00456         /* Locate Ethernet descriptor */
00457         comms = usb_interface_descriptor ( config, ecm->usbnet.comms, 0 );
00458         assert ( comms != NULL );
00459         ethernet = ecm_ethernet_descriptor ( config, comms );
00460         if ( ! ethernet ) {
00461                 DBGC ( ecm, "ECM %p has no Ethernet descriptor\n", ecm );
00462                 rc = -EINVAL;
00463                 goto err_ethernet;
00464         }
00465 
00466         /* Fetch MAC address */
00467         if ( ( rc = ecm_fetch_mac ( usb, ethernet, netdev->hw_addr ) ) != 0 ) {
00468                 DBGC ( ecm, "ECM %p could not fetch MAC address: %s\n",
00469                        ecm, strerror ( rc ) );
00470                 goto err_fetch_mac;
00471         }
00472 
00473         /* Register network device */
00474         if ( ( rc = register_netdev ( netdev ) ) != 0 )
00475                 goto err_register;
00476 
00477         usb_func_set_drvdata ( func, ecm );
00478         return 0;
00479 
00480         unregister_netdev ( netdev );
00481  err_register:
00482  err_fetch_mac:
00483  err_ethernet:
00484  err_describe:
00485         netdev_nullify ( netdev );
00486         netdev_put ( netdev );
00487  err_alloc:
00488         return rc;
00489 }
00490 
00491 /**
00492  * Remove device
00493  *
00494  * @v func              USB function
00495  */
00496 static void ecm_remove ( struct usb_function *func ) {
00497         struct ecm_device *ecm = usb_func_get_drvdata ( func );
00498         struct net_device *netdev = ecm->netdev;
00499 
00500         unregister_netdev ( netdev );
00501         netdev_nullify ( netdev );
00502         netdev_put ( netdev );
00503 }
00504 
00505 /** CDC-ECM device IDs */
00506 static struct usb_device_id ecm_ids[] = {
00507         {
00508                 .name = "cdc-ecm",
00509                 .vendor = USB_ANY_ID,
00510                 .product = USB_ANY_ID,
00511         },
00512 };
00513 
00514 /** CDC-ECM driver */
00515 struct usb_driver ecm_driver __usb_driver = {
00516         .ids = ecm_ids,
00517         .id_count = ( sizeof ( ecm_ids ) / sizeof ( ecm_ids[0] ) ),
00518         .class = USB_CLASS_ID ( USB_CLASS_CDC, USB_SUBCLASS_CDC_ECM, 0 ),
00519         .score = USB_SCORE_NORMAL,
00520         .probe = ecm_probe,
00521         .remove = ecm_remove,
00522 };