From 63b19547cc3d96041d7bc7ab8de6292b0ebaf2c9 Mon Sep 17 00:00:00 2001 From: Jonathan Cameron Date: Sun, 23 Jul 2017 17:25:43 +0100 Subject: iio: Use macro magic to avoid manual assign of driver_module Starting point in boiler plate reduction similar to that done for many similar cases elsewhere in the kernel. Signed-off-by: Jonathan Cameron Reviewed-by: Lars-Peter Clausen --- include/linux/iio/iio.h | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) (limited to 'include/linux') diff --git a/include/linux/iio/iio.h b/include/linux/iio/iio.h index d68bec297a45..97a014300947 100644 --- a/include/linux/iio/iio.h +++ b/include/linux/iio/iio.h @@ -518,6 +518,7 @@ struct iio_buffer_setup_ops { /** * struct iio_dev - industrial I/O device * @id: [INTERN] used to identify device internally + * @driver_module: [INTERN] used to make it harder to undercut users * @modes: [DRIVER] operating modes supported by device * @currentmode: [DRIVER] current operating mode * @dev: [DRIVER] device structure, should be assigned a parent @@ -558,6 +559,7 @@ struct iio_buffer_setup_ops { */ struct iio_dev { int id; + struct module *driver_module; int modes; int currentmode; @@ -604,9 +606,34 @@ struct iio_dev { const struct iio_chan_spec *iio_find_channel_from_si(struct iio_dev *indio_dev, int si); -int iio_device_register(struct iio_dev *indio_dev); +/** + * iio_device_register() - register a device with the IIO subsystem + * @indio_dev: Device structure filled by the device driver + **/ +#define iio_device_register(iio_dev) \ + __iio_device_register((iio_dev), THIS_MODULE) +int __iio_device_register(struct iio_dev *indio_dev, struct module *this_mod); void iio_device_unregister(struct iio_dev *indio_dev); -int devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev); +/** + * devm_iio_device_register - Resource-managed iio_device_register() + * @dev: Device to allocate iio_dev for + * @indio_dev: Device structure filled by the device driver + * + * Managed iio_device_register. The IIO device registered with this + * function is automatically unregistered on driver detach. This function + * calls iio_device_register() internally. Refer to that function for more + * information. + * + * If an iio_dev registered with this function needs to be unregistered + * separately, devm_iio_device_unregister() must be used. + * + * RETURNS: + * 0 on success, negative error number on failure. + */ +#define devm_iio_device_register(dev, indio_dev) \ + __devm_iio_device_register((dev), (indio_dev), THIS_MODULE); +int __devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev, + struct module *this_mod); void devm_iio_device_unregister(struct device *dev, struct iio_dev *indio_dev); int iio_push_event(struct iio_dev *indio_dev, u64 ev_code, s64 timestamp); int iio_device_claim_direct_mode(struct iio_dev *indio_dev); -- cgit v1.2.3 From 035c70aeb64b861aa88a666c61fda8b2ae49aeae Mon Sep 17 00:00:00 2001 From: Jonathan Cameron Date: Sun, 23 Jul 2017 17:25:44 +0100 Subject: iio: triggers: Use macros to avoid boilerplate assignment of owner. This trig_ops.owner assignment occurs in all trigger drivers and can be simply automated using a macro as has been done in many other places in the kernel. Signed-off-by: Jonathan Cameron Reviewed-by: Lars-Peter Clausen --- drivers/iio/industrialio-trigger.c | 18 ++++++++++++------ include/linux/iio/trigger.h | 19 +++++++++++++------ 2 files changed, 25 insertions(+), 12 deletions(-) (limited to 'include/linux') diff --git a/drivers/iio/industrialio-trigger.c b/drivers/iio/industrialio-trigger.c index 9596fedacedb..faf00202915a 100644 --- a/drivers/iio/industrialio-trigger.c +++ b/drivers/iio/industrialio-trigger.c @@ -66,10 +66,13 @@ ATTRIBUTE_GROUPS(iio_trig_dev); static struct iio_trigger *__iio_trigger_find_by_name(const char *name); -int iio_trigger_register(struct iio_trigger *trig_info) +int __iio_trigger_register(struct iio_trigger *trig_info, + struct module *this_mod) { int ret; + trig_info->owner = this_mod; + /* trig_info->ops is required for the module member */ if (!trig_info->ops) return -EINVAL; @@ -105,7 +108,7 @@ error_unregister_id: ida_simple_remove(&iio_trigger_ida, trig_info->id); return ret; } -EXPORT_SYMBOL(iio_trigger_register); +EXPORT_SYMBOL(__iio_trigger_register); void iio_trigger_unregister(struct iio_trigger *trig_info) { @@ -663,9 +666,10 @@ static void devm_iio_trigger_unreg(struct device *dev, void *res) } /** - * devm_iio_trigger_register - Resource-managed iio_trigger_register() + * __devm_iio_trigger_register - Resource-managed iio_trigger_register() * @dev: device this trigger was allocated for * @trig_info: trigger to register + * @this_mod: module registering the trigger * * Managed iio_trigger_register(). The IIO trigger registered with this * function is automatically unregistered on driver detach. This function @@ -678,7 +682,9 @@ static void devm_iio_trigger_unreg(struct device *dev, void *res) * RETURNS: * 0 on success, negative error number on failure. */ -int devm_iio_trigger_register(struct device *dev, struct iio_trigger *trig_info) +int __devm_iio_trigger_register(struct device *dev, + struct iio_trigger *trig_info, + struct module *this_mod) { struct iio_trigger **ptr; int ret; @@ -688,7 +694,7 @@ int devm_iio_trigger_register(struct device *dev, struct iio_trigger *trig_info) return -ENOMEM; *ptr = trig_info; - ret = iio_trigger_register(trig_info); + ret = __iio_trigger_register(trig_info, this_mod); if (!ret) devres_add(dev, ptr); else @@ -696,7 +702,7 @@ int devm_iio_trigger_register(struct device *dev, struct iio_trigger *trig_info) return ret; } -EXPORT_SYMBOL_GPL(devm_iio_trigger_register); +EXPORT_SYMBOL_GPL(__devm_iio_trigger_register); /** * devm_iio_trigger_unregister - Resource-managed iio_trigger_unregister() diff --git a/include/linux/iio/trigger.h b/include/linux/iio/trigger.h index ea08302f2d7b..999793212b40 100644 --- a/include/linux/iio/trigger.h +++ b/include/linux/iio/trigger.h @@ -62,6 +62,7 @@ struct iio_trigger_ops { **/ struct iio_trigger { const struct iio_trigger_ops *ops; + struct module *owner; int id; const char *name; struct device dev; @@ -87,14 +88,14 @@ static inline struct iio_trigger *to_iio_trigger(struct device *d) static inline void iio_trigger_put(struct iio_trigger *trig) { - module_put(trig->ops->owner); + module_put(trig->owner); put_device(&trig->dev); } static inline struct iio_trigger *iio_trigger_get(struct iio_trigger *trig) { get_device(&trig->dev); - __module_get(trig->ops->owner); + __module_get(trig->owner); return trig; } @@ -127,10 +128,16 @@ static inline void *iio_trigger_get_drvdata(struct iio_trigger *trig) * iio_trigger_register() - register a trigger with the IIO core * @trig_info: trigger to be registered **/ -int iio_trigger_register(struct iio_trigger *trig_info); - -int devm_iio_trigger_register(struct device *dev, - struct iio_trigger *trig_info); +#define iio_trigger_register(trig_info) \ + __iio_trigger_register((trig_info), THIS_MODULE) +int __iio_trigger_register(struct iio_trigger *trig_info, + struct module *this_mod); + +#define devm_iio_trigger_register(dev, trig_info) \ + __devm_iio_trigger_register((dev), (trig_info), THIS_MODULE) +int __devm_iio_trigger_register(struct device *dev, + struct iio_trigger *trig_info, + struct module *this_mod); /** * iio_trigger_unregister() - unregister a trigger from the core -- cgit v1.2.3 From 97623c0a80a605ef3fae337081ed008796bf8cc2 Mon Sep 17 00:00:00 2001 From: Jonathan Cameron Date: Sun, 23 Jul 2017 17:26:21 +0100 Subject: iio: drop iio_info.driver_module and iio_trigger_ops.owner. The equivalents are now assigned automatically in the relevant registration calls and so are not needed in these operations structures. Signed-off-by: Jonathan Cameron Reviewed-by: Lars-Peter Clausen --- include/linux/iio/iio.h | 3 --- include/linux/iio/trigger.h | 2 -- 2 files changed, 5 deletions(-) (limited to 'include/linux') diff --git a/include/linux/iio/iio.h b/include/linux/iio/iio.h index 97a014300947..486ffbb1a926 100644 --- a/include/linux/iio/iio.h +++ b/include/linux/iio/iio.h @@ -369,8 +369,6 @@ struct iio_dev; /** * struct iio_info - constant information about device - * @driver_module: module structure used to ensure correct - * ownership of chrdevs etc * @event_attrs: event control attributes * @attrs: general purpose device attributes * @read_raw: function to request a value from the device. @@ -425,7 +423,6 @@ struct iio_dev; * were flushed and there was an error. **/ struct iio_info { - struct module *driver_module; const struct attribute_group *event_attrs; const struct attribute_group *attrs; diff --git a/include/linux/iio/trigger.h b/include/linux/iio/trigger.h index 999793212b40..1afe349af1fa 100644 --- a/include/linux/iio/trigger.h +++ b/include/linux/iio/trigger.h @@ -23,7 +23,6 @@ struct iio_trigger; /** * struct iio_trigger_ops - operations structure for an iio_trigger. - * @owner: used to monitor usage count of the trigger. * @set_trigger_state: switch on/off the trigger on demand * @try_reenable: function to reenable the trigger when the * use count is zero (may be NULL) @@ -34,7 +33,6 @@ struct iio_trigger; * instances of a given device. **/ struct iio_trigger_ops { - struct module *owner; int (*set_trigger_state)(struct iio_trigger *trig, bool state); int (*try_reenable)(struct iio_trigger *trig); int (*validate_device)(struct iio_trigger *trig, -- cgit v1.2.3