Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp103122ybz; Tue, 21 Apr 2020 05:36:27 -0700 (PDT) X-Google-Smtp-Source: APiQypIyg5wcjngd2oGyY7jjS/vIpjii2hkSbPysHm87u3ttpuImKVksrstZF9N38wMex93ZgOEd X-Received: by 2002:a17:907:2101:: with SMTP id qn1mr21727786ejb.207.1587472587030; Tue, 21 Apr 2020 05:36:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1587472587; cv=none; d=google.com; s=arc-20160816; b=uK9o761WtBc9MJuM80kd3f2jPyircdr2WncC2G1LbBkqL8j38kijKLN7Gm2G/RmMe2 mX03CDT9JWTGPEQsvrxB43z46mwz0/05x8YaNv8nOOHOF80F9HK+00nNKEQxfFn6fima JCw1o0296xdJ5hKwOsg9Vy866obd+KiMKSJQdezmcdGcdBrBVpKLFU8WX+TGxI7vuE9l t1dwfOmi0BqR/We7FYUXauw42O/nvxPy0CPpnStafqDFRIdpodRfk+G1hh9MagtMLr4w nQxNDT3qqrKt/UpUfU/4x0bSa6GOJFPYGByTuf9Dm410WnKMtCG/Y9seF1IPYBUk2dHg I0Pw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=BCdNw/6bNXDgvzYMM/yuCjYQaZbw1vGHMQ1wGH6OfQw=; b=p8WRx6Isj+R2+OAtXH76hlkv+ZHOlFnQO9nSurLrMcnnpx4C/sShaHxHQzdlZuxw0J ym1lYeVEiF6et7sl/ijoCF2vdIGwJmpYHrx5UlvOao+/RSOvuq3ldnjqqxmGGj+K8Mz6 qYg4i4XRdF2BxSqJ3xEbGtVhxs1ETKlCed/jkoO6MtbRioMSHTLT+aD6zvZNrhq6QmHX MPCBaoP1IsOeuQTOxImloW1rHiP9VpOAxB01QiIpMnLaGNFqGT3KVIijP8Q3Z3bJmS9p S8naVd4kypFbkG+XshML0UfGFelu/MMfDN+6rnJ0Ka7FCASYl0QGmw/jFSsUMnAN+aL5 iWtQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@st.com header.s=STMicroelectronics header.b=DLOOB3q1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=st.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id j12si1659549edn.292.2020.04.21.05.36.03; Tue, 21 Apr 2020 05:36:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@st.com header.s=STMicroelectronics header.b=DLOOB3q1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=st.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728877AbgDUMdW (ORCPT + 99 others); Tue, 21 Apr 2020 08:33:22 -0400 Received: from mx08-00178001.pphosted.com ([91.207.212.93]:5350 "EHLO mx07-00178001.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728839AbgDUMdU (ORCPT ); Tue, 21 Apr 2020 08:33:20 -0400 Received: from pps.filterd (m0046660.ppops.net [127.0.0.1]) by mx07-00178001.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 03LCO4wA004143; Tue, 21 Apr 2020 14:33:13 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=st.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-type; s=STMicroelectronics; bh=BCdNw/6bNXDgvzYMM/yuCjYQaZbw1vGHMQ1wGH6OfQw=; b=DLOOB3q1xzpkHuWVQE1W3GjjNP5r0gaRi3HZdwgCBZe9UN3Y+2ePKe8zkp1dLYzqxDFF VlNpr1z/paTkNWP1sXYtEyg8WCLDaat6YLCohITc6Dt5L0WFNDO4hthfMgNAf7SpRKW8 qBWXOLNdGZ4fPFLN0tCqQvfYbQiN3MqTflVX9S3fYkIJIvOoVC+3hCOi7rCNF8uNjdy9 TEYRETf/9kMBpfcq2Q7yHRUCSKZbEmBMvjoGC7VjseTN39E2EtFdHiNckZQYOCjFZgCe eM+XbkYTWhsOIZrJ2h0+PWOjxRZW60Zmx/9vppT/jTb2hEiuxxrSr9uE2ajiNX/HDxnd yg== Received: from beta.dmz-eu.st.com (beta.dmz-eu.st.com [164.129.1.35]) by mx07-00178001.pphosted.com with ESMTP id 30fpp8rapj-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 21 Apr 2020 14:33:13 +0200 Received: from euls16034.sgp.st.com (euls16034.sgp.st.com [10.75.44.20]) by beta.dmz-eu.st.com (STMicroelectronics) with ESMTP id AD49A100034; Tue, 21 Apr 2020 14:33:12 +0200 (CEST) Received: from Webmail-eu.st.com (sfhdag5node3.st.com [10.75.127.15]) by euls16034.sgp.st.com (STMicroelectronics) with ESMTP id 9FB322B0FCB; Tue, 21 Apr 2020 14:33:12 +0200 (CEST) Received: from localhost (10.75.127.46) by SFHDAG5NODE3.st.com (10.75.127.15) with Microsoft SMTP Server (TLS) id 15.0.1473.3; Tue, 21 Apr 2020 14:33:11 +0200 From: Fabrice Gasnier To: , CC: , , , , Subject: [PATCH 2/4] usb: gadget: u_serial: add suspend resume callbacks Date: Tue, 21 Apr 2020 14:32:19 +0200 Message-ID: <1587472341-17935-3-git-send-email-fabrice.gasnier@st.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1587472341-17935-1-git-send-email-fabrice.gasnier@st.com> References: <1587472341-17935-1-git-send-email-fabrice.gasnier@st.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.75.127.46] X-ClientProxiedBy: SFHDAG7NODE1.st.com (10.75.127.19) To SFHDAG5NODE3.st.com (10.75.127.15) X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.138,18.0.676 definitions=2020-04-21_05:2020-04-20,2020-04-21 signatures=0 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add suspend resume callbacks to handle the case seen when the bus is suspended by the HOST, and the device opens the port (cat /dev/ttyGS0). Gadget controller (like DWC2) doesn't accept usb requests to be queued in this case (when in L2 state), from the gs_open() call. Error log is printed - configfs-gadget gadget: acm ttyGS0 can't notify serial state, -11 If the HOST resumes (opens) the bus, the port still isn't functional. Use suspend/resume callbacks to monitor the gadget suspended state by using 'suspended' flag. In case the port gets opened (cat /dev/ttyGS0), the I/O stream will be delayed until the bus gets resumed by the HOST. Signed-off-by: Fabrice Gasnier --- drivers/usb/gadget/function/u_serial.c | 57 +++++++++++++++++++++++++++++----- drivers/usb/gadget/function/u_serial.h | 2 ++ 2 files changed, 51 insertions(+), 8 deletions(-) diff --git a/drivers/usb/gadget/function/u_serial.c b/drivers/usb/gadget/function/u_serial.c index 8167d37..3cfc6e2 100644 --- a/drivers/usb/gadget/function/u_serial.c +++ b/drivers/usb/gadget/function/u_serial.c @@ -120,6 +120,8 @@ struct gs_port { wait_queue_head_t drain_wait; /* wait while writes drain */ bool write_busy; wait_queue_head_t close_wait; + bool suspended; /* port suspended */ + bool start_delayed; /* delay start when suspended */ /* REVISIT this state ... */ struct usb_cdc_line_coding port_line_coding; /* 8-N-1 etc */ @@ -630,13 +632,19 @@ static int gs_open(struct tty_struct *tty, struct file *file) /* if connected, start the I/O stream */ if (port->port_usb) { - struct gserial *gser = port->port_usb; - - pr_debug("gs_open: start ttyGS%d\n", port->port_num); - gs_start_io(port); - - if (gser->connect) - gser->connect(gser); + /* if port is suspended, wait resume to start I/0 stream */ + if (!port->suspended) { + struct gserial *gser = port->port_usb; + + pr_debug("gs_open: start ttyGS%d\n", port->port_num); + gs_start_io(port); + + if (gser->connect) + gser->connect(gser); + } else { + pr_debug("delay start of ttyGS%d\n", port->port_num); + port->start_delayed = true; + } } pr_debug("gs_open: ttyGS%d (%p,%p)\n", port->port_num, tty, file); @@ -680,7 +688,7 @@ static void gs_close(struct tty_struct *tty, struct file *file) pr_debug("gs_close: ttyGS%d (%p,%p) ...\n", port->port_num, tty, file); gser = port->port_usb; - if (gser && gser->disconnect) + if (gser && !port->suspended && gser->disconnect) gser->disconnect(gser); /* wait for circular write buffer to drain, disconnect, or at @@ -708,6 +716,7 @@ static void gs_close(struct tty_struct *tty, struct file *file) else kfifo_reset(&port->port_write_buf); + port->start_delayed = false; port->port.count = 0; port->port.tty = NULL; @@ -1403,6 +1412,38 @@ void gserial_disconnect(struct gserial *gser) } EXPORT_SYMBOL_GPL(gserial_disconnect); +void gserial_suspend(struct gserial *gser) +{ + struct gs_port *port = gser->ioport; + unsigned long flags; + + spin_lock_irqsave(&port->port_lock, flags); + port->suspended = true; + spin_unlock_irqrestore(&port->port_lock, flags); +} +EXPORT_SYMBOL_GPL(gserial_suspend); + +void gserial_resume(struct gserial *gser) +{ + struct gs_port *port = gser->ioport; + unsigned long flags; + + spin_lock_irqsave(&port->port_lock, flags); + port->suspended = false; + if (!port->start_delayed) { + spin_unlock_irqrestore(&port->port_lock, flags); + return; + } + + pr_debug("delayed start ttyGS%d\n", port->port_num); + gs_start_io(port); + if (gser->connect) + gser->connect(gser); + port->start_delayed = false; + spin_unlock_irqrestore(&port->port_lock, flags); +} +EXPORT_SYMBOL_GPL(gserial_resume); + static int userial_init(void) { unsigned i; diff --git a/drivers/usb/gadget/function/u_serial.h b/drivers/usb/gadget/function/u_serial.h index e5b08ab..009a959 100644 --- a/drivers/usb/gadget/function/u_serial.h +++ b/drivers/usb/gadget/function/u_serial.h @@ -68,6 +68,8 @@ ssize_t gserial_get_console(unsigned char port_num, char *page); /* connect/disconnect is handled by individual functions */ int gserial_connect(struct gserial *, u8 port_num); void gserial_disconnect(struct gserial *); +void gserial_suspend(struct gserial *p); +void gserial_resume(struct gserial *p); /* functions are bound to configurations by a config or gadget driver */ int gser_bind_config(struct usb_configuration *c, u8 port_num); -- 2.7.4