uwb: add whc-rc radio control driver
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / uwb / uwbd.c
blobb3673d614adbd4efa02884b4559c104f205414d6
1 /*
2 * Ultra Wide Band
3 * Neighborhood Management Daemon
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
20 * 02110-1301, USA.
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.
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.
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).
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().
44 * EVENTS
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_*'.
50 * EVENT HANDLER TABLES
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'.
57 * DEVICES
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).
66 * Devices are considered disconnected when a certain number of
67 * beacons are not received in an amount of time.
69 * Handler functions are called normally uwbd_evt_handle_*().
72 #include <linux/kthread.h>
73 #include <linux/module.h>
74 #include <linux/freezer.h>
75 #include "uwb-internal.h"
77 #define D_LOCAL 1
78 #include <linux/uwb/debug.h>
81 /**
82 * UWBD Event handler function signature
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.
88 * @evt->rc is already referenced and guaranteed to exist. See
89 * uwb_evt_handle().
91 typedef int (*uwbd_evt_handler_f)(struct uwb_event *);
93 /**
94 * Properties of a UWBD event
96 * @handler: the function that will handle this event
97 * @name: text name of event
99 struct uwbd_event {
100 uwbd_evt_handler_f handler;
101 const char *name;
104 /** Table of handlers for and properties of the UWBD Radio Control Events */
105 static
106 struct uwbd_event uwbd_events[] = {
107 [UWB_RC_EVT_BEACON] = {
108 .handler = uwbd_evt_handle_rc_beacon,
109 .name = "BEACON_RECEIVED"
111 [UWB_RC_EVT_BEACON_SIZE] = {
112 .handler = uwbd_evt_handle_rc_beacon_size,
113 .name = "BEACON_SIZE_CHANGE"
115 [UWB_RC_EVT_BPOIE_CHANGE] = {
116 .handler = uwbd_evt_handle_rc_bpoie_change,
117 .name = "BPOIE_CHANGE"
119 [UWB_RC_EVT_BP_SLOT_CHANGE] = {
120 .handler = uwbd_evt_handle_rc_bp_slot_change,
121 .name = "BP_SLOT_CHANGE"
123 [UWB_RC_EVT_DRP_AVAIL] = {
124 .handler = uwbd_evt_handle_rc_drp_avail,
125 .name = "DRP_AVAILABILITY_CHANGE"
127 [UWB_RC_EVT_DRP] = {
128 .handler = uwbd_evt_handle_rc_drp,
129 .name = "DRP"
131 [UWB_RC_EVT_DEV_ADDR_CONFLICT] = {
132 .handler = uwbd_evt_handle_rc_dev_addr_conflict,
133 .name = "DEV_ADDR_CONFLICT",
139 struct uwbd_evt_type_handler {
140 const char *name;
141 struct uwbd_event *uwbd_events;
142 size_t size;
145 #define UWBD_EVT_TYPE_HANDLER(n,a) { \
146 .name = (n), \
147 .uwbd_events = (a), \
148 .size = sizeof(a)/sizeof((a)[0]) \
152 /** Table of handlers for each UWBD Event type. */
153 static
154 struct uwbd_evt_type_handler uwbd_evt_type_handlers[] = {
155 [UWB_RC_CET_GENERAL] = UWBD_EVT_TYPE_HANDLER("RC", uwbd_events)
158 static const
159 size_t uwbd_evt_type_handlers_len =
160 sizeof(uwbd_evt_type_handlers) / sizeof(uwbd_evt_type_handlers[0]);
162 static const struct uwbd_event uwbd_message_handlers[] = {
163 [UWB_EVT_MSG_RESET] = {
164 .handler = uwbd_msg_handle_reset,
165 .name = "reset",
169 static DEFINE_MUTEX(uwbd_event_mutex);
172 * Handle an URC event passed to the UWB Daemon
174 * @evt: the event to handle
175 * @returns: 0 if the event can be kfreed, !0 on the contrary
176 * (somebody else took ownership) [coincidentally, returning
177 * a <0 errno code will free it :)].
179 * Looks up the two indirection tables (one for the type, one for the
180 * subtype) to decide which function handles it and then calls the
181 * handler.
183 * The event structure passed to the event handler has the radio
184 * controller in @evt->rc referenced. The reference will be dropped
185 * once the handler returns, so if it needs it for longer (async),
186 * it'll need to take another one.
188 static
189 int uwbd_event_handle_urc(struct uwb_event *evt)
191 int result;
192 struct uwbd_evt_type_handler *type_table;
193 uwbd_evt_handler_f handler;
194 u8 type, context;
195 u16 event;
197 type = evt->notif.rceb->bEventType;
198 event = le16_to_cpu(evt->notif.rceb->wEvent);
199 context = evt->notif.rceb->bEventContext;
201 if (type > uwbd_evt_type_handlers_len) {
202 if (printk_ratelimit())
203 printk(KERN_ERR "UWBD: event type %u: unknown "
204 "(too high)\n", type);
205 return -EINVAL;
207 type_table = &uwbd_evt_type_handlers[type];
208 if (type_table->uwbd_events == NULL) {
209 if (printk_ratelimit())
210 printk(KERN_ERR "UWBD: event type %u: unknown\n", type);
211 return -EINVAL;
213 if (event > type_table->size) {
214 if (printk_ratelimit())
215 printk(KERN_ERR "UWBD: event %s[%u]: "
216 "unknown (too high)\n", type_table->name, event);
217 return -EINVAL;
219 handler = type_table->uwbd_events[event].handler;
220 if (handler == NULL) {
221 if (printk_ratelimit())
222 printk(KERN_ERR "UWBD: event %s[%u]: unknown\n",
223 type_table->name, event);
224 return -EINVAL;
226 d_printf(3, NULL, "processing 0x%02x/%04x/%02x, %zu bytes\n",
227 type, event, context, evt->notif.size);
228 result = (*handler)(evt);
229 if (result < 0) {
230 if (printk_ratelimit())
231 printk(KERN_ERR "UWBD: event 0x%02x/%04x/%02x, "
232 "table %s[%u]: handling failed: %d\n",
233 type, event, context, type_table->name,
234 event, result);
236 return result;
239 static void uwbd_event_handle_message(struct uwb_event *evt)
241 struct uwb_rc *rc;
242 int result;
244 rc = evt->rc;
246 if (evt->message < 0 || evt->message >= ARRAY_SIZE(uwbd_message_handlers)) {
247 dev_err(&rc->uwb_dev.dev, "UWBD: invalid message type %d\n", evt->message);
248 return;
251 /* If this is a reset event we need to drop the
252 * uwbd_event_mutex or it deadlocks when the reset handler
253 * attempts to flush the uwbd events. */
254 if (evt->message == UWB_EVT_MSG_RESET)
255 mutex_unlock(&uwbd_event_mutex);
257 result = uwbd_message_handlers[evt->message].handler(evt);
258 if (result < 0)
259 dev_err(&rc->uwb_dev.dev, "UWBD: '%s' message failed: %d\n",
260 uwbd_message_handlers[evt->message].name, result);
262 if (evt->message == UWB_EVT_MSG_RESET)
263 mutex_lock(&uwbd_event_mutex);
266 static void uwbd_event_handle(struct uwb_event *evt)
268 struct uwb_rc *rc;
269 int should_keep;
271 rc = evt->rc;
273 if (rc->ready) {
274 switch (evt->type) {
275 case UWB_EVT_TYPE_NOTIF:
276 should_keep = uwbd_event_handle_urc(evt);
277 if (should_keep <= 0)
278 kfree(evt->notif.rceb);
279 break;
280 case UWB_EVT_TYPE_MSG:
281 uwbd_event_handle_message(evt);
282 break;
283 default:
284 dev_err(&rc->uwb_dev.dev, "UWBD: invalid event type %d\n", evt->type);
285 break;
289 __uwb_rc_put(rc); /* for the __uwb_rc_get() in uwb_rc_notif_cb() */
291 /* The UWB Daemon */
294 /** Daemon's PID: used to decide if we can queue or not */
295 static int uwbd_pid;
296 /** Daemon's task struct for managing the kthread */
297 static struct task_struct *uwbd_task;
298 /** Daemon's waitqueue for waiting for new events */
299 static DECLARE_WAIT_QUEUE_HEAD(uwbd_wq);
300 /** Daemon's list of events; we queue/dequeue here */
301 static struct list_head uwbd_event_list = LIST_HEAD_INIT(uwbd_event_list);
302 /** Daemon's list lock to protect concurent access */
303 static DEFINE_SPINLOCK(uwbd_event_list_lock);
307 * UWB Daemon
309 * Listens to all UWB notifications and takes care to track the state
310 * of the UWB neighboorhood for the kernel. When we do a run, we
311 * spinlock, move the list to a private copy and release the
312 * lock. Hold it as little as possible. Not a conflict: it is
313 * guaranteed we own the events in the private list.
315 * FIXME: should change so we don't have a 1HZ timer all the time, but
316 * only if there are devices.
318 static int uwbd(void *unused)
320 unsigned long flags;
321 struct list_head list = LIST_HEAD_INIT(list);
322 struct uwb_event *evt, *nxt;
323 int should_stop = 0;
324 while (1) {
325 wait_event_interruptible_timeout(
326 uwbd_wq,
327 !list_empty(&uwbd_event_list)
328 || (should_stop = kthread_should_stop()),
329 HZ);
330 if (should_stop)
331 break;
332 try_to_freeze();
334 mutex_lock(&uwbd_event_mutex);
335 spin_lock_irqsave(&uwbd_event_list_lock, flags);
336 list_splice_init(&uwbd_event_list, &list);
337 spin_unlock_irqrestore(&uwbd_event_list_lock, flags);
338 list_for_each_entry_safe(evt, nxt, &list, list_node) {
339 list_del(&evt->list_node);
340 uwbd_event_handle(evt);
341 kfree(evt);
343 mutex_unlock(&uwbd_event_mutex);
345 uwb_beca_purge(); /* Purge devices that left */
347 return 0;
351 /** Start the UWB daemon */
352 void uwbd_start(void)
354 uwbd_task = kthread_run(uwbd, NULL, "uwbd");
355 if (uwbd_task == NULL)
356 printk(KERN_ERR "UWB: Cannot start management daemon; "
357 "UWB won't work\n");
358 else
359 uwbd_pid = uwbd_task->pid;
362 /* Stop the UWB daemon and free any unprocessed events */
363 void uwbd_stop(void)
365 unsigned long flags;
366 struct uwb_event *evt, *nxt;
367 kthread_stop(uwbd_task);
368 spin_lock_irqsave(&uwbd_event_list_lock, flags);
369 uwbd_pid = 0;
370 list_for_each_entry_safe(evt, nxt, &uwbd_event_list, list_node) {
371 if (evt->type == UWB_EVT_TYPE_NOTIF)
372 kfree(evt->notif.rceb);
373 kfree(evt);
375 spin_unlock_irqrestore(&uwbd_event_list_lock, flags);
376 uwb_beca_release();
380 * Queue an event for the management daemon
382 * When some lower layer receives an event, it uses this function to
383 * push it forward to the UWB daemon.
385 * Once you pass the event, you don't own it any more, but the daemon
386 * does. It will uwb_event_free() it when done, so make sure you
387 * uwb_event_alloc()ed it or bad things will happen.
389 * If the daemon is not running, we just free the event.
391 void uwbd_event_queue(struct uwb_event *evt)
393 unsigned long flags;
394 spin_lock_irqsave(&uwbd_event_list_lock, flags);
395 if (uwbd_pid != 0) {
396 list_add(&evt->list_node, &uwbd_event_list);
397 wake_up_all(&uwbd_wq);
398 } else {
399 __uwb_rc_put(evt->rc);
400 if (evt->type == UWB_EVT_TYPE_NOTIF)
401 kfree(evt->notif.rceb);
402 kfree(evt);
404 spin_unlock_irqrestore(&uwbd_event_list_lock, flags);
405 return;
408 void uwbd_flush(struct uwb_rc *rc)
410 struct uwb_event *evt, *nxt;
412 mutex_lock(&uwbd_event_mutex);
414 spin_lock_irq(&uwbd_event_list_lock);
415 list_for_each_entry_safe(evt, nxt, &uwbd_event_list, list_node) {
416 if (evt->rc == rc) {
417 __uwb_rc_put(rc);
418 list_del(&evt->list_node);
419 if (evt->type == UWB_EVT_TYPE_NOTIF)
420 kfree(evt->notif.rceb);
421 kfree(evt);
424 spin_unlock_irq(&uwbd_event_list_lock);
426 mutex_unlock(&uwbd_event_mutex);