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/sched.h> /* HZ */
24 #include <linux/mutex.h>
25 #include <linux/freezer.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 spin_lock_irqsave(&i8253_lock, flags);
63 count |= inb_p(0x40) << 8;
64 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 (cpu_data(raw_smp_processor_id()).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);
194 "gameport: device_bind_driver() failed "
195 "for %s (%s) and %s, error: %d\n",
196 gameport->phys, gameport->name,
197 drv->description, error);
198 drv->disconnect(gameport);
199 gameport->dev.driver = NULL;
206 static void gameport_find_driver(struct gameport *gameport)
210 error = device_attach(&gameport->dev);
213 "gameport: device_attach() failed for %s (%s), error: %d\n",
214 gameport->phys, gameport->name, error);
219 * Gameport event processing.
222 enum gameport_event_type {
223 GAMEPORT_REGISTER_PORT,
224 GAMEPORT_ATTACH_DRIVER,
227 struct gameport_event {
228 enum gameport_event_type type;
230 struct module *owner;
231 struct list_head node;
234 static DEFINE_SPINLOCK(gameport_event_lock); /* protects gameport_event_list */
235 static LIST_HEAD(gameport_event_list);
236 static DECLARE_WAIT_QUEUE_HEAD(gameport_wait);
237 static struct task_struct *gameport_task;
239 static int gameport_queue_event(void *object, struct module *owner,
240 enum gameport_event_type event_type)
243 struct gameport_event *event;
246 spin_lock_irqsave(&gameport_event_lock, flags);
249 * Scan event list for the other events for the same gameport port,
250 * starting with the most recent one. If event is the same we
251 * do not need add new one. If event is of different type we
252 * need to add this event and should not look further because
253 * we need to preseve sequence of distinct events.
255 list_for_each_entry_reverse(event, &gameport_event_list, node) {
256 if (event->object == object) {
257 if (event->type == event_type)
263 event = kmalloc(sizeof(struct gameport_event), GFP_ATOMIC);
266 "gameport: Not enough memory to queue event %d\n",
272 if (!try_module_get(owner)) {
274 "gameport: Can't get module reference, dropping event %d\n",
281 event->type = event_type;
282 event->object = object;
283 event->owner = owner;
285 list_add_tail(&event->node, &gameport_event_list);
286 wake_up(&gameport_wait);
289 spin_unlock_irqrestore(&gameport_event_lock, flags);
293 static void gameport_free_event(struct gameport_event *event)
295 module_put(event->owner);
299 static void gameport_remove_duplicate_events(struct gameport_event *event)
301 struct list_head *node, *next;
302 struct gameport_event *e;
305 spin_lock_irqsave(&gameport_event_lock, flags);
307 list_for_each_safe(node, next, &gameport_event_list) {
308 e = list_entry(node, struct gameport_event, node);
309 if (event->object == e->object) {
311 * If this event is of different type we should not
312 * look further - we only suppress duplicate events
313 * that were sent back-to-back.
315 if (event->type != e->type)
319 gameport_free_event(e);
323 spin_unlock_irqrestore(&gameport_event_lock, flags);
326 static struct gameport_event *gameport_get_event(void)
328 struct gameport_event *event;
329 struct list_head *node;
332 spin_lock_irqsave(&gameport_event_lock, flags);
334 if (list_empty(&gameport_event_list)) {
335 spin_unlock_irqrestore(&gameport_event_lock, flags);
339 node = gameport_event_list.next;
340 event = list_entry(node, struct gameport_event, node);
343 spin_unlock_irqrestore(&gameport_event_lock, flags);
348 static void gameport_handle_event(void)
350 struct gameport_event *event;
352 mutex_lock(&gameport_mutex);
355 * Note that we handle only one event here to give swsusp
356 * a chance to freeze kgameportd thread. Gameport events
357 * should be pretty rare so we are not concerned about
358 * taking performance hit.
360 if ((event = gameport_get_event())) {
362 switch (event->type) {
363 case GAMEPORT_REGISTER_PORT:
364 gameport_add_port(event->object);
367 case GAMEPORT_ATTACH_DRIVER:
368 gameport_attach_driver(event->object);
375 gameport_remove_duplicate_events(event);
376 gameport_free_event(event);
379 mutex_unlock(&gameport_mutex);
383 * Remove all events that have been submitted for a given object,
384 * be it a gameport port or a driver.
386 static void gameport_remove_pending_events(void *object)
388 struct list_head *node, *next;
389 struct gameport_event *event;
392 spin_lock_irqsave(&gameport_event_lock, flags);
394 list_for_each_safe(node, next, &gameport_event_list) {
395 event = list_entry(node, struct gameport_event, node);
396 if (event->object == object) {
398 gameport_free_event(event);
402 spin_unlock_irqrestore(&gameport_event_lock, flags);
406 * Destroy child gameport port (if any) that has not been fully registered yet.
408 * Note that we rely on the fact that port can have only one child and therefore
409 * only one child registration request can be pending. Additionally, children
410 * are registered by driver's connect() handler so there can't be a grandchild
411 * pending registration together with a child.
413 static struct gameport *gameport_get_pending_child(struct gameport *parent)
415 struct gameport_event *event;
416 struct gameport *gameport, *child = NULL;
419 spin_lock_irqsave(&gameport_event_lock, flags);
421 list_for_each_entry(event, &gameport_event_list, node) {
422 if (event->type == GAMEPORT_REGISTER_PORT) {
423 gameport = event->object;
424 if (gameport->parent == parent) {
431 spin_unlock_irqrestore(&gameport_event_lock, flags);
435 static int gameport_thread(void *nothing)
439 gameport_handle_event();
440 wait_event_freezable(gameport_wait,
441 kthread_should_stop() || !list_empty(&gameport_event_list));
442 } while (!kthread_should_stop());
444 printk(KERN_DEBUG "gameport: kgameportd exiting\n");
450 * Gameport port operations
453 static ssize_t gameport_show_description(struct device *dev, struct device_attribute *attr, char *buf)
455 struct gameport *gameport = to_gameport_port(dev);
456 return sprintf(buf, "%s\n", gameport->name);
459 static ssize_t gameport_rebind_driver(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
461 struct gameport *gameport = to_gameport_port(dev);
462 struct device_driver *drv;
465 error = mutex_lock_interruptible(&gameport_mutex);
469 if (!strncmp(buf, "none", count)) {
470 gameport_disconnect_port(gameport);
471 } else if (!strncmp(buf, "reconnect", count)) {
472 gameport_reconnect_port(gameport);
473 } else if (!strncmp(buf, "rescan", count)) {
474 gameport_disconnect_port(gameport);
475 gameport_find_driver(gameport);
476 } else if ((drv = driver_find(buf, &gameport_bus)) != NULL) {
477 gameport_disconnect_port(gameport);
478 error = gameport_bind_driver(gameport, to_gameport_driver(drv));
484 mutex_unlock(&gameport_mutex);
486 return error ? error : count;
489 static struct device_attribute gameport_device_attrs[] = {
490 __ATTR(description, S_IRUGO, gameport_show_description, NULL),
491 __ATTR(drvctl, S_IWUSR, NULL, gameport_rebind_driver),
495 static void gameport_release_port(struct device *dev)
497 struct gameport *gameport = to_gameport_port(dev);
500 module_put(THIS_MODULE);
503 void gameport_set_phys(struct gameport *gameport, const char *fmt, ...)
508 vsnprintf(gameport->phys, sizeof(gameport->phys), fmt, args);
511 EXPORT_SYMBOL(gameport_set_phys);
514 * Prepare gameport port for registration.
516 static void gameport_init_port(struct gameport *gameport)
518 static atomic_t gameport_no = ATOMIC_INIT(0);
520 __module_get(THIS_MODULE);
522 mutex_init(&gameport->drv_mutex);
523 device_initialize(&gameport->dev);
524 dev_set_name(&gameport->dev, "gameport%lu", (unsigned long)atomic_inc_return(&gameport_no) - 1);
525 gameport->dev.bus = &gameport_bus;
526 gameport->dev.release = gameport_release_port;
527 if (gameport->parent)
528 gameport->dev.parent = &gameport->parent->dev;
530 INIT_LIST_HEAD(&gameport->node);
531 spin_lock_init(&gameport->timer_lock);
532 init_timer(&gameport->poll_timer);
533 gameport->poll_timer.function = gameport_run_poll_handler;
534 gameport->poll_timer.data = (unsigned long)gameport;
538 * Complete gameport port registration.
539 * Driver core will attempt to find appropriate driver for the port.
541 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 error = device_add(&gameport->dev);
562 "gameport: device_add() failed for %s (%s), error: %d\n",
563 gameport->phys, gameport->name, error);
567 * gameport_destroy_port() completes deregistration process and removes
568 * port from the system
570 static void gameport_destroy_port(struct gameport *gameport)
572 struct gameport *child;
574 child = gameport_get_pending_child(gameport);
576 gameport_remove_pending_events(child);
577 put_device(&child->dev);
580 if (gameport->parent) {
581 gameport->parent->child = NULL;
582 gameport->parent = NULL;
585 if (device_is_registered(&gameport->dev))
586 device_del(&gameport->dev);
588 list_del_init(&gameport->node);
590 gameport_remove_pending_events(gameport);
591 put_device(&gameport->dev);
595 * Reconnect gameport port and all its children (re-initialize attached devices)
597 static void gameport_reconnect_port(struct gameport *gameport)
600 if (!gameport->drv || !gameport->drv->reconnect || gameport->drv->reconnect(gameport)) {
601 gameport_disconnect_port(gameport);
602 gameport_find_driver(gameport);
603 /* Ok, old children are now gone, we are done */
606 gameport = gameport->child;
611 * gameport_disconnect_port() unbinds a port from its driver. As a side effect
612 * all child ports are unbound and destroyed.
614 static void gameport_disconnect_port(struct gameport *gameport)
616 struct gameport *s, *parent;
618 if (gameport->child) {
620 * Children ports should be disconnected and destroyed
621 * first, staring with the leaf one, since we don't want
624 for (s = gameport; s->child; s = s->child)
630 device_release_driver(&s->dev);
631 gameport_destroy_port(s);
632 } while ((s = parent) != gameport);
636 * Ok, no children left, now disconnect this port
638 device_release_driver(&gameport->dev);
642 * Submits register request to kgameportd for subsequent execution.
643 * Note that port registration is always asynchronous.
645 void __gameport_register_port(struct gameport *gameport, struct module *owner)
647 gameport_init_port(gameport);
648 gameport_queue_event(gameport, owner, GAMEPORT_REGISTER_PORT);
650 EXPORT_SYMBOL(__gameport_register_port);
653 * Synchronously unregisters gameport port.
655 void gameport_unregister_port(struct gameport *gameport)
657 mutex_lock(&gameport_mutex);
658 gameport_disconnect_port(gameport);
659 gameport_destroy_port(gameport);
660 mutex_unlock(&gameport_mutex);
662 EXPORT_SYMBOL(gameport_unregister_port);
666 * Gameport driver operations
669 static ssize_t gameport_driver_show_description(struct device_driver *drv, char *buf)
671 struct gameport_driver *driver = to_gameport_driver(drv);
672 return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
675 static struct driver_attribute gameport_driver_attrs[] = {
676 __ATTR(description, S_IRUGO, gameport_driver_show_description, NULL),
680 static int gameport_driver_probe(struct device *dev)
682 struct gameport *gameport = to_gameport_port(dev);
683 struct gameport_driver *drv = to_gameport_driver(dev->driver);
685 drv->connect(gameport, drv);
686 return gameport->drv ? 0 : -ENODEV;
689 static int gameport_driver_remove(struct device *dev)
691 struct gameport *gameport = to_gameport_port(dev);
692 struct gameport_driver *drv = to_gameport_driver(dev->driver);
694 drv->disconnect(gameport);
698 static void gameport_attach_driver(struct gameport_driver *drv)
702 error = driver_attach(&drv->driver);
705 "gameport: driver_attach() failed for %s, error: %d\n",
706 drv->driver.name, error);
709 int __gameport_register_driver(struct gameport_driver *drv, struct module *owner,
710 const char *mod_name)
714 drv->driver.bus = &gameport_bus;
715 drv->driver.owner = owner;
716 drv->driver.mod_name = mod_name;
719 * Temporarily disable automatic binding because probing
720 * takes long time and we are better off doing it in kgameportd
724 error = driver_register(&drv->driver);
727 "gameport: driver_register() failed for %s, error: %d\n",
728 drv->driver.name, error);
733 * Reset ignore flag and let kgameportd bind the driver to free ports
736 error = gameport_queue_event(drv, NULL, GAMEPORT_ATTACH_DRIVER);
738 driver_unregister(&drv->driver);
744 EXPORT_SYMBOL(__gameport_register_driver);
746 void gameport_unregister_driver(struct gameport_driver *drv)
748 struct gameport *gameport;
750 mutex_lock(&gameport_mutex);
752 drv->ignore = true; /* so gameport_find_driver ignores it */
753 gameport_remove_pending_events(drv);
756 list_for_each_entry(gameport, &gameport_list, node) {
757 if (gameport->drv == drv) {
758 gameport_disconnect_port(gameport);
759 gameport_find_driver(gameport);
760 /* we could've deleted some ports, restart */
765 driver_unregister(&drv->driver);
767 mutex_unlock(&gameport_mutex);
769 EXPORT_SYMBOL(gameport_unregister_driver);
771 static int gameport_bus_match(struct device *dev, struct device_driver *drv)
773 struct gameport_driver *gameport_drv = to_gameport_driver(drv);
775 return !gameport_drv->ignore;
778 static struct bus_type gameport_bus = {
780 .dev_attrs = gameport_device_attrs,
781 .drv_attrs = gameport_driver_attrs,
782 .match = gameport_bus_match,
783 .probe = gameport_driver_probe,
784 .remove = gameport_driver_remove,
787 static void gameport_set_drv(struct gameport *gameport, struct gameport_driver *drv)
789 mutex_lock(&gameport->drv_mutex);
791 mutex_unlock(&gameport->drv_mutex);
794 int gameport_open(struct gameport *gameport, struct gameport_driver *drv, int mode)
796 if (gameport->open) {
797 if (gameport->open(gameport, mode)) {
801 if (mode != GAMEPORT_MODE_RAW)
805 gameport_set_drv(gameport, drv);
808 EXPORT_SYMBOL(gameport_open);
810 void gameport_close(struct gameport *gameport)
812 del_timer_sync(&gameport->poll_timer);
813 gameport->poll_handler = NULL;
814 gameport->poll_interval = 0;
815 gameport_set_drv(gameport, NULL);
817 gameport->close(gameport);
819 EXPORT_SYMBOL(gameport_close);
821 static int __init gameport_init(void)
825 error = bus_register(&gameport_bus);
827 printk(KERN_ERR "gameport: failed to register gameport bus, error: %d\n", error);
831 gameport_task = kthread_run(gameport_thread, NULL, "kgameportd");
832 if (IS_ERR(gameport_task)) {
833 bus_unregister(&gameport_bus);
834 error = PTR_ERR(gameport_task);
835 printk(KERN_ERR "gameport: Failed to start kgameportd, error: %d\n", error);
842 static void __exit gameport_exit(void)
844 bus_unregister(&gameport_bus);
845 kthread_stop(gameport_task);
848 subsys_initcall(gameport_init);
849 module_exit(gameport_exit);