chardevs

Learning Objective

Understand the purpose and basic usage of a Linux character device

Overview

  1. What is a character device?

  2. Why do we want to use character devices

  3. How does the user interact with character devices?

  4. How does a kernel module implement a character device?

What is a character device?

Character by character kernel interface

What is a character device?

Streamed, not buffered

What is a character device?

Not necessarily related to physical device

  1. Can implement a device driver

  2. We will come back to this

What is a character device?

A file in /dev

  1. Identified by major and minor numbers

What is a character device?

Generally implemented by a kernel module

  • Why?

Finding active character devices

Generally located in the /dev directory

demo

ls -l /dev

  1. mknod(7)

demo

cat /proc/devices

Interacting with character devices

Same interface as any other file

  1. cat, echo, and other commands

  2. System calls in a program

demo

Playing with devices:

  1. /dev/null: discard input

  2. /dev/zero: zero output

  3. /dev/tty: this terminal

  4. /dev/kmsg: kernel ring buffer access

  5. /dev/urandom: random bytes (click for mythology)

  6. /dev/mem: physical memory access 😨

Implementation demo design

Random number generator

  1. Between 0 and 256

  2. Read with cat

Implementation overview

  1. Need major and minor numbers

  2. Need file operations

  3. Register device with cdev subsystem

  4. Implement open and close handlers

  5. Implement read and write handlers

Starting point

  1. Module stub

Note on __init

  1. Special section

  2. Can reclaim after module load

demo

kdlpdev4.c

Choose major and minors

Contained in dev_t type

Related: MAJOR, MINOR, and MKDEV

  1. We don't use these now

Choose major and minors

In init(): alloc_chrdev_region()

In exit(): unregister_chrdev_region()

An alternative

register_chrdev_region()

  1. Static rather than dynamic

  2. Why don't we use this?

demo

kdlpdev3.c

Necessary structs

struct cdev

struct file operations

Register our character device

In init(): cdev_init() and cdev_add()

In exit(): cdev_del()

demo

kdlpdev2.c

Implement open and close

struct inode

struct file

demo

kdlpdev1.c

Implement read and write

copy_to_user()

__user macro

  1. Useful for static analysis and documentation

  2. No runtime effect

demo

The complete kdlpdev.c

Summary

A character device implements a character-by-character interface with the kernel

  1. This is a common interface for device drivers

Summary

A kernel module can implement a character device

Summary

A character device is identified by a major and minor number

Summary

Character devices are generally located in /dev

  1. Not required

  2. Only the major and minor matte to the kernel

Summary

Character devices are labeled by 'c' in ls -l output

Summary

The /proc/devices file contains a list of major numbers and character devices

Summary

Use open(2), close(2), read(2), write(2), and more to interact with the character device

End