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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261
  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, &association->class,
  875. sizeof ( desc->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, &interface->class, sizeof ( desc->class ) );
  890. desc->count = 1;
  891. interfaces[0] = first;
  892. /* Look for a CDC union descriptor, if applicable */
  893. if ( ( desc->class.class.class == USB_CLASS_CDC ) &&
  894. ( cdc_union = cdc_union_descriptor ( config, interface ) ) ) {
  895. /* Determine interface count */
  896. desc->count = ( ( cdc_union->header.len -
  897. offsetof ( typeof ( *cdc_union ),
  898. interface[0] ) ) /
  899. sizeof ( cdc_union->interface[0] ) );
  900. if ( desc->count > config->interfaces ) {
  901. DBGC ( usb, "USB %s has invalid union functional "
  902. "descriptor with %d interfaces\n",
  903. usb->name, desc->count );
  904. return -ERANGE;
  905. }
  906. /* Describe function */
  907. for ( i = 0 ; i < desc->count ; i++ ) {
  908. if ( cdc_union->interface[i] >= config->interfaces ) {
  909. DBGC ( usb, "USB %s has invalid union "
  910. "functional descriptor covering "
  911. "interface %d\n", usb->name,
  912. cdc_union->interface[i] );
  913. return -ERANGE;
  914. }
  915. interfaces[i] = cdc_union->interface[i];
  916. }
  917. return 0;
  918. }
  919. return 0;
  920. }
  921. /**
  922. * Update list of used interface
  923. *
  924. * @v usb USB device
  925. * @v count Number of interfaces
  926. * @v interface List of interfaces
  927. * @v used List of already-used interfaces
  928. * @ret rc Return status code
  929. */
  930. static int usb_used ( struct usb_device *usb, unsigned int count,
  931. uint8_t *interface, uint8_t *used ) {
  932. unsigned int i;
  933. for ( i = 0 ; i < count ; i++ ) {
  934. if ( used[interface[i]] ) {
  935. DBGC ( usb, "USB %s interface %d already in use\n",
  936. usb->name, interface[i] );
  937. return -EINVAL;
  938. }
  939. used[interface[i]] = 1;
  940. }
  941. return 0;
  942. }
  943. /**
  944. * Find USB device driver
  945. *
  946. * @v desc Function descriptor
  947. * @ret id USB device ID, or NULL
  948. * @ret driver USB device driver, or NULL
  949. */
  950. struct usb_driver * usb_find_driver ( struct usb_function_descriptor *desc,
  951. struct usb_device_id **id ) {
  952. struct usb_driver *driver;
  953. unsigned int i;
  954. /* Look for a matching driver */
  955. for_each_table_entry ( driver, USB_DRIVERS ) {
  956. for ( i = 0 ; i < driver->id_count ; i++ ) {
  957. /* Ignore non-matching driver class */
  958. if ( ( driver->class.class.scalar ^ desc->class.scalar )
  959. & driver->class.mask.scalar )
  960. continue;
  961. /* Look for a matching ID */
  962. *id = &driver->ids[i];
  963. if ( ( ( (*id)->vendor == desc->vendor ) ||
  964. ( (*id)->vendor == USB_ANY_ID ) ) &&
  965. ( ( (*id)->product == desc->product ) ||
  966. ( (*id)->product == USB_ANY_ID ) ) )
  967. return driver;
  968. }
  969. }
  970. /* Not found */
  971. *id = NULL;
  972. return NULL;
  973. }
  974. /**
  975. * Get USB device configuration score
  976. *
  977. * @v usb USB device
  978. * @v config Configuration descriptor
  979. * @ret score Device configuration score, or negative error
  980. */
  981. static int usb_score ( struct usb_device *usb,
  982. struct usb_configuration_descriptor *config ) {
  983. uint8_t used[config->interfaces];
  984. uint8_t interface[config->interfaces];
  985. struct usb_function_descriptor desc;
  986. struct usb_driver *driver;
  987. struct usb_device_id *id;
  988. unsigned int first;
  989. unsigned int score = 0;
  990. int rc;
  991. /* Identify each function in turn */
  992. memset ( used, 0, sizeof ( used ) );
  993. for ( first = 0 ; first < config->interfaces ; first++ ) {
  994. /* Skip interfaces already used */
  995. if ( used[first] )
  996. continue;
  997. /* Describe function */
  998. if ( ( rc = usb_describe ( usb, config, first, interface,
  999. &desc ) ) != 0 )
  1000. return rc;
  1001. /* Update used interfaces */
  1002. if ( ( rc = usb_used ( usb, desc.count, interface,
  1003. used ) ) != 0 )
  1004. return rc;
  1005. /* Look for a driver for this function */
  1006. driver = usb_find_driver ( &desc, &id );
  1007. if ( driver )
  1008. score += driver->score;
  1009. }
  1010. return score;
  1011. }
  1012. /**
  1013. * Probe USB device driver
  1014. *
  1015. * @v func USB function
  1016. * @v config Configuration descriptor
  1017. * @ret rc Return status code
  1018. */
  1019. static int usb_probe ( struct usb_function *func,
  1020. struct usb_configuration_descriptor *config ) {
  1021. struct usb_device *usb = func->usb;
  1022. struct usb_driver *driver;
  1023. struct usb_device_id *id;
  1024. int rc;
  1025. /* Identify driver */
  1026. driver = usb_find_driver ( &func->desc, &id );
  1027. if ( ! driver ) {
  1028. DBGC ( usb, "USB %s %04x:%04x class %d:%d:%d has no driver\n",
  1029. func->name, func->desc.vendor, func->desc.product,
  1030. func->desc.class.class.class,
  1031. func->desc.class.class.subclass,
  1032. func->desc.class.class.protocol );
  1033. return -ENOENT;
  1034. }
  1035. /* Record driver */
  1036. func->driver = driver;
  1037. func->id = id;
  1038. func->dev.driver_name = id->name;
  1039. /* Probe driver */
  1040. if ( ( rc = driver->probe ( func, config ) ) != 0 ) {
  1041. DBGC ( usb, "USB %s failed to probe driver %s: %s\n",
  1042. func->name, id->name, strerror ( rc ) );
  1043. return rc;
  1044. }
  1045. return 0;
  1046. }
  1047. /**
  1048. * Remove USB device driver
  1049. *
  1050. * @v func USB function
  1051. */
  1052. static void usb_remove ( struct usb_function *func ) {
  1053. /* Remove driver */
  1054. func->driver->remove ( func );
  1055. }
  1056. /**
  1057. * Probe all USB device drivers
  1058. *
  1059. * @v usb USB device
  1060. * @v config Configuration descriptor
  1061. */
  1062. static void
  1063. usb_probe_all ( struct usb_device *usb,
  1064. struct usb_configuration_descriptor *config ) {
  1065. struct usb_bus *bus = usb->port->hub->bus;
  1066. struct usb_function *func;
  1067. uint8_t used[config->interfaces];
  1068. unsigned int first;
  1069. unsigned int i;
  1070. int rc;
  1071. /* Identify each function in turn */
  1072. memset ( used, 0, sizeof ( used ) );
  1073. for ( first = 0 ; first < config->interfaces ; first++ ) {
  1074. /* Skip interfaces already used */
  1075. if ( used[first] )
  1076. continue;
  1077. /* Allocate and initialise structure */
  1078. func = zalloc ( sizeof ( *func ) +
  1079. ( config->interfaces *
  1080. sizeof ( func->interface[0] ) ) );
  1081. if ( ! func )
  1082. goto err_alloc;
  1083. func->name = func->dev.name;
  1084. func->usb = usb;
  1085. func->dev.desc.bus_type = BUS_TYPE_USB;
  1086. func->dev.desc.location = usb->address;
  1087. func->dev.desc.vendor = le16_to_cpu ( usb->device.vendor );
  1088. func->dev.desc.device = le16_to_cpu ( usb->device.product );
  1089. snprintf ( func->dev.name, sizeof ( func->dev.name ),
  1090. "%s-%d.%d", usb->name, config->config, first );
  1091. INIT_LIST_HEAD ( &func->dev.children );
  1092. func->dev.parent = bus->dev;
  1093. list_add_tail ( &func->list, &usb->functions );
  1094. /* Identify function */
  1095. if ( ( rc = usb_describe ( usb, config, first, func->interface,
  1096. &func->desc ) ) != 0 )
  1097. goto err_describe;
  1098. assert ( func->desc.count <= config->interfaces );
  1099. /* Mark interfaces as used */
  1100. if ( ( rc = usb_used ( usb, func->desc.count, func->interface,
  1101. used ) ) != 0 )
  1102. goto err_used;
  1103. /* Probe device driver */
  1104. if ( ( rc = usb_probe ( func, config ) ) != 0 )
  1105. goto err_probe;
  1106. DBGC ( usb, "USB %s %04x:%04x class %d:%d:%d interfaces ",
  1107. func->name, func->desc.vendor, func->desc.product,
  1108. func->desc.class.class.class,
  1109. func->desc.class.class.subclass,
  1110. func->desc.class.class.protocol );
  1111. for ( i = 0 ; i < func->desc.count ; i++ )
  1112. DBGC ( usb, "%s%d", ( i ? "," : "" ),
  1113. func->interface[i] );
  1114. DBGC ( usb, " using driver %s\n", func->dev.driver_name );
  1115. /* Add to device hierarchy */
  1116. list_add_tail ( &func->dev.siblings, &bus->dev->children );
  1117. continue;
  1118. list_del ( &func->dev.siblings );
  1119. usb_remove ( func );
  1120. err_probe:
  1121. err_used:
  1122. err_describe:
  1123. list_del ( &func->list );
  1124. free ( func );
  1125. err_alloc:
  1126. /* Continue registering other functions */
  1127. continue;
  1128. }
  1129. }
  1130. /**
  1131. * Remove all device drivers
  1132. *
  1133. * @v usb USB device
  1134. */
  1135. static void usb_remove_all ( struct usb_device *usb ) {
  1136. struct usb_function *func;
  1137. struct usb_function *tmp;
  1138. /* Remove all functions */
  1139. list_for_each_entry_safe ( func, tmp, &usb->functions, list ) {
  1140. /* Remove device driver */
  1141. usb_remove ( func );
  1142. /* Remove from device hierarchy */
  1143. assert ( list_empty ( &func->dev.children ) );
  1144. list_del ( &func->dev.siblings );
  1145. /* Remove from list of functions */
  1146. list_del ( &func->list );
  1147. /* Free function */
  1148. free ( func );
  1149. }
  1150. }
  1151. /**
  1152. * Clear USB device configuration
  1153. *
  1154. * @v usb USB device
  1155. */
  1156. static void usb_deconfigure ( struct usb_device *usb ) {
  1157. unsigned int i;
  1158. /* Remove device drivers */
  1159. usb_remove_all ( usb );
  1160. /* Sanity checks */
  1161. for ( i = 0 ; i < ( sizeof ( usb->ep ) / sizeof ( usb->ep[0] ) ) ; i++){
  1162. if ( i != USB_ENDPOINT_IDX ( USB_EP0_ADDRESS ) )
  1163. assert ( usb->ep[i] == NULL );
  1164. }
  1165. /* Clear device configuration */
  1166. usb_set_configuration ( usb, 0 );
  1167. }
  1168. /**
  1169. * Choose our preferred USB device configuration
  1170. *
  1171. * @v usb USB device
  1172. * @ret rc Return status code
  1173. */
  1174. static int usb_autoconfigure ( struct usb_device *usb ) {
  1175. struct usb_configuration_descriptor *config;
  1176. unsigned int preferred = 0;
  1177. unsigned int index;
  1178. int score;
  1179. int best = 0;
  1180. int rc;
  1181. /* Calculate driver score for each configuration index */
  1182. for ( index = 0 ; index < usb->device.configurations ; index++ ) {
  1183. /* Read configuration descriptor */
  1184. if ( ( rc = usb_config_descriptor ( usb, index,
  1185. &config ) ) != 0 )
  1186. goto err_config;
  1187. /* Get score for this configuration */
  1188. score = usb_score ( usb, config );
  1189. if ( score < 0 ) {
  1190. rc = score;
  1191. goto err_score;
  1192. }
  1193. DBGC2 ( usb, "USB %s configuration %d score %d\n",
  1194. usb->name, config->config, score );
  1195. /* Record as preferred configuration, if applicable */
  1196. if ( score > best ) {
  1197. best = score;
  1198. preferred = index;
  1199. }
  1200. /* Free configuration descriptor */
  1201. free ( config );
  1202. config = NULL;
  1203. }
  1204. /* Read preferred configuration descriptor */
  1205. if ( ( rc = usb_config_descriptor ( usb, preferred, &config ) ) != 0 )
  1206. goto err_preferred;
  1207. /* Set configuration */
  1208. if ( ( rc = usb_set_configuration ( usb, config->config ) ) != 0){
  1209. DBGC ( usb, "USB %s could not set configuration %d: %s\n",
  1210. usb->name, config->config, strerror ( rc ) );
  1211. goto err_set_configuration;
  1212. }
  1213. /* Probe USB device drivers */
  1214. usb_probe_all ( usb, config );
  1215. /* Free configuration descriptor */
  1216. free ( config );
  1217. return 0;
  1218. usb_remove_all ( usb );
  1219. usb_set_configuration ( usb, 0 );
  1220. err_set_configuration:
  1221. free ( config );
  1222. err_preferred:
  1223. return rc;
  1224. err_score:
  1225. free ( config );
  1226. err_config:
  1227. return rc;
  1228. }
  1229. /******************************************************************************
  1230. *
  1231. * USB device
  1232. *
  1233. ******************************************************************************
  1234. */
  1235. /**
  1236. * Allocate USB device
  1237. *
  1238. * @v port USB port
  1239. * @ret usb USB device, or NULL on allocation failure
  1240. */
  1241. static struct usb_device * alloc_usb ( struct usb_port *port ) {
  1242. struct usb_hub *hub = port->hub;
  1243. struct usb_bus *bus = hub->bus;
  1244. struct usb_device *usb;
  1245. /* Allocate and initialise structure */
  1246. usb = zalloc ( sizeof ( *usb ) );
  1247. if ( ! usb )
  1248. return NULL;
  1249. snprintf ( usb->name, sizeof ( usb->name ), "%s%c%d", hub->name,
  1250. ( hub->usb ? '.' : '-' ), port->address );
  1251. usb->port = port;
  1252. INIT_LIST_HEAD ( &usb->functions );
  1253. usb->host = &bus->op->device;
  1254. usb_endpoint_init ( &usb->control, usb, &usb_control_operations );
  1255. INIT_LIST_HEAD ( &usb->complete );
  1256. return usb;
  1257. }
  1258. /**
  1259. * Register USB device
  1260. *
  1261. * @v usb USB device
  1262. * @ret rc Return status code
  1263. */
  1264. static int register_usb ( struct usb_device *usb ) {
  1265. struct usb_port *port = usb->port;
  1266. struct usb_hub *hub = port->hub;
  1267. struct usb_bus *bus = hub->bus;
  1268. unsigned int protocol;
  1269. size_t mtu;
  1270. int rc;
  1271. /* Add to port */
  1272. if ( port->usb != NULL ) {
  1273. DBGC ( hub, "USB hub %s port %d is already registered to %s\n",
  1274. hub->name, port->address, port->usb->name );
  1275. rc = -EALREADY;
  1276. goto err_already;
  1277. }
  1278. port->usb = usb;
  1279. /* Add to bus device list */
  1280. list_add_tail ( &usb->list, &bus->devices );
  1281. /* Enable device */
  1282. if ( ( rc = hub->driver->enable ( hub, port ) ) != 0 ) {
  1283. DBGC ( hub, "USB hub %s port %d could not enable: %s\n",
  1284. hub->name, port->address, strerror ( rc ) );
  1285. goto err_enable;
  1286. }
  1287. /* Allow recovery interval since port may have been reset */
  1288. mdelay ( USB_RESET_RECOVER_DELAY_MS );
  1289. /* Get device speed */
  1290. if ( ( rc = hub->driver->speed ( hub, port ) ) != 0 ) {
  1291. DBGC ( hub, "USB hub %s port %d could not get speed: %s\n",
  1292. hub->name, port->address, strerror ( rc ) );
  1293. goto err_speed;
  1294. }
  1295. usb->speed = port->speed;
  1296. DBGC2 ( usb, "USB %s attached as %s-speed device\n",
  1297. usb->name, usb_speed_name ( usb->speed ) );
  1298. /* Open device */
  1299. if ( ( rc = usb->host->open ( usb ) ) != 0 ) {
  1300. DBGC ( usb, "USB %s could not open: %s\n",
  1301. usb->name, strerror ( rc ) );
  1302. goto err_open;
  1303. }
  1304. /* Describe control endpoint */
  1305. mtu = USB_EP0_DEFAULT_MTU ( usb->speed );
  1306. usb_endpoint_describe ( &usb->control, USB_EP0_ADDRESS,
  1307. USB_EP0_ATTRIBUTES, mtu, USB_EP0_BURST,
  1308. USB_EP0_INTERVAL );
  1309. /* Open control endpoint */
  1310. if ( ( rc = usb_endpoint_open ( &usb->control ) ) != 0 )
  1311. goto err_open_control;
  1312. assert ( usb_endpoint ( usb, USB_EP0_ADDRESS ) == &usb->control );
  1313. /* Assign device address */
  1314. if ( ( rc = usb->host->address ( usb ) ) != 0 ) {
  1315. DBGC ( usb, "USB %s could not set address: %s\n",
  1316. usb->name, strerror ( rc ) );
  1317. goto err_address;
  1318. }
  1319. DBGC2 ( usb, "USB %s assigned address %d\n", usb->name, usb->address );
  1320. /* Allow recovery interval after Set Address command */
  1321. mdelay ( USB_SET_ADDRESS_RECOVER_DELAY_MS );
  1322. /* Read first part of device descriptor to get EP0 MTU */
  1323. if ( ( rc = usb_get_mtu ( usb, &usb->device ) ) != 0 ) {
  1324. DBGC ( usb, "USB %s could not get MTU: %s\n",
  1325. usb->name, strerror ( rc ) );
  1326. goto err_get_mtu;
  1327. }
  1328. /* Calculate EP0 MTU */
  1329. protocol = le16_to_cpu ( usb->device.protocol );
  1330. mtu = ( ( protocol < USB_PROTO_3_0 ) ?
  1331. usb->device.mtu : ( 1 << usb->device.mtu ) );
  1332. DBGC2 ( usb, "USB %s has control MTU %zd (guessed %zd)\n",
  1333. usb->name, mtu, usb->control.mtu );
  1334. /* Update MTU */
  1335. if ( ( rc = usb_endpoint_mtu ( &usb->control, mtu ) ) != 0 )
  1336. goto err_mtu;
  1337. /* Read whole device descriptor */
  1338. if ( ( rc = usb_get_device_descriptor ( usb, &usb->device ) ) != 0 ) {
  1339. DBGC ( usb, "USB %s could not get device descriptor: %s\n",
  1340. usb->name, strerror ( rc ) );
  1341. goto err_get_device_descriptor;
  1342. }
  1343. DBGC ( usb, "USB %s addr %d %04x:%04x class %d:%d:%d (v%s, %s-speed, "
  1344. "MTU %zd)\n", usb->name, usb->address,
  1345. le16_to_cpu ( usb->device.vendor ),
  1346. le16_to_cpu ( usb->device.product ), usb->device.class.class,
  1347. usb->device.class.subclass, usb->device.class.protocol,
  1348. usb_bcd ( le16_to_cpu ( usb->device.protocol ) ),
  1349. usb_speed_name ( usb->speed ), usb->control.mtu );
  1350. /* Configure device */
  1351. if ( ( rc = usb_autoconfigure ( usb ) ) != 0 )
  1352. goto err_autoconfigure;
  1353. return 0;
  1354. usb_deconfigure ( usb );
  1355. err_autoconfigure:
  1356. err_get_device_descriptor:
  1357. err_mtu:
  1358. err_get_mtu:
  1359. err_address:
  1360. usb_endpoint_close ( &usb->control );
  1361. err_open_control:
  1362. usb->host->close ( usb );
  1363. err_open:
  1364. err_speed:
  1365. hub->driver->disable ( hub, port );
  1366. err_enable:
  1367. list_del ( &usb->list );
  1368. port->usb = NULL;
  1369. err_already:
  1370. return rc;
  1371. }
  1372. /**
  1373. * Unregister USB device
  1374. *
  1375. * @v usb USB device
  1376. */
  1377. static void unregister_usb ( struct usb_device *usb ) {
  1378. struct usb_port *port = usb->port;
  1379. struct usb_hub *hub = port->hub;
  1380. struct io_buffer *iobuf;
  1381. struct io_buffer *tmp;
  1382. /* Sanity checks */
  1383. assert ( port->usb == usb );
  1384. /* Clear device configuration */
  1385. usb_deconfigure ( usb );
  1386. /* Close control endpoint */
  1387. usb_endpoint_close ( &usb->control );
  1388. /* Discard any stale control completions */
  1389. list_for_each_entry_safe ( iobuf, tmp, &usb->complete, list ) {
  1390. list_del ( &iobuf->list );
  1391. free_iob ( iobuf );
  1392. }
  1393. /* Close device */
  1394. usb->host->close ( usb );
  1395. /* Disable port */
  1396. hub->driver->disable ( hub, port );
  1397. /* Remove from bus device list */
  1398. list_del ( &usb->list );
  1399. /* Remove from port */
  1400. port->usb = NULL;
  1401. }
  1402. /**
  1403. * Free USB device
  1404. *
  1405. * @v usb USB device
  1406. */
  1407. static void free_usb ( struct usb_device *usb ) {
  1408. unsigned int i;
  1409. /* Sanity checks */
  1410. for ( i = 0 ; i < ( sizeof ( usb->ep ) / sizeof ( usb->ep[0] ) ) ; i++ )
  1411. assert ( usb->ep[i] == NULL );
  1412. assert ( list_empty ( &usb->functions ) );
  1413. assert ( list_empty ( &usb->complete ) );
  1414. /* Free device */
  1415. free ( usb );
  1416. }
  1417. /******************************************************************************
  1418. *
  1419. * USB device hotplug event handling
  1420. *
  1421. ******************************************************************************
  1422. */
  1423. /**
  1424. * Handle newly attached USB device
  1425. *
  1426. * @v port USB port
  1427. * @ret rc Return status code
  1428. */
  1429. static int usb_attached ( struct usb_port *port ) {
  1430. struct usb_device *usb;
  1431. int rc;
  1432. /* Mark port as attached */
  1433. port->attached = 1;
  1434. /* Sanity checks */
  1435. assert ( port->usb == NULL );
  1436. /* Allocate USB device */
  1437. usb = alloc_usb ( port );
  1438. if ( ! usb ) {
  1439. rc = -ENOMEM;
  1440. goto err_alloc;
  1441. }
  1442. /* Register USB device */
  1443. if ( ( rc = register_usb ( usb ) ) != 0 )
  1444. goto err_register;
  1445. return 0;
  1446. unregister_usb ( usb );
  1447. err_register:
  1448. free_usb ( usb );
  1449. err_alloc:
  1450. return rc;
  1451. }
  1452. /**
  1453. * Handle newly detached USB device
  1454. *
  1455. * @v port USB port
  1456. */
  1457. static void usb_detached ( struct usb_port *port ) {
  1458. struct usb_device *usb = port->usb;
  1459. /* Mark port as detached */
  1460. port->attached = 0;
  1461. /* Do nothing if we have no USB device */
  1462. if ( ! usb )
  1463. return;
  1464. /* Unregister USB device */
  1465. unregister_usb ( usb );
  1466. /* Free USB device */
  1467. free_usb ( usb );
  1468. }
  1469. /**
  1470. * Handle newly attached or detached USB device
  1471. *
  1472. * @v port USB port
  1473. * @ret rc Return status code
  1474. */
  1475. static int usb_hotplugged ( struct usb_port *port ) {
  1476. struct usb_hub *hub = port->hub;
  1477. int rc;
  1478. /* Get current port speed */
  1479. if ( ( rc = hub->driver->speed ( hub, port ) ) != 0 ) {
  1480. DBGC ( hub, "USB hub %s port %d could not get speed: %s\n",
  1481. hub->name, port->address, strerror ( rc ) );
  1482. /* Treat as a disconnection */
  1483. port->disconnected = 1;
  1484. port->speed = USB_SPEED_NONE;
  1485. }
  1486. /* Detach device, if applicable */
  1487. if ( port->attached && ( port->disconnected || ! port->speed ) )
  1488. usb_detached ( port );
  1489. /* Clear any recorded disconnections */
  1490. port->disconnected = 0;
  1491. /* Attach device, if applicable */
  1492. if ( port->speed && ( ! port->attached ) &&
  1493. ( ( rc = usb_attached ( port ) ) != 0 ) )
  1494. return rc;
  1495. return 0;
  1496. }
  1497. /******************************************************************************
  1498. *
  1499. * USB process
  1500. *
  1501. ******************************************************************************
  1502. */
  1503. /**
  1504. * Report port status change
  1505. *
  1506. * @v port USB port
  1507. */
  1508. void usb_port_changed ( struct usb_port *port ) {
  1509. /* Record hub port status change */
  1510. list_del ( &port->changed );
  1511. list_add_tail ( &port->changed, &usb_changed );
  1512. }
  1513. /**
  1514. * Handle newly attached or detached USB device
  1515. *
  1516. */
  1517. static void usb_hotplug ( void ) {
  1518. struct usb_port *port;
  1519. /* Handle any changed ports, allowing for the fact that the
  1520. * port list may change as we perform hotplug actions.
  1521. */
  1522. while ( ! list_empty ( &usb_changed ) ) {
  1523. /* Get first changed port */
  1524. port = list_first_entry ( &usb_changed, struct usb_port,
  1525. changed );
  1526. assert ( port != NULL );
  1527. /* Remove from list of changed ports */
  1528. list_del ( &port->changed );
  1529. INIT_LIST_HEAD ( &port->changed );
  1530. /* Perform appropriate hotplug action */
  1531. usb_hotplugged ( port );
  1532. }
  1533. }
  1534. /**
  1535. * USB process
  1536. *
  1537. * @v process USB process
  1538. */
  1539. static void usb_step ( struct process *process __unused ) {
  1540. struct usb_bus *bus;
  1541. struct usb_endpoint *ep;
  1542. /* Poll all buses */
  1543. for_each_usb_bus ( bus )
  1544. usb_poll ( bus );
  1545. /* Attempt to reset first halted endpoint in list, if any. We
  1546. * do not attempt to process the complete list, since this
  1547. * would require extra code to allow for the facts that the
  1548. * halted endpoint list may change as we do so, and that
  1549. * resetting an endpoint may fail.
  1550. */
  1551. if ( ( ep = list_first_entry ( &usb_halted, struct usb_endpoint,
  1552. halted ) ) != NULL )
  1553. usb_endpoint_reset ( ep );
  1554. /* Handle any changed ports */
  1555. usb_hotplug();
  1556. }
  1557. /** USB process */
  1558. PERMANENT_PROCESS ( usb_process, usb_step );
  1559. /******************************************************************************
  1560. *
  1561. * USB hub
  1562. *
  1563. ******************************************************************************
  1564. */
  1565. /**
  1566. * Allocate USB hub
  1567. *
  1568. * @v bus USB bus
  1569. * @v usb Underlying USB device, if any
  1570. * @v ports Number of ports
  1571. * @v driver Hub driver operations
  1572. * @ret hub USB hub, or NULL on allocation failure
  1573. */
  1574. struct usb_hub * alloc_usb_hub ( struct usb_bus *bus, struct usb_device *usb,
  1575. unsigned int ports,
  1576. struct usb_hub_driver_operations *driver ) {
  1577. struct usb_hub *hub;
  1578. struct usb_port *port;
  1579. unsigned int i;
  1580. /* Allocate and initialise structure */
  1581. hub = zalloc ( sizeof ( *hub ) + ( ports * sizeof ( hub->port[0] ) ) );
  1582. if ( ! hub )
  1583. return NULL;
  1584. hub->name = ( usb ? usb->name : bus->name );
  1585. hub->bus = bus;
  1586. hub->usb = usb;
  1587. if ( usb )
  1588. hub->protocol = usb->port->protocol;
  1589. hub->ports = ports;
  1590. hub->driver = driver;
  1591. hub->host = &bus->op->hub;
  1592. /* Initialise port list */
  1593. for ( i = 1 ; i <= hub->ports ; i++ ) {
  1594. port = usb_port ( hub, i );
  1595. port->hub = hub;
  1596. port->address = i;
  1597. if ( usb )
  1598. port->protocol = usb->port->protocol;
  1599. INIT_LIST_HEAD ( &port->changed );
  1600. }
  1601. return hub;
  1602. }
  1603. /**
  1604. * Register USB hub
  1605. *
  1606. * @v hub USB hub
  1607. * @ret rc Return status code
  1608. */
  1609. int register_usb_hub ( struct usb_hub *hub ) {
  1610. struct usb_bus *bus = hub->bus;
  1611. struct usb_port *port;
  1612. unsigned int i;
  1613. int rc;
  1614. /* Add to hub list */
  1615. list_add_tail ( &hub->list, &bus->hubs );
  1616. /* Open hub (host controller) */
  1617. if ( ( rc = hub->host->open ( hub ) ) != 0 ) {
  1618. DBGC ( hub, "USB hub %s could not open: %s\n",
  1619. hub->name, strerror ( rc ) );
  1620. goto err_host_open;
  1621. }
  1622. /* Open hub (driver) */
  1623. if ( ( rc = hub->driver->open ( hub ) ) != 0 ) {
  1624. DBGC ( hub, "USB hub %s could not open: %s\n",
  1625. hub->name, strerror ( rc ) );
  1626. goto err_driver_open;
  1627. }
  1628. /* Delay to allow ports to stabilise */
  1629. mdelay ( USB_PORT_DELAY_MS );
  1630. /* Mark all ports as changed */
  1631. for ( i = 1 ; i <= hub->ports ; i++ ) {
  1632. port = usb_port ( hub, i );
  1633. usb_port_changed ( port );
  1634. }
  1635. /* Some hubs seem to defer reporting device connections until
  1636. * their interrupt endpoint is polled for the first time.
  1637. * Poll the bus once now in order to pick up any such
  1638. * connections.
  1639. */
  1640. usb_poll ( bus );
  1641. return 0;
  1642. hub->driver->close ( hub );
  1643. err_driver_open:
  1644. hub->host->close ( hub );
  1645. err_host_open:
  1646. list_del ( &hub->list );
  1647. return rc;
  1648. }
  1649. /**
  1650. * Unregister USB hub
  1651. *
  1652. * @v hub USB hub
  1653. */
  1654. void unregister_usb_hub ( struct usb_hub *hub ) {
  1655. struct usb_port *port;
  1656. unsigned int i;
  1657. /* Detach all devices */
  1658. for ( i = 1 ; i <= hub->ports ; i++ ) {
  1659. port = usb_port ( hub, i );
  1660. if ( port->attached )
  1661. usb_detached ( port );
  1662. }
  1663. /* Close hub (driver) */
  1664. hub->driver->close ( hub );
  1665. /* Close hub (host controller) */
  1666. hub->host->close ( hub );
  1667. /* Cancel any pending port status changes */
  1668. for ( i = 1 ; i <= hub->ports ; i++ ) {
  1669. port = usb_port ( hub, i );
  1670. list_del ( &port->changed );
  1671. INIT_LIST_HEAD ( &port->changed );
  1672. }
  1673. /* Remove from hub list */
  1674. list_del ( &hub->list );
  1675. }
  1676. /**
  1677. * Free USB hub
  1678. *
  1679. * @v hub USB hub
  1680. */
  1681. void free_usb_hub ( struct usb_hub *hub ) {
  1682. struct usb_port *port;
  1683. unsigned int i;
  1684. /* Sanity checks */
  1685. for ( i = 1 ; i <= hub->ports ; i++ ) {
  1686. port = usb_port ( hub, i );
  1687. assert ( ! port->attached );
  1688. assert ( port->usb == NULL );
  1689. assert ( list_empty ( &port->changed ) );
  1690. }
  1691. /* Free hub */
  1692. free ( hub );
  1693. }
  1694. /******************************************************************************
  1695. *
  1696. * USB bus
  1697. *
  1698. ******************************************************************************
  1699. */
  1700. /**
  1701. * Allocate USB bus
  1702. *
  1703. * @v dev Underlying hardware device
  1704. * @v ports Number of root hub ports
  1705. * @v mtu Largest transfer allowed on the bus
  1706. * @v op Host controller operations
  1707. * @ret bus USB bus, or NULL on allocation failure
  1708. */
  1709. struct usb_bus * alloc_usb_bus ( struct device *dev, unsigned int ports,
  1710. size_t mtu, struct usb_host_operations *op ) {
  1711. struct usb_bus *bus;
  1712. /* Allocate and initialise structure */
  1713. bus = zalloc ( sizeof ( *bus ) );
  1714. if ( ! bus )
  1715. goto err_alloc_bus;
  1716. bus->name = dev->name;
  1717. bus->dev = dev;
  1718. bus->mtu = mtu;
  1719. bus->op = op;
  1720. INIT_LIST_HEAD ( &bus->devices );
  1721. INIT_LIST_HEAD ( &bus->hubs );
  1722. bus->host = &bus->op->bus;
  1723. /* Allocate root hub */
  1724. bus->hub = alloc_usb_hub ( bus, NULL, ports, &op->root );
  1725. if ( ! bus->hub )
  1726. goto err_alloc_hub;
  1727. return bus;
  1728. free_usb_hub ( bus->hub );
  1729. err_alloc_hub:
  1730. free ( bus );
  1731. err_alloc_bus:
  1732. return NULL;
  1733. }
  1734. /**
  1735. * Register USB bus
  1736. *
  1737. * @v bus USB bus
  1738. * @ret rc Return status code
  1739. */
  1740. int register_usb_bus ( struct usb_bus *bus ) {
  1741. int rc;
  1742. /* Sanity checks */
  1743. assert ( bus->hub != NULL );
  1744. /* Open bus */
  1745. if ( ( rc = bus->host->open ( bus ) ) != 0 )
  1746. goto err_open;
  1747. /* Add to list of USB buses */
  1748. list_add_tail ( &bus->list, &usb_buses );
  1749. /* Register root hub */
  1750. if ( ( rc = register_usb_hub ( bus->hub ) ) != 0 )
  1751. goto err_register_hub;
  1752. /* Attach any devices already present */
  1753. usb_hotplug();
  1754. return 0;
  1755. unregister_usb_hub ( bus->hub );
  1756. err_register_hub:
  1757. list_del ( &bus->list );
  1758. bus->host->close ( bus );
  1759. err_open:
  1760. return rc;
  1761. }
  1762. /**
  1763. * Unregister USB bus
  1764. *
  1765. * @v bus USB bus
  1766. */
  1767. void unregister_usb_bus ( struct usb_bus *bus ) {
  1768. /* Sanity checks */
  1769. assert ( bus->hub != NULL );
  1770. /* Unregister root hub */
  1771. unregister_usb_hub ( bus->hub );
  1772. /* Remove from list of USB buses */
  1773. list_del ( &bus->list );
  1774. /* Close bus */
  1775. bus->host->close ( bus );
  1776. /* Sanity checks */
  1777. assert ( list_empty ( &bus->devices ) );
  1778. assert ( list_empty ( &bus->hubs ) );
  1779. }
  1780. /**
  1781. * Free USB bus
  1782. *
  1783. * @v bus USB bus
  1784. */
  1785. void free_usb_bus ( struct usb_bus *bus ) {
  1786. struct usb_endpoint *ep;
  1787. struct usb_port *port;
  1788. /* Sanity checks */
  1789. assert ( list_empty ( &bus->devices ) );
  1790. assert ( list_empty ( &bus->hubs ) );
  1791. list_for_each_entry ( ep, &usb_halted, halted )
  1792. assert ( ep->usb->port->hub->bus != bus );
  1793. list_for_each_entry ( port, &usb_changed, changed )
  1794. assert ( port->hub->bus != bus );
  1795. /* Free root hub */
  1796. free_usb_hub ( bus->hub );
  1797. /* Free bus */
  1798. free ( bus );
  1799. }
  1800. /**
  1801. * Find USB bus by device location
  1802. *
  1803. * @v bus_type Bus type
  1804. * @v location Bus location
  1805. * @ret bus USB bus, or NULL
  1806. */
  1807. struct usb_bus * find_usb_bus_by_location ( unsigned int bus_type,
  1808. unsigned int location ) {
  1809. struct usb_bus *bus;
  1810. for_each_usb_bus ( bus ) {
  1811. if ( ( bus->dev->desc.bus_type == bus_type ) &&
  1812. ( bus->dev->desc.location == location ) )
  1813. return bus;
  1814. }
  1815. return NULL;
  1816. }
  1817. /******************************************************************************
  1818. *
  1819. * USB address assignment
  1820. *
  1821. ******************************************************************************
  1822. */
  1823. /**
  1824. * Allocate device address
  1825. *
  1826. * @v bus USB bus
  1827. * @ret address Device address, or negative error
  1828. */
  1829. int usb_alloc_address ( struct usb_bus *bus ) {
  1830. unsigned int address;
  1831. /* Find first free device address */
  1832. address = ffsll ( ~bus->addresses );
  1833. if ( ! address )
  1834. return -ENOENT;
  1835. /* Mark address as used */
  1836. bus->addresses |= ( 1ULL << ( address - 1 ) );
  1837. return address;
  1838. }
  1839. /**
  1840. * Free device address
  1841. *
  1842. * @v bus USB bus
  1843. * @v address Device address
  1844. */
  1845. void usb_free_address ( struct usb_bus *bus, unsigned int address ) {
  1846. /* Sanity check */
  1847. assert ( address > 0 );
  1848. assert ( bus->addresses & ( 1ULL << ( address - 1 ) ) );
  1849. /* Mark address as free */
  1850. bus->addresses &= ~( 1ULL << ( address - 1 ) );
  1851. }
  1852. /******************************************************************************
  1853. *
  1854. * USB bus topology
  1855. *
  1856. ******************************************************************************
  1857. */
  1858. /**
  1859. * Get USB route string
  1860. *
  1861. * @v usb USB device
  1862. * @ret route USB route string
  1863. */
  1864. unsigned int usb_route_string ( struct usb_device *usb ) {
  1865. struct usb_device *parent;
  1866. unsigned int route;
  1867. /* Navigate up to root hub, constructing route string as we go */
  1868. for ( route = 0 ; ( parent = usb->port->hub->usb ) ; usb = parent ) {
  1869. route <<= 4;
  1870. route |= ( ( usb->port->address > 0xf ) ?
  1871. 0xf : usb->port->address );
  1872. }
  1873. return route;
  1874. }
  1875. /**
  1876. * Get USB depth
  1877. *
  1878. * @v usb USB device
  1879. * @ret depth Hub depth
  1880. */
  1881. unsigned int usb_depth ( struct usb_device *usb ) {
  1882. struct usb_device *parent;
  1883. unsigned int depth;
  1884. /* Navigate up to root hub, constructing depth as we go */
  1885. for ( depth = 0 ; ( parent = usb->port->hub->usb ) ; usb = parent )
  1886. depth++;
  1887. return depth;
  1888. }
  1889. /**
  1890. * Get USB root hub port
  1891. *
  1892. * @v usb USB device
  1893. * @ret port Root hub port
  1894. */
  1895. struct usb_port * usb_root_hub_port ( struct usb_device *usb ) {
  1896. struct usb_device *parent;
  1897. /* Navigate up to root hub */
  1898. while ( ( parent = usb->port->hub->usb ) )
  1899. usb = parent;
  1900. return usb->port;
  1901. }
  1902. /**
  1903. * Get USB transaction translator
  1904. *
  1905. * @v usb USB device
  1906. * @ret port Transaction translator port, or NULL
  1907. */
  1908. struct usb_port * usb_transaction_translator ( struct usb_device *usb ) {
  1909. struct usb_device *parent;
  1910. /* Navigate up to root hub. If we find a low-speed or
  1911. * full-speed device with a higher-speed parent hub, then that
  1912. * device's port is the transaction translator.
  1913. */
  1914. for ( ; ( parent = usb->port->hub->usb ) ; usb = parent ) {
  1915. if ( ( usb->speed <= USB_SPEED_FULL ) &&
  1916. ( parent->speed > USB_SPEED_FULL ) )
  1917. return usb->port;
  1918. }
  1919. return NULL;
  1920. }
  1921. /* Drag in objects via register_usb_bus() */
  1922. REQUIRING_SYMBOL ( register_usb_bus );
  1923. /* Drag in USB configuration */
  1924. REQUIRE_OBJECT ( config_usb );
  1925. /* Drag in hub driver */
  1926. REQUIRE_OBJECT ( usbhub );