1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (c) 1999-2002 Vojtech Pavlik
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>
18 void *port_data
; /* Private pointer for gameport drivers */
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
*);
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
*);
39 struct gameport
*parent
, *child
;
41 struct gameport_driver
*drv
;
42 struct mutex drv_mutex
; /* protects serio->drv so attributes can pin driver */
46 struct list_head node
;
48 #define to_gameport_port(d) container_of(d, struct gameport, dev)
50 struct gameport_driver
{
51 const char *description
;
53 int (*connect
)(struct gameport
*, struct gameport_driver
*drv
);
54 int (*reconnect
)(struct gameport
*);
55 void (*disconnect
)(struct gameport
*);
57 struct device_driver driver
;
61 #define to_gameport_driver(d) container_of_const(d, struct gameport_driver, driver)
63 int gameport_open(struct gameport
*gameport
, struct gameport_driver
*drv
, int mode
);
64 void gameport_close(struct gameport
*gameport
);
66 #if IS_REACHABLE(CONFIG_GAMEPORT)
68 void __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)
73 void gameport_unregister_port(struct gameport
*gameport
);
76 void gameport_set_phys(struct gameport
*gameport
, const char *fmt
, ...);
80 static inline void gameport_register_port(struct gameport
*gameport
)
85 static inline void gameport_unregister_port(struct gameport
*gameport
)
90 static inline __printf(2, 3)
91 void gameport_set_phys(struct gameport
*gameport
, const char *fmt
, ...)
98 static inline struct gameport
*gameport_allocate_port(void)
100 struct gameport
*gameport
= kzalloc(sizeof(struct gameport
), GFP_KERNEL
);
105 static inline void gameport_free_port(struct gameport
*gameport
)
110 static inline void gameport_set_name(struct gameport
*gameport
, const char *name
)
112 strscpy(gameport
->name
, name
, sizeof(gameport
->name
));
116 * Use the following functions to manipulate gameport's per-port
117 * driver-specific data.
119 static inline void *gameport_get_drvdata(struct gameport
*gameport
)
121 return dev_get_drvdata(&gameport
->dev
);
124 static inline void gameport_set_drvdata(struct gameport
*gameport
, void *data
)
126 dev_set_drvdata(&gameport
->dev
, data
);
130 * Use the following functions to pin gameport's driver in process context
132 static inline int gameport_pin_driver(struct gameport
*gameport
)
134 return mutex_lock_interruptible(&gameport
->drv_mutex
);
137 static inline void gameport_unpin_driver(struct gameport
*gameport
)
139 mutex_unlock(&gameport
->drv_mutex
);
142 int __must_check
__gameport_register_driver(struct gameport_driver
*drv
,
143 struct module
*owner
, const char *mod_name
);
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)
149 void gameport_unregister_driver(struct gameport_driver
*drv
);
152 * module_gameport_driver() - Helper macro for registering a gameport driver
153 * @__gameport_driver: gameport_driver struct
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
160 #define module_gameport_driver(__gameport_driver) \
161 module_driver(__gameport_driver, gameport_register_driver, \
162 gameport_unregister_driver)
165 static inline void gameport_trigger(struct gameport
*gameport
)
167 gameport
->trigger(gameport
);
170 static inline unsigned char gameport_read(struct gameport
*gameport
)
172 return gameport
->read(gameport
);
175 static inline int gameport_cooked_read(struct gameport
*gameport
, int *axes
, int *buttons
)
177 if (gameport
->cooked_read
)
178 return gameport
->cooked_read(gameport
, axes
, buttons
);
183 static inline int gameport_calibrate(struct gameport
*gameport
, int *axes
, int *max
)
185 if (gameport
->calibrate
)
186 return gameport
->calibrate(gameport
, axes
, max
);
191 static inline int gameport_time(struct gameport
*gameport
, int time
)
193 return (time
* gameport
->speed
) / 1000;
196 static inline void gameport_set_poll_handler(struct gameport
*gameport
, void (*handler
)(struct gameport
*))
198 gameport
->poll_handler
= handler
;
201 static inline void gameport_set_poll_interval(struct gameport
*gameport
, unsigned int msecs
)
203 gameport
->poll_interval
= msecs
;
206 void gameport_start_polling(struct gameport
*gameport
);
207 void gameport_stop_polling(struct gameport
*gameport
);