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/interrupt.h>
24 #include <linux/sched.h> /* HZ */
25 #include <linux/mutex.h>
26 #include <linux/freezer.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 EXPORT_SYMBOL(__gameport_register_port
);
35 EXPORT_SYMBOL(gameport_unregister_port
);
36 EXPORT_SYMBOL(__gameport_register_driver
);
37 EXPORT_SYMBOL(gameport_unregister_driver
);
38 EXPORT_SYMBOL(gameport_open
);
39 EXPORT_SYMBOL(gameport_close
);
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_port(struct gameport
*gameport
);
55 static void gameport_attach_driver(struct gameport_driver
*drv
);
56 static void gameport_reconnect_port(struct gameport
*gameport
);
57 static void gameport_disconnect_port(struct gameport
*gameport
);
61 #include <asm/i8253.h>
63 #define DELTA(x,y) ((y)-(x)+((y)<(x)?1193182/HZ:0))
64 #define GET_TIME(x) do { x = get_time_pit(); } while (0)
66 static unsigned int get_time_pit(void)
71 spin_lock_irqsave(&i8253_lock
, flags
);
74 count
|= inb_p(0x40) << 8;
75 spin_unlock_irqrestore(&i8253_lock
, flags
);
85 * gameport_measure_speed() measures the gameport i/o speed.
88 static int gameport_measure_speed(struct gameport
*gameport
)
92 unsigned int i
, t
, t1
, t2
, t3
, tx
;
95 if (gameport_open(gameport
, NULL
, GAMEPORT_MODE_RAW
))
100 for(i
= 0; i
< 50; i
++) {
101 local_irq_save_nort(flags
);
103 for (t
= 0; t
< 50; t
++) gameport_read(gameport
);
106 local_irq_restore_nort(flags
);
108 if ((t
= DELTA(t2
,t1
) - DELTA(t3
,t2
)) < tx
) tx
= t
;
111 gameport_close(gameport
);
112 return 59659 / (tx
< 1 ? 1 : tx
);
114 #elif defined (__x86_64__)
117 unsigned long tx
, t1
, t2
, flags
;
119 if (gameport_open(gameport
, NULL
, GAMEPORT_MODE_RAW
))
124 for(i
= 0; i
< 50; i
++) {
125 local_irq_save_nort(flags
);
127 for (t
= 0; t
< 50; t
++) gameport_read(gameport
);
129 local_irq_restore_nort(flags
);
131 if (t2
- t1
< tx
) tx
= t2
- t1
;
134 gameport_close(gameport
);
135 return (cpu_data(raw_smp_processor_id()).loops_per_jiffy
*
136 (unsigned long)HZ
/ (1000 / 50)) / (tx
< 1 ? 1 : tx
);
140 unsigned int j
, t
= 0;
142 if (gameport_open(gameport
, NULL
, GAMEPORT_MODE_RAW
))
145 j
= jiffies
; while (j
== jiffies
);
146 j
= jiffies
; while (j
== jiffies
) { t
++; gameport_read(gameport
); }
148 gameport_close(gameport
);
149 return t
* HZ
/ 1000;
154 void gameport_start_polling(struct gameport
*gameport
)
156 spin_lock(&gameport
->timer_lock
);
158 if (!gameport
->poll_cnt
++) {
159 BUG_ON(!gameport
->poll_handler
);
160 BUG_ON(!gameport
->poll_interval
);
161 mod_timer(&gameport
->poll_timer
, jiffies
+ msecs_to_jiffies(gameport
->poll_interval
));
164 spin_unlock(&gameport
->timer_lock
);
167 void gameport_stop_polling(struct gameport
*gameport
)
169 spin_lock(&gameport
->timer_lock
);
171 if (!--gameport
->poll_cnt
)
172 del_timer(&gameport
->poll_timer
);
174 spin_unlock(&gameport
->timer_lock
);
177 static void gameport_run_poll_handler(unsigned long d
)
179 struct gameport
*gameport
= (struct gameport
*)d
;
181 gameport
->poll_handler(gameport
);
182 if (gameport
->poll_cnt
)
183 mod_timer(&gameport
->poll_timer
, jiffies
+ msecs_to_jiffies(gameport
->poll_interval
));
187 * Basic gameport -> driver core mappings
190 static int gameport_bind_driver(struct gameport
*gameport
, struct gameport_driver
*drv
)
194 gameport
->dev
.driver
= &drv
->driver
;
195 if (drv
->connect(gameport
, drv
)) {
196 gameport
->dev
.driver
= NULL
;
200 error
= device_bind_driver(&gameport
->dev
);
203 "gameport: device_bind_driver() failed "
204 "for %s (%s) and %s, error: %d\n",
205 gameport
->phys
, gameport
->name
,
206 drv
->description
, error
);
207 drv
->disconnect(gameport
);
208 gameport
->dev
.driver
= NULL
;
215 static void gameport_find_driver(struct gameport
*gameport
)
219 error
= device_attach(&gameport
->dev
);
222 "gameport: device_attach() failed for %s (%s), error: %d\n",
223 gameport
->phys
, gameport
->name
, error
);
228 * Gameport event processing.
231 enum gameport_event_type
{
232 GAMEPORT_REGISTER_PORT
,
233 GAMEPORT_ATTACH_DRIVER
,
236 struct gameport_event
{
237 enum gameport_event_type type
;
239 struct module
*owner
;
240 struct list_head node
;
243 static DEFINE_SPINLOCK(gameport_event_lock
); /* protects gameport_event_list */
244 static LIST_HEAD(gameport_event_list
);
245 static DECLARE_WAIT_QUEUE_HEAD(gameport_wait
);
246 static struct task_struct
*gameport_task
;
248 static int gameport_queue_event(void *object
, struct module
*owner
,
249 enum gameport_event_type event_type
)
252 struct gameport_event
*event
;
255 spin_lock_irqsave(&gameport_event_lock
, flags
);
258 * Scan event list for the other events for the same gameport port,
259 * starting with the most recent one. If event is the same we
260 * do not need add new one. If event is of different type we
261 * need to add this event and should not look further because
262 * we need to preseve sequence of distinct events.
264 list_for_each_entry_reverse(event
, &gameport_event_list
, node
) {
265 if (event
->object
== object
) {
266 if (event
->type
== event_type
)
272 event
= kmalloc(sizeof(struct gameport_event
), GFP_ATOMIC
);
275 "gameport: Not enough memory to queue event %d\n",
281 if (!try_module_get(owner
)) {
283 "gameport: Can't get module reference, dropping event %d\n",
290 event
->type
= event_type
;
291 event
->object
= object
;
292 event
->owner
= owner
;
294 list_add_tail(&event
->node
, &gameport_event_list
);
295 wake_up(&gameport_wait
);
298 spin_unlock_irqrestore(&gameport_event_lock
, flags
);
302 static void gameport_free_event(struct gameport_event
*event
)
304 module_put(event
->owner
);
308 static void gameport_remove_duplicate_events(struct gameport_event
*event
)
310 struct list_head
*node
, *next
;
311 struct gameport_event
*e
;
314 spin_lock_irqsave(&gameport_event_lock
, flags
);
316 list_for_each_safe(node
, next
, &gameport_event_list
) {
317 e
= list_entry(node
, struct gameport_event
, node
);
318 if (event
->object
== e
->object
) {
320 * If this event is of different type we should not
321 * look further - we only suppress duplicate events
322 * that were sent back-to-back.
324 if (event
->type
!= e
->type
)
328 gameport_free_event(e
);
332 spin_unlock_irqrestore(&gameport_event_lock
, flags
);
335 static struct gameport_event
*gameport_get_event(void)
337 struct gameport_event
*event
;
338 struct list_head
*node
;
341 spin_lock_irqsave(&gameport_event_lock
, flags
);
343 if (list_empty(&gameport_event_list
)) {
344 spin_unlock_irqrestore(&gameport_event_lock
, flags
);
348 node
= gameport_event_list
.next
;
349 event
= list_entry(node
, struct gameport_event
, node
);
352 spin_unlock_irqrestore(&gameport_event_lock
, flags
);
357 static void gameport_handle_event(void)
359 struct gameport_event
*event
;
361 mutex_lock(&gameport_mutex
);
364 * Note that we handle only one event here to give swsusp
365 * a chance to freeze kgameportd thread. Gameport events
366 * should be pretty rare so we are not concerned about
367 * taking performance hit.
369 if ((event
= gameport_get_event())) {
371 switch (event
->type
) {
372 case GAMEPORT_REGISTER_PORT
:
373 gameport_add_port(event
->object
);
376 case GAMEPORT_ATTACH_DRIVER
:
377 gameport_attach_driver(event
->object
);
384 gameport_remove_duplicate_events(event
);
385 gameport_free_event(event
);
388 mutex_unlock(&gameport_mutex
);
392 * Remove all events that have been submitted for a given object,
393 * be it a gameport port or a driver.
395 static void gameport_remove_pending_events(void *object
)
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
== object
) {
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 dev_set_name(&gameport
->dev
, "gameport%lu", (unsigned long)atomic_inc_return(&gameport_no
) - 1);
533 gameport
->dev
.bus
= &gameport_bus
;
534 gameport
->dev
.release
= gameport_release_port
;
535 if (gameport
->parent
)
536 gameport
->dev
.parent
= &gameport
->parent
->dev
;
538 INIT_LIST_HEAD(&gameport
->node
);
539 spin_lock_init(&gameport
->timer_lock
);
540 init_timer(&gameport
->poll_timer
);
541 gameport
->poll_timer
.function
= gameport_run_poll_handler
;
542 gameport
->poll_timer
.data
= (unsigned long)gameport
;
546 * Complete gameport port registration.
547 * Driver core will attempt to find appropriate driver for the port.
549 static void gameport_add_port(struct gameport
*gameport
)
553 if (gameport
->parent
)
554 gameport
->parent
->child
= gameport
;
556 gameport
->speed
= gameport_measure_speed(gameport
);
558 list_add_tail(&gameport
->node
, &gameport_list
);
561 printk(KERN_INFO
"gameport: %s is %s, io %#x, speed %dkHz\n",
562 gameport
->name
, gameport
->phys
, gameport
->io
, gameport
->speed
);
564 printk(KERN_INFO
"gameport: %s is %s, speed %dkHz\n",
565 gameport
->name
, gameport
->phys
, gameport
->speed
);
567 error
= device_add(&gameport
->dev
);
570 "gameport: device_add() failed for %s (%s), error: %d\n",
571 gameport
->phys
, gameport
->name
, error
);
573 gameport
->registered
= 1;
577 * gameport_destroy_port() completes deregistration process and removes
578 * port from the system
580 static void gameport_destroy_port(struct gameport
*gameport
)
582 struct gameport
*child
;
584 child
= gameport_get_pending_child(gameport
);
586 gameport_remove_pending_events(child
);
587 put_device(&child
->dev
);
590 if (gameport
->parent
) {
591 gameport
->parent
->child
= NULL
;
592 gameport
->parent
= NULL
;
595 if (gameport
->registered
) {
596 device_del(&gameport
->dev
);
597 gameport
->registered
= 0;
600 list_del_init(&gameport
->node
);
602 gameport_remove_pending_events(gameport
);
603 put_device(&gameport
->dev
);
607 * Reconnect gameport port and all its children (re-initialize attached devices)
609 static void gameport_reconnect_port(struct gameport
*gameport
)
612 if (!gameport
->drv
|| !gameport
->drv
->reconnect
|| gameport
->drv
->reconnect(gameport
)) {
613 gameport_disconnect_port(gameport
);
614 gameport_find_driver(gameport
);
615 /* Ok, old children are now gone, we are done */
618 gameport
= gameport
->child
;
623 * gameport_disconnect_port() unbinds a port from its driver. As a side effect
624 * all child ports are unbound and destroyed.
626 static void gameport_disconnect_port(struct gameport
*gameport
)
628 struct gameport
*s
, *parent
;
630 if (gameport
->child
) {
632 * Children ports should be disconnected and destroyed
633 * first, staring with the leaf one, since we don't want
636 for (s
= gameport
; s
->child
; s
= s
->child
)
642 device_release_driver(&s
->dev
);
643 gameport_destroy_port(s
);
644 } while ((s
= parent
) != gameport
);
648 * Ok, no children left, now disconnect this port
650 device_release_driver(&gameport
->dev
);
654 * Submits register request to kgameportd for subsequent execution.
655 * Note that port registration is always asynchronous.
657 void __gameport_register_port(struct gameport
*gameport
, struct module
*owner
)
659 gameport_init_port(gameport
);
660 gameport_queue_event(gameport
, owner
, GAMEPORT_REGISTER_PORT
);
664 * Synchronously unregisters gameport port.
666 void gameport_unregister_port(struct gameport
*gameport
)
668 mutex_lock(&gameport_mutex
);
669 gameport_disconnect_port(gameport
);
670 gameport_destroy_port(gameport
);
671 mutex_unlock(&gameport_mutex
);
676 * Gameport driver operations
679 static ssize_t
gameport_driver_show_description(struct device_driver
*drv
, char *buf
)
681 struct gameport_driver
*driver
= to_gameport_driver(drv
);
682 return sprintf(buf
, "%s\n", driver
->description
? driver
->description
: "(none)");
685 static struct driver_attribute gameport_driver_attrs
[] = {
686 __ATTR(description
, S_IRUGO
, gameport_driver_show_description
, NULL
),
690 static int gameport_driver_probe(struct device
*dev
)
692 struct gameport
*gameport
= to_gameport_port(dev
);
693 struct gameport_driver
*drv
= to_gameport_driver(dev
->driver
);
695 drv
->connect(gameport
, drv
);
696 return gameport
->drv
? 0 : -ENODEV
;
699 static int gameport_driver_remove(struct device
*dev
)
701 struct gameport
*gameport
= to_gameport_port(dev
);
702 struct gameport_driver
*drv
= to_gameport_driver(dev
->driver
);
704 drv
->disconnect(gameport
);
708 static void gameport_attach_driver(struct gameport_driver
*drv
)
712 error
= driver_attach(&drv
->driver
);
715 "gameport: 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
);
737 "gameport: driver_register() failed for %s, error: %d\n",
738 drv
->driver
.name
, error
);
743 * Reset ignore flag and let kgameportd bind the driver to free ports
746 error
= gameport_queue_event(drv
, NULL
, GAMEPORT_ATTACH_DRIVER
);
748 driver_unregister(&drv
->driver
);
755 void gameport_unregister_driver(struct gameport_driver
*drv
)
757 struct gameport
*gameport
;
759 mutex_lock(&gameport_mutex
);
761 drv
->ignore
= 1; /* 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
);
779 static int gameport_bus_match(struct device
*dev
, struct device_driver
*drv
)
781 struct gameport_driver
*gameport_drv
= to_gameport_driver(drv
);
783 return !gameport_drv
->ignore
;
786 static struct bus_type gameport_bus
= {
788 .dev_attrs
= gameport_device_attrs
,
789 .drv_attrs
= gameport_driver_attrs
,
790 .match
= gameport_bus_match
,
791 .probe
= gameport_driver_probe
,
792 .remove
= gameport_driver_remove
,
795 static void gameport_set_drv(struct gameport
*gameport
, struct gameport_driver
*drv
)
797 mutex_lock(&gameport
->drv_mutex
);
799 mutex_unlock(&gameport
->drv_mutex
);
802 int gameport_open(struct gameport
*gameport
, struct gameport_driver
*drv
, int mode
)
804 if (gameport
->open
) {
805 if (gameport
->open(gameport
, mode
)) {
809 if (mode
!= GAMEPORT_MODE_RAW
)
813 gameport_set_drv(gameport
, drv
);
817 void gameport_close(struct gameport
*gameport
)
819 del_timer_sync(&gameport
->poll_timer
);
820 gameport
->poll_handler
= NULL
;
821 gameport
->poll_interval
= 0;
822 gameport_set_drv(gameport
, NULL
);
824 gameport
->close(gameport
);
827 static int __init
gameport_init(void)
831 error
= bus_register(&gameport_bus
);
833 printk(KERN_ERR
"gameport: failed to register gameport bus, error: %d\n", error
);
837 gameport_task
= kthread_run(gameport_thread
, NULL
, "kgameportd");
838 if (IS_ERR(gameport_task
)) {
839 bus_unregister(&gameport_bus
);
840 error
= PTR_ERR(gameport_task
);
841 printk(KERN_ERR
"gameport: Failed to start kgameportd, error: %d\n", error
);
848 static void __exit
gameport_exit(void)
850 bus_unregister(&gameport_bus
);
851 kthread_stop(gameport_task
);
854 subsys_initcall(gameport_init
);
855 module_exit(gameport_exit
);