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

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