usb.c 39 KB

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