3 * Information Element Handling
5 * Copyright (C) 2005-2006 Intel Corporation
6 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
7 * Reinette Chatre <reinette.chatre@intel.com>
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.
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.
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
27 #include "uwb-internal.h"
29 #include <linux/uwb/debug.h>
32 * uwb_ie_next - get the next IE in a buffer
33 * @ptr: start of the buffer containing the IE data
34 * @len: length of the buffer
36 * Both @ptr and @len are updated so subsequent calls to uwb_ie_next()
37 * will get the next IE.
39 * NULL is returned (and @ptr and @len will not be updated) if there
40 * are no more IEs in the buffer or the buffer is too short.
42 struct uwb_ie_hdr *uwb_ie_next(void **ptr, size_t *len)
44 struct uwb_ie_hdr *hdr;
47 if (*len < sizeof(struct uwb_ie_hdr))
51 ie_len = sizeof(struct uwb_ie_hdr) + hdr->length;
61 EXPORT_SYMBOL_GPL(uwb_ie_next);
64 * Get the IEs that a radio controller is sending in its beacon
66 * @uwb_rc: UWB Radio Controller
67 * @returns: Size read from the system
69 * We don't need to lock the uwb_rc's mutex because we don't modify
70 * anything. Once done with the iedata buffer, call
71 * uwb_rc_ie_release(iedata). Don't call kfree on it.
73 ssize_t uwb_rc_get_ie(struct uwb_rc *uwb_rc, struct uwb_rc_evt_get_ie **pget_ie)
76 struct device *dev = &uwb_rc->uwb_dev.dev;
77 struct uwb_rccb *cmd = NULL;
78 struct uwb_rceb *reply = NULL;
79 struct uwb_rc_evt_get_ie *get_ie;
81 d_fnstart(3, dev, "(%p, %p)\n", uwb_rc, pget_ie);
83 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
86 cmd->bCommandType = UWB_RC_CET_GENERAL;
87 cmd->wCommand = cpu_to_le16(UWB_RC_CMD_GET_IE);
88 result = uwb_rc_vcmd(uwb_rc, "GET_IE", cmd, sizeof(*cmd),
89 UWB_RC_CET_GENERAL, UWB_RC_CMD_GET_IE,
93 get_ie = container_of(reply, struct uwb_rc_evt_get_ie, rceb);
94 if (result < sizeof(*get_ie)) {
95 dev_err(dev, "not enough data returned for decoding GET IE "
96 "(%zu bytes received vs %zu needed)\n",
97 result, sizeof(*get_ie));
99 } else if (result < sizeof(*get_ie) + le16_to_cpu(get_ie->wIELength)) {
100 dev_err(dev, "not enough data returned for decoding GET IE "
101 "payload (%zu bytes received vs %zu needed)\n", result,
102 sizeof(*get_ie) + le16_to_cpu(get_ie->wIELength));
109 d_fnend(3, dev, "(%p, %p) = %d\n", uwb_rc, pget_ie, (int)result);
112 EXPORT_SYMBOL_GPL(uwb_rc_get_ie);
116 * Given a pointer to an IE, print it in ASCII/hex followed by a new line
118 * @ie_hdr: pointer to the IE header. Length is in there, and it is
119 * guaranteed that the ie_hdr->length bytes following it are
122 * @_data: context data passed from uwb_ie_for_each(), an struct output_ctx
124 int uwb_ie_dump_hex(struct uwb_dev *uwb_dev, const struct uwb_ie_hdr *ie_hdr,
125 size_t offset, void *_ctx)
127 struct uwb_buf_ctx *ctx = _ctx;
128 const u8 *pl = (void *)(ie_hdr + 1);
131 ctx->bytes += scnprintf(ctx->buf + ctx->bytes, ctx->size - ctx->bytes,
132 "%02x %02x ", (unsigned) ie_hdr->element_id,
133 (unsigned) ie_hdr->length);
135 while (pl_itr < ie_hdr->length && ctx->bytes < ctx->size)
136 ctx->bytes += scnprintf(ctx->buf + ctx->bytes,
137 ctx->size - ctx->bytes,
138 "%02x ", (unsigned) pl[pl_itr++]);
139 if (ctx->bytes < ctx->size)
140 ctx->buf[ctx->bytes++] = '\n';
143 EXPORT_SYMBOL_GPL(uwb_ie_dump_hex);
147 * Verify that a pointer in a buffer points to valid IE
149 * @start: pointer to start of buffer in which IE appears
150 * @itr: pointer to IE inside buffer that will be verified
151 * @top: pointer to end of buffer
153 * @returns: 0 if IE is valid, <0 otherwise
155 * Verification involves checking that the buffer can contain a
156 * header and the amount of data reported in the IE header can be found in
160 int uwb_rc_ie_verify(struct uwb_dev *uwb_dev, const void *start,
161 const void *itr, const void *top)
163 struct device *dev = &uwb_dev->dev;
164 const struct uwb_ie_hdr *ie_hdr;
166 if (top - itr < sizeof(*ie_hdr)) {
167 dev_err(dev, "Bad IE: no data to decode header "
168 "(%zu bytes left vs %zu needed) at offset %zu\n",
169 top - itr, sizeof(*ie_hdr), itr - start);
173 itr += sizeof(*ie_hdr);
174 if (top - itr < ie_hdr->length) {
175 dev_err(dev, "Bad IE: not enough data for payload "
176 "(%zu bytes left vs %zu needed) at offset %zu\n",
177 top - itr, (size_t)ie_hdr->length,
178 (void *)ie_hdr - start);
186 * Walk a buffer filled with consecutive IE's a buffer
188 * @uwb_dev: UWB device this IEs belong to (for err messages mainly)
190 * @fn: function to call with each IE; if it returns 0, we keep
191 * traversing the buffer. If it returns !0, we'll stop and return
194 * @data: pointer passed to @fn
196 * @buf: buffer where the consecutive IEs are located
198 * @size: size of @buf
200 * Each IE is checked for basic correctness (there is space left for
201 * the header and the payload). If that test is failed, we stop
202 * processing. For every good IE, @fn is called.
204 ssize_t uwb_ie_for_each(struct uwb_dev *uwb_dev, uwb_ie_f fn, void *data,
205 const void *buf, size_t size)
208 const struct uwb_ie_hdr *ie_hdr;
209 const void *itr = buf, *top = itr + size;
212 if (uwb_rc_ie_verify(uwb_dev, buf, itr, top) != 0)
215 itr += sizeof(*ie_hdr) + ie_hdr->length;
216 result = fn(uwb_dev, ie_hdr, itr - buf, data);
222 EXPORT_SYMBOL_GPL(uwb_ie_for_each);
226 * Replace all IEs currently being transmitted by a device
228 * @cmd: pointer to the SET-IE command with the IEs to set
229 * @size: size of @buf
231 int uwb_rc_set_ie(struct uwb_rc *rc, struct uwb_rc_cmd_set_ie *cmd)
234 struct device *dev = &rc->uwb_dev.dev;
235 struct uwb_rc_evt_set_ie reply;
237 reply.rceb.bEventType = UWB_RC_CET_GENERAL;
238 reply.rceb.wEvent = UWB_RC_CMD_SET_IE;
239 result = uwb_rc_cmd(rc, "SET-IE", &cmd->rccb,
240 sizeof(*cmd) + le16_to_cpu(cmd->wIELength),
241 &reply.rceb, sizeof(reply));
244 else if (result != sizeof(reply)) {
245 dev_err(dev, "SET-IE: not enough data to decode reply "
246 "(%d bytes received vs %zu needed)\n",
247 result, sizeof(reply));
249 } else if (reply.bResultCode != UWB_RC_RES_SUCCESS) {
250 dev_err(dev, "SET-IE: command execution failed: %s (%d)\n",
251 uwb_rc_strerror(reply.bResultCode), reply.bResultCode);
260 * Determine by IE id if IE is host settable
261 * WUSB 1.0 [8.6.2.8 Table 8.85]
264 * All but UWB_IE_WLP appears in Table 8.85 from WUSB 1.0. Setting this IE
265 * is required for the WLP substack to perform association with its WSS so
266 * we hope that the WUSB spec will be changed to reflect this.
269 int uwb_rc_ie_is_host_settable(enum uwb_ie element_id)
271 if (element_id == UWB_PCA_AVAILABILITY ||
272 element_id == UWB_BP_SWITCH_IE ||
273 element_id == UWB_MAC_CAPABILITIES_IE ||
274 element_id == UWB_PHY_CAPABILITIES_IE ||
275 element_id == UWB_APP_SPEC_PROBE_IE ||
276 element_id == UWB_IDENTIFICATION_IE ||
277 element_id == UWB_MASTER_KEY_ID_IE ||
278 element_id == UWB_IE_WLP ||
279 element_id == UWB_APP_SPEC_IE)
286 * Extract Host Settable IEs from IE
288 * @ie_data: pointer to buffer containing all IEs
289 * @size: size of buffer
291 * @returns: length of buffer that only includes host settable IEs
293 * Given a buffer of IEs we move all Host Settable IEs to front of buffer
294 * by overwriting the IEs that are not Host Settable.
295 * Buffer length is adjusted accordingly.
298 ssize_t uwb_rc_parse_host_settable_ie(struct uwb_dev *uwb_dev,
299 void *ie_data, size_t size)
301 size_t new_len = size;
302 struct uwb_ie_hdr *ie_hdr;
304 void *itr = ie_data, *top = itr + size;
307 if (uwb_rc_ie_verify(uwb_dev, ie_data, itr, top) != 0)
310 ie_length = sizeof(*ie_hdr) + ie_hdr->length;
311 if (uwb_rc_ie_is_host_settable(ie_hdr->element_id)) {
314 memmove(itr, itr + ie_length, top - (itr + ie_length));
315 new_len -= ie_length;
323 /* Cleanup the whole IE management subsystem */
324 void uwb_rc_ie_init(struct uwb_rc *uwb_rc)
326 mutex_init(&uwb_rc->ies_mutex);
331 * Set up cache for host settable IEs currently being transmitted
333 * First we just call GET-IE to get the current IEs being transmitted
334 * (or we workaround and pretend we did) and (because the format is
335 * the same) reuse that as the IE cache (with the command prefix, as
336 * explained in 'struct uwb_rc').
338 * @returns: size of cache created
340 ssize_t uwb_rc_ie_setup(struct uwb_rc *uwb_rc)
342 struct device *dev = &uwb_rc->uwb_dev.dev;
345 struct uwb_rc_evt_get_ie *ie_info;
347 d_fnstart(3, dev, "(%p)\n", uwb_rc);
348 mutex_lock(&uwb_rc->ies_mutex);
349 result = uwb_rc_get_ie(uwb_rc, &ie_info);
353 d_printf(5, dev, "Got IEs %zu bytes (%zu long at %p)\n", result,
354 (size_t)le16_to_cpu(ie_info->wIELength), ie_info);
356 /* Remove IEs that host should not set. */
357 result = uwb_rc_parse_host_settable_ie(&uwb_rc->uwb_dev,
358 ie_info->IEData, le16_to_cpu(ie_info->wIELength));
361 d_printf(5, dev, "purged non-settable IEs to %zu bytes\n", result);
362 uwb_rc->ies = (void *) ie_info;
363 uwb_rc->ies->rccb.bCommandType = UWB_RC_CET_GENERAL;
364 uwb_rc->ies->rccb.wCommand = cpu_to_le16(UWB_RC_CMD_SET_IE);
365 uwb_rc->ies_capacity = capacity;
366 d_printf(5, dev, "IE cache at %p %zu bytes, %zu capacity\n",
367 ie_info, result, capacity);
371 mutex_unlock(&uwb_rc->ies_mutex);
372 d_fnend(3, dev, "(%p) = %zu\n", uwb_rc, result);
377 /* Cleanup the whole IE management subsystem */
378 void uwb_rc_ie_release(struct uwb_rc *uwb_rc)
382 uwb_rc->ies_capacity = 0;
387 int __acc_size(struct uwb_dev *uwb_dev, const struct uwb_ie_hdr *ie_hdr,
388 size_t offset, void *_ctx)
390 size_t *acc_size = _ctx;
391 *acc_size += sizeof(*ie_hdr) + ie_hdr->length;
392 d_printf(6, &uwb_dev->dev, "new acc size %zu\n", *acc_size);
398 * Add a new IE to IEs currently being transmitted by device
400 * @ies: the buffer containing the new IE or IEs to be added to
401 * the device's beacon. The buffer will be verified for
402 * consistence (meaning the headers should be right) and
403 * consistent with the buffer size.
404 * @size: size of @ies (in bytes, total buffer size)
405 * @returns: 0 if ok, <0 errno code on error
407 * According to WHCI 0.95 [4.13.6] the driver will only receive the RCEB
408 * after the device sent the first beacon that includes the IEs specified
409 * in the SET IE command. We thus cannot send this command if the device is
410 * not beaconing. Instead, a SET IE command will be sent later right after
411 * we start beaconing.
413 * Setting an IE on the device will overwrite all current IEs in device. So
414 * we take the current IEs being transmitted by the device, append the
415 * new one, and call SET IE with all the IEs needed.
417 * The local IE cache will only be updated with the new IE if SET IE
418 * completed successfully.
420 int uwb_rc_ie_add(struct uwb_rc *uwb_rc,
421 const struct uwb_ie_hdr *ies, size_t size)
424 struct device *dev = &uwb_rc->uwb_dev.dev;
425 struct uwb_rc_cmd_set_ie *new_ies;
426 size_t ies_size, total_size, acc_size = 0;
428 if (uwb_rc->ies == NULL)
430 uwb_ie_for_each(&uwb_rc->uwb_dev, __acc_size, &acc_size, ies, size);
431 if (acc_size != size) {
432 dev_err(dev, "BUG: bad IEs, misconstructed headers "
433 "[%zu bytes reported vs %zu calculated]\n",
438 mutex_lock(&uwb_rc->ies_mutex);
439 ies_size = le16_to_cpu(uwb_rc->ies->wIELength);
440 total_size = sizeof(*uwb_rc->ies) + ies_size;
441 if (total_size + size > uwb_rc->ies_capacity) {
442 d_printf(4, dev, "Reallocating IE cache from %p capacity %zu "
443 "to capacity %zu\n", uwb_rc->ies, uwb_rc->ies_capacity,
445 new_ies = kzalloc(total_size + size, GFP_KERNEL);
446 if (new_ies == NULL) {
447 dev_err(dev, "No memory for adding new IE\n");
451 memcpy(new_ies, uwb_rc->ies, total_size);
452 uwb_rc->ies_capacity = total_size + size;
454 uwb_rc->ies = new_ies;
455 d_printf(4, dev, "New IE cache at %p capacity %zu\n",
456 uwb_rc->ies, uwb_rc->ies_capacity);
458 memcpy((void *)uwb_rc->ies + total_size, ies, size);
459 uwb_rc->ies->wIELength = cpu_to_le16(ies_size + size);
460 if (uwb_rc->beaconing != -1) {
461 result = uwb_rc_set_ie(uwb_rc, uwb_rc->ies);
463 dev_err(dev, "Cannot set new IE on device: %d\n",
465 uwb_rc->ies->wIELength = cpu_to_le16(ies_size);
469 d_printf(4, dev, "IEs now occupy %hu bytes of %zu capacity at %p\n",
470 le16_to_cpu(uwb_rc->ies->wIELength), uwb_rc->ies_capacity,
473 mutex_unlock(&uwb_rc->ies_mutex);
476 EXPORT_SYMBOL_GPL(uwb_rc_ie_add);
480 * Remove an IE from internal cache
482 * We are dealing with our internal IE cache so no need to verify that the
483 * IEs are valid (it has been done already).
485 * Should be called with ies_mutex held
487 * We do not break out once an IE is found in the cache. It is currently
488 * possible to have more than one IE with the same ID included in the
489 * beacon. We don't reallocate, we just mark the size smaller.
492 int uwb_rc_ie_cache_rm(struct uwb_rc *uwb_rc, enum uwb_ie to_remove)
494 struct uwb_ie_hdr *ie_hdr;
495 size_t new_len = le16_to_cpu(uwb_rc->ies->wIELength);
496 void *itr = uwb_rc->ies->IEData;
497 void *top = itr + new_len;
501 if (ie_hdr->element_id != to_remove) {
502 itr += sizeof(*ie_hdr) + ie_hdr->length;
505 ie_length = sizeof(*ie_hdr) + ie_hdr->length;
506 if (top - itr != ie_length)
507 memmove(itr, itr + ie_length, top - itr + ie_length);
509 new_len -= ie_length;
512 uwb_rc->ies->wIELength = cpu_to_le16(new_len);
518 * Remove an IE currently being transmitted by device
520 * @element_id: id of IE to be removed from device's beacon
522 int uwb_rc_ie_rm(struct uwb_rc *uwb_rc, enum uwb_ie element_id)
524 struct device *dev = &uwb_rc->uwb_dev.dev;
527 if (uwb_rc->ies == NULL)
529 mutex_lock(&uwb_rc->ies_mutex);
530 result = uwb_rc_ie_cache_rm(uwb_rc, element_id);
532 dev_err(dev, "Cannot remove IE from cache.\n");
533 if (uwb_rc->beaconing != -1) {
534 result = uwb_rc_set_ie(uwb_rc, uwb_rc->ies);
536 dev_err(dev, "Cannot set new IE on device.\n");
538 mutex_unlock(&uwb_rc->ies_mutex);
541 EXPORT_SYMBOL_GPL(uwb_rc_ie_rm);