Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
1da177e4 LT |
2 | /* |
3 | * Copyright (c) 1999-2002 Vojtech Pavlik | |
1da177e4 | 4 | */ |
607ca46e DH |
5 | #ifndef _GAMEPORT_H |
6 | #define _GAMEPORT_H | |
1da177e4 | 7 | |
7e044e05 | 8 | #include <linux/types.h> |
1da177e4 | 9 | #include <linux/list.h> |
286295eb | 10 | #include <linux/mutex.h> |
1da177e4 | 11 | #include <linux/device.h> |
4e57b681 | 12 | #include <linux/timer.h> |
5a0e3ad6 | 13 | #include <linux/slab.h> |
607ca46e | 14 | #include <uapi/linux/gameport.h> |
1da177e4 LT |
15 | |
16 | struct gameport { | |
17 | ||
18 | void *port_data; /* Private pointer for gameport drivers */ | |
19 | char name[32]; | |
20 | char phys[32]; | |
21 | ||
22 | int io; | |
23 | int speed; | |
24 | int fuzz; | |
25 | ||
26 | void (*trigger)(struct gameport *); | |
27 | unsigned char (*read)(struct gameport *); | |
28 | int (*cooked_read)(struct gameport *, int *, int *); | |
29 | int (*calibrate)(struct gameport *, int *, int *); | |
30 | int (*open)(struct gameport *, int); | |
31 | void (*close)(struct gameport *); | |
32 | ||
33 | struct timer_list poll_timer; | |
34 | unsigned int poll_interval; /* in msecs */ | |
35 | spinlock_t timer_lock; | |
36 | unsigned int poll_cnt; | |
37 | void (*poll_handler)(struct gameport *); | |
38 | ||
39 | struct gameport *parent, *child; | |
40 | ||
41 | struct gameport_driver *drv; | |
286295eb | 42 | struct mutex drv_mutex; /* protects serio->drv so attributes can pin driver */ |
1da177e4 LT |
43 | |
44 | struct device dev; | |
1da177e4 LT |
45 | |
46 | struct list_head node; | |
47 | }; | |
48 | #define to_gameport_port(d) container_of(d, struct gameport, dev) | |
49 | ||
50 | struct gameport_driver { | |
37b0bb11 | 51 | const char *description; |
1da177e4 LT |
52 | |
53 | int (*connect)(struct gameport *, struct gameport_driver *drv); | |
54 | int (*reconnect)(struct gameport *); | |
55 | void (*disconnect)(struct gameport *); | |
56 | ||
57 | struct device_driver driver; | |
58 | ||
7e044e05 | 59 | bool ignore; |
1da177e4 LT |
60 | }; |
61 | #define to_gameport_driver(d) container_of(d, struct gameport_driver, driver) | |
62 | ||
63 | int gameport_open(struct gameport *gameport, struct gameport_driver *drv, int mode); | |
64 | void gameport_close(struct gameport *gameport); | |
1da177e4 | 65 | |
1680ac7a | 66 | #if IS_REACHABLE(CONFIG_GAMEPORT) |
668d1e60 | 67 | |
1da177e4 | 68 | void __gameport_register_port(struct gameport *gameport, struct module *owner); |
eb5589a8 PG |
69 | /* use a define to avoid include chaining to get THIS_MODULE */ |
70 | #define gameport_register_port(gameport) \ | |
71 | __gameport_register_port(gameport, THIS_MODULE) | |
1da177e4 LT |
72 | |
73 | void gameport_unregister_port(struct gameport *gameport); | |
74 | ||
b9075fa9 JP |
75 | __printf(2, 3) |
76 | void gameport_set_phys(struct gameport *gameport, const char *fmt, ...); | |
668d1e60 AB |
77 | |
78 | #else | |
79 | ||
80 | static inline void gameport_register_port(struct gameport *gameport) | |
81 | { | |
82 | return; | |
83 | } | |
84 | ||
85 | static inline void gameport_unregister_port(struct gameport *gameport) | |
86 | { | |
87 | return; | |
88 | } | |
89 | ||
b9075fa9 JP |
90 | static inline __printf(2, 3) |
91 | void gameport_set_phys(struct gameport *gameport, const char *fmt, ...) | |
668d1e60 AB |
92 | { |
93 | return; | |
94 | } | |
95 | ||
96 | #endif | |
97 | ||
1da177e4 LT |
98 | static inline struct gameport *gameport_allocate_port(void) |
99 | { | |
cd861280 | 100 | struct gameport *gameport = kzalloc(sizeof(struct gameport), GFP_KERNEL); |
1da177e4 LT |
101 | |
102 | return gameport; | |
103 | } | |
104 | ||
105 | static inline void gameport_free_port(struct gameport *gameport) | |
106 | { | |
107 | kfree(gameport); | |
108 | } | |
109 | ||
110 | static inline void gameport_set_name(struct gameport *gameport, const char *name) | |
111 | { | |
a9da7251 | 112 | strscpy(gameport->name, name, sizeof(gameport->name)); |
1da177e4 LT |
113 | } |
114 | ||
1da177e4 | 115 | /* |
0b28002f | 116 | * Use the following functions to manipulate gameport's per-port |
1da177e4 LT |
117 | * driver-specific data. |
118 | */ | |
119 | static inline void *gameport_get_drvdata(struct gameport *gameport) | |
120 | { | |
121 | return dev_get_drvdata(&gameport->dev); | |
122 | } | |
123 | ||
124 | static inline void gameport_set_drvdata(struct gameport *gameport, void *data) | |
125 | { | |
126 | dev_set_drvdata(&gameport->dev, data); | |
127 | } | |
128 | ||
129 | /* | |
0b28002f | 130 | * Use the following functions to pin gameport's driver in process context |
1da177e4 LT |
131 | */ |
132 | static inline int gameport_pin_driver(struct gameport *gameport) | |
133 | { | |
286295eb | 134 | return mutex_lock_interruptible(&gameport->drv_mutex); |
1da177e4 LT |
135 | } |
136 | ||
137 | static inline void gameport_unpin_driver(struct gameport *gameport) | |
138 | { | |
286295eb | 139 | mutex_unlock(&gameport->drv_mutex); |
1da177e4 LT |
140 | } |
141 | ||
eb5589a8 | 142 | int __must_check __gameport_register_driver(struct gameport_driver *drv, |
6902c0be | 143 | struct module *owner, const char *mod_name); |
eb5589a8 PG |
144 | |
145 | /* use a define to avoid include chaining to get THIS_MODULE & friends */ | |
146 | #define gameport_register_driver(drv) \ | |
147 | __gameport_register_driver(drv, THIS_MODULE, KBUILD_MODNAME) | |
1da177e4 LT |
148 | |
149 | void gameport_unregister_driver(struct gameport_driver *drv); | |
150 | ||
45b2604e AL |
151 | /** |
152 | * module_gameport_driver() - Helper macro for registering a gameport driver | |
153 | * @__gameport_driver: gameport_driver struct | |
154 | * | |
155 | * Helper macro for gameport drivers which do not do anything special in | |
156 | * module init/exit. This eliminates a lot of boilerplate. Each module may | |
157 | * only use this macro once, and calling it replaces module_init() and | |
158 | * module_exit(). | |
159 | */ | |
160 | #define module_gameport_driver(__gameport_driver) \ | |
161 | module_driver(__gameport_driver, gameport_register_driver, \ | |
162 | gameport_unregister_driver) | |
163 | ||
25478bb2 | 164 | |
1da177e4 LT |
165 | static inline void gameport_trigger(struct gameport *gameport) |
166 | { | |
f11f1a92 | 167 | gameport->trigger(gameport); |
1da177e4 LT |
168 | } |
169 | ||
170 | static inline unsigned char gameport_read(struct gameport *gameport) | |
171 | { | |
f11f1a92 | 172 | return gameport->read(gameport); |
1da177e4 LT |
173 | } |
174 | ||
175 | static inline int gameport_cooked_read(struct gameport *gameport, int *axes, int *buttons) | |
176 | { | |
177 | if (gameport->cooked_read) | |
178 | return gameport->cooked_read(gameport, axes, buttons); | |
179 | else | |
180 | return -1; | |
181 | } | |
182 | ||
183 | static inline int gameport_calibrate(struct gameport *gameport, int *axes, int *max) | |
184 | { | |
185 | if (gameport->calibrate) | |
186 | return gameport->calibrate(gameport, axes, max); | |
187 | else | |
188 | return -1; | |
189 | } | |
190 | ||
191 | static inline int gameport_time(struct gameport *gameport, int time) | |
192 | { | |
193 | return (time * gameport->speed) / 1000; | |
194 | } | |
195 | ||
196 | static inline void gameport_set_poll_handler(struct gameport *gameport, void (*handler)(struct gameport *)) | |
197 | { | |
198 | gameport->poll_handler = handler; | |
199 | } | |
200 | ||
201 | static inline void gameport_set_poll_interval(struct gameport *gameport, unsigned int msecs) | |
202 | { | |
203 | gameport->poll_interval = msecs; | |
204 | } | |
205 | ||
206 | void gameport_start_polling(struct gameport *gameport); | |
207 | void gameport_stop_polling(struct gameport *gameport); | |
208 | ||
209 | #endif |