usb.c 39 KB

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