17
Devices and Drivers Devices and Drivers (Chapter 7) (Chapter 7) Khattab Alhabashi Khattab Alhabashi UNIX System UNIX System Administration Administration

Devices and Drivers

Embed Size (px)

DESCRIPTION

Devices and Drivers

Citation preview

Page 1: Devices and Drivers

Devices and Drivers (Chapter Devices and Drivers (Chapter 7)7)

Khattab AlhabashiKhattab Alhabashi

UNIX System AdministrationUNIX System Administration

Page 2: Devices and Drivers

IntroductionIntroduction

Definition: A device driver is a program that manages the system’s Definition: A device driver is a program that manages the system’s interaction with a particular piece of hardware.interaction with a particular piece of hardware.

The driver translates between the hardware commands understood by The driver translates between the hardware commands understood by the device and the stylized programming interface used by the kernel.the device and the stylized programming interface used by the kernel.

The existence of the driver layer helps to keep UNIX reasonably The existence of the driver layer helps to keep UNIX reasonably device-independent.device-independent.

Device drivers are part of the kernel; they are not user processes. Device drivers are part of the kernel; they are not user processes. However, a driver can be accessed both from within the kernel and However, a driver can be accessed both from within the kernel and from user space.from user space.

User-level access to devices is provided through special device file User-level access to devices is provided through special device file that live in the /dev directory.that live in the /dev directory.

Page 3: Devices and Drivers

Introduction Introduction Contd.Contd.

Most devices in the system will fall into three categories: Most devices in the system will fall into three categories:

1) SCSI: 1) SCSI: – Devices that attach to a SCSI bus are easy to configure.Devices that attach to a SCSI bus are easy to configure.– Most systems have one driver that allows direct access to the SCSI Most systems have one driver that allows direct access to the SCSI

bus, plus additional drivers for special types of devices such as bus, plus additional drivers for special types of devices such as disks and tapes. disks and tapes.

2) Vendor:2) Vendor:– Most devices you can buy from your hardware vendor will already Most devices you can buy from your hardware vendor will already

be supported by the operating system.be supported by the operating system.– Unless you have explicitly removed drivers, the kernel will usually Unless you have explicitly removed drivers, the kernel will usually

recognize a new device as soon as you install it.recognize a new device as soon as you install it.– The appropriate device files in /dev will already have been created The appropriate device files in /dev will already have been created

for you, or the OS will create them on the fly.for you, or the OS will create them on the fly.

Page 4: Devices and Drivers

Introduction Introduction Contd.Contd.

3) Third-party:3) Third-party:– When you purchase a piece of hardware from a third party When you purchase a piece of hardware from a third party

(someone other than your hardware vendor), it will usually come (someone other than your hardware vendor), it will usually come with an installation script that installs the device, configures the with an installation script that installs the device, configures the kernel if necessary, and makes devices entries.kernel if necessary, and makes devices entries.

– On systems with loadable drivers, kernel configuration may not be On systems with loadable drivers, kernel configuration may not be necessary.necessary.

Page 5: Devices and Drivers

Device Numbers and Jump TablesDevice Numbers and Jump Tables

Device files are mapped to devices via their “major and minor device Device files are mapped to devices via their “major and minor device numbers”, values that are stored in the file’s inode structure.numbers”, values that are stored in the file’s inode structure.

The major device number identifies the driver that the file is The major device number identifies the driver that the file is associated with.associated with.

The minor device number identifies which particular device of a given The minor device number identifies which particular device of a given type is to be addressed. It is often called the unit number or type is to be addressed. It is often called the unit number or “instance” of the device.“instance” of the device.

There are two types of device files:There are two types of device files:– Block device files: it is read or written a block (a group of bytes, Block device files: it is read or written a block (a group of bytes,

usually multiple of 512) at a time.usually multiple of 512) at a time.– Character device files: it can be read or written one byte at a time.Character device files: it can be read or written one byte at a time.

Page 6: Devices and Drivers

Device Numbers and Jump Tables Device Numbers and Jump Tables Contd.Contd.

Some devices support access via both block and character device files.Some devices support access via both block and character device files.

Each driver has routines for performing some or all of the following Each driver has routines for performing some or all of the following functions:functions:

probeprobe attach open close read reset stop attach open close read reset stop

select strategy dump psize write timeoutselect strategy dump psize write timeout

process a transmit interruptprocess a transmit interrupt

process a receive interruptprocess a receive interrupt

ioctl (input / output control)ioctl (input / output control)

Inside the kernel, the addresses of these functions for each driver are Inside the kernel, the addresses of these functions for each driver are stored in a structure called a jump table.stored in a structure called a jump table.

Page 7: Devices and Drivers

Device Numbers and Jump Tables Device Numbers and Jump Tables ContdContd..

There are actually two tables:There are actually two tables:

- One for character device and - One for character device and

- One for block devices.- One for block devices.

The jump tables are indexed by major device numbers.The jump tables are indexed by major device numbers.

When a program performs an operation on a device file, the kernel When a program performs an operation on a device file, the kernel automatically catches the reference, looks up the appropriate function automatically catches the reference, looks up the appropriate function name in the jump table, and transfers control to it. name in the jump table, and transfers control to it.

To perform an unusual operation that doesn’t have a direct analog in To perform an unusual operation that doesn’t have a direct analog in filesystem model (for example, ejecting floppy disk), the ioctl system filesystem model (for example, ejecting floppy disk), the ioctl system call is used to pass a message directly from user space into the driver. call is used to pass a message directly from user space into the driver.

Page 8: Devices and Drivers

Device Numbers and Jump Tables Device Numbers and Jump Tables Contd.Contd.

Three examples were discussed in this chapter about adding device Three examples were discussed in this chapter about adding device drivers:drivers:

- Adding a BSD Device Driver: Adding a completely new device driver to - Adding a BSD Device Driver: Adding a completely new device driver to BSD machine involves adding it to a couple of configuration files and BSD machine involves adding it to a couple of configuration files and editing the kernel source code to include references to the driver’s editing the kernel source code to include references to the driver’s routine.routine.

- Adding an HP-UX Device Driver: On HP-UX systems, the jump tables - Adding an HP-UX Device Driver: On HP-UX systems, the jump tables are constructed from specifications in a text file called are constructed from specifications in a text file called master.master.

- Adding an IRIX Device Driver: IRIX uses a directory of files - Adding an IRIX Device Driver: IRIX uses a directory of files (/var/sysgen/master.d) to perform the same function as the single (/var/sysgen/master.d) to perform the same function as the single master file in HP-UX.master file in HP-UX.

Page 9: Devices and Drivers

Device Numbers and Jump Tables Device Numbers and Jump Tables Contd.Contd.

There are some common steps in all three examples that have to be There are some common steps in all three examples that have to be done after adding and modifying the files specified for each example:done after adding and modifying the files specified for each example:

- Building a new kernel.- Building a new kernel.

- Copying the old kernel aside and installing the new kernel.- Copying the old kernel aside and installing the new kernel.

- Rebooting and testing the new kernel.- Rebooting and testing the new kernel.

- Creating device files and test the device itself. - Creating device files and test the device itself.

Page 10: Devices and Drivers

Device FilesDevice Files

By convention, device files are kept in the By convention, device files are kept in the /dev/dev directory. directory.

ATT systems handle device files quite nicely by using separate ATT systems handle device files quite nicely by using separate subdirectory of subdirectory of /dev/dev for each type of device: for each type of device:

disk, tape, terminal, etcdisk, tape, terminal, etc..

Device files are created with the Device files are created with the mknodmknod command, which has the command, which has the syntax:syntax:

mknodmknod filename type major minorfilename type major minor

wherewhere

- - filenamefilename is the device file to be created. is the device file to be created.

- - typetype is is c c for a character device or for a character device or bb for a block device. for a block device.

- - majormajor and and minorminor are the major and minor device numbers. are the major and minor device numbers.

Page 11: Devices and Drivers

Device Files Device Files Contd.Contd. A shell script called MAKEDEV is sometimes provided (in /dev) to A shell script called MAKEDEV is sometimes provided (in /dev) to

automatically supply default values to automatically supply default values to mknod.mknod.

You need to scan through the script to find the arguments needed for You need to scan through the script to find the arguments needed for your device. For example, to make PTY entries on a SunOS system, your device. For example, to make PTY entries on a SunOS system, you would use the following command:you would use the following command:

# # cd /devcd /dev

## ./MAKEDEV pty ./MAKEDEV pty

Page 12: Devices and Drivers

Naming Conventions For DevicesNaming Conventions For Devices

Devices that have both block and character identities usuallyDevices that have both block and character identities usually

- preface the character device name with the letter r for “row” - preface the character device name with the letter r for “row” (e.g., /dev/sd0 and /dev/rsd0), or(e.g., /dev/sd0 and /dev/rsd0), or

- place it in a subdirectory with a name that starts with r - place it in a subdirectory with a name that starts with r

(e.g., /dev/dsk/dks0d3s0 vs. /dev/rdsk/dks0d3s0).(e.g., /dev/dsk/dks0d3s0 vs. /dev/rdsk/dks0d3s0).

Serial devices are usually named tty followed by a sequence of letters Serial devices are usually named tty followed by a sequence of letters that identify the interface the port is attached to (See chapter 8 for that identify the interface the port is attached to (See chapter 8 for more information more information

about serial ports).about serial ports).

Page 13: Devices and Drivers

Naming Conventions For Devices Naming Conventions For Devices Contd.Contd.

BSD disk names often begin with a two-letter abbreviation for either BSD disk names often begin with a two-letter abbreviation for either the drive of the controller, followed by the drive number and partition the drive of the controller, followed by the drive number and partition name.name.

- Example: - Example: sd0asd0a is the block device representing the partition of the is the block device representing the partition of the first disk drive on a SCSI controller; first disk drive on a SCSI controller; rsd0arsd0a is the corresponding is the corresponding character device.character device.

The names of tapes and devices often include not only a reference to The names of tapes and devices often include not only a reference to the drive itself, but also an indication of whether the device rewinds the drive itself, but also an indication of whether the device rewinds after each tape operation and the density at which it reads and writes.after each tape operation and the density at which it reads and writes.

Page 14: Devices and Drivers

Loadable Kernel ModulesLoadable Kernel Modules

Loadable modules allow device drivers to be linked into and removed Loadable modules allow device drivers to be linked into and removed from the kernel while it is running.from the kernel while it is running.

- This makes the installation of drivers much easier, since the kernel - This makes the installation of drivers much easier, since the kernel binary does not have to be changed.binary does not have to be changed.

- It also allows the kernel to be smaller, because drivers are not - It also allows the kernel to be smaller, because drivers are not loaded unless they are needed.loaded unless they are needed.

Loadable modules are implemented by providing one or more Loadable modules are implemented by providing one or more documents “hooks” into the kernel where additional device drivers can documents “hooks” into the kernel where additional device drivers can grab on.grab on.

A user-level command communicates with the kernel and tells it to A user-level command communicates with the kernel and tells it to load new modules into memory and to make entries for them in the load new modules into memory and to make entries for them in the system’s jump tables.system’s jump tables.

Page 15: Devices and Drivers

Loadable kernel Modules Loadable kernel Modules Contd.Contd.

Although loadable drivers are convenient, they are not entirely safe.Although loadable drivers are convenient, they are not entirely safe.

Any time you load or unload a module, you risk causing a kernel panic.Any time you load or unload a module, you risk causing a kernel panic.

Like other aspects of device and driver management, the Like other aspects of device and driver management, the implementation of loadable modules is operating system dependent.implementation of loadable modules is operating system dependent.

There are four of the example systems that support loadable modules:There are four of the example systems that support loadable modules:

- Solaris: In Solaris, virtually everything is a loadable module. The - Solaris: In Solaris, virtually everything is a loadable module. The modinfomodinfo command lists the currently-loaded modules. You can add a command lists the currently-loaded modules. You can add a driver with the driver with the add_drvadd_drv command. command.

Page 16: Devices and Drivers

Loadable Kernel Modules Loadable Kernel Modules Contd.Contd.

- HP-UX: Generic HP-UX does not support loadable modules, but there - HP-UX: Generic HP-UX does not support loadable modules, but there is an option that allows the loading of STREAMS modules.is an option that allows the loading of STREAMS modules.

- IRIX: IRIX 5.2 supports loadable modules; earlier versions did not. - IRIX: IRIX 5.2 supports loadable modules; earlier versions did not. Modules are manipulated with theModules are manipulated with the ml ml command. command. mlml with the with the listlist option catalogs the modules that the kernel is currently aware of.option catalogs the modules that the kernel is currently aware of.

- SunOS: SunOS versions 4.1.2 and later contain support for loadable - SunOS: SunOS versions 4.1.2 and later contain support for loadable modules. Currently-loaded modules can be listed with modules. Currently-loaded modules can be listed with modstatmodstat..

Page 17: Devices and Drivers

END OF CHAPTEREND OF CHAPTER

QUESTIONS???QUESTIONS???