rh.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. /*
  74. * Reset a fake port
  75. *
  76. * Using a Reset Device IE is too heavyweight as it causes the device
  77. * to enter the UnConnected state and leave the cluster, this can mean
  78. * that when the device reconnects it is connected to a different fake
  79. * port.
  80. *
  81. * Instead, reset authenticated devices with a SetAddress(0), followed
  82. * by a SetAddresss(AuthAddr).
  83. *
  84. * For unauthenticated devices just pretend to reset but do nothing.
  85. * If the device initialization continues to fail it will eventually
  86. * time out after TrustTimeout and enter the UnConnected state.
  87. *
  88. * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
  89. *
  90. * Supposedly we are the only thread accesing @wusbhc->port; in any
  91. * case, maybe we should move the mutex locking from
  92. * wusbhc_devconnect_auth() to here.
  93. *
  94. * @port_idx refers to the wusbhc's port index, not the USB port number
  95. */
  96. static int wusbhc_rh_port_reset(struct wusbhc *wusbhc, u8 port_idx)
  97. {
  98. int result = 0;
  99. struct wusb_port *port = wusb_port_by_idx(wusbhc, port_idx);
  100. struct wusb_dev *wusb_dev = port->wusb_dev;
  101. port->status |= USB_PORT_STAT_RESET;
  102. port->change |= USB_PORT_STAT_C_RESET;
  103. if (wusb_dev->addr & WUSB_DEV_ADDR_UNAUTH)
  104. result = 0;
  105. else
  106. result = wusb_dev_update_address(wusbhc, wusb_dev);
  107. port->status &= ~USB_PORT_STAT_RESET;
  108. port->status |= USB_PORT_STAT_ENABLE;
  109. port->change |= USB_PORT_STAT_C_RESET | USB_PORT_STAT_C_ENABLE;
  110. return result;
  111. }
  112. /*
  113. * Return the hub change status bitmap
  114. *
  115. * The bits in the change status bitmap are cleared when a
  116. * ClearPortFeature request is issued (USB2.0[11.12.3,11.12.4].
  117. *
  118. * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
  119. *
  120. * WARNING!! This gets called from atomic context; we cannot get the
  121. * mutex--the only race condition we can find is some bit
  122. * changing just after we copy it, which shouldn't be too
  123. * big of a problem [and we can't make it an spinlock
  124. * because other parts need to take it and sleep] .
  125. *
  126. * @usb_hcd is refcounted, so it won't dissapear under us
  127. * and before killing a host, the polling of the root hub
  128. * would be stopped anyway.
  129. */
  130. int wusbhc_rh_status_data(struct usb_hcd *usb_hcd, char *_buf)
  131. {
  132. struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
  133. size_t cnt, size;
  134. unsigned long *buf = (unsigned long *) _buf;
  135. /* WE DON'T LOCK, see comment */
  136. size = wusbhc->ports_max + 1 /* hub bit */;
  137. size = (size + 8 - 1) / 8; /* round to bytes */
  138. for (cnt = 0; cnt < wusbhc->ports_max; cnt++)
  139. if (wusb_port_by_idx(wusbhc, cnt)->change)
  140. set_bit(cnt + 1, buf);
  141. else
  142. clear_bit(cnt + 1, buf);
  143. return size;
  144. }
  145. EXPORT_SYMBOL_GPL(wusbhc_rh_status_data);
  146. /*
  147. * Return the hub's desciptor
  148. *
  149. * NOTE: almost cut and paste from ehci-hub.c
  150. *
  151. * @wusbhc is assumed referenced and @wusbhc->mutex unlocked
  152. */
  153. static int wusbhc_rh_get_hub_descr(struct wusbhc *wusbhc, u16 wValue,
  154. u16 wIndex,
  155. struct usb_hub_descriptor *descr,
  156. u16 wLength)
  157. {
  158. u16 temp = 1 + (wusbhc->ports_max / 8);
  159. u8 length = 7 + 2 * temp;
  160. if (wLength < length)
  161. return -ENOSPC;
  162. descr->bDescLength = 7 + 2 * temp;
  163. descr->bDescriptorType = 0x29; /* HUB type */
  164. descr->bNbrPorts = wusbhc->ports_max;
  165. descr->wHubCharacteristics = cpu_to_le16(
  166. 0x00 /* All ports power at once */
  167. | 0x00 /* not part of compound device */
  168. | 0x10 /* No overcurrent protection */
  169. | 0x00 /* 8 FS think time FIXME ?? */
  170. | 0x00); /* No port indicators */
  171. descr->bPwrOn2PwrGood = 0;
  172. descr->bHubContrCurrent = 0;
  173. /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
  174. memset(&descr->bitmap[0], 0, temp);
  175. memset(&descr->bitmap[temp], 0xff, temp);
  176. return 0;
  177. }
  178. /*
  179. * Clear a hub feature
  180. *
  181. * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
  182. *
  183. * Nothing to do, so no locking needed ;)
  184. */
  185. static int wusbhc_rh_clear_hub_feat(struct wusbhc *wusbhc, u16 feature)
  186. {
  187. int result;
  188. switch (feature) {
  189. case C_HUB_LOCAL_POWER:
  190. /* FIXME: maybe plug bit 0 to the power input status,
  191. * if any?
  192. * see wusbhc_rh_get_hub_status() */
  193. case C_HUB_OVER_CURRENT:
  194. result = 0;
  195. break;
  196. default:
  197. result = -EPIPE;
  198. }
  199. return result;
  200. }
  201. /*
  202. * Return hub status (it is always zero...)
  203. *
  204. * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
  205. *
  206. * Nothing to do, so no locking needed ;)
  207. */
  208. static int wusbhc_rh_get_hub_status(struct wusbhc *wusbhc, u32 *buf,
  209. u16 wLength)
  210. {
  211. /* FIXME: maybe plug bit 0 to the power input status (if any)? */
  212. *buf = 0;
  213. return 0;
  214. }
  215. /*
  216. * Set a port feature
  217. *
  218. * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
  219. */
  220. static int wusbhc_rh_set_port_feat(struct wusbhc *wusbhc, u16 feature,
  221. u8 selector, u8 port_idx)
  222. {
  223. struct device *dev = wusbhc->dev;
  224. if (port_idx > wusbhc->ports_max)
  225. return -EINVAL;
  226. switch (feature) {
  227. /* According to USB2.0[11.24.2.13]p2, these features
  228. * are not required to be implemented. */
  229. case USB_PORT_FEAT_C_OVER_CURRENT:
  230. case USB_PORT_FEAT_C_ENABLE:
  231. case USB_PORT_FEAT_C_SUSPEND:
  232. case USB_PORT_FEAT_C_CONNECTION:
  233. case USB_PORT_FEAT_C_RESET:
  234. return 0;
  235. case USB_PORT_FEAT_POWER:
  236. /* No such thing, but we fake it works */
  237. mutex_lock(&wusbhc->mutex);
  238. wusb_port_by_idx(wusbhc, port_idx)->status |= USB_PORT_STAT_POWER;
  239. mutex_unlock(&wusbhc->mutex);
  240. return 0;
  241. case USB_PORT_FEAT_RESET:
  242. return wusbhc_rh_port_reset(wusbhc, port_idx);
  243. case USB_PORT_FEAT_ENABLE:
  244. case USB_PORT_FEAT_SUSPEND:
  245. dev_err(dev, "(port_idx %d) set feat %d/%d UNIMPLEMENTED\n",
  246. port_idx, feature, selector);
  247. return -ENOSYS;
  248. default:
  249. dev_err(dev, "(port_idx %d) set feat %d/%d UNKNOWN\n",
  250. port_idx, feature, selector);
  251. return -EPIPE;
  252. }
  253. return 0;
  254. }
  255. /*
  256. * Clear a port feature...
  257. *
  258. * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
  259. */
  260. static int wusbhc_rh_clear_port_feat(struct wusbhc *wusbhc, u16 feature,
  261. u8 selector, u8 port_idx)
  262. {
  263. int result = 0;
  264. struct device *dev = wusbhc->dev;
  265. if (port_idx > wusbhc->ports_max)
  266. return -EINVAL;
  267. mutex_lock(&wusbhc->mutex);
  268. switch (feature) {
  269. case USB_PORT_FEAT_POWER: /* fake port always on */
  270. /* According to USB2.0[11.24.2.7.1.4], no need to implement? */
  271. case USB_PORT_FEAT_C_OVER_CURRENT:
  272. break;
  273. case USB_PORT_FEAT_C_RESET:
  274. wusb_port_by_idx(wusbhc, port_idx)->change &= ~USB_PORT_STAT_C_RESET;
  275. break;
  276. case USB_PORT_FEAT_C_CONNECTION:
  277. wusb_port_by_idx(wusbhc, port_idx)->change &= ~USB_PORT_STAT_C_CONNECTION;
  278. break;
  279. case USB_PORT_FEAT_ENABLE:
  280. __wusbhc_dev_disable(wusbhc, port_idx);
  281. break;
  282. case USB_PORT_FEAT_C_ENABLE:
  283. wusb_port_by_idx(wusbhc, port_idx)->change &= ~USB_PORT_STAT_C_ENABLE;
  284. break;
  285. case USB_PORT_FEAT_SUSPEND:
  286. case USB_PORT_FEAT_C_SUSPEND:
  287. dev_err(dev, "(port_idx %d) Clear feat %d/%d UNIMPLEMENTED\n",
  288. port_idx, feature, selector);
  289. result = -ENOSYS;
  290. break;
  291. default:
  292. dev_err(dev, "(port_idx %d) Clear feat %d/%d UNKNOWN\n",
  293. port_idx, feature, selector);
  294. result = -EPIPE;
  295. break;
  296. }
  297. mutex_unlock(&wusbhc->mutex);
  298. return result;
  299. }
  300. /*
  301. * Return the port's status
  302. *
  303. * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
  304. */
  305. static int wusbhc_rh_get_port_status(struct wusbhc *wusbhc, u16 port_idx,
  306. u32 *_buf, u16 wLength)
  307. {
  308. u16 *buf = (u16 *) _buf;
  309. if (port_idx > wusbhc->ports_max)
  310. return -EINVAL;
  311. mutex_lock(&wusbhc->mutex);
  312. buf[0] = cpu_to_le16(wusb_port_by_idx(wusbhc, port_idx)->status);
  313. buf[1] = cpu_to_le16(wusb_port_by_idx(wusbhc, port_idx)->change);
  314. mutex_unlock(&wusbhc->mutex);
  315. return 0;
  316. }
  317. /*
  318. * Entry point for Root Hub operations
  319. *
  320. * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
  321. */
  322. int wusbhc_rh_control(struct usb_hcd *usb_hcd, u16 reqntype, u16 wValue,
  323. u16 wIndex, char *buf, u16 wLength)
  324. {
  325. int result = -ENOSYS;
  326. struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
  327. switch (reqntype) {
  328. case GetHubDescriptor:
  329. result = wusbhc_rh_get_hub_descr(
  330. wusbhc, wValue, wIndex,
  331. (struct usb_hub_descriptor *) buf, wLength);
  332. break;
  333. case ClearHubFeature:
  334. result = wusbhc_rh_clear_hub_feat(wusbhc, wValue);
  335. break;
  336. case GetHubStatus:
  337. result = wusbhc_rh_get_hub_status(wusbhc, (u32 *)buf, wLength);
  338. break;
  339. case SetPortFeature:
  340. result = wusbhc_rh_set_port_feat(wusbhc, wValue, wIndex >> 8,
  341. (wIndex & 0xff) - 1);
  342. break;
  343. case ClearPortFeature:
  344. result = wusbhc_rh_clear_port_feat(wusbhc, wValue, wIndex >> 8,
  345. (wIndex & 0xff) - 1);
  346. break;
  347. case GetPortStatus:
  348. result = wusbhc_rh_get_port_status(wusbhc, wIndex - 1,
  349. (u32 *)buf, wLength);
  350. break;
  351. case SetHubFeature:
  352. default:
  353. dev_err(wusbhc->dev, "%s (%p [%p], %x, %x, %x, %p, %x) "
  354. "UNIMPLEMENTED\n", __func__, usb_hcd, wusbhc, reqntype,
  355. wValue, wIndex, buf, wLength);
  356. /* dump_stack(); */
  357. result = -ENOSYS;
  358. }
  359. return result;
  360. }
  361. EXPORT_SYMBOL_GPL(wusbhc_rh_control);
  362. int wusbhc_rh_suspend(struct usb_hcd *usb_hcd)
  363. {
  364. struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
  365. dev_err(wusbhc->dev, "%s (%p [%p]) UNIMPLEMENTED\n", __func__,
  366. usb_hcd, wusbhc);
  367. /* dump_stack(); */
  368. return -ENOSYS;
  369. }
  370. EXPORT_SYMBOL_GPL(wusbhc_rh_suspend);
  371. int wusbhc_rh_resume(struct usb_hcd *usb_hcd)
  372. {
  373. struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
  374. dev_err(wusbhc->dev, "%s (%p [%p]) UNIMPLEMENTED\n", __func__,
  375. usb_hcd, wusbhc);
  376. /* dump_stack(); */
  377. return -ENOSYS;
  378. }
  379. EXPORT_SYMBOL_GPL(wusbhc_rh_resume);
  380. int wusbhc_rh_start_port_reset(struct usb_hcd *usb_hcd, unsigned port_idx)
  381. {
  382. struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
  383. dev_err(wusbhc->dev, "%s (%p [%p], port_idx %u) UNIMPLEMENTED\n",
  384. __func__, usb_hcd, wusbhc, port_idx);
  385. WARN_ON(1);
  386. return -ENOSYS;
  387. }
  388. EXPORT_SYMBOL_GPL(wusbhc_rh_start_port_reset);
  389. static void wusb_port_init(struct wusb_port *port)
  390. {
  391. port->status |= USB_PORT_STAT_HIGH_SPEED;
  392. }
  393. /*
  394. * Alloc fake port specific fields and status.
  395. */
  396. int wusbhc_rh_create(struct wusbhc *wusbhc)
  397. {
  398. int result = -ENOMEM;
  399. size_t port_size, itr;
  400. port_size = wusbhc->ports_max * sizeof(wusbhc->port[0]);
  401. wusbhc->port = kzalloc(port_size, GFP_KERNEL);
  402. if (wusbhc->port == NULL)
  403. goto error_port_alloc;
  404. for (itr = 0; itr < wusbhc->ports_max; itr++)
  405. wusb_port_init(&wusbhc->port[itr]);
  406. result = 0;
  407. error_port_alloc:
  408. return result;
  409. }
  410. void wusbhc_rh_destroy(struct wusbhc *wusbhc)
  411. {
  412. kfree(wusbhc->port);
  413. }