KC7MM Wiki

Amateur Radio with KC7MM

User Tools

Site Tools


linuxusernet:inside_linux:devices

A Look Inside Linux series

A series of short topics on how Linux works and how to make it work the way you want it to, which I present during the weekly sessions of the Linux User Net. The target audience is Hams who are new to Linux and want to know more about it, as well as experienced Linux users who can learn more about their chosen operating system. These are my notes for the presentations. (Russ, KC7MM)

17. Devices in Linux: the /dev directory

One of the stellar features of Linux is the way it manages devices:

  • Device drivers are dynamic kernel modules that can be loaded and unloaded without rebooting.
  • The software interface of each device is mapped to a directory in the Linux file system, where they can be selectively access by programs.

Today we'll look at that second item: the device interfaces. They are all located in The Linux /dev directory. And, there are a lot of them. We'll confine our discussion to the ones we users are most likely to work with.

What it is

The /dev directory is the home of hardware interfaces. These interfaces work in a variety of ways, each one depending on the nature of the device driver that sits behind it.

The purpose of an interface is to allow programs to interact with the device by passing data. The nature of that data interchange depends on function of the device.

  • Some are unidirectional, either reading data or writing it, while others are bidirectional, moving data both in and out.
  • Some pass data as continuous bit streams, others in discrete blocks.
  • Some – such as serial ports – can be accessed directly through the /dev directory, while others – such as disk drives – must go through the additional step of being mounted to another directory before use. (More about that next time.)

How it works

We'll start by looking at /dev directories that you're likely to encounter:

  • Serial devices: /dev/ttySX for UART devices and /dev/ttyUSBX for USB. (The X is a sequential integer: ttyS0, ttyS1…)
    • These employ standard serial protocols such as RS232 and RS485 that use hardware control.
    • Examples: dialup modem, line printer.
  • Serial communication devices: /dev/ttyACMX.
    • Communicate with serial packets, but don't use hardware control.
    • Examples: TNC (terminal node controller), Flexcontrol.
  • Human interface devices: /dev/hidrawX
    • Devices for human interaction with the computer.
    • Examples: keyboard, mouse, audio controller.
  • Block devices: /dev/sdXX, /dev/mmcblkXX, etc
    • Data is transferred in blocks, not streamed.
    • Examples: HDD, SSD, eMMC memory, HD cards, USB drives.
    • To view the system names of these devices, run lsblk in a terminal.

Udev for device management

Most current Linux systems use Udev to manage devices.

  • Udev dynamically creates or removes device node files at in the /dev directory for all types of devices.
  • Udev is a component of systemd, and runs as a service named systemd-udevd.
  • systemd-udevd receives device events directly from the kernel whenever a device is added or removed from the system.
  • When it receives an event, systemd-udevd executes matching instructions specified in Udev rules (more on these later).

At Boot time

Devices that are present at the time the system boots are detected by the kernel, and Udev creates /dev subdirectories for them:

  • Permanently installed internal devices such as disk drives.
  • Removable devices that are connected to a port (USB, SD card reader).

At Run time

  • While the system is running, the kernel detects when removable devices are connected or disconnected.
  • Udev creates or removes the appropriate /dev subdirectory.

Udev rules

Udev rules control the mapping of devices to /dev directories.

Some Udev rules are put in place when a Linux system is installed. These set the default behavior of Udev. The system administrator – you – can create additional rules to suit your particular needs. As we'll see, this comes in handy when you need to consistently map a device to a specific directory.

How they work

Recall that the Udev daemon does its job in response to events that it receives from the kernel. Those events can occur at any time, in any order.

Udev creates device directories in numerical order, starting at zero (0): for example, tty0, followed by tty1, tty2, etc.

Since the incoming device events have no fixed order, devices may be assigned to any /dev subdirectory. Thus, a device that's using /dev/ttyUSB0 now could be assigned to /dev/ttyUSB3 the next time the system is rebooted.

This is fine much of the time, but doesn't work very well in the case in which software is configured to work with a particular device, say /dev/ttyACM3. What you need, then, is for Udev to assign the device to /dev/ttyACM3 every time it is connected. The way to make that happen is to create a Udev rule for the device.

Making your own rules

I wish I could tell you that setting up Udev rule is a trivial process but, alas, it takes some effort – although simply setting a directory mapping for a device is pretty straightforward and shouldn't resent a problem to neophytes. For help with that, look at this excellent post by Johnathan, KI7UEZ, on our groups.io site.

Doing something more esoteric can be tricky. I needed to set group permissions on a /dev/hidraw directory for USB control box for my SDR transceiver. I found it to be a significant challenge, but I was able to make it happen in the end. YMMV, depending on what you need the rule to do.

(BTW, we saw when looking at the Linux file system that access to system hardware is controlled through file permissions on device directories. /dev is where that is done, and Udev rules are the way to set those permissions.)

In any case, the process of creating Udev rules is not something I can cover over the radio. Fortunately, there's a lot of information on it on the Web. The Arch Linux wiki is an excellent source that I found to be most helpful. And here's another good source.

Regarding the Arch Linux wiki, I've found it to be an excellent source of information on Linux technical topics, particularly system configuration. I have benefited from it numerous times, and suggest that a trip there when you're confronting a technical conundrum might be just the thing you need.

Wrap-up

We've seen that:

  1. Hardware in a Linux system is made available to programs via subdirectories of the /dev directory.
  2. Creating and configuring the subdirectories is accomplished by the systemd-udevd service, in response to device events generated by the Linux kernel.
  3. The specifics of how the subdirectories are configured are governed by Udev rules.
  4. The system comes with default rules, but you can make your own Udev rules to get the behavior you need.

Return to series index

linuxusernet/inside_linux/devices.txt · Last modified: 2022/05/04 14:47 by KC7MM