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>
24 #include <linux/sched.h> /* HZ */
26 /*#include <asm/io.h>*/
28 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
29 MODULE_DESCRIPTION("Generic gameport layer");
30 MODULE_LICENSE("GPL");
32 EXPORT_SYMBOL(__gameport_register_port);
33 EXPORT_SYMBOL(gameport_unregister_port);
34 EXPORT_SYMBOL(__gameport_register_driver);
35 EXPORT_SYMBOL(gameport_unregister_driver);
36 EXPORT_SYMBOL(gameport_open);
37 EXPORT_SYMBOL(gameport_close);
38 EXPORT_SYMBOL(gameport_rescan);
39 EXPORT_SYMBOL(gameport_cooked_read);
40 EXPORT_SYMBOL(gameport_set_name);
41 EXPORT_SYMBOL(gameport_set_phys);
42 EXPORT_SYMBOL(gameport_start_polling);
43 EXPORT_SYMBOL(gameport_stop_polling);
46 * gameport_sem protects entire gameport subsystem and is taken
47 * every time gameport port or driver registrered or unregistered.
49 static DECLARE_MUTEX(gameport_sem);
51 static LIST_HEAD(gameport_list);
53 static struct bus_type gameport_bus;
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 * (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 void gameport_bind_driver(struct gameport *gameport, struct gameport_driver *drv)
192 down_write(&gameport_bus.subsys.rwsem);
194 gameport->dev.driver = &drv->driver;
195 if (drv->connect(gameport, drv)) {
196 gameport->dev.driver = NULL;
199 device_bind_driver(&gameport->dev);
201 up_write(&gameport_bus.subsys.rwsem);
204 static void gameport_release_driver(struct gameport *gameport)
206 down_write(&gameport_bus.subsys.rwsem);
207 device_release_driver(&gameport->dev);
208 up_write(&gameport_bus.subsys.rwsem);
211 static void gameport_find_driver(struct gameport *gameport)
213 down_write(&gameport_bus.subsys.rwsem);
214 device_attach(&gameport->dev);
215 up_write(&gameport_bus.subsys.rwsem);
220 * Gameport event processing.
223 enum gameport_event_type {
226 GAMEPORT_REGISTER_PORT,
227 GAMEPORT_REGISTER_DRIVER,
230 struct gameport_event {
231 enum gameport_event_type type;
233 struct module *owner;
234 struct list_head node;
237 static DEFINE_SPINLOCK(gameport_event_lock); /* protects gameport_event_list */
238 static LIST_HEAD(gameport_event_list);
239 static DECLARE_WAIT_QUEUE_HEAD(gameport_wait);
240 static struct task_struct *gameport_task;
242 static void gameport_queue_event(void *object, struct module *owner,
243 enum gameport_event_type event_type)
246 struct gameport_event *event;
248 spin_lock_irqsave(&gameport_event_lock, flags);
251 * Scan event list for the other events for the same gameport port,
252 * starting with the most recent one. If event is the same we
253 * do not need add new one. If event is of different type we
254 * need to add this event and should not look further because
255 * we need to preseve sequence of distinct events.
257 list_for_each_entry_reverse(event, &gameport_event_list, node) {
258 if (event->object == object) {
259 if (event->type == event_type)
265 if ((event = kmalloc(sizeof(struct gameport_event), GFP_ATOMIC))) {
266 if (!try_module_get(owner)) {
267 printk(KERN_WARNING "gameport: Can't get module reference, dropping event %d\n", event_type);
271 event->type = event_type;
272 event->object = object;
273 event->owner = owner;
275 list_add_tail(&event->node, &gameport_event_list);
276 wake_up(&gameport_wait);
278 printk(KERN_ERR "gameport: Not enough memory to queue event %d\n", event_type);
281 spin_unlock_irqrestore(&gameport_event_lock, flags);
284 static void gameport_free_event(struct gameport_event *event)
286 module_put(event->owner);
290 static void gameport_remove_duplicate_events(struct gameport_event *event)
292 struct list_head *node, *next;
293 struct gameport_event *e;
296 spin_lock_irqsave(&gameport_event_lock, flags);
298 list_for_each_safe(node, next, &gameport_event_list) {
299 e = list_entry(node, struct gameport_event, node);
300 if (event->object == e->object) {
302 * If this event is of different type we should not
303 * look further - we only suppress duplicate events
304 * that were sent back-to-back.
306 if (event->type != e->type)
310 gameport_free_event(e);
314 spin_unlock_irqrestore(&gameport_event_lock, flags);
318 static struct gameport_event *gameport_get_event(void)
320 struct gameport_event *event;
321 struct list_head *node;
324 spin_lock_irqsave(&gameport_event_lock, flags);
326 if (list_empty(&gameport_event_list)) {
327 spin_unlock_irqrestore(&gameport_event_lock, flags);
331 node = gameport_event_list.next;
332 event = list_entry(node, struct gameport_event, node);
335 spin_unlock_irqrestore(&gameport_event_lock, flags);
340 static void gameport_handle_event(void)
342 struct gameport_event *event;
343 struct gameport_driver *gameport_drv;
348 * Note that we handle only one event here to give swsusp
349 * a chance to freeze kgameportd thread. Gameport events
350 * should be pretty rare so we are not concerned about
351 * taking performance hit.
353 if ((event = gameport_get_event())) {
355 switch (event->type) {
356 case GAMEPORT_REGISTER_PORT:
357 gameport_add_port(event->object);
360 case GAMEPORT_RECONNECT:
361 gameport_reconnect_port(event->object);
364 case GAMEPORT_RESCAN:
365 gameport_disconnect_port(event->object);
366 gameport_find_driver(event->object);
369 case GAMEPORT_REGISTER_DRIVER:
370 gameport_drv = event->object;
371 driver_register(&gameport_drv->driver);
378 gameport_remove_duplicate_events(event);
379 gameport_free_event(event);
386 * Remove all events that have been submitted for a given gameport port.
388 static void gameport_remove_pending_events(struct gameport *gameport)
390 struct list_head *node, *next;
391 struct gameport_event *event;
394 spin_lock_irqsave(&gameport_event_lock, flags);
396 list_for_each_safe(node, next, &gameport_event_list) {
397 event = list_entry(node, struct gameport_event, node);
398 if (event->object == gameport) {
400 gameport_free_event(event);
404 spin_unlock_irqrestore(&gameport_event_lock, flags);
408 * Destroy child gameport port (if any) that has not been fully registered yet.
410 * Note that we rely on the fact that port can have only one child and therefore
411 * only one child registration request can be pending. Additionally, children
412 * are registered by driver's connect() handler so there can't be a grandchild
413 * pending registration together with a child.
415 static struct gameport *gameport_get_pending_child(struct gameport *parent)
417 struct gameport_event *event;
418 struct gameport *gameport, *child = NULL;
421 spin_lock_irqsave(&gameport_event_lock, flags);
423 list_for_each_entry(event, &gameport_event_list, node) {
424 if (event->type == GAMEPORT_REGISTER_PORT) {
425 gameport = event->object;
426 if (gameport->parent == parent) {
433 spin_unlock_irqrestore(&gameport_event_lock, flags);
437 static int gameport_thread(void *nothing)
440 gameport_handle_event();
441 wait_event_interruptible(gameport_wait,
442 kthread_should_stop() || !list_empty(&gameport_event_list));
444 } while (!kthread_should_stop());
446 printk(KERN_DEBUG "gameport: kgameportd exiting\n");
452 * Gameport port operations
455 static ssize_t gameport_show_description(struct device *dev, struct device_attribute *attr, char *buf)
457 struct gameport *gameport = to_gameport_port(dev);
458 return sprintf(buf, "%s\n", gameport->name);
461 static ssize_t gameport_rebind_driver(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
463 struct gameport *gameport = to_gameport_port(dev);
464 struct device_driver *drv;
467 retval = down_interruptible(&gameport_sem);
472 if (!strncmp(buf, "none", count)) {
473 gameport_disconnect_port(gameport);
474 } else if (!strncmp(buf, "reconnect", count)) {
475 gameport_reconnect_port(gameport);
476 } else if (!strncmp(buf, "rescan", count)) {
477 gameport_disconnect_port(gameport);
478 gameport_find_driver(gameport);
479 } else if ((drv = driver_find(buf, &gameport_bus)) != NULL) {
480 gameport_disconnect_port(gameport);
481 gameport_bind_driver(gameport, to_gameport_driver(drv));
492 static struct device_attribute gameport_device_attrs[] = {
493 __ATTR(description, S_IRUGO, gameport_show_description, NULL),
494 __ATTR(drvctl, S_IWUSR, NULL, gameport_rebind_driver),
498 static void gameport_release_port(struct device *dev)
500 struct gameport *gameport = to_gameport_port(dev);
503 module_put(THIS_MODULE);
506 void gameport_set_phys(struct gameport *gameport, const char *fmt, ...)
511 vsnprintf(gameport->phys, sizeof(gameport->phys), fmt, args);
516 * Prepare gameport port for registration.
518 static void gameport_init_port(struct gameport *gameport)
520 static atomic_t gameport_no = ATOMIC_INIT(0);
522 __module_get(THIS_MODULE);
524 init_MUTEX(&gameport->drv_sem);
525 device_initialize(&gameport->dev);
526 snprintf(gameport->dev.bus_id, sizeof(gameport->dev.bus_id),
527 "gameport%lu", (unsigned long)atomic_inc_return(&gameport_no) - 1);
528 gameport->dev.bus = &gameport_bus;
529 gameport->dev.release = gameport_release_port;
530 if (gameport->parent)
531 gameport->dev.parent = &gameport->parent->dev;
533 spin_lock_init(&gameport->timer_lock);
534 init_timer(&gameport->poll_timer);
535 gameport->poll_timer.function = gameport_run_poll_handler;
536 gameport->poll_timer.data = (unsigned long)gameport;
540 * Complete gameport port registration.
541 * Driver core will attempt to find appropriate driver for the port.
543 static void gameport_add_port(struct gameport *gameport)
545 if (gameport->parent)
546 gameport->parent->child = gameport;
548 gameport->speed = gameport_measure_speed(gameport);
550 list_add_tail(&gameport->node, &gameport_list);
553 printk(KERN_INFO "gameport: %s is %s, io %#x, speed %dkHz\n",
554 gameport->name, gameport->phys, gameport->io, gameport->speed);
556 printk(KERN_INFO "gameport: %s is %s, speed %dkHz\n",
557 gameport->name, gameport->phys, gameport->speed);
559 device_add(&gameport->dev);
560 gameport->registered = 1;
564 * gameport_destroy_port() completes deregistration process and removes
565 * port from the system
567 static void gameport_destroy_port(struct gameport *gameport)
569 struct gameport *child;
571 child = gameport_get_pending_child(gameport);
573 gameport_remove_pending_events(child);
574 put_device(&child->dev);
577 if (gameport->parent) {
578 gameport->parent->child = NULL;
579 gameport->parent = NULL;
582 if (gameport->registered) {
583 device_del(&gameport->dev);
584 list_del_init(&gameport->node);
585 gameport->registered = 0;
588 gameport_remove_pending_events(gameport);
589 put_device(&gameport->dev);
593 * Reconnect gameport port and all its children (re-initialize attached devices)
595 static void gameport_reconnect_port(struct gameport *gameport)
598 if (!gameport->drv || !gameport->drv->reconnect || gameport->drv->reconnect(gameport)) {
599 gameport_disconnect_port(gameport);
600 gameport_find_driver(gameport);
601 /* Ok, old children are now gone, we are done */
604 gameport = gameport->child;
609 * gameport_disconnect_port() unbinds a port from its driver. As a side effect
610 * all child ports are unbound and destroyed.
612 static void gameport_disconnect_port(struct gameport *gameport)
614 struct gameport *s, *parent;
616 if (gameport->child) {
618 * Children ports should be disconnected and destroyed
619 * first, staring with the leaf one, since we don't want
622 for (s = gameport; s->child; s = s->child)
628 gameport_release_driver(s);
629 gameport_destroy_port(s);
630 } while ((s = parent) != gameport);
634 * Ok, no children left, now disconnect this port
636 gameport_release_driver(gameport);
639 void gameport_rescan(struct gameport *gameport)
641 gameport_queue_event(gameport, NULL, GAMEPORT_RESCAN);
644 void gameport_reconnect(struct gameport *gameport)
646 gameport_queue_event(gameport, NULL, GAMEPORT_RECONNECT);
650 * Submits register request to kgameportd for subsequent execution.
651 * Note that port registration is always asynchronous.
653 void __gameport_register_port(struct gameport *gameport, struct module *owner)
655 gameport_init_port(gameport);
656 gameport_queue_event(gameport, owner, GAMEPORT_REGISTER_PORT);
660 * Synchronously unregisters gameport port.
662 void gameport_unregister_port(struct gameport *gameport)
665 gameport_disconnect_port(gameport);
666 gameport_destroy_port(gameport);
672 * Gameport driver operations
675 static ssize_t gameport_driver_show_description(struct device_driver *drv, char *buf)
677 struct gameport_driver *driver = to_gameport_driver(drv);
678 return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
681 static struct driver_attribute gameport_driver_attrs[] = {
682 __ATTR(description, S_IRUGO, gameport_driver_show_description, NULL),
686 static int gameport_driver_probe(struct device *dev)
688 struct gameport *gameport = to_gameport_port(dev);
689 struct gameport_driver *drv = to_gameport_driver(dev->driver);
691 drv->connect(gameport, drv);
692 return gameport->drv ? 0 : -ENODEV;
695 static int gameport_driver_remove(struct device *dev)
697 struct gameport *gameport = to_gameport_port(dev);
698 struct gameport_driver *drv = to_gameport_driver(dev->driver);
700 drv->disconnect(gameport);
704 static struct bus_type gameport_bus = {
706 .probe = gameport_driver_probe,
707 .remove = gameport_driver_remove,
710 void __gameport_register_driver(struct gameport_driver *drv, struct module *owner)
712 drv->driver.bus = &gameport_bus;
713 gameport_queue_event(drv, owner, GAMEPORT_REGISTER_DRIVER);
716 void gameport_unregister_driver(struct gameport_driver *drv)
718 struct gameport *gameport;
721 drv->ignore = 1; /* so gameport_find_driver ignores it */
724 list_for_each_entry(gameport, &gameport_list, node) {
725 if (gameport->drv == drv) {
726 gameport_disconnect_port(gameport);
727 gameport_find_driver(gameport);
728 /* we could've deleted some ports, restart */
733 driver_unregister(&drv->driver);
737 static int gameport_bus_match(struct device *dev, struct device_driver *drv)
739 struct gameport_driver *gameport_drv = to_gameport_driver(drv);
741 return !gameport_drv->ignore;
744 static void gameport_set_drv(struct gameport *gameport, struct gameport_driver *drv)
746 down(&gameport->drv_sem);
748 up(&gameport->drv_sem);
751 int gameport_open(struct gameport *gameport, struct gameport_driver *drv, int mode)
754 if (gameport->open) {
755 if (gameport->open(gameport, mode)) {
759 if (mode != GAMEPORT_MODE_RAW)
763 gameport_set_drv(gameport, drv);
767 void gameport_close(struct gameport *gameport)
769 del_timer_sync(&gameport->poll_timer);
770 gameport->poll_handler = NULL;
771 gameport->poll_interval = 0;
772 gameport_set_drv(gameport, NULL);
774 gameport->close(gameport);
777 static int __init gameport_init(void)
779 gameport_task = kthread_run(gameport_thread, NULL, "kgameportd");
780 if (IS_ERR(gameport_task)) {
781 printk(KERN_ERR "gameport: Failed to start kgameportd\n");
782 return PTR_ERR(gameport_task);
785 gameport_bus.dev_attrs = gameport_device_attrs;
786 gameport_bus.drv_attrs = gameport_driver_attrs;
787 gameport_bus.match = gameport_bus_match;
788 bus_register(&gameport_bus);
793 static void __exit gameport_exit(void)
795 bus_unregister(&gameport_bus);
796 kthread_stop(gameport_task);
799 module_init(gameport_init);
800 module_exit(gameport_exit);