Skip to content

Commit d9f0d82

Browse files
committed
USB: legousbtower: use usb_control_msg_recv()
The usb_control_msg_recv() function can handle data on the stack, as well as properly detecting short reads, so move to use that function instead of the older usb_control_msg() call. This ends up removing a lot of extra lines in the driver. v2: change API of usb_control_msg_send() Cc: Juergen Stuber <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 9ad71af commit d9f0d82

File tree

1 file changed

+20
-41
lines changed

1 file changed

+20
-41
lines changed

drivers/usb/misc/legousbtower.c

Lines changed: 20 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -308,15 +308,9 @@ static int tower_open(struct inode *inode, struct file *file)
308308
int subminor;
309309
int retval = 0;
310310
struct usb_interface *interface;
311-
struct tower_reset_reply *reset_reply;
311+
struct tower_reset_reply reset_reply;
312312
int result;
313313

314-
reset_reply = kmalloc(sizeof(*reset_reply), GFP_KERNEL);
315-
if (!reset_reply) {
316-
retval = -ENOMEM;
317-
goto exit;
318-
}
319-
320314
nonseekable_open(inode, file);
321315
subminor = iminor(inode);
322316

@@ -347,15 +341,12 @@ static int tower_open(struct inode *inode, struct file *file)
347341
}
348342

349343
/* reset the tower */
350-
result = usb_control_msg(dev->udev,
351-
usb_rcvctrlpipe(dev->udev, 0),
352-
LEGO_USB_TOWER_REQUEST_RESET,
353-
USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_DEVICE,
354-
0,
355-
0,
356-
reset_reply,
357-
sizeof(*reset_reply),
358-
1000);
344+
result = usb_control_msg_recv(dev->udev, 0,
345+
LEGO_USB_TOWER_REQUEST_RESET,
346+
USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_DEVICE,
347+
0, 0,
348+
&reset_reply, sizeof(reset_reply), 1000,
349+
GFP_KERNEL);
359350
if (result < 0) {
360351
dev_err(&dev->udev->dev,
361352
"LEGO USB Tower reset control request failed\n");
@@ -394,7 +385,6 @@ static int tower_open(struct inode *inode, struct file *file)
394385
mutex_unlock(&dev->lock);
395386

396387
exit:
397-
kfree(reset_reply);
398388
return retval;
399389
}
400390

@@ -753,7 +743,7 @@ static int tower_probe(struct usb_interface *interface, const struct usb_device_
753743
struct device *idev = &interface->dev;
754744
struct usb_device *udev = interface_to_usbdev(interface);
755745
struct lego_usb_tower *dev;
756-
struct tower_get_version_reply *get_version_reply = NULL;
746+
struct tower_get_version_reply get_version_reply;
757747
int retval = -ENOMEM;
758748
int result;
759749

@@ -798,34 +788,25 @@ static int tower_probe(struct usb_interface *interface, const struct usb_device_
798788
dev->interrupt_in_interval = interrupt_in_interval ? interrupt_in_interval : dev->interrupt_in_endpoint->bInterval;
799789
dev->interrupt_out_interval = interrupt_out_interval ? interrupt_out_interval : dev->interrupt_out_endpoint->bInterval;
800790

801-
get_version_reply = kmalloc(sizeof(*get_version_reply), GFP_KERNEL);
802-
if (!get_version_reply) {
803-
retval = -ENOMEM;
804-
goto error;
805-
}
806-
807791
/* get the firmware version and log it */
808-
result = usb_control_msg(udev,
809-
usb_rcvctrlpipe(udev, 0),
810-
LEGO_USB_TOWER_REQUEST_GET_VERSION,
811-
USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_DEVICE,
812-
0,
813-
0,
814-
get_version_reply,
815-
sizeof(*get_version_reply),
816-
1000);
817-
if (result != sizeof(*get_version_reply)) {
818-
if (result >= 0)
819-
result = -EIO;
792+
result = usb_control_msg_recv(udev, 0,
793+
LEGO_USB_TOWER_REQUEST_GET_VERSION,
794+
USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_DEVICE,
795+
0,
796+
0,
797+
&get_version_reply,
798+
sizeof(get_version_reply),
799+
1000, GFP_KERNEL);
800+
if (!result) {
820801
dev_err(idev, "get version request failed: %d\n", result);
821802
retval = result;
822803
goto error;
823804
}
824805
dev_info(&interface->dev,
825806
"LEGO USB Tower firmware version is %d.%d build %d\n",
826-
get_version_reply->major,
827-
get_version_reply->minor,
828-
le16_to_cpu(get_version_reply->build_no));
807+
get_version_reply.major,
808+
get_version_reply.minor,
809+
le16_to_cpu(get_version_reply.build_no));
829810

830811
/* we can register the device now, as it is ready */
831812
usb_set_intfdata(interface, dev);
@@ -844,11 +825,9 @@ static int tower_probe(struct usb_interface *interface, const struct usb_device_
844825
USB_MAJOR, dev->minor);
845826

846827
exit:
847-
kfree(get_version_reply);
848828
return retval;
849829

850830
error:
851-
kfree(get_version_reply);
852831
tower_delete(dev);
853832
return retval;
854833
}

0 commit comments

Comments
 (0)