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 #include <linux/stddef.h>
15 #include <linux/module.h>
16 #include <linux/ioport.h>
17 #include <linux/init.h>
18 #include <linux/gameport.h>
19 #include <linux/wait.h>
20 #include <linux/slab.h>
21 #include <linux/delay.h>
22 #include <linux/kthread.h>
23 #include <linux/sched.h> /* HZ */
24 #include <linux/mutex.h>
25 #include <linux/freezer.h>
27 /*#include <asm/io.h>*/
29 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
30 MODULE_DESCRIPTION("Generic gameport layer");
31 MODULE_LICENSE("GPL");
33 EXPORT_SYMBOL(__gameport_register_port
);
34 EXPORT_SYMBOL(gameport_unregister_port
);
35 EXPORT_SYMBOL(__gameport_register_driver
);
36 EXPORT_SYMBOL(gameport_unregister_driver
);
37 EXPORT_SYMBOL(gameport_open
);
38 EXPORT_SYMBOL(gameport_close
);
39 EXPORT_SYMBOL(gameport_rescan
);
40 EXPORT_SYMBOL(gameport_set_phys
);
41 EXPORT_SYMBOL(gameport_start_polling
);
42 EXPORT_SYMBOL(gameport_stop_polling
);
45 * gameport_mutex protects entire gameport subsystem and is taken
46 * every time gameport port or driver registrered or unregistered.
48 static DEFINE_MUTEX(gameport_mutex
);
50 static LIST_HEAD(gameport_list
);
52 static struct bus_type gameport_bus
;
54 static void gameport_add_driver(struct gameport_driver
*drv
);
55 static void gameport_add_port(struct gameport
*gameport
);
56 static void gameport_destroy_port(struct gameport
*gameport
);
57 static void gameport_reconnect_port(struct gameport
*gameport
);
58 static void gameport_disconnect_port(struct gameport
*gameport
);
62 #include <asm/i8253.h>
64 #define DELTA(x,y) ((y)-(x)+((y)<(x)?1193182/HZ:0))
65 #define GET_TIME(x) do { x = get_time_pit(); } while (0)
67 static unsigned int get_time_pit(void)
72 spin_lock_irqsave(&i8253_lock
, flags
);
75 count
|= inb_p(0x40) << 8;
76 spin_unlock_irqrestore(&i8253_lock
, flags
);
86 * gameport_measure_speed() measures the gameport i/o speed.
89 static int gameport_measure_speed(struct gameport
*gameport
)
93 unsigned int i
, t
, t1
, t2
, t3
, tx
;
96 if (gameport_open(gameport
, NULL
, GAMEPORT_MODE_RAW
))
101 for(i
= 0; i
< 50; i
++) {
102 local_irq_save(flags
);
104 for (t
= 0; t
< 50; t
++) gameport_read(gameport
);
107 local_irq_restore(flags
);
109 if ((t
= DELTA(t2
,t1
) - DELTA(t3
,t2
)) < tx
) tx
= t
;
112 gameport_close(gameport
);
113 return 59659 / (tx
< 1 ? 1 : tx
);
115 #elif defined (__x86_64__)
118 unsigned long tx
, t1
, t2
, flags
;
120 if (gameport_open(gameport
, NULL
, GAMEPORT_MODE_RAW
))
125 for(i
= 0; i
< 50; i
++) {
126 local_irq_save(flags
);
128 for (t
= 0; t
< 50; t
++) gameport_read(gameport
);
130 local_irq_restore(flags
);
132 if (t2
- t1
< tx
) tx
= t2
- t1
;
135 gameport_close(gameport
);
136 return (cpu_data(raw_smp_processor_id()).loops_per_jiffy
*
137 (unsigned long)HZ
/ (1000 / 50)) / (tx
< 1 ? 1 : tx
);
141 unsigned int j
, t
= 0;
143 if (gameport_open(gameport
, NULL
, GAMEPORT_MODE_RAW
))
146 j
= jiffies
; while (j
== jiffies
);
147 j
= jiffies
; while (j
== jiffies
) { t
++; gameport_read(gameport
); }
149 gameport_close(gameport
);
150 return t
* HZ
/ 1000;
155 void gameport_start_polling(struct gameport
*gameport
)
157 spin_lock(&gameport
->timer_lock
);
159 if (!gameport
->poll_cnt
++) {
160 BUG_ON(!gameport
->poll_handler
);
161 BUG_ON(!gameport
->poll_interval
);
162 mod_timer(&gameport
->poll_timer
, jiffies
+ msecs_to_jiffies(gameport
->poll_interval
));
165 spin_unlock(&gameport
->timer_lock
);
168 void gameport_stop_polling(struct gameport
*gameport
)
170 spin_lock(&gameport
->timer_lock
);
172 if (!--gameport
->poll_cnt
)
173 del_timer(&gameport
->poll_timer
);
175 spin_unlock(&gameport
->timer_lock
);
178 static void gameport_run_poll_handler(unsigned long d
)
180 struct gameport
*gameport
= (struct gameport
*)d
;
182 gameport
->poll_handler(gameport
);
183 if (gameport
->poll_cnt
)
184 mod_timer(&gameport
->poll_timer
, jiffies
+ msecs_to_jiffies(gameport
->poll_interval
));
188 * Basic gameport -> driver core mappings
191 static int gameport_bind_driver(struct gameport
*gameport
, struct gameport_driver
*drv
)
195 gameport
->dev
.driver
= &drv
->driver
;
196 if (drv
->connect(gameport
, drv
)) {
197 gameport
->dev
.driver
= NULL
;
201 error
= device_bind_driver(&gameport
->dev
);
204 "gameport: device_bind_driver() failed "
205 "for %s (%s) and %s, error: %d\n",
206 gameport
->phys
, gameport
->name
,
207 drv
->description
, error
);
208 drv
->disconnect(gameport
);
209 gameport
->dev
.driver
= NULL
;
216 static void gameport_find_driver(struct gameport
*gameport
)
220 error
= device_attach(&gameport
->dev
);
223 "gameport: device_attach() failed for %s (%s), error: %d\n",
224 gameport
->phys
, gameport
->name
, error
);
229 * Gameport event processing.
232 enum gameport_event_type
{
235 GAMEPORT_REGISTER_PORT
,
236 GAMEPORT_REGISTER_DRIVER
,
239 struct gameport_event
{
240 enum gameport_event_type type
;
242 struct module
*owner
;
243 struct list_head node
;
246 static DEFINE_SPINLOCK(gameport_event_lock
); /* protects gameport_event_list */
247 static LIST_HEAD(gameport_event_list
);
248 static DECLARE_WAIT_QUEUE_HEAD(gameport_wait
);
249 static struct task_struct
*gameport_task
;
251 static void gameport_queue_event(void *object
, struct module
*owner
,
252 enum gameport_event_type event_type
)
255 struct gameport_event
*event
;
257 spin_lock_irqsave(&gameport_event_lock
, flags
);
260 * Scan event list for the other events for the same gameport port,
261 * starting with the most recent one. If event is the same we
262 * do not need add new one. If event is of different type we
263 * need to add this event and should not look further because
264 * we need to preseve sequence of distinct events.
266 list_for_each_entry_reverse(event
, &gameport_event_list
, node
) {
267 if (event
->object
== object
) {
268 if (event
->type
== event_type
)
274 if ((event
= kmalloc(sizeof(struct gameport_event
), GFP_ATOMIC
))) {
275 if (!try_module_get(owner
)) {
276 printk(KERN_WARNING
"gameport: Can't get module reference, dropping event %d\n", event_type
);
281 event
->type
= event_type
;
282 event
->object
= object
;
283 event
->owner
= owner
;
285 list_add_tail(&event
->node
, &gameport_event_list
);
286 wake_up(&gameport_wait
);
288 printk(KERN_ERR
"gameport: Not enough memory to queue event %d\n", event_type
);
291 spin_unlock_irqrestore(&gameport_event_lock
, flags
);
294 static void gameport_free_event(struct gameport_event
*event
)
296 module_put(event
->owner
);
300 static void gameport_remove_duplicate_events(struct gameport_event
*event
)
302 struct list_head
*node
, *next
;
303 struct gameport_event
*e
;
306 spin_lock_irqsave(&gameport_event_lock
, flags
);
308 list_for_each_safe(node
, next
, &gameport_event_list
) {
309 e
= list_entry(node
, struct gameport_event
, node
);
310 if (event
->object
== e
->object
) {
312 * If this event is of different type we should not
313 * look further - we only suppress duplicate events
314 * that were sent back-to-back.
316 if (event
->type
!= e
->type
)
320 gameport_free_event(e
);
324 spin_unlock_irqrestore(&gameport_event_lock
, flags
);
327 static struct gameport_event
*gameport_get_event(void)
329 struct gameport_event
*event
;
330 struct list_head
*node
;
333 spin_lock_irqsave(&gameport_event_lock
, flags
);
335 if (list_empty(&gameport_event_list
)) {
336 spin_unlock_irqrestore(&gameport_event_lock
, flags
);
340 node
= gameport_event_list
.next
;
341 event
= list_entry(node
, struct gameport_event
, node
);
344 spin_unlock_irqrestore(&gameport_event_lock
, flags
);
349 static void gameport_handle_event(void)
351 struct gameport_event
*event
;
353 mutex_lock(&gameport_mutex
);
356 * Note that we handle only one event here to give swsusp
357 * a chance to freeze kgameportd thread. Gameport events
358 * should be pretty rare so we are not concerned about
359 * taking performance hit.
361 if ((event
= gameport_get_event())) {
363 switch (event
->type
) {
364 case GAMEPORT_REGISTER_PORT
:
365 gameport_add_port(event
->object
);
368 case GAMEPORT_RECONNECT
:
369 gameport_reconnect_port(event
->object
);
372 case GAMEPORT_RESCAN
:
373 gameport_disconnect_port(event
->object
);
374 gameport_find_driver(event
->object
);
377 case GAMEPORT_REGISTER_DRIVER
:
378 gameport_add_driver(event
->object
);
385 gameport_remove_duplicate_events(event
);
386 gameport_free_event(event
);
389 mutex_unlock(&gameport_mutex
);
393 * Remove all events that have been submitted for a given gameport port.
395 static void gameport_remove_pending_events(struct gameport
*gameport
)
397 struct list_head
*node
, *next
;
398 struct gameport_event
*event
;
401 spin_lock_irqsave(&gameport_event_lock
, flags
);
403 list_for_each_safe(node
, next
, &gameport_event_list
) {
404 event
= list_entry(node
, struct gameport_event
, node
);
405 if (event
->object
== gameport
) {
407 gameport_free_event(event
);
411 spin_unlock_irqrestore(&gameport_event_lock
, flags
);
415 * Destroy child gameport port (if any) that has not been fully registered yet.
417 * Note that we rely on the fact that port can have only one child and therefore
418 * only one child registration request can be pending. Additionally, children
419 * are registered by driver's connect() handler so there can't be a grandchild
420 * pending registration together with a child.
422 static struct gameport
*gameport_get_pending_child(struct gameport
*parent
)
424 struct gameport_event
*event
;
425 struct gameport
*gameport
, *child
= NULL
;
428 spin_lock_irqsave(&gameport_event_lock
, flags
);
430 list_for_each_entry(event
, &gameport_event_list
, node
) {
431 if (event
->type
== GAMEPORT_REGISTER_PORT
) {
432 gameport
= event
->object
;
433 if (gameport
->parent
== parent
) {
440 spin_unlock_irqrestore(&gameport_event_lock
, flags
);
444 static int gameport_thread(void *nothing
)
448 gameport_handle_event();
449 wait_event_freezable(gameport_wait
,
450 kthread_should_stop() || !list_empty(&gameport_event_list
));
451 } while (!kthread_should_stop());
453 printk(KERN_DEBUG
"gameport: kgameportd exiting\n");
459 * Gameport port operations
462 static ssize_t
gameport_show_description(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
464 struct gameport
*gameport
= to_gameport_port(dev
);
465 return sprintf(buf
, "%s\n", gameport
->name
);
468 static ssize_t
gameport_rebind_driver(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
470 struct gameport
*gameport
= to_gameport_port(dev
);
471 struct device_driver
*drv
;
474 error
= mutex_lock_interruptible(&gameport_mutex
);
478 if (!strncmp(buf
, "none", count
)) {
479 gameport_disconnect_port(gameport
);
480 } else if (!strncmp(buf
, "reconnect", count
)) {
481 gameport_reconnect_port(gameport
);
482 } else if (!strncmp(buf
, "rescan", count
)) {
483 gameport_disconnect_port(gameport
);
484 gameport_find_driver(gameport
);
485 } else if ((drv
= driver_find(buf
, &gameport_bus
)) != NULL
) {
486 gameport_disconnect_port(gameport
);
487 error
= gameport_bind_driver(gameport
, to_gameport_driver(drv
));
493 mutex_unlock(&gameport_mutex
);
495 return error
? error
: count
;
498 static struct device_attribute gameport_device_attrs
[] = {
499 __ATTR(description
, S_IRUGO
, gameport_show_description
, NULL
),
500 __ATTR(drvctl
, S_IWUSR
, NULL
, gameport_rebind_driver
),
504 static void gameport_release_port(struct device
*dev
)
506 struct gameport
*gameport
= to_gameport_port(dev
);
509 module_put(THIS_MODULE
);
512 void gameport_set_phys(struct gameport
*gameport
, const char *fmt
, ...)
517 vsnprintf(gameport
->phys
, sizeof(gameport
->phys
), fmt
, args
);
522 * Prepare gameport port for registration.
524 static void gameport_init_port(struct gameport
*gameport
)
526 static atomic_t gameport_no
= ATOMIC_INIT(0);
528 __module_get(THIS_MODULE
);
530 mutex_init(&gameport
->drv_mutex
);
531 device_initialize(&gameport
->dev
);
532 snprintf(gameport
->dev
.bus_id
, sizeof(gameport
->dev
.bus_id
),
533 "gameport%lu", (unsigned long)atomic_inc_return(&gameport_no
) - 1);
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 init_timer(&gameport
->poll_timer
);
542 gameport
->poll_timer
.function
= gameport_run_poll_handler
;
543 gameport
->poll_timer
.data
= (unsigned long)gameport
;
547 * Complete gameport port registration.
548 * Driver core will attempt to find appropriate driver for the port.
550 static void gameport_add_port(struct gameport
*gameport
)
554 if (gameport
->parent
)
555 gameport
->parent
->child
= gameport
;
557 gameport
->speed
= gameport_measure_speed(gameport
);
559 list_add_tail(&gameport
->node
, &gameport_list
);
562 printk(KERN_INFO
"gameport: %s is %s, io %#x, speed %dkHz\n",
563 gameport
->name
, gameport
->phys
, gameport
->io
, gameport
->speed
);
565 printk(KERN_INFO
"gameport: %s is %s, speed %dkHz\n",
566 gameport
->name
, gameport
->phys
, gameport
->speed
);
568 error
= device_add(&gameport
->dev
);
571 "gameport: device_add() failed for %s (%s), error: %d\n",
572 gameport
->phys
, gameport
->name
, error
);
574 gameport
->registered
= 1;
578 * gameport_destroy_port() completes deregistration process and removes
579 * port from the system
581 static void gameport_destroy_port(struct gameport
*gameport
)
583 struct gameport
*child
;
585 child
= gameport_get_pending_child(gameport
);
587 gameport_remove_pending_events(child
);
588 put_device(&child
->dev
);
591 if (gameport
->parent
) {
592 gameport
->parent
->child
= NULL
;
593 gameport
->parent
= NULL
;
596 if (gameport
->registered
) {
597 device_del(&gameport
->dev
);
598 gameport
->registered
= 0;
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
);
654 void gameport_rescan(struct gameport
*gameport
)
656 gameport_queue_event(gameport
, NULL
, GAMEPORT_RESCAN
);
659 void gameport_reconnect(struct gameport
*gameport
)
661 gameport_queue_event(gameport
, NULL
, GAMEPORT_RECONNECT
);
665 * Submits register request to kgameportd for subsequent execution.
666 * Note that port registration is always asynchronous.
668 void __gameport_register_port(struct gameport
*gameport
, struct module
*owner
)
670 gameport_init_port(gameport
);
671 gameport_queue_event(gameport
, owner
, GAMEPORT_REGISTER_PORT
);
675 * Synchronously unregisters gameport port.
677 void gameport_unregister_port(struct gameport
*gameport
)
679 mutex_lock(&gameport_mutex
);
680 gameport_disconnect_port(gameport
);
681 gameport_destroy_port(gameport
);
682 mutex_unlock(&gameport_mutex
);
687 * Gameport driver operations
690 static ssize_t
gameport_driver_show_description(struct device_driver
*drv
, char *buf
)
692 struct gameport_driver
*driver
= to_gameport_driver(drv
);
693 return sprintf(buf
, "%s\n", driver
->description
? driver
->description
: "(none)");
696 static struct driver_attribute gameport_driver_attrs
[] = {
697 __ATTR(description
, S_IRUGO
, gameport_driver_show_description
, NULL
),
701 static int gameport_driver_probe(struct device
*dev
)
703 struct gameport
*gameport
= to_gameport_port(dev
);
704 struct gameport_driver
*drv
= to_gameport_driver(dev
->driver
);
706 drv
->connect(gameport
, drv
);
707 return gameport
->drv
? 0 : -ENODEV
;
710 static int gameport_driver_remove(struct device
*dev
)
712 struct gameport
*gameport
= to_gameport_port(dev
);
713 struct gameport_driver
*drv
= to_gameport_driver(dev
->driver
);
715 drv
->disconnect(gameport
);
719 static void gameport_add_driver(struct gameport_driver
*drv
)
723 error
= driver_register(&drv
->driver
);
726 "gameport: driver_register() failed for %s, error: %d\n",
727 drv
->driver
.name
, error
);
730 void __gameport_register_driver(struct gameport_driver
*drv
, struct module
*owner
)
732 drv
->driver
.bus
= &gameport_bus
;
733 gameport_queue_event(drv
, owner
, GAMEPORT_REGISTER_DRIVER
);
736 void gameport_unregister_driver(struct gameport_driver
*drv
)
738 struct gameport
*gameport
;
740 mutex_lock(&gameport_mutex
);
741 drv
->ignore
= 1; /* so gameport_find_driver ignores it */
744 list_for_each_entry(gameport
, &gameport_list
, node
) {
745 if (gameport
->drv
== drv
) {
746 gameport_disconnect_port(gameport
);
747 gameport_find_driver(gameport
);
748 /* we could've deleted some ports, restart */
753 driver_unregister(&drv
->driver
);
754 mutex_unlock(&gameport_mutex
);
757 static int gameport_bus_match(struct device
*dev
, struct device_driver
*drv
)
759 struct gameport_driver
*gameport_drv
= to_gameport_driver(drv
);
761 return !gameport_drv
->ignore
;
764 static struct bus_type gameport_bus
= {
766 .dev_attrs
= gameport_device_attrs
,
767 .drv_attrs
= gameport_driver_attrs
,
768 .match
= gameport_bus_match
,
769 .probe
= gameport_driver_probe
,
770 .remove
= gameport_driver_remove
,
773 static void gameport_set_drv(struct gameport
*gameport
, struct gameport_driver
*drv
)
775 mutex_lock(&gameport
->drv_mutex
);
777 mutex_unlock(&gameport
->drv_mutex
);
780 int gameport_open(struct gameport
*gameport
, struct gameport_driver
*drv
, int mode
)
782 if (gameport
->open
) {
783 if (gameport
->open(gameport
, mode
)) {
787 if (mode
!= GAMEPORT_MODE_RAW
)
791 gameport_set_drv(gameport
, drv
);
795 void gameport_close(struct gameport
*gameport
)
797 del_timer_sync(&gameport
->poll_timer
);
798 gameport
->poll_handler
= NULL
;
799 gameport
->poll_interval
= 0;
800 gameport_set_drv(gameport
, NULL
);
802 gameport
->close(gameport
);
805 static int __init
gameport_init(void)
809 error
= bus_register(&gameport_bus
);
811 printk(KERN_ERR
"gameport: failed to register gameport bus, error: %d\n", error
);
815 gameport_task
= kthread_run(gameport_thread
, NULL
, "kgameportd");
816 if (IS_ERR(gameport_task
)) {
817 bus_unregister(&gameport_bus
);
818 error
= PTR_ERR(gameport_task
);
819 printk(KERN_ERR
"gameport: Failed to start kgameportd, error: %d\n", error
);
826 static void __exit
gameport_exit(void)
828 bus_unregister(&gameport_bus
);
829 kthread_stop(gameport_task
);
832 subsys_initcall(gameport_init
);
833 module_exit(gameport_exit
);