usb.c 39 KB

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