blob: 5a777d8624da3f791bc08c2228de6b256a6d3bdc [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 */
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +010071#include <linux/kthread.h>
72#include <linux/module.h>
73#include <linux/freezer.h>
David Vrabelbce83692008-12-22 18:22:50 +000074
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +010075#include "uwb-internal.h"
76
David Vrabelbce83692008-12-22 18:22:50 +000077/*
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +010078 * UWBD Event handler function signature
79 *
80 * Return !0 if the event needs not to be freed (ie the handler
81 * takes/took care of it). 0 means the daemon code will free the
82 * event.
83 *
84 * @evt->rc is already referenced and guaranteed to exist. See
85 * uwb_evt_handle().
86 */
87typedef int (*uwbd_evt_handler_f)(struct uwb_event *);
88
89/**
90 * Properties of a UWBD event
91 *
92 * @handler: the function that will handle this event
93 * @name: text name of event
94 */
95struct uwbd_event {
96 uwbd_evt_handler_f handler;
97 const char *name;
98};
99
David Vrabelbce83692008-12-22 18:22:50 +0000100/* Table of handlers for and properties of the UWBD Radio Control Events */
101static struct uwbd_event uwbd_urc_events[] = {
Stefano Panellac5995bd2008-11-04 14:06:31 +0000102 [UWB_RC_EVT_IE_RCV] = {
103 .handler = uwbd_evt_handle_rc_ie_rcv,
104 .name = "IE_RECEIVED"
105 },
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100106 [UWB_RC_EVT_BEACON] = {
107 .handler = uwbd_evt_handle_rc_beacon,
108 .name = "BEACON_RECEIVED"
109 },
110 [UWB_RC_EVT_BEACON_SIZE] = {
111 .handler = uwbd_evt_handle_rc_beacon_size,
112 .name = "BEACON_SIZE_CHANGE"
113 },
114 [UWB_RC_EVT_BPOIE_CHANGE] = {
115 .handler = uwbd_evt_handle_rc_bpoie_change,
116 .name = "BPOIE_CHANGE"
117 },
118 [UWB_RC_EVT_BP_SLOT_CHANGE] = {
119 .handler = uwbd_evt_handle_rc_bp_slot_change,
120 .name = "BP_SLOT_CHANGE"
121 },
122 [UWB_RC_EVT_DRP_AVAIL] = {
123 .handler = uwbd_evt_handle_rc_drp_avail,
124 .name = "DRP_AVAILABILITY_CHANGE"
125 },
126 [UWB_RC_EVT_DRP] = {
127 .handler = uwbd_evt_handle_rc_drp,
128 .name = "DRP"
129 },
130 [UWB_RC_EVT_DEV_ADDR_CONFLICT] = {
131 .handler = uwbd_evt_handle_rc_dev_addr_conflict,
132 .name = "DEV_ADDR_CONFLICT",
133 },
134};
135
136
137
138struct uwbd_evt_type_handler {
139 const char *name;
140 struct uwbd_event *uwbd_events;
141 size_t size;
142};
143
David Vrabelbce83692008-12-22 18:22:50 +0000144/* Table of handlers for each UWBD Event type. */
145static struct uwbd_evt_type_handler uwbd_urc_evt_type_handlers[] = {
146 [UWB_RC_CET_GENERAL] = {
147 .name = "URC",
148 .uwbd_events = uwbd_urc_events,
149 .size = ARRAY_SIZE(uwbd_urc_events),
150 },
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100151};
152
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100153static const struct uwbd_event uwbd_message_handlers[] = {
154 [UWB_EVT_MSG_RESET] = {
155 .handler = uwbd_msg_handle_reset,
156 .name = "reset",
157 },
158};
159
David Vrabelbce83692008-12-22 18:22:50 +0000160/*
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100161 * Handle an URC event passed to the UWB Daemon
162 *
163 * @evt: the event to handle
164 * @returns: 0 if the event can be kfreed, !0 on the contrary
165 * (somebody else took ownership) [coincidentally, returning
166 * a <0 errno code will free it :)].
167 *
168 * Looks up the two indirection tables (one for the type, one for the
169 * subtype) to decide which function handles it and then calls the
170 * handler.
171 *
172 * The event structure passed to the event handler has the radio
173 * controller in @evt->rc referenced. The reference will be dropped
174 * once the handler returns, so if it needs it for longer (async),
175 * it'll need to take another one.
176 */
177static
178int uwbd_event_handle_urc(struct uwb_event *evt)
179{
David Vrabelbce83692008-12-22 18:22:50 +0000180 int result = -EINVAL;
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100181 struct uwbd_evt_type_handler *type_table;
182 uwbd_evt_handler_f handler;
183 u8 type, context;
184 u16 event;
185
186 type = evt->notif.rceb->bEventType;
187 event = le16_to_cpu(evt->notif.rceb->wEvent);
188 context = evt->notif.rceb->bEventContext;
189
David Vrabel3fafdd72009-08-25 15:03:07 +0100190 if (type >= ARRAY_SIZE(uwbd_urc_evt_type_handlers))
David Vrabelbce83692008-12-22 18:22:50 +0000191 goto out;
192 type_table = &uwbd_urc_evt_type_handlers[type];
193 if (type_table->uwbd_events == NULL)
194 goto out;
David Vrabel3fafdd72009-08-25 15:03:07 +0100195 if (event >= type_table->size)
David Vrabelbce83692008-12-22 18:22:50 +0000196 goto out;
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100197 handler = type_table->uwbd_events[event].handler;
David Vrabelbce83692008-12-22 18:22:50 +0000198 if (handler == NULL)
199 goto out;
200
201 result = (*handler)(evt);
202out:
203 if (result < 0)
204 dev_err(&evt->rc->uwb_dev.dev,
205 "UWBD: event 0x%02x/%04x/%02x, handling failed: %d\n",
206 type, event, context, result);
207 return result;
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100208}
209
210static void uwbd_event_handle_message(struct uwb_event *evt)
211{
212 struct uwb_rc *rc;
213 int result;
214
215 rc = evt->rc;
216
217 if (evt->message < 0 || evt->message >= ARRAY_SIZE(uwbd_message_handlers)) {
218 dev_err(&rc->uwb_dev.dev, "UWBD: invalid message type %d\n", evt->message);
219 return;
220 }
221
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100222 result = uwbd_message_handlers[evt->message].handler(evt);
223 if (result < 0)
224 dev_err(&rc->uwb_dev.dev, "UWBD: '%s' message failed: %d\n",
225 uwbd_message_handlers[evt->message].name, result);
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100226}
227
228static void uwbd_event_handle(struct uwb_event *evt)
229{
230 struct uwb_rc *rc;
231 int should_keep;
232
233 rc = evt->rc;
234
235 if (rc->ready) {
236 switch (evt->type) {
237 case UWB_EVT_TYPE_NOTIF:
238 should_keep = uwbd_event_handle_urc(evt);
239 if (should_keep <= 0)
240 kfree(evt->notif.rceb);
241 break;
242 case UWB_EVT_TYPE_MSG:
243 uwbd_event_handle_message(evt);
244 break;
245 default:
246 dev_err(&rc->uwb_dev.dev, "UWBD: invalid event type %d\n", evt->type);
247 break;
248 }
249 }
250
251 __uwb_rc_put(rc); /* for the __uwb_rc_get() in uwb_rc_notif_cb() */
252}
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100253
254/**
255 * UWB Daemon
256 *
257 * Listens to all UWB notifications and takes care to track the state
258 * of the UWB neighboorhood for the kernel. When we do a run, we
259 * spinlock, move the list to a private copy and release the
260 * lock. Hold it as little as possible. Not a conflict: it is
261 * guaranteed we own the events in the private list.
262 *
263 * FIXME: should change so we don't have a 1HZ timer all the time, but
264 * only if there are devices.
265 */
Stefano Panellafec1a592008-11-04 15:39:08 +0000266static int uwbd(void *param)
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100267{
Stefano Panellafec1a592008-11-04 15:39:08 +0000268 struct uwb_rc *rc = param;
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100269 unsigned long flags;
Stefano Panellafec1a592008-11-04 15:39:08 +0000270 struct uwb_event *evt;
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100271 int should_stop = 0;
Stefano Panellafec1a592008-11-04 15:39:08 +0000272
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100273 while (1) {
274 wait_event_interruptible_timeout(
Stefano Panellafec1a592008-11-04 15:39:08 +0000275 rc->uwbd.wq,
276 !list_empty(&rc->uwbd.event_list)
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100277 || (should_stop = kthread_should_stop()),
278 HZ);
279 if (should_stop)
280 break;
281 try_to_freeze();
282
Stefano Panellafec1a592008-11-04 15:39:08 +0000283 spin_lock_irqsave(&rc->uwbd.event_list_lock, flags);
284 if (!list_empty(&rc->uwbd.event_list)) {
285 evt = list_first_entry(&rc->uwbd.event_list, struct uwb_event, list_node);
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100286 list_del(&evt->list_node);
Stefano Panellafec1a592008-11-04 15:39:08 +0000287 } else
288 evt = NULL;
289 spin_unlock_irqrestore(&rc->uwbd.event_list_lock, flags);
290
291 if (evt) {
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100292 uwbd_event_handle(evt);
293 kfree(evt);
294 }
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100295
Stefano Panellafec1a592008-11-04 15:39:08 +0000296 uwb_beca_purge(rc); /* Purge devices that left */
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100297 }
298 return 0;
299}
300
301
302/** Start the UWB daemon */
Stefano Panellafec1a592008-11-04 15:39:08 +0000303void uwbd_start(struct uwb_rc *rc)
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100304{
Stefano Panellafec1a592008-11-04 15:39:08 +0000305 rc->uwbd.task = kthread_run(uwbd, rc, "uwbd");
306 if (rc->uwbd.task == NULL)
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100307 printk(KERN_ERR "UWB: Cannot start management daemon; "
308 "UWB won't work\n");
309 else
Stefano Panellafec1a592008-11-04 15:39:08 +0000310 rc->uwbd.pid = rc->uwbd.task->pid;
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100311}
312
313/* Stop the UWB daemon and free any unprocessed events */
Stefano Panellafec1a592008-11-04 15:39:08 +0000314void uwbd_stop(struct uwb_rc *rc)
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100315{
Stefano Panellafec1a592008-11-04 15:39:08 +0000316 kthread_stop(rc->uwbd.task);
317 uwbd_flush(rc);
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100318}
319
320/*
321 * Queue an event for the management daemon
322 *
323 * When some lower layer receives an event, it uses this function to
324 * push it forward to the UWB daemon.
325 *
326 * Once you pass the event, you don't own it any more, but the daemon
327 * does. It will uwb_event_free() it when done, so make sure you
328 * uwb_event_alloc()ed it or bad things will happen.
329 *
330 * If the daemon is not running, we just free the event.
331 */
332void uwbd_event_queue(struct uwb_event *evt)
333{
Stefano Panellafec1a592008-11-04 15:39:08 +0000334 struct uwb_rc *rc = evt->rc;
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100335 unsigned long flags;
Stefano Panellafec1a592008-11-04 15:39:08 +0000336
337 spin_lock_irqsave(&rc->uwbd.event_list_lock, flags);
338 if (rc->uwbd.pid != 0) {
339 list_add(&evt->list_node, &rc->uwbd.event_list);
340 wake_up_all(&rc->uwbd.wq);
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100341 } else {
342 __uwb_rc_put(evt->rc);
343 if (evt->type == UWB_EVT_TYPE_NOTIF)
344 kfree(evt->notif.rceb);
345 kfree(evt);
346 }
Stefano Panellafec1a592008-11-04 15:39:08 +0000347 spin_unlock_irqrestore(&rc->uwbd.event_list_lock, flags);
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100348 return;
349}
350
351void uwbd_flush(struct uwb_rc *rc)
352{
353 struct uwb_event *evt, *nxt;
354
Stefano Panellafec1a592008-11-04 15:39:08 +0000355 spin_lock_irq(&rc->uwbd.event_list_lock);
356 list_for_each_entry_safe(evt, nxt, &rc->uwbd.event_list, list_node) {
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100357 if (evt->rc == rc) {
358 __uwb_rc_put(rc);
359 list_del(&evt->list_node);
360 if (evt->type == UWB_EVT_TYPE_NOTIF)
361 kfree(evt->notif.rceb);
362 kfree(evt);
363 }
364 }
Stefano Panellafec1a592008-11-04 15:39:08 +0000365 spin_unlock_irq(&rc->uwbd.event_list_lock);
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100366}