rh.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /*
  2. * Wireless USB Host Controller
  3. * Root Hub operations
  4. *
  5. *
  6. * Copyright (C) 2005-2006 Intel Corporation
  7. * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License version
  11. * 2 as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  21. * 02110-1301, USA.
  22. *
  23. *
  24. * We fake a root hub that has fake ports (as many as simultaneous
  25. * devices the Wireless USB Host Controller can deal with). For each
  26. * port we keep an state in @wusbhc->port[index] identical to the one
  27. * specified in the USB2.0[ch11] spec and some extra device
  28. * information that complements the one in 'struct usb_device' (as
  29. * this lacs a hcpriv pointer).
  30. *
  31. * Note this is common to WHCI and HWA host controllers.
  32. *
  33. * Through here we enable most of the state changes that the USB stack
  34. * will use to connect or disconnect devices. We need to do some
  35. * forced adaptation of Wireless USB device states vs. wired:
  36. *
  37. * USB: WUSB:
  38. *
  39. * Port Powered-off port slot n/a
  40. * Powered-on port slot available
  41. * Disconnected port slot available
  42. * Connected port slot assigned device
  43. * device sent DN_Connect
  44. * device was authenticated
  45. * Enabled device is authenticated, transitioned
  46. * from unauth -> auth -> default address
  47. * -> enabled
  48. * Reset disconnect
  49. * Disable disconnect
  50. *
  51. * This maps the standard USB port states with the WUSB device states
  52. * so we can fake ports without having to modify the USB stack.
  53. *
  54. * FIXME: this process will change in the future
  55. *
  56. *
  57. * ENTRY POINTS
  58. *
  59. * Our entry points into here are, as in hcd.c, the USB stack root hub
  60. * ops defined in the usb_hcd struct:
  61. *
  62. * wusbhc_rh_status_data() Provide hub and port status data bitmap
  63. *
  64. * wusbhc_rh_control() Execution of all the major requests
  65. * you can do to a hub (Set|Clear
  66. * features, get descriptors, status, etc).
  67. *
  68. * wusbhc_rh_[suspend|resume]() That
  69. *
  70. * wusbhc_rh_start_port_reset() ??? unimplemented
  71. */
  72. #include "wusbhc.h"
  73. #define D_LOCAL 0
  74. #include <linux/uwb/debug.h>
  75. /*
  76. * Reset a fake port
  77. *
  78. * This can be called to reset a port from any other state or to reset
  79. * it when connecting. In Wireless USB they are different; when doing
  80. * a new connect that involves going over the authentication. When
  81. * just reseting, its a different story.
  82. *
  83. * The Linux USB stack resets a port twice before it considers it
  84. * enabled, so we have to detect and ignore that.
  85. *
  86. * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
  87. *
  88. * Supposedly we are the only thread accesing @wusbhc->port; in any
  89. * case, maybe we should move the mutex locking from
  90. * wusbhc_devconnect_auth() to here.
  91. *
  92. * @port_idx refers to the wusbhc's port index, not the USB port number
  93. */
  94. static int wusbhc_rh_port_reset(struct wusbhc *wusbhc, u8 port_idx)
  95. {
  96. int result = 0;
  97. struct wusb_port *port = wusb_port_by_idx(wusbhc, port_idx);
  98. d_fnstart(3, wusbhc->dev, "(wusbhc %p port_idx %u)\n",
  99. wusbhc, port_idx);
  100. if (port->reset_count == 0) {
  101. wusbhc_devconnect_auth(wusbhc, port_idx);
  102. port->reset_count++;
  103. } else if (port->reset_count == 1)
  104. /* see header */
  105. d_printf(2, wusbhc->dev, "Ignoring second reset on port_idx "
  106. "%u\n", port_idx);
  107. else
  108. result = wusbhc_dev_reset(wusbhc, port_idx);
  109. d_fnend(3, wusbhc->dev, "(wusbhc %p port_idx %u) = %d\n",
  110. wusbhc, port_idx, result);
  111. return result;
  112. }
  113. /*
  114. * Return the hub change status bitmap
  115. *
  116. * The bits in the change status bitmap are cleared when a
  117. * ClearPortFeature request is issued (USB2.0[11.12.3,11.12.4].
  118. *
  119. * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
  120. *
  121. * WARNING!! This gets called from atomic context; we cannot get the
  122. * mutex--the only race condition we can find is some bit
  123. * changing just after we copy it, which shouldn't be too
  124. * big of a problem [and we can't make it an spinlock
  125. * because other parts need to take it and sleep] .
  126. *
  127. * @usb_hcd is refcounted, so it won't dissapear under us
  128. * and before killing a host, the polling of the root hub
  129. * would be stopped anyway.
  130. */
  131. int wusbhc_rh_status_data(struct usb_hcd *usb_hcd, char *_buf)
  132. {
  133. struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
  134. size_t cnt, size;
  135. unsigned long *buf = (unsigned long *) _buf;
  136. d_fnstart(1, wusbhc->dev, "(wusbhc %p)\n", wusbhc);
  137. /* WE DON'T LOCK, see comment */
  138. size = wusbhc->ports_max + 1 /* hub bit */;
  139. size = (size + 8 - 1) / 8; /* round to bytes */
  140. for (cnt = 0; cnt < wusbhc->ports_max; cnt++)
  141. if (wusb_port_by_idx(wusbhc, cnt)->change)
  142. set_bit(cnt + 1, buf);
  143. else
  144. clear_bit(cnt + 1, buf);
  145. d_fnend(1, wusbhc->dev, "(wusbhc %p) %u, buffer:\n", wusbhc, (int)size);
  146. d_dump(1, wusbhc->dev, _buf, size);
  147. return size;
  148. }
  149. EXPORT_SYMBOL_GPL(wusbhc_rh_status_data);
  150. /*
  151. * Return the hub's desciptor
  152. *
  153. * NOTE: almost cut and paste from ehci-hub.c
  154. *
  155. * @wusbhc is assumed referenced and @wusbhc->mutex unlocked
  156. */
  157. static int wusbhc_rh_get_hub_descr(struct wusbhc *wusbhc, u16 wValue,
  158. u16 wIndex,
  159. struct usb_hub_descriptor *descr,
  160. u16 wLength)
  161. {
  162. u16 temp = 1 + (wusbhc->ports_max / 8);
  163. u8 length = 7 + 2 * temp;
  164. if (wLength < length)
  165. return -ENOSPC;
  166. descr->bDescLength = 7 + 2 * temp;
  167. descr->bDescriptorType = 0x29; /* HUB type */
  168. descr->bNbrPorts = wusbhc->ports_max;
  169. descr->wHubCharacteristics = cpu_to_le16(
  170. 0x00 /* All ports power at once */
  171. | 0x00 /* not part of compound device */
  172. | 0x10 /* No overcurrent protection */
  173. | 0x00 /* 8 FS think time FIXME ?? */
  174. | 0x00); /* No port indicators */
  175. descr->bPwrOn2PwrGood = 0;
  176. descr->bHubContrCurrent = 0;
  177. /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
  178. memset(&descr->bitmap[0], 0, temp);
  179. memset(&descr->bitmap[temp], 0xff, temp);
  180. return 0;
  181. }
  182. /*
  183. * Clear a hub feature
  184. *
  185. * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
  186. *
  187. * Nothing to do, so no locking needed ;)
  188. */
  189. static int wusbhc_rh_clear_hub_feat(struct wusbhc *wusbhc, u16 feature)
  190. {
  191. int result;
  192. struct device *dev = wusbhc->dev;
  193. d_fnstart(4, dev, "(%p, feature 0x%04u)\n", wusbhc, feature);
  194. switch (feature) {
  195. case C_HUB_LOCAL_POWER:
  196. /* FIXME: maybe plug bit 0 to the power input status,
  197. * if any?
  198. * see wusbhc_rh_get_hub_status() */
  199. case C_HUB_OVER_CURRENT:
  200. result = 0;
  201. break;
  202. default:
  203. result = -EPIPE;
  204. }
  205. d_fnend(4, dev, "(%p, feature 0x%04u), %d\n", wusbhc, feature, result);
  206. return result;
  207. }
  208. /*
  209. * Return hub status (it is always zero...)
  210. *
  211. * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
  212. *
  213. * Nothing to do, so no locking needed ;)
  214. */
  215. static int wusbhc_rh_get_hub_status(struct wusbhc *wusbhc, u32 *buf,
  216. u16 wLength)
  217. {
  218. /* FIXME: maybe plug bit 0 to the power input status (if any)? */
  219. *buf = 0;
  220. return 0;
  221. }
  222. /*
  223. * Set a port feature
  224. *
  225. * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
  226. */
  227. static int wusbhc_rh_set_port_feat(struct wusbhc *wusbhc, u16 feature,
  228. u8 selector, u8 port_idx)
  229. {
  230. int result = -EINVAL;
  231. struct device *dev = wusbhc->dev;
  232. d_fnstart(4, dev, "(feat 0x%04u, selector 0x%u, port_idx %d)\n",
  233. feature, selector, port_idx);
  234. if (port_idx > wusbhc->ports_max)
  235. goto error;
  236. switch (feature) {
  237. /* According to USB2.0[11.24.2.13]p2, these features
  238. * are not required to be implemented. */
  239. case USB_PORT_FEAT_C_OVER_CURRENT:
  240. case USB_PORT_FEAT_C_ENABLE:
  241. case USB_PORT_FEAT_C_SUSPEND:
  242. case USB_PORT_FEAT_C_CONNECTION:
  243. case USB_PORT_FEAT_C_RESET:
  244. result = 0;
  245. break;
  246. case USB_PORT_FEAT_POWER:
  247. /* No such thing, but we fake it works */
  248. mutex_lock(&wusbhc->mutex);
  249. wusb_port_by_idx(wusbhc, port_idx)->status |= USB_PORT_STAT_POWER;
  250. mutex_unlock(&wusbhc->mutex);
  251. result = 0;
  252. break;
  253. case USB_PORT_FEAT_RESET:
  254. result = wusbhc_rh_port_reset(wusbhc, port_idx);
  255. break;
  256. case USB_PORT_FEAT_ENABLE:
  257. case USB_PORT_FEAT_SUSPEND:
  258. dev_err(dev, "(port_idx %d) set feat %d/%d UNIMPLEMENTED\n",
  259. port_idx, feature, selector);
  260. result = -ENOSYS;
  261. break;
  262. default:
  263. dev_err(dev, "(port_idx %d) set feat %d/%d UNKNOWN\n",
  264. port_idx, feature, selector);
  265. result = -EPIPE;
  266. break;
  267. }
  268. error:
  269. d_fnend(4, dev, "(feat 0x%04u, selector 0x%u, port_idx %d) = %d\n",
  270. feature, selector, port_idx, result);
  271. return result;
  272. }
  273. /*
  274. * Clear a port feature...
  275. *
  276. * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
  277. */
  278. static int wusbhc_rh_clear_port_feat(struct wusbhc *wusbhc, u16 feature,
  279. u8 selector, u8 port_idx)
  280. {
  281. int result = -EINVAL;
  282. struct device *dev = wusbhc->dev;
  283. d_fnstart(4, dev, "(wusbhc %p feat 0x%04x selector %d port_idx %d)\n",
  284. wusbhc, feature, selector, port_idx);
  285. if (port_idx > wusbhc->ports_max)
  286. goto error;
  287. mutex_lock(&wusbhc->mutex);
  288. result = 0;
  289. switch (feature) {
  290. case USB_PORT_FEAT_POWER: /* fake port always on */
  291. /* According to USB2.0[11.24.2.7.1.4], no need to implement? */
  292. case USB_PORT_FEAT_C_OVER_CURRENT:
  293. break;
  294. case USB_PORT_FEAT_C_RESET:
  295. wusb_port_by_idx(wusbhc, port_idx)->change &= ~USB_PORT_STAT_C_RESET;
  296. break;
  297. case USB_PORT_FEAT_C_CONNECTION:
  298. wusb_port_by_idx(wusbhc, port_idx)->change &= ~USB_PORT_STAT_C_CONNECTION;
  299. break;
  300. case USB_PORT_FEAT_ENABLE:
  301. __wusbhc_dev_disable(wusbhc, port_idx);
  302. break;
  303. case USB_PORT_FEAT_C_ENABLE:
  304. wusb_port_by_idx(wusbhc, port_idx)->change &= ~USB_PORT_STAT_C_ENABLE;
  305. break;
  306. case USB_PORT_FEAT_SUSPEND:
  307. case USB_PORT_FEAT_C_SUSPEND:
  308. case 0xffff: /* ??? FIXME */
  309. dev_err(dev, "(port_idx %d) Clear feat %d/%d UNIMPLEMENTED\n",
  310. port_idx, feature, selector);
  311. /* dump_stack(); */
  312. result = -ENOSYS;
  313. break;
  314. default:
  315. dev_err(dev, "(port_idx %d) Clear feat %d/%d UNKNOWN\n",
  316. port_idx, feature, selector);
  317. result = -EPIPE;
  318. break;
  319. }
  320. mutex_unlock(&wusbhc->mutex);
  321. error:
  322. d_fnend(4, dev, "(wusbhc %p feat 0x%04x selector %d port_idx %d) = "
  323. "%d\n", wusbhc, feature, selector, port_idx, result);
  324. return result;
  325. }
  326. /*
  327. * Return the port's status
  328. *
  329. * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
  330. */
  331. static int wusbhc_rh_get_port_status(struct wusbhc *wusbhc, u16 port_idx,
  332. u32 *_buf, u16 wLength)
  333. {
  334. int result = -EINVAL;
  335. u16 *buf = (u16 *) _buf;
  336. d_fnstart(1, wusbhc->dev, "(wusbhc %p port_idx %u wLength %u)\n",
  337. wusbhc, port_idx, wLength);
  338. if (port_idx > wusbhc->ports_max)
  339. goto error;
  340. mutex_lock(&wusbhc->mutex);
  341. buf[0] = cpu_to_le16(wusb_port_by_idx(wusbhc, port_idx)->status);
  342. buf[1] = cpu_to_le16(wusb_port_by_idx(wusbhc, port_idx)->change);
  343. result = 0;
  344. mutex_unlock(&wusbhc->mutex);
  345. error:
  346. d_fnend(1, wusbhc->dev, "(wusbhc %p) = %d, buffer:\n", wusbhc, result);
  347. d_dump(1, wusbhc->dev, _buf, wLength);
  348. return result;
  349. }
  350. /*
  351. * Entry point for Root Hub operations
  352. *
  353. * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
  354. */
  355. int wusbhc_rh_control(struct usb_hcd *usb_hcd, u16 reqntype, u16 wValue,
  356. u16 wIndex, char *buf, u16 wLength)
  357. {
  358. int result = -ENOSYS;
  359. struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
  360. switch (reqntype) {
  361. case GetHubDescriptor:
  362. result = wusbhc_rh_get_hub_descr(
  363. wusbhc, wValue, wIndex,
  364. (struct usb_hub_descriptor *) buf, wLength);
  365. break;
  366. case ClearHubFeature:
  367. result = wusbhc_rh_clear_hub_feat(wusbhc, wValue);
  368. break;
  369. case GetHubStatus:
  370. result = wusbhc_rh_get_hub_status(wusbhc, (u32 *)buf, wLength);
  371. break;
  372. case SetPortFeature:
  373. result = wusbhc_rh_set_port_feat(wusbhc, wValue, wIndex >> 8,
  374. (wIndex & 0xff) - 1);
  375. break;
  376. case ClearPortFeature:
  377. result = wusbhc_rh_clear_port_feat(wusbhc, wValue, wIndex >> 8,
  378. (wIndex & 0xff) - 1);
  379. break;
  380. case GetPortStatus:
  381. result = wusbhc_rh_get_port_status(wusbhc, wIndex - 1,
  382. (u32 *)buf, wLength);
  383. break;
  384. case SetHubFeature:
  385. default:
  386. dev_err(wusbhc->dev, "%s (%p [%p], %x, %x, %x, %p, %x) "
  387. "UNIMPLEMENTED\n", __func__, usb_hcd, wusbhc, reqntype,
  388. wValue, wIndex, buf, wLength);
  389. /* dump_stack(); */
  390. result = -ENOSYS;
  391. }
  392. return result;
  393. }
  394. EXPORT_SYMBOL_GPL(wusbhc_rh_control);
  395. int wusbhc_rh_suspend(struct usb_hcd *usb_hcd)
  396. {
  397. struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
  398. dev_err(wusbhc->dev, "%s (%p [%p]) UNIMPLEMENTED\n", __func__,
  399. usb_hcd, wusbhc);
  400. /* dump_stack(); */
  401. return -ENOSYS;
  402. }
  403. EXPORT_SYMBOL_GPL(wusbhc_rh_suspend);
  404. int wusbhc_rh_resume(struct usb_hcd *usb_hcd)
  405. {
  406. struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
  407. dev_err(wusbhc->dev, "%s (%p [%p]) UNIMPLEMENTED\n", __func__,
  408. usb_hcd, wusbhc);
  409. /* dump_stack(); */
  410. return -ENOSYS;
  411. }
  412. EXPORT_SYMBOL_GPL(wusbhc_rh_resume);
  413. int wusbhc_rh_start_port_reset(struct usb_hcd *usb_hcd, unsigned port_idx)
  414. {
  415. struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
  416. dev_err(wusbhc->dev, "%s (%p [%p], port_idx %u) UNIMPLEMENTED\n",
  417. __func__, usb_hcd, wusbhc, port_idx);
  418. WARN_ON(1);
  419. return -ENOSYS;
  420. }
  421. EXPORT_SYMBOL_GPL(wusbhc_rh_start_port_reset);
  422. static void wusb_port_init(struct wusb_port *port)
  423. {
  424. port->status |= USB_PORT_STAT_HIGH_SPEED;
  425. }
  426. /*
  427. * Alloc fake port specific fields and status.
  428. */
  429. int wusbhc_rh_create(struct wusbhc *wusbhc)
  430. {
  431. int result = -ENOMEM;
  432. size_t port_size, itr;
  433. port_size = wusbhc->ports_max * sizeof(wusbhc->port[0]);
  434. wusbhc->port = kzalloc(port_size, GFP_KERNEL);
  435. if (wusbhc->port == NULL)
  436. goto error_port_alloc;
  437. for (itr = 0; itr < wusbhc->ports_max; itr++)
  438. wusb_port_init(&wusbhc->port[itr]);
  439. result = 0;
  440. error_port_alloc:
  441. return result;
  442. }
  443. void wusbhc_rh_destroy(struct wusbhc *wusbhc)
  444. {
  445. kfree(wusbhc->port);
  446. }