From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: linux-kernel@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Subject: [PATCH 3/4] usb.h: take advantage of container_of_const()
Date: Thu, 1 Dec 2022 20:30:56 +0100 [thread overview]
Message-ID: <20221201193057.1195255-3-gregkh@linuxfoundation.org> (raw)
In-Reply-To: <20221201193057.1195255-1-gregkh@linuxfoundation.org>
Instead of rolling our own const-checking logic in to_usb_interface()
and to_usb_device() use the newly added container_of_const() instead,
making the logic much simpler overall.
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
include/linux/usb.h | 44 ++++----------------------------------------
1 file changed, 4 insertions(+), 40 deletions(-)
diff --git a/include/linux/usb.h b/include/linux/usb.h
index 4b463a5e4ba2..cd5637c950ae 100644
--- a/include/linux/usb.h
+++ b/include/linux/usb.h
@@ -259,26 +259,8 @@ struct usb_interface {
struct work_struct reset_ws; /* for resets in atomic context */
};
-static inline struct usb_interface *__to_usb_interface(struct device *d)
-{
- return container_of(d, struct usb_interface, dev);
-}
-
-static inline const struct usb_interface *__to_usb_interface_const(const struct device *d)
-{
- return container_of(d, struct usb_interface, dev);
-}
-
-/*
- * container_of() will happily take a const * and spit back a non-const * as it
- * is just doing pointer math. But we want to be a bit more careful in the USB
- * driver code, so manually force any const * of a device to also be a const *
- * to a usb_device.
- */
-#define to_usb_interface(dev) \
- _Generic((dev), \
- const struct device *: __to_usb_interface_const, \
- struct device *: __to_usb_interface)(dev)
+#define to_usb_interface(__dev) \
+ container_of_const(struct device, __dev, struct usb_interface, dev)
static inline void *usb_get_intfdata(struct usb_interface *intf)
{
@@ -730,26 +712,8 @@ struct usb_device {
unsigned use_generic_driver:1;
};
-static inline struct usb_device *__to_usb_device(struct device *d)
-{
- return container_of(d, struct usb_device, dev);
-}
-
-static inline const struct usb_device *__to_usb_device_const(const struct device *d)
-{
- return container_of(d, struct usb_device, dev);
-}
-
-/*
- * container_of() will happily take a const * and spit back a non-const * as it
- * is just doing pointer math. But we want to be a bit more careful in the USB
- * driver code, so manually force any const * of a device to also be a const *
- * to a usb_device.
- */
-#define to_usb_device(dev) \
- _Generic((dev), \
- const struct device *: __to_usb_device_const, \
- struct device *: __to_usb_device)(dev)
+#define to_usb_device(__dev) \
+ container_of_const(struct device, __dev, struct usb_device, dev)
static inline struct usb_device *__intf_to_usbdev(struct usb_interface *intf)
{
--
2.38.1
next prev parent reply other threads:[~2022-12-01 19:32 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-12-01 19:30 [PATCH 1/4] container_of: add container_of_const() that preserves const-ness of the pointer Greg Kroah-Hartman
2022-12-01 19:30 ` [PATCH 2/4] device.h: move kobj_to_dev() to use container_of_const() Greg Kroah-Hartman
2022-12-01 19:30 ` Greg Kroah-Hartman [this message]
2022-12-01 19:30 ` [PATCH 4/4] firmware_loader: fix up to_fw_sysfs() to preserve const Greg Kroah-Hartman
2022-12-01 20:25 ` Russ Weight
2022-12-01 22:50 ` [PATCH 1/4] container_of: add container_of_const() that preserves const-ness of the pointer Andy Shevchenko
2022-12-02 0:46 ` Jason Gunthorpe
2022-12-02 6:41 ` Greg Kroah-Hartman
2022-12-01 23:21 ` Sakari Ailus
2022-12-02 10:45 ` Greg Kroah-Hartman
2022-12-02 12:01 ` Andy Shevchenko
2022-12-02 0:48 ` Jason Gunthorpe
2022-12-02 12:48 ` Sakari Ailus
2022-12-02 16:24 ` Greg Kroah-Hartman
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20221201193057.1195255-3-gregkh@linuxfoundation.org \
--to=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox