gameport.h 5.4 KB

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