Linux device driver basics - linux-device-driver

what is the difference between a device and a driver and how they are related ?
plz explain me in context of below diagram

A device is a general device like hardisk, network card etc.
Device driver is a piece of code written to interact with the device, in a more clear way to control the device. It tells how we are going to interact with a device.
The picture you mentioned is related to virtualization:
Qemu - is an Emulator means it make virtual CPU, NIC etc. so that Virtual machines can have their own CPU, NIC etc. Think of it like you don't have anything but you are creating an illusion that you have it,
As explained qemu will create emulated devices, now to operate on them we need some drivers. That is where virtio drivers comes into picture.
Virtio-driver: These driver are written to control the emulated devices.

Related

How drivers work out of the box in x86 and not in embedded computers like Android phone

I'm curious about how the drivers work out of the box in x86 motherboards, like display, USB controllers etc.
For example :
Booting a toy custom kernel in x86 can display to screen without doing any extra work on the drivers space, however for an Android phone which is an embedded system, it seems almost impossible to display to screen with my own toy custom kernel (as there is information out there available about the memory map of the device and how the display is interfaced with the device).
Why is that I/O works out of the box in x86 motherboards and doesn't on embedded computers?
x86 PC firmware has standard software interfaces (a lot like system calls), either modern UEFI or legacy BIOS int 0x10 and other interrupts.
The key point is that it's not just bare-metal x86, it's IBM PC-compatible which means software and even emulated legacy hardware like a PS/2 port, VGA, and even legacy interrupt controller.
If you didn't have all this help from firmware (for the benefit of bootloaders and toy OSes), you'd have a much harder job, e.g. needing at least a basic USB-hid and USB host-controller driver to get keyboard input. The lowest level function to handle a user input
Why is that I/O works out of the box in x86 motherboards and doesn't on embedded computers?
That's not your real question. Embedded machines have working I/O hardware, they just don't come with portable software APIs/ABIs wrapped around drivers as part of the firmware.
I think most vendor's SDK comes with functions to access the I/O hardware (after maybe doing some fiddling to get it into a usable state). i.e. to write your own driver for it.
Embedded doesn't need this in firmware because it's expected that the kernel will be customized for the hardware.
Wouldn't it be better to have a BIOS or UEFI for maximum portability? Does it have any drawbacks to include one?
Yes: code size in the boot ROM, and someone has to write + debug that code. This costs time and developer salary.
No point in booting up what's nearly an OS (a UEFI environment) just to load a kernel which is going to take over the HW anyway.
Also a downside in boot time: any code that runs other than loading the kernel where it wants to be is wasted CPU time that slows down the boot. Having a very lightweight interface that just lets you get your kernel loaded, and leaving all the I/O to it, makes sense for this.
Unlike x86 PCs, there's no expectation that you can use this hardware with an OS install disc / image you downloaded that isn't specifically customized for this hardware.
It's not intended to be easy for hobbyists to play with using training-wheels APIs. Real OSes on this hardware won't use such APIs so why provide them in the first place?

At boot time how OS determines all the hardware?

I have these related questions:
Does anybody know how an OS gets to know all hardware connected on the motherboard? (I guess this is called "Hardware Enumeration").
How does it determine what kind of hardware is residing at an specific IO address (i.e.: serial or parallel or whatever controller)?
How to code a system module which will do this job? (Assuming no OS loaded yet, just BIOS).
I know BIOS is just a validation and an user friendly interface to configure hardware at boot time with no real use after that for most modern OS's (win, Linux, etc). Besides I know that for the BIOS it should not be difficult to find all hardware because it is specifically tuned by the board manufacturer (who knows everything about it!). But for an OS or an application above BIOS that is a complete different story. Right?
Pre-PCI this was much more difficult, you needed a trick for each product, and even with that it was difficult to figure everything out. With usb and pci you can scan the busses to find a vendor and product id, from that you go into a product specific discovery (like the old days this can be difficult). Sometimes the details for that board are protected by NDA or worse you just dont get to know unless you work there on the right team.
The general approach is either based on detection (usb, pcie, etc vendor/product ids) you load a driver or write a driver for that family of product based on the documentation for that family of product. Since you mentioned BIOS, win, linux that implies X86 or a PC, and that pretty much covers the autodetectable. Beyond that you rely on the user who knows what hardware was installed in the system and a driver that came with it. or in some way you ask them what is installed (the specific printer at the end of a cable or out on the network if not auto detectable is an easy to understand example).
In short you take decades of experience in trying to succeed at this and apply it, and still fail from time to time since you are not in 100% control of all the hardware in the system, there are hundreds of vendors out there each doing their own thing.
BIOS enumerates the pci(e) for an x86 pc, for other platforms the OS might do it. The enumeration includes allocating address space for the device based on pci compliant rules. but within that address space you have to know how to program that specific board from vendor documentation if available.
Sorry my English is not very good.
Responding questions 1 and 2:
During the boot process, only the hardware modules strictly necessary to find and start the OS are loaded.
These hardware modules are: motherboard, hard drive, RAM, graphics card, keyboard, mouse, screen (that is detected by the graphics card), network card, CD / DVD, and a few extra peripherals such as USB units.
Each hardware module you connect to a computer has a controller that is like a small BIOS with all the information of the stored device: manufacturer, device type, protocols, etc
The detection process is very simple: the BIOS has hardcoded all the information about the motherboard, with all communication ports. At startup, the BIOS sends a signal to all system ports asking the questions "Who are you? What are you? How do you function?" and all attached devices answers by sending their information. In this way the computer knows how much RAM you have, if there is present a keyboard or a mouse, which storage devices are available, screen resolution, etc ...
Obviously, this process only works with the basic modules needed to boot the system, and does not work with complex peripherals that require specific drivers: printers, scanners, webcams ... all these complex peripherals are loaded by software once the OS has been charged.
Responding question 3:
If you wants to load a specific module during the boot, you must:
- Create a controller for this module.
- If the peripheral is too complex to load everything from the controller, you must to write all the proccess to control that module directly in the BIOS module, or reprogram the IPL to manage that specific module.
I hope I've helped
Actually, when you turn on your system, the BIOS starts the IPL (Initial program load) from ROM. For check the all connected devices are working good and also check the mandatory devices such as keyboard, CMOS, Hard disk and so on. If it is not success, it gives an error flag to the BIOS. The BIOS shows us the error through video devices.
If it is success, the control of boot is transferred to BIOS for further process.
The above all process are called POST (Power On Self Test).
Now the BIOS have the control. It checks all secondary memory devices for boot loader of the OS. If it hit, the bootloader's memory address is transferred to RAM.
The Ram started to execute the bootloader. Here only the os starts to run.
If the bootloader not hit, the bios shows us the boot failure error.
I hope your doubt clarified...

How does the OS interact with peripherals like sound cards/ video cards etc

As far as I understand it, any program gets compiled to a series of assembly instructions for the architecture it is running on. What I fail to understand is how the operating system interacts with peripherals such as a video card. Isn't the driver itself a series of assembly instructions for the CPU?
The only thing I can think think of is that it uses regions of memory that is then monitored by the peripheral or it uses the BUS to communicate operations and receive results. Is there a simple explanation to this process.
Sorry if this question is too general, it's something that's been bothering me.
You're basically right in your guess. Depending on the CPU architecture, peripherals might respond to "memory-mapped I/O" (where they watch for reads and writes to specific memory addresses), or to other specific I/O instructions (such as the x86 IN and OUT instructions).
Device drivers are OS-specific software, and provide an interface between the OS and the hardware.
A specific physical device either has hardware that knows how to respond to whatever signals from the CPU it monitors, or it has its own CPU and software that is often called firmware. The firmware of a device is not specific to any operating system and is usually stored in persistent memory on the device even after it is powered off. However, some peripherals might have firmware that is loaded by the device driver when the OS boots.
There are simple explanations and there are truthfull explanations - choose one!
I'll try a simple one: Along the assembly instructions, there are some, that are specialized to talk to peripherials. The hardware interprets them not by e.g. adding values in registers oder writing something to RAM, but by moving some data from a register or a region in RAM to a peripherial (or the other way round).
Inside the OS, the e.g. the sound driver is responsible for assembling some sound data along with some command data in RAM, and the OS then invokes the bus driver to issue these special instructions to move the command and data to the soundcard. The soundcard hardware will (hopefully) understand the command and interpret the data as sound it should play.

Beginner looking to write linux device driver (usb, pci). Suggestion on device?

I have been reading on linux kernel development and device drivers for a while. I feel ready to give it a go on a real piece of hardware. I would like to write a driver for a, preferably usb (otherwise pci), device for a desktop computer. But every device I seem to think of is already supported (including everything I own atm). So, would welcome any suggestions.
P.S. Willing to buy it, provided it's under £100 (150$).
Anything really practical has already been done out of necessity. My vote would be for something like http://www.amazon.com/Cheeky-Computer-Controlled-Missile-Launcher/dp/B004AIZV48/ref=pd_sim_t_1. It's fun, inexpensive, and currently Windows only.
The protocol should be pretty simple, but will give you good experience on debugging the USB channel in order to figure it out. And when you are done, you'll have a cool toy :)
USB-based devices are generally well supported at the kernel level. What this means is that u rarely have to write a device driver for each and every USB devices at the kernel. THis is because applications can easily use libusb (and several other userspace USB libraries) to talk to the device.
If you look into the USB code in the kernel, you can see that it is among the most complex implementation of all the hardware protocol, but it is also generic across different USB devices. I have done porting work for USB devices before, and trust me, libusb is good enough.
Check it out (for example):
http://libusb.sourceforge.net/doc/examples.html
You could port the Enttec Open DMX USB Interface driver to latest 3.x kernels.
git clone http://git.hbels.com/public/dmx_usb_module
libusb way sounds more appropriate to me too though.

about hardware drivers in protected mode

Recently, I'm trying to write a simple OS. This is a big project.
when I'm writing my code, I'm wondering how modern OS contact hardware under protected mode
In real mode, we can just call the bios interrupt to accomplish this job.
But I'm wondering how to accomplish this goal in protected mode.(Is it using in and out instruction??)
I traced some of the linux source code, but still can't find the appropriate code.
I know it is a basic question to many people, plz help me, tks.
and sorry about my poor English.
In protected mode, the CPU can run in either kernel mode or user mode. In kernel mode, you can always access the hardware. Calling BIOS interrupt is one old method, but a modern OS normally has its own device drivers for the hardware and does not call BIOS too often. If you know the hardware datasheet, you can use in, and out to access the hardware directly. Also, for modern PCI and PCI Express devices, they supported memory mapped IO (X86 CPU also supports this), and it means you can use mov to access the hardware.
For x86, the CPU also allows the user level program to access the hardware using in and out instructions. You can find it on Intel CPU manual. Just set DPL, CPL? (I forgot the correct name).
I guess you'd better read some book about device drivers, such as Linux Device Drivers, 3rd edition. http://lwn.net/Kernel/LDD3/