11da177e4SLinus Torvalds /* 2a8018766SEvgeniy Polyakov * Copyright (c) 2004 Evgeniy Polyakov <zbr@ioremap.net> 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or modify 51da177e4SLinus Torvalds * it under the terms of the GNU General Public License as published by 61da177e4SLinus Torvalds * the Free Software Foundation; either version 2 of the License, or 71da177e4SLinus Torvalds * (at your option) any later version. 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * This program is distributed in the hope that it will be useful, 101da177e4SLinus Torvalds * but WITHOUT ANY WARRANTY; without even the implied warranty of 111da177e4SLinus Torvalds * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 121da177e4SLinus Torvalds * GNU General Public License for more details. 131da177e4SLinus Torvalds */ 141da177e4SLinus Torvalds 151da177e4SLinus Torvalds #include <linux/spinlock.h> 161da177e4SLinus Torvalds #include <linux/list.h> 173f07c014SIngo Molnar #include <linux/sched/signal.h> 181da177e4SLinus Torvalds #include <linux/delay.h> 199623932cSPaul Gortmaker #include <linux/export.h> 201da177e4SLinus Torvalds 211da177e4SLinus Torvalds #include "w1_family.h" 22a9fb1c7bSAdrian Bunk #include "w1.h" 231da177e4SLinus Torvalds 241da177e4SLinus Torvalds DEFINE_SPINLOCK(w1_flock); 251da177e4SLinus Torvalds static LIST_HEAD(w1_families); 261da177e4SLinus Torvalds 27b3be177aSDavid Fries /** 28b3be177aSDavid Fries * w1_register_family() - register a device family driver 29b3be177aSDavid Fries * @newf: family to register 30b3be177aSDavid Fries */ 311da177e4SLinus Torvalds int w1_register_family(struct w1_family *newf) 321da177e4SLinus Torvalds { 331da177e4SLinus Torvalds struct list_head *ent, *n; 341da177e4SLinus Torvalds struct w1_family *f; 351da177e4SLinus Torvalds int ret = 0; 361da177e4SLinus Torvalds 371da177e4SLinus Torvalds spin_lock(&w1_flock); 381da177e4SLinus Torvalds list_for_each_safe(ent, n, &w1_families) { 391da177e4SLinus Torvalds f = list_entry(ent, struct w1_family, family_entry); 401da177e4SLinus Torvalds 411da177e4SLinus Torvalds if (f->fid == newf->fid) { 421da177e4SLinus Torvalds ret = -EEXIST; 431da177e4SLinus Torvalds break; 441da177e4SLinus Torvalds } 451da177e4SLinus Torvalds } 461da177e4SLinus Torvalds 471da177e4SLinus Torvalds if (!ret) { 481da177e4SLinus Torvalds atomic_set(&newf->refcnt, 0); 491da177e4SLinus Torvalds list_add_tail(&newf->family_entry, &w1_families); 501da177e4SLinus Torvalds } 511da177e4SLinus Torvalds spin_unlock(&w1_flock); 521da177e4SLinus Torvalds 53c30c9b15SDavid Fries /* check default devices against the new set of drivers */ 54c30c9b15SDavid Fries w1_reconnect_slaves(newf, 1); 556adf87bdSEvgeniy Polyakov 561da177e4SLinus Torvalds return ret; 571da177e4SLinus Torvalds } 58*50fa2951SAndrew F. Davis EXPORT_SYMBOL(w1_register_family); 591da177e4SLinus Torvalds 60b3be177aSDavid Fries /** 61b3be177aSDavid Fries * w1_unregister_family() - unregister a device family driver 62b3be177aSDavid Fries * @fent: family to unregister 63b3be177aSDavid Fries */ 641da177e4SLinus Torvalds void w1_unregister_family(struct w1_family *fent) 651da177e4SLinus Torvalds { 661da177e4SLinus Torvalds struct list_head *ent, *n; 671da177e4SLinus Torvalds struct w1_family *f; 681da177e4SLinus Torvalds 691da177e4SLinus Torvalds spin_lock(&w1_flock); 701da177e4SLinus Torvalds list_for_each_safe(ent, n, &w1_families) { 711da177e4SLinus Torvalds f = list_entry(ent, struct w1_family, family_entry); 721da177e4SLinus Torvalds 731da177e4SLinus Torvalds if (f->fid == fent->fid) { 741da177e4SLinus Torvalds list_del(&fent->family_entry); 751da177e4SLinus Torvalds break; 761da177e4SLinus Torvalds } 771da177e4SLinus Torvalds } 781da177e4SLinus Torvalds spin_unlock(&w1_flock); 791da177e4SLinus Torvalds 80c30c9b15SDavid Fries /* deatch devices using this family code */ 81c30c9b15SDavid Fries w1_reconnect_slaves(fent, 0); 82c30c9b15SDavid Fries 831da177e4SLinus Torvalds while (atomic_read(&fent->refcnt)) { 84fdc9167aSFjodor Schelichow pr_info("Waiting for family %u to become free: refcnt=%d.\n", 851da177e4SLinus Torvalds fent->fid, atomic_read(&fent->refcnt)); 861da177e4SLinus Torvalds 871da177e4SLinus Torvalds if (msleep_interruptible(1000)) 881da177e4SLinus Torvalds flush_signals(current); 891da177e4SLinus Torvalds } 901da177e4SLinus Torvalds } 91*50fa2951SAndrew F. Davis EXPORT_SYMBOL(w1_unregister_family); 921da177e4SLinus Torvalds 931da177e4SLinus Torvalds /* 941da177e4SLinus Torvalds * Should be called under w1_flock held. 951da177e4SLinus Torvalds */ 961da177e4SLinus Torvalds struct w1_family * w1_family_registered(u8 fid) 971da177e4SLinus Torvalds { 981da177e4SLinus Torvalds struct list_head *ent, *n; 991da177e4SLinus Torvalds struct w1_family *f = NULL; 1001da177e4SLinus Torvalds int ret = 0; 1011da177e4SLinus Torvalds 1021da177e4SLinus Torvalds list_for_each_safe(ent, n, &w1_families) { 1031da177e4SLinus Torvalds f = list_entry(ent, struct w1_family, family_entry); 1041da177e4SLinus Torvalds 1051da177e4SLinus Torvalds if (f->fid == fid) { 1061da177e4SLinus Torvalds ret = 1; 1071da177e4SLinus Torvalds break; 1081da177e4SLinus Torvalds } 1091da177e4SLinus Torvalds } 1101da177e4SLinus Torvalds 1111da177e4SLinus Torvalds return (ret) ? f : NULL; 1121da177e4SLinus Torvalds } 1131da177e4SLinus Torvalds 11470d484bfSEvgeniy Polyakov static void __w1_family_put(struct w1_family *f) 11570d484bfSEvgeniy Polyakov { 116fe3cb823SDavid Fries atomic_dec(&f->refcnt); 11770d484bfSEvgeniy Polyakov } 11870d484bfSEvgeniy Polyakov 1191da177e4SLinus Torvalds void w1_family_put(struct w1_family *f) 1201da177e4SLinus Torvalds { 1211da177e4SLinus Torvalds spin_lock(&w1_flock); 1221da177e4SLinus Torvalds __w1_family_put(f); 1231da177e4SLinus Torvalds spin_unlock(&w1_flock); 1241da177e4SLinus Torvalds } 1251da177e4SLinus Torvalds 12670d484bfSEvgeniy Polyakov #if 0 1271da177e4SLinus Torvalds void w1_family_get(struct w1_family *f) 1281da177e4SLinus Torvalds { 1291da177e4SLinus Torvalds spin_lock(&w1_flock); 1301da177e4SLinus Torvalds __w1_family_get(f); 1311da177e4SLinus Torvalds spin_unlock(&w1_flock); 1321da177e4SLinus Torvalds } 13370d484bfSEvgeniy Polyakov #endif /* 0 */ 1341da177e4SLinus Torvalds 1351da177e4SLinus Torvalds void __w1_family_get(struct w1_family *f) 1361da177e4SLinus Torvalds { 1374e857c58SPeter Zijlstra smp_mb__before_atomic(); 1381da177e4SLinus Torvalds atomic_inc(&f->refcnt); 1394e857c58SPeter Zijlstra smp_mb__after_atomic(); 1401da177e4SLinus Torvalds } 141