usb.c 39 KB

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