gameport: use this_cpu_read instead of lookup
[linux-2.6/libata-dev.git] / drivers / input / gameport / gameport.c
blob88d13f170432becfddae85a675b6101b62cee39f
1 /*
2 * Generic gameport layer
4 * Copyright (c) 1999-2002 Vojtech Pavlik
5 * Copyright (c) 2005 Dmitry Torokhov
6 */
8 /*
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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/stddef.h>
17 #include <linux/module.h>
18 #include <linux/ioport.h>
19 #include <linux/init.h>
20 #include <linux/gameport.h>
21 #include <linux/wait.h>
22 #include <linux/slab.h>
23 #include <linux/delay.h>
24 #include <linux/kthread.h>
25 #include <linux/sched.h> /* HZ */
26 #include <linux/mutex.h>
27 #include <linux/freezer.h>
29 /*#include <asm/io.h>*/
31 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
32 MODULE_DESCRIPTION("Generic gameport layer");
33 MODULE_LICENSE("GPL");
36 * gameport_mutex protects entire gameport subsystem and is taken
37 * every time gameport port or driver registrered or unregistered.
39 static DEFINE_MUTEX(gameport_mutex);
41 static LIST_HEAD(gameport_list);
43 static struct bus_type gameport_bus;
45 static void gameport_add_port(struct gameport *gameport);
46 static void gameport_attach_driver(struct gameport_driver *drv);
47 static void gameport_reconnect_port(struct gameport *gameport);
48 static void gameport_disconnect_port(struct gameport *gameport);
50 #if defined(__i386__)
52 #include <asm/i8253.h>
54 #define DELTA(x,y) ((y)-(x)+((y)<(x)?1193182/HZ:0))
55 #define GET_TIME(x) do { x = get_time_pit(); } while (0)
57 static unsigned int get_time_pit(void)
59 unsigned long flags;
60 unsigned int count;
62 raw_spin_lock_irqsave(&i8253_lock, flags);
63 outb_p(0x00, 0x43);
64 count = inb_p(0x40);
65 count |= inb_p(0x40) << 8;
66 raw_spin_unlock_irqrestore(&i8253_lock, flags);
68 return count;
71 #endif
76 * gameport_measure_speed() measures the gameport i/o speed.
79 static int gameport_measure_speed(struct gameport *gameport)
81 #if defined(__i386__)
83 unsigned int i, t, t1, t2, t3, tx;
84 unsigned long flags;
86 if (gameport_open(gameport, NULL, GAMEPORT_MODE_RAW))
87 return 0;
89 tx = 1 << 30;
91 for(i = 0; i < 50; i++) {
92 local_irq_save(flags);
93 GET_TIME(t1);
94 for (t = 0; t < 50; t++) gameport_read(gameport);
95 GET_TIME(t2);
96 GET_TIME(t3);
97 local_irq_restore(flags);
98 udelay(i * 10);
99 if ((t = DELTA(t2,t1) - DELTA(t3,t2)) < tx) tx = t;
102 gameport_close(gameport);
103 return 59659 / (tx < 1 ? 1 : tx);
105 #elif defined (__x86_64__)
107 unsigned int i, t;
108 unsigned long tx, t1, t2, flags;
110 if (gameport_open(gameport, NULL, GAMEPORT_MODE_RAW))
111 return 0;
113 tx = 1 << 30;
115 for(i = 0; i < 50; i++) {
116 local_irq_save(flags);
117 rdtscl(t1);
118 for (t = 0; t < 50; t++) gameport_read(gameport);
119 rdtscl(t2);
120 local_irq_restore(flags);
121 udelay(i * 10);
122 if (t2 - t1 < tx) tx = t2 - t1;
125 gameport_close(gameport);
126 return (this_cpu_read(cpu_info.loops_per_jiffy) *
127 (unsigned long)HZ / (1000 / 50)) / (tx < 1 ? 1 : tx);
129 #else
131 unsigned int j, t = 0;
133 if (gameport_open(gameport, NULL, GAMEPORT_MODE_RAW))
134 return 0;
136 j = jiffies; while (j == jiffies);
137 j = jiffies; while (j == jiffies) { t++; gameport_read(gameport); }
139 gameport_close(gameport);
140 return t * HZ / 1000;
142 #endif
145 void gameport_start_polling(struct gameport *gameport)
147 spin_lock(&gameport->timer_lock);
149 if (!gameport->poll_cnt++) {
150 BUG_ON(!gameport->poll_handler);
151 BUG_ON(!gameport->poll_interval);
152 mod_timer(&gameport->poll_timer, jiffies + msecs_to_jiffies(gameport->poll_interval));
155 spin_unlock(&gameport->timer_lock);
157 EXPORT_SYMBOL(gameport_start_polling);
159 void gameport_stop_polling(struct gameport *gameport)
161 spin_lock(&gameport->timer_lock);
163 if (!--gameport->poll_cnt)
164 del_timer(&gameport->poll_timer);
166 spin_unlock(&gameport->timer_lock);
168 EXPORT_SYMBOL(gameport_stop_polling);
170 static void gameport_run_poll_handler(unsigned long d)
172 struct gameport *gameport = (struct gameport *)d;
174 gameport->poll_handler(gameport);
175 if (gameport->poll_cnt)
176 mod_timer(&gameport->poll_timer, jiffies + msecs_to_jiffies(gameport->poll_interval));
180 * Basic gameport -> driver core mappings
183 static int gameport_bind_driver(struct gameport *gameport, struct gameport_driver *drv)
185 int error;
187 gameport->dev.driver = &drv->driver;
188 if (drv->connect(gameport, drv)) {
189 gameport->dev.driver = NULL;
190 return -ENODEV;
193 error = device_bind_driver(&gameport->dev);
194 if (error) {
195 dev_warn(&gameport->dev,
196 "device_bind_driver() failed for %s (%s) and %s, error: %d\n",
197 gameport->phys, gameport->name,
198 drv->description, error);
199 drv->disconnect(gameport);
200 gameport->dev.driver = NULL;
201 return error;
204 return 0;
207 static void gameport_find_driver(struct gameport *gameport)
209 int error;
211 error = device_attach(&gameport->dev);
212 if (error < 0)
213 dev_warn(&gameport->dev,
214 "device_attach() failed for %s (%s), error: %d\n",
215 gameport->phys, gameport->name, error);
220 * Gameport event processing.
223 enum gameport_event_type {
224 GAMEPORT_REGISTER_PORT,
225 GAMEPORT_ATTACH_DRIVER,
228 struct gameport_event {
229 enum gameport_event_type type;
230 void *object;
231 struct module *owner;
232 struct list_head node;
235 static DEFINE_SPINLOCK(gameport_event_lock); /* protects gameport_event_list */
236 static LIST_HEAD(gameport_event_list);
237 static DECLARE_WAIT_QUEUE_HEAD(gameport_wait);
238 static struct task_struct *gameport_task;
240 static int gameport_queue_event(void *object, struct module *owner,
241 enum gameport_event_type event_type)
243 unsigned long flags;
244 struct gameport_event *event;
245 int retval = 0;
247 spin_lock_irqsave(&gameport_event_lock, flags);
250 * Scan event list for the other events for the same gameport port,
251 * starting with the most recent one. If event is the same we
252 * do not need add new one. If event is of different type we
253 * need to add this event and should not look further because
254 * we need to preseve sequence of distinct events.
256 list_for_each_entry_reverse(event, &gameport_event_list, node) {
257 if (event->object == object) {
258 if (event->type == event_type)
259 goto out;
260 break;
264 event = kmalloc(sizeof(struct gameport_event), GFP_ATOMIC);
265 if (!event) {
266 pr_err("Not enough memory to queue event %d\n", event_type);
267 retval = -ENOMEM;
268 goto out;
271 if (!try_module_get(owner)) {
272 pr_warning("Can't get module reference, dropping event %d\n",
273 event_type);
274 kfree(event);
275 retval = -EINVAL;
276 goto out;
279 event->type = event_type;
280 event->object = object;
281 event->owner = owner;
283 list_add_tail(&event->node, &gameport_event_list);
284 wake_up(&gameport_wait);
286 out:
287 spin_unlock_irqrestore(&gameport_event_lock, flags);
288 return retval;
291 static void gameport_free_event(struct gameport_event *event)
293 module_put(event->owner);
294 kfree(event);
297 static void gameport_remove_duplicate_events(struct gameport_event *event)
299 struct gameport_event *e, *next;
300 unsigned long flags;
302 spin_lock_irqsave(&gameport_event_lock, flags);
304 list_for_each_entry_safe(e, next, &gameport_event_list, node) {
305 if (event->object == e->object) {
307 * If this event is of different type we should not
308 * look further - we only suppress duplicate events
309 * that were sent back-to-back.
311 if (event->type != e->type)
312 break;
314 list_del_init(&e->node);
315 gameport_free_event(e);
319 spin_unlock_irqrestore(&gameport_event_lock, flags);
322 static struct gameport_event *gameport_get_event(void)
324 struct gameport_event *event = NULL;
325 unsigned long flags;
327 spin_lock_irqsave(&gameport_event_lock, flags);
329 if (!list_empty(&gameport_event_list)) {
330 event = list_first_entry(&gameport_event_list,
331 struct gameport_event, node);
332 list_del_init(&event->node);
335 spin_unlock_irqrestore(&gameport_event_lock, flags);
336 return event;
339 static void gameport_handle_event(void)
341 struct gameport_event *event;
343 mutex_lock(&gameport_mutex);
346 * Note that we handle only one event here to give swsusp
347 * a chance to freeze kgameportd thread. Gameport events
348 * should be pretty rare so we are not concerned about
349 * taking performance hit.
351 if ((event = gameport_get_event())) {
353 switch (event->type) {
355 case GAMEPORT_REGISTER_PORT:
356 gameport_add_port(event->object);
357 break;
359 case GAMEPORT_ATTACH_DRIVER:
360 gameport_attach_driver(event->object);
361 break;
364 gameport_remove_duplicate_events(event);
365 gameport_free_event(event);
368 mutex_unlock(&gameport_mutex);
372 * Remove all events that have been submitted for a given object,
373 * be it a gameport port or a driver.
375 static void gameport_remove_pending_events(void *object)
377 struct gameport_event *event, *next;
378 unsigned long flags;
380 spin_lock_irqsave(&gameport_event_lock, flags);
382 list_for_each_entry_safe(event, next, &gameport_event_list, node) {
383 if (event->object == object) {
384 list_del_init(&event->node);
385 gameport_free_event(event);
389 spin_unlock_irqrestore(&gameport_event_lock, flags);
393 * Destroy child gameport port (if any) that has not been fully registered yet.
395 * Note that we rely on the fact that port can have only one child and therefore
396 * only one child registration request can be pending. Additionally, children
397 * are registered by driver's connect() handler so there can't be a grandchild
398 * pending registration together with a child.
400 static struct gameport *gameport_get_pending_child(struct gameport *parent)
402 struct gameport_event *event;
403 struct gameport *gameport, *child = NULL;
404 unsigned long flags;
406 spin_lock_irqsave(&gameport_event_lock, flags);
408 list_for_each_entry(event, &gameport_event_list, node) {
409 if (event->type == GAMEPORT_REGISTER_PORT) {
410 gameport = event->object;
411 if (gameport->parent == parent) {
412 child = gameport;
413 break;
418 spin_unlock_irqrestore(&gameport_event_lock, flags);
419 return child;
422 static int gameport_thread(void *nothing)
424 set_freezable();
425 do {
426 gameport_handle_event();
427 wait_event_freezable(gameport_wait,
428 kthread_should_stop() || !list_empty(&gameport_event_list));
429 } while (!kthread_should_stop());
431 return 0;
436 * Gameport port operations
439 static ssize_t gameport_show_description(struct device *dev, struct device_attribute *attr, char *buf)
441 struct gameport *gameport = to_gameport_port(dev);
443 return sprintf(buf, "%s\n", gameport->name);
446 static ssize_t gameport_rebind_driver(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
448 struct gameport *gameport = to_gameport_port(dev);
449 struct device_driver *drv;
450 int error;
452 error = mutex_lock_interruptible(&gameport_mutex);
453 if (error)
454 return error;
456 if (!strncmp(buf, "none", count)) {
457 gameport_disconnect_port(gameport);
458 } else if (!strncmp(buf, "reconnect", count)) {
459 gameport_reconnect_port(gameport);
460 } else if (!strncmp(buf, "rescan", count)) {
461 gameport_disconnect_port(gameport);
462 gameport_find_driver(gameport);
463 } else if ((drv = driver_find(buf, &gameport_bus)) != NULL) {
464 gameport_disconnect_port(gameport);
465 error = gameport_bind_driver(gameport, to_gameport_driver(drv));
466 put_driver(drv);
467 } else {
468 error = -EINVAL;
471 mutex_unlock(&gameport_mutex);
473 return error ? error : count;
476 static struct device_attribute gameport_device_attrs[] = {
477 __ATTR(description, S_IRUGO, gameport_show_description, NULL),
478 __ATTR(drvctl, S_IWUSR, NULL, gameport_rebind_driver),
479 __ATTR_NULL
482 static void gameport_release_port(struct device *dev)
484 struct gameport *gameport = to_gameport_port(dev);
486 kfree(gameport);
487 module_put(THIS_MODULE);
490 void gameport_set_phys(struct gameport *gameport, const char *fmt, ...)
492 va_list args;
494 va_start(args, fmt);
495 vsnprintf(gameport->phys, sizeof(gameport->phys), fmt, args);
496 va_end(args);
498 EXPORT_SYMBOL(gameport_set_phys);
501 * Prepare gameport port for registration.
503 static void gameport_init_port(struct gameport *gameport)
505 static atomic_t gameport_no = ATOMIC_INIT(0);
507 __module_get(THIS_MODULE);
509 mutex_init(&gameport->drv_mutex);
510 device_initialize(&gameport->dev);
511 dev_set_name(&gameport->dev, "gameport%lu",
512 (unsigned long)atomic_inc_return(&gameport_no) - 1);
513 gameport->dev.bus = &gameport_bus;
514 gameport->dev.release = gameport_release_port;
515 if (gameport->parent)
516 gameport->dev.parent = &gameport->parent->dev;
518 INIT_LIST_HEAD(&gameport->node);
519 spin_lock_init(&gameport->timer_lock);
520 init_timer(&gameport->poll_timer);
521 gameport->poll_timer.function = gameport_run_poll_handler;
522 gameport->poll_timer.data = (unsigned long)gameport;
526 * Complete gameport port registration.
527 * Driver core will attempt to find appropriate driver for the port.
529 static void gameport_add_port(struct gameport *gameport)
531 int error;
533 if (gameport->parent)
534 gameport->parent->child = gameport;
536 gameport->speed = gameport_measure_speed(gameport);
538 list_add_tail(&gameport->node, &gameport_list);
540 if (gameport->io)
541 dev_info(&gameport->dev, "%s is %s, io %#x, speed %dkHz\n",
542 gameport->name, gameport->phys, gameport->io, gameport->speed);
543 else
544 dev_info(&gameport->dev, "%s is %s, speed %dkHz\n",
545 gameport->name, gameport->phys, gameport->speed);
547 error = device_add(&gameport->dev);
548 if (error)
549 dev_err(&gameport->dev,
550 "device_add() failed for %s (%s), error: %d\n",
551 gameport->phys, gameport->name, error);
555 * gameport_destroy_port() completes deregistration process and removes
556 * port from the system
558 static void gameport_destroy_port(struct gameport *gameport)
560 struct gameport *child;
562 child = gameport_get_pending_child(gameport);
563 if (child) {
564 gameport_remove_pending_events(child);
565 put_device(&child->dev);
568 if (gameport->parent) {
569 gameport->parent->child = NULL;
570 gameport->parent = NULL;
573 if (device_is_registered(&gameport->dev))
574 device_del(&gameport->dev);
576 list_del_init(&gameport->node);
578 gameport_remove_pending_events(gameport);
579 put_device(&gameport->dev);
583 * Reconnect gameport port and all its children (re-initialize attached devices)
585 static void gameport_reconnect_port(struct gameport *gameport)
587 do {
588 if (!gameport->drv || !gameport->drv->reconnect || gameport->drv->reconnect(gameport)) {
589 gameport_disconnect_port(gameport);
590 gameport_find_driver(gameport);
591 /* Ok, old children are now gone, we are done */
592 break;
594 gameport = gameport->child;
595 } while (gameport);
599 * gameport_disconnect_port() unbinds a port from its driver. As a side effect
600 * all child ports are unbound and destroyed.
602 static void gameport_disconnect_port(struct gameport *gameport)
604 struct gameport *s, *parent;
606 if (gameport->child) {
608 * Children ports should be disconnected and destroyed
609 * first, staring with the leaf one, since we don't want
610 * to do recursion
612 for (s = gameport; s->child; s = s->child)
613 /* empty */;
615 do {
616 parent = s->parent;
618 device_release_driver(&s->dev);
619 gameport_destroy_port(s);
620 } while ((s = parent) != gameport);
624 * Ok, no children left, now disconnect this port
626 device_release_driver(&gameport->dev);
630 * Submits register request to kgameportd for subsequent execution.
631 * Note that port registration is always asynchronous.
633 void __gameport_register_port(struct gameport *gameport, struct module *owner)
635 gameport_init_port(gameport);
636 gameport_queue_event(gameport, owner, GAMEPORT_REGISTER_PORT);
638 EXPORT_SYMBOL(__gameport_register_port);
641 * Synchronously unregisters gameport port.
643 void gameport_unregister_port(struct gameport *gameport)
645 mutex_lock(&gameport_mutex);
646 gameport_disconnect_port(gameport);
647 gameport_destroy_port(gameport);
648 mutex_unlock(&gameport_mutex);
650 EXPORT_SYMBOL(gameport_unregister_port);
654 * Gameport driver operations
657 static ssize_t gameport_driver_show_description(struct device_driver *drv, char *buf)
659 struct gameport_driver *driver = to_gameport_driver(drv);
660 return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
663 static struct driver_attribute gameport_driver_attrs[] = {
664 __ATTR(description, S_IRUGO, gameport_driver_show_description, NULL),
665 __ATTR_NULL
668 static int gameport_driver_probe(struct device *dev)
670 struct gameport *gameport = to_gameport_port(dev);
671 struct gameport_driver *drv = to_gameport_driver(dev->driver);
673 drv->connect(gameport, drv);
674 return gameport->drv ? 0 : -ENODEV;
677 static int gameport_driver_remove(struct device *dev)
679 struct gameport *gameport = to_gameport_port(dev);
680 struct gameport_driver *drv = to_gameport_driver(dev->driver);
682 drv->disconnect(gameport);
683 return 0;
686 static void gameport_attach_driver(struct gameport_driver *drv)
688 int error;
690 error = driver_attach(&drv->driver);
691 if (error)
692 pr_err("driver_attach() failed for %s, error: %d\n",
693 drv->driver.name, error);
696 int __gameport_register_driver(struct gameport_driver *drv, struct module *owner,
697 const char *mod_name)
699 int error;
701 drv->driver.bus = &gameport_bus;
702 drv->driver.owner = owner;
703 drv->driver.mod_name = mod_name;
706 * Temporarily disable automatic binding because probing
707 * takes long time and we are better off doing it in kgameportd
709 drv->ignore = true;
711 error = driver_register(&drv->driver);
712 if (error) {
713 pr_err("driver_register() failed for %s, error: %d\n",
714 drv->driver.name, error);
715 return error;
719 * Reset ignore flag and let kgameportd bind the driver to free ports
721 drv->ignore = false;
722 error = gameport_queue_event(drv, NULL, GAMEPORT_ATTACH_DRIVER);
723 if (error) {
724 driver_unregister(&drv->driver);
725 return error;
728 return 0;
730 EXPORT_SYMBOL(__gameport_register_driver);
732 void gameport_unregister_driver(struct gameport_driver *drv)
734 struct gameport *gameport;
736 mutex_lock(&gameport_mutex);
738 drv->ignore = true; /* so gameport_find_driver ignores it */
739 gameport_remove_pending_events(drv);
741 start_over:
742 list_for_each_entry(gameport, &gameport_list, node) {
743 if (gameport->drv == drv) {
744 gameport_disconnect_port(gameport);
745 gameport_find_driver(gameport);
746 /* we could've deleted some ports, restart */
747 goto start_over;
751 driver_unregister(&drv->driver);
753 mutex_unlock(&gameport_mutex);
755 EXPORT_SYMBOL(gameport_unregister_driver);
757 static int gameport_bus_match(struct device *dev, struct device_driver *drv)
759 struct gameport_driver *gameport_drv = to_gameport_driver(drv);
761 return !gameport_drv->ignore;
764 static struct bus_type gameport_bus = {
765 .name = "gameport",
766 .dev_attrs = gameport_device_attrs,
767 .drv_attrs = gameport_driver_attrs,
768 .match = gameport_bus_match,
769 .probe = gameport_driver_probe,
770 .remove = gameport_driver_remove,
773 static void gameport_set_drv(struct gameport *gameport, struct gameport_driver *drv)
775 mutex_lock(&gameport->drv_mutex);
776 gameport->drv = drv;
777 mutex_unlock(&gameport->drv_mutex);
780 int gameport_open(struct gameport *gameport, struct gameport_driver *drv, int mode)
782 if (gameport->open) {
783 if (gameport->open(gameport, mode)) {
784 return -1;
786 } else {
787 if (mode != GAMEPORT_MODE_RAW)
788 return -1;
791 gameport_set_drv(gameport, drv);
792 return 0;
794 EXPORT_SYMBOL(gameport_open);
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);
802 if (gameport->close)
803 gameport->close(gameport);
805 EXPORT_SYMBOL(gameport_close);
807 static int __init gameport_init(void)
809 int error;
811 error = bus_register(&gameport_bus);
812 if (error) {
813 pr_err("failed to register gameport bus, error: %d\n", error);
814 return error;
817 gameport_task = kthread_run(gameport_thread, NULL, "kgameportd");
818 if (IS_ERR(gameport_task)) {
819 bus_unregister(&gameport_bus);
820 error = PTR_ERR(gameport_task);
821 pr_err("Failed to start kgameportd, error: %d\n", error);
822 return error;
825 return 0;
828 static void __exit gameport_exit(void)
830 bus_unregister(&gameport_bus);
831 kthread_stop(gameport_task);
834 subsys_initcall(gameport_init);
835 module_exit(gameport_exit);