Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 1999-2002 Vojtech Pavlik |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | */ |
David Howells | 607ca46 | 2012-10-13 10:46:48 +0100 | [diff] [blame] | 5 | #ifndef _SERIO_H |
| 6 | #define _SERIO_H |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | |
Dmitry Torokhov | 7e044e0 | 2009-05-09 16:08:05 -0700 | [diff] [blame] | 9 | #include <linux/types.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/interrupt.h> |
| 11 | #include <linux/list.h> |
| 12 | #include <linux/spinlock.h> |
Arjan van de Ven | c4e32e9 | 2006-02-19 00:21:55 -0500 | [diff] [blame] | 13 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/device.h> |
| 15 | #include <linux/mod_devicetable.h> |
David Howells | 607ca46 | 2012-10-13 10:46:48 +0100 | [diff] [blame] | 16 | #include <uapi/linux/serio.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
Stephen Chandler Paul | e1443d2 | 2015-07-15 10:20:17 -0700 | [diff] [blame] | 18 | extern struct bus_type serio_bus; |
| 19 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | struct serio { |
| 21 | void *port_data; |
| 22 | |
| 23 | char name[32]; |
| 24 | char phys[32]; |
Hans de Goede | 0456c66 | 2014-04-19 20:39:35 -0700 | [diff] [blame] | 25 | char firmware_id[128]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
Dmitry Torokhov | 7e044e0 | 2009-05-09 16:08:05 -0700 | [diff] [blame] | 27 | bool manual_bind; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
| 29 | struct serio_device_id id; |
| 30 | |
Dmitry Torokhov | 4097461 | 2016-07-25 11:36:54 -0700 | [diff] [blame] | 31 | /* Protects critical sections from port's interrupt handler */ |
| 32 | spinlock_t lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
| 34 | int (*write)(struct serio *, unsigned char); |
| 35 | int (*open)(struct serio *); |
| 36 | void (*close)(struct serio *); |
| 37 | int (*start)(struct serio *); |
| 38 | void (*stop)(struct serio *); |
| 39 | |
Dmitry Eremin-Solenikov | 0982258 | 2010-10-04 21:46:10 -0700 | [diff] [blame] | 40 | struct serio *parent; |
Dmitry Torokhov | 4097461 | 2016-07-25 11:36:54 -0700 | [diff] [blame] | 41 | /* Entry in parent->children list */ |
| 42 | struct list_head child_node; |
Dmitry Eremin-Solenikov | 0982258 | 2010-10-04 21:46:10 -0700 | [diff] [blame] | 43 | struct list_head children; |
Dmitry Torokhov | 4097461 | 2016-07-25 11:36:54 -0700 | [diff] [blame] | 44 | /* Level of nesting in serio hierarchy */ |
| 45 | unsigned int depth; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
Dmitry Torokhov | 4097461 | 2016-07-25 11:36:54 -0700 | [diff] [blame] | 47 | /* |
| 48 | * serio->drv is accessed from interrupt handlers; when modifying |
| 49 | * caller should acquire serio->drv_mutex and serio->lock. |
| 50 | */ |
| 51 | struct serio_driver *drv; |
| 52 | /* Protects serio->drv so attributes can pin current driver */ |
| 53 | struct mutex drv_mutex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
| 55 | struct device dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
| 57 | struct list_head node; |
Dmitry Torokhov | 4097461 | 2016-07-25 11:36:54 -0700 | [diff] [blame] | 58 | |
| 59 | /* |
| 60 | * For use by PS/2 layer when several ports share hardware and |
| 61 | * may get indigestion when exposed to concurrent access (i8042). |
| 62 | */ |
| 63 | struct mutex *ps2_cmd_mutex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | }; |
| 65 | #define to_serio_port(d) container_of(d, struct serio, dev) |
| 66 | |
| 67 | struct serio_driver { |
Dmitry Torokhov | ceee427 | 2010-09-13 23:53:55 -0700 | [diff] [blame] | 68 | const char *description; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
Dmitry Torokhov | ceee427 | 2010-09-13 23:53:55 -0700 | [diff] [blame] | 70 | const struct serio_device_id *id_table; |
Dmitry Torokhov | 7e044e0 | 2009-05-09 16:08:05 -0700 | [diff] [blame] | 71 | bool manual_bind; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
| 73 | void (*write_wakeup)(struct serio *); |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 74 | irqreturn_t (*interrupt)(struct serio *, unsigned char, unsigned int); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | int (*connect)(struct serio *, struct serio_driver *drv); |
| 76 | int (*reconnect)(struct serio *); |
Dmitry Torokhov | 5ea1320 | 2017-03-03 11:47:40 -0800 | [diff] [blame] | 77 | int (*fast_reconnect)(struct serio *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | void (*disconnect)(struct serio *); |
| 79 | void (*cleanup)(struct serio *); |
| 80 | |
| 81 | struct device_driver driver; |
| 82 | }; |
| 83 | #define to_serio_driver(d) container_of(d, struct serio_driver, driver) |
| 84 | |
| 85 | int serio_open(struct serio *serio, struct serio_driver *drv); |
| 86 | void serio_close(struct serio *serio); |
| 87 | void serio_rescan(struct serio *serio); |
| 88 | void serio_reconnect(struct serio *serio); |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 89 | irqreturn_t serio_interrupt(struct serio *serio, unsigned char data, unsigned int flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | |
| 91 | void __serio_register_port(struct serio *serio, struct module *owner); |
Paul Gortmaker | eb5589a | 2011-05-27 09:02:11 -0400 | [diff] [blame] | 92 | |
| 93 | /* use a define to avoid include chaining to get THIS_MODULE */ |
| 94 | #define serio_register_port(serio) \ |
| 95 | __serio_register_port(serio, THIS_MODULE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
| 97 | void serio_unregister_port(struct serio *serio); |
Dmitry Torokhov | dbf4ccd | 2005-06-01 02:40:01 -0500 | [diff] [blame] | 98 | void serio_unregister_child_port(struct serio *serio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | |
Paul Gortmaker | eb5589a | 2011-05-27 09:02:11 -0400 | [diff] [blame] | 100 | int __must_check __serio_register_driver(struct serio_driver *drv, |
| 101 | struct module *owner, const char *mod_name); |
| 102 | |
| 103 | /* use a define to avoid include chaining to get THIS_MODULE & friends */ |
| 104 | #define serio_register_driver(drv) \ |
| 105 | __serio_register_driver(drv, THIS_MODULE, KBUILD_MODNAME) |
| 106 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | void serio_unregister_driver(struct serio_driver *drv); |
| 108 | |
Axel Lin | fa7f86d | 2012-04-03 23:50:15 -0700 | [diff] [blame] | 109 | /** |
| 110 | * module_serio_driver() - Helper macro for registering a serio driver |
| 111 | * @__serio_driver: serio_driver struct |
| 112 | * |
| 113 | * Helper macro for serio drivers which do not do anything special in |
| 114 | * module init/exit. This eliminates a lot of boilerplate. Each module |
| 115 | * may only use this macro once, and calling it replaces module_init() |
| 116 | * and module_exit(). |
| 117 | */ |
| 118 | #define module_serio_driver(__serio_driver) \ |
| 119 | module_driver(__serio_driver, serio_register_driver, \ |
| 120 | serio_unregister_driver) |
| 121 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | static inline int serio_write(struct serio *serio, unsigned char data) |
| 123 | { |
| 124 | if (serio->write) |
| 125 | return serio->write(serio, data); |
| 126 | else |
| 127 | return -1; |
| 128 | } |
| 129 | |
| 130 | static inline void serio_drv_write_wakeup(struct serio *serio) |
| 131 | { |
| 132 | if (serio->drv && serio->drv->write_wakeup) |
| 133 | serio->drv->write_wakeup(serio); |
| 134 | } |
| 135 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | /* |
Akinobu Mita | 0b28002 | 2006-03-26 01:38:58 -0800 | [diff] [blame] | 137 | * Use the following functions to manipulate serio's per-port |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | * driver-specific data. |
| 139 | */ |
| 140 | static inline void *serio_get_drvdata(struct serio *serio) |
| 141 | { |
| 142 | return dev_get_drvdata(&serio->dev); |
| 143 | } |
| 144 | |
| 145 | static inline void serio_set_drvdata(struct serio *serio, void *data) |
| 146 | { |
| 147 | dev_set_drvdata(&serio->dev, data); |
| 148 | } |
| 149 | |
| 150 | /* |
Akinobu Mita | 0b28002 | 2006-03-26 01:38:58 -0800 | [diff] [blame] | 151 | * Use the following functions to protect critical sections in |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | * driver code from port's interrupt handler |
| 153 | */ |
| 154 | static inline void serio_pause_rx(struct serio *serio) |
| 155 | { |
| 156 | spin_lock_irq(&serio->lock); |
| 157 | } |
| 158 | |
| 159 | static inline void serio_continue_rx(struct serio *serio) |
| 160 | { |
| 161 | spin_unlock_irq(&serio->lock); |
| 162 | } |
| 163 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | #endif |