From patchwork Fri Jan 6 16:26:34 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rob Herring X-Patchwork-Id: 90200 Delivered-To: patch@linaro.org Received: by 10.140.20.101 with SMTP id 92csp9519776qgi; Fri, 6 Jan 2017 08:27:42 -0800 (PST) X-Received: by 10.99.229.17 with SMTP id r17mr142934379pgh.149.1483720062427; Fri, 06 Jan 2017 08:27:42 -0800 (PST) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t4si47515680plj.280.2017.01.06.08.27.42 for ; Fri, 06 Jan 2017 08:27:42 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-bluetooth-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-bluetooth-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1759234AbdAFQ1Y (ORCPT ); Fri, 6 Jan 2017 11:27:24 -0500 Received: from mail-oi0-f65.google.com ([209.85.218.65]:34708 "EHLO mail-oi0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1759157AbdAFQ1C (ORCPT ); Fri, 6 Jan 2017 11:27:02 -0500 Received: by mail-oi0-f65.google.com with SMTP id 3so55158221oih.1; Fri, 06 Jan 2017 08:27:02 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=B7ToxYNba0A3Ar49wFT/UYhze5LOFgyJhl6J1DYQhio=; b=VyEX0sMe3Ke3Bh1sF8xEd+C9vgu5WW5ip2cfJgWMNhaG9GD02JYEkmqkWZP3GVVugG 5xvz5tv0XEv3bF9v9K9GUiubTS2OXd98L2l19jgaNxRT3hk1sbTJMUFxbSA3IlsKRQ97 kIjx0B9gRp6bAQRN7Lq093+RQUkj8lRwI8W9mGcpljm1n5sEjKIbNPRXa4OcypWm1hPZ cSEqJNhJ8XBtreD7kTYCB/eyXn+t+93J4ukFiKYxGP6eHNgUpPK29q483r58SgHkRNm1 G2DedueLMGVFJ5PFFGF2F/OcZjXTKHHjEKUEBwDte4t5cRAiHpsNthl448xssJZ7kfrI PzMA== X-Gm-Message-State: AIkVDXJnLRAjKgsxNFXFGmQWLDpFyN565VYIpVLNAI49BCu/DgZEm8z5Y+UkNwliltxBBQ== X-Received: by 10.157.16.16 with SMTP id h16mr1603020ote.40.1483720021548; Fri, 06 Jan 2017 08:27:01 -0800 (PST) Received: from rob-hp-laptop.herring.priv (50-233-148-156-static.hfc.comcastbusiness.net. [50.233.148.156]) by smtp.googlemail.com with ESMTPSA id i135sm5245809oib.18.2017.01.06.08.26.58 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 06 Jan 2017 08:27:01 -0800 (PST) From: Rob Herring To: Greg Kroah-Hartman , Marcel Holtmann , Jiri Slaby , Sebastian Reichel , Arnd Bergmann , "Dr . H . Nikolaus Schaller" , Peter Hurley , Andy Shevchenko , Alan Cox Cc: Loic Poulain , Pavel Machek , NeilBrown , Linus Walleij , linux-bluetooth@vger.kernel.org, linux-serial@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 8/9] serdev: add a tty port controller driver Date: Fri, 6 Jan 2017 10:26:34 -0600 Message-Id: <20170106162635.19677-9-robh@kernel.org> X-Mailer: git-send-email 2.10.1 In-Reply-To: <20170106162635.19677-1-robh@kernel.org> References: <20170106162635.19677-1-robh@kernel.org> Sender: linux-bluetooth-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org Add a serdev controller driver for tty ports. The controller is registered with serdev when tty ports are registered with the TTY core. As the TTY core is built-in only, this has the side effect of making serdev built-in as well. Signed-off-by: Rob Herring --- drivers/tty/serdev/Kconfig | 8 ++ drivers/tty/serdev/Makefile | 2 + drivers/tty/serdev/serdev-ttyport.c | 244 ++++++++++++++++++++++++++++++++++++ include/linux/serdev.h | 20 +++ 4 files changed, 274 insertions(+) create mode 100644 drivers/tty/serdev/serdev-ttyport.c -- 2.10.1 -- To unsubscribe from this list: send the line "unsubscribe linux-bluetooth" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/drivers/tty/serdev/Kconfig b/drivers/tty/serdev/Kconfig index 3b6ecd187bef..7eac4cc42785 100644 --- a/drivers/tty/serdev/Kconfig +++ b/drivers/tty/serdev/Kconfig @@ -6,3 +6,11 @@ menuconfig SERIAL_DEV_BUS help Core support for devices connected via a serial port. +if SERIAL_DEV_BUS + +config SERIAL_DEV_CTRL_TTYPORT + bool "Serial device TTY port controller" + depends on TTY + depends on SERIAL_DEV_BUS=y + +endif diff --git a/drivers/tty/serdev/Makefile b/drivers/tty/serdev/Makefile index 01a9b62183f4..0cbdb9444d9d 100644 --- a/drivers/tty/serdev/Makefile +++ b/drivers/tty/serdev/Makefile @@ -1,3 +1,5 @@ serdev-objs := core.o obj-$(CONFIG_SERIAL_DEV_BUS) += serdev.o + +obj-$(CONFIG_SERIAL_DEV_CTRL_TTYPORT) += serdev-ttyport.o diff --git a/drivers/tty/serdev/serdev-ttyport.c b/drivers/tty/serdev/serdev-ttyport.c new file mode 100644 index 000000000000..ed6fd675ece6 --- /dev/null +++ b/drivers/tty/serdev/serdev-ttyport.c @@ -0,0 +1,244 @@ +/* + * Copyright (C) 2016 Linaro Ltd., Rob Herring + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 and + * only version 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ +#include +#include +#include +#include +#include + +#define SERPORT_BUSY 1 +#define SERPORT_ACTIVE 2 +#define SERPORT_DEAD 3 + +struct serport { + struct tty_port *port; + struct tty_struct *tty; + struct tty_driver *tty_drv; + int tty_idx; + struct mutex lock; + unsigned long flags; +}; + +/* + * Callback functions from the tty port. + */ + +static int ttyport_receive_buf(struct tty_port *port, const unsigned char *cp, + const unsigned char *fp, size_t count) +{ + struct serdev_controller *ctrl = port->client_data; + struct serport *serport = serdev_controller_get_drvdata(ctrl); + + mutex_lock(&serport->lock); + + if (!test_bit(SERPORT_ACTIVE, &serport->flags)) + goto out; + + serdev_controller_receive_buf(ctrl, cp, count); + +out: + mutex_unlock(&serport->lock); + return count; +} + +static void ttyport_write_wakeup(struct tty_port *port) +{ + struct serdev_controller *ctrl = port->client_data; + struct serport *serport = serdev_controller_get_drvdata(ctrl); + + clear_bit(TTY_DO_WRITE_WAKEUP, &port->tty->flags); + + if (test_bit(SERPORT_ACTIVE, &serport->flags)) + serdev_controller_write_wakeup(ctrl); +} + +static const struct tty_port_client_operations client_ops = { + .receive_buf = ttyport_receive_buf, + .write_wakeup = ttyport_write_wakeup, +}; + +/* + * Callback functions from the serdev core. + */ + +static int ttyport_write_buf(struct serdev_controller *ctrl, const unsigned char *data, size_t len) +{ + struct serport *serport = serdev_controller_get_drvdata(ctrl); + struct tty_struct *tty = serport->tty; + + set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); + return serport->tty->ops->write(serport->tty, data, len); +} + +static void ttyport_write_flush(struct serdev_controller *ctrl) +{ + struct serport *serport = serdev_controller_get_drvdata(ctrl); + struct tty_struct *tty = serport->tty; + + tty_driver_flush_buffer(tty); +} + +static int ttyport_write_room(struct serdev_controller *ctrl) +{ + struct serport *serport = serdev_controller_get_drvdata(ctrl); + struct tty_struct *tty = serport->tty; + + return tty_write_room(tty); +} + + +static int ttyport_open(struct serdev_controller *ctrl) +{ + struct serport *serport = serdev_controller_get_drvdata(ctrl); + struct tty_struct *tty; + struct ktermios ktermios; + + tty = tty_init_dev(serport->tty_drv, serport->tty_idx); + serport->tty = tty; + + serport->port->client_ops = &client_ops; + serport->port->client_data = ctrl; + + tty->receive_room = 65536; + + if (tty->ops->open) + tty->ops->open(serport->tty, NULL); + else + tty_port_open(serport->port, tty, NULL); + + /* Bring the UART into a known 8 bits no parity hw fc state */ + ktermios = tty->termios; + ktermios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | + INLCR | IGNCR | ICRNL | IXON); + ktermios.c_oflag &= ~OPOST; + ktermios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN); + ktermios.c_cflag &= ~(CSIZE | PARENB); + ktermios.c_cflag |= CS8; + ktermios.c_cflag |= CRTSCTS; + tty_set_termios(tty, &ktermios); + + set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); + + mutex_lock(&serport->lock); + set_bit(SERPORT_ACTIVE, &serport->flags); + mutex_unlock(&serport->lock); + + tty_unlock(serport->tty); + return 0; +} + +static void ttyport_close(struct serdev_controller *ctrl) +{ + struct serport *serport = serdev_controller_get_drvdata(ctrl); + struct tty_struct *tty = serport->tty; + + mutex_lock(&serport->lock); + + if (tty->ops->close) + tty->ops->close(tty, NULL); + + tty_release_struct(tty, serport->tty_idx); + + clear_bit(SERPORT_ACTIVE, &serport->flags); + mutex_unlock(&serport->lock); +} + +static unsigned int ttyport_set_baudrate(struct serdev_controller *ctrl, unsigned int speed) +{ + struct serport *serport = serdev_controller_get_drvdata(ctrl); + struct tty_struct *tty = serport->tty; + struct ktermios ktermios = tty->termios; + + ktermios.c_cflag &= ~CBAUD; + tty_termios_encode_baud_rate(&ktermios, speed, speed); + + /* tty_set_termios() return not checked as it is always 0 */ + tty_set_termios(tty, &ktermios); + return speed; +} + +static void ttyport_set_flow_control(struct serdev_controller *ctrl, bool enable) +{ + struct serport *serport = serdev_controller_get_drvdata(ctrl); + struct tty_struct *tty = serport->tty; + struct ktermios ktermios = tty->termios; + + if (enable) + ktermios.c_cflag |= CRTSCTS; + else + ktermios.c_cflag &= ~CRTSCTS; + + tty_set_termios(tty, &ktermios); +} + +struct serdev_controller_ops ctrl_ops = { + .write_buf = ttyport_write_buf, + .write_flush = ttyport_write_flush, + .write_room = ttyport_write_room, + .open = ttyport_open, + .close = ttyport_close, + .set_flow_control = ttyport_set_flow_control, + .set_baudrate = ttyport_set_baudrate, +}; + +int serdev_tty_port_register(struct tty_port *port, struct device *parent, + struct tty_driver *drv, int idx) +{ + struct serdev_controller *ctrl; + struct serport *serport; + int ret; + + if (!port || !drv || !parent || !parent->of_node) + return -ENODEV; + + ctrl = serdev_controller_alloc(parent, sizeof(struct serport)); + if (!ctrl) + return -ENOMEM; + serport = serdev_controller_get_drvdata(ctrl); + + mutex_init(&serport->lock); + serport->port = port; + serport->tty_idx = idx; + serport->tty_drv = drv; + + ctrl->ops = &ctrl_ops; + + ret = serdev_controller_add(ctrl); + if (ret) + goto err; + + printk(KERN_INFO "serdev: Serial port %s\n", drv->name); + return 0; + +err: + serdev_controller_put(ctrl); + return ret; +} + +void serdev_tty_port_unregister(struct tty_port *port) +{ + struct serdev_controller *ctrl = port->client_data; + struct serport *serport = serdev_controller_get_drvdata(ctrl); + + if (!serport) + return; + + serdev_controller_remove(ctrl); + port->client_ops = NULL; + port->client_data = NULL; + serdev_controller_put(ctrl); +} + +MODULE_AUTHOR("Rob Herring