rh.c 12 KB

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