pengenalan linux device driver


In a Unix system, several concurrent processes attend to different tasks. Each process
asks for system resources, be it computing power, memory, network connectivity, or
some other resource. The kernel is the big chunk of executable code in charge of han-
dling all such requests. Although the distinction between the different kernel tasks
isn’t always clearly marked, the kernel’s role can be split into the following parts:



Process management

The kernel is in charge of creating and destroying processes and handling their
connection to the outside world (input and output). Communication among dif-
ferent processes (through signals, pipes, or interprocess communication primi-
tives) is basic to the overall system functionality and is also handled by the
kernel. In addition, the scheduler, which controls how processes share the CPU,
is part of process management. More generally, the kernel’s process manage-
ment activity implements the abstraction of several processes on top of a single
CPU or a few of them.

Memory management

The computer’s memory is a major resource, and the policy used to deal with it
is a critical one for system performance. The kernel builds up a virtual address-
ing space for any and all processes on top of the limited available resources. The
different parts of the kernel interact with the memory-management subsystem
through a set of function calls, ranging from the simple malloc/free pair to much
more complex functionalities.

Filesystems

Unix is heavily based on the filesystem concept; almost everything in Unix can
be treated as a file. The kernel builds a structured filesystem on top of unstruc-
tured hardware, and the resulting file abstraction is heavily used throughout the
whole system. In addition, Linux supports multiple filesystem types, that is, dif-
ferent ways of organizing data on the physical medium. For example, disks may
be formatted with the Linux-standard ext3 filesystem, the commonly used FAT
filesystem or several others.

Device control

Almost every system operation eventually maps to a physical device. With the
exception of the processor, memory, and a very few other entities, any and all
device control operations are performed by code that is specific to the device
being addressed. That code is called a device driver. The kernel must have
embedded in it a device driver for every peripheral present on a system, from the
hard drive to the keyboard and the tape drive. This aspect of the kernel’s functions is our primary interest in this book.

Networking

Networking must be managed by the operating system, because most network
operations are not specific to a process: incoming packets are asynchronous
events. The packets must be collected, identified, and dispatched before a pro-
cess takes care of them. The system is in charge of delivering data packets across
program and network interfaces, and it must control the execution of programs
according to their network activity. Additionally, all the routing and address res-
olution issues are implemented within the kernel.

Begitulah isi dari buku linux device driver 3rd edition, sebagai pengantar kita ke  linux device driver, di antara kernel task di atas, untuk device driver pembahasannya tentu akan menyangkut di device control dan juga ke networking (dalam hal ini network interface). Device driver sendiri dalam linux terbagi jadi 3 yaitu :

Character device
A character (char) device is one that can be accessed as a stream of bytes (like a
file); a char driver is in charge of implementing this behavior. Such a driver usu-
ally implements at least the open, close, read, and write system calls. The text
console (/dev/console) and the serial ports (/dev/ttyS0 and friends) are examples
of char devices, as they are well represented by the stream abstraction. Char
devices are accessed by means of filesystem nodes, such as /dev/tty1 and /dev/lp0.
The only relevant difference between a char device and a regular file is that you
can always move back and forth in the regular file, whereas most char devices
are just data channels, which you can only access sequentially. There exist,
nonetheless, char devices that look like data areas, and you can move back and
forth in them; for instance, this usually applies to frame grabbers, where the
applications can access the whole acquired image using mmap or lseek.
 Block devices
Like char devices, block devices are accessed by filesystem nodes in the /dev
directory. A block device is a device (e.g., a disk) that can host a filesystem. In
most Unix systems, a block device can only handle I/O operations that transfer
one or more whole blocks, which are usually 512 bytes (or a larger power of
two) bytes in length. Linux, instead, allows the application to read and write a
block device like a char device—it permits the transfer of any number of bytes at
a time. As a result, block and char devices differ only in the way data is managed
internally by the kernel, and thus in the kernel/driver software interface. Like a
char device, each block device is accessed through a filesystem node, and the dif-
ference between them is transparent to the user. Block drivers have a completely
different interface to the kernel than char drivers.
 Network interface
Any network transaction is made through an interface, that is, a device that is
able to exchange data with other hosts. Usually, an interface is a hardware
device, but it might also be a pure software device, like the loopback interface. A
network interface is in charge of sending and receiving data packets, driven by
the network subsystem of the kernel, without knowing how individual transac-
tions map to the actual packets being transmitted. Many network connections
(especially those using TCP) are stream-oriented, but network devices are, usu-
ally, designed around the transmission and receipt of packets. A network driver
knows nothing about individual connections; it only handles packets.
Not being a stream-oriented device, a network interface isn’t easily mapped to a
node in the filesystem, as /dev/tty1 is. The Unix way to provide access to inter-
faces is still by assigning a unique name to them (such as eth0 ), but that name
doesn’t have a corresponding entry in the filesystem. Communication between
the kernel and a network device driver is completely different from that used
with char and block drivers. Instead of read and write, the kernel calls functions
related to packet transmission.

 Kode device driver di linux bisa di install dan uninstall pada saat runtime atau linux sedang berjalan, dan kode inilah yang dinamakan module.

module bisa di install dengan insmod dan uninstall dengan rmmod, ada juga modprobe untuk menginstall (versi pintar dari insmod). Kode module ditulis dalam bahasa C dan di compile menggunakan Makefile (pastikan ejaan Makefile persis sama sebagai berikut). Bentuk paling simpel (hello world) dari module sebagai berikut :


module hanya bisa memakai header dari kernel yang memang khusus untuk module. MODULE_LICENSE wajib ada, optionnya ada : Dual BSD/GPL, GPL GPL v2, Dual MPL/GPL, Proprietary. yang paling umum biasanya Dual BSD/GPL dan GPL. module_init dan module_exit untuk menentukan fungsi initialisasi dan fungsi releasenya, jadi fungsi yang di tunjuk oleh module_init adalah fungsi yang akan jalan ketika module di install dan module_exit ketika module tepat sebelum di uninstall

printk itu seperti printf versi module, karena header luar tidak bisa di pakai, maka kernel sudah menyiapkan printfnya sendiri, KERN_ALERT di situ bukan typo tapi memang format penulisannya seperti itu, selain KERN_ALERT ada KERN_INFO, KERN_ERR dan lain-lain. _init dan _exit disitu penanda untuk fungsi init dan exit, selain itu fungsi yang di tambahkan _init dan _exit tadi pada saat di load hanya akan sekali pakai (akan di discard setelah di pakai dari memori), karena memang fungsi init dan exit hanya akan sekali pakai.

linux/init.h dan linux/module.h adalah header wajib untuk setiap module karena isinya semua method basic dari module seperti printk tadi, jadi pastikan untuk selalu menginsert 2 header ini.

Seperti yang di sebutkan tadi, untuk mengcompile module harus memakai Makefile, jadinya di terminal kita memakai make, isi dari Makefile untuk program hello world kita adalah sebagai berikut :

Ok, jadi yang perlu di siapkan adalah hello.c (isinya kode module yang sudah di sebutkan tadi), Makefile dan mereka berdua di taruh di dir home anda
jadi setelah ini langkah-langkahnya adalah :
1.ketik make lalu enter di terminal
2.akan jadi sebuah file yang namanya hello.ko, ini adalah kode module yang nantinya bisa di pasang dan lepas
3.lalu insmod ./hello.ko dan voila, module telah berhasil terpasang, untuk memastikan module telah terpasang gunakan perintah lsmod

untuk melihat pesan dari printk, tidak akan muncul di terminal tapi dari syslog, ini bisa dilihat di /var/log/syslog untuk ubuntu (konon untuk linux distro lain, akan muncul di tempat yang berbeda, tapi saya sendiri belum bisa membuktikannya)

dan untuk melepas module tadi tinggal gunakan perintah rmmod hello

sekian untuk pengenalan, di lain waktu pembahasan ini akan di lanjutkan. Karena keterbatasan waktu sekarang cukup sampai disini, kalau ada kekurang atau saran silahkan tinggalkan di komentar, terima kasih.

Komentar