2 * Generic gameport layer
4 * Copyright (c) 1999-2002 Vojtech Pavlik
5 * Copyright (c) 2005 Dmitry Torokhov
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License version 2 as published by
11 * the Free Software Foundation.
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/stddef.h>
17 #include <linux/module.h>
18 #include <linux/ioport.h>
19 #include <linux/init.h>
20 #include <linux/gameport.h>
21 #include <linux/slab.h>
22 #include <linux/delay.h>
23 #include <linux/workqueue.h>
24 #include <linux/sched.h> /* HZ */
25 #include <linux/mutex.h>
26 #include <linux/timekeeping.h>
28 /*#include <asm/io.h>*/
30 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
31 MODULE_DESCRIPTION("Generic gameport layer");
32 MODULE_LICENSE("GPL");
34 static bool use_ktime
= true;
35 module_param(use_ktime
, bool, 0400);
36 MODULE_PARM_DESC(use_ktime
, "Use ktime for measuring I/O speed");
39 * gameport_mutex protects entire gameport subsystem and is taken
40 * every time gameport port or driver registrered or unregistered.
42 static DEFINE_MUTEX(gameport_mutex
);
44 static LIST_HEAD(gameport_list
);
46 static struct bus_type gameport_bus
;
48 static void gameport_add_port(struct gameport
*gameport
);
49 static void gameport_attach_driver(struct gameport_driver
*drv
);
50 static void gameport_reconnect_port(struct gameport
*gameport
);
51 static void gameport_disconnect_port(struct gameport
*gameport
);
55 #include <linux/i8253.h>
57 #define DELTA(x,y) ((y)-(x)+((y)<(x)?1193182/HZ:0))
58 #define GET_TIME(x) do { x = get_time_pit(); } while (0)
60 static unsigned int get_time_pit(void)
65 raw_spin_lock_irqsave(&i8253_lock
, flags
);
68 count
|= inb_p(0x40) << 8;
69 raw_spin_unlock_irqrestore(&i8253_lock
, flags
);
79 * gameport_measure_speed() measures the gameport i/o speed.
82 static int gameport_measure_speed(struct gameport
*gameport
)
84 unsigned int i
, t
, tx
;
88 if (gameport_open(gameport
, NULL
, GAMEPORT_MODE_RAW
))
93 for (i
= 0; i
< 50; i
++) {
94 local_irq_save(flags
);
96 for (t
= 0; t
< 50; t
++)
97 gameport_read(gameport
);
100 local_irq_restore(flags
);
102 t
= (t2
- t1
) - (t3
- t2
);
107 gameport_close(gameport
);
114 static int old_gameport_measure_speed(struct gameport
*gameport
)
116 #if defined(__i386__)
118 unsigned int i
, t
, t1
, t2
, t3
, tx
;
121 if (gameport_open(gameport
, NULL
, GAMEPORT_MODE_RAW
))
126 for(i
= 0; i
< 50; i
++) {
127 local_irq_save(flags
);
129 for (t
= 0; t
< 50; t
++) gameport_read(gameport
);
132 local_irq_restore(flags
);
134 if ((t
= DELTA(t2
,t1
) - DELTA(t3
,t2
)) < tx
) tx
= t
;
137 gameport_close(gameport
);
138 return 59659 / (tx
< 1 ? 1 : tx
);
140 #elif defined (__x86_64__)
143 unsigned long tx
, t1
, t2
, flags
;
145 if (gameport_open(gameport
, NULL
, GAMEPORT_MODE_RAW
))
150 for(i
= 0; i
< 50; i
++) {
151 local_irq_save(flags
);
153 for (t
= 0; t
< 50; t
++) gameport_read(gameport
);
155 local_irq_restore(flags
);
157 if (t2
- t1
< tx
) tx
= t2
- t1
;
160 gameport_close(gameport
);
161 return (this_cpu_read(cpu_info
.loops_per_jiffy
) *
162 (unsigned long)HZ
/ (1000 / 50)) / (tx
< 1 ? 1 : tx
);
166 unsigned int j
, t
= 0;
168 if (gameport_open(gameport
, NULL
, GAMEPORT_MODE_RAW
))
171 j
= jiffies
; while (j
== jiffies
);
172 j
= jiffies
; while (j
== jiffies
) { t
++; gameport_read(gameport
); }
174 gameport_close(gameport
);
175 return t
* HZ
/ 1000;
180 void gameport_start_polling(struct gameport
*gameport
)
182 spin_lock(&gameport
->timer_lock
);
184 if (!gameport
->poll_cnt
++) {
185 BUG_ON(!gameport
->poll_handler
);
186 BUG_ON(!gameport
->poll_interval
);
187 mod_timer(&gameport
->poll_timer
, jiffies
+ msecs_to_jiffies(gameport
->poll_interval
));
190 spin_unlock(&gameport
->timer_lock
);
192 EXPORT_SYMBOL(gameport_start_polling
);
194 void gameport_stop_polling(struct gameport
*gameport
)
196 spin_lock(&gameport
->timer_lock
);
198 if (!--gameport
->poll_cnt
)
199 del_timer(&gameport
->poll_timer
);
201 spin_unlock(&gameport
->timer_lock
);
203 EXPORT_SYMBOL(gameport_stop_polling
);
205 static void gameport_run_poll_handler(struct timer_list
*t
)
207 struct gameport
*gameport
= from_timer(gameport
, t
, poll_timer
);
209 gameport
->poll_handler(gameport
);
210 if (gameport
->poll_cnt
)
211 mod_timer(&gameport
->poll_timer
, jiffies
+ msecs_to_jiffies(gameport
->poll_interval
));
215 * Basic gameport -> driver core mappings
218 static int gameport_bind_driver(struct gameport
*gameport
, struct gameport_driver
*drv
)
222 gameport
->dev
.driver
= &drv
->driver
;
223 if (drv
->connect(gameport
, drv
)) {
224 gameport
->dev
.driver
= NULL
;
228 error
= device_bind_driver(&gameport
->dev
);
230 dev_warn(&gameport
->dev
,
231 "device_bind_driver() failed for %s (%s) and %s, error: %d\n",
232 gameport
->phys
, gameport
->name
,
233 drv
->description
, error
);
234 drv
->disconnect(gameport
);
235 gameport
->dev
.driver
= NULL
;
242 static void gameport_find_driver(struct gameport
*gameport
)
246 error
= device_attach(&gameport
->dev
);
248 dev_warn(&gameport
->dev
,
249 "device_attach() failed for %s (%s), error: %d\n",
250 gameport
->phys
, gameport
->name
, error
);
255 * Gameport event processing.
258 enum gameport_event_type
{
259 GAMEPORT_REGISTER_PORT
,
260 GAMEPORT_ATTACH_DRIVER
,
263 struct gameport_event
{
264 enum gameport_event_type type
;
266 struct module
*owner
;
267 struct list_head node
;
270 static DEFINE_SPINLOCK(gameport_event_lock
); /* protects gameport_event_list */
271 static LIST_HEAD(gameport_event_list
);
273 static struct gameport_event
*gameport_get_event(void)
275 struct gameport_event
*event
= NULL
;
278 spin_lock_irqsave(&gameport_event_lock
, flags
);
280 if (!list_empty(&gameport_event_list
)) {
281 event
= list_first_entry(&gameport_event_list
,
282 struct gameport_event
, node
);
283 list_del_init(&event
->node
);
286 spin_unlock_irqrestore(&gameport_event_lock
, flags
);
290 static void gameport_free_event(struct gameport_event
*event
)
292 module_put(event
->owner
);
296 static void gameport_remove_duplicate_events(struct gameport_event
*event
)
298 struct gameport_event
*e
, *next
;
301 spin_lock_irqsave(&gameport_event_lock
, flags
);
303 list_for_each_entry_safe(e
, next
, &gameport_event_list
, node
) {
304 if (event
->object
== e
->object
) {
306 * If this event is of different type we should not
307 * look further - we only suppress duplicate events
308 * that were sent back-to-back.
310 if (event
->type
!= e
->type
)
313 list_del_init(&e
->node
);
314 gameport_free_event(e
);
318 spin_unlock_irqrestore(&gameport_event_lock
, flags
);
322 static void gameport_handle_events(struct work_struct
*work
)
324 struct gameport_event
*event
;
326 mutex_lock(&gameport_mutex
);
329 * Note that we handle only one event here to give swsusp
330 * a chance to freeze kgameportd thread. Gameport events
331 * should be pretty rare so we are not concerned about
332 * taking performance hit.
334 if ((event
= gameport_get_event())) {
336 switch (event
->type
) {
338 case GAMEPORT_REGISTER_PORT
:
339 gameport_add_port(event
->object
);
342 case GAMEPORT_ATTACH_DRIVER
:
343 gameport_attach_driver(event
->object
);
347 gameport_remove_duplicate_events(event
);
348 gameport_free_event(event
);
351 mutex_unlock(&gameport_mutex
);
354 static DECLARE_WORK(gameport_event_work
, gameport_handle_events
);
356 static int gameport_queue_event(void *object
, struct module
*owner
,
357 enum gameport_event_type event_type
)
360 struct gameport_event
*event
;
363 spin_lock_irqsave(&gameport_event_lock
, flags
);
366 * Scan event list for the other events for the same gameport port,
367 * starting with the most recent one. If event is the same we
368 * do not need add new one. If event is of different type we
369 * need to add this event and should not look further because
370 * we need to preserve sequence of distinct events.
372 list_for_each_entry_reverse(event
, &gameport_event_list
, node
) {
373 if (event
->object
== object
) {
374 if (event
->type
== event_type
)
380 event
= kmalloc(sizeof(struct gameport_event
), GFP_ATOMIC
);
382 pr_err("Not enough memory to queue event %d\n", event_type
);
387 if (!try_module_get(owner
)) {
388 pr_warn("Can't get module reference, dropping event %d\n",
395 event
->type
= event_type
;
396 event
->object
= object
;
397 event
->owner
= owner
;
399 list_add_tail(&event
->node
, &gameport_event_list
);
400 queue_work(system_long_wq
, &gameport_event_work
);
403 spin_unlock_irqrestore(&gameport_event_lock
, flags
);
408 * Remove all events that have been submitted for a given object,
409 * be it a gameport port or a driver.
411 static void gameport_remove_pending_events(void *object
)
413 struct gameport_event
*event
, *next
;
416 spin_lock_irqsave(&gameport_event_lock
, flags
);
418 list_for_each_entry_safe(event
, next
, &gameport_event_list
, node
) {
419 if (event
->object
== object
) {
420 list_del_init(&event
->node
);
421 gameport_free_event(event
);
425 spin_unlock_irqrestore(&gameport_event_lock
, flags
);
429 * Destroy child gameport port (if any) that has not been fully registered yet.
431 * Note that we rely on the fact that port can have only one child and therefore
432 * only one child registration request can be pending. Additionally, children
433 * are registered by driver's connect() handler so there can't be a grandchild
434 * pending registration together with a child.
436 static struct gameport
*gameport_get_pending_child(struct gameport
*parent
)
438 struct gameport_event
*event
;
439 struct gameport
*gameport
, *child
= NULL
;
442 spin_lock_irqsave(&gameport_event_lock
, flags
);
444 list_for_each_entry(event
, &gameport_event_list
, node
) {
445 if (event
->type
== GAMEPORT_REGISTER_PORT
) {
446 gameport
= event
->object
;
447 if (gameport
->parent
== parent
) {
454 spin_unlock_irqrestore(&gameport_event_lock
, flags
);
459 * Gameport port operations
462 static ssize_t
gameport_description_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
464 struct gameport
*gameport
= to_gameport_port(dev
);
466 return sprintf(buf
, "%s\n", gameport
->name
);
468 static DEVICE_ATTR(description
, S_IRUGO
, gameport_description_show
, NULL
);
470 static ssize_t
drvctl_store(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
472 struct gameport
*gameport
= to_gameport_port(dev
);
473 struct device_driver
*drv
;
476 error
= mutex_lock_interruptible(&gameport_mutex
);
480 if (!strncmp(buf
, "none", count
)) {
481 gameport_disconnect_port(gameport
);
482 } else if (!strncmp(buf
, "reconnect", count
)) {
483 gameport_reconnect_port(gameport
);
484 } else if (!strncmp(buf
, "rescan", count
)) {
485 gameport_disconnect_port(gameport
);
486 gameport_find_driver(gameport
);
487 } else if ((drv
= driver_find(buf
, &gameport_bus
)) != NULL
) {
488 gameport_disconnect_port(gameport
);
489 error
= gameport_bind_driver(gameport
, to_gameport_driver(drv
));
494 mutex_unlock(&gameport_mutex
);
496 return error
? error
: count
;
498 static DEVICE_ATTR_WO(drvctl
);
500 static struct attribute
*gameport_device_attrs
[] = {
501 &dev_attr_description
.attr
,
502 &dev_attr_drvctl
.attr
,
505 ATTRIBUTE_GROUPS(gameport_device
);
507 static void gameport_release_port(struct device
*dev
)
509 struct gameport
*gameport
= to_gameport_port(dev
);
512 module_put(THIS_MODULE
);
515 void gameport_set_phys(struct gameport
*gameport
, const char *fmt
, ...)
520 vsnprintf(gameport
->phys
, sizeof(gameport
->phys
), fmt
, args
);
523 EXPORT_SYMBOL(gameport_set_phys
);
526 * Prepare gameport port for registration.
528 static void gameport_init_port(struct gameport
*gameport
)
530 static atomic_t gameport_no
= ATOMIC_INIT(-1);
532 __module_get(THIS_MODULE
);
534 mutex_init(&gameport
->drv_mutex
);
535 device_initialize(&gameport
->dev
);
536 dev_set_name(&gameport
->dev
, "gameport%lu",
537 (unsigned long)atomic_inc_return(&gameport_no
));
538 gameport
->dev
.bus
= &gameport_bus
;
539 gameport
->dev
.release
= gameport_release_port
;
540 if (gameport
->parent
)
541 gameport
->dev
.parent
= &gameport
->parent
->dev
;
543 INIT_LIST_HEAD(&gameport
->node
);
544 spin_lock_init(&gameport
->timer_lock
);
545 timer_setup(&gameport
->poll_timer
, gameport_run_poll_handler
, 0);
549 * Complete gameport port registration.
550 * Driver core will attempt to find appropriate driver for the port.
552 static void gameport_add_port(struct gameport
*gameport
)
556 if (gameport
->parent
)
557 gameport
->parent
->child
= gameport
;
559 gameport
->speed
= use_ktime
?
560 gameport_measure_speed(gameport
) :
561 old_gameport_measure_speed(gameport
);
563 list_add_tail(&gameport
->node
, &gameport_list
);
566 dev_info(&gameport
->dev
, "%s is %s, io %#x, speed %dkHz\n",
567 gameport
->name
, gameport
->phys
, gameport
->io
, gameport
->speed
);
569 dev_info(&gameport
->dev
, "%s is %s, speed %dkHz\n",
570 gameport
->name
, gameport
->phys
, gameport
->speed
);
572 error
= device_add(&gameport
->dev
);
574 dev_err(&gameport
->dev
,
575 "device_add() failed for %s (%s), error: %d\n",
576 gameport
->phys
, gameport
->name
, error
);
580 * gameport_destroy_port() completes deregistration process and removes
581 * port from the system
583 static void gameport_destroy_port(struct gameport
*gameport
)
585 struct gameport
*child
;
587 child
= gameport_get_pending_child(gameport
);
589 gameport_remove_pending_events(child
);
590 put_device(&child
->dev
);
593 if (gameport
->parent
) {
594 gameport
->parent
->child
= NULL
;
595 gameport
->parent
= NULL
;
598 if (device_is_registered(&gameport
->dev
))
599 device_del(&gameport
->dev
);
601 list_del_init(&gameport
->node
);
603 gameport_remove_pending_events(gameport
);
604 put_device(&gameport
->dev
);
608 * Reconnect gameport port and all its children (re-initialize attached devices)
610 static void gameport_reconnect_port(struct gameport
*gameport
)
613 if (!gameport
->drv
|| !gameport
->drv
->reconnect
|| gameport
->drv
->reconnect(gameport
)) {
614 gameport_disconnect_port(gameport
);
615 gameport_find_driver(gameport
);
616 /* Ok, old children are now gone, we are done */
619 gameport
= gameport
->child
;
624 * gameport_disconnect_port() unbinds a port from its driver. As a side effect
625 * all child ports are unbound and destroyed.
627 static void gameport_disconnect_port(struct gameport
*gameport
)
629 struct gameport
*s
, *parent
;
631 if (gameport
->child
) {
633 * Children ports should be disconnected and destroyed
634 * first, staring with the leaf one, since we don't want
637 for (s
= gameport
; s
->child
; s
= s
->child
)
643 device_release_driver(&s
->dev
);
644 gameport_destroy_port(s
);
645 } while ((s
= parent
) != gameport
);
649 * Ok, no children left, now disconnect this port
651 device_release_driver(&gameport
->dev
);
655 * Submits register request to kgameportd for subsequent execution.
656 * Note that port registration is always asynchronous.
658 void __gameport_register_port(struct gameport
*gameport
, struct module
*owner
)
660 gameport_init_port(gameport
);
661 gameport_queue_event(gameport
, owner
, GAMEPORT_REGISTER_PORT
);
663 EXPORT_SYMBOL(__gameport_register_port
);
666 * Synchronously unregisters gameport port.
668 void gameport_unregister_port(struct gameport
*gameport
)
670 mutex_lock(&gameport_mutex
);
671 gameport_disconnect_port(gameport
);
672 gameport_destroy_port(gameport
);
673 mutex_unlock(&gameport_mutex
);
675 EXPORT_SYMBOL(gameport_unregister_port
);
679 * Gameport driver operations
682 static ssize_t
description_show(struct device_driver
*drv
, char *buf
)
684 struct gameport_driver
*driver
= to_gameport_driver(drv
);
685 return sprintf(buf
, "%s\n", driver
->description
? driver
->description
: "(none)");
687 static DRIVER_ATTR_RO(description
);
689 static struct attribute
*gameport_driver_attrs
[] = {
690 &driver_attr_description
.attr
,
693 ATTRIBUTE_GROUPS(gameport_driver
);
695 static int gameport_driver_probe(struct device
*dev
)
697 struct gameport
*gameport
= to_gameport_port(dev
);
698 struct gameport_driver
*drv
= to_gameport_driver(dev
->driver
);
700 drv
->connect(gameport
, drv
);
701 return gameport
->drv
? 0 : -ENODEV
;
704 static int gameport_driver_remove(struct device
*dev
)
706 struct gameport
*gameport
= to_gameport_port(dev
);
707 struct gameport_driver
*drv
= to_gameport_driver(dev
->driver
);
709 drv
->disconnect(gameport
);
713 static void gameport_attach_driver(struct gameport_driver
*drv
)
717 error
= driver_attach(&drv
->driver
);
719 pr_err("driver_attach() failed for %s, error: %d\n",
720 drv
->driver
.name
, error
);
723 int __gameport_register_driver(struct gameport_driver
*drv
, struct module
*owner
,
724 const char *mod_name
)
728 drv
->driver
.bus
= &gameport_bus
;
729 drv
->driver
.owner
= owner
;
730 drv
->driver
.mod_name
= mod_name
;
733 * Temporarily disable automatic binding because probing
734 * takes long time and we are better off doing it in kgameportd
738 error
= driver_register(&drv
->driver
);
740 pr_err("driver_register() failed for %s, error: %d\n",
741 drv
->driver
.name
, error
);
746 * Reset ignore flag and let kgameportd bind the driver to free ports
749 error
= gameport_queue_event(drv
, NULL
, GAMEPORT_ATTACH_DRIVER
);
751 driver_unregister(&drv
->driver
);
757 EXPORT_SYMBOL(__gameport_register_driver
);
759 void gameport_unregister_driver(struct gameport_driver
*drv
)
761 struct gameport
*gameport
;
763 mutex_lock(&gameport_mutex
);
765 drv
->ignore
= true; /* so gameport_find_driver ignores it */
766 gameport_remove_pending_events(drv
);
769 list_for_each_entry(gameport
, &gameport_list
, node
) {
770 if (gameport
->drv
== drv
) {
771 gameport_disconnect_port(gameport
);
772 gameport_find_driver(gameport
);
773 /* we could've deleted some ports, restart */
778 driver_unregister(&drv
->driver
);
780 mutex_unlock(&gameport_mutex
);
782 EXPORT_SYMBOL(gameport_unregister_driver
);
784 static int gameport_bus_match(struct device
*dev
, struct device_driver
*drv
)
786 struct gameport_driver
*gameport_drv
= to_gameport_driver(drv
);
788 return !gameport_drv
->ignore
;
791 static struct bus_type gameport_bus
= {
793 .dev_groups
= gameport_device_groups
,
794 .drv_groups
= gameport_driver_groups
,
795 .match
= gameport_bus_match
,
796 .probe
= gameport_driver_probe
,
797 .remove
= gameport_driver_remove
,
800 static void gameport_set_drv(struct gameport
*gameport
, struct gameport_driver
*drv
)
802 mutex_lock(&gameport
->drv_mutex
);
804 mutex_unlock(&gameport
->drv_mutex
);
807 int gameport_open(struct gameport
*gameport
, struct gameport_driver
*drv
, int mode
)
809 if (gameport
->open
) {
810 if (gameport
->open(gameport
, mode
)) {
814 if (mode
!= GAMEPORT_MODE_RAW
)
818 gameport_set_drv(gameport
, drv
);
821 EXPORT_SYMBOL(gameport_open
);
823 void gameport_close(struct gameport
*gameport
)
825 del_timer_sync(&gameport
->poll_timer
);
826 gameport
->poll_handler
= NULL
;
827 gameport
->poll_interval
= 0;
828 gameport_set_drv(gameport
, NULL
);
830 gameport
->close(gameport
);
832 EXPORT_SYMBOL(gameport_close
);
834 static int __init
gameport_init(void)
838 error
= bus_register(&gameport_bus
);
840 pr_err("failed to register gameport bus, error: %d\n", error
);
848 static void __exit
gameport_exit(void)
850 bus_unregister(&gameport_bus
);
853 * There should not be any outstanding events but work may
854 * still be scheduled so simply cancel it.
856 cancel_work_sync(&gameport_event_work
);
859 subsys_initcall(gameport_init
);
860 module_exit(gameport_exit
);