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