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 */
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_rescan);
41 EXPORT_SYMBOL(gameport_set_phys);
42 EXPORT_SYMBOL(gameport_start_polling);
43 EXPORT_SYMBOL(gameport_stop_polling);
46 * gameport_mutex protects entire gameport subsystem and is taken
47 * every time gameport port or driver registrered or unregistered.
49 static DEFINE_MUTEX(gameport_mutex);
51 static LIST_HEAD(gameport_list);
53 static struct bus_type gameport_bus;
55 static void gameport_add_driver(struct gameport_driver *drv);
56 static void gameport_add_port(struct gameport *gameport);
57 static void gameport_destroy_port(struct gameport *gameport);
58 static void gameport_reconnect_port(struct gameport *gameport);
59 static void gameport_disconnect_port(struct gameport *gameport);
63 #include <asm/i8253.h>
65 #define DELTA(x,y) ((y)-(x)+((y)<(x)?1193182/HZ:0))
66 #define GET_TIME(x) do { x = get_time_pit(); } while (0)
68 static unsigned int get_time_pit(void)
73 spin_lock_irqsave(&i8253_lock, flags);
76 count |= inb_p(0x40) << 8;
77 spin_unlock_irqrestore(&i8253_lock, flags);
87 * gameport_measure_speed() measures the gameport i/o speed.
90 static int gameport_measure_speed(struct gameport *gameport)
94 unsigned int i, t, t1, t2, t3, tx;
97 if (gameport_open(gameport, NULL, GAMEPORT_MODE_RAW))
102 for(i = 0; i < 50; i++) {
103 local_irq_save(flags);
105 for (t = 0; t < 50; t++) gameport_read(gameport);
108 local_irq_restore(flags);
110 if ((t = DELTA(t2,t1) - DELTA(t3,t2)) < tx) tx = t;
113 gameport_close(gameport);
114 return 59659 / (tx < 1 ? 1 : tx);
116 #elif defined (__x86_64__)
119 unsigned long tx, t1, t2, flags;
121 if (gameport_open(gameport, NULL, GAMEPORT_MODE_RAW))
126 for(i = 0; i < 50; i++) {
127 local_irq_save(flags);
129 for (t = 0; t < 50; t++) gameport_read(gameport);
131 local_irq_restore(flags);
133 if (t2 - t1 < tx) tx = t2 - t1;
136 gameport_close(gameport);
137 return (cpu_data(raw_smp_processor_id()).loops_per_jiffy *
138 (unsigned long)HZ / (1000 / 50)) / (tx < 1 ? 1 : tx);
142 unsigned int j, t = 0;
144 if (gameport_open(gameport, NULL, GAMEPORT_MODE_RAW))
147 j = jiffies; while (j == jiffies);
148 j = jiffies; while (j == jiffies) { t++; gameport_read(gameport); }
150 gameport_close(gameport);
151 return t * HZ / 1000;
156 void gameport_start_polling(struct gameport *gameport)
158 spin_lock(&gameport->timer_lock);
160 if (!gameport->poll_cnt++) {
161 BUG_ON(!gameport->poll_handler);
162 BUG_ON(!gameport->poll_interval);
163 mod_timer(&gameport->poll_timer, jiffies + msecs_to_jiffies(gameport->poll_interval));
166 spin_unlock(&gameport->timer_lock);
169 void gameport_stop_polling(struct gameport *gameport)
171 spin_lock(&gameport->timer_lock);
173 if (!--gameport->poll_cnt)
174 del_timer(&gameport->poll_timer);
176 spin_unlock(&gameport->timer_lock);
179 static void gameport_run_poll_handler(unsigned long d)
181 struct gameport *gameport = (struct gameport *)d;
183 gameport->poll_handler(gameport);
184 if (gameport->poll_cnt)
185 mod_timer(&gameport->poll_timer, jiffies + msecs_to_jiffies(gameport->poll_interval));
189 * Basic gameport -> driver core mappings
192 static int gameport_bind_driver(struct gameport *gameport, struct gameport_driver *drv)
196 gameport->dev.driver = &drv->driver;
197 if (drv->connect(gameport, drv)) {
198 gameport->dev.driver = NULL;
202 error = device_bind_driver(&gameport->dev);
205 "gameport: device_bind_driver() failed "
206 "for %s (%s) and %s, error: %d\n",
207 gameport->phys, gameport->name,
208 drv->description, error);
209 drv->disconnect(gameport);
210 gameport->dev.driver = NULL;
217 static void gameport_find_driver(struct gameport *gameport)
221 error = device_attach(&gameport->dev);
224 "gameport: device_attach() failed for %s (%s), error: %d\n",
225 gameport->phys, gameport->name, error);
230 * Gameport event processing.
233 enum gameport_event_type {
236 GAMEPORT_REGISTER_PORT,
237 GAMEPORT_REGISTER_DRIVER,
240 struct gameport_event {
241 enum gameport_event_type type;
243 struct module *owner;
244 struct list_head node;
247 static DEFINE_SPINLOCK(gameport_event_lock); /* protects gameport_event_list */
248 static LIST_HEAD(gameport_event_list);
249 static DECLARE_WAIT_QUEUE_HEAD(gameport_wait);
250 static struct task_struct *gameport_task;
252 static void gameport_queue_event(void *object, struct module *owner,
253 enum gameport_event_type event_type)
256 struct gameport_event *event;
258 spin_lock_irqsave(&gameport_event_lock, flags);
261 * Scan event list for the other events for the same gameport port,
262 * starting with the most recent one. If event is the same we
263 * do not need add new one. If event is of different type we
264 * need to add this event and should not look further because
265 * we need to preseve sequence of distinct events.
267 list_for_each_entry_reverse(event, &gameport_event_list, node) {
268 if (event->object == object) {
269 if (event->type == event_type)
275 if ((event = kmalloc(sizeof(struct gameport_event), GFP_ATOMIC))) {
276 if (!try_module_get(owner)) {
277 printk(KERN_WARNING "gameport: Can't get module reference, dropping event %d\n", event_type);
282 event->type = event_type;
283 event->object = object;
284 event->owner = owner;
286 list_add_tail(&event->node, &gameport_event_list);
287 wake_up(&gameport_wait);
289 printk(KERN_ERR "gameport: Not enough memory to queue event %d\n", event_type);
292 spin_unlock_irqrestore(&gameport_event_lock, flags);
295 static void gameport_free_event(struct gameport_event *event)
297 module_put(event->owner);
301 static void gameport_remove_duplicate_events(struct gameport_event *event)
303 struct list_head *node, *next;
304 struct gameport_event *e;
307 spin_lock_irqsave(&gameport_event_lock, flags);
309 list_for_each_safe(node, next, &gameport_event_list) {
310 e = list_entry(node, struct gameport_event, node);
311 if (event->object == e->object) {
313 * If this event is of different type we should not
314 * look further - we only suppress duplicate events
315 * that were sent back-to-back.
317 if (event->type != e->type)
321 gameport_free_event(e);
325 spin_unlock_irqrestore(&gameport_event_lock, flags);
328 static struct gameport_event *gameport_get_event(void)
330 struct gameport_event *event;
331 struct list_head *node;
334 spin_lock_irqsave(&gameport_event_lock, flags);
336 if (list_empty(&gameport_event_list)) {
337 spin_unlock_irqrestore(&gameport_event_lock, flags);
341 node = gameport_event_list.next;
342 event = list_entry(node, struct gameport_event, node);
345 spin_unlock_irqrestore(&gameport_event_lock, flags);
350 static void gameport_handle_event(void)
352 struct gameport_event *event;
354 mutex_lock(&gameport_mutex);
357 * Note that we handle only one event here to give swsusp
358 * a chance to freeze kgameportd thread. Gameport events
359 * should be pretty rare so we are not concerned about
360 * taking performance hit.
362 if ((event = gameport_get_event())) {
364 switch (event->type) {
365 case GAMEPORT_REGISTER_PORT:
366 gameport_add_port(event->object);
369 case GAMEPORT_RECONNECT:
370 gameport_reconnect_port(event->object);
373 case GAMEPORT_RESCAN:
374 gameport_disconnect_port(event->object);
375 gameport_find_driver(event->object);
378 case GAMEPORT_REGISTER_DRIVER:
379 gameport_add_driver(event->object);
386 gameport_remove_duplicate_events(event);
387 gameport_free_event(event);
390 mutex_unlock(&gameport_mutex);
394 * Remove all events that have been submitted for a given gameport port.
396 static void gameport_remove_pending_events(struct gameport *gameport)
398 struct list_head *node, *next;
399 struct gameport_event *event;
402 spin_lock_irqsave(&gameport_event_lock, flags);
404 list_for_each_safe(node, next, &gameport_event_list) {
405 event = list_entry(node, struct gameport_event, node);
406 if (event->object == gameport) {
408 gameport_free_event(event);
412 spin_unlock_irqrestore(&gameport_event_lock, flags);
416 * Destroy child gameport port (if any) that has not been fully registered yet.
418 * Note that we rely on the fact that port can have only one child and therefore
419 * only one child registration request can be pending. Additionally, children
420 * are registered by driver's connect() handler so there can't be a grandchild
421 * pending registration together with a child.
423 static struct gameport *gameport_get_pending_child(struct gameport *parent)
425 struct gameport_event *event;
426 struct gameport *gameport, *child = NULL;
429 spin_lock_irqsave(&gameport_event_lock, flags);
431 list_for_each_entry(event, &gameport_event_list, node) {
432 if (event->type == GAMEPORT_REGISTER_PORT) {
433 gameport = event->object;
434 if (gameport->parent == parent) {
441 spin_unlock_irqrestore(&gameport_event_lock, flags);
445 static int gameport_thread(void *nothing)
449 gameport_handle_event();
450 wait_event_freezable(gameport_wait,
451 kthread_should_stop() || !list_empty(&gameport_event_list));
452 } while (!kthread_should_stop());
454 printk(KERN_DEBUG "gameport: kgameportd exiting\n");
460 * Gameport port operations
463 static ssize_t gameport_show_description(struct device *dev, struct device_attribute *attr, char *buf)
465 struct gameport *gameport = to_gameport_port(dev);
466 return sprintf(buf, "%s\n", gameport->name);
469 static ssize_t gameport_rebind_driver(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
471 struct gameport *gameport = to_gameport_port(dev);
472 struct device_driver *drv;
475 error = mutex_lock_interruptible(&gameport_mutex);
479 if (!strncmp(buf, "none", count)) {
480 gameport_disconnect_port(gameport);
481 } else if (!strncmp(buf, "reconnect", count)) {
482 gameport_reconnect_port(gameport);
483 } else if (!strncmp(buf, "rescan", count)) {
484 gameport_disconnect_port(gameport);
485 gameport_find_driver(gameport);
486 } else if ((drv = driver_find(buf, &gameport_bus)) != NULL) {
487 gameport_disconnect_port(gameport);
488 error = gameport_bind_driver(gameport, to_gameport_driver(drv));
494 mutex_unlock(&gameport_mutex);
496 return error ? error : count;
499 static struct device_attribute gameport_device_attrs[] = {
500 __ATTR(description, S_IRUGO, gameport_show_description, NULL),
501 __ATTR(drvctl, S_IWUSR, NULL, gameport_rebind_driver),
505 static void gameport_release_port(struct device *dev)
507 struct gameport *gameport = to_gameport_port(dev);
510 module_put(THIS_MODULE);
513 void gameport_set_phys(struct gameport *gameport, const char *fmt, ...)
518 vsnprintf(gameport->phys, sizeof(gameport->phys), fmt, args);
523 * Prepare gameport port for registration.
525 static void gameport_init_port(struct gameport *gameport)
527 static atomic_t gameport_no = ATOMIC_INIT(0);
529 __module_get(THIS_MODULE);
531 mutex_init(&gameport->drv_mutex);
532 device_initialize(&gameport->dev);
533 snprintf(gameport->dev.bus_id, sizeof(gameport->dev.bus_id),
534 "gameport%lu", (unsigned long)atomic_inc_return(&gameport_no) - 1);
535 gameport->dev.bus = &gameport_bus;
536 gameport->dev.release = gameport_release_port;
537 if (gameport->parent)
538 gameport->dev.parent = &gameport->parent->dev;
540 INIT_LIST_HEAD(&gameport->node);
541 spin_lock_init(&gameport->timer_lock);
542 init_timer(&gameport->poll_timer);
543 gameport->poll_timer.function = gameport_run_poll_handler;
544 gameport->poll_timer.data = (unsigned long)gameport;
548 * Complete gameport port registration.
549 * Driver core will attempt to find appropriate driver for the port.
551 static void gameport_add_port(struct gameport *gameport)
555 if (gameport->parent)
556 gameport->parent->child = gameport;
558 gameport->speed = gameport_measure_speed(gameport);
560 list_add_tail(&gameport->node, &gameport_list);
563 printk(KERN_INFO "gameport: %s is %s, io %#x, speed %dkHz\n",
564 gameport->name, gameport->phys, gameport->io, gameport->speed);
566 printk(KERN_INFO "gameport: %s is %s, speed %dkHz\n",
567 gameport->name, gameport->phys, gameport->speed);
569 error = device_add(&gameport->dev);
572 "gameport: device_add() failed for %s (%s), error: %d\n",
573 gameport->phys, gameport->name, error);
575 gameport->registered = 1;
579 * gameport_destroy_port() completes deregistration process and removes
580 * port from the system
582 static void gameport_destroy_port(struct gameport *gameport)
584 struct gameport *child;
586 child = gameport_get_pending_child(gameport);
588 gameport_remove_pending_events(child);
589 put_device(&child->dev);
592 if (gameport->parent) {
593 gameport->parent->child = NULL;
594 gameport->parent = NULL;
597 if (gameport->registered) {
598 device_del(&gameport->dev);
599 gameport->registered = 0;
602 list_del_init(&gameport->node);
604 gameport_remove_pending_events(gameport);
605 put_device(&gameport->dev);
609 * Reconnect gameport port and all its children (re-initialize attached devices)
611 static void gameport_reconnect_port(struct gameport *gameport)
614 if (!gameport->drv || !gameport->drv->reconnect || gameport->drv->reconnect(gameport)) {
615 gameport_disconnect_port(gameport);
616 gameport_find_driver(gameport);
617 /* Ok, old children are now gone, we are done */
620 gameport = gameport->child;
625 * gameport_disconnect_port() unbinds a port from its driver. As a side effect
626 * all child ports are unbound and destroyed.
628 static void gameport_disconnect_port(struct gameport *gameport)
630 struct gameport *s, *parent;
632 if (gameport->child) {
634 * Children ports should be disconnected and destroyed
635 * first, staring with the leaf one, since we don't want
638 for (s = gameport; s->child; s = s->child)
644 device_release_driver(&s->dev);
645 gameport_destroy_port(s);
646 } while ((s = parent) != gameport);
650 * Ok, no children left, now disconnect this port
652 device_release_driver(&gameport->dev);
655 void gameport_rescan(struct gameport *gameport)
657 gameport_queue_event(gameport, NULL, GAMEPORT_RESCAN);
660 void gameport_reconnect(struct gameport *gameport)
662 gameport_queue_event(gameport, NULL, GAMEPORT_RECONNECT);
666 * Submits register request to kgameportd for subsequent execution.
667 * Note that port registration is always asynchronous.
669 void __gameport_register_port(struct gameport *gameport, struct module *owner)
671 gameport_init_port(gameport);
672 gameport_queue_event(gameport, owner, GAMEPORT_REGISTER_PORT);
676 * Synchronously unregisters gameport port.
678 void gameport_unregister_port(struct gameport *gameport)
680 mutex_lock(&gameport_mutex);
681 gameport_disconnect_port(gameport);
682 gameport_destroy_port(gameport);
683 mutex_unlock(&gameport_mutex);
688 * Gameport driver operations
691 static ssize_t gameport_driver_show_description(struct device_driver *drv, char *buf)
693 struct gameport_driver *driver = to_gameport_driver(drv);
694 return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
697 static struct driver_attribute gameport_driver_attrs[] = {
698 __ATTR(description, S_IRUGO, gameport_driver_show_description, NULL),
702 static int gameport_driver_probe(struct device *dev)
704 struct gameport *gameport = to_gameport_port(dev);
705 struct gameport_driver *drv = to_gameport_driver(dev->driver);
707 drv->connect(gameport, drv);
708 return gameport->drv ? 0 : -ENODEV;
711 static int gameport_driver_remove(struct device *dev)
713 struct gameport *gameport = to_gameport_port(dev);
714 struct gameport_driver *drv = to_gameport_driver(dev->driver);
716 drv->disconnect(gameport);
720 static void gameport_add_driver(struct gameport_driver *drv)
724 error = driver_register(&drv->driver);
727 "gameport: driver_register() failed for %s, error: %d\n",
728 drv->driver.name, error);
731 void __gameport_register_driver(struct gameport_driver *drv, struct module *owner)
733 drv->driver.bus = &gameport_bus;
734 gameport_queue_event(drv, owner, GAMEPORT_REGISTER_DRIVER);
737 void gameport_unregister_driver(struct gameport_driver *drv)
739 struct gameport *gameport;
741 mutex_lock(&gameport_mutex);
742 drv->ignore = 1; /* so gameport_find_driver ignores it */
745 list_for_each_entry(gameport, &gameport_list, node) {
746 if (gameport->drv == drv) {
747 gameport_disconnect_port(gameport);
748 gameport_find_driver(gameport);
749 /* we could've deleted some ports, restart */
754 driver_unregister(&drv->driver);
755 mutex_unlock(&gameport_mutex);
758 static int gameport_bus_match(struct device *dev, struct device_driver *drv)
760 struct gameport_driver *gameport_drv = to_gameport_driver(drv);
762 return !gameport_drv->ignore;
765 static struct bus_type gameport_bus = {
767 .dev_attrs = gameport_device_attrs,
768 .drv_attrs = gameport_driver_attrs,
769 .match = gameport_bus_match,
770 .probe = gameport_driver_probe,
771 .remove = gameport_driver_remove,
774 static void gameport_set_drv(struct gameport *gameport, struct gameport_driver *drv)
776 mutex_lock(&gameport->drv_mutex);
778 mutex_unlock(&gameport->drv_mutex);
781 int gameport_open(struct gameport *gameport, struct gameport_driver *drv, int mode)
783 if (gameport->open) {
784 if (gameport->open(gameport, mode)) {
788 if (mode != GAMEPORT_MODE_RAW)
792 gameport_set_drv(gameport, drv);
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);
806 static int __init gameport_init(void)
810 error = bus_register(&gameport_bus);
812 printk(KERN_ERR "gameport: failed to register gameport bus, error: %d\n", error);
816 gameport_task = kthread_run(gameport_thread, NULL, "kgameportd");
817 if (IS_ERR(gameport_task)) {
818 bus_unregister(&gameport_bus);
819 error = PTR_ERR(gameport_task);
820 printk(KERN_ERR "gameport: Failed to start kgameportd, error: %d\n", error);
827 static void __exit gameport_exit(void)
829 bus_unregister(&gameport_bus);
830 kthread_stop(gameport_task);
833 subsys_initcall(gameport_init);
834 module_exit(gameport_exit);