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
25 #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>
32 #include "uwb-internal.h"
34 /* Start Beaconing command structure */
35 struct uwb_rc_cmd_start_beacon {
39 } __attribute__((packed));
42 static int uwb_rc_start_beacon(struct uwb_rc *rc, u16 bpst_offset, u8 channel)
45 struct uwb_rc_cmd_start_beacon *cmd;
46 struct uwb_rc_evt_confirm reply;
48 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
51 cmd->rccb.bCommandType = UWB_RC_CET_GENERAL;
52 cmd->rccb.wCommand = cpu_to_le16(UWB_RC_CMD_START_BEACON);
53 cmd->wBPSTOffset = cpu_to_le16(bpst_offset);
54 cmd->bChannelNumber = channel;
55 reply.rceb.bEventType = UWB_RC_CET_GENERAL;
56 reply.rceb.wEvent = UWB_RC_CMD_START_BEACON;
57 result = uwb_rc_cmd(rc, "START-BEACON", &cmd->rccb, sizeof(*cmd),
58 &reply.rceb, sizeof(reply));
61 if (reply.bResultCode != UWB_RC_RES_SUCCESS) {
62 dev_err(&rc->uwb_dev.dev,
63 "START-BEACON: command execution failed: %s (%d)\n",
64 uwb_rc_strerror(reply.bResultCode), reply.bResultCode);
72 static int uwb_rc_stop_beacon(struct uwb_rc *rc)
76 struct uwb_rc_evt_confirm reply;
78 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
81 cmd->bCommandType = UWB_RC_CET_GENERAL;
82 cmd->wCommand = cpu_to_le16(UWB_RC_CMD_STOP_BEACON);
83 reply.rceb.bEventType = UWB_RC_CET_GENERAL;
84 reply.rceb.wEvent = UWB_RC_CMD_STOP_BEACON;
85 result = uwb_rc_cmd(rc, "STOP-BEACON", cmd, sizeof(*cmd),
86 &reply.rceb, sizeof(reply));
89 if (reply.bResultCode != UWB_RC_RES_SUCCESS) {
90 dev_err(&rc->uwb_dev.dev,
91 "STOP-BEACON: command execution failed: %s (%d)\n",
92 uwb_rc_strerror(reply.bResultCode), reply.bResultCode);
103 * @rc: UWB Radio Controller to operate on
104 * @channel: UWB channel on which to beacon (WUSB[table
105 * 5-12]). If -1, stop beaconing.
106 * @bpst_offset: Beacon Period Start Time offset; FIXME-do zero
108 * According to WHCI 0.95 [4.13.6] the driver will only receive the RCEB
109 * of a SET IE command after the device sent the first beacon that includes
110 * the IEs specified in the SET IE command. So, after we start beaconing we
111 * check if there is anything in the IE cache and call the SET IE command
114 int uwb_rc_beacon(struct uwb_rc *rc, int channel, unsigned bpst_offset)
117 struct device *dev = &rc->uwb_dev.dev;
122 result = uwb_rc_stop_beacon(rc);
124 /* channel >= 0...dah */
125 result = uwb_rc_start_beacon(rc, bpst_offset, channel);
128 if (le16_to_cpu(rc->ies->wIELength) > 0) {
129 result = uwb_rc_set_ie(rc, rc->ies);
131 dev_err(dev, "Cannot set new IE on device: "
133 result = uwb_rc_stop_beacon(rc);
141 rc->beaconing = channel;
148 * The purpose of this is to speed up the lookup of becon information
149 * when a new beacon arrives. The UWB Daemon uses it also to keep a
150 * tab of which devices are in radio distance and which not. When a
151 * device's beacon stays present for more than a certain amount of
152 * time, it is considered a new, usable device. When a beacon ceases
153 * to be received for a certain amount of time, it is considered that
154 * the device is gone.
156 * FIXME: use an allocator for the entries
157 * FIXME: use something faster for search than a list
160 void uwb_bce_kfree(struct kref *_bce)
162 struct uwb_beca_e *bce = container_of(_bce, struct uwb_beca_e, refcnt);
169 /* Find a beacon by dev addr in the cache */
171 struct uwb_beca_e *__uwb_beca_find_bydev(struct uwb_rc *rc,
172 const struct uwb_dev_addr *dev_addr)
174 struct uwb_beca_e *bce, *next;
175 list_for_each_entry_safe(bce, next, &rc->uwb_beca.list, node) {
176 if (!memcmp(&bce->dev_addr, dev_addr, sizeof(bce->dev_addr)))
184 /* Find a beacon by dev addr in the cache */
186 struct uwb_beca_e *__uwb_beca_find_bymac(struct uwb_rc *rc,
187 const struct uwb_mac_addr *mac_addr)
189 struct uwb_beca_e *bce, *next;
190 list_for_each_entry_safe(bce, next, &rc->uwb_beca.list, node) {
191 if (!memcmp(bce->mac_addr, mac_addr->data,
192 sizeof(struct uwb_mac_addr)))
201 * uwb_dev_get_by_devaddr - get a UWB device with a specific DevAddr
202 * @rc: the radio controller that saw the device
203 * @devaddr: DevAddr of the UWB device to find
205 * There may be more than one matching device (in the case of a
206 * DevAddr conflict), but only the first one is returned.
208 struct uwb_dev *uwb_dev_get_by_devaddr(struct uwb_rc *rc,
209 const struct uwb_dev_addr *devaddr)
211 struct uwb_dev *found = NULL;
212 struct uwb_beca_e *bce;
214 mutex_lock(&rc->uwb_beca.mutex);
215 bce = __uwb_beca_find_bydev(rc, devaddr);
217 found = uwb_dev_try_get(rc, bce->uwb_dev);
218 mutex_unlock(&rc->uwb_beca.mutex);
224 * uwb_dev_get_by_macaddr - get a UWB device with a specific EUI-48
225 * @rc: the radio controller that saw the device
226 * @devaddr: EUI-48 of the UWB device to find
228 struct uwb_dev *uwb_dev_get_by_macaddr(struct uwb_rc *rc,
229 const struct uwb_mac_addr *macaddr)
231 struct uwb_dev *found = NULL;
232 struct uwb_beca_e *bce;
234 mutex_lock(&rc->uwb_beca.mutex);
235 bce = __uwb_beca_find_bymac(rc, macaddr);
237 found = uwb_dev_try_get(rc, bce->uwb_dev);
238 mutex_unlock(&rc->uwb_beca.mutex);
243 /* Initialize a beacon cache entry */
244 static void uwb_beca_e_init(struct uwb_beca_e *bce)
246 mutex_init(&bce->mutex);
247 kref_init(&bce->refcnt);
248 stats_init(&bce->lqe_stats);
249 stats_init(&bce->rssi_stats);
253 * Add a beacon to the cache
255 * @be: Beacon event information
256 * @bf: Beacon frame (part of b, really)
257 * @ts_jiffies: Timestamp (in jiffies) when the beacon was received
260 struct uwb_beca_e *__uwb_beca_add(struct uwb_rc *rc,
261 struct uwb_rc_evt_beacon *be,
262 struct uwb_beacon_frame *bf,
263 unsigned long ts_jiffies)
265 struct uwb_beca_e *bce;
267 bce = kzalloc(sizeof(*bce), GFP_KERNEL);
270 uwb_beca_e_init(bce);
271 bce->ts_jiffies = ts_jiffies;
273 list_add(&bce->node, &rc->uwb_beca.list);
278 * Wipe out beacon entries that became stale
280 * Remove associated devicest too.
282 void uwb_beca_purge(struct uwb_rc *rc)
284 struct uwb_beca_e *bce, *next;
285 unsigned long expires;
287 mutex_lock(&rc->uwb_beca.mutex);
288 list_for_each_entry_safe(bce, next, &rc->uwb_beca.list, node) {
289 expires = bce->ts_jiffies + msecs_to_jiffies(beacon_timeout_ms);
290 if (time_after(jiffies, expires)) {
291 uwbd_dev_offair(bce);
294 mutex_unlock(&rc->uwb_beca.mutex);
297 /* Clean up the whole beacon cache. Called on shutdown */
298 void uwb_beca_release(struct uwb_rc *rc)
300 struct uwb_beca_e *bce, *next;
302 mutex_lock(&rc->uwb_beca.mutex);
303 list_for_each_entry_safe(bce, next, &rc->uwb_beca.list, node) {
304 list_del(&bce->node);
307 mutex_unlock(&rc->uwb_beca.mutex);
310 static void uwb_beacon_print(struct uwb_rc *rc, struct uwb_rc_evt_beacon *be,
311 struct uwb_beacon_frame *bf)
313 char macbuf[UWB_ADDR_STRSIZE];
314 char devbuf[UWB_ADDR_STRSIZE];
315 char dstbuf[UWB_ADDR_STRSIZE];
317 uwb_mac_addr_print(macbuf, sizeof(macbuf), &bf->Device_Identifier);
318 uwb_dev_addr_print(devbuf, sizeof(devbuf), &bf->hdr.SrcAddr);
319 uwb_dev_addr_print(dstbuf, sizeof(dstbuf), &bf->hdr.DestAddr);
320 dev_info(&rc->uwb_dev.dev,
321 "BEACON from %s to %s (ch%u offset %u slot %u MAC %s)\n",
322 devbuf, dstbuf, be->bChannelNumber, be->wBPSTOffset,
323 bf->Beacon_Slot_Number, macbuf);
327 * @bce: beacon cache entry, referenced
329 ssize_t uwb_bce_print_IEs(struct uwb_dev *uwb_dev, struct uwb_beca_e *bce,
330 char *buf, size_t size)
333 struct uwb_rc_evt_beacon *be;
334 struct uwb_beacon_frame *bf;
336 struct uwb_ie_hdr *ies;
338 mutex_lock(&bce->mutex);
342 bf = (struct uwb_beacon_frame *)bce->be->BeaconInfo;
343 ies_len = be->wBeaconInfoLength - sizeof(struct uwb_beacon_frame);
344 ies = (struct uwb_ie_hdr *)bf->IEData;
346 result = uwb_ie_dump_hex(ies, ies_len, buf, size);
349 mutex_unlock(&bce->mutex);
355 * Verify that the beacon event, frame and IEs are ok
357 static int uwb_verify_beacon(struct uwb_rc *rc, struct uwb_event *evt,
358 struct uwb_rc_evt_beacon *be)
360 int result = -EINVAL;
361 struct uwb_beacon_frame *bf;
362 struct device *dev = &rc->uwb_dev.dev;
364 /* Is there enough data to decode a beacon frame? */
365 if (evt->notif.size < sizeof(*be) + sizeof(*bf)) {
366 dev_err(dev, "BEACON event: Not enough data to decode "
367 "(%zu vs %zu bytes needed)\n", evt->notif.size,
368 sizeof(*be) + sizeof(*bf));
371 /* FIXME: make sure beacon frame IEs are fine and that the whole thing
379 * Handle UWB_RC_EVT_BEACON events
381 * We check the beacon cache to see how the received beacon fares. If
382 * is there already we refresh the timestamp. If not we create a new
385 * According to the WHCI and WUSB specs, only one beacon frame is
386 * allowed per notification block, so we don't bother about scanning
389 int uwbd_evt_handle_rc_beacon(struct uwb_event *evt)
391 int result = -EINVAL;
393 struct uwb_rc_evt_beacon *be;
394 struct uwb_beacon_frame *bf;
395 struct uwb_beca_e *bce;
396 unsigned long last_ts;
399 be = container_of(evt->notif.rceb, struct uwb_rc_evt_beacon, rceb);
400 result = uwb_verify_beacon(rc, evt, be);
404 /* FIXME: handle alien beacons. */
405 if (be->bBeaconType == UWB_RC_BEACON_TYPE_OL_ALIEN ||
406 be->bBeaconType == UWB_RC_BEACON_TYPE_NOL_ALIEN) {
410 bf = (struct uwb_beacon_frame *) be->BeaconInfo;
413 * Drop beacons from devices with a NULL EUI-48 -- they cannot
414 * be uniquely identified.
416 * It's expected that these will all be WUSB devices and they
417 * have a WUSB specific connection method so ignoring them
418 * here shouldn't be a problem.
420 if (uwb_mac_addr_bcast(&bf->Device_Identifier))
423 mutex_lock(&rc->uwb_beca.mutex);
424 bce = __uwb_beca_find_bymac(rc, &bf->Device_Identifier);
426 /* Not in there, a new device is pinging */
427 uwb_beacon_print(evt->rc, be, bf);
428 bce = __uwb_beca_add(rc, be, bf, evt->ts_jiffies);
430 mutex_unlock(&rc->uwb_beca.mutex);
434 mutex_unlock(&rc->uwb_beca.mutex);
436 mutex_lock(&bce->mutex);
437 /* purge old beacon data */
440 last_ts = bce->ts_jiffies;
442 /* Update commonly used fields */
443 bce->ts_jiffies = evt->ts_jiffies;
445 bce->dev_addr = bf->hdr.SrcAddr;
446 bce->mac_addr = &bf->Device_Identifier;
447 be->wBPSTOffset = le16_to_cpu(be->wBPSTOffset);
448 be->wBeaconInfoLength = le16_to_cpu(be->wBeaconInfoLength);
449 stats_add_sample(&bce->lqe_stats, be->bLQI - 7);
450 stats_add_sample(&bce->rssi_stats, be->bRSSI + 18);
453 * This might be a beacon from a new device.
455 if (bce->uwb_dev == NULL)
456 uwbd_dev_onair(evt->rc, bce);
458 mutex_unlock(&bce->mutex);
460 return 1; /* we keep the event data */
464 * Handle UWB_RC_EVT_BEACON_SIZE events
468 int uwbd_evt_handle_rc_beacon_size(struct uwb_event *evt)
470 int result = -EINVAL;
471 struct device *dev = &evt->rc->uwb_dev.dev;
472 struct uwb_rc_evt_beacon_size *bs;
474 /* Is there enough data to decode the event? */
475 if (evt->notif.size < sizeof(*bs)) {
476 dev_err(dev, "BEACON SIZE notification: Not enough data to "
477 "decode (%zu vs %zu bytes needed)\n",
478 evt->notif.size, sizeof(*bs));
481 bs = container_of(evt->notif.rceb, struct uwb_rc_evt_beacon_size, rceb);
483 dev_info(dev, "Beacon size changed to %u bytes "
484 "(FIXME: action?)\n", le16_to_cpu(bs->wNewBeaconSize));
486 /* temporary hack until we do something with this message... */
487 static unsigned count;
488 if (++count % 1000 == 0)
489 dev_info(dev, "Beacon size changed %u times "
490 "(FIXME: action?)\n", count);
498 * uwbd_evt_handle_rc_bp_slot_change - handle a BP_SLOT_CHANGE event
499 * @evt: the BP_SLOT_CHANGE notification from the radio controller
501 * If the event indicates that no beacon period slots were available
502 * then radio controller has transitioned to a non-beaconing state.
503 * Otherwise, simply save the current beacon slot.
505 int uwbd_evt_handle_rc_bp_slot_change(struct uwb_event *evt)
507 struct uwb_rc *rc = evt->rc;
508 struct device *dev = &rc->uwb_dev.dev;
509 struct uwb_rc_evt_bp_slot_change *bpsc;
511 if (evt->notif.size < sizeof(*bpsc)) {
512 dev_err(dev, "BP SLOT CHANGE event: Not enough data\n");
515 bpsc = container_of(evt->notif.rceb, struct uwb_rc_evt_bp_slot_change, rceb);
517 mutex_lock(&rc->uwb_dev.mutex);
518 if (uwb_rc_evt_bp_slot_change_no_slot(bpsc)) {
519 dev_info(dev, "stopped beaconing: No free slots in BP\n");
522 rc->uwb_dev.beacon_slot = uwb_rc_evt_bp_slot_change_slot_num(bpsc);
523 mutex_unlock(&rc->uwb_dev.mutex);
529 * Handle UWB_RC_EVT_BPOIE_CHANGE events
534 struct uwb_ie_hdr hdr;
537 } __attribute__((packed));
539 int uwbd_evt_handle_rc_bpoie_change(struct uwb_event *evt)
541 int result = -EINVAL;
542 struct device *dev = &evt->rc->uwb_dev.dev;
543 struct uwb_rc_evt_bpoie_change *bpoiec;
544 struct uwb_ie_bpo *bpoie;
545 static unsigned count; /* FIXME: this is a temp hack */
548 /* Is there enough data to decode it? */
549 if (evt->notif.size < sizeof(*bpoiec)) {
550 dev_err(dev, "BPOIEC notification: Not enough data to "
551 "decode (%zu vs %zu bytes needed)\n",
552 evt->notif.size, sizeof(*bpoiec));
555 bpoiec = container_of(evt->notif.rceb, struct uwb_rc_evt_bpoie_change, rceb);
556 iesize = le16_to_cpu(bpoiec->wBPOIELength);
557 if (iesize < sizeof(*bpoie)) {
558 dev_err(dev, "BPOIEC notification: Not enough IE data to "
559 "decode (%zu vs %zu bytes needed)\n",
560 iesize, sizeof(*bpoie));
563 if (++count % 1000 == 0) /* Lame placeholder */
564 dev_info(dev, "BPOIE: %u changes received\n", count);
566 * FIXME: At this point we should go over all the IEs in the
567 * bpoiec->BPOIE array and act on each.
575 * Print beaconing state.
577 static ssize_t uwb_rc_beacon_show(struct device *dev,
578 struct device_attribute *attr, char *buf)
580 struct uwb_dev *uwb_dev = to_uwb_dev(dev);
581 struct uwb_rc *rc = uwb_dev->rc;
584 mutex_lock(&rc->uwb_dev.mutex);
585 result = sprintf(buf, "%d\n", rc->beaconing);
586 mutex_unlock(&rc->uwb_dev.mutex);
591 * Start beaconing on the specified channel, or stop beaconing.
593 static ssize_t uwb_rc_beacon_store(struct device *dev,
594 struct device_attribute *attr,
595 const char *buf, size_t size)
597 struct uwb_dev *uwb_dev = to_uwb_dev(dev);
598 struct uwb_rc *rc = uwb_dev->rc;
600 ssize_t result = -EINVAL;
602 result = sscanf(buf, "%d", &channel);
604 result = uwb_radio_force_channel(rc, channel);
606 return result < 0 ? result : size;
608 DEVICE_ATTR(beacon, S_IRUGO | S_IWUSR, uwb_rc_beacon_show, uwb_rc_beacon_store);