1 // SPDX-License-Identifier: GPL-2.0-only
4 * Information Element Handling
6 * Copyright (C) 2005-2006 Intel Corporation
7 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
8 * Reinette Chatre <reinette.chatre@intel.com>
13 #include <linux/slab.h>
14 #include <linux/export.h>
15 #include "uwb-internal.h"
18 * uwb_ie_next - get the next IE in a buffer
19 * @ptr: start of the buffer containing the IE data
20 * @len: length of the buffer
22 * Both @ptr and @len are updated so subsequent calls to uwb_ie_next()
23 * will get the next IE.
25 * NULL is returned (and @ptr and @len will not be updated) if there
26 * are no more IEs in the buffer or the buffer is too short.
28 struct uwb_ie_hdr
*uwb_ie_next(void **ptr
, size_t *len
)
30 struct uwb_ie_hdr
*hdr
;
33 if (*len
< sizeof(struct uwb_ie_hdr
))
37 ie_len
= sizeof(struct uwb_ie_hdr
) + hdr
->length
;
47 EXPORT_SYMBOL_GPL(uwb_ie_next
);
50 * uwb_ie_dump_hex - print IEs to a character buffer
51 * @ies: the IEs to print.
52 * @len: length of all the IEs.
53 * @buf: the destination buffer.
54 * @size: size of @buf.
56 * Returns the number of characters written.
58 int uwb_ie_dump_hex(const struct uwb_ie_hdr
*ies
, size_t len
,
59 char *buf
, size_t size
)
62 const struct uwb_ie_hdr
*ie
;
68 ie
= uwb_ie_next(&ptr
, &len
);
72 r
+= scnprintf(buf
+ r
, size
- r
, "%02x %02x",
73 (unsigned)ie
->element_id
,
74 (unsigned)ie
->length
);
75 d
= (uint8_t *)ie
+ sizeof(struct uwb_ie_hdr
);
76 while (d
!= ptr
&& r
< size
)
77 r
+= scnprintf(buf
+ r
, size
- r
, " %02x", (unsigned)*d
++);
86 * Get the IEs that a radio controller is sending in its beacon
88 * @uwb_rc: UWB Radio Controller
89 * @returns: Size read from the system
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.
96 ssize_t
uwb_rc_get_ie(struct uwb_rc
*uwb_rc
, struct uwb_rc_evt_get_ie
**pget_ie
)
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
;
104 cmd
= kzalloc(sizeof(*cmd
), GFP_KERNEL
);
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
,
117 get_ie
= container_of(reply
, struct uwb_rc_evt_get_ie
, rceb
);
118 if (result
< sizeof(*get_ie
)) {
119 dev_err(dev
, "not enough data returned for decoding GET IE "
120 "(%zu bytes received vs %zu needed)\n",
121 result
, sizeof(*get_ie
));
123 } else if (result
< sizeof(*get_ie
) + le16_to_cpu(get_ie
->wIELength
)) {
124 dev_err(dev
, "not enough data returned for decoding GET IE "
125 "payload (%zu bytes received vs %zu needed)\n", result
,
126 sizeof(*get_ie
) + le16_to_cpu(get_ie
->wIELength
));
136 * Replace all IEs currently being transmitted by a device
138 * @cmd: pointer to the SET-IE command with the IEs to set
139 * @size: size of @buf
141 int uwb_rc_set_ie(struct uwb_rc
*rc
, struct uwb_rc_cmd_set_ie
*cmd
)
144 struct device
*dev
= &rc
->uwb_dev
.dev
;
145 struct uwb_rc_evt_set_ie reply
;
147 reply
.rceb
.bEventType
= UWB_RC_CET_GENERAL
;
148 reply
.rceb
.wEvent
= UWB_RC_CMD_SET_IE
;
149 result
= uwb_rc_cmd(rc
, "SET-IE", &cmd
->rccb
,
150 sizeof(*cmd
) + le16_to_cpu(cmd
->wIELength
),
151 &reply
.rceb
, sizeof(reply
));
154 else if (result
!= sizeof(reply
)) {
155 dev_err(dev
, "SET-IE: not enough data to decode reply "
156 "(%d bytes received vs %zu needed)\n",
157 result
, sizeof(reply
));
159 } else if (reply
.bResultCode
!= UWB_RC_RES_SUCCESS
) {
160 dev_err(dev
, "SET-IE: command execution failed: %s (%d)\n",
161 uwb_rc_strerror(reply
.bResultCode
), reply
.bResultCode
);
169 /* Cleanup the whole IE management subsystem */
170 void uwb_rc_ie_init(struct uwb_rc
*uwb_rc
)
172 mutex_init(&uwb_rc
->ies_mutex
);
177 * uwb_rc_ie_setup - setup a radio controller's IE manager
178 * @uwb_rc: the radio controller.
180 * The current set of IEs are obtained from the hardware with a GET-IE
181 * command (since the radio controller is not yet beaconing this will
182 * be just the hardware's MAC and PHY Capability IEs).
184 * Returns 0 on success; -ve on an error.
186 int uwb_rc_ie_setup(struct uwb_rc
*uwb_rc
)
188 struct uwb_rc_evt_get_ie
*ie_info
= NULL
;
191 capacity
= uwb_rc_get_ie(uwb_rc
, &ie_info
);
195 mutex_lock(&uwb_rc
->ies_mutex
);
197 uwb_rc
->ies
= (struct uwb_rc_cmd_set_ie
*)ie_info
;
198 uwb_rc
->ies
->rccb
.bCommandType
= UWB_RC_CET_GENERAL
;
199 uwb_rc
->ies
->rccb
.wCommand
= cpu_to_le16(UWB_RC_CMD_SET_IE
);
200 uwb_rc
->ies_capacity
= capacity
;
202 mutex_unlock(&uwb_rc
->ies_mutex
);
208 /* Cleanup the whole IE management subsystem */
209 void uwb_rc_ie_release(struct uwb_rc
*uwb_rc
)
213 uwb_rc
->ies_capacity
= 0;
217 static int uwb_rc_ie_add_one(struct uwb_rc
*rc
, const struct uwb_ie_hdr
*new_ie
)
219 struct uwb_rc_cmd_set_ie
*new_ies
;
221 struct uwb_ie_hdr
*ie
;
222 size_t length
, new_ie_len
, new_capacity
, size
, prev_size
;
224 length
= le16_to_cpu(rc
->ies
->wIELength
);
225 new_ie_len
= sizeof(struct uwb_ie_hdr
) + new_ie
->length
;
226 new_capacity
= sizeof(struct uwb_rc_cmd_set_ie
) + length
+ new_ie_len
;
228 if (new_capacity
> rc
->ies_capacity
) {
229 new_ies
= krealloc(rc
->ies
, new_capacity
, GFP_KERNEL
);
235 ptr
= rc
->ies
->IEData
;
240 ie
= uwb_ie_next(&ptr
, &size
);
241 if (!ie
|| ie
->element_id
> new_ie
->element_id
)
245 memmove(prev_ie
+ new_ie_len
, prev_ie
, prev_size
);
246 memcpy(prev_ie
, new_ie
, new_ie_len
);
247 rc
->ies
->wIELength
= cpu_to_le16(length
+ new_ie_len
);
253 * uwb_rc_ie_add - add new IEs to the radio controller's beacon
254 * @uwb_rc: the radio controller.
255 * @ies: the buffer containing the new IE or IEs to be added to
256 * the device's beacon.
257 * @size: length of all the IEs.
259 * According to WHCI 0.95 [4.13.6] the driver will only receive the RCEB
260 * after the device sent the first beacon that includes the IEs specified
261 * in the SET IE command. We thus cannot send this command if the device is
262 * not beaconing. Instead, a SET IE command will be sent later right after
263 * we start beaconing.
265 * Setting an IE on the device will overwrite all current IEs in device. So
266 * we take the current IEs being transmitted by the device, insert the
267 * new one, and call SET IE with all the IEs needed.
269 * Returns 0 on success; or -ENOMEM.
271 int uwb_rc_ie_add(struct uwb_rc
*uwb_rc
,
272 const struct uwb_ie_hdr
*ies
, size_t size
)
276 const struct uwb_ie_hdr
*ie
;
278 mutex_lock(&uwb_rc
->ies_mutex
);
282 ie
= uwb_ie_next(&ptr
, &size
);
286 result
= uwb_rc_ie_add_one(uwb_rc
, ie
);
292 if (uwb_rc
->beaconing
!= -1)
293 result
= uwb_rc_set_ie(uwb_rc
, uwb_rc
->ies
);
298 mutex_unlock(&uwb_rc
->ies_mutex
);
302 EXPORT_SYMBOL_GPL(uwb_rc_ie_add
);
306 * Remove an IE from internal cache
308 * We are dealing with our internal IE cache so no need to verify that the
309 * IEs are valid (it has been done already).
311 * Should be called with ies_mutex held
313 * We do not break out once an IE is found in the cache. It is currently
314 * possible to have more than one IE with the same ID included in the
315 * beacon. We don't reallocate, we just mark the size smaller.
318 void uwb_rc_ie_cache_rm(struct uwb_rc
*uwb_rc
, enum uwb_ie to_remove
)
320 struct uwb_ie_hdr
*ie
;
321 size_t len
= le16_to_cpu(uwb_rc
->ies
->wIELength
);
325 ptr
= uwb_rc
->ies
->IEData
;
328 ie
= uwb_ie_next(&ptr
, &size
);
331 if (ie
->element_id
== to_remove
) {
332 len
-= sizeof(struct uwb_ie_hdr
) + ie
->length
;
333 memmove(ie
, ptr
, size
);
337 uwb_rc
->ies
->wIELength
= cpu_to_le16(len
);
342 * uwb_rc_ie_rm - remove an IE from the radio controller's beacon
343 * @uwb_rc: the radio controller.
344 * @element_id: the element ID of the IE to remove.
346 * Only IEs previously added with uwb_rc_ie_add() may be removed.
348 * Returns 0 on success; or -ve the SET-IE command to the radio
351 int uwb_rc_ie_rm(struct uwb_rc
*uwb_rc
, enum uwb_ie element_id
)
355 mutex_lock(&uwb_rc
->ies_mutex
);
357 uwb_rc_ie_cache_rm(uwb_rc
, element_id
);
359 if (uwb_rc
->beaconing
!= -1)
360 result
= uwb_rc_set_ie(uwb_rc
, uwb_rc
->ies
);
362 mutex_unlock(&uwb_rc
->ies_mutex
);
366 EXPORT_SYMBOL_GPL(uwb_rc_ie_rm
);