您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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