vserver 1.9.3
[linux-2.6.git] / drivers / w1 / w1_family.c
1 /*
2  *      w1_family.c
3  *
4  * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru>
5  * 
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */
21
22 #include <linux/spinlock.h>
23 #include <linux/list.h>
24
25 #include "w1_family.h"
26
27 spinlock_t w1_flock = SPIN_LOCK_UNLOCKED;
28 static LIST_HEAD(w1_families);
29
30 static int w1_check_family(struct w1_family *f)
31 {
32         if (!f->fops->rname || !f->fops->rbin || !f->fops->rval || !f->fops->rvalname)
33                 return -EINVAL;
34
35         return 0;
36 }
37
38 int w1_register_family(struct w1_family *newf)
39 {
40         struct list_head *ent, *n;
41         struct w1_family *f;
42         int ret = 0;
43
44         if (w1_check_family(newf))
45                 return -EINVAL;
46
47         spin_lock(&w1_flock);
48         list_for_each_safe(ent, n, &w1_families) {
49                 f = list_entry(ent, struct w1_family, family_entry);
50
51                 if (f->fid == newf->fid) {
52                         ret = -EEXIST;
53                         break;
54                 }
55         }
56
57         if (!ret) {
58                 atomic_set(&newf->refcnt, 0);
59                 newf->need_exit = 0;
60                 list_add_tail(&newf->family_entry, &w1_families);
61         }
62
63         spin_unlock(&w1_flock);
64
65         return ret;
66 }
67
68 void w1_unregister_family(struct w1_family *fent)
69 {
70         struct list_head *ent, *n;
71         struct w1_family *f;
72
73         spin_lock(&w1_flock);
74         list_for_each_safe(ent, n, &w1_families) {
75                 f = list_entry(ent, struct w1_family, family_entry);
76
77                 if (f->fid == fent->fid) {
78                         list_del(&fent->family_entry);
79                         break;
80                 }
81         }
82
83         fent->need_exit = 1;
84
85         spin_unlock(&w1_flock);
86
87         while (atomic_read(&fent->refcnt))
88                 schedule_timeout(10);
89 }
90
91 /*
92  * Should be called under w1_flock held.
93  */
94 struct w1_family * w1_family_registered(u8 fid)
95 {
96         struct list_head *ent, *n;
97         struct w1_family *f = NULL;
98         int ret = 0;
99
100         list_for_each_safe(ent, n, &w1_families) {
101                 f = list_entry(ent, struct w1_family, family_entry);
102
103                 if (f->fid == fid) {
104                         ret = 1;
105                         break;
106                 }
107         }
108
109         return (ret) ? f : NULL;
110 }
111
112 void w1_family_put(struct w1_family *f)
113 {
114         spin_lock(&w1_flock);
115         __w1_family_put(f);
116         spin_unlock(&w1_flock);
117 }
118
119 void __w1_family_put(struct w1_family *f)
120 {
121         if (atomic_dec_and_test(&f->refcnt))
122                 f->need_exit = 1;
123 }
124
125 void w1_family_get(struct w1_family *f)
126 {
127         spin_lock(&w1_flock);
128         __w1_family_get(f);
129         spin_unlock(&w1_flock);
130
131 }
132
133 void __w1_family_get(struct w1_family *f)
134 {
135         atomic_inc(&f->refcnt);
136 }
137
138 EXPORT_SYMBOL(w1_family_get);
139 EXPORT_SYMBOL(w1_family_put);
140 EXPORT_SYMBOL(__w1_family_get);
141 EXPORT_SYMBOL(__w1_family_put);
142 EXPORT_SYMBOL(w1_family_registered);
143 EXPORT_SYMBOL(w1_unregister_family);
144 EXPORT_SYMBOL(w1_register_family);