mmc.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 "wusbhc.h"
  41. /* Initialize the MMCIEs handling mechanism */
  42. int wusbhc_mmcie_create(struct wusbhc *wusbhc)
  43. {
  44. u8 mmcies = wusbhc->mmcies_max;
  45. wusbhc->mmcie = kzalloc(mmcies * sizeof(wusbhc->mmcie[0]), GFP_KERNEL);
  46. if (wusbhc->mmcie == NULL)
  47. return -ENOMEM;
  48. mutex_init(&wusbhc->mmcie_mutex);
  49. return 0;
  50. }
  51. /* Release resources used by the MMCIEs handling mechanism */
  52. void wusbhc_mmcie_destroy(struct wusbhc *wusbhc)
  53. {
  54. kfree(wusbhc->mmcie);
  55. }
  56. /*
  57. * Add or replace an MMC Wireless USB IE.
  58. *
  59. * @interval: See WUSB1.0[8.5.3.1]
  60. * @repeat_cnt: See WUSB1.0[8.5.3.1]
  61. * @handle: See WUSB1.0[8.5.3.1]
  62. * @wuie: Pointer to the header of the WUSB IE data to add.
  63. * MUST BE allocated in a kmalloc buffer (no stack or
  64. * vmalloc).
  65. * THE CALLER ALWAYS OWNS THE POINTER (we don't free it
  66. * on remove, we just forget about it).
  67. * @returns: 0 if ok, < 0 errno code on error.
  68. *
  69. * Goes over the *whole* @wusbhc->mmcie array looking for (a) the
  70. * first free spot and (b) if @wuie is already in the array (aka:
  71. * transmitted in the MMCs) the spot were it is.
  72. *
  73. * If present, we "overwrite it" (update).
  74. *
  75. *
  76. * NOTE: Need special ordering rules -- see below WUSB1.0 Table 7-38.
  77. * The host uses the handle as the 'sort' index. We
  78. * allocate the last one always for the WUIE_ID_HOST_INFO, and
  79. * the rest, first come first serve in inverse order.
  80. *
  81. * Host software must make sure that it adds the other IEs in
  82. * the right order... the host hardware is responsible for
  83. * placing the WCTA IEs in the right place with the other IEs
  84. * set by host software.
  85. *
  86. * NOTE: we can access wusbhc->wa_descr without locking because it is
  87. * read only.
  88. */
  89. int wusbhc_mmcie_set(struct wusbhc *wusbhc, u8 interval, u8 repeat_cnt,
  90. struct wuie_hdr *wuie)
  91. {
  92. int result = -ENOBUFS;
  93. struct device *dev = wusbhc->dev;
  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. if (printk_ratelimit())
  120. dev_err(dev, "MMC handle space exhausted\n");
  121. goto error_unlock;
  122. }
  123. }
  124. result = (wusbhc->mmcie_add)(wusbhc, interval, repeat_cnt, handle,
  125. wuie);
  126. if (result >= 0)
  127. wusbhc->mmcie[handle] = wuie;
  128. error_unlock:
  129. mutex_unlock(&wusbhc->mmcie_mutex);
  130. return result;
  131. }
  132. EXPORT_SYMBOL_GPL(wusbhc_mmcie_set);
  133. /*
  134. * Remove an MMC IE previously added with wusbhc_mmcie_set()
  135. *
  136. * @wuie Pointer used to add the WUIE
  137. */
  138. void wusbhc_mmcie_rm(struct wusbhc *wusbhc, struct wuie_hdr *wuie)
  139. {
  140. int result;
  141. struct device *dev = wusbhc->dev;
  142. unsigned handle, itr;
  143. mutex_lock(&wusbhc->mmcie_mutex);
  144. for (itr = 0; itr < wusbhc->mmcies_max; itr++)
  145. if (wusbhc->mmcie[itr] == wuie) {
  146. handle = itr;
  147. goto found;
  148. }
  149. mutex_unlock(&wusbhc->mmcie_mutex);
  150. return;
  151. found:
  152. result = (wusbhc->mmcie_rm)(wusbhc, handle);
  153. if (result == 0)
  154. wusbhc->mmcie[itr] = NULL;
  155. else if (printk_ratelimit())
  156. dev_err(dev, "MMC: Failed to remove IE %p (0x%02x)\n",
  157. wuie, wuie->bIEIdentifier);
  158. mutex_unlock(&wusbhc->mmcie_mutex);
  159. return;
  160. }
  161. EXPORT_SYMBOL_GPL(wusbhc_mmcie_rm);
  162. /*
  163. * wusbhc_start - start transmitting MMCs and accepting connections
  164. * @wusbhc: the HC to start
  165. * @chid: the CHID to use for this host
  166. *
  167. * Establishes a cluster reservation, enables device connections, and
  168. * starts MMCs with appropriate DNTS parameters.
  169. */
  170. int wusbhc_start(struct wusbhc *wusbhc, const struct wusb_ckhdid *chid)
  171. {
  172. int result;
  173. struct device *dev = wusbhc->dev;
  174. WARN_ON(wusbhc->wuie_host_info != NULL);
  175. result = wusbhc_rsv_establish(wusbhc);
  176. if (result < 0) {
  177. dev_err(dev, "cannot establish cluster reservation: %d\n",
  178. result);
  179. goto error_rsv_establish;
  180. }
  181. result = wusbhc_devconnect_start(wusbhc, chid);
  182. if (result < 0) {
  183. dev_err(dev, "error enabling device connections: %d\n", result);
  184. goto error_devconnect_start;
  185. }
  186. result = wusbhc_sec_start(wusbhc);
  187. if (result < 0) {
  188. dev_err(dev, "error starting security in the HC: %d\n", result);
  189. goto error_sec_start;
  190. }
  191. /* FIXME: the choice of the DNTS parameters is somewhat
  192. * arbitrary */
  193. result = wusbhc->set_num_dnts(wusbhc, 0, 15);
  194. if (result < 0) {
  195. dev_err(dev, "Cannot set DNTS parameters: %d\n", result);
  196. goto error_set_num_dnts;
  197. }
  198. result = wusbhc->start(wusbhc);
  199. if (result < 0) {
  200. dev_err(dev, "error starting wusbch: %d\n", result);
  201. goto error_wusbhc_start;
  202. }
  203. wusbhc->active = 1;
  204. return 0;
  205. error_wusbhc_start:
  206. wusbhc_sec_stop(wusbhc);
  207. error_set_num_dnts:
  208. error_sec_start:
  209. wusbhc_devconnect_stop(wusbhc);
  210. error_devconnect_start:
  211. wusbhc_rsv_terminate(wusbhc);
  212. error_rsv_establish:
  213. return result;
  214. }
  215. /*
  216. * Disconnect all from the WUSB Channel
  217. *
  218. * Send a Host Disconnect IE in the MMC, wait, don't send it any more
  219. */
  220. static int __wusbhc_host_disconnect_ie(struct wusbhc *wusbhc)
  221. {
  222. int result = -ENOMEM;
  223. struct wuie_host_disconnect *host_disconnect_ie;
  224. might_sleep();
  225. host_disconnect_ie = kmalloc(sizeof(*host_disconnect_ie), GFP_KERNEL);
  226. if (host_disconnect_ie == NULL)
  227. goto error_alloc;
  228. host_disconnect_ie->hdr.bLength = sizeof(*host_disconnect_ie);
  229. host_disconnect_ie->hdr.bIEIdentifier = WUIE_ID_HOST_DISCONNECT;
  230. result = wusbhc_mmcie_set(wusbhc, 0, 0, &host_disconnect_ie->hdr);
  231. if (result < 0)
  232. goto error_mmcie_set;
  233. /* WUSB1.0[8.5.3.1 & 7.5.2] */
  234. msleep(100);
  235. wusbhc_mmcie_rm(wusbhc, &host_disconnect_ie->hdr);
  236. error_mmcie_set:
  237. kfree(host_disconnect_ie);
  238. error_alloc:
  239. return result;
  240. }
  241. /*
  242. * wusbhc_stop - stop transmitting MMCs
  243. * @wusbhc: the HC to stop
  244. *
  245. * Send a Host Disconnect IE, wait, remove all the MMCs (stop sending MMCs).
  246. *
  247. * If we can't allocate a Host Stop IE, screw it, we don't notify the
  248. * devices we are disconnecting...
  249. */
  250. void wusbhc_stop(struct wusbhc *wusbhc)
  251. {
  252. if (wusbhc->active) {
  253. wusbhc->active = 0;
  254. wusbhc->stop(wusbhc);
  255. wusbhc_sec_stop(wusbhc);
  256. __wusbhc_host_disconnect_ie(wusbhc);
  257. wusbhc_devconnect_stop(wusbhc);
  258. wusbhc_rsv_terminate(wusbhc);
  259. }
  260. }
  261. EXPORT_SYMBOL_GPL(wusbhc_stop);
  262. /*
  263. * Change the CHID in a WUSB Channel
  264. *
  265. * If it is just a new CHID, send a Host Disconnect IE and then change
  266. * the CHID IE.
  267. */
  268. static int __wusbhc_chid_change(struct wusbhc *wusbhc,
  269. const struct wusb_ckhdid *chid)
  270. {
  271. int result = -ENOSYS;
  272. struct device *dev = wusbhc->dev;
  273. dev_err(dev, "%s() not implemented yet\n", __func__);
  274. return result;
  275. BUG_ON(wusbhc->wuie_host_info == NULL);
  276. __wusbhc_host_disconnect_ie(wusbhc);
  277. wusbhc->wuie_host_info->CHID = *chid;
  278. result = wusbhc_mmcie_set(wusbhc, 0, 0, &wusbhc->wuie_host_info->hdr);
  279. if (result < 0)
  280. dev_err(dev, "Can't update Host Info WUSB IE: %d\n", result);
  281. return result;
  282. }
  283. /*
  284. * Set/reset/update a new CHID
  285. *
  286. * Depending on the previous state of the MMCs, start, stop or change
  287. * the sent MMC. This effectively switches the host controller on and
  288. * off (radio wise).
  289. */
  290. int wusbhc_chid_set(struct wusbhc *wusbhc, const struct wusb_ckhdid *chid)
  291. {
  292. int result = 0;
  293. if (memcmp(chid, &wusb_ckhdid_zero, sizeof(chid)) == 0)
  294. chid = NULL;
  295. mutex_lock(&wusbhc->mutex);
  296. if (wusbhc->active) {
  297. if (chid)
  298. result = __wusbhc_chid_change(wusbhc, chid);
  299. else
  300. wusbhc_stop(wusbhc);
  301. } else {
  302. if (chid)
  303. wusbhc_start(wusbhc, chid);
  304. }
  305. mutex_unlock(&wusbhc->mutex);
  306. return result;
  307. }
  308. EXPORT_SYMBOL_GPL(wusbhc_chid_set);