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

usb.c 43KB

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