treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / staging / uwb / beacon.c
blobc483c19c5ef82403050731518c18a83f65018901
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Ultra Wide Band
4 * Beacon management
6 * Copyright (C) 2005-2006 Intel Corporation
7 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
9 * FIXME: docs
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/device.h>
15 #include <linux/err.h>
16 #include <linux/kdev_t.h>
17 #include <linux/slab.h>
19 #include "uwb-internal.h"
21 /* Start Beaconing command structure */
22 struct uwb_rc_cmd_start_beacon {
23 struct uwb_rccb rccb;
24 __le16 wBPSTOffset;
25 u8 bChannelNumber;
26 } __attribute__((packed));
29 static int uwb_rc_start_beacon(struct uwb_rc *rc, u16 bpst_offset, u8 channel)
31 int result;
32 struct uwb_rc_cmd_start_beacon *cmd;
33 struct uwb_rc_evt_confirm reply;
35 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
36 if (cmd == NULL)
37 return -ENOMEM;
38 cmd->rccb.bCommandType = UWB_RC_CET_GENERAL;
39 cmd->rccb.wCommand = cpu_to_le16(UWB_RC_CMD_START_BEACON);
40 cmd->wBPSTOffset = cpu_to_le16(bpst_offset);
41 cmd->bChannelNumber = channel;
42 reply.rceb.bEventType = UWB_RC_CET_GENERAL;
43 reply.rceb.wEvent = UWB_RC_CMD_START_BEACON;
44 result = uwb_rc_cmd(rc, "START-BEACON", &cmd->rccb, sizeof(*cmd),
45 &reply.rceb, sizeof(reply));
46 if (result < 0)
47 goto error_cmd;
48 if (reply.bResultCode != UWB_RC_RES_SUCCESS) {
49 dev_err(&rc->uwb_dev.dev,
50 "START-BEACON: command execution failed: %s (%d)\n",
51 uwb_rc_strerror(reply.bResultCode), reply.bResultCode);
52 result = -EIO;
54 error_cmd:
55 kfree(cmd);
56 return result;
59 static int uwb_rc_stop_beacon(struct uwb_rc *rc)
61 int result;
62 struct uwb_rccb *cmd;
63 struct uwb_rc_evt_confirm reply;
65 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
66 if (cmd == NULL)
67 return -ENOMEM;
68 cmd->bCommandType = UWB_RC_CET_GENERAL;
69 cmd->wCommand = cpu_to_le16(UWB_RC_CMD_STOP_BEACON);
70 reply.rceb.bEventType = UWB_RC_CET_GENERAL;
71 reply.rceb.wEvent = UWB_RC_CMD_STOP_BEACON;
72 result = uwb_rc_cmd(rc, "STOP-BEACON", cmd, sizeof(*cmd),
73 &reply.rceb, sizeof(reply));
74 if (result < 0)
75 goto error_cmd;
76 if (reply.bResultCode != UWB_RC_RES_SUCCESS) {
77 dev_err(&rc->uwb_dev.dev,
78 "STOP-BEACON: command execution failed: %s (%d)\n",
79 uwb_rc_strerror(reply.bResultCode), reply.bResultCode);
80 result = -EIO;
82 error_cmd:
83 kfree(cmd);
84 return result;
88 * Start/stop beacons
90 * @rc: UWB Radio Controller to operate on
91 * @channel: UWB channel on which to beacon (WUSB[table
92 * 5-12]). If -1, stop beaconing.
93 * @bpst_offset: Beacon Period Start Time offset; FIXME-do zero
95 * According to WHCI 0.95 [4.13.6] the driver will only receive the RCEB
96 * of a SET IE command after the device sent the first beacon that includes
97 * the IEs specified in the SET IE command. So, after we start beaconing we
98 * check if there is anything in the IE cache and call the SET IE command
99 * if needed.
101 int uwb_rc_beacon(struct uwb_rc *rc, int channel, unsigned bpst_offset)
103 int result;
104 struct device *dev = &rc->uwb_dev.dev;
106 dev_dbg(dev, "%s: channel = %d\n", __func__, channel);
107 if (channel < 0)
108 channel = -1;
109 if (channel == -1)
110 result = uwb_rc_stop_beacon(rc);
111 else {
112 /* channel >= 0...dah */
113 result = uwb_rc_start_beacon(rc, bpst_offset, channel);
114 if (result < 0) {
115 dev_err(dev, "Cannot start beaconing: %d\n", result);
116 return result;
118 if (le16_to_cpu(rc->ies->wIELength) > 0) {
119 result = uwb_rc_set_ie(rc, rc->ies);
120 if (result < 0) {
121 dev_err(dev, "Cannot set new IE on device: "
122 "%d\n", result);
123 result = uwb_rc_stop_beacon(rc);
124 channel = -1;
125 bpst_offset = 0;
130 if (result >= 0)
131 rc->beaconing = channel;
132 return result;
136 * Beacon cache
138 * The purpose of this is to speed up the lookup of becon information
139 * when a new beacon arrives. The UWB Daemon uses it also to keep a
140 * tab of which devices are in radio distance and which not. When a
141 * device's beacon stays present for more than a certain amount of
142 * time, it is considered a new, usable device. When a beacon ceases
143 * to be received for a certain amount of time, it is considered that
144 * the device is gone.
146 * FIXME: use an allocator for the entries
147 * FIXME: use something faster for search than a list
150 void uwb_bce_kfree(struct kref *_bce)
152 struct uwb_beca_e *bce = container_of(_bce, struct uwb_beca_e, refcnt);
154 kfree(bce->be);
155 kfree(bce);
159 /* Find a beacon by dev addr in the cache */
160 static
161 struct uwb_beca_e *__uwb_beca_find_bydev(struct uwb_rc *rc,
162 const struct uwb_dev_addr *dev_addr)
164 struct uwb_beca_e *bce, *next;
165 list_for_each_entry_safe(bce, next, &rc->uwb_beca.list, node) {
166 if (!memcmp(&bce->dev_addr, dev_addr, sizeof(bce->dev_addr)))
167 goto out;
169 bce = NULL;
170 out:
171 return bce;
174 /* Find a beacon by dev addr in the cache */
175 static
176 struct uwb_beca_e *__uwb_beca_find_bymac(struct uwb_rc *rc,
177 const struct uwb_mac_addr *mac_addr)
179 struct uwb_beca_e *bce, *next;
180 list_for_each_entry_safe(bce, next, &rc->uwb_beca.list, node) {
181 if (!memcmp(bce->mac_addr, mac_addr->data,
182 sizeof(struct uwb_mac_addr)))
183 goto out;
185 bce = NULL;
186 out:
187 return bce;
191 * uwb_dev_get_by_devaddr - get a UWB device with a specific DevAddr
192 * @rc: the radio controller that saw the device
193 * @devaddr: DevAddr of the UWB device to find
195 * There may be more than one matching device (in the case of a
196 * DevAddr conflict), but only the first one is returned.
198 struct uwb_dev *uwb_dev_get_by_devaddr(struct uwb_rc *rc,
199 const struct uwb_dev_addr *devaddr)
201 struct uwb_dev *found = NULL;
202 struct uwb_beca_e *bce;
204 mutex_lock(&rc->uwb_beca.mutex);
205 bce = __uwb_beca_find_bydev(rc, devaddr);
206 if (bce)
207 found = uwb_dev_try_get(rc, bce->uwb_dev);
208 mutex_unlock(&rc->uwb_beca.mutex);
210 return found;
214 * uwb_dev_get_by_macaddr - get a UWB device with a specific EUI-48
215 * @rc: the radio controller that saw the device
216 * @devaddr: EUI-48 of the UWB device to find
218 struct uwb_dev *uwb_dev_get_by_macaddr(struct uwb_rc *rc,
219 const struct uwb_mac_addr *macaddr)
221 struct uwb_dev *found = NULL;
222 struct uwb_beca_e *bce;
224 mutex_lock(&rc->uwb_beca.mutex);
225 bce = __uwb_beca_find_bymac(rc, macaddr);
226 if (bce)
227 found = uwb_dev_try_get(rc, bce->uwb_dev);
228 mutex_unlock(&rc->uwb_beca.mutex);
230 return found;
233 /* Initialize a beacon cache entry */
234 static void uwb_beca_e_init(struct uwb_beca_e *bce)
236 mutex_init(&bce->mutex);
237 kref_init(&bce->refcnt);
238 stats_init(&bce->lqe_stats);
239 stats_init(&bce->rssi_stats);
243 * Add a beacon to the cache
245 * @be: Beacon event information
246 * @bf: Beacon frame (part of b, really)
247 * @ts_jiffies: Timestamp (in jiffies) when the beacon was received
249 static
250 struct uwb_beca_e *__uwb_beca_add(struct uwb_rc *rc,
251 struct uwb_rc_evt_beacon *be,
252 struct uwb_beacon_frame *bf,
253 unsigned long ts_jiffies)
255 struct uwb_beca_e *bce;
257 bce = kzalloc(sizeof(*bce), GFP_KERNEL);
258 if (bce == NULL)
259 return NULL;
260 uwb_beca_e_init(bce);
261 bce->ts_jiffies = ts_jiffies;
262 bce->uwb_dev = NULL;
263 list_add(&bce->node, &rc->uwb_beca.list);
264 return bce;
268 * Wipe out beacon entries that became stale
270 * Remove associated devicest too.
272 void uwb_beca_purge(struct uwb_rc *rc)
274 struct uwb_beca_e *bce, *next;
275 unsigned long expires;
277 mutex_lock(&rc->uwb_beca.mutex);
278 list_for_each_entry_safe(bce, next, &rc->uwb_beca.list, node) {
279 expires = bce->ts_jiffies + msecs_to_jiffies(beacon_timeout_ms);
280 if (time_after(jiffies, expires)) {
281 uwbd_dev_offair(bce);
284 mutex_unlock(&rc->uwb_beca.mutex);
287 /* Clean up the whole beacon cache. Called on shutdown */
288 void uwb_beca_release(struct uwb_rc *rc)
290 struct uwb_beca_e *bce, *next;
292 mutex_lock(&rc->uwb_beca.mutex);
293 list_for_each_entry_safe(bce, next, &rc->uwb_beca.list, node) {
294 list_del(&bce->node);
295 uwb_bce_put(bce);
297 mutex_unlock(&rc->uwb_beca.mutex);
300 static void uwb_beacon_print(struct uwb_rc *rc, struct uwb_rc_evt_beacon *be,
301 struct uwb_beacon_frame *bf)
303 char macbuf[UWB_ADDR_STRSIZE];
304 char devbuf[UWB_ADDR_STRSIZE];
305 char dstbuf[UWB_ADDR_STRSIZE];
307 uwb_mac_addr_print(macbuf, sizeof(macbuf), &bf->Device_Identifier);
308 uwb_dev_addr_print(devbuf, sizeof(devbuf), &bf->hdr.SrcAddr);
309 uwb_dev_addr_print(dstbuf, sizeof(dstbuf), &bf->hdr.DestAddr);
310 dev_info(&rc->uwb_dev.dev,
311 "BEACON from %s to %s (ch%u offset %u slot %u MAC %s)\n",
312 devbuf, dstbuf, be->bChannelNumber, be->wBPSTOffset,
313 bf->Beacon_Slot_Number, macbuf);
317 * @bce: beacon cache entry, referenced
319 ssize_t uwb_bce_print_IEs(struct uwb_dev *uwb_dev, struct uwb_beca_e *bce,
320 char *buf, size_t size)
322 ssize_t result = 0;
323 struct uwb_rc_evt_beacon *be;
324 struct uwb_beacon_frame *bf;
325 int ies_len;
326 struct uwb_ie_hdr *ies;
328 mutex_lock(&bce->mutex);
330 be = bce->be;
331 if (be) {
332 bf = (struct uwb_beacon_frame *)bce->be->BeaconInfo;
333 ies_len = be->wBeaconInfoLength - sizeof(struct uwb_beacon_frame);
334 ies = (struct uwb_ie_hdr *)bf->IEData;
336 result = uwb_ie_dump_hex(ies, ies_len, buf, size);
339 mutex_unlock(&bce->mutex);
341 return result;
345 * Verify that the beacon event, frame and IEs are ok
347 static int uwb_verify_beacon(struct uwb_rc *rc, struct uwb_event *evt,
348 struct uwb_rc_evt_beacon *be)
350 int result = -EINVAL;
351 struct uwb_beacon_frame *bf;
352 struct device *dev = &rc->uwb_dev.dev;
354 /* Is there enough data to decode a beacon frame? */
355 if (evt->notif.size < sizeof(*be) + sizeof(*bf)) {
356 dev_err(dev, "BEACON event: Not enough data to decode "
357 "(%zu vs %zu bytes needed)\n", evt->notif.size,
358 sizeof(*be) + sizeof(*bf));
359 goto error;
361 /* FIXME: make sure beacon frame IEs are fine and that the whole thing
362 * is consistent */
363 result = 0;
364 error:
365 return result;
369 * Handle UWB_RC_EVT_BEACON events
371 * We check the beacon cache to see how the received beacon fares. If
372 * is there already we refresh the timestamp. If not we create a new
373 * entry.
375 * According to the WHCI and WUSB specs, only one beacon frame is
376 * allowed per notification block, so we don't bother about scanning
377 * for more.
379 int uwbd_evt_handle_rc_beacon(struct uwb_event *evt)
381 int result = -EINVAL;
382 struct uwb_rc *rc;
383 struct uwb_rc_evt_beacon *be;
384 struct uwb_beacon_frame *bf;
385 struct uwb_beca_e *bce;
387 rc = evt->rc;
388 be = container_of(evt->notif.rceb, struct uwb_rc_evt_beacon, rceb);
389 result = uwb_verify_beacon(rc, evt, be);
390 if (result < 0)
391 return result;
393 /* FIXME: handle alien beacons. */
394 if (be->bBeaconType == UWB_RC_BEACON_TYPE_OL_ALIEN ||
395 be->bBeaconType == UWB_RC_BEACON_TYPE_NOL_ALIEN) {
396 return -ENOSYS;
399 bf = (struct uwb_beacon_frame *) be->BeaconInfo;
402 * Drop beacons from devices with a NULL EUI-48 -- they cannot
403 * be uniquely identified.
405 * It's expected that these will all be WUSB devices and they
406 * have a WUSB specific connection method so ignoring them
407 * here shouldn't be a problem.
409 if (uwb_mac_addr_bcast(&bf->Device_Identifier))
410 return 0;
412 mutex_lock(&rc->uwb_beca.mutex);
413 bce = __uwb_beca_find_bymac(rc, &bf->Device_Identifier);
414 if (bce == NULL) {
415 /* Not in there, a new device is pinging */
416 uwb_beacon_print(evt->rc, be, bf);
417 bce = __uwb_beca_add(rc, be, bf, evt->ts_jiffies);
418 if (bce == NULL) {
419 mutex_unlock(&rc->uwb_beca.mutex);
420 return -ENOMEM;
423 mutex_unlock(&rc->uwb_beca.mutex);
425 mutex_lock(&bce->mutex);
426 /* purge old beacon data */
427 kfree(bce->be);
429 /* Update commonly used fields */
430 bce->ts_jiffies = evt->ts_jiffies;
431 bce->be = be;
432 bce->dev_addr = bf->hdr.SrcAddr;
433 bce->mac_addr = &bf->Device_Identifier;
434 be->wBPSTOffset = le16_to_cpu(be->wBPSTOffset);
435 be->wBeaconInfoLength = le16_to_cpu(be->wBeaconInfoLength);
436 stats_add_sample(&bce->lqe_stats, be->bLQI - 7);
437 stats_add_sample(&bce->rssi_stats, be->bRSSI + 18);
440 * This might be a beacon from a new device.
442 if (bce->uwb_dev == NULL)
443 uwbd_dev_onair(evt->rc, bce);
445 mutex_unlock(&bce->mutex);
447 return 1; /* we keep the event data */
451 * Handle UWB_RC_EVT_BEACON_SIZE events
453 * XXXXX
455 int uwbd_evt_handle_rc_beacon_size(struct uwb_event *evt)
457 int result = -EINVAL;
458 struct device *dev = &evt->rc->uwb_dev.dev;
459 struct uwb_rc_evt_beacon_size *bs;
461 /* Is there enough data to decode the event? */
462 if (evt->notif.size < sizeof(*bs)) {
463 dev_err(dev, "BEACON SIZE notification: Not enough data to "
464 "decode (%zu vs %zu bytes needed)\n",
465 evt->notif.size, sizeof(*bs));
466 goto error;
468 bs = container_of(evt->notif.rceb, struct uwb_rc_evt_beacon_size, rceb);
469 if (0)
470 dev_info(dev, "Beacon size changed to %u bytes "
471 "(FIXME: action?)\n", le16_to_cpu(bs->wNewBeaconSize));
472 else {
473 /* temporary hack until we do something with this message... */
474 static unsigned count;
475 if (++count % 1000 == 0)
476 dev_info(dev, "Beacon size changed %u times "
477 "(FIXME: action?)\n", count);
479 result = 0;
480 error:
481 return result;
485 * uwbd_evt_handle_rc_bp_slot_change - handle a BP_SLOT_CHANGE event
486 * @evt: the BP_SLOT_CHANGE notification from the radio controller
488 * If the event indicates that no beacon period slots were available
489 * then radio controller has transitioned to a non-beaconing state.
490 * Otherwise, simply save the current beacon slot.
492 int uwbd_evt_handle_rc_bp_slot_change(struct uwb_event *evt)
494 struct uwb_rc *rc = evt->rc;
495 struct device *dev = &rc->uwb_dev.dev;
496 struct uwb_rc_evt_bp_slot_change *bpsc;
498 if (evt->notif.size < sizeof(*bpsc)) {
499 dev_err(dev, "BP SLOT CHANGE event: Not enough data\n");
500 return -EINVAL;
502 bpsc = container_of(evt->notif.rceb, struct uwb_rc_evt_bp_slot_change, rceb);
504 if (uwb_rc_evt_bp_slot_change_no_slot(bpsc)) {
505 dev_err(dev, "stopped beaconing: No free slots in BP\n");
506 mutex_lock(&rc->uwb_dev.mutex);
507 rc->beaconing = -1;
508 mutex_unlock(&rc->uwb_dev.mutex);
509 } else
510 rc->uwb_dev.beacon_slot = uwb_rc_evt_bp_slot_change_slot_num(bpsc);
512 return 0;
516 * Handle UWB_RC_EVT_BPOIE_CHANGE events
518 * XXXXX
520 struct uwb_ie_bpo {
521 struct uwb_ie_hdr hdr;
522 u8 bp_length;
523 u8 data[];
524 } __attribute__((packed));
526 int uwbd_evt_handle_rc_bpoie_change(struct uwb_event *evt)
528 int result = -EINVAL;
529 struct device *dev = &evt->rc->uwb_dev.dev;
530 struct uwb_rc_evt_bpoie_change *bpoiec;
531 struct uwb_ie_bpo *bpoie;
532 static unsigned count; /* FIXME: this is a temp hack */
533 size_t iesize;
535 /* Is there enough data to decode it? */
536 if (evt->notif.size < sizeof(*bpoiec)) {
537 dev_err(dev, "BPOIEC notification: Not enough data to "
538 "decode (%zu vs %zu bytes needed)\n",
539 evt->notif.size, sizeof(*bpoiec));
540 goto error;
542 bpoiec = container_of(evt->notif.rceb, struct uwb_rc_evt_bpoie_change, rceb);
543 iesize = le16_to_cpu(bpoiec->wBPOIELength);
544 if (iesize < sizeof(*bpoie)) {
545 dev_err(dev, "BPOIEC notification: Not enough IE data to "
546 "decode (%zu vs %zu bytes needed)\n",
547 iesize, sizeof(*bpoie));
548 goto error;
550 if (++count % 1000 == 0) /* Lame placeholder */
551 dev_info(dev, "BPOIE: %u changes received\n", count);
553 * FIXME: At this point we should go over all the IEs in the
554 * bpoiec->BPOIE array and act on each.
556 result = 0;
557 error:
558 return result;
562 * Print beaconing state.
564 static ssize_t uwb_rc_beacon_show(struct device *dev,
565 struct device_attribute *attr, char *buf)
567 struct uwb_dev *uwb_dev = to_uwb_dev(dev);
568 struct uwb_rc *rc = uwb_dev->rc;
569 ssize_t result;
571 mutex_lock(&rc->uwb_dev.mutex);
572 result = sprintf(buf, "%d\n", rc->beaconing);
573 mutex_unlock(&rc->uwb_dev.mutex);
574 return result;
578 * Start beaconing on the specified channel, or stop beaconing.
580 static ssize_t uwb_rc_beacon_store(struct device *dev,
581 struct device_attribute *attr,
582 const char *buf, size_t size)
584 struct uwb_dev *uwb_dev = to_uwb_dev(dev);
585 struct uwb_rc *rc = uwb_dev->rc;
586 int channel;
587 ssize_t result = -EINVAL;
589 result = sscanf(buf, "%d", &channel);
590 if (result >= 1)
591 result = uwb_radio_force_channel(rc, channel);
593 return result < 0 ? result : size;
595 DEVICE_ATTR(beacon, S_IRUGO | S_IWUSR, uwb_rc_beacon_show, uwb_rc_beacon_store);