usb.c 35 KB

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