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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261
  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. struct efi_usb_interface *usbintf =
  444. container_of ( usbio, struct efi_usb_interface, usbio );
  445. struct efi_usb_device *usbdev = usbintf->usbdev;
  446. unsigned int request = ( packet->RequestType |
  447. USB_REQUEST_TYPE ( packet->Request ) );
  448. unsigned int value = le16_to_cpu ( packet->Value );
  449. unsigned int index = le16_to_cpu ( packet->Index );
  450. int rc;
  451. DBGC2 ( usbdev, "USBDEV %s control %04x:%04x:%04x:%04x %s %dms "
  452. "%p+%zx\n", usbintf->name, request, value, index,
  453. le16_to_cpu ( packet->Length ),
  454. efi_usb_direction_name ( direction ), timeout, data,
  455. ( ( size_t ) len ) );
  456. /* Clear status */
  457. *status = 0;
  458. /* Block attempts to change the device configuration, since
  459. * this is logically impossible to do given the constraints of
  460. * the EFI_USB_IO_PROTOCOL design.
  461. */
  462. if ( ( request == USB_SET_CONFIGURATION ) &&
  463. ( value != usbdev->config->config ) ) {
  464. DBGC ( usbdev, "USBDEV %s cannot set configuration %d: not "
  465. "logically possible\n", usbintf->name, index );
  466. rc = -ENOTSUP;
  467. goto err_change_config;
  468. }
  469. /* If we are selecting a new alternate setting then close all
  470. * open endpoints.
  471. */
  472. if ( ( request == USB_SET_INTERFACE ) &&
  473. ( value != usbintf->alternate ) )
  474. efi_usb_close_all ( usbintf );
  475. /* Issue control transfer */
  476. if ( ( rc = usb_control ( usbdev->usb, request, value, index,
  477. data, len ) ) != 0 ) {
  478. DBGC ( usbdev, "USBDEV %s control %04x:%04x:%04x:%04x %p+%zx "
  479. "failed: %s\n", usbintf->name, request, value, index,
  480. le16_to_cpu ( packet->Length ), data, ( ( size_t ) len ),
  481. strerror ( rc ) );
  482. /* Assume that any error represents a stall */
  483. *status = EFI_USB_ERR_STALL;
  484. goto err_control;
  485. }
  486. /* Update alternate setting, if applicable */
  487. if ( request == USB_SET_INTERFACE ) {
  488. usbintf->alternate = value;
  489. DBGC ( usbdev, "USBDEV %s alt %d selected\n",
  490. usbintf->name, usbintf->alternate );
  491. }
  492. err_control:
  493. err_change_config:
  494. return EFIRC ( rc );
  495. }
  496. /**
  497. * Perform bulk transfer
  498. *
  499. * @v usbio USB I/O protocol
  500. * @v endpoint Endpoint address
  501. * @v data Data buffer
  502. * @v len Length of data
  503. * @v timeout Timeout (in milliseconds)
  504. * @ret status Transfer status
  505. * @ret efirc EFI status code
  506. */
  507. static EFI_STATUS EFIAPI
  508. efi_usb_bulk_transfer ( EFI_USB_IO_PROTOCOL *usbio, UINT8 endpoint, VOID *data,
  509. UINTN *len, UINTN timeout, UINT32 *status ) {
  510. struct efi_usb_interface *usbintf =
  511. container_of ( usbio, struct efi_usb_interface, usbio );
  512. struct efi_usb_device *usbdev = usbintf->usbdev;
  513. size_t actual = *len;
  514. int rc;
  515. DBGC2 ( usbdev, "USBDEV %s bulk %s %p+%zx %dms\n", usbintf->name,
  516. ( ( endpoint & USB_ENDPOINT_IN ) ? "IN" : "OUT" ), data,
  517. ( ( size_t ) *len ), ( ( unsigned int ) timeout ) );
  518. /* Clear status */
  519. *status = 0;
  520. /* Perform synchronous transfer */
  521. if ( ( rc = efi_usb_sync_transfer ( usbintf, endpoint,
  522. USB_ENDPOINT_ATTR_BULK, timeout,
  523. data, &actual ) ) != 0 ) {
  524. /* Assume that any error represents a timeout */
  525. *status = EFI_USB_ERR_TIMEOUT;
  526. return rc;
  527. }
  528. return 0;
  529. }
  530. /**
  531. * Perform synchronous interrupt transfer
  532. *
  533. * @v usbio USB I/O protocol
  534. * @v endpoint Endpoint address
  535. * @v data Data buffer
  536. * @v len Length of data
  537. * @v timeout Timeout (in milliseconds)
  538. * @ret status Transfer status
  539. * @ret efirc EFI status code
  540. */
  541. static EFI_STATUS EFIAPI
  542. efi_usb_sync_interrupt_transfer ( EFI_USB_IO_PROTOCOL *usbio, UINT8 endpoint,
  543. VOID *data, UINTN *len, UINTN timeout,
  544. UINT32 *status ) {
  545. struct efi_usb_interface *usbintf =
  546. container_of ( usbio, struct efi_usb_interface, usbio );
  547. struct efi_usb_device *usbdev = usbintf->usbdev;
  548. size_t actual = *len;
  549. int rc;
  550. DBGC2 ( usbdev, "USBDEV %s sync intr %s %p+%zx %dms\n", usbintf->name,
  551. ( ( endpoint & USB_ENDPOINT_IN ) ? "IN" : "OUT" ), data,
  552. ( ( size_t ) *len ), ( ( unsigned int ) timeout ) );
  553. /* Clear status */
  554. *status = 0;
  555. /* Perform synchronous transfer */
  556. if ( ( rc = efi_usb_sync_transfer ( usbintf, endpoint,
  557. USB_ENDPOINT_ATTR_INTERRUPT,
  558. timeout, data, &actual ) ) != 0 ) {
  559. /* Assume that any error represents a timeout */
  560. *status = EFI_USB_ERR_TIMEOUT;
  561. return rc;
  562. }
  563. return 0;
  564. }
  565. /**
  566. * Perform asynchronous interrupt transfer
  567. *
  568. * @v usbio USB I/O protocol
  569. * @v endpoint Endpoint address
  570. * @v start Start (rather than stop) transfer
  571. * @v interval Polling interval (in milliseconds)
  572. * @v len Data length
  573. * @v callback Callback function
  574. * @v context Context for callback function
  575. * @ret efirc EFI status code
  576. */
  577. static EFI_STATUS EFIAPI
  578. efi_usb_async_interrupt_transfer ( EFI_USB_IO_PROTOCOL *usbio, UINT8 endpoint,
  579. BOOLEAN start, UINTN interval, UINTN len,
  580. EFI_ASYNC_USB_TRANSFER_CALLBACK callback,
  581. VOID *context ) {
  582. struct efi_usb_interface *usbintf =
  583. container_of ( usbio, struct efi_usb_interface, usbio );
  584. struct efi_usb_device *usbdev = usbintf->usbdev;
  585. int rc;
  586. DBGC2 ( usbdev, "USBDEV %s async intr %s len %#zx int %d %p/%p\n",
  587. usbintf->name,
  588. ( ( endpoint & USB_ENDPOINT_IN ) ? "IN" : "OUT" ),
  589. ( ( size_t ) len ), ( ( unsigned int ) interval ),
  590. callback, context );
  591. /* Start/stop transfer as applicable */
  592. if ( start ) {
  593. /* Start new transfer */
  594. if ( ( rc = efi_usb_async_start ( usbintf, endpoint, interval,
  595. len, callback,
  596. context ) ) != 0 )
  597. goto err_start;
  598. } else {
  599. /* Stop transfer */
  600. efi_usb_async_stop ( usbintf, endpoint );
  601. }
  602. return 0;
  603. err_start:
  604. return EFIRC ( rc );
  605. }
  606. /**
  607. * Perform synchronous isochronous transfer
  608. *
  609. * @v usbio USB I/O protocol
  610. * @v endpoint Endpoint address
  611. * @v data Data buffer
  612. * @v len Length of data
  613. * @ret status Transfer status
  614. * @ret efirc EFI status code
  615. */
  616. static EFI_STATUS EFIAPI
  617. efi_usb_isochronous_transfer ( EFI_USB_IO_PROTOCOL *usbio, UINT8 endpoint,
  618. VOID *data, UINTN len, UINT32 *status ) {
  619. struct efi_usb_interface *usbintf =
  620. container_of ( usbio, struct efi_usb_interface, usbio );
  621. struct efi_usb_device *usbdev = usbintf->usbdev;
  622. DBGC2 ( usbdev, "USBDEV %s sync iso %s %p+%zx\n", usbintf->name,
  623. ( ( endpoint & USB_ENDPOINT_IN ) ? "IN" : "OUT" ), data,
  624. ( ( size_t ) len ) );
  625. /* Clear status */
  626. *status = 0;
  627. /* Not supported */
  628. return EFI_UNSUPPORTED;
  629. }
  630. /**
  631. * Perform asynchronous isochronous transfers
  632. *
  633. * @v usbio USB I/O protocol
  634. * @v endpoint Endpoint address
  635. * @v data Data buffer
  636. * @v len Length of data
  637. * @v callback Callback function
  638. * @v context Context for callback function
  639. * @ret status Transfer status
  640. * @ret efirc EFI status code
  641. */
  642. static EFI_STATUS EFIAPI
  643. efi_usb_async_isochronous_transfer ( EFI_USB_IO_PROTOCOL *usbio, UINT8 endpoint,
  644. VOID *data, UINTN len,
  645. EFI_ASYNC_USB_TRANSFER_CALLBACK callback,
  646. VOID *context ) {
  647. struct efi_usb_interface *usbintf =
  648. container_of ( usbio, struct efi_usb_interface, usbio );
  649. struct efi_usb_device *usbdev = usbintf->usbdev;
  650. DBGC2 ( usbdev, "USBDEV %s async iso %s %p+%zx %p/%p\n", usbintf->name,
  651. ( ( endpoint & USB_ENDPOINT_IN ) ? "IN" : "OUT" ), data,
  652. ( ( size_t ) len ), callback, context );
  653. /* Not supported */
  654. return EFI_UNSUPPORTED;
  655. }
  656. /**
  657. * Get device descriptor
  658. *
  659. * @v usbio USB I/O protocol
  660. * @ret efidesc EFI device descriptor
  661. * @ret efirc EFI status code
  662. */
  663. static EFI_STATUS EFIAPI
  664. efi_usb_get_device_descriptor ( EFI_USB_IO_PROTOCOL *usbio,
  665. EFI_USB_DEVICE_DESCRIPTOR *efidesc ) {
  666. struct efi_usb_interface *usbintf =
  667. container_of ( usbio, struct efi_usb_interface, usbio );
  668. struct efi_usb_device *usbdev = usbintf->usbdev;
  669. DBGC2 ( usbdev, "USBDEV %s get device descriptor\n", usbintf->name );
  670. /* Copy cached device descriptor */
  671. memcpy ( efidesc, &usbdev->usb->device, sizeof ( *efidesc ) );
  672. return 0;
  673. }
  674. /**
  675. * Get configuration descriptor
  676. *
  677. * @v usbio USB I/O protocol
  678. * @ret efidesc EFI interface descriptor
  679. * @ret efirc EFI status code
  680. */
  681. static EFI_STATUS EFIAPI
  682. efi_usb_get_config_descriptor ( EFI_USB_IO_PROTOCOL *usbio,
  683. EFI_USB_CONFIG_DESCRIPTOR *efidesc ) {
  684. struct efi_usb_interface *usbintf =
  685. container_of ( usbio, struct efi_usb_interface, usbio );
  686. struct efi_usb_device *usbdev = usbintf->usbdev;
  687. DBGC2 ( usbdev, "USBDEV %s get configuration descriptor\n",
  688. usbintf->name );
  689. /* Copy cached configuration descriptor */
  690. memcpy ( efidesc, usbdev->config, sizeof ( *efidesc ) );
  691. return 0;
  692. }
  693. /**
  694. * Get interface descriptor
  695. *
  696. * @v usbio USB I/O protocol
  697. * @ret efidesc EFI interface descriptor
  698. * @ret efirc EFI status code
  699. */
  700. static EFI_STATUS EFIAPI
  701. efi_usb_get_interface_descriptor ( EFI_USB_IO_PROTOCOL *usbio,
  702. EFI_USB_INTERFACE_DESCRIPTOR *efidesc ) {
  703. struct efi_usb_interface *usbintf =
  704. container_of ( usbio, struct efi_usb_interface, usbio );
  705. struct efi_usb_device *usbdev = usbintf->usbdev;
  706. struct usb_interface_descriptor *desc;
  707. DBGC2 ( usbdev, "USBDEV %s get interface descriptor\n", usbintf->name );
  708. /* Locate cached interface descriptor */
  709. desc = usb_interface_descriptor ( usbdev->config, usbintf->interface,
  710. usbintf->alternate );
  711. if ( ! desc ) {
  712. DBGC ( usbdev, "USBDEV %s alt %d has no interface descriptor\n",
  713. usbintf->name, usbintf->alternate );
  714. return -ENOENT;
  715. }
  716. /* Copy cached interface descriptor */
  717. memcpy ( efidesc, desc, sizeof ( *efidesc ) );
  718. return 0;
  719. }
  720. /**
  721. * Get endpoint descriptor
  722. *
  723. * @v usbio USB I/O protocol
  724. * @v address Endpoint index
  725. * @ret efidesc EFI interface descriptor
  726. * @ret efirc EFI status code
  727. */
  728. static EFI_STATUS EFIAPI
  729. efi_usb_get_endpoint_descriptor ( EFI_USB_IO_PROTOCOL *usbio, UINT8 index,
  730. EFI_USB_ENDPOINT_DESCRIPTOR *efidesc ) {
  731. struct efi_usb_interface *usbintf =
  732. container_of ( usbio, struct efi_usb_interface, usbio );
  733. struct efi_usb_device *usbdev = usbintf->usbdev;
  734. struct usb_interface_descriptor *interface;
  735. struct usb_endpoint_descriptor *desc;
  736. DBGC2 ( usbdev, "USBDEV %s get endpoint %d descriptor\n",
  737. usbintf->name, index );
  738. /* Locate cached interface descriptor */
  739. interface = usb_interface_descriptor ( usbdev->config,
  740. usbintf->interface,
  741. usbintf->alternate );
  742. if ( ! interface ) {
  743. DBGC ( usbdev, "USBDEV %s alt %d has no interface descriptor\n",
  744. usbintf->name, usbintf->alternate );
  745. return -ENOENT;
  746. }
  747. /* Locate and copy cached endpoint descriptor */
  748. for_each_interface_descriptor ( desc, usbdev->config, interface ) {
  749. if ( ( desc->header.type == USB_ENDPOINT_DESCRIPTOR ) &&
  750. ( index-- == 0 ) ) {
  751. memcpy ( efidesc, desc, sizeof ( *efidesc ) );
  752. return 0;
  753. }
  754. }
  755. return -ENOENT;
  756. }
  757. /**
  758. * Get string descriptor
  759. *
  760. * @v usbio USB I/O protocol
  761. * @v language Language ID
  762. * @v index String index
  763. * @ret string String
  764. * @ret efirc EFI status code
  765. */
  766. static EFI_STATUS EFIAPI
  767. efi_usb_get_string_descriptor ( EFI_USB_IO_PROTOCOL *usbio, UINT16 language,
  768. UINT8 index, CHAR16 **string ) {
  769. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  770. struct efi_usb_interface *usbintf =
  771. container_of ( usbio, struct efi_usb_interface, usbio );
  772. struct efi_usb_device *usbdev = usbintf->usbdev;
  773. struct usb_descriptor_header header;
  774. VOID *buffer;
  775. size_t len;
  776. EFI_STATUS efirc;
  777. int rc;
  778. DBGC2 ( usbdev, "USBDEV %s get string %d:%d descriptor\n",
  779. usbintf->name, language, index );
  780. /* Read descriptor header */
  781. if ( ( rc = usb_get_descriptor ( usbdev->usb, 0, USB_STRING_DESCRIPTOR,
  782. index, language, &header,
  783. sizeof ( header ) ) ) != 0 ) {
  784. DBGC ( usbdev, "USBDEV %s could not get string %d:%d "
  785. "descriptor header: %s\n", usbintf->name, language,
  786. index, strerror ( rc ) );
  787. goto err_get_header;
  788. }
  789. len = header.len;
  790. /* Allocate buffer */
  791. if ( ( efirc = bs->AllocatePool ( EfiBootServicesData, len,
  792. &buffer ) ) != 0 ) {
  793. rc = -EEFI ( efirc );
  794. goto err_alloc;
  795. }
  796. /* Read whole descriptor */
  797. if ( ( rc = usb_get_descriptor ( usbdev->usb, 0, USB_STRING_DESCRIPTOR,
  798. index, language, buffer,
  799. len ) ) != 0 ) {
  800. DBGC ( usbdev, "USBDEV %s could not get string %d:%d "
  801. "descriptor: %s\n", usbintf->name, language,
  802. index, strerror ( rc ) );
  803. goto err_get_descriptor;
  804. }
  805. /* Shuffle down and terminate string */
  806. memmove ( buffer, ( buffer + sizeof ( header ) ),
  807. ( len - sizeof ( header ) ) );
  808. memset ( ( buffer + len - sizeof ( header ) ), 0, sizeof ( **string ) );
  809. /* Return allocated string */
  810. *string = buffer;
  811. return 0;
  812. err_get_descriptor:
  813. bs->FreePool ( buffer );
  814. err_alloc:
  815. err_get_header:
  816. return EFIRC ( rc );
  817. }
  818. /**
  819. * Get supported languages
  820. *
  821. * @v usbio USB I/O protocol
  822. * @ret languages Language ID table
  823. * @ret len Length of language ID table
  824. * @ret efirc EFI status code
  825. */
  826. static EFI_STATUS EFIAPI
  827. efi_usb_get_supported_languages ( EFI_USB_IO_PROTOCOL *usbio,
  828. UINT16 **languages, UINT16 *len ) {
  829. struct efi_usb_interface *usbintf =
  830. container_of ( usbio, struct efi_usb_interface, usbio );
  831. struct efi_usb_device *usbdev = usbintf->usbdev;
  832. DBGC2 ( usbdev, "USBDEV %s get supported languages\n", usbintf->name );
  833. /* Return cached supported languages */
  834. *languages = ( ( ( void * ) usbdev->languages ) +
  835. sizeof ( *(usbdev->languages) ) );
  836. *len = usbdev->languages->len;
  837. return 0;
  838. }
  839. /**
  840. * Reset port
  841. *
  842. * @v usbio USB I/O protocol
  843. * @ret efirc EFI status code
  844. */
  845. static EFI_STATUS EFIAPI
  846. efi_usb_port_reset ( EFI_USB_IO_PROTOCOL *usbio ) {
  847. struct efi_usb_interface *usbintf =
  848. container_of ( usbio, struct efi_usb_interface, usbio );
  849. struct efi_usb_device *usbdev = usbintf->usbdev;
  850. DBGC2 ( usbdev, "USBDEV %s reset port\n", usbintf->name );
  851. /* This is logically impossible to do, since resetting the
  852. * port may destroy state belonging to other
  853. * EFI_USB_IO_PROTOCOL instances belonging to the same USB
  854. * device. (This is yet another artifact of the incredibly
  855. * poor design of the EFI_USB_IO_PROTOCOL.)
  856. */
  857. return EFI_INVALID_PARAMETER;
  858. }
  859. /** USB I/O protocol */
  860. static EFI_USB_IO_PROTOCOL efi_usb_io_protocol = {
  861. .UsbControlTransfer = efi_usb_control_transfer,
  862. .UsbBulkTransfer = efi_usb_bulk_transfer,
  863. .UsbAsyncInterruptTransfer = efi_usb_async_interrupt_transfer,
  864. .UsbSyncInterruptTransfer = efi_usb_sync_interrupt_transfer,
  865. .UsbIsochronousTransfer = efi_usb_isochronous_transfer,
  866. .UsbAsyncIsochronousTransfer = efi_usb_async_isochronous_transfer,
  867. .UsbGetDeviceDescriptor = efi_usb_get_device_descriptor,
  868. .UsbGetConfigDescriptor = efi_usb_get_config_descriptor,
  869. .UsbGetInterfaceDescriptor = efi_usb_get_interface_descriptor,
  870. .UsbGetEndpointDescriptor = efi_usb_get_endpoint_descriptor,
  871. .UsbGetStringDescriptor = efi_usb_get_string_descriptor,
  872. .UsbGetSupportedLanguages = efi_usb_get_supported_languages,
  873. .UsbPortReset = efi_usb_port_reset,
  874. };
  875. /******************************************************************************
  876. *
  877. * USB driver
  878. *
  879. ******************************************************************************
  880. */
  881. /**
  882. * Install interface
  883. *
  884. * @v usbdev EFI USB device
  885. * @v interface Interface number
  886. * @ret rc Return status code
  887. */
  888. static int efi_usb_install ( struct efi_usb_device *usbdev,
  889. unsigned int interface ) {
  890. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  891. struct efi_device *efidev = usbdev->efidev;
  892. struct efi_usb_interface *usbintf;
  893. struct usb_device *usb;
  894. EFI_DEVICE_PATH_PROTOCOL *path_end;
  895. USB_DEVICE_PATH *usbpath;
  896. unsigned int path_count;
  897. size_t path_prefix_len;
  898. size_t path_len;
  899. EFI_STATUS efirc;
  900. int rc;
  901. /* Calculate device path length */
  902. path_count = ( usb_depth ( usbdev->usb ) + 1 );
  903. path_prefix_len = efi_devpath_len ( efidev->path );
  904. path_len = ( path_prefix_len + ( path_count * sizeof ( *usbpath ) ) +
  905. sizeof ( *path_end ) );
  906. /* Allocate and initialise structure */
  907. usbintf = zalloc ( sizeof ( *usbintf ) + path_len );
  908. if ( ! usbintf ) {
  909. rc = -ENOMEM;
  910. goto err_alloc;
  911. }
  912. snprintf ( usbintf->name, sizeof ( usbintf->name ), "%s[%d]",
  913. usbdev->name, interface );
  914. usbintf->usbdev = usbdev;
  915. usbintf->interface = interface;
  916. memcpy ( &usbintf->usbio, &efi_usb_io_protocol,
  917. sizeof ( usbintf->usbio ) );
  918. usbintf->path = ( ( ( void * ) usbintf ) + sizeof ( *usbintf ) );
  919. /* Construct device path */
  920. memcpy ( usbintf->path, efidev->path, path_prefix_len );
  921. path_end = ( ( ( void * ) usbintf->path ) + path_len -
  922. sizeof ( *path_end ) );
  923. path_end->Type = END_DEVICE_PATH_TYPE;
  924. path_end->SubType = END_ENTIRE_DEVICE_PATH_SUBTYPE;
  925. path_end->Length[0] = sizeof ( *path_end );
  926. usbpath = ( ( ( void * ) path_end ) - sizeof ( *usbpath ) );
  927. usbpath->InterfaceNumber = interface;
  928. for ( usb = usbdev->usb ; usb ; usbpath--, usb = usb->port->hub->usb ) {
  929. usbpath->Header.Type = MESSAGING_DEVICE_PATH;
  930. usbpath->Header.SubType = MSG_USB_DP;
  931. usbpath->Header.Length[0] = sizeof ( *usbpath );
  932. usbpath->ParentPortNumber = usb->port->address;
  933. }
  934. /* Add to list of interfaces */
  935. list_add_tail ( &usbintf->list, &usbdev->interfaces );
  936. /* Install protocols */
  937. if ( ( efirc = bs->InstallMultipleProtocolInterfaces (
  938. &usbintf->handle,
  939. &efi_usb_io_protocol_guid, &usbintf->usbio,
  940. &efi_device_path_protocol_guid, usbintf->path,
  941. NULL ) ) != 0 ) {
  942. rc = -EEFI ( efirc );
  943. DBGC ( usbdev, "USBDEV %s could not install protocols: %s\n",
  944. usbintf->name, strerror ( rc ) );
  945. goto err_install_protocol;
  946. }
  947. DBGC ( usbdev, "USBDEV %s installed as %s\n",
  948. usbintf->name, efi_handle_name ( usbintf->handle ) );
  949. return 0;
  950. efi_usb_close_all ( usbintf );
  951. bs->UninstallMultipleProtocolInterfaces (
  952. usbintf->handle,
  953. &efi_usb_io_protocol_guid, &usbintf->usbio,
  954. &efi_device_path_protocol_guid, usbintf->path,
  955. NULL );
  956. err_install_protocol:
  957. list_del ( &usbintf->list );
  958. free ( usbintf );
  959. err_alloc:
  960. return rc;
  961. }
  962. /**
  963. * Uninstall interface
  964. *
  965. * @v usbintf EFI USB interface
  966. */
  967. static void efi_usb_uninstall ( struct efi_usb_interface *usbintf ) {
  968. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  969. /* Close all endpoints */
  970. efi_usb_close_all ( usbintf );
  971. /* Uninstall protocols */
  972. bs->UninstallMultipleProtocolInterfaces (
  973. usbintf->handle,
  974. &efi_usb_io_protocol_guid, &usbintf->usbio,
  975. &efi_device_path_protocol_guid, usbintf->path,
  976. NULL );
  977. /* Remove from list of interfaces */
  978. list_del ( &usbintf->list );
  979. /* Free interface */
  980. free ( usbintf );
  981. }
  982. /**
  983. * Uninstall all interfaces
  984. *
  985. * @v usbdev EFI USB device
  986. */
  987. static void efi_usb_uninstall_all ( struct efi_usb_device *efiusb ) {
  988. struct efi_usb_interface *usbintf;
  989. /* Uninstall all interfaces */
  990. while ( ( usbintf = list_first_entry ( &efiusb->interfaces,
  991. struct efi_usb_interface,
  992. list ) ) ) {
  993. efi_usb_uninstall ( usbintf );
  994. }
  995. }
  996. /**
  997. * Probe device
  998. *
  999. * @v func USB function
  1000. * @v config Configuration descriptor
  1001. * @ret rc Return status code
  1002. */
  1003. static int efi_usb_probe ( struct usb_function *func,
  1004. struct usb_configuration_descriptor *config ) {
  1005. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  1006. struct usb_device *usb = func->usb;
  1007. struct efi_usb_device *usbdev;
  1008. struct efi_usb_interface *usbintf;
  1009. struct efi_device *efidev;
  1010. struct usb_descriptor_header header;
  1011. size_t config_len;
  1012. unsigned int i;
  1013. int rc;
  1014. /* Find parent EFI device */
  1015. efidev = efidev_parent ( &func->dev );
  1016. if ( ! efidev ) {
  1017. rc = -ENOTTY;
  1018. goto err_no_efidev;
  1019. }
  1020. /* Get configuration length */
  1021. config_len = le16_to_cpu ( config->len );
  1022. /* Get supported languages descriptor header */
  1023. if ( ( rc = usb_get_descriptor ( usb, 0, USB_STRING_DESCRIPTOR, 0, 0,
  1024. &header, sizeof ( header ) ) ) != 0 ) {
  1025. /* Assume no strings are present */
  1026. header.len = 0;
  1027. }
  1028. /* Allocate and initialise structure */
  1029. usbdev = zalloc ( sizeof ( *usbdev ) + config_len + header.len );
  1030. if ( ! usbdev ) {
  1031. rc = -ENOMEM;
  1032. goto err_alloc;
  1033. }
  1034. usb_func_set_drvdata ( func, usbdev );
  1035. usbdev->name = func->name;
  1036. usbdev->usb = usb;
  1037. usbdev->efidev = efidev;
  1038. usbdev->config = ( ( ( void * ) usbdev ) + sizeof ( *usbdev ) );
  1039. memcpy ( usbdev->config, config, config_len );
  1040. usbdev->languages = ( ( ( void * ) usbdev->config ) + config_len );
  1041. INIT_LIST_HEAD ( &usbdev->interfaces );
  1042. /* Get supported languages descriptor */
  1043. if ( header.len &&
  1044. ( rc = usb_get_descriptor ( usb, 0, USB_STRING_DESCRIPTOR, 0, 0,
  1045. usbdev->languages,
  1046. header.len ) ) != 0 ) {
  1047. DBGC ( usbdev, "USBDEV %s could not get supported languages: "
  1048. "%s\n", usbdev->name, strerror ( rc ) );
  1049. goto err_get_languages;
  1050. }
  1051. /* Install interfaces */
  1052. for ( i = 0 ; i < func->desc.count ; i++ ) {
  1053. if ( ( rc = efi_usb_install ( usbdev,
  1054. func->interface[i] ) ) != 0 )
  1055. goto err_install;
  1056. }
  1057. /* Connect any external drivers */
  1058. list_for_each_entry ( usbintf, &usbdev->interfaces, list )
  1059. bs->ConnectController ( usbintf->handle, NULL, NULL, TRUE );
  1060. return 0;
  1061. err_install:
  1062. efi_usb_uninstall_all ( usbdev );
  1063. assert ( list_empty ( &usbdev->interfaces ) );
  1064. err_get_languages:
  1065. free ( usbdev );
  1066. err_alloc:
  1067. err_no_efidev:
  1068. return rc;
  1069. }
  1070. /**
  1071. * Remove device
  1072. *
  1073. * @v func USB function
  1074. */
  1075. static void efi_usb_remove ( struct usb_function *func ) {
  1076. struct efi_usb_device *usbdev = usb_func_get_drvdata ( func );
  1077. /* Uninstall all interfaces */
  1078. efi_usb_uninstall_all ( usbdev );
  1079. assert ( list_empty ( &usbdev->interfaces ) );
  1080. /* Free device */
  1081. free ( usbdev );
  1082. }
  1083. /** USB I/O protocol device IDs */
  1084. static struct usb_device_id efi_usb_ids[] = {
  1085. {
  1086. .name = "usbio",
  1087. .vendor = USB_ANY_ID,
  1088. .product = USB_ANY_ID,
  1089. },
  1090. };
  1091. /** USB I/O protocol driver */
  1092. struct usb_driver usbio_driver __usb_driver = {
  1093. .ids = efi_usb_ids,
  1094. .id_count = ( sizeof ( efi_usb_ids ) / sizeof ( efi_usb_ids[0] ) ),
  1095. .class = USB_CLASS_ID ( USB_ANY_ID, USB_ANY_ID, USB_ANY_ID ),
  1096. .score = USB_SCORE_FALLBACK,
  1097. .probe = efi_usb_probe,
  1098. .remove = efi_usb_remove,
  1099. };