123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522 |
- /*
- * Copyright (C) 2014 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.
- *
- * You can also choose to distribute this program under the terms of
- * the Unmodified Binary Distribution Licence (as given in the file
- * COPYING.UBDL), provided that you have satisfied its requirements.
- */
-
- FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
-
- #include <stdint.h>
- #include <errno.h>
- #include <ipxe/netdevice.h>
- #include <ipxe/ethernet.h>
- #include <ipxe/if_ether.h>
- #include <ipxe/base16.h>
- #include <ipxe/profile.h>
- #include <ipxe/usb.h>
- #include "ecm.h"
-
- /** @file
- *
- * CDC-ECM USB Ethernet driver
- *
- */
-
- /** Interrupt completion profiler */
- static struct profiler ecm_intr_profiler __profiler =
- { .name = "ecm.intr" };
-
- /** Bulk IN completion profiler */
- static struct profiler ecm_in_profiler __profiler =
- { .name = "ecm.in" };
-
- /** Bulk OUT profiler */
- static struct profiler ecm_out_profiler __profiler =
- { .name = "ecm.out" };
-
- /******************************************************************************
- *
- * Ethernet functional descriptor
- *
- ******************************************************************************
- */
-
- /**
- * Locate Ethernet functional descriptor
- *
- * @v config Configuration descriptor
- * @v interface Interface descriptor
- * @ret desc Descriptor, or NULL if not found
- */
- struct ecm_ethernet_descriptor *
- ecm_ethernet_descriptor ( struct usb_configuration_descriptor *config,
- struct usb_interface_descriptor *interface ) {
- struct ecm_ethernet_descriptor *desc;
-
- for_each_interface_descriptor ( desc, config, interface ) {
- if ( ( desc->header.type == USB_CS_INTERFACE_DESCRIPTOR ) &&
- ( desc->subtype == CDC_SUBTYPE_ETHERNET ) )
- return desc;
- }
- return NULL;
- }
-
- /**
- * Get hardware MAC address
- *
- * @v usb USB device
- * @v desc Ethernet functional descriptor
- * @v hw_addr Hardware address to fill in
- * @ret rc Return status code
- */
- int ecm_fetch_mac ( struct usb_device *usb,
- struct ecm_ethernet_descriptor *desc, uint8_t *hw_addr ) {
- char buf[ base16_encoded_len ( ETH_ALEN ) + 1 /* NUL */ ];
- int len;
- int rc;
-
- /* Fetch MAC address string */
- len = usb_get_string_descriptor ( usb, desc->mac, 0, buf,
- sizeof ( buf ) );
- if ( len < 0 ) {
- rc = len;
- return rc;
- }
-
- /* Sanity check */
- if ( len != ( ( int ) ( sizeof ( buf ) - 1 /* NUL */ ) ) ) {
- DBGC ( usb, "USB %s has invalid ECM MAC \"%s\"\n",
- usb->name, buf );
- return -EINVAL;
- }
-
- /* Decode MAC address */
- len = base16_decode ( buf, hw_addr, ETH_ALEN );
- if ( len < 0 ) {
- rc = len;
- DBGC ( usb, "USB %s could not decode ECM MAC \"%s\": %s\n",
- usb->name, buf, strerror ( rc ) );
- return rc;
- }
-
- return 0;
- }
-
- /******************************************************************************
- *
- * CDC-ECM communications interface
- *
- ******************************************************************************
- */
-
- /**
- * Complete interrupt transfer
- *
- * @v ep USB endpoint
- * @v iobuf I/O buffer
- * @v rc Completion status code
- */
- static void ecm_intr_complete ( struct usb_endpoint *ep,
- struct io_buffer *iobuf, int rc ) {
- struct ecm_device *ecm = container_of ( ep, struct ecm_device,
- usbnet.intr );
- struct net_device *netdev = ecm->netdev;
- struct usb_setup_packet *message;
- size_t len = iob_len ( iobuf );
-
- /* Profile completions */
- profile_start ( &ecm_intr_profiler );
-
- /* Ignore packets cancelled when the endpoint closes */
- if ( ! ep->open )
- goto ignore;
-
- /* Drop packets with errors */
- if ( rc != 0 ) {
- DBGC ( ecm, "ECM %p interrupt failed: %s\n",
- ecm, strerror ( rc ) );
- DBGC_HDA ( ecm, 0, iobuf->data, iob_len ( iobuf ) );
- goto error;
- }
-
- /* Extract message header */
- if ( len < sizeof ( *message ) ) {
- DBGC ( ecm, "ECM %p underlength interrupt:\n", ecm );
- DBGC_HDA ( ecm, 0, iobuf->data, iob_len ( iobuf ) );
- rc = -EINVAL;
- goto error;
- }
- message = iobuf->data;
-
- /* Parse message header */
- switch ( message->request ) {
-
- case cpu_to_le16 ( CDC_NETWORK_CONNECTION ) :
- if ( message->value && ! netdev_link_ok ( netdev ) ) {
- DBGC ( ecm, "ECM %p link up\n", ecm );
- netdev_link_up ( netdev );
- } else if ( netdev_link_ok ( netdev ) && ! message->value ) {
- DBGC ( ecm, "ECM %p link down\n", ecm );
- netdev_link_down ( netdev );
- }
- break;
-
- case cpu_to_le16 ( CDC_CONNECTION_SPEED_CHANGE ) :
- /* Ignore */
- break;
-
- default:
- DBGC ( ecm, "ECM %p unrecognised interrupt:\n", ecm );
- DBGC_HDA ( ecm, 0, iobuf->data, iob_len ( iobuf ) );
- rc = -ENOTSUP;
- goto error;
- }
-
- /* Free I/O buffer */
- free_iob ( iobuf );
- profile_stop ( &ecm_intr_profiler );
-
- return;
-
- error:
- netdev_rx_err ( netdev, iob_disown ( iobuf ), rc );
- ignore:
- free_iob ( iobuf );
- return;
- }
-
- /** Interrupt endpoint operations */
- static struct usb_endpoint_driver_operations ecm_intr_operations = {
- .complete = ecm_intr_complete,
- };
-
- /******************************************************************************
- *
- * CDC-ECM data interface
- *
- ******************************************************************************
- */
-
- /**
- * Complete bulk IN transfer
- *
- * @v ep USB endpoint
- * @v iobuf I/O buffer
- * @v rc Completion status code
- */
- static void ecm_in_complete ( struct usb_endpoint *ep, struct io_buffer *iobuf,
- int rc ) {
- struct ecm_device *ecm = container_of ( ep, struct ecm_device,
- usbnet.in );
- struct net_device *netdev = ecm->netdev;
-
- /* Profile receive completions */
- profile_start ( &ecm_in_profiler );
-
- /* Ignore packets cancelled when the endpoint closes */
- if ( ! ep->open )
- goto ignore;
-
- /* Record USB errors against the network device */
- if ( rc != 0 ) {
- DBGC ( ecm, "ECM %p bulk IN failed: %s\n",
- ecm, strerror ( rc ) );
- goto error;
- }
-
- /* Hand off to network stack */
- netdev_rx ( netdev, iob_disown ( iobuf ) );
-
- profile_stop ( &ecm_in_profiler );
- return;
-
- error:
- netdev_rx_err ( netdev, iob_disown ( iobuf ), rc );
- ignore:
- free_iob ( iobuf );
- }
-
- /** Bulk IN endpoint operations */
- static struct usb_endpoint_driver_operations ecm_in_operations = {
- .complete = ecm_in_complete,
- };
-
- /**
- * Transmit packet
- *
- * @v ecm CDC-ECM device
- * @v iobuf I/O buffer
- * @ret rc Return status code
- */
- static int ecm_out_transmit ( struct ecm_device *ecm,
- struct io_buffer *iobuf ) {
- int rc;
-
- /* Profile transmissions */
- profile_start ( &ecm_out_profiler );
-
- /* Enqueue I/O buffer */
- if ( ( rc = usb_stream ( &ecm->usbnet.out, iobuf, 1 ) ) != 0 )
- return rc;
-
- profile_stop ( &ecm_out_profiler );
- return 0;
- }
-
- /**
- * Complete bulk OUT transfer
- *
- * @v ep USB endpoint
- * @v iobuf I/O buffer
- * @v rc Completion status code
- */
- static void ecm_out_complete ( struct usb_endpoint *ep, struct io_buffer *iobuf,
- int rc ) {
- struct ecm_device *ecm = container_of ( ep, struct ecm_device,
- usbnet.out );
- struct net_device *netdev = ecm->netdev;
-
- /* Report TX completion */
- netdev_tx_complete_err ( netdev, iobuf, rc );
- }
-
- /** Bulk OUT endpoint operations */
- static struct usb_endpoint_driver_operations ecm_out_operations = {
- .complete = ecm_out_complete,
- };
-
- /******************************************************************************
- *
- * Network device interface
- *
- ******************************************************************************
- */
-
- /**
- * Open network device
- *
- * @v netdev Network device
- * @ret rc Return status code
- */
- static int ecm_open ( struct net_device *netdev ) {
- struct ecm_device *ecm = netdev->priv;
- struct usb_device *usb = ecm->usb;
- unsigned int filter;
- int rc;
-
- /* Open USB network device */
- if ( ( rc = usbnet_open ( &ecm->usbnet ) ) != 0 ) {
- DBGC ( ecm, "ECM %p could not open: %s\n",
- ecm, strerror ( rc ) );
- goto err_open;
- }
-
- /* Set packet filter */
- filter = ( ECM_PACKET_TYPE_PROMISCUOUS |
- ECM_PACKET_TYPE_ALL_MULTICAST |
- ECM_PACKET_TYPE_DIRECTED |
- ECM_PACKET_TYPE_BROADCAST );
- if ( ( rc = usb_control ( usb, ECM_SET_ETHERNET_PACKET_FILTER,
- filter, ecm->usbnet.comms, NULL, 0 ) ) != 0 ){
- DBGC ( ecm, "ECM %p could not set packet filter: %s\n",
- ecm, strerror ( rc ) );
- goto err_set_filter;
- }
-
- return 0;
-
- err_set_filter:
- usbnet_close ( &ecm->usbnet );
- err_open:
- return rc;
- }
-
- /**
- * Close network device
- *
- * @v netdev Network device
- */
- static void ecm_close ( struct net_device *netdev ) {
- struct ecm_device *ecm = netdev->priv;
-
- /* Close USB network device */
- usbnet_close ( &ecm->usbnet );
- }
-
- /**
- * Transmit packet
- *
- * @v netdev Network device
- * @v iobuf I/O buffer
- * @ret rc Return status code
- */
- static int ecm_transmit ( struct net_device *netdev,
- struct io_buffer *iobuf ) {
- struct ecm_device *ecm = netdev->priv;
- int rc;
-
- /* Transmit packet */
- if ( ( rc = ecm_out_transmit ( ecm, iobuf ) ) != 0 )
- return rc;
-
- return 0;
- }
-
- /**
- * Poll for completed and received packets
- *
- * @v netdev Network device
- */
- static void ecm_poll ( struct net_device *netdev ) {
- struct ecm_device *ecm = netdev->priv;
- int rc;
-
- /* Poll USB bus */
- usb_poll ( ecm->bus );
-
- /* Refill endpoints */
- if ( ( rc = usbnet_refill ( &ecm->usbnet ) ) != 0 )
- netdev_rx_err ( netdev, NULL, rc );
- }
-
- /** CDC-ECM network device operations */
- static struct net_device_operations ecm_operations = {
- .open = ecm_open,
- .close = ecm_close,
- .transmit = ecm_transmit,
- .poll = ecm_poll,
- };
-
- /******************************************************************************
- *
- * USB interface
- *
- ******************************************************************************
- */
-
- /**
- * Probe device
- *
- * @v func USB function
- * @v config Configuration descriptor
- * @ret rc Return status code
- */
- static int ecm_probe ( struct usb_function *func,
- struct usb_configuration_descriptor *config ) {
- struct usb_device *usb = func->usb;
- struct net_device *netdev;
- struct ecm_device *ecm;
- struct usb_interface_descriptor *comms;
- struct ecm_ethernet_descriptor *ethernet;
- int rc;
-
- /* Allocate and initialise structure */
- netdev = alloc_etherdev ( sizeof ( *ecm ) );
- if ( ! netdev ) {
- rc = -ENOMEM;
- goto err_alloc;
- }
- netdev_init ( netdev, &ecm_operations );
- netdev->dev = &func->dev;
- ecm = netdev->priv;
- memset ( ecm, 0, sizeof ( *ecm ) );
- ecm->usb = usb;
- ecm->bus = usb->port->hub->bus;
- ecm->netdev = netdev;
- usbnet_init ( &ecm->usbnet, func, &ecm_intr_operations,
- &ecm_in_operations, &ecm_out_operations );
- usb_refill_init ( &ecm->usbnet.intr, 0, 0, ECM_INTR_MAX_FILL );
- usb_refill_init ( &ecm->usbnet.in, 0, ECM_IN_MTU, ECM_IN_MAX_FILL );
- DBGC ( ecm, "ECM %p on %s\n", ecm, func->name );
-
- /* Describe USB network device */
- if ( ( rc = usbnet_describe ( &ecm->usbnet, config ) ) != 0 ) {
- DBGC ( ecm, "ECM %p could not describe: %s\n",
- ecm, strerror ( rc ) );
- goto err_describe;
- }
-
- /* Locate Ethernet descriptor */
- comms = usb_interface_descriptor ( config, ecm->usbnet.comms, 0 );
- assert ( comms != NULL );
- ethernet = ecm_ethernet_descriptor ( config, comms );
- if ( ! ethernet ) {
- DBGC ( ecm, "ECM %p has no Ethernet descriptor\n", ecm );
- rc = -EINVAL;
- goto err_ethernet;
- }
-
- /* Fetch MAC address */
- if ( ( rc = ecm_fetch_mac ( usb, ethernet, netdev->hw_addr ) ) != 0 ) {
- DBGC ( ecm, "ECM %p could not fetch MAC address: %s\n",
- ecm, strerror ( rc ) );
- goto err_fetch_mac;
- }
-
- /* Register network device */
- if ( ( rc = register_netdev ( netdev ) ) != 0 )
- goto err_register;
-
- usb_func_set_drvdata ( func, ecm );
- return 0;
-
- unregister_netdev ( netdev );
- err_register:
- err_fetch_mac:
- err_ethernet:
- err_describe:
- netdev_nullify ( netdev );
- netdev_put ( netdev );
- err_alloc:
- return rc;
- }
-
- /**
- * Remove device
- *
- * @v func USB function
- */
- static void ecm_remove ( struct usb_function *func ) {
- struct ecm_device *ecm = usb_func_get_drvdata ( func );
- struct net_device *netdev = ecm->netdev;
-
- unregister_netdev ( netdev );
- netdev_nullify ( netdev );
- netdev_put ( netdev );
- }
-
- /** CDC-ECM device IDs */
- static struct usb_device_id ecm_ids[] = {
- {
- .name = "cdc-ecm",
- .vendor = USB_ANY_ID,
- .product = USB_ANY_ID,
- },
- };
-
- /** CDC-ECM driver */
- struct usb_driver ecm_driver __usb_driver = {
- .ids = ecm_ids,
- .id_count = ( sizeof ( ecm_ids ) / sizeof ( ecm_ids[0] ) ),
- .class = USB_CLASS_ID ( USB_CLASS_CDC, USB_SUBCLASS_CDC_ECM, 0 ),
- .score = USB_SCORE_NORMAL,
- .probe = ecm_probe,
- .remove = ecm_remove,
- };
|