usb.c 36 KB

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