usb.c 37 KB

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