Initial commit: Linux kernel 4.19 for Unplugged OS

This commit is contained in:
ziv committed 2025-10-06 13:52:42 +00:00
commit d30235b04c
80519 files changed
+35800681

No files matched your search

+24
View File
@@ -0,0 +1,24 @@
#
# Serial bus device driver configuration
#
menuconfig SERIAL_DEV_BUS
tristate "Serial device bus"
help
Core support for devices connected via a serial port.
Note that you typically also want to enable TTY port controller support.
if SERIAL_DEV_BUS
config SERIAL_DEV_CTRL_TTYPORT
bool "Serial device TTY port controller"
help
Say Y here if you want to use the Serial device bus with common TTY
drivers (e.g. serial drivers).
If unsure, say Y.
depends on TTY
depends on SERIAL_DEV_BUS != m
default y
endif
+5
View File
@@ -0,0 +1,5 @@
serdev-objs := core.o
obj-$(CONFIG_SERIAL_DEV_BUS) += serdev.o
obj-$(CONFIG_SERIAL_DEV_CTRL_TTYPORT) += serdev-ttyport.o
+744
View File
@@ -0,0 +1,744 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2016-2017 Linaro Ltd., Rob Herring <robh@kernel.org>
*
* Based on drivers/spmi/spmi.c:
* Copyright (c) 2012-2015, The Linux Foundation. All rights reserved.
*/
#include <linux/acpi.h>
#include <linux/errno.h>
#include <linux/idr.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/pm_domain.h>
#include <linux/pm_runtime.h>
#include <linux/serdev.h>
#include <linux/slab.h>
static bool is_registered;
static DEFINE_IDA(ctrl_ida);
static ssize_t modalias_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
int len;
len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1);
if (len != -ENODEV)
return len;
len = of_device_modalias(dev, buf, PAGE_SIZE);
if (len != -ENODEV)
return len;
if (dev->parent->parent->bus == &platform_bus_type) {
struct platform_device *pdev =
to_platform_device(dev->parent->parent);
len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name);
}
return len;
}
static DEVICE_ATTR_RO(modalias);
static struct attribute *serdev_device_attrs[] = {
&dev_attr_modalias.attr,
NULL,
};
ATTRIBUTE_GROUPS(serdev_device);
static int serdev_device_uevent(struct device *dev, struct kobj_uevent_env *env)
{
int rc;
rc = acpi_device_uevent_modalias(dev, env);
if (rc != -ENODEV)
return rc;
rc = of_device_uevent_modalias(dev, env);
if (rc != -ENODEV)
return rc;
if (dev->parent->parent->bus == &platform_bus_type)
rc = dev->parent->parent->bus->uevent(dev->parent->parent, env);
return rc;
}
static void serdev_device_release(struct device *dev)
{
struct serdev_device *serdev = to_serdev_device(dev);
kfree(serdev);
}
static const struct device_type serdev_device_type = {
.groups = serdev_device_groups,
.uevent = serdev_device_uevent,
.release = serdev_device_release,
};
static bool is_serdev_device(const struct device *dev)
{
return dev->type == &serdev_device_type;
}
static void serdev_ctrl_release(struct device *dev)
{
struct serdev_controller *ctrl = to_serdev_controller(dev);
ida_simple_remove(&ctrl_ida, ctrl->nr);
kfree(ctrl);
}
static const struct device_type serdev_ctrl_type = {
.release = serdev_ctrl_release,
};
static int serdev_device_match(struct device *dev, struct device_driver *drv)
{
if (!is_serdev_device(dev))
return 0;
if (acpi_driver_match_device(dev, drv))
return 1;
if (of_driver_match_device(dev, drv))
return 1;
if (dev->parent->parent->bus == &platform_bus_type &&
dev->parent->parent->bus->match(dev->parent->parent, drv))
return 1;
return 0;
}
/**
* serdev_device_add() - add a device previously constructed via serdev_device_alloc()
* @serdev: serdev_device to be added
*/
int serdev_device_add(struct serdev_device *serdev)
{
struct serdev_controller *ctrl = serdev->ctrl;
struct device *parent = serdev->dev.parent;
int err;
dev_set_name(&serdev->dev, "%s-%d", dev_name(parent), serdev->nr);
/* Only a single slave device is currently supported. */
if (ctrl->serdev) {
dev_err(&serdev->dev, "controller busy\n");
return -EBUSY;
}
ctrl->serdev = serdev;
err = device_add(&serdev->dev);
if (err < 0) {
dev_err(&serdev->dev, "Can't add %s, status %d\n",
dev_name(&serdev->dev), err);
goto err_clear_serdev;
}
dev_dbg(&serdev->dev, "device %s registered\n", dev_name(&serdev->dev));
return 0;
err_clear_serdev:
ctrl->serdev = NULL;
return err;
}
EXPORT_SYMBOL_GPL(serdev_device_add);
/**
* serdev_device_remove(): remove an serdev device
* @serdev: serdev_device to be removed
*/
void serdev_device_remove(struct serdev_device *serdev)
{
struct serdev_controller *ctrl = serdev->ctrl;
device_unregister(&serdev->dev);
ctrl->serdev = NULL;
}
EXPORT_SYMBOL_GPL(serdev_device_remove);
int serdev_device_open(struct serdev_device *serdev)
{
struct serdev_controller *ctrl = serdev->ctrl;
int ret;
if (!ctrl || !ctrl->ops->open)
return -EINVAL;
ret = ctrl->ops->open(ctrl);
if (ret)
return ret;
ret = pm_runtime_get_sync(&ctrl->dev);
if (ret < 0) {
pm_runtime_put_noidle(&ctrl->dev);
goto err_close;
}
return 0;
err_close:
if (ctrl->ops->close)
ctrl->ops->close(ctrl);
return ret;
}
EXPORT_SYMBOL_GPL(serdev_device_open);
void serdev_device_close(struct serdev_device *serdev)
{
struct serdev_controller *ctrl = serdev->ctrl;
if (!ctrl || !ctrl->ops->close)
return;
pm_runtime_put(&ctrl->dev);
ctrl->ops->close(ctrl);
}
EXPORT_SYMBOL_GPL(serdev_device_close);
static void devm_serdev_device_release(struct device *dev, void *dr)
{
serdev_device_close(*(struct serdev_device **)dr);
}
int devm_serdev_device_open(struct device *dev, struct serdev_device *serdev)
{
struct serdev_device **dr;
int ret;
dr = devres_alloc(devm_serdev_device_release, sizeof(*dr), GFP_KERNEL);
if (!dr)
return -ENOMEM;
ret = serdev_device_open(serdev);
if (ret) {
devres_free(dr);
return ret;
}
*dr = serdev;
devres_add(dev, dr);
return 0;
}
EXPORT_SYMBOL_GPL(devm_serdev_device_open);
void serdev_device_write_wakeup(struct serdev_device *serdev)
{
complete(&serdev->write_comp);
}
EXPORT_SYMBOL_GPL(serdev_device_write_wakeup);
int serdev_device_write_buf(struct serdev_device *serdev,
const unsigned char *buf, size_t count)
{
struct serdev_controller *ctrl = serdev->ctrl;
if (!ctrl || !ctrl->ops->write_buf)
return -EINVAL;
return ctrl->ops->write_buf(ctrl, buf, count);
}
EXPORT_SYMBOL_GPL(serdev_device_write_buf);
int serdev_device_write(struct serdev_device *serdev,
const unsigned char *buf, size_t count,
unsigned long timeout)
{
struct serdev_controller *ctrl = serdev->ctrl;
int ret;
if (!ctrl || !ctrl->ops->write_buf ||
(timeout && !serdev->ops->write_wakeup))
return -EINVAL;
mutex_lock(&serdev->write_lock);
do {
reinit_completion(&serdev->write_comp);
ret = ctrl->ops->write_buf(ctrl, buf, count);
if (ret < 0)
break;
buf += ret;
count -= ret;
} while (count &&
(timeout = wait_for_completion_timeout(&serdev->write_comp,
timeout)));
mutex_unlock(&serdev->write_lock);
return ret < 0 ? ret : (count ? -ETIMEDOUT : 0);
}
EXPORT_SYMBOL_GPL(serdev_device_write);
void serdev_device_write_flush(struct serdev_device *serdev)
{
struct serdev_controller *ctrl = serdev->ctrl;
if (!ctrl || !ctrl->ops->write_flush)
return;
ctrl->ops->write_flush(ctrl);
}
EXPORT_SYMBOL_GPL(serdev_device_write_flush);
int serdev_device_write_room(struct serdev_device *serdev)
{
struct serdev_controller *ctrl = serdev->ctrl;
if (!ctrl || !ctrl->ops->write_room)
return 0;
return serdev->ctrl->ops->write_room(ctrl);
}
EXPORT_SYMBOL_GPL(serdev_device_write_room);
unsigned int serdev_device_set_baudrate(struct serdev_device *serdev, unsigned int speed)
{
struct serdev_controller *ctrl = serdev->ctrl;
if (!ctrl || !ctrl->ops->set_baudrate)
return 0;
return ctrl->ops->set_baudrate(ctrl, speed);
}
EXPORT_SYMBOL_GPL(serdev_device_set_baudrate);
void serdev_device_set_flow_control(struct serdev_device *serdev, bool enable)
{
struct serdev_controller *ctrl = serdev->ctrl;
if (!ctrl || !ctrl->ops->set_flow_control)
return;
ctrl->ops->set_flow_control(ctrl, enable);
}
EXPORT_SYMBOL_GPL(serdev_device_set_flow_control);
int serdev_device_set_parity(struct serdev_device *serdev,
enum serdev_parity parity)
{
struct serdev_controller *ctrl = serdev->ctrl;
if (!ctrl || !ctrl->ops->set_parity)
return -ENOTSUPP;
return ctrl->ops->set_parity(ctrl, parity);
}
EXPORT_SYMBOL_GPL(serdev_device_set_parity);
void serdev_device_wait_until_sent(struct serdev_device *serdev, long timeout)
{
struct serdev_controller *ctrl = serdev->ctrl;
if (!ctrl || !ctrl->ops->wait_until_sent)
return;
ctrl->ops->wait_until_sent(ctrl, timeout);
}
EXPORT_SYMBOL_GPL(serdev_device_wait_until_sent);
int serdev_device_get_tiocm(struct serdev_device *serdev)
{
struct serdev_controller *ctrl = serdev->ctrl;
if (!ctrl || !ctrl->ops->get_tiocm)
return -ENOTSUPP;
return ctrl->ops->get_tiocm(ctrl);
}
EXPORT_SYMBOL_GPL(serdev_device_get_tiocm);
int serdev_device_set_tiocm(struct serdev_device *serdev, int set, int clear)
{
struct serdev_controller *ctrl = serdev->ctrl;
if (!ctrl || !ctrl->ops->set_tiocm)
return -ENOTSUPP;
return ctrl->ops->set_tiocm(ctrl, set, clear);
}
EXPORT_SYMBOL_GPL(serdev_device_set_tiocm);
static int serdev_drv_probe(struct device *dev)
{
const struct serdev_device_driver *sdrv = to_serdev_device_driver(dev->driver);
int ret;
ret = dev_pm_domain_attach(dev, true);
if (ret)
return ret;
ret = sdrv->probe(to_serdev_device(dev));
if (ret)
dev_pm_domain_detach(dev, true);
return ret;
}
static int serdev_drv_remove(struct device *dev)
{
const struct serdev_device_driver *sdrv = to_serdev_device_driver(dev->driver);
if (sdrv->remove)
sdrv->remove(to_serdev_device(dev));
dev_pm_domain_detach(dev, true);
return 0;
}
static struct bus_type serdev_bus_type = {
.name = "serial",
.match = serdev_device_match,
.probe = serdev_drv_probe,
.remove = serdev_drv_remove,
};
/**
* serdev_device_alloc() - Allocate a new serdev device
* @ctrl: associated controller
*
* Caller is responsible for either calling serdev_device_add() to add the
* newly allocated controller, or calling serdev_device_put() to discard it.
*/
struct serdev_device *serdev_device_alloc(struct serdev_controller *ctrl)
{
struct serdev_device *serdev;
serdev = kzalloc(sizeof(*serdev), GFP_KERNEL);
if (!serdev)
return NULL;
serdev->ctrl = ctrl;
device_initialize(&serdev->dev);
serdev->dev.parent = &ctrl->dev;
serdev->dev.bus = &serdev_bus_type;
serdev->dev.type = &serdev_device_type;
init_completion(&serdev->write_comp);
mutex_init(&serdev->write_lock);
return serdev;
}
EXPORT_SYMBOL_GPL(serdev_device_alloc);
/**
* serdev_controller_alloc() - Allocate a new serdev controller
* @parent: parent device
* @size: size of private data
*
* Caller is responsible for either calling serdev_controller_add() to add the
* newly allocated controller, or calling serdev_controller_put() to discard it.
* The allocated private data region may be accessed via
* serdev_controller_get_drvdata()
*/
struct serdev_controller *serdev_controller_alloc(struct device *parent,
size_t size)
{
struct serdev_controller *ctrl;
int id;
if (WARN_ON(!parent))
return NULL;
ctrl = kzalloc(sizeof(*ctrl) + size, GFP_KERNEL);
if (!ctrl)
return NULL;
id = ida_simple_get(&ctrl_ida, 0, 0, GFP_KERNEL);
if (id < 0) {
dev_err(parent,
"unable to allocate serdev controller identifier.\n");
goto err_free;
}
ctrl->nr = id;
device_initialize(&ctrl->dev);
ctrl->dev.type = &serdev_ctrl_type;
ctrl->dev.bus = &serdev_bus_type;
ctrl->dev.parent = parent;
ctrl->dev.of_node = parent->of_node;
serdev_controller_set_drvdata(ctrl, &ctrl[1]);
dev_set_name(&ctrl->dev, "serial%d", id);
pm_runtime_no_callbacks(&ctrl->dev);
pm_suspend_ignore_children(&ctrl->dev, true);
dev_dbg(&ctrl->dev, "allocated controller 0x%p id %d\n", ctrl, id);
return ctrl;
err_free:
kfree(ctrl);
return NULL;
}
EXPORT_SYMBOL_GPL(serdev_controller_alloc);
static int of_serdev_register_devices(struct serdev_controller *ctrl)
{
struct device_node *node;
struct serdev_device *serdev = NULL;
int err;
bool found = false;
for_each_available_child_of_node(ctrl->dev.of_node, node) {
if (!of_get_property(node, "compatible", NULL))
continue;
dev_dbg(&ctrl->dev, "adding child %pOF\n", node);
serdev = serdev_device_alloc(ctrl);
if (!serdev)
continue;
serdev->dev.of_node = node;
err = serdev_device_add(serdev);
if (err) {
dev_err(&serdev->dev,
"failure adding device. status %d\n", err);
serdev_device_put(serdev);
} else
found = true;
}
if (!found)
return -ENODEV;
return 0;
}
#ifdef CONFIG_ACPI
static acpi_status acpi_serdev_register_device(struct serdev_controller *ctrl,
struct acpi_device *adev)
{
struct serdev_device *serdev = NULL;
int err;
if (acpi_bus_get_status(adev) || !adev->status.present ||
acpi_device_enumerated(adev))
return AE_OK;
serdev = serdev_device_alloc(ctrl);
if (!serdev) {
dev_err(&ctrl->dev, "failed to allocate serdev device for %s\n",
dev_name(&adev->dev));
return AE_NO_MEMORY;
}
ACPI_COMPANION_SET(&serdev->dev, adev);
acpi_device_set_enumerated(adev);
err = serdev_device_add(serdev);
if (err) {
dev_err(&serdev->dev,
"failure adding ACPI serdev device. status %d\n", err);
serdev_device_put(serdev);
}
return AE_OK;
}
static const struct acpi_device_id serdev_acpi_devices_blacklist[] = {
{ "INT3511", 0 },
{ "INT3512", 0 },
{ },
};
static acpi_status acpi_serdev_add_device(acpi_handle handle, u32 level,
void *data, void **return_value)
{
struct serdev_controller *ctrl = data;
struct acpi_device *adev;
if (acpi_bus_get_device(handle, &adev))
return AE_OK;
/* Skip if black listed */
if (!acpi_match_device_ids(adev, serdev_acpi_devices_blacklist))
return AE_OK;
return acpi_serdev_register_device(ctrl, adev);
}
static int acpi_serdev_register_devices(struct serdev_controller *ctrl)
{
acpi_status status;
acpi_handle handle;
handle = ACPI_HANDLE(ctrl->dev.parent);
if (!handle)
return -ENODEV;
status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
acpi_serdev_add_device, NULL, ctrl, NULL);
if (ACPI_FAILURE(status))
dev_dbg(&ctrl->dev, "failed to enumerate serdev slaves\n");
if (!ctrl->serdev)
return -ENODEV;
return 0;
}
#else
static inline int acpi_serdev_register_devices(struct serdev_controller *ctrl)
{
return -ENODEV;
}
#endif /* CONFIG_ACPI */
static int platform_serdev_register_devices(struct serdev_controller *ctrl)
{
struct serdev_device *serdev;
int err;
if (ctrl->dev.parent->bus != &platform_bus_type)
return -ENODEV;
serdev = serdev_device_alloc(ctrl);
if (!serdev) {
dev_err(&ctrl->dev, "failed to allocate serdev device for %s\n",
dev_name(ctrl->dev.parent));
return -ENOMEM;
}
pm_runtime_no_callbacks(&serdev->dev);
err = serdev_device_add(serdev);
if (err) {
dev_err(&serdev->dev,
"failure adding device. status %d\n", err);
serdev_device_put(serdev);
}
return err;
}
/**
* serdev_controller_add_platform() - Add an serdev controller
* @ctrl: controller to be registered.
* @platform: whether to permit fallthrough to platform device probe
*
* Register a controller previously allocated via serdev_controller_alloc() with
* the serdev core. Optionally permit probing via a platform device fallback.
*/
int serdev_controller_add_platform(struct serdev_controller *ctrl, bool platform)
{
int ret, ret_of, ret_acpi, ret_platform = -ENODEV;
/* Can't register until after driver model init */
if (WARN_ON(!is_registered))
return -EAGAIN;
ret = device_add(&ctrl->dev);
if (ret)
return ret;
pm_runtime_enable(&ctrl->dev);
ret_of = of_serdev_register_devices(ctrl);
ret_acpi = acpi_serdev_register_devices(ctrl);
if (platform)
ret_platform = platform_serdev_register_devices(ctrl);
if (ret_of && ret_acpi && ret_platform) {
dev_dbg(&ctrl->dev, "no devices registered: of:%d acpi:%d "
"platform:%d\n",
ret_of, ret_acpi, ret_platform);
ret = -ENODEV;
goto err_rpm_disable;
}
dev_dbg(&ctrl->dev, "serdev%d registered: dev:%p\n",
ctrl->nr, &ctrl->dev);
return 0;
err_rpm_disable:
pm_runtime_disable(&ctrl->dev);
device_del(&ctrl->dev);
return ret;
};
EXPORT_SYMBOL_GPL(serdev_controller_add_platform);
/* Remove a device associated with a controller */
static int serdev_remove_device(struct device *dev, void *data)
{
struct serdev_device *serdev = to_serdev_device(dev);
if (dev->type == &serdev_device_type)
serdev_device_remove(serdev);
return 0;
}
/**
* serdev_controller_remove(): remove an serdev controller
* @ctrl: controller to remove
*
* Remove a serdev controller. Caller is responsible for calling
* serdev_controller_put() to discard the allocated controller.
*/
void serdev_controller_remove(struct serdev_controller *ctrl)
{
int dummy;
if (!ctrl)
return;
dummy = device_for_each_child(&ctrl->dev, NULL,
serdev_remove_device);
pm_runtime_disable(&ctrl->dev);
device_del(&ctrl->dev);
}
EXPORT_SYMBOL_GPL(serdev_controller_remove);
/**
* serdev_driver_register() - Register client driver with serdev core
* @sdrv: client driver to be associated with client-device.
*
* This API will register the client driver with the serdev framework.
* It is typically called from the driver's module-init function.
*/
int __serdev_device_driver_register(struct serdev_device_driver *sdrv, struct module *owner)
{
sdrv->driver.bus = &serdev_bus_type;
sdrv->driver.owner = owner;
/* force drivers to async probe so I/O is possible in probe */
sdrv->driver.probe_type = PROBE_PREFER_ASYNCHRONOUS;
return driver_register(&sdrv->driver);
}
EXPORT_SYMBOL_GPL(__serdev_device_driver_register);
static void __exit serdev_exit(void)
{
bus_unregister(&serdev_bus_type);
ida_destroy(&ctrl_ida);
}
module_exit(serdev_exit);
static int __init serdev_init(void)
{
int ret;
ret = bus_register(&serdev_bus_type);
if (ret)
return ret;
is_registered = true;
return 0;
}
/* Must be before serial drivers register */
postcore_initcall(serdev_init);
MODULE_AUTHOR("Rob Herring <robh@kernel.org>");
MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("Serial attached device bus");
+346
View File
@@ -0,0 +1,346 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2016-2017 Linaro Ltd., Rob Herring <robh@kernel.org>
*/
#include <linux/kernel.h>
#include <linux/serdev.h>
#include <linux/tty.h>
#include <linux/tty_driver.h>
#include <linux/poll.h>
#include <linux/platform_device.h>
#include <linux/module.h>
#define SERPORT_ACTIVE 1
static char *pdev_tty_port;
module_param(pdev_tty_port, charp, 0644);
MODULE_PARM_DESC(pdev_tty_port, "platform device tty port to claim");
struct serport {
struct tty_port *port;
struct tty_struct *tty;
struct tty_driver *tty_drv;
int tty_idx;
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);
int ret;
if (!test_bit(SERPORT_ACTIVE, &serport->flags))
return 0;
ret = serdev_controller_receive_buf(ctrl, cp, count);
dev_WARN_ONCE(&ctrl->dev, ret < 0 || ret > count,
"receive_buf returns %d (count = %zu)\n",
ret, count);
if (ret < 0)
return 0;
else if (ret > count)
return count;
return ret;
}
static void ttyport_write_wakeup(struct tty_port *port)
{
struct serdev_controller *ctrl = port->client_data;
struct serport *serport = serdev_controller_get_drvdata(ctrl);
struct tty_struct *tty;
tty = tty_port_tty_get(port);
if (!tty)
return;
if (test_and_clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags) &&
test_bit(SERPORT_ACTIVE, &serport->flags))
serdev_controller_write_wakeup(ctrl);
/* Wake up any tty_wait_until_sent() */
wake_up_interruptible(&tty->write_wait);
tty_kref_put(tty);
}
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;
if (!test_bit(SERPORT_ACTIVE, &serport->flags))
return 0;
set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
return 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;
int ret;
tty = tty_init_dev(serport->tty_drv, serport->tty_idx);
if (IS_ERR(tty))
return PTR_ERR(tty);
serport->tty = tty;
if (!tty->ops->open || !tty->ops->close) {
ret = -ENODEV;
goto err_unlock;
}
ret = tty->ops->open(serport->tty, NULL);
if (ret)
goto err_close;
tty_unlock(serport->tty);
/* 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;
/* Hangups are not supported so make sure to ignore carrier detect. */
ktermios.c_cflag |= CLOCAL;
tty_set_termios(tty, &ktermios);
set_bit(SERPORT_ACTIVE, &serport->flags);
return 0;
err_close:
tty->ops->close(tty, NULL);
err_unlock:
tty_unlock(tty);
tty_release_struct(tty, serport->tty_idx);
return ret;
}
static void ttyport_close(struct serdev_controller *ctrl)
{
struct serport *serport = serdev_controller_get_drvdata(ctrl);
struct tty_struct *tty = serport->tty;
clear_bit(SERPORT_ACTIVE, &serport->flags);
tty_lock(tty);
if (tty->ops->close)
tty->ops->close(tty, NULL);
tty_unlock(tty);
tty_release_struct(tty, serport->tty_idx);
}
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 ktermios.c_ospeed;
}
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);
}
static int ttyport_set_parity(struct serdev_controller *ctrl,
enum serdev_parity parity)
{
struct serport *serport = serdev_controller_get_drvdata(ctrl);
struct tty_struct *tty = serport->tty;
struct ktermios ktermios = tty->termios;
ktermios.c_cflag &= ~(PARENB | PARODD | CMSPAR);
if (parity != SERDEV_PARITY_NONE) {
ktermios.c_cflag |= PARENB;
if (parity == SERDEV_PARITY_ODD)
ktermios.c_cflag |= PARODD;
}
tty_set_termios(tty, &ktermios);
if ((tty->termios.c_cflag & (PARENB | PARODD | CMSPAR)) !=
(ktermios.c_cflag & (PARENB | PARODD | CMSPAR)))
return -EINVAL;
return 0;
}
static void ttyport_wait_until_sent(struct serdev_controller *ctrl, long timeout)
{
struct serport *serport = serdev_controller_get_drvdata(ctrl);
struct tty_struct *tty = serport->tty;
tty_wait_until_sent(tty, timeout);
}
static int ttyport_get_tiocm(struct serdev_controller *ctrl)
{
struct serport *serport = serdev_controller_get_drvdata(ctrl);
struct tty_struct *tty = serport->tty;
if (!tty->ops->tiocmget)
return -ENOTSUPP;
return tty->driver->ops->tiocmget(tty);
}
static int ttyport_set_tiocm(struct serdev_controller *ctrl, unsigned int set, unsigned int clear)
{
struct serport *serport = serdev_controller_get_drvdata(ctrl);
struct tty_struct *tty = serport->tty;
if (!tty->ops->tiocmset)
return -ENOTSUPP;
return tty->driver->ops->tiocmset(tty, set, clear);
}
static const 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_parity = ttyport_set_parity,
.set_baudrate = ttyport_set_baudrate,
.wait_until_sent = ttyport_wait_until_sent,
.get_tiocm = ttyport_get_tiocm,
.set_tiocm = ttyport_set_tiocm,
};
struct device *serdev_tty_port_register(struct tty_port *port,
struct device *parent,
struct tty_driver *drv, int idx)
{
struct serdev_controller *ctrl;
struct serport *serport;
bool platform = false;
int ret;
if (!port || !drv || !parent)
return ERR_PTR(-ENODEV);
ctrl = serdev_controller_alloc(parent, sizeof(struct serport));
if (!ctrl)
return ERR_PTR(-ENOMEM);
serport = serdev_controller_get_drvdata(ctrl);
serport->port = port;
serport->tty_idx = idx;
serport->tty_drv = drv;
ctrl->ops = &ctrl_ops;
port->client_ops = &client_ops;
port->client_data = ctrl;
/* There is not always a way to bind specific platform devices because
* they may be defined on platforms without DT or ACPI. When dealing
* with a platform devices, do not allow direct binding unless it is
* whitelisted by module parameter. If a platform device is otherwise
* described by DT or ACPI it will still be bound and this check will
* be ignored.
*/
if (parent->bus == &platform_bus_type) {
if (pdev_tty_port) {
unsigned long pdev_idx;
int tty_len = strlen(drv->name);
if (!strncmp(pdev_tty_port, drv->name, tty_len)) {
if (!kstrtoul(pdev_tty_port + tty_len, 10,
&pdev_idx) && pdev_idx == idx) {
platform = true;
}
}
}
}
ret = serdev_controller_add_platform(ctrl, platform);
if (ret)
goto err_reset_data;
dev_info(&ctrl->dev, "tty port %s%d registered\n", drv->name, idx);
return &ctrl->dev;
err_reset_data:
port->client_data = NULL;
port->client_ops = &tty_port_default_client_ops;
serdev_controller_put(ctrl);
return ERR_PTR(ret);
}
int 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 -ENODEV;
serdev_controller_remove(ctrl);
port->client_data = NULL;
port->client_ops = &tty_port_default_client_ops;
serdev_controller_put(ctrl);
return 0;
}