xhci: dbc: Update driver to use modern APIs
Subsystems (2)
v2 cover xhci: dbc: Update driver to use modern APIs
Update driver to use some of modern APIs. It's not a full clean up series, just a set of ad-hoc changes.
In v2:
- fixed mistakes pointed by Mathias and LKP
- sent with a cover letter
Andy Shevchenko (10):
xhci: dbc: Drop duplicate checks for dma_free_coherent()
xhci: dbc: Convert to use sysfs_streq()
xhci: dbc: Use sysfs_emit() to instead of scnprintf()
xhci: dbc: Use ATTRIBUTE_GROUPS()
xhci: dbc: Check for errors first in xhci_dbc_stop()
xhci: dbc: Don't shadow error codes in store() functions
xhci: dbc: Replace custom return value with proper Linux error code
xhci: dbc: Use sizeof_field() where it makes sense
xhci: dbc: Use sizeof(*pointer) instead of sizeof(type)
xhci: dbc: Add missing headers
drivers/usb/host/xhci-dbgcap.c | 132 +++++++++++++++++---------------- drivers/usb/host/xhci-dbgcap.h | 1 + 2 files changed, 70 insertions(+), 63 deletions(-)
Signature · 1 line
2.40.0.1.gaa8946217a0b
PATCH v2 1/10 xhci: dbc: Drop duplicate checks for dma_free_coherent()
dma_free_coherent() is NULL-aware, not necessary to check for the parameter twice. Drop duplicate conditionals in the caller.
--- drivers/usb/host/xhci-dbgcap.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-)
drivers/usb/host/xhci-dbgcap.c +5 -9
@@ -28,7 +28,7 @@ static void dbc_ring_free(struct device *dev, struct xhci_ring *ring)if (!ring)return ; -if (ring->first_seg && ring->first_seg->trbs) { +if (ring->first_seg) {dma_free_coherent (dev,TRB_SEGMENT_SIZE , ring->first_seg->trbs, ring->first_seg->dma);
@@ -394,9 +394,8 @@ static int dbc_erst_alloc(struct device *dev, struct xhci_ring *evt_ring,static void dbc_erst_free (struct device *dev,struct xhci_erst *erst) { -if (erst->entries) -dma_free_coherent (dev,sizeof (struct xhci_erst_entry ), - erst->entries, erst->erst_dma_addr); +dma_free_coherent (dev,sizeof (struct xhci_erst_entry ), erst->entries, + erst->erst_dma_addr); erst->entries =NULL ; }
@@ -543,11 +542,8 @@ static void xhci_dbc_mem_cleanup(struct xhci_dbc *dbc)xhci_dbc_eps_exit (dbc); -if (dbc->string) { -dma_free_coherent (dbc->dev, dbc->string_size, - dbc->string, dbc->string_dma); - dbc->string =NULL ; - } +dma_free_coherent (dbc->dev, dbc->string_size, dbc->string, dbc->string_dma); + dbc->string =NULL ;dbc_free_ctx (dbc->dev, dbc->ctx); dbc->ctx =NULL ;
Signature · 1 line
2.40.0.1.gaa8946217a0b
PATCH v2 2/10 xhci: dbc: Convert to use sysfs_streq()
In reply to Andy ShevchenkoIt's standard approach to parse values from user space by using sysfs_streq(). Make driver use it.
--- drivers/usb/host/xhci-dbgcap.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/usb/host/xhci-dbgcap.c +2 -2
@@ -957,9 +957,9 @@ static ssize_t dbc_store(struct device *dev, xhci =hcd_to_xhci (dev_get_drvdata (dev)); dbc = xhci->dbc; -if (!strncmp (buf,"enable" ,6 )) +if (sysfs_streq (buf,"enable" ))xhci_dbc_start (dbc); -else if (!strncmp (buf,"disable" ,7 )) +else if (sysfs_streq (buf,"disable" ))xhci_dbc_stop (dbc);else return -EINVAL ;
Signature · 1 line
2.40.0.1.gaa8946217a0b
PATCH v2 3/10 xhci: dbc: Use sysfs_emit() to instead of scnprintf()
In reply to Andy ShevchenkoFollow the advice of the Documentation/filesystems/sysfs.rst and show() should only use sysfs_emit() or sysfs_emit_at() when formatting the value to be returned to user space.
--- drivers/usb/host/xhci-dbgcap.c | 44 +++++++++++++--------------------- drivers/usb/host/xhci-dbgcap.h | 1 + 2 files changed, 17 insertions(+), 28 deletions(-)
drivers/usb/host/xhci-dbgcap.c +16 -28
@@ -910,41 +910,29 @@ static void xhci_dbc_handle_events(struct work_struct *work)mod_delayed_work (system_wq, &dbc->event_work,1 ); } +static const char *const dbc_state_strings[DS_MAX ] = { + [DS_DISABLED ] ="disabled" , + [DS_INITIALIZED ] ="initialized" , + [DS_ENABLED ] ="enabled" , + [DS_CONNECTED ] ="connected" , + [DS_CONFIGURED ] ="configured" , + [DS_STALLED ] ="stalled" , +}; +static ssize_t dbc_show (struct device *dev,struct device_attribute *attr,char *buf) { -const char *p;struct xhci_dbc *dbc;struct xhci_hcd *xhci; xhci =hcd_to_xhci (dev_get_drvdata (dev)); dbc = xhci->dbc; -switch (dbc->state) { -case DS_DISABLED : - p ="disabled" ; -break ; -case DS_INITIALIZED : - p ="initialized" ; -break ; -case DS_ENABLED : - p ="enabled" ; -break ; -case DS_CONNECTED : - p ="connected" ; -break ; -case DS_CONFIGURED : - p ="configured" ; -break ; -case DS_STALLED : - p ="stalled" ; -break ; -default : - p ="unknown" ; - } +if (dbc->state >=ARRAY_SIZE (dbc_state_strings)) +return sysfs_emit (buf,"unknown\n" ); -return sprintf (buf,"%s\n" , p); +return sysfs_emit (buf,"%s\n" , dbc_state_strings[dbc->state]); } static ssize_t dbc_store(struct device *dev,
@@ -977,7 +965,7 @@ static ssize_t dbc_idVendor_show(struct device *dev, xhci =hcd_to_xhci (dev_get_drvdata (dev)); dbc = xhci->dbc; -return sprintf (buf,"%04x\n" , dbc->idVendor); +return sysfs_emit (buf,"%04x\n" , dbc->idVendor); } static ssize_t dbc_idVendor_store(struct device *dev,
@@ -1017,7 +1005,7 @@ static ssize_t dbc_idProduct_show(struct device *dev, xhci =hcd_to_xhci (dev_get_drvdata (dev)); dbc = xhci->dbc; -return sprintf (buf,"%04x\n" , dbc->idProduct); +return sysfs_emit (buf,"%04x\n" , dbc->idProduct); } static ssize_t dbc_idProduct_store(struct device *dev,
@@ -1056,7 +1044,7 @@ static ssize_t dbc_bcdDevice_show(struct device *dev, xhci =hcd_to_xhci (dev_get_drvdata (dev)); dbc = xhci->dbc; -return sprintf (buf,"%04x\n" , dbc->bcdDevice); +return sysfs_emit (buf,"%04x\n" , dbc->bcdDevice); } static ssize_t dbc_bcdDevice_store(struct device *dev,
@@ -1096,7 +1084,7 @@ static ssize_t dbc_bInterfaceProtocol_show(struct device *dev, xhci =hcd_to_xhci (dev_get_drvdata (dev)); dbc = xhci->dbc; -return sprintf (buf,"%02x\n" , dbc->bInterfaceProtocol); +return sysfs_emit (buf,"%02x\n" , dbc->bInterfaceProtocol); } static ssize_t dbc_bInterfaceProtocol_store(struct device *dev,
drivers/usb/host/xhci-dbgcap.h +1 -0
@@ -82,6 +82,7 @@ enum dbc_state {DS_CONNECTED ,DS_CONFIGURED ,DS_STALLED , +DS_MAX }; struct dbc_ep {
Signature · 1 line
2.40.0.1.gaa8946217a0b
PATCH v2 4/10 xhci: dbc: Use ATTRIBUTE_GROUPS()
In reply to Andy ShevchenkoEmbrace ATTRIBUTE_GROUPS() to avoid boiler plate code. This should not introduce any functional changes.
--- drivers/usb/host/xhci-dbgcap.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-)
drivers/usb/host/xhci-dbgcap.c +4 -7
@@ -1123,7 +1123,7 @@ static DEVICE_ATTR_RW(dbc_idProduct);static DEVICE_ATTR_RW (dbc_bcdDevice);static DEVICE_ATTR_RW (dbc_bInterfaceProtocol); -static struct attribute *dbc_dev_attributes[] = { +static struct attribute *dbc_dev_attrs[] = { &dev_attr_dbc.attr, &dev_attr_dbc_idVendor.attr, &dev_attr_dbc_idProduct.attr,
@@ -1131,10 +1131,7 @@ static struct attribute *dbc_dev_attributes[] = { &dev_attr_dbc_bInterfaceProtocol.attr, NULL }; - -static const struct attribute_group dbc_dev_attrib_grp = { - .attrs = dbc_dev_attributes, -}; +ATTRIBUTE_GROUPS (dbc_dev);struct xhci_dbc *xhci_alloc_dbc (struct device *dev,void __iomem *base,const struct dbc_driver *driver)
@@ -1160,7 +1157,7 @@ xhci_alloc_dbc(struct device *dev, void __iomem *base, const struct dbc_driver *INIT_DELAYED_WORK (&dbc->event_work, xhci_dbc_handle_events);spin_lock_init (&dbc->lock); - ret =sysfs_create_group (&dev->kobj, &dbc_dev_attrib_grp); + ret =sysfs_create_groups (&dev->kobj, dbc_dev_groups);if (ret) goto err;
@@ -1179,7 +1176,7 @@ void xhci_dbc_remove(struct xhci_dbc *dbc)xhci_dbc_stop (dbc);/* remove sysfs files */ -sysfs_remove_group (&dbc->dev->kobj, &dbc_dev_attrib_grp); +sysfs_remove_groups (&dbc->dev->kobj, dbc_dev_groups);kfree (dbc); }
Signature · 1 line
2.40.0.1.gaa8946217a0b
PATCH v2 5/10 xhci: dbc: Check for errors first in xhci_dbc_stop()
In reply to Andy ShevchenkoThe usual patter is to check for errors and then continue if none. Apply that pattern to xhci_dbc_stop() code.
--- drivers/usb/host/xhci-dbgcap.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/usb/host/xhci-dbgcap.c +4 -4
@@ -646,11 +646,11 @@ static void xhci_dbc_stop(struct xhci_dbc *dbc)spin_lock_irqsave (&dbc->lock, flags); ret =xhci_do_dbc_stop (dbc);spin_unlock_irqrestore (&dbc->lock, flags); +if (ret) +return ; -if (!ret) { -xhci_dbc_mem_cleanup (dbc); -pm_runtime_put_sync (dbc->dev);/* note, was self.controller */ - } +xhci_dbc_mem_cleanup (dbc); +pm_runtime_put_sync (dbc->dev);/* note, was self.controller */ } static void
Signature · 1 line
2.40.0.1.gaa8946217a0b
Re: [PATCH v2 05/10] xhci: dbc: Check for errors first in xhci_dbc_stop()
Quoted 1 line · Andy Shevchenko
The usual patter is to check for errors and then continue if none.
Pattern. :-)
Quoted 3 lines
Apply that pattern to
xhci_dbc_stop()code.Signed-off-by Andy Shevchenko <[email protected]>
[...]
MBR, Sergey
Re: [PATCH v2 05/10] xhci: dbc: Check for errors first in xhci_dbc_stop()
Quoted 6 lines · Sergei Shtylyov
Quoted 1 line · Andy Shevchenko
The usual patter is to check for errors and then continue if none.
Pattern. :-)
I'll fix it while applying
-Mathias
PATCH v2 6/10 xhci: dbc: Don't shadow error codes in store() functions
In reply to Andy Shevchenkokstrtox() along with regmap API can return different error codes based on the circumstances. Don't shadow them when returning to the caller.
--- drivers/usb/host/xhci-dbgcap.c | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-)
drivers/usb/host/xhci-dbgcap.c +18 -8
@@ -977,9 +977,11 @@ static ssize_t dbc_idVendor_store(struct device *dev,void __iomem *ptr;u16 value;u32 dev_info; +int ret; -if (kstrtou16 (buf,0 , &value)) -return -EINVAL ; + ret =kstrtou16 (buf,0 , &value); +if (ret) +return ret; xhci =hcd_to_xhci (dev_get_drvdata (dev)); dbc = xhci->dbc;
@@ -1017,9 +1019,11 @@ static ssize_t dbc_idProduct_store(struct device *dev,void __iomem *ptr;u32 dev_info;u16 value; +int ret; -if (kstrtou16 (buf,0 , &value)) -return -EINVAL ; + ret =kstrtou16 (buf,0 , &value); +if (ret) +return ret; xhci =hcd_to_xhci (dev_get_drvdata (dev)); dbc = xhci->dbc;
@@ -1056,9 +1060,11 @@ static ssize_t dbc_bcdDevice_store(struct device *dev,void __iomem *ptr;u32 dev_info;u16 value; +int ret; -if (kstrtou16 (buf,0 , &value)) -return -EINVAL ; + ret =kstrtou16 (buf,0 , &value); +if (ret) +return ret; xhci =hcd_to_xhci (dev_get_drvdata (dev)); dbc = xhci->dbc;
@@ -1098,9 +1104,13 @@ static ssize_t dbc_bInterfaceProtocol_store(struct device *dev,u8 value;int ret; -/* bInterfaceProtocol is 8 bit, but xhci only supports values 0 and 1 */ +/* bInterfaceProtocol is 8 bit, but... */ ret =kstrtou8 (buf,0 , &value); -if (ret || value >1 ) +if (ret) +return ret; + +/* ...xhci only supports values 0 and 1 */ +if (value >1 )return -EINVAL ; xhci =hcd_to_xhci (dev_get_drvdata (dev));
Signature · 1 line
2.40.0.1.gaa8946217a0b
PATCH v2 7/10 xhci: dbc: Replace custom return value with proper Linux error code
In reply to Andy ShevchenkoReplace the custom return value with proper Linux error code.
--- drivers/usb/host/xhci-dbgcap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/host/xhci-dbgcap.c +1 -1
@@ -593,7 +593,7 @@ static int xhci_do_dbc_start(struct xhci_dbc *dbc)static int xhci_do_dbc_stop (struct xhci_dbc *dbc) {if (dbc->state ==DS_DISABLED ) -return -1 ; +return -EINVAL ;writel (0 , &dbc->regs->control); dbc->state =DS_DISABLED ;
Signature · 1 line
2.40.0.1.gaa8946217a0b
PATCH v2 8/10 xhci: dbc: Use sizeof_field() where it makes sense
In reply to Andy ShevchenkoInstead of doing custom calculations, use sizeof_field() macro.
--- drivers/usb/host/xhci-dbgcap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/host/xhci-dbgcap.c +1 -1
@@ -374,7 +374,7 @@ static void xhci_dbc_eps_init(struct xhci_dbc *dbc)static void xhci_dbc_eps_exit (struct xhci_dbc *dbc) { -memset (dbc->eps,0 ,sizeof (struct dbc_ep ) *ARRAY_SIZE (dbc->eps)); + memset(dbc->eps, 0, sizeof_field(struct xhci_dbc, eps)); } static int dbc_erst_alloc(struct device *dev, struct xhci_ring *evt_ring,
Signature · 1 line
2.40.0.1.gaa8946217a0b
PATCH v2 9/10 xhci: dbc: Use sizeof(*pointer) instead of sizeof(type)
In reply to Andy ShevchenkoIt is preferred to use sizeof(*pointer) instead of sizeof(type). The type of the variable can change and one needs not change the former (unlike the latter). No functional change intended.
--- drivers/usb/host/xhci-dbgcap.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/usb/host/xhci-dbgcap.c +3 -3
@@ -380,7 +380,7 @@ static void xhci_dbc_eps_exit(struct xhci_dbc *dbc)static int dbc_erst_alloc (struct device *dev,struct xhci_ring *evt_ring,struct xhci_erst *erst,gfp_t flags) { - erst->entries =dma_alloc_coherent (dev,sizeof (struct xhci_erst_entry ), + erst->entries =dma_alloc_coherent (dev,sizeof (*erst->entries), &erst->erst_dma_addr, flags);if (!erst->entries)return -ENOMEM ;
@@ -394,7 +394,7 @@ static int dbc_erst_alloc(struct device *dev, struct xhci_ring *evt_ring,static void dbc_erst_free (struct device *dev,struct xhci_erst *erst) { -dma_free_coherent (dev,sizeof (struct xhci_erst_entry ), erst->entries, +dma_free_coherent (dev,sizeof (*erst->entries), erst->entries, erst->erst_dma_addr); erst->entries =NULL ; }
@@ -494,7 +494,7 @@ static int xhci_dbc_mem_init(struct xhci_dbc *dbc, gfp_t flags) goto ctx_fail;/* Allocate the string table: */ - dbc->string_size =sizeof (struct dbc_str_descs ); + dbc->string_size =sizeof (*dbc->string); dbc->string =dma_alloc_coherent (dev, dbc->string_size, &dbc->string_dma, flags);if (!dbc->string)
Signature · 1 line
2.40.0.1.gaa8946217a0b
PATCH v2 10/10 xhci: dbc: Add missing headers
In reply to Andy ShevchenkoDon't inherit headers "by chances" from asm/bug.h, asm/io.h, etc... Include the needed headers explicitly.
--- drivers/usb/host/xhci-dbgcap.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-)
drivers/usb/host/xhci-dbgcap.c +16 -1
@@ -6,9 +6,24 @@* * Author: Lu Baolu <[email protected]> */ +#include <linux/bug.h> +#include <linux/device.h> #include <linux/dma-mapping.h> -#include <linux/slab.h> +#include <linux/errno.h> +#include <linux/kstrtox.h> +#include <linux/list.h> #include <linux/nls.h> +#include <linux/pm_runtime.h> +#include <linux/slab.h> +#include <linux/spinlock.h> +#include <linux/string.h> +#include <linux/sysfs.h> +#include <linux/types.h> +#include <linux/workqueue.h> + +#include <linux/io-64-nonatomic-lo-hi.h> + +#include <asm/byteorder.h> #include "xhci.h" #include "xhci-trace.h"
Signature · 1 line
2.40.0.1.gaa8946217a0b