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 = {
57 static void gameport_add_port(struct gameport *gameport);
58 static void gameport_destroy_port(struct gameport *gameport);
59 static void gameport_reconnect_port(struct gameport *gameport);
60 static void gameport_disconnect_port(struct gameport *gameport);
64 #include <asm/i8253.h>
66 #define DELTA(x,y) ((y)-(x)+((y)<(x)?1193182/HZ:0))
67 #define GET_TIME(x) do { x = get_time_pit(); } while (0)
69 static unsigned int get_time_pit(void)
74 spin_lock_irqsave(&i8253_lock, flags);
77 count |= inb_p(0x40) << 8;
78 spin_unlock_irqrestore(&i8253_lock, flags);
88 * gameport_measure_speed() measures the gameport i/o speed.
91 static int gameport_measure_speed(struct gameport *gameport)
95 unsigned int i, t, t1, t2, t3, tx;
98 if (gameport_open(gameport, NULL, GAMEPORT_MODE_RAW))
103 for(i = 0; i < 50; i++) {
104 local_irq_save(flags);
106 for (t = 0; t < 50; t++) gameport_read(gameport);
109 local_irq_restore(flags);
111 if ((t = DELTA(t2,t1) - DELTA(t3,t2)) < tx) tx = t;
114 gameport_close(gameport);
115 return 59659 / (tx < 1 ? 1 : tx);
117 #elif defined (__x86_64__)
120 unsigned long tx, t1, t2, flags;
122 if (gameport_open(gameport, NULL, GAMEPORT_MODE_RAW))
127 for(i = 0; i < 50; i++) {
128 local_irq_save(flags);
130 for (t = 0; t < 50; t++) gameport_read(gameport);
132 local_irq_restore(flags);
134 if (t2 - t1 < tx) tx = t2 - t1;
137 gameport_close(gameport);
138 return (cpu_data[raw_smp_processor_id()].loops_per_jiffy * (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 void gameport_bind_driver(struct gameport *gameport, struct gameport_driver *drv)
194 down_write(&gameport_bus.subsys.rwsem);
196 gameport->dev.driver = &drv->driver;
197 if (drv->connect(gameport, drv)) {
198 gameport->dev.driver = NULL;
201 device_bind_driver(&gameport->dev);
203 up_write(&gameport_bus.subsys.rwsem);
206 static void gameport_release_driver(struct gameport *gameport)
208 down_write(&gameport_bus.subsys.rwsem);
209 device_release_driver(&gameport->dev);
210 up_write(&gameport_bus.subsys.rwsem);
213 static void gameport_find_driver(struct gameport *gameport)
215 down_write(&gameport_bus.subsys.rwsem);
216 device_attach(&gameport->dev);
217 up_write(&gameport_bus.subsys.rwsem);
222 * Gameport event processing.
225 enum gameport_event_type {
228 GAMEPORT_REGISTER_PORT,
229 GAMEPORT_REGISTER_DRIVER,
232 struct gameport_event {
233 enum gameport_event_type type;
235 struct module *owner;
236 struct list_head node;
239 static DEFINE_SPINLOCK(gameport_event_lock); /* protects gameport_event_list */
240 static LIST_HEAD(gameport_event_list);
241 static DECLARE_WAIT_QUEUE_HEAD(gameport_wait);
242 static struct task_struct *gameport_task;
244 static void gameport_queue_event(void *object, struct module *owner,
245 enum gameport_event_type event_type)
248 struct gameport_event *event;
250 spin_lock_irqsave(&gameport_event_lock, flags);
253 * Scan event list for the other events for the same gameport port,
254 * starting with the most recent one. If event is the same we
255 * do not need add new one. If event is of different type we
256 * need to add this event and should not look further because
257 * we need to preseve sequence of distinct events.
259 list_for_each_entry_reverse(event, &gameport_event_list, node) {
260 if (event->object == object) {
261 if (event->type == event_type)
267 if ((event = kmalloc(sizeof(struct gameport_event), GFP_ATOMIC))) {
268 if (!try_module_get(owner)) {
269 printk(KERN_WARNING "gameport: Can't get module reference, dropping event %d\n", event_type);
273 event->type = event_type;
274 event->object = object;
275 event->owner = owner;
277 list_add_tail(&event->node, &gameport_event_list);
278 wake_up(&gameport_wait);
280 printk(KERN_ERR "gameport: Not enough memory to queue event %d\n", event_type);
283 spin_unlock_irqrestore(&gameport_event_lock, flags);
286 static void gameport_free_event(struct gameport_event *event)
288 module_put(event->owner);
292 static void gameport_remove_duplicate_events(struct gameport_event *event)
294 struct list_head *node, *next;
295 struct gameport_event *e;
298 spin_lock_irqsave(&gameport_event_lock, flags);
300 list_for_each_safe(node, next, &gameport_event_list) {
301 e = list_entry(node, struct gameport_event, node);
302 if (event->object == e->object) {
304 * If this event is of different type we should not
305 * look further - we only suppress duplicate events
306 * that were sent back-to-back.
308 if (event->type != e->type)
312 gameport_free_event(e);
316 spin_unlock_irqrestore(&gameport_event_lock, flags);
320 static struct gameport_event *gameport_get_event(void)
322 struct gameport_event *event;
323 struct list_head *node;
326 spin_lock_irqsave(&gameport_event_lock, flags);
328 if (list_empty(&gameport_event_list)) {
329 spin_unlock_irqrestore(&gameport_event_lock, flags);
333 node = gameport_event_list.next;
334 event = list_entry(node, struct gameport_event, node);
337 spin_unlock_irqrestore(&gameport_event_lock, flags);
342 static void gameport_handle_event(void)
344 struct gameport_event *event;
345 struct gameport_driver *gameport_drv;
350 * Note that we handle only one event here to give swsusp
351 * a chance to freeze kgameportd thread. Gameport events
352 * should be pretty rare so we are not concerned about
353 * taking performance hit.
355 if ((event = gameport_get_event())) {
357 switch (event->type) {
358 case GAMEPORT_REGISTER_PORT:
359 gameport_add_port(event->object);
362 case GAMEPORT_RECONNECT:
363 gameport_reconnect_port(event->object);
366 case GAMEPORT_RESCAN:
367 gameport_disconnect_port(event->object);
368 gameport_find_driver(event->object);
371 case GAMEPORT_REGISTER_DRIVER:
372 gameport_drv = event->object;
373 driver_register(&gameport_drv->driver);
380 gameport_remove_duplicate_events(event);
381 gameport_free_event(event);
388 * Remove all events that have been submitted for a given gameport port.
390 static void gameport_remove_pending_events(struct gameport *gameport)
392 struct list_head *node, *next;
393 struct gameport_event *event;
396 spin_lock_irqsave(&gameport_event_lock, flags);
398 list_for_each_safe(node, next, &gameport_event_list) {
399 event = list_entry(node, struct gameport_event, node);
400 if (event->object == gameport) {
402 gameport_free_event(event);
406 spin_unlock_irqrestore(&gameport_event_lock, flags);
410 * Destroy child gameport port (if any) that has not been fully registered yet.
412 * Note that we rely on the fact that port can have only one child and therefore
413 * only one child registration request can be pending. Additionally, children
414 * are registered by driver's connect() handler so there can't be a grandchild
415 * pending registration together with a child.
417 static struct gameport *gameport_get_pending_child(struct gameport *parent)
419 struct gameport_event *event;
420 struct gameport *gameport, *child = NULL;
423 spin_lock_irqsave(&gameport_event_lock, flags);
425 list_for_each_entry(event, &gameport_event_list, node) {
426 if (event->type == GAMEPORT_REGISTER_PORT) {
427 gameport = event->object;
428 if (gameport->parent == parent) {
435 spin_unlock_irqrestore(&gameport_event_lock, flags);
439 static int gameport_thread(void *nothing)
442 gameport_handle_event();
443 wait_event_interruptible(gameport_wait,
444 kthread_should_stop() || !list_empty(&gameport_event_list));
446 } while (!kthread_should_stop());
448 printk(KERN_DEBUG "gameport: kgameportd exiting\n");
454 * Gameport port operations
457 static ssize_t gameport_show_description(struct device *dev, struct device_attribute *attr, char *buf)
459 struct gameport *gameport = to_gameport_port(dev);
460 return sprintf(buf, "%s\n", gameport->name);
463 static ssize_t gameport_rebind_driver(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
465 struct gameport *gameport = to_gameport_port(dev);
466 struct device_driver *drv;
469 retval = down_interruptible(&gameport_sem);
474 if (!strncmp(buf, "none", count)) {
475 gameport_disconnect_port(gameport);
476 } else if (!strncmp(buf, "reconnect", count)) {
477 gameport_reconnect_port(gameport);
478 } else if (!strncmp(buf, "rescan", count)) {
479 gameport_disconnect_port(gameport);
480 gameport_find_driver(gameport);
481 } else if ((drv = driver_find(buf, &gameport_bus)) != NULL) {
482 gameport_disconnect_port(gameport);
483 gameport_bind_driver(gameport, to_gameport_driver(drv));
494 static struct device_attribute gameport_device_attrs[] = {
495 __ATTR(description, S_IRUGO, gameport_show_description, NULL),
496 __ATTR(drvctl, S_IWUSR, NULL, gameport_rebind_driver),
500 static void gameport_release_port(struct device *dev)
502 struct gameport *gameport = to_gameport_port(dev);
505 module_put(THIS_MODULE);
508 void gameport_set_phys(struct gameport *gameport, const char *fmt, ...)
513 vsnprintf(gameport->phys, sizeof(gameport->phys), fmt, args);
518 * Prepare gameport port for registration.
520 static void gameport_init_port(struct gameport *gameport)
522 static atomic_t gameport_no = ATOMIC_INIT(0);
524 __module_get(THIS_MODULE);
526 init_MUTEX(&gameport->drv_sem);
527 device_initialize(&gameport->dev);
528 snprintf(gameport->dev.bus_id, sizeof(gameport->dev.bus_id),
529 "gameport%lu", (unsigned long)atomic_inc_return(&gameport_no) - 1);
530 gameport->dev.bus = &gameport_bus;
531 gameport->dev.release = gameport_release_port;
532 if (gameport->parent)
533 gameport->dev.parent = &gameport->parent->dev;
535 spin_lock_init(&gameport->timer_lock);
536 init_timer(&gameport->poll_timer);
537 gameport->poll_timer.function = gameport_run_poll_handler;
538 gameport->poll_timer.data = (unsigned long)gameport;
542 * Complete gameport port registration.
543 * Driver core will attempt to find appropriate driver for the port.
545 static void gameport_add_port(struct gameport *gameport)
547 if (gameport->parent)
548 gameport->parent->child = gameport;
550 gameport->speed = gameport_measure_speed(gameport);
552 list_add_tail(&gameport->node, &gameport_list);
555 printk(KERN_INFO "gameport: %s is %s, io %#x, speed %dkHz\n",
556 gameport->name, gameport->phys, gameport->io, gameport->speed);
558 printk(KERN_INFO "gameport: %s is %s, speed %dkHz\n",
559 gameport->name, gameport->phys, gameport->speed);
561 device_add(&gameport->dev);
562 gameport->registered = 1;
566 * gameport_destroy_port() completes deregistration process and removes
567 * port from the system
569 static void gameport_destroy_port(struct gameport *gameport)
571 struct gameport *child;
573 child = gameport_get_pending_child(gameport);
575 gameport_remove_pending_events(child);
576 put_device(&child->dev);
579 if (gameport->parent) {
580 gameport->parent->child = NULL;
581 gameport->parent = NULL;
584 if (gameport->registered) {
585 device_del(&gameport->dev);
586 list_del_init(&gameport->node);
587 gameport->registered = 0;
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 gameport_release_driver(s);
631 gameport_destroy_port(s);
632 } while ((s = parent) != gameport);
636 * Ok, no children left, now disconnect this port
638 gameport_release_driver(gameport);
641 void gameport_rescan(struct gameport *gameport)
643 gameport_queue_event(gameport, NULL, GAMEPORT_RESCAN);
646 void gameport_reconnect(struct gameport *gameport)
648 gameport_queue_event(gameport, NULL, GAMEPORT_RECONNECT);
652 * Submits register request to kgameportd for subsequent execution.
653 * Note that port registration is always asynchronous.
655 void __gameport_register_port(struct gameport *gameport, struct module *owner)
657 gameport_init_port(gameport);
658 gameport_queue_event(gameport, owner, GAMEPORT_REGISTER_PORT);
662 * Synchronously unregisters gameport port.
664 void gameport_unregister_port(struct gameport *gameport)
667 gameport_disconnect_port(gameport);
668 gameport_destroy_port(gameport);
674 * Gameport driver operations
677 static ssize_t gameport_driver_show_description(struct device_driver *drv, char *buf)
679 struct gameport_driver *driver = to_gameport_driver(drv);
680 return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
683 static struct driver_attribute gameport_driver_attrs[] = {
684 __ATTR(description, S_IRUGO, gameport_driver_show_description, NULL),
688 static int gameport_driver_probe(struct device *dev)
690 struct gameport *gameport = to_gameport_port(dev);
691 struct gameport_driver *drv = to_gameport_driver(dev->driver);
693 drv->connect(gameport, drv);
694 return gameport->drv ? 0 : -ENODEV;
697 static int gameport_driver_remove(struct device *dev)
699 struct gameport *gameport = to_gameport_port(dev);
700 struct gameport_driver *drv = to_gameport_driver(dev->driver);
702 drv->disconnect(gameport);
706 void __gameport_register_driver(struct gameport_driver *drv, struct module *owner)
708 drv->driver.bus = &gameport_bus;
709 drv->driver.probe = gameport_driver_probe;
710 drv->driver.remove = gameport_driver_remove;
711 gameport_queue_event(drv, owner, GAMEPORT_REGISTER_DRIVER);
714 void gameport_unregister_driver(struct gameport_driver *drv)
716 struct gameport *gameport;
719 drv->ignore = 1; /* so gameport_find_driver ignores it */
722 list_for_each_entry(gameport, &gameport_list, node) {
723 if (gameport->drv == drv) {
724 gameport_disconnect_port(gameport);
725 gameport_find_driver(gameport);
726 /* we could've deleted some ports, restart */
731 driver_unregister(&drv->driver);
735 static int gameport_bus_match(struct device *dev, struct device_driver *drv)
737 struct gameport_driver *gameport_drv = to_gameport_driver(drv);
739 return !gameport_drv->ignore;
742 static void gameport_set_drv(struct gameport *gameport, struct gameport_driver *drv)
744 down(&gameport->drv_sem);
746 up(&gameport->drv_sem);
749 int gameport_open(struct gameport *gameport, struct gameport_driver *drv, int mode)
752 if (gameport->open) {
753 if (gameport->open(gameport, mode)) {
757 if (mode != GAMEPORT_MODE_RAW)
761 gameport_set_drv(gameport, drv);
765 void gameport_close(struct gameport *gameport)
767 del_timer_sync(&gameport->poll_timer);
768 gameport->poll_handler = NULL;
769 gameport->poll_interval = 0;
770 gameport_set_drv(gameport, NULL);
772 gameport->close(gameport);
775 static int __init gameport_init(void)
777 gameport_task = kthread_run(gameport_thread, NULL, "kgameportd");
778 if (IS_ERR(gameport_task)) {
779 printk(KERN_ERR "gameport: Failed to start kgameportd\n");
780 return PTR_ERR(gameport_task);
783 gameport_bus.dev_attrs = gameport_device_attrs;
784 gameport_bus.drv_attrs = gameport_driver_attrs;
785 gameport_bus.match = gameport_bus_match;
786 bus_register(&gameport_bus);
791 static void __exit gameport_exit(void)
793 bus_unregister(&gameport_bus);
794 kthread_stop(gameport_task);
797 module_init(gameport_init);
798 module_exit(gameport_exit);