Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Copyright (c) 2004 Evgeniy Polyakov <zbr@ioremap.net>
0004  */
0005 
0006 #include <linux/kernel.h>
0007 #include <linux/list.h>
0008 #include <linux/delay.h>
0009 #include <linux/kthread.h>
0010 #include <linux/slab.h>
0011 #include <linux/sched/signal.h>
0012 #include <linux/export.h>
0013 #include <linux/moduleparam.h>
0014 
0015 #include "w1_internal.h"
0016 #include "w1_netlink.h"
0017 
0018 static int w1_search_count = -1; /* Default is continual scan */
0019 module_param_named(search_count, w1_search_count, int, 0);
0020 
0021 static int w1_enable_pullup = 1;
0022 module_param_named(enable_pullup, w1_enable_pullup, int, 0);
0023 
0024 static struct w1_master *w1_alloc_dev(u32 id, int slave_count, int slave_ttl,
0025                        struct device_driver *driver,
0026                        struct device *device)
0027 {
0028     struct w1_master *dev;
0029     int err;
0030 
0031     /*
0032      * We are in process context(kernel thread), so can sleep.
0033      */
0034     dev = kzalloc(sizeof(struct w1_master) + sizeof(struct w1_bus_master), GFP_KERNEL);
0035     if (!dev) {
0036         pr_err("Failed to allocate %zd bytes for new w1 device.\n",
0037             sizeof(struct w1_master));
0038         return NULL;
0039     }
0040 
0041 
0042     dev->bus_master = (struct w1_bus_master *)(dev + 1);
0043 
0044     dev->owner      = THIS_MODULE;
0045     dev->max_slave_count    = slave_count;
0046     dev->slave_count    = 0;
0047     dev->attempts       = 0;
0048     dev->initialized    = 0;
0049     dev->id         = id;
0050     dev->slave_ttl      = slave_ttl;
0051     dev->search_count   = w1_search_count;
0052     dev->enable_pullup  = w1_enable_pullup;
0053 
0054     /* 1 for w1_process to decrement
0055      * 1 for __w1_remove_master_device to decrement
0056      */
0057     atomic_set(&dev->refcnt, 2);
0058 
0059     INIT_LIST_HEAD(&dev->slist);
0060     INIT_LIST_HEAD(&dev->async_list);
0061     mutex_init(&dev->mutex);
0062     mutex_init(&dev->bus_mutex);
0063     mutex_init(&dev->list_mutex);
0064 
0065     memcpy(&dev->dev, device, sizeof(struct device));
0066     dev_set_name(&dev->dev, "w1_bus_master%u", dev->id);
0067     snprintf(dev->name, sizeof(dev->name), "w1_bus_master%u", dev->id);
0068     dev->dev.init_name = dev->name;
0069 
0070     dev->driver = driver;
0071 
0072     dev->seq = 1;
0073 
0074     err = device_register(&dev->dev);
0075     if (err) {
0076         pr_err("Failed to register master device. err=%d\n", err);
0077         put_device(&dev->dev);
0078         dev = NULL;
0079     }
0080 
0081     return dev;
0082 }
0083 
0084 static void w1_free_dev(struct w1_master *dev)
0085 {
0086     device_unregister(&dev->dev);
0087 }
0088 
0089 /**
0090  * w1_add_master_device() - registers a new master device
0091  * @master: master bus device to register
0092  */
0093 int w1_add_master_device(struct w1_bus_master *master)
0094 {
0095     struct w1_master *dev, *entry;
0096     int retval = 0;
0097     struct w1_netlink_msg msg;
0098     int id, found;
0099 
0100     /* validate minimum functionality */
0101     if (!(master->touch_bit && master->reset_bus) &&
0102         !(master->write_bit && master->read_bit) &&
0103         !(master->write_byte && master->read_byte && master->reset_bus)) {
0104         pr_err("w1_add_master_device: invalid function set\n");
0105         return(-EINVAL);
0106     }
0107 
0108     /* Lock until the device is added (or not) to w1_masters. */
0109     mutex_lock(&w1_mlock);
0110     /* Search for the first available id (starting at 1). */
0111     id = 0;
0112     do {
0113         ++id;
0114         found = 0;
0115         list_for_each_entry(entry, &w1_masters, w1_master_entry) {
0116             if (entry->id == id) {
0117                 found = 1;
0118                 break;
0119             }
0120         }
0121     } while (found);
0122 
0123     dev = w1_alloc_dev(id, w1_max_slave_count, w1_max_slave_ttl,
0124         &w1_master_driver, &w1_master_device);
0125     if (!dev) {
0126         mutex_unlock(&w1_mlock);
0127         return -ENOMEM;
0128     }
0129 
0130     retval =  w1_create_master_attributes(dev);
0131     if (retval) {
0132         mutex_unlock(&w1_mlock);
0133         goto err_out_free_dev;
0134     }
0135 
0136     memcpy(dev->bus_master, master, sizeof(struct w1_bus_master));
0137 
0138     dev->initialized = 1;
0139 
0140     dev->thread = kthread_run(&w1_process, dev, "%s", dev->name);
0141     if (IS_ERR(dev->thread)) {
0142         retval = PTR_ERR(dev->thread);
0143         dev_err(&dev->dev,
0144              "Failed to create new kernel thread. err=%d\n",
0145              retval);
0146         mutex_unlock(&w1_mlock);
0147         goto err_out_rm_attr;
0148     }
0149 
0150     list_add(&dev->w1_master_entry, &w1_masters);
0151     mutex_unlock(&w1_mlock);
0152 
0153     memset(&msg, 0, sizeof(msg));
0154     msg.id.mst.id = dev->id;
0155     msg.type = W1_MASTER_ADD;
0156     w1_netlink_send(dev, &msg);
0157 
0158     return 0;
0159 
0160 #if 0 /* Thread cleanup code, not required currently. */
0161 err_out_kill_thread:
0162     set_bit(W1_ABORT_SEARCH, &dev->flags);
0163     kthread_stop(dev->thread);
0164 #endif
0165 err_out_rm_attr:
0166     w1_destroy_master_attributes(dev);
0167 err_out_free_dev:
0168     w1_free_dev(dev);
0169 
0170     return retval;
0171 }
0172 EXPORT_SYMBOL(w1_add_master_device);
0173 
0174 void __w1_remove_master_device(struct w1_master *dev)
0175 {
0176     struct w1_netlink_msg msg;
0177     struct w1_slave *sl, *sln;
0178 
0179     mutex_lock(&w1_mlock);
0180     list_del(&dev->w1_master_entry);
0181     mutex_unlock(&w1_mlock);
0182 
0183     set_bit(W1_ABORT_SEARCH, &dev->flags);
0184     kthread_stop(dev->thread);
0185 
0186     mutex_lock(&dev->mutex);
0187     mutex_lock(&dev->list_mutex);
0188     list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
0189         mutex_unlock(&dev->list_mutex);
0190         w1_slave_detach(sl);
0191         mutex_lock(&dev->list_mutex);
0192     }
0193     w1_destroy_master_attributes(dev);
0194     mutex_unlock(&dev->list_mutex);
0195     mutex_unlock(&dev->mutex);
0196     atomic_dec(&dev->refcnt);
0197 
0198     while (atomic_read(&dev->refcnt)) {
0199         dev_info(&dev->dev, "Waiting for %s to become free: refcnt=%d.\n",
0200                 dev->name, atomic_read(&dev->refcnt));
0201 
0202         if (msleep_interruptible(1000))
0203             flush_signals(current);
0204         mutex_lock(&dev->list_mutex);
0205         w1_process_callbacks(dev);
0206         mutex_unlock(&dev->list_mutex);
0207     }
0208     mutex_lock(&dev->list_mutex);
0209     w1_process_callbacks(dev);
0210     mutex_unlock(&dev->list_mutex);
0211 
0212     memset(&msg, 0, sizeof(msg));
0213     msg.id.mst.id = dev->id;
0214     msg.type = W1_MASTER_REMOVE;
0215     w1_netlink_send(dev, &msg);
0216 
0217     w1_free_dev(dev);
0218 }
0219 
0220 /**
0221  * w1_remove_master_device() - unregister a master device
0222  * @bm: master bus device to remove
0223  */
0224 void w1_remove_master_device(struct w1_bus_master *bm)
0225 {
0226     struct w1_master *dev, *found = NULL;
0227 
0228     list_for_each_entry(dev, &w1_masters, w1_master_entry) {
0229         if (!dev->initialized)
0230             continue;
0231 
0232         if (dev->bus_master->data == bm->data) {
0233             found = dev;
0234             break;
0235         }
0236     }
0237 
0238     if (!found) {
0239         pr_err("Device doesn't exist.\n");
0240         return;
0241     }
0242 
0243     __w1_remove_master_device(found);
0244 }
0245 EXPORT_SYMBOL(w1_remove_master_device);