gameport.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #ifndef _GAMEPORT_H
  2. #define _GAMEPORT_H
  3. /*
  4. * Copyright (c) 1999-2002 Vojtech Pavlik
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published by
  8. * the Free Software Foundation.
  9. */
  10. #ifdef __KERNEL__
  11. #include <asm/io.h>
  12. #include <linux/types.h>
  13. #include <linux/list.h>
  14. #include <linux/mutex.h>
  15. #include <linux/device.h>
  16. #include <linux/timer.h>
  17. #include <linux/slab.h>
  18. struct gameport {
  19. void *port_data; /* Private pointer for gameport drivers */
  20. char name[32];
  21. char phys[32];
  22. int io;
  23. int speed;
  24. int fuzz;
  25. void (*trigger)(struct gameport *);
  26. unsigned char (*read)(struct gameport *);
  27. int (*cooked_read)(struct gameport *, int *, int *);
  28. int (*calibrate)(struct gameport *, int *, int *);
  29. int (*open)(struct gameport *, int);
  30. void (*close)(struct gameport *);
  31. struct timer_list poll_timer;
  32. unsigned int poll_interval; /* in msecs */
  33. spinlock_t timer_lock;
  34. unsigned int poll_cnt;
  35. void (*poll_handler)(struct gameport *);
  36. struct gameport *parent, *child;
  37. struct gameport_driver *drv;
  38. struct mutex drv_mutex; /* protects serio->drv so attributes can pin driver */
  39. struct device dev;
  40. struct list_head node;
  41. };
  42. #define to_gameport_port(d) container_of(d, struct gameport, dev)
  43. struct gameport_driver {
  44. const char *description;
  45. int (*connect)(struct gameport *, struct gameport_driver *drv);
  46. int (*reconnect)(struct gameport *);
  47. void (*disconnect)(struct gameport *);
  48. struct device_driver driver;
  49. bool ignore;
  50. };
  51. #define to_gameport_driver(d) container_of(d, struct gameport_driver, driver)
  52. int gameport_open(struct gameport *gameport, struct gameport_driver *drv, int mode);
  53. void gameport_close(struct gameport *gameport);
  54. #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
  55. void __gameport_register_port(struct gameport *gameport, struct module *owner);
  56. /* use a define to avoid include chaining to get THIS_MODULE */
  57. #define gameport_register_port(gameport) \
  58. __gameport_register_port(gameport, THIS_MODULE)
  59. void gameport_unregister_port(struct gameport *gameport);
  60. __printf(2, 3)
  61. void gameport_set_phys(struct gameport *gameport, const char *fmt, ...);
  62. #else
  63. static inline void gameport_register_port(struct gameport *gameport)
  64. {
  65. return;
  66. }
  67. static inline void gameport_unregister_port(struct gameport *gameport)
  68. {
  69. return;
  70. }
  71. static inline __printf(2, 3)
  72. void gameport_set_phys(struct gameport *gameport, const char *fmt, ...)
  73. {
  74. return;
  75. }
  76. #endif
  77. static inline struct gameport *gameport_allocate_port(void)
  78. {
  79. struct gameport *gameport = kzalloc(sizeof(struct gameport), GFP_KERNEL);
  80. return gameport;
  81. }
  82. static inline void gameport_free_port(struct gameport *gameport)
  83. {
  84. kfree(gameport);
  85. }
  86. static inline void gameport_set_name(struct gameport *gameport, const char *name)
  87. {
  88. strlcpy(gameport->name, name, sizeof(gameport->name));
  89. }
  90. /*
  91. * Use the following functions to manipulate gameport's per-port
  92. * driver-specific data.
  93. */
  94. static inline void *gameport_get_drvdata(struct gameport *gameport)
  95. {
  96. return dev_get_drvdata(&gameport->dev);
  97. }
  98. static inline void gameport_set_drvdata(struct gameport *gameport, void *data)
  99. {
  100. dev_set_drvdata(&gameport->dev, data);
  101. }
  102. /*
  103. * Use the following functions to pin gameport's driver in process context
  104. */
  105. static inline int gameport_pin_driver(struct gameport *gameport)
  106. {
  107. return mutex_lock_interruptible(&gameport->drv_mutex);
  108. }
  109. static inline void gameport_unpin_driver(struct gameport *gameport)
  110. {
  111. mutex_unlock(&gameport->drv_mutex);
  112. }
  113. int __must_check __gameport_register_driver(struct gameport_driver *drv,
  114. struct module *owner, const char *mod_name);
  115. /* use a define to avoid include chaining to get THIS_MODULE & friends */
  116. #define gameport_register_driver(drv) \
  117. __gameport_register_driver(drv, THIS_MODULE, KBUILD_MODNAME)
  118. void gameport_unregister_driver(struct gameport_driver *drv);
  119. /**
  120. * module_gameport_driver() - Helper macro for registering a gameport driver
  121. * @__gameport_driver: gameport_driver struct
  122. *
  123. * Helper macro for gameport drivers which do not do anything special in
  124. * module init/exit. This eliminates a lot of boilerplate. Each module may
  125. * only use this macro once, and calling it replaces module_init() and
  126. * module_exit().
  127. */
  128. #define module_gameport_driver(__gameport_driver) \
  129. module_driver(__gameport_driver, gameport_register_driver, \
  130. gameport_unregister_driver)
  131. #endif /* __KERNEL__ */
  132. #define GAMEPORT_MODE_DISABLED 0
  133. #define GAMEPORT_MODE_RAW 1
  134. #define GAMEPORT_MODE_COOKED 2
  135. #define GAMEPORT_ID_VENDOR_ANALOG 0x0001
  136. #define GAMEPORT_ID_VENDOR_MADCATZ 0x0002
  137. #define GAMEPORT_ID_VENDOR_LOGITECH 0x0003
  138. #define GAMEPORT_ID_VENDOR_CREATIVE 0x0004
  139. #define GAMEPORT_ID_VENDOR_GENIUS 0x0005
  140. #define GAMEPORT_ID_VENDOR_INTERACT 0x0006
  141. #define GAMEPORT_ID_VENDOR_MICROSOFT 0x0007
  142. #define GAMEPORT_ID_VENDOR_THRUSTMASTER 0x0008
  143. #define GAMEPORT_ID_VENDOR_GRAVIS 0x0009
  144. #define GAMEPORT_ID_VENDOR_GUILLEMOT 0x000a
  145. #ifdef __KERNEL__
  146. static inline void gameport_trigger(struct gameport *gameport)
  147. {
  148. if (gameport->trigger)
  149. gameport->trigger(gameport);
  150. else
  151. outb(0xff, gameport->io);
  152. }
  153. static inline unsigned char gameport_read(struct gameport *gameport)
  154. {
  155. if (gameport->read)
  156. return gameport->read(gameport);
  157. else
  158. return inb(gameport->io);
  159. }
  160. static inline int gameport_cooked_read(struct gameport *gameport, int *axes, int *buttons)
  161. {
  162. if (gameport->cooked_read)
  163. return gameport->cooked_read(gameport, axes, buttons);
  164. else
  165. return -1;
  166. }
  167. static inline int gameport_calibrate(struct gameport *gameport, int *axes, int *max)
  168. {
  169. if (gameport->calibrate)
  170. return gameport->calibrate(gameport, axes, max);
  171. else
  172. return -1;
  173. }
  174. static inline int gameport_time(struct gameport *gameport, int time)
  175. {
  176. return (time * gameport->speed) / 1000;
  177. }
  178. static inline void gameport_set_poll_handler(struct gameport *gameport, void (*handler)(struct gameport *))
  179. {
  180. gameport->poll_handler = handler;
  181. }
  182. static inline void gameport_set_poll_interval(struct gameport *gameport, unsigned int msecs)
  183. {
  184. gameport->poll_interval = msecs;
  185. }
  186. void gameport_start_polling(struct gameport *gameport);
  187. void gameport_stop_polling(struct gameport *gameport);
  188. #endif /* __KERNEL__ */
  189. #endif