Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / tty / serial / serial_base_bus.c
blob5d1677f1b651c22b9ce27b87ea010a95c2596846
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Serial base bus layer for controllers
5 * Copyright (C) 2023 Texas Instruments Incorporated - https://www.ti.com/
6 * Author: Tony Lindgren <tony@atomide.com>
8 * The serial core bus manages the serial core controller instances.
9 */
11 #include <linux/cleanup.h>
12 #include <linux/container_of.h>
13 #include <linux/device.h>
14 #include <linux/idr.h>
15 #include <linux/module.h>
16 #include <linux/serial_core.h>
17 #include <linux/slab.h>
18 #include <linux/spinlock.h>
20 #include "serial_base.h"
22 static bool serial_base_initialized;
24 static const struct device_type serial_ctrl_type = {
25 .name = "ctrl",
28 static const struct device_type serial_port_type = {
29 .name = "port",
32 static int serial_base_match(struct device *dev, const struct device_driver *drv)
34 if (dev->type == &serial_ctrl_type &&
35 str_has_prefix(drv->name, serial_ctrl_type.name))
36 return 1;
38 if (dev->type == &serial_port_type &&
39 str_has_prefix(drv->name, serial_port_type.name))
40 return 1;
42 return 0;
45 static const struct bus_type serial_base_bus_type = {
46 .name = "serial-base",
47 .match = serial_base_match,
50 int serial_base_driver_register(struct device_driver *driver)
52 driver->bus = &serial_base_bus_type;
54 return driver_register(driver);
57 void serial_base_driver_unregister(struct device_driver *driver)
59 driver_unregister(driver);
62 static int serial_base_device_init(struct uart_port *port,
63 struct device *dev,
64 struct device *parent_dev,
65 const struct device_type *type,
66 void (*release)(struct device *dev),
67 unsigned int ctrl_id,
68 unsigned int port_id)
70 device_initialize(dev);
71 dev->type = type;
72 dev->parent = parent_dev;
73 dev->bus = &serial_base_bus_type;
74 dev->release = release;
76 if (!serial_base_initialized) {
77 dev_dbg(port->dev, "uart_add_one_port() called before arch_initcall()?\n");
78 return -EPROBE_DEFER;
81 if (type == &serial_ctrl_type)
82 return dev_set_name(dev, "%s:%d", dev_name(port->dev), ctrl_id);
84 if (type == &serial_port_type)
85 return dev_set_name(dev, "%s:%d.%d", dev_name(port->dev),
86 ctrl_id, port_id);
88 return -EINVAL;
91 static void serial_base_ctrl_release(struct device *dev)
93 struct serial_ctrl_device *ctrl_dev = to_serial_base_ctrl_device(dev);
95 kfree(ctrl_dev);
98 void serial_base_ctrl_device_remove(struct serial_ctrl_device *ctrl_dev)
100 if (!ctrl_dev)
101 return;
103 device_del(&ctrl_dev->dev);
104 put_device(&ctrl_dev->dev);
107 struct serial_ctrl_device *serial_base_ctrl_add(struct uart_port *port,
108 struct device *parent)
110 struct serial_ctrl_device *ctrl_dev;
111 int err;
113 ctrl_dev = kzalloc(sizeof(*ctrl_dev), GFP_KERNEL);
114 if (!ctrl_dev)
115 return ERR_PTR(-ENOMEM);
117 ida_init(&ctrl_dev->port_ida);
119 err = serial_base_device_init(port, &ctrl_dev->dev,
120 parent, &serial_ctrl_type,
121 serial_base_ctrl_release,
122 port->ctrl_id, 0);
123 if (err)
124 goto err_put_device;
126 err = device_add(&ctrl_dev->dev);
127 if (err)
128 goto err_put_device;
130 return ctrl_dev;
132 err_put_device:
133 put_device(&ctrl_dev->dev);
135 return ERR_PTR(err);
138 static void serial_base_port_release(struct device *dev)
140 struct serial_port_device *port_dev = to_serial_base_port_device(dev);
142 kfree(port_dev);
145 struct serial_port_device *serial_base_port_add(struct uart_port *port,
146 struct serial_ctrl_device *ctrl_dev)
148 struct serial_port_device *port_dev;
149 int min = 0, max = -1; /* Use -1 for max to apply IDA defaults */
150 int err;
152 port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL);
153 if (!port_dev)
154 return ERR_PTR(-ENOMEM);
156 /* Device driver specified port_id vs automatic assignment? */
157 if (port->port_id) {
158 min = port->port_id;
159 max = port->port_id;
162 err = ida_alloc_range(&ctrl_dev->port_ida, min, max, GFP_KERNEL);
163 if (err < 0) {
164 kfree(port_dev);
165 return ERR_PTR(err);
168 port->port_id = err;
170 err = serial_base_device_init(port, &port_dev->dev,
171 &ctrl_dev->dev, &serial_port_type,
172 serial_base_port_release,
173 port->ctrl_id, port->port_id);
174 if (err)
175 goto err_put_device;
177 port_dev->port = port;
179 err = device_add(&port_dev->dev);
180 if (err)
181 goto err_put_device;
183 return port_dev;
185 err_put_device:
186 put_device(&port_dev->dev);
187 ida_free(&ctrl_dev->port_ida, port->port_id);
189 return ERR_PTR(err);
192 void serial_base_port_device_remove(struct serial_port_device *port_dev)
194 struct serial_ctrl_device *ctrl_dev;
195 struct device *parent;
197 if (!port_dev)
198 return;
200 parent = port_dev->dev.parent;
201 ctrl_dev = to_serial_base_ctrl_device(parent);
203 device_del(&port_dev->dev);
204 ida_free(&ctrl_dev->port_ida, port_dev->port->port_id);
205 put_device(&port_dev->dev);
208 #ifdef CONFIG_SERIAL_CORE_CONSOLE
211 * serial_base_match_and_update_preferred_console - Match and update a preferred console
212 * @drv: Serial port device driver
213 * @port: Serial port instance
215 * Tries to match and update the preferred console for a serial port for
216 * the kernel command line option console=DEVNAME:0.0.
218 * Cannot be called early for ISA ports, depends on struct device.
220 * Return: 0 on success, negative error code on failure.
222 int serial_base_match_and_update_preferred_console(struct uart_driver *drv,
223 struct uart_port *port)
225 const char *port_match __free(kfree) = NULL;
226 int ret;
228 port_match = kasprintf(GFP_KERNEL, "%s:%d.%d", dev_name(port->dev),
229 port->ctrl_id, port->port_id);
230 if (!port_match)
231 return -ENOMEM;
233 ret = match_devname_and_update_preferred_console(port_match,
234 drv->dev_name,
235 port->line);
236 if (ret == -ENOENT)
237 return 0;
239 return ret;
242 #endif
244 static int serial_base_init(void)
246 int ret;
248 ret = bus_register(&serial_base_bus_type);
249 if (ret)
250 return ret;
252 ret = serial_base_ctrl_init();
253 if (ret)
254 goto err_bus_unregister;
256 ret = serial_base_port_init();
257 if (ret)
258 goto err_ctrl_exit;
260 serial_base_initialized = true;
262 return 0;
264 err_ctrl_exit:
265 serial_base_ctrl_exit();
267 err_bus_unregister:
268 bus_unregister(&serial_base_bus_type);
270 return ret;
272 arch_initcall(serial_base_init);
274 static void serial_base_exit(void)
276 serial_base_port_exit();
277 serial_base_ctrl_exit();
278 bus_unregister(&serial_base_bus_type);
280 module_exit(serial_base_exit);
282 MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
283 MODULE_DESCRIPTION("Serial core bus");
284 MODULE_LICENSE("GPL");