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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718
  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->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_class class;
  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. unsigned int vendor;
  1129. unsigned int product;
  1130. EFI_STATUS efirc;
  1131. int rc;
  1132. /* Get protocol */
  1133. if ( ( efirc = bs->OpenProtocol ( handle, &efi_usb_io_protocol_guid,
  1134. &usb.interface, efi_image_handle,
  1135. handle,
  1136. EFI_OPEN_PROTOCOL_GET_PROTOCOL ))!=0){
  1137. rc = -EEFI ( efirc );
  1138. DBGCP ( handle, "USB %s is not a USB device\n",
  1139. efi_handle_name ( handle ) );
  1140. goto err_open_protocol;
  1141. }
  1142. /* Get device descriptor */
  1143. if ( ( efirc = usb.io->UsbGetDeviceDescriptor ( usb.io,
  1144. &device ) ) != 0 ) {
  1145. rc = -EEFI ( efirc );
  1146. DBGC ( handle, "USB %s could not get device descriptor: "
  1147. "%s\n", efi_handle_name ( handle ), strerror ( rc ) );
  1148. goto err_get_device_descriptor;
  1149. }
  1150. vendor = device.IdVendor;
  1151. product = device.IdProduct;
  1152. /* Get interface descriptor */
  1153. if ( ( efirc = usb.io->UsbGetInterfaceDescriptor ( usb.io,
  1154. &interface ) ) !=0){
  1155. rc = -EEFI ( efirc );
  1156. DBGC ( handle, "USB %s could not get interface descriptor: "
  1157. "%s\n", efi_handle_name ( handle ), strerror ( rc ) );
  1158. goto err_get_interface_descriptor;
  1159. }
  1160. class.class = interface.InterfaceClass;
  1161. class.subclass = interface.InterfaceSubClass;
  1162. class.protocol = interface.InterfaceProtocol;
  1163. /* Look for a driver for this interface */
  1164. driver = usb_find_driver ( vendor, product, &class, &id );
  1165. if ( ! driver ) {
  1166. rc = -ENOTSUP;
  1167. goto err_unsupported;
  1168. }
  1169. /* Success */
  1170. rc = 0;
  1171. err_unsupported:
  1172. err_get_interface_descriptor:
  1173. err_get_device_descriptor:
  1174. bs->CloseProtocol ( handle, &efi_usb_io_protocol_guid,
  1175. efi_image_handle, handle );
  1176. err_open_protocol:
  1177. return rc;
  1178. }
  1179. /**
  1180. * Fetch configuration descriptor
  1181. *
  1182. * @v usbio USB I/O device
  1183. * @ret rc Return status code
  1184. */
  1185. static int usbio_config ( struct usbio_device *usbio ) {
  1186. EFI_HANDLE handle = usbio->handle;
  1187. EFI_USB_IO_PROTOCOL *io = usbio->io;
  1188. EFI_USB_DEVICE_DESCRIPTOR device;
  1189. EFI_USB_CONFIG_DESCRIPTOR partial;
  1190. union {
  1191. struct usb_setup_packet setup;
  1192. EFI_USB_DEVICE_REQUEST efi;
  1193. } msg;
  1194. UINT32 status;
  1195. size_t len;
  1196. unsigned int count;
  1197. unsigned int value;
  1198. unsigned int i;
  1199. EFI_STATUS efirc;
  1200. int rc;
  1201. /* Get device descriptor */
  1202. if ( ( efirc = io->UsbGetDeviceDescriptor ( io, &device ) ) != 0 ) {
  1203. rc = -EEFI ( efirc );
  1204. DBGC ( usbio, "USB %s could not get device descriptor: "
  1205. "%s\n", efi_handle_name ( handle ), strerror ( rc ) );
  1206. goto err_get_device_descriptor;
  1207. }
  1208. count = device.NumConfigurations;
  1209. /* Get current partial configuration descriptor */
  1210. if ( ( efirc = io->UsbGetConfigDescriptor ( io, &partial ) ) != 0 ) {
  1211. rc = -EEFI ( efirc );
  1212. DBGC ( usbio, "USB %s could not get partial configuration "
  1213. "descriptor: %s\n", efi_handle_name ( handle ),
  1214. strerror ( rc ) );
  1215. goto err_get_configuration_descriptor;
  1216. }
  1217. len = le16_to_cpu ( partial.TotalLength );
  1218. /* Allocate configuration descriptor */
  1219. usbio->config = malloc ( len );
  1220. if ( ! usbio->config ) {
  1221. rc = -ENOMEM;
  1222. goto err_alloc;
  1223. }
  1224. /* There is, naturally, no way to retrieve the entire device
  1225. * configuration descriptor via EFI_USB_IO_PROTOCOL. Worse,
  1226. * there is no way to even retrieve the index of the current
  1227. * configuration descriptor. We have to iterate over all
  1228. * possible configuration descriptors looking for the
  1229. * descriptor that matches the current configuration value.
  1230. */
  1231. for ( i = 0 ; i < count ; i++ ) {
  1232. /* Construct request */
  1233. msg.setup.request = cpu_to_le16 ( USB_GET_DESCRIPTOR );
  1234. value = ( ( USB_CONFIGURATION_DESCRIPTOR << 8 ) | i );
  1235. msg.setup.value = cpu_to_le16 ( value );
  1236. msg.setup.index = 0;
  1237. msg.setup.len = cpu_to_le16 ( len );
  1238. /* Get full configuration descriptor */
  1239. if ( ( efirc = io->UsbControlTransfer ( io, &msg.efi,
  1240. EfiUsbDataIn, 0,
  1241. usbio->config, len,
  1242. &status ) ) != 0 ) {
  1243. rc = -EEFI ( efirc );
  1244. DBGC ( usbio, "USB %s could not get configuration %d "
  1245. "descriptor: %s\n", efi_handle_name ( handle ),
  1246. i, strerror ( rc ) );
  1247. goto err_control_transfer;
  1248. }
  1249. /* Ignore unless this is the current configuration */
  1250. if ( usbio->config->config != partial.ConfigurationValue )
  1251. continue;
  1252. /* Check length */
  1253. if ( le16_to_cpu ( usbio->config->len ) != len ) {
  1254. DBGC ( usbio, "USB %s configuration descriptor length "
  1255. "mismatch\n", efi_handle_name ( handle ) );
  1256. rc = -EINVAL;
  1257. goto err_len;
  1258. }
  1259. return 0;
  1260. }
  1261. /* No match found */
  1262. DBGC ( usbio, "USB %s could not find current configuration "
  1263. "descriptor\n", efi_handle_name ( handle ) );
  1264. rc = -ENOENT;
  1265. err_len:
  1266. err_control_transfer:
  1267. free ( usbio->config );
  1268. err_alloc:
  1269. err_get_configuration_descriptor:
  1270. err_get_device_descriptor:
  1271. return rc;
  1272. }
  1273. /**
  1274. * Construct device path for opening other interfaces
  1275. *
  1276. * @v usbio USB I/O device
  1277. * @ret rc Return status code
  1278. */
  1279. static int usbio_path ( struct usbio_device *usbio ) {
  1280. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  1281. EFI_HANDLE handle = usbio->handle;
  1282. EFI_DEVICE_PATH_PROTOCOL *path;
  1283. EFI_DEVICE_PATH_PROTOCOL *end;
  1284. USB_DEVICE_PATH *usbpath;
  1285. union {
  1286. void *interface;
  1287. EFI_DEVICE_PATH_PROTOCOL *path;
  1288. } u;
  1289. size_t len;
  1290. EFI_STATUS efirc;
  1291. int rc;
  1292. /* Open device path protocol */
  1293. if ( ( efirc = bs->OpenProtocol ( handle,
  1294. &efi_device_path_protocol_guid,
  1295. &u.interface, efi_image_handle,
  1296. handle,
  1297. EFI_OPEN_PROTOCOL_GET_PROTOCOL ))!=0){
  1298. rc = -EEFI ( efirc );
  1299. DBGC ( usbio, "USBIO %s cannot open device path protocol: "
  1300. "%s\n", efi_handle_name ( handle ), strerror ( rc ) );
  1301. goto err_open_protocol;
  1302. }
  1303. path = u.interface;
  1304. /* Locate end of device path and sanity check */
  1305. len = efi_devpath_len ( path );
  1306. if ( len < sizeof ( *usbpath ) ) {
  1307. DBGC ( usbio, "USBIO %s underlength device path\n",
  1308. efi_handle_name ( handle ) );
  1309. rc = -EINVAL;
  1310. goto err_underlength;
  1311. }
  1312. usbpath = ( ( ( void * ) path ) + len - sizeof ( *usbpath ) );
  1313. if ( ! ( ( usbpath->Header.Type == MESSAGING_DEVICE_PATH ) &&
  1314. ( usbpath->Header.SubType == MSG_USB_DP ) ) ) {
  1315. DBGC ( usbio, "USBIO %s not a USB device path: ",
  1316. efi_handle_name ( handle ) );
  1317. DBGC ( usbio, "%s\n", efi_devpath_text ( path ) );
  1318. rc = -EINVAL;
  1319. goto err_non_usb;
  1320. }
  1321. /* Allocate copy of device path */
  1322. usbio->path = malloc ( len + sizeof ( *end ) );
  1323. if ( ! usbio->path ) {
  1324. rc = -ENOMEM;
  1325. goto err_alloc;
  1326. }
  1327. memcpy ( usbio->path, path, ( len + sizeof ( *end ) ) );
  1328. usbio->usbpath = ( ( ( void * ) usbio->path ) + len -
  1329. sizeof ( *usbpath ) );
  1330. /* Close protocol */
  1331. bs->CloseProtocol ( handle, &efi_device_path_protocol_guid,
  1332. efi_image_handle, handle );
  1333. return 0;
  1334. free ( usbio->path );
  1335. err_alloc:
  1336. err_non_usb:
  1337. err_underlength:
  1338. bs->CloseProtocol ( handle, &efi_device_path_protocol_guid,
  1339. efi_image_handle, handle );
  1340. err_open_protocol:
  1341. return rc;
  1342. }
  1343. /**
  1344. * Construct interface list
  1345. *
  1346. * @v usbio USB I/O device
  1347. * @ret rc Return status code
  1348. */
  1349. static int usbio_interfaces ( struct usbio_device *usbio ) {
  1350. EFI_HANDLE handle = usbio->handle;
  1351. EFI_USB_IO_PROTOCOL *io = usbio->io;
  1352. EFI_USB_INTERFACE_DESCRIPTOR interface;
  1353. unsigned int first;
  1354. unsigned int count;
  1355. EFI_STATUS efirc;
  1356. int rc;
  1357. /* Get interface descriptor */
  1358. if ( ( efirc = io->UsbGetInterfaceDescriptor ( io, &interface ) ) != 0){
  1359. rc = -EEFI ( efirc );
  1360. DBGC ( usbio, "USB %s could not get interface descriptor: "
  1361. "%s\n", efi_handle_name ( handle ), strerror ( rc ) );
  1362. goto err_get_interface_descriptor;
  1363. }
  1364. /* Record first interface number */
  1365. first = interface.InterfaceNumber;
  1366. count = usbio->config->interfaces;
  1367. assert ( first < count );
  1368. usbio->first = first;
  1369. /* Allocate interface list */
  1370. usbio->interface = zalloc ( count * sizeof ( usbio->interface[0] ) );
  1371. if ( ! usbio->interface ) {
  1372. rc = -ENOMEM;
  1373. goto err_alloc;
  1374. }
  1375. /* Use already-opened protocol for control transfers and for
  1376. * the first interface.
  1377. */
  1378. usbio->interface[0].handle = handle;
  1379. usbio->interface[0].io = io;
  1380. usbio->interface[0].count = 1;
  1381. usbio->interface[first].handle = handle;
  1382. usbio->interface[first].io = io;
  1383. usbio->interface[first].count = 1;
  1384. return 0;
  1385. free ( usbio->interface );
  1386. err_alloc:
  1387. err_get_interface_descriptor:
  1388. return rc;
  1389. }
  1390. /**
  1391. * Attach driver to device
  1392. *
  1393. * @v efidev EFI device
  1394. * @ret rc Return status code
  1395. */
  1396. static int usbio_start ( struct efi_device *efidev ) {
  1397. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  1398. EFI_HANDLE handle = efidev->device;
  1399. struct usbio_device *usbio;
  1400. struct usb_port *port;
  1401. union {
  1402. void *interface;
  1403. EFI_USB_IO_PROTOCOL *io;
  1404. } u;
  1405. EFI_STATUS efirc;
  1406. int rc;
  1407. /* Allocate and initialise structure */
  1408. usbio = zalloc ( sizeof ( *usbio ) );
  1409. if ( ! usbio ) {
  1410. rc = -ENOMEM;
  1411. goto err_alloc;
  1412. }
  1413. efidev_set_drvdata ( efidev, usbio );
  1414. usbio->handle = handle;
  1415. INIT_LIST_HEAD ( &usbio->endpoints );
  1416. /* Open USB I/O protocol */
  1417. if ( ( efirc = bs->OpenProtocol ( handle, &efi_usb_io_protocol_guid,
  1418. &u.interface, efi_image_handle,
  1419. handle,
  1420. ( EFI_OPEN_PROTOCOL_BY_DRIVER |
  1421. EFI_OPEN_PROTOCOL_EXCLUSIVE )))!=0){
  1422. rc = -EEFI ( efirc );
  1423. DBGC ( usbio, "USBIO %s cannot open USB I/O protocol: %s\n",
  1424. efi_handle_name ( handle ), strerror ( rc ) );
  1425. DBGC_EFI_OPENERS ( usbio, handle, &efi_usb_io_protocol_guid );
  1426. goto err_open_usbio;
  1427. }
  1428. usbio->io = u.io;
  1429. /* Describe generic device */
  1430. efi_device_info ( handle, "USB", &usbio->dev );
  1431. usbio->dev.parent = &efidev->dev;
  1432. list_add ( &usbio->dev.siblings, &efidev->dev.children );
  1433. INIT_LIST_HEAD ( &usbio->dev.children );
  1434. /* Fetch configuration descriptor */
  1435. if ( ( rc = usbio_config ( usbio ) ) != 0 )
  1436. goto err_config;
  1437. /* Construct device path */
  1438. if ( ( rc = usbio_path ( usbio ) ) != 0 )
  1439. goto err_path;
  1440. /* Construct interface list */
  1441. if ( ( rc = usbio_interfaces ( usbio ) ) != 0 )
  1442. goto err_interfaces;
  1443. /* Allocate USB bus */
  1444. usbio->bus = alloc_usb_bus ( &usbio->dev, 1 /* single "port" */,
  1445. USBIO_MTU, &usbio_operations );
  1446. if ( ! usbio->bus ) {
  1447. rc = -ENOMEM;
  1448. goto err_alloc_bus;
  1449. }
  1450. usb_bus_set_hostdata ( usbio->bus, usbio );
  1451. usb_hub_set_drvdata ( usbio->bus->hub, usbio );
  1452. /* Set port protocol */
  1453. port = usb_port ( usbio->bus->hub, 1 );
  1454. port->protocol = USB_PROTO_2_0;
  1455. /* Register USB bus */
  1456. if ( ( rc = register_usb_bus ( usbio->bus ) ) != 0 )
  1457. goto err_register;
  1458. return 0;
  1459. unregister_usb_bus ( usbio->bus );
  1460. err_register:
  1461. free_usb_bus ( usbio->bus );
  1462. err_alloc_bus:
  1463. free ( usbio->interface );
  1464. err_interfaces:
  1465. free ( usbio->path );
  1466. err_path:
  1467. free ( usbio->config );
  1468. err_config:
  1469. list_del ( &usbio->dev.siblings );
  1470. bs->CloseProtocol ( handle, &efi_usb_io_protocol_guid,
  1471. efi_image_handle, handle );
  1472. err_open_usbio:
  1473. free ( usbio );
  1474. err_alloc:
  1475. return rc;
  1476. }
  1477. /**
  1478. * Detach driver from device
  1479. *
  1480. * @v efidev EFI device
  1481. */
  1482. static void usbio_stop ( struct efi_device *efidev ) {
  1483. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  1484. EFI_HANDLE handle = efidev->device;
  1485. struct usbio_device *usbio = efidev_get_drvdata ( efidev );
  1486. unregister_usb_bus ( usbio->bus );
  1487. free_usb_bus ( usbio->bus );
  1488. free ( usbio->interface );
  1489. free ( usbio->path );
  1490. free ( usbio->config );
  1491. list_del ( &usbio->dev.siblings );
  1492. bs->CloseProtocol ( handle, &efi_usb_io_protocol_guid,
  1493. efi_image_handle, handle );
  1494. free ( usbio );
  1495. }
  1496. /** EFI USB I/O driver */
  1497. struct efi_driver usbio_driver __efi_driver ( EFI_DRIVER_NORMAL ) = {
  1498. .name = "USBIO",
  1499. .supported = usbio_supported,
  1500. .start = usbio_start,
  1501. .stop = usbio_stop,
  1502. };