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