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/wait.h>
22 #include <linux/slab.h>
23 #include <linux/delay.h>
24 #include <linux/kthread.h>
25 #include <linux/sched.h> /* HZ */
26 #include <linux/mutex.h>
27 #include <linux/freezer.h>
29 /*#include <asm/io.h>*/
31 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
32 MODULE_DESCRIPTION("Generic gameport layer");
33 MODULE_LICENSE("GPL");
36 * gameport_mutex protects entire gameport subsystem and is taken
37 * every time gameport port or driver registrered or unregistered.
39 static DEFINE_MUTEX(gameport_mutex
);
41 static LIST_HEAD(gameport_list
);
43 static struct bus_type gameport_bus
;
45 static void gameport_add_port(struct gameport
*gameport
);
46 static void gameport_attach_driver(struct gameport_driver
*drv
);
47 static void gameport_reconnect_port(struct gameport
*gameport
);
48 static void gameport_disconnect_port(struct gameport
*gameport
);
52 #include <asm/i8253.h>
54 #define DELTA(x,y) ((y)-(x)+((y)<(x)?1193182/HZ:0))
55 #define GET_TIME(x) do { x = get_time_pit(); } while (0)
57 static unsigned int get_time_pit(void)
62 spin_lock_irqsave(&i8253_lock
, flags
);
65 count
|= inb_p(0x40) << 8;
66 spin_unlock_irqrestore(&i8253_lock
, flags
);
76 * gameport_measure_speed() measures the gameport i/o speed.
79 static int gameport_measure_speed(struct gameport
*gameport
)
83 unsigned int i
, t
, t1
, t2
, t3
, tx
;
86 if (gameport_open(gameport
, NULL
, GAMEPORT_MODE_RAW
))
91 for(i
= 0; i
< 50; i
++) {
92 local_irq_save(flags
);
94 for (t
= 0; t
< 50; t
++) gameport_read(gameport
);
97 local_irq_restore(flags
);
99 if ((t
= DELTA(t2
,t1
) - DELTA(t3
,t2
)) < tx
) tx
= t
;
102 gameport_close(gameport
);
103 return 59659 / (tx
< 1 ? 1 : tx
);
105 #elif defined (__x86_64__)
108 unsigned long tx
, t1
, t2
, flags
;
110 if (gameport_open(gameport
, NULL
, GAMEPORT_MODE_RAW
))
115 for(i
= 0; i
< 50; i
++) {
116 local_irq_save(flags
);
118 for (t
= 0; t
< 50; t
++) gameport_read(gameport
);
120 local_irq_restore(flags
);
122 if (t2
- t1
< tx
) tx
= t2
- t1
;
125 gameport_close(gameport
);
126 return (cpu_data(raw_smp_processor_id()).loops_per_jiffy
*
127 (unsigned long)HZ
/ (1000 / 50)) / (tx
< 1 ? 1 : tx
);
131 unsigned int j
, t
= 0;
133 if (gameport_open(gameport
, NULL
, GAMEPORT_MODE_RAW
))
136 j
= jiffies
; while (j
== jiffies
);
137 j
= jiffies
; while (j
== jiffies
) { t
++; gameport_read(gameport
); }
139 gameport_close(gameport
);
140 return t
* HZ
/ 1000;
145 void gameport_start_polling(struct gameport
*gameport
)
147 spin_lock(&gameport
->timer_lock
);
149 if (!gameport
->poll_cnt
++) {
150 BUG_ON(!gameport
->poll_handler
);
151 BUG_ON(!gameport
->poll_interval
);
152 mod_timer(&gameport
->poll_timer
, jiffies
+ msecs_to_jiffies(gameport
->poll_interval
));
155 spin_unlock(&gameport
->timer_lock
);
157 EXPORT_SYMBOL(gameport_start_polling
);
159 void gameport_stop_polling(struct gameport
*gameport
)
161 spin_lock(&gameport
->timer_lock
);
163 if (!--gameport
->poll_cnt
)
164 del_timer(&gameport
->poll_timer
);
166 spin_unlock(&gameport
->timer_lock
);
168 EXPORT_SYMBOL(gameport_stop_polling
);
170 static void gameport_run_poll_handler(unsigned long d
)
172 struct gameport
*gameport
= (struct gameport
*)d
;
174 gameport
->poll_handler(gameport
);
175 if (gameport
->poll_cnt
)
176 mod_timer(&gameport
->poll_timer
, jiffies
+ msecs_to_jiffies(gameport
->poll_interval
));
180 * Basic gameport -> driver core mappings
183 static int gameport_bind_driver(struct gameport
*gameport
, struct gameport_driver
*drv
)
187 gameport
->dev
.driver
= &drv
->driver
;
188 if (drv
->connect(gameport
, drv
)) {
189 gameport
->dev
.driver
= NULL
;
193 error
= device_bind_driver(&gameport
->dev
);
195 dev_warn(&gameport
->dev
,
196 "device_bind_driver() failed for %s (%s) and %s, error: %d\n",
197 gameport
->phys
, gameport
->name
,
198 drv
->description
, error
);
199 drv
->disconnect(gameport
);
200 gameport
->dev
.driver
= NULL
;
207 static void gameport_find_driver(struct gameport
*gameport
)
211 error
= device_attach(&gameport
->dev
);
213 dev_warn(&gameport
->dev
,
214 "device_attach() failed for %s (%s), error: %d\n",
215 gameport
->phys
, gameport
->name
, error
);
220 * Gameport event processing.
223 enum gameport_event_type
{
224 GAMEPORT_REGISTER_PORT
,
225 GAMEPORT_ATTACH_DRIVER
,
228 struct gameport_event
{
229 enum gameport_event_type type
;
231 struct module
*owner
;
232 struct list_head node
;
235 static DEFINE_SPINLOCK(gameport_event_lock
); /* protects gameport_event_list */
236 static LIST_HEAD(gameport_event_list
);
237 static DECLARE_WAIT_QUEUE_HEAD(gameport_wait
);
238 static struct task_struct
*gameport_task
;
240 static int gameport_queue_event(void *object
, struct module
*owner
,
241 enum gameport_event_type event_type
)
244 struct gameport_event
*event
;
247 spin_lock_irqsave(&gameport_event_lock
, flags
);
250 * Scan event list for the other events for the same gameport port,
251 * starting with the most recent one. If event is the same we
252 * do not need add new one. If event is of different type we
253 * need to add this event and should not look further because
254 * we need to preseve sequence of distinct events.
256 list_for_each_entry_reverse(event
, &gameport_event_list
, node
) {
257 if (event
->object
== object
) {
258 if (event
->type
== event_type
)
264 event
= kmalloc(sizeof(struct gameport_event
), GFP_ATOMIC
);
266 pr_err("Not enough memory to queue event %d\n", event_type
);
271 if (!try_module_get(owner
)) {
272 pr_warning("Can't get module reference, dropping event %d\n",
279 event
->type
= event_type
;
280 event
->object
= object
;
281 event
->owner
= owner
;
283 list_add_tail(&event
->node
, &gameport_event_list
);
284 wake_up(&gameport_wait
);
287 spin_unlock_irqrestore(&gameport_event_lock
, flags
);
291 static void gameport_free_event(struct gameport_event
*event
)
293 module_put(event
->owner
);
297 static void gameport_remove_duplicate_events(struct gameport_event
*event
)
299 struct gameport_event
*e
, *next
;
302 spin_lock_irqsave(&gameport_event_lock
, flags
);
304 list_for_each_entry_safe(e
, next
, &gameport_event_list
, node
) {
305 if (event
->object
== e
->object
) {
307 * If this event is of different type we should not
308 * look further - we only suppress duplicate events
309 * that were sent back-to-back.
311 if (event
->type
!= e
->type
)
314 list_del_init(&e
->node
);
315 gameport_free_event(e
);
319 spin_unlock_irqrestore(&gameport_event_lock
, flags
);
322 static struct gameport_event
*gameport_get_event(void)
324 struct gameport_event
*event
= NULL
;
327 spin_lock_irqsave(&gameport_event_lock
, flags
);
329 if (!list_empty(&gameport_event_list
)) {
330 event
= list_first_entry(&gameport_event_list
,
331 struct gameport_event
, node
);
332 list_del_init(&event
->node
);
335 spin_unlock_irqrestore(&gameport_event_lock
, flags
);
339 static void gameport_handle_event(void)
341 struct gameport_event
*event
;
343 mutex_lock(&gameport_mutex
);
346 * Note that we handle only one event here to give swsusp
347 * a chance to freeze kgameportd thread. Gameport events
348 * should be pretty rare so we are not concerned about
349 * taking performance hit.
351 if ((event
= gameport_get_event())) {
353 switch (event
->type
) {
355 case GAMEPORT_REGISTER_PORT
:
356 gameport_add_port(event
->object
);
359 case GAMEPORT_ATTACH_DRIVER
:
360 gameport_attach_driver(event
->object
);
364 gameport_remove_duplicate_events(event
);
365 gameport_free_event(event
);
368 mutex_unlock(&gameport_mutex
);
372 * Remove all events that have been submitted for a given object,
373 * be it a gameport port or a driver.
375 static void gameport_remove_pending_events(void *object
)
377 struct gameport_event
*event
, *next
;
380 spin_lock_irqsave(&gameport_event_lock
, flags
);
382 list_for_each_entry_safe(event
, next
, &gameport_event_list
, node
) {
383 if (event
->object
== object
) {
384 list_del_init(&event
->node
);
385 gameport_free_event(event
);
389 spin_unlock_irqrestore(&gameport_event_lock
, flags
);
393 * Destroy child gameport port (if any) that has not been fully registered yet.
395 * Note that we rely on the fact that port can have only one child and therefore
396 * only one child registration request can be pending. Additionally, children
397 * are registered by driver's connect() handler so there can't be a grandchild
398 * pending registration together with a child.
400 static struct gameport
*gameport_get_pending_child(struct gameport
*parent
)
402 struct gameport_event
*event
;
403 struct gameport
*gameport
, *child
= NULL
;
406 spin_lock_irqsave(&gameport_event_lock
, flags
);
408 list_for_each_entry(event
, &gameport_event_list
, node
) {
409 if (event
->type
== GAMEPORT_REGISTER_PORT
) {
410 gameport
= event
->object
;
411 if (gameport
->parent
== parent
) {
418 spin_unlock_irqrestore(&gameport_event_lock
, flags
);
422 static int gameport_thread(void *nothing
)
426 gameport_handle_event();
427 wait_event_freezable(gameport_wait
,
428 kthread_should_stop() || !list_empty(&gameport_event_list
));
429 } while (!kthread_should_stop());
436 * Gameport port operations
439 static ssize_t
gameport_show_description(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
441 struct gameport
*gameport
= to_gameport_port(dev
);
443 return sprintf(buf
, "%s\n", gameport
->name
);
446 static ssize_t
gameport_rebind_driver(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
448 struct gameport
*gameport
= to_gameport_port(dev
);
449 struct device_driver
*drv
;
452 error
= mutex_lock_interruptible(&gameport_mutex
);
456 if (!strncmp(buf
, "none", count
)) {
457 gameport_disconnect_port(gameport
);
458 } else if (!strncmp(buf
, "reconnect", count
)) {
459 gameport_reconnect_port(gameport
);
460 } else if (!strncmp(buf
, "rescan", count
)) {
461 gameport_disconnect_port(gameport
);
462 gameport_find_driver(gameport
);
463 } else if ((drv
= driver_find(buf
, &gameport_bus
)) != NULL
) {
464 gameport_disconnect_port(gameport
);
465 error
= gameport_bind_driver(gameport
, to_gameport_driver(drv
));
471 mutex_unlock(&gameport_mutex
);
473 return error
? error
: count
;
476 static struct device_attribute gameport_device_attrs
[] = {
477 __ATTR(description
, S_IRUGO
, gameport_show_description
, NULL
),
478 __ATTR(drvctl
, S_IWUSR
, NULL
, gameport_rebind_driver
),
482 static void gameport_release_port(struct device
*dev
)
484 struct gameport
*gameport
= to_gameport_port(dev
);
487 module_put(THIS_MODULE
);
490 void gameport_set_phys(struct gameport
*gameport
, const char *fmt
, ...)
495 vsnprintf(gameport
->phys
, sizeof(gameport
->phys
), fmt
, args
);
498 EXPORT_SYMBOL(gameport_set_phys
);
501 * Prepare gameport port for registration.
503 static void gameport_init_port(struct gameport
*gameport
)
505 static atomic_t gameport_no
= ATOMIC_INIT(0);
507 __module_get(THIS_MODULE
);
509 mutex_init(&gameport
->drv_mutex
);
510 device_initialize(&gameport
->dev
);
511 dev_set_name(&gameport
->dev
, "gameport%lu",
512 (unsigned long)atomic_inc_return(&gameport_no
) - 1);
513 gameport
->dev
.bus
= &gameport_bus
;
514 gameport
->dev
.release
= gameport_release_port
;
515 if (gameport
->parent
)
516 gameport
->dev
.parent
= &gameport
->parent
->dev
;
518 INIT_LIST_HEAD(&gameport
->node
);
519 spin_lock_init(&gameport
->timer_lock
);
520 init_timer(&gameport
->poll_timer
);
521 gameport
->poll_timer
.function
= gameport_run_poll_handler
;
522 gameport
->poll_timer
.data
= (unsigned long)gameport
;
526 * Complete gameport port registration.
527 * Driver core will attempt to find appropriate driver for the port.
529 static void gameport_add_port(struct gameport
*gameport
)
533 if (gameport
->parent
)
534 gameport
->parent
->child
= gameport
;
536 gameport
->speed
= gameport_measure_speed(gameport
);
538 list_add_tail(&gameport
->node
, &gameport_list
);
541 dev_info(&gameport
->dev
, "%s is %s, io %#x, speed %dkHz\n",
542 gameport
->name
, gameport
->phys
, gameport
->io
, gameport
->speed
);
544 dev_info(&gameport
->dev
, "%s is %s, speed %dkHz\n",
545 gameport
->name
, gameport
->phys
, gameport
->speed
);
547 error
= device_add(&gameport
->dev
);
549 dev_err(&gameport
->dev
,
550 "device_add() failed for %s (%s), error: %d\n",
551 gameport
->phys
, gameport
->name
, error
);
555 * gameport_destroy_port() completes deregistration process and removes
556 * port from the system
558 static void gameport_destroy_port(struct gameport
*gameport
)
560 struct gameport
*child
;
562 child
= gameport_get_pending_child(gameport
);
564 gameport_remove_pending_events(child
);
565 put_device(&child
->dev
);
568 if (gameport
->parent
) {
569 gameport
->parent
->child
= NULL
;
570 gameport
->parent
= NULL
;
573 if (device_is_registered(&gameport
->dev
))
574 device_del(&gameport
->dev
);
576 list_del_init(&gameport
->node
);
578 gameport_remove_pending_events(gameport
);
579 put_device(&gameport
->dev
);
583 * Reconnect gameport port and all its children (re-initialize attached devices)
585 static void gameport_reconnect_port(struct gameport
*gameport
)
588 if (!gameport
->drv
|| !gameport
->drv
->reconnect
|| gameport
->drv
->reconnect(gameport
)) {
589 gameport_disconnect_port(gameport
);
590 gameport_find_driver(gameport
);
591 /* Ok, old children are now gone, we are done */
594 gameport
= gameport
->child
;
599 * gameport_disconnect_port() unbinds a port from its driver. As a side effect
600 * all child ports are unbound and destroyed.
602 static void gameport_disconnect_port(struct gameport
*gameport
)
604 struct gameport
*s
, *parent
;
606 if (gameport
->child
) {
608 * Children ports should be disconnected and destroyed
609 * first, staring with the leaf one, since we don't want
612 for (s
= gameport
; s
->child
; s
= s
->child
)
618 device_release_driver(&s
->dev
);
619 gameport_destroy_port(s
);
620 } while ((s
= parent
) != gameport
);
624 * Ok, no children left, now disconnect this port
626 device_release_driver(&gameport
->dev
);
630 * Submits register request to kgameportd for subsequent execution.
631 * Note that port registration is always asynchronous.
633 void __gameport_register_port(struct gameport
*gameport
, struct module
*owner
)
635 gameport_init_port(gameport
);
636 gameport_queue_event(gameport
, owner
, GAMEPORT_REGISTER_PORT
);
638 EXPORT_SYMBOL(__gameport_register_port
);
641 * Synchronously unregisters gameport port.
643 void gameport_unregister_port(struct gameport
*gameport
)
645 mutex_lock(&gameport_mutex
);
646 gameport_disconnect_port(gameport
);
647 gameport_destroy_port(gameport
);
648 mutex_unlock(&gameport_mutex
);
650 EXPORT_SYMBOL(gameport_unregister_port
);
654 * Gameport driver operations
657 static ssize_t
gameport_driver_show_description(struct device_driver
*drv
, char *buf
)
659 struct gameport_driver
*driver
= to_gameport_driver(drv
);
660 return sprintf(buf
, "%s\n", driver
->description
? driver
->description
: "(none)");
663 static struct driver_attribute gameport_driver_attrs
[] = {
664 __ATTR(description
, S_IRUGO
, gameport_driver_show_description
, NULL
),
668 static int gameport_driver_probe(struct device
*dev
)
670 struct gameport
*gameport
= to_gameport_port(dev
);
671 struct gameport_driver
*drv
= to_gameport_driver(dev
->driver
);
673 drv
->connect(gameport
, drv
);
674 return gameport
->drv
? 0 : -ENODEV
;
677 static int gameport_driver_remove(struct device
*dev
)
679 struct gameport
*gameport
= to_gameport_port(dev
);
680 struct gameport_driver
*drv
= to_gameport_driver(dev
->driver
);
682 drv
->disconnect(gameport
);
686 static void gameport_attach_driver(struct gameport_driver
*drv
)
690 error
= driver_attach(&drv
->driver
);
692 pr_err("driver_attach() failed for %s, error: %d\n",
693 drv
->driver
.name
, error
);
696 int __gameport_register_driver(struct gameport_driver
*drv
, struct module
*owner
,
697 const char *mod_name
)
701 drv
->driver
.bus
= &gameport_bus
;
702 drv
->driver
.owner
= owner
;
703 drv
->driver
.mod_name
= mod_name
;
706 * Temporarily disable automatic binding because probing
707 * takes long time and we are better off doing it in kgameportd
711 error
= driver_register(&drv
->driver
);
713 pr_err("driver_register() failed for %s, error: %d\n",
714 drv
->driver
.name
, error
);
719 * Reset ignore flag and let kgameportd bind the driver to free ports
722 error
= gameport_queue_event(drv
, NULL
, GAMEPORT_ATTACH_DRIVER
);
724 driver_unregister(&drv
->driver
);
730 EXPORT_SYMBOL(__gameport_register_driver
);
732 void gameport_unregister_driver(struct gameport_driver
*drv
)
734 struct gameport
*gameport
;
736 mutex_lock(&gameport_mutex
);
738 drv
->ignore
= true; /* so gameport_find_driver ignores it */
739 gameport_remove_pending_events(drv
);
742 list_for_each_entry(gameport
, &gameport_list
, node
) {
743 if (gameport
->drv
== drv
) {
744 gameport_disconnect_port(gameport
);
745 gameport_find_driver(gameport
);
746 /* we could've deleted some ports, restart */
751 driver_unregister(&drv
->driver
);
753 mutex_unlock(&gameport_mutex
);
755 EXPORT_SYMBOL(gameport_unregister_driver
);
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
);
794 EXPORT_SYMBOL(gameport_open
);
796 void gameport_close(struct gameport
*gameport
)
798 del_timer_sync(&gameport
->poll_timer
);
799 gameport
->poll_handler
= NULL
;
800 gameport
->poll_interval
= 0;
801 gameport_set_drv(gameport
, NULL
);
803 gameport
->close(gameport
);
805 EXPORT_SYMBOL(gameport_close
);
807 static int __init
gameport_init(void)
811 error
= bus_register(&gameport_bus
);
813 pr_err("failed to register gameport bus, error: %d\n", error
);
817 gameport_task
= kthread_run(gameport_thread
, NULL
, "kgameportd");
818 if (IS_ERR(gameport_task
)) {
819 bus_unregister(&gameport_bus
);
820 error
= PTR_ERR(gameport_task
);
821 pr_err("Failed to start kgameportd, error: %d\n", error
);
828 static void __exit
gameport_exit(void)
830 bus_unregister(&gameport_bus
);
831 kthread_stop(gameport_task
);
834 subsys_initcall(gameport_init
);
835 module_exit(gameport_exit
);