blob: bb7de09e8d57a37fcf9e3378dfd7498eb08af6cb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) 1999-2002 Vojtech Pavlik
3 *
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.
7 */
David Howells607ca462012-10-13 10:46:48 +01008#ifndef _GAMEPORT_H
9#define _GAMEPORT_H
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
11#include <asm/io.h>
Dmitry Torokhov7e044e02009-05-09 16:08:05 -070012#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
David Howells607ca462012-10-13 10:46:48 +010018#include <uapi/linux/gameport.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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50 struct list_head node;
51};
52#define to_gameport_port(d) container_of(d, struct gameport, dev)
53
54struct gameport_driver {
Dmitry Torokhov37b0bb12010-09-13 23:53:55 -070055 const char *description;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57 int (*connect)(struct gameport *, struct gameport_driver *drv);
58 int (*reconnect)(struct gameport *);
59 void (*disconnect)(struct gameport *);
60
61 struct device_driver driver;
62
Dmitry Torokhov7e044e02009-05-09 16:08:05 -070063 bool ignore;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064};
65#define to_gameport_driver(d) container_of(d, struct gameport_driver, driver)
66
67int gameport_open(struct gameport *gameport, struct gameport_driver *drv, int mode);
68void gameport_close(struct gameport *gameport);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Adrian Bunk668d1e62005-05-28 02:11:12 -050070#if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072void __gameport_register_port(struct gameport *gameport, struct module *owner);
Paul Gortmakereb5589a2011-05-27 09:02:11 -040073/* use a define to avoid include chaining to get THIS_MODULE */
74#define gameport_register_port(gameport) \
75 __gameport_register_port(gameport, THIS_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77void gameport_unregister_port(struct gameport *gameport);
78
Joe Perchesb9075fa2011-10-31 17:11:33 -070079__printf(2, 3)
80void gameport_set_phys(struct gameport *gameport, const char *fmt, ...);
Adrian Bunk668d1e62005-05-28 02:11:12 -050081
82#else
83
84static inline void gameport_register_port(struct gameport *gameport)
85{
86 return;
87}
88
89static inline void gameport_unregister_port(struct gameport *gameport)
90{
91 return;
92}
93
Joe Perchesb9075fa2011-10-31 17:11:33 -070094static inline __printf(2, 3)
95void gameport_set_phys(struct gameport *gameport, const char *fmt, ...)
Adrian Bunk668d1e62005-05-28 02:11:12 -050096{
97 return;
98}
99
100#endif
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102static inline struct gameport *gameport_allocate_port(void)
103{
Robert P. J. Daycd861282006-12-13 00:34:52 -0800104 struct gameport *gameport = kzalloc(sizeof(struct gameport), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106 return gameport;
107}
108
109static inline void gameport_free_port(struct gameport *gameport)
110{
111 kfree(gameport);
112}
113
114static inline void gameport_set_name(struct gameport *gameport, const char *name)
115{
116 strlcpy(gameport->name, name, sizeof(gameport->name));
117}
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119/*
Akinobu Mita0b280022006-03-26 01:38:58 -0800120 * Use the following functions to manipulate gameport's per-port
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 * driver-specific data.
122 */
123static inline void *gameport_get_drvdata(struct gameport *gameport)
124{
125 return dev_get_drvdata(&gameport->dev);
126}
127
128static inline void gameport_set_drvdata(struct gameport *gameport, void *data)
129{
130 dev_set_drvdata(&gameport->dev, data);
131}
132
133/*
Akinobu Mita0b280022006-03-26 01:38:58 -0800134 * Use the following functions to pin gameport's driver in process context
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 */
136static inline int gameport_pin_driver(struct gameport *gameport)
137{
Arjan van de Ven286295e2006-02-19 00:22:03 -0500138 return mutex_lock_interruptible(&gameport->drv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
140
141static inline void gameport_unpin_driver(struct gameport *gameport)
142{
Arjan van de Ven286295e2006-02-19 00:22:03 -0500143 mutex_unlock(&gameport->drv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
Paul Gortmakereb5589a2011-05-27 09:02:11 -0400146int __must_check __gameport_register_driver(struct gameport_driver *drv,
Dmitry Torokhov6902c0b2008-06-06 01:33:22 -0400147 struct module *owner, const char *mod_name);
Paul Gortmakereb5589a2011-05-27 09:02:11 -0400148
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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153void gameport_unregister_driver(struct gameport_driver *drv);
154
Axel Lin45b26042012-04-03 23:51:08 -0700155/**
156 * module_gameport_driver() - Helper macro for registering a gameport driver
157 * @__gameport_driver: gameport_driver struct
158 *
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
162 * module_exit().
163 */
164#define module_gameport_driver(__gameport_driver) \
165 module_driver(__gameport_driver, gameport_register_driver, \
166 gameport_unregister_driver)
167
David Woodhouse25478bb2006-04-25 13:59:30 +0100168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169static inline void gameport_trigger(struct gameport *gameport)
170{
171 if (gameport->trigger)
172 gameport->trigger(gameport);
173 else
174 outb(0xff, gameport->io);
175}
176
177static inline unsigned char gameport_read(struct gameport *gameport)
178{
179 if (gameport->read)
180 return gameport->read(gameport);
181 else
182 return inb(gameport->io);
183}
184
185static inline int gameport_cooked_read(struct gameport *gameport, int *axes, int *buttons)
186{
187 if (gameport->cooked_read)
188 return gameport->cooked_read(gameport, axes, buttons);
189 else
190 return -1;
191}
192
193static inline int gameport_calibrate(struct gameport *gameport, int *axes, int *max)
194{
195 if (gameport->calibrate)
196 return gameport->calibrate(gameport, axes, max);
197 else
198 return -1;
199}
200
201static inline int gameport_time(struct gameport *gameport, int time)
202{
203 return (time * gameport->speed) / 1000;
204}
205
206static inline void gameport_set_poll_handler(struct gameport *gameport, void (*handler)(struct gameport *))
207{
208 gameport->poll_handler = handler;
209}
210
211static inline void gameport_set_poll_interval(struct gameport *gameport, unsigned int msecs)
212{
213 gameport->poll_interval = msecs;
214}
215
216void gameport_start_polling(struct gameport *gameport);
217void gameport_stop_polling(struct gameport *gameport);
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219#endif