diff options
Diffstat (limited to 'drivers/uwb/uwbd.c')
-rw-r--r-- | drivers/uwb/uwbd.c | 176 |
1 files changed, 66 insertions, 110 deletions
diff --git a/drivers/uwb/uwbd.c b/drivers/uwb/uwbd.c index 78908416e42c..57bd6bfef37e 100644 --- a/drivers/uwb/uwbd.c +++ b/drivers/uwb/uwbd.c | |||
@@ -68,17 +68,13 @@ | |||
68 | * | 68 | * |
69 | * Handler functions are called normally uwbd_evt_handle_*(). | 69 | * Handler functions are called normally uwbd_evt_handle_*(). |
70 | */ | 70 | */ |
71 | |||
72 | #include <linux/kthread.h> | 71 | #include <linux/kthread.h> |
73 | #include <linux/module.h> | 72 | #include <linux/module.h> |
74 | #include <linux/freezer.h> | 73 | #include <linux/freezer.h> |
75 | #include "uwb-internal.h" | ||
76 | |||
77 | #define D_LOCAL 1 | ||
78 | #include <linux/uwb/debug.h> | ||
79 | 74 | ||
75 | #include "uwb-internal.h" | ||
80 | 76 | ||
81 | /** | 77 | /* |
82 | * UWBD Event handler function signature | 78 | * UWBD Event handler function signature |
83 | * | 79 | * |
84 | * Return !0 if the event needs not to be freed (ie the handler | 80 | * Return !0 if the event needs not to be freed (ie the handler |
@@ -101,9 +97,12 @@ struct uwbd_event { | |||
101 | const char *name; | 97 | const char *name; |
102 | }; | 98 | }; |
103 | 99 | ||
104 | /** Table of handlers for and properties of the UWBD Radio Control Events */ | 100 | /* Table of handlers for and properties of the UWBD Radio Control Events */ |
105 | static | 101 | static struct uwbd_event uwbd_urc_events[] = { |
106 | struct uwbd_event uwbd_events[] = { | 102 | [UWB_RC_EVT_IE_RCV] = { |
103 | .handler = uwbd_evt_handle_rc_ie_rcv, | ||
104 | .name = "IE_RECEIVED" | ||
105 | }, | ||
107 | [UWB_RC_EVT_BEACON] = { | 106 | [UWB_RC_EVT_BEACON] = { |
108 | .handler = uwbd_evt_handle_rc_beacon, | 107 | .handler = uwbd_evt_handle_rc_beacon, |
109 | .name = "BEACON_RECEIVED" | 108 | .name = "BEACON_RECEIVED" |
@@ -142,23 +141,15 @@ struct uwbd_evt_type_handler { | |||
142 | size_t size; | 141 | size_t size; |
143 | }; | 142 | }; |
144 | 143 | ||
145 | #define UWBD_EVT_TYPE_HANDLER(n,a) { \ | 144 | /* Table of handlers for each UWBD Event type. */ |
146 | .name = (n), \ | 145 | static struct uwbd_evt_type_handler uwbd_urc_evt_type_handlers[] = { |
147 | .uwbd_events = (a), \ | 146 | [UWB_RC_CET_GENERAL] = { |
148 | .size = sizeof(a)/sizeof((a)[0]) \ | 147 | .name = "URC", |
149 | } | 148 | .uwbd_events = uwbd_urc_events, |
150 | 149 | .size = ARRAY_SIZE(uwbd_urc_events), | |
151 | 150 | }, | |
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) | ||
156 | }; | 151 | }; |
157 | 152 | ||
158 | static const | ||
159 | size_t uwbd_evt_type_handlers_len = | ||
160 | sizeof(uwbd_evt_type_handlers) / sizeof(uwbd_evt_type_handlers[0]); | ||
161 | |||
162 | static const struct uwbd_event uwbd_message_handlers[] = { | 153 | static const struct uwbd_event uwbd_message_handlers[] = { |
163 | [UWB_EVT_MSG_RESET] = { | 154 | [UWB_EVT_MSG_RESET] = { |
164 | .handler = uwbd_msg_handle_reset, | 155 | .handler = uwbd_msg_handle_reset, |
@@ -166,9 +157,7 @@ static const struct uwbd_event uwbd_message_handlers[] = { | |||
166 | }, | 157 | }, |
167 | }; | 158 | }; |
168 | 159 | ||
169 | static DEFINE_MUTEX(uwbd_event_mutex); | 160 | /* |
170 | |||
171 | /** | ||
172 | * Handle an URC event passed to the UWB Daemon | 161 | * Handle an URC event passed to the UWB Daemon |
173 | * | 162 | * |
174 | * @evt: the event to handle | 163 | * @evt: the event to handle |
@@ -188,6 +177,7 @@ static DEFINE_MUTEX(uwbd_event_mutex); | |||
188 | static | 177 | static |
189 | int uwbd_event_handle_urc(struct uwb_event *evt) | 178 | int uwbd_event_handle_urc(struct uwb_event *evt) |
190 | { | 179 | { |
180 | int result = -EINVAL; | ||
191 | struct uwbd_evt_type_handler *type_table; | 181 | struct uwbd_evt_type_handler *type_table; |
192 | uwbd_evt_handler_f handler; | 182 | uwbd_evt_handler_f handler; |
193 | u8 type, context; | 183 | u8 type, context; |
@@ -197,26 +187,24 @@ int uwbd_event_handle_urc(struct uwb_event *evt) | |||
197 | event = le16_to_cpu(evt->notif.rceb->wEvent); | 187 | event = le16_to_cpu(evt->notif.rceb->wEvent); |
198 | context = evt->notif.rceb->bEventContext; | 188 | context = evt->notif.rceb->bEventContext; |
199 | 189 | ||
200 | if (type > uwbd_evt_type_handlers_len) { | 190 | if (type > ARRAY_SIZE(uwbd_urc_evt_type_handlers)) |
201 | printk(KERN_ERR "UWBD: event type %u: unknown (too high)\n", type); | 191 | goto out; |
202 | return -EINVAL; | 192 | type_table = &uwbd_urc_evt_type_handlers[type]; |
203 | } | 193 | if (type_table->uwbd_events == NULL) |
204 | type_table = &uwbd_evt_type_handlers[type]; | 194 | goto out; |
205 | if (type_table->uwbd_events == NULL) { | 195 | if (event > type_table->size) |
206 | printk(KERN_ERR "UWBD: event type %u: unknown\n", type); | 196 | goto out; |
207 | return -EINVAL; | ||
208 | } | ||
209 | if (event > type_table->size) { | ||
210 | printk(KERN_ERR "UWBD: event %s[%u]: unknown (too high)\n", | ||
211 | type_table->name, event); | ||
212 | return -EINVAL; | ||
213 | } | ||
214 | handler = type_table->uwbd_events[event].handler; | 197 | handler = type_table->uwbd_events[event].handler; |
215 | if (handler == NULL) { | 198 | if (handler == NULL) |
216 | printk(KERN_ERR "UWBD: event %s[%u]: unknown\n", type_table->name, event); | 199 | goto out; |
217 | return -EINVAL; | 200 | |
218 | } | 201 | result = (*handler)(evt); |
219 | return (*handler)(evt); | 202 | out: |
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; | ||
220 | } | 208 | } |
221 | 209 | ||
222 | static void uwbd_event_handle_message(struct uwb_event *evt) | 210 | static void uwbd_event_handle_message(struct uwb_event *evt) |
@@ -231,19 +219,10 @@ static void uwbd_event_handle_message(struct uwb_event *evt) | |||
231 | return; | 219 | return; |
232 | } | 220 | } |
233 | 221 | ||
234 | /* If this is a reset event we need to drop the | ||
235 | * uwbd_event_mutex or it deadlocks when the reset handler | ||
236 | * attempts to flush the uwbd events. */ | ||
237 | if (evt->message == UWB_EVT_MSG_RESET) | ||
238 | mutex_unlock(&uwbd_event_mutex); | ||
239 | |||
240 | result = uwbd_message_handlers[evt->message].handler(evt); | 222 | result = uwbd_message_handlers[evt->message].handler(evt); |
241 | if (result < 0) | 223 | if (result < 0) |
242 | dev_err(&rc->uwb_dev.dev, "UWBD: '%s' message failed: %d\n", | 224 | dev_err(&rc->uwb_dev.dev, "UWBD: '%s' message failed: %d\n", |
243 | uwbd_message_handlers[evt->message].name, result); | 225 | uwbd_message_handlers[evt->message].name, result); |
244 | |||
245 | if (evt->message == UWB_EVT_MSG_RESET) | ||
246 | mutex_lock(&uwbd_event_mutex); | ||
247 | } | 226 | } |
248 | 227 | ||
249 | static void uwbd_event_handle(struct uwb_event *evt) | 228 | static void uwbd_event_handle(struct uwb_event *evt) |
@@ -271,20 +250,6 @@ static void uwbd_event_handle(struct uwb_event *evt) | |||
271 | 250 | ||
272 | __uwb_rc_put(rc); /* for the __uwb_rc_get() in uwb_rc_notif_cb() */ | 251 | __uwb_rc_put(rc); /* for the __uwb_rc_get() in uwb_rc_notif_cb() */ |
273 | } | 252 | } |
274 | /* The UWB Daemon */ | ||
275 | |||
276 | |||
277 | /** Daemon's PID: used to decide if we can queue or not */ | ||
278 | static int uwbd_pid; | ||
279 | /** Daemon's task struct for managing the kthread */ | ||
280 | static struct task_struct *uwbd_task; | ||
281 | /** Daemon's waitqueue for waiting for new events */ | ||
282 | static DECLARE_WAIT_QUEUE_HEAD(uwbd_wq); | ||
283 | /** Daemon's list of events; we queue/dequeue here */ | ||
284 | static struct list_head uwbd_event_list = LIST_HEAD_INIT(uwbd_event_list); | ||
285 | /** Daemon's list lock to protect concurent access */ | ||
286 | static DEFINE_SPINLOCK(uwbd_event_list_lock); | ||
287 | |||
288 | 253 | ||
289 | /** | 254 | /** |
290 | * UWB Daemon | 255 | * UWB Daemon |
@@ -298,65 +263,58 @@ static DEFINE_SPINLOCK(uwbd_event_list_lock); | |||
298 | * FIXME: should change so we don't have a 1HZ timer all the time, but | 263 | * FIXME: should change so we don't have a 1HZ timer all the time, but |
299 | * only if there are devices. | 264 | * only if there are devices. |
300 | */ | 265 | */ |
301 | static int uwbd(void *unused) | 266 | static int uwbd(void *param) |
302 | { | 267 | { |
268 | struct uwb_rc *rc = param; | ||
303 | unsigned long flags; | 269 | unsigned long flags; |
304 | struct list_head list = LIST_HEAD_INIT(list); | 270 | struct uwb_event *evt; |
305 | struct uwb_event *evt, *nxt; | ||
306 | int should_stop = 0; | 271 | int should_stop = 0; |
272 | |||
307 | while (1) { | 273 | while (1) { |
308 | wait_event_interruptible_timeout( | 274 | wait_event_interruptible_timeout( |
309 | uwbd_wq, | 275 | rc->uwbd.wq, |
310 | !list_empty(&uwbd_event_list) | 276 | !list_empty(&rc->uwbd.event_list) |
311 | || (should_stop = kthread_should_stop()), | 277 | || (should_stop = kthread_should_stop()), |
312 | HZ); | 278 | HZ); |
313 | if (should_stop) | 279 | if (should_stop) |
314 | break; | 280 | break; |
315 | try_to_freeze(); | 281 | try_to_freeze(); |
316 | 282 | ||
317 | mutex_lock(&uwbd_event_mutex); | 283 | spin_lock_irqsave(&rc->uwbd.event_list_lock, flags); |
318 | spin_lock_irqsave(&uwbd_event_list_lock, flags); | 284 | if (!list_empty(&rc->uwbd.event_list)) { |
319 | list_splice_init(&uwbd_event_list, &list); | 285 | evt = list_first_entry(&rc->uwbd.event_list, struct uwb_event, list_node); |
320 | spin_unlock_irqrestore(&uwbd_event_list_lock, flags); | ||
321 | list_for_each_entry_safe(evt, nxt, &list, list_node) { | ||
322 | list_del(&evt->list_node); | 286 | list_del(&evt->list_node); |
287 | } else | ||
288 | evt = NULL; | ||
289 | spin_unlock_irqrestore(&rc->uwbd.event_list_lock, flags); | ||
290 | |||
291 | if (evt) { | ||
323 | uwbd_event_handle(evt); | 292 | uwbd_event_handle(evt); |
324 | kfree(evt); | 293 | kfree(evt); |
325 | } | 294 | } |
326 | mutex_unlock(&uwbd_event_mutex); | ||
327 | 295 | ||
328 | uwb_beca_purge(); /* Purge devices that left */ | 296 | uwb_beca_purge(rc); /* Purge devices that left */ |
329 | } | 297 | } |
330 | return 0; | 298 | return 0; |
331 | } | 299 | } |
332 | 300 | ||
333 | 301 | ||
334 | /** Start the UWB daemon */ | 302 | /** Start the UWB daemon */ |
335 | void uwbd_start(void) | 303 | void uwbd_start(struct uwb_rc *rc) |
336 | { | 304 | { |
337 | uwbd_task = kthread_run(uwbd, NULL, "uwbd"); | 305 | rc->uwbd.task = kthread_run(uwbd, rc, "uwbd"); |
338 | if (uwbd_task == NULL) | 306 | if (rc->uwbd.task == NULL) |
339 | printk(KERN_ERR "UWB: Cannot start management daemon; " | 307 | printk(KERN_ERR "UWB: Cannot start management daemon; " |
340 | "UWB won't work\n"); | 308 | "UWB won't work\n"); |
341 | else | 309 | else |
342 | uwbd_pid = uwbd_task->pid; | 310 | rc->uwbd.pid = rc->uwbd.task->pid; |
343 | } | 311 | } |
344 | 312 | ||
345 | /* Stop the UWB daemon and free any unprocessed events */ | 313 | /* Stop the UWB daemon and free any unprocessed events */ |
346 | void uwbd_stop(void) | 314 | void uwbd_stop(struct uwb_rc *rc) |
347 | { | 315 | { |
348 | unsigned long flags; | 316 | kthread_stop(rc->uwbd.task); |
349 | struct uwb_event *evt, *nxt; | 317 | uwbd_flush(rc); |
350 | kthread_stop(uwbd_task); | ||
351 | spin_lock_irqsave(&uwbd_event_list_lock, flags); | ||
352 | uwbd_pid = 0; | ||
353 | list_for_each_entry_safe(evt, nxt, &uwbd_event_list, list_node) { | ||
354 | if (evt->type == UWB_EVT_TYPE_NOTIF) | ||
355 | kfree(evt->notif.rceb); | ||
356 | kfree(evt); | ||
357 | } | ||
358 | spin_unlock_irqrestore(&uwbd_event_list_lock, flags); | ||
359 | uwb_beca_release(); | ||
360 | } | 318 | } |
361 | 319 | ||
362 | /* | 320 | /* |
@@ -373,18 +331,20 @@ void uwbd_stop(void) | |||
373 | */ | 331 | */ |
374 | void uwbd_event_queue(struct uwb_event *evt) | 332 | void uwbd_event_queue(struct uwb_event *evt) |
375 | { | 333 | { |
334 | struct uwb_rc *rc = evt->rc; | ||
376 | unsigned long flags; | 335 | unsigned long flags; |
377 | spin_lock_irqsave(&uwbd_event_list_lock, flags); | 336 | |
378 | if (uwbd_pid != 0) { | 337 | spin_lock_irqsave(&rc->uwbd.event_list_lock, flags); |
379 | list_add(&evt->list_node, &uwbd_event_list); | 338 | if (rc->uwbd.pid != 0) { |
380 | wake_up_all(&uwbd_wq); | 339 | list_add(&evt->list_node, &rc->uwbd.event_list); |
340 | wake_up_all(&rc->uwbd.wq); | ||
381 | } else { | 341 | } else { |
382 | __uwb_rc_put(evt->rc); | 342 | __uwb_rc_put(evt->rc); |
383 | if (evt->type == UWB_EVT_TYPE_NOTIF) | 343 | if (evt->type == UWB_EVT_TYPE_NOTIF) |
384 | kfree(evt->notif.rceb); | 344 | kfree(evt->notif.rceb); |
385 | kfree(evt); | 345 | kfree(evt); |
386 | } | 346 | } |
387 | spin_unlock_irqrestore(&uwbd_event_list_lock, flags); | 347 | spin_unlock_irqrestore(&rc->uwbd.event_list_lock, flags); |
388 | return; | 348 | return; |
389 | } | 349 | } |
390 | 350 | ||
@@ -392,10 +352,8 @@ void uwbd_flush(struct uwb_rc *rc) | |||
392 | { | 352 | { |
393 | struct uwb_event *evt, *nxt; | 353 | struct uwb_event *evt, *nxt; |
394 | 354 | ||
395 | mutex_lock(&uwbd_event_mutex); | 355 | spin_lock_irq(&rc->uwbd.event_list_lock); |
396 | 356 | list_for_each_entry_safe(evt, nxt, &rc->uwbd.event_list, list_node) { | |
397 | spin_lock_irq(&uwbd_event_list_lock); | ||
398 | list_for_each_entry_safe(evt, nxt, &uwbd_event_list, list_node) { | ||
399 | if (evt->rc == rc) { | 357 | if (evt->rc == rc) { |
400 | __uwb_rc_put(rc); | 358 | __uwb_rc_put(rc); |
401 | list_del(&evt->list_node); | 359 | list_del(&evt->list_node); |
@@ -404,7 +362,5 @@ void uwbd_flush(struct uwb_rc *rc) | |||
404 | kfree(evt); | 362 | kfree(evt); |
405 | } | 363 | } |
406 | } | 364 | } |
407 | spin_unlock_irq(&uwbd_event_list_lock); | 365 | spin_unlock_irq(&rc->uwbd.event_list_lock); |
408 | |||
409 | mutex_unlock(&uwbd_event_mutex); | ||
410 | } | 366 | } |