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

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