From patchwork Fri Oct 23 05:29:52 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "\(Exiting\) Baolin Wang" X-Patchwork-Id: 55461 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-wi0-f198.google.com (mail-wi0-f198.google.com [209.85.212.198]) by patches.linaro.org (Postfix) with ESMTPS id 48D3D20581 for ; Fri, 23 Oct 2015 05:30:30 +0000 (UTC) Received: by wiyb4 with SMTP id b4sf5751960wiy.2 for ; Thu, 22 Oct 2015 22:30:29 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:from:to:cc:subject :date:message-id:sender:precedence:list-id:x-original-sender :x-original-authentication-results:mailing-list:list-post:list-help :list-archive:list-unsubscribe; bh=SetS8g1uhzZnR3GMerUb1Bc14VsxgRMaUpApX9CWQtU=; b=Xu46rEFcr8/dirIZ1bH8OVECIenRGqWPqnPpLxFg9Jaxm+iaPhfFcnBLCPhk2U+1J3 t0vJOZp+DbCcIrhfjGYGrrPmjnkYWbgh4+T10EH1qR6KAekgnGjYqlv+QU+zVUWXh7Po b2BZGD1R/WyoKj7C55lMauC+nkIBgYLjkZ6fAqwGsSsfucMVxZbxsaqi+Ui1fQN7mHOl gAqFR2fQG8lFebAvBJJMW6a7cqM1VK56UAHLao2NqWdHsks6FGptc4Y6b86yw46xPf3i Zld48Y2ifQGUCw1fWw8G0ukRXgTOVddeayDfLuHqVs/xFq5oeZVnGKfDSigaiwl3idd4 Gtew== X-Gm-Message-State: ALoCoQkxznbZJkTUMp0T31O5DCQAvJR2l3ybqBle5JhsNpu70KZ1L8EIL9aJUXgzzUuGMwOFRTCM X-Received: by 10.194.203.33 with SMTP id kn1mr427682wjc.3.1445578229623; Thu, 22 Oct 2015 22:30:29 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.25.31.8 with SMTP id f8ls149007lff.31.gmail; Thu, 22 Oct 2015 22:30:29 -0700 (PDT) X-Received: by 10.25.15.83 with SMTP id e80mr6724216lfi.34.1445578229434; Thu, 22 Oct 2015 22:30:29 -0700 (PDT) Received: from mail-lf0-x230.google.com (mail-lf0-x230.google.com. [2a00:1450:4010:c07::230]) by mx.google.com with ESMTPS id e2si11667617lbp.39.2015.10.22.22.30.29 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 22 Oct 2015 22:30:29 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 2a00:1450:4010:c07::230 as permitted sender) client-ip=2a00:1450:4010:c07::230; Received: by lfbn126 with SMTP id n126so36992769lfb.2 for ; Thu, 22 Oct 2015 22:30:29 -0700 (PDT) X-Received: by 10.112.163.131 with SMTP id yi3mr10183805lbb.36.1445578229233; Thu, 22 Oct 2015 22:30:29 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.59.35 with SMTP id w3csp987324lbq; Thu, 22 Oct 2015 22:30:27 -0700 (PDT) X-Received: by 10.107.3.212 with SMTP id e81mr23656899ioi.129.1445578227030; Thu, 22 Oct 2015 22:30:27 -0700 (PDT) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c9si1815366igg.40.2015.10.22.22.30.26; Thu, 22 Oct 2015 22:30:26 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751988AbbJWFaX (ORCPT + 28 others); Fri, 23 Oct 2015 01:30:23 -0400 Received: from mail-pa0-f43.google.com ([209.85.220.43]:33106 "EHLO mail-pa0-f43.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750953AbbJWFaV (ORCPT ); Fri, 23 Oct 2015 01:30:21 -0400 Received: by pabrc13 with SMTP id rc13so107690265pab.0 for ; Thu, 22 Oct 2015 22:30:20 -0700 (PDT) X-Received: by 10.66.102.38 with SMTP id fl6mr2613164pab.49.1445578220630; Thu, 22 Oct 2015 22:30:20 -0700 (PDT) Received: from baolinwangubtpc.spreadtrum.com ([175.111.195.49]) by smtp.gmail.com with ESMTPSA id fb1sm16762534pab.9.2015.10.22.22.30.16 (version=TLSv1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 22 Oct 2015 22:30:20 -0700 (PDT) From: Baolin Wang To: balbi@ti.com Cc: broonie@kernel.org, arnd@arndb.de, gregkh@linuxfoundation.org, linux-kernel@vger.kernel.org, baolin.wang@linaro.org, r.baldyga@samsung.com, fabio.estevam@freescale.com, Philip.Oberstaller@septentrio.com, arnout@mind.be, scottwood@freescale.com, linux-usb@vger.kernel.org Subject: [PATCH] usb: gadget: Add the console support for usb-to-serial port Date: Fri, 23 Oct 2015 13:29:52 +0800 Message-Id: X-Mailer: git-send-email 1.7.9.5 Sender: linux-kernel-owner@vger.kernel.org Precedence: list List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Original-Sender: baolin.wang@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 2a00:1450:4010:c07::230 as permitted sender) smtp.mailfrom=patch+caf_=patchwork-forward=linaro.org@linaro.org; dkim=neutral (body hash did not verify) header.i=@linaro_org.20150623.gappssmtp.com Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , It dose not work when we want to use the usb-to-serial port based on one usb gadget as a console. Thus this patch adds the console initialization to support this request. Signed-off-by: Baolin Wang --- drivers/usb/gadget/Kconfig | 6 + drivers/usb/gadget/function/u_serial.c | 239 ++++++++++++++++++++++++++++++++ 2 files changed, 245 insertions(+) diff --git a/drivers/usb/gadget/Kconfig b/drivers/usb/gadget/Kconfig index 33834aa..be5aab9 100644 --- a/drivers/usb/gadget/Kconfig +++ b/drivers/usb/gadget/Kconfig @@ -127,6 +127,12 @@ config USB_GADGET_STORAGE_NUM_BUFFERS a module parameter as well. If unsure, say 2. +config U_SERIAL_CONSOLE + bool "Serial gadget console support" + depends on USB_G_SERIAL + help + It supports the serial gadget can be used as a console. + source "drivers/usb/gadget/udc/Kconfig" # diff --git a/drivers/usb/gadget/function/u_serial.c b/drivers/usb/gadget/function/u_serial.c index 9cc6a13..343d530 100644 --- a/drivers/usb/gadget/function/u_serial.c +++ b/drivers/usb/gadget/function/u_serial.c @@ -27,6 +27,8 @@ #include #include #include +#include +#include #include "u_serial.h" @@ -79,6 +81,16 @@ */ #define QUEUE_SIZE 16 #define WRITE_BUF_SIZE 8192 /* TX only */ +#define GS_BUFFER_SIZE (4096) +#define GS_CONSOLE_BUF_SIZE (2 * GS_BUFFER_SIZE) + +struct gscons_info { + struct gs_port *port; + struct tty_driver *tty_driver; + struct work_struct work; + int buf_tail; + char buf[GS_CONSOLE_BUF_SIZE]; +}; /* circular buffer */ struct gs_buf { @@ -117,6 +129,7 @@ struct gs_port { /* REVISIT this state ... */ struct usb_cdc_line_coding port_line_coding; /* 8-N-1 etc */ + struct usb_request *console_req; }; static struct portmaster { @@ -1052,6 +1065,7 @@ gs_port_alloc(unsigned port_num, struct usb_cdc_line_coding *coding) port->port_num = port_num; port->port_line_coding = *coding; + port->console_req = NULL; ports[port_num].port = port; out: @@ -1141,6 +1155,227 @@ err: } EXPORT_SYMBOL_GPL(gserial_alloc_line); +#ifdef CONFIG_U_SERIAL_CONSOLE + +static struct usb_request *gs_request_new(struct usb_ep *ep, int buffer_size) +{ + struct usb_request *req = usb_ep_alloc_request(ep, GFP_ATOMIC); + + if (!req) + return NULL; + + /* now allocate buffers for the requests */ + req->buf = kmalloc(buffer_size, GFP_ATOMIC); + if (!req->buf) { + usb_ep_free_request(ep, req); + return NULL; + } + + return req; +} + +static void gs_request_free(struct usb_request *req, struct usb_ep *ep) +{ + if (req) { + kfree(req->buf); + usb_ep_free_request(ep, req); + } +} + +static void gs_complete_out(struct usb_ep *ep, struct usb_request *req) +{ + if (req->status != 0 && req->status != -ECONNRESET) + return; +} + +static struct console gserial_cons; +static int gs_console_connect(void) +{ + struct gscons_info *info = gserial_cons.data; + int port_num = gserial_cons.index; + struct usb_request *req; + struct gs_port *port; + struct usb_ep *ep; + + if (port_num >= MAX_U_SERIAL_PORTS || port_num < 0) { + pr_err("%s: port num [%d] exceeds the range.\n", + __func__, port_num); + return -ENXIO; + } + + port = ports[port_num].port; + if (!port) { + pr_err("%s: serial line [%d] not allocated.\n", + __func__, port_num); + return -ENODEV; + } + + if (!port->port_usb) { + pr_err("%s: no port usb.\n", __func__); + return -ENODEV; + } + + ep = port->port_usb->in; + if (!ep) { + pr_err("%s: no usb endpoint.\n", __func__); + return -ENXIO; + } + + req = port->console_req; + if (!req) { + req = gs_request_new(ep, GS_BUFFER_SIZE); + if (!req) { + pr_err("%s: request fail.\n", __func__); + return -ENOMEM; + } + req->complete = gs_complete_out; + } + + info->port = port; + + pr_debug("%s: port[%d] console connect!\n", __func__, port_num); + return 0; +} + +static void gs_console_work(struct work_struct *work) +{ + struct gscons_info *info = container_of(work, struct gscons_info, work); + struct gs_port *port = info->port; + struct usb_request *req; + struct usb_ep *ep; + int xfer, ret, count; + char *p; + + if (!port || !port->port_usb) + return; + + req = port->console_req; + ep = port->port_usb->in; + if (!req || !ep) + return; + + spin_lock_irq(&port->port_lock); + count = info->buf_tail; + p = info->buf; + + while (count > 0 && !port->write_busy) { + if (count > GS_BUFFER_SIZE) + xfer = GS_BUFFER_SIZE; + else + xfer = count; + + memcpy(req->buf, p, xfer); + req->length = xfer; + + port->write_busy = true; + spin_unlock(&port->port_lock); + ret = usb_ep_queue(ep, req, GFP_ATOMIC); + spin_lock(&port->port_lock); + port->write_busy = false; + if (ret < 0) + break; + + p += xfer; + count -= xfer; + } + + info->buf_tail -= count; + spin_unlock_irq(&port->port_lock); +} + +static int gs_console_setup(struct console *co, char *options) +{ + struct gscons_info *gscons_info; + + gscons_info = kzalloc(sizeof(struct gscons_info), GFP_KERNEL); + if (!gscons_info) + return -ENOMEM; + + gscons_info->port = NULL; + gscons_info->tty_driver = gs_tty_driver; + INIT_WORK(&gscons_info->work, gs_console_work); + gscons_info->buf_tail = 0; + co->data = gscons_info; + + return 0; +} + +static void gs_console_write(struct console *co, + const char *buf, unsigned count) +{ + struct gscons_info *info = co->data; + int avail, xfer; + char *p; + + avail = GS_CONSOLE_BUF_SIZE - info->buf_tail; + if (count > avail) + xfer = avail; + else + xfer = count; + + p = &info->buf[info->buf_tail]; + memcpy(p, buf, xfer); + info->buf_tail += xfer; + + schedule_work(&info->work); +} + +static struct tty_driver *gs_console_device(struct console *co, int *index) +{ + struct gscons_info *info = co->data; + + *index = co->index; + return info->tty_driver; +} + +static struct console gserial_cons = { + .name = "ttyGS", + .write = gs_console_write, + .device = gs_console_device, + .setup = gs_console_setup, + .flags = CON_PRINTBUFFER, + .index = -1, +}; + +static void gserial_console_init(void) +{ + register_console(&gserial_cons); +} + +static void gserial_console_exit(void) +{ + struct gscons_info *info = gserial_cons.data; + struct gs_port *port = info->port; + struct usb_request *req; + struct usb_ep *ep; + + if (port && port->port_usb) { + req = port->console_req; + ep = port->port_usb->in; + gs_request_free(req, ep); + } + + kfree(info); + unregister_console(&gserial_cons); +} + +#else + +static int gs_console_connect(void) +{ + return 0; +} + +static void gserial_console_init(void) +{ +} + +static void gserial_console_exit(void) +{ +} + +#endif + /** * gserial_connect - notify TTY I/O glue that USB link is active * @gser: the function, set up with endpoints and descriptors @@ -1217,6 +1452,7 @@ int gserial_connect(struct gserial *gser, u8 port_num) gser->disconnect(gser); } + status = gs_console_connect(); spin_unlock_irqrestore(&port->port_lock, flags); return status; @@ -1318,6 +1554,8 @@ static int userial_init(void) goto fail; } + gserial_console_init(); + pr_debug("%s: registered %d ttyGS* device%s\n", __func__, MAX_U_SERIAL_PORTS, (MAX_U_SERIAL_PORTS == 1) ? "" : "s"); @@ -1332,6 +1570,7 @@ module_init(userial_init); static void userial_cleanup(void) { + gserial_console_exit(); tty_unregister_driver(gs_tty_driver); put_tty_driver(gs_tty_driver); gs_tty_driver = NULL;