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