usb.c 35 KB

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