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> |
| 14 | #include <linux/list.h> |
Arjan van de Ven | 286295e | 2006-02-19 00:22:03 -0500 | [diff] [blame] | 15 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/device.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 17 | #include <linux/timer.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
| 19 | struct gameport { |
| 20 | |
| 21 | void *port_data; /* Private pointer for gameport drivers */ |
| 22 | char name[32]; |
| 23 | char phys[32]; |
| 24 | |
| 25 | int io; |
| 26 | int speed; |
| 27 | int fuzz; |
| 28 | |
| 29 | void (*trigger)(struct gameport *); |
| 30 | unsigned char (*read)(struct gameport *); |
| 31 | int (*cooked_read)(struct gameport *, int *, int *); |
| 32 | int (*calibrate)(struct gameport *, int *, int *); |
| 33 | int (*open)(struct gameport *, int); |
| 34 | void (*close)(struct gameport *); |
| 35 | |
| 36 | struct timer_list poll_timer; |
| 37 | unsigned int poll_interval; /* in msecs */ |
| 38 | spinlock_t timer_lock; |
| 39 | unsigned int poll_cnt; |
| 40 | void (*poll_handler)(struct gameport *); |
| 41 | |
| 42 | struct gameport *parent, *child; |
| 43 | |
| 44 | struct gameport_driver *drv; |
Arjan van de Ven | 286295e | 2006-02-19 00:22:03 -0500 | [diff] [blame] | 45 | struct mutex drv_mutex; /* protects serio->drv so attributes can pin driver */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
| 47 | struct device dev; |
| 48 | unsigned int registered; /* port has been fully registered with driver core */ |
| 49 | |
| 50 | struct list_head node; |
| 51 | }; |
| 52 | #define to_gameport_port(d) container_of(d, struct gameport, dev) |
| 53 | |
| 54 | struct gameport_driver { |
| 55 | |
| 56 | void *private; |
| 57 | char *description; |
| 58 | |
| 59 | int (*connect)(struct gameport *, struct gameport_driver *drv); |
| 60 | int (*reconnect)(struct gameport *); |
| 61 | void (*disconnect)(struct gameport *); |
| 62 | |
| 63 | struct device_driver driver; |
| 64 | |
| 65 | unsigned int ignore; |
| 66 | }; |
| 67 | #define to_gameport_driver(d) container_of(d, struct gameport_driver, driver) |
| 68 | |
| 69 | int gameport_open(struct gameport *gameport, struct gameport_driver *drv, int mode); |
| 70 | void gameport_close(struct gameport *gameport); |
| 71 | void gameport_rescan(struct gameport *gameport); |
| 72 | |
Adrian Bunk | 668d1e6 | 2005-05-28 02:11:12 -0500 | [diff] [blame] | 73 | #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE)) |
| 74 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | void __gameport_register_port(struct gameport *gameport, struct module *owner); |
| 76 | static inline void gameport_register_port(struct gameport *gameport) |
| 77 | { |
| 78 | __gameport_register_port(gameport, THIS_MODULE); |
| 79 | } |
| 80 | |
| 81 | void gameport_unregister_port(struct gameport *gameport); |
| 82 | |
Adrian Bunk | 668d1e6 | 2005-05-28 02:11:12 -0500 | [diff] [blame] | 83 | void gameport_set_phys(struct gameport *gameport, const char *fmt, ...) |
| 84 | __attribute__ ((format (printf, 2, 3))); |
| 85 | |
| 86 | #else |
| 87 | |
| 88 | static inline void gameport_register_port(struct gameport *gameport) |
| 89 | { |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | static inline void gameport_unregister_port(struct gameport *gameport) |
| 94 | { |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | static inline void gameport_set_phys(struct gameport *gameport, |
| 99 | const char *fmt, ...) |
| 100 | { |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | #endif |
| 105 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | static inline struct gameport *gameport_allocate_port(void) |
| 107 | { |
Robert P. J. Day | cd86128 | 2006-12-13 00:34:52 -0800 | [diff] [blame] | 108 | struct gameport *gameport = kzalloc(sizeof(struct gameport), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
| 110 | return gameport; |
| 111 | } |
| 112 | |
| 113 | static inline void gameport_free_port(struct gameport *gameport) |
| 114 | { |
| 115 | kfree(gameport); |
| 116 | } |
| 117 | |
| 118 | static inline void gameport_set_name(struct gameport *gameport, const char *name) |
| 119 | { |
| 120 | strlcpy(gameport->name, name, sizeof(gameport->name)); |
| 121 | } |
| 122 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | /* |
Akinobu Mita | 0b28002 | 2006-03-26 01:38:58 -0800 | [diff] [blame] | 124 | * Use the following functions to manipulate gameport's per-port |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | * driver-specific data. |
| 126 | */ |
| 127 | static inline void *gameport_get_drvdata(struct gameport *gameport) |
| 128 | { |
| 129 | return dev_get_drvdata(&gameport->dev); |
| 130 | } |
| 131 | |
| 132 | static inline void gameport_set_drvdata(struct gameport *gameport, void *data) |
| 133 | { |
| 134 | dev_set_drvdata(&gameport->dev, data); |
| 135 | } |
| 136 | |
| 137 | /* |
Akinobu Mita | 0b28002 | 2006-03-26 01:38:58 -0800 | [diff] [blame] | 138 | * Use the following functions to pin gameport's driver in process context |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | */ |
| 140 | static inline int gameport_pin_driver(struct gameport *gameport) |
| 141 | { |
Arjan van de Ven | 286295e | 2006-02-19 00:22:03 -0500 | [diff] [blame] | 142 | return mutex_lock_interruptible(&gameport->drv_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | static inline void gameport_unpin_driver(struct gameport *gameport) |
| 146 | { |
Arjan van de Ven | 286295e | 2006-02-19 00:22:03 -0500 | [diff] [blame] | 147 | mutex_unlock(&gameport->drv_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | void __gameport_register_driver(struct gameport_driver *drv, struct module *owner); |
| 151 | static inline void gameport_register_driver(struct gameport_driver *drv) |
| 152 | { |
| 153 | __gameport_register_driver(drv, THIS_MODULE); |
| 154 | } |
| 155 | |
| 156 | void gameport_unregister_driver(struct gameport_driver *drv); |
| 157 | |
David Woodhouse | 25478bb | 2006-04-25 13:59:30 +0100 | [diff] [blame] | 158 | #endif /* __KERNEL__ */ |
| 159 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | #define GAMEPORT_MODE_DISABLED 0 |
| 161 | #define GAMEPORT_MODE_RAW 1 |
| 162 | #define GAMEPORT_MODE_COOKED 2 |
| 163 | |
| 164 | #define GAMEPORT_ID_VENDOR_ANALOG 0x0001 |
| 165 | #define GAMEPORT_ID_VENDOR_MADCATZ 0x0002 |
| 166 | #define GAMEPORT_ID_VENDOR_LOGITECH 0x0003 |
| 167 | #define GAMEPORT_ID_VENDOR_CREATIVE 0x0004 |
| 168 | #define GAMEPORT_ID_VENDOR_GENIUS 0x0005 |
| 169 | #define GAMEPORT_ID_VENDOR_INTERACT 0x0006 |
| 170 | #define GAMEPORT_ID_VENDOR_MICROSOFT 0x0007 |
| 171 | #define GAMEPORT_ID_VENDOR_THRUSTMASTER 0x0008 |
| 172 | #define GAMEPORT_ID_VENDOR_GRAVIS 0x0009 |
| 173 | #define GAMEPORT_ID_VENDOR_GUILLEMOT 0x000a |
| 174 | |
David Woodhouse | 25478bb | 2006-04-25 13:59:30 +0100 | [diff] [blame] | 175 | #ifdef __KERNEL__ |
| 176 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | static inline void gameport_trigger(struct gameport *gameport) |
| 178 | { |
| 179 | if (gameport->trigger) |
| 180 | gameport->trigger(gameport); |
| 181 | else |
| 182 | outb(0xff, gameport->io); |
| 183 | } |
| 184 | |
| 185 | static inline unsigned char gameport_read(struct gameport *gameport) |
| 186 | { |
| 187 | if (gameport->read) |
| 188 | return gameport->read(gameport); |
| 189 | else |
| 190 | return inb(gameport->io); |
| 191 | } |
| 192 | |
| 193 | static inline int gameport_cooked_read(struct gameport *gameport, int *axes, int *buttons) |
| 194 | { |
| 195 | if (gameport->cooked_read) |
| 196 | return gameport->cooked_read(gameport, axes, buttons); |
| 197 | else |
| 198 | return -1; |
| 199 | } |
| 200 | |
| 201 | static inline int gameport_calibrate(struct gameport *gameport, int *axes, int *max) |
| 202 | { |
| 203 | if (gameport->calibrate) |
| 204 | return gameport->calibrate(gameport, axes, max); |
| 205 | else |
| 206 | return -1; |
| 207 | } |
| 208 | |
| 209 | static inline int gameport_time(struct gameport *gameport, int time) |
| 210 | { |
| 211 | return (time * gameport->speed) / 1000; |
| 212 | } |
| 213 | |
| 214 | static inline void gameport_set_poll_handler(struct gameport *gameport, void (*handler)(struct gameport *)) |
| 215 | { |
| 216 | gameport->poll_handler = handler; |
| 217 | } |
| 218 | |
| 219 | static inline void gameport_set_poll_interval(struct gameport *gameport, unsigned int msecs) |
| 220 | { |
| 221 | gameport->poll_interval = msecs; |
| 222 | } |
| 223 | |
| 224 | void gameport_start_polling(struct gameport *gameport); |
| 225 | void gameport_stop_polling(struct gameport *gameport); |
| 226 | |
David Woodhouse | 25478bb | 2006-04-25 13:59:30 +0100 | [diff] [blame] | 227 | #endif /* __KERNEL__ */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | #endif |