iPXE
usbnet.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2015 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/usb.h>
00029 #include <ipxe/usbnet.h>
00030 
00031 /** @file
00032  *
00033  * USB network devices
00034  *
00035  * USB network devices use a variety of packet formats and interface
00036  * descriptors, but tend to have several features in common:
00037  *
00038  *  - a single bulk OUT endpoint
00039  *
00040  *  - a single bulk IN endpoint using the generic refill mechanism
00041  *
00042  *  - an optional interrupt endpoint using the generic refill mechanism
00043  *
00044  *  - optional use of an alternate setting to enable the data interface
00045  *
00046  */
00047 
00048 /**
00049  * Open USB network device
00050  *
00051  * @v usbnet            USB network device
00052  * @ret rc              Return status code
00053  */
00054 int usbnet_open ( struct usbnet_device *usbnet ) {
00055         struct usb_device *usb = usbnet->func->usb;
00056         int rc;
00057 
00058         /* Open interrupt endpoint, if applicable */
00059         if ( usbnet_has_intr ( usbnet ) &&
00060              ( rc = usb_endpoint_open ( &usbnet->intr ) ) != 0 ) {
00061                 DBGC ( usbnet, "USBNET %s could not open interrupt: %s\n",
00062                        usbnet->func->name, strerror ( rc ) );
00063                 goto err_open_intr;
00064         }
00065 
00066         /* Refill interrupt endpoint, if applicable */
00067         if ( usbnet_has_intr ( usbnet ) &&
00068              ( rc = usb_refill ( &usbnet->intr ) ) != 0 ) {
00069                 DBGC ( usbnet, "USBNET %s could not refill interrupt: %s\n",
00070                        usbnet->func->name, strerror ( rc ) );
00071                 goto err_refill_intr;
00072         }
00073 
00074         /* Select alternate setting for data interface, if applicable */
00075         if ( usbnet->alternate &&
00076              ( ( rc = usb_set_interface ( usb, usbnet->data,
00077                                           usbnet->alternate ) ) != 0 ) ) {
00078                 DBGC ( usbnet, "USBNET %s could not set alternate interface "
00079                        "%d: %s\n", usbnet->func->name, usbnet->alternate,
00080                        strerror ( rc ) );
00081                 goto err_set_interface;
00082         }
00083 
00084         /* Open bulk IN endpoint */
00085         if ( ( rc = usb_endpoint_open ( &usbnet->in ) ) != 0 ) {
00086                 DBGC ( usbnet, "USBNET %s could not open bulk IN: %s\n",
00087                        usbnet->func->name, strerror ( rc ) );
00088                 goto err_open_in;
00089         }
00090 
00091         /* Open bulk OUT endpoint */
00092         if ( ( rc = usb_endpoint_open ( &usbnet->out ) ) != 0 ) {
00093                 DBGC ( usbnet, "USBNET %s could not open bulk OUT: %s\n",
00094                        usbnet->func->name, strerror ( rc ) );
00095                 goto err_open_out;
00096         }
00097 
00098         /* Refill bulk IN endpoint */
00099         if ( ( rc = usb_refill ( &usbnet->in ) ) != 0 ) {
00100                 DBGC ( usbnet, "USBNET %s could not refill bulk IN: %s\n",
00101                        usbnet->func->name, strerror ( rc ) );
00102                 goto err_refill_in;
00103         }
00104 
00105         return 0;
00106 
00107  err_refill_in:
00108         usb_endpoint_close ( &usbnet->out );
00109  err_open_out:
00110         usb_endpoint_close ( &usbnet->in );
00111  err_open_in:
00112         if ( usbnet->alternate )
00113                 usb_set_interface ( usb, usbnet->data, 0 );
00114  err_set_interface:
00115  err_refill_intr:
00116         if ( usbnet_has_intr ( usbnet ) )
00117                 usb_endpoint_close ( &usbnet->intr );
00118  err_open_intr:
00119         return rc;
00120 }
00121 
00122 /**
00123  * Close USB network device
00124  *
00125  * @v usbnet            USB network device
00126  */
00127 void usbnet_close ( struct usbnet_device *usbnet ) {
00128         struct usb_device *usb = usbnet->func->usb;
00129 
00130         /* Close bulk OUT endpoint */
00131         usb_endpoint_close ( &usbnet->out );
00132 
00133         /* Close bulk IN endpoint */
00134         usb_endpoint_close ( &usbnet->in );
00135 
00136         /* Reset alternate setting for data interface, if applicable */
00137         if ( usbnet->alternate )
00138                 usb_set_interface ( usb, usbnet->data, 0 );
00139 
00140         /* Close interrupt endpoint, if applicable */
00141         if ( usbnet_has_intr ( usbnet ) )
00142                 usb_endpoint_close ( &usbnet->intr );
00143 }
00144 
00145 /**
00146  * Refill USB network device bulk IN and interrupt endpoints
00147  *
00148  * @v usbnet            USB network device
00149  * @ret rc              Return status code
00150  */
00151 int usbnet_refill ( struct usbnet_device *usbnet ) {
00152         int rc;
00153 
00154         /* Refill bulk IN endpoint */
00155         if ( ( rc = usb_refill ( &usbnet->in ) ) != 0 )
00156                 return rc;
00157 
00158         /* Refill interrupt endpoint, if applicable */
00159         if ( usbnet_has_intr ( usbnet ) &&
00160              ( rc = usb_refill ( &usbnet->intr ) ) != 0 ) {
00161                 return rc;
00162         }
00163 
00164         return 0;
00165 }
00166 
00167 /**
00168  * Describe communications interface and interrupt endpoint
00169  *
00170  * @v usbnet            USB network device
00171  * @v config            Configuration descriptor
00172  * @ret rc              Return status code
00173  */
00174 static int usbnet_comms_describe ( struct usbnet_device *usbnet,
00175                                    struct usb_configuration_descriptor *config){
00176         struct usb_interface_descriptor *desc;
00177         unsigned int comms;
00178         unsigned int i;
00179         int rc;
00180 
00181         /* Iterate over all available interfaces */
00182         for ( i = 0 ; i < usbnet->func->desc.count ; i++ ) {
00183 
00184                 /* Get interface number */
00185                 comms = usbnet->func->interface[i];
00186 
00187                 /* Locate interface descriptor */
00188                 desc = usb_interface_descriptor ( config, comms, 0 );
00189                 if ( ! desc )
00190                         continue;
00191 
00192                 /* Describe interrupt endpoint */
00193                 if ( ( rc = usb_endpoint_described ( &usbnet->intr, config,
00194                                                      desc, USB_INTERRUPT_IN,
00195                                                      0 ) ) != 0 )
00196                         continue;
00197 
00198                 /* Record communications interface */
00199                 usbnet->comms = comms;
00200                 DBGC ( usbnet, "USBNET %s found communications interface %d\n",
00201                        usbnet->func->name, comms );
00202                 return 0;
00203         }
00204 
00205         DBGC ( usbnet, "USBNET %s found no communications interface\n",
00206                usbnet->func->name );
00207         return -ENOENT;
00208 }
00209 
00210 /**
00211  * Describe data interface and bulk endpoints
00212  *
00213  * @v usbnet            USB network device
00214  * @v config            Configuration descriptor
00215  * @ret rc              Return status code
00216  */
00217 static int usbnet_data_describe ( struct usbnet_device *usbnet,
00218                                   struct usb_configuration_descriptor *config ){
00219         struct usb_interface_descriptor *desc;
00220         unsigned int data;
00221         unsigned int alt;
00222         unsigned int i;
00223         int rc;
00224 
00225         /* Iterate over all available interfaces */
00226         for ( i = 0 ; i < usbnet->func->desc.count ; i++ ) {
00227 
00228                 /* Get interface number */
00229                 data = usbnet->func->interface[i];
00230 
00231                 /* Iterate over all existent alternate settings */
00232                 for ( alt = 0 ; ; alt++ ) {
00233 
00234                         /* Locate interface descriptor */
00235                         desc = usb_interface_descriptor ( config, data, alt );
00236                         if ( ! desc )
00237                                 break;
00238 
00239                         /* Describe bulk IN endpoint */
00240                         if ( ( rc = usb_endpoint_described ( &usbnet->in,
00241                                                              config, desc,
00242                                                              USB_BULK_IN,
00243                                                              0 ) ) != 0 )
00244                                 continue;
00245 
00246                         /* Describe bulk OUT endpoint */
00247                         if ( ( rc = usb_endpoint_described ( &usbnet->out,
00248                                                              config, desc,
00249                                                              USB_BULK_OUT,
00250                                                              0 ) ) != 0 )
00251                                 continue;
00252 
00253                         /* Record data interface and alternate setting */
00254                         usbnet->data = data;
00255                         usbnet->alternate = alt;
00256                         DBGC ( usbnet, "USBNET %s found data interface %d",
00257                                usbnet->func->name, data );
00258                         if ( alt )
00259                                 DBGC ( usbnet, " using alternate %d", alt );
00260                         DBGC ( usbnet, "\n" );
00261                         return 0;
00262                 }
00263         }
00264 
00265         DBGC ( usbnet, "USBNET %s found no data interface\n",
00266                usbnet->func->name );
00267         return -ENOENT;
00268 }
00269 
00270 /**
00271  * Describe USB network device interfaces
00272  *
00273  * @v usbnet            USB network device
00274  * @v config            Configuration descriptor
00275  * @ret rc              Return status code
00276  */
00277 int usbnet_describe ( struct usbnet_device *usbnet,
00278                       struct usb_configuration_descriptor *config ) {
00279         int rc;
00280 
00281         /* Describe communications interface, if applicable */
00282         if ( usbnet_has_intr ( usbnet ) &&
00283              ( rc = usbnet_comms_describe ( usbnet, config ) ) != 0 ) {
00284                 return rc;
00285         }
00286 
00287         /* Describe data interface */
00288         if ( ( rc = usbnet_data_describe ( usbnet, config ) ) != 0 )
00289                 return rc;
00290 
00291         return 0;
00292 }