From patchwork Mon Oct 19 10:06:32 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Gleixner X-Patchwork-Id: 11844027 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 09D5AC433E7 for ; Mon, 19 Oct 2020 10:20:34 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 2023A22263 for ; Mon, 19 Oct 2020 10:20:33 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="ISt9Z2qz"; dkim=fail reason="signature verification failed" (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="WK/KUF9n"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="x8rtyLqc" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 2023A22263 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=linutronix.de Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:References:Subject:To:From:Date: Message-Id:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:List-Owner; bh=mfYnZll9YmIKyy+ogl5VjZXgKKjN/L5i7WhK0DcPc9c=; b=ISt9Z2qzHjztqSDxDHUwTPSEZ YsUQQYuKbCt30YgVQ4udF8thvHBhM4W1pKAzNHJ/IU/sr14oWP8aEst6Zdrj8ZkiVZs2E1qy9Q9qE 7zPMkCRJ04G2Av1+rE1kQqZjDxjTS7YWEVhzEA93tVjOxaR++CkF69CEtGIWxhUSxJNsMKC4qCT/D V4p7KwoWDVMyToG1UfH10lRoQ2EQGaeVsF3272sH5VTJYrGlx0ci79dbIRqWZUJdGvdCF/oTTNezf ZUNOiX5KqoMQngEmv+gaW5VMKSUQBx4qjb9R/1Kl/lzB4Z91uJZLkoFaLV5os1ZyYlmb6R7H6z583 EH9IVamoA==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kUSFV-000331-U3; Mon, 19 Oct 2020 10:18:42 +0000 Received: from galois.linutronix.de ([2a0a:51c0:0:12e:550::1]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kUSEw-0002oX-IX for linux-arm-kernel@lists.infradead.org; Mon, 19 Oct 2020 10:18:10 +0000 Message-Id: <20201019101109.903651690@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1603102685; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: references:references; bh=GspGZAgtTq6gMYfmmkZzHxj2wK5gwCFO/dMv4c+qGNg=; b=WK/KUF9nx+OzfrXpW6AZP63Mk3FL/cEjZl3mU1F9QJ+O6FJ7qFDBgNIs/DFAFqo32p+kRL wd5kaaQ3IHlGruoBf+Rn4HWEy/upe3XziU6BeSZv4kqnKeCytgoMKdNx04mw/kuVTUsi7/ PYSOi/w361WypClc90JwGEnpGTOftBHhyjUjPjTSY2BTbxsTTpxQ5+n0W7qFt+i/X6h3Vb cXkJdxA60UzvFfPnepVkhrjd0HmQQD2yVv8bBmMjy8rEm00gd3dGvgMv8DKmKFb7xE8ttZ W8fcjFZdNQinRYMJy79YUEA9tvaulgCmIKr+DDzUp3pjRz/N51+z8dGvgR3vng== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1603102685; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: references:references; bh=GspGZAgtTq6gMYfmmkZzHxj2wK5gwCFO/dMv4c+qGNg=; b=x8rtyLqc8Pja+okYr4ByLhKQBeW2Ox67gJGffT2WLFy/CoG/rB/9qGuafa5FJRoM3ny9Di GdMrkcGxm7OgQlDg== Date: Mon, 19 Oct 2020 12:06:32 +0200 From: Thomas Gleixner To: LKML Subject: [patch V2 03/13] USB: serial: keyspan_pda: Consolidate room query References: <20201019100629.419020859@linutronix.de> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20201019_061806_863914_8B7ECBF3 X-CRM114-Status: GOOD ( 22.13 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Felipe Balbi , Thomas Winischhofer , Mathias Nyman , Peter Zijlstra , Greg Kroah-Hartman , Sebastian Andrzej Siewior , linux-usb@vger.kernel.org, Johan Hovold , Krzysztof Kozlowski , Valentina Manea , linux-samsung-soc@vger.kernel.org, Kukjin Kim , Alan Stern , Duncan Sands , "Ahmed S. Darwish" , linux-omap@vger.kernel.org, Shuah Khan , linux-arm-kernel@lists.infradead.org Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: Sebastian Andrzej Siewior Having two copies of the same code doesn't make the code more readable and allocating a buffer of 1 byte for a synchronous operation is a pointless exercise. Allocate a byte buffer at init which can be used instead. The buffer is only used in open() and tty->write(). Console writes are not calling into the query. open() obviously happens before write() and the writes are serialized by bit 0 of port->write_urbs_free which protects also the transaction itself. Move the actual query into a helper function and cleanup the usage sites in keyspan_pda_write() and keyspan_pda_open(). Signed-off-by: Sebastian Andrzej Siewior Signed-off-by: Thomas Gleixner Cc: Johan Hovold Cc: Greg Kroah-Hartman Cc: linux-usb@vger.kernel.org --- V2; Allocate a real buffer (Alan) --- drivers/usb/serial/keyspan_pda.c | 108 +++++++++++++++++---------------------- 1 file changed, 49 insertions(+), 59 deletions(-) --- a/drivers/usb/serial/keyspan_pda.c +++ b/drivers/usb/serial/keyspan_pda.c @@ -47,6 +47,7 @@ struct keyspan_pda_private { struct work_struct unthrottle_work; struct usb_serial *serial; struct usb_serial_port *port; + u8 *query_buf; }; @@ -436,6 +437,31 @@ static int keyspan_pda_tiocmset(struct t return rc; } +/* + * Using priv->query_buf is safe here because this is only called for TTY + * operations open() and write(). write() comes post open() obviously and + * write() itself is serialized via bit 0 of port->write_urbs_free. Console + * writes are never calling into this. + */ +static int keyspan_pda_query_room(struct usb_serial *serial, + struct keyspan_pda_private *priv) +{ + int res; + + res = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0), + 6, /* write_room */ + USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_IN, + 0, /* value */ + 0, /* index */ + priv->query_buf, + 1, + 2000); + if (res != 1) + return res < 0 ? res : -EIO; + + return (unsigned int)*priv->query_buf; +} + static int keyspan_pda_write(struct tty_struct *tty, struct usb_serial_port *port, const unsigned char *buf, int count) { @@ -483,39 +509,16 @@ static int keyspan_pda_write(struct tty_ * usage because the console write can't sleep. */ if (count > priv->tx_room && tty) { - u8 *room; - - room = kmalloc(1, GFP_KERNEL); - if (!room) { - rc = -ENOMEM; - goto exit; - } - - rc = usb_control_msg(serial->dev, - usb_rcvctrlpipe(serial->dev, 0), - 6, /* write_room */ - USB_TYPE_VENDOR | USB_RECIP_INTERFACE - | USB_DIR_IN, - 0, /* value: 0 means "remaining room" */ - 0, /* index */ - room, - 1, - 2000); - if (rc > 0) { - dev_dbg(&port->dev, "roomquery says %d\n", *room); - priv->tx_room = *room; - } - kfree(room); + rc = keyspan_pda_query_room(serial, priv); if (rc < 0) { - dev_dbg(&port->dev, "roomquery failed\n"); - goto exit; - } - if (rc == 0) { - dev_dbg(&port->dev, "roomquery returned 0 bytes\n"); - rc = -EIO; /* device didn't return any data */ + dev_dbg(&port->dev, "roomquery failed %d\n", rc); goto exit; } + + dev_dbg(&port->dev, "roomquery says %d\n", rc); + priv->tx_room = rc; } + if (count > priv->tx_room) { /* we're about to completely fill the Tx buffer, so we'll be throttled afterwards. */ @@ -615,45 +618,26 @@ static int keyspan_pda_open(struct tty_s struct usb_serial_port *port) { struct usb_serial *serial = port->serial; - u8 *room; - int rc = 0; struct keyspan_pda_private *priv; + int rc; - /* find out how much room is in the Tx ring */ - room = kmalloc(1, GFP_KERNEL); - if (!room) - return -ENOMEM; + priv = usb_get_serial_port_data(port); - rc = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0), - 6, /* write_room */ - USB_TYPE_VENDOR | USB_RECIP_INTERFACE - | USB_DIR_IN, - 0, /* value */ - 0, /* index */ - room, - 1, - 2000); + /* find out how much room is in the Tx ring */ + rc = keyspan_pda_query_room(serial, priv); if (rc < 0) { - dev_dbg(&port->dev, "%s - roomquery failed\n", __func__); - goto error; - } - if (rc == 0) { - dev_dbg(&port->dev, "%s - roomquery returned 0 bytes\n", __func__); - rc = -EIO; - goto error; + dev_dbg(&port->dev, "roomquery failed %d\n", rc); + return rc; } - priv = usb_get_serial_port_data(port); - priv->tx_room = *room; - priv->tx_throttled = *room ? 0 : 1; + + priv->tx_room = rc; + priv->tx_throttled = rc ? 0 : 1; /*Start reading from the device*/ rc = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL); - if (rc) { + if (rc) dev_dbg(&port->dev, "%s - usb_submit_urb(read int) failed\n", __func__); - goto error; - } -error: - kfree(room); + return rc; } static void keyspan_pda_close(struct usb_serial_port *port) @@ -715,6 +699,11 @@ static int keyspan_pda_port_probe(struct priv = kmalloc(sizeof(struct keyspan_pda_private), GFP_KERNEL); if (!priv) return -ENOMEM; + priv->query_buf = kmalloc(1, GFP_KERNEL); + if (!priv->query_buf) { + kfree(priv); + return -ENOMEM; + } INIT_WORK(&priv->wakeup_work, keyspan_pda_wakeup_write); INIT_WORK(&priv->unthrottle_work, keyspan_pda_request_unthrottle); @@ -731,6 +720,7 @@ static int keyspan_pda_port_remove(struc struct keyspan_pda_private *priv; priv = usb_get_serial_port_data(port); + kfree(priv->query_buf); kfree(priv); return 0;