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