]> Pileus Git - ~andy/linux/blob - drivers/uwb/uwbd.c
uwb: infrastructure for handling Relinquish Request IEs
[~andy/linux] / drivers / uwb / uwbd.c
1 /*
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  */
91 typedef 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  */
99 struct 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 */
105 static
106 struct uwbd_event uwbd_events[] = {
107         [UWB_RC_EVT_IE_RCV] = {
108                 .handler = uwbd_evt_handle_rc_ie_rcv,
109                 .name = "IE_RECEIVED"
110         },
111         [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
143 struct 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. */
157 static
158 struct uwbd_evt_type_handler uwbd_evt_type_handlers[] = {
159         [UWB_RC_CET_GENERAL] = UWBD_EVT_TYPE_HANDLER("RC", uwbd_events)
160 };
161
162 static const
163 size_t uwbd_evt_type_handlers_len =
164         sizeof(uwbd_evt_type_handlers) / sizeof(uwbd_evt_type_handlers[0]);
165
166 static const struct uwbd_event uwbd_message_handlers[] = {
167         [UWB_EVT_MSG_RESET] = {
168                 .handler = uwbd_msg_handle_reset,
169                 .name = "reset",
170         },
171 };
172
173 static DEFINE_MUTEX(uwbd_event_mutex);
174
175 /**
176  * Handle an URC event passed to the UWB Daemon
177  *
178  * @evt: the event to handle
179  * @returns: 0 if the event can be kfreed, !0 on the contrary
180  *           (somebody else took ownership) [coincidentally, returning
181  *           a <0 errno code will free it :)].
182  *
183  * Looks up the two indirection tables (one for the type, one for the
184  * subtype) to decide which function handles it and then calls the
185  * handler.
186  *
187  * The event structure passed to the event handler has the radio
188  * controller in @evt->rc referenced. The reference will be dropped
189  * once the handler returns, so if it needs it for longer (async),
190  * it'll need to take another one.
191  */
192 static
193 int uwbd_event_handle_urc(struct uwb_event *evt)
194 {
195         struct uwbd_evt_type_handler *type_table;
196         uwbd_evt_handler_f handler;
197         u8 type, context;
198         u16 event;
199
200         type = evt->notif.rceb->bEventType;
201         event = le16_to_cpu(evt->notif.rceb->wEvent);
202         context = evt->notif.rceb->bEventContext;
203
204         if (type > uwbd_evt_type_handlers_len) {
205                 printk(KERN_ERR "UWBD: event type %u: unknown (too high)\n", type);
206                 return -EINVAL;
207         }
208         type_table = &uwbd_evt_type_handlers[type];
209         if (type_table->uwbd_events == NULL) {
210                 printk(KERN_ERR "UWBD: event type %u: unknown\n", type);
211                 return -EINVAL;
212         }
213         if (event > type_table->size) {
214                 printk(KERN_ERR "UWBD: event %s[%u]: unknown (too high)\n",
215                        type_table->name, event);
216                 return -EINVAL;
217         }
218         handler = type_table->uwbd_events[event].handler;
219         if (handler == NULL) {
220                 printk(KERN_ERR "UWBD: event %s[%u]: unknown\n", type_table->name, event);
221                 return -EINVAL;
222         }
223         return (*handler)(evt);
224 }
225
226 static void uwbd_event_handle_message(struct uwb_event *evt)
227 {
228         struct uwb_rc *rc;
229         int result;
230
231         rc = evt->rc;
232
233         if (evt->message < 0 || evt->message >= ARRAY_SIZE(uwbd_message_handlers)) {
234                 dev_err(&rc->uwb_dev.dev, "UWBD: invalid message type %d\n", evt->message);
235                 return;
236         }
237
238         /* If this is a reset event we need to drop the
239          * uwbd_event_mutex or it deadlocks when the reset handler
240          * attempts to flush the uwbd events. */
241         if (evt->message == UWB_EVT_MSG_RESET)
242                 mutex_unlock(&uwbd_event_mutex);
243
244         result = uwbd_message_handlers[evt->message].handler(evt);
245         if (result < 0)
246                 dev_err(&rc->uwb_dev.dev, "UWBD: '%s' message failed: %d\n",
247                         uwbd_message_handlers[evt->message].name, result);
248
249         if (evt->message == UWB_EVT_MSG_RESET)
250                 mutex_lock(&uwbd_event_mutex);
251 }
252
253 static void uwbd_event_handle(struct uwb_event *evt)
254 {
255         struct uwb_rc *rc;
256         int should_keep;
257
258         rc = evt->rc;
259
260         if (rc->ready) {
261                 switch (evt->type) {
262                 case UWB_EVT_TYPE_NOTIF:
263                         should_keep = uwbd_event_handle_urc(evt);
264                         if (should_keep <= 0)
265                                 kfree(evt->notif.rceb);
266                         break;
267                 case UWB_EVT_TYPE_MSG:
268                         uwbd_event_handle_message(evt);
269                         break;
270                 default:
271                         dev_err(&rc->uwb_dev.dev, "UWBD: invalid event type %d\n", evt->type);
272                         break;
273                 }
274         }
275
276         __uwb_rc_put(rc);       /* for the __uwb_rc_get() in uwb_rc_notif_cb() */
277 }
278 /* The UWB Daemon */
279
280
281 /** Daemon's PID: used to decide if we can queue or not */
282 static int uwbd_pid;
283 /** Daemon's task struct for managing the kthread */
284 static struct task_struct *uwbd_task;
285 /** Daemon's waitqueue for waiting for new events */
286 static DECLARE_WAIT_QUEUE_HEAD(uwbd_wq);
287 /** Daemon's list of events; we queue/dequeue here */
288 static struct list_head uwbd_event_list = LIST_HEAD_INIT(uwbd_event_list);
289 /** Daemon's list lock to protect concurent access */
290 static DEFINE_SPINLOCK(uwbd_event_list_lock);
291
292
293 /**
294  * UWB Daemon
295  *
296  * Listens to all UWB notifications and takes care to track the state
297  * of the UWB neighboorhood for the kernel. When we do a run, we
298  * spinlock, move the list to a private copy and release the
299  * lock. Hold it as little as possible. Not a conflict: it is
300  * guaranteed we own the events in the private list.
301  *
302  * FIXME: should change so we don't have a 1HZ timer all the time, but
303  *        only if there are devices.
304  */
305 static int uwbd(void *unused)
306 {
307         unsigned long flags;
308         struct list_head list = LIST_HEAD_INIT(list);
309         struct uwb_event *evt, *nxt;
310         int should_stop = 0;
311         while (1) {
312                 wait_event_interruptible_timeout(
313                         uwbd_wq,
314                         !list_empty(&uwbd_event_list)
315                           || (should_stop = kthread_should_stop()),
316                         HZ);
317                 if (should_stop)
318                         break;
319                 try_to_freeze();
320
321                 mutex_lock(&uwbd_event_mutex);
322                 spin_lock_irqsave(&uwbd_event_list_lock, flags);
323                 list_splice_init(&uwbd_event_list, &list);
324                 spin_unlock_irqrestore(&uwbd_event_list_lock, flags);
325                 list_for_each_entry_safe(evt, nxt, &list, list_node) {
326                         list_del(&evt->list_node);
327                         uwbd_event_handle(evt);
328                         kfree(evt);
329                 }
330                 mutex_unlock(&uwbd_event_mutex);
331
332                 uwb_beca_purge();       /* Purge devices that left */
333         }
334         return 0;
335 }
336
337
338 /** Start the UWB daemon */
339 void uwbd_start(void)
340 {
341         uwbd_task = kthread_run(uwbd, NULL, "uwbd");
342         if (uwbd_task == NULL)
343                 printk(KERN_ERR "UWB: Cannot start management daemon; "
344                        "UWB won't work\n");
345         else
346                 uwbd_pid = uwbd_task->pid;
347 }
348
349 /* Stop the UWB daemon and free any unprocessed events */
350 void uwbd_stop(void)
351 {
352         unsigned long flags;
353         struct uwb_event *evt, *nxt;
354         kthread_stop(uwbd_task);
355         spin_lock_irqsave(&uwbd_event_list_lock, flags);
356         uwbd_pid = 0;
357         list_for_each_entry_safe(evt, nxt, &uwbd_event_list, list_node) {
358                 if (evt->type == UWB_EVT_TYPE_NOTIF)
359                         kfree(evt->notif.rceb);
360                 kfree(evt);
361         }
362         spin_unlock_irqrestore(&uwbd_event_list_lock, flags);
363         uwb_beca_release();
364 }
365
366 /*
367  * Queue an event for the management daemon
368  *
369  * When some lower layer receives an event, it uses this function to
370  * push it forward to the UWB daemon.
371  *
372  * Once you pass the event, you don't own it any more, but the daemon
373  * does. It will uwb_event_free() it when done, so make sure you
374  * uwb_event_alloc()ed it or bad things will happen.
375  *
376  * If the daemon is not running, we just free the event.
377  */
378 void uwbd_event_queue(struct uwb_event *evt)
379 {
380         unsigned long flags;
381         spin_lock_irqsave(&uwbd_event_list_lock, flags);
382         if (uwbd_pid != 0) {
383                 list_add(&evt->list_node, &uwbd_event_list);
384                 wake_up_all(&uwbd_wq);
385         } else {
386                 __uwb_rc_put(evt->rc);
387                 if (evt->type == UWB_EVT_TYPE_NOTIF)
388                         kfree(evt->notif.rceb);
389                 kfree(evt);
390         }
391         spin_unlock_irqrestore(&uwbd_event_list_lock, flags);
392         return;
393 }
394
395 void uwbd_flush(struct uwb_rc *rc)
396 {
397         struct uwb_event *evt, *nxt;
398
399         mutex_lock(&uwbd_event_mutex);
400
401         spin_lock_irq(&uwbd_event_list_lock);
402         list_for_each_entry_safe(evt, nxt, &uwbd_event_list, list_node) {
403                 if (evt->rc == rc) {
404                         __uwb_rc_put(rc);
405                         list_del(&evt->list_node);
406                         if (evt->type == UWB_EVT_TYPE_NOTIF)
407                                 kfree(evt->notif.rceb);
408                         kfree(evt);
409                 }
410         }
411         spin_unlock_irq(&uwbd_event_list_lock);
412
413         mutex_unlock(&uwbd_event_mutex);
414 }