rh.c 13 KB

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