blob: b456b08d70ed4bd68aa77ef511b8cc9b05851d97 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _GAMEPORT_H
2#define _GAMEPORT_H
3
4/*
5 * Copyright (c) 1999-2002 Vojtech Pavlik
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published by
9 * the Free Software Foundation.
10 */
11
David Woodhouse25478bb2006-04-25 13:59:30 +010012#ifdef __KERNEL__
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <asm/io.h>
Dmitry Torokhov7e044e02009-05-09 16:08:05 -070014#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/list.h>
Arjan van de Ven286295e2006-02-19 00:22:03 -050016#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/device.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080018#include <linux/timer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21struct gameport {
22
23 void *port_data; /* Private pointer for gameport drivers */
24 char name[32];
25 char phys[32];
26
27 int io;
28 int speed;
29 int fuzz;
30
31 void (*trigger)(struct gameport *);
32 unsigned char (*read)(struct gameport *);
33 int (*cooked_read)(struct gameport *, int *, int *);
34 int (*calibrate)(struct gameport *, int *, int *);
35 int (*open)(struct gameport *, int);
36 void (*close)(struct gameport *);
37
38 struct timer_list poll_timer;
39 unsigned int poll_interval; /* in msecs */
40 spinlock_t timer_lock;
41 unsigned int poll_cnt;
42 void (*poll_handler)(struct gameport *);
43
44 struct gameport *parent, *child;
45
46 struct gameport_driver *drv;
Arjan van de Ven286295e2006-02-19 00:22:03 -050047 struct mutex drv_mutex; /* protects serio->drv so attributes can pin driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49 struct device dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51 struct list_head node;
52};
53#define to_gameport_port(d) container_of(d, struct gameport, dev)
54
55struct gameport_driver {
Dmitry Torokhov37b0bb12010-09-13 23:53:55 -070056 const char *description;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58 int (*connect)(struct gameport *, struct gameport_driver *drv);
59 int (*reconnect)(struct gameport *);
60 void (*disconnect)(struct gameport *);
61
62 struct device_driver driver;
63
Dmitry Torokhov7e044e02009-05-09 16:08:05 -070064 bool ignore;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065};
66#define to_gameport_driver(d) container_of(d, struct gameport_driver, driver)
67
68int gameport_open(struct gameport *gameport, struct gameport_driver *drv, int mode);
69void gameport_close(struct gameport *gameport);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Adrian Bunk668d1e62005-05-28 02:11:12 -050071#if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073void __gameport_register_port(struct gameport *gameport, struct module *owner);
Paul Gortmakereb5589a2011-05-27 09:02:11 -040074/* use a define to avoid include chaining to get THIS_MODULE */
75#define gameport_register_port(gameport) \
76 __gameport_register_port(gameport, THIS_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78void gameport_unregister_port(struct gameport *gameport);
79
Joe Perchesb9075fa2011-10-31 17:11:33 -070080__printf(2, 3)
81void gameport_set_phys(struct gameport *gameport, const char *fmt, ...);
Adrian Bunk668d1e62005-05-28 02:11:12 -050082
83#else
84
85static inline void gameport_register_port(struct gameport *gameport)
86{
87 return;
88}
89
90static inline void gameport_unregister_port(struct gameport *gameport)
91{
92 return;
93}
94
Joe Perchesb9075fa2011-10-31 17:11:33 -070095static inline __printf(2, 3)
96void gameport_set_phys(struct gameport *gameport, const char *fmt, ...)
Adrian Bunk668d1e62005-05-28 02:11:12 -050097{
98 return;
99}
100
101#endif
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103static inline struct gameport *gameport_allocate_port(void)
104{
Robert P. J. Daycd861282006-12-13 00:34:52 -0800105 struct gameport *gameport = kzalloc(sizeof(struct gameport), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 return gameport;
108}
109
110static inline void gameport_free_port(struct gameport *gameport)
111{
112 kfree(gameport);
113}
114
115static inline void gameport_set_name(struct gameport *gameport, const char *name)
116{
117 strlcpy(gameport->name, name, sizeof(gameport->name));
118}
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120/*
Akinobu Mita0b280022006-03-26 01:38:58 -0800121 * Use the following functions to manipulate gameport's per-port
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 * driver-specific data.
123 */
124static inline void *gameport_get_drvdata(struct gameport *gameport)
125{
126 return dev_get_drvdata(&gameport->dev);
127}
128
129static inline void gameport_set_drvdata(struct gameport *gameport, void *data)
130{
131 dev_set_drvdata(&gameport->dev, data);
132}
133
134/*
Akinobu Mita0b280022006-03-26 01:38:58 -0800135 * Use the following functions to pin gameport's driver in process context
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 */
137static inline int gameport_pin_driver(struct gameport *gameport)
138{
Arjan van de Ven286295e2006-02-19 00:22:03 -0500139 return mutex_lock_interruptible(&gameport->drv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141
142static inline void gameport_unpin_driver(struct gameport *gameport)
143{
Arjan van de Ven286295e2006-02-19 00:22:03 -0500144 mutex_unlock(&gameport->drv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
Paul Gortmakereb5589a2011-05-27 09:02:11 -0400147int __must_check __gameport_register_driver(struct gameport_driver *drv,
Dmitry Torokhov6902c0b2008-06-06 01:33:22 -0400148 struct module *owner, const char *mod_name);
Paul Gortmakereb5589a2011-05-27 09:02:11 -0400149
150/* use a define to avoid include chaining to get THIS_MODULE & friends */
151#define gameport_register_driver(drv) \
152 __gameport_register_driver(drv, THIS_MODULE, KBUILD_MODNAME)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154void gameport_unregister_driver(struct gameport_driver *drv);
155
David Woodhouse25478bb2006-04-25 13:59:30 +0100156#endif /* __KERNEL__ */
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158#define GAMEPORT_MODE_DISABLED 0
159#define GAMEPORT_MODE_RAW 1
160#define GAMEPORT_MODE_COOKED 2
161
162#define GAMEPORT_ID_VENDOR_ANALOG 0x0001
163#define GAMEPORT_ID_VENDOR_MADCATZ 0x0002
164#define GAMEPORT_ID_VENDOR_LOGITECH 0x0003
165#define GAMEPORT_ID_VENDOR_CREATIVE 0x0004
166#define GAMEPORT_ID_VENDOR_GENIUS 0x0005
167#define GAMEPORT_ID_VENDOR_INTERACT 0x0006
168#define GAMEPORT_ID_VENDOR_MICROSOFT 0x0007
169#define GAMEPORT_ID_VENDOR_THRUSTMASTER 0x0008
170#define GAMEPORT_ID_VENDOR_GRAVIS 0x0009
171#define GAMEPORT_ID_VENDOR_GUILLEMOT 0x000a
172
David Woodhouse25478bb2006-04-25 13:59:30 +0100173#ifdef __KERNEL__
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175static inline void gameport_trigger(struct gameport *gameport)
176{
177 if (gameport->trigger)
178 gameport->trigger(gameport);
179 else
180 outb(0xff, gameport->io);
181}
182
183static inline unsigned char gameport_read(struct gameport *gameport)
184{
185 if (gameport->read)
186 return gameport->read(gameport);
187 else
188 return inb(gameport->io);
189}
190
191static inline int gameport_cooked_read(struct gameport *gameport, int *axes, int *buttons)
192{
193 if (gameport->cooked_read)
194 return gameport->cooked_read(gameport, axes, buttons);
195 else
196 return -1;
197}
198
199static inline int gameport_calibrate(struct gameport *gameport, int *axes, int *max)
200{
201 if (gameport->calibrate)
202 return gameport->calibrate(gameport, axes, max);
203 else
204 return -1;
205}
206
207static inline int gameport_time(struct gameport *gameport, int time)
208{
209 return (time * gameport->speed) / 1000;
210}
211
212static inline void gameport_set_poll_handler(struct gameport *gameport, void (*handler)(struct gameport *))
213{
214 gameport->poll_handler = handler;
215}
216
217static inline void gameport_set_poll_interval(struct gameport *gameport, unsigned int msecs)
218{
219 gameport->poll_interval = msecs;
220}
221
222void gameport_start_polling(struct gameport *gameport);
223void gameport_stop_polling(struct gameport *gameport);
224
David Woodhouse25478bb2006-04-25 13:59:30 +0100225#endif /* __KERNEL__ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226#endif