blob: 57b5ff61020c4b77556c0adc59b132627c536af7 [file] [log] [blame]
Inaky Perez-Gonzalez22d203e2008-09-17 16:34:08 +01001/*
2 * Ultra Wide Band
3 * Beacon management
4 *
5 * Copyright (C) 2005-2006 Intel Corporation
6 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version
10 * 2 as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 * 02110-1301, USA.
21 *
22 *
23 * FIXME: docs
24 */
Inaky Perez-Gonzalez22d203e2008-09-17 16:34:08 +010025#include <linux/kernel.h>
26#include <linux/init.h>
27#include <linux/module.h>
28#include <linux/device.h>
29#include <linux/err.h>
30#include <linux/kdev_t.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
David Vrabelbce83692008-12-22 18:22:50 +000032
Inaky Perez-Gonzalez22d203e2008-09-17 16:34:08 +010033#include "uwb-internal.h"
34
David Vrabelbce83692008-12-22 18:22:50 +000035/* Start Beaconing command structure */
Inaky Perez-Gonzalez22d203e2008-09-17 16:34:08 +010036struct uwb_rc_cmd_start_beacon {
37 struct uwb_rccb rccb;
38 __le16 wBPSTOffset;
39 u8 bChannelNumber;
40} __attribute__((packed));
41
42
43static int uwb_rc_start_beacon(struct uwb_rc *rc, u16 bpst_offset, u8 channel)
44{
45 int result;
46 struct uwb_rc_cmd_start_beacon *cmd;
47 struct uwb_rc_evt_confirm reply;
48
49 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
50 if (cmd == NULL)
51 return -ENOMEM;
52 cmd->rccb.bCommandType = UWB_RC_CET_GENERAL;
53 cmd->rccb.wCommand = cpu_to_le16(UWB_RC_CMD_START_BEACON);
54 cmd->wBPSTOffset = cpu_to_le16(bpst_offset);
55 cmd->bChannelNumber = channel;
56 reply.rceb.bEventType = UWB_RC_CET_GENERAL;
57 reply.rceb.wEvent = UWB_RC_CMD_START_BEACON;
58 result = uwb_rc_cmd(rc, "START-BEACON", &cmd->rccb, sizeof(*cmd),
59 &reply.rceb, sizeof(reply));
60 if (result < 0)
61 goto error_cmd;
62 if (reply.bResultCode != UWB_RC_RES_SUCCESS) {
63 dev_err(&rc->uwb_dev.dev,
64 "START-BEACON: command execution failed: %s (%d)\n",
65 uwb_rc_strerror(reply.bResultCode), reply.bResultCode);
66 result = -EIO;
67 }
68error_cmd:
69 kfree(cmd);
70 return result;
71}
72
73static int uwb_rc_stop_beacon(struct uwb_rc *rc)
74{
75 int result;
76 struct uwb_rccb *cmd;
77 struct uwb_rc_evt_confirm reply;
78
79 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
80 if (cmd == NULL)
81 return -ENOMEM;
82 cmd->bCommandType = UWB_RC_CET_GENERAL;
83 cmd->wCommand = cpu_to_le16(UWB_RC_CMD_STOP_BEACON);
84 reply.rceb.bEventType = UWB_RC_CET_GENERAL;
85 reply.rceb.wEvent = UWB_RC_CMD_STOP_BEACON;
86 result = uwb_rc_cmd(rc, "STOP-BEACON", cmd, sizeof(*cmd),
87 &reply.rceb, sizeof(reply));
88 if (result < 0)
89 goto error_cmd;
90 if (reply.bResultCode != UWB_RC_RES_SUCCESS) {
91 dev_err(&rc->uwb_dev.dev,
92 "STOP-BEACON: command execution failed: %s (%d)\n",
93 uwb_rc_strerror(reply.bResultCode), reply.bResultCode);
94 result = -EIO;
95 }
96error_cmd:
97 kfree(cmd);
98 return result;
99}
100
101/*
102 * Start/stop beacons
103 *
104 * @rc: UWB Radio Controller to operate on
105 * @channel: UWB channel on which to beacon (WUSB[table
106 * 5-12]). If -1, stop beaconing.
107 * @bpst_offset: Beacon Period Start Time offset; FIXME-do zero
108 *
109 * According to WHCI 0.95 [4.13.6] the driver will only receive the RCEB
110 * of a SET IE command after the device sent the first beacon that includes
111 * the IEs specified in the SET IE command. So, after we start beaconing we
112 * check if there is anything in the IE cache and call the SET IE command
113 * if needed.
114 */
115int uwb_rc_beacon(struct uwb_rc *rc, int channel, unsigned bpst_offset)
116{
117 int result;
118 struct device *dev = &rc->uwb_dev.dev;
119
Thomas Pugliesef79833a2013-12-20 11:54:07 -0600120 dev_dbg(dev, "%s: channel = %d\n", __func__, channel);
Inaky Perez-Gonzalez22d203e2008-09-17 16:34:08 +0100121 if (channel < 0)
122 channel = -1;
123 if (channel == -1)
124 result = uwb_rc_stop_beacon(rc);
125 else {
126 /* channel >= 0...dah */
127 result = uwb_rc_start_beacon(rc, bpst_offset, channel);
128 if (result < 0)
David Vrabel6fae35f2008-11-17 15:53:42 +0000129 return result;
Inaky Perez-Gonzalez22d203e2008-09-17 16:34:08 +0100130 if (le16_to_cpu(rc->ies->wIELength) > 0) {
131 result = uwb_rc_set_ie(rc, rc->ies);
132 if (result < 0) {
133 dev_err(dev, "Cannot set new IE on device: "
134 "%d\n", result);
135 result = uwb_rc_stop_beacon(rc);
136 channel = -1;
137 bpst_offset = 0;
David Vrabel6fae35f2008-11-17 15:53:42 +0000138 }
Inaky Perez-Gonzalez22d203e2008-09-17 16:34:08 +0100139 }
140 }
141
David Vrabel0996e632008-11-17 16:23:22 +0000142 if (result >= 0)
David Vrabel6fae35f2008-11-17 15:53:42 +0000143 rc->beaconing = channel;
Inaky Perez-Gonzalez22d203e2008-09-17 16:34:08 +0100144 return result;
145}
146
147/*
148 * Beacon cache
149 *
150 * The purpose of this is to speed up the lookup of becon information
151 * when a new beacon arrives. The UWB Daemon uses it also to keep a
152 * tab of which devices are in radio distance and which not. When a
153 * device's beacon stays present for more than a certain amount of
154 * time, it is considered a new, usable device. When a beacon ceases
155 * to be received for a certain amount of time, it is considered that
156 * the device is gone.
157 *
158 * FIXME: use an allocator for the entries
159 * FIXME: use something faster for search than a list
160 */
161
Inaky Perez-Gonzalez22d203e2008-09-17 16:34:08 +0100162void uwb_bce_kfree(struct kref *_bce)
163{
164 struct uwb_beca_e *bce = container_of(_bce, struct uwb_beca_e, refcnt);
165
166 kfree(bce->be);
167 kfree(bce);
168}
169
170
171/* Find a beacon by dev addr in the cache */
172static
Stefano Panellafec1a592008-11-04 15:39:08 +0000173struct uwb_beca_e *__uwb_beca_find_bydev(struct uwb_rc *rc,
174 const struct uwb_dev_addr *dev_addr)
Inaky Perez-Gonzalez22d203e2008-09-17 16:34:08 +0100175{
176 struct uwb_beca_e *bce, *next;
Stefano Panellafec1a592008-11-04 15:39:08 +0000177 list_for_each_entry_safe(bce, next, &rc->uwb_beca.list, node) {
Inaky Perez-Gonzalez22d203e2008-09-17 16:34:08 +0100178 if (!memcmp(&bce->dev_addr, dev_addr, sizeof(bce->dev_addr)))
179 goto out;
180 }
181 bce = NULL;
182out:
183 return bce;
184}
185
186/* Find a beacon by dev addr in the cache */
187static
Thomas Pugliese67d0fb22013-12-20 11:54:08 -0600188struct uwb_beca_e *__uwb_beca_find_bymac(struct uwb_rc *rc,
Stefano Panellafec1a592008-11-04 15:39:08 +0000189 const struct uwb_mac_addr *mac_addr)
Inaky Perez-Gonzalez22d203e2008-09-17 16:34:08 +0100190{
191 struct uwb_beca_e *bce, *next;
Stefano Panellafec1a592008-11-04 15:39:08 +0000192 list_for_each_entry_safe(bce, next, &rc->uwb_beca.list, node) {
Inaky Perez-Gonzalez22d203e2008-09-17 16:34:08 +0100193 if (!memcmp(bce->mac_addr, mac_addr->data,
Frank Leipoldc15895e2008-10-20 14:37:53 +0100194 sizeof(struct uwb_mac_addr)))
Inaky Perez-Gonzalez22d203e2008-09-17 16:34:08 +0100195 goto out;
196 }
197 bce = NULL;
198out:
199 return bce;
200}
201
202/**
203 * uwb_dev_get_by_devaddr - get a UWB device with a specific DevAddr
204 * @rc: the radio controller that saw the device
205 * @devaddr: DevAddr of the UWB device to find
206 *
207 * There may be more than one matching device (in the case of a
208 * DevAddr conflict), but only the first one is returned.
209 */
210struct uwb_dev *uwb_dev_get_by_devaddr(struct uwb_rc *rc,
211 const struct uwb_dev_addr *devaddr)
212{
213 struct uwb_dev *found = NULL;
214 struct uwb_beca_e *bce;
215
Stefano Panellafec1a592008-11-04 15:39:08 +0000216 mutex_lock(&rc->uwb_beca.mutex);
217 bce = __uwb_beca_find_bydev(rc, devaddr);
Inaky Perez-Gonzalez22d203e2008-09-17 16:34:08 +0100218 if (bce)
219 found = uwb_dev_try_get(rc, bce->uwb_dev);
Stefano Panellafec1a592008-11-04 15:39:08 +0000220 mutex_unlock(&rc->uwb_beca.mutex);
Inaky Perez-Gonzalez22d203e2008-09-17 16:34:08 +0100221
222 return found;
223}
224
225/**
226 * uwb_dev_get_by_macaddr - get a UWB device with a specific EUI-48
227 * @rc: the radio controller that saw the device
228 * @devaddr: EUI-48 of the UWB device to find
229 */
230struct uwb_dev *uwb_dev_get_by_macaddr(struct uwb_rc *rc,
231 const struct uwb_mac_addr *macaddr)
232{
233 struct uwb_dev *found = NULL;
234 struct uwb_beca_e *bce;
235
Stefano Panellafec1a592008-11-04 15:39:08 +0000236 mutex_lock(&rc->uwb_beca.mutex);
237 bce = __uwb_beca_find_bymac(rc, macaddr);
Inaky Perez-Gonzalez22d203e2008-09-17 16:34:08 +0100238 if (bce)
239 found = uwb_dev_try_get(rc, bce->uwb_dev);
Stefano Panellafec1a592008-11-04 15:39:08 +0000240 mutex_unlock(&rc->uwb_beca.mutex);
Inaky Perez-Gonzalez22d203e2008-09-17 16:34:08 +0100241
242 return found;
243}
244
245/* Initialize a beacon cache entry */
246static void uwb_beca_e_init(struct uwb_beca_e *bce)
247{
248 mutex_init(&bce->mutex);
249 kref_init(&bce->refcnt);
250 stats_init(&bce->lqe_stats);
251 stats_init(&bce->rssi_stats);
252}
253
254/*
255 * Add a beacon to the cache
256 *
257 * @be: Beacon event information
258 * @bf: Beacon frame (part of b, really)
259 * @ts_jiffies: Timestamp (in jiffies) when the beacon was received
260 */
Stefano Panellafec1a592008-11-04 15:39:08 +0000261static
262struct uwb_beca_e *__uwb_beca_add(struct uwb_rc *rc,
263 struct uwb_rc_evt_beacon *be,
Inaky Perez-Gonzalez22d203e2008-09-17 16:34:08 +0100264 struct uwb_beacon_frame *bf,
265 unsigned long ts_jiffies)
266{
267 struct uwb_beca_e *bce;
268
269 bce = kzalloc(sizeof(*bce), GFP_KERNEL);
270 if (bce == NULL)
271 return NULL;
272 uwb_beca_e_init(bce);
273 bce->ts_jiffies = ts_jiffies;
274 bce->uwb_dev = NULL;
Stefano Panellafec1a592008-11-04 15:39:08 +0000275 list_add(&bce->node, &rc->uwb_beca.list);
Inaky Perez-Gonzalez22d203e2008-09-17 16:34:08 +0100276 return bce;
277}
278
279/*
280 * Wipe out beacon entries that became stale
281 *
282 * Remove associated devicest too.
283 */
Stefano Panellafec1a592008-11-04 15:39:08 +0000284void uwb_beca_purge(struct uwb_rc *rc)
Inaky Perez-Gonzalez22d203e2008-09-17 16:34:08 +0100285{
286 struct uwb_beca_e *bce, *next;
David Vrabel45c16cd2008-10-15 14:41:56 +0100287 unsigned long expires;
288
Stefano Panellafec1a592008-11-04 15:39:08 +0000289 mutex_lock(&rc->uwb_beca.mutex);
290 list_for_each_entry_safe(bce, next, &rc->uwb_beca.list, node) {
David Vrabel45c16cd2008-10-15 14:41:56 +0100291 expires = bce->ts_jiffies + msecs_to_jiffies(beacon_timeout_ms);
292 if (time_after(jiffies, expires)) {
Inaky Perez-Gonzalez22d203e2008-09-17 16:34:08 +0100293 uwbd_dev_offair(bce);
Inaky Perez-Gonzalez22d203e2008-09-17 16:34:08 +0100294 }
295 }
Stefano Panellafec1a592008-11-04 15:39:08 +0000296 mutex_unlock(&rc->uwb_beca.mutex);
Inaky Perez-Gonzalez22d203e2008-09-17 16:34:08 +0100297}
298
299/* Clean up the whole beacon cache. Called on shutdown */
Stefano Panellafec1a592008-11-04 15:39:08 +0000300void uwb_beca_release(struct uwb_rc *rc)
Inaky Perez-Gonzalez22d203e2008-09-17 16:34:08 +0100301{
302 struct uwb_beca_e *bce, *next;
Stefano Panellafec1a592008-11-04 15:39:08 +0000303
304 mutex_lock(&rc->uwb_beca.mutex);
305 list_for_each_entry_safe(bce, next, &rc->uwb_beca.list, node) {
Inaky Perez-Gonzalez22d203e2008-09-17 16:34:08 +0100306 list_del(&bce->node);
307 uwb_bce_put(bce);
308 }
Stefano Panellafec1a592008-11-04 15:39:08 +0000309 mutex_unlock(&rc->uwb_beca.mutex);
Inaky Perez-Gonzalez22d203e2008-09-17 16:34:08 +0100310}
311
312static void uwb_beacon_print(struct uwb_rc *rc, struct uwb_rc_evt_beacon *be,
313 struct uwb_beacon_frame *bf)
314{
315 char macbuf[UWB_ADDR_STRSIZE];
316 char devbuf[UWB_ADDR_STRSIZE];
317 char dstbuf[UWB_ADDR_STRSIZE];
318
319 uwb_mac_addr_print(macbuf, sizeof(macbuf), &bf->Device_Identifier);
320 uwb_dev_addr_print(devbuf, sizeof(devbuf), &bf->hdr.SrcAddr);
321 uwb_dev_addr_print(dstbuf, sizeof(dstbuf), &bf->hdr.DestAddr);
322 dev_info(&rc->uwb_dev.dev,
323 "BEACON from %s to %s (ch%u offset %u slot %u MAC %s)\n",
324 devbuf, dstbuf, be->bChannelNumber, be->wBPSTOffset,
325 bf->Beacon_Slot_Number, macbuf);
326}
327
328/*
329 * @bce: beacon cache entry, referenced
330 */
331ssize_t uwb_bce_print_IEs(struct uwb_dev *uwb_dev, struct uwb_beca_e *bce,
332 char *buf, size_t size)
333{
334 ssize_t result = 0;
335 struct uwb_rc_evt_beacon *be;
336 struct uwb_beacon_frame *bf;
David Vrabel1cde7f62008-10-27 16:48:09 +0000337 int ies_len;
338 struct uwb_ie_hdr *ies;
Inaky Perez-Gonzalez22d203e2008-09-17 16:34:08 +0100339
340 mutex_lock(&bce->mutex);
David Vrabel1cde7f62008-10-27 16:48:09 +0000341
Inaky Perez-Gonzalez22d203e2008-09-17 16:34:08 +0100342 be = bce->be;
David Vrabel1cde7f62008-10-27 16:48:09 +0000343 if (be) {
344 bf = (struct uwb_beacon_frame *)bce->be->BeaconInfo;
345 ies_len = be->wBeaconInfoLength - sizeof(struct uwb_beacon_frame);
346 ies = (struct uwb_ie_hdr *)bf->IEData;
347
348 result = uwb_ie_dump_hex(ies, ies_len, buf, size);
349 }
350
Inaky Perez-Gonzalez22d203e2008-09-17 16:34:08 +0100351 mutex_unlock(&bce->mutex);
David Vrabel1cde7f62008-10-27 16:48:09 +0000352
Inaky Perez-Gonzalez22d203e2008-09-17 16:34:08 +0100353 return result;
354}
355
356/*
357 * Verify that the beacon event, frame and IEs are ok
358 */
359static int uwb_verify_beacon(struct uwb_rc *rc, struct uwb_event *evt,
360 struct uwb_rc_evt_beacon *be)
361{
362 int result = -EINVAL;
363 struct uwb_beacon_frame *bf;
364 struct device *dev = &rc->uwb_dev.dev;
365
366 /* Is there enough data to decode a beacon frame? */
367 if (evt->notif.size < sizeof(*be) + sizeof(*bf)) {
368 dev_err(dev, "BEACON event: Not enough data to decode "
369 "(%zu vs %zu bytes needed)\n", evt->notif.size,
370 sizeof(*be) + sizeof(*bf));
371 goto error;
372 }
373 /* FIXME: make sure beacon frame IEs are fine and that the whole thing
374 * is consistent */
375 result = 0;
376error:
377 return result;
378}
379
380/*
381 * Handle UWB_RC_EVT_BEACON events
382 *
383 * We check the beacon cache to see how the received beacon fares. If
384 * is there already we refresh the timestamp. If not we create a new
385 * entry.
386 *
387 * According to the WHCI and WUSB specs, only one beacon frame is
388 * allowed per notification block, so we don't bother about scanning
389 * for more.
390 */
391int uwbd_evt_handle_rc_beacon(struct uwb_event *evt)
392{
393 int result = -EINVAL;
394 struct uwb_rc *rc;
395 struct uwb_rc_evt_beacon *be;
396 struct uwb_beacon_frame *bf;
397 struct uwb_beca_e *bce;
Inaky Perez-Gonzalez22d203e2008-09-17 16:34:08 +0100398 unsigned long last_ts;
399
400 rc = evt->rc;
401 be = container_of(evt->notif.rceb, struct uwb_rc_evt_beacon, rceb);
402 result = uwb_verify_beacon(rc, evt, be);
403 if (result < 0)
404 return result;
405
David Vrabel8092d7c2008-10-16 13:56:53 +0100406 /* FIXME: handle alien beacons. */
Inaky Perez-Gonzalez22d203e2008-09-17 16:34:08 +0100407 if (be->bBeaconType == UWB_RC_BEACON_TYPE_OL_ALIEN ||
408 be->bBeaconType == UWB_RC_BEACON_TYPE_NOL_ALIEN) {
David Vrabel8092d7c2008-10-16 13:56:53 +0100409 return -ENOSYS;
Inaky Perez-Gonzalez22d203e2008-09-17 16:34:08 +0100410 }
David Vrabel8092d7c2008-10-16 13:56:53 +0100411
Inaky Perez-Gonzalez22d203e2008-09-17 16:34:08 +0100412 bf = (struct uwb_beacon_frame *) be->BeaconInfo;
413
414 /*
415 * Drop beacons from devices with a NULL EUI-48 -- they cannot
416 * be uniquely identified.
417 *
418 * It's expected that these will all be WUSB devices and they
419 * have a WUSB specific connection method so ignoring them
420 * here shouldn't be a problem.
421 */
422 if (uwb_mac_addr_bcast(&bf->Device_Identifier))
423 return 0;
424
Stefano Panellafec1a592008-11-04 15:39:08 +0000425 mutex_lock(&rc->uwb_beca.mutex);
426 bce = __uwb_beca_find_bymac(rc, &bf->Device_Identifier);
Inaky Perez-Gonzalez22d203e2008-09-17 16:34:08 +0100427 if (bce == NULL) {
428 /* Not in there, a new device is pinging */
429 uwb_beacon_print(evt->rc, be, bf);
Stefano Panellafec1a592008-11-04 15:39:08 +0000430 bce = __uwb_beca_add(rc, be, bf, evt->ts_jiffies);
Inaky Perez-Gonzalez22d203e2008-09-17 16:34:08 +0100431 if (bce == NULL) {
Stefano Panellafec1a592008-11-04 15:39:08 +0000432 mutex_unlock(&rc->uwb_beca.mutex);
Inaky Perez-Gonzalez22d203e2008-09-17 16:34:08 +0100433 return -ENOMEM;
434 }
435 }
Stefano Panellafec1a592008-11-04 15:39:08 +0000436 mutex_unlock(&rc->uwb_beca.mutex);
Inaky Perez-Gonzalez22d203e2008-09-17 16:34:08 +0100437
438 mutex_lock(&bce->mutex);
439 /* purge old beacon data */
440 kfree(bce->be);
441
442 last_ts = bce->ts_jiffies;
443
444 /* Update commonly used fields */
445 bce->ts_jiffies = evt->ts_jiffies;
446 bce->be = be;
447 bce->dev_addr = bf->hdr.SrcAddr;
448 bce->mac_addr = &bf->Device_Identifier;
449 be->wBPSTOffset = le16_to_cpu(be->wBPSTOffset);
450 be->wBeaconInfoLength = le16_to_cpu(be->wBeaconInfoLength);
451 stats_add_sample(&bce->lqe_stats, be->bLQI - 7);
452 stats_add_sample(&bce->rssi_stats, be->bRSSI + 18);
453
454 /*
455 * This might be a beacon from a new device.
456 */
457 if (bce->uwb_dev == NULL)
458 uwbd_dev_onair(evt->rc, bce);
459
460 mutex_unlock(&bce->mutex);
461
462 return 1; /* we keep the event data */
463}
464
465/*
466 * Handle UWB_RC_EVT_BEACON_SIZE events
467 *
468 * XXXXX
469 */
470int uwbd_evt_handle_rc_beacon_size(struct uwb_event *evt)
471{
472 int result = -EINVAL;
473 struct device *dev = &evt->rc->uwb_dev.dev;
474 struct uwb_rc_evt_beacon_size *bs;
475
476 /* Is there enough data to decode the event? */
477 if (evt->notif.size < sizeof(*bs)) {
478 dev_err(dev, "BEACON SIZE notification: Not enough data to "
479 "decode (%zu vs %zu bytes needed)\n",
480 evt->notif.size, sizeof(*bs));
481 goto error;
482 }
483 bs = container_of(evt->notif.rceb, struct uwb_rc_evt_beacon_size, rceb);
484 if (0)
485 dev_info(dev, "Beacon size changed to %u bytes "
486 "(FIXME: action?)\n", le16_to_cpu(bs->wNewBeaconSize));
487 else {
488 /* temporary hack until we do something with this message... */
489 static unsigned count;
490 if (++count % 1000 == 0)
491 dev_info(dev, "Beacon size changed %u times "
492 "(FIXME: action?)\n", count);
493 }
494 result = 0;
495error:
496 return result;
497}
498
499/**
500 * uwbd_evt_handle_rc_bp_slot_change - handle a BP_SLOT_CHANGE event
501 * @evt: the BP_SLOT_CHANGE notification from the radio controller
502 *
503 * If the event indicates that no beacon period slots were available
504 * then radio controller has transitioned to a non-beaconing state.
505 * Otherwise, simply save the current beacon slot.
506 */
507int uwbd_evt_handle_rc_bp_slot_change(struct uwb_event *evt)
508{
509 struct uwb_rc *rc = evt->rc;
510 struct device *dev = &rc->uwb_dev.dev;
511 struct uwb_rc_evt_bp_slot_change *bpsc;
512
513 if (evt->notif.size < sizeof(*bpsc)) {
514 dev_err(dev, "BP SLOT CHANGE event: Not enough data\n");
515 return -EINVAL;
516 }
517 bpsc = container_of(evt->notif.rceb, struct uwb_rc_evt_bp_slot_change, rceb);
518
Inaky Perez-Gonzalez22d203e2008-09-17 16:34:08 +0100519 if (uwb_rc_evt_bp_slot_change_no_slot(bpsc)) {
Thomas Pugliesef79833a2013-12-20 11:54:07 -0600520 dev_err(dev, "stopped beaconing: No free slots in BP\n");
Thomas Pugliese1fc671b2013-12-20 13:01:03 -0600521 mutex_lock(&rc->uwb_dev.mutex);
Inaky Perez-Gonzalez22d203e2008-09-17 16:34:08 +0100522 rc->beaconing = -1;
Thomas Pugliese1fc671b2013-12-20 13:01:03 -0600523 mutex_unlock(&rc->uwb_dev.mutex);
Inaky Perez-Gonzalez22d203e2008-09-17 16:34:08 +0100524 } else
525 rc->uwb_dev.beacon_slot = uwb_rc_evt_bp_slot_change_slot_num(bpsc);
Inaky Perez-Gonzalez22d203e2008-09-17 16:34:08 +0100526
527 return 0;
528}
529
530/**
531 * Handle UWB_RC_EVT_BPOIE_CHANGE events
532 *
533 * XXXXX
534 */
535struct uwb_ie_bpo {
536 struct uwb_ie_hdr hdr;
537 u8 bp_length;
538 u8 data[];
539} __attribute__((packed));
540
541int uwbd_evt_handle_rc_bpoie_change(struct uwb_event *evt)
542{
543 int result = -EINVAL;
544 struct device *dev = &evt->rc->uwb_dev.dev;
545 struct uwb_rc_evt_bpoie_change *bpoiec;
546 struct uwb_ie_bpo *bpoie;
547 static unsigned count; /* FIXME: this is a temp hack */
548 size_t iesize;
549
550 /* Is there enough data to decode it? */
551 if (evt->notif.size < sizeof(*bpoiec)) {
552 dev_err(dev, "BPOIEC notification: Not enough data to "
553 "decode (%zu vs %zu bytes needed)\n",
554 evt->notif.size, sizeof(*bpoiec));
555 goto error;
556 }
557 bpoiec = container_of(evt->notif.rceb, struct uwb_rc_evt_bpoie_change, rceb);
558 iesize = le16_to_cpu(bpoiec->wBPOIELength);
559 if (iesize < sizeof(*bpoie)) {
560 dev_err(dev, "BPOIEC notification: Not enough IE data to "
561 "decode (%zu vs %zu bytes needed)\n",
562 iesize, sizeof(*bpoie));
563 goto error;
564 }
565 if (++count % 1000 == 0) /* Lame placeholder */
566 dev_info(dev, "BPOIE: %u changes received\n", count);
567 /*
568 * FIXME: At this point we should go over all the IEs in the
569 * bpoiec->BPOIE array and act on each.
570 */
571 result = 0;
572error:
573 return result;
574}
575
Inaky Perez-Gonzalez22d203e2008-09-17 16:34:08 +0100576/*
577 * Print beaconing state.
578 */
579static ssize_t uwb_rc_beacon_show(struct device *dev,
580 struct device_attribute *attr, char *buf)
581{
582 struct uwb_dev *uwb_dev = to_uwb_dev(dev);
583 struct uwb_rc *rc = uwb_dev->rc;
584 ssize_t result;
585
586 mutex_lock(&rc->uwb_dev.mutex);
587 result = sprintf(buf, "%d\n", rc->beaconing);
588 mutex_unlock(&rc->uwb_dev.mutex);
589 return result;
590}
591
592/*
593 * Start beaconing on the specified channel, or stop beaconing.
Inaky Perez-Gonzalez22d203e2008-09-17 16:34:08 +0100594 */
595static ssize_t uwb_rc_beacon_store(struct device *dev,
596 struct device_attribute *attr,
597 const char *buf, size_t size)
598{
599 struct uwb_dev *uwb_dev = to_uwb_dev(dev);
600 struct uwb_rc *rc = uwb_dev->rc;
601 int channel;
Inaky Perez-Gonzalez22d203e2008-09-17 16:34:08 +0100602 ssize_t result = -EINVAL;
603
David Vrabel6fae35f2008-11-17 15:53:42 +0000604 result = sscanf(buf, "%d", &channel);
Inaky Perez-Gonzalez22d203e2008-09-17 16:34:08 +0100605 if (result >= 1)
David Vrabel6fae35f2008-11-17 15:53:42 +0000606 result = uwb_radio_force_channel(rc, channel);
Inaky Perez-Gonzalez22d203e2008-09-17 16:34:08 +0100607
608 return result < 0 ? result : size;
609}
610DEVICE_ATTR(beacon, S_IRUGO | S_IWUSR, uwb_rc_beacon_show, uwb_rc_beacon_store);