Return-Path: Received: from mail-sn1nam02on0137.outbound.protection.outlook.com ([104.47.36.137]:63678 "EHLO NAM02-SN1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1032035AbeCAPa2 (ORCPT ); Thu, 1 Mar 2018 10:30:28 -0500 From: Sasha Levin To: "stable@vger.kernel.org" , "stable-commits@vger.kernel.org" CC: Stephane Grosjean , Marc Kleine-Budde , Sasha Levin Subject: [added to the 4.1 stable tree] can: peak: fix potential bug in packet fragmentation Date: Thu, 1 Mar 2018 15:24:29 +0000 Message-ID: <20180301152116.1486-194-alexander.levin@microsoft.com> References: <20180301152116.1486-1-alexander.levin@microsoft.com> In-Reply-To: <20180301152116.1486-1-alexander.levin@microsoft.com> Content-Language: en-US Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Sender: stable-owner@vger.kernel.org List-ID: From: Stephane Grosjean This patch has been added to the 4.1 stable tree. If you have any objections, please let us know. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D [ Upstream commit d8a243af1a68395e07ac85384a2740d4134c67f4 ] In some rare conditions when running one PEAK USB-FD interface over a non high-speed USB controller, one useless USB fragment might be sent. This patch fixes the way a USB command is fragmented when its length is greater than 64 bytes and when the underlying USB controller is not a high-speed one. Signed-off-by: Stephane Grosjean Cc: linux-stable Signed-off-by: Marc Kleine-Budde Signed-off-by: Sasha Levin --- drivers/net/can/usb/peak_usb/pcan_usb_fd.c | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/drivers/net/can/usb/peak_usb/pcan_usb_fd.c b/drivers/net/can/u= sb/peak_usb/pcan_usb_fd.c index ce44a033f63b..64cc86a82b2d 100644 --- a/drivers/net/can/usb/peak_usb/pcan_usb_fd.c +++ b/drivers/net/can/usb/peak_usb/pcan_usb_fd.c @@ -184,7 +184,7 @@ static int pcan_usb_fd_send_cmd(struct peak_usb_device = *dev, void *cmd_tail) void *cmd_head =3D pcan_usb_fd_cmd_buffer(dev); int err =3D 0; u8 *packet_ptr; - int i, n =3D 1, packet_len; + int packet_len; ptrdiff_t cmd_len; =20 /* usb device unregistered? */ @@ -201,17 +201,13 @@ static int pcan_usb_fd_send_cmd(struct peak_usb_devic= e *dev, void *cmd_tail) } =20 packet_ptr =3D cmd_head; + packet_len =3D cmd_len; =20 /* firmware is not able to re-assemble 512 bytes buffer in full-speed */ - if ((dev->udev->speed !=3D USB_SPEED_HIGH) && - (cmd_len > PCAN_UFD_LOSPD_PKT_SIZE)) { - packet_len =3D PCAN_UFD_LOSPD_PKT_SIZE; - n +=3D cmd_len / packet_len; - } else { - packet_len =3D cmd_len; - } + if (unlikely(dev->udev->speed !=3D USB_SPEED_HIGH)) + packet_len =3D min(packet_len, PCAN_UFD_LOSPD_PKT_SIZE); =20 - for (i =3D 0; i < n; i++) { + do { err =3D usb_bulk_msg(dev->udev, usb_sndbulkpipe(dev->udev, PCAN_USBPRO_EP_CMDOUT), @@ -224,7 +220,12 @@ static int pcan_usb_fd_send_cmd(struct peak_usb_device= *dev, void *cmd_tail) } =20 packet_ptr +=3D packet_len; - } + cmd_len -=3D packet_len; + + if (cmd_len < PCAN_UFD_LOSPD_PKT_SIZE) + packet_len =3D cmd_len; + + } while (packet_len > 0); =20 return err; } --=20 2.14.1