1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (c) 1999-2002 Vojtech Pavlik
9 #include <linux/types.h>
10 #include <linux/list.h>
11 #include <linux/mutex.h>
12 #include <linux/device.h>
13 #include <linux/timer.h>
14 #include <linux/slab.h>
15 #include <uapi/linux/gameport.h>
19 void *port_data; /* Private pointer for gameport drivers */
27 void (*trigger)(struct gameport *);
28 unsigned char (*read)(struct gameport *);
29 int (*cooked_read)(struct gameport *, int *, int *);
30 int (*calibrate)(struct gameport *, int *, int *);
31 int (*open)(struct gameport *, int);
32 void (*close)(struct gameport *);
34 struct timer_list poll_timer;
35 unsigned int poll_interval; /* in msecs */
36 spinlock_t timer_lock;
37 unsigned int poll_cnt;
38 void (*poll_handler)(struct gameport *);
40 struct gameport *parent, *child;
42 struct gameport_driver *drv;
43 struct mutex drv_mutex; /* protects serio->drv so attributes can pin driver */
47 struct list_head node;
49 #define to_gameport_port(d) container_of(d, struct gameport, dev)
51 struct gameport_driver {
52 const char *description;
54 int (*connect)(struct gameport *, struct gameport_driver *drv);
55 int (*reconnect)(struct gameport *);
56 void (*disconnect)(struct gameport *);
58 struct device_driver driver;
62 #define to_gameport_driver(d) container_of(d, struct gameport_driver, driver)
64 int gameport_open(struct gameport *gameport, struct gameport_driver *drv, int mode);
65 void gameport_close(struct gameport *gameport);
67 #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
69 void __gameport_register_port(struct gameport *gameport, struct module *owner);
70 /* use a define to avoid include chaining to get THIS_MODULE */
71 #define gameport_register_port(gameport) \
72 __gameport_register_port(gameport, THIS_MODULE)
74 void gameport_unregister_port(struct gameport *gameport);
77 void gameport_set_phys(struct gameport *gameport, const char *fmt, ...);
81 static inline void gameport_register_port(struct gameport *gameport)
86 static inline void gameport_unregister_port(struct gameport *gameport)
91 static inline __printf(2, 3)
92 void gameport_set_phys(struct gameport *gameport, const char *fmt, ...)
99 static inline struct gameport *gameport_allocate_port(void)
101 struct gameport *gameport = kzalloc(sizeof(struct gameport), GFP_KERNEL);
106 static inline void gameport_free_port(struct gameport *gameport)
111 static inline void gameport_set_name(struct gameport *gameport, const char *name)
113 strlcpy(gameport->name, name, sizeof(gameport->name));
117 * Use the following functions to manipulate gameport's per-port
118 * driver-specific data.
120 static inline void *gameport_get_drvdata(struct gameport *gameport)
122 return dev_get_drvdata(&gameport->dev);
125 static inline void gameport_set_drvdata(struct gameport *gameport, void *data)
127 dev_set_drvdata(&gameport->dev, data);
131 * Use the following functions to pin gameport's driver in process context
133 static inline int gameport_pin_driver(struct gameport *gameport)
135 return mutex_lock_interruptible(&gameport->drv_mutex);
138 static inline void gameport_unpin_driver(struct gameport *gameport)
140 mutex_unlock(&gameport->drv_mutex);
143 int __must_check __gameport_register_driver(struct gameport_driver *drv,
144 struct module *owner, const char *mod_name);
146 /* use a define to avoid include chaining to get THIS_MODULE & friends */
147 #define gameport_register_driver(drv) \
148 __gameport_register_driver(drv, THIS_MODULE, KBUILD_MODNAME)
150 void gameport_unregister_driver(struct gameport_driver *drv);
153 * module_gameport_driver() - Helper macro for registering a gameport driver
154 * @__gameport_driver: gameport_driver struct
156 * Helper macro for gameport drivers which do not do anything special in
157 * module init/exit. This eliminates a lot of boilerplate. Each module may
158 * only use this macro once, and calling it replaces module_init() and
161 #define module_gameport_driver(__gameport_driver) \
162 module_driver(__gameport_driver, gameport_register_driver, \
163 gameport_unregister_driver)
166 static inline void gameport_trigger(struct gameport *gameport)
168 if (gameport->trigger)
169 gameport->trigger(gameport);
171 outb(0xff, gameport->io);
174 static inline unsigned char gameport_read(struct gameport *gameport)
177 return gameport->read(gameport);
179 return inb(gameport->io);
182 static inline int gameport_cooked_read(struct gameport *gameport, int *axes, int *buttons)
184 if (gameport->cooked_read)
185 return gameport->cooked_read(gameport, axes, buttons);
190 static inline int gameport_calibrate(struct gameport *gameport, int *axes, int *max)
192 if (gameport->calibrate)
193 return gameport->calibrate(gameport, axes, max);
198 static inline int gameport_time(struct gameport *gameport, int time)
200 return (time * gameport->speed) / 1000;
203 static inline void gameport_set_poll_handler(struct gameport *gameport, void (*handler)(struct gameport *))
205 gameport->poll_handler = handler;
208 static inline void gameport_set_poll_interval(struct gameport *gameport, unsigned int msecs)
210 gameport->poll_interval = msecs;
213 void gameport_start_polling(struct gameport *gameport);
214 void gameport_stop_polling(struct gameport *gameport);