usb.c 38 KB

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