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

From: Tejun Heo (tj_at_home-tj.org)
Date: 11/04/04

  • Next message: Tejun Heo: "Re: [PATCH 2.6.10-rc1 2/4] driver-model: devparam expanded to accept direct per-device parameters via @args argument"
    Date:	Thu, 4 Nov 2004 16:46:28 +0900
    To: rusty@rustcorp.com.au, mochel@osdl.org, greg@kroah.com
    
    

     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@home-tj.org>

    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@vger.kernel.org
    More majordomo info at http://vger.kernel.org/majordomo-info.html
    Please read the FAQ at http://www.tux.org/lkml/


  • Next message: Tejun Heo: "Re: [PATCH 2.6.10-rc1 2/4] driver-model: devparam expanded to accept direct per-device parameters via @args argument"

    Relevant Pages

    • Re: [PATCH 2.6.10-rc1 4/4] driver-model: attach/detach sysfs node implemented
      ... > This patch implements device interface nodes attach and detach. ... > driver name attaches the device to the driver. ... This will not work for pretty much any bus but PCI because only PCI ...
      (Linux-Kernel)
    • Re: [PATCH 2.6.10-rc1 0/4] driver-model: manual device attach
      ... > Two files named attach and detach are created under each device's ... Reading attach node shows the name of applicable ... Writing a driver name attaches the device to the driver. ... Writing anything to the write-only detach node detaches the ...
      (Linux-Kernel)
    • [PATCH] Fix misspellings collected by members of KJ list.
      ... - * writting a Wavelan ISA driver for the MACH microkernel. ... there are two posibilities reading or writing. ... - * on non x86 architectures, so if you are writting portable code, ...
      (Linux-Kernel)
    • Re: MOUHID attaching problem
      ... driver passes AttachDevice function in CDevice.cpp. ... UHCD driver DLL attach ... InitializeUHCI ++ ... CHub(Root tier 0)::AllocateDeviceArray - attempting to allocate 1 devices ...
      (microsoft.public.windowsce.platbuilder)
    • [PATCH 2.6.10-rc1 0/4] driver-model: manual device attach
      ... These are the manual device attach patches I was talking about in the ... dev.autoattach is read/write integer sysctl node which controls ... driver-model's behavior regarding device - driver association. ...
      (Linux-Kernel)