1 /* SPDX-License-Identifier: GPL-2.0-only */ << 2 /* 1 /* 3 * Copyright (C) 2016-2017 Linaro Ltd., Rob He 2 * Copyright (C) 2016-2017 Linaro Ltd., Rob Herring <robh@kernel.org> >> 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. 4 */ 12 */ 5 #ifndef _LINUX_SERDEV_H 13 #ifndef _LINUX_SERDEV_H 6 #define _LINUX_SERDEV_H 14 #define _LINUX_SERDEV_H 7 15 8 #include <linux/types.h> 16 #include <linux/types.h> 9 #include <linux/device.h> 17 #include <linux/device.h> 10 #include <linux/iopoll.h> << 11 #include <linux/uaccess.h> << 12 #include <linux/termios.h> 18 #include <linux/termios.h> 13 #include <linux/delay.h> 19 #include <linux/delay.h> 14 20 15 struct serdev_controller; 21 struct serdev_controller; 16 struct serdev_device; 22 struct serdev_device; 17 23 18 /* 24 /* 19 * serdev device structures 25 * serdev device structures 20 */ 26 */ 21 27 22 /** 28 /** 23 * struct serdev_device_ops - Callback operati 29 * struct serdev_device_ops - Callback operations for a serdev device 24 * @receive_buf: Function called with d 30 * @receive_buf: Function called with data received from device; 25 * returns number of byte 31 * returns number of bytes accepted; may sleep. 26 * @write_wakeup: Function called when r 32 * @write_wakeup: Function called when ready to transmit more data; must 27 * not sleep. 33 * not sleep. 28 */ 34 */ 29 struct serdev_device_ops { 35 struct serdev_device_ops { 30 size_t (*receive_buf)(struct serdev_de !! 36 int (*receive_buf)(struct serdev_device *, const unsigned char *, size_t); 31 void (*write_wakeup)(struct serdev_dev 37 void (*write_wakeup)(struct serdev_device *); 32 }; 38 }; 33 39 34 /** 40 /** 35 * struct serdev_device - Basic representation 41 * struct serdev_device - Basic representation of an serdev device 36 * @dev: Driver model representation of 42 * @dev: Driver model representation of the device. 37 * @nr: Device number on serdev bus. 43 * @nr: Device number on serdev bus. 38 * @ctrl: serdev controller managing thi 44 * @ctrl: serdev controller managing this device. 39 * @ops: Device operations. 45 * @ops: Device operations. 40 * @write_comp Completion used by serdev_devi 46 * @write_comp Completion used by serdev_device_write() internally 41 * @write_lock Lock to serialize access when 47 * @write_lock Lock to serialize access when writing data 42 */ 48 */ 43 struct serdev_device { 49 struct serdev_device { 44 struct device dev; 50 struct device dev; 45 int nr; 51 int nr; 46 struct serdev_controller *ctrl; 52 struct serdev_controller *ctrl; 47 const struct serdev_device_ops *ops; 53 const struct serdev_device_ops *ops; 48 struct completion write_comp; 54 struct completion write_comp; 49 struct mutex write_lock; 55 struct mutex write_lock; 50 }; 56 }; 51 57 52 static inline struct serdev_device *to_serdev_ 58 static inline struct serdev_device *to_serdev_device(struct device *d) 53 { 59 { 54 return container_of(d, struct serdev_d 60 return container_of(d, struct serdev_device, dev); 55 } 61 } 56 62 57 /** 63 /** 58 * struct serdev_device_driver - serdev slave 64 * struct serdev_device_driver - serdev slave device driver 59 * @driver: serdev device drivers should i 65 * @driver: serdev device drivers should initialize name field of this 60 * structure. 66 * structure. 61 * @probe: binds this driver to a serdev 67 * @probe: binds this driver to a serdev device. 62 * @remove: unbinds this driver from the s 68 * @remove: unbinds this driver from the serdev device. 63 */ 69 */ 64 struct serdev_device_driver { 70 struct serdev_device_driver { 65 struct device_driver driver; 71 struct device_driver driver; 66 int (*probe)(struct serdev_device 72 int (*probe)(struct serdev_device *); 67 void (*remove)(struct serdev_device 73 void (*remove)(struct serdev_device *); 68 }; 74 }; 69 75 70 static inline struct serdev_device_driver *to_ 76 static inline struct serdev_device_driver *to_serdev_device_driver(struct device_driver *d) 71 { 77 { 72 return container_of(d, struct serdev_d 78 return container_of(d, struct serdev_device_driver, driver); 73 } 79 } 74 80 75 enum serdev_parity { 81 enum serdev_parity { 76 SERDEV_PARITY_NONE, 82 SERDEV_PARITY_NONE, 77 SERDEV_PARITY_EVEN, 83 SERDEV_PARITY_EVEN, 78 SERDEV_PARITY_ODD, 84 SERDEV_PARITY_ODD, 79 }; 85 }; 80 86 81 /* 87 /* 82 * serdev controller structures 88 * serdev controller structures 83 */ 89 */ 84 struct serdev_controller_ops { 90 struct serdev_controller_ops { 85 ssize_t (*write_buf)(struct serdev_con !! 91 int (*write_buf)(struct serdev_controller *, const unsigned char *, size_t); 86 void (*write_flush)(struct serdev_cont 92 void (*write_flush)(struct serdev_controller *); 87 int (*write_room)(struct serdev_contro 93 int (*write_room)(struct serdev_controller *); 88 int (*open)(struct serdev_controller * 94 int (*open)(struct serdev_controller *); 89 void (*close)(struct serdev_controller 95 void (*close)(struct serdev_controller *); 90 void (*set_flow_control)(struct serdev 96 void (*set_flow_control)(struct serdev_controller *, bool); 91 int (*set_parity)(struct serdev_contro 97 int (*set_parity)(struct serdev_controller *, enum serdev_parity); 92 unsigned int (*set_baudrate)(struct se 98 unsigned int (*set_baudrate)(struct serdev_controller *, unsigned int); 93 void (*wait_until_sent)(struct serdev_ 99 void (*wait_until_sent)(struct serdev_controller *, long); 94 int (*get_tiocm)(struct serdev_control 100 int (*get_tiocm)(struct serdev_controller *); 95 int (*set_tiocm)(struct serdev_control 101 int (*set_tiocm)(struct serdev_controller *, unsigned int, unsigned int); 96 int (*break_ctl)(struct serdev_control << 97 }; 102 }; 98 103 99 /** 104 /** 100 * struct serdev_controller - interface to the 105 * struct serdev_controller - interface to the serdev controller 101 * @dev: Driver model representation of 106 * @dev: Driver model representation of the device. 102 * @host: Serial port hardware controlle << 103 * @nr: number identifier for this con 107 * @nr: number identifier for this controller/bus. 104 * @serdev: Pointer to slave device for th 108 * @serdev: Pointer to slave device for this controller. 105 * @ops: Controller operations. 109 * @ops: Controller operations. 106 */ 110 */ 107 struct serdev_controller { 111 struct serdev_controller { 108 struct device dev; 112 struct device dev; 109 struct device *host; << 110 unsigned int nr; 113 unsigned int nr; 111 struct serdev_device *serdev; 114 struct serdev_device *serdev; 112 const struct serdev_controller_ops *op 115 const struct serdev_controller_ops *ops; 113 }; 116 }; 114 117 115 static inline struct serdev_controller *to_ser 118 static inline struct serdev_controller *to_serdev_controller(struct device *d) 116 { 119 { 117 return container_of(d, struct serdev_c 120 return container_of(d, struct serdev_controller, dev); 118 } 121 } 119 122 120 static inline void *serdev_device_get_drvdata( 123 static inline void *serdev_device_get_drvdata(const struct serdev_device *serdev) 121 { 124 { 122 return dev_get_drvdata(&serdev->dev); 125 return dev_get_drvdata(&serdev->dev); 123 } 126 } 124 127 125 static inline void serdev_device_set_drvdata(s 128 static inline void serdev_device_set_drvdata(struct serdev_device *serdev, void *data) 126 { 129 { 127 dev_set_drvdata(&serdev->dev, data); 130 dev_set_drvdata(&serdev->dev, data); 128 } 131 } 129 132 130 /** 133 /** 131 * serdev_device_put() - decrement serdev devi 134 * serdev_device_put() - decrement serdev device refcount 132 * @serdev serdev device. 135 * @serdev serdev device. 133 */ 136 */ 134 static inline void serdev_device_put(struct se 137 static inline void serdev_device_put(struct serdev_device *serdev) 135 { 138 { 136 if (serdev) 139 if (serdev) 137 put_device(&serdev->dev); 140 put_device(&serdev->dev); 138 } 141 } 139 142 140 static inline void serdev_device_set_client_op 143 static inline void serdev_device_set_client_ops(struct serdev_device *serdev, 141 144 const struct serdev_device_ops *ops) 142 { 145 { 143 serdev->ops = ops; 146 serdev->ops = ops; 144 } 147 } 145 148 146 static inline 149 static inline 147 void *serdev_controller_get_drvdata(const stru 150 void *serdev_controller_get_drvdata(const struct serdev_controller *ctrl) 148 { 151 { 149 return ctrl ? dev_get_drvdata(&ctrl->d 152 return ctrl ? dev_get_drvdata(&ctrl->dev) : NULL; 150 } 153 } 151 154 152 static inline void serdev_controller_set_drvda 155 static inline void serdev_controller_set_drvdata(struct serdev_controller *ctrl, 153 156 void *data) 154 { 157 { 155 dev_set_drvdata(&ctrl->dev, data); 158 dev_set_drvdata(&ctrl->dev, data); 156 } 159 } 157 160 158 /** 161 /** 159 * serdev_controller_put() - decrement control 162 * serdev_controller_put() - decrement controller refcount 160 * @ctrl serdev controller. 163 * @ctrl serdev controller. 161 */ 164 */ 162 static inline void serdev_controller_put(struc 165 static inline void serdev_controller_put(struct serdev_controller *ctrl) 163 { 166 { 164 if (ctrl) 167 if (ctrl) 165 put_device(&ctrl->dev); 168 put_device(&ctrl->dev); 166 } 169 } 167 170 168 struct serdev_device *serdev_device_alloc(stru 171 struct serdev_device *serdev_device_alloc(struct serdev_controller *); 169 int serdev_device_add(struct serdev_device *); 172 int serdev_device_add(struct serdev_device *); 170 void serdev_device_remove(struct serdev_device 173 void serdev_device_remove(struct serdev_device *); 171 174 172 struct serdev_controller *serdev_controller_al !! 175 struct serdev_controller *serdev_controller_alloc(struct device *, size_t); 173 << 174 << 175 int serdev_controller_add(struct serdev_contro 176 int serdev_controller_add(struct serdev_controller *); 176 void serdev_controller_remove(struct serdev_co 177 void serdev_controller_remove(struct serdev_controller *); 177 178 178 static inline void serdev_controller_write_wak 179 static inline void serdev_controller_write_wakeup(struct serdev_controller *ctrl) 179 { 180 { 180 struct serdev_device *serdev = ctrl->s 181 struct serdev_device *serdev = ctrl->serdev; 181 182 182 if (!serdev || !serdev->ops->write_wak 183 if (!serdev || !serdev->ops->write_wakeup) 183 return; 184 return; 184 185 185 serdev->ops->write_wakeup(serdev); 186 serdev->ops->write_wakeup(serdev); 186 } 187 } 187 188 188 static inline size_t serdev_controller_receive !! 189 static inline int serdev_controller_receive_buf(struct serdev_controller *ctrl, 189 !! 190 const unsigned char *data, 190 !! 191 size_t count) 191 { 192 { 192 struct serdev_device *serdev = ctrl->s 193 struct serdev_device *serdev = ctrl->serdev; 193 194 194 if (!serdev || !serdev->ops->receive_b 195 if (!serdev || !serdev->ops->receive_buf) 195 return 0; 196 return 0; 196 197 197 return serdev->ops->receive_buf(serdev 198 return serdev->ops->receive_buf(serdev, data, count); 198 } 199 } 199 200 200 #if IS_ENABLED(CONFIG_SERIAL_DEV_BUS) 201 #if IS_ENABLED(CONFIG_SERIAL_DEV_BUS) 201 202 202 int serdev_device_open(struct serdev_device *) 203 int serdev_device_open(struct serdev_device *); 203 void serdev_device_close(struct serdev_device 204 void serdev_device_close(struct serdev_device *); 204 int devm_serdev_device_open(struct device *, s 205 int devm_serdev_device_open(struct device *, struct serdev_device *); 205 unsigned int serdev_device_set_baudrate(struct 206 unsigned int serdev_device_set_baudrate(struct serdev_device *, unsigned int); 206 void serdev_device_set_flow_control(struct ser 207 void serdev_device_set_flow_control(struct serdev_device *, bool); 207 int serdev_device_write_buf(struct serdev_devi !! 208 int serdev_device_write_buf(struct serdev_device *, const unsigned char *, size_t); 208 void serdev_device_wait_until_sent(struct serd 209 void serdev_device_wait_until_sent(struct serdev_device *, long); 209 int serdev_device_get_tiocm(struct serdev_devi 210 int serdev_device_get_tiocm(struct serdev_device *); 210 int serdev_device_set_tiocm(struct serdev_devi 211 int serdev_device_set_tiocm(struct serdev_device *, int, int); 211 int serdev_device_break_ctl(struct serdev_devi << 212 void serdev_device_write_wakeup(struct serdev_ 212 void serdev_device_write_wakeup(struct serdev_device *); 213 ssize_t serdev_device_write(struct serdev_devi !! 213 int serdev_device_write(struct serdev_device *, const unsigned char *, size_t, unsigned long); 214 void serdev_device_write_flush(struct serdev_d 214 void serdev_device_write_flush(struct serdev_device *); 215 int serdev_device_write_room(struct serdev_dev 215 int serdev_device_write_room(struct serdev_device *); 216 216 217 /* 217 /* 218 * serdev device driver functions 218 * serdev device driver functions 219 */ 219 */ 220 int __serdev_device_driver_register(struct ser 220 int __serdev_device_driver_register(struct serdev_device_driver *, struct module *); 221 #define serdev_device_driver_register(sdrv) \ 221 #define serdev_device_driver_register(sdrv) \ 222 __serdev_device_driver_register(sdrv, 222 __serdev_device_driver_register(sdrv, THIS_MODULE) 223 223 224 /** 224 /** 225 * serdev_device_driver_unregister() - unregis 225 * serdev_device_driver_unregister() - unregister an serdev client driver 226 * @sdrv: the driver to unregister 226 * @sdrv: the driver to unregister 227 */ 227 */ 228 static inline void serdev_device_driver_unregi 228 static inline void serdev_device_driver_unregister(struct serdev_device_driver *sdrv) 229 { 229 { 230 if (sdrv) 230 if (sdrv) 231 driver_unregister(&sdrv->drive 231 driver_unregister(&sdrv->driver); 232 } 232 } 233 233 234 #define module_serdev_device_driver(__serdev_d 234 #define module_serdev_device_driver(__serdev_device_driver) \ 235 module_driver(__serdev_device_driver, 235 module_driver(__serdev_device_driver, serdev_device_driver_register, \ 236 serdev_device_driver_u 236 serdev_device_driver_unregister) 237 237 238 #else 238 #else 239 239 240 static inline int serdev_device_open(struct se 240 static inline int serdev_device_open(struct serdev_device *sdev) 241 { 241 { 242 return -ENODEV; 242 return -ENODEV; 243 } 243 } 244 static inline void serdev_device_close(struct 244 static inline void serdev_device_close(struct serdev_device *sdev) {} 245 static inline unsigned int serdev_device_set_b 245 static inline unsigned int serdev_device_set_baudrate(struct serdev_device *sdev, unsigned int baudrate) 246 { 246 { 247 return 0; 247 return 0; 248 } 248 } 249 static inline void serdev_device_set_flow_cont 249 static inline void serdev_device_set_flow_control(struct serdev_device *sdev, bool enable) {} 250 static inline int serdev_device_write_buf(stru 250 static inline int serdev_device_write_buf(struct serdev_device *serdev, 251 cons !! 251 const unsigned char *buf, 252 size 252 size_t count) 253 { 253 { 254 return -ENODEV; 254 return -ENODEV; 255 } 255 } 256 static inline void serdev_device_wait_until_se 256 static inline void serdev_device_wait_until_sent(struct serdev_device *sdev, long timeout) {} 257 static inline int serdev_device_get_tiocm(stru 257 static inline int serdev_device_get_tiocm(struct serdev_device *serdev) 258 { 258 { 259 return -EOPNOTSUPP; !! 259 return -ENOTSUPP; 260 } 260 } 261 static inline int serdev_device_set_tiocm(stru 261 static inline int serdev_device_set_tiocm(struct serdev_device *serdev, int set, int clear) 262 { 262 { 263 return -EOPNOTSUPP; !! 263 return -ENOTSUPP; 264 } 264 } 265 static inline int serdev_device_break_ctl(stru !! 265 static inline int serdev_device_write(struct serdev_device *sdev, const unsigned char *buf, 266 { !! 266 size_t count, unsigned long timeout) 267 return -EOPNOTSUPP; << 268 } << 269 static inline ssize_t serdev_device_write(stru << 270 cons << 271 unsi << 272 { 267 { 273 return -ENODEV; 268 return -ENODEV; 274 } 269 } 275 static inline void serdev_device_write_flush(s 270 static inline void serdev_device_write_flush(struct serdev_device *sdev) {} 276 static inline int serdev_device_write_room(str 271 static inline int serdev_device_write_room(struct serdev_device *sdev) 277 { 272 { 278 return 0; 273 return 0; 279 } 274 } 280 275 281 #define serdev_device_driver_register(x) 276 #define serdev_device_driver_register(x) 282 #define serdev_device_driver_unregister(x) 277 #define serdev_device_driver_unregister(x) 283 278 284 #endif /* CONFIG_SERIAL_DEV_BUS */ 279 #endif /* CONFIG_SERIAL_DEV_BUS */ 285 280 286 static inline bool serdev_device_get_cts(struc 281 static inline bool serdev_device_get_cts(struct serdev_device *serdev) 287 { 282 { 288 int status = serdev_device_get_tiocm(s 283 int status = serdev_device_get_tiocm(serdev); 289 return !!(status & TIOCM_CTS); 284 return !!(status & TIOCM_CTS); 290 } 285 } 291 286 292 static inline int serdev_device_wait_for_cts(s 287 static inline int serdev_device_wait_for_cts(struct serdev_device *serdev, bool state, int timeout_ms) 293 { 288 { >> 289 unsigned long timeout; 294 bool signal; 290 bool signal; 295 291 296 return readx_poll_timeout(serdev_devic !! 292 timeout = jiffies + msecs_to_jiffies(timeout_ms); 297 2000, timeou !! 293 while (time_is_after_jiffies(timeout)) { >> 294 signal = serdev_device_get_cts(serdev); >> 295 if (signal == state) >> 296 return 0; >> 297 usleep_range(1000, 2000); >> 298 } >> 299 >> 300 return -ETIMEDOUT; 298 } 301 } 299 302 300 static inline int serdev_device_set_rts(struct 303 static inline int serdev_device_set_rts(struct serdev_device *serdev, bool enable) 301 { 304 { 302 if (enable) 305 if (enable) 303 return serdev_device_set_tiocm 306 return serdev_device_set_tiocm(serdev, TIOCM_RTS, 0); 304 else 307 else 305 return serdev_device_set_tiocm 308 return serdev_device_set_tiocm(serdev, 0, TIOCM_RTS); 306 } 309 } 307 310 308 int serdev_device_set_parity(struct serdev_dev 311 int serdev_device_set_parity(struct serdev_device *serdev, 309 enum serdev_parit 312 enum serdev_parity parity); 310 313 311 /* 314 /* 312 * serdev hooks into TTY core 315 * serdev hooks into TTY core 313 */ 316 */ 314 struct tty_port; 317 struct tty_port; 315 struct tty_driver; 318 struct tty_driver; 316 319 317 #ifdef CONFIG_SERIAL_DEV_CTRL_TTYPORT 320 #ifdef CONFIG_SERIAL_DEV_CTRL_TTYPORT 318 struct device *serdev_tty_port_register(struct 321 struct device *serdev_tty_port_register(struct tty_port *port, 319 struct << 320 struct 322 struct device *parent, 321 struct 323 struct tty_driver *drv, int idx); 322 int serdev_tty_port_unregister(struct tty_port 324 int serdev_tty_port_unregister(struct tty_port *port); 323 #else 325 #else 324 static inline struct device *serdev_tty_port_r 326 static inline struct device *serdev_tty_port_register(struct tty_port *port, 325 str << 326 str 327 struct device *parent, 327 str 328 struct tty_driver *drv, int idx) 328 { 329 { 329 return ERR_PTR(-ENODEV); 330 return ERR_PTR(-ENODEV); 330 } 331 } 331 static inline int serdev_tty_port_unregister(s 332 static inline int serdev_tty_port_unregister(struct tty_port *port) 332 { 333 { 333 return -ENODEV; 334 return -ENODEV; 334 } 335 } 335 #endif /* CONFIG_SERIAL_DEV_CTRL_TTYPORT */ 336 #endif /* CONFIG_SERIAL_DEV_CTRL_TTYPORT */ 336 << 337 struct acpi_resource; << 338 struct acpi_resource_uart_serialbus; << 339 << 340 #ifdef CONFIG_ACPI << 341 bool serdev_acpi_get_uart_resource(struct acpi << 342 struct acpi << 343 #else << 344 static inline bool serdev_acpi_get_uart_resour << 345 << 346 { << 347 return false; << 348 } << 349 #endif /* CONFIG_ACPI */ << 350 337 351 #endif /*_LINUX_SERDEV_H */ 338 #endif /*_LINUX_SERDEV_H */ 352 339
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.