Release 4.11 drivers/input/serio/serio.c
/*
* The Serio abstraction module
*
* Copyright (c) 1999-2004 Vojtech Pavlik
* Copyright (c) 2004 Dmitry Torokhov
* Copyright (c) 2003 Daniele Bellucci
*/
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Should you need to contact me, the author, you can do so either by
* e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
* Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/stddef.h>
#include <linux/module.h>
#include <linux/serio.h>
#include <linux/errno.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/workqueue.h>
#include <linux/mutex.h>
MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
MODULE_DESCRIPTION("Serio abstraction core");
MODULE_LICENSE("GPL");
/*
* serio_mutex protects entire serio subsystem and is taken every time
* serio port or driver registered or unregistered.
*/
static DEFINE_MUTEX(serio_mutex);
static LIST_HEAD(serio_list);
static void serio_add_port(struct serio *serio);
static int serio_reconnect_port(struct serio *serio);
static void serio_disconnect_port(struct serio *serio);
static void serio_reconnect_subtree(struct serio *serio);
static void serio_attach_driver(struct serio_driver *drv);
static int serio_connect_driver(struct serio *serio, struct serio_driver *drv)
{
int retval;
mutex_lock(&serio->drv_mutex);
retval = drv->connect(serio, drv);
mutex_unlock(&serio->drv_mutex);
return retval;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Linus Torvalds | 45 | 91.84% | 1 | 50.00% |
Arjan van de Ven | 4 | 8.16% | 1 | 50.00% |
Total | 49 | 100.00% | 2 | 100.00% |
static int serio_reconnect_driver(struct serio *serio)
{
int retval = -1;
mutex_lock(&serio->drv_mutex);
if (serio->drv && serio->drv->reconnect)
retval = serio->drv->reconnect(serio);
mutex_unlock(&serio->drv_mutex);
return retval;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Linus Torvalds | 55 | 93.22% | 1 | 50.00% |
Arjan van de Ven | 4 | 6.78% | 1 | 50.00% |
Total | 59 | 100.00% | 2 | 100.00% |
static void serio_disconnect_driver(struct serio *serio)
{
mutex_lock(&serio->drv_mutex);
if (serio->drv)
serio->drv->disconnect(serio);
mutex_unlock(&serio->drv_mutex);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Linus Torvalds | 38 | 90.48% | 1 | 50.00% |
Arjan van de Ven | 4 | 9.52% | 1 | 50.00% |
Total | 42 | 100.00% | 2 | 100.00% |
static int serio_match_port(const struct serio_device_id *ids, struct serio *serio)
{
while (ids->type || ids->proto) {
if ((ids->type == SERIO_ANY || ids->type == serio->id.type) &&
(ids->proto == SERIO_ANY || ids->proto == serio->id.proto) &&
(ids->extra == SERIO_ANY || ids->extra == serio->id.extra) &&
(ids->id == SERIO_ANY || ids->id == serio->id.id))
return 1;
ids++;
}
return 0;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Dmitry Torokhov | 112 | 100.00% | 1 | 100.00% |
Total | 112 | 100.00% | 1 | 100.00% |
/*
* Basic serio -> driver core mappings
*/
static int serio_bind_driver(struct serio *serio, struct serio_driver *drv)
{
int error;
if (serio_match_port(drv->id_table, serio)) {
serio->dev.driver = &drv->driver;
if (serio_connect_driver(serio, drv)) {
serio->dev.driver = NULL;
return -ENODEV;
}
error = device_bind_driver(&serio->dev);
if (error) {
dev_warn(&serio->dev,
"device_bind_driver() failed for %s (%s) and %s, error: %d\n",
serio->phys, serio->name,
drv->description, error);
serio_disconnect_driver(serio);
serio->dev.driver = NULL;
return error;
}
}
return 0;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Dmitry Torokhov | 124 | 99.20% | 8 | 88.89% |
Linus Torvalds | 1 | 0.80% | 1 | 11.11% |
Total | 125 | 100.00% | 9 | 100.00% |
static void serio_find_driver(struct serio *serio)
{
int error;
error = device_attach(&serio->dev);
if (error < 0 && error != -EPROBE_DEFER)
dev_warn(&serio->dev,
"device_attach() failed for %s (%s), error: %d\n",
serio->phys, serio->name, error);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Randy Dunlap | 24 | 43.64% | 1 | 16.67% |
Linus Torvalds (pre-git) | 13 | 23.64% | 1 | 16.67% |
Dmitry Torokhov | 13 | 23.64% | 3 | 50.00% |
Grygorii Strashko | 5 | 9.09% | 1 | 16.67% |
Total | 55 | 100.00% | 6 | 100.00% |
/*
* Serio event processing.
*/
enum serio_event_type {
SERIO_RESCAN_PORT,
SERIO_RECONNECT_PORT,
SERIO_RECONNECT_SUBTREE,
SERIO_REGISTER_PORT,
SERIO_ATTACH_DRIVER,
};
struct serio_event {
enum serio_event_type type;
void *object;
struct module *owner;
struct list_head node;
};
static DEFINE_SPINLOCK(serio_event_lock); /* protects serio_event_list */
static LIST_HEAD(serio_event_list);
static struct serio_event *serio_get_event(void)
{
struct serio_event *event = NULL;
unsigned long flags;
spin_lock_irqsave(&serio_event_lock, flags);
if (!list_empty(&serio_event_list)) {
event = list_first_entry(&serio_event_list,
struct serio_event, node);
list_del_init(&event->node);
}
spin_unlock_irqrestore(&serio_event_lock, flags);
return event;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Dmitry Torokhov | 47 | 65.28% | 5 | 45.45% |
Vojtech Pavlik | 21 | 29.17% | 4 | 36.36% |
Linus Torvalds | 2 | 2.78% | 1 | 9.09% |
Adrian Bunk | 2 | 2.78% | 1 | 9.09% |
Total | 72 | 100.00% | 11 | 100.00% |
static void serio_free_event(struct serio_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 serio_remove_duplicate_events(void *object,
enum serio_event_type type)
{
struct serio_event *e, *next;
unsigned long flags;
spin_lock_irqsave(&serio_event_lock, flags);
list_for_each_entry_safe(e, next, &serio_event_list, node) {
if (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 (type != e->type)
break;
list_del_init(&e->node);
serio_free_event(e);
}
}
spin_unlock_irqrestore(&serio_event_lock, flags);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Dmitry Torokhov | 73 | 83.91% | 5 | 62.50% |
Vojtech Pavlik | 8 | 9.20% | 2 | 25.00% |
Duncan Laurie | 6 | 6.90% | 1 | 12.50% |
Total | 87 | 100.00% | 8 | 100.00% |
static void serio_handle_event(struct work_struct *work)
{
struct serio_event *event;
mutex_lock(&serio_mutex);
while ((event = serio_get_event())) {
switch (event->type) {
case SERIO_REGISTER_PORT:
serio_add_port(event->object);
break;
case SERIO_RECONNECT_PORT:
serio_reconnect_port(event->object);
break;
case SERIO_RESCAN_PORT:
serio_disconnect_port(event->object);
serio_find_driver(event->object);
break;
case SERIO_RECONNECT_SUBTREE:
serio_reconnect_subtree(event->object);
break;
case SERIO_ATTACH_DRIVER:
serio_attach_driver(event->object);
break;
}
serio_remove_duplicate_events(event->object, event->type);
serio_free_event(event);
}
mutex_unlock(&serio_mutex);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Dmitry Torokhov | 90 | 72.00% | 2 | 25.00% |
Vojtech Pavlik | 27 | 21.60% | 4 | 50.00% |
Duncan Laurie | 6 | 4.80% | 1 | 12.50% |
Linus Torvalds (pre-git) | 2 | 1.60% | 1 | 12.50% |
Total | 125 | 100.00% | 8 | 100.00% |
static DECLARE_WORK(serio_event_work, serio_handle_event);
static int serio_queue_event(void *object, struct module *owner,
enum serio_event_type event_type)
{
unsigned long flags;
struct serio_event *event;
int retval = 0;
spin_lock_irqsave(&serio_event_lock, flags);
/*
* Scan event list for the other events for the same serio 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 preseve sequence of distinct events.
*/
list_for_each_entry_reverse(event, &serio_event_list, node) {
if (event->object == object) {
if (event->type == event_type)
goto out;
break;
}
}
event = kmalloc(sizeof(struct serio_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, &serio_event_list);
queue_work(system_long_wq, &serio_event_work);
out:
spin_unlock_irqrestore(&serio_event_lock, flags);
return retval;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Dmitry Torokhov | 160 | 84.66% | 9 | 60.00% |
Vojtech Pavlik | 21 | 11.11% | 4 | 26.67% |
Linus Torvalds (pre-git) | 4 | 2.12% | 1 | 6.67% |
David Shaohua Li | 4 | 2.12% | 1 | 6.67% |
Total | 189 | 100.00% | 15 | 100.00% |
/*
* Remove all events that have been submitted for a given
* object, be it serio port or driver.
*/
static void serio_remove_pending_events(void *object)
{
struct serio_event *event, *next;
unsigned long flags;
spin_lock_irqsave(&serio_event_lock, flags);
list_for_each_entry_safe(event, next, &serio_event_list, node) {
if (event->object == object) {
list_del_init(&event->node);
serio_free_event(event);
}
}
spin_unlock_irqrestore(&serio_event_lock, flags);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Vojtech Pavlik | 45 | 61.64% | 2 | 33.33% |
Dmitry Torokhov | 28 | 38.36% | 4 | 66.67% |
Total | 73 | 100.00% | 6 | 100.00% |
/*
* Locate child serio port (if any) that has not been fully registered yet.
*
* Children are registered by driver's connect() handler so there can't be a
* grandchild pending registration together with a child.
*/
static struct serio *serio_get_pending_child(struct serio *parent)
{
struct serio_event *event;
struct serio *serio, *child = NULL;
unsigned long flags;
spin_lock_irqsave(&serio_event_lock, flags);
list_for_each_entry(event, &serio_event_list, node) {
if (event->type == SERIO_REGISTER_PORT) {
serio = event->object;
if (serio->parent == parent) {
child = serio;
break;
}
}
}
spin_unlock_irqrestore(&serio_event_lock, flags);
return child;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Dmitry Torokhov | 78 | 84.78% | 2 | 66.67% |
Vojtech Pavlik | 14 | 15.22% | 1 | 33.33% |
Total | 92 | 100.00% | 3 | 100.00% |
/*
* Serio port operations
*/
static ssize_t serio_show_description(struct device *dev, struct device_attribute *attr, char *buf)
{
struct serio *serio = to_serio_port(dev);
return sprintf(buf, "%s\n", serio->name);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Dmitry Torokhov | 37 | 88.10% | 1 | 50.00% |
Yani Ioannou | 5 | 11.90% | 1 | 50.00% |
Total | 42 | 100.00% | 2 | 100.00% |
static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
{
struct serio *serio = to_serio_port(dev);
return sprintf(buf, "serio:ty%02Xpr%02Xid%02Xex%02X\n",
serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Dmitry Torokhov | 61 | 98.39% | 1 | 50.00% |
Greg Kroah-Hartman | 1 | 1.61% | 1 | 50.00% |
Total | 62 | 100.00% | 2 | 100.00% |
static ssize_t type_show(struct device *dev, struct device_attribute *attr, char *buf)
{
struct serio *serio = to_serio_port(dev);
return sprintf(buf, "%02x\n", serio->id.type);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Dmitry Torokhov | 38 | 86.36% | 1 | 33.33% |
Yani Ioannou | 5 | 11.36% | 1 | 33.33% |
Greg Kroah-Hartman | 1 | 2.27% | 1 | 33.33% |
Total | 44 | 100.00% | 3 | 100.00% |
static ssize_t proto_show(struct device *dev, struct device_attribute *attr, char *buf)
{
struct serio *serio = to_serio_port(dev);
return sprintf(buf, "%02x\n", serio->id.proto);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Dmitry Torokhov | 38 | 86.36% | 1 | 33.33% |
Yani Ioannou | 5 | 11.36% | 1 | 33.33% |
Greg Kroah-Hartman | 1 | 2.27% | 1 | 33.33% |
Total | 44 | 100.00% | 3 | 100.00% |
static ssize_t id_show(struct device *dev, struct device_attribute *attr, char *buf)
{
struct serio *serio = to_serio_port(dev);
return sprintf(buf, "%02x\n", serio->id.id);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Dmitry Torokhov | 38 | 86.36% | 1 | 33.33% |
Yani Ioannou | 5 | 11.36% | 1 | 33.33% |
Greg Kroah-Hartman | 1 | 2.27% | 1 | 33.33% |
Total | 44 | 100.00% | 3 | 100.00% |
static ssize_t extra_show(struct device *dev, struct device_attribute *attr, char *buf)
{
struct serio *serio = to_serio_port(dev);
return sprintf(buf, "%02x\n", serio->id.extra);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Dmitry Torokhov | 38 | 86.36% | 1 | 33.33% |
Yani Ioannou | 5 | 11.36% | 1 | 33.33% |
Greg Kroah-Hartman | 1 | 2.27% | 1 | 33.33% |
Total | 44 | 100.00% | 3 | 100.00% |
static ssize_t drvctl_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
{
struct serio *serio = to_serio_port(dev);
struct device_driver *drv;
int error;
error = mutex_lock_interruptible(&serio_mutex);
if (error)
return error;
if (!strncmp(buf, "none", count)) {
serio_disconnect_port(serio);
} else if (!strncmp(buf, "reconnect", count)) {
serio_reconnect_subtree(serio);
} else if (!strncmp(buf, "rescan", count)) {
serio_disconnect_port(serio);
serio_find_driver(serio);
serio_remove_duplicate_events(serio, SERIO_RESCAN_PORT);
} else if ((drv = driver_find(buf, &serio_bus)) != NULL) {
serio_disconnect_port(serio);
error = serio_bind_driver(serio, to_serio_driver(drv));
serio_remove_duplicate_events(serio, SERIO_RESCAN_PORT);
} else {
error = -EINVAL;
}
mutex_unlock(&serio_mutex);
return error ? error : count;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Dmitry Torokhov | 167 | 86.98% | 4 | 44.44% |
Duncan Laurie | 14 | 7.29% | 1 | 11.11% |
Yani Ioannou | 5 | 2.60% | 1 | 11.11% |
Arjan van de Ven | 4 | 2.08% | 1 | 11.11% |
Dmitry Baryshkov | 1 | 0.52% | 1 | 11.11% |
Greg Kroah-Hartman | 1 | 0.52% | 1 | 11.11% |
Total | 192 | 100.00% | 9 | 100.00% |
static ssize_t serio_show_bind_mode(struct device *dev, struct device_attribute *attr, char *buf)
{
struct serio *serio = to_serio_port(dev);
return sprintf(buf, "%s\n", serio->manual_bind ? "manual" : "auto");
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Dmitry Torokhov | 41 | 89.13% | 1 | 50.00% |
Yani Ioannou | 5 | 10.87% | 1 | 50.00% |
Total | 46 | 100.00% | 2 | 100.00% |
static ssize_t serio_set_bind_mode(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
{
struct serio *serio = to_serio_port(dev);
int retval;
retval = count;
if (!strncmp(buf, "manual", count)) {
serio->manual_bind = true;
} else if (!strncmp(buf, "auto", count)) {
serio->manual_bind = false;
} else {
retval = -EINVAL;
}
return retval;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Dmitry Torokhov | 88 | 94.62% | 2 | 66.67% |
Yani Ioannou | 5 | 5.38% | 1 | 33.33% |
Total | 93 | 100.00% | 3 | 100.00% |
static ssize_t firmware_id_show(struct device *dev, struct device_attribute *attr, char *buf)
{
struct serio *serio = to_serio_port(dev);
return sprintf(buf, "%s\n", serio->firmware_id);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Hans de Goede | 42 | 100.00% | 1 | 100.00% |
Total | 42 | 100.00% | 1 | 100.00% |
static DEVICE_ATTR_RO(type);
static DEVICE_ATTR_RO(proto);
static DEVICE_ATTR_RO(id);
static DEVICE_ATTR_RO(extra);
static struct attribute *serio_device_id_attrs[] = {
&dev_attr_type.attr,
&dev_attr_proto.attr,
&dev_attr_id.attr,
&dev_attr_extra.attr,
NULL
};
static struct attribute_group serio_id_attr_group = {
.name = "id",
.attrs = serio_device_id_attrs,
};
static DEVICE_ATTR_RO(modalias);
static DEVICE_ATTR_WO(drvctl);
static DEVICE_ATTR(description, S_IRUGO, serio_show_description, NULL);
static DEVICE_ATTR(bind_mode, S_IWUSR | S_IRUGO, serio_show_bind_mode, serio_set_bind_mode);
static DEVICE_ATTR_RO(firmware_id);
static struct attribute *serio_device_attrs[] = {
&dev_attr_modalias.attr,
&dev_attr_description.attr,
&dev_attr_drvctl.attr,
&dev_attr_bind_mode.attr,
&dev_attr_firmware_id.attr,
NULL
};
static struct attribute_group serio_device_attr_group = {
.attrs = serio_device_attrs,
};
static const struct attribute_group *serio_device_attr_groups[] = {
&serio_id_attr_group,
&serio_device_attr_group,
NULL
};
static void serio_release_port(struct device *dev)
{
struct serio *serio = to_serio_port(dev);
kfree(serio);
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% |
/*
* Prepare serio port for registration.
*/
static void serio_init_port(struct serio *serio)
{
static atomic_t serio_no = ATOMIC_INIT(-1);
__module_get(THIS_MODULE);
INIT_LIST_HEAD(&serio->node);
INIT_LIST_HEAD(&serio->child_node);
INIT_LIST_HEAD(&serio->children);
spin_lock_init(&serio->lock);
mutex_init(&serio->drv_mutex);
device_initialize(&serio->dev);
dev_set_name(&serio->dev, "serio%lu",
(unsigned long)atomic_inc_return(&serio_no));
serio->dev.bus = &serio_bus;
serio->dev.release = serio_release_port;
serio->dev.groups = serio_device_attr_groups;
if (serio->parent) {
serio->dev.parent = &serio->parent->dev;
serio->depth = serio->parent->depth + 1;
} else
serio->depth = 0;
lockdep_set_subclass(&serio->lock, serio->depth);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Dmitry Torokhov | 103 | 60.23% | 6 | 42.86% |
Jiri Kosina | 33 | 19.30% | 1 | 7.14% |
Dmitry Baryshkov | 16 | 9.36% | 1 | 7.14% |
Randy Dunlap | 8 | 4.68% | 1 | 7.14% |
Andrew Morton | 3 | 1.75% | 1 | 7.14% |
Kay Sievers | 2 | 1.17% | 1 | 7.14% |
Richard Leitner | 2 | 1.17% | 1 | 7.14% |
Arjan van de Ven | 2 | 1.17% | 1 | 7.14% |
Aniroop Mathur | 2 | 1.17% | 1 | 7.14% |
Total | 171 | 100.00% | 14 | 100.00% |
/*
* Complete serio port registration.
* Driver core will attempt to find appropriate driver for the port.
*/
static void serio_add_port(struct serio *serio)
{
struct serio *parent = serio->parent;
int error;
if (parent) {
serio_pause_rx(parent);
list_add_tail(