usb.c 39 KB

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