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 36KB

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