From 72f8c0bfa0de64c68ee59f40eb9b2683bffffbb0 Mon Sep 17 00:00:00 2001 From: Wolfram Sang Date: Tue, 25 Oct 2011 15:16:47 +0200 Subject: lib: devres: add convenience function to remap a resource Almost every platform_driver does the three steps get_resource, request_mem_region, ioremap. This does not only lead to a lot of code duplication, but also a huge number of similar error strings and inconsistent error codes on failure. So, introduce a helper function which simplifies remapping a resource and make it hard to do something wrong and add documentation for it. Signed-off-by: Wolfram Sang Acked-by: Grant Likely Acked-by: Tejun Heo Signed-off-by: Greg Kroah-Hartman --- include/linux/device.h | 3 +++ 1 file changed, 3 insertions(+) (limited to 'include/linux/device.h') diff --git a/include/linux/device.h b/include/linux/device.h index ffbcf95cd97d..c6335982774c 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -490,6 +490,9 @@ extern int devres_release_group(struct device *dev, void *id); extern void *devm_kzalloc(struct device *dev, size_t size, gfp_t gfp); extern void devm_kfree(struct device *dev, void *p); +void __iomem *devm_request_and_ioremap(struct device *dev, + struct resource *res); + struct device_dma_parameters { /* * a low level driver may set these to teach IOMMU code about -- cgit v1.2.3 From 907d0ed1c84114d4e8dafd66af982515d3739c90 Mon Sep 17 00:00:00 2001 From: Lars-Peter Clausen Date: Wed, 16 Nov 2011 10:13:35 +0100 Subject: drivercore: Generalize module_platform_driver This patch generalizes the module_platform_driver macro and introduces a new module_driver macro. The module_driver macro takes a driver name, a register and a unregister function for this driver type. Using these it construct the module init and exit sections which register and unregister the driver. Since such init/exit sections are commonly found in drivers this macro can be used to eliminate a lot of boilerplate code. The macro is not intended to be used by driver modules directly, instead it should be used to generate bus specific macros for registering drivers like the module_platform_driver macro. Signed-off-by: Lars-Peter Clausen Acked-by: Grant Likely Acked-by: Jonathan Cameron Acked-by: Wolfram Sang Signed-off-by: Greg Kroah-Hartman --- include/linux/device.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'include/linux/device.h') diff --git a/include/linux/device.h b/include/linux/device.h index c6335982774c..341fb740d851 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -922,4 +922,25 @@ extern long sysfs_deprecated; #define sysfs_deprecated 0 #endif +/** + * module_driver() - Helper macro for drivers that don't do anything + * special in module init/exit. This eliminates a lot of boilerplate. + * Each module may only use this macro once, and calling it replaces + * module_init() and module_exit(). + * + * Use this macro to construct bus specific macros for registering + * drivers, and do not use it on its own. + */ +#define module_driver(__driver, __register, __unregister) \ +static int __init __driver##_init(void) \ +{ \ + return __register(&(__driver)); \ +} \ +module_init(__driver##_init); \ +static void __exit __driver##_exit(void) \ +{ \ + __unregister(&(__driver)); \ +} \ +module_exit(__driver##_exit); + #endif /* _DEVICE_H_ */ -- cgit v1.2.3 From ca22e56debc57b47c422b749c93217ba62644be2 Mon Sep 17 00:00:00 2001 From: Kay Sievers Date: Wed, 14 Dec 2011 14:29:38 -0800 Subject: driver-core: implement 'sysdev' functionality for regular devices and buses All sysdev classes and sysdev devices will converted to regular devices and buses to properly hook userspace into the event processing. There is no interesting difference between a 'sysdev' and 'device' which would justify to roll an entire own subsystem with different userspace export semantics. Userspace relies on events and generic sysfs subsystem infrastructure from sysdev devices, which are currently not properly available. Every converted sysdev class will create a regular device with the class name in /sys/devices/system and all registered devices will becom a children of theses devices. For compatibility reasons, the sysdev class-wide attributes are created at this parent device. (Do not copy that logic for anything new, subsystem- wide properties belong to the subsystem, not to some fake parent device created in /sys/devices.) Every sysdev driver is implemented as a simple subsystem interface now, and no longer called a driver. After all sysdev classes are ported to regular driver core entities, the sysdev implementation will be entirely removed from the kernel. Signed-off-by: Kay Sievers Signed-off-by: Greg Kroah-Hartman --- include/linux/device.h | 78 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 74 insertions(+), 4 deletions(-) (limited to 'include/linux/device.h') diff --git a/include/linux/device.h b/include/linux/device.h index 341fb740d851..7f9fc1505e94 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -53,6 +53,8 @@ extern void bus_remove_file(struct bus_type *, struct bus_attribute *); * struct bus_type - The bus type of the device * * @name: The name of the bus. + * @dev_name: Used for subsystems to enumerate devices like ("foo%u", dev->id). + * @dev_root: Default device to use as the parent. * @bus_attrs: Default attributes of the bus. * @dev_attrs: Default attributes of the devices on the bus. * @drv_attrs: Default attributes of the device drivers on the bus. @@ -86,6 +88,8 @@ extern void bus_remove_file(struct bus_type *, struct bus_attribute *); */ struct bus_type { const char *name; + const char *dev_name; + struct device *dev_root; struct bus_attribute *bus_attrs; struct device_attribute *dev_attrs; struct driver_attribute *drv_attrs; @@ -106,12 +110,30 @@ struct bus_type { struct subsys_private *p; }; -extern int __must_check bus_register(struct bus_type *bus); +/* This is a #define to keep the compiler from merging different + * instances of the __key variable */ +#define bus_register(subsys) \ +({ \ + static struct lock_class_key __key; \ + __bus_register(subsys, &__key); \ +}) +extern int __must_check __bus_register(struct bus_type *bus, + struct lock_class_key *key); extern void bus_unregister(struct bus_type *bus); extern int __must_check bus_rescan_devices(struct bus_type *bus); /* iterator helpers for buses */ +struct subsys_dev_iter { + struct klist_iter ki; + const struct device_type *type; +}; +void subsys_dev_iter_init(struct subsys_dev_iter *iter, + struct bus_type *subsys, + struct device *start, + const struct device_type *type); +struct device *subsys_dev_iter_next(struct subsys_dev_iter *iter); +void subsys_dev_iter_exit(struct subsys_dev_iter *iter); int bus_for_each_dev(struct bus_type *bus, struct device *start, void *data, int (*fn)(struct device *dev, void *data)); @@ -121,10 +143,10 @@ struct device *bus_find_device(struct bus_type *bus, struct device *start, struct device *bus_find_device_by_name(struct bus_type *bus, struct device *start, const char *name); - +struct device *subsys_find_device_by_id(struct bus_type *bus, unsigned int id, + struct device *hint); int bus_for_each_drv(struct bus_type *bus, struct device_driver *start, void *data, int (*fn)(struct device_driver *, void *)); - void bus_sort_breadthfirst(struct bus_type *bus, int (*compare)(const struct device *a, const struct device *b)); @@ -255,6 +277,33 @@ struct device *driver_find_device(struct device_driver *drv, struct device *start, void *data, int (*match)(struct device *dev, void *data)); +/** + * struct subsys_interface - interfaces to device functions + * @name name of the device function + * @subsystem subsytem of the devices to attach to + * @node the list of functions registered at the subsystem + * @add device hookup to device function handler + * @remove device hookup to device function handler + * + * Simple interfaces attached to a subsystem. Multiple interfaces can + * attach to a subsystem and its devices. Unlike drivers, they do not + * exclusively claim or control devices. Interfaces usually represent + * a specific functionality of a subsystem/class of devices. + */ +struct subsys_interface { + const char *name; + struct bus_type *subsys; + struct list_head node; + int (*add_dev)(struct device *dev, struct subsys_interface *sif); + int (*remove_dev)(struct device *dev, struct subsys_interface *sif); +}; + +int subsys_interface_register(struct subsys_interface *sif); +void subsys_interface_unregister(struct subsys_interface *sif); + +int subsys_system_register(struct bus_type *subsys, + const struct attribute_group **groups); + /** * struct class - device classes * @name: Name of the class. @@ -438,8 +487,28 @@ struct device_attribute { const char *buf, size_t count); }; +struct dev_ext_attribute { + struct device_attribute attr; + void *var; +}; + +ssize_t device_show_ulong(struct device *dev, struct device_attribute *attr, + char *buf); +ssize_t device_store_ulong(struct device *dev, struct device_attribute *attr, + const char *buf, size_t count); +ssize_t device_show_int(struct device *dev, struct device_attribute *attr, + char *buf); +ssize_t device_store_int(struct device *dev, struct device_attribute *attr, + const char *buf, size_t count); + #define DEVICE_ATTR(_name, _mode, _show, _store) \ -struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store) + struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store) +#define DEVICE_ULONG_ATTR(_name, _mode, _var) \ + struct dev_ext_attribute dev_attr_##_name = \ + { __ATTR(_name, _mode, device_show_ulong, device_store_ulong), &(_var) } +#define DEVICE_INT_ATTR(_name, _mode, _var) \ + struct dev_ext_attribute dev_attr_##_name = \ + { __ATTR(_name, _mode, device_show_ulong, device_store_ulong), &(_var) } extern int __must_check device_create_file(struct device *device, const struct device_attribute *entry); @@ -603,6 +672,7 @@ struct device { struct device_node *of_node; /* associated device tree node */ dev_t devt; /* dev_t, creates the sysfs "dev" */ + u32 id; /* device instance */ spinlock_t devres_lock; struct list_head devres_head; -- cgit v1.2.3 From b9d4e714a86a4e88c2f530c76597f7025e5851d6 Mon Sep 17 00:00:00 2001 From: Greg Kroah-Hartman Date: Wed, 4 Jan 2012 15:05:10 -0800 Subject: driver core: remove __must_check from device_create_file With the conversion of the sysdev to a real struct device, more drivers are calling device_create_file, and some of them don't check the return value, which isn't wise. But as they happen to be in parts of the kernel where a warning is considered an error (i.e. powerpc), this breaks the build. So for now, remove the marking on the function, which fixes the build problems. Reported-by: Stephen Rothwell Cc: Kay Sievers Signed-off-by: Greg Kroah-Hartman --- include/linux/device.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'include/linux/device.h') diff --git a/include/linux/device.h b/include/linux/device.h index 7f9fc1505e94..acf505e4fe94 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -510,8 +510,8 @@ ssize_t device_store_int(struct device *dev, struct device_attribute *attr, struct dev_ext_attribute dev_attr_##_name = \ { __ATTR(_name, _mode, device_show_ulong, device_store_ulong), &(_var) } -extern int __must_check device_create_file(struct device *device, - const struct device_attribute *entry); +extern int device_create_file(struct device *device, + const struct device_attribute *entry); extern void device_remove_file(struct device *dev, const struct device_attribute *attr); extern int __must_check device_create_bin_file(struct device *dev, -- cgit v1.2.3