1/* SPDX-License-Identifier: GPL-2.0-only */
2/*
3 * Copyright (c) 1999-2002 Vojtech Pavlik
4 */
5#ifndef _GAMEPORT_H
6#define _GAMEPORT_H
7
8#include <linux/types.h>
9#include <linux/list.h>
10#include <linux/mutex.h>
11#include <linux/device.h>
12#include <linux/timer.h>
13#include <linux/slab.h>
14#include <uapi/linux/gameport.h>
15
16struct 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;
42 struct mutex drv_mutex; /* protects serio->drv so attributes can pin driver */
43
44 struct device dev;
45
46 struct list_head node;
47};
48#define to_gameport_port(d) container_of(d, struct gameport, dev)
49
50struct gameport_driver {
51 const char *description;
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
59 bool ignore;
60};
61#define to_gameport_driver(d) container_of(d, struct gameport_driver, driver)
62
63int gameport_open(struct gameport *gameport, struct gameport_driver *drv, int mode);
64void gameport_close(struct gameport *gameport);
65
66#if IS_REACHABLE(CONFIG_GAMEPORT)
67
68void __gameport_register_port(struct gameport *gameport, struct module *owner);
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)
72
73void gameport_unregister_port(struct gameport *gameport);
74
75__printf(2, 3)
76void gameport_set_phys(struct gameport *gameport, const char *fmt, ...);
77
78#else
79
80static inline void gameport_register_port(struct gameport *gameport)
81{
82 return;
83}
84
85static inline void gameport_unregister_port(struct gameport *gameport)
86{
87 return;
88}
89
90static inline __printf(2, 3)
91void gameport_set_phys(struct gameport *gameport, const char *fmt, ...)
92{
93 return;
94}
95
96#endif
97
98static inline struct gameport *gameport_allocate_port(void)
99{
100 struct gameport *gameport = kzalloc(size: sizeof(struct gameport), GFP_KERNEL);
101
102 return gameport;
103}
104
105static inline void gameport_free_port(struct gameport *gameport)
106{
107 kfree(objp: gameport);
108}
109
110static inline void gameport_set_name(struct gameport *gameport, const char *name)
111{
112 strscpy(p: gameport->name, q: name, size: sizeof(gameport->name));
113}
114
115/*
116 * Use the following functions to manipulate gameport's per-port
117 * driver-specific data.
118 */
119static inline void *gameport_get_drvdata(struct gameport *gameport)
120{
121 return dev_get_drvdata(dev: &gameport->dev);
122}
123
124static inline void gameport_set_drvdata(struct gameport *gameport, void *data)
125{
126 dev_set_drvdata(dev: &gameport->dev, data);
127}
128
129/*
130 * Use the following functions to pin gameport's driver in process context
131 */
132static inline int gameport_pin_driver(struct gameport *gameport)
133{
134 return mutex_lock_interruptible(&gameport->drv_mutex);
135}
136
137static inline void gameport_unpin_driver(struct gameport *gameport)
138{
139 mutex_unlock(lock: &gameport->drv_mutex);
140}
141
142int __must_check __gameport_register_driver(struct gameport_driver *drv,
143 struct module *owner, const char *mod_name);
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)
148
149void gameport_unregister_driver(struct gameport_driver *drv);
150
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
164
165static inline void gameport_trigger(struct gameport *gameport)
166{
167 gameport->trigger(gameport);
168}
169
170static inline unsigned char gameport_read(struct gameport *gameport)
171{
172 return gameport->read(gameport);
173}
174
175static 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
183static 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
191static inline int gameport_time(struct gameport *gameport, int time)
192{
193 return (time * gameport->speed) / 1000;
194}
195
196static inline void gameport_set_poll_handler(struct gameport *gameport, void (*handler)(struct gameport *))
197{
198 gameport->poll_handler = handler;
199}
200
201static inline void gameport_set_poll_interval(struct gameport *gameport, unsigned int msecs)
202{
203 gameport->poll_interval = msecs;
204}
205
206void gameport_start_polling(struct gameport *gameport);
207void gameport_stop_polling(struct gameport *gameport);
208
209#endif
210

source code of linux/include/linux/gameport.h