usb.c 38 KB

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