14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/stddef.h>
17 #include <linux/module.h>
20 #include <linux/gameport.h>
21 #include <linux/slab.h>
24 #include <linux/sched.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)
63 count |=
inb_p(0x40) << 8;
89 for(i = 0; i < 50; i++) {
92 for (t = 0; t < 50; t++) gameport_read(gameport);
101 return 59659 / (tx < 1 ? 1 :
tx);
103 #elif defined (__x86_64__)
113 for(i = 0; i < 50; i++) {
116 for (t = 0; t < 50; t++) gameport_read(gameport);
120 if (t2 - t1 < tx) tx = t2 -
t1;
125 (
unsigned long)
HZ / (1000 / 50)) / (tx < 1 ? 1 :
tx);
129 unsigned int j, t = 0;
134 j =
jiffies;
while (j == jiffies);
135 j =
jiffies;
while (j == jiffies) { t++; gameport_read(gameport); }
138 return t *
HZ / 1000;
168 static void gameport_run_poll_handler(
unsigned long d)
170 struct gameport *gameport = (
struct gameport *)d;
181 static int gameport_bind_driver(
struct gameport *gameport,
struct gameport_driver *
drv)
186 if (drv->
connect(gameport, drv)) {
194 "device_bind_driver() failed for %s (%s) and %s, error: %d\n",
205 static void gameport_find_driver(
struct gameport *gameport)
212 "device_attach() failed for %s (%s), error: %d\n",
213 gameport->
phys, gameport->
name, error);
243 if (!list_empty(&gameport_event_list)) {
246 list_del_init(&
event->node);
249 spin_unlock_irqrestore(&gameport_event_lock, flags);
255 module_put(event->
owner);
276 list_del_init(&e->
node);
277 gameport_free_event(e);
281 spin_unlock_irqrestore(&gameport_event_lock, flags);
297 if ((event = gameport_get_event())) {
299 switch (event->
type) {
302 gameport_add_port(event->
object);
306 gameport_attach_driver(event->
object);
310 gameport_remove_duplicate_events(event);
311 gameport_free_event(event);
317 static DECLARE_WORK(gameport_event_work, gameport_handle_events);
319 static int gameport_queue_event(
void *
object,
struct module *
owner,
336 if (event->
object ==
object) {
337 if (event->
type == event_type)
345 pr_err(
"Not enough memory to queue event %d\n", event_type);
350 if (!try_module_get(owner)) {
351 pr_warning(
"Can't get module reference, dropping event %d\n",
360 event->owner =
owner;
363 queue_work(system_long_wq, &gameport_event_work);
366 spin_unlock_irqrestore(&gameport_event_lock, flags);
374 static void gameport_remove_pending_events(
void *
object)
382 if (event->
object ==
object) {
383 list_del_init(&event->
node);
384 gameport_free_event(event);
388 spin_unlock_irqrestore(&gameport_event_lock, flags);
399 static struct gameport *gameport_get_pending_child(
struct gameport *
parent)
402 struct gameport *gameport, *
child =
NULL;
409 gameport =
event->object;
410 if (gameport->
parent == parent) {
417 spin_unlock_irqrestore(&gameport_event_lock, flags);
442 if (!
strncmp(buf,
"none", count)) {
443 gameport_disconnect_port(gameport);
444 }
else if (!
strncmp(buf,
"reconnect", count)) {
445 gameport_reconnect_port(gameport);
446 }
else if (!
strncmp(buf,
"rescan", count)) {
447 gameport_disconnect_port(gameport);
448 gameport_find_driver(gameport);
450 gameport_disconnect_port(gameport);
458 return error ? error :
count;
467 static void gameport_release_port(
struct device *dev)
488 static void gameport_init_port(
struct gameport *gameport)
498 gameport->
dev.bus = &gameport_bus;
499 gameport->
dev.release = gameport_release_port;
501 gameport->
dev.parent = &gameport->
parent->dev;
503 INIT_LIST_HEAD(&gameport->
node);
506 gameport->
poll_timer.function = gameport_run_poll_handler;
514 static void gameport_add_port(
struct gameport *gameport)
519 gameport->
parent->child = gameport;
521 gameport->
speed = gameport_measure_speed(gameport);
526 dev_info(&gameport->
dev,
"%s is %s, io %#x, speed %dkHz\n",
529 dev_info(&gameport->
dev,
"%s is %s, speed %dkHz\n",
535 "device_add() failed for %s (%s), error: %d\n",
536 gameport->
phys, gameport->
name, error);
543 static void gameport_destroy_port(
struct gameport *gameport)
545 struct gameport *
child;
547 child = gameport_get_pending_child(gameport);
549 gameport_remove_pending_events(child);
558 if (device_is_registered(&gameport->
dev))
561 list_del_init(&gameport->
node);
563 gameport_remove_pending_events(gameport);
570 static void gameport_reconnect_port(
struct gameport *gameport)
573 if (!gameport->
drv || !gameport->
drv->reconnect || gameport->
drv->reconnect(gameport)) {
574 gameport_disconnect_port(gameport);
575 gameport_find_driver(gameport);
579 gameport = gameport->
child;
587 static void gameport_disconnect_port(
struct gameport *gameport)
591 if (gameport->
child) {
604 gameport_destroy_port(s);
605 }
while ((s = parent) != gameport);
620 gameport_init_port(gameport);
631 gameport_disconnect_port(gameport);
632 gameport_destroy_port(gameport);
653 static int gameport_driver_probe(
struct device *dev)
662 static int gameport_driver_remove(
struct device *dev)
677 pr_err(
"driver_attach() failed for %s, error: %d\n",
682 const char *mod_name)
686 drv->
driver.bus = &gameport_bus;
688 drv->
driver.mod_name = mod_name;
698 pr_err(
"driver_register() failed for %s, error: %d\n",
719 struct gameport *gameport;
724 gameport_remove_pending_events(drv);
728 if (gameport->
drv == drv) {
729 gameport_disconnect_port(gameport);
730 gameport_find_driver(gameport);
746 return !gameport_drv->
ignore;
749 static struct bus_type gameport_bus = {
751 .dev_attrs = gameport_device_attrs,
752 .drv_attrs = gameport_driver_attrs,
753 .match = gameport_bus_match,
754 .probe = gameport_driver_probe,
755 .remove = gameport_driver_remove,
758 static void gameport_set_drv(
struct gameport *gameport,
struct gameport_driver *drv)
767 if (gameport->
open) {
768 if (gameport->
open(gameport, mode)) {
776 gameport_set_drv(gameport, drv);
786 gameport_set_drv(gameport,
NULL);
788 gameport->
close(gameport);
792 static int __init gameport_init(
void)
798 pr_err(
"failed to register gameport bus, error: %d\n", error);
806 static void __exit gameport_exit(
void)