blob: 1bc08541c2b9cff63e3ae58267244321034edb5b [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20struct gameport {
21
22 void *port_data; /* Private pointer for gameport drivers */
23 char name[32];
24 char phys[32];
25
26 int io;
27 int speed;
28 int fuzz;
29
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 *);
36
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 *);
42
43 struct gameport *parent, *child;
44
45 struct gameport_driver *drv;
Arjan van de Ven286295e2006-02-19 00:22:03 -050046 struct mutex drv_mutex; /* protects serio->drv so attributes can pin driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48 struct device dev;
49 unsigned int registered; /* port has been fully registered with driver core */
50
51 struct list_head node;
52};
53#define to_gameport_port(d) container_of(d, struct gameport, dev)
54
55struct gameport_driver {
56
57 void *private;
58 char *description;
59
60 int (*connect)(struct gameport *, struct gameport_driver *drv);
61 int (*reconnect)(struct gameport *);
62 void (*disconnect)(struct gameport *);
63
64 struct device_driver driver;
65
Dmitry Torokhov7e044e02009-05-09 16:08:05 -070066 bool ignore;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067};
68#define to_gameport_driver(d) container_of(d, struct gameport_driver, driver)
69
70int gameport_open(struct gameport *gameport, struct gameport_driver *drv, int mode);
71void gameport_close(struct gameport *gameport);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Adrian Bunk668d1e62005-05-28 02:11:12 -050073#if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075void __gameport_register_port(struct gameport *gameport, struct module *owner);
76static inline void gameport_register_port(struct gameport *gameport)
77{
78 __gameport_register_port(gameport, THIS_MODULE);
79}
80
81void gameport_unregister_port(struct gameport *gameport);
82
Adrian Bunk668d1e62005-05-28 02:11:12 -050083void gameport_set_phys(struct gameport *gameport, const char *fmt, ...)
84 __attribute__ ((format (printf, 2, 3)));
85
86#else
87
88static inline void gameport_register_port(struct gameport *gameport)
89{
90 return;
91}
92
93static inline void gameport_unregister_port(struct gameport *gameport)
94{
95 return;
96}
97
98static inline void gameport_set_phys(struct gameport *gameport,
99 const char *fmt, ...)
100{
101 return;
102}
103
104#endif
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106static inline struct gameport *gameport_allocate_port(void)
107{
Robert P. J. Daycd861282006-12-13 00:34:52 -0800108 struct gameport *gameport = kzalloc(sizeof(struct gameport), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110 return gameport;
111}
112
113static inline void gameport_free_port(struct gameport *gameport)
114{
115 kfree(gameport);
116}
117
118static inline void gameport_set_name(struct gameport *gameport, const char *name)
119{
120 strlcpy(gameport->name, name, sizeof(gameport->name));
121}
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123/*
Akinobu Mita0b280022006-03-26 01:38:58 -0800124 * Use the following functions to manipulate gameport's per-port
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 * driver-specific data.
126 */
127static inline void *gameport_get_drvdata(struct gameport *gameport)
128{
129 return dev_get_drvdata(&gameport->dev);
130}
131
132static inline void gameport_set_drvdata(struct gameport *gameport, void *data)
133{
134 dev_set_drvdata(&gameport->dev, data);
135}
136
137/*
Akinobu Mita0b280022006-03-26 01:38:58 -0800138 * Use the following functions to pin gameport's driver in process context
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 */
140static inline int gameport_pin_driver(struct gameport *gameport)
141{
Arjan van de Ven286295e2006-02-19 00:22:03 -0500142 return mutex_lock_interruptible(&gameport->drv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
145static inline void gameport_unpin_driver(struct gameport *gameport)
146{
Arjan van de Ven286295e2006-02-19 00:22:03 -0500147 mutex_unlock(&gameport->drv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149
Dmitry Torokhov6902c0b2008-06-06 01:33:22 -0400150int __gameport_register_driver(struct gameport_driver *drv,
151 struct module *owner, const char *mod_name);
Dmitry Torokhov8c4b3c22008-06-06 01:33:51 -0400152static inline int __must_check gameport_register_driver(struct gameport_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Dmitry Torokhov6902c0b2008-06-06 01:33:22 -0400154 return __gameport_register_driver(drv, THIS_MODULE, KBUILD_MODNAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
157void gameport_unregister_driver(struct gameport_driver *drv);
158
David Woodhouse25478bb2006-04-25 13:59:30 +0100159#endif /* __KERNEL__ */
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161#define GAMEPORT_MODE_DISABLED 0
162#define GAMEPORT_MODE_RAW 1
163#define GAMEPORT_MODE_COOKED 2
164
165#define GAMEPORT_ID_VENDOR_ANALOG 0x0001
166#define GAMEPORT_ID_VENDOR_MADCATZ 0x0002
167#define GAMEPORT_ID_VENDOR_LOGITECH 0x0003
168#define GAMEPORT_ID_VENDOR_CREATIVE 0x0004
169#define GAMEPORT_ID_VENDOR_GENIUS 0x0005
170#define GAMEPORT_ID_VENDOR_INTERACT 0x0006
171#define GAMEPORT_ID_VENDOR_MICROSOFT 0x0007
172#define GAMEPORT_ID_VENDOR_THRUSTMASTER 0x0008
173#define GAMEPORT_ID_VENDOR_GRAVIS 0x0009
174#define GAMEPORT_ID_VENDOR_GUILLEMOT 0x000a
175
David Woodhouse25478bb2006-04-25 13:59:30 +0100176#ifdef __KERNEL__
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178static inline void gameport_trigger(struct gameport *gameport)
179{
180 if (gameport->trigger)
181 gameport->trigger(gameport);
182 else
183 outb(0xff, gameport->io);
184}
185
186static inline unsigned char gameport_read(struct gameport *gameport)
187{
188 if (gameport->read)
189 return gameport->read(gameport);
190 else
191 return inb(gameport->io);
192}
193
194static inline int gameport_cooked_read(struct gameport *gameport, int *axes, int *buttons)
195{
196 if (gameport->cooked_read)
197 return gameport->cooked_read(gameport, axes, buttons);
198 else
199 return -1;
200}
201
202static inline int gameport_calibrate(struct gameport *gameport, int *axes, int *max)
203{
204 if (gameport->calibrate)
205 return gameport->calibrate(gameport, axes, max);
206 else
207 return -1;
208}
209
210static inline int gameport_time(struct gameport *gameport, int time)
211{
212 return (time * gameport->speed) / 1000;
213}
214
215static inline void gameport_set_poll_handler(struct gameport *gameport, void (*handler)(struct gameport *))
216{
217 gameport->poll_handler = handler;
218}
219
220static inline void gameport_set_poll_interval(struct gameport *gameport, unsigned int msecs)
221{
222 gameport->poll_interval = msecs;
223}
224
225void gameport_start_polling(struct gameport *gameport);
226void gameport_stop_polling(struct gameport *gameport);
227
David Woodhouse25478bb2006-04-25 13:59:30 +0100228#endif /* __KERNEL__ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229#endif