ie.c 9.6 KB

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