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/sched.h>
21 #include <linux/slab.h>
22 #include <linux/delay.h>
23 #include <linux/kthread.h>
25 /*#include <asm/io.h>*/
27 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
28 MODULE_DESCRIPTION("Generic gameport layer");
29 MODULE_LICENSE("GPL");
31 EXPORT_SYMBOL(__gameport_register_port
);
32 EXPORT_SYMBOL(gameport_unregister_port
);
33 EXPORT_SYMBOL(__gameport_register_driver
);
34 EXPORT_SYMBOL(gameport_unregister_driver
);
35 EXPORT_SYMBOL(gameport_open
);
36 EXPORT_SYMBOL(gameport_close
);
37 EXPORT_SYMBOL(gameport_rescan
);
38 EXPORT_SYMBOL(gameport_cooked_read
);
39 EXPORT_SYMBOL(gameport_set_name
);
40 EXPORT_SYMBOL(gameport_set_phys
);
41 EXPORT_SYMBOL(gameport_start_polling
);
42 EXPORT_SYMBOL(gameport_stop_polling
);
45 * gameport_sem protects entire gameport subsystem and is taken
46 * every time gameport port or driver registrered or unregistered.
48 static DECLARE_MUTEX(gameport_sem
);
50 static LIST_HEAD(gameport_list
);
52 static struct bus_type gameport_bus
= {
56 static void gameport_add_port(struct gameport
*gameport
);
57 static void gameport_destroy_port(struct gameport
*gameport
);
58 static void gameport_reconnect_port(struct gameport
*gameport
);
59 static void gameport_disconnect_port(struct gameport
*gameport
);
63 #include <asm/i8253.h>
65 #define DELTA(x,y) ((y)-(x)+((y)<(x)?1193182/HZ:0))
66 #define GET_TIME(x) do { x = get_time_pit(); } while (0)
68 static unsigned int get_time_pit(void)
73 spin_lock_irqsave(&i8253_lock
, flags
);
76 count
|= inb_p(0x40) << 8;
77 spin_unlock_irqrestore(&i8253_lock
, flags
);
87 * gameport_measure_speed() measures the gameport i/o speed.
90 static int gameport_measure_speed(struct gameport
*gameport
)
94 unsigned int i
, t
, t1
, t2
, t3
, tx
;
97 if (gameport_open(gameport
, NULL
, GAMEPORT_MODE_RAW
))
102 for(i
= 0; i
< 50; i
++) {
103 local_irq_save(flags
);
105 for (t
= 0; t
< 50; t
++) gameport_read(gameport
);
108 local_irq_restore(flags
);
110 if ((t
= DELTA(t2
,t1
) - DELTA(t3
,t2
)) < tx
) tx
= t
;
113 gameport_close(gameport
);
114 return 59659 / (tx
< 1 ? 1 : tx
);
116 #elif defined (__x86_64__)
119 unsigned long tx
, t1
, t2
, flags
;
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
);
131 local_irq_restore(flags
);
133 if (t2
- t1
< tx
) tx
= t2
- t1
;
136 gameport_close(gameport
);
137 return (cpu_data
[raw_smp_processor_id()].loops_per_jiffy
* (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 void gameport_bind_driver(struct gameport
*gameport
, struct gameport_driver
*drv
)
193 down_write(&gameport_bus
.subsys
.rwsem
);
195 gameport
->dev
.driver
= &drv
->driver
;
196 if (drv
->connect(gameport
, drv
)) {
197 gameport
->dev
.driver
= NULL
;
200 device_bind_driver(&gameport
->dev
);
202 up_write(&gameport_bus
.subsys
.rwsem
);
205 static void gameport_release_driver(struct gameport
*gameport
)
207 down_write(&gameport_bus
.subsys
.rwsem
);
208 device_release_driver(&gameport
->dev
);
209 up_write(&gameport_bus
.subsys
.rwsem
);
212 static void gameport_find_driver(struct gameport
*gameport
)
214 down_write(&gameport_bus
.subsys
.rwsem
);
215 device_attach(&gameport
->dev
);
216 up_write(&gameport_bus
.subsys
.rwsem
);
221 * Gameport event processing.
224 enum gameport_event_type
{
227 GAMEPORT_REGISTER_PORT
,
228 GAMEPORT_REGISTER_DRIVER
,
231 struct gameport_event
{
232 enum gameport_event_type type
;
234 struct module
*owner
;
235 struct list_head node
;
238 static DEFINE_SPINLOCK(gameport_event_lock
); /* protects gameport_event_list */
239 static LIST_HEAD(gameport_event_list
);
240 static DECLARE_WAIT_QUEUE_HEAD(gameport_wait
);
241 static struct task_struct
*gameport_task
;
243 static void gameport_queue_event(void *object
, struct module
*owner
,
244 enum gameport_event_type event_type
)
247 struct gameport_event
*event
;
249 spin_lock_irqsave(&gameport_event_lock
, flags
);
252 * Scan event list for the other events for the same gameport port,
253 * starting with the most recent one. If event is the same we
254 * do not need add new one. If event is of different type we
255 * need to add this event and should not look further because
256 * we need to preseve sequence of distinct events.
258 list_for_each_entry_reverse(event
, &gameport_event_list
, node
) {
259 if (event
->object
== object
) {
260 if (event
->type
== event_type
)
266 if ((event
= kmalloc(sizeof(struct gameport_event
), GFP_ATOMIC
))) {
267 if (!try_module_get(owner
)) {
268 printk(KERN_WARNING
"gameport: Can't get module reference, dropping event %d\n", event_type
);
272 event
->type
= event_type
;
273 event
->object
= object
;
274 event
->owner
= owner
;
276 list_add_tail(&event
->node
, &gameport_event_list
);
277 wake_up(&gameport_wait
);
279 printk(KERN_ERR
"gameport: Not enough memory to queue event %d\n", event_type
);
282 spin_unlock_irqrestore(&gameport_event_lock
, flags
);
285 static void gameport_free_event(struct gameport_event
*event
)
287 module_put(event
->owner
);
291 static void gameport_remove_duplicate_events(struct gameport_event
*event
)
293 struct list_head
*node
, *next
;
294 struct gameport_event
*e
;
297 spin_lock_irqsave(&gameport_event_lock
, flags
);
299 list_for_each_safe(node
, next
, &gameport_event_list
) {
300 e
= list_entry(node
, struct gameport_event
, node
);
301 if (event
->object
== e
->object
) {
303 * If this event is of different type we should not
304 * look further - we only suppress duplicate events
305 * that were sent back-to-back.
307 if (event
->type
!= e
->type
)
311 gameport_free_event(e
);
315 spin_unlock_irqrestore(&gameport_event_lock
, flags
);
319 static struct gameport_event
*gameport_get_event(void)
321 struct gameport_event
*event
;
322 struct list_head
*node
;
325 spin_lock_irqsave(&gameport_event_lock
, flags
);
327 if (list_empty(&gameport_event_list
)) {
328 spin_unlock_irqrestore(&gameport_event_lock
, flags
);
332 node
= gameport_event_list
.next
;
333 event
= list_entry(node
, struct gameport_event
, node
);
336 spin_unlock_irqrestore(&gameport_event_lock
, flags
);
341 static void gameport_handle_events(void)
343 struct gameport_event
*event
;
344 struct gameport_driver
*gameport_drv
;
348 while ((event
= gameport_get_event())) {
350 switch (event
->type
) {
351 case GAMEPORT_REGISTER_PORT
:
352 gameport_add_port(event
->object
);
355 case GAMEPORT_RECONNECT
:
356 gameport_reconnect_port(event
->object
);
359 case GAMEPORT_RESCAN
:
360 gameport_disconnect_port(event
->object
);
361 gameport_find_driver(event
->object
);
364 case GAMEPORT_REGISTER_DRIVER
:
365 gameport_drv
= event
->object
;
366 driver_register(&gameport_drv
->driver
);
373 gameport_remove_duplicate_events(event
);
374 gameport_free_event(event
);
381 * Remove all events that have been submitted for a given gameport port.
383 static void gameport_remove_pending_events(struct gameport
*gameport
)
385 struct list_head
*node
, *next
;
386 struct gameport_event
*event
;
389 spin_lock_irqsave(&gameport_event_lock
, flags
);
391 list_for_each_safe(node
, next
, &gameport_event_list
) {
392 event
= list_entry(node
, struct gameport_event
, node
);
393 if (event
->object
== gameport
) {
395 gameport_free_event(event
);
399 spin_unlock_irqrestore(&gameport_event_lock
, flags
);
403 * Destroy child gameport port (if any) that has not been fully registered yet.
405 * Note that we rely on the fact that port can have only one child and therefore
406 * only one child registration request can be pending. Additionally, children
407 * are registered by driver's connect() handler so there can't be a grandchild
408 * pending registration together with a child.
410 static struct gameport
*gameport_get_pending_child(struct gameport
*parent
)
412 struct gameport_event
*event
;
413 struct gameport
*gameport
, *child
= NULL
;
416 spin_lock_irqsave(&gameport_event_lock
, flags
);
418 list_for_each_entry(event
, &gameport_event_list
, node
) {
419 if (event
->type
== GAMEPORT_REGISTER_PORT
) {
420 gameport
= event
->object
;
421 if (gameport
->parent
== parent
) {
428 spin_unlock_irqrestore(&gameport_event_lock
, flags
);
432 static int gameport_thread(void *nothing
)
435 gameport_handle_events();
436 wait_event_interruptible(gameport_wait
,
437 kthread_should_stop() || !list_empty(&gameport_event_list
));
439 } while (!kthread_should_stop());
441 printk(KERN_DEBUG
"gameport: kgameportd exiting\n");
447 * Gameport port operations
450 static ssize_t
gameport_show_description(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
452 struct gameport
*gameport
= to_gameport_port(dev
);
453 return sprintf(buf
, "%s\n", gameport
->name
);
456 static ssize_t
gameport_rebind_driver(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
458 struct gameport
*gameport
= to_gameport_port(dev
);
459 struct device_driver
*drv
;
462 retval
= down_interruptible(&gameport_sem
);
467 if (!strncmp(buf
, "none", count
)) {
468 gameport_disconnect_port(gameport
);
469 } else if (!strncmp(buf
, "reconnect", count
)) {
470 gameport_reconnect_port(gameport
);
471 } else if (!strncmp(buf
, "rescan", count
)) {
472 gameport_disconnect_port(gameport
);
473 gameport_find_driver(gameport
);
474 } else if ((drv
= driver_find(buf
, &gameport_bus
)) != NULL
) {
475 gameport_disconnect_port(gameport
);
476 gameport_bind_driver(gameport
, to_gameport_driver(drv
));
487 static struct device_attribute gameport_device_attrs
[] = {
488 __ATTR(description
, S_IRUGO
, gameport_show_description
, NULL
),
489 __ATTR(drvctl
, S_IWUSR
, NULL
, gameport_rebind_driver
),
493 static void gameport_release_port(struct device
*dev
)
495 struct gameport
*gameport
= to_gameport_port(dev
);
498 module_put(THIS_MODULE
);
501 void gameport_set_phys(struct gameport
*gameport
, const char *fmt
, ...)
506 vsnprintf(gameport
->phys
, sizeof(gameport
->phys
), fmt
, args
);
511 * Prepare gameport port for registration.
513 static void gameport_init_port(struct gameport
*gameport
)
515 static atomic_t gameport_no
= ATOMIC_INIT(0);
517 __module_get(THIS_MODULE
);
519 init_MUTEX(&gameport
->drv_sem
);
520 device_initialize(&gameport
->dev
);
521 snprintf(gameport
->dev
.bus_id
, sizeof(gameport
->dev
.bus_id
),
522 "gameport%lu", (unsigned long)atomic_inc_return(&gameport_no
) - 1);
523 gameport
->dev
.bus
= &gameport_bus
;
524 gameport
->dev
.release
= gameport_release_port
;
525 if (gameport
->parent
)
526 gameport
->dev
.parent
= &gameport
->parent
->dev
;
528 spin_lock_init(&gameport
->timer_lock
);
529 init_timer(&gameport
->poll_timer
);
530 gameport
->poll_timer
.function
= gameport_run_poll_handler
;
531 gameport
->poll_timer
.data
= (unsigned long)gameport
;
535 * Complete gameport port registration.
536 * Driver core will attempt to find appropriate driver for the port.
538 static void gameport_add_port(struct gameport
*gameport
)
540 if (gameport
->parent
)
541 gameport
->parent
->child
= gameport
;
543 gameport
->speed
= gameport_measure_speed(gameport
);
545 list_add_tail(&gameport
->node
, &gameport_list
);
548 printk(KERN_INFO
"gameport: %s is %s, io %#x, speed %dkHz\n",
549 gameport
->name
, gameport
->phys
, gameport
->io
, gameport
->speed
);
551 printk(KERN_INFO
"gameport: %s is %s, speed %dkHz\n",
552 gameport
->name
, gameport
->phys
, gameport
->speed
);
554 device_add(&gameport
->dev
);
555 gameport
->registered
= 1;
559 * gameport_destroy_port() completes deregistration process and removes
560 * port from the system
562 static void gameport_destroy_port(struct gameport
*gameport
)
564 struct gameport
*child
;
566 child
= gameport_get_pending_child(gameport
);
568 gameport_remove_pending_events(child
);
569 put_device(&child
->dev
);
572 if (gameport
->parent
) {
573 gameport
->parent
->child
= NULL
;
574 gameport
->parent
= NULL
;
577 if (gameport
->registered
) {
578 device_del(&gameport
->dev
);
579 list_del_init(&gameport
->node
);
580 gameport
->registered
= 0;
583 gameport_remove_pending_events(gameport
);
584 put_device(&gameport
->dev
);
588 * Reconnect gameport port and all its children (re-initialize attached devices)
590 static void gameport_reconnect_port(struct gameport
*gameport
)
593 if (!gameport
->drv
|| !gameport
->drv
->reconnect
|| gameport
->drv
->reconnect(gameport
)) {
594 gameport_disconnect_port(gameport
);
595 gameport_find_driver(gameport
);
596 /* Ok, old children are now gone, we are done */
599 gameport
= gameport
->child
;
604 * gameport_disconnect_port() unbinds a port from its driver. As a side effect
605 * all child ports are unbound and destroyed.
607 static void gameport_disconnect_port(struct gameport
*gameport
)
609 struct gameport
*s
, *parent
;
611 if (gameport
->child
) {
613 * Children ports should be disconnected and destroyed
614 * first, staring with the leaf one, since we don't want
617 for (s
= gameport
; s
->child
; s
= s
->child
)
623 gameport_release_driver(s
);
624 gameport_destroy_port(s
);
625 } while ((s
= parent
) != gameport
);
629 * Ok, no children left, now disconnect this port
631 gameport_release_driver(gameport
);
634 void gameport_rescan(struct gameport
*gameport
)
636 gameport_queue_event(gameport
, NULL
, GAMEPORT_RESCAN
);
639 void gameport_reconnect(struct gameport
*gameport
)
641 gameport_queue_event(gameport
, NULL
, GAMEPORT_RECONNECT
);
645 * Submits register request to kgameportd for subsequent execution.
646 * Note that port registration is always asynchronous.
648 void __gameport_register_port(struct gameport
*gameport
, struct module
*owner
)
650 gameport_init_port(gameport
);
651 gameport_queue_event(gameport
, owner
, GAMEPORT_REGISTER_PORT
);
655 * Synchronously unregisters gameport port.
657 void gameport_unregister_port(struct gameport
*gameport
)
660 gameport_disconnect_port(gameport
);
661 gameport_destroy_port(gameport
);
667 * Gameport driver operations
670 static ssize_t
gameport_driver_show_description(struct device_driver
*drv
, char *buf
)
672 struct gameport_driver
*driver
= to_gameport_driver(drv
);
673 return sprintf(buf
, "%s\n", driver
->description
? driver
->description
: "(none)");
676 static struct driver_attribute gameport_driver_attrs
[] = {
677 __ATTR(description
, S_IRUGO
, gameport_driver_show_description
, NULL
),
681 static int gameport_driver_probe(struct device
*dev
)
683 struct gameport
*gameport
= to_gameport_port(dev
);
684 struct gameport_driver
*drv
= to_gameport_driver(dev
->driver
);
686 drv
->connect(gameport
, drv
);
687 return gameport
->drv
? 0 : -ENODEV
;
690 static int gameport_driver_remove(struct device
*dev
)
692 struct gameport
*gameport
= to_gameport_port(dev
);
693 struct gameport_driver
*drv
= to_gameport_driver(dev
->driver
);
695 drv
->disconnect(gameport
);
699 void __gameport_register_driver(struct gameport_driver
*drv
, struct module
*owner
)
701 drv
->driver
.bus
= &gameport_bus
;
702 drv
->driver
.probe
= gameport_driver_probe
;
703 drv
->driver
.remove
= gameport_driver_remove
;
704 gameport_queue_event(drv
, owner
, GAMEPORT_REGISTER_DRIVER
);
707 void gameport_unregister_driver(struct gameport_driver
*drv
)
709 struct gameport
*gameport
;
712 drv
->ignore
= 1; /* so gameport_find_driver ignores it */
715 list_for_each_entry(gameport
, &gameport_list
, node
) {
716 if (gameport
->drv
== drv
) {
717 gameport_disconnect_port(gameport
);
718 gameport_find_driver(gameport
);
719 /* we could've deleted some ports, restart */
724 driver_unregister(&drv
->driver
);
728 static int gameport_bus_match(struct device
*dev
, struct device_driver
*drv
)
730 struct gameport_driver
*gameport_drv
= to_gameport_driver(drv
);
732 return !gameport_drv
->ignore
;
735 static void gameport_set_drv(struct gameport
*gameport
, struct gameport_driver
*drv
)
737 down(&gameport
->drv_sem
);
739 up(&gameport
->drv_sem
);
742 int gameport_open(struct gameport
*gameport
, struct gameport_driver
*drv
, int mode
)
745 if (gameport
->open
) {
746 if (gameport
->open(gameport
, mode
)) {
750 if (mode
!= GAMEPORT_MODE_RAW
)
754 gameport_set_drv(gameport
, drv
);
758 void gameport_close(struct gameport
*gameport
)
760 del_timer_sync(&gameport
->poll_timer
);
761 gameport
->poll_handler
= NULL
;
762 gameport
->poll_interval
= 0;
763 gameport_set_drv(gameport
, NULL
);
765 gameport
->close(gameport
);
768 static int __init
gameport_init(void)
770 gameport_task
= kthread_run(gameport_thread
, NULL
, "kgameportd");
771 if (IS_ERR(gameport_task
)) {
772 printk(KERN_ERR
"gameport: Failed to start kgameportd\n");
773 return PTR_ERR(gameport_task
);
776 gameport_bus
.dev_attrs
= gameport_device_attrs
;
777 gameport_bus
.drv_attrs
= gameport_driver_attrs
;
778 gameport_bus
.match
= gameport_bus_match
;
779 bus_register(&gameport_bus
);
784 static void __exit
gameport_exit(void)
786 bus_unregister(&gameport_bus
);
787 kthread_stop(gameport_task
);
790 module_init(gameport_init
);
791 module_exit(gameport_exit
);