xref: /linux/drivers/tty/serial/serial_base_bus.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
184a9582fSTony Lindgren // SPDX-License-Identifier: GPL-2.0+
284a9582fSTony Lindgren /*
384a9582fSTony Lindgren  * Serial base bus layer for controllers
484a9582fSTony Lindgren  *
584a9582fSTony Lindgren  * Copyright (C) 2023 Texas Instruments Incorporated - https://www.ti.com/
684a9582fSTony Lindgren  * Author: Tony Lindgren <tony@atomide.com>
784a9582fSTony Lindgren  *
884a9582fSTony Lindgren  * The serial core bus manages the serial core controller instances.
984a9582fSTony Lindgren  */
1084a9582fSTony Lindgren 
1112c91cecSTony Lindgren #include <linux/cleanup.h>
1284a9582fSTony Lindgren #include <linux/container_of.h>
1384a9582fSTony Lindgren #include <linux/device.h>
1404c7f60cSTony Lindgren #include <linux/idr.h>
1584a9582fSTony Lindgren #include <linux/module.h>
1684a9582fSTony Lindgren #include <linux/serial_core.h>
1784a9582fSTony Lindgren #include <linux/slab.h>
1884a9582fSTony Lindgren #include <linux/spinlock.h>
1984a9582fSTony Lindgren 
2084a9582fSTony Lindgren #include "serial_base.h"
2184a9582fSTony Lindgren 
2253991424STony Lindgren static bool serial_base_initialized;
2353991424STony Lindgren 
241ef2c2dfSTony Lindgren static const struct device_type serial_ctrl_type = {
251ef2c2dfSTony Lindgren 	.name = "ctrl",
261ef2c2dfSTony Lindgren };
271ef2c2dfSTony Lindgren 
281ef2c2dfSTony Lindgren static const struct device_type serial_port_type = {
291ef2c2dfSTony Lindgren 	.name = "port",
301ef2c2dfSTony Lindgren };
311ef2c2dfSTony Lindgren 
serial_base_match(struct device * dev,const struct device_driver * drv)32*d69d8048SGreg Kroah-Hartman static int serial_base_match(struct device *dev, const struct device_driver *drv)
3384a9582fSTony Lindgren {
347d695d83STony Lindgren 	if (dev->type == &serial_ctrl_type &&
357d695d83STony Lindgren 	    str_has_prefix(drv->name, serial_ctrl_type.name))
367d695d83STony Lindgren 		return 1;
3784a9582fSTony Lindgren 
387d695d83STony Lindgren 	if (dev->type == &serial_port_type &&
397d695d83STony Lindgren 	    str_has_prefix(drv->name, serial_port_type.name))
407d695d83STony Lindgren 		return 1;
417d695d83STony Lindgren 
427d695d83STony Lindgren 	return 0;
4384a9582fSTony Lindgren }
4484a9582fSTony Lindgren 
45f60ce0e4SRicardo B. Marliere static const struct bus_type serial_base_bus_type = {
4684a9582fSTony Lindgren 	.name = "serial-base",
4784a9582fSTony Lindgren 	.match = serial_base_match,
4884a9582fSTony Lindgren };
4984a9582fSTony Lindgren 
serial_base_driver_register(struct device_driver * driver)5084a9582fSTony Lindgren int serial_base_driver_register(struct device_driver *driver)
5184a9582fSTony Lindgren {
5284a9582fSTony Lindgren 	driver->bus = &serial_base_bus_type;
5384a9582fSTony Lindgren 
5484a9582fSTony Lindgren 	return driver_register(driver);
5584a9582fSTony Lindgren }
5684a9582fSTony Lindgren 
serial_base_driver_unregister(struct device_driver * driver)5784a9582fSTony Lindgren void serial_base_driver_unregister(struct device_driver *driver)
5884a9582fSTony Lindgren {
5984a9582fSTony Lindgren 	driver_unregister(driver);
6084a9582fSTony Lindgren }
6184a9582fSTony Lindgren 
serial_base_device_init(struct uart_port * port,struct device * dev,struct device * parent_dev,const struct device_type * type,void (* release)(struct device * dev),unsigned int ctrl_id,unsigned int port_id)6284a9582fSTony Lindgren static int serial_base_device_init(struct uart_port *port,
6384a9582fSTony Lindgren 				   struct device *dev,
6484a9582fSTony Lindgren 				   struct device *parent_dev,
6584a9582fSTony Lindgren 				   const struct device_type *type,
6684a9582fSTony Lindgren 				   void (*release)(struct device *dev),
671ef2c2dfSTony Lindgren 				   unsigned int ctrl_id,
681ef2c2dfSTony Lindgren 				   unsigned int port_id)
6984a9582fSTony Lindgren {
7084a9582fSTony Lindgren 	device_initialize(dev);
7184a9582fSTony Lindgren 	dev->type = type;
7284a9582fSTony Lindgren 	dev->parent = parent_dev;
7384a9582fSTony Lindgren 	dev->bus = &serial_base_bus_type;
7484a9582fSTony Lindgren 	dev->release = release;
7584a9582fSTony Lindgren 
76cef09673SDan Carpenter 	if (!serial_base_initialized) {
77cef09673SDan Carpenter 		dev_dbg(port->dev, "uart_add_one_port() called before arch_initcall()?\n");
78cef09673SDan Carpenter 		return -EPROBE_DEFER;
79cef09673SDan Carpenter 	}
80cef09673SDan Carpenter 
811ef2c2dfSTony Lindgren 	if (type == &serial_ctrl_type)
821ef2c2dfSTony Lindgren 		return dev_set_name(dev, "%s:%d", dev_name(port->dev), ctrl_id);
8384a9582fSTony Lindgren 
841ef2c2dfSTony Lindgren 	if (type == &serial_port_type)
851ef2c2dfSTony Lindgren 		return dev_set_name(dev, "%s:%d.%d", dev_name(port->dev),
861ef2c2dfSTony Lindgren 				    ctrl_id, port_id);
871ef2c2dfSTony Lindgren 
881ef2c2dfSTony Lindgren 	return -EINVAL;
891ef2c2dfSTony Lindgren }
9084a9582fSTony Lindgren 
serial_base_ctrl_release(struct device * dev)9184a9582fSTony Lindgren static void serial_base_ctrl_release(struct device *dev)
9284a9582fSTony Lindgren {
9384a9582fSTony Lindgren 	struct serial_ctrl_device *ctrl_dev = to_serial_base_ctrl_device(dev);
9484a9582fSTony Lindgren 
9584a9582fSTony Lindgren 	kfree(ctrl_dev);
9684a9582fSTony Lindgren }
9784a9582fSTony Lindgren 
serial_base_ctrl_device_remove(struct serial_ctrl_device * ctrl_dev)9884a9582fSTony Lindgren void serial_base_ctrl_device_remove(struct serial_ctrl_device *ctrl_dev)
9984a9582fSTony Lindgren {
10084a9582fSTony Lindgren 	if (!ctrl_dev)
10184a9582fSTony Lindgren 		return;
10284a9582fSTony Lindgren 
10384a9582fSTony Lindgren 	device_del(&ctrl_dev->dev);
1046be1a8d5STony Lindgren 	put_device(&ctrl_dev->dev);
10584a9582fSTony Lindgren }
10684a9582fSTony Lindgren 
serial_base_ctrl_add(struct uart_port * port,struct device * parent)10784a9582fSTony Lindgren struct serial_ctrl_device *serial_base_ctrl_add(struct uart_port *port,
10884a9582fSTony Lindgren 						struct device *parent)
10984a9582fSTony Lindgren {
11084a9582fSTony Lindgren 	struct serial_ctrl_device *ctrl_dev;
11184a9582fSTony Lindgren 	int err;
11284a9582fSTony Lindgren 
11384a9582fSTony Lindgren 	ctrl_dev = kzalloc(sizeof(*ctrl_dev), GFP_KERNEL);
11484a9582fSTony Lindgren 	if (!ctrl_dev)
11584a9582fSTony Lindgren 		return ERR_PTR(-ENOMEM);
11684a9582fSTony Lindgren 
11704c7f60cSTony Lindgren 	ida_init(&ctrl_dev->port_ida);
11804c7f60cSTony Lindgren 
11984a9582fSTony Lindgren 	err = serial_base_device_init(port, &ctrl_dev->dev,
12084a9582fSTony Lindgren 				      parent, &serial_ctrl_type,
12184a9582fSTony Lindgren 				      serial_base_ctrl_release,
1221ef2c2dfSTony Lindgren 				      port->ctrl_id, 0);
12384a9582fSTony Lindgren 	if (err)
12452861a3bSDan Carpenter 		goto err_put_device;
12584a9582fSTony Lindgren 
12684a9582fSTony Lindgren 	err = device_add(&ctrl_dev->dev);
12784a9582fSTony Lindgren 	if (err)
12884a9582fSTony Lindgren 		goto err_put_device;
12984a9582fSTony Lindgren 
13084a9582fSTony Lindgren 	return ctrl_dev;
13184a9582fSTony Lindgren 
13284a9582fSTony Lindgren err_put_device:
13384a9582fSTony Lindgren 	put_device(&ctrl_dev->dev);
13484a9582fSTony Lindgren 
13584a9582fSTony Lindgren 	return ERR_PTR(err);
13684a9582fSTony Lindgren }
13784a9582fSTony Lindgren 
serial_base_port_release(struct device * dev)13884a9582fSTony Lindgren static void serial_base_port_release(struct device *dev)
13984a9582fSTony Lindgren {
14084a9582fSTony Lindgren 	struct serial_port_device *port_dev = to_serial_base_port_device(dev);
14184a9582fSTony Lindgren 
14284a9582fSTony Lindgren 	kfree(port_dev);
14384a9582fSTony Lindgren }
14484a9582fSTony Lindgren 
serial_base_port_add(struct uart_port * port,struct serial_ctrl_device * ctrl_dev)14584a9582fSTony Lindgren struct serial_port_device *serial_base_port_add(struct uart_port *port,
14684a9582fSTony Lindgren 						struct serial_ctrl_device *ctrl_dev)
14784a9582fSTony Lindgren {
14884a9582fSTony Lindgren 	struct serial_port_device *port_dev;
14904c7f60cSTony Lindgren 	int min = 0, max = -1;	/* Use -1 for max to apply IDA defaults */
15084a9582fSTony Lindgren 	int err;
15184a9582fSTony Lindgren 
15284a9582fSTony Lindgren 	port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL);
15384a9582fSTony Lindgren 	if (!port_dev)
15484a9582fSTony Lindgren 		return ERR_PTR(-ENOMEM);
15584a9582fSTony Lindgren 
15604c7f60cSTony Lindgren 	/* Device driver specified port_id vs automatic assignment? */
15704c7f60cSTony Lindgren 	if (port->port_id) {
15804c7f60cSTony Lindgren 		min = port->port_id;
15904c7f60cSTony Lindgren 		max = port->port_id;
16004c7f60cSTony Lindgren 	}
16104c7f60cSTony Lindgren 
16204c7f60cSTony Lindgren 	err = ida_alloc_range(&ctrl_dev->port_ida, min, max, GFP_KERNEL);
16304c7f60cSTony Lindgren 	if (err < 0) {
16404c7f60cSTony Lindgren 		kfree(port_dev);
16504c7f60cSTony Lindgren 		return ERR_PTR(err);
16604c7f60cSTony Lindgren 	}
16704c7f60cSTony Lindgren 
16804c7f60cSTony Lindgren 	port->port_id = err;
16904c7f60cSTony Lindgren 
17084a9582fSTony Lindgren 	err = serial_base_device_init(port, &port_dev->dev,
17184a9582fSTony Lindgren 				      &ctrl_dev->dev, &serial_port_type,
17284a9582fSTony Lindgren 				      serial_base_port_release,
17304c7f60cSTony Lindgren 				      port->ctrl_id, port->port_id);
17484a9582fSTony Lindgren 	if (err)
17552861a3bSDan Carpenter 		goto err_put_device;
17684a9582fSTony Lindgren 
17784a9582fSTony Lindgren 	port_dev->port = port;
17884a9582fSTony Lindgren 
17984a9582fSTony Lindgren 	err = device_add(&port_dev->dev);
18084a9582fSTony Lindgren 	if (err)
18184a9582fSTony Lindgren 		goto err_put_device;
18284a9582fSTony Lindgren 
18384a9582fSTony Lindgren 	return port_dev;
18484a9582fSTony Lindgren 
18584a9582fSTony Lindgren err_put_device:
18684a9582fSTony Lindgren 	put_device(&port_dev->dev);
18704c7f60cSTony Lindgren 	ida_free(&ctrl_dev->port_ida, port->port_id);
18884a9582fSTony Lindgren 
18984a9582fSTony Lindgren 	return ERR_PTR(err);
19084a9582fSTony Lindgren }
19184a9582fSTony Lindgren 
serial_base_port_device_remove(struct serial_port_device * port_dev)19284a9582fSTony Lindgren void serial_base_port_device_remove(struct serial_port_device *port_dev)
19384a9582fSTony Lindgren {
19404c7f60cSTony Lindgren 	struct serial_ctrl_device *ctrl_dev;
19504c7f60cSTony Lindgren 	struct device *parent;
19604c7f60cSTony Lindgren 
19784a9582fSTony Lindgren 	if (!port_dev)
19884a9582fSTony Lindgren 		return;
19984a9582fSTony Lindgren 
20004c7f60cSTony Lindgren 	parent = port_dev->dev.parent;
20104c7f60cSTony Lindgren 	ctrl_dev = to_serial_base_ctrl_device(parent);
20204c7f60cSTony Lindgren 
20384a9582fSTony Lindgren 	device_del(&port_dev->dev);
20404c7f60cSTony Lindgren 	ida_free(&ctrl_dev->port_ida, port_dev->port->port_id);
2056be1a8d5STony Lindgren 	put_device(&port_dev->dev);
20684a9582fSTony Lindgren }
20784a9582fSTony Lindgren 
20812c91cecSTony Lindgren #ifdef CONFIG_SERIAL_CORE_CONSOLE
20912c91cecSTony Lindgren 
21012c91cecSTony Lindgren /**
21112c91cecSTony Lindgren  * serial_base_match_and_update_preferred_console - Match and update a preferred console
21212c91cecSTony Lindgren  * @drv: Serial port device driver
21312c91cecSTony Lindgren  * @port: Serial port instance
21412c91cecSTony Lindgren  *
21512c91cecSTony Lindgren  * Tries to match and update the preferred console for a serial port for
21612c91cecSTony Lindgren  * the kernel command line option console=DEVNAME:0.0.
21712c91cecSTony Lindgren  *
21812c91cecSTony Lindgren  * Cannot be called early for ISA ports, depends on struct device.
21912c91cecSTony Lindgren  *
22012c91cecSTony Lindgren  * Return: 0 on success, negative error code on failure.
22112c91cecSTony Lindgren  */
serial_base_match_and_update_preferred_console(struct uart_driver * drv,struct uart_port * port)22212c91cecSTony Lindgren int serial_base_match_and_update_preferred_console(struct uart_driver *drv,
22312c91cecSTony Lindgren 						   struct uart_port *port)
22412c91cecSTony Lindgren {
22512c91cecSTony Lindgren 	const char *port_match __free(kfree) = NULL;
22612c91cecSTony Lindgren 	int ret;
22712c91cecSTony Lindgren 
22812c91cecSTony Lindgren 	port_match = kasprintf(GFP_KERNEL, "%s:%d.%d", dev_name(port->dev),
22912c91cecSTony Lindgren 			       port->ctrl_id, port->port_id);
23012c91cecSTony Lindgren 	if (!port_match)
23112c91cecSTony Lindgren 		return -ENOMEM;
23212c91cecSTony Lindgren 
23312c91cecSTony Lindgren 	ret = match_devname_and_update_preferred_console(port_match,
23412c91cecSTony Lindgren 							 drv->dev_name,
23512c91cecSTony Lindgren 							 port->line);
23612c91cecSTony Lindgren 	if (ret == -ENOENT)
23712c91cecSTony Lindgren 		return 0;
23812c91cecSTony Lindgren 
23912c91cecSTony Lindgren 	return ret;
24012c91cecSTony Lindgren }
24112c91cecSTony Lindgren 
24212c91cecSTony Lindgren #endif
24312c91cecSTony Lindgren 
serial_base_init(void)24484a9582fSTony Lindgren static int serial_base_init(void)
24584a9582fSTony Lindgren {
24684a9582fSTony Lindgren 	int ret;
24784a9582fSTony Lindgren 
24884a9582fSTony Lindgren 	ret = bus_register(&serial_base_bus_type);
24984a9582fSTony Lindgren 	if (ret)
25084a9582fSTony Lindgren 		return ret;
25184a9582fSTony Lindgren 
25284a9582fSTony Lindgren 	ret = serial_base_ctrl_init();
25384a9582fSTony Lindgren 	if (ret)
25484a9582fSTony Lindgren 		goto err_bus_unregister;
25584a9582fSTony Lindgren 
25684a9582fSTony Lindgren 	ret = serial_base_port_init();
25784a9582fSTony Lindgren 	if (ret)
25884a9582fSTony Lindgren 		goto err_ctrl_exit;
25984a9582fSTony Lindgren 
26053991424STony Lindgren 	serial_base_initialized = true;
26153991424STony Lindgren 
26284a9582fSTony Lindgren 	return 0;
26384a9582fSTony Lindgren 
26484a9582fSTony Lindgren err_ctrl_exit:
26584a9582fSTony Lindgren 	serial_base_ctrl_exit();
26684a9582fSTony Lindgren 
26784a9582fSTony Lindgren err_bus_unregister:
26884a9582fSTony Lindgren 	bus_unregister(&serial_base_bus_type);
26984a9582fSTony Lindgren 
27084a9582fSTony Lindgren 	return ret;
27184a9582fSTony Lindgren }
27253991424STony Lindgren arch_initcall(serial_base_init);
27384a9582fSTony Lindgren 
serial_base_exit(void)27484a9582fSTony Lindgren static void serial_base_exit(void)
27584a9582fSTony Lindgren {
27684a9582fSTony Lindgren 	serial_base_port_exit();
27784a9582fSTony Lindgren 	serial_base_ctrl_exit();
27884a9582fSTony Lindgren 	bus_unregister(&serial_base_bus_type);
27984a9582fSTony Lindgren }
28084a9582fSTony Lindgren module_exit(serial_base_exit);
28184a9582fSTony Lindgren 
28284a9582fSTony Lindgren MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
28384a9582fSTony Lindgren MODULE_DESCRIPTION("Serial core bus");
28484a9582fSTony Lindgren MODULE_LICENSE("GPL");
285