2 * Copyright (c) 1999-2002 Vojtech Pavlik
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published by
6 * the Free Software Foundation.
12 #include <linux/types.h>
13 #include <linux/list.h>
14 #include <linux/mutex.h>
15 #include <linux/device.h>
16 #include <linux/timer.h>
17 #include <linux/slab.h>
18 #include <uapi/linux/gameport.h>
22 void *port_data
; /* Private pointer for gameport drivers */
30 void (*trigger
)(struct gameport
*);
31 unsigned char (*read
)(struct gameport
*);
32 int (*cooked_read
)(struct gameport
*, int *, int *);
33 int (*calibrate
)(struct gameport
*, int *, int *);
34 int (*open
)(struct gameport
*, int);
35 void (*close
)(struct gameport
*);
37 struct timer_list poll_timer
;
38 unsigned int poll_interval
; /* in msecs */
39 spinlock_t timer_lock
;
40 unsigned int poll_cnt
;
41 void (*poll_handler
)(struct gameport
*);
43 struct gameport
*parent
, *child
;
45 struct gameport_driver
*drv
;
46 struct mutex drv_mutex
; /* protects serio->drv so attributes can pin driver */
50 struct list_head node
;
52 #define to_gameport_port(d) container_of(d, struct gameport, dev)
54 struct gameport_driver
{
55 const char *description
;
57 int (*connect
)(struct gameport
*, struct gameport_driver
*drv
);
58 int (*reconnect
)(struct gameport
*);
59 void (*disconnect
)(struct gameport
*);
61 struct device_driver driver
;
65 #define to_gameport_driver(d) container_of(d, struct gameport_driver, driver)
67 int gameport_open(struct gameport
*gameport
, struct gameport_driver
*drv
, int mode
);
68 void gameport_close(struct gameport
*gameport
);
70 #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
72 void __gameport_register_port(struct gameport
*gameport
, struct module
*owner
);
73 /* use a define to avoid include chaining to get THIS_MODULE */
74 #define gameport_register_port(gameport) \
75 __gameport_register_port(gameport, THIS_MODULE)
77 void gameport_unregister_port(struct gameport
*gameport
);
80 void gameport_set_phys(struct gameport
*gameport
, const char *fmt
, ...);
84 static inline void gameport_register_port(struct gameport
*gameport
)
89 static inline void gameport_unregister_port(struct gameport
*gameport
)
94 static inline __printf(2, 3)
95 void gameport_set_phys(struct gameport
*gameport
, const char *fmt
, ...)
102 static inline struct gameport
*gameport_allocate_port(void)
104 struct gameport
*gameport
= kzalloc(sizeof(struct gameport
), GFP_KERNEL
);
109 static inline void gameport_free_port(struct gameport
*gameport
)
114 static inline void gameport_set_name(struct gameport
*gameport
, const char *name
)
116 strlcpy(gameport
->name
, name
, sizeof(gameport
->name
));
120 * Use the following functions to manipulate gameport's per-port
121 * driver-specific data.
123 static inline void *gameport_get_drvdata(struct gameport
*gameport
)
125 return dev_get_drvdata(&gameport
->dev
);
128 static inline void gameport_set_drvdata(struct gameport
*gameport
, void *data
)
130 dev_set_drvdata(&gameport
->dev
, data
);
134 * Use the following functions to pin gameport's driver in process context
136 static inline int gameport_pin_driver(struct gameport
*gameport
)
138 return mutex_lock_interruptible(&gameport
->drv_mutex
);
141 static inline void gameport_unpin_driver(struct gameport
*gameport
)
143 mutex_unlock(&gameport
->drv_mutex
);
146 int __must_check
__gameport_register_driver(struct gameport_driver
*drv
,
147 struct module
*owner
, const char *mod_name
);
149 /* use a define to avoid include chaining to get THIS_MODULE & friends */
150 #define gameport_register_driver(drv) \
151 __gameport_register_driver(drv, THIS_MODULE, KBUILD_MODNAME)
153 void gameport_unregister_driver(struct gameport_driver
*drv
);
156 * module_gameport_driver() - Helper macro for registering a gameport driver
157 * @__gameport_driver: gameport_driver struct
159 * Helper macro for gameport drivers which do not do anything special in
160 * module init/exit. This eliminates a lot of boilerplate. Each module may
161 * only use this macro once, and calling it replaces module_init() and
164 #define module_gameport_driver(__gameport_driver) \
165 module_driver(__gameport_driver, gameport_register_driver, \
166 gameport_unregister_driver)
169 static inline void gameport_trigger(struct gameport
*gameport
)
171 if (gameport
->trigger
)
172 gameport
->trigger(gameport
);
174 outb(0xff, gameport
->io
);
177 static inline unsigned char gameport_read(struct gameport
*gameport
)
180 return gameport
->read(gameport
);
182 return inb(gameport
->io
);
185 static inline int gameport_cooked_read(struct gameport
*gameport
, int *axes
, int *buttons
)
187 if (gameport
->cooked_read
)
188 return gameport
->cooked_read(gameport
, axes
, buttons
);
193 static inline int gameport_calibrate(struct gameport
*gameport
, int *axes
, int *max
)
195 if (gameport
->calibrate
)
196 return gameport
->calibrate(gameport
, axes
, max
);
201 static inline int gameport_time(struct gameport
*gameport
, int time
)
203 return (time
* gameport
->speed
) / 1000;
206 static inline void gameport_set_poll_handler(struct gameport
*gameport
, void (*handler
)(struct gameport
*))
208 gameport
->poll_handler
= handler
;
211 static inline void gameport_set_poll_interval(struct gameport
*gameport
, unsigned int msecs
)
213 gameport
->poll_interval
= msecs
;
216 void gameport_start_polling(struct gameport
*gameport
);
217 void gameport_stop_polling(struct gameport
*gameport
);