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>
27 /*#include <asm/io.h>*/
29 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
30 MODULE_DESCRIPTION("Generic gameport layer");
31 MODULE_LICENSE("GPL");
33 EXPORT_SYMBOL(__gameport_register_port);
34 EXPORT_SYMBOL(gameport_unregister_port);
35 EXPORT_SYMBOL(__gameport_register_driver);
36 EXPORT_SYMBOL(gameport_unregister_driver);
37 EXPORT_SYMBOL(gameport_open);
38 EXPORT_SYMBOL(gameport_close);
39 EXPORT_SYMBOL(gameport_rescan);
40 EXPORT_SYMBOL(gameport_cooked_read);
41 EXPORT_SYMBOL(gameport_set_name);
42 EXPORT_SYMBOL(gameport_set_phys);
43 EXPORT_SYMBOL(gameport_start_polling);
44 EXPORT_SYMBOL(gameport_stop_polling);
47 * gameport_mutex protects entire gameport subsystem and is taken
48 * every time gameport port or driver registrered or unregistered.
50 static DEFINE_MUTEX(gameport_mutex);
52 static LIST_HEAD(gameport_list);
54 static struct bus_type gameport_bus;
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 * (unsigned long)HZ / (1000 / 50)) / (tx < 1 ? 1 : tx);
141 unsigned int j, t = 0;
143 if (gameport_open(gameport, NULL, GAMEPORT_MODE_RAW))
146 j = jiffies; while (j == jiffies);
147 j = jiffies; while (j == jiffies) { t++; gameport_read(gameport); }
149 gameport_close(gameport);
150 return t * HZ / 1000;
155 void gameport_start_polling(struct gameport *gameport)
157 spin_lock(&gameport->timer_lock);
159 if (!gameport->poll_cnt++) {
160 BUG_ON(!gameport->poll_handler);
161 BUG_ON(!gameport->poll_interval);
162 mod_timer(&gameport->poll_timer, jiffies + msecs_to_jiffies(gameport->poll_interval));
165 spin_unlock(&gameport->timer_lock);
168 void gameport_stop_polling(struct gameport *gameport)
170 spin_lock(&gameport->timer_lock);
172 if (!--gameport->poll_cnt)
173 del_timer(&gameport->poll_timer);
175 spin_unlock(&gameport->timer_lock);
178 static void gameport_run_poll_handler(unsigned long d)
180 struct gameport *gameport = (struct gameport *)d;
182 gameport->poll_handler(gameport);
183 if (gameport->poll_cnt)
184 mod_timer(&gameport->poll_timer, jiffies + msecs_to_jiffies(gameport->poll_interval));
188 * Basic gameport -> driver core mappings
191 static void gameport_bind_driver(struct gameport *gameport, struct gameport_driver *drv)
193 down_write(&gameport_bus.subsys.rwsem);
195 gameport->dev.driver = &drv->driver;
196 if (drv->connect(gameport, drv)) {
197 gameport->dev.driver = NULL;
200 device_bind_driver(&gameport->dev);
202 up_write(&gameport_bus.subsys.rwsem);
205 static void gameport_release_driver(struct gameport *gameport)
207 down_write(&gameport_bus.subsys.rwsem);
208 device_release_driver(&gameport->dev);
209 up_write(&gameport_bus.subsys.rwsem);
212 static void gameport_find_driver(struct gameport *gameport)
214 down_write(&gameport_bus.subsys.rwsem);
215 device_attach(&gameport->dev);
216 up_write(&gameport_bus.subsys.rwsem);
221 * Gameport event processing.
224 enum gameport_event_type {
227 GAMEPORT_REGISTER_PORT,
228 GAMEPORT_REGISTER_DRIVER,
231 struct gameport_event {
232 enum gameport_event_type type;
234 struct module *owner;
235 struct list_head node;
238 static DEFINE_SPINLOCK(gameport_event_lock); /* protects gameport_event_list */
239 static LIST_HEAD(gameport_event_list);
240 static DECLARE_WAIT_QUEUE_HEAD(gameport_wait);
241 static struct task_struct *gameport_task;
243 static void gameport_queue_event(void *object, struct module *owner,
244 enum gameport_event_type event_type)
247 struct gameport_event *event;
249 spin_lock_irqsave(&gameport_event_lock, flags);
252 * Scan event list for the other events for the same gameport port,
253 * starting with the most recent one. If event is the same we
254 * do not need add new one. If event is of different type we
255 * need to add this event and should not look further because
256 * we need to preseve sequence of distinct events.
258 list_for_each_entry_reverse(event, &gameport_event_list, node) {
259 if (event->object == object) {
260 if (event->type == event_type)
266 if ((event = kmalloc(sizeof(struct gameport_event), GFP_ATOMIC))) {
267 if (!try_module_get(owner)) {
268 printk(KERN_WARNING "gameport: Can't get module reference, dropping event %d\n", event_type);
272 event->type = event_type;
273 event->object = object;
274 event->owner = owner;
276 list_add_tail(&event->node, &gameport_event_list);
277 wake_up(&gameport_wait);
279 printk(KERN_ERR "gameport: Not enough memory to queue event %d\n", event_type);
282 spin_unlock_irqrestore(&gameport_event_lock, flags);
285 static void gameport_free_event(struct gameport_event *event)
287 module_put(event->owner);
291 static void gameport_remove_duplicate_events(struct gameport_event *event)
293 struct list_head *node, *next;
294 struct gameport_event *e;
297 spin_lock_irqsave(&gameport_event_lock, flags);
299 list_for_each_safe(node, next, &gameport_event_list) {
300 e = list_entry(node, struct gameport_event, node);
301 if (event->object == e->object) {
303 * If this event is of different type we should not
304 * look further - we only suppress duplicate events
305 * that were sent back-to-back.
307 if (event->type != e->type)
311 gameport_free_event(e);
315 spin_unlock_irqrestore(&gameport_event_lock, flags);
319 static struct gameport_event *gameport_get_event(void)
321 struct gameport_event *event;
322 struct list_head *node;
325 spin_lock_irqsave(&gameport_event_lock, flags);
327 if (list_empty(&gameport_event_list)) {
328 spin_unlock_irqrestore(&gameport_event_lock, flags);
332 node = gameport_event_list.next;
333 event = list_entry(node, struct gameport_event, node);
336 spin_unlock_irqrestore(&gameport_event_lock, flags);
341 static void gameport_handle_event(void)
343 struct gameport_event *event;
344 struct gameport_driver *gameport_drv;
346 mutex_lock(&gameport_mutex);
349 * Note that we handle only one event here to give swsusp
350 * a chance to freeze kgameportd thread. Gameport events
351 * should be pretty rare so we are not concerned about
352 * taking performance hit.
354 if ((event = gameport_get_event())) {
356 switch (event->type) {
357 case GAMEPORT_REGISTER_PORT:
358 gameport_add_port(event->object);
361 case GAMEPORT_RECONNECT:
362 gameport_reconnect_port(event->object);
365 case GAMEPORT_RESCAN:
366 gameport_disconnect_port(event->object);
367 gameport_find_driver(event->object);
370 case GAMEPORT_REGISTER_DRIVER:
371 gameport_drv = event->object;
372 driver_register(&gameport_drv->driver);
379 gameport_remove_duplicate_events(event);
380 gameport_free_event(event);
383 mutex_unlock(&gameport_mutex);
387 * Remove all events that have been submitted for a given gameport port.
389 static void gameport_remove_pending_events(struct gameport *gameport)
391 struct list_head *node, *next;
392 struct gameport_event *event;
395 spin_lock_irqsave(&gameport_event_lock, flags);
397 list_for_each_safe(node, next, &gameport_event_list) {
398 event = list_entry(node, struct gameport_event, node);
399 if (event->object == gameport) {
401 gameport_free_event(event);
405 spin_unlock_irqrestore(&gameport_event_lock, flags);
409 * Destroy child gameport port (if any) that has not been fully registered yet.
411 * Note that we rely on the fact that port can have only one child and therefore
412 * only one child registration request can be pending. Additionally, children
413 * are registered by driver's connect() handler so there can't be a grandchild
414 * pending registration together with a child.
416 static struct gameport *gameport_get_pending_child(struct gameport *parent)
418 struct gameport_event *event;
419 struct gameport *gameport, *child = NULL;
422 spin_lock_irqsave(&gameport_event_lock, flags);
424 list_for_each_entry(event, &gameport_event_list, node) {
425 if (event->type == GAMEPORT_REGISTER_PORT) {
426 gameport = event->object;
427 if (gameport->parent == parent) {
434 spin_unlock_irqrestore(&gameport_event_lock, flags);
438 static int gameport_thread(void *nothing)
441 gameport_handle_event();
442 wait_event_interruptible(gameport_wait,
443 kthread_should_stop() || !list_empty(&gameport_event_list));
445 } while (!kthread_should_stop());
447 printk(KERN_DEBUG "gameport: kgameportd exiting\n");
453 * Gameport port operations
456 static ssize_t gameport_show_description(struct device *dev, struct device_attribute *attr, char *buf)
458 struct gameport *gameport = to_gameport_port(dev);
459 return sprintf(buf, "%s\n", gameport->name);
462 static ssize_t gameport_rebind_driver(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
464 struct gameport *gameport = to_gameport_port(dev);
465 struct device_driver *drv;
468 retval = mutex_lock_interruptible(&gameport_mutex);
473 if (!strncmp(buf, "none", count)) {
474 gameport_disconnect_port(gameport);
475 } else if (!strncmp(buf, "reconnect", count)) {
476 gameport_reconnect_port(gameport);
477 } else if (!strncmp(buf, "rescan", count)) {
478 gameport_disconnect_port(gameport);
479 gameport_find_driver(gameport);
480 } else if ((drv = driver_find(buf, &gameport_bus)) != NULL) {
481 gameport_disconnect_port(gameport);
482 gameport_bind_driver(gameport, to_gameport_driver(drv));
488 mutex_unlock(&gameport_mutex);
493 static struct device_attribute gameport_device_attrs[] = {
494 __ATTR(description, S_IRUGO, gameport_show_description, NULL),
495 __ATTR(drvctl, S_IWUSR, NULL, gameport_rebind_driver),
499 static void gameport_release_port(struct device *dev)
501 struct gameport *gameport = to_gameport_port(dev);
504 module_put(THIS_MODULE);
507 void gameport_set_phys(struct gameport *gameport, const char *fmt, ...)
512 vsnprintf(gameport->phys, sizeof(gameport->phys), fmt, args);
517 * Prepare gameport port for registration.
519 static void gameport_init_port(struct gameport *gameport)
521 static atomic_t gameport_no = ATOMIC_INIT(0);
523 __module_get(THIS_MODULE);
525 mutex_init(&gameport->drv_mutex);
526 device_initialize(&gameport->dev);
527 snprintf(gameport->dev.bus_id, sizeof(gameport->dev.bus_id),
528 "gameport%lu", (unsigned long)atomic_inc_return(&gameport_no) - 1);
529 gameport->dev.bus = &gameport_bus;
530 gameport->dev.release = gameport_release_port;
531 if (gameport->parent)
532 gameport->dev.parent = &gameport->parent->dev;
534 spin_lock_init(&gameport->timer_lock);
535 init_timer(&gameport->poll_timer);
536 gameport->poll_timer.function = gameport_run_poll_handler;
537 gameport->poll_timer.data = (unsigned long)gameport;
541 * Complete gameport port registration.
542 * Driver core will attempt to find appropriate driver for the port.
544 static void gameport_add_port(struct gameport *gameport)
546 if (gameport->parent)
547 gameport->parent->child = gameport;
549 gameport->speed = gameport_measure_speed(gameport);
551 list_add_tail(&gameport->node, &gameport_list);
554 printk(KERN_INFO "gameport: %s is %s, io %#x, speed %dkHz\n",
555 gameport->name, gameport->phys, gameport->io, gameport->speed);
557 printk(KERN_INFO "gameport: %s is %s, speed %dkHz\n",
558 gameport->name, gameport->phys, gameport->speed);
560 device_add(&gameport->dev);
561 gameport->registered = 1;
565 * gameport_destroy_port() completes deregistration process and removes
566 * port from the system
568 static void gameport_destroy_port(struct gameport *gameport)
570 struct gameport *child;
572 child = gameport_get_pending_child(gameport);
574 gameport_remove_pending_events(child);
575 put_device(&child->dev);
578 if (gameport->parent) {
579 gameport->parent->child = NULL;
580 gameport->parent = NULL;
583 if (gameport->registered) {
584 device_del(&gameport->dev);
585 list_del_init(&gameport->node);
586 gameport->registered = 0;
589 gameport_remove_pending_events(gameport);
590 put_device(&gameport->dev);
594 * Reconnect gameport port and all its children (re-initialize attached devices)
596 static void gameport_reconnect_port(struct gameport *gameport)
599 if (!gameport->drv || !gameport->drv->reconnect || gameport->drv->reconnect(gameport)) {
600 gameport_disconnect_port(gameport);
601 gameport_find_driver(gameport);
602 /* Ok, old children are now gone, we are done */
605 gameport = gameport->child;
610 * gameport_disconnect_port() unbinds a port from its driver. As a side effect
611 * all child ports are unbound and destroyed.
613 static void gameport_disconnect_port(struct gameport *gameport)
615 struct gameport *s, *parent;
617 if (gameport->child) {
619 * Children ports should be disconnected and destroyed
620 * first, staring with the leaf one, since we don't want
623 for (s = gameport; s->child; s = s->child)
629 gameport_release_driver(s);
630 gameport_destroy_port(s);
631 } while ((s = parent) != gameport);
635 * Ok, no children left, now disconnect this port
637 gameport_release_driver(gameport);
640 void gameport_rescan(struct gameport *gameport)
642 gameport_queue_event(gameport, NULL, GAMEPORT_RESCAN);
645 void gameport_reconnect(struct gameport *gameport)
647 gameport_queue_event(gameport, NULL, GAMEPORT_RECONNECT);
651 * Submits register request to kgameportd for subsequent execution.
652 * Note that port registration is always asynchronous.
654 void __gameport_register_port(struct gameport *gameport, struct module *owner)
656 gameport_init_port(gameport);
657 gameport_queue_event(gameport, owner, GAMEPORT_REGISTER_PORT);
661 * Synchronously unregisters gameport port.
663 void gameport_unregister_port(struct gameport *gameport)
665 mutex_lock(&gameport_mutex);
666 gameport_disconnect_port(gameport);
667 gameport_destroy_port(gameport);
668 mutex_unlock(&gameport_mutex);
673 * Gameport driver operations
676 static ssize_t gameport_driver_show_description(struct device_driver *drv, char *buf)
678 struct gameport_driver *driver = to_gameport_driver(drv);
679 return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
682 static struct driver_attribute gameport_driver_attrs[] = {
683 __ATTR(description, S_IRUGO, gameport_driver_show_description, NULL),
687 static int gameport_driver_probe(struct device *dev)
689 struct gameport *gameport = to_gameport_port(dev);
690 struct gameport_driver *drv = to_gameport_driver(dev->driver);
692 drv->connect(gameport, drv);
693 return gameport->drv ? 0 : -ENODEV;
696 static int gameport_driver_remove(struct device *dev)
698 struct gameport *gameport = to_gameport_port(dev);
699 struct gameport_driver *drv = to_gameport_driver(dev->driver);
701 drv->disconnect(gameport);
705 static struct bus_type gameport_bus = {
707 .probe = gameport_driver_probe,
708 .remove = gameport_driver_remove,
711 void __gameport_register_driver(struct gameport_driver *drv, struct module *owner)
713 drv->driver.bus = &gameport_bus;
714 gameport_queue_event(drv, owner, GAMEPORT_REGISTER_DRIVER);
717 void gameport_unregister_driver(struct gameport_driver *drv)
719 struct gameport *gameport;
721 mutex_lock(&gameport_mutex);
722 drv->ignore = 1; /* so gameport_find_driver ignores it */
725 list_for_each_entry(gameport, &gameport_list, node) {
726 if (gameport->drv == drv) {
727 gameport_disconnect_port(gameport);
728 gameport_find_driver(gameport);
729 /* we could've deleted some ports, restart */
734 driver_unregister(&drv->driver);
735 mutex_unlock(&gameport_mutex);
738 static int gameport_bus_match(struct device *dev, struct device_driver *drv)
740 struct gameport_driver *gameport_drv = to_gameport_driver(drv);
742 return !gameport_drv->ignore;
745 static void gameport_set_drv(struct gameport *gameport, struct gameport_driver *drv)
747 mutex_lock(&gameport->drv_mutex);
749 mutex_unlock(&gameport->drv_mutex);
752 int gameport_open(struct gameport *gameport, struct gameport_driver *drv, int mode)
755 if (gameport->open) {
756 if (gameport->open(gameport, mode)) {
760 if (mode != GAMEPORT_MODE_RAW)
764 gameport_set_drv(gameport, drv);
768 void gameport_close(struct gameport *gameport)
770 del_timer_sync(&gameport->poll_timer);
771 gameport->poll_handler = NULL;
772 gameport->poll_interval = 0;
773 gameport_set_drv(gameport, NULL);
775 gameport->close(gameport);
778 static int __init gameport_init(void)
780 gameport_task = kthread_run(gameport_thread, NULL, "kgameportd");
781 if (IS_ERR(gameport_task)) {
782 printk(KERN_ERR "gameport: Failed to start kgameportd\n");
783 return PTR_ERR(gameport_task);
786 gameport_bus.dev_attrs = gameport_device_attrs;
787 gameport_bus.drv_attrs = gameport_driver_attrs;
788 gameport_bus.match = gameport_bus_match;
789 bus_register(&gameport_bus);
794 static void __exit gameport_exit(void)
796 bus_unregister(&gameport_bus);
797 kthread_stop(gameport_task);
800 module_init(gameport_init);
801 module_exit(gameport_exit);