ie.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. * Ultra Wide Band
  3. * Information Element Handling
  4. *
  5. * Copyright (C) 2005-2006 Intel Corporation
  6. * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
  7. * Reinette Chatre <reinette.chatre@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. * FIXME: docs
  25. */
  26. #include <linux/slab.h>
  27. #include <linux/export.h>
  28. #include "uwb-internal.h"
  29. /**
  30. * uwb_ie_next - get the next IE in a buffer
  31. * @ptr: start of the buffer containing the IE data
  32. * @len: length of the buffer
  33. *
  34. * Both @ptr and @len are updated so subsequent calls to uwb_ie_next()
  35. * will get the next IE.
  36. *
  37. * NULL is returned (and @ptr and @len will not be updated) if there
  38. * are no more IEs in the buffer or the buffer is too short.
  39. */
  40. struct uwb_ie_hdr *uwb_ie_next(void **ptr, size_t *len)
  41. {
  42. struct uwb_ie_hdr *hdr;
  43. size_t ie_len;
  44. if (*len < sizeof(struct uwb_ie_hdr))
  45. return NULL;
  46. hdr = *ptr;
  47. ie_len = sizeof(struct uwb_ie_hdr) + hdr->length;
  48. if (*len < ie_len)
  49. return NULL;
  50. *ptr += ie_len;
  51. *len -= ie_len;
  52. return hdr;
  53. }
  54. EXPORT_SYMBOL_GPL(uwb_ie_next);
  55. /**
  56. * uwb_ie_dump_hex - print IEs to a character buffer
  57. * @ies: the IEs to print.
  58. * @len: length of all the IEs.
  59. * @buf: the destination buffer.
  60. * @size: size of @buf.
  61. *
  62. * Returns the number of characters written.
  63. */
  64. int uwb_ie_dump_hex(const struct uwb_ie_hdr *ies, size_t len,
  65. char *buf, size_t size)
  66. {
  67. void *ptr;
  68. const struct uwb_ie_hdr *ie;
  69. int r = 0;
  70. u8 *d;
  71. ptr = (void *)ies;
  72. for (;;) {
  73. ie = uwb_ie_next(&ptr, &len);
  74. if (!ie)
  75. break;
  76. r += scnprintf(buf + r, size - r, "%02x %02x",
  77. (unsigned)ie->element_id,
  78. (unsigned)ie->length);
  79. d = (uint8_t *)ie + sizeof(struct uwb_ie_hdr);
  80. while (d != ptr && r < size)
  81. r += scnprintf(buf + r, size - r, " %02x", (unsigned)*d++);
  82. if (r < size)
  83. buf[r++] = '\n';
  84. };
  85. return r;
  86. }
  87. /**
  88. * Get the IEs that a radio controller is sending in its beacon
  89. *
  90. * @uwb_rc: UWB Radio Controller
  91. * @returns: Size read from the system
  92. *
  93. * We don't need to lock the uwb_rc's mutex because we don't modify
  94. * anything. Once done with the iedata buffer, call
  95. * uwb_rc_ie_release(iedata). Don't call kfree on it.
  96. */
  97. static
  98. ssize_t uwb_rc_get_ie(struct uwb_rc *uwb_rc, struct uwb_rc_evt_get_ie **pget_ie)
  99. {
  100. ssize_t result;
  101. struct device *dev = &uwb_rc->uwb_dev.dev;
  102. struct uwb_rccb *cmd = NULL;
  103. struct uwb_rceb *reply = NULL;
  104. struct uwb_rc_evt_get_ie *get_ie;
  105. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  106. if (cmd == NULL)
  107. return -ENOMEM;
  108. cmd->bCommandType = UWB_RC_CET_GENERAL;
  109. cmd->wCommand = cpu_to_le16(UWB_RC_CMD_GET_IE);
  110. result = uwb_rc_vcmd(uwb_rc, "GET_IE", cmd, sizeof(*cmd),
  111. UWB_RC_CET_GENERAL, UWB_RC_CMD_GET_IE,
  112. &reply);
  113. kfree(cmd);
  114. if (result < 0)
  115. return result;
  116. get_ie = container_of(reply, struct uwb_rc_evt_get_ie, rceb);
  117. if (result < sizeof(*get_ie)) {
  118. dev_err(dev, "not enough data returned for decoding GET IE "
  119. "(%zu bytes received vs %zu needed)\n",
  120. result, sizeof(*get_ie));
  121. return -EINVAL;
  122. } else if (result < sizeof(*get_ie) + le16_to_cpu(get_ie->wIELength)) {
  123. dev_err(dev, "not enough data returned for decoding GET IE "
  124. "payload (%zu bytes received vs %zu needed)\n", result,
  125. sizeof(*get_ie) + le16_to_cpu(get_ie->wIELength));
  126. return -EINVAL;
  127. }
  128. *pget_ie = get_ie;
  129. return result;
  130. }
  131. /**
  132. * Replace all IEs currently being transmitted by a device
  133. *
  134. * @cmd: pointer to the SET-IE command with the IEs to set
  135. * @size: size of @buf
  136. */
  137. int uwb_rc_set_ie(struct uwb_rc *rc, struct uwb_rc_cmd_set_ie *cmd)
  138. {
  139. int result;
  140. struct device *dev = &rc->uwb_dev.dev;
  141. struct uwb_rc_evt_set_ie reply;
  142. reply.rceb.bEventType = UWB_RC_CET_GENERAL;
  143. reply.rceb.wEvent = UWB_RC_CMD_SET_IE;
  144. result = uwb_rc_cmd(rc, "SET-IE", &cmd->rccb,
  145. sizeof(*cmd) + le16_to_cpu(cmd->wIELength),
  146. &reply.rceb, sizeof(reply));
  147. if (result < 0)
  148. goto error_cmd;
  149. else if (result != sizeof(reply)) {
  150. dev_err(dev, "SET-IE: not enough data to decode reply "
  151. "(%d bytes received vs %zu needed)\n",
  152. result, sizeof(reply));
  153. result = -EIO;
  154. } else if (reply.bResultCode != UWB_RC_RES_SUCCESS) {
  155. dev_err(dev, "SET-IE: command execution failed: %s (%d)\n",
  156. uwb_rc_strerror(reply.bResultCode), reply.bResultCode);
  157. result = -EIO;
  158. } else
  159. result = 0;
  160. error_cmd:
  161. return result;
  162. }
  163. /* Cleanup the whole IE management subsystem */
  164. void uwb_rc_ie_init(struct uwb_rc *uwb_rc)
  165. {
  166. mutex_init(&uwb_rc->ies_mutex);
  167. }
  168. /**
  169. * uwb_rc_ie_setup - setup a radio controller's IE manager
  170. * @uwb_rc: the radio controller.
  171. *
  172. * The current set of IEs are obtained from the hardware with a GET-IE
  173. * command (since the radio controller is not yet beaconing this will
  174. * be just the hardware's MAC and PHY Capability IEs).
  175. *
  176. * Returns 0 on success; -ve on an error.
  177. */
  178. int uwb_rc_ie_setup(struct uwb_rc *uwb_rc)
  179. {
  180. struct uwb_rc_evt_get_ie *ie_info = NULL;
  181. int capacity;
  182. capacity = uwb_rc_get_ie(uwb_rc, &ie_info);
  183. if (capacity < 0)
  184. return capacity;
  185. mutex_lock(&uwb_rc->ies_mutex);
  186. uwb_rc->ies = (struct uwb_rc_cmd_set_ie *)ie_info;
  187. uwb_rc->ies->rccb.bCommandType = UWB_RC_CET_GENERAL;
  188. uwb_rc->ies->rccb.wCommand = cpu_to_le16(UWB_RC_CMD_SET_IE);
  189. uwb_rc->ies_capacity = capacity;
  190. mutex_unlock(&uwb_rc->ies_mutex);
  191. return 0;
  192. }
  193. /* Cleanup the whole IE management subsystem */
  194. void uwb_rc_ie_release(struct uwb_rc *uwb_rc)
  195. {
  196. kfree(uwb_rc->ies);
  197. uwb_rc->ies = NULL;
  198. uwb_rc->ies_capacity = 0;
  199. }
  200. static int uwb_rc_ie_add_one(struct uwb_rc *rc, const struct uwb_ie_hdr *new_ie)
  201. {
  202. struct uwb_rc_cmd_set_ie *new_ies;
  203. void *ptr, *prev_ie;
  204. struct uwb_ie_hdr *ie;
  205. size_t length, new_ie_len, new_capacity, size, prev_size;
  206. length = le16_to_cpu(rc->ies->wIELength);
  207. new_ie_len = sizeof(struct uwb_ie_hdr) + new_ie->length;
  208. new_capacity = sizeof(struct uwb_rc_cmd_set_ie) + length + new_ie_len;
  209. if (new_capacity > rc->ies_capacity) {
  210. new_ies = krealloc(rc->ies, new_capacity, GFP_KERNEL);
  211. if (!new_ies)
  212. return -ENOMEM;
  213. rc->ies = new_ies;
  214. }
  215. ptr = rc->ies->IEData;
  216. size = length;
  217. for (;;) {
  218. prev_ie = ptr;
  219. prev_size = size;
  220. ie = uwb_ie_next(&ptr, &size);
  221. if (!ie || ie->element_id > new_ie->element_id)
  222. break;
  223. }
  224. memmove(prev_ie + new_ie_len, prev_ie, prev_size);
  225. memcpy(prev_ie, new_ie, new_ie_len);
  226. rc->ies->wIELength = cpu_to_le16(length + new_ie_len);
  227. return 0;
  228. }
  229. /**
  230. * uwb_rc_ie_add - add new IEs to the radio controller's beacon
  231. * @uwb_rc: the radio controller.
  232. * @ies: the buffer containing the new IE or IEs to be added to
  233. * the device's beacon.
  234. * @size: length of all the IEs.
  235. *
  236. * According to WHCI 0.95 [4.13.6] the driver will only receive the RCEB
  237. * after the device sent the first beacon that includes the IEs specified
  238. * in the SET IE command. We thus cannot send this command if the device is
  239. * not beaconing. Instead, a SET IE command will be sent later right after
  240. * we start beaconing.
  241. *
  242. * Setting an IE on the device will overwrite all current IEs in device. So
  243. * we take the current IEs being transmitted by the device, insert the
  244. * new one, and call SET IE with all the IEs needed.
  245. *
  246. * Returns 0 on success; or -ENOMEM.
  247. */
  248. int uwb_rc_ie_add(struct uwb_rc *uwb_rc,
  249. const struct uwb_ie_hdr *ies, size_t size)
  250. {
  251. int result = 0;
  252. void *ptr;
  253. const struct uwb_ie_hdr *ie;
  254. mutex_lock(&uwb_rc->ies_mutex);
  255. ptr = (void *)ies;
  256. for (;;) {
  257. ie = uwb_ie_next(&ptr, &size);
  258. if (!ie)
  259. break;
  260. result = uwb_rc_ie_add_one(uwb_rc, ie);
  261. if (result < 0)
  262. break;
  263. }
  264. if (result >= 0) {
  265. if (size == 0) {
  266. if (uwb_rc->beaconing != -1)
  267. result = uwb_rc_set_ie(uwb_rc, uwb_rc->ies);
  268. } else
  269. result = -EINVAL;
  270. }
  271. mutex_unlock(&uwb_rc->ies_mutex);
  272. return result;
  273. }
  274. EXPORT_SYMBOL_GPL(uwb_rc_ie_add);
  275. /*
  276. * Remove an IE from internal cache
  277. *
  278. * We are dealing with our internal IE cache so no need to verify that the
  279. * IEs are valid (it has been done already).
  280. *
  281. * Should be called with ies_mutex held
  282. *
  283. * We do not break out once an IE is found in the cache. It is currently
  284. * possible to have more than one IE with the same ID included in the
  285. * beacon. We don't reallocate, we just mark the size smaller.
  286. */
  287. static
  288. void uwb_rc_ie_cache_rm(struct uwb_rc *uwb_rc, enum uwb_ie to_remove)
  289. {
  290. struct uwb_ie_hdr *ie;
  291. size_t len = le16_to_cpu(uwb_rc->ies->wIELength);
  292. void *ptr;
  293. size_t size;
  294. ptr = uwb_rc->ies->IEData;
  295. size = len;
  296. for (;;) {
  297. ie = uwb_ie_next(&ptr, &size);
  298. if (!ie)
  299. break;
  300. if (ie->element_id == to_remove) {
  301. len -= sizeof(struct uwb_ie_hdr) + ie->length;
  302. memmove(ie, ptr, size);
  303. ptr = ie;
  304. }
  305. }
  306. uwb_rc->ies->wIELength = cpu_to_le16(len);
  307. }
  308. /**
  309. * uwb_rc_ie_rm - remove an IE from the radio controller's beacon
  310. * @uwb_rc: the radio controller.
  311. * @element_id: the element ID of the IE to remove.
  312. *
  313. * Only IEs previously added with uwb_rc_ie_add() may be removed.
  314. *
  315. * Returns 0 on success; or -ve the SET-IE command to the radio
  316. * controller failed.
  317. */
  318. int uwb_rc_ie_rm(struct uwb_rc *uwb_rc, enum uwb_ie element_id)
  319. {
  320. int result = 0;
  321. mutex_lock(&uwb_rc->ies_mutex);
  322. uwb_rc_ie_cache_rm(uwb_rc, element_id);
  323. if (uwb_rc->beaconing != -1)
  324. result = uwb_rc_set_ie(uwb_rc, uwb_rc->ies);
  325. mutex_unlock(&uwb_rc->ies_mutex);
  326. return result;
  327. }
  328. EXPORT_SYMBOL_GPL(uwb_rc_ie_rm);