You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

usbnet.c 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * Copyright (C) 2015 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. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <string.h>
  25. #include <errno.h>
  26. #include <ipxe/usb.h>
  27. #include <ipxe/usbnet.h>
  28. /** @file
  29. *
  30. * USB network devices
  31. *
  32. * USB network devices use a variety of packet formats and interface
  33. * descriptors, but tend to have several features in common:
  34. *
  35. * - a single interrupt endpoint using the generic refill mechanism
  36. *
  37. * - a single bulk IN endpoint using the generic refill mechanism
  38. *
  39. * - a single bulk OUT endpoint
  40. *
  41. * - optional use of an alternate setting to enable the data interface
  42. *
  43. */
  44. /**
  45. * Open USB network device
  46. *
  47. * @v usbnet USB network device
  48. * @ret rc Return status code
  49. */
  50. int usbnet_open ( struct usbnet_device *usbnet ) {
  51. struct usb_device *usb = usbnet->func->usb;
  52. int rc;
  53. /* Open interrupt endpoint */
  54. if ( ( rc = usb_endpoint_open ( &usbnet->intr ) ) != 0 ) {
  55. DBGC ( usbnet, "USBNET %s could not open interrupt: %s\n",
  56. usbnet->func->name, strerror ( rc ) );
  57. goto err_open_intr;
  58. }
  59. /* Refill interrupt endpoint */
  60. if ( ( rc = usb_refill ( &usbnet->intr ) ) != 0 ) {
  61. DBGC ( usbnet, "USBNET %s could not refill interrupt: %s\n",
  62. usbnet->func->name, strerror ( rc ) );
  63. goto err_refill_intr;
  64. }
  65. /* Select alternate setting for data interface, if applicable */
  66. if ( usbnet->alternate &&
  67. ( ( rc = usb_set_interface ( usb, usbnet->data,
  68. usbnet->alternate ) ) != 0 ) ) {
  69. DBGC ( usbnet, "USBNET %s could not set alternate interface "
  70. "%d: %s\n", usbnet->func->name, usbnet->alternate,
  71. strerror ( rc ) );
  72. goto err_set_interface;
  73. }
  74. /* Open bulk IN endpoint */
  75. if ( ( rc = usb_endpoint_open ( &usbnet->in ) ) != 0 ) {
  76. DBGC ( usbnet, "USBNET %s could not open bulk IN: %s\n",
  77. usbnet->func->name, strerror ( rc ) );
  78. goto err_open_in;
  79. }
  80. /* Open bulk OUT endpoint */
  81. if ( ( rc = usb_endpoint_open ( &usbnet->out ) ) != 0 ) {
  82. DBGC ( usbnet, "USBNET %s could not open bulk OUT: %s\n",
  83. usbnet->func->name, strerror ( rc ) );
  84. goto err_open_out;
  85. }
  86. /* Refill bulk IN endpoint */
  87. if ( ( rc = usb_refill ( &usbnet->in ) ) != 0 ) {
  88. DBGC ( usbnet, "USBNET %s could not refill bulk IN: %s\n",
  89. usbnet->func->name, strerror ( rc ) );
  90. goto err_refill_in;
  91. }
  92. return 0;
  93. err_refill_in:
  94. usb_endpoint_close ( &usbnet->out );
  95. err_open_out:
  96. usb_endpoint_close ( &usbnet->in );
  97. err_open_in:
  98. if ( usbnet->alternate )
  99. usb_set_interface ( usb, usbnet->data, 0 );
  100. err_set_interface:
  101. err_refill_intr:
  102. usb_endpoint_close ( &usbnet->intr );
  103. err_open_intr:
  104. return rc;
  105. }
  106. /**
  107. * Close USB network device
  108. *
  109. * @v usbnet USB network device
  110. */
  111. void usbnet_close ( struct usbnet_device *usbnet ) {
  112. struct usb_device *usb = usbnet->func->usb;
  113. /* Close bulk OUT endpoint */
  114. usb_endpoint_close ( &usbnet->out );
  115. /* Close bulk IN endpoint */
  116. usb_endpoint_close ( &usbnet->in );
  117. /* Reset alternate setting for data interface, if applicable */
  118. if ( usbnet->alternate )
  119. usb_set_interface ( usb, usbnet->data, 0 );
  120. /* Close interrupt endpoint */
  121. usb_endpoint_close ( &usbnet->intr );
  122. }
  123. /**
  124. * Refill USB network device bulk IN and interrupt endpoints
  125. *
  126. * @v usbnet USB network device
  127. * @ret rc Return status code
  128. */
  129. int usbnet_refill ( struct usbnet_device *usbnet ) {
  130. int rc;
  131. /* Refill bulk IN endpoint */
  132. if ( ( rc = usb_refill ( &usbnet->in ) ) != 0 )
  133. return rc;
  134. /* Refill interrupt endpoint */
  135. if ( ( rc = usb_refill ( &usbnet->intr ) ) != 0 )
  136. return rc;
  137. return 0;
  138. }
  139. /**
  140. * Describe communications interface and interrupt endpoint
  141. *
  142. * @v usbnet USB network device
  143. * @v config Configuration descriptor
  144. * @ret rc Return status code
  145. */
  146. static int usbnet_comms_describe ( struct usbnet_device *usbnet,
  147. struct usb_configuration_descriptor *config){
  148. struct usb_interface_descriptor *desc;
  149. unsigned int comms;
  150. unsigned int i;
  151. int rc;
  152. /* Iterate over all available interfaces */
  153. for ( i = 0 ; i < usbnet->func->count ; i++ ) {
  154. /* Get interface number */
  155. comms = usbnet->func->interface[i];
  156. /* Locate interface descriptor */
  157. desc = usb_interface_descriptor ( config, comms, 0 );
  158. if ( ! desc )
  159. continue;
  160. /* Describe interrupt endpoint */
  161. if ( ( rc = usb_endpoint_described ( &usbnet->intr, config,
  162. desc, USB_INTERRUPT,
  163. 0 ) ) != 0 )
  164. continue;
  165. /* Record communications interface */
  166. usbnet->comms = comms;
  167. DBGC ( usbnet, "USBNET %s found communications interface %d\n",
  168. usbnet->func->name, comms );
  169. return 0;
  170. }
  171. DBGC ( usbnet, "USBNET %s found no communications interface\n",
  172. usbnet->func->name );
  173. return -ENOENT;
  174. }
  175. /**
  176. * Describe data interface and bulk endpoints
  177. *
  178. * @v usbnet USB network device
  179. * @v config Configuration descriptor
  180. * @ret rc Return status code
  181. */
  182. static int usbnet_data_describe ( struct usbnet_device *usbnet,
  183. struct usb_configuration_descriptor *config ){
  184. struct usb_interface_descriptor *desc;
  185. unsigned int data;
  186. unsigned int alt;
  187. unsigned int i;
  188. int rc;
  189. /* Iterate over all available interfaces */
  190. for ( i = 0 ; i < usbnet->func->count ; i++ ) {
  191. /* Get interface number */
  192. data = usbnet->func->interface[i];
  193. /* Iterate over all existent alternate settings */
  194. for ( alt = 0 ; ; alt++ ) {
  195. /* Locate interface descriptor */
  196. desc = usb_interface_descriptor ( config, data, alt );
  197. if ( ! desc )
  198. break;
  199. /* Describe bulk IN endpoint */
  200. if ( ( rc = usb_endpoint_described ( &usbnet->in,
  201. config, desc,
  202. USB_BULK_IN,
  203. 0 ) ) != 0 )
  204. continue;
  205. /* Describe bulk OUT endpoint */
  206. if ( ( rc = usb_endpoint_described ( &usbnet->out,
  207. config, desc,
  208. USB_BULK_OUT,
  209. 0 ) ) != 0 )
  210. continue;
  211. /* Record data interface and alternate setting */
  212. usbnet->data = data;
  213. usbnet->alternate = alt;
  214. DBGC ( usbnet, "USBNET %s found data interface %d",
  215. usbnet->func->name, data );
  216. if ( alt )
  217. DBGC ( usbnet, " using alternate %d", alt );
  218. DBGC ( usbnet, "\n" );
  219. return 0;
  220. }
  221. }
  222. DBGC ( usbnet, "USBNET %s found no data interface\n",
  223. usbnet->func->name );
  224. return -ENOENT;
  225. }
  226. /**
  227. * Describe USB network device interfaces
  228. *
  229. * @v usbnet USB network device
  230. * @v config Configuration descriptor
  231. * @ret rc Return status code
  232. */
  233. int usbnet_describe ( struct usbnet_device *usbnet,
  234. struct usb_configuration_descriptor *config ) {
  235. int rc;
  236. /* Describe communications interface */
  237. if ( ( rc = usbnet_comms_describe ( usbnet, config ) ) != 0 )
  238. return rc;
  239. /* Describe data interface */
  240. if ( ( rc = usbnet_data_describe ( usbnet, config ) ) != 0 )
  241. return rc;
  242. return 0;
  243. }