[PATCH] Remove duplicate code in signal.c
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / w1 / w1_family.c
CommitLineData
1da177e4 1/*
7785925d 2 * w1_family.c
1da177e4
LT
3 *
4 * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru>
7785925d 5 *
1da177e4
LT
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#include <linux/delay.h>
25
26#include "w1_family.h"
27
28DEFINE_SPINLOCK(w1_flock);
29static LIST_HEAD(w1_families);
e5c515b4 30extern void w1_reconnect_slaves(struct w1_family *f);
1da177e4 31
1da177e4
LT
32int w1_register_family(struct w1_family *newf)
33{
34 struct list_head *ent, *n;
35 struct w1_family *f;
36 int ret = 0;
37
1da177e4
LT
38 spin_lock(&w1_flock);
39 list_for_each_safe(ent, n, &w1_families) {
40 f = list_entry(ent, struct w1_family, family_entry);
41
42 if (f->fid == newf->fid) {
43 ret = -EEXIST;
44 break;
45 }
46 }
47
48 if (!ret) {
49 atomic_set(&newf->refcnt, 0);
50 newf->need_exit = 0;
51 list_add_tail(&newf->family_entry, &w1_families);
52 }
1da177e4
LT
53 spin_unlock(&w1_flock);
54
6adf87bd
EP
55 w1_reconnect_slaves(newf);
56
1da177e4
LT
57 return ret;
58}
59
60void w1_unregister_family(struct w1_family *fent)
61{
62 struct list_head *ent, *n;
63 struct w1_family *f;
64
65 spin_lock(&w1_flock);
66 list_for_each_safe(ent, n, &w1_families) {
67 f = list_entry(ent, struct w1_family, family_entry);
68
69 if (f->fid == fent->fid) {
70 list_del(&fent->family_entry);
71 break;
72 }
73 }
74
75 fent->need_exit = 1;
76
77 spin_unlock(&w1_flock);
78
79 while (atomic_read(&fent->refcnt)) {
80 printk(KERN_INFO "Waiting for family %u to become free: refcnt=%d.\n",
81 fent->fid, atomic_read(&fent->refcnt));
82
83 if (msleep_interruptible(1000))
84 flush_signals(current);
85 }
86}
87
88/*
89 * Should be called under w1_flock held.
90 */
91struct w1_family * w1_family_registered(u8 fid)
92{
93 struct list_head *ent, *n;
94 struct w1_family *f = NULL;
95 int ret = 0;
96
97 list_for_each_safe(ent, n, &w1_families) {
98 f = list_entry(ent, struct w1_family, family_entry);
99
100 if (f->fid == fid) {
101 ret = 1;
102 break;
103 }
104 }
105
106 return (ret) ? f : NULL;
107}
108
109void w1_family_put(struct w1_family *f)
110{
111 spin_lock(&w1_flock);
112 __w1_family_put(f);
113 spin_unlock(&w1_flock);
114}
115
116void __w1_family_put(struct w1_family *f)
117{
118 if (atomic_dec_and_test(&f->refcnt))
119 f->need_exit = 1;
120}
121
122void w1_family_get(struct w1_family *f)
123{
124 spin_lock(&w1_flock);
125 __w1_family_get(f);
126 spin_unlock(&w1_flock);
127
128}
129
130void __w1_family_get(struct w1_family *f)
131{
132 smp_mb__before_atomic_inc();
133 atomic_inc(&f->refcnt);
134 smp_mb__after_atomic_inc();
135}
136
137EXPORT_SYMBOL(w1_family_get);
138EXPORT_SYMBOL(w1_family_put);
139EXPORT_SYMBOL(w1_family_registered);
140EXPORT_SYMBOL(w1_unregister_family);
141EXPORT_SYMBOL(w1_register_family);