mmc.c 7.6 KB

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