iPXE
ncm.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 <string.h>
00027 #include <errno.h>
00028 #include <ipxe/netdevice.h>
00029 #include <ipxe/ethernet.h>
00030 #include <ipxe/if_ether.h>
00031 #include <ipxe/profile.h>
00032 #include <ipxe/usb.h>
00033 #include <ipxe/usbnet.h>
00034 #include "ecm.h"
00035 #include "ncm.h"
00036 
00037 /** @file
00038  *
00039  * CDC-NCM USB Ethernet driver
00040  *
00041  */
00042 
00043 /** Interrupt completion profiler */
00044 static struct profiler ncm_intr_profiler __profiler =
00045         { .name = "ncm.intr" };
00046 
00047 /** Bulk IN completion profiler */
00048 static struct profiler ncm_in_profiler __profiler =
00049         { .name = "ncm.in" };
00050 
00051 /** Bulk IN per-datagram profiler */
00052 static struct profiler ncm_in_datagram_profiler __profiler =
00053         { .name = "ncm.in_dgram" };
00054 
00055 /** Bulk OUT profiler */
00056 static struct profiler ncm_out_profiler __profiler =
00057         { .name = "ncm.out" };
00058 
00059 /******************************************************************************
00060  *
00061  * CDC-NCM communications interface
00062  *
00063  ******************************************************************************
00064  */
00065 
00066 /**
00067  * Complete interrupt transfer
00068  *
00069  * @v ep                USB endpoint
00070  * @v iobuf             I/O buffer
00071  * @v rc                Completion status code
00072  */
00073 static void ncm_intr_complete ( struct usb_endpoint *ep,
00074                                 struct io_buffer *iobuf, int rc ) {
00075         struct ncm_device *ncm = container_of ( ep, struct ncm_device,
00076                                                 usbnet.intr );
00077         struct net_device *netdev = ncm->netdev;
00078         struct usb_setup_packet *message;
00079         size_t len = iob_len ( iobuf );
00080 
00081         /* Profile completions */
00082         profile_start ( &ncm_intr_profiler );
00083 
00084         /* Ignore packets cancelled when the endpoint closes */
00085         if ( ! ep->open )
00086                 goto ignore;
00087 
00088         /* Ignore packets with errors */
00089         if ( rc != 0 ) {
00090                 DBGC ( ncm, "NCM %p interrupt failed: %s\n",
00091                        ncm, strerror ( rc ) );
00092                 DBGC_HDA ( ncm, 0, iobuf->data, iob_len ( iobuf ) );
00093                 goto error;
00094         }
00095 
00096         /* Extract message header */
00097         if ( len < sizeof ( *message ) ) {
00098                 DBGC ( ncm, "NCM %p underlength interrupt:\n", ncm );
00099                 DBGC_HDA ( ncm, 0, iobuf->data, iob_len ( iobuf ) );
00100                 rc = -EINVAL;
00101                 goto error;
00102         }
00103         message = iobuf->data;
00104 
00105         /* Parse message header */
00106         switch ( message->request ) {
00107 
00108         case cpu_to_le16 ( CDC_NETWORK_CONNECTION ) :
00109                 if ( message->value ) {
00110                         DBGC ( ncm, "NCM %p link up\n", ncm );
00111                         netdev_link_up ( netdev );
00112                 } else {
00113                         DBGC ( ncm, "NCM %p link down\n", ncm );
00114                         netdev_link_down ( netdev );
00115                 }
00116                 break;
00117 
00118         case cpu_to_le16 ( CDC_CONNECTION_SPEED_CHANGE ) :
00119                 /* Ignore */
00120                 break;
00121 
00122         default:
00123                 DBGC ( ncm, "NCM %p unrecognised interrupt:\n", ncm );
00124                 DBGC_HDA ( ncm, 0, iobuf->data, iob_len ( iobuf ) );
00125                 goto error;
00126         }
00127 
00128         /* Free I/O buffer */
00129         free_iob ( iobuf );
00130         profile_stop ( &ncm_intr_profiler );
00131 
00132         return;
00133 
00134  error:
00135         netdev_rx_err ( netdev, iob_disown ( iobuf ), rc );
00136  ignore:
00137         free_iob ( iobuf );
00138         return;
00139 }
00140 
00141 /** Interrupt endpoint operations */
00142 static struct usb_endpoint_driver_operations ncm_intr_operations = {
00143         .complete = ncm_intr_complete,
00144 };
00145 
00146 /******************************************************************************
00147  *
00148  * CDC-NCM data interface
00149  *
00150  ******************************************************************************
00151  */
00152 
00153 /**
00154  * Prefill bulk IN endpoint
00155  *
00156  * @v ncm               CDC-NCM device
00157  * @ret rc              Return status code
00158  */
00159 static int ncm_in_prefill ( struct ncm_device *ncm ) {
00160         struct usb_bus *bus = ncm->bus;
00161         size_t mtu;
00162         unsigned int count;
00163         int rc;
00164 
00165         /* Some devices have a very small number of internal buffers,
00166          * and rely on being able to pack multiple packets into each
00167          * buffer.  We therefore want to use large buffers if
00168          * possible.  However, large allocations have a reasonable
00169          * chance of failure, especially if this is not the first or
00170          * only device to be opened.
00171          *
00172          * We therefore attempt to find a usable buffer size, starting
00173          * large and working downwards until allocation succeeds.
00174          * Smaller buffers will still work, albeit with a higher
00175          * chance of packet loss and so lower overall throughput.
00176          */
00177         for ( mtu = ncm->mtu ; mtu >= NCM_MIN_NTB_INPUT_SIZE ; mtu >>= 1 ) {
00178 
00179                 /* Attempt allocation at this MTU */
00180                 if ( mtu > NCM_MAX_NTB_INPUT_SIZE )
00181                         continue;
00182                 if ( mtu > bus->mtu )
00183                         continue;
00184                 count = ( NCM_IN_MIN_SIZE / mtu );
00185                 if ( count < NCM_IN_MIN_COUNT )
00186                         count = NCM_IN_MIN_COUNT;
00187                 if ( ( count * mtu ) > NCM_IN_MAX_SIZE )
00188                         continue;
00189                 usb_refill_init ( &ncm->usbnet.in, 0, mtu, count );
00190                 if ( ( rc = usb_prefill ( &ncm->usbnet.in ) ) != 0 ) {
00191                         DBGC ( ncm, "NCM %p could not prefill %dx %zd-byte "
00192                                "buffers for bulk IN\n", ncm, count, mtu );
00193                         continue;
00194                 }
00195 
00196                 DBGC ( ncm, "NCM %p using %dx %zd-byte buffers for bulk IN\n",
00197                        ncm, count, mtu );
00198                 return 0;
00199         }
00200 
00201         DBGC ( ncm, "NCM %p could not prefill bulk IN endpoint\n", ncm );
00202         return -ENOMEM;
00203 }
00204 
00205 /**
00206  * Complete bulk IN transfer
00207  *
00208  * @v ep                USB endpoint
00209  * @v iobuf             I/O buffer
00210  * @v rc                Completion status code
00211  */
00212 static void ncm_in_complete ( struct usb_endpoint *ep, struct io_buffer *iobuf,
00213                               int rc ) {
00214         struct ncm_device *ncm = container_of ( ep, struct ncm_device,
00215                                                 usbnet.in );
00216         struct net_device *netdev = ncm->netdev;
00217         struct ncm_transfer_header *nth;
00218         struct ncm_datagram_pointer *ndp;
00219         struct ncm_datagram_descriptor *desc;
00220         struct io_buffer *pkt;
00221         unsigned int remaining;
00222         size_t ndp_offset;
00223         size_t ndp_len;
00224         size_t pkt_offset;
00225         size_t pkt_len;
00226         size_t headroom;
00227         size_t len;
00228 
00229         /* Profile overall bulk IN completion */
00230         profile_start ( &ncm_in_profiler );
00231 
00232         /* Ignore packets cancelled when the endpoint closes */
00233         if ( ! ep->open )
00234                 goto ignore;
00235 
00236         /* Record USB errors against the network device */
00237         if ( rc != 0 ) {
00238                 DBGC ( ncm, "NCM %p bulk IN failed: %s\n",
00239                        ncm, strerror ( rc ) );
00240                 goto error;
00241         }
00242 
00243         /* Locate transfer header */
00244         len = iob_len ( iobuf );
00245         if ( sizeof ( *nth ) > len ) {
00246                 DBGC ( ncm, "NCM %p packet too short for NTH:\n", ncm );
00247                 rc = -EINVAL;
00248                 goto error;
00249         }
00250         nth = iobuf->data;
00251 
00252         /* Locate datagram pointer */
00253         ndp_offset = le16_to_cpu ( nth->offset );
00254         if ( ( ndp_offset + sizeof ( *ndp ) ) > len ) {
00255                 DBGC ( ncm, "NCM %p packet too short for NDP:\n", ncm );
00256                 rc = -EINVAL;
00257                 goto error;
00258         }
00259         ndp = ( iobuf->data + ndp_offset );
00260         ndp_len = le16_to_cpu ( ndp->header_len );
00261         if ( ndp_len < offsetof ( typeof ( *ndp ), desc ) ) {
00262                 DBGC ( ncm, "NCM %p NDP header length too short:\n", ncm );
00263                 rc = -EINVAL;
00264                 goto error;
00265         }
00266         if ( ( ndp_offset + ndp_len ) > len ) {
00267                 DBGC ( ncm, "NCM %p packet too short for NDP:\n", ncm );
00268                 rc = -EINVAL;
00269                 goto error;
00270         }
00271 
00272         /* Process datagrams */
00273         remaining = ( ( ndp_len - offsetof ( typeof ( *ndp ), desc ) ) /
00274                       sizeof ( ndp->desc[0] ) );
00275         for ( desc = ndp->desc ; remaining && desc->offset ; remaining-- ) {
00276 
00277                 /* Profile individual datagrams */
00278                 profile_start ( &ncm_in_datagram_profiler );
00279 
00280                 /* Locate datagram */
00281                 pkt_offset = le16_to_cpu ( desc->offset );
00282                 pkt_len = le16_to_cpu ( desc->len );
00283                 if ( pkt_len < ETH_HLEN ) {
00284                         DBGC ( ncm, "NCM %p underlength datagram:\n", ncm );
00285                         rc = -EINVAL;
00286                         goto error;
00287                 }
00288                 if ( ( pkt_offset + pkt_len ) > len ) {
00289                         DBGC ( ncm, "NCM %p datagram exceeds packet:\n", ncm );
00290                         rc = -EINVAL;
00291                         goto error;
00292                 }
00293 
00294                 /* Move to next descriptor */
00295                 desc++;
00296 
00297                 /* Copy data to a new I/O buffer.  Our USB buffers may
00298                  * be very large and so we choose to recycle the
00299                  * buffers directly rather than attempt reallocation
00300                  * while the device is running.  We therefore copy the
00301                  * data to a new I/O buffer even if this is the only
00302                  * (or last) packet within the buffer.
00303                  *
00304                  * We reserve enough space at the start of each buffer
00305                  * to allow for our own transmission header, to
00306                  * support protocols such as ARP which may modify the
00307                  * received packet and reuse the same I/O buffer for
00308                  * transmission.
00309                  */
00310                 headroom = ( sizeof ( struct ncm_ntb_header ) + ncm->padding );
00311                 pkt = alloc_iob ( headroom + pkt_len );
00312                 if ( ! pkt ) {
00313                         /* Record error and continue */
00314                         netdev_rx_err ( netdev, NULL, -ENOMEM );
00315                         continue;
00316                 }
00317                 iob_reserve ( pkt, headroom );
00318                 memcpy ( iob_put ( pkt, pkt_len ),
00319                          ( iobuf->data + pkt_offset ), pkt_len );
00320 
00321                 /* Strip CRC, if present */
00322                 if ( ndp->magic & cpu_to_le32 ( NCM_DATAGRAM_POINTER_MAGIC_CRC))
00323                         iob_unput ( pkt, 4 /* CRC32 */ );
00324 
00325                 /* Hand off to network stack */
00326                 netdev_rx ( netdev, pkt );
00327                 profile_stop ( &ncm_in_datagram_profiler );
00328         }
00329 
00330         /* Recycle I/O buffer */
00331         usb_recycle ( &ncm->usbnet.in, iobuf );
00332         profile_stop ( &ncm_in_profiler );
00333 
00334         return;
00335 
00336  error:
00337         /* Record error against network device */
00338         DBGC_HDA ( ncm, 0, iobuf->data, iob_len ( iobuf ) );
00339         netdev_rx_err ( netdev, NULL, rc );
00340  ignore:
00341         usb_recycle ( &ncm->usbnet.in, iobuf );
00342 }
00343 
00344 /** Bulk IN endpoint operations */
00345 static struct usb_endpoint_driver_operations ncm_in_operations = {
00346         .complete = ncm_in_complete,
00347 };
00348 
00349 /**
00350  * Transmit packet
00351  *
00352  * @v ncm               CDC-NCM device
00353  * @v iobuf             I/O buffer
00354  * @ret rc              Return status code
00355  */
00356 static int ncm_out_transmit ( struct ncm_device *ncm,
00357                               struct io_buffer *iobuf ) {
00358         struct ncm_ntb_header *header;
00359         size_t len = iob_len ( iobuf );
00360         size_t header_len = ( sizeof ( *header ) + ncm->padding );
00361         int rc;
00362 
00363         /* Profile transmissions */
00364         profile_start ( &ncm_out_profiler );
00365 
00366         /* Prepend header */
00367         if ( ( rc = iob_ensure_headroom ( iobuf, header_len ) ) != 0 )
00368                 return rc;
00369         header = iob_push ( iobuf, header_len );
00370 
00371         /* Populate header */
00372         header->nth.magic = cpu_to_le32 ( NCM_TRANSFER_HEADER_MAGIC );
00373         header->nth.header_len = cpu_to_le16 ( sizeof ( header->nth ) );
00374         header->nth.sequence = cpu_to_le16 ( ncm->sequence );
00375         header->nth.len = cpu_to_le16 ( iob_len ( iobuf ) );
00376         header->nth.offset =
00377                 cpu_to_le16 ( offsetof ( typeof ( *header ), ndp ) );
00378         header->ndp.magic = cpu_to_le32 ( NCM_DATAGRAM_POINTER_MAGIC );
00379         header->ndp.header_len = cpu_to_le16 ( sizeof ( header->ndp ) +
00380                                                sizeof ( header->desc ) );
00381         header->ndp.offset = cpu_to_le16 ( 0 );
00382         header->desc[0].offset = cpu_to_le16 ( header_len );
00383         header->desc[0].len = cpu_to_le16 ( len );
00384         memset ( &header->desc[1], 0, sizeof ( header->desc[1] ) );
00385 
00386         /* Enqueue I/O buffer */
00387         if ( ( rc = usb_stream ( &ncm->usbnet.out, iobuf, 0 ) ) != 0 )
00388                 return rc;
00389 
00390         /* Increment sequence number */
00391         ncm->sequence++;
00392 
00393         profile_stop ( &ncm_out_profiler );
00394         return 0;
00395 }
00396 
00397 /**
00398  * Complete bulk OUT transfer
00399  *
00400  * @v ep                USB endpoint
00401  * @v iobuf             I/O buffer
00402  * @v rc                Completion status code
00403  */
00404 static void ncm_out_complete ( struct usb_endpoint *ep, struct io_buffer *iobuf,
00405                                int rc ) {
00406         struct ncm_device *ncm = container_of ( ep, struct ncm_device,
00407                                                 usbnet.out );
00408         struct net_device *netdev = ncm->netdev;
00409 
00410         /* Report TX completion */
00411         netdev_tx_complete_err ( netdev, iobuf, rc );
00412 }
00413 
00414 /** Bulk OUT endpoint operations */
00415 static struct usb_endpoint_driver_operations ncm_out_operations = {
00416         .complete = ncm_out_complete,
00417 };
00418 
00419 /******************************************************************************
00420  *
00421  * Network device interface
00422  *
00423  ******************************************************************************
00424  */
00425 
00426 /**
00427  * Open network device
00428  *
00429  * @v netdev            Network device
00430  * @ret rc              Return status code
00431  */
00432 static int ncm_open ( struct net_device *netdev ) {
00433         struct ncm_device *ncm = netdev->priv;
00434         struct usb_device *usb = ncm->usb;
00435         struct ncm_set_ntb_input_size size;
00436         int rc;
00437 
00438         /* Reset sequence number */
00439         ncm->sequence = 0;
00440 
00441         /* Prefill I/O buffers */
00442         if ( ( rc = ncm_in_prefill ( ncm ) ) != 0 )
00443                 goto err_prefill;
00444 
00445         /* Set maximum input size */
00446         memset ( &size, 0, sizeof ( size ) );
00447         size.mtu = cpu_to_le32 ( ncm->usbnet.in.len );
00448         if ( ( rc = usb_control ( usb, NCM_SET_NTB_INPUT_SIZE, 0,
00449                                   ncm->usbnet.comms, &size,
00450                                   sizeof ( size ) ) ) != 0 ) {
00451                 DBGC ( ncm, "NCM %p could not set input size to %zd: %s\n",
00452                        ncm, ncm->usbnet.in.len, strerror ( rc ) );
00453                 goto err_set_ntb_input_size;
00454         }
00455 
00456         /* Set MAC address */
00457         if ( ( rc = usb_control ( usb, NCM_SET_NET_ADDRESS, 0,
00458                                   ncm->usbnet.comms, netdev->ll_addr,
00459                                   netdev->ll_protocol->ll_addr_len ) ) != 0 ) {
00460                 DBGC ( ncm, "NCM %p could not set MAC address: %s\n",
00461                        ncm, strerror ( rc ) );
00462                 /* Ignore error and continue */
00463         }
00464 
00465         /* Open USB network device */
00466         if ( ( rc = usbnet_open ( &ncm->usbnet ) ) != 0 ) {
00467                 DBGC ( ncm, "NCM %p could not open: %s\n",
00468                        ncm, strerror ( rc ) );
00469                 goto err_open;
00470         }
00471 
00472         return 0;
00473 
00474         usbnet_close ( &ncm->usbnet );
00475  err_open:
00476  err_set_ntb_input_size:
00477         usb_flush ( &ncm->usbnet.in );
00478  err_prefill:
00479         return rc;
00480 }
00481 
00482 /**
00483  * Close network device
00484  *
00485  * @v netdev            Network device
00486  */
00487 static void ncm_close ( struct net_device *netdev ) {
00488         struct ncm_device *ncm = netdev->priv;
00489 
00490         /* Close USB network device */
00491         usbnet_close ( &ncm->usbnet );
00492 }
00493 
00494 /**
00495  * Transmit packet
00496  *
00497  * @v netdev            Network device
00498  * @v iobuf             I/O buffer
00499  * @ret rc              Return status code
00500  */
00501 static int ncm_transmit ( struct net_device *netdev,
00502                           struct io_buffer *iobuf ) {
00503         struct ncm_device *ncm = netdev->priv;
00504         int rc;
00505 
00506         /* Transmit packet */
00507         if ( ( rc = ncm_out_transmit ( ncm, iobuf ) ) != 0 )
00508                 return rc;
00509 
00510         return 0;
00511 }
00512 
00513 /**
00514  * Poll for completed and received packets
00515  *
00516  * @v netdev            Network device
00517  */
00518 static void ncm_poll ( struct net_device *netdev ) {
00519         struct ncm_device *ncm = netdev->priv;
00520         int rc;
00521 
00522         /* Poll USB bus */
00523         usb_poll ( ncm->bus );
00524 
00525         /* Refill endpoints */
00526         if ( ( rc = usbnet_refill ( &ncm->usbnet ) ) != 0 )
00527                 netdev_rx_err ( netdev, NULL, rc );
00528 
00529 }
00530 
00531 /** CDC-NCM network device operations */
00532 static struct net_device_operations ncm_operations = {
00533         .open           = ncm_open,
00534         .close          = ncm_close,
00535         .transmit       = ncm_transmit,
00536         .poll           = ncm_poll,
00537 };
00538 
00539 /******************************************************************************
00540  *
00541  * USB interface
00542  *
00543  ******************************************************************************
00544  */
00545 
00546 /**
00547  * Probe device
00548  *
00549  * @v func              USB function
00550  * @v config            Configuration descriptor
00551  * @ret rc              Return status code
00552  */
00553 static int ncm_probe ( struct usb_function *func,
00554                        struct usb_configuration_descriptor *config ) {
00555         struct usb_device *usb = func->usb;
00556         struct net_device *netdev;
00557         struct ncm_device *ncm;
00558         struct usb_interface_descriptor *comms;
00559         struct ecm_ethernet_descriptor *ethernet;
00560         struct ncm_ntb_parameters params;
00561         int rc;
00562 
00563         /* Allocate and initialise structure */
00564         netdev = alloc_etherdev ( sizeof ( *ncm ) );
00565         if ( ! netdev ) {
00566                 rc = -ENOMEM;
00567                 goto err_alloc;
00568         }
00569         netdev_init ( netdev, &ncm_operations );
00570         netdev->dev = &func->dev;
00571         ncm = netdev->priv;
00572         memset ( ncm, 0, sizeof ( *ncm ) );
00573         ncm->usb = usb;
00574         ncm->bus = usb->port->hub->bus;
00575         ncm->netdev = netdev;
00576         usbnet_init ( &ncm->usbnet, func, &ncm_intr_operations,
00577                       &ncm_in_operations, &ncm_out_operations );
00578         usb_refill_init ( &ncm->usbnet.intr, 0, 0, NCM_INTR_COUNT );
00579         DBGC ( ncm, "NCM %p on %s\n", ncm, func->name );
00580 
00581         /* Describe USB network device */
00582         if ( ( rc = usbnet_describe ( &ncm->usbnet, config ) ) != 0 ) {
00583                 DBGC ( ncm, "NCM %p could not describe: %s\n",
00584                        ncm, strerror ( rc ) );
00585                 goto err_describe;
00586         }
00587 
00588         /* Locate Ethernet descriptor */
00589         comms = usb_interface_descriptor ( config, ncm->usbnet.comms, 0 );
00590         assert ( comms != NULL );
00591         ethernet = ecm_ethernet_descriptor ( config, comms );
00592         if ( ! ethernet ) {
00593                 DBGC ( ncm, "NCM %p has no Ethernet descriptor\n", ncm );
00594                 rc = -EINVAL;
00595                 goto err_ethernet;
00596         }
00597 
00598         /* Fetch MAC address */
00599         if ( ( rc = ecm_fetch_mac ( usb, ethernet, netdev->hw_addr ) ) != 0 ) {
00600                 DBGC ( ncm, "NCM %p could not fetch MAC address: %s\n",
00601                        ncm, strerror ( rc ) );
00602                 goto err_fetch_mac;
00603         }
00604 
00605         /* Get NTB parameters */
00606         if ( ( rc = usb_control ( usb, NCM_GET_NTB_PARAMETERS, 0,
00607                                   ncm->usbnet.comms, &params,
00608                                   sizeof ( params ) ) ) != 0 ) {
00609                 DBGC ( ncm, "NCM %p could not get NTB parameters: %s\n",
00610                        ncm, strerror ( rc ) );
00611                 goto err_ntb_parameters;
00612         }
00613 
00614         /* Get maximum supported input size */
00615         ncm->mtu = le32_to_cpu ( params.in.mtu );
00616         DBGC2 ( ncm, "NCM %p maximum IN size is %zd bytes\n", ncm, ncm->mtu );
00617 
00618         /* Calculate transmit padding */
00619         ncm->padding = ( ( le16_to_cpu ( params.out.remainder ) -
00620                            sizeof ( struct ncm_ntb_header ) - ETH_HLEN ) &
00621                          ( le16_to_cpu ( params.out.divisor ) - 1 ) );
00622         DBGC2 ( ncm, "NCM %p using %zd-byte transmit padding\n",
00623                 ncm, ncm->padding );
00624         assert ( ( ( sizeof ( struct ncm_ntb_header ) + ncm->padding +
00625                      ETH_HLEN ) % le16_to_cpu ( params.out.divisor ) ) ==
00626                  le16_to_cpu ( params.out.remainder ) );
00627 
00628         /* Register network device */
00629         if ( ( rc = register_netdev ( netdev ) ) != 0 )
00630                 goto err_register;
00631 
00632         usb_func_set_drvdata ( func, ncm );
00633         return 0;
00634 
00635         unregister_netdev ( netdev );
00636  err_register:
00637  err_ntb_parameters:
00638  err_fetch_mac:
00639  err_ethernet:
00640  err_describe:
00641         netdev_nullify ( netdev );
00642         netdev_put ( netdev );
00643  err_alloc:
00644         return rc;
00645 }
00646 
00647 /**
00648  * Remove device
00649  *
00650  * @v func              USB function
00651  */
00652 static void ncm_remove ( struct usb_function *func ) {
00653         struct ncm_device *ncm = usb_func_get_drvdata ( func );
00654         struct net_device *netdev = ncm->netdev;
00655 
00656         unregister_netdev ( netdev );
00657         netdev_nullify ( netdev );
00658         netdev_put ( netdev );
00659 }
00660 
00661 /** CDC-NCM device IDs */
00662 static struct usb_device_id ncm_ids[] = {
00663         {
00664                 .name = "cdc-ncm",
00665                 .vendor = USB_ANY_ID,
00666                 .product = USB_ANY_ID,
00667         },
00668 };
00669 
00670 /** CDC-NCM driver */
00671 struct usb_driver ncm_driver __usb_driver = {
00672         .ids = ncm_ids,
00673         .id_count = ( sizeof ( ncm_ids ) / sizeof ( ncm_ids[0] ) ),
00674         .class = USB_CLASS_ID ( USB_CLASS_CDC, USB_SUBCLASS_CDC_NCM, 0 ),
00675         .score = USB_SCORE_NORMAL,
00676         .probe = ncm_probe,
00677         .remove = ncm_remove,
00678 };