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

usb.c 46KB

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