Linux 3.2.102
[pandora-kernel.git] / net / irda / irqueue.c
1 /*********************************************************************
2  *
3  * Filename:      irqueue.c
4  * Version:       0.3
5  * Description:   General queue implementation
6  * Status:        Experimental.
7  * Author:        Dag Brattli <dagb@cs.uit.no>
8  * Created at:    Tue Jun  9 13:29:31 1998
9  * Modified at:   Sun Dec 12 13:48:22 1999
10  * Modified by:   Dag Brattli <dagb@cs.uit.no>
11  * Modified at:   Thu Jan  4 14:29:10 CET 2001
12  * Modified by:   Marc Zyngier <mzyngier@freesurf.fr>
13  *
14  *     Copyright (C) 1998-1999, Aage Kvalnes <aage@cs.uit.no>
15  *     Copyright (C) 1998, Dag Brattli,
16  *     All Rights Reserved.
17  *
18  *     This code is taken from the Vortex Operating System written by Aage
19  *     Kvalnes. Aage has agreed that this code can use the GPL licence,
20  *     although he does not use that licence in his own code.
21  *
22  *     This copyright does however _not_ include the ELF hash() function
23  *     which I currently don't know which licence or copyright it
24  *     has. Please inform me if you know.
25  *
26  *     This program is free software; you can redistribute it and/or
27  *     modify it under the terms of the GNU General Public License as
28  *     published by the Free Software Foundation; either version 2 of
29  *     the License, or (at your option) any later version.
30  *
31  *     Neither Dag Brattli nor University of Tromsø admit liability nor
32  *     provide warranty for any of this software. This material is
33  *     provided "AS-IS" and at no charge.
34  *
35  ********************************************************************/
36
37 /*
38  * NOTE :
39  * There are various problems with this package :
40  *      o the hash function for ints is pathetic (but could be changed)
41  *      o locking is sometime suspicious (especially during enumeration)
42  *      o most users have only a few elements (== overhead)
43  *      o most users never use search, so don't benefit from hashing
44  * Problem already fixed :
45  *      o not 64 bit compliant (most users do hashv = (int) self)
46  *      o hashbin_remove() is broken => use hashbin_remove_this()
47  * I think most users would be better served by a simple linked list
48  * (like include/linux/list.h) with a global spinlock per list.
49  * Jean II
50  */
51
52 /*
53  * Notes on the concurrent access to hashbin and other SMP issues
54  * -------------------------------------------------------------
55  *      Hashbins are very often in the IrDA stack a global repository of
56  * information, and therefore used in a very asynchronous manner following
57  * various events (driver calls, timers, user calls...).
58  *      Therefore, very often it is highly important to consider the
59  * management of concurrent access to the hashbin and how to guarantee the
60  * consistency of the operations on it.
61  *
62  *      First, we need to define the objective of locking :
63  *              1) Protect user data (content pointed by the hashbin)
64  *              2) Protect hashbin structure itself (linked list in each bin)
65  *
66  *                           OLD LOCKING
67  *                           -----------
68  *
69  *      The previous locking strategy, either HB_LOCAL or HB_GLOBAL were
70  * both inadequate in *both* aspect.
71  *              o HB_GLOBAL was using a spinlock for each bin (local locking).
72  *              o HB_LOCAL was disabling irq on *all* CPUs, so use a single
73  *                global semaphore.
74  *      The problems were :
75  *              A) Global irq disabling is no longer supported by the kernel
76  *              B) No protection for the hashbin struct global data
77  *                      o hashbin_delete()
78  *                      o hb_current
79  *              C) No protection for user data in some cases
80  *
81  *      A) HB_LOCAL use global irq disabling, so doesn't work on kernel
82  * 2.5.X. Even when it is supported (kernel 2.4.X and earlier), its
83  * performance is not satisfactory on SMP setups. Most hashbins were
84  * HB_LOCAL, so (A) definitely need fixing.
85  *      B) HB_LOCAL could be modified to fix (B). However, because HB_GLOBAL
86  * lock only the individual bins, it will never be able to lock the
87  * global data, so can't do (B).
88  *      C) Some functions return pointer to data that is still in the
89  * hashbin :
90  *              o hashbin_find()
91  *              o hashbin_get_first()
92  *              o hashbin_get_next()
93  *      As the data is still in the hashbin, it may be changed or free'd
94  * while the caller is examinimg the data. In those case, locking can't
95  * be done within the hashbin, but must include use of the data within
96  * the caller.
97  *      The caller can easily do this with HB_LOCAL (just disable irqs).
98  * However, this is impossible with HB_GLOBAL because the caller has no
99  * way to know the proper bin, so don't know which spinlock to use.
100  *
101  *      Quick summary : can no longer use HB_LOCAL, and HB_GLOBAL is
102  * fundamentally broken and will never work.
103  *
104  *                           NEW LOCKING
105  *                           -----------
106  *
107  *      To fix those problems, I've introduce a few changes in the
108  * hashbin locking :
109  *              1) New HB_LOCK scheme
110  *              2) hashbin->hb_spinlock
111  *              3) New hashbin usage policy
112  *
113  * HB_LOCK :
114  * -------
115  *      HB_LOCK is a locking scheme intermediate between the old HB_LOCAL
116  * and HB_GLOBAL. It uses a single spinlock to protect the whole content
117  * of the hashbin. As it is a single spinlock, it can protect the global
118  * data of the hashbin and not only the bins themselves.
119  *      HB_LOCK can only protect some of the hashbin calls, so it only lock
120  * call that can be made 100% safe and leave other call unprotected.
121  *      HB_LOCK in theory is slower than HB_GLOBAL, but as the hashbin
122  * content is always small contention is not high, so it doesn't matter
123  * much. HB_LOCK is probably faster than HB_LOCAL.
124  *
125  * hashbin->hb_spinlock :
126  * --------------------
127  *      The spinlock that HB_LOCK uses is available for caller, so that
128  * the caller can protect unprotected calls (see below).
129  *      If the caller want to do entirely its own locking (HB_NOLOCK), he
130  * can do so and may use safely this spinlock.
131  *      Locking is done like this :
132  *              spin_lock_irqsave(&hashbin->hb_spinlock, flags);
133  *      Releasing the lock :
134  *              spin_unlock_irqrestore(&hashbin->hb_spinlock, flags);
135  *
136  * Safe & Protected calls :
137  * ----------------------
138  *      The following calls are safe or protected via HB_LOCK :
139  *              o hashbin_new()         -> safe
140  *              o hashbin_delete()
141  *              o hashbin_insert()
142  *              o hashbin_remove_first()
143  *              o hashbin_remove()
144  *              o hashbin_remove_this()
145  *              o HASHBIN_GET_SIZE()    -> atomic
146  *
147  *      The following calls only protect the hashbin itself :
148  *              o hashbin_lock_find()
149  *              o hashbin_find_next()
150  *
151  * Unprotected calls :
152  * -----------------
153  *      The following calls need to be protected by the caller :
154  *              o hashbin_find()
155  *              o hashbin_get_first()
156  *              o hashbin_get_next()
157  *
158  * Locking Policy :
159  * --------------
160  *      If the hashbin is used only in a single thread of execution
161  * (explicitly or implicitely), you can use HB_NOLOCK
162  *      If the calling module already provide concurrent access protection,
163  * you may use HB_NOLOCK.
164  *
165  *      In all other cases, you need to use HB_LOCK and lock the hashbin
166  * every time before calling one of the unprotected calls. You also must
167  * use the pointer returned by the unprotected call within the locked
168  * region.
169  *
170  * Extra care for enumeration :
171  * --------------------------
172  *      hashbin_get_first() and hashbin_get_next() use the hashbin to
173  * store the current position, in hb_current.
174  *      As long as the hashbin remains locked, this is safe. If you unlock
175  * the hashbin, the current position may change if anybody else modify
176  * or enumerate the hashbin.
177  *      Summary : do the full enumeration while locked.
178  *
179  *      Alternatively, you may use hashbin_find_next(). But, this will
180  * be slower, is more complex to use and doesn't protect the hashbin
181  * content. So, care is needed here as well.
182  *
183  * Other issues :
184  * ------------
185  *      I believe that we are overdoing it by using spin_lock_irqsave()
186  * and we should use only spin_lock_bh() or similar. But, I don't have
187  * the balls to try it out.
188  *      Don't believe that because hashbin are now (somewhat) SMP safe
189  * that the rest of the code is. Higher layers tend to be safest,
190  * but LAP and LMP would need some serious dedicated love.
191  *
192  * Jean II
193  */
194 #include <linux/module.h>
195 #include <linux/slab.h>
196
197 #include <net/irda/irda.h>
198 #include <net/irda/irqueue.h>
199
200 /************************ QUEUE SUBROUTINES ************************/
201
202 /*
203  * Hashbin
204  */
205 #define GET_HASHBIN(x) ( x & HASHBIN_MASK )
206
207 /*
208  * Function hash (name)
209  *
210  *    This function hash the input string 'name' using the ELF hash
211  *    function for strings.
212  */
213 static __u32 hash( const char* name)
214 {
215         __u32 h = 0;
216         __u32 g;
217
218         while(*name) {
219                 h = (h<<4) + *name++;
220                 if ((g = (h & 0xf0000000)))
221                         h ^=g>>24;
222                 h &=~g;
223         }
224         return h;
225 }
226
227 /*
228  * Function enqueue_first (queue, proc)
229  *
230  *    Insert item first in queue.
231  *
232  */
233 static void enqueue_first(irda_queue_t **queue, irda_queue_t* element)
234 {
235
236         IRDA_DEBUG( 4, "%s()\n", __func__);
237
238         /*
239          * Check if queue is empty.
240          */
241         if ( *queue == NULL ) {
242                 /*
243                  * Queue is empty.  Insert one element into the queue.
244                  */
245                 element->q_next = element->q_prev = *queue = element;
246
247         } else {
248                 /*
249                  * Queue is not empty.  Insert element into front of queue.
250                  */
251                 element->q_next          = (*queue);
252                 (*queue)->q_prev->q_next = element;
253                 element->q_prev          = (*queue)->q_prev;
254                 (*queue)->q_prev         = element;
255                 (*queue)                 = element;
256         }
257 }
258
259
260 /*
261  * Function dequeue (queue)
262  *
263  *    Remove first entry in queue
264  *
265  */
266 static irda_queue_t *dequeue_first(irda_queue_t **queue)
267 {
268         irda_queue_t *ret;
269
270         IRDA_DEBUG( 4, "dequeue_first()\n");
271
272         /*
273          * Set return value
274          */
275         ret =  *queue;
276
277         if ( *queue == NULL ) {
278                 /*
279                  * Queue was empty.
280                  */
281         } else if ( (*queue)->q_next == *queue ) {
282                 /*
283                  *  Queue only contained a single element. It will now be
284                  *  empty.
285                  */
286                 *queue = NULL;
287         } else {
288                 /*
289                  * Queue contained several element.  Remove the first one.
290                  */
291                 (*queue)->q_prev->q_next = (*queue)->q_next;
292                 (*queue)->q_next->q_prev = (*queue)->q_prev;
293                 *queue = (*queue)->q_next;
294         }
295
296         /*
297          * Return the removed entry (or NULL of queue was empty).
298          */
299         return ret;
300 }
301
302 /*
303  * Function dequeue_general (queue, element)
304  *
305  *
306  */
307 static irda_queue_t *dequeue_general(irda_queue_t **queue, irda_queue_t* element)
308 {
309         irda_queue_t *ret;
310
311         IRDA_DEBUG( 4, "dequeue_general()\n");
312
313         /*
314          * Set return value
315          */
316         ret =  *queue;
317
318         if ( *queue == NULL ) {
319                 /*
320                  * Queue was empty.
321                  */
322         } else if ( (*queue)->q_next == *queue ) {
323                 /*
324                  *  Queue only contained a single element. It will now be
325                  *  empty.
326                  */
327                 *queue = NULL;
328
329         } else {
330                 /*
331                  *  Remove specific element.
332                  */
333                 element->q_prev->q_next = element->q_next;
334                 element->q_next->q_prev = element->q_prev;
335                 if ( (*queue) == element)
336                         (*queue) = element->q_next;
337         }
338
339         /*
340          * Return the removed entry (or NULL of queue was empty).
341          */
342         return ret;
343 }
344
345 /************************ HASHBIN MANAGEMENT ************************/
346
347 /*
348  * Function hashbin_create ( type, name )
349  *
350  *    Create hashbin!
351  *
352  */
353 hashbin_t *hashbin_new(int type)
354 {
355         hashbin_t* hashbin;
356
357         /*
358          * Allocate new hashbin
359          */
360         hashbin = kzalloc(sizeof(*hashbin), GFP_ATOMIC);
361         if (!hashbin)
362                 return NULL;
363
364         /*
365          * Initialize structure
366          */
367         hashbin->hb_type = type;
368         hashbin->magic = HB_MAGIC;
369         //hashbin->hb_current = NULL;
370
371         /* Make sure all spinlock's are unlocked */
372         if ( hashbin->hb_type & HB_LOCK ) {
373                 spin_lock_init(&hashbin->hb_spinlock);
374         }
375
376         return hashbin;
377 }
378 EXPORT_SYMBOL(hashbin_new);
379
380
381 /*
382  * Function hashbin_delete (hashbin, free_func)
383  *
384  *    Destroy hashbin, the free_func can be a user supplied special routine
385  *    for deallocating this structure if it's complex. If not the user can
386  *    just supply kfree, which should take care of the job.
387  */
388 int hashbin_delete( hashbin_t* hashbin, FREE_FUNC free_func)
389 {
390         irda_queue_t* queue;
391         unsigned long flags = 0;
392         int i;
393
394         IRDA_ASSERT(hashbin != NULL, return -1;);
395         IRDA_ASSERT(hashbin->magic == HB_MAGIC, return -1;);
396
397         /* Synchronize */
398         if (hashbin->hb_type & HB_LOCK)
399                 spin_lock_irqsave(&hashbin->hb_spinlock, flags);
400
401         /*
402          *  Free the entries in the hashbin, TODO: use hashbin_clear when
403          *  it has been shown to work
404          */
405         for (i = 0; i < HASHBIN_SIZE; i ++ ) {
406                 while (1) {
407                         queue = dequeue_first((irda_queue_t**) &hashbin->hb_queue[i]);
408
409                         if (!queue)
410                                 break;
411
412                         if (free_func) {
413                                 if (hashbin->hb_type & HB_LOCK)
414                                         spin_unlock_irqrestore(&hashbin->hb_spinlock, flags);
415                                 free_func(queue);
416                                 if (hashbin->hb_type & HB_LOCK)
417                                         spin_lock_irqsave(&hashbin->hb_spinlock, flags);
418                         }
419                 }
420         }
421
422         /* Cleanup local data */
423         hashbin->hb_current = NULL;
424         hashbin->magic = ~HB_MAGIC;
425
426         /* Release lock */
427         if (hashbin->hb_type & HB_LOCK)
428                 spin_unlock_irqrestore(&hashbin->hb_spinlock, flags);
429
430         /*
431          *  Free the hashbin structure
432          */
433         kfree(hashbin);
434
435         return 0;
436 }
437 EXPORT_SYMBOL(hashbin_delete);
438
439 /********************* HASHBIN LIST OPERATIONS *********************/
440
441 /*
442  * Function hashbin_insert (hashbin, entry, name)
443  *
444  *    Insert an entry into the hashbin
445  *
446  */
447 void hashbin_insert(hashbin_t* hashbin, irda_queue_t* entry, long hashv,
448                     const char* name)
449 {
450         unsigned long flags = 0;
451         int bin;
452
453         IRDA_DEBUG( 4, "%s()\n", __func__);
454
455         IRDA_ASSERT( hashbin != NULL, return;);
456         IRDA_ASSERT( hashbin->magic == HB_MAGIC, return;);
457
458         /*
459          * Locate hashbin
460          */
461         if ( name )
462                 hashv = hash( name );
463         bin = GET_HASHBIN( hashv );
464
465         /* Synchronize */
466         if ( hashbin->hb_type & HB_LOCK ) {
467                 spin_lock_irqsave(&hashbin->hb_spinlock, flags);
468         } /* Default is no-lock  */
469
470         /*
471          * Store name and key
472          */
473         entry->q_hash = hashv;
474         if ( name )
475                 strlcpy( entry->q_name, name, sizeof(entry->q_name));
476
477         /*
478          * Insert new entry first
479          */
480         enqueue_first( (irda_queue_t**) &hashbin->hb_queue[ bin ],
481                        entry);
482         hashbin->hb_size++;
483
484         /* Release lock */
485         if ( hashbin->hb_type & HB_LOCK ) {
486                 spin_unlock_irqrestore(&hashbin->hb_spinlock, flags);
487         } /* Default is no-lock  */
488 }
489 EXPORT_SYMBOL(hashbin_insert);
490
491 /*
492  *  Function hashbin_remove_first (hashbin)
493  *
494  *    Remove first entry of the hashbin
495  *
496  * Note : this function no longer use hashbin_remove(), but does things
497  * similar to hashbin_remove_this(), so can be considered safe.
498  * Jean II
499  */
500 void *hashbin_remove_first( hashbin_t *hashbin)
501 {
502         unsigned long flags = 0;
503         irda_queue_t *entry = NULL;
504
505         /* Synchronize */
506         if ( hashbin->hb_type & HB_LOCK ) {
507                 spin_lock_irqsave(&hashbin->hb_spinlock, flags);
508         } /* Default is no-lock  */
509
510         entry = hashbin_get_first( hashbin);
511         if ( entry != NULL) {
512                 int     bin;
513                 long    hashv;
514                 /*
515                  * Locate hashbin
516                  */
517                 hashv = entry->q_hash;
518                 bin = GET_HASHBIN( hashv );
519
520                 /*
521                  * Dequeue the entry...
522                  */
523                 dequeue_general( (irda_queue_t**) &hashbin->hb_queue[ bin ],
524                                  (irda_queue_t*) entry );
525                 hashbin->hb_size--;
526                 entry->q_next = NULL;
527                 entry->q_prev = NULL;
528
529                 /*
530                  *  Check if this item is the currently selected item, and in
531                  *  that case we must reset hb_current
532                  */
533                 if ( entry == hashbin->hb_current)
534                         hashbin->hb_current = NULL;
535         }
536
537         /* Release lock */
538         if ( hashbin->hb_type & HB_LOCK ) {
539                 spin_unlock_irqrestore(&hashbin->hb_spinlock, flags);
540         } /* Default is no-lock  */
541
542         return entry;
543 }
544
545
546 /*
547  *  Function hashbin_remove (hashbin, hashv, name)
548  *
549  *    Remove entry with the given name
550  *
551  *  The use of this function is highly discouraged, because the whole
552  *  concept behind hashbin_remove() is broken. In many cases, it's not
553  *  possible to guarantee the unicity of the index (either hashv or name),
554  *  leading to removing the WRONG entry.
555  *  The only simple safe use is :
556  *              hashbin_remove(hasbin, (int) self, NULL);
557  *  In other case, you must think hard to guarantee unicity of the index.
558  *  Jean II
559  */
560 void* hashbin_remove( hashbin_t* hashbin, long hashv, const char* name)
561 {
562         int bin, found = FALSE;
563         unsigned long flags = 0;
564         irda_queue_t* entry;
565
566         IRDA_DEBUG( 4, "%s()\n", __func__);
567
568         IRDA_ASSERT( hashbin != NULL, return NULL;);
569         IRDA_ASSERT( hashbin->magic == HB_MAGIC, return NULL;);
570
571         /*
572          * Locate hashbin
573          */
574         if ( name )
575                 hashv = hash( name );
576         bin = GET_HASHBIN( hashv );
577
578         /* Synchronize */
579         if ( hashbin->hb_type & HB_LOCK ) {
580                 spin_lock_irqsave(&hashbin->hb_spinlock, flags);
581         } /* Default is no-lock  */
582
583         /*
584          * Search for entry
585          */
586         entry = hashbin->hb_queue[ bin ];
587         if ( entry ) {
588                 do {
589                         /*
590                          * Check for key
591                          */
592                         if ( entry->q_hash == hashv ) {
593                                 /*
594                                  * Name compare too?
595                                  */
596                                 if ( name ) {
597                                         if ( strcmp( entry->q_name, name) == 0)
598                                         {
599                                                 found = TRUE;
600                                                 break;
601                                         }
602                                 } else {
603                                         found = TRUE;
604                                         break;
605                                 }
606                         }
607                         entry = entry->q_next;
608                 } while ( entry != hashbin->hb_queue[ bin ] );
609         }
610
611         /*
612          * If entry was found, dequeue it
613          */
614         if ( found ) {
615                 dequeue_general( (irda_queue_t**) &hashbin->hb_queue[ bin ],
616                                  (irda_queue_t*) entry );
617                 hashbin->hb_size--;
618
619                 /*
620                  *  Check if this item is the currently selected item, and in
621                  *  that case we must reset hb_current
622                  */
623                 if ( entry == hashbin->hb_current)
624                         hashbin->hb_current = NULL;
625         }
626
627         /* Release lock */
628         if ( hashbin->hb_type & HB_LOCK ) {
629                 spin_unlock_irqrestore(&hashbin->hb_spinlock, flags);
630         } /* Default is no-lock  */
631
632
633         /* Return */
634         if ( found )
635                 return entry;
636         else
637                 return NULL;
638
639 }
640 EXPORT_SYMBOL(hashbin_remove);
641
642 /*
643  *  Function hashbin_remove_this (hashbin, entry)
644  *
645  *    Remove entry with the given name
646  *
647  * In some cases, the user of hashbin can't guarantee the unicity
648  * of either the hashv or name.
649  * In those cases, using the above function is guaranteed to cause troubles,
650  * so we use this one instead...
651  * And by the way, it's also faster, because we skip the search phase ;-)
652  */
653 void* hashbin_remove_this( hashbin_t* hashbin, irda_queue_t* entry)
654 {
655         unsigned long flags = 0;
656         int     bin;
657         long    hashv;
658
659         IRDA_DEBUG( 4, "%s()\n", __func__);
660
661         IRDA_ASSERT( hashbin != NULL, return NULL;);
662         IRDA_ASSERT( hashbin->magic == HB_MAGIC, return NULL;);
663         IRDA_ASSERT( entry != NULL, return NULL;);
664
665         /* Synchronize */
666         if ( hashbin->hb_type & HB_LOCK ) {
667                 spin_lock_irqsave(&hashbin->hb_spinlock, flags);
668         } /* Default is no-lock  */
669
670         /* Check if valid and not already removed... */
671         if((entry->q_next == NULL) || (entry->q_prev == NULL)) {
672                 entry = NULL;
673                 goto out;
674         }
675
676         /*
677          * Locate hashbin
678          */
679         hashv = entry->q_hash;
680         bin = GET_HASHBIN( hashv );
681
682         /*
683          * Dequeue the entry...
684          */
685         dequeue_general( (irda_queue_t**) &hashbin->hb_queue[ bin ],
686                          (irda_queue_t*) entry );
687         hashbin->hb_size--;
688         entry->q_next = NULL;
689         entry->q_prev = NULL;
690
691         /*
692          *  Check if this item is the currently selected item, and in
693          *  that case we must reset hb_current
694          */
695         if ( entry == hashbin->hb_current)
696                 hashbin->hb_current = NULL;
697 out:
698         /* Release lock */
699         if ( hashbin->hb_type & HB_LOCK ) {
700                 spin_unlock_irqrestore(&hashbin->hb_spinlock, flags);
701         } /* Default is no-lock  */
702
703         return entry;
704 }
705 EXPORT_SYMBOL(hashbin_remove_this);
706
707 /*********************** HASHBIN ENUMERATION ***********************/
708
709 /*
710  * Function hashbin_common_find (hashbin, hashv, name)
711  *
712  *    Find item with the given hashv or name
713  *
714  */
715 void* hashbin_find( hashbin_t* hashbin, long hashv, const char* name )
716 {
717         int bin;
718         irda_queue_t* entry;
719
720         IRDA_DEBUG( 4, "hashbin_find()\n");
721
722         IRDA_ASSERT( hashbin != NULL, return NULL;);
723         IRDA_ASSERT( hashbin->magic == HB_MAGIC, return NULL;);
724
725         /*
726          * Locate hashbin
727          */
728         if ( name )
729                 hashv = hash( name );
730         bin = GET_HASHBIN( hashv );
731
732         /*
733          * Search for entry
734          */
735         entry = hashbin->hb_queue[ bin];
736         if ( entry ) {
737                 do {
738                         /*
739                          * Check for key
740                          */
741                         if ( entry->q_hash == hashv ) {
742                                 /*
743                                  * Name compare too?
744                                  */
745                                 if ( name ) {
746                                         if ( strcmp( entry->q_name, name ) == 0 ) {
747                                                 return entry;
748                                         }
749                                 } else {
750                                         return entry;
751                                 }
752                         }
753                         entry = entry->q_next;
754                 } while ( entry != hashbin->hb_queue[ bin ] );
755         }
756
757         return NULL;
758 }
759 EXPORT_SYMBOL(hashbin_find);
760
761 /*
762  * Function hashbin_lock_find (hashbin, hashv, name)
763  *
764  *    Find item with the given hashv or name
765  *
766  * Same, but with spinlock protection...
767  * I call it safe, but it's only safe with respect to the hashbin, not its
768  * content. - Jean II
769  */
770 void* hashbin_lock_find( hashbin_t* hashbin, long hashv, const char* name )
771 {
772         unsigned long flags = 0;
773         irda_queue_t* entry;
774
775         /* Synchronize */
776         spin_lock_irqsave(&hashbin->hb_spinlock, flags);
777
778         /*
779          * Search for entry
780          */
781         entry = hashbin_find(hashbin, hashv, name);
782
783         /* Release lock */
784         spin_unlock_irqrestore(&hashbin->hb_spinlock, flags);
785
786         return entry;
787 }
788 EXPORT_SYMBOL(hashbin_lock_find);
789
790 /*
791  * Function hashbin_find (hashbin, hashv, name, pnext)
792  *
793  *    Find an item with the given hashv or name, and its successor
794  *
795  * This function allow to do concurrent enumerations without the
796  * need to lock over the whole session, because the caller keep the
797  * context of the search. On the other hand, it might fail and return
798  * NULL if the entry is removed. - Jean II
799  */
800 void* hashbin_find_next( hashbin_t* hashbin, long hashv, const char* name,
801                          void ** pnext)
802 {
803         unsigned long flags = 0;
804         irda_queue_t* entry;
805
806         /* Synchronize */
807         spin_lock_irqsave(&hashbin->hb_spinlock, flags);
808
809         /*
810          * Search for current entry
811          * This allow to check if the current item is still in the
812          * hashbin or has been removed.
813          */
814         entry = hashbin_find(hashbin, hashv, name);
815
816         /*
817          * Trick hashbin_get_next() to return what we want
818          */
819         if(entry) {
820                 hashbin->hb_current = entry;
821                 *pnext = hashbin_get_next( hashbin );
822         } else
823                 *pnext = NULL;
824
825         /* Release lock */
826         spin_unlock_irqrestore(&hashbin->hb_spinlock, flags);
827
828         return entry;
829 }
830
831 /*
832  * Function hashbin_get_first (hashbin)
833  *
834  *    Get a pointer to first element in hashbin, this function must be
835  *    called before any calls to hashbin_get_next()!
836  *
837  */
838 irda_queue_t *hashbin_get_first( hashbin_t* hashbin)
839 {
840         irda_queue_t *entry;
841         int i;
842
843         IRDA_ASSERT( hashbin != NULL, return NULL;);
844         IRDA_ASSERT( hashbin->magic == HB_MAGIC, return NULL;);
845
846         if ( hashbin == NULL)
847                 return NULL;
848
849         for ( i = 0; i < HASHBIN_SIZE; i ++ ) {
850                 entry = hashbin->hb_queue[ i];
851                 if ( entry) {
852                         hashbin->hb_current = entry;
853                         return entry;
854                 }
855         }
856         /*
857          *  Did not find any item in hashbin
858          */
859         return NULL;
860 }
861 EXPORT_SYMBOL(hashbin_get_first);
862
863 /*
864  * Function hashbin_get_next (hashbin)
865  *
866  *    Get next item in hashbin. A series of hashbin_get_next() calls must
867  *    be started by a call to hashbin_get_first(). The function returns
868  *    NULL when all items have been traversed
869  *
870  * The context of the search is stored within the hashbin, so you must
871  * protect yourself from concurrent enumerations. - Jean II
872  */
873 irda_queue_t *hashbin_get_next( hashbin_t *hashbin)
874 {
875         irda_queue_t* entry;
876         int bin;
877         int i;
878
879         IRDA_ASSERT( hashbin != NULL, return NULL;);
880         IRDA_ASSERT( hashbin->magic == HB_MAGIC, return NULL;);
881
882         if ( hashbin->hb_current == NULL) {
883                 IRDA_ASSERT( hashbin->hb_current != NULL, return NULL;);
884                 return NULL;
885         }
886         entry = hashbin->hb_current->q_next;
887         bin = GET_HASHBIN( entry->q_hash);
888
889         /*
890          *  Make sure that we are not back at the beginning of the queue
891          *  again
892          */
893         if ( entry != hashbin->hb_queue[ bin ]) {
894                 hashbin->hb_current = entry;
895
896                 return entry;
897         }
898
899         /*
900          *  Check that this is not the last queue in hashbin
901          */
902         if ( bin >= HASHBIN_SIZE)
903                 return NULL;
904
905         /*
906          *  Move to next queue in hashbin
907          */
908         bin++;
909         for ( i = bin; i < HASHBIN_SIZE; i++ ) {
910                 entry = hashbin->hb_queue[ i];
911                 if ( entry) {
912                         hashbin->hb_current = entry;
913
914                         return entry;
915                 }
916         }
917         return NULL;
918 }
919 EXPORT_SYMBOL(hashbin_get_next);