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.

usbhub.c 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /*
  2. * Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <unistd.h>
  27. #include <errno.h>
  28. #include <assert.h>
  29. #include <byteswap.h>
  30. #include <ipxe/usb.h>
  31. #include "usbhub.h"
  32. /** @file
  33. *
  34. * USB hub driver
  35. *
  36. */
  37. /**
  38. * Refill interrupt ring
  39. *
  40. * @v hubdev Hub device
  41. */
  42. static void hub_refill ( struct usb_hub_device *hubdev ) {
  43. int rc;
  44. /* Refill interrupt endpoint */
  45. if ( ( rc = usb_refill ( &hubdev->intr ) ) != 0 ) {
  46. DBGC ( hubdev, "HUB %s could not refill interrupt: %s\n",
  47. hubdev->name, strerror ( rc ) );
  48. /* Continue attempting to refill */
  49. return;
  50. }
  51. /* Stop refill process */
  52. process_del ( &hubdev->refill );
  53. }
  54. /** Refill process descriptor */
  55. static struct process_descriptor hub_refill_desc =
  56. PROC_DESC ( struct usb_hub_device, refill, hub_refill );
  57. /**
  58. * Complete interrupt transfer
  59. *
  60. * @v ep USB endpoint
  61. * @v iobuf I/O buffer
  62. * @v rc Completion status code
  63. */
  64. static void hub_complete ( struct usb_endpoint *ep,
  65. struct io_buffer *iobuf, int rc ) {
  66. struct usb_hub_device *hubdev =
  67. container_of ( ep, struct usb_hub_device, intr );
  68. struct usb_hub *hub = hubdev->hub;
  69. uint8_t *data = iobuf->data;
  70. unsigned int bits = ( 8 * iob_len ( iobuf ) );
  71. unsigned int i;
  72. /* Ignore packets cancelled when the endpoint closes */
  73. if ( ! ep->open )
  74. goto done;
  75. /* Ignore packets with errors */
  76. if ( rc != 0 ) {
  77. DBGC ( hubdev, "HUB %s interrupt failed: %s\n",
  78. hubdev->name, strerror ( rc ) );
  79. DBGC_HDA ( hubdev, 0, iobuf->data, iob_len ( iobuf ) );
  80. goto done;
  81. }
  82. /* Report any port status changes */
  83. for ( i = 1 ; i <= hub->ports ; i++ ) {
  84. /* Sanity check */
  85. if ( i > bits ) {
  86. DBGC ( hubdev, "HUB %s underlength interrupt:\n",
  87. hubdev->name );
  88. DBGC_HDA ( hubdev, 0, iobuf->data, iob_len ( iobuf ) );
  89. goto done;
  90. }
  91. /* Report port status change if applicable */
  92. if ( data[ i / 8 ] & ( 1 << ( i % 8 ) ) ) {
  93. DBGC2 ( hubdev, "HUB %s port %d status changed\n",
  94. hubdev->name, i );
  95. usb_port_changed ( usb_port ( hub, i ) );
  96. }
  97. }
  98. done:
  99. /* Start refill process */
  100. process_add ( &hubdev->refill );
  101. }
  102. /** Interrupt endpoint operations */
  103. static struct usb_endpoint_driver_operations usb_hub_intr_operations = {
  104. .complete = hub_complete,
  105. };
  106. /**
  107. * Open hub
  108. *
  109. * @v hub USB hub
  110. * @ret rc Return status code
  111. */
  112. static int hub_open ( struct usb_hub *hub ) {
  113. struct usb_hub_device *hubdev = usb_hub_get_drvdata ( hub );
  114. struct usb_device *usb = hubdev->usb;
  115. unsigned int i;
  116. int rc;
  117. /* Ensure ports are powered */
  118. for ( i = 1 ; i <= hub->ports ; i++ ) {
  119. if ( ( rc = usb_hub_set_port_feature ( usb, i,
  120. USB_HUB_PORT_POWER,
  121. 0 ) ) != 0 ) {
  122. DBGC ( hubdev, "HUB %s port %d could not apply power: "
  123. "%s\n", hubdev->name, i, strerror ( rc ) );
  124. goto err_power;
  125. }
  126. }
  127. /* Open interrupt endpoint */
  128. if ( ( rc = usb_endpoint_open ( &hubdev->intr ) ) != 0 ) {
  129. DBGC ( hubdev, "HUB %s could not register interrupt: %s\n",
  130. hubdev->name, strerror ( rc ) );
  131. goto err_open;
  132. }
  133. /* Start refill process */
  134. process_add ( &hubdev->refill );
  135. /* Refill interrupt ring */
  136. hub_refill ( hubdev );
  137. return 0;
  138. usb_endpoint_close ( &hubdev->intr );
  139. err_open:
  140. err_power:
  141. return rc;
  142. }
  143. /**
  144. * Close hub
  145. *
  146. * @v hub USB hub
  147. */
  148. static void hub_close ( struct usb_hub *hub ) {
  149. struct usb_hub_device *hubdev = usb_hub_get_drvdata ( hub );
  150. /* Close interrupt endpoint */
  151. usb_endpoint_close ( &hubdev->intr );
  152. /* Stop refill process */
  153. process_del ( &hubdev->refill );
  154. }
  155. /**
  156. * Enable port
  157. *
  158. * @v hub USB hub
  159. * @v port USB port
  160. * @ret rc Return status code
  161. */
  162. static int hub_enable ( struct usb_hub *hub, struct usb_port *port ) {
  163. struct usb_hub_device *hubdev = usb_hub_get_drvdata ( hub );
  164. struct usb_device *usb = hubdev->usb;
  165. struct usb_hub_port_status status;
  166. unsigned int current;
  167. unsigned int i;
  168. int rc;
  169. /* Initiate reset if applicable */
  170. if ( ( hub->protocol < USB_PROTO_3_0 ) &&
  171. ( ( rc = usb_hub_set_port_feature ( usb, port->address,
  172. USB_HUB_PORT_RESET, 0 ) )!=0)){
  173. DBGC ( hubdev, "HUB %s port %d could not initiate reset: %s\n",
  174. hubdev->name, port->address, strerror ( rc ) );
  175. return rc;
  176. }
  177. /* Wait for port to become enabled */
  178. for ( i = 0 ; i < USB_HUB_ENABLE_MAX_WAIT_MS ; i++ ) {
  179. /* Check for port being enabled */
  180. if ( ( rc = usb_hub_get_port_status ( usb, port->address,
  181. &status ) ) != 0 ) {
  182. DBGC ( hubdev, "HUB %s port %d could not get status: "
  183. "%s\n", hubdev->name, port->address,
  184. strerror ( rc ) );
  185. return rc;
  186. }
  187. current = le16_to_cpu ( status.current );
  188. if ( current & ( 1 << USB_HUB_PORT_ENABLE ) )
  189. return 0;
  190. /* Delay */
  191. mdelay ( 1 );
  192. }
  193. DBGC ( hubdev, "HUB %s port %d timed out waiting for enable\n",
  194. hubdev->name, port->address );
  195. return -ETIMEDOUT;
  196. }
  197. /**
  198. * Disable port
  199. *
  200. * @v hub USB hub
  201. * @v port USB port
  202. * @ret rc Return status code
  203. */
  204. static int hub_disable ( struct usb_hub *hub, struct usb_port *port ) {
  205. struct usb_hub_device *hubdev = usb_hub_get_drvdata ( hub );
  206. struct usb_device *usb = hubdev->usb;
  207. int rc;
  208. /* Disable port */
  209. if ( ( rc = usb_hub_clear_port_feature ( usb, port->address,
  210. USB_HUB_PORT_ENABLE, 0 ) )!=0){
  211. DBGC ( hubdev, "HUB %s port %d could not disable: %s\n",
  212. hubdev->name, port->address, strerror ( rc ) );
  213. return rc;
  214. }
  215. return 0;
  216. }
  217. /**
  218. * Clear port status change bits
  219. *
  220. * @v hubdev USB hub device
  221. * @v port Port number
  222. * @v changed Port status change bits
  223. * @ret rc Return status code
  224. */
  225. static int hub_clear_changes ( struct usb_hub_device *hubdev,
  226. unsigned int port, uint16_t changed ) {
  227. struct usb_device *usb = hubdev->usb;
  228. unsigned int bit;
  229. unsigned int feature;
  230. int rc;
  231. /* Clear each set bit */
  232. for ( bit = 0 ; bit < 16 ; bit++ ) {
  233. /* Skip unset bits */
  234. if ( ! ( changed & ( 1 << bit ) ) )
  235. continue;
  236. /* Skip unused features */
  237. feature = USB_HUB_C_FEATURE ( bit );
  238. if ( ! ( hubdev->features & ( 1 << feature ) ) )
  239. continue;
  240. /* Clear bit */
  241. if ( ( rc = usb_hub_clear_port_feature ( usb, port,
  242. feature, 0 ) ) != 0 ) {
  243. DBGC ( hubdev, "HUB %s port %d could not clear feature "
  244. "%d: %s\n", hubdev->name, port, feature,
  245. strerror ( rc ) );
  246. return rc;
  247. }
  248. }
  249. return 0;
  250. }
  251. /**
  252. * Update port speed
  253. *
  254. * @v hub USB hub
  255. * @v port USB port
  256. * @ret rc Return status code
  257. */
  258. static int hub_speed ( struct usb_hub *hub, struct usb_port *port ) {
  259. struct usb_hub_device *hubdev = usb_hub_get_drvdata ( hub );
  260. struct usb_device *usb = hubdev->usb;
  261. struct usb_hub_port_status status;
  262. unsigned int current;
  263. unsigned int changed;
  264. int rc;
  265. /* Get port status */
  266. if ( ( rc = usb_hub_get_port_status ( usb, port->address,
  267. &status ) ) != 0 ) {
  268. DBGC ( hubdev, "HUB %s port %d could not get status: %s\n",
  269. hubdev->name, port->address, strerror ( rc ) );
  270. return rc;
  271. }
  272. current = le16_to_cpu ( status.current );
  273. changed = le16_to_cpu ( status.changed );
  274. DBGC2 ( hubdev, "HUB %s port %d status is %04x:%04x\n",
  275. hubdev->name, port->address, changed, current );
  276. /* Update port speed */
  277. if ( current & ( 1 << USB_HUB_PORT_CONNECTION ) ) {
  278. if ( hub->protocol >= USB_PROTO_3_0 ) {
  279. port->speed = USB_SPEED_SUPER;
  280. } else if ( current & ( 1 << USB_HUB_PORT_LOW_SPEED ) ) {
  281. port->speed = USB_SPEED_LOW;
  282. } else if ( current & ( 1 << USB_HUB_PORT_HIGH_SPEED ) ) {
  283. port->speed = USB_SPEED_HIGH;
  284. } else {
  285. port->speed = USB_SPEED_FULL;
  286. }
  287. } else {
  288. port->speed = USB_SPEED_NONE;
  289. }
  290. /* Clear port status change bits */
  291. if ( ( rc = hub_clear_changes ( hubdev, port->address, changed ) ) != 0)
  292. return rc;
  293. return 0;
  294. }
  295. /** USB hub operations */
  296. static struct usb_hub_driver_operations hub_operations = {
  297. .open = hub_open,
  298. .close = hub_close,
  299. .enable = hub_enable,
  300. .disable = hub_disable,
  301. .speed = hub_speed,
  302. };
  303. /**
  304. * Probe USB hub
  305. *
  306. * @v func USB function
  307. * @v config Configuration descriptor
  308. * @ret rc Return status code
  309. */
  310. static int hub_probe ( struct usb_function *func,
  311. struct usb_configuration_descriptor *config ) {
  312. struct usb_device *usb = func->usb;
  313. struct usb_bus *bus = usb->port->hub->bus;
  314. struct usb_hub_device *hubdev;
  315. struct usb_interface_descriptor *interface;
  316. union usb_hub_descriptor desc;
  317. unsigned int depth;
  318. unsigned int ports;
  319. int enhanced;
  320. int rc;
  321. /* Allocate and initialise structure */
  322. hubdev = zalloc ( sizeof ( *hubdev ) );
  323. if ( ! hubdev ) {
  324. rc = -ENOMEM;
  325. goto err_alloc;
  326. }
  327. enhanced = ( usb->port->protocol >= USB_PROTO_3_0 );
  328. hubdev->name = func->name;
  329. hubdev->usb = usb;
  330. hubdev->features =
  331. ( enhanced ? USB_HUB_FEATURES_ENHANCED : USB_HUB_FEATURES );
  332. usb_endpoint_init ( &hubdev->intr, usb, &usb_hub_intr_operations );
  333. usb_refill_init ( &hubdev->intr, 0, USB_HUB_INTR_FILL );
  334. process_init_stopped ( &hubdev->refill, &hub_refill_desc, NULL );
  335. /* Locate hub interface descriptor */
  336. interface = usb_interface_descriptor ( config, func->interface[0], 0 );
  337. if ( ! interface ) {
  338. DBGC ( hubdev, "HUB %s has no interface descriptor\n",
  339. hubdev->name );
  340. rc = -EINVAL;
  341. goto err_interface;
  342. }
  343. /* Locate interrupt endpoint descriptor */
  344. if ( ( rc = usb_endpoint_described ( &hubdev->intr, config, interface,
  345. USB_INTERRUPT, 0 ) ) != 0 ) {
  346. DBGC ( hubdev, "HUB %s could not describe interrupt endpoint: "
  347. "%s\n", hubdev->name, strerror ( rc ) );
  348. goto err_endpoint;
  349. }
  350. /* Set hub depth */
  351. depth = usb_depth ( usb );
  352. if ( enhanced ) {
  353. if ( ( rc = usb_hub_set_hub_depth ( usb, depth ) ) != 0 ) {
  354. DBGC ( hubdev, "HUB %s could not set hub depth to %d: "
  355. "%s\n", hubdev->name, depth, strerror ( rc ) );
  356. goto err_set_hub_depth;
  357. }
  358. }
  359. /* Get hub descriptor */
  360. if ( ( rc = usb_hub_get_descriptor ( usb, enhanced, &desc ) ) != 0 ) {
  361. DBGC ( hubdev, "HUB %s could not get hub descriptor: %s\n",
  362. hubdev->name, strerror ( rc ) );
  363. goto err_hub_descriptor;
  364. }
  365. ports = desc.basic.ports;
  366. DBGC ( hubdev, "HUB %s has %d ports at depth %d%s\n", hubdev->name,
  367. ports, depth, ( enhanced ? " (enhanced)" : "" ) );
  368. /* Allocate hub */
  369. hubdev->hub = alloc_usb_hub ( bus, usb, ports, &hub_operations );
  370. if ( ! hubdev->hub ) {
  371. rc = -ENOMEM;
  372. goto err_alloc_hub;
  373. }
  374. usb_hub_set_drvdata ( hubdev->hub, hubdev );
  375. /* Register hub */
  376. if ( ( rc = register_usb_hub ( hubdev->hub ) ) != 0 ) {
  377. DBGC ( hubdev, "HUB %s could not register: %s\n",
  378. hubdev->name, strerror ( rc ) );
  379. goto err_register_hub;
  380. }
  381. usb_func_set_drvdata ( func, hubdev );
  382. return 0;
  383. unregister_usb_hub ( hubdev->hub );
  384. err_register_hub:
  385. free_usb_hub ( hubdev->hub );
  386. err_alloc_hub:
  387. err_hub_descriptor:
  388. err_set_hub_depth:
  389. err_endpoint:
  390. err_interface:
  391. free ( hubdev );
  392. err_alloc:
  393. return rc;
  394. }
  395. /**
  396. * Remove USB hub
  397. *
  398. * @v func USB function
  399. * @ret rc Return status code
  400. */
  401. static void hub_remove ( struct usb_function *func ) {
  402. struct usb_hub_device *hubdev = usb_func_get_drvdata ( func );
  403. struct usb_hub *hub = hubdev->hub;
  404. struct usb_device *usb = hubdev->usb;
  405. struct usb_port *port;
  406. unsigned int i;
  407. /* If hub has been unplugged, mark all ports as unplugged */
  408. if ( usb->port->speed == USB_SPEED_NONE ) {
  409. for ( i = 1 ; i <= hub->ports ; i++ ) {
  410. port = usb_port ( hub, i );
  411. port->speed = USB_SPEED_NONE;
  412. }
  413. }
  414. /* Unregister hub */
  415. unregister_usb_hub ( hubdev->hub );
  416. assert ( ! process_running ( &hubdev->refill ) );
  417. /* Free hub */
  418. free_usb_hub ( hubdev->hub );
  419. /* Free hub device */
  420. free ( hubdev );
  421. }
  422. /** USB hub device IDs */
  423. static struct usb_device_id hub_ids[] = {
  424. {
  425. .name = "hub-1",
  426. .vendor = USB_ANY_ID,
  427. .product = USB_ANY_ID,
  428. .class = {
  429. .class = USB_CLASS_HUB,
  430. .subclass = 0,
  431. .protocol = 0,
  432. },
  433. },
  434. {
  435. .name = "hub-2",
  436. .vendor = USB_ANY_ID,
  437. .product = USB_ANY_ID,
  438. .class = {
  439. .class = USB_CLASS_HUB,
  440. .subclass = 0,
  441. .protocol = 1,
  442. },
  443. },
  444. };
  445. /** USB hub driver */
  446. struct usb_driver usb_hub_driver __usb_driver = {
  447. .ids = hub_ids,
  448. .id_count = ( sizeof ( hub_ids ) / sizeof ( hub_ids[0] ) ),
  449. .probe = hub_probe,
  450. .remove = hub_remove,
  451. };