On Sat, Aug 20, 2011 at 12:32:59AM +0200, Michal Nazarewicz wrote:From: Michal Nazarewicz <mina86@xxxxxxxxxx>
In a few places kernel wants to print a human-readable USB
device speed name (eg. "high") instead of a raw number
(eg. 3). Usually a switch is introduced in those places
leading to code repetition. To mitigate this issue, this
commit introduces usb_device_speed_name() function, which
returns a human-readable name of provided speed.
Signed-off-by: Michal Nazarewicz <mina86@xxxxxxxxxx>
---
drivers/usb/Makefile | 3 +++
drivers/usb/common.c | 24 ++++++++++++++++++++++++
drivers/usb/gadget/udc-core.c | 19 ++-----------------
include/linux/usb/ch9.h | 8 ++++++++
4 files changed, 37 insertions(+), 17 deletions(-)
create mode 100644 drivers/usb/common.c
diff --git a/drivers/usb/Makefile b/drivers/usb/Makefile
index 30ddf8d..a3167cc 100644
--- a/drivers/usb/Makefile
+++ b/drivers/usb/Makefile
@@ -51,3 +51,6 @@ obj-$(CONFIG_USB_MUSB_HDRC) += musb/
obj-$(CONFIG_USB_RENESAS_USBHS) += renesas_usbhs/
obj-$(CONFIG_USB_OTG_UTILS) += otg/
obj-$(CONFIG_USB_GADGET) += gadget/
+
+obj-$(CONFIG_USB) += common.o
+obj-$(CONFIG_USB_GADGET) += common.o
diff --git a/drivers/usb/common.c b/drivers/usb/common.c
new file mode 100644
index 0000000..e9d7141
--- /dev/null
+++ b/drivers/usb/common.c
@@ -0,0 +1,24 @@
+/*
+ * Provides code common for host and device side USB.
+ */
+
+#include <linux/kernel.h> /* for ARRAY_SIZE() */
+#include <linux/module.h> /* for EXPORT_SYMBOL_GPL() */
+#include <linux/usb/ch9.h>
+
+const char *usb_device_speed_name(enum usb_device_speed speed)
somehow usb_speed_string() sounds better to me.
+{
+ static const char *const names[] = {
+ "UNKNOWN",
+ "low-speed",
+ "full-speed",
+ "high-speed",
+ "wireless",
+ "super-speed",
+ };
+
+ if (speed < 0 || speed >= ARRAY_SIZE(names))
+ speed = USB_SPEED_UNKNOWN;
I generally dislike depending on a particular ordering of enumerations.
While's it's highly unlikely that anyone will change that, if someone
does change, then it'll take a long time to actually find the bug on the
print here :-)
I woud rather use a switch in this case.
diff --git a/drivers/usb/gadget/udc-core.c b/drivers/usb/gadget/udc-core.c
index 05ba472..e1ecdbc 100644
--- a/drivers/usb/gadget/udc-core.c
+++ b/drivers/usb/gadget/udc-core.c
@@ -375,23 +375,8 @@ static ssize_t usb_udc_speed_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
- struct usb_gadget *gadget = udc->gadget;
-
- switch (gadget->speed) {
- case USB_SPEED_LOW:
- return snprintf(buf, PAGE_SIZE, "low-speed\n");
- case USB_SPEED_FULL:
- return snprintf(buf, PAGE_SIZE, "full-speed\n");
- case USB_SPEED_HIGH:
- return snprintf(buf, PAGE_SIZE, "high-speed\n");
- case USB_SPEED_WIRELESS:
- return snprintf(buf, PAGE_SIZE, "wireless\n");
- case USB_SPEED_SUPER:
- return snprintf(buf, PAGE_SIZE, "super-speed\n");
- case USB_SPEED_UNKNOWN: /* FALLTHROUGH */
- default:
- return snprintf(buf, PAGE_SIZE, "UNKNOWN\n");
- }
+ return snprintf(buf, PAGE_SIZE, "%s\n",
+ usb_device_speed_name(udc->gadget->speed));
}
static DEVICE_ATTR(speed, S_IRUSR, usb_udc_speed_show, NULL);
you said 'several locations', but there's only one ? No other driver is
using something similar ?