add d80211 from a recent wireless-dev checkout
[openwrt.git] / package / d80211 / src / ieee80211_dev.c
1 /*
2  * Copyright (c) 2006 Jiri Benc <jbenc@suse.cz>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  */
8
9 #include <linux/kernel.h>
10 #include <linux/interrupt.h>
11 #include <linux/if.h>
12 #include <linux/if_ether.h>
13 #include <linux/netdevice.h>
14 #include <net/d80211.h>
15 #include "ieee80211_i.h"
16 #include "ieee80211_led.h"
17
18 struct ieee80211_dev_list {
19         struct list_head list;
20         int dev_index;
21         struct ieee80211_local *local;
22 };
23
24 static LIST_HEAD(dev_list);
25 static DEFINE_SPINLOCK(dev_list_lock);
26
27
28 /* Caller must hold dev_list_lock */
29 static struct ieee80211_dev_list *__ieee80211_dev_find(int index)
30 {
31         struct ieee80211_dev_list *dev_item;
32
33         list_for_each_entry(dev_item, &dev_list, list) {
34                 if (dev_item->dev_index == index)
35                         return dev_item;
36         }
37         return NULL;
38 }
39
40 int ieee80211_dev_alloc_index(struct ieee80211_local *local)
41 {
42         struct ieee80211_dev_list *dev_item, *new;
43         int index = 0;
44
45         new = kmalloc(sizeof(struct ieee80211_dev_list), GFP_KERNEL);
46         if (!new)
47                 return -ENOMEM;
48         new->local = local;
49         spin_lock(&dev_list_lock);
50         list_for_each_entry(dev_item, &dev_list, list) {
51                 if (index < dev_item->dev_index)
52                         break;
53                 index++;
54         }
55         new->dev_index = index;
56         list_add_tail(&new->list, &dev_item->list);
57         spin_unlock(&dev_list_lock);
58         local->hw.index = index;
59         return index;
60 }
61
62 void ieee80211_dev_free_index(struct ieee80211_local *local)
63 {
64         struct ieee80211_dev_list *dev_item;
65
66         spin_lock(&dev_list_lock);
67         dev_item = __ieee80211_dev_find(local->hw.index);
68         if (dev_item)
69                 list_del(&dev_item->list);
70         spin_unlock(&dev_list_lock);
71         if (dev_item)
72                 kfree(dev_item);
73         local->hw.index = -1;
74 }
75
76 struct ieee80211_local *ieee80211_dev_find(int index)
77 {
78         struct ieee80211_dev_list *dev_item;
79
80         spin_lock(&dev_list_lock);
81         dev_item = __ieee80211_dev_find(index);
82         spin_unlock(&dev_list_lock);
83         return dev_item ? dev_item->local : NULL;
84 }
85
86 int ieee80211_dev_find_index(struct ieee80211_local *local)
87 {
88         struct ieee80211_dev_list *dev_item;
89         int index = -1;
90
91         spin_lock(&dev_list_lock);
92         list_for_each_entry(dev_item, &dev_list, list) {
93                 if (dev_item->local == local) {
94                         index = dev_item->dev_index;
95                         break;
96                 }
97         }
98         spin_unlock(&dev_list_lock);
99         return index;
100 }
101
102 struct ieee80211_local *ieee80211_dev_alloc(gfp_t flags)
103 {
104         struct ieee80211_local *local;
105
106         local = kzalloc(sizeof(struct ieee80211_local), flags);
107         if (!local)
108                 return NULL;
109         local->hw.index = -1;
110         ieee80211_dev_sysfs_init(local);
111         return local;
112 }
113
114 void ieee80211_dev_free(struct ieee80211_local *local)
115 {
116         ieee80211_dev_sysfs_put(local);
117 }