5 * Copyright (C) 2005-2006 Intel Corporation
6 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
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.
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.
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
26 #include <linux/kernel.h>
27 #include <linux/init.h>
28 #include <linux/module.h>
29 #include <linux/device.h>
30 #include <linux/err.h>
31 #include <linux/kdev_t.h>
32 #include "uwb-internal.h"
35 #include <linux/uwb/debug.h>
37 /** Start Beaconing command structure */
38 struct uwb_rc_cmd_start_beacon {
42 } __attribute__((packed));
45 static int uwb_rc_start_beacon(struct uwb_rc *rc, u16 bpst_offset, u8 channel)
48 struct uwb_rc_cmd_start_beacon *cmd;
49 struct uwb_rc_evt_confirm reply;
51 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
54 cmd->rccb.bCommandType = UWB_RC_CET_GENERAL;
55 cmd->rccb.wCommand = cpu_to_le16(UWB_RC_CMD_START_BEACON);
56 cmd->wBPSTOffset = cpu_to_le16(bpst_offset);
57 cmd->bChannelNumber = channel;
58 reply.rceb.bEventType = UWB_RC_CET_GENERAL;
59 reply.rceb.wEvent = UWB_RC_CMD_START_BEACON;
60 result = uwb_rc_cmd(rc, "START-BEACON", &cmd->rccb, sizeof(*cmd),
61 &reply.rceb, sizeof(reply));
64 if (reply.bResultCode != UWB_RC_RES_SUCCESS) {
65 dev_err(&rc->uwb_dev.dev,
66 "START-BEACON: command execution failed: %s (%d)\n",
67 uwb_rc_strerror(reply.bResultCode), reply.bResultCode);
75 static int uwb_rc_stop_beacon(struct uwb_rc *rc)
79 struct uwb_rc_evt_confirm reply;
81 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
84 cmd->bCommandType = UWB_RC_CET_GENERAL;
85 cmd->wCommand = cpu_to_le16(UWB_RC_CMD_STOP_BEACON);
86 reply.rceb.bEventType = UWB_RC_CET_GENERAL;
87 reply.rceb.wEvent = UWB_RC_CMD_STOP_BEACON;
88 result = uwb_rc_cmd(rc, "STOP-BEACON", cmd, sizeof(*cmd),
89 &reply.rceb, sizeof(reply));
92 if (reply.bResultCode != UWB_RC_RES_SUCCESS) {
93 dev_err(&rc->uwb_dev.dev,
94 "STOP-BEACON: command execution failed: %s (%d)\n",
95 uwb_rc_strerror(reply.bResultCode), reply.bResultCode);
106 * @rc: UWB Radio Controller to operate on
107 * @channel: UWB channel on which to beacon (WUSB[table
108 * 5-12]). If -1, stop beaconing.
109 * @bpst_offset: Beacon Period Start Time offset; FIXME-do zero
111 * According to WHCI 0.95 [4.13.6] the driver will only receive the RCEB
112 * of a SET IE command after the device sent the first beacon that includes
113 * the IEs specified in the SET IE command. So, after we start beaconing we
114 * check if there is anything in the IE cache and call the SET IE command
117 int uwb_rc_beacon(struct uwb_rc *rc, int channel, unsigned bpst_offset)
120 struct device *dev = &rc->uwb_dev.dev;
122 mutex_lock(&rc->uwb_dev.mutex);
126 result = uwb_rc_stop_beacon(rc);
128 /* channel >= 0...dah */
129 result = uwb_rc_start_beacon(rc, bpst_offset, channel);
132 if (le16_to_cpu(rc->ies->wIELength) > 0) {
133 result = uwb_rc_set_ie(rc, rc->ies);
135 dev_err(dev, "Cannot set new IE on device: "
137 result = uwb_rc_stop_beacon(rc);
147 rc->beaconing = channel;
149 uwb_notify(rc, NULL, uwb_bg_joined(rc) ? UWB_NOTIF_BG_JOIN : UWB_NOTIF_BG_LEAVE);
152 mutex_unlock(&rc->uwb_dev.mutex);
159 * The purpose of this is to speed up the lookup of becon information
160 * when a new beacon arrives. The UWB Daemon uses it also to keep a
161 * tab of which devices are in radio distance and which not. When a
162 * device's beacon stays present for more than a certain amount of
163 * time, it is considered a new, usable device. When a beacon ceases
164 * to be received for a certain amount of time, it is considered that
165 * the device is gone.
167 * FIXME: use an allocator for the entries
168 * FIXME: use something faster for search than a list
171 void uwb_bce_kfree(struct kref *_bce)
173 struct uwb_beca_e *bce = container_of(_bce, struct uwb_beca_e, refcnt);
180 /* Find a beacon by dev addr in the cache */
182 struct uwb_beca_e *__uwb_beca_find_bydev(struct uwb_rc *rc,
183 const struct uwb_dev_addr *dev_addr)
185 struct uwb_beca_e *bce, *next;
186 list_for_each_entry_safe(bce, next, &rc->uwb_beca.list, node) {
187 d_printf(6, NULL, "looking for addr %02x:%02x in %02x:%02x\n",
188 dev_addr->data[0], dev_addr->data[1],
189 bce->dev_addr.data[0], bce->dev_addr.data[1]);
190 if (!memcmp(&bce->dev_addr, dev_addr, sizeof(bce->dev_addr)))
198 /* Find a beacon by dev addr in the cache */
200 struct uwb_beca_e *__uwb_beca_find_bymac(struct uwb_rc *rc,
201 const struct uwb_mac_addr *mac_addr)
203 struct uwb_beca_e *bce, *next;
204 list_for_each_entry_safe(bce, next, &rc->uwb_beca.list, node) {
205 if (!memcmp(bce->mac_addr, mac_addr->data,
206 sizeof(struct uwb_mac_addr)))
215 * uwb_dev_get_by_devaddr - get a UWB device with a specific DevAddr
216 * @rc: the radio controller that saw the device
217 * @devaddr: DevAddr of the UWB device to find
219 * There may be more than one matching device (in the case of a
220 * DevAddr conflict), but only the first one is returned.
222 struct uwb_dev *uwb_dev_get_by_devaddr(struct uwb_rc *rc,
223 const struct uwb_dev_addr *devaddr)
225 struct uwb_dev *found = NULL;
226 struct uwb_beca_e *bce;
228 mutex_lock(&rc->uwb_beca.mutex);
229 bce = __uwb_beca_find_bydev(rc, devaddr);
231 found = uwb_dev_try_get(rc, bce->uwb_dev);
232 mutex_unlock(&rc->uwb_beca.mutex);
238 * uwb_dev_get_by_macaddr - get a UWB device with a specific EUI-48
239 * @rc: the radio controller that saw the device
240 * @devaddr: EUI-48 of the UWB device to find
242 struct uwb_dev *uwb_dev_get_by_macaddr(struct uwb_rc *rc,
243 const struct uwb_mac_addr *macaddr)
245 struct uwb_dev *found = NULL;
246 struct uwb_beca_e *bce;
248 mutex_lock(&rc->uwb_beca.mutex);
249 bce = __uwb_beca_find_bymac(rc, macaddr);
251 found = uwb_dev_try_get(rc, bce->uwb_dev);
252 mutex_unlock(&rc->uwb_beca.mutex);
257 /* Initialize a beacon cache entry */
258 static void uwb_beca_e_init(struct uwb_beca_e *bce)
260 mutex_init(&bce->mutex);
261 kref_init(&bce->refcnt);
262 stats_init(&bce->lqe_stats);
263 stats_init(&bce->rssi_stats);
267 * Add a beacon to the cache
269 * @be: Beacon event information
270 * @bf: Beacon frame (part of b, really)
271 * @ts_jiffies: Timestamp (in jiffies) when the beacon was received
274 struct uwb_beca_e *__uwb_beca_add(struct uwb_rc *rc,
275 struct uwb_rc_evt_beacon *be,
276 struct uwb_beacon_frame *bf,
277 unsigned long ts_jiffies)
279 struct uwb_beca_e *bce;
281 bce = kzalloc(sizeof(*bce), GFP_KERNEL);
284 uwb_beca_e_init(bce);
285 bce->ts_jiffies = ts_jiffies;
287 list_add(&bce->node, &rc->uwb_beca.list);
292 * Wipe out beacon entries that became stale
294 * Remove associated devicest too.
296 void uwb_beca_purge(struct uwb_rc *rc)
298 struct uwb_beca_e *bce, *next;
299 unsigned long expires;
301 mutex_lock(&rc->uwb_beca.mutex);
302 list_for_each_entry_safe(bce, next, &rc->uwb_beca.list, node) {
303 expires = bce->ts_jiffies + msecs_to_jiffies(beacon_timeout_ms);
304 if (time_after(jiffies, expires)) {
305 uwbd_dev_offair(bce);
306 list_del(&bce->node);
310 mutex_unlock(&rc->uwb_beca.mutex);
313 /* Clean up the whole beacon cache. Called on shutdown */
314 void uwb_beca_release(struct uwb_rc *rc)
316 struct uwb_beca_e *bce, *next;
318 mutex_lock(&rc->uwb_beca.mutex);
319 list_for_each_entry_safe(bce, next, &rc->uwb_beca.list, node) {
320 list_del(&bce->node);
323 mutex_unlock(&rc->uwb_beca.mutex);
326 static void uwb_beacon_print(struct uwb_rc *rc, struct uwb_rc_evt_beacon *be,
327 struct uwb_beacon_frame *bf)
329 char macbuf[UWB_ADDR_STRSIZE];
330 char devbuf[UWB_ADDR_STRSIZE];
331 char dstbuf[UWB_ADDR_STRSIZE];
333 uwb_mac_addr_print(macbuf, sizeof(macbuf), &bf->Device_Identifier);
334 uwb_dev_addr_print(devbuf, sizeof(devbuf), &bf->hdr.SrcAddr);
335 uwb_dev_addr_print(dstbuf, sizeof(dstbuf), &bf->hdr.DestAddr);
336 dev_info(&rc->uwb_dev.dev,
337 "BEACON from %s to %s (ch%u offset %u slot %u MAC %s)\n",
338 devbuf, dstbuf, be->bChannelNumber, be->wBPSTOffset,
339 bf->Beacon_Slot_Number, macbuf);
343 * @bce: beacon cache entry, referenced
345 ssize_t uwb_bce_print_IEs(struct uwb_dev *uwb_dev, struct uwb_beca_e *bce,
346 char *buf, size_t size)
349 struct uwb_rc_evt_beacon *be;
350 struct uwb_beacon_frame *bf;
352 struct uwb_ie_hdr *ies;
354 mutex_lock(&bce->mutex);
358 bf = (struct uwb_beacon_frame *)bce->be->BeaconInfo;
359 ies_len = be->wBeaconInfoLength - sizeof(struct uwb_beacon_frame);
360 ies = (struct uwb_ie_hdr *)bf->IEData;
362 result = uwb_ie_dump_hex(ies, ies_len, buf, size);
365 mutex_unlock(&bce->mutex);
371 * Verify that the beacon event, frame and IEs are ok
373 static int uwb_verify_beacon(struct uwb_rc *rc, struct uwb_event *evt,
374 struct uwb_rc_evt_beacon *be)
376 int result = -EINVAL;
377 struct uwb_beacon_frame *bf;
378 struct device *dev = &rc->uwb_dev.dev;
380 /* Is there enough data to decode a beacon frame? */
381 if (evt->notif.size < sizeof(*be) + sizeof(*bf)) {
382 dev_err(dev, "BEACON event: Not enough data to decode "
383 "(%zu vs %zu bytes needed)\n", evt->notif.size,
384 sizeof(*be) + sizeof(*bf));
387 /* FIXME: make sure beacon frame IEs are fine and that the whole thing
395 * Handle UWB_RC_EVT_BEACON events
397 * We check the beacon cache to see how the received beacon fares. If
398 * is there already we refresh the timestamp. If not we create a new
401 * According to the WHCI and WUSB specs, only one beacon frame is
402 * allowed per notification block, so we don't bother about scanning
405 int uwbd_evt_handle_rc_beacon(struct uwb_event *evt)
407 int result = -EINVAL;
409 struct uwb_rc_evt_beacon *be;
410 struct uwb_beacon_frame *bf;
411 struct uwb_beca_e *bce;
412 unsigned long last_ts;
415 be = container_of(evt->notif.rceb, struct uwb_rc_evt_beacon, rceb);
416 result = uwb_verify_beacon(rc, evt, be);
420 /* FIXME: handle alien beacons. */
421 if (be->bBeaconType == UWB_RC_BEACON_TYPE_OL_ALIEN ||
422 be->bBeaconType == UWB_RC_BEACON_TYPE_NOL_ALIEN) {
426 bf = (struct uwb_beacon_frame *) be->BeaconInfo;
429 * Drop beacons from devices with a NULL EUI-48 -- they cannot
430 * be uniquely identified.
432 * It's expected that these will all be WUSB devices and they
433 * have a WUSB specific connection method so ignoring them
434 * here shouldn't be a problem.
436 if (uwb_mac_addr_bcast(&bf->Device_Identifier))
439 mutex_lock(&rc->uwb_beca.mutex);
440 bce = __uwb_beca_find_bymac(rc, &bf->Device_Identifier);
442 /* Not in there, a new device is pinging */
443 uwb_beacon_print(evt->rc, be, bf);
444 bce = __uwb_beca_add(rc, be, bf, evt->ts_jiffies);
446 mutex_unlock(&rc->uwb_beca.mutex);
450 mutex_unlock(&rc->uwb_beca.mutex);
452 mutex_lock(&bce->mutex);
453 /* purge old beacon data */
456 last_ts = bce->ts_jiffies;
458 /* Update commonly used fields */
459 bce->ts_jiffies = evt->ts_jiffies;
461 bce->dev_addr = bf->hdr.SrcAddr;
462 bce->mac_addr = &bf->Device_Identifier;
463 be->wBPSTOffset = le16_to_cpu(be->wBPSTOffset);
464 be->wBeaconInfoLength = le16_to_cpu(be->wBeaconInfoLength);
465 stats_add_sample(&bce->lqe_stats, be->bLQI - 7);
466 stats_add_sample(&bce->rssi_stats, be->bRSSI + 18);
469 * This might be a beacon from a new device.
471 if (bce->uwb_dev == NULL)
472 uwbd_dev_onair(evt->rc, bce);
474 mutex_unlock(&bce->mutex);
476 return 1; /* we keep the event data */
480 * Handle UWB_RC_EVT_BEACON_SIZE events
484 int uwbd_evt_handle_rc_beacon_size(struct uwb_event *evt)
486 int result = -EINVAL;
487 struct device *dev = &evt->rc->uwb_dev.dev;
488 struct uwb_rc_evt_beacon_size *bs;
490 /* Is there enough data to decode the event? */
491 if (evt->notif.size < sizeof(*bs)) {
492 dev_err(dev, "BEACON SIZE notification: Not enough data to "
493 "decode (%zu vs %zu bytes needed)\n",
494 evt->notif.size, sizeof(*bs));
497 bs = container_of(evt->notif.rceb, struct uwb_rc_evt_beacon_size, rceb);
499 dev_info(dev, "Beacon size changed to %u bytes "
500 "(FIXME: action?)\n", le16_to_cpu(bs->wNewBeaconSize));
502 /* temporary hack until we do something with this message... */
503 static unsigned count;
504 if (++count % 1000 == 0)
505 dev_info(dev, "Beacon size changed %u times "
506 "(FIXME: action?)\n", count);
514 * uwbd_evt_handle_rc_bp_slot_change - handle a BP_SLOT_CHANGE event
515 * @evt: the BP_SLOT_CHANGE notification from the radio controller
517 * If the event indicates that no beacon period slots were available
518 * then radio controller has transitioned to a non-beaconing state.
519 * Otherwise, simply save the current beacon slot.
521 int uwbd_evt_handle_rc_bp_slot_change(struct uwb_event *evt)
523 struct uwb_rc *rc = evt->rc;
524 struct device *dev = &rc->uwb_dev.dev;
525 struct uwb_rc_evt_bp_slot_change *bpsc;
527 if (evt->notif.size < sizeof(*bpsc)) {
528 dev_err(dev, "BP SLOT CHANGE event: Not enough data\n");
531 bpsc = container_of(evt->notif.rceb, struct uwb_rc_evt_bp_slot_change, rceb);
533 mutex_lock(&rc->uwb_dev.mutex);
534 if (uwb_rc_evt_bp_slot_change_no_slot(bpsc)) {
535 dev_info(dev, "stopped beaconing: No free slots in BP\n");
538 rc->uwb_dev.beacon_slot = uwb_rc_evt_bp_slot_change_slot_num(bpsc);
539 mutex_unlock(&rc->uwb_dev.mutex);
545 * Handle UWB_RC_EVT_BPOIE_CHANGE events
550 struct uwb_ie_hdr hdr;
553 } __attribute__((packed));
555 int uwbd_evt_handle_rc_bpoie_change(struct uwb_event *evt)
557 int result = -EINVAL;
558 struct device *dev = &evt->rc->uwb_dev.dev;
559 struct uwb_rc_evt_bpoie_change *bpoiec;
560 struct uwb_ie_bpo *bpoie;
561 static unsigned count; /* FIXME: this is a temp hack */
564 /* Is there enough data to decode it? */
565 if (evt->notif.size < sizeof(*bpoiec)) {
566 dev_err(dev, "BPOIEC notification: Not enough data to "
567 "decode (%zu vs %zu bytes needed)\n",
568 evt->notif.size, sizeof(*bpoiec));
571 bpoiec = container_of(evt->notif.rceb, struct uwb_rc_evt_bpoie_change, rceb);
572 iesize = le16_to_cpu(bpoiec->wBPOIELength);
573 if (iesize < sizeof(*bpoie)) {
574 dev_err(dev, "BPOIEC notification: Not enough IE data to "
575 "decode (%zu vs %zu bytes needed)\n",
576 iesize, sizeof(*bpoie));
579 if (++count % 1000 == 0) /* Lame placeholder */
580 dev_info(dev, "BPOIE: %u changes received\n", count);
582 * FIXME: At this point we should go over all the IEs in the
583 * bpoiec->BPOIE array and act on each.
591 * uwb_bg_joined - is the RC in a beacon group?
592 * @rc: the radio controller
594 * Returns true if the radio controller is in a beacon group (even if
595 * it's the sole member).
597 int uwb_bg_joined(struct uwb_rc *rc)
599 return rc->beaconing != -1;
601 EXPORT_SYMBOL_GPL(uwb_bg_joined);
604 * Print beaconing state.
606 static ssize_t uwb_rc_beacon_show(struct device *dev,
607 struct device_attribute *attr, char *buf)
609 struct uwb_dev *uwb_dev = to_uwb_dev(dev);
610 struct uwb_rc *rc = uwb_dev->rc;
613 mutex_lock(&rc->uwb_dev.mutex);
614 result = sprintf(buf, "%d\n", rc->beaconing);
615 mutex_unlock(&rc->uwb_dev.mutex);
620 * Start beaconing on the specified channel, or stop beaconing.
622 * The BPST offset of when to start searching for a beacon group to
623 * join may be specified.
625 static ssize_t uwb_rc_beacon_store(struct device *dev,
626 struct device_attribute *attr,
627 const char *buf, size_t size)
629 struct uwb_dev *uwb_dev = to_uwb_dev(dev);
630 struct uwb_rc *rc = uwb_dev->rc;
632 unsigned bpst_offset = 0;
633 ssize_t result = -EINVAL;
635 result = sscanf(buf, "%d %u\n", &channel, &bpst_offset);
637 result = uwb_rc_beacon(rc, channel, bpst_offset);
639 return result < 0 ? result : size;
641 DEVICE_ATTR(beacon, S_IRUGO | S_IWUSR, uwb_rc_beacon_show, uwb_rc_beacon_store);