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/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> 19 20 #include "serial_base.h" 21 22 static bool serial_base_initialized; 23 24 static const struct device_type serial_ctrl_type = { 25 .name = "ctrl", 26 }; 27 28 static const struct device_type serial_port_type = { 29 .name = "port", 30 }; 31 32 static int serial_base_match(struct device *dev, struct device_driver *drv) 33 { 34 if (dev->type == &serial_ctrl_type && 35 str_has_prefix(drv->name, serial_ctrl_type.name)) 36 return 1; 37 38 if (dev->type == &serial_port_type && 39 str_has_prefix(drv->name, serial_port_type.name)) 40 return 1; 41 42 return 0; 43 } 44 45 static const struct bus_type serial_base_bus_type = { 46 .name = "serial-base", 47 .match = serial_base_match, 48 }; 49 50 int serial_base_driver_register(struct device_driver *driver) 51 { 52 driver->bus = &serial_base_bus_type; 53 54 return driver_register(driver); 55 } 56 57 void serial_base_driver_unregister(struct device_driver *driver) 58 { 59 driver_unregister(driver); 60 } 61 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) 69 { 70 device_initialize(dev); 71 dev->type = type; 72 dev->parent = parent_dev; 73 dev->bus = &serial_base_bus_type; 74 dev->release = release; 75 76 if (!serial_base_initialized) { 77 dev_dbg(port->dev, "uart_add_one_port() called before arch_initcall()?\n"); 78 return -EPROBE_DEFER; 79 } 80 81 if (type == &serial_ctrl_type) 82 return dev_set_name(dev, "%s:%d", dev_name(port->dev), ctrl_id); 83 84 if (type == &serial_port_type) 85 return dev_set_name(dev, "%s:%d.%d", dev_name(port->dev), 86 ctrl_id, port_id); 87 88 return -EINVAL; 89 } 90 91 static void serial_base_ctrl_release(struct device *dev) 92 { 93 struct serial_ctrl_device *ctrl_dev = to_serial_base_ctrl_device(dev); 94 95 kfree(ctrl_dev); 96 } 97 98 void serial_base_ctrl_device_remove(struct serial_ctrl_device *ctrl_dev) 99 { 100 if (!ctrl_dev) 101 return; 102 103 device_del(&ctrl_dev->dev); 104 put_device(&ctrl_dev->dev); 105 } 106 107 struct serial_ctrl_device *serial_base_ctrl_add(struct uart_port *port, 108 struct device *parent) 109 { 110 struct serial_ctrl_device *ctrl_dev; 111 int err; 112 113 ctrl_dev = kzalloc(sizeof(*ctrl_dev), GFP_KERNEL); 114 if (!ctrl_dev) 115 return ERR_PTR(-ENOMEM); 116 117 ida_init(&ctrl_dev->port_ida); 118 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; 125 126 err = device_add(&ctrl_dev->dev); 127 if (err) 128 goto err_put_device; 129 130 return ctrl_dev; 131 132 err_put_device: 133 put_device(&ctrl_dev->dev); 134 135 return ERR_PTR(err); 136 } 137 138 static void serial_base_port_release(struct device *dev) 139 { 140 struct serial_port_device *port_dev = to_serial_base_port_device(dev); 141 142 kfree(port_dev); 143 } 144 145 struct serial_port_device *serial_base_port_add(struct uart_port *port, 146 struct serial_ctrl_device *ctrl_dev) 147 { 148 struct serial_port_device *port_dev; 149 int min = 0, max = -1; /* Use -1 for max to apply IDA defaults */ 150 int err; 151 152 port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL); 153 if (!port_dev) 154 return ERR_PTR(-ENOMEM); 155 156 /* Device driver specified port_id vs automatic assignment? */ 157 if (port->port_id) { 158 min = port->port_id; 159 max = port->port_id; 160 } 161 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); 166 } 167 168 port->port_id = err; 169 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; 176 177 port_dev->port = port; 178 179 err = device_add(&port_dev->dev); 180 if (err) 181 goto err_put_device; 182 183 return port_dev; 184 185 err_put_device: 186 put_device(&port_dev->dev); 187 ida_free(&ctrl_dev->port_ida, port->port_id); 188 189 return ERR_PTR(err); 190 } 191 192 void serial_base_port_device_remove(struct serial_port_device *port_dev) 193 { 194 struct serial_ctrl_device *ctrl_dev; 195 struct device *parent; 196 197 if (!port_dev) 198 return; 199 200 parent = port_dev->dev.parent; 201 ctrl_dev = to_serial_base_ctrl_device(parent); 202 203 device_del(&port_dev->dev); 204 ida_free(&ctrl_dev->port_ida, port_dev->port->port_id); 205 put_device(&port_dev->dev); 206 } 207 208 #ifdef CONFIG_SERIAL_CORE_CONSOLE 209 210 static int serial_base_add_one_prefcon(const char *match, const char *dev_name, 211 int port_id) 212 { 213 int ret; 214 215 ret = add_preferred_console_match(match, dev_name, port_id); 216 if (ret == -ENOENT) 217 return 0; 218 219 return ret; 220 } 221 222 #ifdef __sparc__ 223 224 /* Handle Sparc ttya and ttyb options as done in console_setup() */ 225 static int serial_base_add_sparc_console(const char *dev_name, int idx) 226 { 227 const char *name; 228 229 switch (idx) { 230 case 0: 231 name = "ttya"; 232 break; 233 case 1: 234 name = "ttyb"; 235 break; 236 default: 237 return 0; 238 } 239 240 return serial_base_add_one_prefcon(name, dev_name, idx); 241 } 242 243 #else 244 245 static inline int serial_base_add_sparc_console(const char *dev_name, int idx) 246 { 247 return 0; 248 } 249 250 #endif 251 252 static int serial_base_add_prefcon(const char *name, int idx) 253 { 254 const char *char_match __free(kfree) = NULL; 255 const char *nmbr_match __free(kfree) = NULL; 256 int ret; 257 258 /* Handle ttyS specific options */ 259 if (strstarts(name, "ttyS")) { 260 /* No name, just a number */ 261 nmbr_match = kasprintf(GFP_KERNEL, "%i", idx); 262 if (!nmbr_match) 263 return -ENODEV; 264 265 ret = serial_base_add_one_prefcon(nmbr_match, name, idx); 266 if (ret) 267 return ret; 268 269 /* Sparc ttya and ttyb */ 270 ret = serial_base_add_sparc_console(name, idx); 271 if (ret) 272 return ret; 273 } 274 275 /* Handle the traditional character device name style console=ttyS0 */ 276 char_match = kasprintf(GFP_KERNEL, "%s%i", name, idx); 277 if (!char_match) 278 return -ENOMEM; 279 280 return serial_base_add_one_prefcon(char_match, name, idx); 281 } 282 283 /** 284 * serial_base_add_preferred_console - Adds a preferred console 285 * @drv: Serial port device driver 286 * @port: Serial port instance 287 * 288 * Tries to add a preferred console for a serial port if specified in the 289 * kernel command line. Supports both the traditional character device such 290 * as console=ttyS0, and a hardware addressing based console=DEVNAME:0.0 291 * style name. 292 * 293 * Translates the kernel command line option using a hardware based addressing 294 * console=DEVNAME:0.0 to the serial port character device such as ttyS0. 295 * Cannot be called early for ISA ports, depends on struct device. 296 * 297 * Note that duplicates are ignored by add_preferred_console(). 298 * 299 * Return: 0 on success, negative error code on failure. 300 */ 301 int serial_base_add_preferred_console(struct uart_driver *drv, 302 struct uart_port *port) 303 { 304 const char *port_match __free(kfree) = NULL; 305 int ret; 306 307 ret = serial_base_add_prefcon(drv->dev_name, port->line); 308 if (ret) 309 return ret; 310 311 port_match = kasprintf(GFP_KERNEL, "%s:%i.%i", dev_name(port->dev), 312 port->ctrl_id, port->port_id); 313 if (!port_match) 314 return -ENOMEM; 315 316 /* Translate a hardware addressing style console=DEVNAME:0.0 */ 317 return serial_base_add_one_prefcon(port_match, drv->dev_name, port->line); 318 } 319 320 #endif 321 322 #ifdef CONFIG_SERIAL_8250_CONSOLE 323 324 /* 325 * Early ISA ports initialize the console before there is no struct device. 326 * This should be only called from serial8250_isa_init_preferred_console(), 327 * other callers are likely wrong and should rely on earlycon instead. 328 */ 329 int serial_base_add_isa_preferred_console(const char *name, int idx) 330 { 331 return serial_base_add_prefcon(name, idx); 332 } 333 334 #endif 335 336 static int serial_base_init(void) 337 { 338 int ret; 339 340 ret = bus_register(&serial_base_bus_type); 341 if (ret) 342 return ret; 343 344 ret = serial_base_ctrl_init(); 345 if (ret) 346 goto err_bus_unregister; 347 348 ret = serial_base_port_init(); 349 if (ret) 350 goto err_ctrl_exit; 351 352 serial_base_initialized = true; 353 354 return 0; 355 356 err_ctrl_exit: 357 serial_base_ctrl_exit(); 358 359 err_bus_unregister: 360 bus_unregister(&serial_base_bus_type); 361 362 return ret; 363 } 364 arch_initcall(serial_base_init); 365 366 static void serial_base_exit(void) 367 { 368 serial_base_port_exit(); 369 serial_base_ctrl_exit(); 370 bus_unregister(&serial_base_bus_type); 371 } 372 module_exit(serial_base_exit); 373 374 MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>"); 375 MODULE_DESCRIPTION("Serial core bus"); 376 MODULE_LICENSE("GPL"); 377