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.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255
  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 len = ( ep->len ? ep->len : ep->mtu );
  526. unsigned int fill;
  527. int rc;
  528. /* Sanity checks */
  529. assert ( ep->fill == 0 );
  530. assert ( ep->max > 0 );
  531. assert ( list_empty ( &ep->recycled ) );
  532. /* Fill recycled buffer list */
  533. for ( fill = 0 ; fill < ep->max ; fill++ ) {
  534. /* Allocate I/O buffer */
  535. iobuf = alloc_iob ( len );
  536. if ( ! iobuf ) {
  537. rc = -ENOMEM;
  538. goto err_alloc;
  539. }
  540. /* Add to recycled buffer list */
  541. list_add_tail ( &iobuf->list, &ep->recycled );
  542. }
  543. return 0;
  544. err_alloc:
  545. usb_flush ( ep );
  546. return rc;
  547. }
  548. /**
  549. * Refill endpoint
  550. *
  551. * @v ep USB endpoint
  552. * @ret rc Return status code
  553. */
  554. int usb_refill ( struct usb_endpoint *ep ) {
  555. struct io_buffer *iobuf;
  556. size_t len = ( ep->len ? ep->len : ep->mtu );
  557. int rc;
  558. /* Sanity checks */
  559. assert ( ep->open );
  560. assert ( ep->max > 0 );
  561. /* Refill endpoint */
  562. while ( ep->fill < ep->max ) {
  563. /* Get or allocate buffer */
  564. if ( list_empty ( &ep->recycled ) ) {
  565. /* Recycled buffer list is empty; allocate new buffer */
  566. iobuf = alloc_iob ( len );
  567. if ( ! iobuf )
  568. return -ENOMEM;
  569. } else {
  570. /* Get buffer from recycled buffer list */
  571. iobuf = list_first_entry ( &ep->recycled,
  572. struct io_buffer, list );
  573. assert ( iobuf != NULL );
  574. list_del ( &iobuf->list );
  575. }
  576. /* Reset buffer to maximum size */
  577. assert ( iob_len ( iobuf ) <= len );
  578. iob_put ( iobuf, ( len - iob_len ( iobuf ) ) );
  579. /* Enqueue buffer */
  580. if ( ( rc = usb_stream ( ep, iobuf, 0 ) ) != 0 ) {
  581. list_add ( &iobuf->list, &ep->recycled );
  582. return rc;
  583. }
  584. }
  585. return 0;
  586. }
  587. /**
  588. * Discard endpoint recycled buffer list
  589. *
  590. * @v ep USB endpoint
  591. */
  592. void usb_flush ( struct usb_endpoint *ep ) {
  593. struct io_buffer *iobuf;
  594. struct io_buffer *tmp;
  595. /* Sanity checks */
  596. assert ( ! ep->open );
  597. assert ( ep->max > 0 );
  598. /* Free all I/O buffers */
  599. list_for_each_entry_safe ( iobuf, tmp, &ep->recycled, list ) {
  600. list_del ( &iobuf->list );
  601. free_iob ( iobuf );
  602. }
  603. }
  604. /******************************************************************************
  605. *
  606. * Control endpoint
  607. *
  608. ******************************************************************************
  609. */
  610. /** USB control transfer pseudo-header */
  611. struct usb_control_pseudo_header {
  612. /** Completion status */
  613. int rc;
  614. };
  615. /**
  616. * Complete USB control transfer
  617. *
  618. * @v ep USB endpoint
  619. * @v iobuf I/O buffer
  620. * @v rc Completion status code
  621. */
  622. static void usb_control_complete ( struct usb_endpoint *ep,
  623. struct io_buffer *iobuf, int rc ) {
  624. struct usb_device *usb = ep->usb;
  625. struct usb_control_pseudo_header *pshdr;
  626. /* Record completion status in buffer */
  627. pshdr = iob_push ( iobuf, sizeof ( *pshdr ) );
  628. pshdr->rc = rc;
  629. /* Add to list of completed I/O buffers */
  630. list_add_tail ( &iobuf->list, &usb->complete );
  631. }
  632. /** USB control endpoint driver operations */
  633. static struct usb_endpoint_driver_operations usb_control_operations = {
  634. .complete = usb_control_complete,
  635. };
  636. /**
  637. * Issue USB control transaction
  638. *
  639. * @v usb USB device
  640. * @v request Request
  641. * @v value Value parameter
  642. * @v index Index parameter
  643. * @v data Data buffer (if any)
  644. * @v len Length of data
  645. * @ret rc Return status code
  646. */
  647. int usb_control ( struct usb_device *usb, unsigned int request,
  648. unsigned int value, unsigned int index, void *data,
  649. size_t len ) {
  650. struct usb_bus *bus = usb->port->hub->bus;
  651. struct usb_endpoint *ep = &usb->control;
  652. struct io_buffer *iobuf;
  653. struct io_buffer *cmplt;
  654. union {
  655. struct usb_setup_packet setup;
  656. struct usb_control_pseudo_header pshdr;
  657. } *headroom;
  658. struct usb_control_pseudo_header *pshdr;
  659. unsigned int i;
  660. int rc;
  661. /* Allocate I/O buffer */
  662. iobuf = alloc_iob ( sizeof ( *headroom ) + len );
  663. if ( ! iobuf ) {
  664. rc = -ENOMEM;
  665. goto err_alloc;
  666. }
  667. iob_reserve ( iobuf, sizeof ( *headroom ) );
  668. iob_put ( iobuf, len );
  669. if ( request & USB_DIR_IN ) {
  670. memset ( data, 0, len );
  671. } else {
  672. memcpy ( iobuf->data, data, len );
  673. }
  674. /* Enqueue message */
  675. if ( ( rc = usb_message ( ep, request, value, index, iobuf ) ) != 0 )
  676. goto err_message;
  677. /* Wait for completion */
  678. for ( i = 0 ; i < USB_CONTROL_MAX_WAIT_MS ; i++ ) {
  679. /* Poll bus */
  680. usb_poll ( bus );
  681. /* Check for completion */
  682. while ( ( cmplt = list_first_entry ( &usb->complete,
  683. struct io_buffer,
  684. list ) ) ) {
  685. /* Remove from completion list */
  686. list_del ( &cmplt->list );
  687. /* Extract and strip completion status */
  688. pshdr = cmplt->data;
  689. iob_pull ( cmplt, sizeof ( *pshdr ) );
  690. rc = pshdr->rc;
  691. /* Discard stale completions */
  692. if ( cmplt != iobuf ) {
  693. DBGC ( usb, "USB %s stale control completion: "
  694. "%s\n", usb->name, strerror ( rc ) );
  695. DBGC_HDA ( usb, 0, cmplt->data,
  696. iob_len ( cmplt ) );
  697. free_iob ( cmplt );
  698. continue;
  699. }
  700. /* Fail immediately if completion was in error */
  701. if ( rc != 0 ) {
  702. DBGC ( usb, "USB %s control %04x:%04x:%04x "
  703. "failed: %s\n", usb->name, request,
  704. value, index, strerror ( rc ) );
  705. free_iob ( cmplt );
  706. return rc;
  707. }
  708. /* Copy completion to data buffer, if applicable */
  709. assert ( iob_len ( cmplt ) <= len );
  710. if ( request & USB_DIR_IN )
  711. memcpy ( data, cmplt->data, iob_len ( cmplt ) );
  712. free_iob ( cmplt );
  713. return 0;
  714. }
  715. /* Delay */
  716. mdelay ( 1 );
  717. }
  718. DBGC ( usb, "USB %s timed out waiting for control %04x:%04x:%04x\n",
  719. usb->name, request, value, index );
  720. return -ETIMEDOUT;
  721. err_message:
  722. free_iob ( iobuf );
  723. err_alloc:
  724. return rc;
  725. }
  726. /**
  727. * Get USB string descriptor
  728. *
  729. * @v usb USB device
  730. * @v index String index
  731. * @v language Language ID
  732. * @v buf Data buffer
  733. * @v len Length of buffer
  734. * @ret len String length (excluding NUL), or negative error
  735. */
  736. int usb_get_string_descriptor ( struct usb_device *usb, unsigned int index,
  737. unsigned int language, char *buf, size_t len ) {
  738. size_t max = ( len ? ( len - 1 /* NUL */ ) : 0 );
  739. struct {
  740. struct usb_descriptor_header header;
  741. uint16_t character[max];
  742. } __attribute__ (( packed )) *desc;
  743. unsigned int actual;
  744. unsigned int i;
  745. int rc;
  746. /* Allocate buffer for string */
  747. desc = malloc ( sizeof ( *desc ) );
  748. if ( ! desc ) {
  749. rc = -ENOMEM;
  750. goto err_alloc;
  751. }
  752. /* Get descriptor */
  753. if ( ( rc = usb_get_descriptor ( usb, 0, USB_STRING_DESCRIPTOR, index,
  754. language, &desc->header,
  755. sizeof ( *desc ) ) ) != 0 )
  756. goto err_get_descriptor;
  757. /* Copy to buffer */
  758. actual = ( ( desc->header.len - sizeof ( desc->header ) ) /
  759. sizeof ( desc->character[0] ) );
  760. for ( i = 0 ; ( ( i < actual ) && ( i < max ) ) ; i++ )
  761. buf[i] = le16_to_cpu ( desc->character[i] );
  762. if ( len )
  763. buf[i] = '\0';
  764. /* Free buffer */
  765. free ( desc );
  766. return actual;
  767. err_get_descriptor:
  768. free ( desc );
  769. err_alloc:
  770. return rc;
  771. }
  772. /******************************************************************************
  773. *
  774. * USB device driver
  775. *
  776. ******************************************************************************
  777. */
  778. /**
  779. * Get USB configuration descriptor
  780. *
  781. * @v usb USB device
  782. * @v index Configuration index
  783. * @ret config Configuration descriptor
  784. * @ret rc Return status code
  785. *
  786. * The configuration descriptor is dynamically allocated and must
  787. * eventually be freed by the caller.
  788. */
  789. static int
  790. usb_config_descriptor ( struct usb_device *usb, unsigned int index,
  791. struct usb_configuration_descriptor **config ) {
  792. struct usb_configuration_descriptor partial;
  793. size_t len;
  794. int rc;
  795. /* Read first part of configuration descriptor to get size */
  796. if ( ( rc = usb_get_config_descriptor ( usb, index, &partial,
  797. sizeof ( partial ) ) ) != 0 ) {
  798. DBGC ( usb, "USB %s could not get configuration descriptor %d: "
  799. "%s\n", usb->name, index, strerror ( rc ) );
  800. goto err_get_partial;
  801. }
  802. len = le16_to_cpu ( partial.len );
  803. if ( len < sizeof ( partial ) ) {
  804. DBGC ( usb, "USB %s underlength configuraton descriptor %d\n",
  805. usb->name, index );
  806. rc = -EINVAL;
  807. goto err_partial_len;
  808. }
  809. /* Allocate buffer for whole configuration descriptor */
  810. *config = malloc ( len );
  811. if ( ! *config ) {
  812. rc = -ENOMEM;
  813. goto err_alloc_config;
  814. }
  815. /* Read whole configuration descriptor */
  816. if ( ( rc = usb_get_config_descriptor ( usb, index, *config,
  817. len ) ) != 0 ) {
  818. DBGC ( usb, "USB %s could not get configuration descriptor %d: "
  819. "%s\n", usb->name, index, strerror ( rc ) );
  820. goto err_get_config_descriptor;
  821. }
  822. if ( (*config)->len != partial.len ) {
  823. DBGC ( usb, "USB %s bad configuration descriptor %d length\n",
  824. usb->name, index );
  825. rc = -EINVAL;
  826. goto err_config_len;
  827. }
  828. return 0;
  829. err_config_len:
  830. err_get_config_descriptor:
  831. free ( *config );
  832. err_alloc_config:
  833. err_partial_len:
  834. err_get_partial:
  835. return rc;
  836. }
  837. /**
  838. * Describe USB function
  839. *
  840. * @v usb USB device
  841. * @v config Configuration descriptor
  842. * @v first First interface number
  843. * @v interfaces Interface list to fill in
  844. * @v desc Function descriptor to fill in
  845. * @ret rc Return status code
  846. */
  847. static int usb_describe ( struct usb_device *usb,
  848. struct usb_configuration_descriptor *config,
  849. unsigned int first, uint8_t *interfaces,
  850. struct usb_function_descriptor *desc ) {
  851. struct usb_interface_association_descriptor *association;
  852. struct usb_interface_descriptor *interface;
  853. struct cdc_union_descriptor *cdc_union;
  854. unsigned int i;
  855. /* Fill in vendor and product ID */
  856. memset ( desc, 0, sizeof ( *desc ) );
  857. desc->vendor = le16_to_cpu ( usb->device.vendor );
  858. desc->product = le16_to_cpu ( usb->device.product );
  859. /* First, look for an interface association descriptor */
  860. association = usb_interface_association_descriptor ( config, first );
  861. if ( association ) {
  862. /* Sanity check */
  863. assert ( association->first == first );
  864. if ( ( first + association->count ) > config->interfaces ) {
  865. DBGC ( usb, "USB %s has invalid association [%d-%d)\n",
  866. usb->name, first, ( first + association->count));
  867. return -ERANGE;
  868. }
  869. /* Describe function */
  870. memcpy ( &desc->class, &association->class,
  871. sizeof ( desc->class ) );
  872. desc->count = association->count;
  873. for ( i = 0 ; i < association->count ; i++ )
  874. interfaces[i] = ( first + i );
  875. return 0;
  876. }
  877. /* Next, look for an interface descriptor */
  878. interface = usb_interface_descriptor ( config, first, 0 );
  879. if ( ! interface ) {
  880. DBGC ( usb, "USB %s has no descriptor for interface %d\n",
  881. usb->name, first );
  882. return -ENOENT;
  883. }
  884. /* Describe function */
  885. memcpy ( &desc->class, &interface->class, sizeof ( desc->class ) );
  886. desc->count = 1;
  887. interfaces[0] = first;
  888. /* Look for a CDC union descriptor, if applicable */
  889. if ( ( desc->class.class.class == USB_CLASS_CDC ) &&
  890. ( cdc_union = cdc_union_descriptor ( config, interface ) ) ) {
  891. /* Determine interface count */
  892. desc->count = ( ( cdc_union->header.len -
  893. offsetof ( typeof ( *cdc_union ),
  894. interface[0] ) ) /
  895. sizeof ( cdc_union->interface[0] ) );
  896. if ( desc->count > config->interfaces ) {
  897. DBGC ( usb, "USB %s has invalid union functional "
  898. "descriptor with %d interfaces\n",
  899. usb->name, desc->count );
  900. return -ERANGE;
  901. }
  902. /* Describe function */
  903. for ( i = 0 ; i < desc->count ; i++ ) {
  904. if ( cdc_union->interface[i] >= config->interfaces ) {
  905. DBGC ( usb, "USB %s has invalid union "
  906. "functional descriptor covering "
  907. "interface %d\n", usb->name,
  908. cdc_union->interface[i] );
  909. return -ERANGE;
  910. }
  911. interfaces[i] = cdc_union->interface[i];
  912. }
  913. return 0;
  914. }
  915. return 0;
  916. }
  917. /**
  918. * Update list of used interface
  919. *
  920. * @v usb USB device
  921. * @v count Number of interfaces
  922. * @v interface List of interfaces
  923. * @v used List of already-used interfaces
  924. * @ret rc Return status code
  925. */
  926. static int usb_used ( struct usb_device *usb, unsigned int count,
  927. uint8_t *interface, uint8_t *used ) {
  928. unsigned int i;
  929. for ( i = 0 ; i < count ; i++ ) {
  930. if ( used[interface[i]] ) {
  931. DBGC ( usb, "USB %s interface %d already in use\n",
  932. usb->name, interface[i] );
  933. return -EINVAL;
  934. }
  935. used[interface[i]] = 1;
  936. }
  937. return 0;
  938. }
  939. /**
  940. * Find USB device driver
  941. *
  942. * @v desc Function descriptor
  943. * @ret id USB device ID, or NULL
  944. * @ret driver USB device driver, or NULL
  945. */
  946. struct usb_driver * usb_find_driver ( struct usb_function_descriptor *desc,
  947. struct usb_device_id **id ) {
  948. struct usb_driver *driver;
  949. unsigned int i;
  950. /* Look for a matching driver */
  951. for_each_table_entry ( driver, USB_DRIVERS ) {
  952. for ( i = 0 ; i < driver->id_count ; i++ ) {
  953. /* Ignore non-matching driver class */
  954. if ( ( driver->class.class.scalar ^ desc->class.scalar )
  955. & driver->class.mask.scalar )
  956. continue;
  957. /* Look for a matching ID */
  958. *id = &driver->ids[i];
  959. if ( ( ( (*id)->vendor == desc->vendor ) ||
  960. ( (*id)->vendor == USB_ANY_ID ) ) &&
  961. ( ( (*id)->product == desc->product ) ||
  962. ( (*id)->product == USB_ANY_ID ) ) )
  963. return driver;
  964. }
  965. }
  966. /* Not found */
  967. *id = NULL;
  968. return NULL;
  969. }
  970. /**
  971. * Get USB device configuration score
  972. *
  973. * @v usb USB device
  974. * @v config Configuration descriptor
  975. * @ret score Device configuration score, or negative error
  976. */
  977. static int usb_score ( struct usb_device *usb,
  978. struct usb_configuration_descriptor *config ) {
  979. uint8_t used[config->interfaces];
  980. uint8_t interface[config->interfaces];
  981. struct usb_function_descriptor desc;
  982. struct usb_driver *driver;
  983. struct usb_device_id *id;
  984. unsigned int first;
  985. unsigned int score = 0;
  986. int rc;
  987. /* Identify each function in turn */
  988. memset ( used, 0, sizeof ( used ) );
  989. for ( first = 0 ; first < config->interfaces ; first++ ) {
  990. /* Skip interfaces already used */
  991. if ( used[first] )
  992. continue;
  993. /* Describe function */
  994. if ( ( rc = usb_describe ( usb, config, first, interface,
  995. &desc ) ) != 0 )
  996. return rc;
  997. /* Update used interfaces */
  998. if ( ( rc = usb_used ( usb, desc.count, interface,
  999. used ) ) != 0 )
  1000. return rc;
  1001. /* Look for a driver for this function */
  1002. driver = usb_find_driver ( &desc, &id );
  1003. if ( driver )
  1004. score += driver->score;
  1005. }
  1006. return score;
  1007. }
  1008. /**
  1009. * Probe USB device driver
  1010. *
  1011. * @v func USB function
  1012. * @v config Configuration descriptor
  1013. * @ret rc Return status code
  1014. */
  1015. static int usb_probe ( struct usb_function *func,
  1016. struct usb_configuration_descriptor *config ) {
  1017. struct usb_device *usb = func->usb;
  1018. struct usb_driver *driver;
  1019. struct usb_device_id *id;
  1020. int rc;
  1021. /* Identify driver */
  1022. driver = usb_find_driver ( &func->desc, &id );
  1023. if ( ! driver ) {
  1024. DBGC ( usb, "USB %s %04x:%04x class %d:%d:%d has no driver\n",
  1025. func->name, func->desc.vendor, func->desc.product,
  1026. func->desc.class.class.class,
  1027. func->desc.class.class.subclass,
  1028. func->desc.class.class.protocol );
  1029. return -ENOENT;
  1030. }
  1031. /* Probe driver */
  1032. if ( ( rc = driver->probe ( func, config ) ) != 0 ) {
  1033. DBGC ( usb, "USB %s failed to probe driver %s: %s\n",
  1034. func->name, id->name, strerror ( rc ) );
  1035. return rc;
  1036. }
  1037. /* Record driver */
  1038. func->driver = driver;
  1039. func->dev.driver_name = id->name;
  1040. return 0;
  1041. }
  1042. /**
  1043. * Remove USB device driver
  1044. *
  1045. * @v func USB function
  1046. */
  1047. static void usb_remove ( struct usb_function *func ) {
  1048. /* Remove driver */
  1049. func->driver->remove ( func );
  1050. }
  1051. /**
  1052. * Probe all USB device drivers
  1053. *
  1054. * @v usb USB device
  1055. * @v config Configuration descriptor
  1056. */
  1057. static void
  1058. usb_probe_all ( struct usb_device *usb,
  1059. struct usb_configuration_descriptor *config ) {
  1060. struct usb_bus *bus = usb->port->hub->bus;
  1061. struct usb_function *func;
  1062. uint8_t used[config->interfaces];
  1063. unsigned int first;
  1064. unsigned int i;
  1065. int rc;
  1066. /* Identify each function in turn */
  1067. memset ( used, 0, sizeof ( used ) );
  1068. for ( first = 0 ; first < config->interfaces ; first++ ) {
  1069. /* Skip interfaces already used */
  1070. if ( used[first] )
  1071. continue;
  1072. /* Allocate and initialise structure */
  1073. func = zalloc ( sizeof ( *func ) +
  1074. ( config->interfaces *
  1075. sizeof ( func->interface[0] ) ) );
  1076. if ( ! func )
  1077. goto err_alloc;
  1078. func->name = func->dev.name;
  1079. func->usb = usb;
  1080. func->dev.desc.bus_type = BUS_TYPE_USB;
  1081. func->dev.desc.location = usb->address;
  1082. func->dev.desc.vendor = le16_to_cpu ( usb->device.vendor );
  1083. func->dev.desc.device = le16_to_cpu ( usb->device.product );
  1084. snprintf ( func->dev.name, sizeof ( func->dev.name ),
  1085. "%s-%d.%d", usb->name, config->config, first );
  1086. INIT_LIST_HEAD ( &func->dev.children );
  1087. func->dev.parent = bus->dev;
  1088. list_add_tail ( &func->list, &usb->functions );
  1089. /* Identify function */
  1090. if ( ( rc = usb_describe ( usb, config, first, func->interface,
  1091. &func->desc ) ) != 0 )
  1092. goto err_describe;
  1093. assert ( func->desc.count <= config->interfaces );
  1094. /* Mark interfaces as used */
  1095. if ( ( rc = usb_used ( usb, func->desc.count, func->interface,
  1096. used ) ) != 0 )
  1097. goto err_used;
  1098. /* Probe device driver */
  1099. if ( ( rc = usb_probe ( func, config ) ) != 0 )
  1100. goto err_probe;
  1101. DBGC ( usb, "USB %s %04x:%04x class %d:%d:%d interfaces ",
  1102. func->name, func->desc.vendor, func->desc.product,
  1103. func->desc.class.class.class,
  1104. func->desc.class.class.subclass,
  1105. func->desc.class.class.protocol );
  1106. for ( i = 0 ; i < func->desc.count ; i++ )
  1107. DBGC ( usb, "%s%d", ( i ? "," : "" ),
  1108. func->interface[i] );
  1109. DBGC ( usb, " using driver %s\n", func->dev.driver_name );
  1110. /* Add to device hierarchy */
  1111. list_add_tail ( &func->dev.siblings, &bus->dev->children );
  1112. continue;
  1113. list_del ( &func->dev.siblings );
  1114. usb_remove ( func );
  1115. err_probe:
  1116. err_used:
  1117. err_describe:
  1118. list_del ( &func->list );
  1119. free ( func );
  1120. err_alloc:
  1121. /* Continue registering other functions */
  1122. continue;
  1123. }
  1124. }
  1125. /**
  1126. * Remove all device drivers
  1127. *
  1128. * @v usb USB device
  1129. */
  1130. static void usb_remove_all ( struct usb_device *usb ) {
  1131. struct usb_function *func;
  1132. struct usb_function *tmp;
  1133. /* Remove all functions */
  1134. list_for_each_entry_safe ( func, tmp, &usb->functions, list ) {
  1135. /* Remove device driver */
  1136. usb_remove ( func );
  1137. /* Remove from device hierarchy */
  1138. assert ( list_empty ( &func->dev.children ) );
  1139. list_del ( &func->dev.siblings );
  1140. /* Remove from list of functions */
  1141. list_del ( &func->list );
  1142. /* Free function */
  1143. free ( func );
  1144. }
  1145. }
  1146. /**
  1147. * Clear USB device configuration
  1148. *
  1149. * @v usb USB device
  1150. */
  1151. static void usb_deconfigure ( struct usb_device *usb ) {
  1152. unsigned int i;
  1153. /* Remove device drivers */
  1154. usb_remove_all ( usb );
  1155. /* Sanity checks */
  1156. for ( i = 0 ; i < ( sizeof ( usb->ep ) / sizeof ( usb->ep[0] ) ) ; i++){
  1157. if ( i != USB_ENDPOINT_IDX ( USB_EP0_ADDRESS ) )
  1158. assert ( usb->ep[i] == NULL );
  1159. }
  1160. /* Clear device configuration */
  1161. usb_set_configuration ( usb, 0 );
  1162. }
  1163. /**
  1164. * Choose our preferred USB device configuration
  1165. *
  1166. * @v usb USB device
  1167. * @ret rc Return status code
  1168. */
  1169. static int usb_autoconfigure ( struct usb_device *usb ) {
  1170. struct usb_configuration_descriptor *config;
  1171. unsigned int preferred = 0;
  1172. unsigned int index;
  1173. int score;
  1174. int best = 0;
  1175. int rc;
  1176. /* Calculate driver score for each configuration index */
  1177. for ( index = 0 ; index < usb->device.configurations ; index++ ) {
  1178. /* Read configuration descriptor */
  1179. if ( ( rc = usb_config_descriptor ( usb, index,
  1180. &config ) ) != 0 )
  1181. goto err_config;
  1182. /* Get score for this configuration */
  1183. score = usb_score ( usb, config );
  1184. if ( score < 0 ) {
  1185. rc = score;
  1186. goto err_score;
  1187. }
  1188. DBGC2 ( usb, "USB %s configuration %d score %d\n",
  1189. usb->name, config->config, score );
  1190. /* Record as preferred configuration, if applicable */
  1191. if ( score > best ) {
  1192. best = score;
  1193. preferred = index;
  1194. }
  1195. /* Free configuration descriptor */
  1196. free ( config );
  1197. config = NULL;
  1198. }
  1199. /* Read preferred configuration descriptor */
  1200. if ( ( rc = usb_config_descriptor ( usb, preferred, &config ) ) != 0 )
  1201. goto err_preferred;
  1202. /* Set configuration */
  1203. if ( ( rc = usb_set_configuration ( usb, config->config ) ) != 0){
  1204. DBGC ( usb, "USB %s could not set configuration %d: %s\n",
  1205. usb->name, config->config, strerror ( rc ) );
  1206. goto err_set_configuration;
  1207. }
  1208. /* Probe USB device drivers */
  1209. usb_probe_all ( usb, config );
  1210. /* Free configuration descriptor */
  1211. free ( config );
  1212. return 0;
  1213. usb_remove_all ( usb );
  1214. usb_set_configuration ( usb, 0 );
  1215. err_set_configuration:
  1216. free ( config );
  1217. err_preferred:
  1218. return rc;
  1219. err_score:
  1220. free ( config );
  1221. err_config:
  1222. return rc;
  1223. }
  1224. /******************************************************************************
  1225. *
  1226. * USB device
  1227. *
  1228. ******************************************************************************
  1229. */
  1230. /**
  1231. * Allocate USB device
  1232. *
  1233. * @v port USB port
  1234. * @ret usb USB device, or NULL on allocation failure
  1235. */
  1236. static struct usb_device * alloc_usb ( struct usb_port *port ) {
  1237. struct usb_hub *hub = port->hub;
  1238. struct usb_bus *bus = hub->bus;
  1239. struct usb_device *usb;
  1240. /* Allocate and initialise structure */
  1241. usb = zalloc ( sizeof ( *usb ) );
  1242. if ( ! usb )
  1243. return NULL;
  1244. snprintf ( usb->name, sizeof ( usb->name ), "%s%c%d", hub->name,
  1245. ( hub->usb ? '.' : '-' ), port->address );
  1246. usb->port = port;
  1247. INIT_LIST_HEAD ( &usb->functions );
  1248. usb->host = &bus->op->device;
  1249. usb_endpoint_init ( &usb->control, usb, &usb_control_operations );
  1250. INIT_LIST_HEAD ( &usb->complete );
  1251. return usb;
  1252. }
  1253. /**
  1254. * Register USB device
  1255. *
  1256. * @v usb USB device
  1257. * @ret rc Return status code
  1258. */
  1259. static int register_usb ( struct usb_device *usb ) {
  1260. struct usb_port *port = usb->port;
  1261. struct usb_hub *hub = port->hub;
  1262. struct usb_bus *bus = hub->bus;
  1263. unsigned int protocol;
  1264. size_t mtu;
  1265. int rc;
  1266. /* Add to port */
  1267. if ( port->usb != NULL ) {
  1268. DBGC ( hub, "USB hub %s port %d is already registered to %s\n",
  1269. hub->name, port->address, port->usb->name );
  1270. rc = -EALREADY;
  1271. goto err_already;
  1272. }
  1273. port->usb = usb;
  1274. /* Add to bus device list */
  1275. list_add_tail ( &usb->list, &bus->devices );
  1276. /* Enable device */
  1277. if ( ( rc = hub->driver->enable ( hub, port ) ) != 0 ) {
  1278. DBGC ( hub, "USB hub %s port %d could not enable: %s\n",
  1279. hub->name, port->address, strerror ( rc ) );
  1280. goto err_enable;
  1281. }
  1282. /* Allow recovery interval since port may have been reset */
  1283. mdelay ( USB_RESET_RECOVER_DELAY_MS );
  1284. /* Get device speed */
  1285. if ( ( rc = hub->driver->speed ( hub, port ) ) != 0 ) {
  1286. DBGC ( hub, "USB hub %s port %d could not get speed: %s\n",
  1287. hub->name, port->address, strerror ( rc ) );
  1288. goto err_speed;
  1289. }
  1290. usb->speed = port->speed;
  1291. DBGC2 ( usb, "USB %s attached as %s-speed device\n",
  1292. usb->name, usb_speed_name ( usb->speed ) );
  1293. /* Open device */
  1294. if ( ( rc = usb->host->open ( usb ) ) != 0 ) {
  1295. DBGC ( usb, "USB %s could not open: %s\n",
  1296. usb->name, strerror ( rc ) );
  1297. goto err_open;
  1298. }
  1299. /* Describe control endpoint */
  1300. mtu = USB_EP0_DEFAULT_MTU ( usb->speed );
  1301. usb_endpoint_describe ( &usb->control, USB_EP0_ADDRESS,
  1302. USB_EP0_ATTRIBUTES, mtu, USB_EP0_BURST,
  1303. USB_EP0_INTERVAL );
  1304. /* Open control endpoint */
  1305. if ( ( rc = usb_endpoint_open ( &usb->control ) ) != 0 )
  1306. goto err_open_control;
  1307. assert ( usb_endpoint ( usb, USB_EP0_ADDRESS ) == &usb->control );
  1308. /* Assign device address */
  1309. if ( ( rc = usb->host->address ( usb ) ) != 0 ) {
  1310. DBGC ( usb, "USB %s could not set address: %s\n",
  1311. usb->name, strerror ( rc ) );
  1312. goto err_address;
  1313. }
  1314. DBGC2 ( usb, "USB %s assigned address %d\n", usb->name, usb->address );
  1315. /* Allow recovery interval after Set Address command */
  1316. mdelay ( USB_SET_ADDRESS_RECOVER_DELAY_MS );
  1317. /* Read first part of device descriptor to get EP0 MTU */
  1318. if ( ( rc = usb_get_mtu ( usb, &usb->device ) ) != 0 ) {
  1319. DBGC ( usb, "USB %s could not get MTU: %s\n",
  1320. usb->name, strerror ( rc ) );
  1321. goto err_get_mtu;
  1322. }
  1323. /* Calculate EP0 MTU */
  1324. protocol = le16_to_cpu ( usb->device.protocol );
  1325. mtu = ( ( protocol < USB_PROTO_3_0 ) ?
  1326. usb->device.mtu : ( 1 << usb->device.mtu ) );
  1327. DBGC2 ( usb, "USB %s has control MTU %zd (guessed %zd)\n",
  1328. usb->name, mtu, usb->control.mtu );
  1329. /* Update MTU */
  1330. if ( ( rc = usb_endpoint_mtu ( &usb->control, mtu ) ) != 0 )
  1331. goto err_mtu;
  1332. /* Read whole device descriptor */
  1333. if ( ( rc = usb_get_device_descriptor ( usb, &usb->device ) ) != 0 ) {
  1334. DBGC ( usb, "USB %s could not get device descriptor: %s\n",
  1335. usb->name, strerror ( rc ) );
  1336. goto err_get_device_descriptor;
  1337. }
  1338. DBGC ( usb, "USB %s addr %d %04x:%04x class %d:%d:%d (v%s, %s-speed, "
  1339. "MTU %zd)\n", usb->name, usb->address,
  1340. le16_to_cpu ( usb->device.vendor ),
  1341. le16_to_cpu ( usb->device.product ), usb->device.class.class,
  1342. usb->device.class.subclass, usb->device.class.protocol,
  1343. usb_bcd ( le16_to_cpu ( usb->device.protocol ) ),
  1344. usb_speed_name ( usb->speed ), usb->control.mtu );
  1345. /* Configure device */
  1346. if ( ( rc = usb_autoconfigure ( usb ) ) != 0 )
  1347. goto err_autoconfigure;
  1348. return 0;
  1349. usb_deconfigure ( usb );
  1350. err_autoconfigure:
  1351. err_get_device_descriptor:
  1352. err_mtu:
  1353. err_get_mtu:
  1354. err_address:
  1355. usb_endpoint_close ( &usb->control );
  1356. err_open_control:
  1357. usb->host->close ( usb );
  1358. err_open:
  1359. err_speed:
  1360. hub->driver->disable ( hub, port );
  1361. err_enable:
  1362. list_del ( &usb->list );
  1363. port->usb = NULL;
  1364. err_already:
  1365. return rc;
  1366. }
  1367. /**
  1368. * Unregister USB device
  1369. *
  1370. * @v usb USB device
  1371. */
  1372. static void unregister_usb ( struct usb_device *usb ) {
  1373. struct usb_port *port = usb->port;
  1374. struct usb_hub *hub = port->hub;
  1375. struct io_buffer *iobuf;
  1376. struct io_buffer *tmp;
  1377. /* Sanity checks */
  1378. assert ( port->usb == usb );
  1379. /* Clear device configuration */
  1380. usb_deconfigure ( usb );
  1381. /* Close control endpoint */
  1382. usb_endpoint_close ( &usb->control );
  1383. /* Discard any stale control completions */
  1384. list_for_each_entry_safe ( iobuf, tmp, &usb->complete, list ) {
  1385. list_del ( &iobuf->list );
  1386. free_iob ( iobuf );
  1387. }
  1388. /* Close device */
  1389. usb->host->close ( usb );
  1390. /* Disable port */
  1391. hub->driver->disable ( hub, port );
  1392. /* Remove from bus device list */
  1393. list_del ( &usb->list );
  1394. /* Remove from port */
  1395. port->usb = NULL;
  1396. }
  1397. /**
  1398. * Free USB device
  1399. *
  1400. * @v usb USB device
  1401. */
  1402. static void free_usb ( struct usb_device *usb ) {
  1403. unsigned int i;
  1404. /* Sanity checks */
  1405. for ( i = 0 ; i < ( sizeof ( usb->ep ) / sizeof ( usb->ep[0] ) ) ; i++ )
  1406. assert ( usb->ep[i] == NULL );
  1407. assert ( list_empty ( &usb->functions ) );
  1408. assert ( list_empty ( &usb->complete ) );
  1409. /* Free device */
  1410. free ( usb );
  1411. }
  1412. /******************************************************************************
  1413. *
  1414. * USB device hotplug event handling
  1415. *
  1416. ******************************************************************************
  1417. */
  1418. /**
  1419. * Handle newly attached USB device
  1420. *
  1421. * @v port USB port
  1422. * @ret rc Return status code
  1423. */
  1424. static int usb_attached ( struct usb_port *port ) {
  1425. struct usb_device *usb;
  1426. int rc;
  1427. /* Mark port as attached */
  1428. port->attached = 1;
  1429. /* Sanity checks */
  1430. assert ( port->usb == NULL );
  1431. /* Allocate USB device */
  1432. usb = alloc_usb ( port );
  1433. if ( ! usb ) {
  1434. rc = -ENOMEM;
  1435. goto err_alloc;
  1436. }
  1437. /* Register USB device */
  1438. if ( ( rc = register_usb ( usb ) ) != 0 )
  1439. goto err_register;
  1440. return 0;
  1441. unregister_usb ( usb );
  1442. err_register:
  1443. free_usb ( usb );
  1444. err_alloc:
  1445. return rc;
  1446. }
  1447. /**
  1448. * Handle newly detached USB device
  1449. *
  1450. * @v port USB port
  1451. */
  1452. static void usb_detached ( struct usb_port *port ) {
  1453. struct usb_device *usb = port->usb;
  1454. /* Mark port as detached */
  1455. port->attached = 0;
  1456. /* Do nothing if we have no USB device */
  1457. if ( ! usb )
  1458. return;
  1459. /* Unregister USB device */
  1460. unregister_usb ( usb );
  1461. /* Free USB device */
  1462. free_usb ( usb );
  1463. }
  1464. /**
  1465. * Handle newly attached or detached USB device
  1466. *
  1467. * @v port USB port
  1468. * @ret rc Return status code
  1469. */
  1470. static int usb_hotplugged ( struct usb_port *port ) {
  1471. struct usb_hub *hub = port->hub;
  1472. int rc;
  1473. /* Get current port speed */
  1474. if ( ( rc = hub->driver->speed ( hub, port ) ) != 0 ) {
  1475. DBGC ( hub, "USB hub %s port %d could not get speed: %s\n",
  1476. hub->name, port->address, strerror ( rc ) );
  1477. /* Treat as a disconnection */
  1478. port->disconnected = 1;
  1479. port->speed = USB_SPEED_NONE;
  1480. }
  1481. /* Detach device, if applicable */
  1482. if ( port->attached && ( port->disconnected || ! port->speed ) )
  1483. usb_detached ( port );
  1484. /* Clear any recorded disconnections */
  1485. port->disconnected = 0;
  1486. /* Attach device, if applicable */
  1487. if ( port->speed && ( ! port->attached ) &&
  1488. ( ( rc = usb_attached ( port ) ) != 0 ) )
  1489. return rc;
  1490. return 0;
  1491. }
  1492. /******************************************************************************
  1493. *
  1494. * USB process
  1495. *
  1496. ******************************************************************************
  1497. */
  1498. /**
  1499. * Report port status change
  1500. *
  1501. * @v port USB port
  1502. */
  1503. void usb_port_changed ( struct usb_port *port ) {
  1504. /* Record hub port status change */
  1505. list_del ( &port->changed );
  1506. list_add_tail ( &port->changed, &usb_changed );
  1507. }
  1508. /**
  1509. * Handle newly attached or detached USB device
  1510. *
  1511. */
  1512. static void usb_hotplug ( void ) {
  1513. struct usb_port *port;
  1514. /* Handle any changed ports, allowing for the fact that the
  1515. * port list may change as we perform hotplug actions.
  1516. */
  1517. while ( ! list_empty ( &usb_changed ) ) {
  1518. /* Get first changed port */
  1519. port = list_first_entry ( &usb_changed, struct usb_port,
  1520. changed );
  1521. assert ( port != NULL );
  1522. /* Remove from list of changed ports */
  1523. list_del ( &port->changed );
  1524. INIT_LIST_HEAD ( &port->changed );
  1525. /* Perform appropriate hotplug action */
  1526. usb_hotplugged ( port );
  1527. }
  1528. }
  1529. /**
  1530. * USB process
  1531. *
  1532. * @v process USB process
  1533. */
  1534. static void usb_step ( struct process *process __unused ) {
  1535. struct usb_bus *bus;
  1536. struct usb_endpoint *ep;
  1537. /* Poll all buses */
  1538. for_each_usb_bus ( bus )
  1539. usb_poll ( bus );
  1540. /* Attempt to reset first halted endpoint in list, if any. We
  1541. * do not attempt to process the complete list, since this
  1542. * would require extra code to allow for the facts that the
  1543. * halted endpoint list may change as we do so, and that
  1544. * resetting an endpoint may fail.
  1545. */
  1546. if ( ( ep = list_first_entry ( &usb_halted, struct usb_endpoint,
  1547. halted ) ) != NULL )
  1548. usb_endpoint_reset ( ep );
  1549. /* Handle any changed ports */
  1550. usb_hotplug();
  1551. }
  1552. /** USB process */
  1553. PERMANENT_PROCESS ( usb_process, usb_step );
  1554. /******************************************************************************
  1555. *
  1556. * USB hub
  1557. *
  1558. ******************************************************************************
  1559. */
  1560. /**
  1561. * Allocate USB hub
  1562. *
  1563. * @v bus USB bus
  1564. * @v usb Underlying USB device, if any
  1565. * @v ports Number of ports
  1566. * @v driver Hub driver operations
  1567. * @ret hub USB hub, or NULL on allocation failure
  1568. */
  1569. struct usb_hub * alloc_usb_hub ( struct usb_bus *bus, struct usb_device *usb,
  1570. unsigned int ports,
  1571. struct usb_hub_driver_operations *driver ) {
  1572. struct usb_hub *hub;
  1573. struct usb_port *port;
  1574. unsigned int i;
  1575. /* Allocate and initialise structure */
  1576. hub = zalloc ( sizeof ( *hub ) + ( ports * sizeof ( hub->port[0] ) ) );
  1577. if ( ! hub )
  1578. return NULL;
  1579. hub->name = ( usb ? usb->name : bus->name );
  1580. hub->bus = bus;
  1581. hub->usb = usb;
  1582. if ( usb )
  1583. hub->protocol = usb->port->protocol;
  1584. hub->ports = ports;
  1585. hub->driver = driver;
  1586. hub->host = &bus->op->hub;
  1587. /* Initialise port list */
  1588. for ( i = 1 ; i <= hub->ports ; i++ ) {
  1589. port = usb_port ( hub, i );
  1590. port->hub = hub;
  1591. port->address = i;
  1592. if ( usb )
  1593. port->protocol = usb->port->protocol;
  1594. INIT_LIST_HEAD ( &port->changed );
  1595. }
  1596. return hub;
  1597. }
  1598. /**
  1599. * Register USB hub
  1600. *
  1601. * @v hub USB hub
  1602. * @ret rc Return status code
  1603. */
  1604. int register_usb_hub ( struct usb_hub *hub ) {
  1605. struct usb_bus *bus = hub->bus;
  1606. struct usb_port *port;
  1607. unsigned int i;
  1608. int rc;
  1609. /* Add to hub list */
  1610. list_add_tail ( &hub->list, &bus->hubs );
  1611. /* Open hub (host controller) */
  1612. if ( ( rc = hub->host->open ( hub ) ) != 0 ) {
  1613. DBGC ( hub, "USB hub %s could not open: %s\n",
  1614. hub->name, strerror ( rc ) );
  1615. goto err_host_open;
  1616. }
  1617. /* Open hub (driver) */
  1618. if ( ( rc = hub->driver->open ( hub ) ) != 0 ) {
  1619. DBGC ( hub, "USB hub %s could not open: %s\n",
  1620. hub->name, strerror ( rc ) );
  1621. goto err_driver_open;
  1622. }
  1623. /* Delay to allow ports to stabilise */
  1624. mdelay ( USB_PORT_DELAY_MS );
  1625. /* Mark all ports as changed */
  1626. for ( i = 1 ; i <= hub->ports ; i++ ) {
  1627. port = usb_port ( hub, i );
  1628. usb_port_changed ( port );
  1629. }
  1630. /* Some hubs seem to defer reporting device connections until
  1631. * their interrupt endpoint is polled for the first time.
  1632. * Poll the bus once now in order to pick up any such
  1633. * connections.
  1634. */
  1635. usb_poll ( bus );
  1636. return 0;
  1637. hub->driver->close ( hub );
  1638. err_driver_open:
  1639. hub->host->close ( hub );
  1640. err_host_open:
  1641. list_del ( &hub->list );
  1642. return rc;
  1643. }
  1644. /**
  1645. * Unregister USB hub
  1646. *
  1647. * @v hub USB hub
  1648. */
  1649. void unregister_usb_hub ( struct usb_hub *hub ) {
  1650. struct usb_port *port;
  1651. unsigned int i;
  1652. /* Detach all devices */
  1653. for ( i = 1 ; i <= hub->ports ; i++ ) {
  1654. port = usb_port ( hub, i );
  1655. if ( port->attached )
  1656. usb_detached ( port );
  1657. }
  1658. /* Close hub (driver) */
  1659. hub->driver->close ( hub );
  1660. /* Close hub (host controller) */
  1661. hub->host->close ( hub );
  1662. /* Cancel any pending port status changes */
  1663. for ( i = 1 ; i <= hub->ports ; i++ ) {
  1664. port = usb_port ( hub, i );
  1665. list_del ( &port->changed );
  1666. INIT_LIST_HEAD ( &port->changed );
  1667. }
  1668. /* Remove from hub list */
  1669. list_del ( &hub->list );
  1670. }
  1671. /**
  1672. * Free USB hub
  1673. *
  1674. * @v hub USB hub
  1675. */
  1676. void free_usb_hub ( struct usb_hub *hub ) {
  1677. struct usb_port *port;
  1678. unsigned int i;
  1679. /* Sanity checks */
  1680. for ( i = 1 ; i <= hub->ports ; i++ ) {
  1681. port = usb_port ( hub, i );
  1682. assert ( ! port->attached );
  1683. assert ( port->usb == NULL );
  1684. assert ( list_empty ( &port->changed ) );
  1685. }
  1686. /* Free hub */
  1687. free ( hub );
  1688. }
  1689. /******************************************************************************
  1690. *
  1691. * USB bus
  1692. *
  1693. ******************************************************************************
  1694. */
  1695. /**
  1696. * Allocate USB bus
  1697. *
  1698. * @v dev Underlying hardware device
  1699. * @v ports Number of root hub ports
  1700. * @v mtu Largest transfer allowed on the bus
  1701. * @v op Host controller operations
  1702. * @ret bus USB bus, or NULL on allocation failure
  1703. */
  1704. struct usb_bus * alloc_usb_bus ( struct device *dev, unsigned int ports,
  1705. size_t mtu, struct usb_host_operations *op ) {
  1706. struct usb_bus *bus;
  1707. /* Allocate and initialise structure */
  1708. bus = zalloc ( sizeof ( *bus ) );
  1709. if ( ! bus )
  1710. goto err_alloc_bus;
  1711. bus->name = dev->name;
  1712. bus->dev = dev;
  1713. bus->mtu = mtu;
  1714. bus->op = op;
  1715. INIT_LIST_HEAD ( &bus->devices );
  1716. INIT_LIST_HEAD ( &bus->hubs );
  1717. bus->host = &bus->op->bus;
  1718. /* Allocate root hub */
  1719. bus->hub = alloc_usb_hub ( bus, NULL, ports, &op->root );
  1720. if ( ! bus->hub )
  1721. goto err_alloc_hub;
  1722. return bus;
  1723. free_usb_hub ( bus->hub );
  1724. err_alloc_hub:
  1725. free ( bus );
  1726. err_alloc_bus:
  1727. return NULL;
  1728. }
  1729. /**
  1730. * Register USB bus
  1731. *
  1732. * @v bus USB bus
  1733. * @ret rc Return status code
  1734. */
  1735. int register_usb_bus ( struct usb_bus *bus ) {
  1736. int rc;
  1737. /* Sanity checks */
  1738. assert ( bus->hub != NULL );
  1739. /* Open bus */
  1740. if ( ( rc = bus->host->open ( bus ) ) != 0 )
  1741. goto err_open;
  1742. /* Add to list of USB buses */
  1743. list_add_tail ( &bus->list, &usb_buses );
  1744. /* Register root hub */
  1745. if ( ( rc = register_usb_hub ( bus->hub ) ) != 0 )
  1746. goto err_register_hub;
  1747. /* Attach any devices already present */
  1748. usb_hotplug();
  1749. return 0;
  1750. unregister_usb_hub ( bus->hub );
  1751. err_register_hub:
  1752. list_del ( &bus->list );
  1753. bus->host->close ( bus );
  1754. err_open:
  1755. return rc;
  1756. }
  1757. /**
  1758. * Unregister USB bus
  1759. *
  1760. * @v bus USB bus
  1761. */
  1762. void unregister_usb_bus ( struct usb_bus *bus ) {
  1763. /* Sanity checks */
  1764. assert ( bus->hub != NULL );
  1765. /* Unregister root hub */
  1766. unregister_usb_hub ( bus->hub );
  1767. /* Remove from list of USB buses */
  1768. list_del ( &bus->list );
  1769. /* Close bus */
  1770. bus->host->close ( bus );
  1771. /* Sanity checks */
  1772. assert ( list_empty ( &bus->devices ) );
  1773. assert ( list_empty ( &bus->hubs ) );
  1774. }
  1775. /**
  1776. * Free USB bus
  1777. *
  1778. * @v bus USB bus
  1779. */
  1780. void free_usb_bus ( struct usb_bus *bus ) {
  1781. struct usb_endpoint *ep;
  1782. struct usb_port *port;
  1783. /* Sanity checks */
  1784. assert ( list_empty ( &bus->devices ) );
  1785. assert ( list_empty ( &bus->hubs ) );
  1786. list_for_each_entry ( ep, &usb_halted, halted )
  1787. assert ( ep->usb->port->hub->bus != bus );
  1788. list_for_each_entry ( port, &usb_changed, changed )
  1789. assert ( port->hub->bus != bus );
  1790. /* Free root hub */
  1791. free_usb_hub ( bus->hub );
  1792. /* Free bus */
  1793. free ( bus );
  1794. }
  1795. /**
  1796. * Find USB bus by device location
  1797. *
  1798. * @v bus_type Bus type
  1799. * @v location Bus location
  1800. * @ret bus USB bus, or NULL
  1801. */
  1802. struct usb_bus * find_usb_bus_by_location ( unsigned int bus_type,
  1803. unsigned int location ) {
  1804. struct usb_bus *bus;
  1805. for_each_usb_bus ( bus ) {
  1806. if ( ( bus->dev->desc.bus_type == bus_type ) &&
  1807. ( bus->dev->desc.location == location ) )
  1808. return bus;
  1809. }
  1810. return NULL;
  1811. }
  1812. /******************************************************************************
  1813. *
  1814. * USB address assignment
  1815. *
  1816. ******************************************************************************
  1817. */
  1818. /**
  1819. * Allocate device address
  1820. *
  1821. * @v bus USB bus
  1822. * @ret address Device address, or negative error
  1823. */
  1824. int usb_alloc_address ( struct usb_bus *bus ) {
  1825. unsigned int address;
  1826. /* Find first free device address */
  1827. address = ffsll ( ~bus->addresses );
  1828. if ( ! address )
  1829. return -ENOENT;
  1830. /* Mark address as used */
  1831. bus->addresses |= ( 1ULL << ( address - 1 ) );
  1832. return address;
  1833. }
  1834. /**
  1835. * Free device address
  1836. *
  1837. * @v bus USB bus
  1838. * @v address Device address
  1839. */
  1840. void usb_free_address ( struct usb_bus *bus, unsigned int address ) {
  1841. /* Sanity check */
  1842. assert ( address > 0 );
  1843. assert ( bus->addresses & ( 1ULL << ( address - 1 ) ) );
  1844. /* Mark address as free */
  1845. bus->addresses &= ~( 1ULL << ( address - 1 ) );
  1846. }
  1847. /******************************************************************************
  1848. *
  1849. * USB bus topology
  1850. *
  1851. ******************************************************************************
  1852. */
  1853. /**
  1854. * Get USB route string
  1855. *
  1856. * @v usb USB device
  1857. * @ret route USB route string
  1858. */
  1859. unsigned int usb_route_string ( struct usb_device *usb ) {
  1860. struct usb_device *parent;
  1861. unsigned int route;
  1862. /* Navigate up to root hub, constructing route string as we go */
  1863. for ( route = 0 ; ( parent = usb->port->hub->usb ) ; usb = parent ) {
  1864. route <<= 4;
  1865. route |= ( ( usb->port->address > 0xf ) ?
  1866. 0xf : usb->port->address );
  1867. }
  1868. return route;
  1869. }
  1870. /**
  1871. * Get USB depth
  1872. *
  1873. * @v usb USB device
  1874. * @ret depth Hub depth
  1875. */
  1876. unsigned int usb_depth ( struct usb_device *usb ) {
  1877. struct usb_device *parent;
  1878. unsigned int depth;
  1879. /* Navigate up to root hub, constructing depth as we go */
  1880. for ( depth = 0 ; ( parent = usb->port->hub->usb ) ; usb = parent )
  1881. depth++;
  1882. return depth;
  1883. }
  1884. /**
  1885. * Get USB root hub port
  1886. *
  1887. * @v usb USB device
  1888. * @ret port Root hub port
  1889. */
  1890. struct usb_port * usb_root_hub_port ( struct usb_device *usb ) {
  1891. struct usb_device *parent;
  1892. /* Navigate up to root hub */
  1893. while ( ( parent = usb->port->hub->usb ) )
  1894. usb = parent;
  1895. return usb->port;
  1896. }
  1897. /**
  1898. * Get USB transaction translator
  1899. *
  1900. * @v usb USB device
  1901. * @ret port Transaction translator port, or NULL
  1902. */
  1903. struct usb_port * usb_transaction_translator ( struct usb_device *usb ) {
  1904. struct usb_device *parent;
  1905. /* Navigate up to root hub. If we find a low-speed or
  1906. * full-speed device with a higher-speed parent hub, then that
  1907. * device's port is the transaction translator.
  1908. */
  1909. for ( ; ( parent = usb->port->hub->usb ) ; usb = parent ) {
  1910. if ( ( usb->speed <= USB_SPEED_FULL ) &&
  1911. ( parent->speed > USB_SPEED_FULL ) )
  1912. return usb->port;
  1913. }
  1914. return NULL;
  1915. }
  1916. /* Drag in objects via register_usb_bus() */
  1917. REQUIRING_SYMBOL ( register_usb_bus );
  1918. /* Drag in USB configuration */
  1919. REQUIRE_OBJECT ( config_usb );
  1920. /* Drag in hub driver */
  1921. REQUIRE_OBJECT ( usbhub );