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