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

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