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.

usb.c 41KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716
  1. /*
  2. * Copyright (C) 2014 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. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <unistd.h>
  24. #include <errno.h>
  25. #include <assert.h>
  26. #include <byteswap.h>
  27. #include <ipxe/usb.h>
  28. /** @file
  29. *
  30. * Universal Serial Bus (USB)
  31. *
  32. */
  33. /******************************************************************************
  34. *
  35. * Utility functions
  36. *
  37. ******************************************************************************
  38. */
  39. /**
  40. * Get USB endpoint name (for debugging)
  41. *
  42. * @v address Endpoint address
  43. * @ret name Endpoint name
  44. */
  45. static inline const char * usb_endpoint_name ( unsigned int address ) {
  46. static char buf[ 9 /* "EPxx OUT" + NUL */ ];
  47. snprintf ( buf, sizeof ( buf ), "EP%d%s",
  48. ( address & USB_ENDPOINT_MAX ),
  49. ( address ?
  50. ( ( address & USB_ENDPOINT_IN ) ? " IN" : " OUT" ) : "" ));
  51. return buf;
  52. }
  53. /**
  54. * Get USB speed name (for debugging)
  55. *
  56. * @v speed Speed
  57. * @ret name Speed name
  58. */
  59. static inline const char * usb_speed_name ( unsigned int speed ) {
  60. static const char *exponents[4] = { "", "k", "M", "G" };
  61. static char buf[ 10 /* "xxxxxXbps" + NUL */ ];
  62. unsigned int mantissa;
  63. unsigned int exponent;
  64. /* Extract mantissa and exponent */
  65. mantissa = USB_SPEED_MANTISSA ( speed );
  66. exponent = USB_SPEED_EXPONENT ( speed );
  67. /* Name speed */
  68. switch ( speed ) {
  69. case USB_SPEED_NONE: return "DETACHED";
  70. case USB_SPEED_LOW: return "low";
  71. case USB_SPEED_FULL: return "full";
  72. case USB_SPEED_HIGH: return "high";
  73. case USB_SPEED_SUPER: return "super";
  74. default:
  75. snprintf ( buf, sizeof ( buf ), "%d%sbps",
  76. mantissa, exponents[exponent] );
  77. return buf;
  78. }
  79. }
  80. /**
  81. * Transcribe USB BCD-coded value (for debugging)
  82. *
  83. * @v bcd BCD-coded value
  84. * @ret string Transcribed value
  85. */
  86. static inline const char * usb_bcd ( uint16_t bcd ) {
  87. static char buf[ 6 /* "xx.xx" + NUL */ ];
  88. uint8_t high = ( bcd >> 8 );
  89. uint8_t low = ( bcd >> 0 );
  90. snprintf ( buf, sizeof ( buf ), "%x.%02x", high, low );
  91. return buf;
  92. }
  93. /******************************************************************************
  94. *
  95. * USB descriptors
  96. *
  97. ******************************************************************************
  98. */
  99. /**
  100. * Locate USB interface association descriptor
  101. *
  102. * @v config Configuraton descriptor
  103. * @v first First interface number
  104. * @ret desc Interface association descriptor, or NULL if not found
  105. */
  106. static struct usb_interface_association_descriptor *
  107. usb_interface_association_descriptor ( struct usb_configuration_descriptor
  108. *config,
  109. unsigned int first ) {
  110. struct usb_interface_association_descriptor *desc;
  111. /* Find a matching interface association descriptor */
  112. for_each_config_descriptor ( desc, config ) {
  113. if ( ( desc->header.type ==
  114. USB_INTERFACE_ASSOCIATION_DESCRIPTOR ) &&
  115. ( desc->first == first ) )
  116. return desc;
  117. }
  118. return NULL;
  119. }
  120. /**
  121. * Locate USB interface descriptor
  122. *
  123. * @v config Configuraton descriptor
  124. * @v interface Interface number
  125. * @v alternate Alternate setting
  126. * @ret desc Interface descriptor, or NULL if not found
  127. */
  128. struct usb_interface_descriptor *
  129. usb_interface_descriptor ( struct usb_configuration_descriptor *config,
  130. unsigned int interface, unsigned int alternate ) {
  131. struct usb_interface_descriptor *desc;
  132. /* Find a matching interface descriptor */
  133. for_each_config_descriptor ( desc, config ) {
  134. if ( ( desc->header.type == USB_INTERFACE_DESCRIPTOR ) &&
  135. ( desc->interface == interface ) &&
  136. ( desc->alternate == alternate ) )
  137. return desc;
  138. }
  139. return NULL;
  140. }
  141. /**
  142. * Locate USB endpoint descriptor
  143. *
  144. * @v config Configuration descriptor
  145. * @v interface Interface descriptor
  146. * @v type Endpoint (internal) type
  147. * @v index Endpoint index
  148. * @ret desc Descriptor, or NULL if not found
  149. */
  150. struct usb_endpoint_descriptor *
  151. usb_endpoint_descriptor ( struct usb_configuration_descriptor *config,
  152. struct usb_interface_descriptor *interface,
  153. unsigned int type, unsigned int index ) {
  154. struct usb_endpoint_descriptor *desc;
  155. unsigned int attributes = ( type & USB_ENDPOINT_ATTR_TYPE_MASK );
  156. unsigned int direction = ( type & USB_DIR_IN );
  157. /* Find a matching endpoint descriptor */
  158. for_each_interface_descriptor ( desc, config, interface ) {
  159. if ( ( desc->header.type == USB_ENDPOINT_DESCRIPTOR ) &&
  160. ( ( desc->attributes &
  161. USB_ENDPOINT_ATTR_TYPE_MASK ) == attributes ) &&
  162. ( ( desc->endpoint & USB_DIR_IN ) == direction ) &&
  163. ( index-- == 0 ) )
  164. return desc;
  165. }
  166. return NULL;
  167. }
  168. /**
  169. * Locate USB endpoint companion descriptor
  170. *
  171. * @v config Configuration descriptor
  172. * @v desc Endpoint descriptor
  173. * @ret descx Companion descriptor, or NULL if not found
  174. */
  175. struct usb_endpoint_companion_descriptor *
  176. usb_endpoint_companion_descriptor ( struct usb_configuration_descriptor *config,
  177. struct usb_endpoint_descriptor *desc ) {
  178. struct usb_endpoint_companion_descriptor *descx;
  179. /* Get companion descriptor, if present */
  180. descx = container_of ( usb_next_descriptor ( &desc->header ),
  181. struct usb_endpoint_companion_descriptor,
  182. header );
  183. return ( ( usb_is_within_config ( config, &descx->header ) &&
  184. descx->header.type == USB_ENDPOINT_COMPANION_DESCRIPTOR )
  185. ? descx : NULL );
  186. }
  187. /******************************************************************************
  188. *
  189. * USB endpoint
  190. *
  191. ******************************************************************************
  192. */
  193. /**
  194. * Describe USB endpoint from device configuration
  195. *
  196. * @v ep USB endpoint
  197. * @v config Configuration descriptor
  198. * @v interface Interface descriptor
  199. * @v type Endpoint (internal) type
  200. * @v index Endpoint index
  201. * @ret rc Return status code
  202. */
  203. int usb_endpoint_described ( struct usb_endpoint *ep,
  204. struct usb_configuration_descriptor *config,
  205. struct usb_interface_descriptor *interface,
  206. unsigned int type, unsigned int index ) {
  207. struct usb_endpoint_descriptor *desc;
  208. struct usb_endpoint_companion_descriptor *descx;
  209. unsigned int sizes;
  210. unsigned int burst;
  211. size_t mtu;
  212. /* Locate endpoint descriptor */
  213. desc = usb_endpoint_descriptor ( config, interface, type, index );
  214. if ( ! desc )
  215. return -ENOENT;
  216. /* Locate companion descriptor, if any */
  217. descx = usb_endpoint_companion_descriptor ( config, desc );
  218. /* Calculate MTU and burst size */
  219. sizes = le16_to_cpu ( desc->sizes );
  220. mtu = USB_ENDPOINT_MTU ( sizes );
  221. burst = ( descx ? descx->burst : USB_ENDPOINT_BURST ( sizes ) );
  222. /* Describe endpoint */
  223. usb_endpoint_describe ( ep, desc->endpoint, desc->attributes,
  224. mtu, burst );
  225. return 0;
  226. }
  227. /**
  228. * Open USB endpoint
  229. *
  230. * @v ep USB endpoint
  231. * @ret rc Return status code
  232. */
  233. int usb_endpoint_open ( struct usb_endpoint *ep ) {
  234. struct usb_device *usb = ep->usb;
  235. unsigned int idx = USB_ENDPOINT_IDX ( ep->address );
  236. int rc;
  237. /* Populate host controller operations */
  238. ep->host = &usb->port->hub->bus->op->endpoint;
  239. /* Add to endpoint list */
  240. if ( usb->ep[idx] != NULL ) {
  241. DBGC ( usb, "USB %s %s is already open\n",
  242. usb->name, usb_endpoint_name ( ep->address ) );
  243. rc = -EALREADY;
  244. goto err_already;
  245. }
  246. usb->ep[idx] = ep;
  247. /* Clear any stale error status */
  248. ep->rc = 0;
  249. /* Open endpoint */
  250. if ( ( rc = ep->host->open ( ep ) ) != 0 ) {
  251. DBGC ( usb, "USB %s %s could not open: %s\n", usb->name,
  252. usb_endpoint_name ( ep->address ), strerror ( rc ) );
  253. goto err_open;
  254. }
  255. ep->open = 1;
  256. DBGC2 ( usb, "USB %s %s opened with MTU %zd (burst %d)\n", usb->name,
  257. usb_endpoint_name ( ep->address ), ep->mtu, ep->burst );
  258. return 0;
  259. ep->open = 0;
  260. ep->host->close ( ep );
  261. err_open:
  262. usb->ep[idx] = NULL;
  263. err_already:
  264. return rc;
  265. }
  266. /**
  267. * Close USB endpoint
  268. *
  269. * @v ep USB endpoint
  270. */
  271. void usb_endpoint_close ( struct usb_endpoint *ep ) {
  272. struct usb_device *usb = ep->usb;
  273. unsigned int idx = USB_ENDPOINT_IDX ( ep->address );
  274. /* Sanity checks */
  275. assert ( usb->ep[idx] == ep );
  276. /* Close endpoint */
  277. ep->open = 0;
  278. ep->host->close ( ep );
  279. /* Remove from endpoint list */
  280. usb->ep[idx] = NULL;
  281. }
  282. /**
  283. * Reset USB endpoint
  284. *
  285. * @v ep USB endpoint
  286. * @ret rc Return status code
  287. */
  288. static int usb_endpoint_reset ( struct usb_endpoint *ep ) {
  289. struct usb_device *usb = ep->usb;
  290. unsigned int type;
  291. int rc;
  292. /* Reset endpoint */
  293. if ( ( rc = ep->host->reset ( ep ) ) != 0 ) {
  294. DBGC ( usb, "USB %s %s could not reset: %s\n",
  295. usb->name, usb_endpoint_name ( ep->address ),
  296. strerror ( rc ) );
  297. return rc;
  298. }
  299. /* Clear endpoint halt, if applicable */
  300. type = ( ep->attributes & USB_ENDPOINT_ATTR_TYPE_MASK );
  301. if ( ( type != USB_ENDPOINT_ATTR_CONTROL ) &&
  302. ( ( rc = usb_clear_feature ( usb, USB_RECIP_ENDPOINT,
  303. USB_ENDPOINT_HALT,
  304. ep->address ) ) != 0 ) ) {
  305. DBGC ( usb, "USB %s %s could not clear endpoint halt: %s\n",
  306. usb->name, usb_endpoint_name ( ep->address ),
  307. strerror ( rc ) );
  308. return rc;
  309. }
  310. /* Clear recorded error */
  311. ep->rc = 0;
  312. DBGC ( usb, "USB %s %s reset\n",
  313. usb->name, usb_endpoint_name ( ep->address ) );
  314. return 0;
  315. }
  316. /**
  317. * Update endpoint MTU
  318. *
  319. * @v ep USB endpoint
  320. * @v mtu New MTU
  321. * @ret rc Return status code
  322. */
  323. static int usb_endpoint_mtu ( struct usb_endpoint *ep, size_t mtu ) {
  324. struct usb_device *usb = ep->usb;
  325. int rc;
  326. /* Update MTU */
  327. ep->mtu = mtu;
  328. if ( ( rc = ep->host->mtu ( ep ) ) != 0 ) {
  329. DBGC ( usb, "USB %s %s could not update MTU: %s\n",
  330. usb->name, usb_endpoint_name ( ep->address ),
  331. strerror ( rc ) );
  332. return rc;
  333. }
  334. return 0;
  335. }
  336. /**
  337. * Enqueue USB message transfer
  338. *
  339. * @v ep USB endpoint
  340. * @v request Request
  341. * @v value Value parameter
  342. * @v index Index parameter
  343. * @v iobuf I/O buffer
  344. * @ret rc Return status code
  345. */
  346. int usb_message ( struct usb_endpoint *ep, unsigned int request,
  347. unsigned int value, unsigned int index,
  348. struct io_buffer *iobuf ) {
  349. struct usb_device *usb = ep->usb;
  350. struct usb_port *port = usb->port;
  351. struct usb_setup_packet packet;
  352. size_t len = iob_len ( iobuf );
  353. int rc;
  354. /* Fail immediately if device has been unplugged */
  355. if ( port->speed == USB_SPEED_NONE )
  356. return -ENODEV;
  357. /* Reset endpoint if required */
  358. if ( ( ep->rc != 0 ) && ( ( rc = usb_endpoint_reset ( ep ) ) != 0 ) )
  359. return rc;
  360. /* Zero input data buffer (if applicable) */
  361. if ( request & USB_DIR_IN )
  362. memset ( iobuf->data, 0, len );
  363. /* Construct setup packet */
  364. packet.request = cpu_to_le16 ( request );
  365. packet.value = cpu_to_le16 ( value );
  366. packet.index = cpu_to_le16 ( index );
  367. packet.len = cpu_to_le16 ( len );
  368. /* Enqueue message transfer */
  369. if ( ( rc = ep->host->message ( ep, &packet, iobuf ) ) != 0 ) {
  370. DBGC ( usb, "USB %s %s could not enqueue message transfer: "
  371. "%s\n", usb->name, usb_endpoint_name ( ep->address ),
  372. strerror ( rc ) );
  373. return rc;
  374. }
  375. return 0;
  376. }
  377. /**
  378. * Enqueue USB stream transfer
  379. *
  380. * @v ep USB endpoint
  381. * @v iobuf I/O buffer
  382. * @ret rc Return status code
  383. */
  384. int usb_stream ( struct usb_endpoint *ep, struct io_buffer *iobuf ) {
  385. struct usb_device *usb = ep->usb;
  386. struct usb_port *port = usb->port;
  387. int rc;
  388. /* Fail immediately if device has been unplugged */
  389. if ( port->speed == USB_SPEED_NONE )
  390. return -ENODEV;
  391. /* Reset endpoint if required */
  392. if ( ( ep->rc != 0 ) && ( ( rc = usb_endpoint_reset ( ep ) ) != 0 ) )
  393. return rc;
  394. /* Enqueue stream transfer */
  395. if ( ( rc = ep->host->stream ( ep, iobuf ) ) != 0 ) {
  396. DBGC ( usb, "USB %s %s could not enqueue stream transfer: %s\n",
  397. usb->name, usb_endpoint_name ( ep->address ),
  398. strerror ( rc ) );
  399. return rc;
  400. }
  401. return 0;
  402. }
  403. /**
  404. * Complete transfer (possibly with error)
  405. *
  406. * @v ep USB endpoint
  407. * @v iobuf I/O buffer
  408. * @v rc Completion status code
  409. */
  410. void usb_complete_err ( struct usb_endpoint *ep, struct io_buffer *iobuf,
  411. int rc ) {
  412. struct usb_device *usb = ep->usb;
  413. /* Record error (if any) */
  414. ep->rc = rc;
  415. if ( ( rc != 0 ) && ep->open ) {
  416. DBGC ( usb, "USB %s %s completion failed: %s\n",
  417. usb->name, usb_endpoint_name ( ep->address ),
  418. strerror ( rc ) );
  419. }
  420. /* Report completion */
  421. ep->driver->complete ( ep, iobuf, rc );
  422. }
  423. /******************************************************************************
  424. *
  425. * Control endpoint
  426. *
  427. ******************************************************************************
  428. */
  429. /**
  430. * Complete USB control transfer
  431. *
  432. * @v ep USB endpoint
  433. * @v iobuf I/O buffer
  434. * @v rc Completion status code
  435. */
  436. static void usb_control_complete ( struct usb_endpoint *ep,
  437. struct io_buffer *iobuf, int rc ) {
  438. struct usb_device *usb = ep->usb;
  439. /* Check for failures */
  440. if ( rc != 0 ) {
  441. DBGC ( usb, "USB %s control transaction failed: %s\n",
  442. usb->name, strerror ( rc ) );
  443. free_iob ( iobuf );
  444. return;
  445. }
  446. /* Add to list of completed I/O buffers */
  447. list_add_tail ( &iobuf->list, &usb->complete );
  448. }
  449. /** USB control endpoint driver operations */
  450. static struct usb_endpoint_driver_operations usb_control_operations = {
  451. .complete = usb_control_complete,
  452. };
  453. /**
  454. * Issue USB control transaction
  455. *
  456. * @v usb USB device
  457. * @v request Request
  458. * @v value Value parameter
  459. * @v index Index parameter
  460. * @v data Data buffer (if any)
  461. * @v len Length of data
  462. * @ret rc Return status code
  463. */
  464. int usb_control ( struct usb_device *usb, unsigned int request,
  465. unsigned int value, unsigned int index, void *data,
  466. size_t len ) {
  467. struct usb_bus *bus = usb->port->hub->bus;
  468. struct usb_endpoint *ep = &usb->control;
  469. struct io_buffer *iobuf;
  470. struct io_buffer *cmplt;
  471. unsigned int i;
  472. int rc;
  473. /* Allocate I/O buffer */
  474. iobuf = alloc_iob ( len );
  475. if ( ! iobuf ) {
  476. rc = -ENOMEM;
  477. goto err_alloc;
  478. }
  479. iob_put ( iobuf, len );
  480. if ( request & USB_DIR_IN ) {
  481. memset ( data, 0, len );
  482. } else {
  483. memcpy ( iobuf->data, data, len );
  484. }
  485. /* Enqueue message */
  486. if ( ( rc = usb_message ( ep, request, value, index, iobuf ) ) != 0 )
  487. goto err_message;
  488. /* Wait for completion */
  489. for ( i = 0 ; i < USB_CONTROL_MAX_WAIT_MS ; i++ ) {
  490. /* Poll bus */
  491. usb_poll ( bus );
  492. /* Check for completion */
  493. while ( ( cmplt = list_first_entry ( &usb->complete,
  494. struct io_buffer,
  495. list ) ) ) {
  496. /* Remove from completion list */
  497. list_del ( &cmplt->list );
  498. /* Discard stale completions */
  499. if ( cmplt != iobuf ) {
  500. DBGC ( usb, "USB %s stale control "
  501. "completion:\n", usb->name );
  502. DBGC_HDA ( usb, 0, cmplt->data,
  503. iob_len ( cmplt ) );
  504. free_iob ( cmplt );
  505. continue;
  506. }
  507. /* Copy completion to data buffer, if applicable */
  508. assert ( iob_len ( cmplt ) <= len );
  509. if ( request & USB_DIR_IN )
  510. memcpy ( data, cmplt->data, iob_len ( cmplt ) );
  511. free_iob ( cmplt );
  512. return 0;
  513. }
  514. /* Fail immediately if endpoint is in an error state */
  515. if ( ep->rc )
  516. return ep->rc;
  517. /* Delay */
  518. mdelay ( 1 );
  519. }
  520. DBGC ( usb, "USB %s timed out waiting for control transaction\n",
  521. usb->name );
  522. return -ETIMEDOUT;
  523. err_message:
  524. free_iob ( iobuf );
  525. err_alloc:
  526. return rc;
  527. }
  528. /**
  529. * Get USB string descriptor
  530. *
  531. * @v usb USB device
  532. * @v index String index
  533. * @v language Language ID
  534. * @v buf Data buffer
  535. * @v len Length of buffer
  536. * @ret len String length (excluding NUL), or negative error
  537. */
  538. int usb_get_string_descriptor ( struct usb_device *usb, unsigned int index,
  539. unsigned int language, char *buf, size_t len ) {
  540. size_t max = ( len ? ( len - 1 /* NUL */ ) : 0 );
  541. struct {
  542. struct usb_descriptor_header header;
  543. uint16_t character[max];
  544. } __attribute__ (( packed )) *desc;
  545. unsigned int actual;
  546. unsigned int i;
  547. int rc;
  548. /* Allocate buffer for string */
  549. desc = malloc ( sizeof ( *desc ) );
  550. if ( ! desc ) {
  551. rc = -ENOMEM;
  552. goto err_alloc;
  553. }
  554. /* Get descriptor */
  555. if ( ( rc = usb_get_descriptor ( usb, 0, USB_STRING_DESCRIPTOR, index,
  556. language, &desc->header,
  557. sizeof ( *desc ) ) ) != 0 )
  558. goto err_get_descriptor;
  559. /* Copy to buffer */
  560. actual = ( ( desc->header.len - sizeof ( desc->header ) ) /
  561. sizeof ( desc->character[0] ) );
  562. for ( i = 0 ; ( ( i < actual ) && ( i < max ) ) ; i++ )
  563. buf[i] = le16_to_cpu ( desc->character[i] );
  564. if ( len )
  565. buf[i] = '\0';
  566. /* Free buffer */
  567. free ( desc );
  568. return actual;
  569. err_get_descriptor:
  570. free ( desc );
  571. err_alloc:
  572. return rc;
  573. }
  574. /******************************************************************************
  575. *
  576. * USB device driver
  577. *
  578. ******************************************************************************
  579. */
  580. /**
  581. * Describe USB function
  582. *
  583. * @v func USB function
  584. * @v config Configuration descriptor
  585. * @v first First interface number
  586. * @ret rc Return status code
  587. */
  588. static int usb_function ( struct usb_function *func,
  589. struct usb_configuration_descriptor *config,
  590. unsigned int first ) {
  591. struct usb_device *usb = func->usb;
  592. struct usb_interface_association_descriptor *association;
  593. struct usb_interface_descriptor *interface;
  594. unsigned int i;
  595. /* First, look for an interface association descriptor */
  596. association = usb_interface_association_descriptor ( config, first );
  597. if ( association ) {
  598. /* Sanity check */
  599. if ( ( association->first + association->count ) >
  600. config->interfaces ) {
  601. DBGC ( usb, "USB %s has invalid association [%d-%d)\n",
  602. func->name, association->first,
  603. ( association->first + association->count ) );
  604. return -ERANGE;
  605. }
  606. /* Describe function */
  607. memcpy ( &func->class, &association->class,
  608. sizeof ( func->class ) );
  609. func->count = association->count;
  610. for ( i = 0 ; i < association->count ; i++ )
  611. func->interface[i] = ( association->first + i );
  612. return 0;
  613. }
  614. /* Next, look for an interface descriptor */
  615. interface = usb_interface_descriptor ( config, first, 0 );
  616. if ( ! interface ) {
  617. DBGC ( usb, "USB %s has no interface descriptor\n",
  618. func->name );
  619. return -ENOENT;
  620. }
  621. /* Describe function */
  622. memcpy ( &func->class, &interface->class, sizeof ( func->class ) );
  623. func->count = 1;
  624. func->interface[0] = first;
  625. return 0;
  626. }
  627. /**
  628. * Check for a USB device ID match
  629. *
  630. * @v func USB function
  631. * @v id Device ID
  632. * @ret matches Device ID matches
  633. */
  634. static int
  635. usb_device_id_matches ( struct usb_function *func, struct usb_device_id *id ) {
  636. return ( ( ( id->vendor == func->dev.desc.vendor ) ||
  637. ( id->vendor == USB_ANY_ID ) ) &&
  638. ( ( id->product == func->dev.desc.device ) ||
  639. ( id->product == USB_ANY_ID ) ) &&
  640. ( id->class.class == func->class.class ) &&
  641. ( id->class.subclass == func->class.subclass ) &&
  642. ( id->class.protocol == func->class.protocol ) );
  643. }
  644. /**
  645. * Probe USB device driver
  646. *
  647. * @v func USB function
  648. * @v config Configuration descriptor
  649. * @ret rc Return status code
  650. */
  651. static int usb_probe ( struct usb_function *func,
  652. struct usb_configuration_descriptor *config ) {
  653. struct usb_device *usb = func->usb;
  654. struct usb_driver *driver;
  655. struct usb_device_id *id;
  656. unsigned int i;
  657. int rc;
  658. /* Look for a matching driver */
  659. for_each_table_entry ( driver, USB_DRIVERS ) {
  660. for ( i = 0 ; i < driver->id_count ; i++ ) {
  661. /* Check for a matching ID */
  662. id = &driver->ids[i];
  663. if ( ! usb_device_id_matches ( func, id ) )
  664. continue;
  665. /* Probe driver */
  666. if ( ( rc = driver->probe ( func, config ) ) != 0 ) {
  667. DBGC ( usb, "USB %s failed to probe driver %s: "
  668. "%s\n", func->name, id->name,
  669. strerror ( rc ) );
  670. /* Continue trying other drivers */
  671. continue;
  672. }
  673. /* Record driver */
  674. func->driver = driver;
  675. func->dev.driver_name = id->name;
  676. return 0;
  677. }
  678. }
  679. /* No driver found */
  680. DBGC ( usb, "USB %s %04x:%04x class %d:%d:%d has no driver\n",
  681. func->name, func->dev.desc.vendor, func->dev.desc.device,
  682. func->class.class, func->class.subclass, func->class.protocol );
  683. return -ENOENT;
  684. }
  685. /**
  686. * Remove USB device driver
  687. *
  688. * @v func USB function
  689. */
  690. static void usb_remove ( struct usb_function *func ) {
  691. /* Remove driver */
  692. func->driver->remove ( func );
  693. }
  694. /**
  695. * Probe all USB device drivers
  696. *
  697. * @v usb USB device
  698. * @v config Configuration descriptor
  699. */
  700. static void
  701. usb_probe_all ( struct usb_device *usb,
  702. struct usb_configuration_descriptor *config ) {
  703. struct usb_bus *bus = usb->port->hub->bus;
  704. struct usb_function *func;
  705. uint8_t used[config->interfaces];
  706. unsigned int first;
  707. unsigned int i;
  708. int rc;
  709. /* Identify each function in turn */
  710. memset ( used, 0, sizeof ( used ) );
  711. for ( first = 0 ; first < config->interfaces ; first++ ) {
  712. /* Skip interfaces already used */
  713. if ( used[first] )
  714. continue;
  715. /* Allocate and initialise structure */
  716. func = zalloc ( sizeof ( *func ) +
  717. ( config->interfaces *
  718. sizeof ( func->interface[0] ) ) );
  719. if ( ! func )
  720. goto err_alloc;
  721. func->name = func->dev.name;
  722. func->usb = usb;
  723. func->dev.desc.bus_type = BUS_TYPE_USB;
  724. func->dev.desc.location = usb->address;
  725. func->dev.desc.vendor = le16_to_cpu ( usb->device.vendor );
  726. func->dev.desc.device = le16_to_cpu ( usb->device.product );
  727. snprintf ( func->dev.name, sizeof ( func->dev.name ),
  728. "%s-%d.%d", usb->name, config->config, first );
  729. INIT_LIST_HEAD ( &func->dev.children );
  730. func->dev.parent = bus->dev;
  731. /* Identify function */
  732. if ( ( rc = usb_function ( func, config, first ) ) != 0 )
  733. goto err_function;
  734. assert ( func->count <= config->interfaces );
  735. /* Mark interfaces as used */
  736. for ( i = 0 ; i < func->count ; i++ ) {
  737. assert ( func->interface[i] < config->interfaces );
  738. used[ func->interface[i] ] = 1;
  739. }
  740. /* Probe device driver */
  741. if ( ( rc = usb_probe ( func, config ) ) != 0 )
  742. goto err_probe;
  743. DBGC ( usb, "USB %s %04x:%04x class %d:%d:%d interfaces ",
  744. func->name, func->dev.desc.vendor, func->dev.desc.device,
  745. func->class.class, func->class.subclass,
  746. func->class.protocol );
  747. for ( i = 0 ; i < func->count ; i++ )
  748. DBGC ( usb, "%s%d", ( i ? "," : "" ),
  749. func->interface[i] );
  750. DBGC ( usb, " using driver %s\n", func->dev.driver_name );
  751. /* Add to list of functions */
  752. list_add ( &func->list, &usb->functions );
  753. /* Add to device hierarchy */
  754. list_add_tail ( &func->dev.siblings, &bus->dev->children );
  755. continue;
  756. list_del ( &func->dev.siblings );
  757. list_del ( &func->list );
  758. usb_remove ( func );
  759. err_probe:
  760. free ( func );
  761. err_alloc:
  762. err_function:
  763. /* Continue registering other functions */
  764. continue;
  765. }
  766. }
  767. /**
  768. * Remove all device drivers
  769. *
  770. * @v usb USB device
  771. */
  772. static void usb_remove_all ( struct usb_device *usb ) {
  773. struct usb_function *func;
  774. struct usb_function *tmp;
  775. /* Remove all functions */
  776. list_for_each_entry_safe ( func, tmp, &usb->functions, list ) {
  777. /* Remove device driver */
  778. usb_remove ( func );
  779. /* Remove from device hierarchy */
  780. assert ( list_empty ( &func->dev.children ) );
  781. list_del ( &func->dev.siblings );
  782. /* Remove from list of functions */
  783. list_del ( &func->list );
  784. /* Free function */
  785. free ( func );
  786. }
  787. }
  788. /**
  789. * Select USB device configuration
  790. *
  791. * @v usb USB device
  792. * @v index Configuration index
  793. * @ret rc Return status code
  794. */
  795. static int usb_configure ( struct usb_device *usb, unsigned int index ) {
  796. struct usb_configuration_descriptor partial;
  797. struct usb_configuration_descriptor *config;
  798. size_t len;
  799. int rc;
  800. /* Read first part of configuration descriptor to get size */
  801. if ( ( rc = usb_get_config_descriptor ( usb, index, &partial,
  802. sizeof ( partial ) ) ) != 0 ) {
  803. DBGC ( usb, "USB %s could not get configuration descriptor %d: "
  804. "%s\n", usb->name, index, strerror ( rc ) );
  805. goto err_get_partial;
  806. }
  807. len = le16_to_cpu ( partial.len );
  808. if ( len < sizeof ( partial ) ) {
  809. DBGC ( usb, "USB %s underlength configuraton descriptor %d\n",
  810. usb->name, index );
  811. rc = -EINVAL;
  812. goto err_partial_len;
  813. }
  814. /* Allocate buffer for whole configuration descriptor */
  815. config = malloc ( len );
  816. if ( ! config ) {
  817. rc = -ENOMEM;
  818. goto err_alloc_config;
  819. }
  820. /* Read whole configuration descriptor */
  821. if ( ( rc = usb_get_config_descriptor ( usb, index, config,
  822. len ) ) != 0 ) {
  823. DBGC ( usb, "USB %s could not get configuration descriptor %d: "
  824. "%s\n", usb->name, index, strerror ( rc ) );
  825. goto err_get_config_descriptor;
  826. }
  827. if ( config->len != partial.len ) {
  828. DBGC ( usb, "USB %s bad configuration descriptor %d length\n",
  829. usb->name, index );
  830. rc = -EINVAL;
  831. goto err_config_len;
  832. }
  833. /* Set configuration */
  834. if ( ( rc = usb_set_configuration ( usb, config->config ) ) != 0){
  835. DBGC ( usb, "USB %s could not set configuration %d: %s\n",
  836. usb->name, config->config, strerror ( rc ) );
  837. goto err_set_configuration;
  838. }
  839. /* Probe USB device drivers */
  840. usb_probe_all ( usb, config );
  841. /* Free configuration descriptor */
  842. free ( config );
  843. return 0;
  844. usb_remove_all ( usb );
  845. usb_set_configuration ( usb, 0 );
  846. err_set_configuration:
  847. err_config_len:
  848. err_get_config_descriptor:
  849. free ( config );
  850. err_alloc_config:
  851. err_partial_len:
  852. err_get_partial:
  853. return rc;
  854. }
  855. /**
  856. * Clear USB device configuration
  857. *
  858. * @v usb USB device
  859. */
  860. static void usb_deconfigure ( struct usb_device *usb ) {
  861. unsigned int i;
  862. /* Remove device drivers */
  863. usb_remove_all ( usb );
  864. /* Sanity checks */
  865. for ( i = 0 ; i < ( sizeof ( usb->ep ) / sizeof ( usb->ep[0] ) ) ; i++){
  866. if ( i != USB_ENDPOINT_IDX ( USB_EP0_ADDRESS ) )
  867. assert ( usb->ep[i] == NULL );
  868. }
  869. /* Clear device configuration */
  870. usb_set_configuration ( usb, 0 );
  871. }
  872. /**
  873. * Find and select a supported USB device configuration
  874. *
  875. * @v usb USB device
  876. * @ret rc Return status code
  877. */
  878. static int usb_configure_any ( struct usb_device *usb ) {
  879. unsigned int index;
  880. int rc = -ENOENT;
  881. /* Attempt all configuration indexes */
  882. for ( index = 0 ; index < usb->device.configurations ; index++ ) {
  883. /* Attempt this configuration index */
  884. if ( ( rc = usb_configure ( usb, index ) ) != 0 )
  885. continue;
  886. /* If we have no drivers, then try the next configuration */
  887. if ( list_empty ( &usb->functions ) ) {
  888. rc = -ENOTSUP;
  889. usb_deconfigure ( usb );
  890. continue;
  891. }
  892. return 0;
  893. }
  894. return rc;
  895. }
  896. /******************************************************************************
  897. *
  898. * USB device
  899. *
  900. ******************************************************************************
  901. */
  902. /**
  903. * Allocate USB device
  904. *
  905. * @v port USB port
  906. * @ret usb USB device, or NULL on allocation failure
  907. */
  908. static struct usb_device * alloc_usb ( struct usb_port *port ) {
  909. struct usb_hub *hub = port->hub;
  910. struct usb_bus *bus = hub->bus;
  911. struct usb_device *usb;
  912. /* Allocate and initialise structure */
  913. usb = zalloc ( sizeof ( *usb ) );
  914. if ( ! usb )
  915. return NULL;
  916. snprintf ( usb->name, sizeof ( usb->name ), "%s%c%d", hub->name,
  917. ( hub->usb ? '.' : '-' ), port->address );
  918. usb->port = port;
  919. INIT_LIST_HEAD ( &usb->functions );
  920. usb->host = &bus->op->device;
  921. usb_endpoint_init ( &usb->control, usb, &usb_control_operations );
  922. INIT_LIST_HEAD ( &usb->complete );
  923. return usb;
  924. }
  925. /**
  926. * Register USB device
  927. *
  928. * @v usb USB device
  929. * @ret rc Return status code
  930. */
  931. static int register_usb ( struct usb_device *usb ) {
  932. struct usb_port *port = usb->port;
  933. struct usb_hub *hub = port->hub;
  934. struct usb_bus *bus = hub->bus;
  935. unsigned int protocol;
  936. size_t mtu;
  937. int rc;
  938. /* Add to port */
  939. if ( port->usb != NULL ) {
  940. DBGC ( hub, "USB hub %s port %d is already registered to %s\n",
  941. hub->name, port->address, port->usb->name );
  942. rc = -EALREADY;
  943. goto err_already;
  944. }
  945. port->usb = usb;
  946. /* Add to bus device list */
  947. list_add_tail ( &usb->list, &bus->devices );
  948. /* Enable device */
  949. if ( ( rc = hub->driver->enable ( hub, port ) ) != 0 ) {
  950. DBGC ( hub, "USB hub %s port %d could not enable: %s\n",
  951. hub->name, port->address, strerror ( rc ) );
  952. goto err_enable;
  953. }
  954. /* Get device speed */
  955. if ( ( rc = hub->driver->speed ( hub, port ) ) != 0 ) {
  956. DBGC ( hub, "USB hub %s port %d could not get speed: %s\n",
  957. hub->name, port->address, strerror ( rc ) );
  958. goto err_speed;
  959. }
  960. DBGC2 ( usb, "USB %s attached as %s-speed device\n",
  961. usb->name, usb_speed_name ( port->speed ) );
  962. /* Open device */
  963. if ( ( rc = usb->host->open ( usb ) ) != 0 ) {
  964. DBGC ( usb, "USB %s could not open: %s\n",
  965. usb->name, strerror ( rc ) );
  966. goto err_open;
  967. }
  968. /* Describe control endpoint */
  969. mtu = USB_EP0_DEFAULT_MTU ( port->speed );
  970. usb_endpoint_describe ( &usb->control, USB_EP0_ADDRESS,
  971. USB_EP0_ATTRIBUTES, mtu, USB_EP0_BURST );
  972. /* Open control endpoint */
  973. if ( ( rc = usb_endpoint_open ( &usb->control ) ) != 0 )
  974. goto err_open_control;
  975. assert ( usb_endpoint ( usb, USB_EP0_ADDRESS ) == &usb->control );
  976. /* Assign device address */
  977. if ( ( rc = usb->host->address ( usb ) ) != 0 ) {
  978. DBGC ( usb, "USB %s could not set address: %s\n",
  979. usb->name, strerror ( rc ) );
  980. goto err_address;
  981. }
  982. DBGC2 ( usb, "USB %s assigned address %d\n", usb->name, usb->address );
  983. /* Read first part of device descriptor to get EP0 MTU */
  984. if ( ( rc = usb_get_mtu ( usb, &usb->device ) ) != 0 ) {
  985. DBGC ( usb, "USB %s could not get MTU: %s\n",
  986. usb->name, strerror ( rc ) );
  987. goto err_get_mtu;
  988. }
  989. /* Calculate EP0 MTU */
  990. protocol = le16_to_cpu ( usb->device.protocol );
  991. mtu = ( ( protocol < USB_PROTO_3_0 ) ?
  992. usb->device.mtu : ( 1 << usb->device.mtu ) );
  993. DBGC2 ( usb, "USB %s has control MTU %zd (guessed %zd)\n",
  994. usb->name, mtu, usb->control.mtu );
  995. /* Update MTU */
  996. if ( ( rc = usb_endpoint_mtu ( &usb->control, mtu ) ) != 0 )
  997. goto err_mtu;
  998. /* Read whole device descriptor */
  999. if ( ( rc = usb_get_device_descriptor ( usb, &usb->device ) ) != 0 ) {
  1000. DBGC ( usb, "USB %s could not get device descriptor: %s\n",
  1001. usb->name, strerror ( rc ) );
  1002. goto err_get_device_descriptor;
  1003. }
  1004. DBGC ( usb, "USB %s addr %d %04x:%04x class %d:%d:%d (v%s, %s-speed, "
  1005. "MTU %zd)\n", usb->name, usb->address,
  1006. le16_to_cpu ( usb->device.vendor ),
  1007. le16_to_cpu ( usb->device.product ), usb->device.class.class,
  1008. usb->device.class.subclass, usb->device.class.protocol,
  1009. usb_bcd ( le16_to_cpu ( usb->device.protocol ) ),
  1010. usb_speed_name ( port->speed ), usb->control.mtu );
  1011. /* Configure device */
  1012. if ( ( rc = usb_configure_any ( usb ) ) != 0 )
  1013. goto err_configure_any;
  1014. return 0;
  1015. usb_deconfigure ( usb );
  1016. err_configure_any:
  1017. err_get_device_descriptor:
  1018. err_mtu:
  1019. err_get_mtu:
  1020. err_address:
  1021. usb_endpoint_close ( &usb->control );
  1022. err_open_control:
  1023. usb->host->close ( usb );
  1024. err_open:
  1025. err_speed:
  1026. hub->driver->disable ( hub, port );
  1027. err_enable:
  1028. list_del ( &usb->list );
  1029. port->usb = NULL;
  1030. err_already:
  1031. return rc;
  1032. }
  1033. /**
  1034. * Unregister USB device
  1035. *
  1036. * @v usb USB device
  1037. */
  1038. static void unregister_usb ( struct usb_device *usb ) {
  1039. struct usb_port *port = usb->port;
  1040. struct usb_hub *hub = port->hub;
  1041. struct io_buffer *iobuf;
  1042. struct io_buffer *tmp;
  1043. /* Sanity checks */
  1044. assert ( port->usb == usb );
  1045. /* Clear device configuration */
  1046. usb_deconfigure ( usb );
  1047. /* Close control endpoint */
  1048. usb_endpoint_close ( &usb->control );
  1049. /* Discard any stale control completions */
  1050. list_for_each_entry_safe ( iobuf, tmp, &usb->complete, list ) {
  1051. list_del ( &iobuf->list );
  1052. free_iob ( iobuf );
  1053. }
  1054. /* Close device */
  1055. usb->host->close ( usb );
  1056. /* Disable port */
  1057. hub->driver->disable ( hub, port );
  1058. /* Remove from bus device list */
  1059. list_del ( &usb->list );
  1060. /* Remove from port */
  1061. port->usb = NULL;
  1062. }
  1063. /**
  1064. * Free USB device
  1065. *
  1066. * @v usb USB device
  1067. */
  1068. static void free_usb ( struct usb_device *usb ) {
  1069. unsigned int i;
  1070. /* Sanity checks */
  1071. for ( i = 0 ; i < ( sizeof ( usb->ep ) / sizeof ( usb->ep[0] ) ) ; i++ )
  1072. assert ( usb->ep[i] == NULL );
  1073. assert ( list_empty ( &usb->functions ) );
  1074. assert ( list_empty ( &usb->complete ) );
  1075. /* Free device */
  1076. free ( usb );
  1077. }
  1078. /******************************************************************************
  1079. *
  1080. * USB device hotplug event handling
  1081. *
  1082. ******************************************************************************
  1083. */
  1084. /**
  1085. * Handle newly attached USB device
  1086. *
  1087. * @v port USB port
  1088. * @ret rc Return status code
  1089. */
  1090. static int usb_attached ( struct usb_port *port ) {
  1091. struct usb_device *usb;
  1092. int rc;
  1093. /* Sanity checks */
  1094. assert ( port->usb == NULL );
  1095. /* Allocate USB device */
  1096. usb = alloc_usb ( port );
  1097. if ( ! usb ) {
  1098. rc = -ENOMEM;
  1099. goto err_alloc;
  1100. }
  1101. /* Register USB device */
  1102. if ( ( rc = register_usb ( usb ) ) != 0 )
  1103. goto err_register;
  1104. return 0;
  1105. unregister_usb ( usb );
  1106. err_register:
  1107. free_usb ( usb );
  1108. err_alloc:
  1109. return rc;
  1110. }
  1111. /**
  1112. * Handle newly detached USB device
  1113. *
  1114. * @v port USB port
  1115. */
  1116. static void usb_detached ( struct usb_port *port ) {
  1117. struct usb_device *usb = port->usb;
  1118. /* Sanity checks */
  1119. assert ( port->usb != NULL );
  1120. /* Unregister USB device */
  1121. unregister_usb ( usb );
  1122. /* Free USB device */
  1123. free_usb ( usb );
  1124. }
  1125. /**
  1126. * Handle newly attached or detached USB devices
  1127. *
  1128. * @v port USB port
  1129. * @ret rc Return status code
  1130. */
  1131. static int usb_hotplug ( struct usb_port *port ) {
  1132. struct usb_hub *hub = port->hub;
  1133. int rc;
  1134. /* Get current port speed */
  1135. if ( ( rc = hub->driver->speed ( hub, port ) ) != 0 ) {
  1136. DBGC ( hub, "USB hub %s port %d could not get speed: %s\n",
  1137. hub->name, port->address, strerror ( rc ) );
  1138. return rc;
  1139. }
  1140. /* Handle attached/detached device as applicable */
  1141. if ( port->speed && ! port->usb ) {
  1142. /* Newly attached device */
  1143. return usb_attached ( port );
  1144. } else if ( port->usb && ! port->speed ) {
  1145. /* Newly detached device */
  1146. usb_detached ( port );
  1147. return 0;
  1148. } else {
  1149. /* Ignore */
  1150. return 0;
  1151. }
  1152. }
  1153. /******************************************************************************
  1154. *
  1155. * USB process
  1156. *
  1157. ******************************************************************************
  1158. */
  1159. /**
  1160. * Report port status change
  1161. *
  1162. * @v port USB port
  1163. */
  1164. void usb_port_changed ( struct usb_port *port ) {
  1165. struct usb_hub *hub = port->hub;
  1166. struct usb_bus *bus = hub->bus;
  1167. /* Record hub port status change */
  1168. list_del ( &port->list );
  1169. list_add_tail ( &port->list, &bus->changed );
  1170. }
  1171. /**
  1172. * USB process
  1173. *
  1174. * @v bus USB bus
  1175. */
  1176. static void usb_step ( struct usb_bus *bus ) {
  1177. struct usb_port *port;
  1178. /* Poll bus */
  1179. usb_poll ( bus );
  1180. /* Handle any changed ports, allowing for the fact that the
  1181. * port list may change as we perform hotplug actions.
  1182. */
  1183. while ( ! list_empty ( &bus->changed ) ) {
  1184. /* Get first changed port */
  1185. port = list_first_entry ( &bus->changed, struct usb_port, list);
  1186. assert ( port != NULL );
  1187. /* Remove from list of changed ports */
  1188. list_del ( &port->list );
  1189. INIT_LIST_HEAD ( &port->list );
  1190. /* Perform appropriate hotplug action */
  1191. usb_hotplug ( port );
  1192. }
  1193. }
  1194. /** USB process */
  1195. static struct process_descriptor usb_process_desc =
  1196. PROC_DESC ( struct usb_bus, process, usb_step );
  1197. /******************************************************************************
  1198. *
  1199. * USB hub
  1200. *
  1201. ******************************************************************************
  1202. */
  1203. /**
  1204. * Allocate USB hub
  1205. *
  1206. * @v bus USB bus
  1207. * @v usb Underlying USB device, if any
  1208. * @v ports Number of ports
  1209. * @v driver Hub driver operations
  1210. * @ret hub USB hub, or NULL on allocation failure
  1211. */
  1212. struct usb_hub * alloc_usb_hub ( struct usb_bus *bus, struct usb_device *usb,
  1213. unsigned int ports,
  1214. struct usb_hub_driver_operations *driver ) {
  1215. struct usb_hub *hub;
  1216. struct usb_port *port;
  1217. unsigned int i;
  1218. /* Allocate and initialise structure */
  1219. hub = zalloc ( sizeof ( *hub ) + ( ports * sizeof ( hub->port[0] ) ) );
  1220. if ( ! hub )
  1221. return NULL;
  1222. hub->name = ( usb ? usb->name : bus->name );
  1223. hub->bus = bus;
  1224. hub->usb = usb;
  1225. if ( usb )
  1226. hub->protocol = usb->port->protocol;
  1227. hub->ports = ports;
  1228. hub->driver = driver;
  1229. /* Initialise port list */
  1230. for ( i = 1 ; i <= hub->ports ; i++ ) {
  1231. port = usb_port ( hub, i );
  1232. port->hub = hub;
  1233. port->address = i;
  1234. if ( usb )
  1235. port->protocol = usb->port->protocol;
  1236. INIT_LIST_HEAD ( &port->list );
  1237. }
  1238. return hub;
  1239. }
  1240. /**
  1241. * Register USB hub
  1242. *
  1243. * @v hub USB hub
  1244. * @ret rc Return status code
  1245. */
  1246. int register_usb_hub ( struct usb_hub *hub ) {
  1247. struct usb_bus *bus = hub->bus;
  1248. struct usb_port *port;
  1249. unsigned int i;
  1250. int rc;
  1251. /* Add to hub list */
  1252. list_add_tail ( &hub->list, &bus->hubs );
  1253. /* Open hub */
  1254. if ( ( rc = hub->driver->open ( hub ) ) != 0 ) {
  1255. DBGC ( hub, "USB hub %s could not open: %s\n",
  1256. hub->name, strerror ( rc ) );
  1257. goto err_open;
  1258. }
  1259. /* Delay to allow ports to stabilise */
  1260. mdelay ( USB_PORT_DELAY_MS );
  1261. /* Attach any devices already present */
  1262. for ( i = 1 ; i <= hub->ports ; i++ ) {
  1263. port = usb_port ( hub, i );
  1264. usb_hotplug ( port );
  1265. }
  1266. /* Some hubs seem to defer reporting device connections until
  1267. * their interrupt endpoint is polled for the first time.
  1268. * Poll the bus once now in order to pick up any such
  1269. * connections.
  1270. */
  1271. usb_step ( bus );
  1272. return 0;
  1273. hub->driver->close ( hub );
  1274. err_open:
  1275. list_del ( &hub->list );
  1276. return rc;
  1277. }
  1278. /**
  1279. * Unregister USB hub
  1280. *
  1281. * @v hub USB hub
  1282. */
  1283. void unregister_usb_hub ( struct usb_hub *hub ) {
  1284. struct usb_port *port;
  1285. unsigned int i;
  1286. /* Detach all devices */
  1287. for ( i = 1 ; i <= hub->ports ; i++ ) {
  1288. port = usb_port ( hub, i );
  1289. if ( port->usb )
  1290. usb_detached ( port );
  1291. }
  1292. /* Close hub */
  1293. hub->driver->close ( hub );
  1294. /* Cancel any pending port status changes */
  1295. for ( i = 1 ; i <= hub->ports ; i++ ) {
  1296. port = usb_port ( hub, i );
  1297. list_del ( &port->list );
  1298. INIT_LIST_HEAD ( &port->list );
  1299. }
  1300. /* Remove from hub list */
  1301. list_del ( &hub->list );
  1302. }
  1303. /**
  1304. * Free USB hub
  1305. *
  1306. * @v hub USB hub
  1307. */
  1308. void free_usb_hub ( struct usb_hub *hub ) {
  1309. struct usb_port *port;
  1310. unsigned int i;
  1311. /* Sanity checks */
  1312. for ( i = 1 ; i <= hub->ports ; i++ ) {
  1313. port = usb_port ( hub, i );
  1314. assert ( port->usb == NULL );
  1315. assert ( list_empty ( &port->list ) );
  1316. }
  1317. /* Free hub */
  1318. free ( hub );
  1319. }
  1320. /******************************************************************************
  1321. *
  1322. * USB bus
  1323. *
  1324. ******************************************************************************
  1325. */
  1326. /**
  1327. * Allocate USB bus
  1328. *
  1329. * @v dev Underlying hardware device
  1330. * @v ports Number of root hub ports
  1331. * @v op Host controller operations
  1332. * @ret bus USB bus, or NULL on allocation failure
  1333. */
  1334. struct usb_bus * alloc_usb_bus ( struct device *dev, unsigned int ports,
  1335. struct usb_host_operations *op ) {
  1336. struct usb_bus *bus;
  1337. /* Allocate and initialise structure */
  1338. bus = zalloc ( sizeof ( *bus ) );
  1339. if ( ! bus )
  1340. goto err_alloc_bus;
  1341. bus->name = dev->name;
  1342. bus->dev = dev;
  1343. bus->op = op;
  1344. INIT_LIST_HEAD ( &bus->devices );
  1345. INIT_LIST_HEAD ( &bus->hubs );
  1346. INIT_LIST_HEAD ( &bus->changed );
  1347. process_init_stopped ( &bus->process, &usb_process_desc, NULL );
  1348. bus->host = &bus->op->bus;
  1349. /* Allocate root hub */
  1350. bus->hub = alloc_usb_hub ( bus, NULL, ports, &op->hub );
  1351. if ( ! bus->hub )
  1352. goto err_alloc_hub;
  1353. return bus;
  1354. free_usb_hub ( bus->hub );
  1355. err_alloc_hub:
  1356. free ( bus );
  1357. err_alloc_bus:
  1358. return NULL;
  1359. }
  1360. /**
  1361. * Register USB bus
  1362. *
  1363. * @v bus USB bus
  1364. * @ret rc Return status code
  1365. */
  1366. int register_usb_bus ( struct usb_bus *bus ) {
  1367. int rc;
  1368. /* Sanity checks */
  1369. assert ( bus->hub != NULL );
  1370. /* Open bus */
  1371. if ( ( rc = bus->host->open ( bus ) ) != 0 )
  1372. goto err_open;
  1373. /* Register root hub */
  1374. if ( ( rc = register_usb_hub ( bus->hub ) ) != 0 )
  1375. goto err_register_hub;
  1376. /* Start bus process */
  1377. process_add ( &bus->process );
  1378. return 0;
  1379. unregister_usb_hub ( bus->hub );
  1380. err_register_hub:
  1381. bus->host->close ( bus );
  1382. err_open:
  1383. return rc;
  1384. }
  1385. /**
  1386. * Unregister USB bus
  1387. *
  1388. * @v bus USB bus
  1389. */
  1390. void unregister_usb_bus ( struct usb_bus *bus ) {
  1391. /* Sanity checks */
  1392. assert ( bus->hub != NULL );
  1393. assert ( process_running ( &bus->process ) );
  1394. /* Stop bus process */
  1395. process_del ( &bus->process );
  1396. /* Unregister root hub */
  1397. unregister_usb_hub ( bus->hub );
  1398. /* Close bus */
  1399. bus->host->close ( bus );
  1400. /* Sanity checks */
  1401. assert ( list_empty ( &bus->devices ) );
  1402. assert ( list_empty ( &bus->hubs ) );
  1403. assert ( ! process_running ( &bus->process ) );
  1404. }
  1405. /**
  1406. * Free USB bus
  1407. *
  1408. * @v bus USB bus
  1409. */
  1410. void free_usb_bus ( struct usb_bus *bus ) {
  1411. /* Sanity checks */
  1412. assert ( list_empty ( &bus->devices ) );
  1413. assert ( list_empty ( &bus->hubs ) );
  1414. assert ( ! process_running ( &bus->process ) );
  1415. /* Free root hub */
  1416. free_usb_hub ( bus->hub );
  1417. /* Free bus */
  1418. free ( bus );
  1419. }
  1420. /******************************************************************************
  1421. *
  1422. * USB bus topology
  1423. *
  1424. ******************************************************************************
  1425. */
  1426. /**
  1427. * Get USB route string
  1428. *
  1429. * @v usb USB device
  1430. * @ret route USB route string
  1431. */
  1432. unsigned int usb_route_string ( struct usb_device *usb ) {
  1433. unsigned int route;
  1434. /* Navigate up to root hub, constructing route string as we go */
  1435. for ( route = 0 ; usb->port->hub->usb ; usb = usb->port->hub->usb ) {
  1436. route <<= 4;
  1437. route |= ( ( usb->port->address > 0xf ) ?
  1438. 0xf : usb->port->address );
  1439. }
  1440. return route;
  1441. }
  1442. /**
  1443. * Get USB depth
  1444. *
  1445. * @v usb USB device
  1446. * @ret depth Hub depth
  1447. */
  1448. unsigned int usb_depth ( struct usb_device *usb ) {
  1449. unsigned int depth;
  1450. /* Navigate up to root hub, constructing depth as we go */
  1451. for ( depth = 0 ; usb->port->hub->usb ; usb = usb->port->hub->usb )
  1452. depth++;
  1453. return depth;
  1454. }
  1455. /**
  1456. * Get USB root hub port
  1457. *
  1458. * @v usb USB device
  1459. * @ret port Root hub port
  1460. */
  1461. struct usb_port * usb_root_hub_port ( struct usb_device *usb ) {
  1462. /* Navigate up to root hub */
  1463. while ( usb->port->hub->usb )
  1464. usb = usb->port->hub->usb;
  1465. return usb->port;
  1466. }
  1467. /* Drag in hub driver */
  1468. REQUIRE_OBJECT ( usbhub );