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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262
  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. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <strings.h>
  28. #include <unistd.h>
  29. #include <errno.h>
  30. #include <assert.h>
  31. #include <byteswap.h>
  32. #include <ipxe/usb.h>
  33. #include <ipxe/cdc.h>
  34. /** @file
  35. *
  36. * Universal Serial Bus (USB)
  37. *
  38. */
  39. /** List of USB buses */
  40. struct list_head usb_buses = LIST_HEAD_INIT ( usb_buses );
  41. /** List of changed ports */
  42. static struct list_head usb_changed = LIST_HEAD_INIT ( usb_changed );
  43. /** List of halted endpoints */
  44. static struct list_head usb_halted = LIST_HEAD_INIT ( usb_halted );
  45. /******************************************************************************
  46. *
  47. * Utility functions
  48. *
  49. ******************************************************************************
  50. */
  51. /**
  52. * Get USB speed name (for debugging)
  53. *
  54. * @v speed Speed
  55. * @ret name Speed name
  56. */
  57. static inline const char * usb_speed_name ( unsigned int speed ) {
  58. static const char *exponents[4] = { "", "k", "M", "G" };
  59. static char buf[ 10 /* "xxxxxXbps" + NUL */ ];
  60. unsigned int mantissa;
  61. unsigned int exponent;
  62. /* Extract mantissa and exponent */
  63. mantissa = USB_SPEED_MANTISSA ( speed );
  64. exponent = USB_SPEED_EXPONENT ( speed );
  65. /* Name speed */
  66. switch ( speed ) {
  67. case USB_SPEED_NONE: return "DETACHED";
  68. case USB_SPEED_LOW: return "low";
  69. case USB_SPEED_FULL: return "full";
  70. case USB_SPEED_HIGH: return "high";
  71. case USB_SPEED_SUPER: return "super";
  72. default:
  73. snprintf ( buf, sizeof ( buf ), "%d%sbps",
  74. mantissa, exponents[exponent] );
  75. return buf;
  76. }
  77. }
  78. /**
  79. * Transcribe USB BCD-coded value (for debugging)
  80. *
  81. * @v bcd BCD-coded value
  82. * @ret string Transcribed value
  83. */
  84. static inline const char * usb_bcd ( uint16_t bcd ) {
  85. static char buf[ 6 /* "xx.xx" + NUL */ ];
  86. uint8_t high = ( bcd >> 8 );
  87. uint8_t low = ( bcd >> 0 );
  88. snprintf ( buf, sizeof ( buf ), "%x.%02x", high, low );
  89. return buf;
  90. }
  91. /******************************************************************************
  92. *
  93. * USB descriptors
  94. *
  95. ******************************************************************************
  96. */
  97. /**
  98. * Locate USB interface association descriptor
  99. *
  100. * @v config Configuraton descriptor
  101. * @v first First interface number
  102. * @ret desc Interface association descriptor, or NULL if not found
  103. */
  104. static struct usb_interface_association_descriptor *
  105. usb_interface_association_descriptor ( struct usb_configuration_descriptor
  106. *config,
  107. unsigned int first ) {
  108. struct usb_interface_association_descriptor *desc;
  109. /* Find a matching interface association descriptor */
  110. for_each_config_descriptor ( desc, config ) {
  111. if ( ( desc->header.type ==
  112. USB_INTERFACE_ASSOCIATION_DESCRIPTOR ) &&
  113. ( desc->first == first ) )
  114. return desc;
  115. }
  116. return NULL;
  117. }
  118. /**
  119. * Locate USB interface descriptor
  120. *
  121. * @v config Configuraton descriptor
  122. * @v interface Interface number
  123. * @v alternate Alternate setting
  124. * @ret desc Interface descriptor, or NULL if not found
  125. */
  126. struct usb_interface_descriptor *
  127. usb_interface_descriptor ( struct usb_configuration_descriptor *config,
  128. unsigned int interface, unsigned int alternate ) {
  129. struct usb_interface_descriptor *desc;
  130. /* Find a matching interface descriptor */
  131. for_each_config_descriptor ( desc, config ) {
  132. if ( ( desc->header.type == USB_INTERFACE_DESCRIPTOR ) &&
  133. ( desc->interface == interface ) &&
  134. ( desc->alternate == alternate ) )
  135. return desc;
  136. }
  137. return NULL;
  138. }
  139. /**
  140. * Locate USB endpoint descriptor
  141. *
  142. * @v config Configuration descriptor
  143. * @v interface Interface descriptor
  144. * @v type Endpoint (internal) type
  145. * @v index Endpoint index
  146. * @ret desc Descriptor, or NULL if not found
  147. */
  148. struct usb_endpoint_descriptor *
  149. usb_endpoint_descriptor ( struct usb_configuration_descriptor *config,
  150. struct usb_interface_descriptor *interface,
  151. unsigned int type, unsigned int index ) {
  152. struct usb_endpoint_descriptor *desc;
  153. unsigned int attributes = ( type & USB_ENDPOINT_ATTR_TYPE_MASK );
  154. unsigned int direction = ( type & USB_DIR_IN );
  155. /* Find a matching endpoint descriptor */
  156. for_each_interface_descriptor ( desc, config, interface ) {
  157. if ( ( desc->header.type == USB_ENDPOINT_DESCRIPTOR ) &&
  158. ( ( desc->attributes &
  159. USB_ENDPOINT_ATTR_TYPE_MASK ) == attributes ) &&
  160. ( ( desc->endpoint & USB_DIR_IN ) == direction ) &&
  161. ( index-- == 0 ) )
  162. return desc;
  163. }
  164. return NULL;
  165. }
  166. /**
  167. * Locate USB endpoint companion descriptor
  168. *
  169. * @v config Configuration descriptor
  170. * @v desc Endpoint descriptor
  171. * @ret descx Companion descriptor, or NULL if not found
  172. */
  173. struct usb_endpoint_companion_descriptor *
  174. usb_endpoint_companion_descriptor ( struct usb_configuration_descriptor *config,
  175. struct usb_endpoint_descriptor *desc ) {
  176. struct usb_endpoint_companion_descriptor *descx;
  177. /* Get companion descriptor, if present */
  178. descx = container_of ( usb_next_descriptor ( &desc->header ),
  179. struct usb_endpoint_companion_descriptor,
  180. header );
  181. return ( ( usb_is_within_config ( config, &descx->header ) &&
  182. descx->header.type == USB_ENDPOINT_COMPANION_DESCRIPTOR )
  183. ? descx : NULL );
  184. }
  185. /******************************************************************************
  186. *
  187. * USB endpoint
  188. *
  189. ******************************************************************************
  190. */
  191. /**
  192. * Get USB endpoint name (for debugging)
  193. *
  194. * @v ep USB endpoint
  195. * @ret name Endpoint name
  196. */
  197. const char * usb_endpoint_name ( struct usb_endpoint *ep ) {
  198. static char buf[ 9 /* "EPxx OUT" + NUL */ ];
  199. unsigned int address = ep->address;
  200. snprintf ( buf, sizeof ( buf ), "EP%d%s",
  201. ( address & USB_ENDPOINT_MAX ),
  202. ( address ?
  203. ( ( address & USB_ENDPOINT_IN ) ? " IN" : " OUT" ) : "" ));
  204. return buf;
  205. }
  206. /**
  207. * Describe USB endpoint from device configuration
  208. *
  209. * @v ep USB endpoint
  210. * @v config Configuration descriptor
  211. * @v interface Interface descriptor
  212. * @v type Endpoint (internal) type
  213. * @v index Endpoint index
  214. * @ret rc Return status code
  215. */
  216. int usb_endpoint_described ( struct usb_endpoint *ep,
  217. struct usb_configuration_descriptor *config,
  218. struct usb_interface_descriptor *interface,
  219. unsigned int type, unsigned int index ) {
  220. struct usb_device *usb = ep->usb;
  221. struct usb_endpoint_descriptor *desc;
  222. struct usb_endpoint_companion_descriptor *descx;
  223. unsigned int sizes;
  224. unsigned int burst;
  225. unsigned int interval;
  226. size_t mtu;
  227. /* Locate endpoint descriptor */
  228. desc = usb_endpoint_descriptor ( config, interface, type, index );
  229. if ( ! desc )
  230. return -ENOENT;
  231. /* Locate companion descriptor, if any */
  232. descx = usb_endpoint_companion_descriptor ( config, desc );
  233. /* Calculate MTU and burst size */
  234. sizes = le16_to_cpu ( desc->sizes );
  235. mtu = USB_ENDPOINT_MTU ( sizes );
  236. burst = ( descx ? descx->burst : USB_ENDPOINT_BURST ( sizes ) );
  237. /* Calculate interval */
  238. if ( ( type & USB_ENDPOINT_ATTR_TYPE_MASK ) ==
  239. USB_ENDPOINT_ATTR_INTERRUPT ) {
  240. if ( usb->speed >= USB_SPEED_HIGH ) {
  241. /* 2^(desc->interval-1) is a microframe count */
  242. interval = ( 1 << ( desc->interval - 1 ) );
  243. } else {
  244. /* desc->interval is a (whole) frame count */
  245. interval = ( desc->interval << 3 );
  246. }
  247. } else {
  248. /* desc->interval is a microframe count */
  249. interval = desc->interval;
  250. }
  251. /* Describe endpoint */
  252. usb_endpoint_describe ( ep, desc->endpoint, desc->attributes,
  253. mtu, burst, interval );
  254. return 0;
  255. }
  256. /**
  257. * Open USB endpoint
  258. *
  259. * @v ep USB endpoint
  260. * @ret rc Return status code
  261. */
  262. int usb_endpoint_open ( struct usb_endpoint *ep ) {
  263. struct usb_device *usb = ep->usb;
  264. unsigned int idx = USB_ENDPOINT_IDX ( ep->address );
  265. int rc;
  266. /* Populate host controller operations */
  267. ep->host = &usb->port->hub->bus->op->endpoint;
  268. /* Add to endpoint list */
  269. if ( usb->ep[idx] != NULL ) {
  270. DBGC ( usb, "USB %s %s is already open\n",
  271. usb->name, usb_endpoint_name ( ep ) );
  272. rc = -EALREADY;
  273. goto err_already;
  274. }
  275. usb->ep[idx] = ep;
  276. INIT_LIST_HEAD ( &ep->halted );
  277. /* Open endpoint */
  278. if ( ( rc = ep->host->open ( ep ) ) != 0 ) {
  279. DBGC ( usb, "USB %s %s could not open: %s\n", usb->name,
  280. usb_endpoint_name ( ep ), strerror ( rc ) );
  281. goto err_open;
  282. }
  283. ep->open = 1;
  284. DBGC2 ( usb, "USB %s %s opened with MTU %zd, burst %d, interval %d\n",
  285. usb->name, usb_endpoint_name ( ep ), ep->mtu, ep->burst,
  286. ep->interval );
  287. return 0;
  288. ep->open = 0;
  289. ep->host->close ( ep );
  290. err_open:
  291. usb->ep[idx] = NULL;
  292. err_already:
  293. if ( ep->max )
  294. usb_flush ( ep );
  295. return rc;
  296. }
  297. /**
  298. * Clear transaction translator (if applicable)
  299. *
  300. * @v ep USB endpoint
  301. * @ret rc Return status code
  302. */
  303. static int usb_endpoint_clear_tt ( struct usb_endpoint *ep ) {
  304. struct usb_device *usb = ep->usb;
  305. struct usb_port *tt;
  306. int rc;
  307. /* Do nothing if this is a periodic endpoint */
  308. if ( ep->attributes & USB_ENDPOINT_ATTR_PERIODIC )
  309. return 0;
  310. /* Do nothing if this endpoint is not behind a transaction translator */
  311. tt = usb_transaction_translator ( usb );
  312. if ( ! tt )
  313. return 0;
  314. /* Clear transaction translator buffer */
  315. if ( ( rc = tt->hub->driver->clear_tt ( tt->hub, tt, ep ) ) != 0 ) {
  316. DBGC ( usb, "USB %s %s could not clear transaction translator: "
  317. "%s\n", usb->name, usb_endpoint_name ( ep ),
  318. strerror ( rc ) );
  319. return rc;
  320. }
  321. return 0;
  322. }
  323. /**
  324. * Close USB endpoint
  325. *
  326. * @v ep USB endpoint
  327. */
  328. void usb_endpoint_close ( struct usb_endpoint *ep ) {
  329. struct usb_device *usb = ep->usb;
  330. unsigned int idx = USB_ENDPOINT_IDX ( ep->address );
  331. /* Sanity checks */
  332. assert ( usb->ep[idx] == ep );
  333. /* Close endpoint */
  334. ep->open = 0;
  335. ep->host->close ( ep );
  336. assert ( ep->fill == 0 );
  337. /* Remove from endpoint list */
  338. usb->ep[idx] = NULL;
  339. list_del ( &ep->halted );
  340. /* Discard any recycled buffers, if applicable */
  341. if ( ep->max )
  342. usb_flush ( ep );
  343. /* Clear transaction translator, if applicable */
  344. usb_endpoint_clear_tt ( ep );
  345. }
  346. /**
  347. * Reset USB endpoint
  348. *
  349. * @v ep USB endpoint
  350. * @ret rc Return status code
  351. */
  352. static int usb_endpoint_reset ( struct usb_endpoint *ep ) {
  353. struct usb_device *usb = ep->usb;
  354. unsigned int type;
  355. int rc;
  356. /* Sanity check */
  357. assert ( ! list_empty ( &ep->halted ) );
  358. /* Reset endpoint */
  359. if ( ( rc = ep->host->reset ( ep ) ) != 0 ) {
  360. DBGC ( usb, "USB %s %s could not reset: %s\n",
  361. usb->name, usb_endpoint_name ( ep ), strerror ( rc ) );
  362. return rc;
  363. }
  364. /* Clear transaction translator, if applicable */
  365. if ( ( rc = usb_endpoint_clear_tt ( ep ) ) != 0 )
  366. return rc;
  367. /* Clear endpoint halt, if applicable */
  368. type = ( ep->attributes & USB_ENDPOINT_ATTR_TYPE_MASK );
  369. if ( ( type != USB_ENDPOINT_ATTR_CONTROL ) &&
  370. ( ( rc = usb_clear_feature ( usb, USB_RECIP_ENDPOINT,
  371. USB_ENDPOINT_HALT,
  372. ep->address ) ) != 0 ) ) {
  373. DBGC ( usb, "USB %s %s could not clear endpoint halt: %s\n",
  374. usb->name, usb_endpoint_name ( ep ), strerror ( rc ) );
  375. return rc;
  376. }
  377. /* Remove from list of halted endpoints */
  378. list_del ( &ep->halted );
  379. INIT_LIST_HEAD ( &ep->halted );
  380. DBGC ( usb, "USB %s %s reset\n",
  381. usb->name, usb_endpoint_name ( ep ) );
  382. return 0;
  383. }
  384. /**
  385. * Update endpoint MTU
  386. *
  387. * @v ep USB endpoint
  388. * @v mtu New MTU
  389. * @ret rc Return status code
  390. */
  391. static int usb_endpoint_mtu ( struct usb_endpoint *ep, size_t mtu ) {
  392. struct usb_device *usb = ep->usb;
  393. int rc;
  394. /* Update MTU */
  395. ep->mtu = mtu;
  396. if ( ( rc = ep->host->mtu ( ep ) ) != 0 ) {
  397. DBGC ( usb, "USB %s %s could not update MTU: %s\n",
  398. usb->name, usb_endpoint_name ( ep ), strerror ( rc ) );
  399. return rc;
  400. }
  401. return 0;
  402. }
  403. /**
  404. * Enqueue USB message transfer
  405. *
  406. * @v ep USB endpoint
  407. * @v request Request
  408. * @v value Value parameter
  409. * @v index Index parameter
  410. * @v iobuf I/O buffer
  411. * @ret rc Return status code
  412. *
  413. * The I/O buffer must have sufficient headroom to contain a setup
  414. * packet.
  415. */
  416. int usb_message ( struct usb_endpoint *ep, unsigned int request,
  417. unsigned int value, unsigned int index,
  418. struct io_buffer *iobuf ) {
  419. struct usb_device *usb = ep->usb;
  420. struct usb_port *port = usb->port;
  421. struct usb_setup_packet *packet;
  422. size_t len = iob_len ( iobuf );
  423. int rc;
  424. /* Sanity check */
  425. assert ( iob_headroom ( iobuf ) >= sizeof ( *packet ) );
  426. /* Fail immediately if device has been unplugged */
  427. if ( port->disconnected )
  428. return -ENODEV;
  429. /* Reset endpoint if required */
  430. if ( ( ! list_empty ( &ep->halted ) ) &&
  431. ( ( rc = usb_endpoint_reset ( ep ) ) != 0 ) )
  432. return rc;
  433. /* Zero input data buffer (if applicable) */
  434. if ( request & USB_DIR_IN )
  435. memset ( iobuf->data, 0, len );
  436. /* Construct setup packet */
  437. packet = iob_push ( iobuf, sizeof ( *packet ) );
  438. packet->request = cpu_to_le16 ( request );
  439. packet->value = cpu_to_le16 ( value );
  440. packet->index = cpu_to_le16 ( index );
  441. packet->len = cpu_to_le16 ( len );
  442. /* Enqueue message transfer */
  443. if ( ( rc = ep->host->message ( ep, iobuf ) ) != 0 ) {
  444. DBGC ( usb, "USB %s %s could not enqueue message transfer: "
  445. "%s\n", usb->name, usb_endpoint_name ( ep ),
  446. strerror ( rc ) );
  447. return rc;
  448. }
  449. /* Increment fill level */
  450. ep->fill++;
  451. return 0;
  452. }
  453. /**
  454. * Enqueue USB stream transfer
  455. *
  456. * @v ep USB endpoint
  457. * @v iobuf I/O buffer
  458. * @v terminate Terminate using a short packet
  459. * @ret rc Return status code
  460. */
  461. int usb_stream ( struct usb_endpoint *ep, struct io_buffer *iobuf,
  462. int terminate ) {
  463. struct usb_device *usb = ep->usb;
  464. struct usb_port *port = usb->port;
  465. int zlp;
  466. int rc;
  467. /* Fail immediately if device has been unplugged */
  468. if ( port->disconnected )
  469. return -ENODEV;
  470. /* Reset endpoint if required */
  471. if ( ( ! list_empty ( &ep->halted ) ) &&
  472. ( ( rc = usb_endpoint_reset ( ep ) ) != 0 ) )
  473. return rc;
  474. /* Append a zero-length packet if necessary */
  475. zlp = terminate;
  476. if ( iob_len ( iobuf ) & ( ep->mtu - 1 ) )
  477. zlp = 0;
  478. /* Enqueue stream transfer */
  479. if ( ( rc = ep->host->stream ( ep, iobuf, zlp ) ) != 0 ) {
  480. DBGC ( usb, "USB %s %s could not enqueue stream transfer: %s\n",
  481. usb->name, usb_endpoint_name ( ep ), strerror ( rc ) );
  482. return rc;
  483. }
  484. /* Increment fill level */
  485. ep->fill++;
  486. return 0;
  487. }
  488. /**
  489. * Complete transfer (possibly with error)
  490. *
  491. * @v ep USB endpoint
  492. * @v iobuf I/O buffer
  493. * @v rc Completion status code
  494. */
  495. void usb_complete_err ( struct usb_endpoint *ep, struct io_buffer *iobuf,
  496. int rc ) {
  497. struct usb_device *usb = ep->usb;
  498. /* Decrement fill level */
  499. assert ( ep->fill > 0 );
  500. ep->fill--;
  501. /* Schedule reset, if applicable */
  502. if ( ( rc != 0 ) && ep->open ) {
  503. DBGC ( usb, "USB %s %s completion failed: %s\n",
  504. usb->name, usb_endpoint_name ( ep ), strerror ( rc ) );
  505. list_del ( &ep->halted );
  506. list_add_tail ( &ep->halted, &usb_halted );
  507. }
  508. /* Report completion */
  509. ep->driver->complete ( ep, iobuf, rc );
  510. }
  511. /******************************************************************************
  512. *
  513. * Endpoint refilling
  514. *
  515. ******************************************************************************
  516. */
  517. /**
  518. * Prefill endpoint recycled buffer list
  519. *
  520. * @v ep USB endpoint
  521. * @ret rc Return status code
  522. */
  523. int usb_prefill ( struct usb_endpoint *ep ) {
  524. struct io_buffer *iobuf;
  525. size_t reserve = ep->reserve;
  526. size_t len = ( ep->len ? ep->len : ep->mtu );
  527. unsigned int fill;
  528. int rc;
  529. /* Sanity checks */
  530. assert ( ep->fill == 0 );
  531. assert ( ep->max > 0 );
  532. assert ( list_empty ( &ep->recycled ) );
  533. /* Fill recycled buffer list */
  534. for ( fill = 0 ; fill < ep->max ; fill++ ) {
  535. /* Allocate I/O buffer */
  536. iobuf = alloc_iob ( reserve + len );
  537. if ( ! iobuf ) {
  538. rc = -ENOMEM;
  539. goto err_alloc;
  540. }
  541. iob_reserve ( iobuf, reserve );
  542. /* Add to recycled buffer list */
  543. list_add_tail ( &iobuf->list, &ep->recycled );
  544. }
  545. return 0;
  546. err_alloc:
  547. usb_flush ( ep );
  548. return rc;
  549. }
  550. /**
  551. * Refill endpoint
  552. *
  553. * @v ep USB endpoint
  554. * @ret rc Return status code
  555. */
  556. int usb_refill ( struct usb_endpoint *ep ) {
  557. struct io_buffer *iobuf;
  558. size_t reserve = ep->reserve;
  559. size_t len = ( ep->len ? ep->len : ep->mtu );
  560. int rc;
  561. /* Sanity checks */
  562. assert ( ep->open );
  563. assert ( ep->max > 0 );
  564. /* Refill endpoint */
  565. while ( ep->fill < ep->max ) {
  566. /* Get or allocate buffer */
  567. if ( list_empty ( &ep->recycled ) ) {
  568. /* Recycled buffer list is empty; allocate new buffer */
  569. iobuf = alloc_iob ( reserve + len );
  570. if ( ! iobuf )
  571. return -ENOMEM;
  572. iob_reserve ( iobuf, reserve );
  573. } else {
  574. /* Get buffer from recycled buffer list */
  575. iobuf = list_first_entry ( &ep->recycled,
  576. struct io_buffer, list );
  577. assert ( iobuf != NULL );
  578. list_del ( &iobuf->list );
  579. }
  580. /* Reset buffer to maximum size */
  581. assert ( iob_len ( iobuf ) <= len );
  582. iob_put ( iobuf, ( len - iob_len ( iobuf ) ) );
  583. /* Enqueue buffer */
  584. if ( ( rc = usb_stream ( ep, iobuf, 0 ) ) != 0 ) {
  585. list_add ( &iobuf->list, &ep->recycled );
  586. return rc;
  587. }
  588. }
  589. return 0;
  590. }
  591. /**
  592. * Discard endpoint recycled buffer list
  593. *
  594. * @v ep USB endpoint
  595. */
  596. void usb_flush ( struct usb_endpoint *ep ) {
  597. struct io_buffer *iobuf;
  598. struct io_buffer *tmp;
  599. /* Sanity checks */
  600. assert ( ! ep->open );
  601. assert ( ep->max > 0 );
  602. /* Free all I/O buffers */
  603. list_for_each_entry_safe ( iobuf, tmp, &ep->recycled, list ) {
  604. list_del ( &iobuf->list );
  605. free_iob ( iobuf );
  606. }
  607. }
  608. /******************************************************************************
  609. *
  610. * Control endpoint
  611. *
  612. ******************************************************************************
  613. */
  614. /** USB control transfer pseudo-header */
  615. struct usb_control_pseudo_header {
  616. /** Completion status */
  617. int rc;
  618. };
  619. /**
  620. * Complete USB control transfer
  621. *
  622. * @v ep USB endpoint
  623. * @v iobuf I/O buffer
  624. * @v rc Completion status code
  625. */
  626. static void usb_control_complete ( struct usb_endpoint *ep,
  627. struct io_buffer *iobuf, int rc ) {
  628. struct usb_device *usb = ep->usb;
  629. struct usb_control_pseudo_header *pshdr;
  630. /* Record completion status in buffer */
  631. pshdr = iob_push ( iobuf, sizeof ( *pshdr ) );
  632. pshdr->rc = rc;
  633. /* Add to list of completed I/O buffers */
  634. list_add_tail ( &iobuf->list, &usb->complete );
  635. }
  636. /** USB control endpoint driver operations */
  637. static struct usb_endpoint_driver_operations usb_control_operations = {
  638. .complete = usb_control_complete,
  639. };
  640. /**
  641. * Issue USB control transaction
  642. *
  643. * @v usb USB device
  644. * @v request Request
  645. * @v value Value parameter
  646. * @v index Index parameter
  647. * @v data Data buffer (if any)
  648. * @v len Length of data
  649. * @ret rc Return status code
  650. */
  651. int usb_control ( struct usb_device *usb, unsigned int request,
  652. unsigned int value, unsigned int index, void *data,
  653. size_t len ) {
  654. struct usb_bus *bus = usb->port->hub->bus;
  655. struct usb_endpoint *ep = &usb->control;
  656. struct io_buffer *iobuf;
  657. struct io_buffer *cmplt;
  658. union {
  659. struct usb_setup_packet setup;
  660. struct usb_control_pseudo_header pshdr;
  661. } *headroom;
  662. struct usb_control_pseudo_header *pshdr;
  663. unsigned int i;
  664. int rc;
  665. /* Allocate I/O buffer */
  666. iobuf = alloc_iob ( sizeof ( *headroom ) + len );
  667. if ( ! iobuf ) {
  668. rc = -ENOMEM;
  669. goto err_alloc;
  670. }
  671. iob_reserve ( iobuf, sizeof ( *headroom ) );
  672. iob_put ( iobuf, len );
  673. if ( request & USB_DIR_IN ) {
  674. memset ( data, 0, len );
  675. } else {
  676. memcpy ( iobuf->data, data, len );
  677. }
  678. /* Enqueue message */
  679. if ( ( rc = usb_message ( ep, request, value, index, iobuf ) ) != 0 )
  680. goto err_message;
  681. /* Wait for completion */
  682. for ( i = 0 ; i < USB_CONTROL_MAX_WAIT_MS ; i++ ) {
  683. /* Poll bus */
  684. usb_poll ( bus );
  685. /* Check for completion */
  686. while ( ( cmplt = list_first_entry ( &usb->complete,
  687. struct io_buffer,
  688. list ) ) ) {
  689. /* Remove from completion list */
  690. list_del ( &cmplt->list );
  691. /* Extract and strip completion status */
  692. pshdr = cmplt->data;
  693. iob_pull ( cmplt, sizeof ( *pshdr ) );
  694. rc = pshdr->rc;
  695. /* Discard stale completions */
  696. if ( cmplt != iobuf ) {
  697. DBGC ( usb, "USB %s stale control completion: "
  698. "%s\n", usb->name, strerror ( rc ) );
  699. DBGC_HDA ( usb, 0, cmplt->data,
  700. iob_len ( cmplt ) );
  701. free_iob ( cmplt );
  702. continue;
  703. }
  704. /* Fail immediately if completion was in error */
  705. if ( rc != 0 ) {
  706. DBGC ( usb, "USB %s control %04x:%04x:%04x "
  707. "failed: %s\n", usb->name, request,
  708. value, index, strerror ( rc ) );
  709. free_iob ( cmplt );
  710. return rc;
  711. }
  712. /* Copy completion to data buffer, if applicable */
  713. assert ( iob_len ( cmplt ) <= len );
  714. if ( request & USB_DIR_IN )
  715. memcpy ( data, cmplt->data, iob_len ( cmplt ) );
  716. free_iob ( cmplt );
  717. return 0;
  718. }
  719. /* Delay */
  720. mdelay ( 1 );
  721. }
  722. DBGC ( usb, "USB %s timed out waiting for control %04x:%04x:%04x\n",
  723. usb->name, request, value, index );
  724. return -ETIMEDOUT;
  725. err_message:
  726. free_iob ( iobuf );
  727. err_alloc:
  728. return rc;
  729. }
  730. /**
  731. * Get USB string descriptor
  732. *
  733. * @v usb USB device
  734. * @v index String index
  735. * @v language Language ID
  736. * @v buf Data buffer
  737. * @v len Length of buffer
  738. * @ret len String length (excluding NUL), or negative error
  739. */
  740. int usb_get_string_descriptor ( struct usb_device *usb, unsigned int index,
  741. unsigned int language, char *buf, size_t len ) {
  742. size_t max = ( len ? ( len - 1 /* NUL */ ) : 0 );
  743. struct {
  744. struct usb_descriptor_header header;
  745. uint16_t character[max];
  746. } __attribute__ (( packed )) *desc;
  747. unsigned int actual;
  748. unsigned int i;
  749. int rc;
  750. /* Allocate buffer for string */
  751. desc = malloc ( sizeof ( *desc ) );
  752. if ( ! desc ) {
  753. rc = -ENOMEM;
  754. goto err_alloc;
  755. }
  756. /* Get descriptor */
  757. if ( ( rc = usb_get_descriptor ( usb, 0, USB_STRING_DESCRIPTOR, index,
  758. language, &desc->header,
  759. sizeof ( *desc ) ) ) != 0 )
  760. goto err_get_descriptor;
  761. /* Copy to buffer */
  762. actual = ( ( desc->header.len - sizeof ( desc->header ) ) /
  763. sizeof ( desc->character[0] ) );
  764. for ( i = 0 ; ( ( i < actual ) && ( i < max ) ) ; i++ )
  765. buf[i] = le16_to_cpu ( desc->character[i] );
  766. if ( len )
  767. buf[i] = '\0';
  768. /* Free buffer */
  769. free ( desc );
  770. return actual;
  771. err_get_descriptor:
  772. free ( desc );
  773. err_alloc:
  774. return rc;
  775. }
  776. /******************************************************************************
  777. *
  778. * USB device driver
  779. *
  780. ******************************************************************************
  781. */
  782. /**
  783. * Get USB configuration descriptor
  784. *
  785. * @v usb USB device
  786. * @v index Configuration index
  787. * @ret config Configuration descriptor
  788. * @ret rc Return status code
  789. *
  790. * The configuration descriptor is dynamically allocated and must
  791. * eventually be freed by the caller.
  792. */
  793. static int
  794. usb_config_descriptor ( struct usb_device *usb, unsigned int index,
  795. struct usb_configuration_descriptor **config ) {
  796. struct usb_configuration_descriptor partial;
  797. size_t len;
  798. int rc;
  799. /* Read first part of configuration descriptor to get size */
  800. if ( ( rc = usb_get_config_descriptor ( usb, index, &partial,
  801. sizeof ( partial ) ) ) != 0 ) {
  802. DBGC ( usb, "USB %s could not get configuration descriptor %d: "
  803. "%s\n", usb->name, index, strerror ( rc ) );
  804. goto err_get_partial;
  805. }
  806. len = le16_to_cpu ( partial.len );
  807. if ( len < sizeof ( partial ) ) {
  808. DBGC ( usb, "USB %s underlength configuraton descriptor %d\n",
  809. usb->name, index );
  810. rc = -EINVAL;
  811. goto err_partial_len;
  812. }
  813. /* Allocate buffer for whole configuration descriptor */
  814. *config = malloc ( len );
  815. if ( ! *config ) {
  816. rc = -ENOMEM;
  817. goto err_alloc_config;
  818. }
  819. /* Read whole configuration descriptor */
  820. if ( ( rc = usb_get_config_descriptor ( usb, index, *config,
  821. len ) ) != 0 ) {
  822. DBGC ( usb, "USB %s could not get configuration descriptor %d: "
  823. "%s\n", usb->name, index, strerror ( rc ) );
  824. goto err_get_config_descriptor;
  825. }
  826. if ( (*config)->len != partial.len ) {
  827. DBGC ( usb, "USB %s bad configuration descriptor %d length\n",
  828. usb->name, index );
  829. rc = -EINVAL;
  830. goto err_config_len;
  831. }
  832. return 0;
  833. err_config_len:
  834. err_get_config_descriptor:
  835. free ( *config );
  836. err_alloc_config:
  837. err_partial_len:
  838. err_get_partial:
  839. return rc;
  840. }
  841. /**
  842. * Describe USB function
  843. *
  844. * @v usb USB device
  845. * @v config Configuration descriptor
  846. * @v first First interface number
  847. * @v interfaces Interface list to fill in
  848. * @v desc Function descriptor to fill in
  849. * @ret rc Return status code
  850. */
  851. static int usb_describe ( struct usb_device *usb,
  852. struct usb_configuration_descriptor *config,
  853. unsigned int first, uint8_t *interfaces,
  854. struct usb_function_descriptor *desc ) {
  855. struct usb_interface_association_descriptor *association;
  856. struct usb_interface_descriptor *interface;
  857. struct cdc_union_descriptor *cdc_union;
  858. unsigned int i;
  859. /* Fill in vendor and product ID */
  860. memset ( desc, 0, sizeof ( *desc ) );
  861. desc->vendor = le16_to_cpu ( usb->device.vendor );
  862. desc->product = le16_to_cpu ( usb->device.product );
  863. /* First, look for an interface association descriptor */
  864. association = usb_interface_association_descriptor ( config, first );
  865. if ( association ) {
  866. /* Sanity check */
  867. assert ( association->first == first );
  868. if ( ( first + association->count ) > config->interfaces ) {
  869. DBGC ( usb, "USB %s has invalid association [%d-%d)\n",
  870. usb->name, first, ( first + association->count));
  871. return -ERANGE;
  872. }
  873. /* Describe function */
  874. memcpy ( &desc->class.class, &association->class,
  875. sizeof ( desc->class.class ) );
  876. desc->count = association->count;
  877. for ( i = 0 ; i < association->count ; i++ )
  878. interfaces[i] = ( first + i );
  879. return 0;
  880. }
  881. /* Next, look for an interface descriptor */
  882. interface = usb_interface_descriptor ( config, first, 0 );
  883. if ( ! interface ) {
  884. DBGC ( usb, "USB %s has no descriptor for interface %d\n",
  885. usb->name, first );
  886. return -ENOENT;
  887. }
  888. /* Describe function */
  889. memcpy ( &desc->class.class, &interface->class,
  890. sizeof ( desc->class.class ) );
  891. desc->count = 1;
  892. interfaces[0] = first;
  893. /* Look for a CDC union descriptor, if applicable */
  894. if ( ( desc->class.class.class == USB_CLASS_CDC ) &&
  895. ( cdc_union = cdc_union_descriptor ( config, interface ) ) ) {
  896. /* Determine interface count */
  897. desc->count = ( ( cdc_union->header.len -
  898. offsetof ( typeof ( *cdc_union ),
  899. interface[0] ) ) /
  900. sizeof ( cdc_union->interface[0] ) );
  901. if ( desc->count > config->interfaces ) {
  902. DBGC ( usb, "USB %s has invalid union functional "
  903. "descriptor with %d interfaces\n",
  904. usb->name, desc->count );
  905. return -ERANGE;
  906. }
  907. /* Describe function */
  908. for ( i = 0 ; i < desc->count ; i++ ) {
  909. if ( cdc_union->interface[i] >= config->interfaces ) {
  910. DBGC ( usb, "USB %s has invalid union "
  911. "functional descriptor covering "
  912. "interface %d\n", usb->name,
  913. cdc_union->interface[i] );
  914. return -ERANGE;
  915. }
  916. interfaces[i] = cdc_union->interface[i];
  917. }
  918. return 0;
  919. }
  920. return 0;
  921. }
  922. /**
  923. * Update list of used interface
  924. *
  925. * @v usb USB device
  926. * @v count Number of interfaces
  927. * @v interface List of interfaces
  928. * @v used List of already-used interfaces
  929. * @ret rc Return status code
  930. */
  931. static int usb_used ( struct usb_device *usb, unsigned int count,
  932. uint8_t *interface, uint8_t *used ) {
  933. unsigned int i;
  934. for ( i = 0 ; i < count ; i++ ) {
  935. if ( used[interface[i]] ) {
  936. DBGC ( usb, "USB %s interface %d already in use\n",
  937. usb->name, interface[i] );
  938. return -EINVAL;
  939. }
  940. used[interface[i]] = 1;
  941. }
  942. return 0;
  943. }
  944. /**
  945. * Find USB device driver
  946. *
  947. * @v desc Function descriptor
  948. * @ret id USB device ID, or NULL
  949. * @ret driver USB device driver, or NULL
  950. */
  951. struct usb_driver * usb_find_driver ( struct usb_function_descriptor *desc,
  952. struct usb_device_id **id ) {
  953. struct usb_driver *driver;
  954. unsigned int i;
  955. /* Look for a matching driver */
  956. for_each_table_entry ( driver, USB_DRIVERS ) {
  957. for ( i = 0 ; i < driver->id_count ; i++ ) {
  958. /* Ignore non-matching driver class */
  959. if ( ( driver->class.class.scalar ^ desc->class.scalar )
  960. & driver->class.mask.scalar )
  961. continue;
  962. /* Look for a matching ID */
  963. *id = &driver->ids[i];
  964. if ( ( ( (*id)->vendor == desc->vendor ) ||
  965. ( (*id)->vendor == USB_ANY_ID ) ) &&
  966. ( ( (*id)->product == desc->product ) ||
  967. ( (*id)->product == USB_ANY_ID ) ) )
  968. return driver;
  969. }
  970. }
  971. /* Not found */
  972. *id = NULL;
  973. return NULL;
  974. }
  975. /**
  976. * Get USB device configuration score
  977. *
  978. * @v usb USB device
  979. * @v config Configuration descriptor
  980. * @ret score Device configuration score, or negative error
  981. */
  982. static int usb_score ( struct usb_device *usb,
  983. struct usb_configuration_descriptor *config ) {
  984. uint8_t used[config->interfaces];
  985. uint8_t interface[config->interfaces];
  986. struct usb_function_descriptor desc;
  987. struct usb_driver *driver;
  988. struct usb_device_id *id;
  989. unsigned int first;
  990. unsigned int score = 0;
  991. int rc;
  992. /* Identify each function in turn */
  993. memset ( used, 0, sizeof ( used ) );
  994. for ( first = 0 ; first < config->interfaces ; first++ ) {
  995. /* Skip interfaces already used */
  996. if ( used[first] )
  997. continue;
  998. /* Describe function */
  999. if ( ( rc = usb_describe ( usb, config, first, interface,
  1000. &desc ) ) != 0 )
  1001. return rc;
  1002. /* Update used interfaces */
  1003. if ( ( rc = usb_used ( usb, desc.count, interface,
  1004. used ) ) != 0 )
  1005. return rc;
  1006. /* Look for a driver for this function */
  1007. driver = usb_find_driver ( &desc, &id );
  1008. if ( driver )
  1009. score += driver->score;
  1010. }
  1011. return score;
  1012. }
  1013. /**
  1014. * Probe USB device driver
  1015. *
  1016. * @v func USB function
  1017. * @v config Configuration descriptor
  1018. * @ret rc Return status code
  1019. */
  1020. static int usb_probe ( struct usb_function *func,
  1021. struct usb_configuration_descriptor *config ) {
  1022. struct usb_device *usb = func->usb;
  1023. struct usb_driver *driver;
  1024. struct usb_device_id *id;
  1025. int rc;
  1026. /* Identify driver */
  1027. driver = usb_find_driver ( &func->desc, &id );
  1028. if ( ! driver ) {
  1029. DBGC ( usb, "USB %s %04x:%04x class %d:%d:%d has no driver\n",
  1030. func->name, func->desc.vendor, func->desc.product,
  1031. func->desc.class.class.class,
  1032. func->desc.class.class.subclass,
  1033. func->desc.class.class.protocol );
  1034. return -ENOENT;
  1035. }
  1036. /* Record driver */
  1037. func->driver = driver;
  1038. func->id = id;
  1039. func->dev.driver_name = id->name;
  1040. /* Probe driver */
  1041. if ( ( rc = driver->probe ( func, config ) ) != 0 ) {
  1042. DBGC ( usb, "USB %s failed to probe driver %s: %s\n",
  1043. func->name, id->name, strerror ( rc ) );
  1044. return rc;
  1045. }
  1046. return 0;
  1047. }
  1048. /**
  1049. * Remove USB device driver
  1050. *
  1051. * @v func USB function
  1052. */
  1053. static void usb_remove ( struct usb_function *func ) {
  1054. /* Remove driver */
  1055. func->driver->remove ( func );
  1056. }
  1057. /**
  1058. * Probe all USB device drivers
  1059. *
  1060. * @v usb USB device
  1061. * @v config Configuration descriptor
  1062. */
  1063. static void
  1064. usb_probe_all ( struct usb_device *usb,
  1065. struct usb_configuration_descriptor *config ) {
  1066. struct usb_bus *bus = usb->port->hub->bus;
  1067. struct usb_function *func;
  1068. uint8_t used[config->interfaces];
  1069. unsigned int first;
  1070. unsigned int i;
  1071. int rc;
  1072. /* Identify each function in turn */
  1073. memset ( used, 0, sizeof ( used ) );
  1074. for ( first = 0 ; first < config->interfaces ; first++ ) {
  1075. /* Skip interfaces already used */
  1076. if ( used[first] )
  1077. continue;
  1078. /* Allocate and initialise structure */
  1079. func = zalloc ( sizeof ( *func ) +
  1080. ( config->interfaces *
  1081. sizeof ( func->interface[0] ) ) );
  1082. if ( ! func )
  1083. goto err_alloc;
  1084. func->name = func->dev.name;
  1085. func->usb = usb;
  1086. func->dev.desc.bus_type = BUS_TYPE_USB;
  1087. func->dev.desc.location = usb->address;
  1088. func->dev.desc.vendor = le16_to_cpu ( usb->device.vendor );
  1089. func->dev.desc.device = le16_to_cpu ( usb->device.product );
  1090. snprintf ( func->dev.name, sizeof ( func->dev.name ),
  1091. "%s-%d.%d", usb->name, config->config, first );
  1092. INIT_LIST_HEAD ( &func->dev.children );
  1093. func->dev.parent = bus->dev;
  1094. list_add_tail ( &func->list, &usb->functions );
  1095. /* Identify function */
  1096. if ( ( rc = usb_describe ( usb, config, first, func->interface,
  1097. &func->desc ) ) != 0 )
  1098. goto err_describe;
  1099. assert ( func->desc.count <= config->interfaces );
  1100. /* Mark interfaces as used */
  1101. if ( ( rc = usb_used ( usb, func->desc.count, func->interface,
  1102. used ) ) != 0 )
  1103. goto err_used;
  1104. /* Probe device driver */
  1105. if ( ( rc = usb_probe ( func, config ) ) != 0 )
  1106. goto err_probe;
  1107. DBGC ( usb, "USB %s %04x:%04x class %d:%d:%d interfaces ",
  1108. func->name, func->desc.vendor, func->desc.product,
  1109. func->desc.class.class.class,
  1110. func->desc.class.class.subclass,
  1111. func->desc.class.class.protocol );
  1112. for ( i = 0 ; i < func->desc.count ; i++ )
  1113. DBGC ( usb, "%s%d", ( i ? "," : "" ),
  1114. func->interface[i] );
  1115. DBGC ( usb, " using driver %s\n", func->dev.driver_name );
  1116. /* Add to device hierarchy */
  1117. list_add_tail ( &func->dev.siblings, &bus->dev->children );
  1118. continue;
  1119. list_del ( &func->dev.siblings );
  1120. usb_remove ( func );
  1121. err_probe:
  1122. err_used:
  1123. err_describe:
  1124. list_del ( &func->list );
  1125. free ( func );
  1126. err_alloc:
  1127. /* Continue registering other functions */
  1128. continue;
  1129. }
  1130. }
  1131. /**
  1132. * Remove all device drivers
  1133. *
  1134. * @v usb USB device
  1135. */
  1136. static void usb_remove_all ( struct usb_device *usb ) {
  1137. struct usb_function *func;
  1138. struct usb_function *tmp;
  1139. /* Remove all functions */
  1140. list_for_each_entry_safe ( func, tmp, &usb->functions, list ) {
  1141. /* Remove device driver */
  1142. usb_remove ( func );
  1143. /* Remove from device hierarchy */
  1144. assert ( list_empty ( &func->dev.children ) );
  1145. list_del ( &func->dev.siblings );
  1146. /* Remove from list of functions */
  1147. list_del ( &func->list );
  1148. /* Free function */
  1149. free ( func );
  1150. }
  1151. }
  1152. /**
  1153. * Clear USB device configuration
  1154. *
  1155. * @v usb USB device
  1156. */
  1157. static void usb_deconfigure ( struct usb_device *usb ) {
  1158. unsigned int i;
  1159. /* Remove device drivers */
  1160. usb_remove_all ( usb );
  1161. /* Sanity checks */
  1162. for ( i = 0 ; i < ( sizeof ( usb->ep ) / sizeof ( usb->ep[0] ) ) ; i++){
  1163. if ( i != USB_ENDPOINT_IDX ( USB_EP0_ADDRESS ) )
  1164. assert ( usb->ep[i] == NULL );
  1165. }
  1166. /* Clear device configuration */
  1167. usb_set_configuration ( usb, 0 );
  1168. }
  1169. /**
  1170. * Choose our preferred USB device configuration
  1171. *
  1172. * @v usb USB device
  1173. * @ret rc Return status code
  1174. */
  1175. static int usb_autoconfigure ( struct usb_device *usb ) {
  1176. struct usb_configuration_descriptor *config;
  1177. unsigned int preferred = 0;
  1178. unsigned int index;
  1179. int score;
  1180. int best = 0;
  1181. int rc;
  1182. /* Calculate driver score for each configuration index */
  1183. for ( index = 0 ; index < usb->device.configurations ; index++ ) {
  1184. /* Read configuration descriptor */
  1185. if ( ( rc = usb_config_descriptor ( usb, index,
  1186. &config ) ) != 0 )
  1187. goto err_config;
  1188. /* Get score for this configuration */
  1189. score = usb_score ( usb, config );
  1190. if ( score < 0 ) {
  1191. rc = score;
  1192. goto err_score;
  1193. }
  1194. DBGC2 ( usb, "USB %s configuration %d score %d\n",
  1195. usb->name, config->config, score );
  1196. /* Record as preferred configuration, if applicable */
  1197. if ( score > best ) {
  1198. best = score;
  1199. preferred = index;
  1200. }
  1201. /* Free configuration descriptor */
  1202. free ( config );
  1203. config = NULL;
  1204. }
  1205. /* Read preferred configuration descriptor */
  1206. if ( ( rc = usb_config_descriptor ( usb, preferred, &config ) ) != 0 )
  1207. goto err_preferred;
  1208. /* Set configuration */
  1209. if ( ( rc = usb_set_configuration ( usb, config->config ) ) != 0){
  1210. DBGC ( usb, "USB %s could not set configuration %d: %s\n",
  1211. usb->name, config->config, strerror ( rc ) );
  1212. goto err_set_configuration;
  1213. }
  1214. /* Probe USB device drivers */
  1215. usb_probe_all ( usb, config );
  1216. /* Free configuration descriptor */
  1217. free ( config );
  1218. return 0;
  1219. usb_remove_all ( usb );
  1220. usb_set_configuration ( usb, 0 );
  1221. err_set_configuration:
  1222. free ( config );
  1223. err_preferred:
  1224. return rc;
  1225. err_score:
  1226. free ( config );
  1227. err_config:
  1228. return rc;
  1229. }
  1230. /******************************************************************************
  1231. *
  1232. * USB device
  1233. *
  1234. ******************************************************************************
  1235. */
  1236. /**
  1237. * Allocate USB device
  1238. *
  1239. * @v port USB port
  1240. * @ret usb USB device, or NULL on allocation failure
  1241. */
  1242. static struct usb_device * alloc_usb ( struct usb_port *port ) {
  1243. struct usb_hub *hub = port->hub;
  1244. struct usb_bus *bus = hub->bus;
  1245. struct usb_device *usb;
  1246. /* Allocate and initialise structure */
  1247. usb = zalloc ( sizeof ( *usb ) );
  1248. if ( ! usb )
  1249. return NULL;
  1250. snprintf ( usb->name, sizeof ( usb->name ), "%s%c%d", hub->name,
  1251. ( hub->usb ? '.' : '-' ), port->address );
  1252. usb->port = port;
  1253. INIT_LIST_HEAD ( &usb->functions );
  1254. usb->host = &bus->op->device;
  1255. usb_endpoint_init ( &usb->control, usb, &usb_control_operations );
  1256. INIT_LIST_HEAD ( &usb->complete );
  1257. return usb;
  1258. }
  1259. /**
  1260. * Register USB device
  1261. *
  1262. * @v usb USB device
  1263. * @ret rc Return status code
  1264. */
  1265. static int register_usb ( struct usb_device *usb ) {
  1266. struct usb_port *port = usb->port;
  1267. struct usb_hub *hub = port->hub;
  1268. struct usb_bus *bus = hub->bus;
  1269. unsigned int protocol;
  1270. size_t mtu;
  1271. int rc;
  1272. /* Add to port */
  1273. if ( port->usb != NULL ) {
  1274. DBGC ( hub, "USB hub %s port %d is already registered to %s\n",
  1275. hub->name, port->address, port->usb->name );
  1276. rc = -EALREADY;
  1277. goto err_already;
  1278. }
  1279. port->usb = usb;
  1280. /* Add to bus device list */
  1281. list_add_tail ( &usb->list, &bus->devices );
  1282. /* Enable device */
  1283. if ( ( rc = hub->driver->enable ( hub, port ) ) != 0 ) {
  1284. DBGC ( hub, "USB hub %s port %d could not enable: %s\n",
  1285. hub->name, port->address, strerror ( rc ) );
  1286. goto err_enable;
  1287. }
  1288. /* Allow recovery interval since port may have been reset */
  1289. mdelay ( USB_RESET_RECOVER_DELAY_MS );
  1290. /* Get device speed */
  1291. if ( ( rc = hub->driver->speed ( hub, port ) ) != 0 ) {
  1292. DBGC ( hub, "USB hub %s port %d could not get speed: %s\n",
  1293. hub->name, port->address, strerror ( rc ) );
  1294. goto err_speed;
  1295. }
  1296. usb->speed = port->speed;
  1297. DBGC2 ( usb, "USB %s attached as %s-speed device\n",
  1298. usb->name, usb_speed_name ( usb->speed ) );
  1299. /* Open device */
  1300. if ( ( rc = usb->host->open ( usb ) ) != 0 ) {
  1301. DBGC ( usb, "USB %s could not open: %s\n",
  1302. usb->name, strerror ( rc ) );
  1303. goto err_open;
  1304. }
  1305. /* Describe control endpoint */
  1306. mtu = USB_EP0_DEFAULT_MTU ( usb->speed );
  1307. usb_endpoint_describe ( &usb->control, USB_EP0_ADDRESS,
  1308. USB_EP0_ATTRIBUTES, mtu, USB_EP0_BURST,
  1309. USB_EP0_INTERVAL );
  1310. /* Open control endpoint */
  1311. if ( ( rc = usb_endpoint_open ( &usb->control ) ) != 0 )
  1312. goto err_open_control;
  1313. assert ( usb_endpoint ( usb, USB_EP0_ADDRESS ) == &usb->control );
  1314. /* Assign device address */
  1315. if ( ( rc = usb->host->address ( usb ) ) != 0 ) {
  1316. DBGC ( usb, "USB %s could not set address: %s\n",
  1317. usb->name, strerror ( rc ) );
  1318. goto err_address;
  1319. }
  1320. DBGC2 ( usb, "USB %s assigned address %d\n", usb->name, usb->address );
  1321. /* Allow recovery interval after Set Address command */
  1322. mdelay ( USB_SET_ADDRESS_RECOVER_DELAY_MS );
  1323. /* Read first part of device descriptor to get EP0 MTU */
  1324. if ( ( rc = usb_get_mtu ( usb, &usb->device ) ) != 0 ) {
  1325. DBGC ( usb, "USB %s could not get MTU: %s\n",
  1326. usb->name, strerror ( rc ) );
  1327. goto err_get_mtu;
  1328. }
  1329. /* Calculate EP0 MTU */
  1330. protocol = le16_to_cpu ( usb->device.protocol );
  1331. mtu = ( ( protocol < USB_PROTO_3_0 ) ?
  1332. usb->device.mtu : ( 1 << usb->device.mtu ) );
  1333. DBGC2 ( usb, "USB %s has control MTU %zd (guessed %zd)\n",
  1334. usb->name, mtu, usb->control.mtu );
  1335. /* Update MTU */
  1336. if ( ( rc = usb_endpoint_mtu ( &usb->control, mtu ) ) != 0 )
  1337. goto err_mtu;
  1338. /* Read whole device descriptor */
  1339. if ( ( rc = usb_get_device_descriptor ( usb, &usb->device ) ) != 0 ) {
  1340. DBGC ( usb, "USB %s could not get device descriptor: %s\n",
  1341. usb->name, strerror ( rc ) );
  1342. goto err_get_device_descriptor;
  1343. }
  1344. DBGC ( usb, "USB %s addr %d %04x:%04x class %d:%d:%d (v%s, %s-speed, "
  1345. "MTU %zd)\n", usb->name, usb->address,
  1346. le16_to_cpu ( usb->device.vendor ),
  1347. le16_to_cpu ( usb->device.product ), usb->device.class.class,
  1348. usb->device.class.subclass, usb->device.class.protocol,
  1349. usb_bcd ( le16_to_cpu ( usb->device.protocol ) ),
  1350. usb_speed_name ( usb->speed ), usb->control.mtu );
  1351. /* Configure device */
  1352. if ( ( rc = usb_autoconfigure ( usb ) ) != 0 )
  1353. goto err_autoconfigure;
  1354. return 0;
  1355. usb_deconfigure ( usb );
  1356. err_autoconfigure:
  1357. err_get_device_descriptor:
  1358. err_mtu:
  1359. err_get_mtu:
  1360. err_address:
  1361. usb_endpoint_close ( &usb->control );
  1362. err_open_control:
  1363. usb->host->close ( usb );
  1364. err_open:
  1365. err_speed:
  1366. hub->driver->disable ( hub, port );
  1367. err_enable:
  1368. list_del ( &usb->list );
  1369. port->usb = NULL;
  1370. err_already:
  1371. return rc;
  1372. }
  1373. /**
  1374. * Unregister USB device
  1375. *
  1376. * @v usb USB device
  1377. */
  1378. static void unregister_usb ( struct usb_device *usb ) {
  1379. struct usb_port *port = usb->port;
  1380. struct usb_hub *hub = port->hub;
  1381. struct io_buffer *iobuf;
  1382. struct io_buffer *tmp;
  1383. /* Sanity checks */
  1384. assert ( port->usb == usb );
  1385. /* Clear device configuration */
  1386. usb_deconfigure ( usb );
  1387. /* Close control endpoint */
  1388. usb_endpoint_close ( &usb->control );
  1389. /* Discard any stale control completions */
  1390. list_for_each_entry_safe ( iobuf, tmp, &usb->complete, list ) {
  1391. list_del ( &iobuf->list );
  1392. free_iob ( iobuf );
  1393. }
  1394. /* Close device */
  1395. usb->host->close ( usb );
  1396. /* Disable port */
  1397. hub->driver->disable ( hub, port );
  1398. /* Remove from bus device list */
  1399. list_del ( &usb->list );
  1400. /* Remove from port */
  1401. port->usb = NULL;
  1402. }
  1403. /**
  1404. * Free USB device
  1405. *
  1406. * @v usb USB device
  1407. */
  1408. static void free_usb ( struct usb_device *usb ) {
  1409. unsigned int i;
  1410. /* Sanity checks */
  1411. for ( i = 0 ; i < ( sizeof ( usb->ep ) / sizeof ( usb->ep[0] ) ) ; i++ )
  1412. assert ( usb->ep[i] == NULL );
  1413. assert ( list_empty ( &usb->functions ) );
  1414. assert ( list_empty ( &usb->complete ) );
  1415. /* Free device */
  1416. free ( usb );
  1417. }
  1418. /******************************************************************************
  1419. *
  1420. * USB device hotplug event handling
  1421. *
  1422. ******************************************************************************
  1423. */
  1424. /**
  1425. * Handle newly attached USB device
  1426. *
  1427. * @v port USB port
  1428. * @ret rc Return status code
  1429. */
  1430. static int usb_attached ( struct usb_port *port ) {
  1431. struct usb_device *usb;
  1432. int rc;
  1433. /* Mark port as attached */
  1434. port->attached = 1;
  1435. /* Sanity checks */
  1436. assert ( port->usb == NULL );
  1437. /* Allocate USB device */
  1438. usb = alloc_usb ( port );
  1439. if ( ! usb ) {
  1440. rc = -ENOMEM;
  1441. goto err_alloc;
  1442. }
  1443. /* Register USB device */
  1444. if ( ( rc = register_usb ( usb ) ) != 0 )
  1445. goto err_register;
  1446. return 0;
  1447. unregister_usb ( usb );
  1448. err_register:
  1449. free_usb ( usb );
  1450. err_alloc:
  1451. return rc;
  1452. }
  1453. /**
  1454. * Handle newly detached USB device
  1455. *
  1456. * @v port USB port
  1457. */
  1458. static void usb_detached ( struct usb_port *port ) {
  1459. struct usb_device *usb = port->usb;
  1460. /* Mark port as detached */
  1461. port->attached = 0;
  1462. /* Do nothing if we have no USB device */
  1463. if ( ! usb )
  1464. return;
  1465. /* Unregister USB device */
  1466. unregister_usb ( usb );
  1467. /* Free USB device */
  1468. free_usb ( usb );
  1469. }
  1470. /**
  1471. * Handle newly attached or detached USB device
  1472. *
  1473. * @v port USB port
  1474. * @ret rc Return status code
  1475. */
  1476. static int usb_hotplugged ( struct usb_port *port ) {
  1477. struct usb_hub *hub = port->hub;
  1478. int rc;
  1479. /* Get current port speed */
  1480. if ( ( rc = hub->driver->speed ( hub, port ) ) != 0 ) {
  1481. DBGC ( hub, "USB hub %s port %d could not get speed: %s\n",
  1482. hub->name, port->address, strerror ( rc ) );
  1483. /* Treat as a disconnection */
  1484. port->disconnected = 1;
  1485. port->speed = USB_SPEED_NONE;
  1486. }
  1487. /* Detach device, if applicable */
  1488. if ( port->attached && ( port->disconnected || ! port->speed ) )
  1489. usb_detached ( port );
  1490. /* Clear any recorded disconnections */
  1491. port->disconnected = 0;
  1492. /* Attach device, if applicable */
  1493. if ( port->speed && ( ! port->attached ) &&
  1494. ( ( rc = usb_attached ( port ) ) != 0 ) )
  1495. return rc;
  1496. return 0;
  1497. }
  1498. /******************************************************************************
  1499. *
  1500. * USB process
  1501. *
  1502. ******************************************************************************
  1503. */
  1504. /**
  1505. * Report port status change
  1506. *
  1507. * @v port USB port
  1508. */
  1509. void usb_port_changed ( struct usb_port *port ) {
  1510. /* Record hub port status change */
  1511. list_del ( &port->changed );
  1512. list_add_tail ( &port->changed, &usb_changed );
  1513. }
  1514. /**
  1515. * Handle newly attached or detached USB device
  1516. *
  1517. */
  1518. static void usb_hotplug ( void ) {
  1519. struct usb_port *port;
  1520. /* Handle any changed ports, allowing for the fact that the
  1521. * port list may change as we perform hotplug actions.
  1522. */
  1523. while ( ! list_empty ( &usb_changed ) ) {
  1524. /* Get first changed port */
  1525. port = list_first_entry ( &usb_changed, struct usb_port,
  1526. changed );
  1527. assert ( port != NULL );
  1528. /* Remove from list of changed ports */
  1529. list_del ( &port->changed );
  1530. INIT_LIST_HEAD ( &port->changed );
  1531. /* Perform appropriate hotplug action */
  1532. usb_hotplugged ( port );
  1533. }
  1534. }
  1535. /**
  1536. * USB process
  1537. *
  1538. * @v process USB process
  1539. */
  1540. static void usb_step ( struct process *process __unused ) {
  1541. struct usb_bus *bus;
  1542. struct usb_endpoint *ep;
  1543. /* Poll all buses */
  1544. for_each_usb_bus ( bus )
  1545. usb_poll ( bus );
  1546. /* Attempt to reset first halted endpoint in list, if any. We
  1547. * do not attempt to process the complete list, since this
  1548. * would require extra code to allow for the facts that the
  1549. * halted endpoint list may change as we do so, and that
  1550. * resetting an endpoint may fail.
  1551. */
  1552. if ( ( ep = list_first_entry ( &usb_halted, struct usb_endpoint,
  1553. halted ) ) != NULL )
  1554. usb_endpoint_reset ( ep );
  1555. /* Handle any changed ports */
  1556. usb_hotplug();
  1557. }
  1558. /** USB process */
  1559. PERMANENT_PROCESS ( usb_process, usb_step );
  1560. /******************************************************************************
  1561. *
  1562. * USB hub
  1563. *
  1564. ******************************************************************************
  1565. */
  1566. /**
  1567. * Allocate USB hub
  1568. *
  1569. * @v bus USB bus
  1570. * @v usb Underlying USB device, if any
  1571. * @v ports Number of ports
  1572. * @v driver Hub driver operations
  1573. * @ret hub USB hub, or NULL on allocation failure
  1574. */
  1575. struct usb_hub * alloc_usb_hub ( struct usb_bus *bus, struct usb_device *usb,
  1576. unsigned int ports,
  1577. struct usb_hub_driver_operations *driver ) {
  1578. struct usb_hub *hub;
  1579. struct usb_port *port;
  1580. unsigned int i;
  1581. /* Allocate and initialise structure */
  1582. hub = zalloc ( sizeof ( *hub ) + ( ports * sizeof ( hub->port[0] ) ) );
  1583. if ( ! hub )
  1584. return NULL;
  1585. hub->name = ( usb ? usb->name : bus->name );
  1586. hub->bus = bus;
  1587. hub->usb = usb;
  1588. if ( usb )
  1589. hub->protocol = usb->port->protocol;
  1590. hub->ports = ports;
  1591. hub->driver = driver;
  1592. hub->host = &bus->op->hub;
  1593. /* Initialise port list */
  1594. for ( i = 1 ; i <= hub->ports ; i++ ) {
  1595. port = usb_port ( hub, i );
  1596. port->hub = hub;
  1597. port->address = i;
  1598. if ( usb )
  1599. port->protocol = usb->port->protocol;
  1600. INIT_LIST_HEAD ( &port->changed );
  1601. }
  1602. return hub;
  1603. }
  1604. /**
  1605. * Register USB hub
  1606. *
  1607. * @v hub USB hub
  1608. * @ret rc Return status code
  1609. */
  1610. int register_usb_hub ( struct usb_hub *hub ) {
  1611. struct usb_bus *bus = hub->bus;
  1612. struct usb_port *port;
  1613. unsigned int i;
  1614. int rc;
  1615. /* Add to hub list */
  1616. list_add_tail ( &hub->list, &bus->hubs );
  1617. /* Open hub (host controller) */
  1618. if ( ( rc = hub->host->open ( hub ) ) != 0 ) {
  1619. DBGC ( hub, "USB hub %s could not open: %s\n",
  1620. hub->name, strerror ( rc ) );
  1621. goto err_host_open;
  1622. }
  1623. /* Open hub (driver) */
  1624. if ( ( rc = hub->driver->open ( hub ) ) != 0 ) {
  1625. DBGC ( hub, "USB hub %s could not open: %s\n",
  1626. hub->name, strerror ( rc ) );
  1627. goto err_driver_open;
  1628. }
  1629. /* Delay to allow ports to stabilise */
  1630. mdelay ( USB_PORT_DELAY_MS );
  1631. /* Mark all ports as changed */
  1632. for ( i = 1 ; i <= hub->ports ; i++ ) {
  1633. port = usb_port ( hub, i );
  1634. usb_port_changed ( port );
  1635. }
  1636. /* Some hubs seem to defer reporting device connections until
  1637. * their interrupt endpoint is polled for the first time.
  1638. * Poll the bus once now in order to pick up any such
  1639. * connections.
  1640. */
  1641. usb_poll ( bus );
  1642. return 0;
  1643. hub->driver->close ( hub );
  1644. err_driver_open:
  1645. hub->host->close ( hub );
  1646. err_host_open:
  1647. list_del ( &hub->list );
  1648. return rc;
  1649. }
  1650. /**
  1651. * Unregister USB hub
  1652. *
  1653. * @v hub USB hub
  1654. */
  1655. void unregister_usb_hub ( struct usb_hub *hub ) {
  1656. struct usb_port *port;
  1657. unsigned int i;
  1658. /* Detach all devices */
  1659. for ( i = 1 ; i <= hub->ports ; i++ ) {
  1660. port = usb_port ( hub, i );
  1661. if ( port->attached )
  1662. usb_detached ( port );
  1663. }
  1664. /* Close hub (driver) */
  1665. hub->driver->close ( hub );
  1666. /* Close hub (host controller) */
  1667. hub->host->close ( hub );
  1668. /* Cancel any pending port status changes */
  1669. for ( i = 1 ; i <= hub->ports ; i++ ) {
  1670. port = usb_port ( hub, i );
  1671. list_del ( &port->changed );
  1672. INIT_LIST_HEAD ( &port->changed );
  1673. }
  1674. /* Remove from hub list */
  1675. list_del ( &hub->list );
  1676. }
  1677. /**
  1678. * Free USB hub
  1679. *
  1680. * @v hub USB hub
  1681. */
  1682. void free_usb_hub ( struct usb_hub *hub ) {
  1683. struct usb_port *port;
  1684. unsigned int i;
  1685. /* Sanity checks */
  1686. for ( i = 1 ; i <= hub->ports ; i++ ) {
  1687. port = usb_port ( hub, i );
  1688. assert ( ! port->attached );
  1689. assert ( port->usb == NULL );
  1690. assert ( list_empty ( &port->changed ) );
  1691. }
  1692. /* Free hub */
  1693. free ( hub );
  1694. }
  1695. /******************************************************************************
  1696. *
  1697. * USB bus
  1698. *
  1699. ******************************************************************************
  1700. */
  1701. /**
  1702. * Allocate USB bus
  1703. *
  1704. * @v dev Underlying hardware device
  1705. * @v ports Number of root hub ports
  1706. * @v mtu Largest transfer allowed on the bus
  1707. * @v op Host controller operations
  1708. * @ret bus USB bus, or NULL on allocation failure
  1709. */
  1710. struct usb_bus * alloc_usb_bus ( struct device *dev, unsigned int ports,
  1711. size_t mtu, struct usb_host_operations *op ) {
  1712. struct usb_bus *bus;
  1713. /* Allocate and initialise structure */
  1714. bus = zalloc ( sizeof ( *bus ) );
  1715. if ( ! bus )
  1716. goto err_alloc_bus;
  1717. bus->name = dev->name;
  1718. bus->dev = dev;
  1719. bus->mtu = mtu;
  1720. bus->op = op;
  1721. INIT_LIST_HEAD ( &bus->devices );
  1722. INIT_LIST_HEAD ( &bus->hubs );
  1723. bus->host = &bus->op->bus;
  1724. /* Allocate root hub */
  1725. bus->hub = alloc_usb_hub ( bus, NULL, ports, &op->root );
  1726. if ( ! bus->hub )
  1727. goto err_alloc_hub;
  1728. return bus;
  1729. free_usb_hub ( bus->hub );
  1730. err_alloc_hub:
  1731. free ( bus );
  1732. err_alloc_bus:
  1733. return NULL;
  1734. }
  1735. /**
  1736. * Register USB bus
  1737. *
  1738. * @v bus USB bus
  1739. * @ret rc Return status code
  1740. */
  1741. int register_usb_bus ( struct usb_bus *bus ) {
  1742. int rc;
  1743. /* Sanity checks */
  1744. assert ( bus->hub != NULL );
  1745. /* Open bus */
  1746. if ( ( rc = bus->host->open ( bus ) ) != 0 )
  1747. goto err_open;
  1748. /* Add to list of USB buses */
  1749. list_add_tail ( &bus->list, &usb_buses );
  1750. /* Register root hub */
  1751. if ( ( rc = register_usb_hub ( bus->hub ) ) != 0 )
  1752. goto err_register_hub;
  1753. /* Attach any devices already present */
  1754. usb_hotplug();
  1755. return 0;
  1756. unregister_usb_hub ( bus->hub );
  1757. err_register_hub:
  1758. list_del ( &bus->list );
  1759. bus->host->close ( bus );
  1760. err_open:
  1761. return rc;
  1762. }
  1763. /**
  1764. * Unregister USB bus
  1765. *
  1766. * @v bus USB bus
  1767. */
  1768. void unregister_usb_bus ( struct usb_bus *bus ) {
  1769. /* Sanity checks */
  1770. assert ( bus->hub != NULL );
  1771. /* Unregister root hub */
  1772. unregister_usb_hub ( bus->hub );
  1773. /* Remove from list of USB buses */
  1774. list_del ( &bus->list );
  1775. /* Close bus */
  1776. bus->host->close ( bus );
  1777. /* Sanity checks */
  1778. assert ( list_empty ( &bus->devices ) );
  1779. assert ( list_empty ( &bus->hubs ) );
  1780. }
  1781. /**
  1782. * Free USB bus
  1783. *
  1784. * @v bus USB bus
  1785. */
  1786. void free_usb_bus ( struct usb_bus *bus ) {
  1787. struct usb_endpoint *ep;
  1788. struct usb_port *port;
  1789. /* Sanity checks */
  1790. assert ( list_empty ( &bus->devices ) );
  1791. assert ( list_empty ( &bus->hubs ) );
  1792. list_for_each_entry ( ep, &usb_halted, halted )
  1793. assert ( ep->usb->port->hub->bus != bus );
  1794. list_for_each_entry ( port, &usb_changed, changed )
  1795. assert ( port->hub->bus != bus );
  1796. /* Free root hub */
  1797. free_usb_hub ( bus->hub );
  1798. /* Free bus */
  1799. free ( bus );
  1800. }
  1801. /**
  1802. * Find USB bus by device location
  1803. *
  1804. * @v bus_type Bus type
  1805. * @v location Bus location
  1806. * @ret bus USB bus, or NULL
  1807. */
  1808. struct usb_bus * find_usb_bus_by_location ( unsigned int bus_type,
  1809. unsigned int location ) {
  1810. struct usb_bus *bus;
  1811. for_each_usb_bus ( bus ) {
  1812. if ( ( bus->dev->desc.bus_type == bus_type ) &&
  1813. ( bus->dev->desc.location == location ) )
  1814. return bus;
  1815. }
  1816. return NULL;
  1817. }
  1818. /******************************************************************************
  1819. *
  1820. * USB address assignment
  1821. *
  1822. ******************************************************************************
  1823. */
  1824. /**
  1825. * Allocate device address
  1826. *
  1827. * @v bus USB bus
  1828. * @ret address Device address, or negative error
  1829. */
  1830. int usb_alloc_address ( struct usb_bus *bus ) {
  1831. unsigned int address;
  1832. /* Find first free device address */
  1833. address = ffsll ( ~bus->addresses );
  1834. if ( ! address )
  1835. return -ENOENT;
  1836. /* Mark address as used */
  1837. bus->addresses |= ( 1ULL << ( address - 1 ) );
  1838. return address;
  1839. }
  1840. /**
  1841. * Free device address
  1842. *
  1843. * @v bus USB bus
  1844. * @v address Device address
  1845. */
  1846. void usb_free_address ( struct usb_bus *bus, unsigned int address ) {
  1847. /* Sanity check */
  1848. assert ( address > 0 );
  1849. assert ( bus->addresses & ( 1ULL << ( address - 1 ) ) );
  1850. /* Mark address as free */
  1851. bus->addresses &= ~( 1ULL << ( address - 1 ) );
  1852. }
  1853. /******************************************************************************
  1854. *
  1855. * USB bus topology
  1856. *
  1857. ******************************************************************************
  1858. */
  1859. /**
  1860. * Get USB route string
  1861. *
  1862. * @v usb USB device
  1863. * @ret route USB route string
  1864. */
  1865. unsigned int usb_route_string ( struct usb_device *usb ) {
  1866. struct usb_device *parent;
  1867. unsigned int route;
  1868. /* Navigate up to root hub, constructing route string as we go */
  1869. for ( route = 0 ; ( parent = usb->port->hub->usb ) ; usb = parent ) {
  1870. route <<= 4;
  1871. route |= ( ( usb->port->address > 0xf ) ?
  1872. 0xf : usb->port->address );
  1873. }
  1874. return route;
  1875. }
  1876. /**
  1877. * Get USB depth
  1878. *
  1879. * @v usb USB device
  1880. * @ret depth Hub depth
  1881. */
  1882. unsigned int usb_depth ( struct usb_device *usb ) {
  1883. struct usb_device *parent;
  1884. unsigned int depth;
  1885. /* Navigate up to root hub, constructing depth as we go */
  1886. for ( depth = 0 ; ( parent = usb->port->hub->usb ) ; usb = parent )
  1887. depth++;
  1888. return depth;
  1889. }
  1890. /**
  1891. * Get USB root hub port
  1892. *
  1893. * @v usb USB device
  1894. * @ret port Root hub port
  1895. */
  1896. struct usb_port * usb_root_hub_port ( struct usb_device *usb ) {
  1897. struct usb_device *parent;
  1898. /* Navigate up to root hub */
  1899. while ( ( parent = usb->port->hub->usb ) )
  1900. usb = parent;
  1901. return usb->port;
  1902. }
  1903. /**
  1904. * Get USB transaction translator
  1905. *
  1906. * @v usb USB device
  1907. * @ret port Transaction translator port, or NULL
  1908. */
  1909. struct usb_port * usb_transaction_translator ( struct usb_device *usb ) {
  1910. struct usb_device *parent;
  1911. /* Navigate up to root hub. If we find a low-speed or
  1912. * full-speed device with a higher-speed parent hub, then that
  1913. * device's port is the transaction translator.
  1914. */
  1915. for ( ; ( parent = usb->port->hub->usb ) ; usb = parent ) {
  1916. if ( ( usb->speed <= USB_SPEED_FULL ) &&
  1917. ( parent->speed > USB_SPEED_FULL ) )
  1918. return usb->port;
  1919. }
  1920. return NULL;
  1921. }
  1922. /* Drag in objects via register_usb_bus() */
  1923. REQUIRING_SYMBOL ( register_usb_bus );
  1924. /* Drag in USB configuration */
  1925. REQUIRE_OBJECT ( config_usb );
  1926. /* Drag in hub driver */
  1927. REQUIRE_OBJECT ( usbhub );