uwb: add basic radio manager
[safe/jmp/linux-2.6] / drivers / uwb / beacon.c
1 /*
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  */
25
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"
33
34 #define D_LOCAL 0
35 #include <linux/uwb/debug.h>
36
37 /** Start Beaconing command structure */
38 struct uwb_rc_cmd_start_beacon {
39         struct uwb_rccb rccb;
40         __le16 wBPSTOffset;
41         u8 bChannelNumber;
42 } __attribute__((packed));
43
44
45 static int uwb_rc_start_beacon(struct uwb_rc *rc, u16 bpst_offset, u8 channel)
46 {
47         int result;
48         struct uwb_rc_cmd_start_beacon *cmd;
49         struct uwb_rc_evt_confirm reply;
50
51         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
52         if (cmd == NULL)
53                 return -ENOMEM;
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));
62         if (result < 0)
63                 goto error_cmd;
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);
68                 result = -EIO;
69         }
70 error_cmd:
71         kfree(cmd);
72         return result;
73 }
74
75 static int uwb_rc_stop_beacon(struct uwb_rc *rc)
76 {
77         int result;
78         struct uwb_rccb *cmd;
79         struct uwb_rc_evt_confirm reply;
80
81         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
82         if (cmd == NULL)
83                 return -ENOMEM;
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));
90         if (result < 0)
91                 goto error_cmd;
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);
96                 result = -EIO;
97         }
98 error_cmd:
99         kfree(cmd);
100         return result;
101 }
102
103 /*
104  * Start/stop beacons
105  *
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
110  *
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
115  * if needed.
116  */
117 int uwb_rc_beacon(struct uwb_rc *rc, int channel, unsigned bpst_offset)
118 {
119         int result;
120         struct device *dev = &rc->uwb_dev.dev;
121
122         if (channel < 0)
123                 channel = -1;
124         if (channel == -1)
125                 result = uwb_rc_stop_beacon(rc);
126         else {
127                 /* channel >= 0...dah */
128                 result = uwb_rc_start_beacon(rc, bpst_offset, channel);
129                 if (result < 0)
130                         return result;
131                 if (le16_to_cpu(rc->ies->wIELength) > 0) {
132                         result = uwb_rc_set_ie(rc, rc->ies);
133                         if (result < 0) {
134                                 dev_err(dev, "Cannot set new IE on device: "
135                                         "%d\n", result);
136                                 result = uwb_rc_stop_beacon(rc);
137                                 channel = -1;
138                                 bpst_offset = 0;
139                         }
140                 }
141         }
142
143         if (result >= 0) {
144                 rc->beaconing = channel;
145                 uwb_notify(rc, NULL, uwb_bg_joined(rc) ? UWB_NOTIF_BG_JOIN : UWB_NOTIF_BG_LEAVE);
146         }
147         return result;
148 }
149
150 /*
151  * Beacon cache
152  *
153  * The purpose of this is to speed up the lookup of becon information
154  * when a new beacon arrives. The UWB Daemon uses it also to keep a
155  * tab of which devices are in radio distance and which not. When a
156  * device's beacon stays present for more than a certain amount of
157  * time, it is considered a new, usable device. When a beacon ceases
158  * to be received for a certain amount of time, it is considered that
159  * the device is gone.
160  *
161  * FIXME: use an allocator for the entries
162  * FIXME: use something faster for search than a list
163  */
164
165 void uwb_bce_kfree(struct kref *_bce)
166 {
167         struct uwb_beca_e *bce = container_of(_bce, struct uwb_beca_e, refcnt);
168
169         kfree(bce->be);
170         kfree(bce);
171 }
172
173
174 /* Find a beacon by dev addr in the cache */
175 static
176 struct uwb_beca_e *__uwb_beca_find_bydev(struct uwb_rc *rc,
177                                          const struct uwb_dev_addr *dev_addr)
178 {
179         struct uwb_beca_e *bce, *next;
180         list_for_each_entry_safe(bce, next, &rc->uwb_beca.list, node) {
181                 d_printf(6, NULL, "looking for addr %02x:%02x in %02x:%02x\n",
182                          dev_addr->data[0], dev_addr->data[1],
183                          bce->dev_addr.data[0], bce->dev_addr.data[1]);
184                 if (!memcmp(&bce->dev_addr, dev_addr, sizeof(bce->dev_addr)))
185                         goto out;
186         }
187         bce = NULL;
188 out:
189         return bce;
190 }
191
192 /* Find a beacon by dev addr in the cache */
193 static
194 struct uwb_beca_e *__uwb_beca_find_bymac(struct uwb_rc *rc, 
195                                          const struct uwb_mac_addr *mac_addr)
196 {
197         struct uwb_beca_e *bce, *next;
198         list_for_each_entry_safe(bce, next, &rc->uwb_beca.list, node) {
199                 if (!memcmp(bce->mac_addr, mac_addr->data,
200                             sizeof(struct uwb_mac_addr)))
201                         goto out;
202         }
203         bce = NULL;
204 out:
205         return bce;
206 }
207
208 /**
209  * uwb_dev_get_by_devaddr - get a UWB device with a specific DevAddr
210  * @rc:      the radio controller that saw the device
211  * @devaddr: DevAddr of the UWB device to find
212  *
213  * There may be more than one matching device (in the case of a
214  * DevAddr conflict), but only the first one is returned.
215  */
216 struct uwb_dev *uwb_dev_get_by_devaddr(struct uwb_rc *rc,
217                                        const struct uwb_dev_addr *devaddr)
218 {
219         struct uwb_dev *found = NULL;
220         struct uwb_beca_e *bce;
221
222         mutex_lock(&rc->uwb_beca.mutex);
223         bce = __uwb_beca_find_bydev(rc, devaddr);
224         if (bce)
225                 found = uwb_dev_try_get(rc, bce->uwb_dev);
226         mutex_unlock(&rc->uwb_beca.mutex);
227
228         return found;
229 }
230
231 /**
232  * uwb_dev_get_by_macaddr - get a UWB device with a specific EUI-48
233  * @rc:      the radio controller that saw the device
234  * @devaddr: EUI-48 of the UWB device to find
235  */
236 struct uwb_dev *uwb_dev_get_by_macaddr(struct uwb_rc *rc,
237                                        const struct uwb_mac_addr *macaddr)
238 {
239         struct uwb_dev *found = NULL;
240         struct uwb_beca_e *bce;
241
242         mutex_lock(&rc->uwb_beca.mutex);
243         bce = __uwb_beca_find_bymac(rc, macaddr);
244         if (bce)
245                 found = uwb_dev_try_get(rc, bce->uwb_dev);
246         mutex_unlock(&rc->uwb_beca.mutex);
247
248         return found;
249 }
250
251 /* Initialize a beacon cache entry */
252 static void uwb_beca_e_init(struct uwb_beca_e *bce)
253 {
254         mutex_init(&bce->mutex);
255         kref_init(&bce->refcnt);
256         stats_init(&bce->lqe_stats);
257         stats_init(&bce->rssi_stats);
258 }
259
260 /*
261  * Add a beacon to the cache
262  *
263  * @be:         Beacon event information
264  * @bf:         Beacon frame (part of b, really)
265  * @ts_jiffies: Timestamp (in jiffies) when the beacon was received
266  */
267 static
268 struct uwb_beca_e *__uwb_beca_add(struct uwb_rc *rc,
269                                   struct uwb_rc_evt_beacon *be,
270                                   struct uwb_beacon_frame *bf,
271                                   unsigned long ts_jiffies)
272 {
273         struct uwb_beca_e *bce;
274
275         bce = kzalloc(sizeof(*bce), GFP_KERNEL);
276         if (bce == NULL)
277                 return NULL;
278         uwb_beca_e_init(bce);
279         bce->ts_jiffies = ts_jiffies;
280         bce->uwb_dev = NULL;
281         list_add(&bce->node, &rc->uwb_beca.list);
282         return bce;
283 }
284
285 /*
286  * Wipe out beacon entries that became stale
287  *
288  * Remove associated devicest too.
289  */
290 void uwb_beca_purge(struct uwb_rc *rc)
291 {
292         struct uwb_beca_e *bce, *next;
293         unsigned long expires;
294
295         mutex_lock(&rc->uwb_beca.mutex);
296         list_for_each_entry_safe(bce, next, &rc->uwb_beca.list, node) {
297                 expires = bce->ts_jiffies + msecs_to_jiffies(beacon_timeout_ms);
298                 if (time_after(jiffies, expires)) {
299                         uwbd_dev_offair(bce);
300                         list_del(&bce->node);
301                         uwb_bce_put(bce);
302                 }
303         }
304         mutex_unlock(&rc->uwb_beca.mutex);
305 }
306
307 /* Clean up the whole beacon cache. Called on shutdown */
308 void uwb_beca_release(struct uwb_rc *rc)
309 {
310         struct uwb_beca_e *bce, *next;
311
312         mutex_lock(&rc->uwb_beca.mutex);
313         list_for_each_entry_safe(bce, next, &rc->uwb_beca.list, node) {
314                 list_del(&bce->node);
315                 uwb_bce_put(bce);
316         }
317         mutex_unlock(&rc->uwb_beca.mutex);
318 }
319
320 static void uwb_beacon_print(struct uwb_rc *rc, struct uwb_rc_evt_beacon *be,
321                              struct uwb_beacon_frame *bf)
322 {
323         char macbuf[UWB_ADDR_STRSIZE];
324         char devbuf[UWB_ADDR_STRSIZE];
325         char dstbuf[UWB_ADDR_STRSIZE];
326
327         uwb_mac_addr_print(macbuf, sizeof(macbuf), &bf->Device_Identifier);
328         uwb_dev_addr_print(devbuf, sizeof(devbuf), &bf->hdr.SrcAddr);
329         uwb_dev_addr_print(dstbuf, sizeof(dstbuf), &bf->hdr.DestAddr);
330         dev_info(&rc->uwb_dev.dev,
331                  "BEACON from %s to %s (ch%u offset %u slot %u MAC %s)\n",
332                  devbuf, dstbuf, be->bChannelNumber, be->wBPSTOffset,
333                  bf->Beacon_Slot_Number, macbuf);
334 }
335
336 /*
337  * @bce: beacon cache entry, referenced
338  */
339 ssize_t uwb_bce_print_IEs(struct uwb_dev *uwb_dev, struct uwb_beca_e *bce,
340                           char *buf, size_t size)
341 {
342         ssize_t result = 0;
343         struct uwb_rc_evt_beacon *be;
344         struct uwb_beacon_frame *bf;
345         int ies_len;
346         struct uwb_ie_hdr *ies;
347
348         mutex_lock(&bce->mutex);
349
350         be = bce->be;
351         if (be) {
352                 bf = (struct uwb_beacon_frame *)bce->be->BeaconInfo;
353                 ies_len = be->wBeaconInfoLength - sizeof(struct uwb_beacon_frame);
354                 ies = (struct uwb_ie_hdr *)bf->IEData;
355
356                 result = uwb_ie_dump_hex(ies, ies_len, buf, size);
357         }
358
359         mutex_unlock(&bce->mutex);
360
361         return result;
362 }
363
364 /*
365  * Verify that the beacon event, frame and IEs are ok
366  */
367 static int uwb_verify_beacon(struct uwb_rc *rc, struct uwb_event *evt,
368                              struct uwb_rc_evt_beacon *be)
369 {
370         int result = -EINVAL;
371         struct uwb_beacon_frame *bf;
372         struct device *dev = &rc->uwb_dev.dev;
373
374         /* Is there enough data to decode a beacon frame? */
375         if (evt->notif.size < sizeof(*be) + sizeof(*bf)) {
376                 dev_err(dev, "BEACON event: Not enough data to decode "
377                         "(%zu vs %zu bytes needed)\n", evt->notif.size,
378                         sizeof(*be) + sizeof(*bf));
379                 goto error;
380         }
381         /* FIXME: make sure beacon frame IEs are fine and that the whole thing
382          * is consistent */
383         result = 0;
384 error:
385         return result;
386 }
387
388 /*
389  * Handle UWB_RC_EVT_BEACON events
390  *
391  * We check the beacon cache to see how the received beacon fares. If
392  * is there already we refresh the timestamp. If not we create a new
393  * entry.
394  *
395  * According to the WHCI and WUSB specs, only one beacon frame is
396  * allowed per notification block, so we don't bother about scanning
397  * for more.
398  */
399 int uwbd_evt_handle_rc_beacon(struct uwb_event *evt)
400 {
401         int result = -EINVAL;
402         struct uwb_rc *rc;
403         struct uwb_rc_evt_beacon *be;
404         struct uwb_beacon_frame *bf;
405         struct uwb_beca_e *bce;
406         unsigned long last_ts;
407
408         rc = evt->rc;
409         be = container_of(evt->notif.rceb, struct uwb_rc_evt_beacon, rceb);
410         result = uwb_verify_beacon(rc, evt, be);
411         if (result < 0)
412                 return result;
413
414         /* FIXME: handle alien beacons. */
415         if (be->bBeaconType == UWB_RC_BEACON_TYPE_OL_ALIEN ||
416             be->bBeaconType == UWB_RC_BEACON_TYPE_NOL_ALIEN) {
417                 return -ENOSYS;
418         }
419
420         bf = (struct uwb_beacon_frame *) be->BeaconInfo;
421
422         /*
423          * Drop beacons from devices with a NULL EUI-48 -- they cannot
424          * be uniquely identified.
425          *
426          * It's expected that these will all be WUSB devices and they
427          * have a WUSB specific connection method so ignoring them
428          * here shouldn't be a problem.
429          */
430         if (uwb_mac_addr_bcast(&bf->Device_Identifier))
431                 return 0;
432
433         mutex_lock(&rc->uwb_beca.mutex);
434         bce = __uwb_beca_find_bymac(rc, &bf->Device_Identifier);
435         if (bce == NULL) {
436                 /* Not in there, a new device is pinging */
437                 uwb_beacon_print(evt->rc, be, bf);
438                 bce = __uwb_beca_add(rc, be, bf, evt->ts_jiffies);
439                 if (bce == NULL) {
440                         mutex_unlock(&rc->uwb_beca.mutex);
441                         return -ENOMEM;
442                 }
443         }
444         mutex_unlock(&rc->uwb_beca.mutex);
445
446         mutex_lock(&bce->mutex);
447         /* purge old beacon data */
448         kfree(bce->be);
449
450         last_ts = bce->ts_jiffies;
451
452         /* Update commonly used fields */
453         bce->ts_jiffies = evt->ts_jiffies;
454         bce->be = be;
455         bce->dev_addr = bf->hdr.SrcAddr;
456         bce->mac_addr = &bf->Device_Identifier;
457         be->wBPSTOffset = le16_to_cpu(be->wBPSTOffset);
458         be->wBeaconInfoLength = le16_to_cpu(be->wBeaconInfoLength);
459         stats_add_sample(&bce->lqe_stats, be->bLQI - 7);
460         stats_add_sample(&bce->rssi_stats, be->bRSSI + 18);
461
462         /*
463          * This might be a beacon from a new device.
464          */
465         if (bce->uwb_dev == NULL)
466                 uwbd_dev_onair(evt->rc, bce);
467
468         mutex_unlock(&bce->mutex);
469
470         return 1; /* we keep the event data */
471 }
472
473 /*
474  * Handle UWB_RC_EVT_BEACON_SIZE events
475  *
476  * XXXXX
477  */
478 int uwbd_evt_handle_rc_beacon_size(struct uwb_event *evt)
479 {
480         int result = -EINVAL;
481         struct device *dev = &evt->rc->uwb_dev.dev;
482         struct uwb_rc_evt_beacon_size *bs;
483
484         /* Is there enough data to decode the event? */
485         if (evt->notif.size < sizeof(*bs)) {
486                 dev_err(dev, "BEACON SIZE notification: Not enough data to "
487                         "decode (%zu vs %zu bytes needed)\n",
488                         evt->notif.size, sizeof(*bs));
489                 goto error;
490         }
491         bs = container_of(evt->notif.rceb, struct uwb_rc_evt_beacon_size, rceb);
492         if (0)
493                 dev_info(dev, "Beacon size changed to %u bytes "
494                         "(FIXME: action?)\n", le16_to_cpu(bs->wNewBeaconSize));
495         else {
496                 /* temporary hack until we do something with this message... */
497                 static unsigned count;
498                 if (++count % 1000 == 0)
499                         dev_info(dev, "Beacon size changed %u times "
500                                 "(FIXME: action?)\n", count);
501         }
502         result = 0;
503 error:
504         return result;
505 }
506
507 /**
508  * uwbd_evt_handle_rc_bp_slot_change - handle a BP_SLOT_CHANGE event
509  * @evt: the BP_SLOT_CHANGE notification from the radio controller
510  *
511  * If the event indicates that no beacon period slots were available
512  * then radio controller has transitioned to a non-beaconing state.
513  * Otherwise, simply save the current beacon slot.
514  */
515 int uwbd_evt_handle_rc_bp_slot_change(struct uwb_event *evt)
516 {
517         struct uwb_rc *rc = evt->rc;
518         struct device *dev = &rc->uwb_dev.dev;
519         struct uwb_rc_evt_bp_slot_change *bpsc;
520
521         if (evt->notif.size < sizeof(*bpsc)) {
522                 dev_err(dev, "BP SLOT CHANGE event: Not enough data\n");
523                 return -EINVAL;
524         }
525         bpsc = container_of(evt->notif.rceb, struct uwb_rc_evt_bp_slot_change, rceb);
526
527         mutex_lock(&rc->uwb_dev.mutex);
528         if (uwb_rc_evt_bp_slot_change_no_slot(bpsc)) {
529                 dev_info(dev, "stopped beaconing: No free slots in BP\n");
530                 rc->beaconing = -1;
531         } else
532                 rc->uwb_dev.beacon_slot = uwb_rc_evt_bp_slot_change_slot_num(bpsc);
533         mutex_unlock(&rc->uwb_dev.mutex);
534
535         return 0;
536 }
537
538 /**
539  * Handle UWB_RC_EVT_BPOIE_CHANGE events
540  *
541  * XXXXX
542  */
543 struct uwb_ie_bpo {
544         struct uwb_ie_hdr hdr;
545         u8                bp_length;
546         u8                data[];
547 } __attribute__((packed));
548
549 int uwbd_evt_handle_rc_bpoie_change(struct uwb_event *evt)
550 {
551         int result = -EINVAL;
552         struct device *dev = &evt->rc->uwb_dev.dev;
553         struct uwb_rc_evt_bpoie_change *bpoiec;
554         struct uwb_ie_bpo *bpoie;
555         static unsigned count;  /* FIXME: this is a temp hack */
556         size_t iesize;
557
558         /* Is there enough data to decode it? */
559         if (evt->notif.size < sizeof(*bpoiec)) {
560                 dev_err(dev, "BPOIEC notification: Not enough data to "
561                         "decode (%zu vs %zu bytes needed)\n",
562                         evt->notif.size, sizeof(*bpoiec));
563                 goto error;
564         }
565         bpoiec = container_of(evt->notif.rceb, struct uwb_rc_evt_bpoie_change, rceb);
566         iesize = le16_to_cpu(bpoiec->wBPOIELength);
567         if (iesize < sizeof(*bpoie)) {
568                 dev_err(dev, "BPOIEC notification: Not enough IE data to "
569                         "decode (%zu vs %zu bytes needed)\n",
570                         iesize, sizeof(*bpoie));
571                 goto error;
572         }
573         if (++count % 1000 == 0)        /* Lame placeholder */
574                 dev_info(dev, "BPOIE: %u changes received\n", count);
575         /*
576          * FIXME: At this point we should go over all the IEs in the
577          *        bpoiec->BPOIE array and act on each.
578          */
579         result = 0;
580 error:
581         return result;
582 }
583
584 /**
585  * uwb_bg_joined - is the RC in a beacon group?
586  * @rc: the radio controller
587  *
588  * Returns true if the radio controller is in a beacon group (even if
589  * it's the sole member).
590  */
591 int uwb_bg_joined(struct uwb_rc *rc)
592 {
593         return rc->beaconing != -1;
594 }
595 EXPORT_SYMBOL_GPL(uwb_bg_joined);
596
597 /*
598  * Print beaconing state.
599  */
600 static ssize_t uwb_rc_beacon_show(struct device *dev,
601                                   struct device_attribute *attr, char *buf)
602 {
603         struct uwb_dev *uwb_dev = to_uwb_dev(dev);
604         struct uwb_rc *rc = uwb_dev->rc;
605         ssize_t result;
606
607         mutex_lock(&rc->uwb_dev.mutex);
608         result = sprintf(buf, "%d\n", rc->beaconing);
609         mutex_unlock(&rc->uwb_dev.mutex);
610         return result;
611 }
612
613 /*
614  * Start beaconing on the specified channel, or stop beaconing.
615  */
616 static ssize_t uwb_rc_beacon_store(struct device *dev,
617                                    struct device_attribute *attr,
618                                    const char *buf, size_t size)
619 {
620         struct uwb_dev *uwb_dev = to_uwb_dev(dev);
621         struct uwb_rc *rc = uwb_dev->rc;
622         int channel;
623         ssize_t result = -EINVAL;
624
625         result = sscanf(buf, "%d", &channel);
626         if (result >= 1)
627                 result = uwb_radio_force_channel(rc, channel);
628
629         return result < 0 ? result : size;
630 }
631 DEVICE_ATTR(beacon, S_IRUGO | S_IWUSR, uwb_rc_beacon_show, uwb_rc_beacon_store);