Inaky Perez-Gonzalez | 183b9b5 | 2008-09-17 16:34:06 +0100 | [diff] [blame] | 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 | * |
Daniel Mack | 1537a36 | 2010-01-29 15:57:49 +0800 | [diff] [blame] | 46 | * Events have a type, a subtype, a length, some other stuff and the |
Inaky Perez-Gonzalez | 183b9b5 | 2008-09-17 16:34:06 +0100 | [diff] [blame] | 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-Gonzalez | 183b9b5 | 2008-09-17 16:34:06 +0100 | [diff] [blame] | 71 | #include <linux/kthread.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 72 | #include <linux/slab.h> |
Inaky Perez-Gonzalez | 183b9b5 | 2008-09-17 16:34:06 +0100 | [diff] [blame] | 73 | #include <linux/module.h> |
| 74 | #include <linux/freezer.h> |
David Vrabel | bce8369 | 2008-12-22 18:22:50 +0000 | [diff] [blame] | 75 | |
Inaky Perez-Gonzalez | 183b9b5 | 2008-09-17 16:34:06 +0100 | [diff] [blame] | 76 | #include "uwb-internal.h" |
| 77 | |
David Vrabel | bce8369 | 2008-12-22 18:22:50 +0000 | [diff] [blame] | 78 | /* |
Inaky Perez-Gonzalez | 183b9b5 | 2008-09-17 16:34:06 +0100 | [diff] [blame] | 79 | * UWBD Event handler function signature |
| 80 | * |
| 81 | * Return !0 if the event needs not to be freed (ie the handler |
| 82 | * takes/took care of it). 0 means the daemon code will free the |
| 83 | * event. |
| 84 | * |
| 85 | * @evt->rc is already referenced and guaranteed to exist. See |
| 86 | * uwb_evt_handle(). |
| 87 | */ |
| 88 | typedef int (*uwbd_evt_handler_f)(struct uwb_event *); |
| 89 | |
| 90 | /** |
| 91 | * Properties of a UWBD event |
| 92 | * |
| 93 | * @handler: the function that will handle this event |
| 94 | * @name: text name of event |
| 95 | */ |
| 96 | struct uwbd_event { |
| 97 | uwbd_evt_handler_f handler; |
| 98 | const char *name; |
| 99 | }; |
| 100 | |
David Vrabel | bce8369 | 2008-12-22 18:22:50 +0000 | [diff] [blame] | 101 | /* Table of handlers for and properties of the UWBD Radio Control Events */ |
| 102 | static struct uwbd_event uwbd_urc_events[] = { |
Stefano Panella | c5995bd | 2008-11-04 14:06:31 +0000 | [diff] [blame] | 103 | [UWB_RC_EVT_IE_RCV] = { |
| 104 | .handler = uwbd_evt_handle_rc_ie_rcv, |
| 105 | .name = "IE_RECEIVED" |
| 106 | }, |
Inaky Perez-Gonzalez | 183b9b5 | 2008-09-17 16:34:06 +0100 | [diff] [blame] | 107 | [UWB_RC_EVT_BEACON] = { |
| 108 | .handler = uwbd_evt_handle_rc_beacon, |
| 109 | .name = "BEACON_RECEIVED" |
| 110 | }, |
| 111 | [UWB_RC_EVT_BEACON_SIZE] = { |
| 112 | .handler = uwbd_evt_handle_rc_beacon_size, |
| 113 | .name = "BEACON_SIZE_CHANGE" |
| 114 | }, |
| 115 | [UWB_RC_EVT_BPOIE_CHANGE] = { |
| 116 | .handler = uwbd_evt_handle_rc_bpoie_change, |
| 117 | .name = "BPOIE_CHANGE" |
| 118 | }, |
| 119 | [UWB_RC_EVT_BP_SLOT_CHANGE] = { |
| 120 | .handler = uwbd_evt_handle_rc_bp_slot_change, |
| 121 | .name = "BP_SLOT_CHANGE" |
| 122 | }, |
| 123 | [UWB_RC_EVT_DRP_AVAIL] = { |
| 124 | .handler = uwbd_evt_handle_rc_drp_avail, |
| 125 | .name = "DRP_AVAILABILITY_CHANGE" |
| 126 | }, |
| 127 | [UWB_RC_EVT_DRP] = { |
| 128 | .handler = uwbd_evt_handle_rc_drp, |
| 129 | .name = "DRP" |
| 130 | }, |
| 131 | [UWB_RC_EVT_DEV_ADDR_CONFLICT] = { |
| 132 | .handler = uwbd_evt_handle_rc_dev_addr_conflict, |
| 133 | .name = "DEV_ADDR_CONFLICT", |
| 134 | }, |
| 135 | }; |
| 136 | |
| 137 | |
| 138 | |
| 139 | struct uwbd_evt_type_handler { |
| 140 | const char *name; |
| 141 | struct uwbd_event *uwbd_events; |
| 142 | size_t size; |
| 143 | }; |
| 144 | |
David Vrabel | bce8369 | 2008-12-22 18:22:50 +0000 | [diff] [blame] | 145 | /* Table of handlers for each UWBD Event type. */ |
| 146 | static struct uwbd_evt_type_handler uwbd_urc_evt_type_handlers[] = { |
| 147 | [UWB_RC_CET_GENERAL] = { |
| 148 | .name = "URC", |
| 149 | .uwbd_events = uwbd_urc_events, |
| 150 | .size = ARRAY_SIZE(uwbd_urc_events), |
| 151 | }, |
Inaky Perez-Gonzalez | 183b9b5 | 2008-09-17 16:34:06 +0100 | [diff] [blame] | 152 | }; |
| 153 | |
Inaky Perez-Gonzalez | 183b9b5 | 2008-09-17 16:34:06 +0100 | [diff] [blame] | 154 | static const struct uwbd_event uwbd_message_handlers[] = { |
| 155 | [UWB_EVT_MSG_RESET] = { |
| 156 | .handler = uwbd_msg_handle_reset, |
| 157 | .name = "reset", |
| 158 | }, |
| 159 | }; |
| 160 | |
David Vrabel | bce8369 | 2008-12-22 18:22:50 +0000 | [diff] [blame] | 161 | /* |
Inaky Perez-Gonzalez | 183b9b5 | 2008-09-17 16:34:06 +0100 | [diff] [blame] | 162 | * Handle an URC event passed to the UWB Daemon |
| 163 | * |
| 164 | * @evt: the event to handle |
| 165 | * @returns: 0 if the event can be kfreed, !0 on the contrary |
| 166 | * (somebody else took ownership) [coincidentally, returning |
| 167 | * a <0 errno code will free it :)]. |
| 168 | * |
| 169 | * Looks up the two indirection tables (one for the type, one for the |
| 170 | * subtype) to decide which function handles it and then calls the |
| 171 | * handler. |
| 172 | * |
| 173 | * The event structure passed to the event handler has the radio |
| 174 | * controller in @evt->rc referenced. The reference will be dropped |
| 175 | * once the handler returns, so if it needs it for longer (async), |
| 176 | * it'll need to take another one. |
| 177 | */ |
| 178 | static |
| 179 | int uwbd_event_handle_urc(struct uwb_event *evt) |
| 180 | { |
David Vrabel | bce8369 | 2008-12-22 18:22:50 +0000 | [diff] [blame] | 181 | int result = -EINVAL; |
Inaky Perez-Gonzalez | 183b9b5 | 2008-09-17 16:34:06 +0100 | [diff] [blame] | 182 | struct uwbd_evt_type_handler *type_table; |
| 183 | uwbd_evt_handler_f handler; |
| 184 | u8 type, context; |
| 185 | u16 event; |
| 186 | |
| 187 | type = evt->notif.rceb->bEventType; |
| 188 | event = le16_to_cpu(evt->notif.rceb->wEvent); |
| 189 | context = evt->notif.rceb->bEventContext; |
| 190 | |
David Vrabel | 3fafdd7 | 2009-08-25 15:03:07 +0100 | [diff] [blame] | 191 | if (type >= ARRAY_SIZE(uwbd_urc_evt_type_handlers)) |
David Vrabel | bce8369 | 2008-12-22 18:22:50 +0000 | [diff] [blame] | 192 | goto out; |
| 193 | type_table = &uwbd_urc_evt_type_handlers[type]; |
| 194 | if (type_table->uwbd_events == NULL) |
| 195 | goto out; |
David Vrabel | 3fafdd7 | 2009-08-25 15:03:07 +0100 | [diff] [blame] | 196 | if (event >= type_table->size) |
David Vrabel | bce8369 | 2008-12-22 18:22:50 +0000 | [diff] [blame] | 197 | goto out; |
Inaky Perez-Gonzalez | 183b9b5 | 2008-09-17 16:34:06 +0100 | [diff] [blame] | 198 | handler = type_table->uwbd_events[event].handler; |
David Vrabel | bce8369 | 2008-12-22 18:22:50 +0000 | [diff] [blame] | 199 | if (handler == NULL) |
| 200 | goto out; |
| 201 | |
| 202 | result = (*handler)(evt); |
| 203 | out: |
| 204 | if (result < 0) |
| 205 | dev_err(&evt->rc->uwb_dev.dev, |
| 206 | "UWBD: event 0x%02x/%04x/%02x, handling failed: %d\n", |
| 207 | type, event, context, result); |
| 208 | return result; |
Inaky Perez-Gonzalez | 183b9b5 | 2008-09-17 16:34:06 +0100 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | static void uwbd_event_handle_message(struct uwb_event *evt) |
| 212 | { |
| 213 | struct uwb_rc *rc; |
| 214 | int result; |
| 215 | |
| 216 | rc = evt->rc; |
| 217 | |
| 218 | if (evt->message < 0 || evt->message >= ARRAY_SIZE(uwbd_message_handlers)) { |
| 219 | dev_err(&rc->uwb_dev.dev, "UWBD: invalid message type %d\n", evt->message); |
| 220 | return; |
| 221 | } |
| 222 | |
Inaky Perez-Gonzalez | 183b9b5 | 2008-09-17 16:34:06 +0100 | [diff] [blame] | 223 | result = uwbd_message_handlers[evt->message].handler(evt); |
| 224 | if (result < 0) |
| 225 | dev_err(&rc->uwb_dev.dev, "UWBD: '%s' message failed: %d\n", |
| 226 | uwbd_message_handlers[evt->message].name, result); |
Inaky Perez-Gonzalez | 183b9b5 | 2008-09-17 16:34:06 +0100 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | static void uwbd_event_handle(struct uwb_event *evt) |
| 230 | { |
| 231 | struct uwb_rc *rc; |
| 232 | int should_keep; |
| 233 | |
| 234 | rc = evt->rc; |
| 235 | |
| 236 | if (rc->ready) { |
| 237 | switch (evt->type) { |
| 238 | case UWB_EVT_TYPE_NOTIF: |
| 239 | should_keep = uwbd_event_handle_urc(evt); |
| 240 | if (should_keep <= 0) |
| 241 | kfree(evt->notif.rceb); |
| 242 | break; |
| 243 | case UWB_EVT_TYPE_MSG: |
| 244 | uwbd_event_handle_message(evt); |
| 245 | break; |
| 246 | default: |
| 247 | dev_err(&rc->uwb_dev.dev, "UWBD: invalid event type %d\n", evt->type); |
| 248 | break; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | __uwb_rc_put(rc); /* for the __uwb_rc_get() in uwb_rc_notif_cb() */ |
| 253 | } |
Inaky Perez-Gonzalez | 183b9b5 | 2008-09-17 16:34:06 +0100 | [diff] [blame] | 254 | |
| 255 | /** |
| 256 | * UWB Daemon |
| 257 | * |
| 258 | * Listens to all UWB notifications and takes care to track the state |
Jesper Juhl | 06b72d0 | 2011-07-17 21:15:54 +0200 | [diff] [blame] | 259 | * of the UWB neighbourhood for the kernel. When we do a run, we |
Inaky Perez-Gonzalez | 183b9b5 | 2008-09-17 16:34:06 +0100 | [diff] [blame] | 260 | * spinlock, move the list to a private copy and release the |
| 261 | * lock. Hold it as little as possible. Not a conflict: it is |
| 262 | * guaranteed we own the events in the private list. |
| 263 | * |
| 264 | * FIXME: should change so we don't have a 1HZ timer all the time, but |
| 265 | * only if there are devices. |
| 266 | */ |
Stefano Panella | fec1a59 | 2008-11-04 15:39:08 +0000 | [diff] [blame] | 267 | static int uwbd(void *param) |
Inaky Perez-Gonzalez | 183b9b5 | 2008-09-17 16:34:06 +0100 | [diff] [blame] | 268 | { |
Stefano Panella | fec1a59 | 2008-11-04 15:39:08 +0000 | [diff] [blame] | 269 | struct uwb_rc *rc = param; |
Inaky Perez-Gonzalez | 183b9b5 | 2008-09-17 16:34:06 +0100 | [diff] [blame] | 270 | unsigned long flags; |
Stefano Panella | fec1a59 | 2008-11-04 15:39:08 +0000 | [diff] [blame] | 271 | struct uwb_event *evt; |
Inaky Perez-Gonzalez | 183b9b5 | 2008-09-17 16:34:06 +0100 | [diff] [blame] | 272 | int should_stop = 0; |
Stefano Panella | fec1a59 | 2008-11-04 15:39:08 +0000 | [diff] [blame] | 273 | |
Inaky Perez-Gonzalez | 183b9b5 | 2008-09-17 16:34:06 +0100 | [diff] [blame] | 274 | while (1) { |
| 275 | wait_event_interruptible_timeout( |
Stefano Panella | fec1a59 | 2008-11-04 15:39:08 +0000 | [diff] [blame] | 276 | rc->uwbd.wq, |
| 277 | !list_empty(&rc->uwbd.event_list) |
Inaky Perez-Gonzalez | 183b9b5 | 2008-09-17 16:34:06 +0100 | [diff] [blame] | 278 | || (should_stop = kthread_should_stop()), |
| 279 | HZ); |
| 280 | if (should_stop) |
| 281 | break; |
| 282 | try_to_freeze(); |
| 283 | |
Stefano Panella | fec1a59 | 2008-11-04 15:39:08 +0000 | [diff] [blame] | 284 | spin_lock_irqsave(&rc->uwbd.event_list_lock, flags); |
| 285 | if (!list_empty(&rc->uwbd.event_list)) { |
| 286 | evt = list_first_entry(&rc->uwbd.event_list, struct uwb_event, list_node); |
Inaky Perez-Gonzalez | 183b9b5 | 2008-09-17 16:34:06 +0100 | [diff] [blame] | 287 | list_del(&evt->list_node); |
Stefano Panella | fec1a59 | 2008-11-04 15:39:08 +0000 | [diff] [blame] | 288 | } else |
| 289 | evt = NULL; |
| 290 | spin_unlock_irqrestore(&rc->uwbd.event_list_lock, flags); |
| 291 | |
| 292 | if (evt) { |
Inaky Perez-Gonzalez | 183b9b5 | 2008-09-17 16:34:06 +0100 | [diff] [blame] | 293 | uwbd_event_handle(evt); |
| 294 | kfree(evt); |
| 295 | } |
Inaky Perez-Gonzalez | 183b9b5 | 2008-09-17 16:34:06 +0100 | [diff] [blame] | 296 | |
Stefano Panella | fec1a59 | 2008-11-04 15:39:08 +0000 | [diff] [blame] | 297 | uwb_beca_purge(rc); /* Purge devices that left */ |
Inaky Perez-Gonzalez | 183b9b5 | 2008-09-17 16:34:06 +0100 | [diff] [blame] | 298 | } |
| 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | |
| 303 | /** Start the UWB daemon */ |
Stefano Panella | fec1a59 | 2008-11-04 15:39:08 +0000 | [diff] [blame] | 304 | void uwbd_start(struct uwb_rc *rc) |
Inaky Perez-Gonzalez | 183b9b5 | 2008-09-17 16:34:06 +0100 | [diff] [blame] | 305 | { |
Stefano Panella | fec1a59 | 2008-11-04 15:39:08 +0000 | [diff] [blame] | 306 | rc->uwbd.task = kthread_run(uwbd, rc, "uwbd"); |
| 307 | if (rc->uwbd.task == NULL) |
Inaky Perez-Gonzalez | 183b9b5 | 2008-09-17 16:34:06 +0100 | [diff] [blame] | 308 | printk(KERN_ERR "UWB: Cannot start management daemon; " |
| 309 | "UWB won't work\n"); |
| 310 | else |
Stefano Panella | fec1a59 | 2008-11-04 15:39:08 +0000 | [diff] [blame] | 311 | rc->uwbd.pid = rc->uwbd.task->pid; |
Inaky Perez-Gonzalez | 183b9b5 | 2008-09-17 16:34:06 +0100 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | /* Stop the UWB daemon and free any unprocessed events */ |
Stefano Panella | fec1a59 | 2008-11-04 15:39:08 +0000 | [diff] [blame] | 315 | void uwbd_stop(struct uwb_rc *rc) |
Inaky Perez-Gonzalez | 183b9b5 | 2008-09-17 16:34:06 +0100 | [diff] [blame] | 316 | { |
Stefano Panella | fec1a59 | 2008-11-04 15:39:08 +0000 | [diff] [blame] | 317 | kthread_stop(rc->uwbd.task); |
| 318 | uwbd_flush(rc); |
Inaky Perez-Gonzalez | 183b9b5 | 2008-09-17 16:34:06 +0100 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | /* |
| 322 | * Queue an event for the management daemon |
| 323 | * |
| 324 | * When some lower layer receives an event, it uses this function to |
| 325 | * push it forward to the UWB daemon. |
| 326 | * |
| 327 | * Once you pass the event, you don't own it any more, but the daemon |
| 328 | * does. It will uwb_event_free() it when done, so make sure you |
| 329 | * uwb_event_alloc()ed it or bad things will happen. |
| 330 | * |
| 331 | * If the daemon is not running, we just free the event. |
| 332 | */ |
| 333 | void uwbd_event_queue(struct uwb_event *evt) |
| 334 | { |
Stefano Panella | fec1a59 | 2008-11-04 15:39:08 +0000 | [diff] [blame] | 335 | struct uwb_rc *rc = evt->rc; |
Inaky Perez-Gonzalez | 183b9b5 | 2008-09-17 16:34:06 +0100 | [diff] [blame] | 336 | unsigned long flags; |
Stefano Panella | fec1a59 | 2008-11-04 15:39:08 +0000 | [diff] [blame] | 337 | |
| 338 | spin_lock_irqsave(&rc->uwbd.event_list_lock, flags); |
| 339 | if (rc->uwbd.pid != 0) { |
| 340 | list_add(&evt->list_node, &rc->uwbd.event_list); |
| 341 | wake_up_all(&rc->uwbd.wq); |
Inaky Perez-Gonzalez | 183b9b5 | 2008-09-17 16:34:06 +0100 | [diff] [blame] | 342 | } else { |
| 343 | __uwb_rc_put(evt->rc); |
| 344 | if (evt->type == UWB_EVT_TYPE_NOTIF) |
| 345 | kfree(evt->notif.rceb); |
| 346 | kfree(evt); |
| 347 | } |
Stefano Panella | fec1a59 | 2008-11-04 15:39:08 +0000 | [diff] [blame] | 348 | spin_unlock_irqrestore(&rc->uwbd.event_list_lock, flags); |
Inaky Perez-Gonzalez | 183b9b5 | 2008-09-17 16:34:06 +0100 | [diff] [blame] | 349 | return; |
| 350 | } |
| 351 | |
| 352 | void uwbd_flush(struct uwb_rc *rc) |
| 353 | { |
| 354 | struct uwb_event *evt, *nxt; |
| 355 | |
Stefano Panella | fec1a59 | 2008-11-04 15:39:08 +0000 | [diff] [blame] | 356 | spin_lock_irq(&rc->uwbd.event_list_lock); |
| 357 | list_for_each_entry_safe(evt, nxt, &rc->uwbd.event_list, list_node) { |
Inaky Perez-Gonzalez | 183b9b5 | 2008-09-17 16:34:06 +0100 | [diff] [blame] | 358 | if (evt->rc == rc) { |
| 359 | __uwb_rc_put(rc); |
| 360 | list_del(&evt->list_node); |
| 361 | if (evt->type == UWB_EVT_TYPE_NOTIF) |
| 362 | kfree(evt->notif.rceb); |
| 363 | kfree(evt); |
| 364 | } |
| 365 | } |
Stefano Panella | fec1a59 | 2008-11-04 15:39:08 +0000 | [diff] [blame] | 366 | spin_unlock_irq(&rc->uwbd.event_list_lock); |
Inaky Perez-Gonzalez | 183b9b5 | 2008-09-17 16:34:06 +0100 | [diff] [blame] | 367 | } |