mmc.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * WUSB Wire Adapter: Control/Data Streaming Interface (WUSB[8])
  4. * MMC (Microscheduled Management Command) handling
  5. *
  6. * Copyright (C) 2005-2006 Intel Corporation
  7. * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
  8. *
  9. * WUIEs and MMC IEs...well, they are almost the same at the end. MMC
  10. * IEs are Wireless USB IEs that go into the MMC period...[what is
  11. * that? look in Design-overview.txt].
  12. *
  13. *
  14. * This is a simple subsystem to keep track of which IEs are being
  15. * sent by the host in the MMC period.
  16. *
  17. * For each WUIE we ask to send, we keep it in an array, so we can
  18. * request its removal later, or replace the content. They are tracked
  19. * by pointer, so be sure to use the same pointer if you want to
  20. * remove it or update the contents.
  21. *
  22. * FIXME:
  23. * - add timers that autoremove intervalled IEs?
  24. */
  25. #include <linux/usb/wusb.h>
  26. #include <linux/slab.h>
  27. #include <linux/export.h>
  28. #include "wusbhc.h"
  29. /* Initialize the MMCIEs handling mechanism */
  30. int wusbhc_mmcie_create(struct wusbhc *wusbhc)
  31. {
  32. u8 mmcies = wusbhc->mmcies_max;
  33. wusbhc->mmcie = kcalloc(mmcies, sizeof(wusbhc->mmcie[0]), GFP_KERNEL);
  34. if (wusbhc->mmcie == NULL)
  35. return -ENOMEM;
  36. mutex_init(&wusbhc->mmcie_mutex);
  37. return 0;
  38. }
  39. /* Release resources used by the MMCIEs handling mechanism */
  40. void wusbhc_mmcie_destroy(struct wusbhc *wusbhc)
  41. {
  42. kfree(wusbhc->mmcie);
  43. }
  44. /*
  45. * Add or replace an MMC Wireless USB IE.
  46. *
  47. * @interval: See WUSB1.0[8.5.3.1]
  48. * @repeat_cnt: See WUSB1.0[8.5.3.1]
  49. * @handle: See WUSB1.0[8.5.3.1]
  50. * @wuie: Pointer to the header of the WUSB IE data to add.
  51. * MUST BE allocated in a kmalloc buffer (no stack or
  52. * vmalloc).
  53. * THE CALLER ALWAYS OWNS THE POINTER (we don't free it
  54. * on remove, we just forget about it).
  55. * @returns: 0 if ok, < 0 errno code on error.
  56. *
  57. * Goes over the *whole* @wusbhc->mmcie array looking for (a) the
  58. * first free spot and (b) if @wuie is already in the array (aka:
  59. * transmitted in the MMCs) the spot were it is.
  60. *
  61. * If present, we "overwrite it" (update).
  62. *
  63. *
  64. * NOTE: Need special ordering rules -- see below WUSB1.0 Table 7-38.
  65. * The host uses the handle as the 'sort' index. We
  66. * allocate the last one always for the WUIE_ID_HOST_INFO, and
  67. * the rest, first come first serve in inverse order.
  68. *
  69. * Host software must make sure that it adds the other IEs in
  70. * the right order... the host hardware is responsible for
  71. * placing the WCTA IEs in the right place with the other IEs
  72. * set by host software.
  73. *
  74. * NOTE: we can access wusbhc->wa_descr without locking because it is
  75. * read only.
  76. */
  77. int wusbhc_mmcie_set(struct wusbhc *wusbhc, u8 interval, u8 repeat_cnt,
  78. struct wuie_hdr *wuie)
  79. {
  80. int result = -ENOBUFS;
  81. unsigned handle, itr;
  82. /* Search a handle, taking into account the ordering */
  83. mutex_lock(&wusbhc->mmcie_mutex);
  84. switch (wuie->bIEIdentifier) {
  85. case WUIE_ID_HOST_INFO:
  86. /* Always last */
  87. handle = wusbhc->mmcies_max - 1;
  88. break;
  89. case WUIE_ID_ISOCH_DISCARD:
  90. dev_err(wusbhc->dev, "Special ordering case for WUIE ID 0x%x "
  91. "unimplemented\n", wuie->bIEIdentifier);
  92. result = -ENOSYS;
  93. goto error_unlock;
  94. default:
  95. /* search for it or find the last empty slot */
  96. handle = ~0;
  97. for (itr = 0; itr < wusbhc->mmcies_max - 1; itr++) {
  98. if (wusbhc->mmcie[itr] == wuie) {
  99. handle = itr;
  100. break;
  101. }
  102. if (wusbhc->mmcie[itr] == NULL)
  103. handle = itr;
  104. }
  105. if (handle == ~0)
  106. goto error_unlock;
  107. }
  108. result = (wusbhc->mmcie_add)(wusbhc, interval, repeat_cnt, handle,
  109. wuie);
  110. if (result >= 0)
  111. wusbhc->mmcie[handle] = wuie;
  112. error_unlock:
  113. mutex_unlock(&wusbhc->mmcie_mutex);
  114. return result;
  115. }
  116. EXPORT_SYMBOL_GPL(wusbhc_mmcie_set);
  117. /*
  118. * Remove an MMC IE previously added with wusbhc_mmcie_set()
  119. *
  120. * @wuie Pointer used to add the WUIE
  121. */
  122. void wusbhc_mmcie_rm(struct wusbhc *wusbhc, struct wuie_hdr *wuie)
  123. {
  124. int result;
  125. unsigned handle, itr;
  126. mutex_lock(&wusbhc->mmcie_mutex);
  127. for (itr = 0; itr < wusbhc->mmcies_max; itr++) {
  128. if (wusbhc->mmcie[itr] == wuie) {
  129. handle = itr;
  130. goto found;
  131. }
  132. }
  133. mutex_unlock(&wusbhc->mmcie_mutex);
  134. return;
  135. found:
  136. result = (wusbhc->mmcie_rm)(wusbhc, handle);
  137. if (result == 0)
  138. wusbhc->mmcie[itr] = NULL;
  139. mutex_unlock(&wusbhc->mmcie_mutex);
  140. }
  141. EXPORT_SYMBOL_GPL(wusbhc_mmcie_rm);
  142. static int wusbhc_mmc_start(struct wusbhc *wusbhc)
  143. {
  144. int ret;
  145. mutex_lock(&wusbhc->mutex);
  146. ret = wusbhc->start(wusbhc);
  147. if (ret >= 0)
  148. wusbhc->active = 1;
  149. mutex_unlock(&wusbhc->mutex);
  150. return ret;
  151. }
  152. static void wusbhc_mmc_stop(struct wusbhc *wusbhc)
  153. {
  154. mutex_lock(&wusbhc->mutex);
  155. wusbhc->active = 0;
  156. wusbhc->stop(wusbhc, WUSB_CHANNEL_STOP_DELAY_MS);
  157. mutex_unlock(&wusbhc->mutex);
  158. }
  159. /*
  160. * wusbhc_start - start transmitting MMCs and accepting connections
  161. * @wusbhc: the HC to start
  162. *
  163. * Establishes a cluster reservation, enables device connections, and
  164. * starts MMCs with appropriate DNTS parameters.
  165. */
  166. int wusbhc_start(struct wusbhc *wusbhc)
  167. {
  168. int result;
  169. struct device *dev = wusbhc->dev;
  170. WARN_ON(wusbhc->wuie_host_info != NULL);
  171. BUG_ON(wusbhc->uwb_rc == NULL);
  172. result = wusbhc_rsv_establish(wusbhc);
  173. if (result < 0) {
  174. dev_err(dev, "cannot establish cluster reservation: %d\n",
  175. result);
  176. goto error_rsv_establish;
  177. }
  178. result = wusbhc_devconnect_start(wusbhc);
  179. if (result < 0) {
  180. dev_err(dev, "error enabling device connections: %d\n",
  181. result);
  182. goto error_devconnect_start;
  183. }
  184. result = wusbhc_sec_start(wusbhc);
  185. if (result < 0) {
  186. dev_err(dev, "error starting security in the HC: %d\n",
  187. result);
  188. goto error_sec_start;
  189. }
  190. result = wusbhc->set_num_dnts(wusbhc, wusbhc->dnts_interval,
  191. wusbhc->dnts_num_slots);
  192. if (result < 0) {
  193. dev_err(dev, "Cannot set DNTS parameters: %d\n", result);
  194. goto error_set_num_dnts;
  195. }
  196. result = wusbhc_mmc_start(wusbhc);
  197. if (result < 0) {
  198. dev_err(dev, "error starting wusbch: %d\n", result);
  199. goto error_wusbhc_start;
  200. }
  201. return 0;
  202. error_wusbhc_start:
  203. wusbhc_sec_stop(wusbhc);
  204. error_set_num_dnts:
  205. error_sec_start:
  206. wusbhc_devconnect_stop(wusbhc);
  207. error_devconnect_start:
  208. wusbhc_rsv_terminate(wusbhc);
  209. error_rsv_establish:
  210. return result;
  211. }
  212. /*
  213. * wusbhc_stop - stop transmitting MMCs
  214. * @wusbhc: the HC to stop
  215. *
  216. * Stops the WUSB channel and removes the cluster reservation.
  217. */
  218. void wusbhc_stop(struct wusbhc *wusbhc)
  219. {
  220. wusbhc_mmc_stop(wusbhc);
  221. wusbhc_sec_stop(wusbhc);
  222. wusbhc_devconnect_stop(wusbhc);
  223. wusbhc_rsv_terminate(wusbhc);
  224. }
  225. /*
  226. * Set/reset/update a new CHID
  227. *
  228. * Depending on the previous state of the MMCs, start, stop or change
  229. * the sent MMC. This effectively switches the host controller on and
  230. * off (radio wise).
  231. */
  232. int wusbhc_chid_set(struct wusbhc *wusbhc, const struct wusb_ckhdid *chid)
  233. {
  234. int result = 0;
  235. if (memcmp(chid, &wusb_ckhdid_zero, sizeof(*chid)) == 0)
  236. chid = NULL;
  237. mutex_lock(&wusbhc->mutex);
  238. if (chid) {
  239. if (wusbhc->active) {
  240. mutex_unlock(&wusbhc->mutex);
  241. return -EBUSY;
  242. }
  243. wusbhc->chid = *chid;
  244. }
  245. /* register with UWB if we haven't already since we are about to start
  246. the radio. */
  247. if ((chid) && (wusbhc->uwb_rc == NULL)) {
  248. wusbhc->uwb_rc = uwb_rc_get_by_grandpa(wusbhc->dev->parent);
  249. if (wusbhc->uwb_rc == NULL) {
  250. result = -ENODEV;
  251. dev_err(wusbhc->dev,
  252. "Cannot get associated UWB Host Controller\n");
  253. goto error_rc_get;
  254. }
  255. result = wusbhc_pal_register(wusbhc);
  256. if (result < 0) {
  257. dev_err(wusbhc->dev, "Cannot register as a UWB PAL\n");
  258. goto error_pal_register;
  259. }
  260. }
  261. mutex_unlock(&wusbhc->mutex);
  262. if (chid)
  263. result = uwb_radio_start(&wusbhc->pal);
  264. else if (wusbhc->uwb_rc)
  265. uwb_radio_stop(&wusbhc->pal);
  266. return result;
  267. error_pal_register:
  268. uwb_rc_put(wusbhc->uwb_rc);
  269. wusbhc->uwb_rc = NULL;
  270. error_rc_get:
  271. mutex_unlock(&wusbhc->mutex);
  272. return result;
  273. }
  274. EXPORT_SYMBOL_GPL(wusbhc_chid_set);