You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

usb.c 50KB

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