1 /* 2 * Copyright (C) 2016-2017 Linaro Ltd., Rob Herring <[email protected]> 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License version 2 and 6 * only version 2 as published by the Free Software Foundation. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 */ 13 #ifndef _LINUX_SERDEV_H 14 #define _LINUX_SERDEV_H 15 16 #include <linux/types.h> 17 #include <linux/device.h> 18 #include <linux/termios.h> 19 20 struct serdev_controller; 21 struct serdev_device; 22 23 /* 24 * serdev device structures 25 */ 26 27 /** 28 * struct serdev_device_ops - Callback operations for a serdev device 29 * @receive_buf: Function called with data received from device. 30 * @write_wakeup: Function called when ready to transmit more data. 31 */ 32 struct serdev_device_ops { 33 int (*receive_buf)(struct serdev_device *, const unsigned char *, size_t); 34 void (*write_wakeup)(struct serdev_device *); 35 }; 36 37 /** 38 * struct serdev_device - Basic representation of an serdev device 39 * @dev: Driver model representation of the device. 40 * @nr: Device number on serdev bus. 41 * @ctrl: serdev controller managing this device. 42 * @ops: Device operations. 43 */ 44 struct serdev_device { 45 struct device dev; 46 int nr; 47 struct serdev_controller *ctrl; 48 const struct serdev_device_ops *ops; 49 }; 50 51 static inline struct serdev_device *to_serdev_device(struct device *d) 52 { 53 return container_of(d, struct serdev_device, dev); 54 } 55 56 /** 57 * struct serdev_device_driver - serdev slave device driver 58 * @driver: serdev device drivers should initialize name field of this 59 * structure. 60 * @probe: binds this driver to a serdev device. 61 * @remove: unbinds this driver from the serdev device. 62 */ 63 struct serdev_device_driver { 64 struct device_driver driver; 65 int (*probe)(struct serdev_device *); 66 void (*remove)(struct serdev_device *); 67 }; 68 69 static inline struct serdev_device_driver *to_serdev_device_driver(struct device_driver *d) 70 { 71 return container_of(d, struct serdev_device_driver, driver); 72 } 73 74 /* 75 * serdev controller structures 76 */ 77 struct serdev_controller_ops { 78 int (*write_buf)(struct serdev_controller *, const unsigned char *, size_t); 79 void (*write_flush)(struct serdev_controller *); 80 int (*write_room)(struct serdev_controller *); 81 int (*open)(struct serdev_controller *); 82 void (*close)(struct serdev_controller *); 83 void (*set_flow_control)(struct serdev_controller *, bool); 84 unsigned int (*set_baudrate)(struct serdev_controller *, unsigned int); 85 void (*wait_until_sent)(struct serdev_controller *, long); 86 int (*get_tiocm)(struct serdev_controller *); 87 int (*set_tiocm)(struct serdev_controller *, unsigned int, unsigned int); 88 }; 89 90 /** 91 * struct serdev_controller - interface to the serdev controller 92 * @dev: Driver model representation of the device. 93 * @nr: number identifier for this controller/bus. 94 * @serdev: Pointer to slave device for this controller. 95 * @ops: Controller operations. 96 */ 97 struct serdev_controller { 98 struct device dev; 99 unsigned int nr; 100 struct serdev_device *serdev; 101 const struct serdev_controller_ops *ops; 102 }; 103 104 static inline struct serdev_controller *to_serdev_controller(struct device *d) 105 { 106 return container_of(d, struct serdev_controller, dev); 107 } 108 109 static inline void *serdev_device_get_drvdata(const struct serdev_device *serdev) 110 { 111 return dev_get_drvdata(&serdev->dev); 112 } 113 114 static inline void serdev_device_set_drvdata(struct serdev_device *serdev, void *data) 115 { 116 dev_set_drvdata(&serdev->dev, data); 117 } 118 119 /** 120 * serdev_device_put() - decrement serdev device refcount 121 * @serdev serdev device. 122 */ 123 static inline void serdev_device_put(struct serdev_device *serdev) 124 { 125 if (serdev) 126 put_device(&serdev->dev); 127 } 128 129 static inline void serdev_device_set_client_ops(struct serdev_device *serdev, 130 const struct serdev_device_ops *ops) 131 { 132 serdev->ops = ops; 133 } 134 135 static inline 136 void *serdev_controller_get_drvdata(const struct serdev_controller *ctrl) 137 { 138 return ctrl ? dev_get_drvdata(&ctrl->dev) : NULL; 139 } 140 141 static inline void serdev_controller_set_drvdata(struct serdev_controller *ctrl, 142 void *data) 143 { 144 dev_set_drvdata(&ctrl->dev, data); 145 } 146 147 /** 148 * serdev_controller_put() - decrement controller refcount 149 * @ctrl serdev controller. 150 */ 151 static inline void serdev_controller_put(struct serdev_controller *ctrl) 152 { 153 if (ctrl) 154 put_device(&ctrl->dev); 155 } 156 157 struct serdev_device *serdev_device_alloc(struct serdev_controller *); 158 int serdev_device_add(struct serdev_device *); 159 void serdev_device_remove(struct serdev_device *); 160 161 struct serdev_controller *serdev_controller_alloc(struct device *, size_t); 162 int serdev_controller_add(struct serdev_controller *); 163 void serdev_controller_remove(struct serdev_controller *); 164 165 static inline void serdev_controller_write_wakeup(struct serdev_controller *ctrl) 166 { 167 struct serdev_device *serdev = ctrl->serdev; 168 169 if (!serdev || !serdev->ops->write_wakeup) 170 return; 171 172 serdev->ops->write_wakeup(ctrl->serdev); 173 } 174 175 static inline int serdev_controller_receive_buf(struct serdev_controller *ctrl, 176 const unsigned char *data, 177 size_t count) 178 { 179 struct serdev_device *serdev = ctrl->serdev; 180 181 if (!serdev || !serdev->ops->receive_buf) 182 return -EINVAL; 183 184 return serdev->ops->receive_buf(ctrl->serdev, data, count); 185 } 186 187 #if IS_ENABLED(CONFIG_SERIAL_DEV_BUS) 188 189 int serdev_device_open(struct serdev_device *); 190 void serdev_device_close(struct serdev_device *); 191 unsigned int serdev_device_set_baudrate(struct serdev_device *, unsigned int); 192 void serdev_device_set_flow_control(struct serdev_device *, bool); 193 void serdev_device_wait_until_sent(struct serdev_device *, long); 194 int serdev_device_get_tiocm(struct serdev_device *); 195 int serdev_device_set_tiocm(struct serdev_device *, int, int); 196 int serdev_device_write_buf(struct serdev_device *, const unsigned char *, size_t); 197 void serdev_device_write_flush(struct serdev_device *); 198 int serdev_device_write_room(struct serdev_device *); 199 200 /* 201 * serdev device driver functions 202 */ 203 int __serdev_device_driver_register(struct serdev_device_driver *, struct module *); 204 #define serdev_device_driver_register(sdrv) \ 205 __serdev_device_driver_register(sdrv, THIS_MODULE) 206 207 /** 208 * serdev_device_driver_unregister() - unregister an serdev client driver 209 * @sdrv: the driver to unregister 210 */ 211 static inline void serdev_device_driver_unregister(struct serdev_device_driver *sdrv) 212 { 213 if (sdrv) 214 driver_unregister(&sdrv->driver); 215 } 216 217 #define module_serdev_device_driver(__serdev_device_driver) \ 218 module_driver(__serdev_device_driver, serdev_device_driver_register, \ 219 serdev_device_driver_unregister) 220 221 #else 222 223 static inline int serdev_device_open(struct serdev_device *sdev) 224 { 225 return -ENODEV; 226 } 227 static inline void serdev_device_close(struct serdev_device *sdev) {} 228 static inline unsigned int serdev_device_set_baudrate(struct serdev_device *sdev, unsigned int baudrate) 229 { 230 return 0; 231 } 232 static inline void serdev_device_set_flow_control(struct serdev_device *sdev, bool enable) {} 233 static inline void serdev_device_wait_until_sent(struct serdev_device *sdev, long timeout) {} 234 static inline int serdev_device_get_tiocm(struct serdev_device *serdev) 235 { 236 return -ENOTSUPP; 237 } 238 static inline int serdev_device_set_tiocm(struct serdev_device *serdev, int set, int clear) 239 { 240 return -ENOTSUPP; 241 } 242 static inline int serdev_device_write_buf(struct serdev_device *sdev, const unsigned char *buf, size_t count) 243 { 244 return -ENODEV; 245 } 246 static inline void serdev_device_write_flush(struct serdev_device *sdev) {} 247 static inline int serdev_device_write_room(struct serdev_device *sdev) 248 { 249 return 0; 250 } 251 252 #define serdev_device_driver_register(x) 253 #define serdev_device_driver_unregister(x) 254 255 #endif /* CONFIG_SERIAL_DEV_BUS */ 256 257 /* 258 * serdev hooks into TTY core 259 */ 260 struct tty_port; 261 struct tty_driver; 262 263 #ifdef CONFIG_SERIAL_DEV_CTRL_TTYPORT 264 struct device *serdev_tty_port_register(struct tty_port *port, 265 struct device *parent, 266 struct tty_driver *drv, int idx); 267 void serdev_tty_port_unregister(struct tty_port *port); 268 #else 269 static inline struct device *serdev_tty_port_register(struct tty_port *port, 270 struct device *parent, 271 struct tty_driver *drv, int idx) 272 { 273 return ERR_PTR(-ENODEV); 274 } 275 static inline void serdev_tty_port_unregister(struct tty_port *port) {} 276 #endif /* CONFIG_SERIAL_DEV_CTRL_TTYPORT */ 277 278 #endif /*_LINUX_SERDEV_H */ 279