initial commit with v2.6.9
[linux-2.6.9-moxart.git] / drivers / w1 / w1_family.c
blob6a62d2adb9d18665dda57d3168119125c7137d85
1 /*
2 * w1_family.c
4 * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru>
5 *
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/spinlock.h>
23 #include <linux/list.h>
25 #include "w1_family.h"
27 spinlock_t w1_flock = SPIN_LOCK_UNLOCKED;
28 static LIST_HEAD(w1_families);
30 static int w1_check_family(struct w1_family *f)
32 if (!f->fops->rname || !f->fops->rbin || !f->fops->rval || !f->fops->rvalname)
33 return -EINVAL;
35 return 0;
38 int w1_register_family(struct w1_family *newf)
40 struct list_head *ent, *n;
41 struct w1_family *f;
42 int ret = 0;
44 if (w1_check_family(newf))
45 return -EINVAL;
47 spin_lock(&w1_flock);
48 list_for_each_safe(ent, n, &w1_families) {
49 f = list_entry(ent, struct w1_family, family_entry);
51 if (f->fid == newf->fid) {
52 ret = -EEXIST;
53 break;
57 if (!ret) {
58 atomic_set(&newf->refcnt, 0);
59 newf->need_exit = 0;
60 list_add_tail(&newf->family_entry, &w1_families);
63 spin_unlock(&w1_flock);
65 return ret;
68 void w1_unregister_family(struct w1_family *fent)
70 struct list_head *ent, *n;
71 struct w1_family *f;
73 spin_lock(&w1_flock);
74 list_for_each_safe(ent, n, &w1_families) {
75 f = list_entry(ent, struct w1_family, family_entry);
77 if (f->fid == fent->fid) {
78 list_del(&fent->family_entry);
79 break;
83 fent->need_exit = 1;
85 spin_unlock(&w1_flock);
87 while (atomic_read(&fent->refcnt))
88 schedule_timeout(10);
92 * Should be called under w1_flock held.
94 struct w1_family * w1_family_registered(u8 fid)
96 struct list_head *ent, *n;
97 struct w1_family *f = NULL;
98 int ret = 0;
100 list_for_each_safe(ent, n, &w1_families) {
101 f = list_entry(ent, struct w1_family, family_entry);
103 if (f->fid == fid) {
104 ret = 1;
105 break;
109 return (ret) ? f : NULL;
112 void w1_family_put(struct w1_family *f)
114 spin_lock(&w1_flock);
115 __w1_family_put(f);
116 spin_unlock(&w1_flock);
119 void __w1_family_put(struct w1_family *f)
121 if (atomic_dec_and_test(&f->refcnt))
122 f->need_exit = 1;
125 void w1_family_get(struct w1_family *f)
127 spin_lock(&w1_flock);
128 __w1_family_get(f);
129 spin_unlock(&w1_flock);
133 void __w1_family_get(struct w1_family *f)
135 atomic_inc(&f->refcnt);
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);