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

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