usb.c 36 KB

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