rh.c 13 KB

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