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