usb.c 32 KB

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