xref: /linux/drivers/w1/w1_family.c (revision 75bf465f0bc33e9b776a46d6a1b9b990f5fb7c37)
1*c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
3a8018766SEvgeniy Polyakov  * Copyright (c) 2004 Evgeniy Polyakov <zbr@ioremap.net>
41da177e4SLinus Torvalds  */
51da177e4SLinus Torvalds 
61da177e4SLinus Torvalds #include <linux/spinlock.h>
71da177e4SLinus Torvalds #include <linux/list.h>
83f07c014SIngo Molnar #include <linux/sched/signal.h>
91da177e4SLinus Torvalds #include <linux/delay.h>
109623932cSPaul Gortmaker #include <linux/export.h>
111da177e4SLinus Torvalds 
12de0d6dbdSAndrew F. Davis #include "w1_internal.h"
131da177e4SLinus Torvalds 
141da177e4SLinus Torvalds DEFINE_SPINLOCK(w1_flock);
151da177e4SLinus Torvalds static LIST_HEAD(w1_families);
161da177e4SLinus Torvalds 
17b3be177aSDavid Fries /**
18b3be177aSDavid Fries  * w1_register_family() - register a device family driver
19b3be177aSDavid Fries  * @newf:	family to register
20b3be177aSDavid Fries  */
w1_register_family(struct w1_family * newf)211da177e4SLinus Torvalds int w1_register_family(struct w1_family *newf)
221da177e4SLinus Torvalds {
231da177e4SLinus Torvalds 	struct list_head *ent, *n;
241da177e4SLinus Torvalds 	struct w1_family *f;
251da177e4SLinus Torvalds 	int ret = 0;
261da177e4SLinus Torvalds 
271da177e4SLinus Torvalds 	spin_lock(&w1_flock);
281da177e4SLinus Torvalds 	list_for_each_safe(ent, n, &w1_families) {
291da177e4SLinus Torvalds 		f = list_entry(ent, struct w1_family, family_entry);
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds 		if (f->fid == newf->fid) {
321da177e4SLinus Torvalds 			ret = -EEXIST;
331da177e4SLinus Torvalds 			break;
341da177e4SLinus Torvalds 		}
351da177e4SLinus Torvalds 	}
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds 	if (!ret) {
381da177e4SLinus Torvalds 		atomic_set(&newf->refcnt, 0);
391da177e4SLinus Torvalds 		list_add_tail(&newf->family_entry, &w1_families);
401da177e4SLinus Torvalds 	}
411da177e4SLinus Torvalds 	spin_unlock(&w1_flock);
421da177e4SLinus Torvalds 
43c30c9b15SDavid Fries 	/* check default devices against the new set of drivers */
44c30c9b15SDavid Fries 	w1_reconnect_slaves(newf, 1);
456adf87bdSEvgeniy Polyakov 
461da177e4SLinus Torvalds 	return ret;
471da177e4SLinus Torvalds }
4850fa2951SAndrew F. Davis EXPORT_SYMBOL(w1_register_family);
491da177e4SLinus Torvalds 
50b3be177aSDavid Fries /**
51b3be177aSDavid Fries  * w1_unregister_family() - unregister a device family driver
52b3be177aSDavid Fries  * @fent:	family to unregister
53b3be177aSDavid Fries  */
w1_unregister_family(struct w1_family * fent)541da177e4SLinus Torvalds void w1_unregister_family(struct w1_family *fent)
551da177e4SLinus Torvalds {
561da177e4SLinus Torvalds 	struct list_head *ent, *n;
571da177e4SLinus Torvalds 	struct w1_family *f;
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds 	spin_lock(&w1_flock);
601da177e4SLinus Torvalds 	list_for_each_safe(ent, n, &w1_families) {
611da177e4SLinus Torvalds 		f = list_entry(ent, struct w1_family, family_entry);
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds 		if (f->fid == fent->fid) {
641da177e4SLinus Torvalds 			list_del(&fent->family_entry);
651da177e4SLinus Torvalds 			break;
661da177e4SLinus Torvalds 		}
671da177e4SLinus Torvalds 	}
681da177e4SLinus Torvalds 	spin_unlock(&w1_flock);
691da177e4SLinus Torvalds 
70c30c9b15SDavid Fries 	/* deatch devices using this family code */
71c30c9b15SDavid Fries 	w1_reconnect_slaves(fent, 0);
72c30c9b15SDavid Fries 
731da177e4SLinus Torvalds 	while (atomic_read(&fent->refcnt)) {
74fdc9167aSFjodor Schelichow 		pr_info("Waiting for family %u to become free: refcnt=%d.\n",
751da177e4SLinus Torvalds 				fent->fid, atomic_read(&fent->refcnt));
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds 		if (msleep_interruptible(1000))
781da177e4SLinus Torvalds 			flush_signals(current);
791da177e4SLinus Torvalds 	}
801da177e4SLinus Torvalds }
8150fa2951SAndrew F. Davis EXPORT_SYMBOL(w1_unregister_family);
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds /*
841da177e4SLinus Torvalds  * Should be called under w1_flock held.
851da177e4SLinus Torvalds  */
w1_family_registered(u8 fid)861da177e4SLinus Torvalds struct w1_family * w1_family_registered(u8 fid)
871da177e4SLinus Torvalds {
881da177e4SLinus Torvalds 	struct list_head *ent, *n;
891da177e4SLinus Torvalds 	struct w1_family *f = NULL;
901da177e4SLinus Torvalds 	int ret = 0;
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds 	list_for_each_safe(ent, n, &w1_families) {
931da177e4SLinus Torvalds 		f = list_entry(ent, struct w1_family, family_entry);
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds 		if (f->fid == fid) {
961da177e4SLinus Torvalds 			ret = 1;
971da177e4SLinus Torvalds 			break;
981da177e4SLinus Torvalds 		}
991da177e4SLinus Torvalds 	}
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds 	return (ret) ? f : NULL;
1021da177e4SLinus Torvalds }
1031da177e4SLinus Torvalds 
__w1_family_put(struct w1_family * f)10470d484bfSEvgeniy Polyakov static void __w1_family_put(struct w1_family *f)
10570d484bfSEvgeniy Polyakov {
106fe3cb823SDavid Fries 	atomic_dec(&f->refcnt);
10770d484bfSEvgeniy Polyakov }
10870d484bfSEvgeniy Polyakov 
w1_family_put(struct w1_family * f)1091da177e4SLinus Torvalds void w1_family_put(struct w1_family *f)
1101da177e4SLinus Torvalds {
1111da177e4SLinus Torvalds 	spin_lock(&w1_flock);
1121da177e4SLinus Torvalds 	__w1_family_put(f);
1131da177e4SLinus Torvalds 	spin_unlock(&w1_flock);
1141da177e4SLinus Torvalds }
1151da177e4SLinus Torvalds 
11670d484bfSEvgeniy Polyakov #if 0
1171da177e4SLinus Torvalds void w1_family_get(struct w1_family *f)
1181da177e4SLinus Torvalds {
1191da177e4SLinus Torvalds 	spin_lock(&w1_flock);
1201da177e4SLinus Torvalds 	__w1_family_get(f);
1211da177e4SLinus Torvalds 	spin_unlock(&w1_flock);
1221da177e4SLinus Torvalds }
12370d484bfSEvgeniy Polyakov #endif  /*  0  */
1241da177e4SLinus Torvalds 
__w1_family_get(struct w1_family * f)1251da177e4SLinus Torvalds void __w1_family_get(struct w1_family *f)
1261da177e4SLinus Torvalds {
1274e857c58SPeter Zijlstra 	smp_mb__before_atomic();
1281da177e4SLinus Torvalds 	atomic_inc(&f->refcnt);
1294e857c58SPeter Zijlstra 	smp_mb__after_atomic();
1301da177e4SLinus Torvalds }
131