rh.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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;
  138. unsigned long *buf = (unsigned long *) _buf;
  139. /* WE DON'T LOCK, see comment */
  140. size = wusbhc->ports_max + 1 /* hub bit */;
  141. size = (size + 8 - 1) / 8; /* round to bytes */
  142. for (cnt = 0; cnt < wusbhc->ports_max; cnt++)
  143. if (wusb_port_by_idx(wusbhc, cnt)->change)
  144. set_bit(cnt + 1, buf);
  145. else
  146. clear_bit(cnt + 1, buf);
  147. return size;
  148. }
  149. EXPORT_SYMBOL_GPL(wusbhc_rh_status_data);
  150. /*
  151. * Return the hub's descriptor
  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->u.hs.DeviceRemovable[0], 0, temp);
  179. memset(&descr->u.hs.DeviceRemovable[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. switch (feature) {
  193. case C_HUB_LOCAL_POWER:
  194. /* FIXME: maybe plug bit 0 to the power input status,
  195. * if any?
  196. * see wusbhc_rh_get_hub_status() */
  197. case C_HUB_OVER_CURRENT:
  198. result = 0;
  199. break;
  200. default:
  201. result = -EPIPE;
  202. }
  203. return result;
  204. }
  205. /*
  206. * Return hub status (it is always zero...)
  207. *
  208. * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
  209. *
  210. * Nothing to do, so no locking needed ;)
  211. */
  212. static int wusbhc_rh_get_hub_status(struct wusbhc *wusbhc, u32 *buf,
  213. u16 wLength)
  214. {
  215. /* FIXME: maybe plug bit 0 to the power input status (if any)? */
  216. *buf = 0;
  217. return 0;
  218. }
  219. /*
  220. * Set a port feature
  221. *
  222. * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
  223. */
  224. static int wusbhc_rh_set_port_feat(struct wusbhc *wusbhc, u16 feature,
  225. u8 selector, u8 port_idx)
  226. {
  227. struct device *dev = wusbhc->dev;
  228. if (port_idx > wusbhc->ports_max)
  229. return -EINVAL;
  230. switch (feature) {
  231. /* According to USB2.0[11.24.2.13]p2, these features
  232. * are not required to be implemented. */
  233. case USB_PORT_FEAT_C_OVER_CURRENT:
  234. case USB_PORT_FEAT_C_ENABLE:
  235. case USB_PORT_FEAT_C_SUSPEND:
  236. case USB_PORT_FEAT_C_CONNECTION:
  237. case USB_PORT_FEAT_C_RESET:
  238. return 0;
  239. case USB_PORT_FEAT_POWER:
  240. /* No such thing, but we fake it works */
  241. mutex_lock(&wusbhc->mutex);
  242. wusb_port_by_idx(wusbhc, port_idx)->status |= USB_PORT_STAT_POWER;
  243. mutex_unlock(&wusbhc->mutex);
  244. return 0;
  245. case USB_PORT_FEAT_RESET:
  246. return wusbhc_rh_port_reset(wusbhc, port_idx);
  247. case USB_PORT_FEAT_ENABLE:
  248. case USB_PORT_FEAT_SUSPEND:
  249. dev_err(dev, "(port_idx %d) set feat %d/%d UNIMPLEMENTED\n",
  250. port_idx, feature, selector);
  251. return -ENOSYS;
  252. default:
  253. dev_err(dev, "(port_idx %d) set feat %d/%d UNKNOWN\n",
  254. port_idx, feature, selector);
  255. return -EPIPE;
  256. }
  257. return 0;
  258. }
  259. /*
  260. * Clear a port feature...
  261. *
  262. * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
  263. */
  264. static int wusbhc_rh_clear_port_feat(struct wusbhc *wusbhc, u16 feature,
  265. u8 selector, u8 port_idx)
  266. {
  267. int result = 0;
  268. struct device *dev = wusbhc->dev;
  269. if (port_idx > wusbhc->ports_max)
  270. return -EINVAL;
  271. mutex_lock(&wusbhc->mutex);
  272. switch (feature) {
  273. case USB_PORT_FEAT_POWER: /* fake port always on */
  274. /* According to USB2.0[11.24.2.7.1.4], no need to implement? */
  275. case USB_PORT_FEAT_C_OVER_CURRENT:
  276. break;
  277. case USB_PORT_FEAT_C_RESET:
  278. wusb_port_by_idx(wusbhc, port_idx)->change &= ~USB_PORT_STAT_C_RESET;
  279. break;
  280. case USB_PORT_FEAT_C_CONNECTION:
  281. wusb_port_by_idx(wusbhc, port_idx)->change &= ~USB_PORT_STAT_C_CONNECTION;
  282. break;
  283. case USB_PORT_FEAT_ENABLE:
  284. __wusbhc_dev_disable(wusbhc, port_idx);
  285. break;
  286. case USB_PORT_FEAT_C_ENABLE:
  287. wusb_port_by_idx(wusbhc, port_idx)->change &= ~USB_PORT_STAT_C_ENABLE;
  288. break;
  289. case USB_PORT_FEAT_SUSPEND:
  290. case USB_PORT_FEAT_C_SUSPEND:
  291. dev_err(dev, "(port_idx %d) Clear feat %d/%d UNIMPLEMENTED\n",
  292. port_idx, feature, selector);
  293. result = -ENOSYS;
  294. break;
  295. default:
  296. dev_err(dev, "(port_idx %d) Clear feat %d/%d UNKNOWN\n",
  297. port_idx, feature, selector);
  298. result = -EPIPE;
  299. break;
  300. }
  301. mutex_unlock(&wusbhc->mutex);
  302. return result;
  303. }
  304. /*
  305. * Return the port's status
  306. *
  307. * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
  308. */
  309. static int wusbhc_rh_get_port_status(struct wusbhc *wusbhc, u16 port_idx,
  310. u32 *_buf, u16 wLength)
  311. {
  312. __le16 *buf = (__le16 *)_buf;
  313. if (port_idx > wusbhc->ports_max)
  314. return -EINVAL;
  315. mutex_lock(&wusbhc->mutex);
  316. buf[0] = cpu_to_le16(wusb_port_by_idx(wusbhc, port_idx)->status);
  317. buf[1] = cpu_to_le16(wusb_port_by_idx(wusbhc, port_idx)->change);
  318. mutex_unlock(&wusbhc->mutex);
  319. return 0;
  320. }
  321. /*
  322. * Entry point for Root Hub operations
  323. *
  324. * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
  325. */
  326. int wusbhc_rh_control(struct usb_hcd *usb_hcd, u16 reqntype, u16 wValue,
  327. u16 wIndex, char *buf, u16 wLength)
  328. {
  329. int result = -ENOSYS;
  330. struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
  331. switch (reqntype) {
  332. case GetHubDescriptor:
  333. result = wusbhc_rh_get_hub_descr(
  334. wusbhc, wValue, wIndex,
  335. (struct usb_hub_descriptor *) buf, wLength);
  336. break;
  337. case ClearHubFeature:
  338. result = wusbhc_rh_clear_hub_feat(wusbhc, wValue);
  339. break;
  340. case GetHubStatus:
  341. result = wusbhc_rh_get_hub_status(wusbhc, (u32 *)buf, wLength);
  342. break;
  343. case SetPortFeature:
  344. result = wusbhc_rh_set_port_feat(wusbhc, wValue, wIndex >> 8,
  345. (wIndex & 0xff) - 1);
  346. break;
  347. case ClearPortFeature:
  348. result = wusbhc_rh_clear_port_feat(wusbhc, wValue, wIndex >> 8,
  349. (wIndex & 0xff) - 1);
  350. break;
  351. case GetPortStatus:
  352. result = wusbhc_rh_get_port_status(wusbhc, wIndex - 1,
  353. (u32 *)buf, wLength);
  354. break;
  355. case SetHubFeature:
  356. default:
  357. dev_err(wusbhc->dev, "%s (%pK [%pK], %x, %x, %x, %pK, %x) "
  358. "UNIMPLEMENTED\n", __func__, usb_hcd, wusbhc, reqntype,
  359. wValue, wIndex, buf, wLength);
  360. /* dump_stack(); */
  361. result = -ENOSYS;
  362. }
  363. return result;
  364. }
  365. EXPORT_SYMBOL_GPL(wusbhc_rh_control);
  366. int wusbhc_rh_suspend(struct usb_hcd *usb_hcd)
  367. {
  368. struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
  369. dev_err(wusbhc->dev, "%s (%pK [%pK]) UNIMPLEMENTED\n", __func__,
  370. usb_hcd, wusbhc);
  371. /* dump_stack(); */
  372. return -ENOSYS;
  373. }
  374. EXPORT_SYMBOL_GPL(wusbhc_rh_suspend);
  375. int wusbhc_rh_resume(struct usb_hcd *usb_hcd)
  376. {
  377. struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
  378. dev_err(wusbhc->dev, "%s (%pK [%pK]) UNIMPLEMENTED\n", __func__,
  379. usb_hcd, wusbhc);
  380. /* dump_stack(); */
  381. return -ENOSYS;
  382. }
  383. EXPORT_SYMBOL_GPL(wusbhc_rh_resume);
  384. int wusbhc_rh_start_port_reset(struct usb_hcd *usb_hcd, unsigned port_idx)
  385. {
  386. struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
  387. dev_err(wusbhc->dev, "%s (%pK [%pK], port_idx %u) UNIMPLEMENTED\n",
  388. __func__, usb_hcd, wusbhc, port_idx);
  389. WARN_ON(1);
  390. return -ENOSYS;
  391. }
  392. EXPORT_SYMBOL_GPL(wusbhc_rh_start_port_reset);
  393. static void wusb_port_init(struct wusb_port *port)
  394. {
  395. port->status |= USB_PORT_STAT_HIGH_SPEED;
  396. }
  397. /*
  398. * Alloc fake port specific fields and status.
  399. */
  400. int wusbhc_rh_create(struct wusbhc *wusbhc)
  401. {
  402. int result = -ENOMEM;
  403. size_t port_size, itr;
  404. port_size = wusbhc->ports_max * sizeof(wusbhc->port[0]);
  405. wusbhc->port = kzalloc(port_size, GFP_KERNEL);
  406. if (wusbhc->port == NULL)
  407. goto error_port_alloc;
  408. for (itr = 0; itr < wusbhc->ports_max; itr++)
  409. wusb_port_init(&wusbhc->port[itr]);
  410. result = 0;
  411. error_port_alloc:
  412. return result;
  413. }
  414. void wusbhc_rh_destroy(struct wusbhc *wusbhc)
  415. {
  416. kfree(wusbhc->port);
  417. }