11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * w1_family.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru> 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or modify 81da177e4SLinus Torvalds * it under the terms of the GNU General Public License as published by 91da177e4SLinus Torvalds * the Free Software Foundation; either version 2 of the License, or 101da177e4SLinus Torvalds * (at your option) any later version. 111da177e4SLinus Torvalds * 121da177e4SLinus Torvalds * This program is distributed in the hope that it will be useful, 131da177e4SLinus Torvalds * but WITHOUT ANY WARRANTY; without even the implied warranty of 141da177e4SLinus Torvalds * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 151da177e4SLinus Torvalds * GNU General Public License for more details. 161da177e4SLinus Torvalds * 171da177e4SLinus Torvalds * You should have received a copy of the GNU General Public License 181da177e4SLinus Torvalds * along with this program; if not, write to the Free Software 191da177e4SLinus Torvalds * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 201da177e4SLinus Torvalds */ 211da177e4SLinus Torvalds 221da177e4SLinus Torvalds #include <linux/spinlock.h> 231da177e4SLinus Torvalds #include <linux/list.h> 244e57b681STim Schmielau #include <linux/sched.h> /* schedule_timeout() */ 251da177e4SLinus Torvalds #include <linux/delay.h> 261da177e4SLinus Torvalds 271da177e4SLinus Torvalds #include "w1_family.h" 28a9fb1c7bSAdrian Bunk #include "w1.h" 291da177e4SLinus Torvalds 301da177e4SLinus Torvalds DEFINE_SPINLOCK(w1_flock); 311da177e4SLinus Torvalds static LIST_HEAD(w1_families); 321da177e4SLinus Torvalds 331da177e4SLinus Torvalds int w1_register_family(struct w1_family *newf) 341da177e4SLinus Torvalds { 351da177e4SLinus Torvalds struct list_head *ent, *n; 361da177e4SLinus Torvalds struct w1_family *f; 371da177e4SLinus Torvalds int ret = 0; 381da177e4SLinus Torvalds 391da177e4SLinus Torvalds spin_lock(&w1_flock); 401da177e4SLinus Torvalds list_for_each_safe(ent, n, &w1_families) { 411da177e4SLinus Torvalds f = list_entry(ent, struct w1_family, family_entry); 421da177e4SLinus Torvalds 431da177e4SLinus Torvalds if (f->fid == newf->fid) { 441da177e4SLinus Torvalds ret = -EEXIST; 451da177e4SLinus Torvalds break; 461da177e4SLinus Torvalds } 471da177e4SLinus Torvalds } 481da177e4SLinus Torvalds 491da177e4SLinus Torvalds if (!ret) { 501da177e4SLinus Torvalds atomic_set(&newf->refcnt, 0); 511da177e4SLinus Torvalds newf->need_exit = 0; 521da177e4SLinus Torvalds list_add_tail(&newf->family_entry, &w1_families); 531da177e4SLinus Torvalds } 541da177e4SLinus Torvalds spin_unlock(&w1_flock); 551da177e4SLinus Torvalds 566adf87bdSEvgeniy Polyakov w1_reconnect_slaves(newf); 576adf87bdSEvgeniy Polyakov 581da177e4SLinus Torvalds return ret; 591da177e4SLinus Torvalds } 601da177e4SLinus Torvalds 611da177e4SLinus Torvalds void w1_unregister_family(struct w1_family *fent) 621da177e4SLinus Torvalds { 631da177e4SLinus Torvalds struct list_head *ent, *n; 641da177e4SLinus Torvalds struct w1_family *f; 651da177e4SLinus Torvalds 661da177e4SLinus Torvalds spin_lock(&w1_flock); 671da177e4SLinus Torvalds list_for_each_safe(ent, n, &w1_families) { 681da177e4SLinus Torvalds f = list_entry(ent, struct w1_family, family_entry); 691da177e4SLinus Torvalds 701da177e4SLinus Torvalds if (f->fid == fent->fid) { 711da177e4SLinus Torvalds list_del(&fent->family_entry); 721da177e4SLinus Torvalds break; 731da177e4SLinus Torvalds } 741da177e4SLinus Torvalds } 751da177e4SLinus Torvalds 761da177e4SLinus Torvalds fent->need_exit = 1; 771da177e4SLinus Torvalds 781da177e4SLinus Torvalds spin_unlock(&w1_flock); 791da177e4SLinus Torvalds 801da177e4SLinus Torvalds while (atomic_read(&fent->refcnt)) { 811da177e4SLinus Torvalds printk(KERN_INFO "Waiting for family %u to become free: refcnt=%d.\n", 821da177e4SLinus Torvalds fent->fid, atomic_read(&fent->refcnt)); 831da177e4SLinus Torvalds 841da177e4SLinus Torvalds if (msleep_interruptible(1000)) 851da177e4SLinus Torvalds flush_signals(current); 861da177e4SLinus Torvalds } 871da177e4SLinus Torvalds } 881da177e4SLinus Torvalds 891da177e4SLinus Torvalds /* 901da177e4SLinus Torvalds * Should be called under w1_flock held. 911da177e4SLinus Torvalds */ 921da177e4SLinus Torvalds struct w1_family * w1_family_registered(u8 fid) 931da177e4SLinus Torvalds { 941da177e4SLinus Torvalds struct list_head *ent, *n; 951da177e4SLinus Torvalds struct w1_family *f = NULL; 961da177e4SLinus Torvalds int ret = 0; 971da177e4SLinus Torvalds 981da177e4SLinus Torvalds list_for_each_safe(ent, n, &w1_families) { 991da177e4SLinus Torvalds f = list_entry(ent, struct w1_family, family_entry); 1001da177e4SLinus Torvalds 1011da177e4SLinus Torvalds if (f->fid == fid) { 1021da177e4SLinus Torvalds ret = 1; 1031da177e4SLinus Torvalds break; 1041da177e4SLinus Torvalds } 1051da177e4SLinus Torvalds } 1061da177e4SLinus Torvalds 1071da177e4SLinus Torvalds return (ret) ? f : NULL; 1081da177e4SLinus Torvalds } 1091da177e4SLinus Torvalds 110*70d484bfSEvgeniy Polyakov static void __w1_family_put(struct w1_family *f) 111*70d484bfSEvgeniy Polyakov { 112*70d484bfSEvgeniy Polyakov if (atomic_dec_and_test(&f->refcnt)) 113*70d484bfSEvgeniy Polyakov f->need_exit = 1; 114*70d484bfSEvgeniy Polyakov } 115*70d484bfSEvgeniy Polyakov 1161da177e4SLinus Torvalds void w1_family_put(struct w1_family *f) 1171da177e4SLinus Torvalds { 1181da177e4SLinus Torvalds spin_lock(&w1_flock); 1191da177e4SLinus Torvalds __w1_family_put(f); 1201da177e4SLinus Torvalds spin_unlock(&w1_flock); 1211da177e4SLinus Torvalds } 1221da177e4SLinus Torvalds 123*70d484bfSEvgeniy Polyakov #if 0 1241da177e4SLinus Torvalds void w1_family_get(struct w1_family *f) 1251da177e4SLinus Torvalds { 1261da177e4SLinus Torvalds spin_lock(&w1_flock); 1271da177e4SLinus Torvalds __w1_family_get(f); 1281da177e4SLinus Torvalds spin_unlock(&w1_flock); 1291da177e4SLinus Torvalds } 130*70d484bfSEvgeniy Polyakov #endif /* 0 */ 1311da177e4SLinus Torvalds 1321da177e4SLinus Torvalds void __w1_family_get(struct w1_family *f) 1331da177e4SLinus Torvalds { 1341da177e4SLinus Torvalds smp_mb__before_atomic_inc(); 1351da177e4SLinus Torvalds atomic_inc(&f->refcnt); 1361da177e4SLinus Torvalds smp_mb__after_atomic_inc(); 1371da177e4SLinus Torvalds } 1381da177e4SLinus Torvalds 1391da177e4SLinus Torvalds EXPORT_SYMBOL(w1_unregister_family); 1401da177e4SLinus Torvalds EXPORT_SYMBOL(w1_register_family); 141