Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #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 Woodhouse | 25478bb | 2006-04-25 13:59:30 +0100 | [diff] [blame] | 12 | #ifdef __KERNEL__ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <asm/io.h> |
Dmitry Torokhov | 7e044e0 | 2009-05-09 16:08:05 -0700 | [diff] [blame] | 14 | #include <linux/types.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/list.h> |
Arjan van de Ven | 286295e | 2006-02-19 00:22:03 -0500 | [diff] [blame] | 16 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/device.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 18 | #include <linux/timer.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 19 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
| 21 | struct 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 Ven | 286295e | 2006-02-19 00:22:03 -0500 | [diff] [blame] | 47 | struct mutex drv_mutex; /* protects serio->drv so attributes can pin driver */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
| 49 | struct device dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
| 51 | struct list_head node; |
| 52 | }; |
| 53 | #define to_gameport_port(d) container_of(d, struct gameport, dev) |
| 54 | |
| 55 | struct gameport_driver { |
Dmitry Torokhov | 37b0bb1 | 2010-09-13 23:53:55 -0700 | [diff] [blame] | 56 | const char *description; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Dmitry Torokhov | 7e044e0 | 2009-05-09 16:08:05 -0700 | [diff] [blame] | 64 | bool ignore; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | }; |
| 66 | #define to_gameport_driver(d) container_of(d, struct gameport_driver, driver) |
| 67 | |
| 68 | int gameport_open(struct gameport *gameport, struct gameport_driver *drv, int mode); |
| 69 | void gameport_close(struct gameport *gameport); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
Adrian Bunk | 668d1e6 | 2005-05-28 02:11:12 -0500 | [diff] [blame] | 71 | #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE)) |
| 72 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | void __gameport_register_port(struct gameport *gameport, struct module *owner); |
Paul Gortmaker | eb5589a | 2011-05-27 09:02:11 -0400 | [diff] [blame] | 74 | /* 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
| 78 | void gameport_unregister_port(struct gameport *gameport); |
| 79 | |
Joe Perches | b9075fa | 2011-10-31 17:11:33 -0700 | [diff] [blame] | 80 | __printf(2, 3) |
| 81 | void gameport_set_phys(struct gameport *gameport, const char *fmt, ...); |
Adrian Bunk | 668d1e6 | 2005-05-28 02:11:12 -0500 | [diff] [blame] | 82 | |
| 83 | #else |
| 84 | |
| 85 | static inline void gameport_register_port(struct gameport *gameport) |
| 86 | { |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | static inline void gameport_unregister_port(struct gameport *gameport) |
| 91 | { |
| 92 | return; |
| 93 | } |
| 94 | |
Joe Perches | b9075fa | 2011-10-31 17:11:33 -0700 | [diff] [blame] | 95 | static inline __printf(2, 3) |
| 96 | void gameport_set_phys(struct gameport *gameport, const char *fmt, ...) |
Adrian Bunk | 668d1e6 | 2005-05-28 02:11:12 -0500 | [diff] [blame] | 97 | { |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | #endif |
| 102 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | static inline struct gameport *gameport_allocate_port(void) |
| 104 | { |
Robert P. J. Day | cd86128 | 2006-12-13 00:34:52 -0800 | [diff] [blame] | 105 | struct gameport *gameport = kzalloc(sizeof(struct gameport), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
| 107 | return gameport; |
| 108 | } |
| 109 | |
| 110 | static inline void gameport_free_port(struct gameport *gameport) |
| 111 | { |
| 112 | kfree(gameport); |
| 113 | } |
| 114 | |
| 115 | static inline void gameport_set_name(struct gameport *gameport, const char *name) |
| 116 | { |
| 117 | strlcpy(gameport->name, name, sizeof(gameport->name)); |
| 118 | } |
| 119 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | /* |
Akinobu Mita | 0b28002 | 2006-03-26 01:38:58 -0800 | [diff] [blame] | 121 | * Use the following functions to manipulate gameport's per-port |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | * driver-specific data. |
| 123 | */ |
| 124 | static inline void *gameport_get_drvdata(struct gameport *gameport) |
| 125 | { |
| 126 | return dev_get_drvdata(&gameport->dev); |
| 127 | } |
| 128 | |
| 129 | static inline void gameport_set_drvdata(struct gameport *gameport, void *data) |
| 130 | { |
| 131 | dev_set_drvdata(&gameport->dev, data); |
| 132 | } |
| 133 | |
| 134 | /* |
Akinobu Mita | 0b28002 | 2006-03-26 01:38:58 -0800 | [diff] [blame] | 135 | * Use the following functions to pin gameport's driver in process context |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | */ |
| 137 | static inline int gameport_pin_driver(struct gameport *gameport) |
| 138 | { |
Arjan van de Ven | 286295e | 2006-02-19 00:22:03 -0500 | [diff] [blame] | 139 | return mutex_lock_interruptible(&gameport->drv_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | static inline void gameport_unpin_driver(struct gameport *gameport) |
| 143 | { |
Arjan van de Ven | 286295e | 2006-02-19 00:22:03 -0500 | [diff] [blame] | 144 | mutex_unlock(&gameport->drv_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Paul Gortmaker | eb5589a | 2011-05-27 09:02:11 -0400 | [diff] [blame] | 147 | int __must_check __gameport_register_driver(struct gameport_driver *drv, |
Dmitry Torokhov | 6902c0b | 2008-06-06 01:33:22 -0400 | [diff] [blame] | 148 | struct module *owner, const char *mod_name); |
Paul Gortmaker | eb5589a | 2011-05-27 09:02:11 -0400 | [diff] [blame] | 149 | |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | |
| 154 | void gameport_unregister_driver(struct gameport_driver *drv); |
| 155 | |
Axel Lin | 45b2604 | 2012-04-03 23:51:08 -0700 | [diff] [blame] | 156 | /** |
| 157 | * module_gameport_driver() - Helper macro for registering a gameport driver |
| 158 | * @__gameport_driver: gameport_driver struct |
| 159 | * |
| 160 | * Helper macro for gameport drivers which do not do anything special in |
| 161 | * module init/exit. This eliminates a lot of boilerplate. Each module may |
| 162 | * only use this macro once, and calling it replaces module_init() and |
| 163 | * module_exit(). |
| 164 | */ |
| 165 | #define module_gameport_driver(__gameport_driver) \ |
| 166 | module_driver(__gameport_driver, gameport_register_driver, \ |
| 167 | gameport_unregister_driver) |
| 168 | |
David Woodhouse | 25478bb | 2006-04-25 13:59:30 +0100 | [diff] [blame] | 169 | #endif /* __KERNEL__ */ |
| 170 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | #define GAMEPORT_MODE_DISABLED 0 |
| 172 | #define GAMEPORT_MODE_RAW 1 |
| 173 | #define GAMEPORT_MODE_COOKED 2 |
| 174 | |
| 175 | #define GAMEPORT_ID_VENDOR_ANALOG 0x0001 |
| 176 | #define GAMEPORT_ID_VENDOR_MADCATZ 0x0002 |
| 177 | #define GAMEPORT_ID_VENDOR_LOGITECH 0x0003 |
| 178 | #define GAMEPORT_ID_VENDOR_CREATIVE 0x0004 |
| 179 | #define GAMEPORT_ID_VENDOR_GENIUS 0x0005 |
| 180 | #define GAMEPORT_ID_VENDOR_INTERACT 0x0006 |
| 181 | #define GAMEPORT_ID_VENDOR_MICROSOFT 0x0007 |
| 182 | #define GAMEPORT_ID_VENDOR_THRUSTMASTER 0x0008 |
| 183 | #define GAMEPORT_ID_VENDOR_GRAVIS 0x0009 |
| 184 | #define GAMEPORT_ID_VENDOR_GUILLEMOT 0x000a |
| 185 | |
David Woodhouse | 25478bb | 2006-04-25 13:59:30 +0100 | [diff] [blame] | 186 | #ifdef __KERNEL__ |
| 187 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | static inline void gameport_trigger(struct gameport *gameport) |
| 189 | { |
| 190 | if (gameport->trigger) |
| 191 | gameport->trigger(gameport); |
| 192 | else |
| 193 | outb(0xff, gameport->io); |
| 194 | } |
| 195 | |
| 196 | static inline unsigned char gameport_read(struct gameport *gameport) |
| 197 | { |
| 198 | if (gameport->read) |
| 199 | return gameport->read(gameport); |
| 200 | else |
| 201 | return inb(gameport->io); |
| 202 | } |
| 203 | |
| 204 | static inline int gameport_cooked_read(struct gameport *gameport, int *axes, int *buttons) |
| 205 | { |
| 206 | if (gameport->cooked_read) |
| 207 | return gameport->cooked_read(gameport, axes, buttons); |
| 208 | else |
| 209 | return -1; |
| 210 | } |
| 211 | |
| 212 | static inline int gameport_calibrate(struct gameport *gameport, int *axes, int *max) |
| 213 | { |
| 214 | if (gameport->calibrate) |
| 215 | return gameport->calibrate(gameport, axes, max); |
| 216 | else |
| 217 | return -1; |
| 218 | } |
| 219 | |
| 220 | static inline int gameport_time(struct gameport *gameport, int time) |
| 221 | { |
| 222 | return (time * gameport->speed) / 1000; |
| 223 | } |
| 224 | |
| 225 | static inline void gameport_set_poll_handler(struct gameport *gameport, void (*handler)(struct gameport *)) |
| 226 | { |
| 227 | gameport->poll_handler = handler; |
| 228 | } |
| 229 | |
| 230 | static inline void gameport_set_poll_interval(struct gameport *gameport, unsigned int msecs) |
| 231 | { |
| 232 | gameport->poll_interval = msecs; |
| 233 | } |
| 234 | |
| 235 | void gameport_start_polling(struct gameport *gameport); |
| 236 | void gameport_stop_polling(struct gameport *gameport); |
| 237 | |
David Woodhouse | 25478bb | 2006-04-25 13:59:30 +0100 | [diff] [blame] | 238 | #endif /* __KERNEL__ */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | #endif |