Blame view

drivers/uwb/ie.c 9.64 KB
22d203ece   Inaky Perez-Gonzalez   uwb: add the UWB ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  /*
   * Ultra Wide Band
   * Information Element Handling
   *
   * Copyright (C) 2005-2006 Intel Corporation
   * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
   * Reinette Chatre <reinette.chatre@intel.com>
   *
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU General Public License version
   * 2 as published by the Free Software Foundation.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write to the Free Software
   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
   * 02110-1301, USA.
   *
   *
   * FIXME: docs
   */
5a0e3ad6a   Tejun Heo   include cleanup: ...
26
  #include <linux/slab.h>
475c0a6b2   Paul Gortmaker   uwb: Add export.h...
27
  #include <linux/export.h>
22d203ece   Inaky Perez-Gonzalez   uwb: add the UWB ...
28
  #include "uwb-internal.h"
22d203ece   Inaky Perez-Gonzalez   uwb: add the UWB ...
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
  
  /**
   * uwb_ie_next - get the next IE in a buffer
   * @ptr: start of the buffer containing the IE data
   * @len: length of the buffer
   *
   * Both @ptr and @len are updated so subsequent calls to uwb_ie_next()
   * will get the next IE.
   *
   * NULL is returned (and @ptr and @len will not be updated) if there
   * are no more IEs in the buffer or the buffer is too short.
   */
  struct uwb_ie_hdr *uwb_ie_next(void **ptr, size_t *len)
  {
  	struct uwb_ie_hdr *hdr;
  	size_t ie_len;
  
  	if (*len < sizeof(struct uwb_ie_hdr))
  		return NULL;
  
  	hdr = *ptr;
  	ie_len = sizeof(struct uwb_ie_hdr) + hdr->length;
  
  	if (*len < ie_len)
  		return NULL;
  
  	*ptr += ie_len;
  	*len -= ie_len;
  
  	return hdr;
  }
  EXPORT_SYMBOL_GPL(uwb_ie_next);
  
  /**
1cde7f68c   David Vrabel   uwb: order IEs by...
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
   * uwb_ie_dump_hex - print IEs to a character buffer
   * @ies: the IEs to print.
   * @len: length of all the IEs.
   * @buf: the destination buffer.
   * @size: size of @buf.
   *
   * Returns the number of characters written.
   */
  int uwb_ie_dump_hex(const struct uwb_ie_hdr *ies, size_t len,
  		    char *buf, size_t size)
  {
  	void *ptr;
  	const struct uwb_ie_hdr *ie;
  	int r = 0;
  	u8 *d;
  
  	ptr = (void *)ies;
  	for (;;) {
  		ie = uwb_ie_next(&ptr, &len);
  		if (!ie)
  			break;
  
  		r += scnprintf(buf + r, size - r, "%02x %02x",
  			       (unsigned)ie->element_id,
  			       (unsigned)ie->length);
  		d = (uint8_t *)ie + sizeof(struct uwb_ie_hdr);
  		while (d != ptr && r < size)
  			r += scnprintf(buf + r, size - r, " %02x", (unsigned)*d++);
  		if (r < size)
  			buf[r++] = '
  ';
  	};
  
  	return r;
  }
  
  /**
22d203ece   Inaky Perez-Gonzalez   uwb: add the UWB ...
100
101
102
103
104
105
106
107
108
   * Get the IEs that a radio controller is sending in its beacon
   *
   * @uwb_rc:  UWB Radio Controller
   * @returns: Size read from the system
   *
   * We don't need to lock the uwb_rc's mutex because we don't modify
   * anything. Once done with the iedata buffer, call
   * uwb_rc_ie_release(iedata). Don't call kfree on it.
   */
1cde7f68c   David Vrabel   uwb: order IEs by...
109
  static
22d203ece   Inaky Perez-Gonzalez   uwb: add the UWB ...
110
111
112
113
114
115
116
  ssize_t uwb_rc_get_ie(struct uwb_rc *uwb_rc, struct uwb_rc_evt_get_ie **pget_ie)
  {
  	ssize_t result;
  	struct device *dev = &uwb_rc->uwb_dev.dev;
  	struct uwb_rccb *cmd = NULL;
  	struct uwb_rceb *reply = NULL;
  	struct uwb_rc_evt_get_ie *get_ie;
22d203ece   Inaky Perez-Gonzalez   uwb: add the UWB ...
117
118
  	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  	if (cmd == NULL)
1cde7f68c   David Vrabel   uwb: order IEs by...
119
  		return -ENOMEM;
22d203ece   Inaky Perez-Gonzalez   uwb: add the UWB ...
120
121
122
123
124
  	cmd->bCommandType = UWB_RC_CET_GENERAL;
  	cmd->wCommand = cpu_to_le16(UWB_RC_CMD_GET_IE);
  	result = uwb_rc_vcmd(uwb_rc, "GET_IE", cmd, sizeof(*cmd),
  			     UWB_RC_CET_GENERAL, UWB_RC_CMD_GET_IE,
  			     &reply);
1cde7f68c   David Vrabel   uwb: order IEs by...
125
  	kfree(cmd);
22d203ece   Inaky Perez-Gonzalez   uwb: add the UWB ...
126
  	if (result < 0)
1cde7f68c   David Vrabel   uwb: order IEs by...
127
  		return result;
22d203ece   Inaky Perez-Gonzalez   uwb: add the UWB ...
128
129
130
131
132
133
  	get_ie = container_of(reply, struct uwb_rc_evt_get_ie, rceb);
  	if (result < sizeof(*get_ie)) {
  		dev_err(dev, "not enough data returned for decoding GET IE "
  			"(%zu bytes received vs %zu needed)
  ",
  			result, sizeof(*get_ie));
1cde7f68c   David Vrabel   uwb: order IEs by...
134
  		return -EINVAL;
22d203ece   Inaky Perez-Gonzalez   uwb: add the UWB ...
135
136
137
138
139
  	} else if (result < sizeof(*get_ie) + le16_to_cpu(get_ie->wIELength)) {
  		dev_err(dev, "not enough data returned for decoding GET IE "
  			"payload (%zu bytes received vs %zu needed)
  ", result,
  			sizeof(*get_ie) + le16_to_cpu(get_ie->wIELength));
22d203ece   Inaky Perez-Gonzalez   uwb: add the UWB ...
140
141
  		return -EINVAL;
  	}
22d203ece   Inaky Perez-Gonzalez   uwb: add the UWB ...
142

1cde7f68c   David Vrabel   uwb: order IEs by...
143
  	*pget_ie = get_ie;
22d203ece   Inaky Perez-Gonzalez   uwb: add the UWB ...
144
145
  	return result;
  }
22d203ece   Inaky Perez-Gonzalez   uwb: add the UWB ...
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
  
  
  /**
   * Replace all IEs currently being transmitted by a device
   *
   * @cmd:    pointer to the SET-IE command with the IEs to set
   * @size:   size of @buf
   */
  int uwb_rc_set_ie(struct uwb_rc *rc, struct uwb_rc_cmd_set_ie *cmd)
  {
  	int result;
  	struct device *dev = &rc->uwb_dev.dev;
  	struct uwb_rc_evt_set_ie reply;
  
  	reply.rceb.bEventType = UWB_RC_CET_GENERAL;
  	reply.rceb.wEvent = UWB_RC_CMD_SET_IE;
  	result = uwb_rc_cmd(rc, "SET-IE", &cmd->rccb,
  			    sizeof(*cmd) + le16_to_cpu(cmd->wIELength),
  			    &reply.rceb, sizeof(reply));
  	if (result < 0)
  		goto error_cmd;
  	else if (result != sizeof(reply)) {
  		dev_err(dev, "SET-IE: not enough data to decode reply "
  			"(%d bytes received vs %zu needed)
  ",
  			result, sizeof(reply));
  		result = -EIO;
  	} else if (reply.bResultCode != UWB_RC_RES_SUCCESS) {
  		dev_err(dev, "SET-IE: command execution failed: %s (%d)
  ",
  			uwb_rc_strerror(reply.bResultCode), reply.bResultCode);
  		result = -EIO;
  	} else
  		result = 0;
  error_cmd:
  	return result;
  }
22d203ece   Inaky Perez-Gonzalez   uwb: add the UWB ...
183
184
185
186
187
188
189
190
  /* Cleanup the whole IE management subsystem */
  void uwb_rc_ie_init(struct uwb_rc *uwb_rc)
  {
  	mutex_init(&uwb_rc->ies_mutex);
  }
  
  
  /**
1cde7f68c   David Vrabel   uwb: order IEs by...
191
192
   * uwb_rc_ie_setup - setup a radio controller's IE manager
   * @uwb_rc: the radio controller.
22d203ece   Inaky Perez-Gonzalez   uwb: add the UWB ...
193
   *
1cde7f68c   David Vrabel   uwb: order IEs by...
194
195
196
   * The current set of IEs are obtained from the hardware with a GET-IE
   * command (since the radio controller is not yet beaconing this will
   * be just the hardware's MAC and PHY Capability IEs).
22d203ece   Inaky Perez-Gonzalez   uwb: add the UWB ...
197
   *
1cde7f68c   David Vrabel   uwb: order IEs by...
198
   * Returns 0 on success; -ve on an error.
22d203ece   Inaky Perez-Gonzalez   uwb: add the UWB ...
199
   */
1cde7f68c   David Vrabel   uwb: order IEs by...
200
  int uwb_rc_ie_setup(struct uwb_rc *uwb_rc)
22d203ece   Inaky Perez-Gonzalez   uwb: add the UWB ...
201
  {
1cde7f68c   David Vrabel   uwb: order IEs by...
202
203
204
205
206
207
  	struct uwb_rc_evt_get_ie *ie_info = NULL;
  	int capacity;
  
  	capacity = uwb_rc_get_ie(uwb_rc, &ie_info);
  	if (capacity < 0)
  		return capacity;
22d203ece   Inaky Perez-Gonzalez   uwb: add the UWB ...
208

22d203ece   Inaky Perez-Gonzalez   uwb: add the UWB ...
209
  	mutex_lock(&uwb_rc->ies_mutex);
1cde7f68c   David Vrabel   uwb: order IEs by...
210
211
  
  	uwb_rc->ies = (struct uwb_rc_cmd_set_ie *)ie_info;
22d203ece   Inaky Perez-Gonzalez   uwb: add the UWB ...
212
213
214
  	uwb_rc->ies->rccb.bCommandType = UWB_RC_CET_GENERAL;
  	uwb_rc->ies->rccb.wCommand = cpu_to_le16(UWB_RC_CMD_SET_IE);
  	uwb_rc->ies_capacity = capacity;
1cde7f68c   David Vrabel   uwb: order IEs by...
215

22d203ece   Inaky Perez-Gonzalez   uwb: add the UWB ...
216
  	mutex_unlock(&uwb_rc->ies_mutex);
1cde7f68c   David Vrabel   uwb: order IEs by...
217
218
  
  	return 0;
22d203ece   Inaky Perez-Gonzalez   uwb: add the UWB ...
219
220
221
222
223
224
225
226
227
228
  }
  
  
  /* Cleanup the whole IE management subsystem */
  void uwb_rc_ie_release(struct uwb_rc *uwb_rc)
  {
  	kfree(uwb_rc->ies);
  	uwb_rc->ies = NULL;
  	uwb_rc->ies_capacity = 0;
  }
1cde7f68c   David Vrabel   uwb: order IEs by...
229
  static int uwb_rc_ie_add_one(struct uwb_rc *rc, const struct uwb_ie_hdr *new_ie)
22d203ece   Inaky Perez-Gonzalez   uwb: add the UWB ...
230
  {
1cde7f68c   David Vrabel   uwb: order IEs by...
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
  	struct uwb_rc_cmd_set_ie *new_ies;
  	void *ptr, *prev_ie;
  	struct uwb_ie_hdr *ie;
  	size_t length, new_ie_len, new_capacity, size, prev_size;
  
  	length = le16_to_cpu(rc->ies->wIELength);
  	new_ie_len = sizeof(struct uwb_ie_hdr) + new_ie->length;
  	new_capacity = sizeof(struct uwb_rc_cmd_set_ie) + length + new_ie_len;
  
  	if (new_capacity > rc->ies_capacity) {
  		new_ies = krealloc(rc->ies, new_capacity, GFP_KERNEL);
  		if (!new_ies)
  			return -ENOMEM;
  		rc->ies = new_ies;
  	}
  
  	ptr = rc->ies->IEData;
  	size = length;
  	for (;;) {
  		prev_ie = ptr;
  		prev_size = size;
  		ie = uwb_ie_next(&ptr, &size);
  		if (!ie || ie->element_id > new_ie->element_id)
  			break;
  	}
  
  	memmove(prev_ie + new_ie_len, prev_ie, prev_size);
  	memcpy(prev_ie, new_ie, new_ie_len);
  	rc->ies->wIELength = cpu_to_le16(length + new_ie_len);
22d203ece   Inaky Perez-Gonzalez   uwb: add the UWB ...
260
261
  	return 0;
  }
22d203ece   Inaky Perez-Gonzalez   uwb: add the UWB ...
262
  /**
1cde7f68c   David Vrabel   uwb: order IEs by...
263
264
   * uwb_rc_ie_add - add new IEs to the radio controller's beacon
   * @uwb_rc: the radio controller.
22d203ece   Inaky Perez-Gonzalez   uwb: add the UWB ...
265
   * @ies: the buffer containing the new IE or IEs to be added to
1cde7f68c   David Vrabel   uwb: order IEs by...
266
267
   *       the device's beacon.
   * @size: length of all the IEs.
22d203ece   Inaky Perez-Gonzalez   uwb: add the UWB ...
268
269
270
271
272
273
274
275
   *
   * According to WHCI 0.95 [4.13.6] the driver will only receive the RCEB
   * after the device sent the first beacon that includes the IEs specified
   * in the SET IE command. We thus cannot send this command if the device is
   * not beaconing. Instead, a SET IE command will be sent later right after
   * we start beaconing.
   *
   * Setting an IE on the device will overwrite all current IEs in device. So
1cde7f68c   David Vrabel   uwb: order IEs by...
276
   * we take the current IEs being transmitted by the device, insert the
22d203ece   Inaky Perez-Gonzalez   uwb: add the UWB ...
277
278
   * new one, and call SET IE with all the IEs needed.
   *
1cde7f68c   David Vrabel   uwb: order IEs by...
279
   * Returns 0 on success; or -ENOMEM.
22d203ece   Inaky Perez-Gonzalez   uwb: add the UWB ...
280
281
282
283
284
   */
  int uwb_rc_ie_add(struct uwb_rc *uwb_rc,
  		  const struct uwb_ie_hdr *ies, size_t size)
  {
  	int result = 0;
1cde7f68c   David Vrabel   uwb: order IEs by...
285
286
  	void *ptr;
  	const struct uwb_ie_hdr *ie;
22d203ece   Inaky Perez-Gonzalez   uwb: add the UWB ...
287
  	mutex_lock(&uwb_rc->ies_mutex);
1cde7f68c   David Vrabel   uwb: order IEs by...
288
289
290
291
292
293
294
295
296
297
  
  	ptr = (void *)ies;
  	for (;;) {
  		ie = uwb_ie_next(&ptr, &size);
  		if (!ie)
  			break;
  
  		result = uwb_rc_ie_add_one(uwb_rc, ie);
  		if (result < 0)
  			break;
22d203ece   Inaky Perez-Gonzalez   uwb: add the UWB ...
298
  	}
1cde7f68c   David Vrabel   uwb: order IEs by...
299
300
301
302
  	if (result >= 0) {
  		if (size == 0) {
  			if (uwb_rc->beaconing != -1)
  				result = uwb_rc_set_ie(uwb_rc, uwb_rc->ies);
22d203ece   Inaky Perez-Gonzalez   uwb: add the UWB ...
303
  		} else
1cde7f68c   David Vrabel   uwb: order IEs by...
304
  			result = -EINVAL;
22d203ece   Inaky Perez-Gonzalez   uwb: add the UWB ...
305
  	}
1cde7f68c   David Vrabel   uwb: order IEs by...
306

22d203ece   Inaky Perez-Gonzalez   uwb: add the UWB ...
307
  	mutex_unlock(&uwb_rc->ies_mutex);
1cde7f68c   David Vrabel   uwb: order IEs by...
308

22d203ece   Inaky Perez-Gonzalez   uwb: add the UWB ...
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
  	return result;
  }
  EXPORT_SYMBOL_GPL(uwb_rc_ie_add);
  
  
  /*
   * Remove an IE from internal cache
   *
   * We are dealing with our internal IE cache so no need to verify that the
   * IEs are valid (it has been done already).
   *
   * Should be called with ies_mutex held
   *
   * We do not break out once an IE is found in the cache. It is currently
   * possible to have more than one IE with the same ID included in the
   * beacon. We don't reallocate, we just mark the size smaller.
   */
  static
1cde7f68c   David Vrabel   uwb: order IEs by...
327
  void uwb_rc_ie_cache_rm(struct uwb_rc *uwb_rc, enum uwb_ie to_remove)
22d203ece   Inaky Perez-Gonzalez   uwb: add the UWB ...
328
  {
1cde7f68c   David Vrabel   uwb: order IEs by...
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
  	struct uwb_ie_hdr *ie;
  	size_t len = le16_to_cpu(uwb_rc->ies->wIELength);
  	void *ptr;
  	size_t size;
  
  	ptr = uwb_rc->ies->IEData;
  	size = len;
  	for (;;) {
  		ie = uwb_ie_next(&ptr, &size);
  		if (!ie)
  			break;
  		if (ie->element_id == to_remove) {
  			len -= sizeof(struct uwb_ie_hdr) + ie->length;
  			memmove(ie, ptr, size);
  			ptr = ie;
22d203ece   Inaky Perez-Gonzalez   uwb: add the UWB ...
344
345
  		}
  	}
1cde7f68c   David Vrabel   uwb: order IEs by...
346
  	uwb_rc->ies->wIELength = cpu_to_le16(len);
22d203ece   Inaky Perez-Gonzalez   uwb: add the UWB ...
347
348
349
350
  }
  
  
  /**
1cde7f68c   David Vrabel   uwb: order IEs by...
351
352
353
   * uwb_rc_ie_rm - remove an IE from the radio controller's beacon
   * @uwb_rc: the radio controller.
   * @element_id: the element ID of the IE to remove.
22d203ece   Inaky Perez-Gonzalez   uwb: add the UWB ...
354
   *
1cde7f68c   David Vrabel   uwb: order IEs by...
355
356
357
358
   * Only IEs previously added with uwb_rc_ie_add() may be removed.
   *
   * Returns 0 on success; or -ve the SET-IE command to the radio
   * controller failed.
22d203ece   Inaky Perez-Gonzalez   uwb: add the UWB ...
359
360
361
   */
  int uwb_rc_ie_rm(struct uwb_rc *uwb_rc, enum uwb_ie element_id)
  {
1cde7f68c   David Vrabel   uwb: order IEs by...
362
  	int result = 0;
22d203ece   Inaky Perez-Gonzalez   uwb: add the UWB ...
363

22d203ece   Inaky Perez-Gonzalez   uwb: add the UWB ...
364
  	mutex_lock(&uwb_rc->ies_mutex);
1cde7f68c   David Vrabel   uwb: order IEs by...
365
366
367
368
  
  	uwb_rc_ie_cache_rm(uwb_rc, element_id);
  
  	if (uwb_rc->beaconing != -1)
22d203ece   Inaky Perez-Gonzalez   uwb: add the UWB ...
369
  		result = uwb_rc_set_ie(uwb_rc, uwb_rc->ies);
1cde7f68c   David Vrabel   uwb: order IEs by...
370

22d203ece   Inaky Perez-Gonzalez   uwb: add the UWB ...
371
  	mutex_unlock(&uwb_rc->ies_mutex);
1cde7f68c   David Vrabel   uwb: order IEs by...
372

22d203ece   Inaky Perez-Gonzalez   uwb: add the UWB ...
373
374
375
  	return result;
  }
  EXPORT_SYMBOL_GPL(uwb_rc_ie_rm);