blob: 3e1b9b285258440037342adb50aef6884a952318 [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
12#include <asm/io.h>
13#include <linux/list.h>
Arjan van de Ven286295e2006-02-19 00:22:03 -050014#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/device.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080016#include <linux/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18struct gameport {
19
20 void *port_data; /* Private pointer for gameport drivers */
21 char name[32];
22 char phys[32];
23
24 int io;
25 int speed;
26 int fuzz;
27
28 void (*trigger)(struct gameport *);
29 unsigned char (*read)(struct gameport *);
30 int (*cooked_read)(struct gameport *, int *, int *);
31 int (*calibrate)(struct gameport *, int *, int *);
32 int (*open)(struct gameport *, int);
33 void (*close)(struct gameport *);
34
35 struct timer_list poll_timer;
36 unsigned int poll_interval; /* in msecs */
37 spinlock_t timer_lock;
38 unsigned int poll_cnt;
39 void (*poll_handler)(struct gameport *);
40
41 struct gameport *parent, *child;
42
43 struct gameport_driver *drv;
Arjan van de Ven286295e2006-02-19 00:22:03 -050044 struct mutex drv_mutex; /* protects serio->drv so attributes can pin driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46 struct device dev;
47 unsigned int registered; /* port has been fully registered with driver core */
48
49 struct list_head node;
50};
51#define to_gameport_port(d) container_of(d, struct gameport, dev)
52
53struct gameport_driver {
54
55 void *private;
56 char *description;
57
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
64 unsigned int ignore;
65};
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);
70void gameport_rescan(struct gameport *gameport);
71
Adrian Bunk668d1e62005-05-28 02:11:12 -050072#if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074void __gameport_register_port(struct gameport *gameport, struct module *owner);
75static inline void gameport_register_port(struct gameport *gameport)
76{
77 __gameport_register_port(gameport, THIS_MODULE);
78}
79
80void gameport_unregister_port(struct gameport *gameport);
81
Adrian Bunk668d1e62005-05-28 02:11:12 -050082void gameport_set_phys(struct gameport *gameport, const char *fmt, ...)
83 __attribute__ ((format (printf, 2, 3)));
84
85#else
86
87static inline void gameport_register_port(struct gameport *gameport)
88{
89 return;
90}
91
92static inline void gameport_unregister_port(struct gameport *gameport)
93{
94 return;
95}
96
97static inline void gameport_set_phys(struct gameport *gameport,
98 const char *fmt, ...)
99{
100 return;
101}
102
103#endif
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105static inline struct gameport *gameport_allocate_port(void)
106{
107 struct gameport *gameport = kcalloc(1, sizeof(struct gameport), GFP_KERNEL);
108
109 return gameport;
110}
111
112static inline void gameport_free_port(struct gameport *gameport)
113{
114 kfree(gameport);
115}
116
117static inline void gameport_set_name(struct gameport *gameport, const char *name)
118{
119 strlcpy(gameport->name, name, sizeof(gameport->name));
120}
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122/*
123 * Use the following fucntions to manipulate gameport's per-port
124 * driver-specific data.
125 */
126static inline void *gameport_get_drvdata(struct gameport *gameport)
127{
128 return dev_get_drvdata(&gameport->dev);
129}
130
131static inline void gameport_set_drvdata(struct gameport *gameport, void *data)
132{
133 dev_set_drvdata(&gameport->dev, data);
134}
135
136/*
137 * Use the following fucntions to pin gameport's driver in process context
138 */
139static inline int gameport_pin_driver(struct gameport *gameport)
140{
Arjan van de Ven286295e2006-02-19 00:22:03 -0500141 return mutex_lock_interruptible(&gameport->drv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
144static inline void gameport_unpin_driver(struct gameport *gameport)
145{
Arjan van de Ven286295e2006-02-19 00:22:03 -0500146 mutex_unlock(&gameport->drv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
149void __gameport_register_driver(struct gameport_driver *drv, struct module *owner);
150static inline void gameport_register_driver(struct gameport_driver *drv)
151{
152 __gameport_register_driver(drv, THIS_MODULE);
153}
154
155void gameport_unregister_driver(struct gameport_driver *drv);
156
157#define GAMEPORT_MODE_DISABLED 0
158#define GAMEPORT_MODE_RAW 1
159#define GAMEPORT_MODE_COOKED 2
160
161#define GAMEPORT_ID_VENDOR_ANALOG 0x0001
162#define GAMEPORT_ID_VENDOR_MADCATZ 0x0002
163#define GAMEPORT_ID_VENDOR_LOGITECH 0x0003
164#define GAMEPORT_ID_VENDOR_CREATIVE 0x0004
165#define GAMEPORT_ID_VENDOR_GENIUS 0x0005
166#define GAMEPORT_ID_VENDOR_INTERACT 0x0006
167#define GAMEPORT_ID_VENDOR_MICROSOFT 0x0007
168#define GAMEPORT_ID_VENDOR_THRUSTMASTER 0x0008
169#define GAMEPORT_ID_VENDOR_GRAVIS 0x0009
170#define GAMEPORT_ID_VENDOR_GUILLEMOT 0x000a
171
172static inline void gameport_trigger(struct gameport *gameport)
173{
174 if (gameport->trigger)
175 gameport->trigger(gameport);
176 else
177 outb(0xff, gameport->io);
178}
179
180static inline unsigned char gameport_read(struct gameport *gameport)
181{
182 if (gameport->read)
183 return gameport->read(gameport);
184 else
185 return inb(gameport->io);
186}
187
188static inline int gameport_cooked_read(struct gameport *gameport, int *axes, int *buttons)
189{
190 if (gameport->cooked_read)
191 return gameport->cooked_read(gameport, axes, buttons);
192 else
193 return -1;
194}
195
196static inline int gameport_calibrate(struct gameport *gameport, int *axes, int *max)
197{
198 if (gameport->calibrate)
199 return gameport->calibrate(gameport, axes, max);
200 else
201 return -1;
202}
203
204static inline int gameport_time(struct gameport *gameport, int time)
205{
206 return (time * gameport->speed) / 1000;
207}
208
209static inline void gameport_set_poll_handler(struct gameport *gameport, void (*handler)(struct gameport *))
210{
211 gameport->poll_handler = handler;
212}
213
214static inline void gameport_set_poll_interval(struct gameport *gameport, unsigned int msecs)
215{
216 gameport->poll_interval = msecs;
217}
218
219void gameport_start_polling(struct gameport *gameport);
220void gameport_stop_polling(struct gameport *gameport);
221
222#endif