2005-04-16 15:20:36 -07:00
|
|
|
/*
|
2005-05-20 11:33:25 -07:00
|
|
|
* w1_int.c
|
2005-04-16 15:20:36 -07:00
|
|
|
*
|
|
|
|
* Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru>
|
2005-05-20 11:33:25 -07:00
|
|
|
*
|
2005-04-16 15:20:36 -07:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/list.h>
|
|
|
|
#include <linux/delay.h>
|
2006-02-20 01:15:37 -07:00
|
|
|
#include <linux/kthread.h>
|
2005-04-16 15:20:36 -07:00
|
|
|
|
|
|
|
#include "w1.h"
|
|
|
|
#include "w1_log.h"
|
|
|
|
#include "w1_netlink.h"
|
2006-04-23 23:46:14 -07:00
|
|
|
#include "w1_int.h"
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2008-10-15 22:04:45 -07:00
|
|
|
static int w1_search_count = -1; /* Default is continual scan */
|
|
|
|
module_param_named(search_count, w1_search_count, int, 0);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2008-10-15 22:04:42 -07:00
|
|
|
static int w1_enable_pullup = 1;
|
|
|
|
module_param_named(enable_pullup, w1_enable_pullup, int, 0);
|
|
|
|
|
2005-05-20 11:33:25 -07:00
|
|
|
static struct w1_master * w1_alloc_dev(u32 id, int slave_count, int slave_ttl,
|
|
|
|
struct device_driver *driver,
|
|
|
|
struct device *device)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
|
|
|
struct w1_master *dev;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We are in process context(kernel thread), so can sleep.
|
|
|
|
*/
|
some kmalloc/memset ->kzalloc (tree wide)
Transform some calls to kmalloc/memset to a single kzalloc (or kcalloc).
Here is a short excerpt of the semantic patch performing
this transformation:
@@
type T2;
expression x;
identifier f,fld;
expression E;
expression E1,E2;
expression e1,e2,e3,y;
statement S;
@@
x =
- kmalloc
+ kzalloc
(E1,E2)
... when != \(x->fld=E;\|y=f(...,x,...);\|f(...,x,...);\|x=E;\|while(...) S\|for(e1;e2;e3) S\)
- memset((T2)x,0,E1);
@@
expression E1,E2,E3;
@@
- kzalloc(E1 * E2,E3)
+ kcalloc(E1,E2,E3)
[akpm@linux-foundation.org: get kcalloc args the right way around]
Signed-off-by: Yoann Padioleau <padator@wanadoo.fr>
Cc: Richard Henderson <rth@twiddle.net>
Cc: Ivan Kokshaysky <ink@jurassic.park.msu.ru>
Acked-by: Russell King <rmk@arm.linux.org.uk>
Cc: Bryan Wu <bryan.wu@analog.com>
Acked-by: Jiri Slaby <jirislaby@gmail.com>
Cc: Dave Airlie <airlied@linux.ie>
Acked-by: Roland Dreier <rolandd@cisco.com>
Cc: Jiri Kosina <jkosina@suse.cz>
Acked-by: Dmitry Torokhov <dtor@mail.ru>
Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Acked-by: Mauro Carvalho Chehab <mchehab@infradead.org>
Acked-by: Pierre Ossman <drzeus-list@drzeus.cx>
Cc: Jeff Garzik <jeff@garzik.org>
Cc: "David S. Miller" <davem@davemloft.net>
Acked-by: Greg KH <greg@kroah.com>
Cc: James Bottomley <James.Bottomley@steeleye.com>
Cc: "Antonino A. Daplas" <adaplas@pol.net>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2007-07-19 01:49:03 -07:00
|
|
|
dev = kzalloc(sizeof(struct w1_master) + sizeof(struct w1_bus_master), GFP_KERNEL);
|
2005-04-16 15:20:36 -07:00
|
|
|
if (!dev) {
|
|
|
|
printk(KERN_ERR
|
|
|
|
"Failed to allocate %zd bytes for new w1 device.\n",
|
|
|
|
sizeof(struct w1_master));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
dev->bus_master = (struct w1_bus_master *)(dev + 1);
|
|
|
|
|
2005-05-20 11:33:25 -07:00
|
|
|
dev->owner = THIS_MODULE;
|
|
|
|
dev->max_slave_count = slave_count;
|
|
|
|
dev->slave_count = 0;
|
|
|
|
dev->attempts = 0;
|
|
|
|
dev->initialized = 0;
|
|
|
|
dev->id = id;
|
2005-04-16 15:20:36 -07:00
|
|
|
dev->slave_ttl = slave_ttl;
|
2008-10-15 22:04:45 -07:00
|
|
|
dev->search_count = w1_search_count;
|
2008-10-15 22:04:42 -07:00
|
|
|
dev->enable_pullup = w1_enable_pullup;
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2008-10-15 22:04:40 -07:00
|
|
|
/* 1 for w1_process to decrement
|
|
|
|
* 1 for __w1_remove_master_device to decrement
|
|
|
|
*/
|
2005-04-16 15:20:36 -07:00
|
|
|
atomic_set(&dev->refcnt, 2);
|
|
|
|
|
|
|
|
INIT_LIST_HEAD(&dev->slist);
|
2006-04-03 01:04:27 -07:00
|
|
|
mutex_init(&dev->mutex);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
|
|
|
memcpy(&dev->dev, device, sizeof(struct device));
|
2009-01-06 11:44:34 -07:00
|
|
|
dev_set_name(&dev->dev, "w1_bus_master%u", dev->id);
|
2005-04-16 15:20:36 -07:00
|
|
|
snprintf(dev->name, sizeof(dev->name), "w1_bus_master%u", dev->id);
|
|
|
|
|
|
|
|
dev->driver = driver;
|
|
|
|
|
|
|
|
dev->seq = 1;
|
|
|
|
|
|
|
|
err = device_register(&dev->dev);
|
|
|
|
if (err) {
|
|
|
|
printk(KERN_ERR "Failed to register master device. err=%d\n", err);
|
|
|
|
memset(dev, 0, sizeof(struct w1_master));
|
|
|
|
kfree(dev);
|
|
|
|
dev = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return dev;
|
|
|
|
}
|
|
|
|
|
2006-04-04 09:35:22 -07:00
|
|
|
static void w1_free_dev(struct w1_master *dev)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
|
|
|
device_unregister(&dev->dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
int w1_add_master_device(struct w1_bus_master *master)
|
|
|
|
{
|
2008-10-15 22:04:53 -07:00
|
|
|
struct w1_master *dev, *entry;
|
2005-04-16 15:20:36 -07:00
|
|
|
int retval = 0;
|
|
|
|
struct w1_netlink_msg msg;
|
2008-10-15 22:04:53 -07:00
|
|
|
int id, found;
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2005-06-03 14:21:46 -07:00
|
|
|
/* validate minimum functionality */
|
|
|
|
if (!(master->touch_bit && master->reset_bus) &&
|
2007-05-08 00:31:20 -07:00
|
|
|
!(master->write_bit && master->read_bit) &&
|
|
|
|
!(master->write_byte && master->read_byte && master->reset_bus)) {
|
2005-06-03 14:29:25 -07:00
|
|
|
printk(KERN_ERR "w1_add_master_device: invalid function set\n");
|
|
|
|
return(-EINVAL);
|
2005-06-03 14:21:46 -07:00
|
|
|
}
|
2008-10-15 22:04:42 -07:00
|
|
|
/* While it would be electrically possible to make a device that
|
|
|
|
* generated a strong pullup in bit bang mode, only hardare that
|
|
|
|
* controls 1-wire time frames are even expected to support a strong
|
|
|
|
* pullup. w1_io.c would need to support calling set_pullup before
|
|
|
|
* the last write_bit operation of a w1_write_8 which it currently
|
|
|
|
* doesn't.
|
|
|
|
*/
|
|
|
|
if (!master->write_byte && !master->touch_bit && master->set_pullup) {
|
|
|
|
printk(KERN_ERR "w1_add_master_device: set_pullup requires "
|
|
|
|
"write_byte or touch_bit, disabling\n");
|
|
|
|
master->set_pullup = NULL;
|
|
|
|
}
|
2005-06-03 14:21:46 -07:00
|
|
|
|
2008-10-15 22:04:53 -07:00
|
|
|
/* Lock until the device is added (or not) to w1_masters. */
|
|
|
|
mutex_lock(&w1_mlock);
|
|
|
|
/* Search for the first available id (starting at 1). */
|
|
|
|
id = 0;
|
|
|
|
do {
|
|
|
|
++id;
|
|
|
|
found = 0;
|
|
|
|
list_for_each_entry(entry, &w1_masters, w1_master_entry) {
|
|
|
|
if (entry->id == id) {
|
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (found);
|
|
|
|
|
|
|
|
dev = w1_alloc_dev(id, w1_max_slave_count, w1_max_slave_ttl,
|
|
|
|
&w1_master_driver, &w1_master_device);
|
|
|
|
if (!dev) {
|
|
|
|
mutex_unlock(&w1_mlock);
|
2005-04-16 15:20:36 -07:00
|
|
|
return -ENOMEM;
|
2008-10-15 22:04:53 -07:00
|
|
|
}
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2008-10-15 22:04:40 -07:00
|
|
|
retval = w1_create_master_attributes(dev);
|
2008-10-15 22:04:53 -07:00
|
|
|
if (retval) {
|
|
|
|
mutex_unlock(&w1_mlock);
|
2008-10-15 22:04:40 -07:00
|
|
|
goto err_out_free_dev;
|
2008-10-15 22:04:53 -07:00
|
|
|
}
|
2008-10-15 22:04:40 -07:00
|
|
|
|
|
|
|
memcpy(dev->bus_master, master, sizeof(struct w1_bus_master));
|
|
|
|
|
|
|
|
dev->initialized = 1;
|
|
|
|
|
2006-02-20 01:15:37 -07:00
|
|
|
dev->thread = kthread_run(&w1_process, dev, "%s", dev->name);
|
|
|
|
if (IS_ERR(dev->thread)) {
|
|
|
|
retval = PTR_ERR(dev->thread);
|
2005-04-16 15:20:36 -07:00
|
|
|
dev_err(&dev->dev,
|
|
|
|
"Failed to create new kernel thread. err=%d\n",
|
2006-02-20 01:15:37 -07:00
|
|
|
retval);
|
2008-10-15 22:04:53 -07:00
|
|
|
mutex_unlock(&w1_mlock);
|
2008-10-15 22:04:40 -07:00
|
|
|
goto err_out_rm_attr;
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
list_add(&dev->w1_master_entry, &w1_masters);
|
2006-04-03 01:04:27 -07:00
|
|
|
mutex_unlock(&w1_mlock);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2006-03-23 09:11:58 -07:00
|
|
|
memset(&msg, 0, sizeof(msg));
|
2005-04-16 15:20:36 -07:00
|
|
|
msg.id.mst.id = dev->id;
|
|
|
|
msg.type = W1_MASTER_ADD;
|
|
|
|
w1_netlink_send(dev, &msg);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
2008-10-15 22:04:40 -07:00
|
|
|
#if 0 /* Thread cleanup code, not required currently. */
|
2005-04-16 15:20:36 -07:00
|
|
|
err_out_kill_thread:
|
2006-02-20 01:15:37 -07:00
|
|
|
kthread_stop(dev->thread);
|
2008-10-15 22:04:40 -07:00
|
|
|
#endif
|
|
|
|
err_out_rm_attr:
|
|
|
|
w1_destroy_master_attributes(dev);
|
2005-04-16 15:20:36 -07:00
|
|
|
err_out_free_dev:
|
|
|
|
w1_free_dev(dev);
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
void __w1_remove_master_device(struct w1_master *dev)
|
|
|
|
{
|
|
|
|
struct w1_netlink_msg msg;
|
2008-10-15 22:04:38 -07:00
|
|
|
struct w1_slave *sl, *sln;
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2006-02-20 01:15:37 -07:00
|
|
|
kthread_stop(dev->thread);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2008-10-15 22:04:38 -07:00
|
|
|
mutex_lock(&w1_mlock);
|
|
|
|
list_del(&dev->w1_master_entry);
|
|
|
|
mutex_unlock(&w1_mlock);
|
|
|
|
|
|
|
|
mutex_lock(&dev->mutex);
|
|
|
|
list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry)
|
|
|
|
w1_slave_detach(sl);
|
|
|
|
w1_destroy_master_attributes(dev);
|
|
|
|
mutex_unlock(&dev->mutex);
|
|
|
|
atomic_dec(&dev->refcnt);
|
|
|
|
|
2005-04-16 15:20:36 -07:00
|
|
|
while (atomic_read(&dev->refcnt)) {
|
2006-02-20 01:15:37 -07:00
|
|
|
dev_info(&dev->dev, "Waiting for %s to become free: refcnt=%d.\n",
|
2005-04-16 15:20:36 -07:00
|
|
|
dev->name, atomic_read(&dev->refcnt));
|
|
|
|
|
|
|
|
if (msleep_interruptible(1000))
|
|
|
|
flush_signals(current);
|
|
|
|
}
|
|
|
|
|
2006-03-23 09:11:58 -07:00
|
|
|
memset(&msg, 0, sizeof(msg));
|
2005-04-16 15:20:36 -07:00
|
|
|
msg.id.mst.id = dev->id;
|
|
|
|
msg.type = W1_MASTER_REMOVE;
|
|
|
|
w1_netlink_send(dev, &msg);
|
|
|
|
|
|
|
|
w1_free_dev(dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
void w1_remove_master_device(struct w1_bus_master *bm)
|
|
|
|
{
|
2007-08-22 14:01:51 -07:00
|
|
|
struct w1_master *dev, *found = NULL;
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2005-05-20 11:33:25 -07:00
|
|
|
list_for_each_entry(dev, &w1_masters, w1_master_entry) {
|
2005-04-16 15:20:36 -07:00
|
|
|
if (!dev->initialized)
|
|
|
|
continue;
|
|
|
|
|
2007-08-22 14:01:51 -07:00
|
|
|
if (dev->bus_master->data == bm->data) {
|
|
|
|
found = dev;
|
2005-04-16 15:20:36 -07:00
|
|
|
break;
|
2007-08-22 14:01:51 -07:00
|
|
|
}
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
|
|
|
|
2007-08-22 14:01:51 -07:00
|
|
|
if (!found) {
|
2005-04-16 15:20:36 -07:00
|
|
|
printk(KERN_ERR "Device doesn't exist.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-08-22 14:01:51 -07:00
|
|
|
__w1_remove_master_device(found);
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT_SYMBOL(w1_add_master_device);
|
|
|
|
EXPORT_SYMBOL(w1_remove_master_device);
|