The Linux USB input subsystem, Part I

Source: Internet
Author: User

 

The Linux USB input subsystem is a single, harmonized way to manage all input devices. this is a relatively new approach for Linux, with the system being partly inconfigurated in kernel version 2.4 and fully integrated in the 2.5 development series.

This article covers four basic areas: a description of what the input subsystem does, a short historical perspective on development, A description of how the input subsystem is implemented in the kernel and an overview of the User-space API for the input subsystem and how you can use it in your programs. the first three areas are discussed in this article. the user-space API, the final topic, will be discussed in Part II of this article.

What is the input subsystem?

The input subsystem is the part of the Linux kernel that manages the varous input devices (such as keyboards, mice, joysticks, tablets and a wide range of other devices) that a user uses to interact with the kernel, command line and graphical user interface. this subsystem is supported in the kernel because these devices usually are accessed through special hardware interfaces (such as serial ports, PS/2 ports, Apple desktop bus and the Universal Serial Bus ), which are protected and managed by the kernel. the kernel then exposes the user input in a consistent, device-independent way to user space through a range of defined APIs.

How we got here

The Linux input subsystem is primarily the work of vojtech Pavlik, who saw the need for a flexible input system from his early work on joystick support for Linux and his later work on supporting USB. the first integration for the input subsystem replaced existing joystick and USB drivers in the 2.3 development kernel series. this support carried over to version 2.4, and input support in the 2.4 series is basically limited to joysticks and USB input devices.

The 2.5 development kernel series fully integrates the input subsystem. this tutorial is based on the full integration, which will be the input API for the 2.6 stable kernel. although some differences exist in the user-space APIs between 2.4 and 2.5 kernels at the time of this writing, there is ongoing work to harmonize them-mainly by updating the 2.4 kernel.

Under the hood-understanding the kernel Internals

The three elements of the input subsystem areInput Core,DriversAndEvent Handlers. The relationship between them is shown in Figure 1. note that while the normal path is from low-level hardware to drivers, drivers to input core, input core to handler and handler to user space, there usually is a return path as well. this return path allows for such things as setting the LEDs on a keyboard and providing motion commands to force feedback joysticks. both Directions use the same event definition, with differentTypeIdentifiers.

Figure 1. The relationship between the elements of the input subsystem

The interaction between various elements is throughEvents, Which are implemented as structures (see Listing 1 ). the first field (time) is a simple timestamp, while the other fields are more interesting. the Type field shows the generic type of event being reported, for example, a key press or button press, relative motion (like moving a mouse) or absolute motion (like moving a joystick ). the code field tells which of the varous buttons or axes are being manipulated, while the value field tells you what the state or motion is. for example, if the type is a key or button, code tells you which key or button it is, and value tells you if the button has been pressed or released. similarly, if type is a relative axis, then code tells you which axis, and value tells you how much motion you have and the direction of that motion on that axis. if you move a mouse in a diagonal direction, while simultaneously moving the scroll wheel, you will get three relative events per update: one for motion in the vertical direction (Y-axis ), one for motion in the horizontal direction (x-axis) and one for motion of the wheel.

Listing 1. event-dev-struct.txt

Event Handlers provide the interface to user space, converting the standard event format into the format required by a special API. handlers usually take care of the device nodes (/dev entries) too. the most common handler is the keyboard handler, which is the "standard input" that most programmers (especially C programmers) are familiar.

Drivers usually interface with low-level hardware, such as USB, PCI memory or I/O regions, or serial port I/O regions. they convert the low-level hardware version of the user input into the standard event format before sending it to the input core. the input core uses a standard kernel plugin design, with input_register_device () used to add each device and input_unregister_device () used to remove it. the argument to these CILS is the input_dev structure, which is shown in Listing 1. although this structure looks quite large, most of the entries are provided to allow a driver to specify the capabilities of the device, such as which event types and codes may be sent or stored ed by the device.

In addition to managing drivers and handlers, the input core also exports a useful/proc filesystem interface, which can be used to see what devices and handlers are currently active. here is a typical example from/proc/bus/input/devices showing a USB mouse:

I: Bus=0003 Vendor=046d Product=c002 Version=0120N: Name="Logitech USB-PS/2 Mouse M-BA47"P: Phys=usb-00:01.2-2.2/input0H: Handlers=mouse0 event2B: EV=7B: KEY=f0000 0 0 0 0 0 0 0 0B: REL=103

The I: line is the identity information-showing Bus Type 3 (which is USB) and the vendor, product and version information from the USB descriptors in the mouse. the N: line shows the name, which in this case is a string provided by the USB descriptors. the P: line shows the physical device information; here, it's structure information comprised of the PCI address for the USB controller, the USB tree and the input interface. the input0 part indicates this is the first logical input device for the physical device. some devices, such as multimedia keyboards, can map part of the physical device to one logical input device and map another part to a second logical input device. the H: line shows the handler drivers associated with this device; we'll discuss this in more detail later in the article. the various B: lines show the bitfields that identify the devices 'capabilities, in this case some keys for the buttons and relative axes for the ball and the scroll wheel.

Listing 2. Register. c

This/proc interface is a useful way to test some simple drivers. let's consider the example of a driver that registers on init and unregisters on removal, as shown in Listing 2. this does some preliminary initialization using init_input_dev (). it sets up the name, physical and identification descriptors, and then sets up the bit arrays to indicate that the device is capable of providing one type of event (ev_key indicating buttons and keys) with two possible codes (key_a and key_ B, indicating the key labels ). the initialization routine then registers the device with the input core. if you add this code to the kernel (usingModprobe), You can see the new device has been added to/proc/bus/input/devices, as shown below:

I: Bus=0019 Vendor=0001 Product=0001 Version=0100N: Name="Example 1 device"P: Phys=A/Fake/PathH: Handlers=kbd event3B: EV=3B: KEY=10000 40000000

If we actually want to send events from our device driver to the input core, we need to call input_event or one of the convenience wrappers, such as input_report_key or input_report_abs, provided in <Linux/input. h>. an example of code that does this is shown in listing 3. this example is basically the same setup as the previous one, should t that we add a timer that callex2_timeout (). this new routine sends four presses of key_a and four presses of key_ B. note that 16 key press events are created in total, because a separate event is created for each press and each release. these events are passed to the input core and, in turn, to the keyboard handler, which will cause the pattern "aaaabbbb" or "aaaabbbb", depending on the shift key selection, to be transmitted to the console or command line. the timer is then set up to run four seconds later, looping infinitely. the four-second delay is intended to give you enough time to remove the module when you have seen enough of the pattern. if you reduce the delay, make sure you have another way of accessing the system, such as an SSH connection. also note the call to the input_sync function. this function is used to inform the event handler (in this case, the keyboard handler) that the device has transmitted an internally consistent set of data. the handler may choose to buffer events until input_sync is called.

Listing 3. aaaabbbb. c

Let's look at one final example of a driver, this time showing how relative information is provided, shown in Listing 4. this example is a driver that emulates a mouse. the initial setup configures the device to have two relative axes (rel_x and rel_y) and one key (btn_left ). as in the previous example, we use a timer to runEx3_timeout. This timer then CILS input_report_rel to provide small relative motion (five UNIT steps-the relative movement is the third argument to the function) consisting of 30 steps right, 30 steps down, 30 steps left and 30 steps up, so the cursor is moved in a square pattern. to provide the definition of movement, the timeout is only 20 milliseconds. again, note the call to input_sync, which is used to ensure that input handlers only process events that make up a consistent set. this specification wasn' t strictly necessary in our previous example. but, it is definitely required to convey information like relative movement to the input core, because more than one axis may be required to represent movement. if you were moving diagonally, you wowould do something like:

...input_report_rel(..., REL_X, ...);input_report_rel(..., REL_Y, ...);input_sync(...);...

Which ensures that the mouse will move diagonally and not between SS, then up.

Listing 4. squares. c

 

As the USB input subsystem spreads further with each kernel release, it's time to understand what it's doing for your devices. handlers-getting to user space

In the previous section, we saw that the device drivers basically sat between the hardware and the input core, translating hardware events, usually interrupts, into input events. to make use of those input events, we use handlers, which provide a user-space interface.

The input subsystem should des most of the handlers you'll likely need: a keyboard handler to provide a console, a mouse handler for applications like the X Window System, A joystick handler for games and also a touchscreen handler. there is also a general-purpose handler called the event handler, which basically provides input events to user space. this means you almost never need to write a handler in the kernel, because you can do the same thing with the event handler and equivalent code in user space. this API discussion is covered in the second part of this article.

Acknowledgements

I 'd like to thank Greg kroah-Harman and vojtech Pavlik for their compatibility with this article.

 

 

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.