PM/Hibernate: Fix memory shrinking
[linux-2.6/verdex.git] / drivers / uwb / neh.c
blob0af8916d9bef44c01c9f5746ea2293e356030e5e
1 /*
2 * WUSB Wire Adapter: Radio Control Interface (WUSB[8])
3 * Notification and Event Handling
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 * The RC interface of the Host Wire Adapter (USB dongle) or WHCI PCI
24 * card delivers a stream of notifications and events to the
25 * notification end event endpoint or area. This code takes care of
26 * getting a buffer with that data, breaking it up in separate
27 * notifications and events and then deliver those.
29 * Events are answers to commands and they carry a context ID that
30 * associates them to the command. Notifications are that,
31 * notifications, they come out of the blue and have a context ID of
32 * zero. Think of the context ID kind of like a handler. The
33 * uwb_rc_neh_* code deals with managing context IDs.
35 * This is why you require a handle to operate on a UWB host. When you
36 * open a handle a context ID is assigned to you.
38 * So, as it is done is:
40 * 1. Add an event handler [uwb_rc_neh_add()] (assigns a ctx id)
41 * 2. Issue command [rc->cmd(rc, ...)]
42 * 3. Arm the timeout timer [uwb_rc_neh_arm()]
43 * 4, Release the reference to the neh [uwb_rc_neh_put()]
44 * 5. Wait for the callback
45 * 6. Command result (RCEB) is passed to the callback
47 * If (2) fails, you should remove the handle [uwb_rc_neh_rm()]
48 * instead of arming the timer.
50 * Handles are for using in *serialized* code, single thread.
52 * When the notification/event comes, the IRQ handler/endpoint
53 * callback passes the data read to uwb_rc_neh_grok() which will break
54 * it up in a discrete series of events, look up who is listening for
55 * them and execute the pertinent callbacks.
57 * If the reader detects an error while reading the data stream, call
58 * uwb_rc_neh_error().
60 * CONSTRAINTS/ASSUMPTIONS:
62 * - Most notifications/events are small (less thank .5k), copying
63 * around is ok.
65 * - Notifications/events are ALWAYS smaller than PAGE_SIZE
67 * - Notifications/events always come in a single piece (ie: a buffer
68 * will always contain entire notifications/events).
70 * - we cannot know in advance how long each event is (because they
71 * lack a length field in their header--smart move by the standards
72 * body, btw). So we need a facility to get the event size given the
73 * header. This is what the EST code does (notif/Event Size
74 * Tables), check nest.c--as well, you can associate the size to
75 * the handle [w/ neh->extra_size()].
77 * - Most notifications/events are fixed size; only a few are variable
78 * size (NEST takes care of that).
80 * - Listeners of events expect them, so they usually provide a
81 * buffer, as they know the size. Listeners to notifications don't,
82 * so we allocate their buffers dynamically.
84 #include <linux/kernel.h>
85 #include <linux/timer.h>
86 #include <linux/err.h>
88 #include "uwb-internal.h"
91 * UWB Radio Controller Notification/Event Handle
93 * Represents an entity waiting for an event coming from the UWB Radio
94 * Controller with a given context id (context) and type (evt_type and
95 * evt). On reception of the notification/event, the callback (cb) is
96 * called with the event.
98 * If the timer expires before the event is received, the callback is
99 * called with -ETIMEDOUT as the event size.
101 struct uwb_rc_neh {
102 struct kref kref;
104 struct uwb_rc *rc;
105 u8 evt_type;
106 __le16 evt;
107 u8 context;
108 uwb_rc_cmd_cb_f cb;
109 void *arg;
111 struct timer_list timer;
112 struct list_head list_node;
115 static void uwb_rc_neh_timer(unsigned long arg);
117 static void uwb_rc_neh_release(struct kref *kref)
119 struct uwb_rc_neh *neh = container_of(kref, struct uwb_rc_neh, kref);
121 kfree(neh);
124 static void uwb_rc_neh_get(struct uwb_rc_neh *neh)
126 kref_get(&neh->kref);
130 * uwb_rc_neh_put - release reference to a neh
131 * @neh: the neh
133 void uwb_rc_neh_put(struct uwb_rc_neh *neh)
135 kref_put(&neh->kref, uwb_rc_neh_release);
140 * Assigns @neh a context id from @rc's pool
142 * @rc: UWB Radio Controller descriptor; @rc->neh_lock taken
143 * @neh: Notification/Event Handle
144 * @returns 0 if context id was assigned ok; < 0 errno on error (if
145 * all the context IDs are taken).
147 * (assumes @wa is locked).
149 * NOTE: WUSB spec reserves context ids 0x00 for notifications and
150 * 0xff is invalid, so they must not be used. Initialization
151 * fills up those two in the bitmap so they are not allocated.
153 * We spread the allocation around to reduce the posiblity of two
154 * consecutive opened @neh's getting the same context ID assigned (to
155 * avoid surprises with late events that timed out long time ago). So
156 * first we search from where @rc->ctx_roll is, if not found, we
157 * search from zero.
159 static
160 int __uwb_rc_ctx_get(struct uwb_rc *rc, struct uwb_rc_neh *neh)
162 int result;
163 result = find_next_zero_bit(rc->ctx_bm, UWB_RC_CTX_MAX,
164 rc->ctx_roll++);
165 if (result < UWB_RC_CTX_MAX)
166 goto found;
167 result = find_first_zero_bit(rc->ctx_bm, UWB_RC_CTX_MAX);
168 if (result < UWB_RC_CTX_MAX)
169 goto found;
170 return -ENFILE;
171 found:
172 set_bit(result, rc->ctx_bm);
173 neh->context = result;
174 return 0;
178 /** Releases @neh's context ID back to @rc (@rc->neh_lock is locked). */
179 static
180 void __uwb_rc_ctx_put(struct uwb_rc *rc, struct uwb_rc_neh *neh)
182 struct device *dev = &rc->uwb_dev.dev;
183 if (neh->context == 0)
184 return;
185 if (test_bit(neh->context, rc->ctx_bm) == 0) {
186 dev_err(dev, "context %u not set in bitmap\n",
187 neh->context);
188 WARN_ON(1);
190 clear_bit(neh->context, rc->ctx_bm);
191 neh->context = 0;
195 * uwb_rc_neh_add - add a neh for a radio controller command
196 * @rc: the radio controller
197 * @cmd: the radio controller command
198 * @expected_type: the type of the expected response event
199 * @expected_event: the expected event ID
200 * @cb: callback for when the event is received
201 * @arg: argument for the callback
203 * Creates a neh and adds it to the list of those waiting for an
204 * event. A context ID will be assigned to the command.
206 struct uwb_rc_neh *uwb_rc_neh_add(struct uwb_rc *rc, struct uwb_rccb *cmd,
207 u8 expected_type, u16 expected_event,
208 uwb_rc_cmd_cb_f cb, void *arg)
210 int result;
211 unsigned long flags;
212 struct device *dev = &rc->uwb_dev.dev;
213 struct uwb_rc_neh *neh;
215 neh = kzalloc(sizeof(*neh), GFP_KERNEL);
216 if (neh == NULL) {
217 result = -ENOMEM;
218 goto error_kzalloc;
221 kref_init(&neh->kref);
222 INIT_LIST_HEAD(&neh->list_node);
223 init_timer(&neh->timer);
224 neh->timer.function = uwb_rc_neh_timer;
225 neh->timer.data = (unsigned long)neh;
227 neh->rc = rc;
228 neh->evt_type = expected_type;
229 neh->evt = cpu_to_le16(expected_event);
230 neh->cb = cb;
231 neh->arg = arg;
233 spin_lock_irqsave(&rc->neh_lock, flags);
234 result = __uwb_rc_ctx_get(rc, neh);
235 if (result >= 0) {
236 cmd->bCommandContext = neh->context;
237 list_add_tail(&neh->list_node, &rc->neh_list);
238 uwb_rc_neh_get(neh);
240 spin_unlock_irqrestore(&rc->neh_lock, flags);
241 if (result < 0)
242 goto error_ctx_get;
244 return neh;
246 error_ctx_get:
247 kfree(neh);
248 error_kzalloc:
249 dev_err(dev, "cannot open handle to radio controller: %d\n", result);
250 return ERR_PTR(result);
253 static void __uwb_rc_neh_rm(struct uwb_rc *rc, struct uwb_rc_neh *neh)
255 __uwb_rc_ctx_put(rc, neh);
256 list_del(&neh->list_node);
260 * uwb_rc_neh_rm - remove a neh.
261 * @rc: the radio controller
262 * @neh: the neh to remove
264 * Remove an active neh immediately instead of waiting for the event
265 * (or a time out).
267 void uwb_rc_neh_rm(struct uwb_rc *rc, struct uwb_rc_neh *neh)
269 unsigned long flags;
271 spin_lock_irqsave(&rc->neh_lock, flags);
272 __uwb_rc_neh_rm(rc, neh);
273 spin_unlock_irqrestore(&rc->neh_lock, flags);
275 del_timer_sync(&neh->timer);
276 uwb_rc_neh_put(neh);
280 * uwb_rc_neh_arm - arm an event handler timeout timer
282 * @rc: UWB Radio Controller
283 * @neh: Notification/event handler for @rc
285 * The timer is only armed if the neh is active.
287 void uwb_rc_neh_arm(struct uwb_rc *rc, struct uwb_rc_neh *neh)
289 unsigned long flags;
291 spin_lock_irqsave(&rc->neh_lock, flags);
292 if (neh->context)
293 mod_timer(&neh->timer,
294 jiffies + msecs_to_jiffies(UWB_RC_CMD_TIMEOUT_MS));
295 spin_unlock_irqrestore(&rc->neh_lock, flags);
298 static void uwb_rc_neh_cb(struct uwb_rc_neh *neh, struct uwb_rceb *rceb, size_t size)
300 (*neh->cb)(neh->rc, neh->arg, rceb, size);
301 uwb_rc_neh_put(neh);
304 static bool uwb_rc_neh_match(struct uwb_rc_neh *neh, const struct uwb_rceb *rceb)
306 return neh->evt_type == rceb->bEventType
307 && neh->evt == rceb->wEvent
308 && neh->context == rceb->bEventContext;
312 * Find the handle waiting for a RC Radio Control Event
314 * @rc: UWB Radio Controller
315 * @rceb: Pointer to the RCEB buffer
316 * @event_size: Pointer to the size of the RCEB buffer. Might be
317 * adjusted to take into account the @neh->extra_size
318 * settings.
320 * If the listener has no buffer (NULL buffer), one is allocated for
321 * the right size (the amount of data received). @neh->ptr will point
322 * to the event payload, which always starts with a 'struct
323 * uwb_rceb'. kfree() it when done.
325 static
326 struct uwb_rc_neh *uwb_rc_neh_lookup(struct uwb_rc *rc,
327 const struct uwb_rceb *rceb)
329 struct uwb_rc_neh *neh = NULL, *h;
330 unsigned long flags;
332 spin_lock_irqsave(&rc->neh_lock, flags);
334 list_for_each_entry(h, &rc->neh_list, list_node) {
335 if (uwb_rc_neh_match(h, rceb)) {
336 neh = h;
337 break;
341 if (neh)
342 __uwb_rc_neh_rm(rc, neh);
344 spin_unlock_irqrestore(&rc->neh_lock, flags);
346 return neh;
351 * Process notifications coming from the radio control interface
353 * @rc: UWB Radio Control Interface descriptor
354 * @neh: Notification/Event Handler @neh->ptr points to
355 * @uwb_evt->buffer.
357 * This function is called by the event/notif handling subsystem when
358 * notifications arrive (hwarc_probe() arms a notification/event handle
359 * that calls back this function for every received notification; this
360 * function then will rearm itself).
362 * Notification data buffers are dynamically allocated by the NEH
363 * handling code in neh.c [uwb_rc_neh_lookup()]. What is actually
364 * allocated is space to contain the notification data.
366 * Buffers are prefixed with a Radio Control Event Block (RCEB) as
367 * defined by the WUSB Wired-Adapter Radio Control interface. We
368 * just use it for the notification code.
370 * On each case statement we just transcode endianess of the different
371 * fields. We declare a pointer to a RCI definition of an event, and
372 * then to a UWB definition of the same event (which are the same,
373 * remember). Event if we use different pointers
375 static
376 void uwb_rc_notif(struct uwb_rc *rc, struct uwb_rceb *rceb, ssize_t size)
378 struct device *dev = &rc->uwb_dev.dev;
379 struct uwb_event *uwb_evt;
381 if (size == -ESHUTDOWN)
382 return;
383 if (size < 0) {
384 dev_err(dev, "ignoring event with error code %zu\n",
385 size);
386 return;
389 uwb_evt = kzalloc(sizeof(*uwb_evt), GFP_ATOMIC);
390 if (unlikely(uwb_evt == NULL)) {
391 dev_err(dev, "no memory to queue event 0x%02x/%04x/%02x\n",
392 rceb->bEventType, le16_to_cpu(rceb->wEvent),
393 rceb->bEventContext);
394 return;
396 uwb_evt->rc = __uwb_rc_get(rc); /* will be put by uwbd's uwbd_event_handle() */
397 uwb_evt->ts_jiffies = jiffies;
398 uwb_evt->type = UWB_EVT_TYPE_NOTIF;
399 uwb_evt->notif.size = size;
400 uwb_evt->notif.rceb = rceb;
402 uwbd_event_queue(uwb_evt);
405 static void uwb_rc_neh_grok_event(struct uwb_rc *rc, struct uwb_rceb *rceb, size_t size)
407 struct device *dev = &rc->uwb_dev.dev;
408 struct uwb_rc_neh *neh;
409 struct uwb_rceb *notif;
411 if (rceb->bEventContext == 0) {
412 notif = kmalloc(size, GFP_ATOMIC);
413 if (notif) {
414 memcpy(notif, rceb, size);
415 uwb_rc_notif(rc, notif, size);
416 } else
417 dev_err(dev, "event 0x%02x/%04x/%02x (%zu bytes): no memory\n",
418 rceb->bEventType, le16_to_cpu(rceb->wEvent),
419 rceb->bEventContext, size);
420 } else {
421 neh = uwb_rc_neh_lookup(rc, rceb);
422 if (neh) {
423 del_timer_sync(&neh->timer);
424 uwb_rc_neh_cb(neh, rceb, size);
425 } else
426 dev_warn(dev, "event 0x%02x/%04x/%02x (%zu bytes): nobody cared\n",
427 rceb->bEventType, le16_to_cpu(rceb->wEvent),
428 rceb->bEventContext, size);
433 * Given a buffer with one or more UWB RC events/notifications, break
434 * them up and dispatch them.
436 * @rc: UWB Radio Controller
437 * @buf: Buffer with the stream of notifications/events
438 * @buf_size: Amount of data in the buffer
440 * Note each notification/event starts always with a 'struct
441 * uwb_rceb', so the minimum size if 4 bytes.
443 * The device may pass us events formatted differently than expected.
444 * These are first filtered, potentially creating a new event in a new
445 * memory location. If a new event is created by the filter it is also
446 * freed here.
448 * For each notif/event, tries to guess the size looking at the EST
449 * tables, then looks for a neh that is waiting for that event and if
450 * found, copies the payload to the neh's buffer and calls it back. If
451 * not, the data is ignored.
453 * Note that if we can't find a size description in the EST tables, we
454 * still might find a size in the 'neh' handle in uwb_rc_neh_lookup().
456 * Assumptions:
458 * @rc->neh_lock is NOT taken
460 * We keep track of various sizes here:
461 * size: contains the size of the buffer that is processed for the
462 * incoming event. this buffer may contain events that are not
463 * formatted as WHCI.
464 * real_size: the actual space taken by this event in the buffer.
465 * We need to keep track of the real size of an event to be able to
466 * advance the buffer correctly.
467 * event_size: the size of the event as expected by the core layer
468 * [OR] the size of the event after filtering. if the filtering
469 * created a new event in a new memory location then this is
470 * effectively the size of a new event buffer
472 void uwb_rc_neh_grok(struct uwb_rc *rc, void *buf, size_t buf_size)
474 struct device *dev = &rc->uwb_dev.dev;
475 void *itr;
476 struct uwb_rceb *rceb;
477 size_t size, real_size, event_size;
478 int needtofree;
480 itr = buf;
481 size = buf_size;
482 while (size > 0) {
483 if (size < sizeof(*rceb)) {
484 dev_err(dev, "not enough data in event buffer to "
485 "process incoming events (%zu left, minimum is "
486 "%zu)\n", size, sizeof(*rceb));
487 break;
490 rceb = itr;
491 if (rc->filter_event) {
492 needtofree = rc->filter_event(rc, &rceb, size,
493 &real_size, &event_size);
494 if (needtofree < 0 && needtofree != -ENOANO) {
495 dev_err(dev, "BUG: Unable to filter event "
496 "(0x%02x/%04x/%02x) from "
497 "device. \n", rceb->bEventType,
498 le16_to_cpu(rceb->wEvent),
499 rceb->bEventContext);
500 break;
502 } else
503 needtofree = -ENOANO;
504 /* do real processing if there was no filtering or the
505 * filtering didn't act */
506 if (needtofree == -ENOANO) {
507 ssize_t ret = uwb_est_find_size(rc, rceb, size);
508 if (ret < 0)
509 break;
510 if (ret > size) {
511 dev_err(dev, "BUG: hw sent incomplete event "
512 "0x%02x/%04x/%02x (%zd bytes), only got "
513 "%zu bytes. We don't handle that.\n",
514 rceb->bEventType, le16_to_cpu(rceb->wEvent),
515 rceb->bEventContext, ret, size);
516 break;
518 real_size = event_size = ret;
520 uwb_rc_neh_grok_event(rc, rceb, event_size);
522 if (needtofree == 1)
523 kfree(rceb);
525 itr += real_size;
526 size -= real_size;
529 EXPORT_SYMBOL_GPL(uwb_rc_neh_grok);
533 * The entity that reads from the device notification/event channel has
534 * detected an error.
536 * @rc: UWB Radio Controller
537 * @error: Errno error code
540 void uwb_rc_neh_error(struct uwb_rc *rc, int error)
542 struct uwb_rc_neh *neh;
543 unsigned long flags;
545 for (;;) {
546 spin_lock_irqsave(&rc->neh_lock, flags);
547 if (list_empty(&rc->neh_list)) {
548 spin_unlock_irqrestore(&rc->neh_lock, flags);
549 break;
551 neh = list_first_entry(&rc->neh_list, struct uwb_rc_neh, list_node);
552 __uwb_rc_neh_rm(rc, neh);
553 spin_unlock_irqrestore(&rc->neh_lock, flags);
555 del_timer_sync(&neh->timer);
556 uwb_rc_neh_cb(neh, NULL, error);
559 EXPORT_SYMBOL_GPL(uwb_rc_neh_error);
562 static void uwb_rc_neh_timer(unsigned long arg)
564 struct uwb_rc_neh *neh = (struct uwb_rc_neh *)arg;
565 struct uwb_rc *rc = neh->rc;
566 unsigned long flags;
568 spin_lock_irqsave(&rc->neh_lock, flags);
569 if (neh->context)
570 __uwb_rc_neh_rm(rc, neh);
571 else
572 neh = NULL;
573 spin_unlock_irqrestore(&rc->neh_lock, flags);
575 if (neh)
576 uwb_rc_neh_cb(neh, NULL, -ETIMEDOUT);
579 /** Initializes the @rc's neh subsystem
581 void uwb_rc_neh_create(struct uwb_rc *rc)
583 spin_lock_init(&rc->neh_lock);
584 INIT_LIST_HEAD(&rc->neh_list);
585 set_bit(0, rc->ctx_bm); /* 0 is reserved (see [WUSB] table 8-65) */
586 set_bit(0xff, rc->ctx_bm); /* and 0xff is invalid */
587 rc->ctx_roll = 1;
591 /** Release's the @rc's neh subsystem */
592 void uwb_rc_neh_destroy(struct uwb_rc *rc)
594 unsigned long flags;
595 struct uwb_rc_neh *neh;
597 for (;;) {
598 spin_lock_irqsave(&rc->neh_lock, flags);
599 if (list_empty(&rc->neh_list)) {
600 spin_unlock_irqrestore(&rc->neh_lock, flags);
601 break;
603 neh = list_first_entry(&rc->neh_list, struct uwb_rc_neh, list_node);
604 __uwb_rc_neh_rm(rc, neh);
605 spin_unlock_irqrestore(&rc->neh_lock, flags);
607 del_timer_sync(&neh->timer);
608 uwb_rc_neh_put(neh);