1

I am learning device drivers and I got this doubt , is keyboard driver a character device driver in Linux?

Franc
  • 229
  • 3
  • 15

2 Answers2

2

Yes, the keyboard driver is a character device. If you do:

$ cat /proc/bus/input/devices

then you'll see a list of devices, among which should be your keyboard. That'll include something like:

H: Handlers=sysrq kbd event18

From there, see /dev/input/event18:

$ ls -l /dev/input/event18
crw-rw---- 1 root input 13, 82 Jul  9 15:44 /dev/input/event18

Note that that's a character device.

If you cat that device, then type something, you'll see activity:

$ sudo cat /dev/input/event18
... type something, see the byte stream as characters

See this link for a simple Python script that can consume those bytes and interpret them; I'll reproduce the script here:

#!/usr/bin/env python3

import struct

f = open("/dev/input/event18", "rb"); # Open the file in the read-binary mode

while True:
    data = f.read(24):
    print(struct.unpack('4IHHI', data))

According to the site I referenced, the fields from left-to-right represent:

  • Time Stamp_INT
  • 0
  • Time Stamp_DEC
  • 0
  • type
  • code (key pressed)
  • value (press/release)
Andy Dalton
  • 13,654
  • 1
  • 25
  • 45
1

The keyboard driver in Linux appears in the kernel input subsystem, and userspace sees those as character drivers in /dev/input.

You can use evtest to see what kind of events they produce.

(And as you only have character devices and block devices, and the operations on block devices make no sense for keyboard, it pretty much must be a character device.)

dirkt
  • 31,679
  • 3
  • 40
  • 73