Firmware for MNT ZZ9000 graphics and ARM coprocessor card for Amiga computers.
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_hub.c 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Most of this source has been derived from the Linux USB
  4. * project:
  5. * (C) Copyright Linus Torvalds 1999
  6. * (C) Copyright Johannes Erdfelt 1999-2001
  7. * (C) Copyright Andreas Gal 1999
  8. * (C) Copyright Gregory P. Smith 1999
  9. * (C) Copyright Deti Fliegl 1999 (new USB architecture)
  10. * (C) Copyright Randy Dunlap 2000
  11. * (C) Copyright David Brownell 2000 (kernel hotplug, usb_device_id)
  12. * (C) Copyright Yggdrasil Computing, Inc. 2000
  13. * (usb_device_id matching changes by Adam J. Richter)
  14. *
  15. * Adapted for U-Boot:
  16. * (C) Copyright 2001 Denis Peter, MPL AG Switzerland
  17. */
  18. /****************************************************************************
  19. * HUB "Driver"
  20. * Probes device for being a hub and configurate it
  21. */
  22. //#include <common.h>
  23. //#include <command.h>
  24. #include <errno.h>
  25. #include "memalign.h"
  26. //#include <asm/processor.h>
  27. //#include <asm/unaligned.h>
  28. //#include <linux/ctype.h>
  29. #include "list.h"
  30. #include <asm/byteorder.h>
  31. //#include <asm/unaligned.h>
  32. #include <stdio.h>
  33. #include <string.h>
  34. #include "usb.h"
  35. #define max(x, y) ({ \
  36. typeof(x) _max1 = (x); \
  37. typeof(y) _max2 = (y); \
  38. (void) (&_max1 == &_max2); \
  39. _max1 > _max2 ? _max1 : _max2; })
  40. #define USB_BUFSIZ 512
  41. #define HUB_SHORT_RESET_TIME 20
  42. #define HUB_LONG_RESET_TIME 200
  43. #define PORT_OVERCURRENT_MAX_SCAN_COUNT 3
  44. struct usb_device_scan {
  45. struct usb_device *dev; /* USB hub device to scan */
  46. struct usb_hub_device *hub; /* USB hub struct */
  47. int port; /* USB port to scan */
  48. struct list_head list;
  49. };
  50. static LIST_HEAD(usb_scan_list);
  51. void usb_hub_reset_devices(struct usb_hub_device *hub, int port)
  52. {
  53. return;
  54. }
  55. static inline bool usb_hub_is_superspeed(struct usb_device *hdev)
  56. {
  57. return hdev->descriptor.bDeviceProtocol == 3;
  58. }
  59. static int usb_get_hub_descriptor(struct usb_device *dev, void *data, int size)
  60. {
  61. unsigned short dtype = USB_DT_HUB;
  62. if (usb_hub_is_superspeed(dev))
  63. dtype = USB_DT_SS_HUB;
  64. return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  65. USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
  66. dtype << 8, 0, data, size, USB_CNTL_TIMEOUT);
  67. }
  68. static int usb_clear_port_feature(struct usb_device *dev, int port, int feature)
  69. {
  70. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  71. USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature,
  72. port, NULL, 0, USB_CNTL_TIMEOUT);
  73. }
  74. static int usb_set_port_feature(struct usb_device *dev, int port, int feature)
  75. {
  76. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  77. USB_REQ_SET_FEATURE, USB_RT_PORT, feature,
  78. port, NULL, 0, USB_CNTL_TIMEOUT);
  79. }
  80. static int usb_get_hub_status(struct usb_device *dev, void *data)
  81. {
  82. return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  83. USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
  84. data, sizeof(struct usb_hub_status), USB_CNTL_TIMEOUT);
  85. }
  86. int usb_get_port_status(struct usb_device *dev, int port, void *data)
  87. {
  88. int ret;
  89. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  90. USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port,
  91. data, sizeof(struct usb_port_status), USB_CNTL_TIMEOUT);
  92. return ret;
  93. }
  94. static void usb_hub_power_on(struct usb_hub_device *hub)
  95. {
  96. int i;
  97. struct usb_device *dev;
  98. unsigned pgood_delay = hub->desc.bPwrOn2PwrGood * 2;
  99. dev = hub->pusb_dev;
  100. printf("[usb-hub] enabling power on all ports\n");
  101. for (i = 0; i < dev->maxchild; i++) {
  102. usb_set_port_feature(dev, i + 1, USB_PORT_FEAT_POWER);
  103. //printf("port %d returns %lX\n", i + 1, dev->status);
  104. }
  105. /*
  106. * Wait for power to become stable,
  107. * plus spec-defined max time for device to connect
  108. * but allow this time to be increased via env variable as some
  109. * devices break the spec and require longer warm-up times
  110. */
  111. //env = env_get("usb_pgood_delay");
  112. //if (env)
  113. // pgood_delay = max(pgood_delay,
  114. // (unsigned)simple_strtol(env, NULL, 0));
  115. //printf("pgood_delay=%dms\n", pgood_delay);
  116. /*
  117. * Do a minimum delay of the larger value of 100ms or pgood_delay
  118. * so that the power can stablize before the devices are queried
  119. */
  120. hub->query_delay = get_timer(0) + max(100, (int)pgood_delay);
  121. /*
  122. * Record the power-on timeout here. The max. delay (timeout)
  123. * will be done based on this value in the USB port loop in
  124. * usb_hub_configure() later.
  125. */
  126. hub->connect_timeout = hub->query_delay + 1000;
  127. //printf("devnum=%d poweron: query_delay=%d connect_timeout=%d\n",
  128. // dev->devnum, max(100, (int)pgood_delay),
  129. // max(100, (int)pgood_delay) + 1000);
  130. }
  131. static struct usb_hub_device hub_dev[USB_MAX_HUB];
  132. static int usb_hub_index;
  133. void usb_hub_reset(void)
  134. {
  135. usb_hub_index = 0;
  136. /* Zero out global hub_dev in case its re-used again */
  137. memset(hub_dev, 0, sizeof(hub_dev));
  138. }
  139. static struct usb_hub_device *usb_hub_allocate(void)
  140. {
  141. if (usb_hub_index < USB_MAX_HUB)
  142. return &hub_dev[usb_hub_index++];
  143. printf("ERROR: USB_MAX_HUB (%d) reached\n", USB_MAX_HUB);
  144. return NULL;
  145. }
  146. #define MAX_TRIES 5
  147. static inline char *portspeed(int portstatus)
  148. {
  149. char *speed_str;
  150. switch (portstatus & USB_PORT_STAT_SPEED_MASK) {
  151. case USB_PORT_STAT_SUPER_SPEED:
  152. speed_str = "5 Gb/s";
  153. break;
  154. case USB_PORT_STAT_HIGH_SPEED:
  155. speed_str = "480 Mb/s";
  156. break;
  157. case USB_PORT_STAT_LOW_SPEED:
  158. speed_str = "1.5 Mb/s";
  159. break;
  160. default:
  161. speed_str = "12 Mb/s";
  162. break;
  163. }
  164. return speed_str;
  165. }
  166. /**
  167. * usb_hub_port_reset() - reset a port given its usb_device pointer
  168. *
  169. * Reset a hub port and see if a device is present on that port, providing
  170. * sufficient time for it to show itself. The port status is returned.
  171. *
  172. * @dev: USB device to reset
  173. * @port: Port number to reset (note ports are numbered from 0 here)
  174. * @portstat: Returns port status
  175. */
  176. static int usb_hub_port_reset(struct usb_device *dev, int port,
  177. unsigned short *portstat)
  178. {
  179. int err, tries;
  180. ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
  181. unsigned short portstatus, portchange;
  182. int delay = HUB_SHORT_RESET_TIME; /* start with short reset delay */
  183. printf("[usb-hub] %s: resetting port %d...\n", __func__, port + 1);
  184. for (tries = 0; tries < MAX_TRIES; tries++) {
  185. err = usb_set_port_feature(dev, port + 1, USB_PORT_FEAT_RESET);
  186. if (err < 0)
  187. return err;
  188. mdelay(delay);
  189. if (usb_get_port_status(dev, port + 1, portsts) < 0) {
  190. printf("[usb-hub] get_port_status failed status %lX\n",
  191. dev->status);
  192. return -1;
  193. }
  194. portstatus = le16_to_cpu(portsts->wPortStatus);
  195. portchange = le16_to_cpu(portsts->wPortChange);
  196. printf("[usb-hub] portstatus %x, change %x, %s\n", portstatus, portchange,
  197. portspeed(portstatus));
  198. /*printf("STAT_C_CONNECTION = %d STAT_CONNECTION = %d" \
  199. " USB_PORT_STAT_ENABLE %d\n",
  200. (portchange & USB_PORT_STAT_C_CONNECTION) ? 1 : 0,
  201. (portstatus & USB_PORT_STAT_CONNECTION) ? 1 : 0,
  202. (portstatus & USB_PORT_STAT_ENABLE) ? 1 : 0);*/
  203. /*
  204. * Perhaps we should check for the following here:
  205. * - C_CONNECTION hasn't been set.
  206. * - CONNECTION is still set.
  207. *
  208. * Doing so would ensure that the device is still connected
  209. * to the bus, and hasn't been unplugged or replaced while the
  210. * USB bus reset was going on.
  211. *
  212. * However, if we do that, then (at least) a San Disk Ultra
  213. * USB 3.0 16GB device fails to reset on (at least) an NVIDIA
  214. * Tegra Jetson TK1 board. For some reason, the device appears
  215. * to briefly drop off the bus when this second bus reset is
  216. * executed, yet if we retry this loop, it'll eventually come
  217. * back after another reset or two.
  218. */
  219. if (portstatus & USB_PORT_STAT_ENABLE)
  220. break;
  221. /* Switch to long reset delay for the next round */
  222. delay = HUB_LONG_RESET_TIME;
  223. }
  224. if (tries == MAX_TRIES) {
  225. printf("Cannot enable port %i after %i retries, " \
  226. "disabling port.\n", port + 1, MAX_TRIES);
  227. printf("Maybe the USB cable is bad?\n");
  228. return -1;
  229. }
  230. usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_RESET);
  231. *portstat = portstatus;
  232. return 0;
  233. }
  234. int usb_hub_port_connect_change(struct usb_device *dev, int port)
  235. {
  236. ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
  237. unsigned short portstatus;
  238. int ret, speed;
  239. /* Check status */
  240. ret = usb_get_port_status(dev, port + 1, portsts);
  241. if (ret < 0) {
  242. printf("get_port_status failed\n");
  243. return ret;
  244. }
  245. portstatus = le16_to_cpu(portsts->wPortStatus);
  246. printf("[usb-hub] portstatus %x, change %x, %s\n",
  247. portstatus,
  248. le16_to_cpu(portsts->wPortChange),
  249. portspeed(portstatus));
  250. /* Clear the connection change status */
  251. usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_CONNECTION);
  252. /* Disconnect any existing devices under this port */
  253. if (((!(portstatus & USB_PORT_STAT_CONNECTION)) &&
  254. (!(portstatus & USB_PORT_STAT_ENABLE))) ||
  255. usb_device_has_child_on_port(dev, port)) {
  256. printf("usb_disconnect(&hub->children[port]);\n");
  257. /* Return now if nothing is connected */
  258. if (!(portstatus & USB_PORT_STAT_CONNECTION))
  259. return -ENOTCONN;
  260. }
  261. /* Reset the port */
  262. ret = usb_hub_port_reset(dev, port, &portstatus);
  263. if (ret < 0) {
  264. if (ret != -ENXIO)
  265. printf("cannot reset port %i!?\n", port + 1);
  266. return ret;
  267. }
  268. switch (portstatus & USB_PORT_STAT_SPEED_MASK) {
  269. case USB_PORT_STAT_SUPER_SPEED:
  270. speed = USB_SPEED_SUPER;
  271. break;
  272. case USB_PORT_STAT_HIGH_SPEED:
  273. speed = USB_SPEED_HIGH;
  274. break;
  275. case USB_PORT_STAT_LOW_SPEED:
  276. speed = USB_SPEED_LOW;
  277. break;
  278. default:
  279. speed = USB_SPEED_FULL;
  280. break;
  281. }
  282. struct usb_device *usb;
  283. ret = usb_alloc_new_device(dev->controller, &usb);
  284. if (ret) {
  285. printf("cannot create new device: ret=%d", ret);
  286. return ret;
  287. }
  288. dev->children[port] = usb;
  289. usb->speed = speed;
  290. usb->parent = dev;
  291. usb->portnr = port + 1;
  292. /* Run it through the hoops (find a driver, etc) */
  293. ret = usb_new_device(usb);
  294. if (ret < 0) {
  295. /* Woops, disable the port */
  296. usb_free_device(dev->controller);
  297. dev->children[port] = NULL;
  298. }
  299. if (ret < 0) {
  300. printf("hub: disabling port %d\n", port + 1);
  301. usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_ENABLE);
  302. }
  303. return ret;
  304. }
  305. static int usb_scan_port(struct usb_device_scan *usb_scan)
  306. {
  307. ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
  308. unsigned short portstatus;
  309. unsigned short portchange;
  310. struct usb_device *dev;
  311. struct usb_hub_device *hub;
  312. int ret = 0;
  313. int i;
  314. dev = usb_scan->dev;
  315. hub = usb_scan->hub;
  316. i = usb_scan->port;
  317. /*
  318. * Don't talk to the device before the query delay is expired.
  319. * This is needed for voltages to stabalize.
  320. */
  321. if (get_timer(0) < hub->query_delay)
  322. return 0;
  323. ret = usb_get_port_status(dev, i + 1, portsts);
  324. if (ret < 0) {
  325. printf("get_port_status failed\n");
  326. if (get_timer(0) >= hub->connect_timeout) {
  327. printf("[usb-hub] devnum=%d port=%d: timeout\n",
  328. dev->devnum, i + 1);
  329. /* Remove this device from scanning list */
  330. list_del(&usb_scan->list);
  331. free(usb_scan); // FIXME dynamic allocation
  332. return 0;
  333. }
  334. return 0;
  335. }
  336. portstatus = le16_to_cpu(portsts->wPortStatus);
  337. portchange = le16_to_cpu(portsts->wPortChange);
  338. printf("[usb-hub] Port %d Status %X Change %X\n", i + 1, portstatus, portchange);
  339. /*
  340. * No connection change happened, wait a bit more.
  341. *
  342. * For some situation, the hub reports no connection change but a
  343. * device is connected to the port (eg: CCS bit is set but CSC is not
  344. * in the PORTSC register of a root hub), ignore such case.
  345. */
  346. if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
  347. !(portstatus & USB_PORT_STAT_CONNECTION)) {
  348. if (get_timer(0) >= hub->connect_timeout) {
  349. printf("[usb-hub] devnum=%d port=%d: timeout\n",
  350. dev->devnum, i + 1);
  351. /* Remove this device from scanning list */
  352. list_del(&usb_scan->list);
  353. free(usb_scan); // FIXME dynamic allocation
  354. return 0;
  355. }
  356. return 0;
  357. }
  358. if (portchange & USB_PORT_STAT_C_RESET) {
  359. printf("[usb-hub] port %d reset change\n", i + 1);
  360. usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_C_RESET);
  361. }
  362. if ((portchange & USB_SS_PORT_STAT_C_BH_RESET) &&
  363. usb_hub_is_superspeed(dev)) {
  364. printf("[usb-hub] port %d BH reset change\n", i + 1);
  365. usb_clear_port_feature(dev, i + 1, USB_SS_PORT_FEAT_C_BH_RESET);
  366. }
  367. /* A new USB device is ready at this point */
  368. printf("[usb-hub] devnum=%d port=%d: USB dev found\n", dev->devnum, i + 1);
  369. usb_hub_port_connect_change(dev, i);
  370. if (portchange & USB_PORT_STAT_C_ENABLE) {
  371. printf("[usb-hub] port %d enable change, status %x\n", i + 1, portstatus);
  372. usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_C_ENABLE);
  373. /*
  374. * The following hack causes a ghost device problem
  375. * to Faraday EHCI
  376. */
  377. #ifndef CONFIG_USB_EHCI_FARADAY
  378. /*
  379. * EM interference sometimes causes bad shielded USB
  380. * devices to be shutdown by the hub, this hack enables
  381. * them again. Works at least with mouse driver
  382. */
  383. if (!(portstatus & USB_PORT_STAT_ENABLE) &&
  384. (portstatus & USB_PORT_STAT_CONNECTION) &&
  385. usb_device_has_child_on_port(dev, i)) {
  386. printf("already running port %i disabled by hub (EMI?), re-enabling...\n",
  387. i + 1);
  388. usb_hub_port_connect_change(dev, i);
  389. }
  390. #endif
  391. }
  392. if (portstatus & USB_PORT_STAT_SUSPEND) {
  393. printf("port %d suspend change\n", i + 1);
  394. usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_SUSPEND);
  395. }
  396. if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
  397. printf("port %d over-current change\n", i + 1);
  398. usb_clear_port_feature(dev, i + 1,
  399. USB_PORT_FEAT_C_OVER_CURRENT);
  400. /* Only power-on this one port */
  401. usb_set_port_feature(dev, i + 1, USB_PORT_FEAT_POWER);
  402. hub->overcurrent_count[i]++;
  403. /*
  404. * If the max-scan-count is not reached, return without removing
  405. * the device from scan-list. This will re-issue a new scan.
  406. */
  407. if (hub->overcurrent_count[i] <=
  408. PORT_OVERCURRENT_MAX_SCAN_COUNT)
  409. return 0;
  410. /* Otherwise the device will get removed */
  411. printf("Port %d over-current occurred %d times\n", i + 1,
  412. hub->overcurrent_count[i]);
  413. }
  414. /*
  415. * We're done with this device, so let's remove this device from
  416. * scanning list
  417. */
  418. list_del(&usb_scan->list);
  419. free(usb_scan); // FIXME dynamic allocation
  420. return 0;
  421. }
  422. static int usb_device_list_scan(void)
  423. {
  424. struct usb_device_scan *usb_scan;
  425. struct usb_device_scan *tmp;
  426. static int running;
  427. int ret = 0;
  428. /* Only run this loop once for each controller */
  429. if (running)
  430. return 0;
  431. running = 1;
  432. while (1) {
  433. /* We're done, once the list is empty again */
  434. if (list_empty(&usb_scan_list))
  435. goto out;
  436. list_for_each_entry_safe(usb_scan, tmp, &usb_scan_list, list) {
  437. int ret;
  438. /* Scan this port */
  439. ret = usb_scan_port(usb_scan);
  440. if (ret)
  441. goto out;
  442. }
  443. }
  444. out:
  445. /*
  446. * This USB controller has finished scanning all its connected
  447. * USB devices. Set "running" back to 0, so that other USB controllers
  448. * will scan their devices too.
  449. */
  450. running = 0;
  451. return ret;
  452. }
  453. static struct usb_hub_device *usb_get_hub_device(struct usb_device *dev)
  454. {
  455. struct usb_hub_device *hub;
  456. /* "allocate" Hub device */
  457. hub = usb_hub_allocate();
  458. return hub;
  459. }
  460. static int usb_hub_configure(struct usb_device *dev)
  461. {
  462. int i, length;
  463. ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, USB_BUFSIZ);
  464. unsigned char *bitmap;
  465. short hubCharacteristics;
  466. struct usb_hub_descriptor *descriptor;
  467. struct usb_hub_device *hub;
  468. struct usb_hub_status *hubsts;
  469. int ret;
  470. hub = usb_get_hub_device(dev);
  471. if (hub == NULL)
  472. return -ENOMEM;
  473. hub->pusb_dev = dev;
  474. /* Get the the hub descriptor */
  475. ret = usb_get_hub_descriptor(dev, buffer, 4);
  476. if (ret < 0) {
  477. printf("usb_hub_configure: failed to get hub " \
  478. "descriptor, giving up %lX\n", dev->status);
  479. return ret;
  480. }
  481. descriptor = (struct usb_hub_descriptor *)buffer;
  482. length = min_t(int, descriptor->bLength,
  483. sizeof(struct usb_hub_descriptor));
  484. ret = usb_get_hub_descriptor(dev, buffer, length);
  485. if (ret < 0) {
  486. printf("usb_hub_configure: failed to get hub " \
  487. "descriptor 2nd giving up %lX\n", dev->status);
  488. return ret;
  489. }
  490. memcpy((unsigned char *)&hub->desc, buffer, length);
  491. /* adjust 16bit values */
  492. put_unaligned(le16_to_cpu(get_unaligned(
  493. &descriptor->wHubCharacteristics)),
  494. &hub->desc.wHubCharacteristics);
  495. /* set the bitmap */
  496. bitmap = (unsigned char *)&hub->desc.u.hs.DeviceRemovable[0];
  497. /* devices not removable by default */
  498. memset(bitmap, 0xff, (USB_MAXCHILDREN+1+7)/8);
  499. bitmap = (unsigned char *)&hub->desc.u.hs.PortPowerCtrlMask[0];
  500. memset(bitmap, 0xff, (USB_MAXCHILDREN+1+7)/8); /* PowerMask = 1B */
  501. for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++)
  502. hub->desc.u.hs.DeviceRemovable[i] =
  503. descriptor->u.hs.DeviceRemovable[i];
  504. for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++)
  505. hub->desc.u.hs.PortPowerCtrlMask[i] =
  506. descriptor->u.hs.PortPowerCtrlMask[i];
  507. dev->maxchild = descriptor->bNbrPorts;
  508. printf("[usb-hub] %d ports detected\n", dev->maxchild);
  509. hubCharacteristics = get_unaligned(&hub->desc.wHubCharacteristics);
  510. switch (hubCharacteristics & HUB_CHAR_LPSM) {
  511. case 0x00:
  512. printf("[usb-hub] ganged power switching\n");
  513. break;
  514. case 0x01:
  515. printf("[usb-hub] individual port power switching\n");
  516. break;
  517. case 0x02:
  518. case 0x03:
  519. printf("[usb-hub] unknown reserved power switching mode\n");
  520. break;
  521. }
  522. if (hubCharacteristics & HUB_CHAR_COMPOUND)
  523. printf("[usb-hub] part of a compound device\n");
  524. else
  525. printf("[usb-hub] standalone hub\n");
  526. switch (hubCharacteristics & HUB_CHAR_OCPM) {
  527. case 0x00:
  528. printf("[usb-hub] global over-current protection\n");
  529. break;
  530. case 0x08:
  531. printf("[usb-hub] individual port over-current protection\n");
  532. break;
  533. case 0x10:
  534. case 0x18:
  535. printf("[usb-hub] no over-current protection\n");
  536. break;
  537. }
  538. switch (dev->descriptor.bDeviceProtocol) {
  539. case USB_HUB_PR_FS:
  540. break;
  541. case USB_HUB_PR_HS_SINGLE_TT:
  542. //printf("Single TT\n");
  543. break;
  544. case USB_HUB_PR_HS_MULTI_TT:
  545. ret = usb_set_interface(dev, 0, 1);
  546. if (ret == 0) {
  547. //printf("TT per port\n");
  548. hub->tt.multi = true;
  549. } else {
  550. //printf("Using single TT (err %d)\n", ret);
  551. }
  552. break;
  553. case USB_HUB_PR_SS:
  554. /* USB 3.0 hubs don't have a TT */
  555. break;
  556. default:
  557. printf("Unrecognized hub protocol %d\n",
  558. dev->descriptor.bDeviceProtocol);
  559. break;
  560. }
  561. /* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */
  562. switch (hubCharacteristics & HUB_CHAR_TTTT) {
  563. case HUB_TTTT_8_BITS:
  564. if (dev->descriptor.bDeviceProtocol != 0) {
  565. hub->tt.think_time = 666;
  566. printf("[usb-hub] TT requires at most %d FS bit times (%d ns)\n",
  567. 8, hub->tt.think_time);
  568. }
  569. break;
  570. case HUB_TTTT_16_BITS:
  571. hub->tt.think_time = 666 * 2;
  572. printf("[usb-hub] TT requires at most %d FS bit times (%d ns)\n",
  573. 16, hub->tt.think_time);
  574. break;
  575. case HUB_TTTT_24_BITS:
  576. hub->tt.think_time = 666 * 3;
  577. printf("[usb-hub] TT requires at most %d FS bit times (%d ns)\n",
  578. 24, hub->tt.think_time);
  579. break;
  580. case HUB_TTTT_32_BITS:
  581. hub->tt.think_time = 666 * 4;
  582. printf("[usb-hub] TT requires at most %d FS bit times (%d ns)\n",
  583. 32, hub->tt.think_time);
  584. break;
  585. }
  586. printf("[usb-hub] power on to power good time: %dms\n",
  587. descriptor->bPwrOn2PwrGood * 2);
  588. printf("[usb-hub] hub controller current requirement: %dmA\n",
  589. descriptor->bHubContrCurrent);
  590. for (i = 0; i < dev->maxchild; i++)
  591. printf("[usb-hub] port %d is%s removable\n", i + 1,
  592. hub->desc.u.hs.DeviceRemovable[(i + 1) / 8] & \
  593. (1 << ((i + 1) % 8)) ? " not" : "");
  594. if (sizeof(struct usb_hub_status) > USB_BUFSIZ) {
  595. printf("usb_hub_configure: failed to get Status - " \
  596. "too long: %d\n", descriptor->bLength);
  597. return -EFBIG;
  598. }
  599. ret = usb_get_hub_status(dev, buffer);
  600. if (ret < 0) {
  601. printf("usb_hub_configure: failed to get Status %lX\n",
  602. dev->status);
  603. return ret;
  604. }
  605. hubsts = (struct usb_hub_status *)buffer;
  606. /*printf("get_hub_status returned status %X, change %X\n",
  607. le16_to_cpu(hubsts->wHubStatus),
  608. le16_to_cpu(hubsts->wHubChange));*/
  609. /*printf("local power source is %s\n",
  610. (le16_to_cpu(hubsts->wHubStatus) & HUB_STATUS_LOCAL_POWER) ? \
  611. "lost (inactive)" : "good");*/
  612. /*printf("%sover-current condition exists\n",
  613. (le16_to_cpu(hubsts->wHubStatus) & HUB_STATUS_OVERCURRENT) ? \
  614. "" : "no ");*/
  615. usb_hub_power_on(hub);
  616. /*
  617. * Reset any devices that may be in a bad state when applying
  618. * the power. This is a __weak function. Resetting of the devices
  619. * should occur in the board file of the device.
  620. */
  621. for (i = 0; i < dev->maxchild; i++)
  622. usb_hub_reset_devices(hub, i + 1);
  623. /*
  624. * Only add the connected USB devices, including potential hubs,
  625. * to a scanning list. This list will get scanned and devices that
  626. * are detected (either via port connected or via port timeout)
  627. * will get removed from this list. Scanning of the devices on this
  628. * list will continue until all devices are removed.
  629. */
  630. for (i = 0; i < dev->maxchild; i++) {
  631. struct usb_device_scan *usb_scan;
  632. usb_scan = calloc(1, sizeof(*usb_scan)); // FIXME dynamic allocation
  633. if (!usb_scan) {
  634. printf("Can't allocate memory for USB device!\n");
  635. return -ENOMEM;
  636. }
  637. usb_scan->dev = dev;
  638. usb_scan->hub = hub;
  639. usb_scan->port = i;
  640. list_add_tail(&usb_scan->list, &usb_scan_list);
  641. }
  642. /*
  643. * And now call the scanning code which loops over the generated list
  644. */
  645. ret = usb_device_list_scan();
  646. return ret;
  647. }
  648. static int usb_hub_check(struct usb_device *dev, int ifnum)
  649. {
  650. struct usb_interface *iface;
  651. struct usb_endpoint_descriptor *ep = NULL;
  652. iface = &dev->config.if_desc[ifnum];
  653. /* Is it a hub? */
  654. if (iface->desc.bInterfaceClass != USB_CLASS_HUB)
  655. goto err;
  656. /* Some hubs have a subclass of 1, which AFAICT according to the */
  657. /* specs is not defined, but it works */
  658. if ((iface->desc.bInterfaceSubClass != 0) &&
  659. (iface->desc.bInterfaceSubClass != 1))
  660. goto err;
  661. /* Multiple endpoints? What kind of mutant ninja-hub is this? */
  662. if (iface->desc.bNumEndpoints != 1)
  663. goto err;
  664. ep = &iface->ep_desc[0];
  665. /* Output endpoint? Curiousier and curiousier.. */
  666. if (!(ep->bEndpointAddress & USB_DIR_IN))
  667. goto err;
  668. /* If it's not an interrupt endpoint, we'd better punt! */
  669. if ((ep->bmAttributes & 3) != 3)
  670. goto err;
  671. /* We found a hub */
  672. printf("[usb-hub] USB hub found\n");
  673. return 0;
  674. err:
  675. printf("[usb-hub] not a USB hub: bInterfaceClass=%d, bInterfaceSubClass=%d, bNumEndpoints=%d\n",
  676. iface->desc.bInterfaceClass, iface->desc.bInterfaceSubClass,
  677. iface->desc.bNumEndpoints);
  678. if (ep) {
  679. printf(" bEndpointAddress=%#x, bmAttributes=%d",
  680. ep->bEndpointAddress, ep->bmAttributes);
  681. }
  682. return -ENOENT;
  683. }
  684. int usb_hub_probe(struct usb_device *dev, int ifnum)
  685. {
  686. int ret;
  687. ret = usb_hub_check(dev, ifnum);
  688. if (ret)
  689. return 0;
  690. ret = usb_hub_configure(dev);
  691. return ret;
  692. }