usb.c 32 KB

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