1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (c) 2004 Evgeniy Polyakov <zbr@ioremap.net>
4 */
5
6#include <linux/kernel.h>
7#include <linux/list.h>
8#include <linux/delay.h>
9#include <linux/kthread.h>
10#include <linux/slab.h>
11#include <linux/sched/signal.h>
12#include <linux/export.h>
13#include <linux/moduleparam.h>
14
15#include "w1_internal.h"
16#include "w1_netlink.h"
17
18static int w1_search_count = -1; /* Default is continual scan */
19module_param_named(search_count, w1_search_count, int, 0);
20
21static int w1_enable_pullup = 1;
22module_param_named(enable_pullup, w1_enable_pullup, int, 0);
23
24static struct w1_master *w1_alloc_dev(u32 id, int slave_count, int slave_ttl,
25				       struct device_driver *driver,
26				       struct device *device)
27{
28	struct w1_master *dev;
29	int err;
30
31	/*
32	 * We are in process context(kernel thread), so can sleep.
33	 */
34	dev = kzalloc(sizeof(struct w1_master) + sizeof(struct w1_bus_master), GFP_KERNEL);
35	if (!dev) {
36		pr_err("Failed to allocate %zd bytes for new w1 device.\n",
37			sizeof(struct w1_master));
38		return NULL;
39	}
40
41
42	dev->bus_master = (struct w1_bus_master *)(dev + 1);
43
44	dev->owner		= THIS_MODULE;
45	dev->max_slave_count	= slave_count;
46	dev->slave_count	= 0;
47	dev->attempts		= 0;
48	dev->initialized	= 0;
49	dev->id			= id;
50	dev->slave_ttl		= slave_ttl;
51	dev->search_count	= w1_search_count;
52	dev->enable_pullup	= w1_enable_pullup;
53
54	/* For __w1_remove_master_device to decrement
55	 */
56	atomic_set(&dev->refcnt, 1);
57
58	INIT_LIST_HEAD(&dev->slist);
59	INIT_LIST_HEAD(&dev->async_list);
60	mutex_init(&dev->mutex);
61	mutex_init(&dev->bus_mutex);
62	mutex_init(&dev->list_mutex);
63
64	memcpy(&dev->dev, device, sizeof(struct device));
65	dev_set_name(&dev->dev, "w1_bus_master%u", dev->id);
66	snprintf(dev->name, sizeof(dev->name), "w1_bus_master%u", dev->id);
67	dev->dev.init_name = dev->name;
68
69	dev->driver = driver;
70
71	dev->seq = 1;
72
73	err = device_register(&dev->dev);
74	if (err) {
75		pr_err("Failed to register master device. err=%d\n", err);
76		put_device(&dev->dev);
77		dev = NULL;
78	}
79
80	return dev;
81}
82
83static void w1_free_dev(struct w1_master *dev)
84{
85	device_unregister(&dev->dev);
86}
87
88/**
89 * w1_add_master_device() - registers a new master device
90 * @master:	master bus device to register
91 */
92int w1_add_master_device(struct w1_bus_master *master)
93{
94	struct w1_master *dev, *entry;
95	int retval = 0;
96	struct w1_netlink_msg msg;
97	int id, found;
98
99	/* validate minimum functionality */
100	if (!(master->touch_bit && master->reset_bus) &&
101	    !(master->write_bit && master->read_bit) &&
102	    !(master->write_byte && master->read_byte && master->reset_bus)) {
103		pr_err("w1_add_master_device: invalid function set\n");
104		return(-EINVAL);
105	}
106
107	/* Lock until the device is added (or not) to w1_masters. */
108	mutex_lock(&w1_mlock);
109	/* Search for the first available id (starting at 1). */
110	id = 0;
111	do {
112		++id;
113		found = 0;
114		list_for_each_entry(entry, &w1_masters, w1_master_entry) {
115			if (entry->id == id) {
116				found = 1;
117				break;
118			}
119		}
120	} while (found);
121
122	dev = w1_alloc_dev(id, w1_max_slave_count, w1_max_slave_ttl,
123		&w1_master_driver, &w1_master_device);
124	if (!dev) {
125		mutex_unlock(&w1_mlock);
126		return -ENOMEM;
127	}
128
129	retval =  w1_create_master_attributes(dev);
130	if (retval) {
131		mutex_unlock(&w1_mlock);
132		goto err_out_free_dev;
133	}
134
135	memcpy(dev->bus_master, master, sizeof(struct w1_bus_master));
136
137	dev->initialized = 1;
138
139	dev->thread = kthread_run(&w1_process, dev, "%s", dev->name);
140	if (IS_ERR(dev->thread)) {
141		retval = PTR_ERR(dev->thread);
142		dev_err(&dev->dev,
143			 "Failed to create new kernel thread. err=%d\n",
144			 retval);
145		mutex_unlock(&w1_mlock);
146		goto err_out_rm_attr;
147	}
148
149	list_add(&dev->w1_master_entry, &w1_masters);
150	mutex_unlock(&w1_mlock);
151
152	memset(&msg, 0, sizeof(msg));
153	msg.id.mst.id = dev->id;
154	msg.type = W1_MASTER_ADD;
155	w1_netlink_send(dev, &msg);
156
157	return 0;
158
159#if 0 /* Thread cleanup code, not required currently. */
160err_out_kill_thread:
161	set_bit(W1_ABORT_SEARCH, &dev->flags);
162	kthread_stop(dev->thread);
163#endif
164err_out_rm_attr:
165	w1_destroy_master_attributes(dev);
166err_out_free_dev:
167	w1_free_dev(dev);
168
169	return retval;
170}
171EXPORT_SYMBOL(w1_add_master_device);
172
173void __w1_remove_master_device(struct w1_master *dev)
174{
175	struct w1_netlink_msg msg;
176	struct w1_slave *sl, *sln;
177
178	mutex_lock(&w1_mlock);
179	list_del(&dev->w1_master_entry);
180	mutex_unlock(&w1_mlock);
181
182	set_bit(W1_ABORT_SEARCH, &dev->flags);
183	kthread_stop(dev->thread);
184
185	mutex_lock(&dev->mutex);
186	mutex_lock(&dev->list_mutex);
187	list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
188		mutex_unlock(&dev->list_mutex);
189		w1_slave_detach(sl);
190		mutex_lock(&dev->list_mutex);
191	}
192	w1_destroy_master_attributes(dev);
193	mutex_unlock(&dev->list_mutex);
194	mutex_unlock(&dev->mutex);
195	atomic_dec(&dev->refcnt);
196
197	while (atomic_read(&dev->refcnt)) {
198		dev_info(&dev->dev, "Waiting for %s to become free: refcnt=%d.\n",
199				dev->name, atomic_read(&dev->refcnt));
200
201		if (msleep_interruptible(1000))
202			flush_signals(current);
203		mutex_lock(&dev->list_mutex);
204		w1_process_callbacks(dev);
205		mutex_unlock(&dev->list_mutex);
206	}
207	mutex_lock(&dev->list_mutex);
208	w1_process_callbacks(dev);
209	mutex_unlock(&dev->list_mutex);
210
211	memset(&msg, 0, sizeof(msg));
212	msg.id.mst.id = dev->id;
213	msg.type = W1_MASTER_REMOVE;
214	w1_netlink_send(dev, &msg);
215
216	w1_free_dev(dev);
217}
218
219/**
220 * w1_remove_master_device() - unregister a master device
221 * @bm:	master bus device to remove
222 */
223void w1_remove_master_device(struct w1_bus_master *bm)
224{
225	struct w1_master *dev, *found = NULL;
226
227	list_for_each_entry(dev, &w1_masters, w1_master_entry) {
228		if (!dev->initialized)
229			continue;
230
231		if (dev->bus_master->data == bm->data) {
232			found = dev;
233			break;
234		}
235	}
236
237	if (!found) {
238		pr_err("Device doesn't exist.\n");
239		return;
240	}
241
242	__w1_remove_master_device(found);
243}
244EXPORT_SYMBOL(w1_remove_master_device);
245