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.

usbio.c 45KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729
  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 <string.h>
  26. #include <errno.h>
  27. #include <assert.h>
  28. #include <ipxe/efi/efi.h>
  29. #include <ipxe/efi/efi_driver.h>
  30. #include <ipxe/efi/efi_utils.h>
  31. #include <ipxe/efi/Protocol/UsbIo.h>
  32. #include <ipxe/usb.h>
  33. #include "usbio.h"
  34. /** @file
  35. *
  36. * EFI_USB_IO_PROTOCOL pseudo Host Controller Interface driver
  37. *
  38. *
  39. * The EFI_USB_IO_PROTOCOL is an almost unbelievably poorly designed
  40. * abstraction of a USB device. It would be just about forgivable for
  41. * an API to support only synchronous operation for bulk OUT
  42. * endpoints. It is imbecilic to support only synchronous operation
  43. * for bulk IN endpoints. This apparently intern-designed API
  44. * throttles a typical NIC down to 1.5% of its maximum throughput.
  45. * That isn't a typo. It really is that slow.
  46. *
  47. * We can't even work around this stupidity by talking to the host
  48. * controller abstraction directly, because an identical limitation
  49. * exists in the EFI_USB2_HC_PROTOCOL.
  50. *
  51. * Unless you derive therapeutic value from watching download progress
  52. * indicators lethargically creep through every single integer from 0
  53. * to 100, you should use iPXE's native USB host controller drivers
  54. * instead. (Or just upgrade from UEFI to "legacy" BIOS, which will
  55. * produce a similar speed increase.)
  56. *
  57. *
  58. * For added excitement, the EFI_USB_IO_PROTOCOL makes the
  59. * (demonstrably incorrect) assumption that a USB driver needs to
  60. * attach to exactly one interface within a USB device, and provides a
  61. * helper method to retrieve "the" interface descriptor. Since pretty
  62. * much every USB network device requires binding to a pair of
  63. * control+data interfaces, this aspect of EFI_USB_IO_PROTOCOL is of
  64. * no use to us.
  65. *
  66. * We have our own existing code for reading USB descriptors, so we
  67. * don't actually care that the UsbGetInterfaceDescriptor() method
  68. * provided by EFI_USB_IO_PROTOCOL is useless for network devices. We
  69. * can read the descriptors ourselves (via UsbControlTransfer()) and
  70. * get all of the information we need this way. We can even work
  71. * around the fact that EFI_USB_IO_PROTOCOL provides separate handles
  72. * for each of the two interfaces comprising our network device.
  73. *
  74. * However, if we discover that we need to select an alternative
  75. * device configuration (e.g. for devices exposing both RNDIS and
  76. * ECM), then all hell breaks loose. EFI_USB_IO_PROTOCOL starts to
  77. * panic because its cached interface and endpoint descriptors will no
  78. * longer be valid. As mentioned above, the cached descriptors are
  79. * useless for network devices anyway so we _really_ don't care about
  80. * this, but EFI_USB_IO_PROTOCOL certainly cares. It prints out a
  81. * manic warning message containing no fewer than six exclamation
  82. * marks and then literally commits seppuku in the middle of the
  83. * UsbControlTransfer() method by attempting to uninstall itself.
  84. * Quite how the caller is supposed to react when asked to stop using
  85. * the EFI_USB_IO_PROTOCOL instance while in the middle of an
  86. * uninterruptible call to said instance is left as an exercise for
  87. * the interested reader.
  88. *
  89. * There is no sensible way to work around this, so we just
  90. * preemptively fail if asked to change the device configuration, on
  91. * the basis that reporting a sarcastic error message is often
  92. * preferable to jumping through a NULL pointer and crashing the
  93. * system.
  94. */
  95. /* Disambiguate the various error causes */
  96. #define ENOTSUP_MORONIC_SPECIFICATION \
  97. __einfo_error ( EINFO_ENOTSUP_MORONIC_SPECIFICATION )
  98. #define EINFO_ENOTSUP_MORONIC_SPECIFICATION \
  99. __einfo_uniqify ( EINFO_ENOTSUP, 0x01, \
  100. "EFI_USB_IO_PROTOCOL was designed by morons" )
  101. /******************************************************************************
  102. *
  103. * Device model
  104. *
  105. ******************************************************************************
  106. */
  107. /**
  108. * Determine endpoint interface number
  109. *
  110. * @v usbio USB I/O device
  111. * @v ep USB Endpoint
  112. * @ret interface Interface number, or negative error
  113. */
  114. static int usbio_interface ( struct usbio_device *usbio,
  115. struct usb_endpoint *ep ) {
  116. EFI_HANDLE handle = usbio->handle;
  117. struct usb_device *usb = ep->usb;
  118. struct usb_configuration_descriptor *config;
  119. struct usb_interface_descriptor *interface;
  120. struct usb_endpoint_descriptor *endpoint;
  121. struct usb_function *func;
  122. unsigned int i;
  123. /* The control endpoint is not part of a described interface */
  124. if ( ep->address == USB_EP0_ADDRESS )
  125. return 0;
  126. /* Iterate over all interface descriptors looking for a match */
  127. config = usbio->config;
  128. for_each_config_descriptor ( interface, config ) {
  129. /* Skip non-interface descriptors */
  130. if ( interface->header.type != USB_INTERFACE_DESCRIPTOR )
  131. continue;
  132. /* Iterate over all endpoint descriptors looking for a match */
  133. for_each_interface_descriptor ( endpoint, config, interface ) {
  134. /* Skip non-endpoint descriptors */
  135. if ( endpoint->header.type != USB_ENDPOINT_DESCRIPTOR )
  136. continue;
  137. /* Check endpoint address */
  138. if ( endpoint->endpoint != ep->address )
  139. continue;
  140. /* Check interface belongs to this function */
  141. list_for_each_entry ( func, &usb->functions, list ) {
  142. /* Skip non-matching functions */
  143. if ( func->interface[0] != usbio->first )
  144. continue;
  145. /* Iterate over all interfaces for a match */
  146. for ( i = 0 ; i < func->desc.count ; i++ ) {
  147. if ( interface->interface ==
  148. func->interface[i] )
  149. return interface->interface;
  150. }
  151. }
  152. }
  153. }
  154. DBGC ( usbio, "USBIO %s cannot find interface for %s",
  155. efi_handle_name ( handle ), usb_endpoint_name ( ep ) );
  156. return -ENOENT;
  157. }
  158. /**
  159. * Open USB I/O interface
  160. *
  161. * @v usbio USB I/O device
  162. * @v interface Interface number
  163. * @ret rc Return status code
  164. */
  165. static int usbio_open ( struct usbio_device *usbio, unsigned int interface ) {
  166. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  167. EFI_HANDLE handle = usbio->handle;
  168. struct usbio_interface *intf = &usbio->interface[interface];
  169. EFI_DEVICE_PATH_PROTOCOL *path;
  170. EFI_DEVICE_PATH_PROTOCOL *end;
  171. USB_DEVICE_PATH *usbpath;
  172. union {
  173. void *interface;
  174. EFI_USB_IO_PROTOCOL *io;
  175. } u;
  176. EFI_STATUS efirc;
  177. int rc;
  178. /* Sanity check */
  179. assert ( interface < usbio->config->interfaces );
  180. /* If interface is already open, just increment the usage count */
  181. if ( intf->count ) {
  182. intf->count++;
  183. return 0;
  184. }
  185. /* Construct device path for this interface */
  186. path = usbio->path;
  187. usbpath = usbio->usbpath;
  188. usbpath->InterfaceNumber = interface;
  189. end = efi_devpath_end ( path );
  190. /* Locate handle for this endpoint's interface */
  191. if ( ( efirc = bs->LocateDevicePath ( &efi_usb_io_protocol_guid, &path,
  192. &intf->handle ) ) != 0 ) {
  193. rc = -EEFI ( efirc );
  194. DBGC ( usbio, "USBIO %s could not locate ",
  195. efi_handle_name ( handle ) );
  196. DBGC ( usbio, "%s: %s\n",
  197. efi_devpath_text ( usbio->path ), strerror ( rc ) );
  198. return rc;
  199. }
  200. /* Check that expected path was located */
  201. if ( path != end ) {
  202. DBGC ( usbio, "USBIO %s located incomplete ",
  203. efi_handle_name ( handle ) );
  204. DBGC ( usbio, "%s\n", efi_handle_name ( intf->handle ) );
  205. return -EXDEV;
  206. }
  207. /* Open USB I/O protocol on this handle */
  208. if ( ( efirc = bs->OpenProtocol ( intf->handle,
  209. &efi_usb_io_protocol_guid,
  210. &u.interface, efi_image_handle,
  211. intf->handle,
  212. ( EFI_OPEN_PROTOCOL_BY_DRIVER |
  213. EFI_OPEN_PROTOCOL_EXCLUSIVE )))!=0){
  214. rc = -EEFI ( efirc );
  215. DBGC ( usbio, "USBIO %s cannot open ",
  216. efi_handle_name ( handle ) );
  217. DBGC ( usbio, "%s: %s\n",
  218. efi_handle_name ( intf->handle ), strerror ( rc ) );
  219. DBGC_EFI_OPENERS ( usbio, intf->handle,
  220. &efi_usb_io_protocol_guid );
  221. return rc;
  222. }
  223. intf->io = u.io;
  224. /* Increment usage count */
  225. intf->count++;
  226. return 0;
  227. }
  228. /**
  229. * Close USB I/O interface
  230. *
  231. * @v usbio USB I/O device
  232. * @v interface Interface number
  233. */
  234. static void usbio_close ( struct usbio_device *usbio, unsigned int interface ) {
  235. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  236. struct usbio_interface *intf = &usbio->interface[interface];
  237. /* Sanity checks */
  238. assert ( interface < usbio->config->interfaces );
  239. assert ( intf->count > 0 );
  240. /* Decrement usage count */
  241. intf->count--;
  242. /* Do nothing if interface is still in use */
  243. if ( intf->count )
  244. return;
  245. /* Close USB I/O protocol */
  246. bs->CloseProtocol ( intf->handle, &efi_usb_io_protocol_guid,
  247. efi_image_handle, intf->handle );
  248. }
  249. /******************************************************************************
  250. *
  251. * Control endpoints
  252. *
  253. ******************************************************************************
  254. */
  255. /**
  256. * Open control endpoint
  257. *
  258. * @v endpoint Endpoint
  259. * @ret rc Return status code
  260. */
  261. static int usbio_control_open ( struct usbio_endpoint *endpoint __unused ) {
  262. /* Nothing to do */
  263. return 0;
  264. }
  265. /**
  266. * Close control endpoint
  267. *
  268. * @v endpoint Endpoint
  269. */
  270. static void usbio_control_close ( struct usbio_endpoint *endpoint __unused ) {
  271. /* Nothing to do */
  272. }
  273. /**
  274. * Poll control endpoint
  275. *
  276. * @v endpoint Endpoint
  277. */
  278. static void usbio_control_poll ( struct usbio_endpoint *endpoint ) {
  279. struct usbio_device *usbio = endpoint->usbio;
  280. struct usb_endpoint *ep = endpoint->ep;
  281. EFI_HANDLE handle = usbio->handle;
  282. EFI_USB_IO_PROTOCOL *io;
  283. union {
  284. struct usb_setup_packet setup;
  285. EFI_USB_DEVICE_REQUEST efi;
  286. } *msg;
  287. EFI_USB_DATA_DIRECTION direction;
  288. struct io_buffer *iobuf;
  289. unsigned int index;
  290. unsigned int flags;
  291. unsigned int recipient;
  292. unsigned int interface;
  293. uint16_t request;
  294. void *data;
  295. size_t len;
  296. UINT32 status;
  297. EFI_STATUS efirc;
  298. int rc;
  299. /* Do nothing if ring is empty */
  300. if ( endpoint->cons == endpoint->prod )
  301. return;
  302. /* Consume next transfer */
  303. index = ( endpoint->cons++ % USBIO_RING_COUNT );
  304. iobuf = endpoint->iobuf[index];
  305. flags = endpoint->flags[index];
  306. /* Sanity check */
  307. if ( ! ( flags & USBIO_MESSAGE ) ) {
  308. DBGC ( usbio, "USBIO %s %s non-message transfer\n",
  309. efi_handle_name ( handle ), usb_endpoint_name ( ep ) );
  310. rc = -ENOTSUP;
  311. goto err_not_message;
  312. }
  313. /* Construct transfer */
  314. msg = iob_push ( iobuf, sizeof ( *msg ) );
  315. iob_pull ( iobuf, sizeof ( *msg ) );
  316. request = le16_to_cpu ( msg->setup.request );
  317. len = iob_len ( iobuf );
  318. if ( len ) {
  319. data = iobuf->data;
  320. direction = ( ( request & USB_DIR_IN ) ?
  321. EfiUsbDataIn : EfiUsbDataOut );
  322. } else {
  323. data = NULL;
  324. direction = EfiUsbNoData;
  325. }
  326. /* Determine interface for this transfer */
  327. recipient = ( request & USB_RECIP_MASK );
  328. if ( recipient == USB_RECIP_INTERFACE ) {
  329. /* Recipient is an interface: use interface number directly */
  330. interface = le16_to_cpu ( msg->setup.index );
  331. } else {
  332. /* Route all other requests through the first interface */
  333. interface = 0;
  334. }
  335. /* Open interface */
  336. if ( ( rc = usbio_open ( usbio, interface ) ) != 0 )
  337. goto err_open;
  338. io = usbio->interface[interface].io;
  339. /* Due to the design of EFI_USB_IO_PROTOCOL, attempting to set
  340. * the configuration to a non-default value is basically a
  341. * self-destruct button.
  342. */
  343. if ( ( request == USB_SET_CONFIGURATION ) &&
  344. ( le16_to_cpu ( msg->setup.value ) != usbio->config->config ) ) {
  345. rc = -ENOTSUP_MORONIC_SPECIFICATION;
  346. DBGC ( usbio, "USBIO %s cannot change configuration: %s\n",
  347. efi_handle_name ( handle ), strerror ( rc ) );
  348. goto err_moronic_specification;
  349. }
  350. /* Submit transfer */
  351. if ( ( efirc = io->UsbControlTransfer ( io, &msg->efi, direction, 0,
  352. data, len, &status ) ) != 0 ) {
  353. rc = -EEFI ( efirc );
  354. DBGC ( usbio, "USBIO %s %s could not submit control transfer ",
  355. efi_handle_name ( handle ), usb_endpoint_name ( ep ) );
  356. DBGC ( usbio, "via %s: %s (status %04x)\n",
  357. efi_handle_name ( usbio->interface[interface].handle ),
  358. strerror ( rc ), status );
  359. goto err_transfer;
  360. }
  361. /* Close interface */
  362. usbio_close ( usbio, interface );
  363. /* Complete transfer */
  364. usb_complete ( ep, iobuf );
  365. return;
  366. err_transfer:
  367. err_moronic_specification:
  368. usbio_close ( usbio, interface );
  369. err_open:
  370. err_not_message:
  371. usb_complete_err ( ep, iobuf, rc );
  372. }
  373. /** Control endpoint operations */
  374. static struct usbio_operations usbio_control_operations = {
  375. .open = usbio_control_open,
  376. .close = usbio_control_close,
  377. .poll = usbio_control_poll,
  378. };
  379. /******************************************************************************
  380. *
  381. * Bulk IN endpoints
  382. *
  383. ******************************************************************************
  384. */
  385. /**
  386. * Open bulk IN endpoint
  387. *
  388. * @v endpoint Endpoint
  389. * @ret rc Return status code
  390. */
  391. static int usbio_bulk_in_open ( struct usbio_endpoint *endpoint __unused ) {
  392. /* Nothing to do */
  393. return 0;
  394. }
  395. /**
  396. * Close bulk IN endpoint
  397. *
  398. * @v endpoint Endpoint
  399. */
  400. static void usbio_bulk_in_close ( struct usbio_endpoint *endpoint __unused ) {
  401. /* Nothing to do */
  402. }
  403. /**
  404. * Poll bulk IN endpoint
  405. *
  406. * @v endpoint Endpoint
  407. */
  408. static void usbio_bulk_in_poll ( struct usbio_endpoint *endpoint ) {
  409. struct usbio_device *usbio = endpoint->usbio;
  410. struct usb_endpoint *ep = endpoint->ep;
  411. EFI_USB_IO_PROTOCOL *io = endpoint->io;
  412. EFI_HANDLE handle = usbio->handle;
  413. struct io_buffer *iobuf;
  414. unsigned int index;
  415. UINTN len;
  416. UINT32 status;
  417. EFI_STATUS efirc;
  418. int rc;
  419. /* Do nothing if ring is empty */
  420. if ( endpoint->cons == endpoint->prod )
  421. return;
  422. /* Attempt (but do not yet consume) next transfer */
  423. index = ( endpoint->cons % USBIO_RING_COUNT );
  424. iobuf = endpoint->iobuf[index];
  425. /* Construct transfer */
  426. len = iob_len ( iobuf );
  427. /* Upon being turned on, the EFI_USB_IO_PROTOCOL did nothing
  428. * for several minutes before firing a small ARP packet a few
  429. * millimetres into the ether.
  430. */
  431. efirc = io->UsbBulkTransfer ( io, ep->address, iobuf->data,
  432. &len, 1, &status );
  433. if ( efirc == EFI_TIMEOUT )
  434. return;
  435. /* Consume transfer */
  436. endpoint->cons++;
  437. /* Check for failure */
  438. if ( efirc != 0 ) {
  439. rc = -EEFI ( efirc );
  440. DBGC2 ( usbio, "USBIO %s %s could not submit bulk IN transfer: "
  441. "%s (status %04x)\n", efi_handle_name ( handle ),
  442. usb_endpoint_name ( ep ), strerror ( rc ), status );
  443. usb_complete_err ( ep, iobuf, rc );
  444. return;
  445. }
  446. /* Update length */
  447. iob_put ( iobuf, ( len - iob_len ( iobuf ) ) );
  448. /* Complete transfer */
  449. usb_complete ( ep, iobuf );
  450. }
  451. /** Bulk endpoint operations */
  452. static struct usbio_operations usbio_bulk_in_operations = {
  453. .open = usbio_bulk_in_open,
  454. .close = usbio_bulk_in_close,
  455. .poll = usbio_bulk_in_poll,
  456. };
  457. /******************************************************************************
  458. *
  459. * Bulk OUT endpoints
  460. *
  461. ******************************************************************************
  462. */
  463. /**
  464. * Open bulk OUT endpoint
  465. *
  466. * @v endpoint Endpoint
  467. * @ret rc Return status code
  468. */
  469. static int usbio_bulk_out_open ( struct usbio_endpoint *endpoint __unused ) {
  470. /* Nothing to do */
  471. return 0;
  472. }
  473. /**
  474. * Close bulk OUT endpoint
  475. *
  476. * @v endpoint Endpoint
  477. */
  478. static void usbio_bulk_out_close ( struct usbio_endpoint *endpoint __unused ) {
  479. /* Nothing to do */
  480. }
  481. /**
  482. * Poll bulk OUT endpoint
  483. *
  484. * @v endpoint Endpoint
  485. */
  486. static void usbio_bulk_out_poll ( struct usbio_endpoint *endpoint ) {
  487. struct usbio_device *usbio = endpoint->usbio;
  488. struct usb_endpoint *ep = endpoint->ep;
  489. EFI_USB_IO_PROTOCOL *io = endpoint->io;
  490. EFI_HANDLE handle = usbio->handle;
  491. struct io_buffer *iobuf;
  492. unsigned int index;
  493. unsigned int flags;
  494. UINTN len;
  495. UINT32 status;
  496. EFI_STATUS efirc;
  497. int rc;
  498. /* Do nothing if ring is empty */
  499. if ( endpoint->cons == endpoint->prod )
  500. return;
  501. /* Consume next transfer */
  502. index = ( endpoint->cons++ % USBIO_RING_COUNT );
  503. iobuf = endpoint->iobuf[index];
  504. flags = endpoint->flags[index];
  505. /* Construct transfer */
  506. len = iob_len ( iobuf );
  507. /* Submit transfer */
  508. if ( ( efirc = io->UsbBulkTransfer ( io, ep->address, iobuf->data,
  509. &len, 0, &status ) ) != 0 ) {
  510. rc = -EEFI ( efirc );
  511. DBGC ( usbio, "USBIO %s %s could not submit bulk OUT transfer: "
  512. "%s (status %04x)\n", efi_handle_name ( handle ),
  513. usb_endpoint_name ( ep ), strerror ( rc ), status );
  514. goto err;
  515. }
  516. /* Update length */
  517. iob_put ( iobuf, ( len - iob_len ( iobuf ) ) );
  518. /* Submit zero-length transfer if required */
  519. len = 0;
  520. if ( ( flags & USBIO_ZLEN ) &&
  521. ( efirc = io->UsbBulkTransfer ( io, ep->address, NULL, &len, 0,
  522. &status ) ) != 0 ) {
  523. rc = -EEFI ( efirc );
  524. DBGC ( usbio, "USBIO %s %s could not submit zero-length "
  525. "transfer: %s (status %04x)\n",
  526. efi_handle_name ( handle ), usb_endpoint_name ( ep ),
  527. strerror ( rc ), status );
  528. goto err;
  529. }
  530. /* Complete transfer */
  531. usb_complete ( ep, iobuf );
  532. return;
  533. err:
  534. usb_complete_err ( ep, iobuf, rc );
  535. }
  536. /** Bulk endpoint operations */
  537. static struct usbio_operations usbio_bulk_out_operations = {
  538. .open = usbio_bulk_out_open,
  539. .close = usbio_bulk_out_close,
  540. .poll = usbio_bulk_out_poll,
  541. };
  542. /******************************************************************************
  543. *
  544. * Interrupt endpoints
  545. *
  546. ******************************************************************************
  547. *
  548. * The EFI_USB_IO_PROTOCOL provides two ways to interact with
  549. * interrupt endpoints, neither of which naturally model the hardware
  550. * interaction. The UsbSyncInterruptTransfer() method allows imposes
  551. * a 1ms overhead for every interrupt transfer (which could result in
  552. * up to a 50% decrease in overall throughput for the device). The
  553. * UsbAsyncInterruptTransfer() method provides no way for us to
  554. * prevent transfers when no I/O buffers are available.
  555. *
  556. * We work around this design by utilising a small, fixed ring buffer
  557. * into which the interrupt callback delivers the data. This aims to
  558. * provide buffer space even if no I/O buffers have yet been enqueued.
  559. * The scheme is not guaranteed since the fixed ring buffer may also
  560. * become full. However:
  561. *
  562. * - devices which send a constant stream of interrupts (and which
  563. * therefore might exhaust the fixed ring buffer) tend to be
  564. * responding to every interrupt request, and can tolerate lost
  565. * packets, and
  566. *
  567. * - devices which cannot tolerate lost interrupt packets tend to send
  568. * only a few small messages.
  569. *
  570. * The scheme should therefore work in practice.
  571. */
  572. /**
  573. * Interrupt endpoint callback
  574. *
  575. * @v data Received data
  576. * @v len Length of received data
  577. * @v context Callback context
  578. * @v status Transfer status
  579. * @ret efirc EFI status code
  580. */
  581. static EFI_STATUS EFIAPI usbio_interrupt_callback ( VOID *data, UINTN len,
  582. VOID *context,
  583. UINT32 status ) {
  584. struct usbio_interrupt_ring *intr = context;
  585. struct usbio_endpoint *endpoint = intr->endpoint;
  586. struct usbio_device *usbio = endpoint->usbio;
  587. struct usb_endpoint *ep = endpoint->ep;
  588. EFI_HANDLE handle = usbio->handle;
  589. unsigned int fill;
  590. unsigned int index;
  591. /* Sanity check */
  592. assert ( len <= ep->mtu );
  593. /* Do nothing if ring is empty */
  594. fill = ( intr->prod - intr->cons );
  595. if ( fill >= USBIO_INTR_COUNT ) {
  596. DBGC ( usbio, "USBIO %s %s dropped interrupt completion\n",
  597. efi_handle_name ( handle ), usb_endpoint_name ( ep ) );
  598. return 0;
  599. }
  600. /* Do nothing if transfer was unsuccessful */
  601. if ( status != 0 ) {
  602. DBGC ( usbio, "USBIO %s %s interrupt completion status %04x\n",
  603. efi_handle_name ( handle ), usb_endpoint_name ( ep ),
  604. status );
  605. return 0; /* Unclear what failure actually means here */
  606. }
  607. /* Copy data to buffer and increment producer counter */
  608. index = ( intr->prod % USBIO_INTR_COUNT );
  609. memcpy ( intr->data[index], data, len );
  610. intr->len[index] = len;
  611. intr->prod++;
  612. return 0;
  613. }
  614. /**
  615. * Open interrupt endpoint
  616. *
  617. * @v endpoint Endpoint
  618. * @ret rc Return status code
  619. */
  620. static int usbio_interrupt_open ( struct usbio_endpoint *endpoint ) {
  621. struct usbio_device *usbio = endpoint->usbio;
  622. struct usbio_interrupt_ring *intr;
  623. struct usb_endpoint *ep = endpoint->ep;
  624. EFI_USB_IO_PROTOCOL *io = endpoint->io;
  625. EFI_HANDLE handle = usbio->handle;
  626. unsigned int interval;
  627. unsigned int i;
  628. void *data;
  629. EFI_STATUS efirc;
  630. int rc;
  631. /* Allocate interrupt ring buffer */
  632. intr = zalloc ( sizeof ( *intr ) + ( USBIO_INTR_COUNT * ep->mtu ) );
  633. if ( ! intr ) {
  634. rc = -ENOMEM;
  635. goto err_alloc;
  636. }
  637. endpoint->intr = intr;
  638. intr->endpoint = endpoint;
  639. data = ( ( ( void * ) intr ) + sizeof ( *intr ) );
  640. for ( i = 0 ; i < USBIO_INTR_COUNT ; i++ ) {
  641. intr->data[i] = data;
  642. data += ep->mtu;
  643. }
  644. /* Determine polling interval */
  645. interval = ( ep->interval >> 3 /* microframes -> milliseconds */ );
  646. if ( ! interval )
  647. interval = 1; /* May not be zero */
  648. /* Add to periodic schedule */
  649. if ( ( efirc = io->UsbAsyncInterruptTransfer ( io, ep->address, TRUE,
  650. interval, ep->mtu,
  651. usbio_interrupt_callback,
  652. intr ) ) != 0 ) {
  653. rc = -EEFI ( efirc );
  654. DBGC ( usbio, "USBIO %s %s could not schedule interrupt "
  655. "transfer: %s\n", efi_handle_name ( handle ),
  656. usb_endpoint_name ( ep ), strerror ( rc ) );
  657. goto err_schedule;
  658. }
  659. return 0;
  660. io->UsbAsyncInterruptTransfer ( io, ep->address, FALSE, 0, 0,
  661. NULL, NULL );
  662. err_schedule:
  663. free ( intr );
  664. err_alloc:
  665. return rc;
  666. }
  667. /**
  668. * Close interrupt endpoint
  669. *
  670. * @v endpoint Endpoint
  671. */
  672. static void usbio_interrupt_close ( struct usbio_endpoint *endpoint ) {
  673. struct usb_endpoint *ep = endpoint->ep;
  674. EFI_USB_IO_PROTOCOL *io = endpoint->io;
  675. /* Remove from periodic schedule */
  676. io->UsbAsyncInterruptTransfer ( io, ep->address, FALSE, 0, 0,
  677. NULL, NULL );
  678. /* Free interrupt ring buffer */
  679. free ( endpoint->intr );
  680. }
  681. /**
  682. * Poll interrupt endpoint
  683. *
  684. * @v endpoint Endpoint
  685. */
  686. static void usbio_interrupt_poll ( struct usbio_endpoint *endpoint ) {
  687. struct usbio_interrupt_ring *intr = endpoint->intr;
  688. struct usb_endpoint *ep = endpoint->ep;
  689. struct io_buffer *iobuf;
  690. unsigned int index;
  691. unsigned int intr_index;
  692. size_t len;
  693. /* Do nothing if ring is empty */
  694. if ( endpoint->cons == endpoint->prod )
  695. return;
  696. /* Do nothing if interrupt ring is empty */
  697. if ( intr->cons == intr->prod )
  698. return;
  699. /* Consume next transfer */
  700. index = ( endpoint->cons++ % USBIO_RING_COUNT );
  701. iobuf = endpoint->iobuf[index];
  702. /* Populate I/O buffer */
  703. intr_index = ( intr->cons++ % USBIO_INTR_COUNT );
  704. len = intr->len[intr_index];
  705. assert ( len <= iob_len ( iobuf ) );
  706. iob_put ( iobuf, ( len - iob_len ( iobuf ) ) );
  707. memcpy ( iobuf->data, intr->data[intr_index], len );
  708. /* Complete transfer */
  709. usb_complete ( ep, iobuf );
  710. }
  711. /** Interrupt endpoint operations */
  712. static struct usbio_operations usbio_interrupt_operations = {
  713. .open = usbio_interrupt_open,
  714. .close = usbio_interrupt_close,
  715. .poll = usbio_interrupt_poll,
  716. };
  717. /******************************************************************************
  718. *
  719. * Endpoint operations
  720. *
  721. ******************************************************************************
  722. */
  723. /**
  724. * Open endpoint
  725. *
  726. * @v ep USB endpoint
  727. * @ret rc Return status code
  728. */
  729. static int usbio_endpoint_open ( struct usb_endpoint *ep ) {
  730. struct usb_bus *bus = ep->usb->port->hub->bus;
  731. struct usbio_device *usbio = usb_bus_get_hostdata ( bus );
  732. struct usbio_endpoint *endpoint;
  733. EFI_HANDLE handle = usbio->handle;
  734. unsigned int attr = ( ep->attributes & USB_ENDPOINT_ATTR_TYPE_MASK );
  735. int interface;
  736. int rc;
  737. /* Allocate and initialise structure */
  738. endpoint = zalloc ( sizeof ( *endpoint ) );
  739. if ( ! endpoint ) {
  740. rc = -ENOMEM;
  741. goto err_alloc;
  742. }
  743. usb_endpoint_set_hostdata ( ep, endpoint );
  744. endpoint->usbio = usbio;
  745. endpoint->ep = ep;
  746. /* Identify endpoint operations */
  747. if ( attr == USB_ENDPOINT_ATTR_CONTROL ) {
  748. endpoint->op = &usbio_control_operations;
  749. } else if ( attr == USB_ENDPOINT_ATTR_BULK ) {
  750. endpoint->op = ( ( ep->address & USB_DIR_IN ) ?
  751. &usbio_bulk_in_operations :
  752. &usbio_bulk_out_operations );
  753. } else if ( attr == USB_ENDPOINT_ATTR_INTERRUPT ) {
  754. endpoint->op = &usbio_interrupt_operations;
  755. } else {
  756. rc = -ENOTSUP;
  757. goto err_operations;
  758. }
  759. /* Identify interface for this endpoint */
  760. interface = usbio_interface ( usbio, ep );
  761. if ( interface < 0 ) {
  762. rc = interface;
  763. goto err_interface;
  764. }
  765. endpoint->interface = interface;
  766. /* Open interface */
  767. if ( ( rc = usbio_open ( usbio, interface ) ) != 0 )
  768. goto err_open_interface;
  769. endpoint->handle = usbio->interface[interface].handle;
  770. endpoint->io = usbio->interface[interface].io;
  771. DBGC ( usbio, "USBIO %s %s using ",
  772. efi_handle_name ( handle ), usb_endpoint_name ( ep ) );
  773. DBGC ( usbio, "%s\n", efi_handle_name ( endpoint->handle ) );
  774. /* Open endpoint */
  775. if ( ( rc = endpoint->op->open ( endpoint ) ) != 0 )
  776. goto err_open_endpoint;
  777. /* Add to list of endpoints */
  778. list_add_tail ( &endpoint->list, &usbio->endpoints );
  779. return 0;
  780. list_del ( &endpoint->list );
  781. endpoint->op->close ( endpoint );
  782. err_open_endpoint:
  783. usbio_close ( usbio, interface );
  784. err_open_interface:
  785. err_interface:
  786. err_operations:
  787. free ( endpoint );
  788. err_alloc:
  789. return rc;
  790. }
  791. /**
  792. * Close endpoint
  793. *
  794. * @v ep USB endpoint
  795. */
  796. static void usbio_endpoint_close ( struct usb_endpoint *ep ) {
  797. struct usbio_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
  798. struct usbio_device *usbio = endpoint->usbio;
  799. struct io_buffer *iobuf;
  800. unsigned int index;
  801. /* Remove from list of endpoints */
  802. list_del ( &endpoint->list );
  803. /* Close endpoint */
  804. endpoint->op->close ( endpoint );
  805. /* Close interface */
  806. usbio_close ( usbio, endpoint->interface );
  807. /* Cancel any incomplete transfers */
  808. while ( endpoint->cons != endpoint->prod ) {
  809. index = ( endpoint->cons++ % USBIO_RING_COUNT );
  810. iobuf = endpoint->iobuf[index];
  811. usb_complete_err ( ep, iobuf, -ECANCELED );
  812. }
  813. /* Free endpoint */
  814. free ( endpoint );
  815. }
  816. /**
  817. * Reset endpoint
  818. *
  819. * @v ep USB endpoint
  820. * @ret rc Return status code
  821. */
  822. static int usbio_endpoint_reset ( struct usb_endpoint *ep __unused ) {
  823. /* Nothing to do */
  824. return 0;
  825. }
  826. /**
  827. * Update MTU
  828. *
  829. * @v ep USB endpoint
  830. * @ret rc Return status code
  831. */
  832. static int usbio_endpoint_mtu ( struct usb_endpoint *ep __unused ) {
  833. /* Nothing to do */
  834. return 0;
  835. }
  836. /**
  837. * Enqueue transfer
  838. *
  839. * @v ep USB endpoint
  840. * @v iobuf I/O buffer
  841. * @v flags Transfer flags
  842. * @ret rc Return status code
  843. */
  844. static int usbio_endpoint_enqueue ( struct usb_endpoint *ep,
  845. struct io_buffer *iobuf,
  846. unsigned int flags ) {
  847. struct usbio_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
  848. unsigned int fill;
  849. unsigned int index;
  850. /* Fail if shutdown is in progress */
  851. if ( efi_shutdown_in_progress )
  852. return -ECANCELED;
  853. /* Fail if transfer ring is full */
  854. fill = ( endpoint->prod - endpoint->cons );
  855. if ( fill >= USBIO_RING_COUNT )
  856. return -ENOBUFS;
  857. /* Add to ring */
  858. index = ( endpoint->prod++ % USBIO_RING_COUNT );
  859. endpoint->iobuf[index] = iobuf;
  860. endpoint->flags[index] = flags;
  861. return 0;
  862. }
  863. /**
  864. * Enqueue message transfer
  865. *
  866. * @v ep USB endpoint
  867. * @v iobuf I/O buffer
  868. * @ret rc Return status code
  869. */
  870. static int usbio_endpoint_message ( struct usb_endpoint *ep,
  871. struct io_buffer *iobuf ) {
  872. struct usb_setup_packet *setup;
  873. /* Adjust I/O buffer to start of data payload */
  874. assert ( iob_len ( iobuf ) >= sizeof ( *setup ) );
  875. iob_pull ( iobuf, sizeof ( *setup ) );
  876. /* Enqueue transfer */
  877. return usbio_endpoint_enqueue ( ep, iobuf, USBIO_MESSAGE );
  878. }
  879. /**
  880. * Enqueue stream transfer
  881. *
  882. * @v ep USB endpoint
  883. * @v iobuf I/O buffer
  884. * @v zlp Append a zero-length packet
  885. * @ret rc Return status code
  886. */
  887. static int usbio_endpoint_stream ( struct usb_endpoint *ep,
  888. struct io_buffer *iobuf, int zlp ) {
  889. /* Enqueue transfer */
  890. return usbio_endpoint_enqueue ( ep, iobuf, ( zlp ? USBIO_ZLEN : 0 ) );
  891. }
  892. /**
  893. * Poll for completions
  894. *
  895. * @v endpoint Endpoint
  896. */
  897. static void usbio_endpoint_poll ( struct usbio_endpoint *endpoint ) {
  898. /* Do nothing if shutdown is in progress */
  899. if ( efi_shutdown_in_progress )
  900. return;
  901. /* Poll endpoint */
  902. endpoint->op->poll ( endpoint );
  903. }
  904. /******************************************************************************
  905. *
  906. * Device operations
  907. *
  908. ******************************************************************************
  909. */
  910. /**
  911. * Open device
  912. *
  913. * @v usb USB device
  914. * @ret rc Return status code
  915. */
  916. static int usbio_device_open ( struct usb_device *usb ) {
  917. struct usbio_device *usbio =
  918. usb_bus_get_hostdata ( usb->port->hub->bus );
  919. usb_set_hostdata ( usb, usbio );
  920. return 0;
  921. }
  922. /**
  923. * Close device
  924. *
  925. * @v usb USB device
  926. */
  927. static void usbio_device_close ( struct usb_device *usb __unused ) {
  928. /* Nothing to do */
  929. }
  930. /**
  931. * Assign device address
  932. *
  933. * @v usb USB device
  934. * @ret rc Return status code
  935. */
  936. static int usbio_device_address ( struct usb_device *usb __unused ) {
  937. /* Nothing to do */
  938. return 0;
  939. }
  940. /******************************************************************************
  941. *
  942. * Hub operations
  943. *
  944. ******************************************************************************
  945. */
  946. /**
  947. * Open hub
  948. *
  949. * @v hub USB hub
  950. * @ret rc Return status code
  951. */
  952. static int usbio_hub_open ( struct usb_hub *hub ) {
  953. /* Disallow non-root hubs */
  954. if ( hub->usb )
  955. return -ENOTSUP;
  956. /* Nothing to do */
  957. return 0;
  958. }
  959. /**
  960. * Close hub
  961. *
  962. * @v hub USB hub
  963. */
  964. static void usbio_hub_close ( struct usb_hub *hub __unused ) {
  965. /* Nothing to do */
  966. }
  967. /******************************************************************************
  968. *
  969. * Root hub operations
  970. *
  971. ******************************************************************************
  972. */
  973. /**
  974. * Open root hub
  975. *
  976. * @v hub USB hub
  977. * @ret rc Return status code
  978. */
  979. static int usbio_root_open ( struct usb_hub *hub __unused ) {
  980. /* Nothing to do */
  981. return 0;
  982. }
  983. /**
  984. * Close root hub
  985. *
  986. * @v hub USB hub
  987. */
  988. static void usbio_root_close ( struct usb_hub *hub __unused ) {
  989. /* Nothing to do */
  990. }
  991. /**
  992. * Enable port
  993. *
  994. * @v hub USB hub
  995. * @v port USB port
  996. * @ret rc Return status code
  997. */
  998. static int usbio_root_enable ( struct usb_hub *hub __unused,
  999. struct usb_port *port __unused ) {
  1000. /* Nothing to do */
  1001. return 0;
  1002. }
  1003. /**
  1004. * Disable port
  1005. *
  1006. * @v hub USB hub
  1007. * @v port USB port
  1008. * @ret rc Return status code
  1009. */
  1010. static int usbio_root_disable ( struct usb_hub *hub __unused,
  1011. struct usb_port *port __unused ) {
  1012. /* Nothing to do */
  1013. return 0;
  1014. }
  1015. /**
  1016. * Update root hub port speed
  1017. *
  1018. * @v hub USB hub
  1019. * @v port USB port
  1020. * @ret rc Return status code
  1021. */
  1022. static int usbio_root_speed ( struct usb_hub *hub __unused,
  1023. struct usb_port *port ) {
  1024. /* Not actually exposed via EFI_USB_IO_PROTOCOL */
  1025. port->speed = USB_SPEED_HIGH;
  1026. return 0;
  1027. }
  1028. /**
  1029. * Clear transaction translator buffer
  1030. *
  1031. * @v hub USB hub
  1032. * @v port USB port
  1033. * @v ep USB endpoint
  1034. * @ret rc Return status code
  1035. */
  1036. static int usbio_root_clear_tt ( struct usb_hub *hub __unused,
  1037. struct usb_port *port __unused,
  1038. struct usb_endpoint *ep __unused ) {
  1039. /* Should never be called; this is a root hub */
  1040. return -ENOTSUP;
  1041. }
  1042. /******************************************************************************
  1043. *
  1044. * Bus operations
  1045. *
  1046. ******************************************************************************
  1047. */
  1048. /**
  1049. * Open USB bus
  1050. *
  1051. * @v bus USB bus
  1052. * @ret rc Return status code
  1053. */
  1054. static int usbio_bus_open ( struct usb_bus *bus __unused ) {
  1055. /* Nothing to do */
  1056. return 0;
  1057. }
  1058. /**
  1059. * Close USB bus
  1060. *
  1061. * @v bus USB bus
  1062. */
  1063. static void usbio_bus_close ( struct usb_bus *bus __unused ) {
  1064. /* Nothing to do */
  1065. }
  1066. /**
  1067. * Poll USB bus
  1068. *
  1069. * @v bus USB bus
  1070. */
  1071. static void usbio_bus_poll ( struct usb_bus *bus ) {
  1072. struct usbio_device *usbio = usb_bus_get_hostdata ( bus );
  1073. struct usbio_endpoint *endpoint;
  1074. /* Poll all endpoints. We trust that completion handlers are
  1075. * minimal and will not do anything that could plausibly
  1076. * affect the endpoint list itself.
  1077. */
  1078. list_for_each_entry ( endpoint, &usbio->endpoints, list )
  1079. usbio_endpoint_poll ( endpoint );
  1080. }
  1081. /******************************************************************************
  1082. *
  1083. * EFI driver interface
  1084. *
  1085. ******************************************************************************
  1086. */
  1087. /** USB I/O host controller driver operations */
  1088. static struct usb_host_operations usbio_operations = {
  1089. .endpoint = {
  1090. .open = usbio_endpoint_open,
  1091. .close = usbio_endpoint_close,
  1092. .reset = usbio_endpoint_reset,
  1093. .mtu = usbio_endpoint_mtu,
  1094. .message = usbio_endpoint_message,
  1095. .stream = usbio_endpoint_stream,
  1096. },
  1097. .device = {
  1098. .open = usbio_device_open,
  1099. .close = usbio_device_close,
  1100. .address = usbio_device_address,
  1101. },
  1102. .bus = {
  1103. .open = usbio_bus_open,
  1104. .close = usbio_bus_close,
  1105. .poll = usbio_bus_poll,
  1106. },
  1107. .hub = {
  1108. .open = usbio_hub_open,
  1109. .close = usbio_hub_close,
  1110. },
  1111. .root = {
  1112. .open = usbio_root_open,
  1113. .close = usbio_root_close,
  1114. .enable = usbio_root_enable,
  1115. .disable = usbio_root_disable,
  1116. .speed = usbio_root_speed,
  1117. .clear_tt = usbio_root_clear_tt,
  1118. },
  1119. };
  1120. /**
  1121. * Check to see if driver supports a device
  1122. *
  1123. * @v handle EFI device handle
  1124. * @ret rc Return status code
  1125. */
  1126. static int usbio_supported ( EFI_HANDLE handle ) {
  1127. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  1128. EFI_USB_DEVICE_DESCRIPTOR device;
  1129. EFI_USB_INTERFACE_DESCRIPTOR interface;
  1130. struct usb_function_descriptor desc;
  1131. struct usb_driver *driver;
  1132. struct usb_device_id *id;
  1133. union {
  1134. void *interface;
  1135. EFI_USB_IO_PROTOCOL *io;
  1136. } usb;
  1137. EFI_STATUS efirc;
  1138. int rc;
  1139. /* Get protocol */
  1140. if ( ( efirc = bs->OpenProtocol ( handle, &efi_usb_io_protocol_guid,
  1141. &usb.interface, efi_image_handle,
  1142. handle,
  1143. EFI_OPEN_PROTOCOL_GET_PROTOCOL ))!=0){
  1144. rc = -EEFI ( efirc );
  1145. DBGCP ( handle, "USB %s is not a USB device\n",
  1146. efi_handle_name ( handle ) );
  1147. goto err_open_protocol;
  1148. }
  1149. /* Get device descriptor */
  1150. if ( ( efirc = usb.io->UsbGetDeviceDescriptor ( usb.io,
  1151. &device ) ) != 0 ) {
  1152. rc = -EEFI ( efirc );
  1153. DBGC ( handle, "USB %s could not get device descriptor: "
  1154. "%s\n", efi_handle_name ( handle ), strerror ( rc ) );
  1155. goto err_get_device_descriptor;
  1156. }
  1157. memset ( &desc, 0, sizeof ( desc ) );
  1158. desc.vendor = device.IdVendor;
  1159. desc.product = device.IdProduct;
  1160. /* Get interface descriptor */
  1161. if ( ( efirc = usb.io->UsbGetInterfaceDescriptor ( usb.io,
  1162. &interface ) ) !=0){
  1163. rc = -EEFI ( efirc );
  1164. DBGC ( handle, "USB %s could not get interface descriptor: "
  1165. "%s\n", efi_handle_name ( handle ), strerror ( rc ) );
  1166. goto err_get_interface_descriptor;
  1167. }
  1168. desc.class.class.class = interface.InterfaceClass;
  1169. desc.class.class.subclass = interface.InterfaceSubClass;
  1170. desc.class.class.protocol = interface.InterfaceProtocol;
  1171. /* Look for a driver for this interface */
  1172. driver = usb_find_driver ( &desc, &id );
  1173. if ( ! driver ) {
  1174. rc = -ENOTSUP;
  1175. goto err_unsupported;
  1176. }
  1177. /* Success */
  1178. rc = 0;
  1179. err_unsupported:
  1180. err_get_interface_descriptor:
  1181. err_get_device_descriptor:
  1182. bs->CloseProtocol ( handle, &efi_usb_io_protocol_guid,
  1183. efi_image_handle, handle );
  1184. err_open_protocol:
  1185. return rc;
  1186. }
  1187. /**
  1188. * Fetch configuration descriptor
  1189. *
  1190. * @v usbio USB I/O device
  1191. * @ret rc Return status code
  1192. */
  1193. static int usbio_config ( struct usbio_device *usbio ) {
  1194. EFI_HANDLE handle = usbio->handle;
  1195. EFI_USB_IO_PROTOCOL *io = usbio->io;
  1196. EFI_USB_DEVICE_DESCRIPTOR device;
  1197. EFI_USB_CONFIG_DESCRIPTOR partial;
  1198. union {
  1199. struct usb_setup_packet setup;
  1200. EFI_USB_DEVICE_REQUEST efi;
  1201. } msg;
  1202. UINT32 status;
  1203. size_t len;
  1204. unsigned int count;
  1205. unsigned int value;
  1206. unsigned int i;
  1207. EFI_STATUS efirc;
  1208. int rc;
  1209. /* Get device descriptor */
  1210. if ( ( efirc = io->UsbGetDeviceDescriptor ( io, &device ) ) != 0 ) {
  1211. rc = -EEFI ( efirc );
  1212. DBGC ( usbio, "USB %s could not get device descriptor: "
  1213. "%s\n", efi_handle_name ( handle ), strerror ( rc ) );
  1214. goto err_get_device_descriptor;
  1215. }
  1216. count = device.NumConfigurations;
  1217. /* Get current partial configuration descriptor */
  1218. if ( ( efirc = io->UsbGetConfigDescriptor ( io, &partial ) ) != 0 ) {
  1219. rc = -EEFI ( efirc );
  1220. DBGC ( usbio, "USB %s could not get partial configuration "
  1221. "descriptor: %s\n", efi_handle_name ( handle ),
  1222. strerror ( rc ) );
  1223. goto err_get_configuration_descriptor;
  1224. }
  1225. len = le16_to_cpu ( partial.TotalLength );
  1226. /* Allocate configuration descriptor */
  1227. usbio->config = malloc ( len );
  1228. if ( ! usbio->config ) {
  1229. rc = -ENOMEM;
  1230. goto err_alloc;
  1231. }
  1232. /* There is, naturally, no way to retrieve the entire device
  1233. * configuration descriptor via EFI_USB_IO_PROTOCOL. Worse,
  1234. * there is no way to even retrieve the index of the current
  1235. * configuration descriptor. We have to iterate over all
  1236. * possible configuration descriptors looking for the
  1237. * descriptor that matches the current configuration value.
  1238. */
  1239. for ( i = 0 ; i < count ; i++ ) {
  1240. /* Construct request */
  1241. msg.setup.request = cpu_to_le16 ( USB_GET_DESCRIPTOR );
  1242. value = ( ( USB_CONFIGURATION_DESCRIPTOR << 8 ) | i );
  1243. msg.setup.value = cpu_to_le16 ( value );
  1244. msg.setup.index = 0;
  1245. msg.setup.len = cpu_to_le16 ( len );
  1246. /* Get full configuration descriptor */
  1247. if ( ( efirc = io->UsbControlTransfer ( io, &msg.efi,
  1248. EfiUsbDataIn, 0,
  1249. usbio->config, len,
  1250. &status ) ) != 0 ) {
  1251. rc = -EEFI ( efirc );
  1252. DBGC ( usbio, "USB %s could not get configuration %d "
  1253. "descriptor: %s\n", efi_handle_name ( handle ),
  1254. i, strerror ( rc ) );
  1255. goto err_control_transfer;
  1256. }
  1257. /* Ignore unless this is the current configuration */
  1258. if ( usbio->config->config != partial.ConfigurationValue )
  1259. continue;
  1260. /* Check length */
  1261. if ( le16_to_cpu ( usbio->config->len ) != len ) {
  1262. DBGC ( usbio, "USB %s configuration descriptor length "
  1263. "mismatch\n", efi_handle_name ( handle ) );
  1264. rc = -EINVAL;
  1265. goto err_len;
  1266. }
  1267. return 0;
  1268. }
  1269. /* No match found */
  1270. DBGC ( usbio, "USB %s could not find current configuration "
  1271. "descriptor\n", efi_handle_name ( handle ) );
  1272. rc = -ENOENT;
  1273. err_len:
  1274. err_control_transfer:
  1275. free ( usbio->config );
  1276. err_alloc:
  1277. err_get_configuration_descriptor:
  1278. err_get_device_descriptor:
  1279. return rc;
  1280. }
  1281. /**
  1282. * Construct device path for opening other interfaces
  1283. *
  1284. * @v usbio USB I/O device
  1285. * @ret rc Return status code
  1286. */
  1287. static int usbio_path ( struct usbio_device *usbio ) {
  1288. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  1289. EFI_HANDLE handle = usbio->handle;
  1290. EFI_DEVICE_PATH_PROTOCOL *path;
  1291. EFI_DEVICE_PATH_PROTOCOL *end;
  1292. USB_DEVICE_PATH *usbpath;
  1293. union {
  1294. void *interface;
  1295. EFI_DEVICE_PATH_PROTOCOL *path;
  1296. } u;
  1297. size_t len;
  1298. EFI_STATUS efirc;
  1299. int rc;
  1300. /* Open device path protocol */
  1301. if ( ( efirc = bs->OpenProtocol ( handle,
  1302. &efi_device_path_protocol_guid,
  1303. &u.interface, efi_image_handle,
  1304. handle,
  1305. EFI_OPEN_PROTOCOL_GET_PROTOCOL ))!=0){
  1306. rc = -EEFI ( efirc );
  1307. DBGC ( usbio, "USBIO %s cannot open device path protocol: "
  1308. "%s\n", efi_handle_name ( handle ), strerror ( rc ) );
  1309. goto err_open_protocol;
  1310. }
  1311. path = u.interface;
  1312. /* Locate end of device path and sanity check */
  1313. len = efi_devpath_len ( path );
  1314. if ( len < sizeof ( *usbpath ) ) {
  1315. DBGC ( usbio, "USBIO %s underlength device path\n",
  1316. efi_handle_name ( handle ) );
  1317. rc = -EINVAL;
  1318. goto err_underlength;
  1319. }
  1320. usbpath = ( ( ( void * ) path ) + len - sizeof ( *usbpath ) );
  1321. if ( ! ( ( usbpath->Header.Type == MESSAGING_DEVICE_PATH ) &&
  1322. ( usbpath->Header.SubType == MSG_USB_DP ) ) ) {
  1323. DBGC ( usbio, "USBIO %s not a USB device path: ",
  1324. efi_handle_name ( handle ) );
  1325. DBGC ( usbio, "%s\n", efi_devpath_text ( path ) );
  1326. rc = -EINVAL;
  1327. goto err_non_usb;
  1328. }
  1329. /* Allocate copy of device path */
  1330. usbio->path = malloc ( len + sizeof ( *end ) );
  1331. if ( ! usbio->path ) {
  1332. rc = -ENOMEM;
  1333. goto err_alloc;
  1334. }
  1335. memcpy ( usbio->path, path, ( len + sizeof ( *end ) ) );
  1336. usbio->usbpath = ( ( ( void * ) usbio->path ) + len -
  1337. sizeof ( *usbpath ) );
  1338. /* Close protocol */
  1339. bs->CloseProtocol ( handle, &efi_device_path_protocol_guid,
  1340. efi_image_handle, handle );
  1341. return 0;
  1342. free ( usbio->path );
  1343. err_alloc:
  1344. err_non_usb:
  1345. err_underlength:
  1346. bs->CloseProtocol ( handle, &efi_device_path_protocol_guid,
  1347. efi_image_handle, handle );
  1348. err_open_protocol:
  1349. return rc;
  1350. }
  1351. /**
  1352. * Construct interface list
  1353. *
  1354. * @v usbio USB I/O device
  1355. * @ret rc Return status code
  1356. */
  1357. static int usbio_interfaces ( struct usbio_device *usbio ) {
  1358. EFI_HANDLE handle = usbio->handle;
  1359. EFI_USB_IO_PROTOCOL *io = usbio->io;
  1360. EFI_USB_INTERFACE_DESCRIPTOR interface;
  1361. unsigned int first;
  1362. unsigned int count;
  1363. EFI_STATUS efirc;
  1364. int rc;
  1365. /* Get interface descriptor */
  1366. if ( ( efirc = io->UsbGetInterfaceDescriptor ( io, &interface ) ) != 0){
  1367. rc = -EEFI ( efirc );
  1368. DBGC ( usbio, "USB %s could not get interface descriptor: "
  1369. "%s\n", efi_handle_name ( handle ), strerror ( rc ) );
  1370. goto err_get_interface_descriptor;
  1371. }
  1372. /* Record first interface number */
  1373. first = interface.InterfaceNumber;
  1374. count = usbio->config->interfaces;
  1375. assert ( first < count );
  1376. usbio->first = first;
  1377. /* Allocate interface list */
  1378. usbio->interface = zalloc ( count * sizeof ( usbio->interface[0] ) );
  1379. if ( ! usbio->interface ) {
  1380. rc = -ENOMEM;
  1381. goto err_alloc;
  1382. }
  1383. /* Use already-opened protocol for control transfers and for
  1384. * the first interface.
  1385. */
  1386. usbio->interface[0].handle = handle;
  1387. usbio->interface[0].io = io;
  1388. usbio->interface[0].count = 1;
  1389. usbio->interface[first].handle = handle;
  1390. usbio->interface[first].io = io;
  1391. usbio->interface[first].count = 1;
  1392. return 0;
  1393. free ( usbio->interface );
  1394. err_alloc:
  1395. err_get_interface_descriptor:
  1396. return rc;
  1397. }
  1398. /**
  1399. * Attach driver to device
  1400. *
  1401. * @v efidev EFI device
  1402. * @ret rc Return status code
  1403. */
  1404. static int usbio_start ( struct efi_device *efidev ) {
  1405. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  1406. EFI_HANDLE handle = efidev->device;
  1407. struct usbio_device *usbio;
  1408. struct usb_port *port;
  1409. union {
  1410. void *interface;
  1411. EFI_USB_IO_PROTOCOL *io;
  1412. } u;
  1413. EFI_STATUS efirc;
  1414. int rc;
  1415. /* Allocate and initialise structure */
  1416. usbio = zalloc ( sizeof ( *usbio ) );
  1417. if ( ! usbio ) {
  1418. rc = -ENOMEM;
  1419. goto err_alloc;
  1420. }
  1421. efidev_set_drvdata ( efidev, usbio );
  1422. usbio->handle = handle;
  1423. INIT_LIST_HEAD ( &usbio->endpoints );
  1424. /* Open USB I/O protocol */
  1425. if ( ( efirc = bs->OpenProtocol ( handle, &efi_usb_io_protocol_guid,
  1426. &u.interface, efi_image_handle,
  1427. handle,
  1428. ( EFI_OPEN_PROTOCOL_BY_DRIVER |
  1429. EFI_OPEN_PROTOCOL_EXCLUSIVE )))!=0){
  1430. rc = -EEFI ( efirc );
  1431. DBGC ( usbio, "USBIO %s cannot open USB I/O protocol: %s\n",
  1432. efi_handle_name ( handle ), strerror ( rc ) );
  1433. DBGC_EFI_OPENERS ( usbio, handle, &efi_usb_io_protocol_guid );
  1434. goto err_open_usbio;
  1435. }
  1436. usbio->io = u.io;
  1437. /* Describe generic device */
  1438. efi_device_info ( handle, "USB", &usbio->dev );
  1439. usbio->dev.parent = &efidev->dev;
  1440. list_add ( &usbio->dev.siblings, &efidev->dev.children );
  1441. INIT_LIST_HEAD ( &usbio->dev.children );
  1442. /* Fetch configuration descriptor */
  1443. if ( ( rc = usbio_config ( usbio ) ) != 0 )
  1444. goto err_config;
  1445. /* Construct device path */
  1446. if ( ( rc = usbio_path ( usbio ) ) != 0 )
  1447. goto err_path;
  1448. /* Construct interface list */
  1449. if ( ( rc = usbio_interfaces ( usbio ) ) != 0 )
  1450. goto err_interfaces;
  1451. /* Allocate USB bus */
  1452. usbio->bus = alloc_usb_bus ( &usbio->dev, 1 /* single "port" */,
  1453. USBIO_MTU, &usbio_operations );
  1454. if ( ! usbio->bus ) {
  1455. rc = -ENOMEM;
  1456. goto err_alloc_bus;
  1457. }
  1458. usb_bus_set_hostdata ( usbio->bus, usbio );
  1459. usb_hub_set_drvdata ( usbio->bus->hub, usbio );
  1460. /* Set port protocol */
  1461. port = usb_port ( usbio->bus->hub, 1 );
  1462. port->protocol = USB_PROTO_2_0;
  1463. /* Register USB bus */
  1464. if ( ( rc = register_usb_bus ( usbio->bus ) ) != 0 )
  1465. goto err_register;
  1466. return 0;
  1467. unregister_usb_bus ( usbio->bus );
  1468. err_register:
  1469. free_usb_bus ( usbio->bus );
  1470. err_alloc_bus:
  1471. free ( usbio->interface );
  1472. err_interfaces:
  1473. free ( usbio->path );
  1474. err_path:
  1475. free ( usbio->config );
  1476. err_config:
  1477. list_del ( &usbio->dev.siblings );
  1478. bs->CloseProtocol ( handle, &efi_usb_io_protocol_guid,
  1479. efi_image_handle, handle );
  1480. err_open_usbio:
  1481. free ( usbio );
  1482. err_alloc:
  1483. return rc;
  1484. }
  1485. /**
  1486. * Detach driver from device
  1487. *
  1488. * @v efidev EFI device
  1489. */
  1490. static void usbio_stop ( struct efi_device *efidev ) {
  1491. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  1492. EFI_HANDLE handle = efidev->device;
  1493. struct usbio_device *usbio = efidev_get_drvdata ( efidev );
  1494. unregister_usb_bus ( usbio->bus );
  1495. free_usb_bus ( usbio->bus );
  1496. free ( usbio->interface );
  1497. free ( usbio->path );
  1498. free ( usbio->config );
  1499. list_del ( &usbio->dev.siblings );
  1500. bs->CloseProtocol ( handle, &efi_usb_io_protocol_guid,
  1501. efi_image_handle, handle );
  1502. free ( usbio );
  1503. }
  1504. /** EFI USB I/O driver */
  1505. struct efi_driver usbio_driver __efi_driver ( EFI_DRIVER_NORMAL ) = {
  1506. .name = "USBIO",
  1507. .supported = usbio_supported,
  1508. .start = usbio_start,
  1509. .stop = usbio_stop,
  1510. };