1 // SPDX-License-Identifier: GPL-2.0-only
3 * Generic gameport layer
5 * Copyright (c) 1999-2002 Vojtech Pavlik
6 * Copyright (c) 2005 Dmitry Torokhov
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/stddef.h>
13 #include <linux/module.h>
14 #include <linux/ioport.h>
15 #include <linux/init.h>
16 #include <linux/gameport.h>
17 #include <linux/slab.h>
18 #include <linux/delay.h>
19 #include <linux/workqueue.h>
20 #include <linux/sched.h> /* HZ */
21 #include <linux/mutex.h>
22 #include <linux/timekeeping.h>
24 /*#include <asm/io.h>*/
26 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
27 MODULE_DESCRIPTION("Generic gameport layer");
28 MODULE_LICENSE("GPL");
30 static bool use_ktime
= true;
31 module_param(use_ktime
, bool, 0400);
32 MODULE_PARM_DESC(use_ktime
, "Use ktime for measuring I/O speed");
35 * gameport_mutex protects entire gameport subsystem and is taken
36 * every time gameport port or driver registrered or unregistered.
38 static DEFINE_MUTEX(gameport_mutex
);
40 static LIST_HEAD(gameport_list
);
42 static struct bus_type gameport_bus
;
44 static void gameport_add_port(struct gameport
*gameport
);
45 static void gameport_attach_driver(struct gameport_driver
*drv
);
46 static void gameport_reconnect_port(struct gameport
*gameport
);
47 static void gameport_disconnect_port(struct gameport
*gameport
);
51 #include <linux/i8253.h>
53 #define DELTA(x,y) ((y)-(x)+((y)<(x)?1193182/HZ:0))
54 #define GET_TIME(x) do { x = get_time_pit(); } while (0)
56 static unsigned int get_time_pit(void)
61 raw_spin_lock_irqsave(&i8253_lock
, flags
);
64 count
|= inb_p(0x40) << 8;
65 raw_spin_unlock_irqrestore(&i8253_lock
, flags
);
75 * gameport_measure_speed() measures the gameport i/o speed.
78 static int gameport_measure_speed(struct gameport
*gameport
)
80 unsigned int i
, t
, tx
;
84 if (gameport_open(gameport
, NULL
, GAMEPORT_MODE_RAW
))
89 for (i
= 0; i
< 50; i
++) {
90 local_irq_save(flags
);
92 for (t
= 0; t
< 50; t
++)
93 gameport_read(gameport
);
96 local_irq_restore(flags
);
98 t
= (t2
- t1
) - (t3
- t2
);
103 gameport_close(gameport
);
110 static int old_gameport_measure_speed(struct gameport
*gameport
)
112 #if defined(__i386__)
114 unsigned int i
, t
, t1
, t2
, t3
, tx
;
117 if (gameport_open(gameport
, NULL
, GAMEPORT_MODE_RAW
))
122 for(i
= 0; i
< 50; i
++) {
123 local_irq_save(flags
);
125 for (t
= 0; t
< 50; t
++) gameport_read(gameport
);
128 local_irq_restore(flags
);
130 if ((t
= DELTA(t2
,t1
) - DELTA(t3
,t2
)) < tx
) tx
= t
;
133 gameport_close(gameport
);
134 return 59659 / (tx
< 1 ? 1 : tx
);
136 #elif defined (__x86_64__)
139 unsigned long tx
, t1
, t2
, flags
;
141 if (gameport_open(gameport
, NULL
, GAMEPORT_MODE_RAW
))
146 for(i
= 0; i
< 50; i
++) {
147 local_irq_save(flags
);
149 for (t
= 0; t
< 50; t
++) gameport_read(gameport
);
151 local_irq_restore(flags
);
153 if (t2
- t1
< tx
) tx
= t2
- t1
;
156 gameport_close(gameport
);
157 return (this_cpu_read(cpu_info
.loops_per_jiffy
) *
158 (unsigned long)HZ
/ (1000 / 50)) / (tx
< 1 ? 1 : tx
);
162 unsigned int j
, t
= 0;
164 if (gameport_open(gameport
, NULL
, GAMEPORT_MODE_RAW
))
167 j
= jiffies
; while (j
== jiffies
);
168 j
= jiffies
; while (j
== jiffies
) { t
++; gameport_read(gameport
); }
170 gameport_close(gameport
);
171 return t
* HZ
/ 1000;
176 void gameport_start_polling(struct gameport
*gameport
)
178 spin_lock(&gameport
->timer_lock
);
180 if (!gameport
->poll_cnt
++) {
181 BUG_ON(!gameport
->poll_handler
);
182 BUG_ON(!gameport
->poll_interval
);
183 mod_timer(&gameport
->poll_timer
, jiffies
+ msecs_to_jiffies(gameport
->poll_interval
));
186 spin_unlock(&gameport
->timer_lock
);
188 EXPORT_SYMBOL(gameport_start_polling
);
190 void gameport_stop_polling(struct gameport
*gameport
)
192 spin_lock(&gameport
->timer_lock
);
194 if (!--gameport
->poll_cnt
)
195 del_timer(&gameport
->poll_timer
);
197 spin_unlock(&gameport
->timer_lock
);
199 EXPORT_SYMBOL(gameport_stop_polling
);
201 static void gameport_run_poll_handler(struct timer_list
*t
)
203 struct gameport
*gameport
= from_timer(gameport
, t
, poll_timer
);
205 gameport
->poll_handler(gameport
);
206 if (gameport
->poll_cnt
)
207 mod_timer(&gameport
->poll_timer
, jiffies
+ msecs_to_jiffies(gameport
->poll_interval
));
211 * Basic gameport -> driver core mappings
214 static int gameport_bind_driver(struct gameport
*gameport
, struct gameport_driver
*drv
)
218 gameport
->dev
.driver
= &drv
->driver
;
219 if (drv
->connect(gameport
, drv
)) {
220 gameport
->dev
.driver
= NULL
;
224 error
= device_bind_driver(&gameport
->dev
);
226 dev_warn(&gameport
->dev
,
227 "device_bind_driver() failed for %s (%s) and %s, error: %d\n",
228 gameport
->phys
, gameport
->name
,
229 drv
->description
, error
);
230 drv
->disconnect(gameport
);
231 gameport
->dev
.driver
= NULL
;
238 static void gameport_find_driver(struct gameport
*gameport
)
242 error
= device_attach(&gameport
->dev
);
244 dev_warn(&gameport
->dev
,
245 "device_attach() failed for %s (%s), error: %d\n",
246 gameport
->phys
, gameport
->name
, error
);
251 * Gameport event processing.
254 enum gameport_event_type
{
255 GAMEPORT_REGISTER_PORT
,
256 GAMEPORT_ATTACH_DRIVER
,
259 struct gameport_event
{
260 enum gameport_event_type type
;
262 struct module
*owner
;
263 struct list_head node
;
266 static DEFINE_SPINLOCK(gameport_event_lock
); /* protects gameport_event_list */
267 static LIST_HEAD(gameport_event_list
);
269 static struct gameport_event
*gameport_get_event(void)
271 struct gameport_event
*event
= NULL
;
274 spin_lock_irqsave(&gameport_event_lock
, flags
);
276 if (!list_empty(&gameport_event_list
)) {
277 event
= list_first_entry(&gameport_event_list
,
278 struct gameport_event
, node
);
279 list_del_init(&event
->node
);
282 spin_unlock_irqrestore(&gameport_event_lock
, flags
);
286 static void gameport_free_event(struct gameport_event
*event
)
288 module_put(event
->owner
);
292 static void gameport_remove_duplicate_events(struct gameport_event
*event
)
294 struct gameport_event
*e
, *next
;
297 spin_lock_irqsave(&gameport_event_lock
, flags
);
299 list_for_each_entry_safe(e
, next
, &gameport_event_list
, node
) {
300 if (event
->object
== e
->object
) {
302 * If this event is of different type we should not
303 * look further - we only suppress duplicate events
304 * that were sent back-to-back.
306 if (event
->type
!= e
->type
)
309 list_del_init(&e
->node
);
310 gameport_free_event(e
);
314 spin_unlock_irqrestore(&gameport_event_lock
, flags
);
318 static void gameport_handle_events(struct work_struct
*work
)
320 struct gameport_event
*event
;
322 mutex_lock(&gameport_mutex
);
325 * Note that we handle only one event here to give swsusp
326 * a chance to freeze kgameportd thread. Gameport events
327 * should be pretty rare so we are not concerned about
328 * taking performance hit.
330 if ((event
= gameport_get_event())) {
332 switch (event
->type
) {
334 case GAMEPORT_REGISTER_PORT
:
335 gameport_add_port(event
->object
);
338 case GAMEPORT_ATTACH_DRIVER
:
339 gameport_attach_driver(event
->object
);
343 gameport_remove_duplicate_events(event
);
344 gameport_free_event(event
);
347 mutex_unlock(&gameport_mutex
);
350 static DECLARE_WORK(gameport_event_work
, gameport_handle_events
);
352 static int gameport_queue_event(void *object
, struct module
*owner
,
353 enum gameport_event_type event_type
)
356 struct gameport_event
*event
;
359 spin_lock_irqsave(&gameport_event_lock
, flags
);
362 * Scan event list for the other events for the same gameport port,
363 * starting with the most recent one. If event is the same we
364 * do not need add new one. If event is of different type we
365 * need to add this event and should not look further because
366 * we need to preserve sequence of distinct events.
368 list_for_each_entry_reverse(event
, &gameport_event_list
, node
) {
369 if (event
->object
== object
) {
370 if (event
->type
== event_type
)
376 event
= kmalloc(sizeof(struct gameport_event
), GFP_ATOMIC
);
378 pr_err("Not enough memory to queue event %d\n", event_type
);
383 if (!try_module_get(owner
)) {
384 pr_warn("Can't get module reference, dropping event %d\n",
391 event
->type
= event_type
;
392 event
->object
= object
;
393 event
->owner
= owner
;
395 list_add_tail(&event
->node
, &gameport_event_list
);
396 queue_work(system_long_wq
, &gameport_event_work
);
399 spin_unlock_irqrestore(&gameport_event_lock
, flags
);
404 * Remove all events that have been submitted for a given object,
405 * be it a gameport port or a driver.
407 static void gameport_remove_pending_events(void *object
)
409 struct gameport_event
*event
, *next
;
412 spin_lock_irqsave(&gameport_event_lock
, flags
);
414 list_for_each_entry_safe(event
, next
, &gameport_event_list
, node
) {
415 if (event
->object
== object
) {
416 list_del_init(&event
->node
);
417 gameport_free_event(event
);
421 spin_unlock_irqrestore(&gameport_event_lock
, flags
);
425 * Destroy child gameport port (if any) that has not been fully registered yet.
427 * Note that we rely on the fact that port can have only one child and therefore
428 * only one child registration request can be pending. Additionally, children
429 * are registered by driver's connect() handler so there can't be a grandchild
430 * pending registration together with a child.
432 static struct gameport
*gameport_get_pending_child(struct gameport
*parent
)
434 struct gameport_event
*event
;
435 struct gameport
*gameport
, *child
= NULL
;
438 spin_lock_irqsave(&gameport_event_lock
, flags
);
440 list_for_each_entry(event
, &gameport_event_list
, node
) {
441 if (event
->type
== GAMEPORT_REGISTER_PORT
) {
442 gameport
= event
->object
;
443 if (gameport
->parent
== parent
) {
450 spin_unlock_irqrestore(&gameport_event_lock
, flags
);
455 * Gameport port operations
458 static ssize_t
gameport_description_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
460 struct gameport
*gameport
= to_gameport_port(dev
);
462 return sprintf(buf
, "%s\n", gameport
->name
);
464 static DEVICE_ATTR(description
, S_IRUGO
, gameport_description_show
, NULL
);
466 static ssize_t
drvctl_store(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
468 struct gameport
*gameport
= to_gameport_port(dev
);
469 struct device_driver
*drv
;
472 error
= mutex_lock_interruptible(&gameport_mutex
);
476 if (!strncmp(buf
, "none", count
)) {
477 gameport_disconnect_port(gameport
);
478 } else if (!strncmp(buf
, "reconnect", count
)) {
479 gameport_reconnect_port(gameport
);
480 } else if (!strncmp(buf
, "rescan", count
)) {
481 gameport_disconnect_port(gameport
);
482 gameport_find_driver(gameport
);
483 } else if ((drv
= driver_find(buf
, &gameport_bus
)) != NULL
) {
484 gameport_disconnect_port(gameport
);
485 error
= gameport_bind_driver(gameport
, to_gameport_driver(drv
));
490 mutex_unlock(&gameport_mutex
);
492 return error
? error
: count
;
494 static DEVICE_ATTR_WO(drvctl
);
496 static struct attribute
*gameport_device_attrs
[] = {
497 &dev_attr_description
.attr
,
498 &dev_attr_drvctl
.attr
,
501 ATTRIBUTE_GROUPS(gameport_device
);
503 static void gameport_release_port(struct device
*dev
)
505 struct gameport
*gameport
= to_gameport_port(dev
);
508 module_put(THIS_MODULE
);
511 void gameport_set_phys(struct gameport
*gameport
, const char *fmt
, ...)
516 vsnprintf(gameport
->phys
, sizeof(gameport
->phys
), fmt
, args
);
519 EXPORT_SYMBOL(gameport_set_phys
);
522 * Prepare gameport port for registration.
524 static void gameport_init_port(struct gameport
*gameport
)
526 static atomic_t gameport_no
= ATOMIC_INIT(-1);
528 __module_get(THIS_MODULE
);
530 mutex_init(&gameport
->drv_mutex
);
531 device_initialize(&gameport
->dev
);
532 dev_set_name(&gameport
->dev
, "gameport%lu",
533 (unsigned long)atomic_inc_return(&gameport_no
));
534 gameport
->dev
.bus
= &gameport_bus
;
535 gameport
->dev
.release
= gameport_release_port
;
536 if (gameport
->parent
)
537 gameport
->dev
.parent
= &gameport
->parent
->dev
;
539 INIT_LIST_HEAD(&gameport
->node
);
540 spin_lock_init(&gameport
->timer_lock
);
541 timer_setup(&gameport
->poll_timer
, gameport_run_poll_handler
, 0);
545 * Complete gameport port registration.
546 * Driver core will attempt to find appropriate driver for the port.
548 static void gameport_add_port(struct gameport
*gameport
)
552 if (gameport
->parent
)
553 gameport
->parent
->child
= gameport
;
555 gameport
->speed
= use_ktime
?
556 gameport_measure_speed(gameport
) :
557 old_gameport_measure_speed(gameport
);
559 list_add_tail(&gameport
->node
, &gameport_list
);
562 dev_info(&gameport
->dev
, "%s is %s, io %#x, speed %dkHz\n",
563 gameport
->name
, gameport
->phys
, gameport
->io
, gameport
->speed
);
565 dev_info(&gameport
->dev
, "%s is %s, speed %dkHz\n",
566 gameport
->name
, gameport
->phys
, gameport
->speed
);
568 error
= device_add(&gameport
->dev
);
570 dev_err(&gameport
->dev
,
571 "device_add() failed for %s (%s), error: %d\n",
572 gameport
->phys
, gameport
->name
, error
);
576 * gameport_destroy_port() completes deregistration process and removes
577 * port from the system
579 static void gameport_destroy_port(struct gameport
*gameport
)
581 struct gameport
*child
;
583 child
= gameport_get_pending_child(gameport
);
585 gameport_remove_pending_events(child
);
586 put_device(&child
->dev
);
589 if (gameport
->parent
) {
590 gameport
->parent
->child
= NULL
;
591 gameport
->parent
= NULL
;
594 if (device_is_registered(&gameport
->dev
))
595 device_del(&gameport
->dev
);
597 list_del_init(&gameport
->node
);
599 gameport_remove_pending_events(gameport
);
600 put_device(&gameport
->dev
);
604 * Reconnect gameport port and all its children (re-initialize attached devices)
606 static void gameport_reconnect_port(struct gameport
*gameport
)
609 if (!gameport
->drv
|| !gameport
->drv
->reconnect
|| gameport
->drv
->reconnect(gameport
)) {
610 gameport_disconnect_port(gameport
);
611 gameport_find_driver(gameport
);
612 /* Ok, old children are now gone, we are done */
615 gameport
= gameport
->child
;
620 * gameport_disconnect_port() unbinds a port from its driver. As a side effect
621 * all child ports are unbound and destroyed.
623 static void gameport_disconnect_port(struct gameport
*gameport
)
625 struct gameport
*s
, *parent
;
627 if (gameport
->child
) {
629 * Children ports should be disconnected and destroyed
630 * first, staring with the leaf one, since we don't want
633 for (s
= gameport
; s
->child
; s
= s
->child
)
639 device_release_driver(&s
->dev
);
640 gameport_destroy_port(s
);
641 } while ((s
= parent
) != gameport
);
645 * Ok, no children left, now disconnect this port
647 device_release_driver(&gameport
->dev
);
651 * Submits register request to kgameportd for subsequent execution.
652 * Note that port registration is always asynchronous.
654 void __gameport_register_port(struct gameport
*gameport
, struct module
*owner
)
656 gameport_init_port(gameport
);
657 gameport_queue_event(gameport
, owner
, GAMEPORT_REGISTER_PORT
);
659 EXPORT_SYMBOL(__gameport_register_port
);
662 * Synchronously unregisters gameport port.
664 void gameport_unregister_port(struct gameport
*gameport
)
666 mutex_lock(&gameport_mutex
);
667 gameport_disconnect_port(gameport
);
668 gameport_destroy_port(gameport
);
669 mutex_unlock(&gameport_mutex
);
671 EXPORT_SYMBOL(gameport_unregister_port
);
675 * Gameport driver operations
678 static ssize_t
description_show(struct device_driver
*drv
, char *buf
)
680 struct gameport_driver
*driver
= to_gameport_driver(drv
);
681 return sprintf(buf
, "%s\n", driver
->description
? driver
->description
: "(none)");
683 static DRIVER_ATTR_RO(description
);
685 static struct attribute
*gameport_driver_attrs
[] = {
686 &driver_attr_description
.attr
,
689 ATTRIBUTE_GROUPS(gameport_driver
);
691 static int gameport_driver_probe(struct device
*dev
)
693 struct gameport
*gameport
= to_gameport_port(dev
);
694 struct gameport_driver
*drv
= to_gameport_driver(dev
->driver
);
696 drv
->connect(gameport
, drv
);
697 return gameport
->drv
? 0 : -ENODEV
;
700 static int gameport_driver_remove(struct device
*dev
)
702 struct gameport
*gameport
= to_gameport_port(dev
);
703 struct gameport_driver
*drv
= to_gameport_driver(dev
->driver
);
705 drv
->disconnect(gameport
);
709 static void gameport_attach_driver(struct gameport_driver
*drv
)
713 error
= driver_attach(&drv
->driver
);
715 pr_err("driver_attach() failed for %s, error: %d\n",
716 drv
->driver
.name
, error
);
719 int __gameport_register_driver(struct gameport_driver
*drv
, struct module
*owner
,
720 const char *mod_name
)
724 drv
->driver
.bus
= &gameport_bus
;
725 drv
->driver
.owner
= owner
;
726 drv
->driver
.mod_name
= mod_name
;
729 * Temporarily disable automatic binding because probing
730 * takes long time and we are better off doing it in kgameportd
734 error
= driver_register(&drv
->driver
);
736 pr_err("driver_register() failed for %s, error: %d\n",
737 drv
->driver
.name
, error
);
742 * Reset ignore flag and let kgameportd bind the driver to free ports
745 error
= gameport_queue_event(drv
, NULL
, GAMEPORT_ATTACH_DRIVER
);
747 driver_unregister(&drv
->driver
);
753 EXPORT_SYMBOL(__gameport_register_driver
);
755 void gameport_unregister_driver(struct gameport_driver
*drv
)
757 struct gameport
*gameport
;
759 mutex_lock(&gameport_mutex
);
761 drv
->ignore
= true; /* so gameport_find_driver ignores it */
762 gameport_remove_pending_events(drv
);
765 list_for_each_entry(gameport
, &gameport_list
, node
) {
766 if (gameport
->drv
== drv
) {
767 gameport_disconnect_port(gameport
);
768 gameport_find_driver(gameport
);
769 /* we could've deleted some ports, restart */
774 driver_unregister(&drv
->driver
);
776 mutex_unlock(&gameport_mutex
);
778 EXPORT_SYMBOL(gameport_unregister_driver
);
780 static int gameport_bus_match(struct device
*dev
, struct device_driver
*drv
)
782 struct gameport_driver
*gameport_drv
= to_gameport_driver(drv
);
784 return !gameport_drv
->ignore
;
787 static struct bus_type gameport_bus
= {
789 .dev_groups
= gameport_device_groups
,
790 .drv_groups
= gameport_driver_groups
,
791 .match
= gameport_bus_match
,
792 .probe
= gameport_driver_probe
,
793 .remove
= gameport_driver_remove
,
796 static void gameport_set_drv(struct gameport
*gameport
, struct gameport_driver
*drv
)
798 mutex_lock(&gameport
->drv_mutex
);
800 mutex_unlock(&gameport
->drv_mutex
);
803 int gameport_open(struct gameport
*gameport
, struct gameport_driver
*drv
, int mode
)
805 if (gameport
->open
) {
806 if (gameport
->open(gameport
, mode
)) {
810 if (mode
!= GAMEPORT_MODE_RAW
)
814 gameport_set_drv(gameport
, drv
);
817 EXPORT_SYMBOL(gameport_open
);
819 void gameport_close(struct gameport
*gameport
)
821 del_timer_sync(&gameport
->poll_timer
);
822 gameport
->poll_handler
= NULL
;
823 gameport
->poll_interval
= 0;
824 gameport_set_drv(gameport
, NULL
);
826 gameport
->close(gameport
);
828 EXPORT_SYMBOL(gameport_close
);
830 static int __init
gameport_init(void)
834 error
= bus_register(&gameport_bus
);
836 pr_err("failed to register gameport bus, error: %d\n", error
);
844 static void __exit
gameport_exit(void)
846 bus_unregister(&gameport_bus
);
849 * There should not be any outstanding events but work may
850 * still be scheduled so simply cancel it.
852 cancel_work_sync(&gameport_event_work
);
855 subsys_initcall(gameport_init
);
856 module_exit(gameport_exit
);