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>
27 /*#include <asm/io.h>*/
29 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
30 MODULE_DESCRIPTION("Generic gameport layer");
31 MODULE_LICENSE("GPL");
34 * gameport_mutex protects entire gameport subsystem and is taken
35 * every time gameport port or driver registrered or unregistered.
37 static DEFINE_MUTEX(gameport_mutex
);
39 static LIST_HEAD(gameport_list
);
41 static struct bus_type gameport_bus
;
43 static void gameport_add_port(struct gameport
*gameport
);
44 static void gameport_attach_driver(struct gameport_driver
*drv
);
45 static void gameport_reconnect_port(struct gameport
*gameport
);
46 static void gameport_disconnect_port(struct gameport
*gameport
);
50 #include <asm/i8253.h>
52 #define DELTA(x,y) ((y)-(x)+((y)<(x)?1193182/HZ:0))
53 #define GET_TIME(x) do { x = get_time_pit(); } while (0)
55 static unsigned int get_time_pit(void)
60 raw_spin_lock_irqsave(&i8253_lock
, flags
);
63 count
|= inb_p(0x40) << 8;
64 raw_spin_unlock_irqrestore(&i8253_lock
, flags
);
74 * gameport_measure_speed() measures the gameport i/o speed.
77 static int gameport_measure_speed(struct gameport
*gameport
)
81 unsigned int i
, t
, t1
, t2
, t3
, tx
;
84 if (gameport_open(gameport
, NULL
, GAMEPORT_MODE_RAW
))
89 for(i
= 0; i
< 50; i
++) {
90 local_irq_save(flags
);
92 for (t
= 0; t
< 50; t
++) gameport_read(gameport
);
95 local_irq_restore(flags
);
97 if ((t
= DELTA(t2
,t1
) - DELTA(t3
,t2
)) < tx
) tx
= t
;
100 gameport_close(gameport
);
101 return 59659 / (tx
< 1 ? 1 : tx
);
103 #elif defined (__x86_64__)
106 unsigned long tx
, t1
, t2
, flags
;
108 if (gameport_open(gameport
, NULL
, GAMEPORT_MODE_RAW
))
113 for(i
= 0; i
< 50; i
++) {
114 local_irq_save(flags
);
116 for (t
= 0; t
< 50; t
++) gameport_read(gameport
);
118 local_irq_restore(flags
);
120 if (t2
- t1
< tx
) tx
= t2
- t1
;
123 gameport_close(gameport
);
124 return (this_cpu_read(cpu_info
.loops_per_jiffy
) *
125 (unsigned long)HZ
/ (1000 / 50)) / (tx
< 1 ? 1 : tx
);
129 unsigned int j
, t
= 0;
131 if (gameport_open(gameport
, NULL
, GAMEPORT_MODE_RAW
))
134 j
= jiffies
; while (j
== jiffies
);
135 j
= jiffies
; while (j
== jiffies
) { t
++; gameport_read(gameport
); }
137 gameport_close(gameport
);
138 return t
* HZ
/ 1000;
143 void gameport_start_polling(struct gameport
*gameport
)
145 spin_lock(&gameport
->timer_lock
);
147 if (!gameport
->poll_cnt
++) {
148 BUG_ON(!gameport
->poll_handler
);
149 BUG_ON(!gameport
->poll_interval
);
150 mod_timer(&gameport
->poll_timer
, jiffies
+ msecs_to_jiffies(gameport
->poll_interval
));
153 spin_unlock(&gameport
->timer_lock
);
155 EXPORT_SYMBOL(gameport_start_polling
);
157 void gameport_stop_polling(struct gameport
*gameport
)
159 spin_lock(&gameport
->timer_lock
);
161 if (!--gameport
->poll_cnt
)
162 del_timer(&gameport
->poll_timer
);
164 spin_unlock(&gameport
->timer_lock
);
166 EXPORT_SYMBOL(gameport_stop_polling
);
168 static void gameport_run_poll_handler(unsigned long d
)
170 struct gameport
*gameport
= (struct gameport
*)d
;
172 gameport
->poll_handler(gameport
);
173 if (gameport
->poll_cnt
)
174 mod_timer(&gameport
->poll_timer
, jiffies
+ msecs_to_jiffies(gameport
->poll_interval
));
178 * Basic gameport -> driver core mappings
181 static int gameport_bind_driver(struct gameport
*gameport
, struct gameport_driver
*drv
)
185 gameport
->dev
.driver
= &drv
->driver
;
186 if (drv
->connect(gameport
, drv
)) {
187 gameport
->dev
.driver
= NULL
;
191 error
= device_bind_driver(&gameport
->dev
);
193 dev_warn(&gameport
->dev
,
194 "device_bind_driver() failed for %s (%s) and %s, error: %d\n",
195 gameport
->phys
, gameport
->name
,
196 drv
->description
, error
);
197 drv
->disconnect(gameport
);
198 gameport
->dev
.driver
= NULL
;
205 static void gameport_find_driver(struct gameport
*gameport
)
209 error
= device_attach(&gameport
->dev
);
211 dev_warn(&gameport
->dev
,
212 "device_attach() failed for %s (%s), error: %d\n",
213 gameport
->phys
, gameport
->name
, error
);
218 * Gameport event processing.
221 enum gameport_event_type
{
222 GAMEPORT_REGISTER_PORT
,
223 GAMEPORT_ATTACH_DRIVER
,
226 struct gameport_event
{
227 enum gameport_event_type type
;
229 struct module
*owner
;
230 struct list_head node
;
233 static DEFINE_SPINLOCK(gameport_event_lock
); /* protects gameport_event_list */
234 static LIST_HEAD(gameport_event_list
);
236 static struct gameport_event
*gameport_get_event(void)
238 struct gameport_event
*event
= NULL
;
241 spin_lock_irqsave(&gameport_event_lock
, flags
);
243 if (!list_empty(&gameport_event_list
)) {
244 event
= list_first_entry(&gameport_event_list
,
245 struct gameport_event
, node
);
246 list_del_init(&event
->node
);
249 spin_unlock_irqrestore(&gameport_event_lock
, flags
);
253 static void gameport_free_event(struct gameport_event
*event
)
255 module_put(event
->owner
);
259 static void gameport_remove_duplicate_events(struct gameport_event
*event
)
261 struct gameport_event
*e
, *next
;
264 spin_lock_irqsave(&gameport_event_lock
, flags
);
266 list_for_each_entry_safe(e
, next
, &gameport_event_list
, node
) {
267 if (event
->object
== e
->object
) {
269 * If this event is of different type we should not
270 * look further - we only suppress duplicate events
271 * that were sent back-to-back.
273 if (event
->type
!= e
->type
)
276 list_del_init(&e
->node
);
277 gameport_free_event(e
);
281 spin_unlock_irqrestore(&gameport_event_lock
, flags
);
285 static void gameport_handle_events(struct work_struct
*work
)
287 struct gameport_event
*event
;
289 mutex_lock(&gameport_mutex
);
292 * Note that we handle only one event here to give swsusp
293 * a chance to freeze kgameportd thread. Gameport events
294 * should be pretty rare so we are not concerned about
295 * taking performance hit.
297 if ((event
= gameport_get_event())) {
299 switch (event
->type
) {
301 case GAMEPORT_REGISTER_PORT
:
302 gameport_add_port(event
->object
);
305 case GAMEPORT_ATTACH_DRIVER
:
306 gameport_attach_driver(event
->object
);
310 gameport_remove_duplicate_events(event
);
311 gameport_free_event(event
);
314 mutex_unlock(&gameport_mutex
);
317 static DECLARE_WORK(gameport_event_work
, gameport_handle_events
);
319 static int gameport_queue_event(void *object
, struct module
*owner
,
320 enum gameport_event_type event_type
)
323 struct gameport_event
*event
;
326 spin_lock_irqsave(&gameport_event_lock
, flags
);
329 * Scan event list for the other events for the same gameport port,
330 * starting with the most recent one. If event is the same we
331 * do not need add new one. If event is of different type we
332 * need to add this event and should not look further because
333 * we need to preserve sequence of distinct events.
335 list_for_each_entry_reverse(event
, &gameport_event_list
, node
) {
336 if (event
->object
== object
) {
337 if (event
->type
== event_type
)
343 event
= kmalloc(sizeof(struct gameport_event
), GFP_ATOMIC
);
345 pr_err("Not enough memory to queue event %d\n", event_type
);
350 if (!try_module_get(owner
)) {
351 pr_warning("Can't get module reference, dropping event %d\n",
358 event
->type
= event_type
;
359 event
->object
= object
;
360 event
->owner
= owner
;
362 list_add_tail(&event
->node
, &gameport_event_list
);
363 queue_work(system_long_wq
, &gameport_event_work
);
366 spin_unlock_irqrestore(&gameport_event_lock
, flags
);
371 * Remove all events that have been submitted for a given object,
372 * be it a gameport port or a driver.
374 static void gameport_remove_pending_events(void *object
)
376 struct gameport_event
*event
, *next
;
379 spin_lock_irqsave(&gameport_event_lock
, flags
);
381 list_for_each_entry_safe(event
, next
, &gameport_event_list
, node
) {
382 if (event
->object
== object
) {
383 list_del_init(&event
->node
);
384 gameport_free_event(event
);
388 spin_unlock_irqrestore(&gameport_event_lock
, flags
);
392 * Destroy child gameport port (if any) that has not been fully registered yet.
394 * Note that we rely on the fact that port can have only one child and therefore
395 * only one child registration request can be pending. Additionally, children
396 * are registered by driver's connect() handler so there can't be a grandchild
397 * pending registration together with a child.
399 static struct gameport
*gameport_get_pending_child(struct gameport
*parent
)
401 struct gameport_event
*event
;
402 struct gameport
*gameport
, *child
= NULL
;
405 spin_lock_irqsave(&gameport_event_lock
, flags
);
407 list_for_each_entry(event
, &gameport_event_list
, node
) {
408 if (event
->type
== GAMEPORT_REGISTER_PORT
) {
409 gameport
= event
->object
;
410 if (gameport
->parent
== parent
) {
417 spin_unlock_irqrestore(&gameport_event_lock
, flags
);
422 * Gameport port operations
425 static ssize_t
gameport_show_description(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
427 struct gameport
*gameport
= to_gameport_port(dev
);
429 return sprintf(buf
, "%s\n", gameport
->name
);
432 static ssize_t
gameport_rebind_driver(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
434 struct gameport
*gameport
= to_gameport_port(dev
);
435 struct device_driver
*drv
;
438 error
= mutex_lock_interruptible(&gameport_mutex
);
442 if (!strncmp(buf
, "none", count
)) {
443 gameport_disconnect_port(gameport
);
444 } else if (!strncmp(buf
, "reconnect", count
)) {
445 gameport_reconnect_port(gameport
);
446 } else if (!strncmp(buf
, "rescan", count
)) {
447 gameport_disconnect_port(gameport
);
448 gameport_find_driver(gameport
);
449 } else if ((drv
= driver_find(buf
, &gameport_bus
)) != NULL
) {
450 gameport_disconnect_port(gameport
);
451 error
= gameport_bind_driver(gameport
, to_gameport_driver(drv
));
457 mutex_unlock(&gameport_mutex
);
459 return error
? error
: count
;
462 static struct device_attribute gameport_device_attrs
[] = {
463 __ATTR(description
, S_IRUGO
, gameport_show_description
, NULL
),
464 __ATTR(drvctl
, S_IWUSR
, NULL
, gameport_rebind_driver
),
468 static void gameport_release_port(struct device
*dev
)
470 struct gameport
*gameport
= to_gameport_port(dev
);
473 module_put(THIS_MODULE
);
476 void gameport_set_phys(struct gameport
*gameport
, const char *fmt
, ...)
481 vsnprintf(gameport
->phys
, sizeof(gameport
->phys
), fmt
, args
);
484 EXPORT_SYMBOL(gameport_set_phys
);
487 * Prepare gameport port for registration.
489 static void gameport_init_port(struct gameport
*gameport
)
491 static atomic_t gameport_no
= ATOMIC_INIT(0);
493 __module_get(THIS_MODULE
);
495 mutex_init(&gameport
->drv_mutex
);
496 device_initialize(&gameport
->dev
);
497 dev_set_name(&gameport
->dev
, "gameport%lu",
498 (unsigned long)atomic_inc_return(&gameport_no
) - 1);
499 gameport
->dev
.bus
= &gameport_bus
;
500 gameport
->dev
.release
= gameport_release_port
;
501 if (gameport
->parent
)
502 gameport
->dev
.parent
= &gameport
->parent
->dev
;
504 INIT_LIST_HEAD(&gameport
->node
);
505 spin_lock_init(&gameport
->timer_lock
);
506 init_timer(&gameport
->poll_timer
);
507 gameport
->poll_timer
.function
= gameport_run_poll_handler
;
508 gameport
->poll_timer
.data
= (unsigned long)gameport
;
512 * Complete gameport port registration.
513 * Driver core will attempt to find appropriate driver for the port.
515 static void gameport_add_port(struct gameport
*gameport
)
519 if (gameport
->parent
)
520 gameport
->parent
->child
= gameport
;
522 gameport
->speed
= gameport_measure_speed(gameport
);
524 list_add_tail(&gameport
->node
, &gameport_list
);
527 dev_info(&gameport
->dev
, "%s is %s, io %#x, speed %dkHz\n",
528 gameport
->name
, gameport
->phys
, gameport
->io
, gameport
->speed
);
530 dev_info(&gameport
->dev
, "%s is %s, speed %dkHz\n",
531 gameport
->name
, gameport
->phys
, gameport
->speed
);
533 error
= device_add(&gameport
->dev
);
535 dev_err(&gameport
->dev
,
536 "device_add() failed for %s (%s), error: %d\n",
537 gameport
->phys
, gameport
->name
, error
);
541 * gameport_destroy_port() completes deregistration process and removes
542 * port from the system
544 static void gameport_destroy_port(struct gameport
*gameport
)
546 struct gameport
*child
;
548 child
= gameport_get_pending_child(gameport
);
550 gameport_remove_pending_events(child
);
551 put_device(&child
->dev
);
554 if (gameport
->parent
) {
555 gameport
->parent
->child
= NULL
;
556 gameport
->parent
= NULL
;
559 if (device_is_registered(&gameport
->dev
))
560 device_del(&gameport
->dev
);
562 list_del_init(&gameport
->node
);
564 gameport_remove_pending_events(gameport
);
565 put_device(&gameport
->dev
);
569 * Reconnect gameport port and all its children (re-initialize attached devices)
571 static void gameport_reconnect_port(struct gameport
*gameport
)
574 if (!gameport
->drv
|| !gameport
->drv
->reconnect
|| gameport
->drv
->reconnect(gameport
)) {
575 gameport_disconnect_port(gameport
);
576 gameport_find_driver(gameport
);
577 /* Ok, old children are now gone, we are done */
580 gameport
= gameport
->child
;
585 * gameport_disconnect_port() unbinds a port from its driver. As a side effect
586 * all child ports are unbound and destroyed.
588 static void gameport_disconnect_port(struct gameport
*gameport
)
590 struct gameport
*s
, *parent
;
592 if (gameport
->child
) {
594 * Children ports should be disconnected and destroyed
595 * first, staring with the leaf one, since we don't want
598 for (s
= gameport
; s
->child
; s
= s
->child
)
604 device_release_driver(&s
->dev
);
605 gameport_destroy_port(s
);
606 } while ((s
= parent
) != gameport
);
610 * Ok, no children left, now disconnect this port
612 device_release_driver(&gameport
->dev
);
616 * Submits register request to kgameportd for subsequent execution.
617 * Note that port registration is always asynchronous.
619 void __gameport_register_port(struct gameport
*gameport
, struct module
*owner
)
621 gameport_init_port(gameport
);
622 gameport_queue_event(gameport
, owner
, GAMEPORT_REGISTER_PORT
);
624 EXPORT_SYMBOL(__gameport_register_port
);
627 * Synchronously unregisters gameport port.
629 void gameport_unregister_port(struct gameport
*gameport
)
631 mutex_lock(&gameport_mutex
);
632 gameport_disconnect_port(gameport
);
633 gameport_destroy_port(gameport
);
634 mutex_unlock(&gameport_mutex
);
636 EXPORT_SYMBOL(gameport_unregister_port
);
640 * Gameport driver operations
643 static ssize_t
gameport_driver_show_description(struct device_driver
*drv
, char *buf
)
645 struct gameport_driver
*driver
= to_gameport_driver(drv
);
646 return sprintf(buf
, "%s\n", driver
->description
? driver
->description
: "(none)");
649 static struct driver_attribute gameport_driver_attrs
[] = {
650 __ATTR(description
, S_IRUGO
, gameport_driver_show_description
, NULL
),
654 static int gameport_driver_probe(struct device
*dev
)
656 struct gameport
*gameport
= to_gameport_port(dev
);
657 struct gameport_driver
*drv
= to_gameport_driver(dev
->driver
);
659 drv
->connect(gameport
, drv
);
660 return gameport
->drv
? 0 : -ENODEV
;
663 static int gameport_driver_remove(struct device
*dev
)
665 struct gameport
*gameport
= to_gameport_port(dev
);
666 struct gameport_driver
*drv
= to_gameport_driver(dev
->driver
);
668 drv
->disconnect(gameport
);
672 static void gameport_attach_driver(struct gameport_driver
*drv
)
676 error
= driver_attach(&drv
->driver
);
678 pr_err("driver_attach() failed for %s, error: %d\n",
679 drv
->driver
.name
, error
);
682 int __gameport_register_driver(struct gameport_driver
*drv
, struct module
*owner
,
683 const char *mod_name
)
687 drv
->driver
.bus
= &gameport_bus
;
688 drv
->driver
.owner
= owner
;
689 drv
->driver
.mod_name
= mod_name
;
692 * Temporarily disable automatic binding because probing
693 * takes long time and we are better off doing it in kgameportd
697 error
= driver_register(&drv
->driver
);
699 pr_err("driver_register() failed for %s, error: %d\n",
700 drv
->driver
.name
, error
);
705 * Reset ignore flag and let kgameportd bind the driver to free ports
708 error
= gameport_queue_event(drv
, NULL
, GAMEPORT_ATTACH_DRIVER
);
710 driver_unregister(&drv
->driver
);
716 EXPORT_SYMBOL(__gameport_register_driver
);
718 void gameport_unregister_driver(struct gameport_driver
*drv
)
720 struct gameport
*gameport
;
722 mutex_lock(&gameport_mutex
);
724 drv
->ignore
= true; /* so gameport_find_driver ignores it */
725 gameport_remove_pending_events(drv
);
728 list_for_each_entry(gameport
, &gameport_list
, node
) {
729 if (gameport
->drv
== drv
) {
730 gameport_disconnect_port(gameport
);
731 gameport_find_driver(gameport
);
732 /* we could've deleted some ports, restart */
737 driver_unregister(&drv
->driver
);
739 mutex_unlock(&gameport_mutex
);
741 EXPORT_SYMBOL(gameport_unregister_driver
);
743 static int gameport_bus_match(struct device
*dev
, struct device_driver
*drv
)
745 struct gameport_driver
*gameport_drv
= to_gameport_driver(drv
);
747 return !gameport_drv
->ignore
;
750 static struct bus_type gameport_bus
= {
752 .dev_attrs
= gameport_device_attrs
,
753 .drv_attrs
= gameport_driver_attrs
,
754 .match
= gameport_bus_match
,
755 .probe
= gameport_driver_probe
,
756 .remove
= gameport_driver_remove
,
759 static void gameport_set_drv(struct gameport
*gameport
, struct gameport_driver
*drv
)
761 mutex_lock(&gameport
->drv_mutex
);
763 mutex_unlock(&gameport
->drv_mutex
);
766 int gameport_open(struct gameport
*gameport
, struct gameport_driver
*drv
, int mode
)
768 if (gameport
->open
) {
769 if (gameport
->open(gameport
, mode
)) {
773 if (mode
!= GAMEPORT_MODE_RAW
)
777 gameport_set_drv(gameport
, drv
);
780 EXPORT_SYMBOL(gameport_open
);
782 void gameport_close(struct gameport
*gameport
)
784 del_timer_sync(&gameport
->poll_timer
);
785 gameport
->poll_handler
= NULL
;
786 gameport
->poll_interval
= 0;
787 gameport_set_drv(gameport
, NULL
);
789 gameport
->close(gameport
);
791 EXPORT_SYMBOL(gameport_close
);
793 static int __init
gameport_init(void)
797 error
= bus_register(&gameport_bus
);
799 pr_err("failed to register gameport bus, error: %d\n", error
);
807 static void __exit
gameport_exit(void)
809 bus_unregister(&gameport_bus
);
812 * There should not be any outstanding events but work may
813 * still be scheduled so simply cancel it.
815 cancel_work_sync(&gameport_event_work
);
818 subsys_initcall(gameport_init
);
819 module_exit(gameport_exit
);