1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Serial base bus layer for controllers 4 * 5 * Copyright (C) 2023 Texas Instruments Incorporated - https://www.ti.com/ 6 * Author: Tony Lindgren <tony@atomide.com> 7 * 8 * The serial core bus manages the serial core controller instances. 9 */ 10 11 #include <linux/container_of.h> 12 #include <linux/device.h> 13 #include <linux/idr.h> 14 #include <linux/module.h> 15 #include <linux/serial_core.h> 16 #include <linux/slab.h> 17 #include <linux/spinlock.h> 18 19 #include "serial_base.h" 20 21 static bool serial_base_initialized; 22 23 static const struct device_type serial_ctrl_type = { 24 .name = "ctrl", 25 }; 26 27 static const struct device_type serial_port_type = { 28 .name = "port", 29 }; 30 31 static int serial_base_match(struct device *dev, struct device_driver *drv) 32 { 33 if (dev->type == &serial_ctrl_type && 34 str_has_prefix(drv->name, serial_ctrl_type.name)) 35 return 1; 36 37 if (dev->type == &serial_port_type && 38 str_has_prefix(drv->name, serial_port_type.name)) 39 return 1; 40 41 return 0; 42 } 43 44 static const struct bus_type serial_base_bus_type = { 45 .name = "serial-base", 46 .match = serial_base_match, 47 }; 48 49 int serial_base_driver_register(struct device_driver *driver) 50 { 51 driver->bus = &serial_base_bus_type; 52 53 return driver_register(driver); 54 } 55 56 void serial_base_driver_unregister(struct device_driver *driver) 57 { 58 driver_unregister(driver); 59 } 60 61 static int serial_base_device_init(struct uart_port *port, 62 struct device *dev, 63 struct device *parent_dev, 64 const struct device_type *type, 65 void (*release)(struct device *dev), 66 unsigned int ctrl_id, 67 unsigned int port_id) 68 { 69 device_initialize(dev); 70 dev->type = type; 71 dev->parent = parent_dev; 72 dev->bus = &serial_base_bus_type; 73 dev->release = release; 74 75 if (!serial_base_initialized) { 76 dev_dbg(port->dev, "uart_add_one_port() called before arch_initcall()?\n"); 77 return -EPROBE_DEFER; 78 } 79 80 if (type == &serial_ctrl_type) 81 return dev_set_name(dev, "%s:%d", dev_name(port->dev), ctrl_id); 82 83 if (type == &serial_port_type) 84 return dev_set_name(dev, "%s:%d.%d", dev_name(port->dev), 85 ctrl_id, port_id); 86 87 return -EINVAL; 88 } 89 90 static void serial_base_ctrl_release(struct device *dev) 91 { 92 struct serial_ctrl_device *ctrl_dev = to_serial_base_ctrl_device(dev); 93 94 kfree(ctrl_dev); 95 } 96 97 void serial_base_ctrl_device_remove(struct serial_ctrl_device *ctrl_dev) 98 { 99 if (!ctrl_dev) 100 return; 101 102 device_del(&ctrl_dev->dev); 103 put_device(&ctrl_dev->dev); 104 } 105 106 struct serial_ctrl_device *serial_base_ctrl_add(struct uart_port *port, 107 struct device *parent) 108 { 109 struct serial_ctrl_device *ctrl_dev; 110 int err; 111 112 ctrl_dev = kzalloc(sizeof(*ctrl_dev), GFP_KERNEL); 113 if (!ctrl_dev) 114 return ERR_PTR(-ENOMEM); 115 116 ida_init(&ctrl_dev->port_ida); 117 118 err = serial_base_device_init(port, &ctrl_dev->dev, 119 parent, &serial_ctrl_type, 120 serial_base_ctrl_release, 121 port->ctrl_id, 0); 122 if (err) 123 goto err_put_device; 124 125 err = device_add(&ctrl_dev->dev); 126 if (err) 127 goto err_put_device; 128 129 return ctrl_dev; 130 131 err_put_device: 132 put_device(&ctrl_dev->dev); 133 134 return ERR_PTR(err); 135 } 136 137 static void serial_base_port_release(struct device *dev) 138 { 139 struct serial_port_device *port_dev = to_serial_base_port_device(dev); 140 141 kfree(port_dev); 142 } 143 144 struct serial_port_device *serial_base_port_add(struct uart_port *port, 145 struct serial_ctrl_device *ctrl_dev) 146 { 147 struct serial_port_device *port_dev; 148 int min = 0, max = -1; /* Use -1 for max to apply IDA defaults */ 149 int err; 150 151 port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL); 152 if (!port_dev) 153 return ERR_PTR(-ENOMEM); 154 155 /* Device driver specified port_id vs automatic assignment? */ 156 if (port->port_id) { 157 min = port->port_id; 158 max = port->port_id; 159 } 160 161 err = ida_alloc_range(&ctrl_dev->port_ida, min, max, GFP_KERNEL); 162 if (err < 0) { 163 kfree(port_dev); 164 return ERR_PTR(err); 165 } 166 167 port->port_id = err; 168 169 err = serial_base_device_init(port, &port_dev->dev, 170 &ctrl_dev->dev, &serial_port_type, 171 serial_base_port_release, 172 port->ctrl_id, port->port_id); 173 if (err) 174 goto err_put_device; 175 176 port_dev->port = port; 177 178 err = device_add(&port_dev->dev); 179 if (err) 180 goto err_put_device; 181 182 return port_dev; 183 184 err_put_device: 185 put_device(&port_dev->dev); 186 ida_free(&ctrl_dev->port_ida, port->port_id); 187 188 return ERR_PTR(err); 189 } 190 191 void serial_base_port_device_remove(struct serial_port_device *port_dev) 192 { 193 struct serial_ctrl_device *ctrl_dev; 194 struct device *parent; 195 196 if (!port_dev) 197 return; 198 199 parent = port_dev->dev.parent; 200 ctrl_dev = to_serial_base_ctrl_device(parent); 201 202 device_del(&port_dev->dev); 203 ida_free(&ctrl_dev->port_ida, port_dev->port->port_id); 204 put_device(&port_dev->dev); 205 } 206 207 static int serial_base_init(void) 208 { 209 int ret; 210 211 ret = bus_register(&serial_base_bus_type); 212 if (ret) 213 return ret; 214 215 ret = serial_base_ctrl_init(); 216 if (ret) 217 goto err_bus_unregister; 218 219 ret = serial_base_port_init(); 220 if (ret) 221 goto err_ctrl_exit; 222 223 serial_base_initialized = true; 224 225 return 0; 226 227 err_ctrl_exit: 228 serial_base_ctrl_exit(); 229 230 err_bus_unregister: 231 bus_unregister(&serial_base_bus_type); 232 233 return ret; 234 } 235 arch_initcall(serial_base_init); 236 237 static void serial_base_exit(void) 238 { 239 serial_base_port_exit(); 240 serial_base_ctrl_exit(); 241 bus_unregister(&serial_base_bus_type); 242 } 243 module_exit(serial_base_exit); 244 245 MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>"); 246 MODULE_DESCRIPTION("Serial core bus"); 247 MODULE_LICENSE("GPL"); 248