From patchwork Mon Nov 16 07:05:12 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: 56572 Delivered-To: patch@linaro.org Received: by 10.112.155.196 with SMTP id vy4csp1154569lbb; Sun, 15 Nov 2015 23:06:27 -0800 (PST) X-Received: by 10.68.130.69 with SMTP id oc5mr51593847pbb.140.1447657587081; Sun, 15 Nov 2015 23:06:27 -0800 (PST) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id sk1si48671475pbc.113.2015.11.15.23.06.26; Sun, 15 Nov 2015 23:06:27 -0800 (PST) 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; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dkim=neutral (body hash did not verify) header.i=@linaro_org.20150623.gappssmtp.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751526AbbKPHFp (ORCPT + 28 others); Mon, 16 Nov 2015 02:05:45 -0500 Received: from mail-pa0-f51.google.com ([209.85.220.51]:33342 "EHLO mail-pa0-f51.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750822AbbKPHFk (ORCPT ); Mon, 16 Nov 2015 02:05:40 -0500 Received: by pabfh17 with SMTP id fh17so168100047pab.0 for ; Sun, 15 Nov 2015 23:05:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro_org.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id; bh=4XlmGRAuGoPgKOUxKYIMegMbzfmN0SqlG+D5A/Saq2k=; b=vEzEo6Cumx8dXj4CxhJFkMMBVxh3AifmZkvPuVCD3pIlf3Y+V9Jax+srB5ECxvVrlu g6y7YpOFWxK9DxddMJLkV12bkkdmruycGfYmAKIXxJbtm68DAJhXFWNtHwEN1G6kDsSC cJ/Wi9b+PaiEyevz8V74GkQf0aagR2fD7t2gyXaiOMcV9AxgGKik0aDIewhMtFGuxRH2 1k0un5Q3LAN+NMsd7hamqiJruoI0HLwQFl3InBJxXvdGu9prYc9+e1O6/Jl8f2UEQ+06 6mYdyLYW/i/nANZ1zQajznPLtaAh8ag9b+/3fxje2PzAWdYEKW1UlYh4ra2Dthg1/DGC K0Pw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=4XlmGRAuGoPgKOUxKYIMegMbzfmN0SqlG+D5A/Saq2k=; b=Wi9AxC7Oo7QjtsXNBR+Jpp8gK91uhLwqBCbGiacRan65jrobkZxLSK31jQoWxcoDvt GrTo3hkB6Uc1sY5s8KAH48xACYog2lOZ9FrYj9Tzb0RhMMPDFoYaTvg7d6xTUAodgeSE 7G23O3sJkvyVszpKgYKm+n2/D/6pkZpaGRxrUIt9TRfvcDYp6TrHkbzM9EX3+XBG/gzV FtEHkPkgfo77jVce8TE/GRy5MX6u9udCOfwk6GImhDpjQBzcILYF3RocX8qNIqOHgKo3 jGNNMsz+ifQ44Y5KEpAtWT76paT+6bTzZUHglHJ8BpcPSlF97+JXG4Dl7gZXuMqXmC3d Ycsg== X-Gm-Message-State: ALoCoQlQTby6+ETW+lTZul+SpGLy02I7y/AdwJMgeBkKECKr/wawrvMdXOCSBPPDDNVfh2oFSCWR X-Received: by 10.68.233.233 with SMTP id tz9mr51443077pbc.15.1447657539896; Sun, 15 Nov 2015 23:05:39 -0800 (PST) Received: from baolinwangubtpc.spreadtrum.com ([175.111.195.49]) by smtp.gmail.com with ESMTPSA id at5sm27585636pbc.32.2015.11.15.23.05.36 (version=TLSv1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 15 Nov 2015 23:05:39 -0800 (PST) From: Baolin Wang To: balbi@ti.com, gregkh@linuxfoundation.org Cc: r.baldyga@samsung.com, fabio.estevam@freescale.com, Philip.Oberstaller@septentrio.com, peter@hurleysoftware.com, scottwood@freescale.com, broonie@kernel.org, baolin.wang@linaro.org, linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] usb: gadget: Add the console support for usb-to-serial port Date: Mon, 16 Nov 2015 15:05:12 +0800 Message-Id: X-Mailer: git-send-email 1.7.9.5 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 | 238 ++++++++++++++++++++++++++++++++ 2 files changed, 244 insertions(+) -- 1.7.9.5 -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/ 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 f7771d8..4ade527 100644 --- a/drivers/usb/gadget/function/u_serial.c +++ b/drivers/usb/gadget/function/u_serial.c @@ -27,6 +27,7 @@ #include #include #include +#include #include "u_serial.h" @@ -79,6 +80,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 { @@ -118,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 { @@ -1054,6 +1066,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: @@ -1143,6 +1156,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 @@ -1219,6 +1453,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; @@ -1320,6 +1555,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"); @@ -1334,6 +1571,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;