]> git.ipfire.org Git - thirdparty/ipxe.git/commitdiff
[usb] Add generic USB network device framework
authorMichael Brown <mcb30@ipxe.org>
Mon, 16 Feb 2015 14:05:45 +0000 (14:05 +0000)
committerMichael Brown <mcb30@ipxe.org>
Tue, 17 Feb 2015 01:21:53 +0000 (01:21 +0000)
Signed-off-by: Michael Brown <mcb30@ipxe.org>
src/drivers/usb/usbnet.c [new file with mode: 0644]
src/include/ipxe/errfile.h
src/include/ipxe/usbnet.h [new file with mode: 0644]

diff --git a/src/drivers/usb/usbnet.c b/src/drivers/usb/usbnet.c
new file mode 100644 (file)
index 0000000..d24cb3d
--- /dev/null
@@ -0,0 +1,280 @@
+/*
+ * Copyright (C) 2015 Michael Brown <mbrown@fensystems.co.uk>.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+FILE_LICENCE ( GPL2_OR_LATER );
+
+#include <string.h>
+#include <errno.h>
+#include <ipxe/usb.h>
+#include <ipxe/usbnet.h>
+
+/** @file
+ *
+ * USB network devices
+ *
+ * USB network devices use a variety of packet formats and interface
+ * descriptors, but tend to have several features in common:
+ *
+ *  - a single interrupt endpoint using the generic refill mechanism
+ *
+ *  - a single bulk IN endpoint using the generic refill mechanism
+ *
+ *  - a single bulk OUT endpoint
+ *
+ *  - optional use of an alternate setting to enable the data interface
+ *
+ */
+
+/**
+ * Open USB network device
+ *
+ * @v usbnet           USB network device
+ * @ret rc             Return status code
+ */
+int usbnet_open ( struct usbnet_device *usbnet ) {
+       struct usb_device *usb = usbnet->func->usb;
+       int rc;
+
+       /* Open interrupt endpoint */
+       if ( ( rc = usb_endpoint_open ( &usbnet->intr ) ) != 0 ) {
+               DBGC ( usbnet, "USBNET %s could not open interrupt: %s\n",
+                      usbnet->func->name, strerror ( rc ) );
+               goto err_open_intr;
+       }
+
+       /* Refill interrupt endpoint */
+       if ( ( rc = usb_refill ( &usbnet->intr ) ) != 0 ) {
+               DBGC ( usbnet, "USBNET %s could not refill interrupt: %s\n",
+                      usbnet->func->name, strerror ( rc ) );
+               goto err_refill_intr;
+       }
+
+       /* Select alternate setting for data interface, if applicable */
+       if ( usbnet->alternate &&
+            ( ( rc = usb_set_interface ( usb, usbnet->data,
+                                         usbnet->alternate ) ) != 0 ) ) {
+               DBGC ( usbnet, "USBNET %s could not set alternate interface "
+                      "%d: %s\n", usbnet->func->name, usbnet->alternate,
+                      strerror ( rc ) );
+               goto err_set_interface;
+       }
+
+       /* Open bulk IN endpoint */
+       if ( ( rc = usb_endpoint_open ( &usbnet->in ) ) != 0 ) {
+               DBGC ( usbnet, "USBNET %s could not open bulk IN: %s\n",
+                      usbnet->func->name, strerror ( rc ) );
+               goto err_open_in;
+       }
+
+       /* Open bulk OUT endpoint */
+       if ( ( rc = usb_endpoint_open ( &usbnet->out ) ) != 0 ) {
+               DBGC ( usbnet, "USBNET %s could not open bulk OUT: %s\n",
+                      usbnet->func->name, strerror ( rc ) );
+               goto err_open_out;
+       }
+
+       /* Refill bulk IN endpoint */
+       if ( ( rc = usb_refill ( &usbnet->in ) ) != 0 ) {
+               DBGC ( usbnet, "USBNET %s could not refill bulk IN: %s\n",
+                      usbnet->func->name, strerror ( rc ) );
+               goto err_refill_in;
+       }
+
+       return 0;
+
+ err_refill_in:
+       usb_endpoint_close ( &usbnet->out );
+ err_open_out:
+       usb_endpoint_close ( &usbnet->in );
+ err_open_in:
+       if ( usbnet->alternate )
+               usb_set_interface ( usb, usbnet->data, 0 );
+ err_set_interface:
+ err_refill_intr:
+       usb_endpoint_close ( &usbnet->intr );
+ err_open_intr:
+       return rc;
+}
+
+/**
+ * Close USB network device
+ *
+ * @v usbnet           USB network device
+ */
+void usbnet_close ( struct usbnet_device *usbnet ) {
+       struct usb_device *usb = usbnet->func->usb;
+
+       /* Close bulk OUT endpoint */
+       usb_endpoint_close ( &usbnet->out );
+
+       /* Close bulk IN endpoint */
+       usb_endpoint_close ( &usbnet->in );
+
+       /* Reset alternate setting for data interface, if applicable */
+       if ( usbnet->alternate )
+               usb_set_interface ( usb, usbnet->data, 0 );
+
+       /* Close interrupt endpoint */
+       usb_endpoint_close ( &usbnet->intr );
+}
+
+/**
+ * Refill USB network device bulk IN and interrupt endpoints
+ *
+ * @v usbnet           USB network device
+ * @ret rc             Return status code
+ */
+int usbnet_refill ( struct usbnet_device *usbnet ) {
+       int rc;
+
+       /* Refill bulk IN endpoint */
+       if ( ( rc = usb_refill ( &usbnet->in ) ) != 0 )
+               return rc;
+
+       /* Refill interrupt endpoint */
+       if ( ( rc = usb_refill ( &usbnet->intr ) ) != 0 )
+               return rc;
+
+       return 0;
+}
+
+/**
+ * Describe communications interface and interrupt endpoint
+ *
+ * @v usbnet           USB network device
+ * @v config           Configuration descriptor
+ * @ret rc             Return status code
+ */
+static int usbnet_comms_describe ( struct usbnet_device *usbnet,
+                                  struct usb_configuration_descriptor *config){
+       struct usb_interface_descriptor *desc;
+       unsigned int comms;
+       unsigned int i;
+       int rc;
+
+       /* Iterate over all available interfaces */
+       for ( i = 0 ; i < usbnet->func->count ; i++ ) {
+
+               /* Get interface number */
+               comms = usbnet->func->interface[i];
+
+               /* Locate interface descriptor */
+               desc = usb_interface_descriptor ( config, comms, 0 );
+               if ( ! desc )
+                       continue;
+
+               /* Describe interrupt endpoint */
+               if ( ( rc = usb_endpoint_described ( &usbnet->intr, config,
+                                                    desc, USB_INTERRUPT,
+                                                    0 ) ) != 0 )
+                       continue;
+
+               /* Record communications interface */
+               usbnet->comms = comms;
+               DBGC ( usbnet, "USBNET %s found communications interface %d\n",
+                      usbnet->func->name, comms );
+               return 0;
+       }
+
+       DBGC ( usbnet, "USBNET %s found no communications interface\n",
+              usbnet->func->name );
+       return -ENOENT;
+}
+
+/**
+ * Describe data interface and bulk endpoints
+ *
+ * @v usbnet           USB network device
+ * @v config           Configuration descriptor
+ * @ret rc             Return status code
+ */
+static int usbnet_data_describe ( struct usbnet_device *usbnet,
+                                 struct usb_configuration_descriptor *config ){
+       struct usb_interface_descriptor *desc;
+       unsigned int data;
+       unsigned int alt;
+       unsigned int i;
+       int rc;
+
+       /* Iterate over all available interfaces */
+       for ( i = 0 ; i < usbnet->func->count ; i++ ) {
+
+               /* Get interface number */
+               data = usbnet->func->interface[i];
+
+               /* Iterate over all existent alternate settings */
+               for ( alt = 0 ; ; alt++ ) {
+
+                       /* Locate interface descriptor */
+                       desc = usb_interface_descriptor ( config, data, alt );
+                       if ( ! desc )
+                               break;
+
+                       /* Describe bulk IN endpoint */
+                       if ( ( rc = usb_endpoint_described ( &usbnet->in,
+                                                            config, desc,
+                                                            USB_BULK_IN,
+                                                            0 ) ) != 0 )
+                               continue;
+
+                       /* Describe bulk OUT endpoint */
+                       if ( ( rc = usb_endpoint_described ( &usbnet->out,
+                                                            config, desc,
+                                                            USB_BULK_OUT,
+                                                            0 ) ) != 0 )
+                               continue;
+
+                       /* Record data interface and alternate setting */
+                       usbnet->data = data;
+                       usbnet->alternate = alt;
+                       DBGC ( usbnet, "USBNET %s found data interface %d",
+                              usbnet->func->name, data );
+                       if ( alt )
+                               DBGC ( usbnet, " using alternate %d", alt );
+                       DBGC ( usbnet, "\n" );
+                       return 0;
+               }
+       }
+
+       DBGC ( usbnet, "USBNET %s found no data interface\n",
+              usbnet->func->name );
+       return -ENOENT;
+}
+
+/**
+ * Describe USB network device interfaces
+ *
+ * @v usbnet           USB network device
+ * @v config           Configuration descriptor
+ * @ret rc             Return status code
+ */
+int usbnet_describe ( struct usbnet_device *usbnet,
+                     struct usb_configuration_descriptor *config ) {
+       int rc;
+
+       /* Describe communications interface */
+       if ( ( rc = usbnet_comms_describe ( usbnet, config ) ) != 0 )
+               return rc;
+
+       /* Describe data interface */
+       if ( ( rc = usbnet_data_describe ( usbnet, config ) ) != 0 )
+               return rc;
+
+       return 0;
+}
index 087475494842ebbadee8f9627c54e3f3ef189475..353d8ca3aa47f4ecb324d082ef2bf7b9ab5d116f 100644 (file)
@@ -163,6 +163,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
 #define ERRFILE_netvsc              ( ERRFILE_DRIVER | 0x006b0000 )
 #define ERRFILE_ecm                 ( ERRFILE_DRIVER | 0x006c0000 )
 #define ERRFILE_ncm                 ( ERRFILE_DRIVER | 0x006d0000 )
+#define ERRFILE_usbnet              ( ERRFILE_DRIVER | 0x006e0000 )
 
 #define ERRFILE_scsi                ( ERRFILE_DRIVER | 0x00700000 )
 #define ERRFILE_arbel               ( ERRFILE_DRIVER | 0x00710000 )
diff --git a/src/include/ipxe/usbnet.h b/src/include/ipxe/usbnet.h
new file mode 100644 (file)
index 0000000..b5c1e88
--- /dev/null
@@ -0,0 +1,62 @@
+#ifndef _IPXE_USBNET_H
+#define _IPXE_USBNET_H
+
+/** @file
+ *
+ * USB network devices
+ *
+ */
+
+FILE_LICENCE ( GPL2_OR_LATER );
+
+#include <ipxe/usb.h>
+
+/** A USB network device */
+struct usbnet_device {
+       /** USB function */
+       struct usb_function *func;
+
+       /** Communications interface */
+       unsigned int comms;
+       /** Data interface */
+       unsigned int data;
+       /** Alternate setting for data interface */
+       unsigned int alternate;
+
+       /** Interrupt endpoint */
+       struct usb_endpoint intr;
+       /** Bulk IN endpoint */
+       struct usb_endpoint in;
+       /** Bulk OUT endpoint */
+       struct usb_endpoint out;
+};
+
+/**
+ * Initialise USB network device
+ *
+ * @v usbnet           USB network device
+ * @v func             USB function
+ * @v intr             Interrupt endpoint operations
+ * @v in               Bulk IN endpoint operations
+ * @v out              Bulk OUT endpoint operations
+ */
+static inline __attribute__ (( always_inline )) void
+usbnet_init ( struct usbnet_device *usbnet, struct usb_function *func,
+             struct usb_endpoint_driver_operations *intr,
+             struct usb_endpoint_driver_operations *in,
+             struct usb_endpoint_driver_operations *out ) {
+       struct usb_device *usb = func->usb;
+
+       usbnet->func = func;
+       usb_endpoint_init ( &usbnet->intr, usb, intr );
+       usb_endpoint_init ( &usbnet->in, usb, in );
+       usb_endpoint_init ( &usbnet->out, usb, out );
+}
+
+extern int usbnet_open ( struct usbnet_device *usbnet );
+extern void usbnet_close ( struct usbnet_device *usbnet );
+extern int usbnet_refill ( struct usbnet_device *usbnet );
+extern int usbnet_describe ( struct usbnet_device *usbnet,
+                            struct usb_configuration_descriptor *config );
+
+#endif /* _IPXE_USBNET_H */