usb.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  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. /* silence compiler warning if USB_BUFSIZ is > 256 [= sizeof(char)] */
  815. i = descriptor->bLength;
  816. if (i > USB_BUFSIZ) {
  817. USB_HUB_PRINTF("usb_hub_configure: failed to get hub descriptor - too long: %d\N",
  818. descriptor->bLength);
  819. return -1;
  820. }
  821. if (usb_get_hub_descriptor(dev, buffer, descriptor->bLength) < 0) {
  822. USB_HUB_PRINTF("usb_hub_configure: failed to get hub descriptor 2nd giving up %lX\n",dev->status);
  823. return -1;
  824. }
  825. memcpy((unsigned char *)&hub->desc,buffer,descriptor->bLength);
  826. /* adjust 16bit values */
  827. hub->desc.wHubCharacteristics=swap_16(descriptor->wHubCharacteristics);
  828. /* set the bitmap */
  829. bitmap=(unsigned char *)&hub->desc.DeviceRemovable[0];
  830. memset(bitmap,0xff,(USB_MAXCHILDREN+1+7)/8); /* devices not removable by default */
  831. bitmap=(unsigned char *)&hub->desc.PortPowerCtrlMask[0];
  832. memset(bitmap,0xff,(USB_MAXCHILDREN+1+7)/8); /* PowerMask = 1B */
  833. for(i=0;i<((hub->desc.bNbrPorts + 1 + 7)/8);i++) {
  834. hub->desc.DeviceRemovable[i]=descriptor->DeviceRemovable[i];
  835. }
  836. for(i=0;i<((hub->desc.bNbrPorts + 1 + 7)/8);i++) {
  837. hub->desc.DeviceRemovable[i]=descriptor->PortPowerCtrlMask[i];
  838. }
  839. dev->maxchild = descriptor->bNbrPorts;
  840. USB_HUB_PRINTF("%d ports detected\n", dev->maxchild);
  841. switch (hub->desc.wHubCharacteristics & HUB_CHAR_LPSM) {
  842. case 0x00:
  843. USB_HUB_PRINTF("ganged power switching\n");
  844. break;
  845. case 0x01:
  846. USB_HUB_PRINTF("individual port power switching\n");
  847. break;
  848. case 0x02:
  849. case 0x03:
  850. USB_HUB_PRINTF("unknown reserved power switching mode\n");
  851. break;
  852. }
  853. if (hub->desc.wHubCharacteristics & HUB_CHAR_COMPOUND)
  854. USB_HUB_PRINTF("part of a compound device\n");
  855. else
  856. USB_HUB_PRINTF("standalone hub\n");
  857. switch (hub->desc.wHubCharacteristics & HUB_CHAR_OCPM) {
  858. case 0x00:
  859. USB_HUB_PRINTF("global over-current protection\n");
  860. break;
  861. case 0x08:
  862. USB_HUB_PRINTF("individual port over-current protection\n");
  863. break;
  864. case 0x10:
  865. case 0x18:
  866. USB_HUB_PRINTF("no over-current protection\n");
  867. break;
  868. }
  869. USB_HUB_PRINTF("power on to power good time: %dms\n", descriptor->bPwrOn2PwrGood * 2);
  870. USB_HUB_PRINTF("hub controller current requirement: %dmA\n", descriptor->bHubContrCurrent);
  871. for (i = 0; i < dev->maxchild; i++)
  872. USB_HUB_PRINTF("port %d is%s removable\n", i + 1,
  873. hub->desc.DeviceRemovable[(i + 1)/8] & (1 << ((i + 1)%8)) ? " not" : "");
  874. if (sizeof(struct usb_hub_status) > USB_BUFSIZ) {
  875. USB_HUB_PRINTF("usb_hub_configure: failed to get Status - too long: %d\n",
  876. descriptor->bLength);
  877. return -1;
  878. }
  879. if (usb_get_hub_status(dev, buffer) < 0) {
  880. USB_HUB_PRINTF("usb_hub_configure: failed to get Status %lX\n",dev->status);
  881. return -1;
  882. }
  883. hubsts = (struct usb_hub_status *)buffer;
  884. USB_HUB_PRINTF("get_hub_status returned status %X, change %X\n",
  885. swap_16(hubsts->wHubStatus),swap_16(hubsts->wHubChange));
  886. USB_HUB_PRINTF("local power source is %s\n",
  887. (swap_16(hubsts->wHubStatus) & HUB_STATUS_LOCAL_POWER) ? "lost (inactive)" : "good");
  888. USB_HUB_PRINTF("%sover-current condition exists\n",
  889. (swap_16(hubsts->wHubStatus) & HUB_STATUS_OVERCURRENT) ? "" : "no ");
  890. usb_hub_power_on(hub);
  891. for (i = 0; i < dev->maxchild; i++) {
  892. struct usb_port_status portsts;
  893. unsigned short portstatus, portchange;
  894. if (usb_get_port_status(dev, i + 1, &portsts) < 0) {
  895. USB_HUB_PRINTF("get_port_status failed\n");
  896. continue;
  897. }
  898. portstatus = swap_16(portsts.wPortStatus);
  899. portchange = swap_16(portsts.wPortChange);
  900. USB_HUB_PRINTF("Port %d Status %X Change %X\n",i+1,portstatus,portchange);
  901. if (portchange & USB_PORT_STAT_C_CONNECTION) {
  902. USB_HUB_PRINTF("port %d connection change\n", i + 1);
  903. usb_hub_port_connect_change(dev, i);
  904. }
  905. if (portchange & USB_PORT_STAT_C_ENABLE) {
  906. USB_HUB_PRINTF("port %d enable change, status %x\n", i + 1, portstatus);
  907. usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_C_ENABLE);
  908. /* EM interference sometimes causes bad shielded USB devices to
  909. * be shutdown by the hub, this hack enables them again.
  910. * Works at least with mouse driver */
  911. if (!(portstatus & USB_PORT_STAT_ENABLE) &&
  912. (portstatus & USB_PORT_STAT_CONNECTION) && (dev->children[i])) {
  913. USB_HUB_PRINTF("already running port %i disabled by hub (EMI?), re-enabling...\n",
  914. i + 1);
  915. usb_hub_port_connect_change(dev, i);
  916. }
  917. }
  918. if (portstatus & USB_PORT_STAT_SUSPEND) {
  919. USB_HUB_PRINTF("port %d suspend change\n", i + 1);
  920. usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_SUSPEND);
  921. }
  922. if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
  923. USB_HUB_PRINTF("port %d over-current change\n", i + 1);
  924. usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_C_OVER_CURRENT);
  925. usb_hub_power_on(hub);
  926. }
  927. if (portchange & USB_PORT_STAT_C_RESET) {
  928. USB_HUB_PRINTF("port %d reset change\n", i + 1);
  929. usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_C_RESET);
  930. }
  931. } /* end for i all ports */
  932. return 0;
  933. }
  934. int usb_hub_probe(struct usb_device *dev, int ifnum)
  935. {
  936. struct usb_interface_descriptor *iface;
  937. struct usb_endpoint_descriptor *ep;
  938. int ret;
  939. iface = &dev->config.if_desc[ifnum];
  940. /* Is it a hub? */
  941. if (iface->bInterfaceClass != USB_CLASS_HUB)
  942. return 0;
  943. /* Some hubs have a subclass of 1, which AFAICT according to the */
  944. /* specs is not defined, but it works */
  945. if ((iface->bInterfaceSubClass != 0) &&
  946. (iface->bInterfaceSubClass != 1))
  947. return 0;
  948. /* Multiple endpoints? What kind of mutant ninja-hub is this? */
  949. if (iface->bNumEndpoints != 1)
  950. return 0;
  951. ep = &iface->ep_desc[0];
  952. /* Output endpoint? Curiousier and curiousier.. */
  953. if (!(ep->bEndpointAddress & USB_DIR_IN))
  954. return 0;
  955. /* If it's not an interrupt endpoint, we'd better punt! */
  956. if ((ep->bmAttributes & 3) != 3)
  957. return 0;
  958. /* We found a hub */
  959. USB_HUB_PRINTF("USB hub found\n");
  960. ret=usb_hub_configure(dev);
  961. return ret;
  962. }
  963. #endif /* (CONFIG_COMMANDS & CFG_CMD_USB) */
  964. /* EOF */