blob: 73e73f6be48ac75bd0e27036c61427ba7808da3a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Evgeniy Polyakov77859252005-05-20 22:33:25 +04002 * w1_family.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru>
Evgeniy Polyakov77859252005-05-20 22:33:25 +04005 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
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#include <linux/delay.h>
25
26#include "w1_family.h"
27
28DEFINE_SPINLOCK(w1_flock);
29static LIST_HEAD(w1_families);
30
31static int w1_check_family(struct w1_family *f)
32{
Evgeniy Polyakovca775c62005-05-20 22:49:08 +040033 if (!f->fops->rname || !f->fops->rbin)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 return -EINVAL;
35
36 return 0;
37}
38
39int w1_register_family(struct w1_family *newf)
40{
41 struct list_head *ent, *n;
42 struct w1_family *f;
43 int ret = 0;
44
45 if (w1_check_family(newf))
46 return -EINVAL;
47
48 spin_lock(&w1_flock);
49 list_for_each_safe(ent, n, &w1_families) {
50 f = list_entry(ent, struct w1_family, family_entry);
51
52 if (f->fid == newf->fid) {
53 ret = -EEXIST;
54 break;
55 }
56 }
57
58 if (!ret) {
59 atomic_set(&newf->refcnt, 0);
60 newf->need_exit = 0;
61 list_add_tail(&newf->family_entry, &w1_families);
62 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 spin_unlock(&w1_flock);
64
Evgeniy Polyakov6adf87b2005-06-04 01:29:25 +040065 w1_reconnect_slaves(newf);
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 return ret;
68}
69
70void w1_unregister_family(struct w1_family *fent)
71{
72 struct list_head *ent, *n;
73 struct w1_family *f;
74
75 spin_lock(&w1_flock);
76 list_for_each_safe(ent, n, &w1_families) {
77 f = list_entry(ent, struct w1_family, family_entry);
78
79 if (f->fid == fent->fid) {
80 list_del(&fent->family_entry);
81 break;
82 }
83 }
84
85 fent->need_exit = 1;
86
87 spin_unlock(&w1_flock);
88
89 while (atomic_read(&fent->refcnt)) {
90 printk(KERN_INFO "Waiting for family %u to become free: refcnt=%d.\n",
91 fent->fid, atomic_read(&fent->refcnt));
92
93 if (msleep_interruptible(1000))
94 flush_signals(current);
95 }
96}
97
98/*
99 * Should be called under w1_flock held.
100 */
101struct w1_family * w1_family_registered(u8 fid)
102{
103 struct list_head *ent, *n;
104 struct w1_family *f = NULL;
105 int ret = 0;
106
107 list_for_each_safe(ent, n, &w1_families) {
108 f = list_entry(ent, struct w1_family, family_entry);
109
110 if (f->fid == fid) {
111 ret = 1;
112 break;
113 }
114 }
115
116 return (ret) ? f : NULL;
117}
118
119void w1_family_put(struct w1_family *f)
120{
121 spin_lock(&w1_flock);
122 __w1_family_put(f);
123 spin_unlock(&w1_flock);
124}
125
126void __w1_family_put(struct w1_family *f)
127{
128 if (atomic_dec_and_test(&f->refcnt))
129 f->need_exit = 1;
130}
131
132void w1_family_get(struct w1_family *f)
133{
134 spin_lock(&w1_flock);
135 __w1_family_get(f);
136 spin_unlock(&w1_flock);
137
138}
139
140void __w1_family_get(struct w1_family *f)
141{
142 smp_mb__before_atomic_inc();
143 atomic_inc(&f->refcnt);
144 smp_mb__after_atomic_inc();
145}
146
147EXPORT_SYMBOL(w1_family_get);
148EXPORT_SYMBOL(w1_family_put);
149EXPORT_SYMBOL(w1_family_registered);
150EXPORT_SYMBOL(w1_unregister_family);
151EXPORT_SYMBOL(w1_register_family);