Release 4.11 drivers/input/gameport/gameport.c
/*
* Generic gameport layer
*
* Copyright (c) 1999-2002 Vojtech Pavlik
* Copyright (c) 2005 Dmitry Torokhov
*/
/*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published by
* the Free Software Foundation.
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/stddef.h>
#include <linux/module.h>
#include <linux/ioport.h>
#include <linux/init.h>
#include <linux/gameport.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/workqueue.h>
#include <linux/sched.h> /* HZ */
#include <linux/mutex.h>
#include <linux/timekeeping.h>
/*#include <asm/io.h>*/
MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
MODULE_DESCRIPTION("Generic gameport layer");
MODULE_LICENSE("GPL");
static bool use_ktime = true;
module_param(use_ktime, bool, 0400);
MODULE_PARM_DESC(use_ktime, "Use ktime for measuring I/O speed");
/*
* gameport_mutex protects entire gameport subsystem and is taken
* every time gameport port or driver registrered or unregistered.
*/
static DEFINE_MUTEX(gameport_mutex);
static LIST_HEAD(gameport_list);
static struct bus_type gameport_bus;
static void gameport_add_port(struct gameport *gameport);
static void gameport_attach_driver(struct gameport_driver *drv);
static void gameport_reconnect_port(struct gameport *gameport);
static void gameport_disconnect_port(struct gameport *gameport);
#if defined(__i386__)
#include <linux/i8253.h>
#define DELTA(x,y) ((y)-(x)+((y)<(x)?1193182/HZ:0))
#define GET_TIME(x) do { x = get_time_pit(); } while (0)
static unsigned int get_time_pit(void)
{
unsigned long flags;
unsigned int count;
raw_spin_lock_irqsave(&i8253_lock, flags);
outb_p(0x00, 0x43);
count = inb_p(0x40);
count |= inb_p(0x40) << 8;
raw_spin_unlock_irqrestore(&i8253_lock, flags);
return count;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Vojtech Pavlik | 57 | 96.61% | 1 | 50.00% |
Thomas Gleixner | 2 | 3.39% | 1 | 50.00% |
Total | 59 | 100.00% | 2 | 100.00% |
#endif
/*
* gameport_measure_speed() measures the gameport i/o speed.
*/
static int gameport_measure_speed(struct gameport *gameport)
{
unsigned int i, t, tx;
u64 t1, t2, t3;
unsigned long flags;
if (gameport_open(gameport, NULL, GAMEPORT_MODE_RAW))
return 0;
tx = ~0;
for (i = 0; i < 50; i++) {
local_irq_save(flags);
t1 = ktime_get_ns();
for (t = 0; t < 50; t++)
gameport_read(gameport);
t2 = ktime_get_ns();
t3 = ktime_get_ns();
local_irq_restore(flags);
udelay(i * 10);
t = (t2 - t1) - (t3 - t2);
if (t < tx)
tx = t;
}
gameport_close(gameport);
t = 1000000 * 50;
if (tx)
t /= tx;
return t;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Takashi Iwai | 150 | 93.75% | 1 | 50.00% |
Linus Torvalds (pre-git) | 10 | 6.25% | 1 | 50.00% |
Total | 160 | 100.00% | 2 | 100.00% |
static int old_gameport_measure_speed(struct gameport *gameport)
{
#if defined(__i386__)
unsigned int i, t, t1, t2, t3, tx;
unsigned long flags;
if (gameport_open(gameport, NULL, GAMEPORT_MODE_RAW))
return 0;
tx = 1 << 30;
for(i = 0; i < 50; i++) {
local_irq_save(flags);
GET_TIME(t1);
for (t = 0; t < 50; t++) gameport_read(gameport);
GET_TIME(t2);
GET_TIME(t3);
local_irq_restore(flags);
udelay(i * 10);
if ((t = DELTA(t2,t1) - DELTA(t3,t2)) < tx) tx = t;
}
gameport_close(gameport);
return 59659 / (tx < 1 ? 1 : tx);
#elif defined (__x86_64__)
unsigned int i, t;
unsigned long tx, t1, t2, flags;
if (gameport_open(gameport, NULL, GAMEPORT_MODE_RAW))
return 0;
tx = 1 << 30;
for(i = 0; i < 50; i++) {
local_irq_save(flags);
t1 = rdtsc();
for (t = 0; t < 50; t++) gameport_read(gameport);
t2 = rdtsc();
local_irq_restore(flags);
udelay(i * 10);
if (t2 - t1 < tx) tx = t2 - t1;
}
gameport_close(gameport);
return (this_cpu_read(cpu_info.loops_per_jiffy) *
(unsigned long)HZ / (1000 / 50)) / (tx < 1 ? 1 : tx);
#else
unsigned int j, t = 0;
if (gameport_open(gameport, NULL, GAMEPORT_MODE_RAW))
return 0;
j = jiffies; while (j == jiffies);
j = jiffies; while (j == jiffies) { t++; gameport_read(gameport); }
gameport_close(gameport);
return t * HZ / 1000;
#endif
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Linus Torvalds (pre-git) | 192 | 49.74% | 1 | 11.11% |
Vojtech Pavlik | 164 | 42.49% | 2 | 22.22% |
Gabriel Devenyi | 10 | 2.59% | 1 | 11.11% |
Takashi Iwai | 10 | 2.59% | 1 | 11.11% |
Andrew Lutomirski | 6 | 1.55% | 2 | 22.22% |
Christoph Lameter | 3 | 0.78% | 1 | 11.11% |
Mike Travis | 1 | 0.26% | 1 | 11.11% |
Total | 386 | 100.00% | 9 | 100.00% |
void gameport_start_polling(struct gameport *gameport)
{
spin_lock(&gameport->timer_lock);
if (!gameport->poll_cnt++) {
BUG_ON(!gameport->poll_handler);
BUG_ON(!gameport->poll_interval);
mod_timer(&gameport->poll_timer, jiffies + msecs_to_jiffies(gameport->poll_interval));
}
spin_unlock(&gameport->timer_lock);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Dmitry Torokhov | 69 | 100.00% | 1 | 100.00% |
Total | 69 | 100.00% | 1 | 100.00% |
EXPORT_SYMBOL(gameport_start_polling);
void gameport_stop_polling(struct gameport *gameport)
{
spin_lock(&gameport->timer_lock);
if (!--gameport->poll_cnt)
del_timer(&gameport->poll_timer);
spin_unlock(&gameport->timer_lock);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Dmitry Torokhov | 42 | 100.00% | 1 | 100.00% |
Total | 42 | 100.00% | 1 | 100.00% |
EXPORT_SYMBOL(gameport_stop_polling);
static void gameport_run_poll_handler(unsigned long d)
{
struct gameport *gameport = (struct gameport *)d;
gameport->poll_handler(gameport);
if (gameport->poll_cnt)
mod_timer(&gameport->poll_timer, jiffies + msecs_to_jiffies(gameport->poll_interval));
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Dmitry Torokhov | 52 | 100.00% | 1 | 100.00% |
Total | 52 | 100.00% | 1 | 100.00% |
/*
* Basic gameport -> driver core mappings
*/
static int gameport_bind_driver(struct gameport *gameport, struct gameport_driver *drv)
{
int error;
gameport->dev.driver = &drv->driver;
if (drv->connect(gameport, drv)) {
gameport->dev.driver = NULL;
return -ENODEV;
}
error = device_bind_driver(&gameport->dev);
if (error) {
dev_warn(&gameport->dev,
"device_bind_driver() failed for %s (%s) and %s, error: %d\n",
gameport->phys, gameport->name,
drv->description, error);
drv->disconnect(gameport);
gameport->dev.driver = NULL;
return error;
}
return 0;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Dmitry Torokhov | 93 | 80.17% | 6 | 85.71% |
Linus Torvalds (pre-git) | 23 | 19.83% | 1 | 14.29% |
Total | 116 | 100.00% | 7 | 100.00% |
static void gameport_find_driver(struct gameport *gameport)
{
int error;
error = device_attach(&gameport->dev);
if (error < 0)
dev_warn(&gameport->dev,
"device_attach() failed for %s (%s), error: %d\n",
gameport->phys, gameport->name, error);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Randy Dunlap | 24 | 48.00% | 1 | 20.00% |
Dmitry Torokhov | 17 | 34.00% | 3 | 60.00% |
Linus Torvalds (pre-git) | 9 | 18.00% | 1 | 20.00% |
Total | 50 | 100.00% | 5 | 100.00% |
/*
* Gameport event processing.
*/
enum gameport_event_type {
GAMEPORT_REGISTER_PORT,
GAMEPORT_ATTACH_DRIVER,
};
struct gameport_event {
enum gameport_event_type type;
void *object;
struct module *owner;
struct list_head node;
};
static DEFINE_SPINLOCK(gameport_event_lock); /* protects gameport_event_list */
static LIST_HEAD(gameport_event_list);
static struct gameport_event *gameport_get_event(void)
{
struct gameport_event *event = NULL;
unsigned long flags;
spin_lock_irqsave(&gameport_event_lock, flags);
if (!list_empty(&gameport_event_list)) {
event = list_first_entry(&gameport_event_list,
struct gameport_event, node);
list_del_init(&event->node);
}
spin_unlock_irqrestore(&gameport_event_lock, flags);
return event;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Dmitry Torokhov | 68 | 94.44% | 3 | 60.00% |
Linus Torvalds | 2 | 2.78% | 1 | 20.00% |
Adrian Bunk | 2 | 2.78% | 1 | 20.00% |
Total | 72 | 100.00% | 5 | 100.00% |
static void gameport_free_event(struct gameport_event *event)
{
module_put(event->owner);
kfree(event);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Dmitry Torokhov | 23 | 100.00% | 1 | 100.00% |
Total | 23 | 100.00% | 1 | 100.00% |
static void gameport_remove_duplicate_events(struct gameport_event *event)
{
struct gameport_event *e, *next;
unsigned long flags;
spin_lock_irqsave(&gameport_event_lock, flags);
list_for_each_entry_safe(e, next, &gameport_event_list, node) {
if (event->object == e->object) {
/*
* If this event is of different type we should not
* look further - we only suppress duplicate events
* that were sent back-to-back.
*/
if (event->type != e->type)
break;
list_del_init(&e->node);
gameport_free_event(e);
}
}
spin_unlock_irqrestore(&gameport_event_lock, flags);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Dmitry Torokhov | 88 | 100.00% | 2 | 100.00% |
Total | 88 | 100.00% | 2 | 100.00% |
static void gameport_handle_events(struct work_struct *work)
{
struct gameport_event *event;
mutex_lock(&gameport_mutex);
/*
* Note that we handle only one event here to give swsusp
* a chance to freeze kgameportd thread. Gameport events
* should be pretty rare so we are not concerned about
* taking performance hit.
*/
if ((event = gameport_get_event())) {
switch (event->type) {
case GAMEPORT_REGISTER_PORT:
gameport_add_port(event->object);
break;
case GAMEPORT_ATTACH_DRIVER:
gameport_attach_driver(event->object);
break;
}
gameport_remove_duplicate_events(event);
gameport_free_event(event);
}
mutex_unlock(&gameport_mutex);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Dmitry Torokhov | 80 | 100.00% | 2 | 100.00% |
Total | 80 | 100.00% | 2 | 100.00% |
static DECLARE_WORK(gameport_event_work, gameport_handle_events);
static int gameport_queue_event(void *object, struct module *owner,
enum gameport_event_type event_type)
{
unsigned long flags;
struct gameport_event *event;
int retval = 0;
spin_lock_irqsave(&gameport_event_lock, flags);
/*
* Scan event list for the other events for the same gameport port,
* starting with the most recent one. If event is the same we
* do not need add new one. If event is of different type we
* need to add this event and should not look further because
* we need to preserve sequence of distinct events.
*/
list_for_each_entry_reverse(event, &gameport_event_list, node) {
if (event->object == object) {
if (event->type == event_type)
goto out;
break;
}
}
event = kmalloc(sizeof(struct gameport_event), GFP_ATOMIC);
if (!event) {
pr_err("Not enough memory to queue event %d\n", event_type);
retval = -ENOMEM;
goto out;
}
if (!try_module_get(owner)) {
pr_warning("Can't get module reference, dropping event %d\n",
event_type);
kfree(event);
retval = -EINVAL;
goto out;
}
event->type = event_type;
event->object = object;
event->owner = owner;
list_add_tail(&event->node, &gameport_event_list);
queue_work(system_long_wq, &gameport_event_work);
out:
spin_unlock_irqrestore(&gameport_event_lock, flags);
return retval;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Dmitry Torokhov | 189 | 100.00% | 4 | 100.00% |
Total | 189 | 100.00% | 4 | 100.00% |
/*
* Remove all events that have been submitted for a given object,
* be it a gameport port or a driver.
*/
static void gameport_remove_pending_events(void *object)
{
struct gameport_event *event, *next;
unsigned long flags;
spin_lock_irqsave(&gameport_event_lock, flags);
list_for_each_entry_safe(event, next, &gameport_event_list, node) {
if (event->object == object) {
list_del_init(&event->node);
gameport_free_event(event);
}
}
spin_unlock_irqrestore(&gameport_event_lock, flags);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Dmitry Torokhov | 73 | 100.00% | 3 | 100.00% |
Total | 73 | 100.00% | 3 | 100.00% |
/*
* Destroy child gameport port (if any) that has not been fully registered yet.
*
* Note that we rely on the fact that port can have only one child and therefore
* only one child registration request can be pending. Additionally, children
* are registered by driver's connect() handler so there can't be a grandchild
* pending registration together with a child.
*/
static struct gameport *gameport_get_pending_child(struct gameport *parent)
{
struct gameport_event *event;
struct gameport *gameport, *child = NULL;
unsigned long flags;
spin_lock_irqsave(&gameport_event_lock, flags);
list_for_each_entry(event, &gameport_event_list, node) {
if (event->type == GAMEPORT_REGISTER_PORT) {
gameport = event->object;
if (gameport->parent == parent) {
child = gameport;
break;
}
}
}
spin_unlock_irqrestore(&gameport_event_lock, flags);
return child;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Dmitry Torokhov | 92 | 100.00% | 1 | 100.00% |
Total | 92 | 100.00% | 1 | 100.00% |
/*
* Gameport port operations
*/
static ssize_t gameport_description_show(struct device *dev, struct device_attribute *attr, char *buf)
{
struct gameport *gameport = to_gameport_port(dev);
return sprintf(buf, "%s\n", gameport->name);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Dmitry Torokhov | 36 | 85.71% | 1 | 33.33% |
Yani Ioannou | 5 | 11.90% | 1 | 33.33% |
Greg Kroah-Hartman | 1 | 2.38% | 1 | 33.33% |
Total | 42 | 100.00% | 3 | 100.00% |
static DEVICE_ATTR(description, S_IRUGO, gameport_description_show, NULL);
static ssize_t drvctl_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
{
struct gameport *gameport = to_gameport_port(dev);
struct device_driver *drv;
int error;
error = mutex_lock_interruptible(&gameport_mutex);
if (error)
return error;
if (!strncmp(buf, "none", count)) {
gameport_disconnect_port(gameport);
} else if (!strncmp(buf, "reconnect", count)) {
gameport_reconnect_port(gameport);
} else if (!strncmp(buf, "rescan", count)) {
gameport_disconnect_port(gameport);
gameport_find_driver(gameport);
} else if ((drv = driver_find(buf, &gameport_bus)) != NULL) {
gameport_disconnect_port(gameport);
error = gameport_bind_driver(gameport, to_gameport_driver(drv));
} else {
error = -EINVAL;
}
mutex_unlock(&gameport_mutex);
return error ? error : count;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Dmitry Torokhov | 168 | 94.38% | 2 | 40.00% |
Yani Ioannou | 5 | 2.81% | 1 | 20.00% |
Arjan van de Ven | 4 | 2.25% | 1 | 20.00% |
Greg Kroah-Hartman | 1 | 0.56% | 1 | 20.00% |
Total | 178 | 100.00% | 5 | 100.00% |
static DEVICE_ATTR_WO(drvctl);
static struct attribute *gameport_device_attrs[] = {
&dev_attr_description.attr,
&dev_attr_drvctl.attr,
NULL,
};
ATTRIBUTE_GROUPS(gameport_device);
static void gameport_release_port(struct device *dev)
{
struct gameport *gameport = to_gameport_port(dev);
kfree(gameport);
module_put(THIS_MODULE);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Dmitry Torokhov | 31 | 100.00% | 1 | 100.00% |
Total | 31 | 100.00% | 1 | 100.00% |
void gameport_set_phys(struct gameport *gameport, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
vsnprintf(gameport->phys, sizeof(gameport->phys), fmt, args);
va_end(args);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Dmitry Torokhov | 50 | 100.00% | 1 | 100.00% |
Total | 50 | 100.00% | 1 | 100.00% |
EXPORT_SYMBOL(gameport_set_phys);
/*
* Prepare gameport port for registration.
*/
static void gameport_init_port(struct gameport *gameport)
{
static atomic_t gameport_no = ATOMIC_INIT(-1);
__module_get(THIS_MODULE);
mutex_init(&gameport->drv_mutex);
device_initialize(&gameport->dev);
dev_set_name(&gameport->dev, "gameport%lu",
(unsigned long)atomic_inc_return(&gameport_no));
gameport->dev.bus = &gameport_bus;
gameport->dev.release = gameport_release_port;
if (gameport->parent)
gameport->dev.parent = &gameport->parent->dev;
INIT_LIST_HEAD(&gameport->node);
spin_lock_init(&gameport->timer_lock);
init_timer(&gameport->poll_timer);
gameport->poll_timer.function = gameport_run_poll_handler;
gameport->poll_timer.data = (unsigned long)gameport;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Dmitry Torokhov | 128 | 90.14% | 3 | 42.86% |
Randy Dunlap | 8 | 5.63% | 1 | 14.29% |
Aniroop Mathur | 2 | 1.41% | 1 | 14.29% |
Kay Sievers | 2 | 1.41% | 1 | 14.29% |
Arjan van de Ven | 2 | 1.41% | 1 | 14.29% |
Total | 142 | 100.00% | 7 | 100.00% |
/*
* Complete gameport port registration.
* Driver core will attempt to find appropriate driver for the port.
*/
static void gameport_add_port(struct gameport *gameport)
{
int error;
if (gameport->parent)
gameport->parent->child = gameport;
gameport->speed = use_ktime ?
gameport_measure_speed(gameport) :
old_gameport_measure_speed(gameport);
list_add_tail(&gameport->node, &gameport_list);
if (gameport->io)
dev_info(&gameport->dev, "%s is %s, io %#x, speed %dkHz\n",
gameport->name, gameport->phys, gameport->io, gameport->speed);
else
dev_info(&gameport->dev, "%s is %s, speed %dkHz\n",
gameport->name, gameport->phys, gameport->speed);
error = device_add(&gameport->dev);
if (error)
dev_err(&gameport->dev,
"device_add() failed for %s (%s), error: %d\n",
gameport->phys, gameport->name, error);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Dmitry Torokhov | 115 | 79.86% | 2 | 50.00% |
Randy Dunlap | 22 | 15.28% | 1 | 25.00% |
Takashi Iwai | 7 | 4.86% | 1 | 25.00% |
Total | 144 | 100.00% | 4 | 100.00% |
/*
* gameport_destroy_port() completes deregistration process and removes
* port from the system
*/
static void gameport_destroy_port(struct gameport *gameport)
{
struct gameport *child;
child = gameport_get_pending_child(gameport);
if (child) {
gameport_remove_pending_events(child);
put_device(&child->dev);
}
if (gameport->parent) {
gameport->parent->child = NULL;
gameport->parent = NULL;
}
if (device_is_registered(&gameport->dev))
device_del(&gameport->dev);
list_del_init(&gameport->node);
gameport_remove_pending_events(gameport);
put_device(&gameport->dev);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Dmitry Torokhov | 103 | 100.00% | 2 | 100.00% |
Total | 103 | 100.00% | 2 | 100.00% |
/*
* Reconnect gameport port and all its children (re-initialize attached devices)
*/
static void gameport_reconnect_port(struct gameport *gameport)
{
do {
if (!gameport->drv || !gameport->drv->reconnect || gameport->drv->reconnect(gameport)) {
gameport_disconnect_port(gameport);
gameport_find_driver(gameport);
/* Ok, old children are now gone, we are done */
break;
}
gameport = gameport->child;
} while (gameport);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Dmitry Torokhov | 62 | 100.00% | 1 | 100.00% |
Total | 62 | 100.00% | 1 | 100.00% |
/*
* gameport_disconnect_port() unbinds a port from its driver. As a side effect
* all child ports are unbound and destroyed.
*/
static void gameport_disconnect_port(struct gameport *gameport)
{
struct gameport *s, *parent;
if