In preparation for making the struct driver parameter to the match
callbacks const, introduce container_of_const and make use of it instead
of normal container in upcasting macros.

Signed-off-by: Ahmad Fatoum <a.fat...@pengutronix.de>
---
 include/acpi.h               |  5 +----
 include/efi/efi-device.h     |  5 +----
 include/linux/container_of.h | 14 ++++++++++++++
 include/linux/virtio.h       | 10 ++--------
 4 files changed, 18 insertions(+), 16 deletions(-)

diff --git a/include/acpi.h b/include/acpi.h
index fc0da30610a6..fa94d972e3fe 100644
--- a/include/acpi.h
+++ b/include/acpi.h
@@ -125,10 +125,7 @@ struct acpi_driver {
 
 extern struct bus_type acpi_bus;
 
-static inline struct acpi_driver *to_acpi_driver(struct driver *drv)
-{
-       return container_of(drv, struct acpi_driver, driver);
-}
+#define to_acpi_driver(drv) container_of_const((drv), struct acpi_driver, 
driver)
 
 #define device_acpi_driver(drv)        \
        register_efi_driver_macro(device, acpi, drv)
diff --git a/include/efi/efi-device.h b/include/efi/efi-device.h
index 5d2110356fd4..a8fc99a0e12b 100644
--- a/include/efi/efi-device.h
+++ b/include/efi/efi-device.h
@@ -33,10 +33,7 @@ static inline struct efi_device *to_efi_device(struct device 
*dev)
        return container_of(dev, struct efi_device, dev);
 }
 
-static inline struct efi_driver *to_efi_driver(struct driver *drv)
-{
-       return container_of(drv, struct efi_driver, driver);
-}
+#define to_efi_driver(drv) container_of_const((drv), struct efi_driver, driver)
 
 static inline int efi_driver_register(struct efi_driver *efidrv)
 {
diff --git a/include/linux/container_of.h b/include/linux/container_of.h
index 2f4944b791b8..8669ef0154d9 100644
--- a/include/linux/container_of.h
+++ b/include/linux/container_of.h
@@ -21,6 +21,20 @@
                      "pointer type mismatch in container_of()");       \
        ((type *)(__mptr - offsetof(type, member))); })
 
+
+/**
+ * container_of_const - cast a member of a structure out to the containing
+ *                     structure and preserve the const-ness of the pointer
+ * @ptr:               the pointer to the member
+ * @type:              the type of the container struct this is embedded in.
+ * @member:            the name of the member within the struct.
+ */
+#define container_of_const(ptr, type, member)                          \
+       _Generic(ptr,                                                   \
+               const typeof(*(ptr)) *: ((const type *)container_of(ptr, type, 
member)),\
+               default: ((type *)container_of(ptr, type, member))      \
+       )
+
 /**
  * container_of_safe - cast a member of a structure out to the containing 
structure
  * @ptr:       the pointer to the member.
diff --git a/include/linux/virtio.h b/include/linux/virtio.h
index 3d4c88336055..eb4293003294 100644
--- a/include/linux/virtio.h
+++ b/include/linux/virtio.h
@@ -43,10 +43,7 @@ struct virtio_device {
        u32 status_param;
 };
 
-static inline struct virtio_device *dev_to_virtio(struct device *_dev)
-{
-       return container_of(_dev, struct virtio_device, dev);
-}
+#define dev_to_virtio(_dev)    container_of_const(_dev, struct virtio_device, 
dev)
 
 void virtio_add_status(struct virtio_device *dev, unsigned int status);
 int register_virtio_device(struct virtio_device *dev);
@@ -96,10 +93,7 @@ struct virtio_driver {
        void (*config_changed)(struct virtio_device *dev);
 };
 
-static inline struct virtio_driver *drv_to_virtio(struct driver *drv)
-{
-       return container_of(drv, struct virtio_driver, driver);
-}
+#define drv_to_virtio(__drv)   container_of_const(__drv, struct virtio_driver, 
driver)
 
 int virtio_driver_register(struct virtio_driver *drv);
 
-- 
2.39.5


Reply via email to