4 * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru>
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)
38 int w1_register_family(struct w1_family *newf)
40 struct list_head *ent, *n;
44 if (w1_check_family(newf))
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) {
58 atomic_set(&newf->refcnt, 0);
60 list_add_tail(&newf->family_entry, &w1_families);
63 spin_unlock(&w1_flock);
68 void w1_unregister_family(struct w1_family *fent)
70 struct list_head *ent, *n;
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);
85 spin_unlock(&w1_flock);
87 while (atomic_read(&fent->refcnt))
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;
100 list_for_each_safe(ent, n, &w1_families) {
101 f = list_entry(ent, struct w1_family, family_entry);
109 return (ret) ? f : NULL;
112 void w1_family_put(struct w1_family *f)
114 spin_lock(&w1_flock);
116 spin_unlock(&w1_flock);
119 void __w1_family_put(struct w1_family *f)
121 if (atomic_dec_and_test(&f->refcnt))
125 void w1_family_get(struct w1_family *f)
127 spin_lock(&w1_flock);
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);