gameport.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. #endif /* __KERNEL__ */
  120. #define GAMEPORT_MODE_DISABLED 0
  121. #define GAMEPORT_MODE_RAW 1
  122. #define GAMEPORT_MODE_COOKED 2
  123. #define GAMEPORT_ID_VENDOR_ANALOG 0x0001
  124. #define GAMEPORT_ID_VENDOR_MADCATZ 0x0002
  125. #define GAMEPORT_ID_VENDOR_LOGITECH 0x0003
  126. #define GAMEPORT_ID_VENDOR_CREATIVE 0x0004
  127. #define GAMEPORT_ID_VENDOR_GENIUS 0x0005
  128. #define GAMEPORT_ID_VENDOR_INTERACT 0x0006
  129. #define GAMEPORT_ID_VENDOR_MICROSOFT 0x0007
  130. #define GAMEPORT_ID_VENDOR_THRUSTMASTER 0x0008
  131. #define GAMEPORT_ID_VENDOR_GRAVIS 0x0009
  132. #define GAMEPORT_ID_VENDOR_GUILLEMOT 0x000a
  133. #ifdef __KERNEL__
  134. static inline void gameport_trigger(struct gameport *gameport)
  135. {
  136. if (gameport->trigger)
  137. gameport->trigger(gameport);
  138. else
  139. outb(0xff, gameport->io);
  140. }
  141. static inline unsigned char gameport_read(struct gameport *gameport)
  142. {
  143. if (gameport->read)
  144. return gameport->read(gameport);
  145. else
  146. return inb(gameport->io);
  147. }
  148. static inline int gameport_cooked_read(struct gameport *gameport, int *axes, int *buttons)
  149. {
  150. if (gameport->cooked_read)
  151. return gameport->cooked_read(gameport, axes, buttons);
  152. else
  153. return -1;
  154. }
  155. static inline int gameport_calibrate(struct gameport *gameport, int *axes, int *max)
  156. {
  157. if (gameport->calibrate)
  158. return gameport->calibrate(gameport, axes, max);
  159. else
  160. return -1;
  161. }
  162. static inline int gameport_time(struct gameport *gameport, int time)
  163. {
  164. return (time * gameport->speed) / 1000;
  165. }
  166. static inline void gameport_set_poll_handler(struct gameport *gameport, void (*handler)(struct gameport *))
  167. {
  168. gameport->poll_handler = handler;
  169. }
  170. static inline void gameport_set_poll_interval(struct gameport *gameport, unsigned int msecs)
  171. {
  172. gameport->poll_interval = msecs;
  173. }
  174. void gameport_start_polling(struct gameport *gameport);
  175. void gameport_stop_polling(struct gameport *gameport);
  176. #endif /* __KERNEL__ */
  177. #endif