Re: [PATCH 2.6.10-rc1 4/4] driver-model: attach/detach sysfs node implemented

From: Tejun Heo
Date: Thu Nov 04 2004 - 02:54:59 EST


ma_04_manual_attach.patch

This patch implements device interface nodes attach and detach.
Reading attach node shows the name of applicable drivers. Writing a
driver name attaches the device to the driver. Writing anything to
the write-only detach node detaches the driver from the currently
associated driver.


Signed-off-by: Tejun Heo <tj@xxxxxxxxxxx>


Index: linux-export/drivers/base/interface.c
===================================================================
--- linux-export.orig/drivers/base/interface.c 2004-11-04 11:04:15.000000000 +0900
+++ linux-export/drivers/base/interface.c 2004-11-04 11:04:15.000000000 +0900
@@ -13,6 +13,7 @@
#include <linux/err.h>
#include <linux/stat.h>
#include <linux/string.h>
+#include <linux/ctype.h>

/**
* detach_state - control the default power state for the device.
@@ -46,7 +47,113 @@ static ssize_t detach_state_store(struct
static DEVICE_ATTR(detach_state, 0644, detach_state_show, detach_state_store);


+/**
+ * attach - manually attaches the device to the specified driver
+ *
+ * When read, this node shows the list of the attachable drivers.
+ * Writing the name of a driver attaches the device to the
+ * driver.
+ */
+
+struct attach_show_arg {
+ struct device * dev;
+ char * buf;
+ size_t left;
+};
+
+static int attach_show_helper(struct device_driver * drv, void * void_arg)
+{
+ struct attach_show_arg * arg = void_arg;
+ int ret;
+
+ if (drv->bus->match(arg->dev, drv)) {
+ ret = snprintf(arg->buf, arg->left, "%s\n", drv->name);
+ if (ret >= arg->left)
+ return -ENOSPC;
+ arg->buf += ret;
+ arg->left -= ret;
+ }
+
+ return 0;
+}
+
+static ssize_t attach_show(struct device * dev, char * buf)
+{
+ struct attach_show_arg arg = { dev, buf, PAGE_SIZE };
+ int ret = 0;
+
+ if (dev->bus->match)
+ ret = bus_for_each_drv(dev->bus, NULL, &arg, attach_show_helper);
+
+ return ret ?: PAGE_SIZE - arg.left;
+}
+
+static int attach_store_helper(struct device_driver * drv, void * arg)
+{
+ const char * p = *(void **)arg;
+ int len;
+
+ len = strlen(drv->name);
+ if (!strncmp(drv->name, p, len) &&
+ (p[len] == '\0' || isspace(p[len]))) {
+ *(void **)(arg) = get_driver(drv);
+ return 1;
+ }
+
+ return 0;
+}
+
+static ssize_t attach_store(struct device * dev, const char * buf, size_t n)
+{
+ void * arg = (void *)buf;
+ struct device_driver *drv;
+ int error;
+
+ if (bus_for_each_drv(dev->bus, NULL, &arg, attach_store_helper) == 0)
+ return -ENOENT;
+ drv = arg;
+
+ /* Skip driver name */
+ while (*buf != '\0' && !isspace(*buf))
+ buf++;
+
+ /* Attach */
+ error = -EBUSY;
+ down_write(&dev->bus->subsys.rwsem);
+ if (dev->driver == NULL)
+ error = driver_probe_device(drv, dev, buf);
+ up_write(&dev->bus->subsys.rwsem);
+
+ if (error)
+ printk(KERN_WARNING "%s: probe of %s failed with error %d\n",
+ drv->name, dev->bus_id, error);
+
+ return error ?: n;
+}
+
+static DEVICE_ATTR(attach, 0644, attach_show, attach_store);
+
+
+/**
+ * detach - manually detaches the device from its associated driver.
+ *
+ * This is a write-only node. When any value is written, it detaches
+ * the device from its associated driver.
+ */
+static ssize_t detach_store(struct device * dev, const char * buf, size_t n)
+{
+ down_write(&dev->bus->subsys.rwsem);
+ device_release_driver(dev);
+ up_write(&dev->bus->subsys.rwsem);
+ return n;
+}
+
+static DEVICE_ATTR(detach, 0200, NULL, detach_store);
+
+
struct attribute * dev_default_attrs[] = {
&dev_attr_detach_state.attr,
+ &dev_attr_attach.attr,
+ &dev_attr_detach.attr,
NULL,
};
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/