Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ecm.c 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <stdint.h>
  21. #include <errno.h>
  22. #include <ipxe/if_ether.h>
  23. #include <ipxe/base16.h>
  24. #include <ipxe/usb.h>
  25. #include "ecm.h"
  26. /** @file
  27. *
  28. * CDC-ECM USB Ethernet driver
  29. *
  30. */
  31. /**
  32. * Locate Ethernet functional descriptor
  33. *
  34. * @v config Configuration descriptor
  35. * @v interface Interface descriptor
  36. * @ret desc Descriptor, or NULL if not found
  37. */
  38. struct ecm_ethernet_descriptor *
  39. ecm_ethernet_descriptor ( struct usb_configuration_descriptor *config,
  40. struct usb_interface_descriptor *interface ) {
  41. struct ecm_ethernet_descriptor *desc;
  42. for_each_interface_descriptor ( desc, config, interface ) {
  43. if ( ( desc->header.type == USB_CS_INTERFACE_DESCRIPTOR ) &&
  44. ( desc->subtype == CDC_SUBTYPE_ETHERNET ) )
  45. return desc;
  46. }
  47. return NULL;
  48. }
  49. /**
  50. * Get hardware MAC address
  51. *
  52. * @v usb USB device
  53. * @v desc Ethernet functional descriptor
  54. * @v hw_addr Hardware address to fill in
  55. * @ret rc Return status code
  56. */
  57. int ecm_fetch_mac ( struct usb_device *usb,
  58. struct ecm_ethernet_descriptor *desc, uint8_t *hw_addr ) {
  59. char buf[ base16_encoded_len ( ETH_ALEN ) + 1 /* NUL */ ];
  60. int len;
  61. int rc;
  62. /* Fetch MAC address string */
  63. len = usb_get_string_descriptor ( usb, desc->mac, 0, buf,
  64. sizeof ( buf ) );
  65. if ( len < 0 ) {
  66. rc = len;
  67. return rc;
  68. }
  69. /* Sanity check */
  70. if ( len != ( ( int ) ( sizeof ( buf ) - 1 /* NUL */ ) ) )
  71. return -EINVAL;
  72. /* Decode MAC address */
  73. len = base16_decode ( buf, hw_addr );
  74. if ( len < 0 ) {
  75. rc = len;
  76. return rc;
  77. }
  78. return 0;
  79. }