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