Skip to content

Commit f5cfbec

Browse files
himadricsjhovold
authored andcommitted
USB: serial: cp210x: use usb_control_msg_recv() and usb_control_msg_send()
The new wrapper functions for usb_control_msg() can accept data from stack and treat short reads as errors. Hence use the wrappers functions. Please note that because of this change, cp210x_read_reg_block() will no longer log the length of short reads. Signed-off-by: Himadri Pandya <[email protected]> Link: https://lore.kernel.org/r/[email protected] [ johan: style changes ] Signed-off-by: Johan Hovold <[email protected]>
1 parent 74f2664 commit f5cfbec

File tree

1 file changed

+31
-78
lines changed

1 file changed

+31
-78
lines changed

drivers/usb/serial/cp210x.c

Lines changed: 31 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -629,30 +629,20 @@ static int cp210x_read_reg_block(struct usb_serial_port *port, u8 req,
629629
{
630630
struct usb_serial *serial = port->serial;
631631
struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
632-
void *dmabuf;
633632
int result;
634633

635-
dmabuf = kmalloc(bufsize, GFP_KERNEL);
636-
if (!dmabuf)
637-
return -ENOMEM;
638634

639-
result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
640-
req, REQTYPE_INTERFACE_TO_HOST, 0,
641-
port_priv->bInterfaceNumber, dmabuf, bufsize,
642-
USB_CTRL_GET_TIMEOUT);
643-
if (result == bufsize) {
644-
memcpy(buf, dmabuf, bufsize);
645-
result = 0;
646-
} else {
635+
result = usb_control_msg_recv(serial->dev, 0, req,
636+
REQTYPE_INTERFACE_TO_HOST, 0,
637+
port_priv->bInterfaceNumber, buf, bufsize,
638+
USB_CTRL_SET_TIMEOUT, GFP_KERNEL);
639+
if (result) {
647640
dev_err(&port->dev, "failed get req 0x%x size %d status: %d\n",
648641
req, bufsize, result);
649-
if (result >= 0)
650-
result = -EIO;
642+
return result;
651643
}
652644

653-
kfree(dmabuf);
654-
655-
return result;
645+
return 0;
656646
}
657647

658648
/*
@@ -670,31 +660,19 @@ static int cp210x_read_u8_reg(struct usb_serial_port *port, u8 req, u8 *val)
670660
static int cp210x_read_vendor_block(struct usb_serial *serial, u8 type, u16 val,
671661
void *buf, int bufsize)
672662
{
673-
void *dmabuf;
674663
int result;
675664

676-
dmabuf = kmalloc(bufsize, GFP_KERNEL);
677-
if (!dmabuf)
678-
return -ENOMEM;
679-
680-
result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
681-
CP210X_VENDOR_SPECIFIC, type, val,
682-
cp210x_interface_num(serial), dmabuf, bufsize,
683-
USB_CTRL_GET_TIMEOUT);
684-
if (result == bufsize) {
685-
memcpy(buf, dmabuf, bufsize);
686-
result = 0;
687-
} else {
665+
result = usb_control_msg_recv(serial->dev, 0, CP210X_VENDOR_SPECIFIC,
666+
type, val, cp210x_interface_num(serial), buf, bufsize,
667+
USB_CTRL_GET_TIMEOUT, GFP_KERNEL);
668+
if (result) {
688669
dev_err(&serial->interface->dev,
689670
"failed to get vendor val 0x%04x size %d: %d\n", val,
690671
bufsize, result);
691-
if (result >= 0)
692-
result = -EIO;
672+
return result;
693673
}
694674

695-
kfree(dmabuf);
696-
697-
return result;
675+
return 0;
698676
}
699677

700678
/*
@@ -728,21 +706,13 @@ static int cp210x_write_reg_block(struct usb_serial_port *port, u8 req,
728706
{
729707
struct usb_serial *serial = port->serial;
730708
struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
731-
void *dmabuf;
732709
int result;
733710

734-
dmabuf = kmemdup(buf, bufsize, GFP_KERNEL);
735-
if (!dmabuf)
736-
return -ENOMEM;
737-
738-
result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
739-
req, REQTYPE_HOST_TO_INTERFACE, 0,
740-
port_priv->bInterfaceNumber, dmabuf, bufsize,
741-
USB_CTRL_SET_TIMEOUT);
742-
743-
kfree(dmabuf);
744-
745-
if (result < 0) {
711+
result = usb_control_msg_send(serial->dev, 0, req,
712+
REQTYPE_HOST_TO_INTERFACE, 0,
713+
port_priv->bInterfaceNumber, buf, bufsize,
714+
USB_CTRL_SET_TIMEOUT, GFP_KERNEL);
715+
if (result) {
746716
dev_err(&port->dev, "failed set req 0x%x size %d status: %d\n",
747717
req, bufsize, result);
748718
return result;
@@ -771,21 +741,12 @@ static int cp210x_write_u32_reg(struct usb_serial_port *port, u8 req, u32 val)
771741
static int cp210x_write_vendor_block(struct usb_serial *serial, u8 type,
772742
u16 val, void *buf, int bufsize)
773743
{
774-
void *dmabuf;
775744
int result;
776745

777-
dmabuf = kmemdup(buf, bufsize, GFP_KERNEL);
778-
if (!dmabuf)
779-
return -ENOMEM;
780-
781-
result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
782-
CP210X_VENDOR_SPECIFIC, type, val,
783-
cp210x_interface_num(serial), dmabuf, bufsize,
784-
USB_CTRL_SET_TIMEOUT);
785-
786-
kfree(dmabuf);
787-
788-
if (result < 0) {
746+
result = usb_control_msg_send(serial->dev, 0, CP210X_VENDOR_SPECIFIC,
747+
type, val, cp210x_interface_num(serial), buf, bufsize,
748+
USB_CTRL_SET_TIMEOUT, GFP_KERNEL);
749+
if (result) {
789750
dev_err(&serial->interface->dev,
790751
"failed to set vendor val 0x%04x size %d: %d\n", val,
791752
bufsize, result);
@@ -950,29 +911,21 @@ static int cp210x_get_tx_queue_byte_count(struct usb_serial_port *port,
950911
{
951912
struct usb_serial *serial = port->serial;
952913
struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
953-
struct cp210x_comm_status *sts;
914+
struct cp210x_comm_status sts;
954915
int result;
955916

956-
sts = kmalloc(sizeof(*sts), GFP_KERNEL);
957-
if (!sts)
958-
return -ENOMEM;
959-
960-
result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
961-
CP210X_GET_COMM_STATUS, REQTYPE_INTERFACE_TO_HOST,
962-
0, port_priv->bInterfaceNumber, sts, sizeof(*sts),
963-
USB_CTRL_GET_TIMEOUT);
964-
if (result == sizeof(*sts)) {
965-
*count = le32_to_cpu(sts->ulAmountInOutQueue);
966-
result = 0;
967-
} else {
917+
result = usb_control_msg_recv(serial->dev, 0, CP210X_GET_COMM_STATUS,
918+
REQTYPE_INTERFACE_TO_HOST, 0,
919+
port_priv->bInterfaceNumber, &sts, sizeof(sts),
920+
USB_CTRL_GET_TIMEOUT, GFP_KERNEL);
921+
if (result) {
968922
dev_err(&port->dev, "failed to get comm status: %d\n", result);
969-
if (result >= 0)
970-
result = -EIO;
923+
return result;
971924
}
972925

973-
kfree(sts);
926+
*count = le32_to_cpu(sts.ulAmountInOutQueue);
974927

975-
return result;
928+
return 0;
976929
}
977930

978931
static bool cp210x_tx_empty(struct usb_serial_port *port)

0 commit comments

Comments
 (0)