Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

usbio.c 44KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717
  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. assert ( iob_len ( iobuf ) >= sizeof ( *msg ) );
  315. msg = iobuf->data;
  316. iob_pull ( iobuf, sizeof ( *msg ) );
  317. request = le16_to_cpu ( msg->setup.request );
  318. len = iob_len ( iobuf );
  319. if ( len ) {
  320. data = iobuf->data;
  321. direction = ( ( request & USB_DIR_IN ) ?
  322. EfiUsbDataIn : EfiUsbDataOut );
  323. } else {
  324. data = NULL;
  325. direction = EfiUsbNoData;
  326. }
  327. /* Determine interface for this transfer */
  328. recipient = ( request & USB_RECIP_MASK );
  329. if ( recipient == USB_RECIP_INTERFACE ) {
  330. /* Recipient is an interface: use interface number directly */
  331. interface = le16_to_cpu ( msg->setup.index );
  332. } else {
  333. /* Route all other requests through the first interface */
  334. interface = 0;
  335. }
  336. /* Open interface */
  337. if ( ( rc = usbio_open ( usbio, interface ) ) != 0 )
  338. goto err_open;
  339. io = usbio->interface[interface].io;
  340. /* Due to the design of EFI_USB_IO_PROTOCOL, attempting to set
  341. * the configuration to a non-default value is basically a
  342. * self-destruct button.
  343. */
  344. if ( ( request == USB_SET_CONFIGURATION ) &&
  345. ( le16_to_cpu ( msg->setup.value ) != usbio->config->config ) ) {
  346. rc = -ENOTSUP_MORONIC_SPECIFICATION;
  347. DBGC ( usbio, "USBIO %s cannot change configuration: %s\n",
  348. efi_handle_name ( handle ), strerror ( rc ) );
  349. goto err_moronic_specification;
  350. }
  351. /* Submit transfer */
  352. if ( ( efirc = io->UsbControlTransfer ( io, &msg->efi, direction, 0,
  353. data, len, &status ) ) != 0 ) {
  354. rc = -EEFI ( efirc );
  355. DBGC ( usbio, "USBIO %s %s could not submit control transfer ",
  356. efi_handle_name ( handle ), usb_endpoint_name ( ep ) );
  357. DBGC ( usbio, "via %s: %s (status %04x)\n",
  358. efi_handle_name ( usbio->interface[interface].handle ),
  359. strerror ( rc ), status );
  360. goto err_transfer;
  361. }
  362. /* Close interface */
  363. usbio_close ( usbio, interface );
  364. /* Complete transfer */
  365. usb_complete ( ep, iobuf );
  366. return;
  367. err_transfer:
  368. err_moronic_specification:
  369. usbio_close ( usbio, interface );
  370. err_open:
  371. err_not_message:
  372. usb_complete_err ( ep, iobuf, rc );
  373. }
  374. /** Control endpoint operations */
  375. static struct usbio_operations usbio_control_operations = {
  376. .open = usbio_control_open,
  377. .close = usbio_control_close,
  378. .poll = usbio_control_poll,
  379. };
  380. /******************************************************************************
  381. *
  382. * Bulk IN endpoints
  383. *
  384. ******************************************************************************
  385. */
  386. /**
  387. * Open bulk IN endpoint
  388. *
  389. * @v endpoint Endpoint
  390. * @ret rc Return status code
  391. */
  392. static int usbio_bulk_in_open ( struct usbio_endpoint *endpoint __unused ) {
  393. /* Nothing to do */
  394. return 0;
  395. }
  396. /**
  397. * Close bulk IN endpoint
  398. *
  399. * @v endpoint Endpoint
  400. */
  401. static void usbio_bulk_in_close ( struct usbio_endpoint *endpoint __unused ) {
  402. /* Nothing to do */
  403. }
  404. /**
  405. * Poll bulk IN endpoint
  406. *
  407. * @v endpoint Endpoint
  408. */
  409. static void usbio_bulk_in_poll ( struct usbio_endpoint *endpoint ) {
  410. struct usbio_device *usbio = endpoint->usbio;
  411. struct usb_endpoint *ep = endpoint->ep;
  412. EFI_USB_IO_PROTOCOL *io = endpoint->io;
  413. EFI_HANDLE handle = usbio->handle;
  414. struct io_buffer *iobuf;
  415. unsigned int index;
  416. UINTN len;
  417. UINT32 status;
  418. EFI_STATUS efirc;
  419. int rc;
  420. /* Do nothing if ring is empty */
  421. if ( endpoint->cons == endpoint->prod )
  422. return;
  423. /* Attempt (but do not yet consume) next transfer */
  424. index = ( endpoint->cons % USBIO_RING_COUNT );
  425. iobuf = endpoint->iobuf[index];
  426. /* Construct transfer */
  427. len = iob_len ( iobuf );
  428. /* Upon being turned on, the EFI_USB_IO_PROTOCOL did nothing
  429. * for several minutes before firing a small ARP packet a few
  430. * millimetres into the ether.
  431. */
  432. efirc = io->UsbBulkTransfer ( io, ep->address, iobuf->data,
  433. &len, 1, &status );
  434. if ( efirc == EFI_TIMEOUT )
  435. return;
  436. /* Consume transfer */
  437. endpoint->cons++;
  438. /* Check for failure */
  439. if ( efirc != 0 ) {
  440. rc = -EEFI ( efirc );
  441. DBGC2 ( usbio, "USBIO %s %s could not submit bulk IN transfer: "
  442. "%s (status %04x)\n", efi_handle_name ( handle ),
  443. usb_endpoint_name ( ep ), strerror ( rc ), status );
  444. usb_complete_err ( ep, iobuf, rc );
  445. return;
  446. }
  447. /* Update length */
  448. iob_put ( iobuf, ( len - iob_len ( iobuf ) ) );
  449. /* Complete transfer */
  450. usb_complete ( ep, iobuf );
  451. }
  452. /** Bulk endpoint operations */
  453. static struct usbio_operations usbio_bulk_in_operations = {
  454. .open = usbio_bulk_in_open,
  455. .close = usbio_bulk_in_close,
  456. .poll = usbio_bulk_in_poll,
  457. };
  458. /******************************************************************************
  459. *
  460. * Bulk OUT endpoints
  461. *
  462. ******************************************************************************
  463. */
  464. /**
  465. * Open bulk OUT endpoint
  466. *
  467. * @v endpoint Endpoint
  468. * @ret rc Return status code
  469. */
  470. static int usbio_bulk_out_open ( struct usbio_endpoint *endpoint __unused ) {
  471. /* Nothing to do */
  472. return 0;
  473. }
  474. /**
  475. * Close bulk OUT endpoint
  476. *
  477. * @v endpoint Endpoint
  478. */
  479. static void usbio_bulk_out_close ( struct usbio_endpoint *endpoint __unused ) {
  480. /* Nothing to do */
  481. }
  482. /**
  483. * Poll bulk OUT endpoint
  484. *
  485. * @v endpoint Endpoint
  486. */
  487. static void usbio_bulk_out_poll ( struct usbio_endpoint *endpoint ) {
  488. struct usbio_device *usbio = endpoint->usbio;
  489. struct usb_endpoint *ep = endpoint->ep;
  490. EFI_USB_IO_PROTOCOL *io = endpoint->io;
  491. EFI_HANDLE handle = usbio->handle;
  492. struct io_buffer *iobuf;
  493. unsigned int index;
  494. unsigned int flags;
  495. UINTN len;
  496. UINT32 status;
  497. EFI_STATUS efirc;
  498. int rc;
  499. /* Do nothing if ring is empty */
  500. if ( endpoint->cons == endpoint->prod )
  501. return;
  502. /* Consume next transfer */
  503. index = ( endpoint->cons++ % USBIO_RING_COUNT );
  504. iobuf = endpoint->iobuf[index];
  505. flags = endpoint->flags[index];
  506. /* Construct transfer */
  507. len = iob_len ( iobuf );
  508. /* Submit transfer */
  509. if ( ( efirc = io->UsbBulkTransfer ( io, ep->address, iobuf->data,
  510. &len, 0, &status ) ) != 0 ) {
  511. rc = -EEFI ( efirc );
  512. DBGC ( usbio, "USBIO %s %s could not submit bulk OUT transfer: "
  513. "%s (status %04x)\n", efi_handle_name ( handle ),
  514. usb_endpoint_name ( ep ), strerror ( rc ), status );
  515. goto err;
  516. }
  517. /* Update length */
  518. iob_put ( iobuf, ( len - iob_len ( iobuf ) ) );
  519. /* Submit zero-length transfer if required */
  520. len = 0;
  521. if ( ( flags & USBIO_ZLEN ) &&
  522. ( efirc = io->UsbBulkTransfer ( io, ep->address, NULL, &len, 0,
  523. &status ) ) != 0 ) {
  524. rc = -EEFI ( efirc );
  525. DBGC ( usbio, "USBIO %s %s could not submit zero-length "
  526. "transfer: %s (status %04x)\n",
  527. efi_handle_name ( handle ), usb_endpoint_name ( ep ),
  528. strerror ( rc ), status );
  529. goto err;
  530. }
  531. /* Complete transfer */
  532. usb_complete ( ep, iobuf );
  533. return;
  534. err:
  535. usb_complete_err ( ep, iobuf, rc );
  536. }
  537. /** Bulk endpoint operations */
  538. static struct usbio_operations usbio_bulk_out_operations = {
  539. .open = usbio_bulk_out_open,
  540. .close = usbio_bulk_out_close,
  541. .poll = usbio_bulk_out_poll,
  542. };
  543. /******************************************************************************
  544. *
  545. * Interrupt endpoints
  546. *
  547. ******************************************************************************
  548. *
  549. * The EFI_USB_IO_PROTOCOL provides two ways to interact with
  550. * interrupt endpoints, neither of which naturally model the hardware
  551. * interaction. The UsbSyncInterruptTransfer() method allows imposes
  552. * a 1ms overhead for every interrupt transfer (which could result in
  553. * up to a 50% decrease in overall throughput for the device). The
  554. * UsbAsyncInterruptTransfer() method provides no way for us to
  555. * prevent transfers when no I/O buffers are available.
  556. *
  557. * We work around this design by utilising a small, fixed ring buffer
  558. * into which the interrupt callback delivers the data. This aims to
  559. * provide buffer space even if no I/O buffers have yet been enqueued.
  560. * The scheme is not guaranteed since the fixed ring buffer may also
  561. * become full. However:
  562. *
  563. * - devices which send a constant stream of interrupts (and which
  564. * therefore might exhaust the fixed ring buffer) tend to be
  565. * responding to every interrupt request, and can tolerate lost
  566. * packets, and
  567. *
  568. * - devices which cannot tolerate lost interrupt packets tend to send
  569. * only a few small messages.
  570. *
  571. * The scheme should therefore work in practice.
  572. */
  573. /**
  574. * Interrupt endpoint callback
  575. *
  576. * @v data Received data
  577. * @v len Length of received data
  578. * @v context Callback context
  579. * @v status Transfer status
  580. * @ret efirc EFI status code
  581. */
  582. static EFI_STATUS EFIAPI usbio_interrupt_callback ( VOID *data, UINTN len,
  583. VOID *context,
  584. UINT32 status ) {
  585. struct usbio_interrupt_ring *intr = context;
  586. struct usbio_endpoint *endpoint = intr->endpoint;
  587. struct usbio_device *usbio = endpoint->usbio;
  588. struct usb_endpoint *ep = endpoint->ep;
  589. EFI_HANDLE handle = usbio->handle;
  590. unsigned int fill;
  591. unsigned int index;
  592. /* Sanity check */
  593. assert ( len <= ep->mtu );
  594. /* Do nothing if ring is empty */
  595. fill = ( intr->prod - intr->cons );
  596. if ( fill >= USBIO_INTR_COUNT ) {
  597. DBGC ( usbio, "USBIO %s %s dropped interrupt completion\n",
  598. efi_handle_name ( handle ), usb_endpoint_name ( ep ) );
  599. return 0;
  600. }
  601. /* Do nothing if transfer was unsuccessful */
  602. if ( status != 0 ) {
  603. DBGC ( usbio, "USBIO %s %s interrupt completion status %04x\n",
  604. efi_handle_name ( handle ), usb_endpoint_name ( ep ),
  605. status );
  606. return 0; /* Unclear what failure actually means here */
  607. }
  608. /* Copy data to buffer and increment producer counter */
  609. index = ( intr->prod % USBIO_INTR_COUNT );
  610. memcpy ( intr->data[index], data, len );
  611. intr->len[index] = len;
  612. intr->prod++;
  613. return 0;
  614. }
  615. /**
  616. * Open interrupt endpoint
  617. *
  618. * @v endpoint Endpoint
  619. * @ret rc Return status code
  620. */
  621. static int usbio_interrupt_open ( struct usbio_endpoint *endpoint ) {
  622. struct usbio_device *usbio = endpoint->usbio;
  623. struct usbio_interrupt_ring *intr;
  624. struct usb_endpoint *ep = endpoint->ep;
  625. EFI_USB_IO_PROTOCOL *io = endpoint->io;
  626. EFI_HANDLE handle = usbio->handle;
  627. unsigned int interval;
  628. unsigned int i;
  629. void *data;
  630. EFI_STATUS efirc;
  631. int rc;
  632. /* Allocate interrupt ring buffer */
  633. intr = zalloc ( sizeof ( *intr ) + ( USBIO_INTR_COUNT * ep->mtu ) );
  634. if ( ! intr ) {
  635. rc = -ENOMEM;
  636. goto err_alloc;
  637. }
  638. endpoint->intr = intr;
  639. intr->endpoint = endpoint;
  640. data = ( ( ( void * ) intr ) + sizeof ( *intr ) );
  641. for ( i = 0 ; i < USBIO_INTR_COUNT ; i++ ) {
  642. intr->data[i] = data;
  643. data += ep->mtu;
  644. }
  645. /* Determine polling interval */
  646. interval = ( ep->interval >> 3 /* microframes -> milliseconds */ );
  647. if ( ! interval )
  648. interval = 1; /* May not be zero */
  649. /* Add to periodic schedule */
  650. if ( ( efirc = io->UsbAsyncInterruptTransfer ( io, ep->address, TRUE,
  651. interval, ep->mtu,
  652. usbio_interrupt_callback,
  653. intr ) ) != 0 ) {
  654. rc = -EEFI ( efirc );
  655. DBGC ( usbio, "USBIO %s %s could not schedule interrupt "
  656. "transfer: %s\n", efi_handle_name ( handle ),
  657. usb_endpoint_name ( ep ), strerror ( rc ) );
  658. goto err_schedule;
  659. }
  660. return 0;
  661. io->UsbAsyncInterruptTransfer ( io, ep->address, FALSE, 0, 0,
  662. NULL, NULL );
  663. err_schedule:
  664. free ( intr );
  665. err_alloc:
  666. return rc;
  667. }
  668. /**
  669. * Close interrupt endpoint
  670. *
  671. * @v endpoint Endpoint
  672. */
  673. static void usbio_interrupt_close ( struct usbio_endpoint *endpoint ) {
  674. struct usb_endpoint *ep = endpoint->ep;
  675. EFI_USB_IO_PROTOCOL *io = endpoint->io;
  676. /* Remove from periodic schedule */
  677. io->UsbAsyncInterruptTransfer ( io, ep->address, FALSE, 0, 0,
  678. NULL, NULL );
  679. /* Free interrupt ring buffer */
  680. free ( endpoint->intr );
  681. }
  682. /**
  683. * Poll interrupt endpoint
  684. *
  685. * @v endpoint Endpoint
  686. */
  687. static void usbio_interrupt_poll ( struct usbio_endpoint *endpoint ) {
  688. struct usbio_interrupt_ring *intr = endpoint->intr;
  689. struct usb_endpoint *ep = endpoint->ep;
  690. struct io_buffer *iobuf;
  691. unsigned int index;
  692. unsigned int intr_index;
  693. size_t len;
  694. /* Do nothing if ring is empty */
  695. if ( endpoint->cons == endpoint->prod )
  696. return;
  697. /* Do nothing if interrupt ring is empty */
  698. if ( intr->cons == intr->prod )
  699. return;
  700. /* Consume next transfer */
  701. index = ( endpoint->cons++ % USBIO_RING_COUNT );
  702. iobuf = endpoint->iobuf[index];
  703. /* Populate I/O buffer */
  704. intr_index = ( intr->cons++ % USBIO_INTR_COUNT );
  705. len = intr->len[intr_index];
  706. assert ( len <= iob_len ( iobuf ) );
  707. iob_put ( iobuf, ( len - iob_len ( iobuf ) ) );
  708. memcpy ( iobuf->data, intr->data[intr_index], len );
  709. /* Complete transfer */
  710. usb_complete ( ep, iobuf );
  711. }
  712. /** Interrupt endpoint operations */
  713. static struct usbio_operations usbio_interrupt_operations = {
  714. .open = usbio_interrupt_open,
  715. .close = usbio_interrupt_close,
  716. .poll = usbio_interrupt_poll,
  717. };
  718. /******************************************************************************
  719. *
  720. * Endpoint operations
  721. *
  722. ******************************************************************************
  723. */
  724. /**
  725. * Open endpoint
  726. *
  727. * @v ep USB endpoint
  728. * @ret rc Return status code
  729. */
  730. static int usbio_endpoint_open ( struct usb_endpoint *ep ) {
  731. struct usb_bus *bus = ep->usb->port->hub->bus;
  732. struct usbio_device *usbio = usb_bus_get_hostdata ( bus );
  733. struct usbio_endpoint *endpoint;
  734. EFI_HANDLE handle = usbio->handle;
  735. unsigned int attr = ( ep->attributes & USB_ENDPOINT_ATTR_TYPE_MASK );
  736. int interface;
  737. int rc;
  738. /* Allocate and initialise structure */
  739. endpoint = zalloc ( sizeof ( *endpoint ) );
  740. if ( ! endpoint ) {
  741. rc = -ENOMEM;
  742. goto err_alloc;
  743. }
  744. usb_endpoint_set_hostdata ( ep, endpoint );
  745. endpoint->usbio = usbio;
  746. endpoint->ep = ep;
  747. /* Identify endpoint operations */
  748. if ( attr == USB_ENDPOINT_ATTR_CONTROL ) {
  749. endpoint->op = &usbio_control_operations;
  750. } else if ( attr == USB_ENDPOINT_ATTR_BULK ) {
  751. endpoint->op = ( ( ep->address & USB_DIR_IN ) ?
  752. &usbio_bulk_in_operations :
  753. &usbio_bulk_out_operations );
  754. } else if ( attr == USB_ENDPOINT_ATTR_INTERRUPT ) {
  755. endpoint->op = &usbio_interrupt_operations;
  756. } else {
  757. rc = -ENOTSUP;
  758. goto err_operations;
  759. }
  760. /* Identify interface for this endpoint */
  761. interface = usbio_interface ( usbio, ep );
  762. if ( interface < 0 ) {
  763. rc = interface;
  764. goto err_interface;
  765. }
  766. endpoint->interface = interface;
  767. /* Open interface */
  768. if ( ( rc = usbio_open ( usbio, interface ) ) != 0 )
  769. goto err_open_interface;
  770. endpoint->handle = usbio->interface[interface].handle;
  771. endpoint->io = usbio->interface[interface].io;
  772. DBGC ( usbio, "USBIO %s %s using ",
  773. efi_handle_name ( handle ), usb_endpoint_name ( ep ) );
  774. DBGC ( usbio, "%s\n", efi_handle_name ( endpoint->handle ) );
  775. /* Open endpoint */
  776. if ( ( rc = endpoint->op->open ( endpoint ) ) != 0 )
  777. goto err_open_endpoint;
  778. /* Add to list of endpoints */
  779. list_add_tail ( &endpoint->list, &usbio->endpoints );
  780. return 0;
  781. list_del ( &endpoint->list );
  782. endpoint->op->close ( endpoint );
  783. err_open_endpoint:
  784. usbio_close ( usbio, interface );
  785. err_open_interface:
  786. err_interface:
  787. err_operations:
  788. free ( endpoint );
  789. err_alloc:
  790. return rc;
  791. }
  792. /**
  793. * Close endpoint
  794. *
  795. * @v ep USB endpoint
  796. */
  797. static void usbio_endpoint_close ( struct usb_endpoint *ep ) {
  798. struct usbio_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
  799. struct usbio_device *usbio = endpoint->usbio;
  800. struct io_buffer *iobuf;
  801. unsigned int index;
  802. /* Remove from list of endpoints */
  803. list_del ( &endpoint->list );
  804. /* Close endpoint */
  805. endpoint->op->close ( endpoint );
  806. /* Close interface */
  807. usbio_close ( usbio, endpoint->interface );
  808. /* Cancel any incomplete transfers */
  809. while ( endpoint->cons != endpoint->prod ) {
  810. index = ( endpoint->cons++ % USBIO_RING_COUNT );
  811. iobuf = endpoint->iobuf[index];
  812. usb_complete_err ( ep, iobuf, -ECANCELED );
  813. }
  814. /* Free endpoint */
  815. free ( endpoint );
  816. }
  817. /**
  818. * Reset endpoint
  819. *
  820. * @v ep USB endpoint
  821. * @ret rc Return status code
  822. */
  823. static int usbio_endpoint_reset ( struct usb_endpoint *ep __unused ) {
  824. /* Nothing to do */
  825. return 0;
  826. }
  827. /**
  828. * Update MTU
  829. *
  830. * @v ep USB endpoint
  831. * @ret rc Return status code
  832. */
  833. static int usbio_endpoint_mtu ( struct usb_endpoint *ep __unused ) {
  834. /* Nothing to do */
  835. return 0;
  836. }
  837. /**
  838. * Enqueue transfer
  839. *
  840. * @v ep USB endpoint
  841. * @v iobuf I/O buffer
  842. * @v flags Transfer flags
  843. * @ret rc Return status code
  844. */
  845. static int usbio_endpoint_enqueue ( struct usb_endpoint *ep,
  846. struct io_buffer *iobuf,
  847. unsigned int flags ) {
  848. struct usbio_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
  849. unsigned int fill;
  850. unsigned int index;
  851. /* Fail if transfer ring is full */
  852. fill = ( endpoint->prod - endpoint->cons );
  853. if ( fill >= USBIO_RING_COUNT )
  854. return -ENOBUFS;
  855. /* Add to ring */
  856. index = ( endpoint->prod++ % USBIO_RING_COUNT );
  857. endpoint->iobuf[index] = iobuf;
  858. endpoint->flags[index] = flags;
  859. return 0;
  860. }
  861. /**
  862. * Enqueue message transfer
  863. *
  864. * @v ep USB endpoint
  865. * @v iobuf I/O buffer
  866. * @ret rc Return status code
  867. */
  868. static int usbio_endpoint_message ( struct usb_endpoint *ep,
  869. struct io_buffer *iobuf ) {
  870. /* Enqueue transfer */
  871. return usbio_endpoint_enqueue ( ep, iobuf, USBIO_MESSAGE );
  872. }
  873. /**
  874. * Enqueue stream transfer
  875. *
  876. * @v ep USB endpoint
  877. * @v iobuf I/O buffer
  878. * @v zlp Append a zero-length packet
  879. * @ret rc Return status code
  880. */
  881. static int usbio_endpoint_stream ( struct usb_endpoint *ep,
  882. struct io_buffer *iobuf, int zlp ) {
  883. /* Enqueue transfer */
  884. return usbio_endpoint_enqueue ( ep, iobuf, ( zlp ? USBIO_ZLEN : 0 ) );
  885. }
  886. /**
  887. * Poll for completions
  888. *
  889. * @v endpoint Endpoint
  890. */
  891. static void usbio_endpoint_poll ( struct usbio_endpoint *endpoint ) {
  892. /* Poll endpoint */
  893. endpoint->op->poll ( endpoint );
  894. }
  895. /******************************************************************************
  896. *
  897. * Device operations
  898. *
  899. ******************************************************************************
  900. */
  901. /**
  902. * Open device
  903. *
  904. * @v usb USB device
  905. * @ret rc Return status code
  906. */
  907. static int usbio_device_open ( struct usb_device *usb ) {
  908. struct usbio_device *usbio =
  909. usb_bus_get_hostdata ( usb->port->hub->bus );
  910. usb_set_hostdata ( usb, usbio );
  911. return 0;
  912. }
  913. /**
  914. * Close device
  915. *
  916. * @v usb USB device
  917. */
  918. static void usbio_device_close ( struct usb_device *usb __unused ) {
  919. /* Nothing to do */
  920. }
  921. /**
  922. * Assign device address
  923. *
  924. * @v usb USB device
  925. * @ret rc Return status code
  926. */
  927. static int usbio_device_address ( struct usb_device *usb __unused ) {
  928. /* Nothing to do */
  929. return 0;
  930. }
  931. /******************************************************************************
  932. *
  933. * Hub operations
  934. *
  935. ******************************************************************************
  936. */
  937. /**
  938. * Open hub
  939. *
  940. * @v hub USB hub
  941. * @ret rc Return status code
  942. */
  943. static int usbio_hub_open ( struct usb_hub *hub ) {
  944. /* Disallow non-root hubs */
  945. if ( hub->usb )
  946. return -ENOTSUP;
  947. /* Nothing to do */
  948. return 0;
  949. }
  950. /**
  951. * Close hub
  952. *
  953. * @v hub USB hub
  954. */
  955. static void usbio_hub_close ( struct usb_hub *hub __unused ) {
  956. /* Nothing to do */
  957. }
  958. /******************************************************************************
  959. *
  960. * Root hub operations
  961. *
  962. ******************************************************************************
  963. */
  964. /**
  965. * Open root hub
  966. *
  967. * @v hub USB hub
  968. * @ret rc Return status code
  969. */
  970. static int usbio_root_open ( struct usb_hub *hub __unused ) {
  971. /* Nothing to do */
  972. return 0;
  973. }
  974. /**
  975. * Close root hub
  976. *
  977. * @v hub USB hub
  978. */
  979. static void usbio_root_close ( struct usb_hub *hub __unused ) {
  980. /* Nothing to do */
  981. }
  982. /**
  983. * Enable port
  984. *
  985. * @v hub USB hub
  986. * @v port USB port
  987. * @ret rc Return status code
  988. */
  989. static int usbio_root_enable ( struct usb_hub *hub __unused,
  990. struct usb_port *port __unused ) {
  991. /* Nothing to do */
  992. return 0;
  993. }
  994. /**
  995. * Disable port
  996. *
  997. * @v hub USB hub
  998. * @v port USB port
  999. * @ret rc Return status code
  1000. */
  1001. static int usbio_root_disable ( struct usb_hub *hub __unused,
  1002. struct usb_port *port __unused ) {
  1003. /* Nothing to do */
  1004. return 0;
  1005. }
  1006. /**
  1007. * Update root hub port speed
  1008. *
  1009. * @v hub USB hub
  1010. * @v port USB port
  1011. * @ret rc Return status code
  1012. */
  1013. static int usbio_root_speed ( struct usb_hub *hub __unused,
  1014. struct usb_port *port ) {
  1015. /* Not actually exposed via EFI_USB_IO_PROTOCOL */
  1016. port->speed = USB_SPEED_HIGH;
  1017. return 0;
  1018. }
  1019. /**
  1020. * Clear transaction translator buffer
  1021. *
  1022. * @v hub USB hub
  1023. * @v port USB port
  1024. * @v ep USB endpoint
  1025. * @ret rc Return status code
  1026. */
  1027. static int usbio_root_clear_tt ( struct usb_hub *hub __unused,
  1028. struct usb_port *port __unused,
  1029. struct usb_endpoint *ep __unused ) {
  1030. /* Should never be called; this is a root hub */
  1031. return -ENOTSUP;
  1032. }
  1033. /******************************************************************************
  1034. *
  1035. * Bus operations
  1036. *
  1037. ******************************************************************************
  1038. */
  1039. /**
  1040. * Open USB bus
  1041. *
  1042. * @v bus USB bus
  1043. * @ret rc Return status code
  1044. */
  1045. static int usbio_bus_open ( struct usb_bus *bus __unused ) {
  1046. /* Nothing to do */
  1047. return 0;
  1048. }
  1049. /**
  1050. * Close USB bus
  1051. *
  1052. * @v bus USB bus
  1053. */
  1054. static void usbio_bus_close ( struct usb_bus *bus __unused ) {
  1055. /* Nothing to do */
  1056. }
  1057. /**
  1058. * Poll USB bus
  1059. *
  1060. * @v bus USB bus
  1061. */
  1062. static void usbio_bus_poll ( struct usb_bus *bus ) {
  1063. struct usbio_device *usbio = usb_bus_get_hostdata ( bus );
  1064. struct usbio_endpoint *endpoint;
  1065. /* Poll all endpoints. We trust that completion handlers are
  1066. * minimal and will not do anything that could plausibly
  1067. * affect the endpoint list itself.
  1068. */
  1069. list_for_each_entry ( endpoint, &usbio->endpoints, list )
  1070. usbio_endpoint_poll ( endpoint );
  1071. }
  1072. /******************************************************************************
  1073. *
  1074. * EFI driver interface
  1075. *
  1076. ******************************************************************************
  1077. */
  1078. /** USB I/O host controller driver operations */
  1079. static struct usb_host_operations usbio_operations = {
  1080. .endpoint = {
  1081. .open = usbio_endpoint_open,
  1082. .close = usbio_endpoint_close,
  1083. .reset = usbio_endpoint_reset,
  1084. .mtu = usbio_endpoint_mtu,
  1085. .message = usbio_endpoint_message,
  1086. .stream = usbio_endpoint_stream,
  1087. },
  1088. .device = {
  1089. .open = usbio_device_open,
  1090. .close = usbio_device_close,
  1091. .address = usbio_device_address,
  1092. },
  1093. .bus = {
  1094. .open = usbio_bus_open,
  1095. .close = usbio_bus_close,
  1096. .poll = usbio_bus_poll,
  1097. },
  1098. .hub = {
  1099. .open = usbio_hub_open,
  1100. .close = usbio_hub_close,
  1101. },
  1102. .root = {
  1103. .open = usbio_root_open,
  1104. .close = usbio_root_close,
  1105. .enable = usbio_root_enable,
  1106. .disable = usbio_root_disable,
  1107. .speed = usbio_root_speed,
  1108. .clear_tt = usbio_root_clear_tt,
  1109. },
  1110. };
  1111. /**
  1112. * Check to see if driver supports a device
  1113. *
  1114. * @v handle EFI device handle
  1115. * @ret rc Return status code
  1116. */
  1117. static int usbio_supported ( EFI_HANDLE handle ) {
  1118. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  1119. EFI_USB_DEVICE_DESCRIPTOR device;
  1120. EFI_USB_INTERFACE_DESCRIPTOR interface;
  1121. struct usb_function_descriptor desc;
  1122. struct usb_driver *driver;
  1123. struct usb_device_id *id;
  1124. union {
  1125. void *interface;
  1126. EFI_USB_IO_PROTOCOL *io;
  1127. } usb;
  1128. EFI_STATUS efirc;
  1129. int rc;
  1130. /* Get protocol */
  1131. if ( ( efirc = bs->OpenProtocol ( handle, &efi_usb_io_protocol_guid,
  1132. &usb.interface, efi_image_handle,
  1133. handle,
  1134. EFI_OPEN_PROTOCOL_GET_PROTOCOL ))!=0){
  1135. rc = -EEFI ( efirc );
  1136. DBGCP ( handle, "USB %s is not a USB device\n",
  1137. efi_handle_name ( handle ) );
  1138. goto err_open_protocol;
  1139. }
  1140. /* Get device descriptor */
  1141. if ( ( efirc = usb.io->UsbGetDeviceDescriptor ( usb.io,
  1142. &device ) ) != 0 ) {
  1143. rc = -EEFI ( efirc );
  1144. DBGC ( handle, "USB %s could not get device descriptor: "
  1145. "%s\n", efi_handle_name ( handle ), strerror ( rc ) );
  1146. goto err_get_device_descriptor;
  1147. }
  1148. memset ( &desc, 0, sizeof ( desc ) );
  1149. desc.vendor = device.IdVendor;
  1150. desc.product = device.IdProduct;
  1151. /* Get interface descriptor */
  1152. if ( ( efirc = usb.io->UsbGetInterfaceDescriptor ( usb.io,
  1153. &interface ) ) !=0){
  1154. rc = -EEFI ( efirc );
  1155. DBGC ( handle, "USB %s could not get interface descriptor: "
  1156. "%s\n", efi_handle_name ( handle ), strerror ( rc ) );
  1157. goto err_get_interface_descriptor;
  1158. }
  1159. desc.class.class.class = interface.InterfaceClass;
  1160. desc.class.class.subclass = interface.InterfaceSubClass;
  1161. desc.class.class.protocol = interface.InterfaceProtocol;
  1162. /* Look for a driver for this interface */
  1163. driver = usb_find_driver ( &desc, &id );
  1164. if ( ! driver ) {
  1165. rc = -ENOTSUP;
  1166. goto err_unsupported;
  1167. }
  1168. /* Success */
  1169. rc = 0;
  1170. err_unsupported:
  1171. err_get_interface_descriptor:
  1172. err_get_device_descriptor:
  1173. bs->CloseProtocol ( handle, &efi_usb_io_protocol_guid,
  1174. efi_image_handle, handle );
  1175. err_open_protocol:
  1176. return rc;
  1177. }
  1178. /**
  1179. * Fetch configuration descriptor
  1180. *
  1181. * @v usbio USB I/O device
  1182. * @ret rc Return status code
  1183. */
  1184. static int usbio_config ( struct usbio_device *usbio ) {
  1185. EFI_HANDLE handle = usbio->handle;
  1186. EFI_USB_IO_PROTOCOL *io = usbio->io;
  1187. EFI_USB_DEVICE_DESCRIPTOR device;
  1188. EFI_USB_CONFIG_DESCRIPTOR partial;
  1189. union {
  1190. struct usb_setup_packet setup;
  1191. EFI_USB_DEVICE_REQUEST efi;
  1192. } msg;
  1193. UINT32 status;
  1194. size_t len;
  1195. unsigned int count;
  1196. unsigned int value;
  1197. unsigned int i;
  1198. EFI_STATUS efirc;
  1199. int rc;
  1200. /* Get device descriptor */
  1201. if ( ( efirc = io->UsbGetDeviceDescriptor ( io, &device ) ) != 0 ) {
  1202. rc = -EEFI ( efirc );
  1203. DBGC ( usbio, "USB %s could not get device descriptor: "
  1204. "%s\n", efi_handle_name ( handle ), strerror ( rc ) );
  1205. goto err_get_device_descriptor;
  1206. }
  1207. count = device.NumConfigurations;
  1208. /* Get current partial configuration descriptor */
  1209. if ( ( efirc = io->UsbGetConfigDescriptor ( io, &partial ) ) != 0 ) {
  1210. rc = -EEFI ( efirc );
  1211. DBGC ( usbio, "USB %s could not get partial configuration "
  1212. "descriptor: %s\n", efi_handle_name ( handle ),
  1213. strerror ( rc ) );
  1214. goto err_get_configuration_descriptor;
  1215. }
  1216. len = le16_to_cpu ( partial.TotalLength );
  1217. /* Allocate configuration descriptor */
  1218. usbio->config = malloc ( len );
  1219. if ( ! usbio->config ) {
  1220. rc = -ENOMEM;
  1221. goto err_alloc;
  1222. }
  1223. /* There is, naturally, no way to retrieve the entire device
  1224. * configuration descriptor via EFI_USB_IO_PROTOCOL. Worse,
  1225. * there is no way to even retrieve the index of the current
  1226. * configuration descriptor. We have to iterate over all
  1227. * possible configuration descriptors looking for the
  1228. * descriptor that matches the current configuration value.
  1229. */
  1230. for ( i = 0 ; i < count ; i++ ) {
  1231. /* Construct request */
  1232. msg.setup.request = cpu_to_le16 ( USB_GET_DESCRIPTOR );
  1233. value = ( ( USB_CONFIGURATION_DESCRIPTOR << 8 ) | i );
  1234. msg.setup.value = cpu_to_le16 ( value );
  1235. msg.setup.index = 0;
  1236. msg.setup.len = cpu_to_le16 ( len );
  1237. /* Get full configuration descriptor */
  1238. if ( ( efirc = io->UsbControlTransfer ( io, &msg.efi,
  1239. EfiUsbDataIn, 0,
  1240. usbio->config, len,
  1241. &status ) ) != 0 ) {
  1242. rc = -EEFI ( efirc );
  1243. DBGC ( usbio, "USB %s could not get configuration %d "
  1244. "descriptor: %s\n", efi_handle_name ( handle ),
  1245. i, strerror ( rc ) );
  1246. goto err_control_transfer;
  1247. }
  1248. /* Ignore unless this is the current configuration */
  1249. if ( usbio->config->config != partial.ConfigurationValue )
  1250. continue;
  1251. /* Check length */
  1252. if ( le16_to_cpu ( usbio->config->len ) != len ) {
  1253. DBGC ( usbio, "USB %s configuration descriptor length "
  1254. "mismatch\n", efi_handle_name ( handle ) );
  1255. rc = -EINVAL;
  1256. goto err_len;
  1257. }
  1258. return 0;
  1259. }
  1260. /* No match found */
  1261. DBGC ( usbio, "USB %s could not find current configuration "
  1262. "descriptor\n", efi_handle_name ( handle ) );
  1263. rc = -ENOENT;
  1264. err_len:
  1265. err_control_transfer:
  1266. free ( usbio->config );
  1267. err_alloc:
  1268. err_get_configuration_descriptor:
  1269. err_get_device_descriptor:
  1270. return rc;
  1271. }
  1272. /**
  1273. * Construct device path for opening other interfaces
  1274. *
  1275. * @v usbio USB I/O device
  1276. * @ret rc Return status code
  1277. */
  1278. static int usbio_path ( struct usbio_device *usbio ) {
  1279. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  1280. EFI_HANDLE handle = usbio->handle;
  1281. EFI_DEVICE_PATH_PROTOCOL *path;
  1282. EFI_DEVICE_PATH_PROTOCOL *end;
  1283. USB_DEVICE_PATH *usbpath;
  1284. union {
  1285. void *interface;
  1286. EFI_DEVICE_PATH_PROTOCOL *path;
  1287. } u;
  1288. size_t len;
  1289. EFI_STATUS efirc;
  1290. int rc;
  1291. /* Open device path protocol */
  1292. if ( ( efirc = bs->OpenProtocol ( handle,
  1293. &efi_device_path_protocol_guid,
  1294. &u.interface, efi_image_handle,
  1295. handle,
  1296. EFI_OPEN_PROTOCOL_GET_PROTOCOL ))!=0){
  1297. rc = -EEFI ( efirc );
  1298. DBGC ( usbio, "USBIO %s cannot open device path protocol: "
  1299. "%s\n", efi_handle_name ( handle ), strerror ( rc ) );
  1300. goto err_open_protocol;
  1301. }
  1302. path = u.interface;
  1303. /* Locate end of device path and sanity check */
  1304. len = efi_devpath_len ( path );
  1305. if ( len < sizeof ( *usbpath ) ) {
  1306. DBGC ( usbio, "USBIO %s underlength device path\n",
  1307. efi_handle_name ( handle ) );
  1308. rc = -EINVAL;
  1309. goto err_underlength;
  1310. }
  1311. usbpath = ( ( ( void * ) path ) + len - sizeof ( *usbpath ) );
  1312. if ( ! ( ( usbpath->Header.Type == MESSAGING_DEVICE_PATH ) &&
  1313. ( usbpath->Header.SubType == MSG_USB_DP ) ) ) {
  1314. DBGC ( usbio, "USBIO %s not a USB device path: ",
  1315. efi_handle_name ( handle ) );
  1316. DBGC ( usbio, "%s\n", efi_devpath_text ( path ) );
  1317. rc = -EINVAL;
  1318. goto err_non_usb;
  1319. }
  1320. /* Allocate copy of device path */
  1321. usbio->path = malloc ( len + sizeof ( *end ) );
  1322. if ( ! usbio->path ) {
  1323. rc = -ENOMEM;
  1324. goto err_alloc;
  1325. }
  1326. memcpy ( usbio->path, path, ( len + sizeof ( *end ) ) );
  1327. usbio->usbpath = ( ( ( void * ) usbio->path ) + len -
  1328. sizeof ( *usbpath ) );
  1329. /* Close protocol */
  1330. bs->CloseProtocol ( handle, &efi_device_path_protocol_guid,
  1331. efi_image_handle, handle );
  1332. return 0;
  1333. free ( usbio->path );
  1334. err_alloc:
  1335. err_non_usb:
  1336. err_underlength:
  1337. bs->CloseProtocol ( handle, &efi_device_path_protocol_guid,
  1338. efi_image_handle, handle );
  1339. err_open_protocol:
  1340. return rc;
  1341. }
  1342. /**
  1343. * Construct interface list
  1344. *
  1345. * @v usbio USB I/O device
  1346. * @ret rc Return status code
  1347. */
  1348. static int usbio_interfaces ( struct usbio_device *usbio ) {
  1349. EFI_HANDLE handle = usbio->handle;
  1350. EFI_USB_IO_PROTOCOL *io = usbio->io;
  1351. EFI_USB_INTERFACE_DESCRIPTOR interface;
  1352. unsigned int first;
  1353. unsigned int count;
  1354. EFI_STATUS efirc;
  1355. int rc;
  1356. /* Get interface descriptor */
  1357. if ( ( efirc = io->UsbGetInterfaceDescriptor ( io, &interface ) ) != 0){
  1358. rc = -EEFI ( efirc );
  1359. DBGC ( usbio, "USB %s could not get interface descriptor: "
  1360. "%s\n", efi_handle_name ( handle ), strerror ( rc ) );
  1361. goto err_get_interface_descriptor;
  1362. }
  1363. /* Record first interface number */
  1364. first = interface.InterfaceNumber;
  1365. count = usbio->config->interfaces;
  1366. assert ( first < count );
  1367. usbio->first = first;
  1368. /* Allocate interface list */
  1369. usbio->interface = zalloc ( count * sizeof ( usbio->interface[0] ) );
  1370. if ( ! usbio->interface ) {
  1371. rc = -ENOMEM;
  1372. goto err_alloc;
  1373. }
  1374. /* Use already-opened protocol for control transfers and for
  1375. * the first interface.
  1376. */
  1377. usbio->interface[0].handle = handle;
  1378. usbio->interface[0].io = io;
  1379. usbio->interface[0].count = 1;
  1380. usbio->interface[first].handle = handle;
  1381. usbio->interface[first].io = io;
  1382. usbio->interface[first].count = 1;
  1383. return 0;
  1384. free ( usbio->interface );
  1385. err_alloc:
  1386. err_get_interface_descriptor:
  1387. return rc;
  1388. }
  1389. /**
  1390. * Attach driver to device
  1391. *
  1392. * @v efidev EFI device
  1393. * @ret rc Return status code
  1394. */
  1395. static int usbio_start ( struct efi_device *efidev ) {
  1396. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  1397. EFI_HANDLE handle = efidev->device;
  1398. struct usbio_device *usbio;
  1399. struct usb_port *port;
  1400. union {
  1401. void *interface;
  1402. EFI_USB_IO_PROTOCOL *io;
  1403. } u;
  1404. EFI_STATUS efirc;
  1405. int rc;
  1406. /* Allocate and initialise structure */
  1407. usbio = zalloc ( sizeof ( *usbio ) );
  1408. if ( ! usbio ) {
  1409. rc = -ENOMEM;
  1410. goto err_alloc;
  1411. }
  1412. efidev_set_drvdata ( efidev, usbio );
  1413. usbio->handle = handle;
  1414. INIT_LIST_HEAD ( &usbio->endpoints );
  1415. /* Open USB I/O protocol */
  1416. if ( ( efirc = bs->OpenProtocol ( handle, &efi_usb_io_protocol_guid,
  1417. &u.interface, efi_image_handle,
  1418. handle,
  1419. ( EFI_OPEN_PROTOCOL_BY_DRIVER |
  1420. EFI_OPEN_PROTOCOL_EXCLUSIVE )))!=0){
  1421. rc = -EEFI ( efirc );
  1422. DBGC ( usbio, "USBIO %s cannot open USB I/O protocol: %s\n",
  1423. efi_handle_name ( handle ), strerror ( rc ) );
  1424. DBGC_EFI_OPENERS ( usbio, handle, &efi_usb_io_protocol_guid );
  1425. goto err_open_usbio;
  1426. }
  1427. usbio->io = u.io;
  1428. /* Describe generic device */
  1429. efi_device_info ( handle, "USB", &usbio->dev );
  1430. usbio->dev.parent = &efidev->dev;
  1431. list_add ( &usbio->dev.siblings, &efidev->dev.children );
  1432. INIT_LIST_HEAD ( &usbio->dev.children );
  1433. /* Fetch configuration descriptor */
  1434. if ( ( rc = usbio_config ( usbio ) ) != 0 )
  1435. goto err_config;
  1436. /* Construct device path */
  1437. if ( ( rc = usbio_path ( usbio ) ) != 0 )
  1438. goto err_path;
  1439. /* Construct interface list */
  1440. if ( ( rc = usbio_interfaces ( usbio ) ) != 0 )
  1441. goto err_interfaces;
  1442. /* Allocate USB bus */
  1443. usbio->bus = alloc_usb_bus ( &usbio->dev, 1 /* single "port" */,
  1444. USBIO_MTU, &usbio_operations );
  1445. if ( ! usbio->bus ) {
  1446. rc = -ENOMEM;
  1447. goto err_alloc_bus;
  1448. }
  1449. usb_bus_set_hostdata ( usbio->bus, usbio );
  1450. usb_hub_set_drvdata ( usbio->bus->hub, usbio );
  1451. /* Set port protocol */
  1452. port = usb_port ( usbio->bus->hub, 1 );
  1453. port->protocol = USB_PROTO_2_0;
  1454. /* Register USB bus */
  1455. if ( ( rc = register_usb_bus ( usbio->bus ) ) != 0 )
  1456. goto err_register;
  1457. return 0;
  1458. unregister_usb_bus ( usbio->bus );
  1459. err_register:
  1460. free_usb_bus ( usbio->bus );
  1461. err_alloc_bus:
  1462. free ( usbio->interface );
  1463. err_interfaces:
  1464. free ( usbio->path );
  1465. err_path:
  1466. free ( usbio->config );
  1467. err_config:
  1468. list_del ( &usbio->dev.siblings );
  1469. bs->CloseProtocol ( handle, &efi_usb_io_protocol_guid,
  1470. efi_image_handle, handle );
  1471. err_open_usbio:
  1472. free ( usbio );
  1473. err_alloc:
  1474. return rc;
  1475. }
  1476. /**
  1477. * Detach driver from device
  1478. *
  1479. * @v efidev EFI device
  1480. */
  1481. static void usbio_stop ( struct efi_device *efidev ) {
  1482. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  1483. EFI_HANDLE handle = efidev->device;
  1484. struct usbio_device *usbio = efidev_get_drvdata ( efidev );
  1485. unregister_usb_bus ( usbio->bus );
  1486. free_usb_bus ( usbio->bus );
  1487. free ( usbio->interface );
  1488. free ( usbio->path );
  1489. free ( usbio->config );
  1490. list_del ( &usbio->dev.siblings );
  1491. bs->CloseProtocol ( handle, &efi_usb_io_protocol_guid,
  1492. efi_image_handle, handle );
  1493. free ( usbio );
  1494. }
  1495. /** EFI USB I/O driver */
  1496. struct efi_driver usbio_driver __efi_driver ( EFI_DRIVER_NORMAL ) = {
  1497. .name = "USBIO",
  1498. .supported = usbio_supported,
  1499. .start = usbio_start,
  1500. .stop = usbio_stop,
  1501. };