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

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