Analysis of driving principle of input device in Android (I.)

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/eilianlau/article/details/6969361

In the case of Android, the event input device driver principle analysis is not as good as the Linux input subsystem, anyway, this is unchanged, in the Android bottom-up development for Linux basic driver design is still unchanged, Of course the android underlying mechanism also adds several mechanisms that belong to Android itself. Typical IPC

The input device drivers in Android include: Joystick (joystick), mouse (mouse), and event devices.

1. Input subsystem structure, find two gray often beautiful pictures on the Internet.

The following is a more beautiful, more intuitive to see input subsystem what is what, more can reflect the user space, kernel space, how the driver is connected ...

Input driver is also a character device, the main device number is 13, the secondary device number is automatically generated between 64~95, this input driver is quite complex. There are a few files to focus on in the Android kernel

A) include/linux/input.h (drive header file)

b) driver/input/input.c (drive core implementation, including a large number of operating interfaces)

c) Driver/input/event.c (event mechanism)

d) driver/input/joydev.c (Joystick drive)

e) driver/input/mousedev.c (mouse drive)

In fact, none of the above is going to be our own. The kernel has been implemented for us, but we need to interact with Inputcore when we write hardware drivers, so we need to use the interfaces in the above functions, which means that the functions above are transparent.

2. Event-driven principle and its implementation

In the kernel, a input_dev is used to describe an input device, which is defined as follows, in which the kernel uses input_register_device (struct Input_dev *dev) to register an input device

This structure is very long, so it is listed a few .... It's defined in the input.h.

struct Input_dev {

。。。。。。。。。。。
struct input_id id;/* point to input_id structure */
BOOL Sync;
struct device dev;/** These devices all belong to the bus device model */
struct List_head h_list; //
struct List_head node; List node for input_handle linked list
};

Use Input_handler to represent the interface of the input device and register with Input_register_handler (Structinput_handler *handler)

struct Input_handler {

void *private;

。。。。。。。。。。
Int (*connect) (struct Input_handler *handler, struct input_dev *dev, const struct input_device_id *id);
void (*disconnect) (struct input_handle *handle);
void (*start) (struct input_handle *handle);

const struct File_operations *fops;
int minor;
const char *name;

const struct INPUT_DEVICE_ID *id_table;

struct List_head h_list;
struct List_head node;
};

Event-driven implementation process

1) Input Device registration

int Input_register_device (struct Input_dev *dev)
{
static atomic_t input_no = Atomic_init (0);
struct Input_handler *handler;
const char *path;
int error;
/* Every input device generates EV_SYN/SYN_REPORT events. */
__set_bit (Ev_syn, dev->evbit);//see to Inpu.h

/* key_reserved is not supposed to being transmitted to userspace. */
__clear_bit (key_reserved, dev->keybit);
/* Make sure this bitmasks not mentioned in Dev->evbit is clean. */
Input_cleanse_bitmasks (Dev);
/*
* If delay and period is pre-set by the driver, then autorepeating
* is handled by the driver itself and we don ' t do it in input.c.
*/
Init_timer (&dev->timer);
Handles repeated keystrokes. Assign a default value if it is not assigned
if (!dev->rep[rep_delay] &&!dev->rep[rep_period]) {
Dev->timer.data = (long) dev;
Dev->timer.function = Input_repeat_key;
Dev->rep[rep_delay] = 250;
Dev->rep[rep_period] = 33;
}
if (!dev->getkeycode)//Get key Scan code
Dev->getkeycode = Input_default_getkeycode;
if (!dev->setkeycode)//Set key value
Dev->setkeycode = Input_default_setkeycode;
Dev_set_name (&dev->dev, "Input%ld",
(unsigned long) Atomic_inc_return (&input_no)-1);
Register the device encapsulated in Input_dev with the SYSFS
Error = Device_add (&dev->dev);
if (Error)
return error;
Path = Kobject_get_path (&dev->dev.kobj, Gfp_kernel);
PRINTK (kern_info "input:%s as%s\n",
Dev->name? Dev->name: "Unspecified device", path? Path: "N/a");
Kfree (path);

Error = mutex_lock_interruptible (&input_mutex);
if (Error) {
Device_del (&dev->dev);
return error;
}
Hang the Input_device in the input_dev_list linked list
List_add_tail (&dev->node, &input_dev_list);
Call Input_attach_handler (Dev, handler) for each handler that is mounted in the input_dev_list;
List_for_each_entry (handler, &input_handler_list, node)
Input_attach_handler (Dev, handler);
Input_wakeup_procfs_readers ();
Mutex_unlock (&input_mutex);
return 0;
}

The above function first hooks the input_device to the Input_dev_list list and then calls Input_attach_handler (Dev, handler) for each handler that is mounted in input_dev_list To match, for example, device and driver matching in the equipment model, all input devices are mounted on input_dev_list and all handler are mounted on input_handler_list, So how do they relate to each other? The matching process is as follows

Static int Input_attach_handler (struct Input_dev *dev, struct Input_handler *handler)
{
     const struct INPUT_DEVICE_ID *id;
    int error;
    id = Input_match_device (handler, Dev);
    if (!id)
        return-enodev;
    error = Handler->connect (handler, dev, id);
    if (Error && Error! =-enodev)
        PRINTK (kern_err
             "input:failed to attach handler%s to device%s,"
              "error:%d\n",
             handler->name, Kobject_name (&dev->dev.kobj), error);
    return error;
}

The above function calls Input_match_device to Handler, and Dev passes the input_device_id *id to match if the match succeeds call Handler->connect to correlate the struct Input_dev *dev, and struct Input_handler *handler structure. Here's a look at the process of Input_match_device (handler, Dev)

#define MATCH_BIT (BIT, max) \
for (i = 0; i < bits_to_longs (max); i++) \
if ((Id->bit[i] & dev->bit[i])! = Id->bit[i]) \
Break \
if (i! = bits_to_longs (max)) \
Continue

static const struct INPUT_DEVICE_ID *input_match_device (struct input_handler *handler,struct input_dev *dev)
{
const struct INPUT_DEVICE_ID *id;
int i;
for (id = handler->id_table; Id->flags | | id->driver_info; id++) {//flags Configuration matching type
if (Id->flags & Input_device_id_match_bus)//Match bus type
if (id->bustype! = dev->id.bustype)
Continue
if (Id->flags & Input_device_id_match_vendor)//matching manufacturer
if (Id->vendor! = Dev->id.vendor)
Continue
if (Id->flags & input_device_id_match_product)//matching manufacturer
if (id->product! = dev->id.product)
Continue
if (Id->flags & input_device_id_match_version)//Match version number
if (id->version! = dev->id.version)
Continue
If the above Id->flags match succeeds or if the id->flags is not defined, execute the following function
Match_bit (Evbit, Ev_max);
Match_bit (Keybit, Key_max);
Match_bit (Relbit, Rel_max);
Match_bit (Absbit, Abs_max);
Match_bit (Mscbit, Msc_max);
Match_bit (Ledbit, Led_max);
Match_bit (Sndbit, Snd_max);
Match_bit (Ffbit, Ff_max);
Match_bit (Swbit, Sw_max);
if (!handler->match | | handler->match (handler, Dev))
return ID;
}
return NULL;
}

Where handler's registration and similar above, interested friends can read the Linux kernel source code

Analysis of driving principle of input device in Android (I.)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.