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 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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. /* Delay to allow ports to stabilise on out-of-spec hubs */
  138. if ( hubdev->flags & USB_HUB_SLOW_START )
  139. mdelay ( USB_HUB_SLOW_START_DELAY_MS );
  140. return 0;
  141. usb_endpoint_close ( &hubdev->intr );
  142. err_open:
  143. err_power:
  144. return rc;
  145. }
  146. /**
  147. * Close hub
  148. *
  149. * @v hub USB hub
  150. */
  151. static void hub_close ( struct usb_hub *hub ) {
  152. struct usb_hub_device *hubdev = usb_hub_get_drvdata ( hub );
  153. /* Close interrupt endpoint */
  154. usb_endpoint_close ( &hubdev->intr );
  155. /* Stop refill process */
  156. process_del ( &hubdev->refill );
  157. }
  158. /**
  159. * Enable port
  160. *
  161. * @v hub USB hub
  162. * @v port USB port
  163. * @ret rc Return status code
  164. */
  165. static int hub_enable ( struct usb_hub *hub, struct usb_port *port ) {
  166. struct usb_hub_device *hubdev = usb_hub_get_drvdata ( hub );
  167. struct usb_device *usb = hubdev->usb;
  168. struct usb_hub_port_status status;
  169. unsigned int current;
  170. unsigned int i;
  171. int rc;
  172. /* Initiate reset if applicable */
  173. if ( ( hub->protocol < USB_PROTO_3_0 ) &&
  174. ( ( rc = usb_hub_set_port_feature ( usb, port->address,
  175. USB_HUB_PORT_RESET, 0 ) )!=0)){
  176. DBGC ( hubdev, "HUB %s port %d could not initiate reset: %s\n",
  177. hubdev->name, port->address, strerror ( rc ) );
  178. return rc;
  179. }
  180. /* Wait for port to become enabled */
  181. for ( i = 0 ; i < USB_HUB_ENABLE_MAX_WAIT_MS ; i++ ) {
  182. /* Check for port being enabled */
  183. if ( ( rc = usb_hub_get_port_status ( usb, port->address,
  184. &status ) ) != 0 ) {
  185. DBGC ( hubdev, "HUB %s port %d could not get status: "
  186. "%s\n", hubdev->name, port->address,
  187. strerror ( rc ) );
  188. return rc;
  189. }
  190. current = le16_to_cpu ( status.current );
  191. if ( current & ( 1 << USB_HUB_PORT_ENABLE ) )
  192. return 0;
  193. /* Delay */
  194. mdelay ( 1 );
  195. }
  196. DBGC ( hubdev, "HUB %s port %d timed out waiting for enable\n",
  197. hubdev->name, port->address );
  198. return -ETIMEDOUT;
  199. }
  200. /**
  201. * Disable port
  202. *
  203. * @v hub USB hub
  204. * @v port USB port
  205. * @ret rc Return status code
  206. */
  207. static int hub_disable ( struct usb_hub *hub, struct usb_port *port ) {
  208. struct usb_hub_device *hubdev = usb_hub_get_drvdata ( hub );
  209. struct usb_device *usb = hubdev->usb;
  210. int rc;
  211. /* Disable port */
  212. if ( ( rc = usb_hub_clear_port_feature ( usb, port->address,
  213. USB_HUB_PORT_ENABLE, 0 ) )!=0){
  214. DBGC ( hubdev, "HUB %s port %d could not disable: %s\n",
  215. hubdev->name, port->address, strerror ( rc ) );
  216. return rc;
  217. }
  218. return 0;
  219. }
  220. /**
  221. * Clear port status change bits
  222. *
  223. * @v hubdev USB hub device
  224. * @v port Port number
  225. * @v changed Port status change bits
  226. * @ret rc Return status code
  227. */
  228. static int hub_clear_changes ( struct usb_hub_device *hubdev,
  229. unsigned int port, uint16_t changed ) {
  230. struct usb_device *usb = hubdev->usb;
  231. unsigned int bit;
  232. unsigned int feature;
  233. int rc;
  234. /* Clear each set bit */
  235. for ( bit = 0 ; bit < 16 ; bit++ ) {
  236. /* Skip unset bits */
  237. if ( ! ( changed & ( 1 << bit ) ) )
  238. continue;
  239. /* Skip unused features */
  240. feature = USB_HUB_C_FEATURE ( bit );
  241. if ( ! ( hubdev->features & ( 1 << feature ) ) )
  242. continue;
  243. /* Clear bit */
  244. if ( ( rc = usb_hub_clear_port_feature ( usb, port,
  245. feature, 0 ) ) != 0 ) {
  246. DBGC ( hubdev, "HUB %s port %d could not clear feature "
  247. "%d: %s\n", hubdev->name, port, feature,
  248. strerror ( rc ) );
  249. return rc;
  250. }
  251. }
  252. return 0;
  253. }
  254. /**
  255. * Update port speed
  256. *
  257. * @v hub USB hub
  258. * @v port USB port
  259. * @ret rc Return status code
  260. */
  261. static int hub_speed ( struct usb_hub *hub, struct usb_port *port ) {
  262. struct usb_hub_device *hubdev = usb_hub_get_drvdata ( hub );
  263. struct usb_device *usb = hubdev->usb;
  264. struct usb_hub_port_status status;
  265. unsigned int current;
  266. unsigned int changed;
  267. int rc;
  268. /* Get port status */
  269. if ( ( rc = usb_hub_get_port_status ( usb, port->address,
  270. &status ) ) != 0 ) {
  271. DBGC ( hubdev, "HUB %s port %d could not get status: %s\n",
  272. hubdev->name, port->address, strerror ( rc ) );
  273. return rc;
  274. }
  275. current = le16_to_cpu ( status.current );
  276. changed = le16_to_cpu ( status.changed );
  277. DBGC2 ( hubdev, "HUB %s port %d status is %04x:%04x\n",
  278. hubdev->name, port->address, changed, current );
  279. /* Update port speed */
  280. if ( current & ( 1 << USB_HUB_PORT_CONNECTION ) ) {
  281. if ( hub->protocol >= USB_PROTO_3_0 ) {
  282. port->speed = USB_SPEED_SUPER;
  283. } else if ( current & ( 1 << USB_HUB_PORT_LOW_SPEED ) ) {
  284. port->speed = USB_SPEED_LOW;
  285. } else if ( current & ( 1 << USB_HUB_PORT_HIGH_SPEED ) ) {
  286. port->speed = USB_SPEED_HIGH;
  287. } else {
  288. port->speed = USB_SPEED_FULL;
  289. }
  290. } else {
  291. port->speed = USB_SPEED_NONE;
  292. }
  293. /* Record disconnections */
  294. port->disconnected |= ( changed & ( 1 << USB_HUB_PORT_CONNECTION ) );
  295. /* Clear port status change bits */
  296. if ( ( rc = hub_clear_changes ( hubdev, port->address, changed ) ) != 0)
  297. return rc;
  298. return 0;
  299. }
  300. /**
  301. * Clear transaction translator buffer
  302. *
  303. * @v hub USB hub
  304. * @v port USB port
  305. * @v ep USB endpoint
  306. * @ret rc Return status code
  307. */
  308. static int hub_clear_tt ( struct usb_hub *hub, struct usb_port *port,
  309. struct usb_endpoint *ep ) {
  310. struct usb_hub_device *hubdev = usb_hub_get_drvdata ( hub );
  311. struct usb_device *usb = hubdev->usb;
  312. int rc;
  313. /* Clear transaction translator buffer. All hubs must support
  314. * single-TT operation; we simplify our code by supporting
  315. * only this configuration.
  316. */
  317. if ( ( rc = usb_hub_clear_tt_buffer ( usb, ep->usb->address,
  318. ep->address, ep->attributes,
  319. USB_HUB_TT_SINGLE ) ) != 0 ) {
  320. DBGC ( hubdev, "HUB %s port %d could not clear TT buffer: %s\n",
  321. hubdev->name, port->address, strerror ( rc ) );
  322. return rc;
  323. }
  324. return 0;
  325. }
  326. /** USB hub operations */
  327. static struct usb_hub_driver_operations hub_operations = {
  328. .open = hub_open,
  329. .close = hub_close,
  330. .enable = hub_enable,
  331. .disable = hub_disable,
  332. .speed = hub_speed,
  333. .clear_tt = hub_clear_tt,
  334. };
  335. /**
  336. * Probe USB hub
  337. *
  338. * @v func USB function
  339. * @v config Configuration descriptor
  340. * @ret rc Return status code
  341. */
  342. static int hub_probe ( struct usb_function *func,
  343. struct usb_configuration_descriptor *config ) {
  344. struct usb_device *usb = func->usb;
  345. struct usb_bus *bus = usb->port->hub->bus;
  346. struct usb_hub_device *hubdev;
  347. struct usb_interface_descriptor *interface;
  348. union usb_hub_descriptor desc;
  349. unsigned int depth;
  350. unsigned int ports;
  351. int enhanced;
  352. int rc;
  353. /* Allocate and initialise structure */
  354. hubdev = zalloc ( sizeof ( *hubdev ) );
  355. if ( ! hubdev ) {
  356. rc = -ENOMEM;
  357. goto err_alloc;
  358. }
  359. enhanced = ( usb->port->protocol >= USB_PROTO_3_0 );
  360. hubdev->name = func->name;
  361. hubdev->usb = usb;
  362. hubdev->features =
  363. ( enhanced ? USB_HUB_FEATURES_ENHANCED : USB_HUB_FEATURES );
  364. hubdev->flags = func->id->driver_data;
  365. usb_endpoint_init ( &hubdev->intr, usb, &usb_hub_intr_operations );
  366. usb_refill_init ( &hubdev->intr, 0, 0, USB_HUB_INTR_FILL );
  367. process_init_stopped ( &hubdev->refill, &hub_refill_desc, NULL );
  368. /* Locate hub interface descriptor */
  369. interface = usb_interface_descriptor ( config, func->interface[0], 0 );
  370. if ( ! interface ) {
  371. DBGC ( hubdev, "HUB %s has no interface descriptor\n",
  372. hubdev->name );
  373. rc = -EINVAL;
  374. goto err_interface;
  375. }
  376. /* Locate interrupt endpoint descriptor */
  377. if ( ( rc = usb_endpoint_described ( &hubdev->intr, config, interface,
  378. USB_INTERRUPT_IN, 0 ) ) != 0 ) {
  379. DBGC ( hubdev, "HUB %s could not describe interrupt endpoint: "
  380. "%s\n", hubdev->name, strerror ( rc ) );
  381. goto err_endpoint;
  382. }
  383. /* Set hub depth */
  384. depth = usb_depth ( usb );
  385. if ( enhanced ) {
  386. if ( ( rc = usb_hub_set_hub_depth ( usb, depth ) ) != 0 ) {
  387. DBGC ( hubdev, "HUB %s could not set hub depth to %d: "
  388. "%s\n", hubdev->name, depth, strerror ( rc ) );
  389. goto err_set_hub_depth;
  390. }
  391. }
  392. /* Get hub descriptor */
  393. if ( ( rc = usb_hub_get_descriptor ( usb, enhanced, &desc ) ) != 0 ) {
  394. DBGC ( hubdev, "HUB %s could not get hub descriptor: %s\n",
  395. hubdev->name, strerror ( rc ) );
  396. goto err_hub_descriptor;
  397. }
  398. ports = desc.basic.ports;
  399. DBGC ( hubdev, "HUB %s has %d ports at depth %d%s\n", hubdev->name,
  400. ports, depth, ( enhanced ? " (enhanced)" : "" ) );
  401. /* Allocate hub */
  402. hubdev->hub = alloc_usb_hub ( bus, usb, ports, &hub_operations );
  403. if ( ! hubdev->hub ) {
  404. rc = -ENOMEM;
  405. goto err_alloc_hub;
  406. }
  407. usb_hub_set_drvdata ( hubdev->hub, hubdev );
  408. /* Register hub */
  409. if ( ( rc = register_usb_hub ( hubdev->hub ) ) != 0 ) {
  410. DBGC ( hubdev, "HUB %s could not register: %s\n",
  411. hubdev->name, strerror ( rc ) );
  412. goto err_register_hub;
  413. }
  414. usb_func_set_drvdata ( func, hubdev );
  415. return 0;
  416. unregister_usb_hub ( hubdev->hub );
  417. err_register_hub:
  418. free_usb_hub ( hubdev->hub );
  419. err_alloc_hub:
  420. err_hub_descriptor:
  421. err_set_hub_depth:
  422. err_endpoint:
  423. err_interface:
  424. free ( hubdev );
  425. err_alloc:
  426. return rc;
  427. }
  428. /**
  429. * Remove USB hub
  430. *
  431. * @v func USB function
  432. * @ret rc Return status code
  433. */
  434. static void hub_remove ( struct usb_function *func ) {
  435. struct usb_hub_device *hubdev = usb_func_get_drvdata ( func );
  436. struct usb_hub *hub = hubdev->hub;
  437. struct usb_device *usb = hubdev->usb;
  438. struct usb_port *port;
  439. unsigned int i;
  440. /* If hub has been unplugged, mark all ports as unplugged */
  441. if ( usb->port->disconnected ) {
  442. for ( i = 1 ; i <= hub->ports ; i++ ) {
  443. port = usb_port ( hub, i );
  444. port->disconnected = 1;
  445. port->speed = USB_SPEED_NONE;
  446. }
  447. }
  448. /* Unregister hub */
  449. unregister_usb_hub ( hubdev->hub );
  450. assert ( ! process_running ( &hubdev->refill ) );
  451. /* Free hub */
  452. free_usb_hub ( hubdev->hub );
  453. /* Free hub device */
  454. free ( hubdev );
  455. }
  456. /** USB hub device IDs */
  457. static struct usb_device_id hub_ids[] = {
  458. {
  459. .name = "avocent-hub",
  460. .vendor = 0x0624,
  461. .product = 0x0248,
  462. .driver_data = USB_HUB_SLOW_START,
  463. },
  464. {
  465. .name = "hub",
  466. .vendor = USB_ANY_ID,
  467. .product = USB_ANY_ID,
  468. },
  469. };
  470. /** USB hub driver */
  471. struct usb_driver usb_hub_driver __usb_driver = {
  472. .ids = hub_ids,
  473. .id_count = ( sizeof ( hub_ids ) / sizeof ( hub_ids[0] ) ),
  474. .class = USB_CLASS_ID ( USB_CLASS_HUB, 0, USB_ANY_ID ),
  475. .score = USB_SCORE_NORMAL,
  476. .probe = hub_probe,
  477. .remove = hub_remove,
  478. };