Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

usb.c 40KB

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