Introduction to Linux Device Drivers Class 4: Input Device Drivers with Linux Kernel Aug 7, 2014 Khem Raj
Introduction Driver Collection Supporting input devices Keyboards Mice Touchscreens Wheels …
Introduction Standardize handling of similar devices E.g. mice can be USB, bluetooth, PS/2 Standard event system to interact with userspace applications Input core extract common bits e.g. serio Name one advantage of kernel’s input subsystem ?
Input Subsystem Input device driver Input event driver Talks to the device Input event driver Talks to Applications
Input Subsystem XWin Qt GUI apps Input Devices /dev/input/* Userspace Kernel Space h/w space XWin Qt GUI apps Input Devices /dev/input/* Input Core subsystem Input Device Drivers Input Event Handers Serio USB HID HIDP PS/2 Keyboard USB Mice Bluetooth Mice
Input Events Understood by different Graphical systems e.g. X11, QT etc. H/W Independent abstraction Evdev Generic Input Event Driver
Input Event Handlers /** * struct input_value - input value representation * @type: type of value (EV_KEY, EV_ABS, etc) * @code: the value code * @value: the value */ struct input_value { __u16 type; __u16 code; __s32 value; }; What is a Input Event Driver ?
Input Device Drivers Serio Used to access keyboard controllers PS/2 mouse and keyboards, trakcpoints Serial port controllers Touch pads Using serio via callback serio_register_driver() sends cmds to controllers To Add new driver Define open, close, start, stop, etc. Use serio_register_port ()
Application/emulates mouse moves Example Console/gpm server Application/emulates mouse moves /dev/input/event0 Dispatch coordinates through sysfs (/sys/device/..) evdev Virtual Mouse Driver Input core
Application Opens a sysfs node Generate random relative coordinates open("/sys/devices/platform/vms/coordinates", O_RDWR) Generate random relative coordinates Write it down to device file descriptor Close the file descriptor
Mouse Driver Defines a struct input_dev – Input device Defines platform_device – Device struct Initialization Register a platform device and allocate struct input_dev platform_device_register_simple and input_allocate_device Create a sysfs node to read simulated coordinates sysfs_create_group(..) Define what data it will generate ( coordinates ) Use set_bit () to set members in input_dev struct e.g. evbit set_bit(REL_X, …) set_bit(REL_Y, …) Register with the input subsystem input_register_device () Write Function Gets the buffer with coordinates Use input_report_rel () to generate events ( REL_X, REL_Y) input_sync () denotes completions of events
Mouse Driver Attach to sysfs write function DEVICE_ATTR(<devicename>, 0644, NULL, <driver write function>); Define attribute group and descriptor Exit/Cleanup Unregister from input subsystem input_unregister_device () Clean sysfs node sysfs_remove_group () Unregister Driver platform_device_unregister () How does device driver write function gets registered with sysfs ?
Input Subsystem Creates evdev structure from events Send it on /dev/input/eventX gpm receives event in input_event format gpm -m /dev/input/eventX -t evdev
Evdev Interface This driver uses generic evdev interface Some devices define own Event drivers They populate input_handler struct Register input_register_handler(&mousedev_handler); Which structure is initialized to define event driver ?
Output Events Keyboard/mice LEDs and Sound beeps Set by evbits and callback function See drivers/input/misc/pcspkr.c Callback - static int pcspkr_event () static int __devinit pcspkr_probe () Declared evbits pcspkr_dev->evbit[0] = BIT(EV_SND); pcspkr_dev->sndbit[0] = BIT(SND_BELL) | BIT(SND_TONE); /* The Callback routine */ pcspkr_dev->event = pcspkr_event;
Debugging Input Drivers Use evbug module Dumps type, code and value (input_event struct) Output looks like /* Touchpad Movement */ evbug.c Event. Dev: isa0060/serio1/input0: Type: 3, Code: 28, Value: 0 How do you debug input drivers ?
Source Event handlers Device drivers reside close to subsystem code drivers/input directory But keyboard driver is in drivers/char Device drivers reside close to subsystem code net/bluetooth Documentation/input describes some of input interfaces Most of interfaces in include/linux/input.h
Thanks Q & A