usb.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384
  1. /*
  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. * See file CREDITS for list of people who contributed to this
  19. * project.
  20. *
  21. * This program is free software; you can redistribute it and/or
  22. * modify it under the terms of the GNU General Public License as
  23. * published by the Free Software Foundation; either version 2 of
  24. * the License, or (at your option) any later version.
  25. *
  26. * This program is distributed in the hope that it will be useful,
  27. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. * GNU General Public License for more details.
  30. *
  31. * You should have received a copy of the GNU General Public License
  32. * along with this program; if not, write to the Free Software
  33. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  34. * MA 02111-1307 USA
  35. *
  36. */
  37. /*
  38. * How it works:
  39. *
  40. * Since this is a bootloader, the devices will not be automatic
  41. * (re)configured on hotplug, but after a restart of the USB the
  42. * device should work.
  43. *
  44. * For each transfer (except "Interrupt") we wait for completion.
  45. */
  46. #include <common.h>
  47. #include <command.h>
  48. #include <asm/processor.h>
  49. #include <linux/ctype.h>
  50. #include <asm/byteorder.h>
  51. #include <usb.h>
  52. #ifdef CONFIG_4xx
  53. #include <asm/4xx_pci.h>
  54. #endif
  55. #ifdef DEBUG
  56. #define USB_DEBUG
  57. #define USB_HUB_DEBUG
  58. #endif
  59. #ifdef USB_DEBUG
  60. #define USB_PRINTF(fmt, args...) printf(fmt , ##args)
  61. #else
  62. #define USB_PRINTF(fmt, args...)
  63. #endif
  64. #define USB_BUFSIZ 512
  65. static struct usb_device usb_dev[USB_MAX_DEVICE];
  66. static int dev_index;
  67. static int running;
  68. static int asynch_allowed;
  69. static struct devrequest setup_packet;
  70. char usb_started; /* flag for the started/stopped USB status */
  71. /**********************************************************************
  72. * some forward declerations...
  73. */
  74. void usb_scan_devices(void);
  75. int usb_hub_probe(struct usb_device *dev, int ifnum);
  76. void usb_hub_reset(void);
  77. static int hub_port_reset(struct usb_device *dev, int port,
  78. unsigned short *portstat);
  79. /***********************************************************************
  80. * wait_ms
  81. */
  82. inline void wait_ms(unsigned long ms)
  83. {
  84. while (ms-- > 0)
  85. udelay(1000);
  86. }
  87. /***************************************************************************
  88. * Init USB Device
  89. */
  90. int usb_init(void)
  91. {
  92. int result;
  93. running = 0;
  94. dev_index = 0;
  95. asynch_allowed = 1;
  96. usb_hub_reset();
  97. /* init low_level USB */
  98. printf("USB: ");
  99. result = usb_lowlevel_init();
  100. /* if lowlevel init is OK, scan the bus for devices
  101. * i.e. search HUBs and configure them */
  102. if (result == 0) {
  103. printf("scanning bus for devices... ");
  104. running = 1;
  105. usb_scan_devices();
  106. usb_started = 1;
  107. return 0;
  108. } else {
  109. printf("Error, couldn't init Lowlevel part\n");
  110. usb_started = 0;
  111. return -1;
  112. }
  113. }
  114. /******************************************************************************
  115. * Stop USB this stops the LowLevel Part and deregisters USB devices.
  116. */
  117. int usb_stop(void)
  118. {
  119. int res = 0;
  120. if (usb_started) {
  121. asynch_allowed = 1;
  122. usb_started = 0;
  123. usb_hub_reset();
  124. res = usb_lowlevel_stop();
  125. }
  126. return res;
  127. }
  128. /*
  129. * disables the asynch behaviour of the control message. This is used for data
  130. * transfers that uses the exclusiv access to the control and bulk messages.
  131. */
  132. void usb_disable_asynch(int disable)
  133. {
  134. asynch_allowed = !disable;
  135. }
  136. /*-------------------------------------------------------------------
  137. * Message wrappers.
  138. *
  139. */
  140. /*
  141. * submits an Interrupt Message
  142. */
  143. int usb_submit_int_msg(struct usb_device *dev, unsigned long pipe,
  144. void *buffer, int transfer_len, int interval)
  145. {
  146. return submit_int_msg(dev, pipe, buffer, transfer_len, interval);
  147. }
  148. /*
  149. * submits a control message and waits for comletion (at least timeout * 1ms)
  150. * If timeout is 0, we don't wait for completion (used as example to set and
  151. * clear keyboards LEDs). For data transfers, (storage transfers) we don't
  152. * allow control messages with 0 timeout, by previousely resetting the flag
  153. * asynch_allowed (usb_disable_asynch(1)).
  154. * returns the transfered length if OK or -1 if error. The transfered length
  155. * and the current status are stored in the dev->act_len and dev->status.
  156. */
  157. int usb_control_msg(struct usb_device *dev, unsigned int pipe,
  158. unsigned char request, unsigned char requesttype,
  159. unsigned short value, unsigned short index,
  160. void *data, unsigned short size, int timeout)
  161. {
  162. if ((timeout == 0) && (!asynch_allowed)) {
  163. /* request for a asynch control pipe is not allowed */
  164. return -1;
  165. }
  166. /* set setup command */
  167. setup_packet.requesttype = requesttype;
  168. setup_packet.request = request;
  169. setup_packet.value = cpu_to_le16(value);
  170. setup_packet.index = cpu_to_le16(index);
  171. setup_packet.length = cpu_to_le16(size);
  172. USB_PRINTF("usb_control_msg: request: 0x%X, requesttype: 0x%X, " \
  173. "value 0x%X index 0x%X length 0x%X\n",
  174. request, requesttype, value, index, size);
  175. dev->status = USB_ST_NOT_PROC; /*not yet processed */
  176. submit_control_msg(dev, pipe, data, size, &setup_packet);
  177. if (timeout == 0)
  178. return (int)size;
  179. /*
  180. * Wait for status to update until timeout expires, USB driver
  181. * interrupt handler may set the status when the USB operation has
  182. * been completed.
  183. */
  184. while (timeout--) {
  185. if (!((volatile unsigned long)dev->status & USB_ST_NOT_PROC))
  186. break;
  187. wait_ms(1);
  188. }
  189. if (dev->status)
  190. return -1;
  191. return dev->act_len;
  192. }
  193. /*-------------------------------------------------------------------
  194. * submits bulk message, and waits for completion. returns 0 if Ok or
  195. * -1 if Error.
  196. * synchronous behavior
  197. */
  198. int usb_bulk_msg(struct usb_device *dev, unsigned int pipe,
  199. void *data, int len, int *actual_length, int timeout)
  200. {
  201. if (len < 0)
  202. return -1;
  203. dev->status = USB_ST_NOT_PROC; /*not yet processed */
  204. submit_bulk_msg(dev, pipe, data, len);
  205. while (timeout--) {
  206. if (!((volatile unsigned long)dev->status & USB_ST_NOT_PROC))
  207. break;
  208. wait_ms(1);
  209. }
  210. *actual_length = dev->act_len;
  211. if (dev->status == 0)
  212. return 0;
  213. else
  214. return -1;
  215. }
  216. /*-------------------------------------------------------------------
  217. * Max Packet stuff
  218. */
  219. /*
  220. * returns the max packet size, depending on the pipe direction and
  221. * the configurations values
  222. */
  223. int usb_maxpacket(struct usb_device *dev, unsigned long pipe)
  224. {
  225. /* direction is out -> use emaxpacket out */
  226. if ((pipe & USB_DIR_IN) == 0)
  227. return dev->epmaxpacketout[((pipe>>15) & 0xf)];
  228. else
  229. return dev->epmaxpacketin[((pipe>>15) & 0xf)];
  230. }
  231. /* The routine usb_set_maxpacket_ep() is extracted from the loop of routine
  232. * usb_set_maxpacket(), because the optimizer of GCC 4.x chokes on this routine
  233. * when it is inlined in 1 single routine. What happens is that the register r3
  234. * is used as loop-count 'i', but gets overwritten later on.
  235. * This is clearly a compiler bug, but it is easier to workaround it here than
  236. * to update the compiler (Occurs with at least several GCC 4.{1,2},x
  237. * CodeSourcery compilers like e.g. 2007q3, 2008q1, 2008q3 lite editions on ARM)
  238. */
  239. static void __attribute__((noinline))
  240. usb_set_maxpacket_ep(struct usb_device *dev, struct usb_endpoint_descriptor *ep)
  241. {
  242. int b;
  243. b = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
  244. if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
  245. USB_ENDPOINT_XFER_CONTROL) {
  246. /* Control => bidirectional */
  247. dev->epmaxpacketout[b] = ep->wMaxPacketSize;
  248. dev->epmaxpacketin[b] = ep->wMaxPacketSize;
  249. USB_PRINTF("##Control EP epmaxpacketout/in[%d] = %d\n",
  250. b, dev->epmaxpacketin[b]);
  251. } else {
  252. if ((ep->bEndpointAddress & 0x80) == 0) {
  253. /* OUT Endpoint */
  254. if (ep->wMaxPacketSize > dev->epmaxpacketout[b]) {
  255. dev->epmaxpacketout[b] = ep->wMaxPacketSize;
  256. USB_PRINTF("##EP epmaxpacketout[%d] = %d\n",
  257. b, dev->epmaxpacketout[b]);
  258. }
  259. } else {
  260. /* IN Endpoint */
  261. if (ep->wMaxPacketSize > dev->epmaxpacketin[b]) {
  262. dev->epmaxpacketin[b] = ep->wMaxPacketSize;
  263. USB_PRINTF("##EP epmaxpacketin[%d] = %d\n",
  264. b, dev->epmaxpacketin[b]);
  265. }
  266. } /* if out */
  267. } /* if control */
  268. }
  269. /*
  270. * set the max packed value of all endpoints in the given configuration
  271. */
  272. int usb_set_maxpacket(struct usb_device *dev)
  273. {
  274. int i, ii;
  275. for (i = 0; i < dev->config.desc.bNumInterfaces; i++)
  276. for (ii = 0; ii < dev->config.if_desc[i].desc.bNumEndpoints; ii++)
  277. usb_set_maxpacket_ep(dev,
  278. &dev->config.if_desc[i].ep_desc[ii]);
  279. return 0;
  280. }
  281. /*******************************************************************************
  282. * Parse the config, located in buffer, and fills the dev->config structure.
  283. * Note that all little/big endian swapping are done automatically.
  284. */
  285. int usb_parse_config(struct usb_device *dev, unsigned char *buffer, int cfgno)
  286. {
  287. struct usb_descriptor_header *head;
  288. int index, ifno, epno, curr_if_num;
  289. int i;
  290. unsigned char *ch;
  291. ifno = -1;
  292. epno = -1;
  293. curr_if_num = -1;
  294. dev->configno = cfgno;
  295. head = (struct usb_descriptor_header *) &buffer[0];
  296. if (head->bDescriptorType != USB_DT_CONFIG) {
  297. printf(" ERROR: NOT USB_CONFIG_DESC %x\n",
  298. head->bDescriptorType);
  299. return -1;
  300. }
  301. memcpy(&dev->config, buffer, buffer[0]);
  302. le16_to_cpus(&(dev->config.desc.wTotalLength));
  303. dev->config.no_of_if = 0;
  304. index = dev->config.desc.bLength;
  305. /* Ok the first entry must be a configuration entry,
  306. * now process the others */
  307. head = (struct usb_descriptor_header *) &buffer[index];
  308. while (index + 1 < dev->config.desc.wTotalLength) {
  309. switch (head->bDescriptorType) {
  310. case USB_DT_INTERFACE:
  311. if (((struct usb_interface_descriptor *) \
  312. &buffer[index])->bInterfaceNumber != curr_if_num) {
  313. /* this is a new interface, copy new desc */
  314. ifno = dev->config.no_of_if;
  315. dev->config.no_of_if++;
  316. memcpy(&dev->config.if_desc[ifno],
  317. &buffer[index], buffer[index]);
  318. dev->config.if_desc[ifno].no_of_ep = 0;
  319. dev->config.if_desc[ifno].num_altsetting = 1;
  320. curr_if_num =
  321. dev->config.if_desc[ifno].desc.bInterfaceNumber;
  322. } else {
  323. /* found alternate setting for the interface */
  324. dev->config.if_desc[ifno].num_altsetting++;
  325. }
  326. break;
  327. case USB_DT_ENDPOINT:
  328. epno = dev->config.if_desc[ifno].no_of_ep;
  329. /* found an endpoint */
  330. dev->config.if_desc[ifno].no_of_ep++;
  331. memcpy(&dev->config.if_desc[ifno].ep_desc[epno],
  332. &buffer[index], buffer[index]);
  333. le16_to_cpus(&(dev->config.if_desc[ifno].ep_desc[epno].\
  334. wMaxPacketSize));
  335. USB_PRINTF("if %d, ep %d\n", ifno, epno);
  336. break;
  337. default:
  338. if (head->bLength == 0)
  339. return 1;
  340. USB_PRINTF("unknown Description Type : %x\n",
  341. head->bDescriptorType);
  342. {
  343. ch = (unsigned char *)head;
  344. for (i = 0; i < head->bLength; i++)
  345. USB_PRINTF("%02X ", *ch++);
  346. USB_PRINTF("\n\n\n");
  347. }
  348. break;
  349. }
  350. index += head->bLength;
  351. head = (struct usb_descriptor_header *)&buffer[index];
  352. }
  353. return 1;
  354. }
  355. /***********************************************************************
  356. * Clears an endpoint
  357. * endp: endpoint number in bits 0-3;
  358. * direction flag in bit 7 (1 = IN, 0 = OUT)
  359. */
  360. int usb_clear_halt(struct usb_device *dev, int pipe)
  361. {
  362. int result;
  363. int endp = usb_pipeendpoint(pipe)|(usb_pipein(pipe)<<7);
  364. result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  365. USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT, 0,
  366. endp, NULL, 0, USB_CNTL_TIMEOUT * 3);
  367. /* don't clear if failed */
  368. if (result < 0)
  369. return result;
  370. /*
  371. * NOTE: we do not get status and verify reset was successful
  372. * as some devices are reported to lock up upon this check..
  373. */
  374. usb_endpoint_running(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe));
  375. /* toggle is reset on clear */
  376. usb_settoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe), 0);
  377. return 0;
  378. }
  379. /**********************************************************************
  380. * get_descriptor type
  381. */
  382. int usb_get_descriptor(struct usb_device *dev, unsigned char type,
  383. unsigned char index, void *buf, int size)
  384. {
  385. int res;
  386. res = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  387. USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
  388. (type << 8) + index, 0,
  389. buf, size, USB_CNTL_TIMEOUT);
  390. return res;
  391. }
  392. /**********************************************************************
  393. * gets configuration cfgno and store it in the buffer
  394. */
  395. int usb_get_configuration_no(struct usb_device *dev,
  396. unsigned char *buffer, int cfgno)
  397. {
  398. int result;
  399. unsigned int tmp;
  400. struct usb_configuration_descriptor *config;
  401. config = (struct usb_configuration_descriptor *)&buffer[0];
  402. result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno, buffer, 9);
  403. if (result < 9) {
  404. if (result < 0)
  405. printf("unable to get descriptor, error %lX\n",
  406. dev->status);
  407. else
  408. printf("config descriptor too short " \
  409. "(expected %i, got %i)\n", 9, result);
  410. return -1;
  411. }
  412. tmp = le16_to_cpu(config->wTotalLength);
  413. if (tmp > USB_BUFSIZ) {
  414. USB_PRINTF("usb_get_configuration_no: failed to get " \
  415. "descriptor - too long: %d\n", tmp);
  416. return -1;
  417. }
  418. result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno, buffer, tmp);
  419. USB_PRINTF("get_conf_no %d Result %d, wLength %d\n",
  420. cfgno, result, tmp);
  421. return result;
  422. }
  423. /********************************************************************
  424. * set address of a device to the value in dev->devnum.
  425. * This can only be done by addressing the device via the default address (0)
  426. */
  427. int usb_set_address(struct usb_device *dev)
  428. {
  429. int res;
  430. USB_PRINTF("set address %d\n", dev->devnum);
  431. res = usb_control_msg(dev, usb_snddefctrl(dev),
  432. USB_REQ_SET_ADDRESS, 0,
  433. (dev->devnum), 0,
  434. NULL, 0, USB_CNTL_TIMEOUT);
  435. return res;
  436. }
  437. /********************************************************************
  438. * set interface number to interface
  439. */
  440. int usb_set_interface(struct usb_device *dev, int interface, int alternate)
  441. {
  442. struct usb_interface *if_face = NULL;
  443. int ret, i;
  444. for (i = 0; i < dev->config.desc.bNumInterfaces; i++) {
  445. if (dev->config.if_desc[i].desc.bInterfaceNumber == interface) {
  446. if_face = &dev->config.if_desc[i];
  447. break;
  448. }
  449. }
  450. if (!if_face) {
  451. printf("selecting invalid interface %d", interface);
  452. return -1;
  453. }
  454. /*
  455. * We should return now for devices with only one alternate setting.
  456. * According to 9.4.10 of the Universal Serial Bus Specification
  457. * Revision 2.0 such devices can return with a STALL. This results in
  458. * some USB sticks timeouting during initialization and then being
  459. * unusable in U-Boot.
  460. */
  461. if (if_face->num_altsetting == 1)
  462. return 0;
  463. ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  464. USB_REQ_SET_INTERFACE, USB_RECIP_INTERFACE,
  465. alternate, interface, NULL, 0,
  466. USB_CNTL_TIMEOUT * 5);
  467. if (ret < 0)
  468. return ret;
  469. return 0;
  470. }
  471. /********************************************************************
  472. * set configuration number to configuration
  473. */
  474. int usb_set_configuration(struct usb_device *dev, int configuration)
  475. {
  476. int res;
  477. USB_PRINTF("set configuration %d\n", configuration);
  478. /* set setup command */
  479. res = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  480. USB_REQ_SET_CONFIGURATION, 0,
  481. configuration, 0,
  482. NULL, 0, USB_CNTL_TIMEOUT);
  483. if (res == 0) {
  484. dev->toggle[0] = 0;
  485. dev->toggle[1] = 0;
  486. return 0;
  487. } else
  488. return -1;
  489. }
  490. /********************************************************************
  491. * set protocol to protocol
  492. */
  493. int usb_set_protocol(struct usb_device *dev, int ifnum, int protocol)
  494. {
  495. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  496. USB_REQ_SET_PROTOCOL, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  497. protocol, ifnum, NULL, 0, USB_CNTL_TIMEOUT);
  498. }
  499. /********************************************************************
  500. * set idle
  501. */
  502. int usb_set_idle(struct usb_device *dev, int ifnum, int duration, int report_id)
  503. {
  504. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  505. USB_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  506. (duration << 8) | report_id, ifnum, NULL, 0, USB_CNTL_TIMEOUT);
  507. }
  508. /********************************************************************
  509. * get report
  510. */
  511. int usb_get_report(struct usb_device *dev, int ifnum, unsigned char type,
  512. unsigned char id, void *buf, int size)
  513. {
  514. return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  515. USB_REQ_GET_REPORT,
  516. USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  517. (type << 8) + id, ifnum, buf, size, USB_CNTL_TIMEOUT);
  518. }
  519. /********************************************************************
  520. * get class descriptor
  521. */
  522. int usb_get_class_descriptor(struct usb_device *dev, int ifnum,
  523. unsigned char type, unsigned char id, void *buf, int size)
  524. {
  525. return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  526. USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN,
  527. (type << 8) + id, ifnum, buf, size, USB_CNTL_TIMEOUT);
  528. }
  529. /********************************************************************
  530. * get string index in buffer
  531. */
  532. int usb_get_string(struct usb_device *dev, unsigned short langid,
  533. unsigned char index, void *buf, int size)
  534. {
  535. int i;
  536. int result;
  537. for (i = 0; i < 3; ++i) {
  538. /* some devices are flaky */
  539. result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  540. USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
  541. (USB_DT_STRING << 8) + index, langid, buf, size,
  542. USB_CNTL_TIMEOUT);
  543. if (result > 0)
  544. break;
  545. }
  546. return result;
  547. }
  548. static void usb_try_string_workarounds(unsigned char *buf, int *length)
  549. {
  550. int newlength, oldlength = *length;
  551. for (newlength = 2; newlength + 1 < oldlength; newlength += 2)
  552. if (!isprint(buf[newlength]) || buf[newlength + 1])
  553. break;
  554. if (newlength > 2) {
  555. buf[0] = newlength;
  556. *length = newlength;
  557. }
  558. }
  559. static int usb_string_sub(struct usb_device *dev, unsigned int langid,
  560. unsigned int index, unsigned char *buf)
  561. {
  562. int rc;
  563. /* Try to read the string descriptor by asking for the maximum
  564. * possible number of bytes */
  565. rc = usb_get_string(dev, langid, index, buf, 255);
  566. /* If that failed try to read the descriptor length, then
  567. * ask for just that many bytes */
  568. if (rc < 2) {
  569. rc = usb_get_string(dev, langid, index, buf, 2);
  570. if (rc == 2)
  571. rc = usb_get_string(dev, langid, index, buf, buf[0]);
  572. }
  573. if (rc >= 2) {
  574. if (!buf[0] && !buf[1])
  575. usb_try_string_workarounds(buf, &rc);
  576. /* There might be extra junk at the end of the descriptor */
  577. if (buf[0] < rc)
  578. rc = buf[0];
  579. rc = rc - (rc & 1); /* force a multiple of two */
  580. }
  581. if (rc < 2)
  582. rc = -1;
  583. return rc;
  584. }
  585. /********************************************************************
  586. * usb_string:
  587. * Get string index and translate it to ascii.
  588. * returns string length (> 0) or error (< 0)
  589. */
  590. int usb_string(struct usb_device *dev, int index, char *buf, size_t size)
  591. {
  592. unsigned char mybuf[USB_BUFSIZ];
  593. unsigned char *tbuf;
  594. int err;
  595. unsigned int u, idx;
  596. if (size <= 0 || !buf || !index)
  597. return -1;
  598. buf[0] = 0;
  599. tbuf = &mybuf[0];
  600. /* get langid for strings if it's not yet known */
  601. if (!dev->have_langid) {
  602. err = usb_string_sub(dev, 0, 0, tbuf);
  603. if (err < 0) {
  604. USB_PRINTF("error getting string descriptor 0 " \
  605. "(error=%lx)\n", dev->status);
  606. return -1;
  607. } else if (tbuf[0] < 4) {
  608. USB_PRINTF("string descriptor 0 too short\n");
  609. return -1;
  610. } else {
  611. dev->have_langid = -1;
  612. dev->string_langid = tbuf[2] | (tbuf[3] << 8);
  613. /* always use the first langid listed */
  614. USB_PRINTF("USB device number %d default " \
  615. "language ID 0x%x\n",
  616. dev->devnum, dev->string_langid);
  617. }
  618. }
  619. err = usb_string_sub(dev, dev->string_langid, index, tbuf);
  620. if (err < 0)
  621. return err;
  622. size--; /* leave room for trailing NULL char in output buffer */
  623. for (idx = 0, u = 2; u < err; u += 2) {
  624. if (idx >= size)
  625. break;
  626. if (tbuf[u+1]) /* high byte */
  627. buf[idx++] = '?'; /* non-ASCII character */
  628. else
  629. buf[idx++] = tbuf[u];
  630. }
  631. buf[idx] = 0;
  632. err = idx;
  633. return err;
  634. }
  635. /********************************************************************
  636. * USB device handling:
  637. * the USB device are static allocated [USB_MAX_DEVICE].
  638. */
  639. /* returns a pointer to the device with the index [index].
  640. * if the device is not assigned (dev->devnum==-1) returns NULL
  641. */
  642. struct usb_device *usb_get_dev_index(int index)
  643. {
  644. if (usb_dev[index].devnum == -1)
  645. return NULL;
  646. else
  647. return &usb_dev[index];
  648. }
  649. /* returns a pointer of a new device structure or NULL, if
  650. * no device struct is available
  651. */
  652. struct usb_device *usb_alloc_new_device(void)
  653. {
  654. int i;
  655. USB_PRINTF("New Device %d\n", dev_index);
  656. if (dev_index == USB_MAX_DEVICE) {
  657. printf("ERROR, too many USB Devices, max=%d\n", USB_MAX_DEVICE);
  658. return NULL;
  659. }
  660. /* default Address is 0, real addresses start with 1 */
  661. usb_dev[dev_index].devnum = dev_index + 1;
  662. usb_dev[dev_index].maxchild = 0;
  663. for (i = 0; i < USB_MAXCHILDREN; i++)
  664. usb_dev[dev_index].children[i] = NULL;
  665. usb_dev[dev_index].parent = NULL;
  666. dev_index++;
  667. return &usb_dev[dev_index - 1];
  668. }
  669. /*
  670. * By the time we get here, the device has gotten a new device ID
  671. * and is in the default state. We need to identify the thing and
  672. * get the ball rolling..
  673. *
  674. * Returns 0 for success, != 0 for error.
  675. */
  676. int usb_new_device(struct usb_device *dev)
  677. {
  678. int addr, err;
  679. int tmp;
  680. unsigned char tmpbuf[USB_BUFSIZ];
  681. /* We still haven't set the Address yet */
  682. addr = dev->devnum;
  683. dev->devnum = 0;
  684. #ifdef CONFIG_LEGACY_USB_INIT_SEQ
  685. /* this is the old and known way of initializing devices, it is
  686. * different than what Windows and Linux are doing. Windows and Linux
  687. * both retrieve 64 bytes while reading the device descriptor
  688. * Several USB stick devices report ERR: CTL_TIMEOUT, caused by an
  689. * invalid header while reading 8 bytes as device descriptor. */
  690. dev->descriptor.bMaxPacketSize0 = 8; /* Start off at 8 bytes */
  691. dev->maxpacketsize = PACKET_SIZE_8;
  692. dev->epmaxpacketin[0] = 8;
  693. dev->epmaxpacketout[0] = 8;
  694. err = usb_get_descriptor(dev, USB_DT_DEVICE, 0, &dev->descriptor, 8);
  695. if (err < 8) {
  696. printf("\n USB device not responding, " \
  697. "giving up (status=%lX)\n", dev->status);
  698. return 1;
  699. }
  700. #else
  701. /* This is a Windows scheme of initialization sequence, with double
  702. * reset of the device (Linux uses the same sequence)
  703. * Some equipment is said to work only with such init sequence; this
  704. * patch is based on the work by Alan Stern:
  705. * http://sourceforge.net/mailarchive/forum.php?
  706. * thread_id=5729457&forum_id=5398
  707. */
  708. struct usb_device_descriptor *desc;
  709. int port = -1;
  710. struct usb_device *parent = dev->parent;
  711. unsigned short portstatus;
  712. /* send 64-byte GET-DEVICE-DESCRIPTOR request. Since the descriptor is
  713. * only 18 bytes long, this will terminate with a short packet. But if
  714. * the maxpacket size is 8 or 16 the device may be waiting to transmit
  715. * some more, or keeps on retransmitting the 8 byte header. */
  716. desc = (struct usb_device_descriptor *)tmpbuf;
  717. dev->descriptor.bMaxPacketSize0 = 64; /* Start off at 64 bytes */
  718. /* Default to 64 byte max packet size */
  719. dev->maxpacketsize = PACKET_SIZE_64;
  720. dev->epmaxpacketin[0] = 64;
  721. dev->epmaxpacketout[0] = 64;
  722. err = usb_get_descriptor(dev, USB_DT_DEVICE, 0, desc, 64);
  723. if (err < 0) {
  724. USB_PRINTF("usb_new_device: usb_get_descriptor() failed\n");
  725. return 1;
  726. }
  727. dev->descriptor.bMaxPacketSize0 = desc->bMaxPacketSize0;
  728. /* find the port number we're at */
  729. if (parent) {
  730. int j;
  731. for (j = 0; j < parent->maxchild; j++) {
  732. if (parent->children[j] == dev) {
  733. port = j;
  734. break;
  735. }
  736. }
  737. if (port < 0) {
  738. printf("usb_new_device:cannot locate device's port.\n");
  739. return 1;
  740. }
  741. /* reset the port for the second time */
  742. err = hub_port_reset(dev->parent, port, &portstatus);
  743. if (err < 0) {
  744. printf("\n Couldn't reset port %i\n", port);
  745. return 1;
  746. }
  747. }
  748. #endif
  749. dev->epmaxpacketin[0] = dev->descriptor.bMaxPacketSize0;
  750. dev->epmaxpacketout[0] = dev->descriptor.bMaxPacketSize0;
  751. switch (dev->descriptor.bMaxPacketSize0) {
  752. case 8:
  753. dev->maxpacketsize = PACKET_SIZE_8;
  754. break;
  755. case 16:
  756. dev->maxpacketsize = PACKET_SIZE_16;
  757. break;
  758. case 32:
  759. dev->maxpacketsize = PACKET_SIZE_32;
  760. break;
  761. case 64:
  762. dev->maxpacketsize = PACKET_SIZE_64;
  763. break;
  764. }
  765. dev->devnum = addr;
  766. err = usb_set_address(dev); /* set address */
  767. if (err < 0) {
  768. printf("\n USB device not accepting new address " \
  769. "(error=%lX)\n", dev->status);
  770. return 1;
  771. }
  772. wait_ms(10); /* Let the SET_ADDRESS settle */
  773. tmp = sizeof(dev->descriptor);
  774. err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
  775. &dev->descriptor, sizeof(dev->descriptor));
  776. if (err < tmp) {
  777. if (err < 0)
  778. printf("unable to get device descriptor (error=%d)\n",
  779. err);
  780. else
  781. printf("USB device descriptor short read " \
  782. "(expected %i, got %i)\n", tmp, err);
  783. return 1;
  784. }
  785. /* correct le values */
  786. le16_to_cpus(&dev->descriptor.bcdUSB);
  787. le16_to_cpus(&dev->descriptor.idVendor);
  788. le16_to_cpus(&dev->descriptor.idProduct);
  789. le16_to_cpus(&dev->descriptor.bcdDevice);
  790. /* only support for one config for now */
  791. usb_get_configuration_no(dev, &tmpbuf[0], 0);
  792. usb_parse_config(dev, &tmpbuf[0], 0);
  793. usb_set_maxpacket(dev);
  794. /* we set the default configuration here */
  795. if (usb_set_configuration(dev, dev->config.desc.bConfigurationValue)) {
  796. printf("failed to set default configuration " \
  797. "len %d, status %lX\n", dev->act_len, dev->status);
  798. return -1;
  799. }
  800. USB_PRINTF("new device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
  801. dev->descriptor.iManufacturer, dev->descriptor.iProduct,
  802. dev->descriptor.iSerialNumber);
  803. memset(dev->mf, 0, sizeof(dev->mf));
  804. memset(dev->prod, 0, sizeof(dev->prod));
  805. memset(dev->serial, 0, sizeof(dev->serial));
  806. if (dev->descriptor.iManufacturer)
  807. usb_string(dev, dev->descriptor.iManufacturer,
  808. dev->mf, sizeof(dev->mf));
  809. if (dev->descriptor.iProduct)
  810. usb_string(dev, dev->descriptor.iProduct,
  811. dev->prod, sizeof(dev->prod));
  812. if (dev->descriptor.iSerialNumber)
  813. usb_string(dev, dev->descriptor.iSerialNumber,
  814. dev->serial, sizeof(dev->serial));
  815. USB_PRINTF("Manufacturer %s\n", dev->mf);
  816. USB_PRINTF("Product %s\n", dev->prod);
  817. USB_PRINTF("SerialNumber %s\n", dev->serial);
  818. /* now prode if the device is a hub */
  819. usb_hub_probe(dev, 0);
  820. return 0;
  821. }
  822. /* build device Tree */
  823. void usb_scan_devices(void)
  824. {
  825. int i;
  826. struct usb_device *dev;
  827. /* first make all devices unknown */
  828. for (i = 0; i < USB_MAX_DEVICE; i++) {
  829. memset(&usb_dev[i], 0, sizeof(struct usb_device));
  830. usb_dev[i].devnum = -1;
  831. }
  832. dev_index = 0;
  833. /* device 0 is always present (root hub, so let it analyze) */
  834. dev = usb_alloc_new_device();
  835. if (usb_new_device(dev))
  836. printf("No USB Device found\n");
  837. else
  838. printf("%d USB Device(s) found\n", dev_index);
  839. /* insert "driver" if possible */
  840. #ifdef CONFIG_USB_KEYBOARD
  841. drv_usb_kbd_init();
  842. USB_PRINTF("scan end\n");
  843. #endif
  844. }
  845. /****************************************************************************
  846. * HUB "Driver"
  847. * Probes device for being a hub and configurate it
  848. */
  849. #ifdef USB_HUB_DEBUG
  850. #define USB_HUB_PRINTF(fmt, args...) printf(fmt , ##args)
  851. #else
  852. #define USB_HUB_PRINTF(fmt, args...)
  853. #endif
  854. static struct usb_hub_device hub_dev[USB_MAX_HUB];
  855. static int usb_hub_index;
  856. int usb_get_hub_descriptor(struct usb_device *dev, void *data, int size)
  857. {
  858. return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  859. USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
  860. USB_DT_HUB << 8, 0, data, size, USB_CNTL_TIMEOUT);
  861. }
  862. int usb_clear_hub_feature(struct usb_device *dev, int feature)
  863. {
  864. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  865. USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature,
  866. 0, NULL, 0, USB_CNTL_TIMEOUT);
  867. }
  868. int usb_clear_port_feature(struct usb_device *dev, int port, int feature)
  869. {
  870. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  871. USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature,
  872. port, NULL, 0, USB_CNTL_TIMEOUT);
  873. }
  874. int usb_set_port_feature(struct usb_device *dev, int port, int feature)
  875. {
  876. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  877. USB_REQ_SET_FEATURE, USB_RT_PORT, feature,
  878. port, NULL, 0, USB_CNTL_TIMEOUT);
  879. }
  880. int usb_get_hub_status(struct usb_device *dev, void *data)
  881. {
  882. return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  883. USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
  884. data, sizeof(struct usb_hub_status), USB_CNTL_TIMEOUT);
  885. }
  886. int usb_get_port_status(struct usb_device *dev, int port, void *data)
  887. {
  888. return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  889. USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port,
  890. data, sizeof(struct usb_hub_status), USB_CNTL_TIMEOUT);
  891. }
  892. static void usb_hub_power_on(struct usb_hub_device *hub)
  893. {
  894. int i;
  895. struct usb_device *dev;
  896. dev = hub->pusb_dev;
  897. /* Enable power to the ports */
  898. USB_HUB_PRINTF("enabling power on all ports\n");
  899. for (i = 0; i < dev->maxchild; i++) {
  900. usb_set_port_feature(dev, i + 1, USB_PORT_FEAT_POWER);
  901. USB_HUB_PRINTF("port %d returns %lX\n", i + 1, dev->status);
  902. wait_ms(hub->desc.bPwrOn2PwrGood * 2);
  903. }
  904. }
  905. void usb_hub_reset(void)
  906. {
  907. usb_hub_index = 0;
  908. }
  909. struct usb_hub_device *usb_hub_allocate(void)
  910. {
  911. if (usb_hub_index < USB_MAX_HUB)
  912. return &hub_dev[usb_hub_index++];
  913. printf("ERROR: USB_MAX_HUB (%d) reached\n", USB_MAX_HUB);
  914. return NULL;
  915. }
  916. #define MAX_TRIES 5
  917. static inline char *portspeed(int portstatus)
  918. {
  919. if (portstatus & (1 << USB_PORT_FEAT_HIGHSPEED))
  920. return "480 Mb/s";
  921. else if (portstatus & (1 << USB_PORT_FEAT_LOWSPEED))
  922. return "1.5 Mb/s";
  923. else
  924. return "12 Mb/s";
  925. }
  926. static int hub_port_reset(struct usb_device *dev, int port,
  927. unsigned short *portstat)
  928. {
  929. int tries;
  930. struct usb_port_status portsts;
  931. unsigned short portstatus, portchange;
  932. USB_HUB_PRINTF("hub_port_reset: resetting port %d...\n", port);
  933. for (tries = 0; tries < MAX_TRIES; tries++) {
  934. usb_set_port_feature(dev, port + 1, USB_PORT_FEAT_RESET);
  935. wait_ms(200);
  936. if (usb_get_port_status(dev, port + 1, &portsts) < 0) {
  937. USB_HUB_PRINTF("get_port_status failed status %lX\n",
  938. dev->status);
  939. return -1;
  940. }
  941. portstatus = le16_to_cpu(portsts.wPortStatus);
  942. portchange = le16_to_cpu(portsts.wPortChange);
  943. USB_HUB_PRINTF("portstatus %x, change %x, %s\n",
  944. portstatus, portchange,
  945. portspeed(portstatus));
  946. USB_HUB_PRINTF("STAT_C_CONNECTION = %d STAT_CONNECTION = %d" \
  947. " USB_PORT_STAT_ENABLE %d\n",
  948. (portchange & USB_PORT_STAT_C_CONNECTION) ? 1 : 0,
  949. (portstatus & USB_PORT_STAT_CONNECTION) ? 1 : 0,
  950. (portstatus & USB_PORT_STAT_ENABLE) ? 1 : 0);
  951. if ((portchange & USB_PORT_STAT_C_CONNECTION) ||
  952. !(portstatus & USB_PORT_STAT_CONNECTION))
  953. return -1;
  954. if (portstatus & USB_PORT_STAT_ENABLE)
  955. break;
  956. wait_ms(200);
  957. }
  958. if (tries == MAX_TRIES) {
  959. USB_HUB_PRINTF("Cannot enable port %i after %i retries, " \
  960. "disabling port.\n", port + 1, MAX_TRIES);
  961. USB_HUB_PRINTF("Maybe the USB cable is bad?\n");
  962. return -1;
  963. }
  964. usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_RESET);
  965. *portstat = portstatus;
  966. return 0;
  967. }
  968. void usb_hub_port_connect_change(struct usb_device *dev, int port)
  969. {
  970. struct usb_device *usb;
  971. struct usb_port_status portsts;
  972. unsigned short portstatus, portchange;
  973. /* Check status */
  974. if (usb_get_port_status(dev, port + 1, &portsts) < 0) {
  975. USB_HUB_PRINTF("get_port_status failed\n");
  976. return;
  977. }
  978. portstatus = le16_to_cpu(portsts.wPortStatus);
  979. portchange = le16_to_cpu(portsts.wPortChange);
  980. USB_HUB_PRINTF("portstatus %x, change %x, %s\n",
  981. portstatus, portchange, portspeed(portstatus));
  982. /* Clear the connection change status */
  983. usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_CONNECTION);
  984. /* Disconnect any existing devices under this port */
  985. if (((!(portstatus & USB_PORT_STAT_CONNECTION)) &&
  986. (!(portstatus & USB_PORT_STAT_ENABLE))) || (dev->children[port])) {
  987. USB_HUB_PRINTF("usb_disconnect(&hub->children[port]);\n");
  988. /* Return now if nothing is connected */
  989. if (!(portstatus & USB_PORT_STAT_CONNECTION))
  990. return;
  991. }
  992. wait_ms(200);
  993. /* Reset the port */
  994. if (hub_port_reset(dev, port, &portstatus) < 0) {
  995. printf("cannot reset port %i!?\n", port + 1);
  996. return;
  997. }
  998. wait_ms(200);
  999. /* Allocate a new device struct for it */
  1000. usb = usb_alloc_new_device();
  1001. if (portstatus & USB_PORT_STAT_HIGH_SPEED)
  1002. usb->speed = USB_SPEED_HIGH;
  1003. else if (portstatus & USB_PORT_STAT_LOW_SPEED)
  1004. usb->speed = USB_SPEED_LOW;
  1005. else
  1006. usb->speed = USB_SPEED_FULL;
  1007. dev->children[port] = usb;
  1008. usb->parent = dev;
  1009. /* Run it through the hoops (find a driver, etc) */
  1010. if (usb_new_device(usb)) {
  1011. /* Woops, disable the port */
  1012. USB_HUB_PRINTF("hub: disabling port %d\n", port + 1);
  1013. usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_ENABLE);
  1014. }
  1015. }
  1016. int usb_hub_configure(struct usb_device *dev)
  1017. {
  1018. unsigned char buffer[USB_BUFSIZ], *bitmap;
  1019. struct usb_hub_descriptor *descriptor;
  1020. struct usb_hub_status *hubsts;
  1021. int i;
  1022. struct usb_hub_device *hub;
  1023. /* "allocate" Hub device */
  1024. hub = usb_hub_allocate();
  1025. if (hub == NULL)
  1026. return -1;
  1027. hub->pusb_dev = dev;
  1028. /* Get the the hub descriptor */
  1029. if (usb_get_hub_descriptor(dev, buffer, 4) < 0) {
  1030. USB_HUB_PRINTF("usb_hub_configure: failed to get hub " \
  1031. "descriptor, giving up %lX\n", dev->status);
  1032. return -1;
  1033. }
  1034. descriptor = (struct usb_hub_descriptor *)buffer;
  1035. /* silence compiler warning if USB_BUFSIZ is > 256 [= sizeof(char)] */
  1036. i = descriptor->bLength;
  1037. if (i > USB_BUFSIZ) {
  1038. USB_HUB_PRINTF("usb_hub_configure: failed to get hub " \
  1039. "descriptor - too long: %d\n",
  1040. descriptor->bLength);
  1041. return -1;
  1042. }
  1043. if (usb_get_hub_descriptor(dev, buffer, descriptor->bLength) < 0) {
  1044. USB_HUB_PRINTF("usb_hub_configure: failed to get hub " \
  1045. "descriptor 2nd giving up %lX\n", dev->status);
  1046. return -1;
  1047. }
  1048. memcpy((unsigned char *)&hub->desc, buffer, descriptor->bLength);
  1049. /* adjust 16bit values */
  1050. hub->desc.wHubCharacteristics =
  1051. le16_to_cpu(descriptor->wHubCharacteristics);
  1052. /* set the bitmap */
  1053. bitmap = (unsigned char *)&hub->desc.DeviceRemovable[0];
  1054. /* devices not removable by default */
  1055. memset(bitmap, 0xff, (USB_MAXCHILDREN+1+7)/8);
  1056. bitmap = (unsigned char *)&hub->desc.PortPowerCtrlMask[0];
  1057. memset(bitmap, 0xff, (USB_MAXCHILDREN+1+7)/8); /* PowerMask = 1B */
  1058. for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++)
  1059. hub->desc.DeviceRemovable[i] = descriptor->DeviceRemovable[i];
  1060. for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++)
  1061. hub->desc.PortPowerCtrlMask[i] = descriptor->PortPowerCtrlMask[i];
  1062. dev->maxchild = descriptor->bNbrPorts;
  1063. USB_HUB_PRINTF("%d ports detected\n", dev->maxchild);
  1064. switch (hub->desc.wHubCharacteristics & HUB_CHAR_LPSM) {
  1065. case 0x00:
  1066. USB_HUB_PRINTF("ganged power switching\n");
  1067. break;
  1068. case 0x01:
  1069. USB_HUB_PRINTF("individual port power switching\n");
  1070. break;
  1071. case 0x02:
  1072. case 0x03:
  1073. USB_HUB_PRINTF("unknown reserved power switching mode\n");
  1074. break;
  1075. }
  1076. if (hub->desc.wHubCharacteristics & HUB_CHAR_COMPOUND)
  1077. USB_HUB_PRINTF("part of a compound device\n");
  1078. else
  1079. USB_HUB_PRINTF("standalone hub\n");
  1080. switch (hub->desc.wHubCharacteristics & HUB_CHAR_OCPM) {
  1081. case 0x00:
  1082. USB_HUB_PRINTF("global over-current protection\n");
  1083. break;
  1084. case 0x08:
  1085. USB_HUB_PRINTF("individual port over-current protection\n");
  1086. break;
  1087. case 0x10:
  1088. case 0x18:
  1089. USB_HUB_PRINTF("no over-current protection\n");
  1090. break;
  1091. }
  1092. USB_HUB_PRINTF("power on to power good time: %dms\n",
  1093. descriptor->bPwrOn2PwrGood * 2);
  1094. USB_HUB_PRINTF("hub controller current requirement: %dmA\n",
  1095. descriptor->bHubContrCurrent);
  1096. for (i = 0; i < dev->maxchild; i++)
  1097. USB_HUB_PRINTF("port %d is%s removable\n", i + 1,
  1098. hub->desc.DeviceRemovable[(i + 1) / 8] & \
  1099. (1 << ((i + 1) % 8)) ? " not" : "");
  1100. if (sizeof(struct usb_hub_status) > USB_BUFSIZ) {
  1101. USB_HUB_PRINTF("usb_hub_configure: failed to get Status - " \
  1102. "too long: %d\n", descriptor->bLength);
  1103. return -1;
  1104. }
  1105. if (usb_get_hub_status(dev, buffer) < 0) {
  1106. USB_HUB_PRINTF("usb_hub_configure: failed to get Status %lX\n",
  1107. dev->status);
  1108. return -1;
  1109. }
  1110. hubsts = (struct usb_hub_status *)buffer;
  1111. USB_HUB_PRINTF("get_hub_status returned status %X, change %X\n",
  1112. le16_to_cpu(hubsts->wHubStatus),
  1113. le16_to_cpu(hubsts->wHubChange));
  1114. USB_HUB_PRINTF("local power source is %s\n",
  1115. (le16_to_cpu(hubsts->wHubStatus) & HUB_STATUS_LOCAL_POWER) ? \
  1116. "lost (inactive)" : "good");
  1117. USB_HUB_PRINTF("%sover-current condition exists\n",
  1118. (le16_to_cpu(hubsts->wHubStatus) & HUB_STATUS_OVERCURRENT) ? \
  1119. "" : "no ");
  1120. usb_hub_power_on(hub);
  1121. for (i = 0; i < dev->maxchild; i++) {
  1122. struct usb_port_status portsts;
  1123. unsigned short portstatus, portchange;
  1124. if (usb_get_port_status(dev, i + 1, &portsts) < 0) {
  1125. USB_HUB_PRINTF("get_port_status failed\n");
  1126. continue;
  1127. }
  1128. portstatus = le16_to_cpu(portsts.wPortStatus);
  1129. portchange = le16_to_cpu(portsts.wPortChange);
  1130. USB_HUB_PRINTF("Port %d Status %X Change %X\n",
  1131. i + 1, portstatus, portchange);
  1132. if (portchange & USB_PORT_STAT_C_CONNECTION) {
  1133. USB_HUB_PRINTF("port %d connection change\n", i + 1);
  1134. usb_hub_port_connect_change(dev, i);
  1135. }
  1136. if (portchange & USB_PORT_STAT_C_ENABLE) {
  1137. USB_HUB_PRINTF("port %d enable change, status %x\n",
  1138. i + 1, portstatus);
  1139. usb_clear_port_feature(dev, i + 1,
  1140. USB_PORT_FEAT_C_ENABLE);
  1141. /* EM interference sometimes causes bad shielded USB
  1142. * devices to be shutdown by the hub, this hack enables
  1143. * them again. Works at least with mouse driver */
  1144. if (!(portstatus & USB_PORT_STAT_ENABLE) &&
  1145. (portstatus & USB_PORT_STAT_CONNECTION) &&
  1146. ((dev->children[i]))) {
  1147. USB_HUB_PRINTF("already running port %i " \
  1148. "disabled by hub (EMI?), " \
  1149. "re-enabling...\n", i + 1);
  1150. usb_hub_port_connect_change(dev, i);
  1151. }
  1152. }
  1153. if (portstatus & USB_PORT_STAT_SUSPEND) {
  1154. USB_HUB_PRINTF("port %d suspend change\n", i + 1);
  1155. usb_clear_port_feature(dev, i + 1,
  1156. USB_PORT_FEAT_SUSPEND);
  1157. }
  1158. if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
  1159. USB_HUB_PRINTF("port %d over-current change\n", i + 1);
  1160. usb_clear_port_feature(dev, i + 1,
  1161. USB_PORT_FEAT_C_OVER_CURRENT);
  1162. usb_hub_power_on(hub);
  1163. }
  1164. if (portchange & USB_PORT_STAT_C_RESET) {
  1165. USB_HUB_PRINTF("port %d reset change\n", i + 1);
  1166. usb_clear_port_feature(dev, i + 1,
  1167. USB_PORT_FEAT_C_RESET);
  1168. }
  1169. } /* end for i all ports */
  1170. return 0;
  1171. }
  1172. int usb_hub_probe(struct usb_device *dev, int ifnum)
  1173. {
  1174. struct usb_interface *iface;
  1175. struct usb_endpoint_descriptor *ep;
  1176. int ret;
  1177. iface = &dev->config.if_desc[ifnum];
  1178. /* Is it a hub? */
  1179. if (iface->desc.bInterfaceClass != USB_CLASS_HUB)
  1180. return 0;
  1181. /* Some hubs have a subclass of 1, which AFAICT according to the */
  1182. /* specs is not defined, but it works */
  1183. if ((iface->desc.bInterfaceSubClass != 0) &&
  1184. (iface->desc.bInterfaceSubClass != 1))
  1185. return 0;
  1186. /* Multiple endpoints? What kind of mutant ninja-hub is this? */
  1187. if (iface->desc.bNumEndpoints != 1)
  1188. return 0;
  1189. ep = &iface->ep_desc[0];
  1190. /* Output endpoint? Curiousier and curiousier.. */
  1191. if (!(ep->bEndpointAddress & USB_DIR_IN))
  1192. return 0;
  1193. /* If it's not an interrupt endpoint, we'd better punt! */
  1194. if ((ep->bmAttributes & 3) != 3)
  1195. return 0;
  1196. /* We found a hub */
  1197. USB_HUB_PRINTF("USB hub found\n");
  1198. ret = usb_hub_configure(dev);
  1199. return ret;
  1200. }
  1201. /* EOF */