Re: RFC: Code to snatch a device from a generic driver

From: Greg KH
Date: Tue Jan 11 2005 - 14:40:43 EST


On Tue, Jan 11, 2005 at 01:10:50PM +1030, Ron wrote:
>
> Hi,
>
> We're presently working on enabling the cpad[1] and wacom kernel
> modules to retrieve their particular devices from a more generic
> driver that may have already claimed them, without resorting to
> patching other drivers as we see with the quirks in usbhid.
> In 2.6 we can no longer even pull the 'add below hid' stunt
> that got us by from userspace in 2.4 [2] -- however the new driver
> core model as I understand it seems like it should be able to
> handle this very nicely from within the module itself.
>
> The following code (derived from a patch sent to me by Jan
> Steinhoff) seems to do the job, but is surely not correct yet.

No, try something like the following. It creates a sysfs file that you
can write to to unbind the device manually.

The binding a driver to a device manually is left as an exercise to the
reader :)

Seriously, I'm working on adding this to the driver core so you don't
have to do stuff like this in drivers.

thanks,

greg k-h


diff -Nru a/drivers/base/bus.c b/drivers/base/bus.c
--- a/drivers/base/bus.c 2005-01-11 09:35:48 -08:00
+++ b/drivers/base/bus.c 2005-01-11 09:35:48 -08:00
@@ -243,6 +243,17 @@
return ret;
}

+/* manually detach a device from it's associated driver. */
+/* Any write will cause it to happen. */
+static ssize_t device_unbind(struct device *dev, const char *buf, size_t count)
+{
+ down_write(&dev->bus->subsys.rwsem);
+ device_release_driver(dev);
+ up_write(&dev->bus->subsys.rwsem);
+ return count;
+}
+static DEVICE_ATTR(unbind, S_IWUSR, NULL, device_unbind);
+
/**
* device_bind_driver - bind a driver to one device.
* @dev: device.
@@ -264,6 +275,7 @@
sysfs_create_link(&dev->driver->kobj, &dev->kobj,
kobject_name(&dev->kobj));
sysfs_create_link(&dev->kobj, &dev->driver->kobj, "driver");
+ device_create_file(dev, &dev_attr_unbind);
}


@@ -389,6 +401,7 @@
if (drv) {
sysfs_remove_link(&drv->kobj, kobject_name(&dev->kobj));
sysfs_remove_link(&dev->kobj, "driver");
+ device_remove_file(dev, &dev_attr_unbind);
list_del_init(&dev->driver_list);
device_detach_shutdown(dev);
if (drv->remove)
-
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/