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/module.h> 14 #include <linux/serial_core.h> 15 #include <linux/slab.h> 16 #include <linux/spinlock.h> 17 18 #include "serial_base.h" 19 20 static bool serial_base_initialized; 21 22 static int serial_base_match(struct device *dev, struct device_driver *drv) 23 { 24 int len = strlen(drv->name); 25 26 return !strncmp(dev_name(dev), drv->name, len); 27 } 28 29 static struct bus_type serial_base_bus_type = { 30 .name = "serial-base", 31 .match = serial_base_match, 32 }; 33 34 int serial_base_driver_register(struct device_driver *driver) 35 { 36 driver->bus = &serial_base_bus_type; 37 38 return driver_register(driver); 39 } 40 41 void serial_base_driver_unregister(struct device_driver *driver) 42 { 43 driver_unregister(driver); 44 } 45 46 static int serial_base_device_init(struct uart_port *port, 47 struct device *dev, 48 struct device *parent_dev, 49 const struct device_type *type, 50 void (*release)(struct device *dev), 51 int id) 52 { 53 device_initialize(dev); 54 dev->type = type; 55 dev->parent = parent_dev; 56 dev->bus = &serial_base_bus_type; 57 dev->release = release; 58 59 if (!serial_base_initialized) { 60 dev_dbg(port->dev, "uart_add_one_port() called before arch_initcall()?\n"); 61 return -EPROBE_DEFER; 62 } 63 64 return dev_set_name(dev, "%s.%s.%d", type->name, dev_name(port->dev), id); 65 } 66 67 static const struct device_type serial_ctrl_type = { 68 .name = "ctrl", 69 }; 70 71 static void serial_base_ctrl_release(struct device *dev) 72 { 73 struct serial_ctrl_device *ctrl_dev = to_serial_base_ctrl_device(dev); 74 75 kfree(ctrl_dev); 76 } 77 78 void serial_base_ctrl_device_remove(struct serial_ctrl_device *ctrl_dev) 79 { 80 if (!ctrl_dev) 81 return; 82 83 device_del(&ctrl_dev->dev); 84 } 85 86 struct serial_ctrl_device *serial_base_ctrl_add(struct uart_port *port, 87 struct device *parent) 88 { 89 struct serial_ctrl_device *ctrl_dev; 90 int err; 91 92 ctrl_dev = kzalloc(sizeof(*ctrl_dev), GFP_KERNEL); 93 if (!ctrl_dev) 94 return ERR_PTR(-ENOMEM); 95 96 err = serial_base_device_init(port, &ctrl_dev->dev, 97 parent, &serial_ctrl_type, 98 serial_base_ctrl_release, 99 port->ctrl_id); 100 if (err) 101 goto err_put_device; 102 103 err = device_add(&ctrl_dev->dev); 104 if (err) 105 goto err_put_device; 106 107 return ctrl_dev; 108 109 err_put_device: 110 put_device(&ctrl_dev->dev); 111 112 return ERR_PTR(err); 113 } 114 115 static const struct device_type serial_port_type = { 116 .name = "port", 117 }; 118 119 static void serial_base_port_release(struct device *dev) 120 { 121 struct serial_port_device *port_dev = to_serial_base_port_device(dev); 122 123 kfree(port_dev); 124 } 125 126 struct serial_port_device *serial_base_port_add(struct uart_port *port, 127 struct serial_ctrl_device *ctrl_dev) 128 { 129 struct serial_port_device *port_dev; 130 int err; 131 132 port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL); 133 if (!port_dev) 134 return ERR_PTR(-ENOMEM); 135 136 err = serial_base_device_init(port, &port_dev->dev, 137 &ctrl_dev->dev, &serial_port_type, 138 serial_base_port_release, 139 port->line); 140 if (err) 141 goto err_put_device; 142 143 port_dev->port = port; 144 145 err = device_add(&port_dev->dev); 146 if (err) 147 goto err_put_device; 148 149 return port_dev; 150 151 err_put_device: 152 put_device(&port_dev->dev); 153 154 return ERR_PTR(err); 155 } 156 157 void serial_base_port_device_remove(struct serial_port_device *port_dev) 158 { 159 if (!port_dev) 160 return; 161 162 device_del(&port_dev->dev); 163 } 164 165 static int serial_base_init(void) 166 { 167 int ret; 168 169 ret = bus_register(&serial_base_bus_type); 170 if (ret) 171 return ret; 172 173 ret = serial_base_ctrl_init(); 174 if (ret) 175 goto err_bus_unregister; 176 177 ret = serial_base_port_init(); 178 if (ret) 179 goto err_ctrl_exit; 180 181 serial_base_initialized = true; 182 183 return 0; 184 185 err_ctrl_exit: 186 serial_base_ctrl_exit(); 187 188 err_bus_unregister: 189 bus_unregister(&serial_base_bus_type); 190 191 return ret; 192 } 193 arch_initcall(serial_base_init); 194 195 static void serial_base_exit(void) 196 { 197 serial_base_port_exit(); 198 serial_base_ctrl_exit(); 199 bus_unregister(&serial_base_bus_type); 200 } 201 module_exit(serial_base_exit); 202 203 MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>"); 204 MODULE_DESCRIPTION("Serial core bus"); 205 MODULE_LICENSE("GPL"); 206