0001
0002
0003
0004
0005 #ifndef _GAMEPORT_H
0006 #define _GAMEPORT_H
0007
0008 #include <asm/io.h>
0009 #include <linux/types.h>
0010 #include <linux/list.h>
0011 #include <linux/mutex.h>
0012 #include <linux/device.h>
0013 #include <linux/timer.h>
0014 #include <linux/slab.h>
0015 #include <uapi/linux/gameport.h>
0016
0017 struct gameport {
0018
0019 void *port_data;
0020 char name[32];
0021 char phys[32];
0022
0023 int io;
0024 int speed;
0025 int fuzz;
0026
0027 void (*trigger)(struct gameport *);
0028 unsigned char (*read)(struct gameport *);
0029 int (*cooked_read)(struct gameport *, int *, int *);
0030 int (*calibrate)(struct gameport *, int *, int *);
0031 int (*open)(struct gameport *, int);
0032 void (*close)(struct gameport *);
0033
0034 struct timer_list poll_timer;
0035 unsigned int poll_interval;
0036 spinlock_t timer_lock;
0037 unsigned int poll_cnt;
0038 void (*poll_handler)(struct gameport *);
0039
0040 struct gameport *parent, *child;
0041
0042 struct gameport_driver *drv;
0043 struct mutex drv_mutex;
0044
0045 struct device dev;
0046
0047 struct list_head node;
0048 };
0049 #define to_gameport_port(d) container_of(d, struct gameport, dev)
0050
0051 struct gameport_driver {
0052 const char *description;
0053
0054 int (*connect)(struct gameport *, struct gameport_driver *drv);
0055 int (*reconnect)(struct gameport *);
0056 void (*disconnect)(struct gameport *);
0057
0058 struct device_driver driver;
0059
0060 bool ignore;
0061 };
0062 #define to_gameport_driver(d) container_of(d, struct gameport_driver, driver)
0063
0064 int gameport_open(struct gameport *gameport, struct gameport_driver *drv, int mode);
0065 void gameport_close(struct gameport *gameport);
0066
0067 #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
0068
0069 void __gameport_register_port(struct gameport *gameport, struct module *owner);
0070
0071 #define gameport_register_port(gameport) \
0072 __gameport_register_port(gameport, THIS_MODULE)
0073
0074 void gameport_unregister_port(struct gameport *gameport);
0075
0076 __printf(2, 3)
0077 void gameport_set_phys(struct gameport *gameport, const char *fmt, ...);
0078
0079 #else
0080
0081 static inline void gameport_register_port(struct gameport *gameport)
0082 {
0083 return;
0084 }
0085
0086 static inline void gameport_unregister_port(struct gameport *gameport)
0087 {
0088 return;
0089 }
0090
0091 static inline __printf(2, 3)
0092 void gameport_set_phys(struct gameport *gameport, const char *fmt, ...)
0093 {
0094 return;
0095 }
0096
0097 #endif
0098
0099 static inline struct gameport *gameport_allocate_port(void)
0100 {
0101 struct gameport *gameport = kzalloc(sizeof(struct gameport), GFP_KERNEL);
0102
0103 return gameport;
0104 }
0105
0106 static inline void gameport_free_port(struct gameport *gameport)
0107 {
0108 kfree(gameport);
0109 }
0110
0111 static inline void gameport_set_name(struct gameport *gameport, const char *name)
0112 {
0113 strlcpy(gameport->name, name, sizeof(gameport->name));
0114 }
0115
0116
0117
0118
0119
0120 static inline void *gameport_get_drvdata(struct gameport *gameport)
0121 {
0122 return dev_get_drvdata(&gameport->dev);
0123 }
0124
0125 static inline void gameport_set_drvdata(struct gameport *gameport, void *data)
0126 {
0127 dev_set_drvdata(&gameport->dev, data);
0128 }
0129
0130
0131
0132
0133 static inline int gameport_pin_driver(struct gameport *gameport)
0134 {
0135 return mutex_lock_interruptible(&gameport->drv_mutex);
0136 }
0137
0138 static inline void gameport_unpin_driver(struct gameport *gameport)
0139 {
0140 mutex_unlock(&gameport->drv_mutex);
0141 }
0142
0143 int __must_check __gameport_register_driver(struct gameport_driver *drv,
0144 struct module *owner, const char *mod_name);
0145
0146
0147 #define gameport_register_driver(drv) \
0148 __gameport_register_driver(drv, THIS_MODULE, KBUILD_MODNAME)
0149
0150 void gameport_unregister_driver(struct gameport_driver *drv);
0151
0152
0153
0154
0155
0156
0157
0158
0159
0160
0161 #define module_gameport_driver(__gameport_driver) \
0162 module_driver(__gameport_driver, gameport_register_driver, \
0163 gameport_unregister_driver)
0164
0165
0166 static inline void gameport_trigger(struct gameport *gameport)
0167 {
0168 if (gameport->trigger)
0169 gameport->trigger(gameport);
0170 else
0171 outb(0xff, gameport->io);
0172 }
0173
0174 static inline unsigned char gameport_read(struct gameport *gameport)
0175 {
0176 if (gameport->read)
0177 return gameport->read(gameport);
0178 else
0179 return inb(gameport->io);
0180 }
0181
0182 static inline int gameport_cooked_read(struct gameport *gameport, int *axes, int *buttons)
0183 {
0184 if (gameport->cooked_read)
0185 return gameport->cooked_read(gameport, axes, buttons);
0186 else
0187 return -1;
0188 }
0189
0190 static inline int gameport_calibrate(struct gameport *gameport, int *axes, int *max)
0191 {
0192 if (gameport->calibrate)
0193 return gameport->calibrate(gameport, axes, max);
0194 else
0195 return -1;
0196 }
0197
0198 static inline int gameport_time(struct gameport *gameport, int time)
0199 {
0200 return (time * gameport->speed) / 1000;
0201 }
0202
0203 static inline void gameport_set_poll_handler(struct gameport *gameport, void (*handler)(struct gameport *))
0204 {
0205 gameport->poll_handler = handler;
0206 }
0207
0208 static inline void gameport_set_poll_interval(struct gameport *gameport, unsigned int msecs)
0209 {
0210 gameport->poll_interval = msecs;
0211 }
0212
0213 void gameport_start_polling(struct gameport *gameport);
0214 void gameport_stop_polling(struct gameport *gameport);
0215
0216 #endif