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.

efi_usb.c 35KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296
  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 <stdlib.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <unistd.h>
  28. #include <errno.h>
  29. #include <assert.h>
  30. #include <ipxe/efi/efi.h>
  31. #include <ipxe/efi/efi_utils.h>
  32. #include <ipxe/efi/efi_driver.h>
  33. #include <ipxe/efi/efi_usb.h>
  34. #include <ipxe/usb.h>
  35. /** @file
  36. *
  37. * EFI USB I/O PROTOCOL
  38. *
  39. */
  40. /**
  41. * Transcribe data direction (for debugging)
  42. *
  43. * @v direction Data direction
  44. * @ret text Transcribed data direction
  45. */
  46. static const char * efi_usb_direction_name ( EFI_USB_DATA_DIRECTION direction ){
  47. switch ( direction ) {
  48. case EfiUsbDataIn: return "in";
  49. case EfiUsbDataOut: return "out";
  50. case EfiUsbNoData: return "none";
  51. default: return "<UNKNOWN>";
  52. }
  53. }
  54. /******************************************************************************
  55. *
  56. * Endpoints
  57. *
  58. ******************************************************************************
  59. */
  60. /**
  61. * Poll USB bus (from endpoint event timer)
  62. *
  63. * @v event EFI event
  64. * @v context EFI USB endpoint
  65. */
  66. static VOID EFIAPI efi_usb_timer ( EFI_EVENT event __unused,
  67. VOID *context ) {
  68. struct efi_usb_endpoint *usbep = context;
  69. struct usb_bus *bus = usbep->usbintf->usbdev->usb->port->hub->bus;
  70. /* Poll bus */
  71. usb_poll ( bus );
  72. /* Refill endpoint */
  73. usb_refill ( &usbep->ep );
  74. }
  75. /**
  76. * Get endpoint MTU
  77. *
  78. * @v usbintf EFI USB interface
  79. * @v endpoint Endpoint address
  80. * @ret mtu Endpoint MTU, or negative error
  81. */
  82. static int efi_usb_mtu ( struct efi_usb_interface *usbintf,
  83. unsigned int endpoint ) {
  84. struct efi_usb_device *usbdev = usbintf->usbdev;
  85. struct usb_interface_descriptor *interface;
  86. struct usb_endpoint_descriptor *desc;
  87. /* Locate cached interface descriptor */
  88. interface = usb_interface_descriptor ( usbdev->config,
  89. usbintf->interface,
  90. usbintf->alternate );
  91. if ( ! interface ) {
  92. DBGC ( usbdev, "USBDEV %s alt %d has no interface descriptor\n",
  93. usbintf->name, usbintf->alternate );
  94. return -ENOENT;
  95. }
  96. /* Locate and copy cached endpoint descriptor */
  97. for_each_interface_descriptor ( desc, usbdev->config, interface ) {
  98. if ( ( desc->header.type == USB_ENDPOINT_DESCRIPTOR ) &&
  99. ( desc->endpoint == endpoint ) )
  100. return USB_ENDPOINT_MTU ( le16_to_cpu ( desc->sizes ) );
  101. }
  102. DBGC ( usbdev, "USBDEV %s alt %d ep %02x has no descriptor\n",
  103. usbintf->name, usbintf->alternate, endpoint );
  104. return -ENOENT;
  105. }
  106. /**
  107. * Open endpoint
  108. *
  109. * @v usbintf EFI USB interface
  110. * @v endpoint Endpoint address
  111. * @v attributes Endpoint attributes
  112. * @v interval Interval (in milliseconds)
  113. * @v driver Driver operations
  114. * @ret rc Return status code
  115. */
  116. static int efi_usb_open ( struct efi_usb_interface *usbintf,
  117. unsigned int endpoint, unsigned int attributes,
  118. unsigned int interval,
  119. struct usb_endpoint_driver_operations *driver ) {
  120. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  121. struct efi_usb_device *usbdev = usbintf->usbdev;
  122. struct efi_usb_endpoint *usbep;
  123. unsigned int index = USB_ENDPOINT_IDX ( endpoint );
  124. int mtu;
  125. EFI_STATUS efirc;
  126. int rc;
  127. /* Get endpoint MTU */
  128. mtu = efi_usb_mtu ( usbintf, endpoint );
  129. if ( mtu < 0 ) {
  130. rc = mtu;
  131. goto err_mtu;
  132. }
  133. /* Allocate and initialise structure */
  134. usbep = zalloc ( sizeof ( *usbep ) );
  135. if ( ! usbep ) {
  136. rc = -ENOMEM;
  137. goto err_alloc;
  138. }
  139. usbep->usbintf = usbintf;
  140. usb_endpoint_init ( &usbep->ep, usbdev->usb, driver );
  141. usb_endpoint_describe ( &usbep->ep, endpoint, attributes, mtu, 0,
  142. ( interval << 3 /* microframes */ ) );
  143. /* Open endpoint */
  144. if ( ( rc = usb_endpoint_open ( &usbep->ep ) ) != 0 ) {
  145. DBGC ( usbdev, "USBDEV %s %s could not open: %s\n",
  146. usbintf->name, usb_endpoint_name ( &usbep->ep ),
  147. strerror ( rc ) );
  148. goto err_open;
  149. }
  150. /* Record opened endpoint */
  151. usbintf->endpoint[index] = usbep;
  152. DBGC ( usbdev, "USBDEV %s %s opened\n",
  153. usbintf->name, usb_endpoint_name ( &usbep->ep ) );
  154. /* Create event */
  155. if ( ( efirc = bs->CreateEvent ( ( EVT_TIMER | EVT_NOTIFY_SIGNAL ),
  156. TPL_CALLBACK, efi_usb_timer, usbep,
  157. &usbep->event ) ) != 0 ) {
  158. rc = -EEFI ( efirc );
  159. DBGC ( usbdev, "USBDEV %s %s could not create event: %s\n",
  160. usbintf->name, usb_endpoint_name ( &usbep->ep ),
  161. strerror ( rc ) );
  162. goto err_event;
  163. }
  164. return 0;
  165. bs->CloseEvent ( usbep->event );
  166. err_event:
  167. usbintf->endpoint[index] = usbep;
  168. usb_endpoint_close ( &usbep->ep );
  169. err_open:
  170. free ( usbep );
  171. err_alloc:
  172. err_mtu:
  173. return rc;
  174. }
  175. /**
  176. * Close endpoint
  177. *
  178. * @v usbep EFI USB endpoint
  179. */
  180. static void efi_usb_close ( struct efi_usb_endpoint *usbep ) {
  181. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  182. struct efi_usb_interface *usbintf = usbep->usbintf;
  183. struct efi_usb_device *usbdev = usbintf->usbdev;
  184. unsigned int index = USB_ENDPOINT_IDX ( usbep->ep.address );
  185. /* Sanity check */
  186. assert ( usbintf->endpoint[index] == usbep );
  187. /* Cancel timer (if applicable) and close event */
  188. bs->SetTimer ( usbep->event, TimerCancel, 0 );
  189. bs->CloseEvent ( usbep->event );
  190. /* Close endpoint */
  191. usb_endpoint_close ( &usbep->ep );
  192. DBGC ( usbdev, "USBDEV %s %s closed\n",
  193. usbintf->name, usb_endpoint_name ( &usbep->ep ) );
  194. /* Free endpoint */
  195. free ( usbep );
  196. /* Record closed endpoint */
  197. usbintf->endpoint[index] = NULL;
  198. }
  199. /**
  200. * Close all endpoints
  201. *
  202. * @v usbintf EFI USB interface
  203. */
  204. static void efi_usb_close_all ( struct efi_usb_interface *usbintf ) {
  205. struct efi_usb_endpoint *usbep;
  206. unsigned int i;
  207. for ( i = 0 ; i < ( sizeof ( usbintf->endpoint ) /
  208. sizeof ( usbintf->endpoint[0] ) ) ; i++ ) {
  209. usbep = usbintf->endpoint[i];
  210. if ( usbep )
  211. efi_usb_close ( usbep );
  212. }
  213. }
  214. /**
  215. * Complete synchronous transfer
  216. *
  217. * @v ep USB endpoint
  218. * @v iobuf I/O buffer
  219. * @v rc Completion status code
  220. */
  221. static void efi_usb_sync_complete ( struct usb_endpoint *ep,
  222. struct io_buffer *iobuf __unused, int rc ) {
  223. struct efi_usb_endpoint *usbep =
  224. container_of ( ep, struct efi_usb_endpoint, ep );
  225. /* Record completion status */
  226. usbep->rc = rc;
  227. }
  228. /** Synchronous endpoint operations */
  229. static struct usb_endpoint_driver_operations efi_usb_sync_driver = {
  230. .complete = efi_usb_sync_complete,
  231. };
  232. /**
  233. * Perform synchronous transfer
  234. *
  235. * @v usbintf USB endpoint
  236. * @v endpoint Endpoint address
  237. * @v attributes Endpoint attributes
  238. * @v timeout Timeout (in milliseconds)
  239. * @v data Data buffer
  240. * @v len Length of data buffer
  241. * @ret rc Return status code
  242. */
  243. static int efi_usb_sync_transfer ( struct efi_usb_interface *usbintf,
  244. unsigned int endpoint,
  245. unsigned int attributes,
  246. unsigned int timeout,
  247. void *data, size_t *len ) {
  248. struct efi_usb_device *usbdev = usbintf->usbdev;
  249. struct efi_usb_endpoint *usbep;
  250. struct io_buffer *iobuf;
  251. unsigned int index = USB_ENDPOINT_IDX ( endpoint );
  252. unsigned int i;
  253. int rc;
  254. /* Open endpoint, if applicable */
  255. if ( ( ! usbintf->endpoint[index] ) &&
  256. ( ( rc = efi_usb_open ( usbintf, endpoint, attributes, 0,
  257. &efi_usb_sync_driver ) ) != 0 ) ) {
  258. goto err_open;
  259. }
  260. usbep = usbintf->endpoint[index];
  261. /* Allocate and construct I/O buffer */
  262. iobuf = alloc_iob ( *len );
  263. if ( ! iobuf ) {
  264. rc = -ENOMEM;
  265. goto err_alloc;
  266. }
  267. iob_put ( iobuf, *len );
  268. if ( ! ( endpoint & USB_ENDPOINT_IN ) )
  269. memcpy ( iobuf->data, data, *len );
  270. /* Initialise completion status */
  271. usbep->rc = -EINPROGRESS;
  272. /* Enqueue transfer */
  273. if ( ( rc = usb_stream ( &usbep->ep, iobuf, 0 ) ) != 0 ) {
  274. DBGC ( usbdev, "USBDEV %s %s could not enqueue: %s\n",
  275. usbintf->name, usb_endpoint_name ( &usbep->ep ),
  276. strerror ( rc ) );
  277. goto err_stream;
  278. }
  279. /* Wait for completion */
  280. rc = -ETIMEDOUT;
  281. for ( i = 0 ; ( ( timeout == 0 ) || ( i < timeout ) ) ; i++ ) {
  282. /* Poll bus */
  283. usb_poll ( usbdev->usb->port->hub->bus );
  284. /* Check for completion */
  285. if ( usbep->rc != -EINPROGRESS ) {
  286. rc = usbep->rc;
  287. break;
  288. }
  289. /* Delay */
  290. mdelay ( 1 );
  291. }
  292. /* Check for errors */
  293. if ( rc != 0 ) {
  294. DBGC ( usbdev, "USBDEV %s %s failed: %s\n", usbintf->name,
  295. usb_endpoint_name ( &usbep->ep ), strerror ( rc ) );
  296. goto err_completion;
  297. }
  298. /* Copy completion to data buffer, if applicable */
  299. assert ( iob_len ( iobuf ) <= *len );
  300. if ( endpoint & USB_ENDPOINT_IN )
  301. memcpy ( data, iobuf->data, iob_len ( iobuf ) );
  302. *len = iob_len ( iobuf );
  303. /* Free I/O buffer */
  304. free_iob ( iobuf );
  305. /* Leave endpoint open */
  306. return 0;
  307. err_completion:
  308. err_stream:
  309. free_iob ( iobuf );
  310. err_alloc:
  311. efi_usb_close ( usbep );
  312. err_open:
  313. return EFIRC ( rc );
  314. }
  315. /**
  316. * Complete asynchronous transfer
  317. *
  318. * @v ep USB endpoint
  319. * @v iobuf I/O buffer
  320. * @v rc Completion status code
  321. */
  322. static void efi_usb_async_complete ( struct usb_endpoint *ep,
  323. struct io_buffer *iobuf, int rc ) {
  324. struct efi_usb_endpoint *usbep =
  325. container_of ( ep, struct efi_usb_endpoint, ep );
  326. UINT32 status;
  327. /* Ignore packets cancelled when the endpoint closes */
  328. if ( ! ep->open )
  329. goto drop;
  330. /* Construct status */
  331. status = ( ( rc == 0 ) ? 0 : EFI_USB_ERR_STALL );
  332. /* Report completion */
  333. usbep->callback ( iobuf->data, iob_len ( iobuf ), usbep->context,
  334. status );
  335. drop:
  336. /* Recycle I/O buffer */
  337. usb_recycle ( &usbep->ep, iobuf );
  338. }
  339. /** Asynchronous endpoint operations */
  340. static struct usb_endpoint_driver_operations efi_usb_async_driver = {
  341. .complete = efi_usb_async_complete,
  342. };
  343. /**
  344. * Start asynchronous transfer
  345. *
  346. * @v usbintf EFI USB interface
  347. * @v endpoint Endpoint address
  348. * @v interval Interval (in milliseconds)
  349. * @v len Transfer length
  350. * @v callback Callback function
  351. * @v context Context for callback function
  352. * @ret rc Return status code
  353. */
  354. static int efi_usb_async_start ( struct efi_usb_interface *usbintf,
  355. unsigned int endpoint, unsigned int interval,
  356. size_t len,
  357. EFI_ASYNC_USB_TRANSFER_CALLBACK callback,
  358. void *context ) {
  359. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  360. struct efi_usb_device *usbdev = usbintf->usbdev;
  361. struct efi_usb_endpoint *usbep;
  362. unsigned int index = USB_ENDPOINT_IDX ( endpoint );
  363. EFI_STATUS efirc;
  364. int rc;
  365. /* Open endpoint */
  366. if ( ( rc = efi_usb_open ( usbintf, endpoint,
  367. USB_ENDPOINT_ATTR_INTERRUPT, interval,
  368. &efi_usb_async_driver ) ) != 0 )
  369. goto err_open;
  370. usbep = usbintf->endpoint[index];
  371. /* Record callback parameters */
  372. usbep->callback = callback;
  373. usbep->context = context;
  374. /* Prefill endpoint */
  375. usb_refill_init ( &usbep->ep, 0, len, EFI_USB_ASYNC_FILL );
  376. if ( ( rc = usb_prefill ( &usbep->ep ) ) != 0 ) {
  377. DBGC ( usbdev, "USBDEV %s %s could not prefill: %s\n",
  378. usbintf->name, usb_endpoint_name ( &usbep->ep ),
  379. strerror ( rc ) );
  380. goto err_prefill;
  381. }
  382. /* Start timer */
  383. if ( ( efirc = bs->SetTimer ( usbep->event, TimerPeriodic,
  384. ( interval * 10000 ) ) ) != 0 ) {
  385. rc = -EEFI ( efirc );
  386. DBGC ( usbdev, "USBDEV %s %s could not set timer: %s\n",
  387. usbintf->name, usb_endpoint_name ( &usbep->ep ),
  388. strerror ( rc ) );
  389. goto err_timer;
  390. }
  391. return 0;
  392. bs->SetTimer ( usbep->event, TimerCancel, 0 );
  393. err_timer:
  394. err_prefill:
  395. efi_usb_close ( usbep );
  396. err_open:
  397. return rc;
  398. }
  399. /**
  400. * Stop asynchronous transfer
  401. *
  402. * @v usbintf EFI USB interface
  403. * @v endpoint Endpoint address
  404. */
  405. static void efi_usb_async_stop ( struct efi_usb_interface *usbintf,
  406. unsigned int endpoint ) {
  407. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  408. struct efi_usb_endpoint *usbep;
  409. unsigned int index = USB_ENDPOINT_IDX ( endpoint );
  410. /* Do nothing if endpoint is already closed */
  411. usbep = usbintf->endpoint[index];
  412. if ( ! usbep )
  413. return;
  414. /* Stop timer */
  415. bs->SetTimer ( usbep->event, TimerCancel, 0 );
  416. /* Close endpoint */
  417. efi_usb_close ( usbep );
  418. }
  419. /******************************************************************************
  420. *
  421. * USB I/O protocol
  422. *
  423. ******************************************************************************
  424. */
  425. /**
  426. * Perform control transfer
  427. *
  428. * @v usbio USB I/O protocol
  429. * @v packet Setup packet
  430. * @v direction Data direction
  431. * @v timeout Timeout (in milliseconds)
  432. * @v data Data buffer
  433. * @v len Length of data
  434. * @ret status Transfer status
  435. * @ret efirc EFI status code
  436. */
  437. static EFI_STATUS EFIAPI
  438. efi_usb_control_transfer ( EFI_USB_IO_PROTOCOL *usbio,
  439. EFI_USB_DEVICE_REQUEST *packet,
  440. EFI_USB_DATA_DIRECTION direction,
  441. UINT32 timeout, VOID *data, UINTN len,
  442. UINT32 *status ) {
  443. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  444. struct efi_usb_interface *usbintf =
  445. container_of ( usbio, struct efi_usb_interface, usbio );
  446. struct efi_usb_device *usbdev = usbintf->usbdev;
  447. unsigned int request = ( packet->RequestType |
  448. USB_REQUEST_TYPE ( packet->Request ) );
  449. unsigned int value = le16_to_cpu ( packet->Value );
  450. unsigned int index = le16_to_cpu ( packet->Index );
  451. EFI_TPL saved_tpl;
  452. int rc;
  453. DBGC2 ( usbdev, "USBDEV %s control %04x:%04x:%04x:%04x %s %dms "
  454. "%p+%zx\n", usbintf->name, request, value, index,
  455. le16_to_cpu ( packet->Length ),
  456. efi_usb_direction_name ( direction ), timeout, data,
  457. ( ( size_t ) len ) );
  458. /* Raise TPL */
  459. saved_tpl = bs->RaiseTPL ( TPL_CALLBACK );
  460. /* Clear status */
  461. *status = 0;
  462. /* Block attempts to change the device configuration, since
  463. * this is logically impossible to do given the constraints of
  464. * the EFI_USB_IO_PROTOCOL design.
  465. */
  466. if ( ( request == USB_SET_CONFIGURATION ) &&
  467. ( value != usbdev->config->config ) ) {
  468. DBGC ( usbdev, "USBDEV %s cannot set configuration %d: not "
  469. "logically possible\n", usbintf->name, index );
  470. rc = -ENOTSUP;
  471. goto err_change_config;
  472. }
  473. /* If we are selecting a new alternate setting then close all
  474. * open endpoints.
  475. */
  476. if ( ( request == USB_SET_INTERFACE ) &&
  477. ( value != usbintf->alternate ) )
  478. efi_usb_close_all ( usbintf );
  479. /* Issue control transfer */
  480. if ( ( rc = usb_control ( usbdev->usb, request, value, index,
  481. data, len ) ) != 0 ) {
  482. DBGC ( usbdev, "USBDEV %s control %04x:%04x:%04x:%04x %p+%zx "
  483. "failed: %s\n", usbintf->name, request, value, index,
  484. le16_to_cpu ( packet->Length ), data, ( ( size_t ) len ),
  485. strerror ( rc ) );
  486. /* Assume that any error represents a stall */
  487. *status = EFI_USB_ERR_STALL;
  488. goto err_control;
  489. }
  490. /* Update alternate setting, if applicable */
  491. if ( request == USB_SET_INTERFACE ) {
  492. usbintf->alternate = value;
  493. DBGC ( usbdev, "USBDEV %s alt %d selected\n",
  494. usbintf->name, usbintf->alternate );
  495. }
  496. err_control:
  497. err_change_config:
  498. bs->RestoreTPL ( saved_tpl );
  499. return EFIRC ( rc );
  500. }
  501. /**
  502. * Perform bulk transfer
  503. *
  504. * @v usbio USB I/O protocol
  505. * @v endpoint Endpoint address
  506. * @v data Data buffer
  507. * @v len Length of data
  508. * @v timeout Timeout (in milliseconds)
  509. * @ret status Transfer status
  510. * @ret efirc EFI status code
  511. */
  512. static EFI_STATUS EFIAPI
  513. efi_usb_bulk_transfer ( EFI_USB_IO_PROTOCOL *usbio, UINT8 endpoint, VOID *data,
  514. UINTN *len, UINTN timeout, UINT32 *status ) {
  515. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  516. struct efi_usb_interface *usbintf =
  517. container_of ( usbio, struct efi_usb_interface, usbio );
  518. struct efi_usb_device *usbdev = usbintf->usbdev;
  519. size_t actual = *len;
  520. EFI_TPL saved_tpl;
  521. int rc;
  522. DBGC2 ( usbdev, "USBDEV %s bulk %s %p+%zx %dms\n", usbintf->name,
  523. ( ( endpoint & USB_ENDPOINT_IN ) ? "IN" : "OUT" ), data,
  524. ( ( size_t ) *len ), ( ( unsigned int ) timeout ) );
  525. /* Raise TPL */
  526. saved_tpl = bs->RaiseTPL ( TPL_CALLBACK );
  527. /* Clear status */
  528. *status = 0;
  529. /* Perform synchronous transfer */
  530. if ( ( rc = efi_usb_sync_transfer ( usbintf, endpoint,
  531. USB_ENDPOINT_ATTR_BULK, timeout,
  532. data, &actual ) ) != 0 ) {
  533. /* Assume that any error represents a timeout */
  534. *status = EFI_USB_ERR_TIMEOUT;
  535. goto err_transfer;
  536. }
  537. err_transfer:
  538. bs->RestoreTPL ( saved_tpl );
  539. return EFIRC ( rc );
  540. }
  541. /**
  542. * Perform synchronous interrupt transfer
  543. *
  544. * @v usbio USB I/O protocol
  545. * @v endpoint Endpoint address
  546. * @v data Data buffer
  547. * @v len Length of data
  548. * @v timeout Timeout (in milliseconds)
  549. * @ret status Transfer status
  550. * @ret efirc EFI status code
  551. */
  552. static EFI_STATUS EFIAPI
  553. efi_usb_sync_interrupt_transfer ( EFI_USB_IO_PROTOCOL *usbio, UINT8 endpoint,
  554. VOID *data, UINTN *len, UINTN timeout,
  555. UINT32 *status ) {
  556. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  557. struct efi_usb_interface *usbintf =
  558. container_of ( usbio, struct efi_usb_interface, usbio );
  559. struct efi_usb_device *usbdev = usbintf->usbdev;
  560. size_t actual = *len;
  561. EFI_TPL saved_tpl;
  562. int rc;
  563. DBGC2 ( usbdev, "USBDEV %s sync intr %s %p+%zx %dms\n", usbintf->name,
  564. ( ( endpoint & USB_ENDPOINT_IN ) ? "IN" : "OUT" ), data,
  565. ( ( size_t ) *len ), ( ( unsigned int ) timeout ) );
  566. /* Raise TPL */
  567. saved_tpl = bs->RaiseTPL ( TPL_CALLBACK );
  568. /* Clear status */
  569. *status = 0;
  570. /* Perform synchronous transfer */
  571. if ( ( rc = efi_usb_sync_transfer ( usbintf, endpoint,
  572. USB_ENDPOINT_ATTR_INTERRUPT,
  573. timeout, data, &actual ) ) != 0 ) {
  574. /* Assume that any error represents a timeout */
  575. *status = EFI_USB_ERR_TIMEOUT;
  576. goto err_transfer;
  577. }
  578. err_transfer:
  579. bs->RestoreTPL ( saved_tpl );
  580. return EFIRC ( rc );
  581. }
  582. /**
  583. * Perform asynchronous interrupt transfer
  584. *
  585. * @v usbio USB I/O protocol
  586. * @v endpoint Endpoint address
  587. * @v start Start (rather than stop) transfer
  588. * @v interval Polling interval (in milliseconds)
  589. * @v len Data length
  590. * @v callback Callback function
  591. * @v context Context for callback function
  592. * @ret efirc EFI status code
  593. */
  594. static EFI_STATUS EFIAPI
  595. efi_usb_async_interrupt_transfer ( EFI_USB_IO_PROTOCOL *usbio, UINT8 endpoint,
  596. BOOLEAN start, UINTN interval, UINTN len,
  597. EFI_ASYNC_USB_TRANSFER_CALLBACK callback,
  598. VOID *context ) {
  599. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  600. struct efi_usb_interface *usbintf =
  601. container_of ( usbio, struct efi_usb_interface, usbio );
  602. struct efi_usb_device *usbdev = usbintf->usbdev;
  603. EFI_TPL saved_tpl;
  604. int rc;
  605. DBGC2 ( usbdev, "USBDEV %s async intr %s len %#zx int %d %p/%p\n",
  606. usbintf->name,
  607. ( ( endpoint & USB_ENDPOINT_IN ) ? "IN" : "OUT" ),
  608. ( ( size_t ) len ), ( ( unsigned int ) interval ),
  609. callback, context );
  610. /* Raise TPL */
  611. saved_tpl = bs->RaiseTPL ( TPL_CALLBACK );
  612. /* Start/stop transfer as applicable */
  613. if ( start ) {
  614. /* Start new transfer */
  615. if ( ( rc = efi_usb_async_start ( usbintf, endpoint, interval,
  616. len, callback,
  617. context ) ) != 0 )
  618. goto err_start;
  619. } else {
  620. /* Stop transfer */
  621. efi_usb_async_stop ( usbintf, endpoint );
  622. /* Success */
  623. rc = 0;
  624. }
  625. err_start:
  626. bs->RestoreTPL ( saved_tpl );
  627. return EFIRC ( rc );
  628. }
  629. /**
  630. * Perform synchronous isochronous transfer
  631. *
  632. * @v usbio USB I/O protocol
  633. * @v endpoint Endpoint address
  634. * @v data Data buffer
  635. * @v len Length of data
  636. * @ret status Transfer status
  637. * @ret efirc EFI status code
  638. */
  639. static EFI_STATUS EFIAPI
  640. efi_usb_isochronous_transfer ( EFI_USB_IO_PROTOCOL *usbio, UINT8 endpoint,
  641. VOID *data, UINTN len, UINT32 *status ) {
  642. struct efi_usb_interface *usbintf =
  643. container_of ( usbio, struct efi_usb_interface, usbio );
  644. struct efi_usb_device *usbdev = usbintf->usbdev;
  645. DBGC2 ( usbdev, "USBDEV %s sync iso %s %p+%zx\n", usbintf->name,
  646. ( ( endpoint & USB_ENDPOINT_IN ) ? "IN" : "OUT" ), data,
  647. ( ( size_t ) len ) );
  648. /* Clear status */
  649. *status = 0;
  650. /* Not supported */
  651. return EFI_UNSUPPORTED;
  652. }
  653. /**
  654. * Perform asynchronous isochronous transfers
  655. *
  656. * @v usbio USB I/O protocol
  657. * @v endpoint Endpoint address
  658. * @v data Data buffer
  659. * @v len Length of data
  660. * @v callback Callback function
  661. * @v context Context for callback function
  662. * @ret status Transfer status
  663. * @ret efirc EFI status code
  664. */
  665. static EFI_STATUS EFIAPI
  666. efi_usb_async_isochronous_transfer ( EFI_USB_IO_PROTOCOL *usbio, UINT8 endpoint,
  667. VOID *data, UINTN len,
  668. EFI_ASYNC_USB_TRANSFER_CALLBACK callback,
  669. VOID *context ) {
  670. struct efi_usb_interface *usbintf =
  671. container_of ( usbio, struct efi_usb_interface, usbio );
  672. struct efi_usb_device *usbdev = usbintf->usbdev;
  673. DBGC2 ( usbdev, "USBDEV %s async iso %s %p+%zx %p/%p\n", usbintf->name,
  674. ( ( endpoint & USB_ENDPOINT_IN ) ? "IN" : "OUT" ), data,
  675. ( ( size_t ) len ), callback, context );
  676. /* Not supported */
  677. return EFI_UNSUPPORTED;
  678. }
  679. /**
  680. * Get device descriptor
  681. *
  682. * @v usbio USB I/O protocol
  683. * @ret efidesc EFI device descriptor
  684. * @ret efirc EFI status code
  685. */
  686. static EFI_STATUS EFIAPI
  687. efi_usb_get_device_descriptor ( EFI_USB_IO_PROTOCOL *usbio,
  688. EFI_USB_DEVICE_DESCRIPTOR *efidesc ) {
  689. struct efi_usb_interface *usbintf =
  690. container_of ( usbio, struct efi_usb_interface, usbio );
  691. struct efi_usb_device *usbdev = usbintf->usbdev;
  692. DBGC2 ( usbdev, "USBDEV %s get device descriptor\n", usbintf->name );
  693. /* Copy cached device descriptor */
  694. memcpy ( efidesc, &usbdev->usb->device, sizeof ( *efidesc ) );
  695. return 0;
  696. }
  697. /**
  698. * Get configuration descriptor
  699. *
  700. * @v usbio USB I/O protocol
  701. * @ret efidesc EFI interface descriptor
  702. * @ret efirc EFI status code
  703. */
  704. static EFI_STATUS EFIAPI
  705. efi_usb_get_config_descriptor ( EFI_USB_IO_PROTOCOL *usbio,
  706. EFI_USB_CONFIG_DESCRIPTOR *efidesc ) {
  707. struct efi_usb_interface *usbintf =
  708. container_of ( usbio, struct efi_usb_interface, usbio );
  709. struct efi_usb_device *usbdev = usbintf->usbdev;
  710. DBGC2 ( usbdev, "USBDEV %s get configuration descriptor\n",
  711. usbintf->name );
  712. /* Copy cached configuration descriptor */
  713. memcpy ( efidesc, usbdev->config, sizeof ( *efidesc ) );
  714. return 0;
  715. }
  716. /**
  717. * Get interface descriptor
  718. *
  719. * @v usbio USB I/O protocol
  720. * @ret efidesc EFI interface descriptor
  721. * @ret efirc EFI status code
  722. */
  723. static EFI_STATUS EFIAPI
  724. efi_usb_get_interface_descriptor ( EFI_USB_IO_PROTOCOL *usbio,
  725. EFI_USB_INTERFACE_DESCRIPTOR *efidesc ) {
  726. struct efi_usb_interface *usbintf =
  727. container_of ( usbio, struct efi_usb_interface, usbio );
  728. struct efi_usb_device *usbdev = usbintf->usbdev;
  729. struct usb_interface_descriptor *desc;
  730. DBGC2 ( usbdev, "USBDEV %s get interface descriptor\n", usbintf->name );
  731. /* Locate cached interface descriptor */
  732. desc = usb_interface_descriptor ( usbdev->config, usbintf->interface,
  733. usbintf->alternate );
  734. if ( ! desc ) {
  735. DBGC ( usbdev, "USBDEV %s alt %d has no interface descriptor\n",
  736. usbintf->name, usbintf->alternate );
  737. return -ENOENT;
  738. }
  739. /* Copy cached interface descriptor */
  740. memcpy ( efidesc, desc, sizeof ( *efidesc ) );
  741. return 0;
  742. }
  743. /**
  744. * Get endpoint descriptor
  745. *
  746. * @v usbio USB I/O protocol
  747. * @v address Endpoint index
  748. * @ret efidesc EFI interface descriptor
  749. * @ret efirc EFI status code
  750. */
  751. static EFI_STATUS EFIAPI
  752. efi_usb_get_endpoint_descriptor ( EFI_USB_IO_PROTOCOL *usbio, UINT8 index,
  753. EFI_USB_ENDPOINT_DESCRIPTOR *efidesc ) {
  754. struct efi_usb_interface *usbintf =
  755. container_of ( usbio, struct efi_usb_interface, usbio );
  756. struct efi_usb_device *usbdev = usbintf->usbdev;
  757. struct usb_interface_descriptor *interface;
  758. struct usb_endpoint_descriptor *desc;
  759. DBGC2 ( usbdev, "USBDEV %s get endpoint %d descriptor\n",
  760. usbintf->name, index );
  761. /* Locate cached interface descriptor */
  762. interface = usb_interface_descriptor ( usbdev->config,
  763. usbintf->interface,
  764. usbintf->alternate );
  765. if ( ! interface ) {
  766. DBGC ( usbdev, "USBDEV %s alt %d has no interface descriptor\n",
  767. usbintf->name, usbintf->alternate );
  768. return -ENOENT;
  769. }
  770. /* Locate and copy cached endpoint descriptor */
  771. for_each_interface_descriptor ( desc, usbdev->config, interface ) {
  772. if ( ( desc->header.type == USB_ENDPOINT_DESCRIPTOR ) &&
  773. ( index-- == 0 ) ) {
  774. memcpy ( efidesc, desc, sizeof ( *efidesc ) );
  775. return 0;
  776. }
  777. }
  778. return -ENOENT;
  779. }
  780. /**
  781. * Get string descriptor
  782. *
  783. * @v usbio USB I/O protocol
  784. * @v language Language ID
  785. * @v index String index
  786. * @ret string String
  787. * @ret efirc EFI status code
  788. */
  789. static EFI_STATUS EFIAPI
  790. efi_usb_get_string_descriptor ( EFI_USB_IO_PROTOCOL *usbio, UINT16 language,
  791. UINT8 index, CHAR16 **string ) {
  792. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  793. struct efi_usb_interface *usbintf =
  794. container_of ( usbio, struct efi_usb_interface, usbio );
  795. struct efi_usb_device *usbdev = usbintf->usbdev;
  796. struct usb_descriptor_header header;
  797. VOID *buffer;
  798. size_t len;
  799. EFI_TPL saved_tpl;
  800. EFI_STATUS efirc;
  801. int rc;
  802. DBGC2 ( usbdev, "USBDEV %s get string %d:%d descriptor\n",
  803. usbintf->name, language, index );
  804. /* Raise TPL */
  805. saved_tpl = bs->RaiseTPL ( TPL_CALLBACK );
  806. /* Read descriptor header */
  807. if ( ( rc = usb_get_descriptor ( usbdev->usb, 0, USB_STRING_DESCRIPTOR,
  808. index, language, &header,
  809. sizeof ( header ) ) ) != 0 ) {
  810. DBGC ( usbdev, "USBDEV %s could not get string %d:%d "
  811. "descriptor header: %s\n", usbintf->name, language,
  812. index, strerror ( rc ) );
  813. goto err_get_header;
  814. }
  815. len = header.len;
  816. /* Allocate buffer */
  817. if ( ( efirc = bs->AllocatePool ( EfiBootServicesData, len,
  818. &buffer ) ) != 0 ) {
  819. rc = -EEFI ( efirc );
  820. goto err_alloc;
  821. }
  822. /* Read whole descriptor */
  823. if ( ( rc = usb_get_descriptor ( usbdev->usb, 0, USB_STRING_DESCRIPTOR,
  824. index, language, buffer,
  825. len ) ) != 0 ) {
  826. DBGC ( usbdev, "USBDEV %s could not get string %d:%d "
  827. "descriptor: %s\n", usbintf->name, language,
  828. index, strerror ( rc ) );
  829. goto err_get_descriptor;
  830. }
  831. /* Shuffle down and terminate string */
  832. memmove ( buffer, ( buffer + sizeof ( header ) ),
  833. ( len - sizeof ( header ) ) );
  834. memset ( ( buffer + len - sizeof ( header ) ), 0, sizeof ( **string ) );
  835. /* Restore TPL */
  836. bs->RestoreTPL ( saved_tpl );
  837. /* Return allocated string */
  838. *string = buffer;
  839. return 0;
  840. err_get_descriptor:
  841. bs->FreePool ( buffer );
  842. err_alloc:
  843. err_get_header:
  844. bs->RestoreTPL ( saved_tpl );
  845. return EFIRC ( rc );
  846. }
  847. /**
  848. * Get supported languages
  849. *
  850. * @v usbio USB I/O protocol
  851. * @ret languages Language ID table
  852. * @ret len Length of language ID table
  853. * @ret efirc EFI status code
  854. */
  855. static EFI_STATUS EFIAPI
  856. efi_usb_get_supported_languages ( EFI_USB_IO_PROTOCOL *usbio,
  857. UINT16 **languages, UINT16 *len ) {
  858. struct efi_usb_interface *usbintf =
  859. container_of ( usbio, struct efi_usb_interface, usbio );
  860. struct efi_usb_device *usbdev = usbintf->usbdev;
  861. DBGC2 ( usbdev, "USBDEV %s get supported languages\n", usbintf->name );
  862. /* Return cached supported languages */
  863. *languages = ( ( ( void * ) usbdev->languages ) +
  864. sizeof ( *(usbdev->languages) ) );
  865. *len = usbdev->languages->len;
  866. return 0;
  867. }
  868. /**
  869. * Reset port
  870. *
  871. * @v usbio USB I/O protocol
  872. * @ret efirc EFI status code
  873. */
  874. static EFI_STATUS EFIAPI
  875. efi_usb_port_reset ( EFI_USB_IO_PROTOCOL *usbio ) {
  876. struct efi_usb_interface *usbintf =
  877. container_of ( usbio, struct efi_usb_interface, usbio );
  878. struct efi_usb_device *usbdev = usbintf->usbdev;
  879. DBGC2 ( usbdev, "USBDEV %s reset port\n", usbintf->name );
  880. /* This is logically impossible to do, since resetting the
  881. * port may destroy state belonging to other
  882. * EFI_USB_IO_PROTOCOL instances belonging to the same USB
  883. * device. (This is yet another artifact of the incredibly
  884. * poor design of the EFI_USB_IO_PROTOCOL.)
  885. */
  886. return EFI_INVALID_PARAMETER;
  887. }
  888. /** USB I/O protocol */
  889. static EFI_USB_IO_PROTOCOL efi_usb_io_protocol = {
  890. .UsbControlTransfer = efi_usb_control_transfer,
  891. .UsbBulkTransfer = efi_usb_bulk_transfer,
  892. .UsbAsyncInterruptTransfer = efi_usb_async_interrupt_transfer,
  893. .UsbSyncInterruptTransfer = efi_usb_sync_interrupt_transfer,
  894. .UsbIsochronousTransfer = efi_usb_isochronous_transfer,
  895. .UsbAsyncIsochronousTransfer = efi_usb_async_isochronous_transfer,
  896. .UsbGetDeviceDescriptor = efi_usb_get_device_descriptor,
  897. .UsbGetConfigDescriptor = efi_usb_get_config_descriptor,
  898. .UsbGetInterfaceDescriptor = efi_usb_get_interface_descriptor,
  899. .UsbGetEndpointDescriptor = efi_usb_get_endpoint_descriptor,
  900. .UsbGetStringDescriptor = efi_usb_get_string_descriptor,
  901. .UsbGetSupportedLanguages = efi_usb_get_supported_languages,
  902. .UsbPortReset = efi_usb_port_reset,
  903. };
  904. /******************************************************************************
  905. *
  906. * USB driver
  907. *
  908. ******************************************************************************
  909. */
  910. /**
  911. * Install interface
  912. *
  913. * @v usbdev EFI USB device
  914. * @v interface Interface number
  915. * @ret rc Return status code
  916. */
  917. static int efi_usb_install ( struct efi_usb_device *usbdev,
  918. unsigned int interface ) {
  919. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  920. struct efi_device *efidev = usbdev->efidev;
  921. struct efi_usb_interface *usbintf;
  922. struct usb_device *usb;
  923. EFI_DEVICE_PATH_PROTOCOL *path_end;
  924. USB_DEVICE_PATH *usbpath;
  925. unsigned int path_count;
  926. size_t path_prefix_len;
  927. size_t path_len;
  928. EFI_STATUS efirc;
  929. int rc;
  930. /* Calculate device path length */
  931. path_count = ( usb_depth ( usbdev->usb ) + 1 );
  932. path_prefix_len = efi_devpath_len ( efidev->path );
  933. path_len = ( path_prefix_len + ( path_count * sizeof ( *usbpath ) ) +
  934. sizeof ( *path_end ) );
  935. /* Allocate and initialise structure */
  936. usbintf = zalloc ( sizeof ( *usbintf ) + path_len );
  937. if ( ! usbintf ) {
  938. rc = -ENOMEM;
  939. goto err_alloc;
  940. }
  941. snprintf ( usbintf->name, sizeof ( usbintf->name ), "%s[%d]",
  942. usbdev->name, interface );
  943. usbintf->usbdev = usbdev;
  944. usbintf->interface = interface;
  945. memcpy ( &usbintf->usbio, &efi_usb_io_protocol,
  946. sizeof ( usbintf->usbio ) );
  947. usbintf->path = ( ( ( void * ) usbintf ) + sizeof ( *usbintf ) );
  948. /* Construct device path */
  949. memcpy ( usbintf->path, efidev->path, path_prefix_len );
  950. path_end = ( ( ( void * ) usbintf->path ) + path_len -
  951. sizeof ( *path_end ) );
  952. path_end->Type = END_DEVICE_PATH_TYPE;
  953. path_end->SubType = END_ENTIRE_DEVICE_PATH_SUBTYPE;
  954. path_end->Length[0] = sizeof ( *path_end );
  955. usbpath = ( ( ( void * ) path_end ) - sizeof ( *usbpath ) );
  956. usbpath->InterfaceNumber = interface;
  957. for ( usb = usbdev->usb ; usb ; usbpath--, usb = usb->port->hub->usb ) {
  958. usbpath->Header.Type = MESSAGING_DEVICE_PATH;
  959. usbpath->Header.SubType = MSG_USB_DP;
  960. usbpath->Header.Length[0] = sizeof ( *usbpath );
  961. usbpath->ParentPortNumber = usb->port->address;
  962. }
  963. /* Add to list of interfaces */
  964. list_add_tail ( &usbintf->list, &usbdev->interfaces );
  965. /* Install protocols */
  966. if ( ( efirc = bs->InstallMultipleProtocolInterfaces (
  967. &usbintf->handle,
  968. &efi_usb_io_protocol_guid, &usbintf->usbio,
  969. &efi_device_path_protocol_guid, usbintf->path,
  970. NULL ) ) != 0 ) {
  971. rc = -EEFI ( efirc );
  972. DBGC ( usbdev, "USBDEV %s could not install protocols: %s\n",
  973. usbintf->name, strerror ( rc ) );
  974. goto err_install_protocol;
  975. }
  976. DBGC ( usbdev, "USBDEV %s installed as %s\n",
  977. usbintf->name, efi_handle_name ( usbintf->handle ) );
  978. return 0;
  979. efi_usb_close_all ( usbintf );
  980. bs->UninstallMultipleProtocolInterfaces (
  981. usbintf->handle,
  982. &efi_usb_io_protocol_guid, &usbintf->usbio,
  983. &efi_device_path_protocol_guid, usbintf->path,
  984. NULL );
  985. err_install_protocol:
  986. list_del ( &usbintf->list );
  987. free ( usbintf );
  988. err_alloc:
  989. return rc;
  990. }
  991. /**
  992. * Uninstall interface
  993. *
  994. * @v usbintf EFI USB interface
  995. */
  996. static void efi_usb_uninstall ( struct efi_usb_interface *usbintf ) {
  997. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  998. /* Close all endpoints */
  999. efi_usb_close_all ( usbintf );
  1000. /* Uninstall protocols */
  1001. bs->UninstallMultipleProtocolInterfaces (
  1002. usbintf->handle,
  1003. &efi_usb_io_protocol_guid, &usbintf->usbio,
  1004. &efi_device_path_protocol_guid, usbintf->path,
  1005. NULL );
  1006. /* Remove from list of interfaces */
  1007. list_del ( &usbintf->list );
  1008. /* Free interface */
  1009. free ( usbintf );
  1010. }
  1011. /**
  1012. * Uninstall all interfaces
  1013. *
  1014. * @v usbdev EFI USB device
  1015. */
  1016. static void efi_usb_uninstall_all ( struct efi_usb_device *efiusb ) {
  1017. struct efi_usb_interface *usbintf;
  1018. /* Uninstall all interfaces */
  1019. while ( ( usbintf = list_first_entry ( &efiusb->interfaces,
  1020. struct efi_usb_interface,
  1021. list ) ) ) {
  1022. efi_usb_uninstall ( usbintf );
  1023. }
  1024. }
  1025. /**
  1026. * Probe device
  1027. *
  1028. * @v func USB function
  1029. * @v config Configuration descriptor
  1030. * @ret rc Return status code
  1031. */
  1032. static int efi_usb_probe ( struct usb_function *func,
  1033. struct usb_configuration_descriptor *config ) {
  1034. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  1035. struct usb_device *usb = func->usb;
  1036. struct efi_usb_device *usbdev;
  1037. struct efi_usb_interface *usbintf;
  1038. struct efi_device *efidev;
  1039. struct usb_descriptor_header header;
  1040. size_t config_len;
  1041. unsigned int i;
  1042. int rc;
  1043. /* Find parent EFI device */
  1044. efidev = efidev_parent ( &func->dev );
  1045. if ( ! efidev ) {
  1046. rc = -ENOTTY;
  1047. goto err_no_efidev;
  1048. }
  1049. /* Get configuration length */
  1050. config_len = le16_to_cpu ( config->len );
  1051. /* Get supported languages descriptor header */
  1052. if ( ( rc = usb_get_descriptor ( usb, 0, USB_STRING_DESCRIPTOR, 0, 0,
  1053. &header, sizeof ( header ) ) ) != 0 ) {
  1054. /* Assume no strings are present */
  1055. header.len = 0;
  1056. }
  1057. /* Allocate and initialise structure */
  1058. usbdev = zalloc ( sizeof ( *usbdev ) + config_len + header.len );
  1059. if ( ! usbdev ) {
  1060. rc = -ENOMEM;
  1061. goto err_alloc;
  1062. }
  1063. usb_func_set_drvdata ( func, usbdev );
  1064. usbdev->name = func->name;
  1065. usbdev->usb = usb;
  1066. usbdev->efidev = efidev;
  1067. usbdev->config = ( ( ( void * ) usbdev ) + sizeof ( *usbdev ) );
  1068. memcpy ( usbdev->config, config, config_len );
  1069. usbdev->languages = ( ( ( void * ) usbdev->config ) + config_len );
  1070. INIT_LIST_HEAD ( &usbdev->interfaces );
  1071. /* Get supported languages descriptor */
  1072. if ( header.len &&
  1073. ( rc = usb_get_descriptor ( usb, 0, USB_STRING_DESCRIPTOR, 0, 0,
  1074. usbdev->languages,
  1075. header.len ) ) != 0 ) {
  1076. DBGC ( usbdev, "USBDEV %s could not get supported languages: "
  1077. "%s\n", usbdev->name, strerror ( rc ) );
  1078. goto err_get_languages;
  1079. }
  1080. /* Install interfaces */
  1081. for ( i = 0 ; i < func->desc.count ; i++ ) {
  1082. if ( ( rc = efi_usb_install ( usbdev,
  1083. func->interface[i] ) ) != 0 )
  1084. goto err_install;
  1085. }
  1086. /* Connect any external drivers */
  1087. list_for_each_entry ( usbintf, &usbdev->interfaces, list )
  1088. bs->ConnectController ( usbintf->handle, NULL, NULL, TRUE );
  1089. return 0;
  1090. err_install:
  1091. efi_usb_uninstall_all ( usbdev );
  1092. assert ( list_empty ( &usbdev->interfaces ) );
  1093. err_get_languages:
  1094. free ( usbdev );
  1095. err_alloc:
  1096. err_no_efidev:
  1097. return rc;
  1098. }
  1099. /**
  1100. * Remove device
  1101. *
  1102. * @v func USB function
  1103. */
  1104. static void efi_usb_remove ( struct usb_function *func ) {
  1105. struct efi_usb_device *usbdev = usb_func_get_drvdata ( func );
  1106. /* Uninstall all interfaces */
  1107. efi_usb_uninstall_all ( usbdev );
  1108. assert ( list_empty ( &usbdev->interfaces ) );
  1109. /* Free device */
  1110. free ( usbdev );
  1111. }
  1112. /** USB I/O protocol device IDs */
  1113. static struct usb_device_id efi_usb_ids[] = {
  1114. {
  1115. .name = "usbio",
  1116. .vendor = USB_ANY_ID,
  1117. .product = USB_ANY_ID,
  1118. },
  1119. };
  1120. /** USB I/O protocol driver */
  1121. struct usb_driver usbio_driver __usb_driver = {
  1122. .ids = efi_usb_ids,
  1123. .id_count = ( sizeof ( efi_usb_ids ) / sizeof ( efi_usb_ids[0] ) ),
  1124. .class = USB_CLASS_ID ( USB_ANY_ID, USB_ANY_ID, USB_ANY_ID ),
  1125. .score = USB_SCORE_FALLBACK,
  1126. .probe = efi_usb_probe,
  1127. .remove = efi_usb_remove,
  1128. };