blob: ec42ce92dbce8b647d0d95f0d9675a20da2e216a [file] [log] [blame]
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +01001/*
2 * Ultra Wide Band
3 * Neighborhood Management Daemon
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 * This daemon takes care of maintaing information that describes the
24 * UWB neighborhood that the radios in this machine can see. It also
25 * keeps a tab of which devices are visible, makes sure each HC sits
26 * on a different channel to avoid interfering, etc.
27 *
28 * Different drivers (radio controller, device, any API in general)
29 * communicate with this daemon through an event queue. Daemon wakes
30 * up, takes a list of events and handles them one by one; handling
31 * function is extracted from a table based on the event's type and
32 * subtype. Events are freed only if the handling function says so.
33 *
34 * . Lock protecting the event list has to be an spinlock and locked
35 * with IRQSAVE because it might be called from an interrupt
36 * context (ie: when events arrive and the notification drops
37 * down from the ISR).
38 *
39 * . UWB radio controller drivers queue events to the daemon using
40 * uwbd_event_queue(). They just get the event, chew it to make it
41 * look like UWBD likes it and pass it in a buffer allocated with
42 * uwb_event_alloc().
43 *
44 * EVENTS
45 *
46 * Events have a type, a subtype, a lenght, some other stuff and the
47 * data blob, which depends on the event. The header is 'struct
48 * uwb_event'; for payloads, see 'struct uwbd_evt_*'.
49 *
50 * EVENT HANDLER TABLES
51 *
52 * To find a handling function for an event, the type is used to index
53 * a subtype-table in the type-table. The subtype-table is indexed
54 * with the subtype to get the function that handles the event. Start
55 * with the main type-table 'uwbd_evt_type_handler'.
56 *
57 * DEVICES
58 *
59 * Devices are created when a bunch of beacons have been received and
60 * it is stablished that the device has stable radio presence. CREATED
61 * only, not configured. Devices are ONLY configured when an
62 * Application-Specific IE Probe is receieved, in which the device
63 * declares which Protocol ID it groks. Then the device is CONFIGURED
64 * (and the driver->probe() stuff of the device model is invoked).
65 *
66 * Devices are considered disconnected when a certain number of
67 * beacons are not received in an amount of time.
68 *
69 * Handler functions are called normally uwbd_evt_handle_*().
70 */
71
72#include <linux/kthread.h>
73#include <linux/module.h>
74#include <linux/freezer.h>
75#include "uwb-internal.h"
76
77#define D_LOCAL 1
78#include <linux/uwb/debug.h>
79
80
81/**
82 * UWBD Event handler function signature
83 *
84 * Return !0 if the event needs not to be freed (ie the handler
85 * takes/took care of it). 0 means the daemon code will free the
86 * event.
87 *
88 * @evt->rc is already referenced and guaranteed to exist. See
89 * uwb_evt_handle().
90 */
91typedef int (*uwbd_evt_handler_f)(struct uwb_event *);
92
93/**
94 * Properties of a UWBD event
95 *
96 * @handler: the function that will handle this event
97 * @name: text name of event
98 */
99struct uwbd_event {
100 uwbd_evt_handler_f handler;
101 const char *name;
102};
103
104/** Table of handlers for and properties of the UWBD Radio Control Events */
105static
106struct uwbd_event uwbd_events[] = {
Stefano Panellac5995bd2008-11-04 14:06:31 +0000107 [UWB_RC_EVT_IE_RCV] = {
108 .handler = uwbd_evt_handle_rc_ie_rcv,
109 .name = "IE_RECEIVED"
110 },
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100111 [UWB_RC_EVT_BEACON] = {
112 .handler = uwbd_evt_handle_rc_beacon,
113 .name = "BEACON_RECEIVED"
114 },
115 [UWB_RC_EVT_BEACON_SIZE] = {
116 .handler = uwbd_evt_handle_rc_beacon_size,
117 .name = "BEACON_SIZE_CHANGE"
118 },
119 [UWB_RC_EVT_BPOIE_CHANGE] = {
120 .handler = uwbd_evt_handle_rc_bpoie_change,
121 .name = "BPOIE_CHANGE"
122 },
123 [UWB_RC_EVT_BP_SLOT_CHANGE] = {
124 .handler = uwbd_evt_handle_rc_bp_slot_change,
125 .name = "BP_SLOT_CHANGE"
126 },
127 [UWB_RC_EVT_DRP_AVAIL] = {
128 .handler = uwbd_evt_handle_rc_drp_avail,
129 .name = "DRP_AVAILABILITY_CHANGE"
130 },
131 [UWB_RC_EVT_DRP] = {
132 .handler = uwbd_evt_handle_rc_drp,
133 .name = "DRP"
134 },
135 [UWB_RC_EVT_DEV_ADDR_CONFLICT] = {
136 .handler = uwbd_evt_handle_rc_dev_addr_conflict,
137 .name = "DEV_ADDR_CONFLICT",
138 },
139};
140
141
142
143struct uwbd_evt_type_handler {
144 const char *name;
145 struct uwbd_event *uwbd_events;
146 size_t size;
147};
148
149#define UWBD_EVT_TYPE_HANDLER(n,a) { \
150 .name = (n), \
151 .uwbd_events = (a), \
152 .size = sizeof(a)/sizeof((a)[0]) \
153}
154
155
156/** Table of handlers for each UWBD Event type. */
157static
158struct uwbd_evt_type_handler uwbd_evt_type_handlers[] = {
159 [UWB_RC_CET_GENERAL] = UWBD_EVT_TYPE_HANDLER("RC", uwbd_events)
160};
161
162static const
163size_t uwbd_evt_type_handlers_len =
164 sizeof(uwbd_evt_type_handlers) / sizeof(uwbd_evt_type_handlers[0]);
165
166static const struct uwbd_event uwbd_message_handlers[] = {
167 [UWB_EVT_MSG_RESET] = {
168 .handler = uwbd_msg_handle_reset,
169 .name = "reset",
170 },
171};
172
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100173/**
174 * Handle an URC event passed to the UWB Daemon
175 *
176 * @evt: the event to handle
177 * @returns: 0 if the event can be kfreed, !0 on the contrary
178 * (somebody else took ownership) [coincidentally, returning
179 * a <0 errno code will free it :)].
180 *
181 * Looks up the two indirection tables (one for the type, one for the
182 * subtype) to decide which function handles it and then calls the
183 * handler.
184 *
185 * The event structure passed to the event handler has the radio
186 * controller in @evt->rc referenced. The reference will be dropped
187 * once the handler returns, so if it needs it for longer (async),
188 * it'll need to take another one.
189 */
190static
191int uwbd_event_handle_urc(struct uwb_event *evt)
192{
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100193 struct uwbd_evt_type_handler *type_table;
194 uwbd_evt_handler_f handler;
195 u8 type, context;
196 u16 event;
197
198 type = evt->notif.rceb->bEventType;
199 event = le16_to_cpu(evt->notif.rceb->wEvent);
200 context = evt->notif.rceb->bEventContext;
201
202 if (type > uwbd_evt_type_handlers_len) {
David Vrabel8092d7c2008-10-16 13:56:53 +0100203 printk(KERN_ERR "UWBD: event type %u: unknown (too high)\n", type);
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100204 return -EINVAL;
205 }
206 type_table = &uwbd_evt_type_handlers[type];
207 if (type_table->uwbd_events == NULL) {
David Vrabel8092d7c2008-10-16 13:56:53 +0100208 printk(KERN_ERR "UWBD: event type %u: unknown\n", type);
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100209 return -EINVAL;
210 }
211 if (event > type_table->size) {
David Vrabel8092d7c2008-10-16 13:56:53 +0100212 printk(KERN_ERR "UWBD: event %s[%u]: unknown (too high)\n",
213 type_table->name, event);
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100214 return -EINVAL;
215 }
216 handler = type_table->uwbd_events[event].handler;
217 if (handler == NULL) {
David Vrabel8092d7c2008-10-16 13:56:53 +0100218 printk(KERN_ERR "UWBD: event %s[%u]: unknown\n", type_table->name, event);
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100219 return -EINVAL;
220 }
David Vrabel8092d7c2008-10-16 13:56:53 +0100221 return (*handler)(evt);
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100222}
223
224static void uwbd_event_handle_message(struct uwb_event *evt)
225{
226 struct uwb_rc *rc;
227 int result;
228
229 rc = evt->rc;
230
231 if (evt->message < 0 || evt->message >= ARRAY_SIZE(uwbd_message_handlers)) {
232 dev_err(&rc->uwb_dev.dev, "UWBD: invalid message type %d\n", evt->message);
233 return;
234 }
235
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100236 result = uwbd_message_handlers[evt->message].handler(evt);
237 if (result < 0)
238 dev_err(&rc->uwb_dev.dev, "UWBD: '%s' message failed: %d\n",
239 uwbd_message_handlers[evt->message].name, result);
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100240}
241
242static void uwbd_event_handle(struct uwb_event *evt)
243{
244 struct uwb_rc *rc;
245 int should_keep;
246
247 rc = evt->rc;
248
249 if (rc->ready) {
250 switch (evt->type) {
251 case UWB_EVT_TYPE_NOTIF:
252 should_keep = uwbd_event_handle_urc(evt);
253 if (should_keep <= 0)
254 kfree(evt->notif.rceb);
255 break;
256 case UWB_EVT_TYPE_MSG:
257 uwbd_event_handle_message(evt);
258 break;
259 default:
260 dev_err(&rc->uwb_dev.dev, "UWBD: invalid event type %d\n", evt->type);
261 break;
262 }
263 }
264
265 __uwb_rc_put(rc); /* for the __uwb_rc_get() in uwb_rc_notif_cb() */
266}
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100267
268/**
269 * UWB Daemon
270 *
271 * Listens to all UWB notifications and takes care to track the state
272 * of the UWB neighboorhood for the kernel. When we do a run, we
273 * spinlock, move the list to a private copy and release the
274 * lock. Hold it as little as possible. Not a conflict: it is
275 * guaranteed we own the events in the private list.
276 *
277 * FIXME: should change so we don't have a 1HZ timer all the time, but
278 * only if there are devices.
279 */
Stefano Panellafec1a592008-11-04 15:39:08 +0000280static int uwbd(void *param)
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100281{
Stefano Panellafec1a592008-11-04 15:39:08 +0000282 struct uwb_rc *rc = param;
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100283 unsigned long flags;
Stefano Panellafec1a592008-11-04 15:39:08 +0000284 struct uwb_event *evt;
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100285 int should_stop = 0;
Stefano Panellafec1a592008-11-04 15:39:08 +0000286
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100287 while (1) {
288 wait_event_interruptible_timeout(
Stefano Panellafec1a592008-11-04 15:39:08 +0000289 rc->uwbd.wq,
290 !list_empty(&rc->uwbd.event_list)
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100291 || (should_stop = kthread_should_stop()),
292 HZ);
293 if (should_stop)
294 break;
295 try_to_freeze();
296
Stefano Panellafec1a592008-11-04 15:39:08 +0000297 spin_lock_irqsave(&rc->uwbd.event_list_lock, flags);
298 if (!list_empty(&rc->uwbd.event_list)) {
299 evt = list_first_entry(&rc->uwbd.event_list, struct uwb_event, list_node);
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100300 list_del(&evt->list_node);
Stefano Panellafec1a592008-11-04 15:39:08 +0000301 } else
302 evt = NULL;
303 spin_unlock_irqrestore(&rc->uwbd.event_list_lock, flags);
304
305 if (evt) {
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100306 uwbd_event_handle(evt);
307 kfree(evt);
308 }
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100309
Stefano Panellafec1a592008-11-04 15:39:08 +0000310 uwb_beca_purge(rc); /* Purge devices that left */
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100311 }
312 return 0;
313}
314
315
316/** Start the UWB daemon */
Stefano Panellafec1a592008-11-04 15:39:08 +0000317void uwbd_start(struct uwb_rc *rc)
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100318{
Stefano Panellafec1a592008-11-04 15:39:08 +0000319 rc->uwbd.task = kthread_run(uwbd, rc, "uwbd");
320 if (rc->uwbd.task == NULL)
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100321 printk(KERN_ERR "UWB: Cannot start management daemon; "
322 "UWB won't work\n");
323 else
Stefano Panellafec1a592008-11-04 15:39:08 +0000324 rc->uwbd.pid = rc->uwbd.task->pid;
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100325}
326
327/* Stop the UWB daemon and free any unprocessed events */
Stefano Panellafec1a592008-11-04 15:39:08 +0000328void uwbd_stop(struct uwb_rc *rc)
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100329{
Stefano Panellafec1a592008-11-04 15:39:08 +0000330 kthread_stop(rc->uwbd.task);
331 uwbd_flush(rc);
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100332}
333
334/*
335 * Queue an event for the management daemon
336 *
337 * When some lower layer receives an event, it uses this function to
338 * push it forward to the UWB daemon.
339 *
340 * Once you pass the event, you don't own it any more, but the daemon
341 * does. It will uwb_event_free() it when done, so make sure you
342 * uwb_event_alloc()ed it or bad things will happen.
343 *
344 * If the daemon is not running, we just free the event.
345 */
346void uwbd_event_queue(struct uwb_event *evt)
347{
Stefano Panellafec1a592008-11-04 15:39:08 +0000348 struct uwb_rc *rc = evt->rc;
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100349 unsigned long flags;
Stefano Panellafec1a592008-11-04 15:39:08 +0000350
351 spin_lock_irqsave(&rc->uwbd.event_list_lock, flags);
352 if (rc->uwbd.pid != 0) {
353 list_add(&evt->list_node, &rc->uwbd.event_list);
354 wake_up_all(&rc->uwbd.wq);
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100355 } else {
356 __uwb_rc_put(evt->rc);
357 if (evt->type == UWB_EVT_TYPE_NOTIF)
358 kfree(evt->notif.rceb);
359 kfree(evt);
360 }
Stefano Panellafec1a592008-11-04 15:39:08 +0000361 spin_unlock_irqrestore(&rc->uwbd.event_list_lock, flags);
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100362 return;
363}
364
365void uwbd_flush(struct uwb_rc *rc)
366{
367 struct uwb_event *evt, *nxt;
368
Stefano Panellafec1a592008-11-04 15:39:08 +0000369 spin_lock_irq(&rc->uwbd.event_list_lock);
370 list_for_each_entry_safe(evt, nxt, &rc->uwbd.event_list, list_node) {
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100371 if (evt->rc == rc) {
372 __uwb_rc_put(rc);
373 list_del(&evt->list_node);
374 if (evt->type == UWB_EVT_TYPE_NOTIF)
375 kfree(evt->notif.rceb);
376 kfree(evt);
377 }
378 }
Stefano Panellafec1a592008-11-04 15:39:08 +0000379 spin_unlock_irq(&rc->uwbd.event_list_lock);
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100380}