* [PATCH v3 1/4 RESEND] USB: serial: cp210x: New register access functions.
@ 2016-01-16 19:46 Konstantin Shkolnyy
2016-01-18 17:32 ` Johan Hovold
0 siblings, 1 reply; 4+ messages in thread
From: Konstantin Shkolnyy @ 2016-01-16 19:46 UTC (permalink / raw)
To: johan; +Cc: linux-usb, linux-kernel, Konstantin Shkolnyy
cp210x_get_config and cp210x_set_config are cumbersome to use. This change
introduces new register access functions to replace them. New functions
are not yet called - the switch is done gradually in following changes.
Signed-off-by: Konstantin Shkolnyy <konstantin.shkolnyy@gmail.com>
---
change in v3: Presented new function addition as a separate patch #1,
to simplify code review.
drivers/usb/serial/cp210x.c | 166 ++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 166 insertions(+)
diff --git a/drivers/usb/serial/cp210x.c b/drivers/usb/serial/cp210x.c
index fd67958..324eb7e 100644
--- a/drivers/usb/serial/cp210x.c
+++ b/drivers/usb/serial/cp210x.c
@@ -323,6 +323,172 @@ struct cp210x_comm_status {
#define PURGE_ALL 0x000f
/*
+ * Reads a variable-sized block of CP210X_ registers, identified by req.
+ * Returns data into buf in native USB byte order.
+ */
+static int cp210x_read_reg_block(struct usb_serial_port *port, u8 req,
+ void *buf, int bufsize)
+{
+ struct usb_serial *serial = port->serial;
+ struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
+ void *dmabuf;
+ int result;
+
+ dmabuf = kmalloc(bufsize, GFP_KERNEL);
+ if (!dmabuf) {
+ /*
+ * FIXME Some callers don't bother to check for error,
+ * at least give them consistent junk until they are fixed
+ */
+ memset(buf, 0, bufsize);
+ return -ENOMEM;
+ }
+
+ result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
+ req, REQTYPE_INTERFACE_TO_HOST, 0,
+ port_priv->bInterfaceNumber, dmabuf, bufsize,
+ USB_CTRL_SET_TIMEOUT);
+ if (result == bufsize) {
+ memcpy(buf, dmabuf, bufsize);
+ result = 0;
+ } else {
+ dev_err(&port->dev, "failed get req 0x%x size %d status: %d\n",
+ req, bufsize, result);
+ if (result >= 0)
+ result = -EPROTO;
+
+ /*
+ * FIXME Some callers don't bother to check for error,
+ * at least give them consistent junk until they are fixed
+ */
+ memset(buf, 0, bufsize);
+ }
+
+ kfree(dmabuf);
+
+ return result;
+}
+
+/*
+ * Reads any 32-bit CP210X_ register identified by req.
+ */
+static int cp210x_read_u32_reg(struct usb_serial_port *port, u8 req, u32 *val)
+{
+ __le32 le32_val;
+ int err;
+
+ err = cp210x_read_reg_block(port, req, &le32_val, sizeof(le32_val));
+ if (err) {
+ /*
+ * FIXME Some callers don't bother to check for error,
+ * at least give them consistent junk until they are fixed
+ */
+ *val = 0;
+ return err;
+ }
+
+ *val = le32_to_cpu(le32_val);
+
+ return 0;
+}
+
+/*
+ * Reads any 16-bit CP210X_ register identified by req.
+ */
+static int cp210x_read_u16_reg(struct usb_serial_port *port, u8 req, u16 *val)
+{
+ __le16 le16_val;
+ int err;
+
+ err = cp210x_read_reg_block(port, req, &le16_val, sizeof(le16_val));
+ if (err)
+ return err;
+
+ *val = le16_to_cpu(le16_val);
+
+ return 0;
+}
+
+/*
+ * Reads any 8-bit CP210X_ register identified by req.
+ */
+static int cp210x_read_u8_reg(struct usb_serial_port *port, u8 req, u8 *val)
+{
+ return cp210x_read_reg_block(port, req, val, sizeof(*val));
+}
+
+/*
+ * Writes any 16-bit CP210X_ register (req) whose value is passed
+ * entirely in the wValue field of the USB request.
+ */
+static int cp210x_write_u16_reg(struct usb_serial_port *port, u8 req, u16 val)
+{
+ struct usb_serial *serial = port->serial;
+ struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
+ int result;
+
+ result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
+ req, REQTYPE_HOST_TO_INTERFACE, val,
+ port_priv->bInterfaceNumber, NULL, 0,
+ USB_CTRL_SET_TIMEOUT);
+ if (result < 0) {
+ dev_err(&port->dev, "failed set request 0x%x status: %d\n",
+ req, result);
+ }
+
+ return result;
+}
+
+/*
+ * Writes a variable-sized block of CP210X_ registers, identified by req.
+ * Data in buf must be in native USB byte order.
+ */
+static int cp210x_write_reg_block(struct usb_serial_port *port, u8 req,
+ void *buf, int bufsize)
+{
+ struct usb_serial *serial = port->serial;
+ struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
+ void *dmabuf;
+ int result;
+
+ dmabuf = kmalloc(bufsize, GFP_KERNEL);
+ if (!dmabuf)
+ return -ENOMEM;
+
+ memcpy(dmabuf, buf, bufsize);
+
+ result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
+ req, REQTYPE_HOST_TO_INTERFACE, 0,
+ port_priv->bInterfaceNumber, dmabuf, bufsize,
+ USB_CTRL_SET_TIMEOUT);
+
+ kfree(dmabuf);
+
+ if (result == bufsize) {
+ result = 0;
+ } else {
+ dev_err(&port->dev, "failed set req 0x%x size %d status: %d\n",
+ req, bufsize, result);
+ if (result >= 0)
+ result = -EPROTO;
+ }
+
+ return result;
+}
+
+/*
+ * Writes any 32-bit CP210X_ register identified by req.
+ */
+static int cp210x_write_u32_reg(struct usb_serial_port *port, u8 req, u32 val)
+{
+ __le32 le32_val;
+
+ le32_val = cpu_to_le32(val);
+
+ return cp210x_write_reg_block(port, req, &le32_val, sizeof(le32_val));
+}
+
+/*
* cp210x_get_config
* Reads from the CP210x configuration registers
* 'size' is specified in bytes.
--
1.8.4.5
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH v3 1/4 RESEND] USB: serial: cp210x: New register access functions.
2016-01-16 19:46 [PATCH v3 1/4 RESEND] USB: serial: cp210x: New register access functions Konstantin Shkolnyy
@ 2016-01-18 17:32 ` Johan Hovold
2016-01-18 19:59 ` Konstantin Shkolnyy
0 siblings, 1 reply; 4+ messages in thread
From: Johan Hovold @ 2016-01-18 17:32 UTC (permalink / raw)
To: Konstantin Shkolnyy; +Cc: johan, linux-usb, linux-kernel
On Sat, Jan 16, 2016 at 01:46:24PM -0600, Konstantin Shkolnyy wrote:
> cp210x_get_config and cp210x_set_config are cumbersome to use. This change
> introduces new register access functions to replace them. New functions
> are not yet called - the switch is done gradually in following changes.
>
> Signed-off-by: Konstantin Shkolnyy <konstantin.shkolnyy@gmail.com>
> ---
> change in v3: Presented new function addition as a separate patch #1,
> to simplify code review.
Thanks for the v3.
You should not be adding (static) functions before they have a caller as
this generates compiler warnings.
Please define the new helper functions when converting their call
sites. You can split the 8, 16 and 32-bit helpers in separate patches and
possibly also separate read and write if that makes sense in order to
keep the diffs smaller.
> drivers/usb/serial/cp210x.c | 166 ++++++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 166 insertions(+)
>
> diff --git a/drivers/usb/serial/cp210x.c b/drivers/usb/serial/cp210x.c
> index fd67958..324eb7e 100644
> --- a/drivers/usb/serial/cp210x.c
> +++ b/drivers/usb/serial/cp210x.c
> @@ -323,6 +323,172 @@ struct cp210x_comm_status {
> #define PURGE_ALL 0x000f
>
> /*
> + * Reads a variable-sized block of CP210X_ registers, identified by req.
> + * Returns data into buf in native USB byte order.
> + */
> +static int cp210x_read_reg_block(struct usb_serial_port *port, u8 req,
> + void *buf, int bufsize)
> +{
> + struct usb_serial *serial = port->serial;
> + struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
> + void *dmabuf;
> + int result;
> +
> + dmabuf = kmalloc(bufsize, GFP_KERNEL);
> + if (!dmabuf) {
> + /*
> + * FIXME Some callers don't bother to check for error,
> + * at least give them consistent junk until they are fixed
> + */
> + memset(buf, 0, bufsize);
Instead of adding these FIXMEs and possibly risk introducing regressions
(these functions used to leave the previous values unchanged on errors),
how about adding the missing error handling first?
> + return -ENOMEM;
> + }
Thanks,
Johan
^ permalink raw reply [flat|nested] 4+ messages in thread
* RE: [PATCH v3 1/4 RESEND] USB: serial: cp210x: New register access functions.
2016-01-18 17:32 ` Johan Hovold
@ 2016-01-18 19:59 ` Konstantin Shkolnyy
2016-01-18 20:08 ` Johan Hovold
0 siblings, 1 reply; 4+ messages in thread
From: Konstantin Shkolnyy @ 2016-01-18 19:59 UTC (permalink / raw)
To: Johan Hovold, Konstantin Shkolnyy
Cc: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org
> -----Original Message-----
> From: linux-usb-owner@vger.kernel.org [mailto:linux-usb-
> owner@vger.kernel.org] On Behalf Of Johan Hovold
> Sent: Monday, January 18, 2016 11:33
> To: Konstantin Shkolnyy
> Cc: johan@kernel.org; linux-usb@vger.kernel.org; linux-
> kernel@vger.kernel.org
> Subject: Re: [PATCH v3 1/4 RESEND] USB: serial: cp210x: New register access
> functions.
>
> On Sat, Jan 16, 2016 at 01:46:24PM -0600, Konstantin Shkolnyy wrote:
> > cp210x_get_config and cp210x_set_config are cumbersome to use. This
> change
> > introduces new register access functions to replace them. New functions
> > are not yet called - the switch is done gradually in following changes.
> >
> > Signed-off-by: Konstantin Shkolnyy <konstantin.shkolnyy@gmail.com>
> > ---
> > change in v3: Presented new function addition as a separate patch #1,
> > to simplify code review.
>
> Thanks for the v3.
>
> You should not be adding (static) functions before they have a caller as
> this generates compiler warnings.
>
> Please define the new helper functions when converting their call
> sites. You can split the 8, 16 and 32-bit helpers in separate patches and
> possibly also separate read and write if that makes sense in order to
> keep the diffs smaller.
I did split 8, 16 and 32 into 3 patches in v2, then it seemed to me it would be easier to review if I also separated new functions.
I can just use an amended v2 as v4.
I add reads and writes together to avoid type mismatches in the code that reads/writes the same variable,
> > drivers/usb/serial/cp210x.c | 166
> ++++++++++++++++++++++++++++++++++++++++++++
> > 1 file changed, 166 insertions(+)
> >
> > diff --git a/drivers/usb/serial/cp210x.c b/drivers/usb/serial/cp210x.c
> > index fd67958..324eb7e 100644
> > --- a/drivers/usb/serial/cp210x.c
> > +++ b/drivers/usb/serial/cp210x.c
> > @@ -323,6 +323,172 @@ struct cp210x_comm_status {
> > #define PURGE_ALL 0x000f
> >
> > /*
> > + * Reads a variable-sized block of CP210X_ registers, identified by req.
> > + * Returns data into buf in native USB byte order.
> > + */
> > +static int cp210x_read_reg_block(struct usb_serial_port *port, u8 req,
> > + void *buf, int bufsize)
> > +{
> > + struct usb_serial *serial = port->serial;
> > + struct cp210x_port_private *port_priv =
> usb_get_serial_port_data(port);
> > + void *dmabuf;
> > + int result;
> > +
> > + dmabuf = kmalloc(bufsize, GFP_KERNEL);
> > + if (!dmabuf) {
> > + /*
> > + * FIXME Some callers don't bother to check for error,
> > + * at least give them consistent junk until they are fixed
> > + */
> > + memset(buf, 0, bufsize);
>
> Instead of adding these FIXMEs and possibly risk introducing regressions
> (these functions used to leave the previous values unchanged on errors),
The current cp210x_get_config() does clear the caller's storage on an error. So my refactoring just preserves the old behavior.
> how about adding the missing error handling first?
It's easier to work with and see bugs in cleaned up code, that's why I do it in this order.
I do intend to fix the error handling later.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v3 1/4 RESEND] USB: serial: cp210x: New register access functions.
2016-01-18 19:59 ` Konstantin Shkolnyy
@ 2016-01-18 20:08 ` Johan Hovold
0 siblings, 0 replies; 4+ messages in thread
From: Johan Hovold @ 2016-01-18 20:08 UTC (permalink / raw)
To: Konstantin Shkolnyy
Cc: Johan Hovold, Konstantin Shkolnyy, linux-usb@vger.kernel.org,
linux-kernel@vger.kernel.org
On Mon, Jan 18, 2016 at 07:59:48PM +0000, Konstantin Shkolnyy wrote:
> > -----Original Message-----
> > From: linux-usb-owner@vger.kernel.org [mailto:linux-usb-
> > owner@vger.kernel.org] On Behalf Of Johan Hovold
> > Sent: Monday, January 18, 2016 11:33
> > To: Konstantin Shkolnyy
> > Cc: johan@kernel.org; linux-usb@vger.kernel.org; linux-
> > kernel@vger.kernel.org
> > Subject: Re: [PATCH v3 1/4 RESEND] USB: serial: cp210x: New register access
> > functions.
> >
> > On Sat, Jan 16, 2016 at 01:46:24PM -0600, Konstantin Shkolnyy wrote:
> > > cp210x_get_config and cp210x_set_config are cumbersome to use. This
> > change
> > > introduces new register access functions to replace them. New functions
> > > are not yet called - the switch is done gradually in following changes.
> > >
> > > Signed-off-by: Konstantin Shkolnyy <konstantin.shkolnyy@gmail.com>
> > > ---
> > > change in v3: Presented new function addition as a separate patch #1,
> > > to simplify code review.
> >
> > Thanks for the v3.
> >
> > You should not be adding (static) functions before they have a caller as
> > this generates compiler warnings.
> >
> > Please define the new helper functions when converting their call
> > sites. You can split the 8, 16 and 32-bit helpers in separate patches and
> > possibly also separate read and write if that makes sense in order to
> > keep the diffs smaller.
>
> I did split 8, 16 and 32 into 3 patches in v2, then it seemed to me it
> would be easier to review if I also separated new functions.
> I can just use an amended v2 as v4.
Sounds good.
> I add reads and writes together to avoid type mismatches in the code
> that reads/writes the same variable,
Probably makes sense.
> > > drivers/usb/serial/cp210x.c | 166
> > ++++++++++++++++++++++++++++++++++++++++++++
> > > 1 file changed, 166 insertions(+)
> > >
> > > diff --git a/drivers/usb/serial/cp210x.c b/drivers/usb/serial/cp210x.c
> > > index fd67958..324eb7e 100644
> > > --- a/drivers/usb/serial/cp210x.c
> > > +++ b/drivers/usb/serial/cp210x.c
> > > @@ -323,6 +323,172 @@ struct cp210x_comm_status {
> > > #define PURGE_ALL 0x000f
> > >
> > > /*
> > > + * Reads a variable-sized block of CP210X_ registers, identified by req.
> > > + * Returns data into buf in native USB byte order.
> > > + */
> > > +static int cp210x_read_reg_block(struct usb_serial_port *port, u8 req,
> > > + void *buf, int bufsize)
> > > +{
> > > + struct usb_serial *serial = port->serial;
> > > + struct cp210x_port_private *port_priv =
> > usb_get_serial_port_data(port);
> > > + void *dmabuf;
> > > + int result;
> > > +
> > > + dmabuf = kmalloc(bufsize, GFP_KERNEL);
> > > + if (!dmabuf) {
> > > + /*
> > > + * FIXME Some callers don't bother to check for error,
> > > + * at least give them consistent junk until they are fixed
> > > + */
> > > + memset(buf, 0, bufsize);
> >
> > Instead of adding these FIXMEs and possibly risk introducing regressions
> > (these functions used to leave the previous values unchanged on errors),
>
> The current cp210x_get_config() does clear the caller's storage on an
> error. So my refactoring just preserves the old behavior.
Ah, the current code only fails to clear the callers storage on
allocation errors. Then I guess it's ok to fix up the call sites after.
> > how about adding the missing error handling first?
>
> It's easier to work with and see bugs in cleaned up code, that's why I
> do it in this order.
> I do intend to fix the error handling later.
Sounds good.
Thanks,
Johan
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2016-01-18 20:08 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-01-16 19:46 [PATCH v3 1/4 RESEND] USB: serial: cp210x: New register access functions Konstantin Shkolnyy
2016-01-18 17:32 ` Johan Hovold
2016-01-18 19:59 ` Konstantin Shkolnyy
2016-01-18 20:08 ` Johan Hovold
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).