Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * Copyright (c) 2004 Evgeniy Polyakov <zbr@ioremap.net>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) #include <linux/moduleparam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include <linux/timer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include <linux/kthread.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include <linux/freezer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include <linux/hwmon.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include <linux/atomic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #include "w1_internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #include "w1_netlink.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #define W1_FAMILY_DEFAULT	0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) static int w1_timeout = 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) module_param_named(timeout, w1_timeout, int, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) MODULE_PARM_DESC(timeout, "time in seconds between automatic slave searches");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) static int w1_timeout_us = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) module_param_named(timeout_us, w1_timeout_us, int, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) MODULE_PARM_DESC(timeout_us,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) 		 "time in microseconds between automatic slave searches");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) /* A search stops when w1_max_slave_count devices have been found in that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39)  * search.  The next search will start over and detect the same set of devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40)  * on a static 1-wire bus.  Memory is not allocated based on this number, just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41)  * on the number of devices known to the kernel.  Having a high number does not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42)  * consume additional resources.  As a special case, if there is only one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43)  * device on the network and w1_max_slave_count is set to 1, the device id can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44)  * be read directly skipping the normal slower search process.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) int w1_max_slave_count = 64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) module_param_named(max_slave_count, w1_max_slave_count, int, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) MODULE_PARM_DESC(max_slave_count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 	"maximum number of slaves detected in a search");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) int w1_max_slave_ttl = 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) module_param_named(slave_ttl, w1_max_slave_ttl, int, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) MODULE_PARM_DESC(slave_ttl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 	"Number of searches not seeing a slave before it will be removed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) DEFINE_MUTEX(w1_mlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) LIST_HEAD(w1_masters);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) static int w1_master_match(struct device *dev, struct device_driver *drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) static int w1_master_probe(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 	return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) static void w1_master_release(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 	struct w1_master *md = dev_to_w1_master(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 	dev_dbg(dev, "%s: Releasing %s.\n", __func__, md->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 	memset(md, 0, sizeof(struct w1_master) + sizeof(struct w1_bus_master));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 	kfree(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) static void w1_slave_release(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 	struct w1_slave *sl = dev_to_w1_slave(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 	dev_dbg(dev, "%s: Releasing %s [%p]\n", __func__, sl->name, sl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 	w1_family_put(sl->family);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 	sl->master->slave_count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) static ssize_t name_show(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 	struct w1_slave *sl = dev_to_w1_slave(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 	return sprintf(buf, "%s\n", sl->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) static DEVICE_ATTR_RO(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) static ssize_t id_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 	struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 	struct w1_slave *sl = dev_to_w1_slave(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 	ssize_t count = sizeof(sl->reg_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 	memcpy(buf, (u8 *)&sl->reg_num, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) static DEVICE_ATTR_RO(id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) static struct attribute *w1_slave_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 	&dev_attr_name.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 	&dev_attr_id.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 	NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) ATTRIBUTE_GROUPS(w1_slave);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) /* Default family */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) static ssize_t rw_write(struct file *filp, struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 			struct bin_attribute *bin_attr, char *buf, loff_t off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 			size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 	struct w1_slave *sl = kobj_to_w1_slave(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 	mutex_lock(&sl->master->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 	if (w1_reset_select_slave(sl)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 		count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 		goto out_up;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 	w1_write_block(sl->master, buf, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) out_up:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 	mutex_unlock(&sl->master->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) static ssize_t rw_read(struct file *filp, struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 		       struct bin_attribute *bin_attr, char *buf, loff_t off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 		       size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	struct w1_slave *sl = kobj_to_w1_slave(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 	mutex_lock(&sl->master->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	w1_read_block(sl->master, buf, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 	mutex_unlock(&sl->master->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) static BIN_ATTR_RW(rw, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) static struct bin_attribute *w1_slave_bin_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 	&bin_attr_rw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 	NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) static const struct attribute_group w1_slave_default_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	.bin_attrs = w1_slave_bin_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) static const struct attribute_group *w1_slave_default_groups[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 	&w1_slave_default_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 	NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) static const struct w1_family_ops w1_default_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 	.groups		= w1_slave_default_groups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) static struct w1_family w1_default_family = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 	.fops = &w1_default_fops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) static int w1_uevent(struct device *dev, struct kobj_uevent_env *env);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) static struct bus_type w1_bus_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 	.name = "w1",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 	.match = w1_master_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 	.uevent = w1_uevent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) struct device_driver w1_master_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 	.name = "w1_master_driver",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 	.bus = &w1_bus_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 	.probe = w1_master_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) struct device w1_master_device = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 	.parent = NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 	.bus = &w1_bus_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 	.init_name = "w1 bus master",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 	.driver = &w1_master_driver,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 	.release = &w1_master_release
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) static struct device_driver w1_slave_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 	.name = "w1_slave_driver",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 	.bus = &w1_bus_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) #if 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) struct device w1_slave_device = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 	.parent = NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 	.bus = &w1_bus_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 	.init_name = "w1 bus slave",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 	.driver = &w1_slave_driver,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 	.release = &w1_slave_release
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) #endif  /*  0  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) static ssize_t w1_master_attribute_show_name(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 	struct w1_master *md = dev_to_w1_master(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 	ssize_t count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 	mutex_lock(&md->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 	count = sprintf(buf, "%s\n", md->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 	mutex_unlock(&md->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) static ssize_t w1_master_attribute_store_search(struct device * dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 						struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 						const char * buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 	long tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 	struct w1_master *md = dev_to_w1_master(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 	ret = kstrtol(buf, 0, &tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 	mutex_lock(&md->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 	md->search_count = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 	mutex_unlock(&md->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 	/* Only wake if it is going to be searching. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 	if (tmp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 		wake_up_process(md->thread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) static ssize_t w1_master_attribute_show_search(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 					       struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 					       char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 	struct w1_master *md = dev_to_w1_master(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 	ssize_t count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 	mutex_lock(&md->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 	count = sprintf(buf, "%d\n", md->search_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 	mutex_unlock(&md->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) static ssize_t w1_master_attribute_store_pullup(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 						struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 						const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 	long tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 	struct w1_master *md = dev_to_w1_master(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 	ret = kstrtol(buf, 0, &tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 	mutex_lock(&md->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 	md->enable_pullup = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 	mutex_unlock(&md->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) static ssize_t w1_master_attribute_show_pullup(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 					       struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 					       char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 	struct w1_master *md = dev_to_w1_master(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 	ssize_t count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 	mutex_lock(&md->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 	count = sprintf(buf, "%d\n", md->enable_pullup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 	mutex_unlock(&md->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) static ssize_t w1_master_attribute_show_pointer(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 	struct w1_master *md = dev_to_w1_master(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 	ssize_t count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 	mutex_lock(&md->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 	count = sprintf(buf, "0x%p\n", md->bus_master);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 	mutex_unlock(&md->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) static ssize_t w1_master_attribute_show_timeout(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 	ssize_t count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 	count = sprintf(buf, "%d\n", w1_timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) static ssize_t w1_master_attribute_show_timeout_us(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 	struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 	ssize_t count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 	count = sprintf(buf, "%d\n", w1_timeout_us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) static ssize_t w1_master_attribute_store_max_slave_count(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 	struct device_attribute *attr, const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 	int tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 	struct w1_master *md = dev_to_w1_master(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 	if (kstrtoint(buf, 0, &tmp) || tmp < 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 	mutex_lock(&md->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 	md->max_slave_count = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 	/* allow each time the max_slave_count is updated */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 	clear_bit(W1_WARN_MAX_COUNT, &md->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 	mutex_unlock(&md->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) static ssize_t w1_master_attribute_show_max_slave_count(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	struct w1_master *md = dev_to_w1_master(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 	ssize_t count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 	mutex_lock(&md->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 	count = sprintf(buf, "%d\n", md->max_slave_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 	mutex_unlock(&md->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) static ssize_t w1_master_attribute_show_attempts(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 	struct w1_master *md = dev_to_w1_master(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 	ssize_t count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 	mutex_lock(&md->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 	count = sprintf(buf, "%lu\n", md->attempts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 	mutex_unlock(&md->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) static ssize_t w1_master_attribute_show_slave_count(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 	struct w1_master *md = dev_to_w1_master(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 	ssize_t count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 	mutex_lock(&md->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 	count = sprintf(buf, "%d\n", md->slave_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 	mutex_unlock(&md->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) static ssize_t w1_master_attribute_show_slaves(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 	struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 	struct w1_master *md = dev_to_w1_master(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	int c = PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 	struct list_head *ent, *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 	struct w1_slave *sl = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 	mutex_lock(&md->list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 	list_for_each_safe(ent, n, &md->slist) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 		sl = list_entry(ent, struct w1_slave, w1_slave_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 		c -= snprintf(buf + PAGE_SIZE - c, c, "%s\n", sl->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 	if (!sl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 		c -= snprintf(buf + PAGE_SIZE - c, c, "not found.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 	mutex_unlock(&md->list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	return PAGE_SIZE - c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) static ssize_t w1_master_attribute_show_add(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 	struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 	int c = PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 	c -= snprintf(buf+PAGE_SIZE - c, c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 		"write device id xx-xxxxxxxxxxxx to add slave\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 	return PAGE_SIZE - c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) static int w1_atoreg_num(struct device *dev, const char *buf, size_t count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 	struct w1_reg_num *rn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 	unsigned int family;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 	unsigned long long id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 	u64 rn64_le;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 	/* The CRC value isn't read from the user because the sysfs directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 	 * doesn't include it and most messages from the bus search don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 	 * print it either.  It would be unreasonable for the user to then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 	 * provide it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 	const char *error_msg = "bad slave string format, expecting "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 		"ff-dddddddddddd\n";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 	if (buf[2] != '-') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 		dev_err(dev, "%s", error_msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 	i = sscanf(buf, "%02x-%012llx", &family, &id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 	if (i != 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 		dev_err(dev, "%s", error_msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 	rn->family = family;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 	rn->id = id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 	rn64_le = cpu_to_le64(*(u64 *)rn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 	rn->crc = w1_calc_crc8((u8 *)&rn64_le, 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) #if 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 	dev_info(dev, "With CRC device is %02x.%012llx.%02x.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 		  rn->family, (unsigned long long)rn->id, rn->crc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) /* Searches the slaves in the w1_master and returns a pointer or NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438)  * Note: must not hold list_mutex
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) struct w1_slave *w1_slave_search_device(struct w1_master *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 	struct w1_reg_num *rn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 	struct w1_slave *sl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	mutex_lock(&dev->list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 	list_for_each_entry(sl, &dev->slist, w1_slave_entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 		if (sl->reg_num.family == rn->family &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 				sl->reg_num.id == rn->id &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 				sl->reg_num.crc == rn->crc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 			mutex_unlock(&dev->list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 			return sl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 	mutex_unlock(&dev->list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) static ssize_t w1_master_attribute_store_add(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 						struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 						const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 	struct w1_master *md = dev_to_w1_master(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 	struct w1_reg_num rn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 	struct w1_slave *sl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 	ssize_t result = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 	if (w1_atoreg_num(dev, buf, count, &rn))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 	mutex_lock(&md->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 	sl = w1_slave_search_device(md, &rn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 	/* It would be nice to do a targeted search one the one-wire bus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 	 * for the new device to see if it is out there or not.  But the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 	 * current search doesn't support that.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 	if (sl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 		dev_info(dev, "Device %s already exists\n", sl->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 		result = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 		w1_attach_slave_device(md, &rn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 	mutex_unlock(&md->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 	return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) static ssize_t w1_master_attribute_show_remove(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 	struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 	int c = PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 	c -= snprintf(buf+PAGE_SIZE - c, c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 		"write device id xx-xxxxxxxxxxxx to remove slave\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 	return PAGE_SIZE - c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) static ssize_t w1_master_attribute_store_remove(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 						struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 						const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 	struct w1_master *md = dev_to_w1_master(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 	struct w1_reg_num rn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 	struct w1_slave *sl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 	ssize_t result = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	if (w1_atoreg_num(dev, buf, count, &rn))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	mutex_lock(&md->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 	sl = w1_slave_search_device(md, &rn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 	if (sl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 		result = w1_slave_detach(sl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 		/* refcnt 0 means it was detached in the call */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 		if (result == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 			result = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 		dev_info(dev, "Device %02x-%012llx doesn't exists\n", rn.family,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 			(unsigned long long)rn.id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 		result = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 	mutex_unlock(&md->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 	return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) #define W1_MASTER_ATTR_RO(_name, _mode)				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 	struct device_attribute w1_master_attribute_##_name =	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 		__ATTR(w1_master_##_name, _mode,		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 		       w1_master_attribute_show_##_name, NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) #define W1_MASTER_ATTR_RW(_name, _mode)				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 	struct device_attribute w1_master_attribute_##_name =	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 		__ATTR(w1_master_##_name, _mode,		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 		       w1_master_attribute_show_##_name,	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 		       w1_master_attribute_store_##_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) static W1_MASTER_ATTR_RO(name, S_IRUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) static W1_MASTER_ATTR_RO(slaves, S_IRUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) static W1_MASTER_ATTR_RO(slave_count, S_IRUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) static W1_MASTER_ATTR_RW(max_slave_count, S_IRUGO | S_IWUSR | S_IWGRP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) static W1_MASTER_ATTR_RO(attempts, S_IRUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) static W1_MASTER_ATTR_RO(timeout, S_IRUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) static W1_MASTER_ATTR_RO(timeout_us, S_IRUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) static W1_MASTER_ATTR_RO(pointer, S_IRUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) static W1_MASTER_ATTR_RW(search, S_IRUGO | S_IWUSR | S_IWGRP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) static W1_MASTER_ATTR_RW(pullup, S_IRUGO | S_IWUSR | S_IWGRP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) static W1_MASTER_ATTR_RW(add, S_IRUGO | S_IWUSR | S_IWGRP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) static W1_MASTER_ATTR_RW(remove, S_IRUGO | S_IWUSR | S_IWGRP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) static struct attribute *w1_master_default_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 	&w1_master_attribute_name.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 	&w1_master_attribute_slaves.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 	&w1_master_attribute_slave_count.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 	&w1_master_attribute_max_slave_count.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 	&w1_master_attribute_attempts.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 	&w1_master_attribute_timeout.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 	&w1_master_attribute_timeout_us.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 	&w1_master_attribute_pointer.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 	&w1_master_attribute_search.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 	&w1_master_attribute_pullup.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 	&w1_master_attribute_add.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 	&w1_master_attribute_remove.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) static const struct attribute_group w1_master_defattr_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	.attrs = w1_master_default_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) int w1_create_master_attributes(struct w1_master *master)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 	return sysfs_create_group(&master->dev.kobj, &w1_master_defattr_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) void w1_destroy_master_attributes(struct w1_master *master)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 	sysfs_remove_group(&master->dev.kobj, &w1_master_defattr_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) static int w1_uevent(struct device *dev, struct kobj_uevent_env *env)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 	struct w1_master *md = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 	struct w1_slave *sl = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 	char *event_owner, *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 	if (dev->driver == &w1_master_driver) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 		md = container_of(dev, struct w1_master, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 		event_owner = "master";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 		name = md->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 	} else if (dev->driver == &w1_slave_driver) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 		sl = container_of(dev, struct w1_slave, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 		event_owner = "slave";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 		name = sl->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 		dev_dbg(dev, "Unknown event.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 	dev_dbg(dev, "Hotplug event for %s %s, bus_id=%s.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 			event_owner, name, dev_name(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 	if (dev->driver != &w1_slave_driver || !sl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 	err = add_uevent_var(env, "W1_FID=%02X", sl->reg_num.family);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 	err = add_uevent_var(env, "W1_SLAVE_ID=%024LX",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 			     (unsigned long long)sl->reg_num.id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) end:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) static int w1_family_notify(unsigned long action, struct w1_slave *sl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 	const struct w1_family_ops *fops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 	fops = sl->family->fops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 	if (!fops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 	switch (action) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 	case BUS_NOTIFY_ADD_DEVICE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 		/* if the family driver needs to initialize something... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 		if (fops->add_slave) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 			err = fops->add_slave(sl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 			if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 				dev_err(&sl->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 					"add_slave() call failed. err=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 					err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 				return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 		if (fops->groups) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 			err = sysfs_create_groups(&sl->dev.kobj, fops->groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 			if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 				dev_err(&sl->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 					"sysfs group creation failed. err=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 					err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 				return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 		if (IS_REACHABLE(CONFIG_HWMON) && fops->chip_info) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 			struct device *hwmon
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 				= hwmon_device_register_with_info(&sl->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 						"w1_slave_temp", sl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 						fops->chip_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 						NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 			if (IS_ERR(hwmon)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 				dev_warn(&sl->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 					 "could not create hwmon device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 				sl->hwmon = hwmon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 	case BUS_NOTIFY_DEL_DEVICE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 		if (IS_REACHABLE(CONFIG_HWMON) && fops->chip_info &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 			    sl->hwmon)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 			hwmon_device_unregister(sl->hwmon);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 		if (fops->remove_slave)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 			sl->family->fops->remove_slave(sl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 		if (fops->groups)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 			sysfs_remove_groups(&sl->dev.kobj, fops->groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) static int __w1_attach_slave_device(struct w1_slave *sl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 	sl->dev.parent = &sl->master->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 	sl->dev.driver = &w1_slave_driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 	sl->dev.bus = &w1_bus_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 	sl->dev.release = &w1_slave_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 	sl->dev.groups = w1_slave_groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 	sl->dev.of_node = of_find_matching_node(sl->master->dev.of_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 						sl->family->of_match_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 	dev_set_name(&sl->dev, "%02x-%012llx",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 		 (unsigned int) sl->reg_num.family,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 		 (unsigned long long) sl->reg_num.id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	snprintf(&sl->name[0], sizeof(sl->name),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 		 "%02x-%012llx",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 		 (unsigned int) sl->reg_num.family,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 		 (unsigned long long) sl->reg_num.id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 	dev_dbg(&sl->dev, "%s: registering %s as %p.\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 		dev_name(&sl->dev), sl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 	/* suppress for w1_family_notify before sending KOBJ_ADD */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 	dev_set_uevent_suppress(&sl->dev, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 	err = device_register(&sl->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 		dev_err(&sl->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 			"Device registration [%s] failed. err=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 			dev_name(&sl->dev), err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 		put_device(&sl->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 	w1_family_notify(BUS_NOTIFY_ADD_DEVICE, sl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 	dev_set_uevent_suppress(&sl->dev, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 	kobject_uevent(&sl->dev.kobj, KOBJ_ADD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 	mutex_lock(&sl->master->list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 	list_add_tail(&sl->w1_slave_entry, &sl->master->slist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	mutex_unlock(&sl->master->list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) int w1_attach_slave_device(struct w1_master *dev, struct w1_reg_num *rn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 	struct w1_slave *sl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 	struct w1_family *f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 	struct w1_netlink_msg msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 	sl = kzalloc(sizeof(struct w1_slave), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	if (!sl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 		dev_err(&dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 			 "%s: failed to allocate new slave device.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 			 __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 	sl->owner = THIS_MODULE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 	sl->master = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 	set_bit(W1_SLAVE_ACTIVE, &sl->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 	memset(&msg, 0, sizeof(msg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	memcpy(&sl->reg_num, rn, sizeof(sl->reg_num));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 	atomic_set(&sl->refcnt, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 	atomic_inc(&sl->master->refcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 	dev->slave_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 	dev_info(&dev->dev, "Attaching one wire slave %02x.%012llx crc %02x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 		  rn->family, (unsigned long long)rn->id, rn->crc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 	/* slave modules need to be loaded in a context with unlocked mutex */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 	mutex_unlock(&dev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 	request_module("w1-family-0x%02X", rn->family);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 	mutex_lock(&dev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 	spin_lock(&w1_flock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 	f = w1_family_registered(rn->family);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 	if (!f) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 		f= &w1_default_family;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 		dev_info(&dev->dev, "Family %x for %02x.%012llx.%02x is not registered.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 			  rn->family, rn->family,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 			  (unsigned long long)rn->id, rn->crc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 	__w1_family_get(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 	spin_unlock(&w1_flock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 	sl->family = f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 	err = __w1_attach_slave_device(sl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 		dev_err(&dev->dev, "%s: Attaching %s failed.\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 			 sl->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 		dev->slave_count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 		w1_family_put(sl->family);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 		atomic_dec(&sl->master->refcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 		kfree(sl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 	sl->ttl = dev->slave_ttl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 	memcpy(msg.id.id, rn, sizeof(msg.id));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 	msg.type = W1_SLAVE_ADD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 	w1_netlink_send(dev, &msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) int w1_unref_slave(struct w1_slave *sl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 	struct w1_master *dev = sl->master;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 	int refcnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 	mutex_lock(&dev->list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 	refcnt = atomic_sub_return(1, &sl->refcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 	if (refcnt == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 		struct w1_netlink_msg msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 		dev_dbg(&sl->dev, "%s: detaching %s [%p].\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 			sl->name, sl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 		list_del(&sl->w1_slave_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 		memset(&msg, 0, sizeof(msg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 		memcpy(msg.id.id, &sl->reg_num, sizeof(msg.id));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 		msg.type = W1_SLAVE_REMOVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 		w1_netlink_send(sl->master, &msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 		w1_family_notify(BUS_NOTIFY_DEL_DEVICE, sl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 		device_unregister(&sl->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 		#ifdef DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 		memset(sl, 0, sizeof(*sl));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 		#endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 		kfree(sl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 	atomic_dec(&dev->refcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 	mutex_unlock(&dev->list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 	return refcnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) int w1_slave_detach(struct w1_slave *sl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 	/* Only detach a slave once as it decreases the refcnt each time. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 	int destroy_now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 	mutex_lock(&sl->master->list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 	destroy_now = !test_bit(W1_SLAVE_DETACH, &sl->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 	set_bit(W1_SLAVE_DETACH, &sl->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 	mutex_unlock(&sl->master->list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 	if (destroy_now)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 		destroy_now = !w1_unref_slave(sl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 	return destroy_now ? 0 : -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) struct w1_master *w1_search_master_id(u32 id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 	struct w1_master *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 	int found = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 	mutex_lock(&w1_mlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 	list_for_each_entry(dev, &w1_masters, w1_master_entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 		if (dev->id == id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 			found = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 			atomic_inc(&dev->refcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 	mutex_unlock(&w1_mlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 	return (found)?dev:NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) struct w1_slave *w1_search_slave(struct w1_reg_num *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 	struct w1_master *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 	struct w1_slave *sl = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 	int found = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 	mutex_lock(&w1_mlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 	list_for_each_entry(dev, &w1_masters, w1_master_entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 		mutex_lock(&dev->list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 		list_for_each_entry(sl, &dev->slist, w1_slave_entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 			if (sl->reg_num.family == id->family &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 					sl->reg_num.id == id->id &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 					sl->reg_num.crc == id->crc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 				found = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 				atomic_inc(&dev->refcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 				atomic_inc(&sl->refcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 		mutex_unlock(&dev->list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 		if (found)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 	mutex_unlock(&w1_mlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 	return (found)?sl:NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) void w1_reconnect_slaves(struct w1_family *f, int attach)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 	struct w1_slave *sl, *sln;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 	struct w1_master *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 	mutex_lock(&w1_mlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 	list_for_each_entry(dev, &w1_masters, w1_master_entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 		dev_dbg(&dev->dev, "Reconnecting slaves in device %s "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 			"for family %02x.\n", dev->name, f->fid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 		mutex_lock(&dev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 		mutex_lock(&dev->list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 		list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 			/* If it is a new family, slaves with the default
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 			 * family driver and are that family will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 			 * connected.  If the family is going away, devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 			 * matching that family are reconneced.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 			if ((attach && sl->family->fid == W1_FAMILY_DEFAULT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 				&& sl->reg_num.family == f->fid) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 				(!attach && sl->family->fid == f->fid)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 				struct w1_reg_num rn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 				mutex_unlock(&dev->list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 				memcpy(&rn, &sl->reg_num, sizeof(rn));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 				/* If it was already in use let the automatic
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 				 * scan pick it up again later.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 				if (!w1_slave_detach(sl))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 					w1_attach_slave_device(dev, &rn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 				mutex_lock(&dev->list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 		dev_dbg(&dev->dev, "Reconnecting slaves in device %s "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 			"has been finished.\n", dev->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 		mutex_unlock(&dev->list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 		mutex_unlock(&dev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 	mutex_unlock(&w1_mlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) void w1_slave_found(struct w1_master *dev, u64 rn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 	struct w1_slave *sl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 	struct w1_reg_num *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 	u64 rn_le = cpu_to_le64(rn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 	atomic_inc(&dev->refcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 	tmp = (struct w1_reg_num *) &rn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 	sl = w1_slave_search_device(dev, tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 	if (sl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 		set_bit(W1_SLAVE_ACTIVE, &sl->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 		if (rn && tmp->crc == w1_calc_crc8((u8 *)&rn_le, 7))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 			w1_attach_slave_device(dev, tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 	atomic_dec(&dev->refcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938)  * w1_search() - Performs a ROM Search & registers any devices found.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939)  * @dev: The master device to search
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940)  * @search_type: W1_SEARCH to search all devices, or W1_ALARM_SEARCH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941)  * to return only devices in the alarmed state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942)  * @cb: Function to call when a device is found
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944)  * The 1-wire search is a simple binary tree search.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945)  * For each bit of the address, we read two bits and write one bit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946)  * The bit written will put to sleep all devies that don't match that bit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947)  * When the two reads differ, the direction choice is obvious.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948)  * When both bits are 0, we must choose a path to take.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949)  * When we can scan all 64 bits without having to choose a path, we are done.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951)  * See "Application note 187 1-wire search algorithm" at www.maxim-ic.com
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) void w1_search(struct w1_master *dev, u8 search_type, w1_slave_found_callback cb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 	u64 last_rn, rn, tmp64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 	int i, slave_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 	int last_zero, last_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 	int search_bit, desc_bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 	u8  triplet_ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 	search_bit = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 	rn = dev->search_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 	last_rn = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 	last_device = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 	last_zero = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 	desc_bit = 64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 	while ( !last_device && (slave_count++ < dev->max_slave_count) ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 		last_rn = rn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 		rn = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 		 * Reset bus and all 1-wire device state machines
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 		 * so they can respond to our requests.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 		 * Return 0 - device(s) present, 1 - no devices present.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 		mutex_lock(&dev->bus_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 		if (w1_reset_bus(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 			mutex_unlock(&dev->bus_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 			dev_dbg(&dev->dev, "No devices present on the wire.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 		/* Do fast search on single slave bus */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 		if (dev->max_slave_count == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 			int rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 			w1_write_8(dev, W1_READ_ROM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 			rv = w1_read_block(dev, (u8 *)&rn, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 			mutex_unlock(&dev->bus_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 			if (rv == 8 && rn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 				cb(dev, rn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 		/* Start the search */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 		w1_write_8(dev, search_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 		for (i = 0; i < 64; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 			/* Determine the direction/search bit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 			if (i == desc_bit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 				search_bit = 1;	  /* took the 0 path last time, so take the 1 path */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 			else if (i > desc_bit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 				search_bit = 0;	  /* take the 0 path on the next branch */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 				search_bit = ((last_rn >> i) & 0x1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 			/* Read two bits and write one bit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 			triplet_ret = w1_triplet(dev, search_bit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 			/* quit if no device responded */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 			if ( (triplet_ret & 0x03) == 0x03 )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 			/* If both directions were valid, and we took the 0 path... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 			if (triplet_ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 				last_zero = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 			/* extract the direction taken & update the device number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 			tmp64 = (triplet_ret >> 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 			rn |= (tmp64 << i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 			if (test_bit(W1_ABORT_SEARCH, &dev->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 				mutex_unlock(&dev->bus_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 				dev_dbg(&dev->dev, "Abort w1_search\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 				return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 		mutex_unlock(&dev->bus_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 		if ( (triplet_ret & 0x03) != 0x03 ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 			if ((desc_bit == last_zero) || (last_zero < 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 				last_device = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 				dev->search_id = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 				dev->search_id = rn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 			desc_bit = last_zero;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 			cb(dev, rn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 		if (!last_device && slave_count == dev->max_slave_count &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 			!test_bit(W1_WARN_MAX_COUNT, &dev->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 			/* Only max_slave_count will be scanned in a search,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 			 * but it will start where it left off next search
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 			 * until all ids are identified and then it will start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 			 * over.  A continued search will report the previous
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 			 * last id as the first id (provided it is still on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 			 * bus).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 			dev_info(&dev->dev, "%s: max_slave_count %d reached, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 				"will continue next search.\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 				dev->max_slave_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 			set_bit(W1_WARN_MAX_COUNT, &dev->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) void w1_search_process_cb(struct w1_master *dev, u8 search_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 	w1_slave_found_callback cb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 	struct w1_slave *sl, *sln;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 	mutex_lock(&dev->list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 	list_for_each_entry(sl, &dev->slist, w1_slave_entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 		clear_bit(W1_SLAVE_ACTIVE, &sl->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 	mutex_unlock(&dev->list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 	w1_search_devices(dev, search_type, cb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 	mutex_lock(&dev->list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 	list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 		if (!test_bit(W1_SLAVE_ACTIVE, &sl->flags) && !--sl->ttl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 			mutex_unlock(&dev->list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 			w1_slave_detach(sl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 			mutex_lock(&dev->list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 		else if (test_bit(W1_SLAVE_ACTIVE, &sl->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 			sl->ttl = dev->slave_ttl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 	mutex_unlock(&dev->list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 	if (dev->search_count > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 		dev->search_count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) static void w1_search_process(struct w1_master *dev, u8 search_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 	w1_search_process_cb(dev, search_type, w1_slave_found);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096)  * w1_process_callbacks() - execute each dev->async_list callback entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097)  * @dev: w1_master device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099)  * The w1 master list_mutex must be held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101)  * Return: 1 if there were commands to executed 0 otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) int w1_process_callbacks(struct w1_master *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 	struct w1_async_cmd *async_cmd, *async_n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 	/* The list can be added to in another thread, loop until it is empty */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 	while (!list_empty(&dev->async_list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 		list_for_each_entry_safe(async_cmd, async_n, &dev->async_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 			async_entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 			/* drop the lock, if it is a search it can take a long
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 			 * time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 			mutex_unlock(&dev->list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 			async_cmd->cb(dev, async_cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 			ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 			mutex_lock(&dev->list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) int w1_process(void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 	struct w1_master *dev = (struct w1_master *) data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 	/* As long as w1_timeout is only set by a module parameter the sleep
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 	 * time can be calculated in jiffies once.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 	const unsigned long jtime =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 	  usecs_to_jiffies(w1_timeout * 1000000 + w1_timeout_us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 	/* remainder if it woke up early */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 	unsigned long jremain = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 	for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 		if (!jremain && dev->search_count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 			mutex_lock(&dev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 			w1_search_process(dev, W1_SEARCH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 			mutex_unlock(&dev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 		mutex_lock(&dev->list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 		/* Note, w1_process_callback drops the lock while processing,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 		 * but locks it again before returning.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 		if (!w1_process_callbacks(dev) && jremain) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 			/* a wake up is either to stop the thread, process
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 			 * callbacks, or search, it isn't process callbacks, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 			 * schedule a search.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 			jremain = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 		__set_current_state(TASK_INTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 		/* hold list_mutex until after interruptible to prevent loosing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 		 * the wakeup signal when async_cmd is added.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 		mutex_unlock(&dev->list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 		if (kthread_should_stop())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 		/* Only sleep when the search is active. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 		if (dev->search_count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 			if (!jremain)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 				jremain = jtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 			jremain = schedule_timeout(jremain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 			schedule();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 	atomic_dec(&dev->refcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) static int __init w1_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 	pr_info("Driver for 1-wire Dallas network protocol.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 	w1_init_netlink();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 	retval = bus_register(&w1_bus_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 	if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 		pr_err("Failed to register bus. err=%d.\n", retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 		goto err_out_exit_init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 	retval = driver_register(&w1_master_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 	if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 		pr_err("Failed to register master driver. err=%d.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 			retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 		goto err_out_bus_unregister;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 	retval = driver_register(&w1_slave_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 	if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 		pr_err("Failed to register slave driver. err=%d.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 			retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 		goto err_out_master_unregister;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) #if 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) /* For undoing the slave register if there was a step after it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) err_out_slave_unregister:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 	driver_unregister(&w1_slave_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) err_out_master_unregister:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 	driver_unregister(&w1_master_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) err_out_bus_unregister:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 	bus_unregister(&w1_bus_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) err_out_exit_init:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) static void __exit w1_fini(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 	struct w1_master *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 	/* Set netlink removal messages and some cleanup */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 	list_for_each_entry(dev, &w1_masters, w1_master_entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 		__w1_remove_master_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 	w1_fini_netlink();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 	driver_unregister(&w1_slave_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 	driver_unregister(&w1_master_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 	bus_unregister(&w1_bus_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) module_init(w1_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) module_exit(w1_fini);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) MODULE_LICENSE("GPL");