You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

usbio.c 45KB

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