Devices

From Linux 101, The beginner's guide to all things Linux.

Jump to: navigation, search

Contents

[edit] Introduction

The files under /dev represent actual hardware devices on your system. This is approach of treating devices as an entry on a filesystem is not so different from Windows (at least for some devices), but what is different is that you can view them in Linux, under /dev.

This works because you write and read from devices just like you write and read from files. So if you can reuse the code to do this, then you're a good programmer.

[edit] Historical Approach

For many years the approach to creating these devices was with a command called mknod. To be effective at using the command you had to know a good bit about the hardware and things along that line.

One thing to note is that most users never, ever had to use mknod. Most distributions provided some mechanism for supplying all of the /dev entries that a user could ever need.

[edit] Modern Approach

In the past in Linux automatically detecting hardware was impossible. Companies did not follow standards. Hardware was not designed to allow much autodetection. It was rough, at times just to get hardware to even work.

Of course in time this changed, PCI was developed and more and more companies started using (or being forced to use) standards. And then it began to get easier to do autodection.

In the kernel, the devfs (the /dev filesystem) was developed. It automatically set up the /dev entries to work with the kernel.

The kernel developers have since declared devfs obsolete for the udev. It does the same task, it is just an updated devfs, and is userspace based, instead of being a part of the kernel. This means it is more secure.

The udev is extremely useful for devices which are hotswapable (can be plugged in and out while the system is on, like USB and the like). It will automatically oversee the creation, deletion, and permission management of the /dev file entries.

[edit] Some important devices

[edit] Audio

/dev/dsp
/dev/mixer 
represent the output stream for the Open Sound System drivers (OSS).

[edit] Clock

/dev/rtc 
the real time clock of the system

[edit] IDE Drives

(cdroms/dvdroms, harddrives)

/dev/cdroms/
/dev/ide/ 
udev/devfs directory of cdrom/ide drives on the system
/dev/fd 
floppy drive
/dev/hd* 
Various hard drives (see below for more information)

[edit] Mouse

/dev/psaux
/dev/mouse 
depending on your hardware, one of these usually represents a PS/2 mouse
/dev/input/mice
/dev/usbmouse 
if you use a usb mouse, this is where the device's input can be read from. the mice driver will take the input of all usb mice connected (ie, if you have 2 connected and move both, their combined sum of movements goes to mice)

[edit] SCSI/Serial Drives

(SCSI cdroms/harddrives, hotplugable storage media)

/dev/sd* 
explained below

[edit] Video

/dev/fb 
if you use a framebuffer, this is the device's entry. This is just for information.
/dev/nvidia0 
if you have an nvidia video card and use the nvidia's drivers, this is its device.

[edit] Miscellaneous

/dev/kmem
/dev/mem 
the entire contents of your computers memory.
/dev/null 
a "Do nothing" device. All data written to this device is ignored.
/dev/random
/dev/urandom 
the output of this device is random bits that are cryptographically secure.
/dev/zero 
all output bits are zero. Useful for wiping a device clean.

[edit] Explanation of /dev/hd's and /dev/sd's

Most computers have two IDE buses: the primary and the secondary. Each bus also allows for a master device and a slave device. This allows four IDE devices to be connected to the system. If you have ever installed a cdrom/dvdrom/harddrive, then you have already some knowledge of this.

There are also four /dev/hd entries, they are:

  • /dev/hda the primary bus's master
  • /dev/hdb the primary bus's slave
  • /dev/hdc the secondary bus's master
  • /dev/hdd the secondary bus's slave

So, if you did not know how your devices were mapped out in the system, you can gather this information from just knowing that. Sometimes if you have two IDE drives (ie, one harddrive and one cdrom drive) you may only see a /dev/hda and a /dev/hdc. This is because the computer manufacturer put the devices on different wires on your computer, which was a good idea since it means the two devices are not competing for one cable, but rather each has its own.

You can refer to the entire harddrive with the /dev/hd name. This is really powerful. However, you (and the system) also needs a way to refer to your partitions on that disk. So to do this, adding a number to the end of the disk refers you to the partition. For example:

/dev/hda1 is your first harddrive's first partition
/dev/hdc3 is your secondary bus's master drive's third partition.

The serial devices also follow the same pattern, except the a,b,c,d,etc naming usually happens based on the order that the system recognizes them. For example if you connect an external zip disk, then it'll be /dev/sda. If you connect a digital camera next, it will be /dev/sdb, and so forth.

Personal tools