usb.c 38 KB

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