SlideShare a Scribd company logo
Linux For Embedded Systems
ForArabs
Ahmed ElArabawy
Course 102:
UnderstandingLinux
Lecture 25:
Devices & Device Drivers
What is a Device Driver ??
Hardware devices (processor, memory, storage, peripherals)
Initialization
(bootloader)
Operating System
(Linux Kernel)
Device drivers
ApplicationsUtilities
C LibraryOther Libraries
What is a Device Driver ?
• A device driver is a kernel module responsible for handling a
hardware device in order to isolate the operating system and
the user application from the details of this hardware device
• This way, the Linux Kernel and the user applications running
on top of it don’t have to know about the details of the
hardware device
• For example, the user application deals with a hard disk (of
any kind) and a flash drive the same way, although the internal
handling of them is completely different
• This leads to possibility for the introduction of new hardware
devices everyday, and the Linux Kernel can support them using
the proper device driver without the need to modify the
kernel
Correct but not Complete ….
• This definition represent only some of the device drivers and
not all of them
• device drivers are not limited to the support of hardware
devices
• Some device drivers perform a complete software function
and don’t perform any hardware device interaction
• Examples are the device drivers supporting these devices,
• Random number generator (/dev/random & /dev/urandom)
• Zeros generator (/dev/zero)
• Output Sink (/dev/null)
• We need to understand the more general definition of Devices
and Device Driver
The Extendable Architecture of Linux
• Linux kernel is under continuous development with thousands
of developers with different objectives
• It supports a lot of fancy and advanced features
• Not all of these features are required by all users
• It also has support for tons of hardware devices (printers,
scanners, cameras, storage devices, …. )
• To build the kernel with all of its features and hardware device
support will result in a very large image (unnecessarily) and
some times with conflicting functions
• With such a large image, Linux may not be able to run on a lot
of systems including embedded systems, and will need too
much resources (again unnecessarily)
The Extendable Architecture of Linux
• The solution for that problem, is that a lot of the advanced features, and
the hardware devices support functionality are considered optional
features
• When building the kernel, we go through a configuration phase, to set
for each optional feature,
• Enable: to build the code for that feature and include it in the kernel image
build in a static way
• Disable: To bypass the build the code for that feature, and hence the kernel
would not support it
• Module: The code is built as a separate module, resulting in a separate binary
(*.ko), which can be dynamically added/removed to/from the kernel at run
time on need basis
• Also, kernel modules can be developed and built independent of the
kernel build and then added to the kernel tree for future load/unload
• A lot of the kernel features can be built as a “Kernel Loadable Modules”
to keep the kernel image smaller, and to add the flexibility of adding or
removing them when needed
Kernel Loadable Module (KLM)
• A KLM is a binary file (*.ko) that can be created as an
additional step when building the Linux Kernel, or as a
completely separate procedure
• KLM’s reside in the Linux tree in,
/lib/modules/{kernel release}/
• KLM’s can be loaded or unloaded to/from the Linux Kernel at
run time (after the kernel has booted)
• Using KLM enables the following,
• No need to rebuild the kernel every time a new KLM is introduced
• No need to reboot the kernel every time we need to load/unload
a KLM
• The KLM runs inside the kernel
Loading Kernel Modules
• Kernel Modules can be activated as follows,
• Kernel modules can be loaded in the kernel on demand based on
user commands (we will go this later in this lecture)
• The Linux kernel supports automatic discovery of new hardware
devices, and this may result in the automatic load of the
associated kernel modules (using the udev subsystem in the
kernel). An example is when inserting a USB flash drive
• The Linux kernel has a list of kernel modules to load at startup
(via some startup scripts)
• Some Kernel Modules are needed in the boot process of the
Linux Kernel, and hence they are statically built in the kernel
• Other modules are necessary for system operation, and it is
decided to build them statically in the kernel (they can not be
unloaded, since they are becoming part of the kernel image)
Selecting Modules to Start at Boot
(/etc/modules)
• The file /etc/modules will contain the list of modules to start
at kernel startup
KLM Utilities
Inserting a Module
(insmod Command)
$ insmod <module file>
$ insmod <module file> <arguments>
• This command inserts a module in the kernel
$ sudo insmod /lib/module/`uname –r`/my-driver.ko
• Note that the module full path needs to be specified
• You can pass parameters to the module, these parameters are
processed by the module code
$ sudo insmod /lib/module/`uname –r`/my-device.ko debug_enable=1
• Sometimes, a kernel module would require another module to
be loaded first, the user has to take care of this dependency,
otherwise, the insmod command may fail
Removing a Module
(rmmod Command)
$ rmmod <kernel module>
• This command removes the kernel module from the kernel
$ sudo rmmod my-driver
• Since we are addressing now the kernel module inside the kernel
and not its binary file inside the Linux tree, no path is specified.
Just the module name
Load/Unload a Module
(modprobe Command)
$ modprobe <kernel module name >
$ modprobe -r <kernel module name>
• This command replaces both insmod and rmmod Commands
$ sudo modprobe my-driver
$ sudo modprobe –r my-driver
• It is a much richer command, because,
• It has the capability of understanding the module dependencies, and load
any modules needed (if not already loaded) before loading the desired
module
• When removing a module, any dependencies that are not used by other
modules, will be removed as well
• It also has a means to find the modules, so we don’t need to specify the full
path of the module file. The module name should be enough
• The modprobe command also performs other special features such as
setting default parameter options in its configuration files, and using
these options when loading the specified module
• Default module parameters are specified in,
• /etc/modprobe.conf
• /etc/modprobe.d/*.conf
Listing the Loaded KLMs
(lsmod Command)
$ lsmod
Displaying Kernel Module info
(modinfo Command)
$ modinfo <kernel module name>
• This command shows information about the module
$ modinfo my-driver
• This includes,
• Full path for the .ko file
• Author
• License type
• Description of the module function
• Valid parameters
• Dependencies on other modules
modinfo
What are Device Drivers ??
• A device driver is a kernel module
• It can be either built statically in the kernel image, or loaded
dynamically
• Automatically loaded via udev
• On demand via user commands (modprobe)
• At boot time via startup scripts
• The device driver registers itself to the system with a number
(called the major number)
• The device driver runs in the kernel and performs the required
functions (interacts with hardware interrupts, communicate
with hardware devices, access Linux Kernel structures,
communicate with other device drivers, …etc)
• The device driver supports one or more devices
What are Devices ??
• A device is a special file (also called device node) representation
located in /dev
• The file is not a real file. It does not reside on the disk, it is just a
place holder (that just lives in memory)
• Each device binds to a specific device driver at device creation time
• It is just an abstraction to enable the user plane to access the device
driver as if it is normal file
• It is the way the Linux kernel uses to establish the statement
“Everything is a file”, so you can,
• Request information from the device driver by reading from the
device file
• Send information to it by writing to the device file
• One or more devices can bind with the same device driver (they
must have the same Major Number), but each one has to pick a
unique number (called the Minor Number)
Major & Minor Numbers
• Each Device will have a pair of numbers,
• Major Number:
• This is the number that identify the device driver associated with this
device
• The Major number is reserved and registered by the device driver
when it is loaded in the kernel
• Minor Number:
• This number is associated with the device file
• It is the way to differentiate the different device files that are
associated with the same device driver
• It is also the way that the device driver file to know which device is
communicating with it
Major & Minor Numbers
Device Classification
• Two main classes of devices:
• Character devices
• Serial streams of data
• Read and Write operations are done in a serial manner
• Examples:
• Keyboard
• Mouse
• /dev/ones
• /dev/tty1
• /dev/ttyS1
• Represented by a file with type “c”
• Most common devices
• Block devices
• Read and Write are performed in Blocks
• Blocks are addressable, and hence can read in a non sequential way
• Examples are:
• Storage devices (hard disk, flash)
• /dev/sda1
• Represented by a file with type “b”
Device Classification
Hardware Device,DeviceDriver,& DeviceFile
Hardware
Kernel
User Plane
Creating a Device file
(mknod Command)
$ mknod <device file name> <c|b> <Major> <Minor>
$ mknod -m <permissions> <device file name> <c|b> <Major> <Minor>
• This command creates a device file
• The device file name is the file to be created. It should be located in /dev
• The device class is either a “Character” or “Block” device
• The Major Number is the number for the device driver to attach to
• The Minor Number is the number for this device file (it should be unique
within all devices attached to that device driver)
• You can deal with the device as a normal file
• It can be read only, write only, or read-write
• You can read from the file using like any file (use cat, or input redirection)
• You can write to a device file like any file (for example use echo or output
redirection)
• To create a device node
$ mknod /dev/my-new-device c 235 1
$ mknod -m 666 /dev/my-new-device c 235 1
Examples of Commonly
Used Devices
SCSI Disk Drive Devices
• All SCSI Disk drive devices (such as those used for SCSI Hard-
disks) are attached to the sd driver which registers the Major
Number of 8
• The naming of the device files:
• /dev/sd[a-z][1-15]
• The number at the end is the partition number
• The device file without a number refers to the whole disk
TTY Terminals & Serial Interfaces
• All Linux virtual terminals have devices names
• /dev/tty[n] (example /dev/tty1 )
• All Linux Serial Interfaces use the device names
• /dev/ttyS[n] (example /dev/ttyS1)
Device Utilities
Configuration of Serial Interfaces
(setserial Command)
$ setserial <options> <serial device file> <parameters to set>
• This command is used to read/write configuration of serial
ports
• This includes,
• Setting the I/O port used by the serial interface
• Setting the IRQ channel used by the serial interface
• Baud rate
• Other info
• Example:
$ sudo setserial -a /dev/ttyS1
$ sudo setserial /dev/ttyS1 baud_base 115200
Listing USB Hardware Devices
(lsusb Command)
$ lsusb
• This command lists the USB Hardware devices and information
about them
$ lsusb (List USB Devices)
$ lsusb -a (List USB Devices with all information about them)
$ lsusb -t (List USB Devices in a tree hierarchy structure)
Listing PCI Hardware Devices
(lspci Command)
$ lspci
• This command lists the PCI Hardware devices and information
about them
$ lspci
$ lspci -tv (Use tree hierarchical Structure with detailed info)
Copy And Convert data
(dd Command)
$ dd if=<source> of=<destination> <options>
• This command copies data from one file, perform any needed
conversions, and store it an another file
• It is a very useful command specially that it can use device
files as either source, destination, or both
• Be cautious when using this command. It can wipe a whole
partition or drive on your machine
• Example:
$ dd if=/dev/urandom of=~/random-data-file bs=4 count=1000
$ dd if=/dev/sr0 of=/my-file.iso bs=2048 conv=noerror,sync
$ dd if=/dev/sda of=~/disk.img
$ dd if=/dev/sda of=/dev/sdb
Dump of a File
(od Command)
$ od <Options> <file>
• This command dumps files in different formats
• Examples:
$ od -x file.img (output the file in Hexadecimal format)
$ od -s file.img (output the file in decimal format)
http://Linux4EmbeddedSystems.com

More Related Content

PDF
Course 102: Lecture 20: Networking In Linux (Basic Concepts)
PDF
Course 102: Lecture 9: Input Output Internals
PDF
Course 102: Lecture 26: FileSystems in Linux (Part 1)
PPTX
U-Boot Porting on New Hardware
PDF
Course 102: Lecture 10: Learning About the Shell
PPT
Basic 50 linus command
PDF
File System Hierarchy
PDF
Course 102: Lecture 14: Users and Permissions
Course 102: Lecture 20: Networking In Linux (Basic Concepts)
Course 102: Lecture 9: Input Output Internals
Course 102: Lecture 26: FileSystems in Linux (Part 1)
U-Boot Porting on New Hardware
Course 102: Lecture 10: Learning About the Shell
Basic 50 linus command
File System Hierarchy
Course 102: Lecture 14: Users and Permissions

What's hot (20)

PDF
Course 102: Lecture 18: Process Life Cycle
PPTX
Linux Kernel Booting Process (1) - For NLKB
PDF
Course 102: Lecture 17: Process Monitoring
PDF
Course 102: Lecture 2: Unwrapping Linux
PDF
Course 101: Lecture 5: Linux & GNU
PDF
Course 102: Lecture 12: Basic Text Handling
PDF
Course 102: Lecture 4: Using Wild Cards
PDF
Course 102: Lecture 28: Virtual FileSystems
PDF
Course 102: Lecture 7: Simple Utilities
PDF
Course 102: Lecture 24: Archiving and Compression of Files
PDF
Linux Systems: Getting started with setting up an Embedded platform
PDF
Jagan Teki - U-boot from scratch
PPTX
Linux User Management
PPTX
Swap Administration in linux platform
PPT
U boot porting guide for SoC
PDF
Kernel Module Programming
PDF
I2c drivers
PDF
Course 102: Lecture 3: Basic Concepts And Commands
PDF
Building Embedded Linux Systems Introduction
Course 102: Lecture 18: Process Life Cycle
Linux Kernel Booting Process (1) - For NLKB
Course 102: Lecture 17: Process Monitoring
Course 102: Lecture 2: Unwrapping Linux
Course 101: Lecture 5: Linux & GNU
Course 102: Lecture 12: Basic Text Handling
Course 102: Lecture 4: Using Wild Cards
Course 102: Lecture 28: Virtual FileSystems
Course 102: Lecture 7: Simple Utilities
Course 102: Lecture 24: Archiving and Compression of Files
Linux Systems: Getting started with setting up an Embedded platform
Jagan Teki - U-boot from scratch
Linux User Management
Swap Administration in linux platform
U boot porting guide for SoC
Kernel Module Programming
I2c drivers
Course 102: Lecture 3: Basic Concepts And Commands
Building Embedded Linux Systems Introduction
Ad

Viewers also liked (19)

PDF
Embedded Systems: Lecture 7: Unwrapping the Raspberry Pi
PDF
C 102 lec_29_what_s_next
PDF
Course 102: Lecture 22: Package Management
PDF
Embedded Systems: Lecture 4: Selecting the Proper RTOS
PDF
Embedded Systems: Lecture 12: Introduction to Git & GitHub (Part 3)
PDF
Embedded Systems: Lecture 10: Introduction to Git & GitHub (Part 1)
PDF
Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi
PDF
Embedded Systems: Lecture 8: Lab 1: Building a Raspberry Pi Based WiFi AP
PDF
Course 102: Lecture 27: FileSystems in Linux (Part 2)
PDF
Embedded Systems: Lecture 11: Introduction to Git & GitHub (Part 2)
PDF
Embedded Systems: Lecture 1: Course Overview
PDF
Course 102: Lecture 8: Composite Commands
PDF
Course 102: Lecture 5: File Handling Internals
PDF
Embedded Systems: Lecture 14: Introduction to GNU Toolchain (Binary Utilities)
PDF
Course 102: Lecture 11: Environment Variables
PDF
Course 102: Lecture 13: Regular Expressions
PPT
SYSTEM SOFTWARE
PPTX
The TCP/IP Stack in the Linux Kernel
PPTX
Hardware & Software
Embedded Systems: Lecture 7: Unwrapping the Raspberry Pi
C 102 lec_29_what_s_next
Course 102: Lecture 22: Package Management
Embedded Systems: Lecture 4: Selecting the Proper RTOS
Embedded Systems: Lecture 12: Introduction to Git & GitHub (Part 3)
Embedded Systems: Lecture 10: Introduction to Git & GitHub (Part 1)
Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi
Embedded Systems: Lecture 8: Lab 1: Building a Raspberry Pi Based WiFi AP
Course 102: Lecture 27: FileSystems in Linux (Part 2)
Embedded Systems: Lecture 11: Introduction to Git & GitHub (Part 2)
Embedded Systems: Lecture 1: Course Overview
Course 102: Lecture 8: Composite Commands
Course 102: Lecture 5: File Handling Internals
Embedded Systems: Lecture 14: Introduction to GNU Toolchain (Binary Utilities)
Course 102: Lecture 11: Environment Variables
Course 102: Lecture 13: Regular Expressions
SYSTEM SOFTWARE
The TCP/IP Stack in the Linux Kernel
Hardware & Software
Ad

Similar to Course 102: Lecture 25: Devices and Device Drivers (20)

PPTX
Device Drivers and Running Modules
PPT
Linuxdd[1]
PPTX
Linux device drivers
PPT
Driver_linux
PPT
lecture_1_introduction_linux_1234567.ppt
PPT
lecture_1_introduction.ppt
PPT
Device drivers tsp
PPT
Linux Device Driver for Writing a real world driver for embedded Linux
PPT
Linux Device Driver,LDD,
PPT
linux device driver
PDF
Studienarb linux kernel-dev
PPTX
Device Drivers
PPT
Linux Kernel Development
PDF
Linux kernel modules
PPTX
Linux Device Driver’s
PPT
Embedded system - embedded system programming
PPT
“black boxes” that make a particular p..
PDF
brief intro to Linux device drivers
PDF
Embedded Android : System Development - Part II (Linux device drivers)
PPTX
LINUX M1 P4 notes.pptxgyfdes e4e4e54v 4
Device Drivers and Running Modules
Linuxdd[1]
Linux device drivers
Driver_linux
lecture_1_introduction_linux_1234567.ppt
lecture_1_introduction.ppt
Device drivers tsp
Linux Device Driver for Writing a real world driver for embedded Linux
Linux Device Driver,LDD,
linux device driver
Studienarb linux kernel-dev
Device Drivers
Linux Kernel Development
Linux kernel modules
Linux Device Driver’s
Embedded system - embedded system programming
“black boxes” that make a particular p..
brief intro to Linux device drivers
Embedded Android : System Development - Part II (Linux device drivers)
LINUX M1 P4 notes.pptxgyfdes e4e4e54v 4

More from Ahmed El-Arabawy (8)

PDF
Course 102: Lecture 19: Using Signals
PDF
Course 102: Lecture 16: Process Management (Part 2)
PDF
Course 102: Lecture 6: Seeking Help
PDF
Course 102: Lecture 1: Course Overview
PDF
Course 101: Lecture 6: Installing Ubuntu
PDF
Course 101: Lecture 4: A Tour in RTOS Land
PDF
Course 101: Lecture 2: Introduction to Operating Systems
PDF
Course 101: Lecture 1: Introduction to Embedded Systems
Course 102: Lecture 19: Using Signals
Course 102: Lecture 16: Process Management (Part 2)
Course 102: Lecture 6: Seeking Help
Course 102: Lecture 1: Course Overview
Course 101: Lecture 6: Installing Ubuntu
Course 101: Lecture 4: A Tour in RTOS Land
Course 101: Lecture 2: Introduction to Operating Systems
Course 101: Lecture 1: Introduction to Embedded Systems

Recently uploaded (20)

PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Approach and Philosophy of On baking technology
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Encapsulation theory and applications.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Empathic Computing: Creating Shared Understanding
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
cuic standard and advanced reporting.pdf
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Encapsulation_ Review paper, used for researhc scholars
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Approach and Philosophy of On baking technology
Network Security Unit 5.pdf for BCA BBA.
Diabetes mellitus diagnosis method based random forest with bat algorithm
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Encapsulation theory and applications.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Advanced methodologies resolving dimensionality complications for autism neur...
20250228 LYD VKU AI Blended-Learning.pptx
Empathic Computing: Creating Shared Understanding
Unlocking AI with Model Context Protocol (MCP)
cuic standard and advanced reporting.pdf
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Dropbox Q2 2025 Financial Results & Investor Presentation
Review of recent advances in non-invasive hemoglobin estimation
Encapsulation_ Review paper, used for researhc scholars

Course 102: Lecture 25: Devices and Device Drivers

  • 1. Linux For Embedded Systems ForArabs Ahmed ElArabawy Course 102: UnderstandingLinux
  • 2. Lecture 25: Devices & Device Drivers
  • 3. What is a Device Driver ?? Hardware devices (processor, memory, storage, peripherals) Initialization (bootloader) Operating System (Linux Kernel) Device drivers ApplicationsUtilities C LibraryOther Libraries
  • 4. What is a Device Driver ? • A device driver is a kernel module responsible for handling a hardware device in order to isolate the operating system and the user application from the details of this hardware device • This way, the Linux Kernel and the user applications running on top of it don’t have to know about the details of the hardware device • For example, the user application deals with a hard disk (of any kind) and a flash drive the same way, although the internal handling of them is completely different • This leads to possibility for the introduction of new hardware devices everyday, and the Linux Kernel can support them using the proper device driver without the need to modify the kernel
  • 5. Correct but not Complete …. • This definition represent only some of the device drivers and not all of them • device drivers are not limited to the support of hardware devices • Some device drivers perform a complete software function and don’t perform any hardware device interaction • Examples are the device drivers supporting these devices, • Random number generator (/dev/random & /dev/urandom) • Zeros generator (/dev/zero) • Output Sink (/dev/null) • We need to understand the more general definition of Devices and Device Driver
  • 6. The Extendable Architecture of Linux • Linux kernel is under continuous development with thousands of developers with different objectives • It supports a lot of fancy and advanced features • Not all of these features are required by all users • It also has support for tons of hardware devices (printers, scanners, cameras, storage devices, …. ) • To build the kernel with all of its features and hardware device support will result in a very large image (unnecessarily) and some times with conflicting functions • With such a large image, Linux may not be able to run on a lot of systems including embedded systems, and will need too much resources (again unnecessarily)
  • 7. The Extendable Architecture of Linux • The solution for that problem, is that a lot of the advanced features, and the hardware devices support functionality are considered optional features • When building the kernel, we go through a configuration phase, to set for each optional feature, • Enable: to build the code for that feature and include it in the kernel image build in a static way • Disable: To bypass the build the code for that feature, and hence the kernel would not support it • Module: The code is built as a separate module, resulting in a separate binary (*.ko), which can be dynamically added/removed to/from the kernel at run time on need basis • Also, kernel modules can be developed and built independent of the kernel build and then added to the kernel tree for future load/unload • A lot of the kernel features can be built as a “Kernel Loadable Modules” to keep the kernel image smaller, and to add the flexibility of adding or removing them when needed
  • 8. Kernel Loadable Module (KLM) • A KLM is a binary file (*.ko) that can be created as an additional step when building the Linux Kernel, or as a completely separate procedure • KLM’s reside in the Linux tree in, /lib/modules/{kernel release}/ • KLM’s can be loaded or unloaded to/from the Linux Kernel at run time (after the kernel has booted) • Using KLM enables the following, • No need to rebuild the kernel every time a new KLM is introduced • No need to reboot the kernel every time we need to load/unload a KLM • The KLM runs inside the kernel
  • 9. Loading Kernel Modules • Kernel Modules can be activated as follows, • Kernel modules can be loaded in the kernel on demand based on user commands (we will go this later in this lecture) • The Linux kernel supports automatic discovery of new hardware devices, and this may result in the automatic load of the associated kernel modules (using the udev subsystem in the kernel). An example is when inserting a USB flash drive • The Linux kernel has a list of kernel modules to load at startup (via some startup scripts) • Some Kernel Modules are needed in the boot process of the Linux Kernel, and hence they are statically built in the kernel • Other modules are necessary for system operation, and it is decided to build them statically in the kernel (they can not be unloaded, since they are becoming part of the kernel image)
  • 10. Selecting Modules to Start at Boot (/etc/modules) • The file /etc/modules will contain the list of modules to start at kernel startup
  • 12. Inserting a Module (insmod Command) $ insmod <module file> $ insmod <module file> <arguments> • This command inserts a module in the kernel $ sudo insmod /lib/module/`uname –r`/my-driver.ko • Note that the module full path needs to be specified • You can pass parameters to the module, these parameters are processed by the module code $ sudo insmod /lib/module/`uname –r`/my-device.ko debug_enable=1 • Sometimes, a kernel module would require another module to be loaded first, the user has to take care of this dependency, otherwise, the insmod command may fail
  • 13. Removing a Module (rmmod Command) $ rmmod <kernel module> • This command removes the kernel module from the kernel $ sudo rmmod my-driver • Since we are addressing now the kernel module inside the kernel and not its binary file inside the Linux tree, no path is specified. Just the module name
  • 14. Load/Unload a Module (modprobe Command) $ modprobe <kernel module name > $ modprobe -r <kernel module name> • This command replaces both insmod and rmmod Commands $ sudo modprobe my-driver $ sudo modprobe –r my-driver • It is a much richer command, because, • It has the capability of understanding the module dependencies, and load any modules needed (if not already loaded) before loading the desired module • When removing a module, any dependencies that are not used by other modules, will be removed as well • It also has a means to find the modules, so we don’t need to specify the full path of the module file. The module name should be enough • The modprobe command also performs other special features such as setting default parameter options in its configuration files, and using these options when loading the specified module • Default module parameters are specified in, • /etc/modprobe.conf • /etc/modprobe.d/*.conf
  • 15. Listing the Loaded KLMs (lsmod Command) $ lsmod
  • 16. Displaying Kernel Module info (modinfo Command) $ modinfo <kernel module name> • This command shows information about the module $ modinfo my-driver • This includes, • Full path for the .ko file • Author • License type • Description of the module function • Valid parameters • Dependencies on other modules
  • 18. What are Device Drivers ?? • A device driver is a kernel module • It can be either built statically in the kernel image, or loaded dynamically • Automatically loaded via udev • On demand via user commands (modprobe) • At boot time via startup scripts • The device driver registers itself to the system with a number (called the major number) • The device driver runs in the kernel and performs the required functions (interacts with hardware interrupts, communicate with hardware devices, access Linux Kernel structures, communicate with other device drivers, …etc) • The device driver supports one or more devices
  • 19. What are Devices ?? • A device is a special file (also called device node) representation located in /dev • The file is not a real file. It does not reside on the disk, it is just a place holder (that just lives in memory) • Each device binds to a specific device driver at device creation time • It is just an abstraction to enable the user plane to access the device driver as if it is normal file • It is the way the Linux kernel uses to establish the statement “Everything is a file”, so you can, • Request information from the device driver by reading from the device file • Send information to it by writing to the device file • One or more devices can bind with the same device driver (they must have the same Major Number), but each one has to pick a unique number (called the Minor Number)
  • 20. Major & Minor Numbers • Each Device will have a pair of numbers, • Major Number: • This is the number that identify the device driver associated with this device • The Major number is reserved and registered by the device driver when it is loaded in the kernel • Minor Number: • This number is associated with the device file • It is the way to differentiate the different device files that are associated with the same device driver • It is also the way that the device driver file to know which device is communicating with it
  • 21. Major & Minor Numbers
  • 22. Device Classification • Two main classes of devices: • Character devices • Serial streams of data • Read and Write operations are done in a serial manner • Examples: • Keyboard • Mouse • /dev/ones • /dev/tty1 • /dev/ttyS1 • Represented by a file with type “c” • Most common devices • Block devices • Read and Write are performed in Blocks • Blocks are addressable, and hence can read in a non sequential way • Examples are: • Storage devices (hard disk, flash) • /dev/sda1 • Represented by a file with type “b”
  • 25. Creating a Device file (mknod Command) $ mknod <device file name> <c|b> <Major> <Minor> $ mknod -m <permissions> <device file name> <c|b> <Major> <Minor> • This command creates a device file • The device file name is the file to be created. It should be located in /dev • The device class is either a “Character” or “Block” device • The Major Number is the number for the device driver to attach to • The Minor Number is the number for this device file (it should be unique within all devices attached to that device driver) • You can deal with the device as a normal file • It can be read only, write only, or read-write • You can read from the file using like any file (use cat, or input redirection) • You can write to a device file like any file (for example use echo or output redirection) • To create a device node $ mknod /dev/my-new-device c 235 1 $ mknod -m 666 /dev/my-new-device c 235 1
  • 27. SCSI Disk Drive Devices • All SCSI Disk drive devices (such as those used for SCSI Hard- disks) are attached to the sd driver which registers the Major Number of 8 • The naming of the device files: • /dev/sd[a-z][1-15] • The number at the end is the partition number • The device file without a number refers to the whole disk
  • 28. TTY Terminals & Serial Interfaces • All Linux virtual terminals have devices names • /dev/tty[n] (example /dev/tty1 ) • All Linux Serial Interfaces use the device names • /dev/ttyS[n] (example /dev/ttyS1)
  • 30. Configuration of Serial Interfaces (setserial Command) $ setserial <options> <serial device file> <parameters to set> • This command is used to read/write configuration of serial ports • This includes, • Setting the I/O port used by the serial interface • Setting the IRQ channel used by the serial interface • Baud rate • Other info • Example: $ sudo setserial -a /dev/ttyS1 $ sudo setserial /dev/ttyS1 baud_base 115200
  • 31. Listing USB Hardware Devices (lsusb Command) $ lsusb • This command lists the USB Hardware devices and information about them $ lsusb (List USB Devices) $ lsusb -a (List USB Devices with all information about them) $ lsusb -t (List USB Devices in a tree hierarchy structure)
  • 32. Listing PCI Hardware Devices (lspci Command) $ lspci • This command lists the PCI Hardware devices and information about them $ lspci $ lspci -tv (Use tree hierarchical Structure with detailed info)
  • 33. Copy And Convert data (dd Command) $ dd if=<source> of=<destination> <options> • This command copies data from one file, perform any needed conversions, and store it an another file • It is a very useful command specially that it can use device files as either source, destination, or both • Be cautious when using this command. It can wipe a whole partition or drive on your machine • Example: $ dd if=/dev/urandom of=~/random-data-file bs=4 count=1000 $ dd if=/dev/sr0 of=/my-file.iso bs=2048 conv=noerror,sync $ dd if=/dev/sda of=~/disk.img $ dd if=/dev/sda of=/dev/sdb
  • 34. Dump of a File (od Command) $ od <Options> <file> • This command dumps files in different formats • Examples: $ od -x file.img (output the file in Hexadecimal format) $ od -s file.img (output the file in decimal format)