{"id":7512,"date":"2022-02-16T10:36:17","date_gmt":"2022-02-16T10:36:17","guid":{"rendered":"http:\/\/fastbitlab.com\/?p=7512"},"modified":"2023-09-13T15:27:40","modified_gmt":"2023-09-13T09:57:40","slug":"linux-device-driver-programming-lecture-28-a-char-driver-char-device-and-char-device-number","status":"publish","type":"post","link":"https:\/\/fastbitlab.com\/blog\/linux-device-driver-programming-lecture-28-a-char-driver-char-device-and-char-device-number\/","title":{"rendered":"Linux Device Driver Programming Lecture 28- A char driver, char device and char device number"},"content":{"rendered":"<div class=\"boldgrid-section\" style=\"background-image: linear-gradient(to left, #eeeeee, #eeeeee);\" data-bg-color-1=\"#EEEEEE\" data-bg-color-2=\"#EEEEEE\" data-bg-direction=\"to left\">\n<div class=\"container\">\n<div class=\"row\" style=\"padding-top: 35px; padding-bottom: 0px; background-image: linear-gradient(to left, #eeeeee, #eeeeee);\" data-bg-color-1=\"#EEEEEE\" data-bg-color-2=\"#EEEEEE\" data-bg-direction=\"to left\">\n<div class=\"col-md-1 col-sm-12 col-xs-12 col-lg-1\">\n<p>&nbsp;<\/p>\n<\/div>\n<div class=\"col-md-10 col-sm-12 col-xs-12 col-lg-10\">\n<h1 class=\"\" style=\"font-size: 30px; text-align: center; border-width: 0px; line-height: 50px;\"><span style=\"color: #000080;\"><strong>A char driver, char device and char device number<\/strong><\/span><\/h1>\n<div class=\"row bg-editor-hr-wrap\" style=\"border-width: 0px; margin-top: -25px;\">\n<div class=\"col-lg-12 col-md-12 col-xs-12 col-sm-12\">\n<div>\n<p>&nbsp;<\/p>\n<div class=\"bg-hr bg-hr-10 color2-color\" style=\"border-style: solid; border-width: 0px 0px 3px;\"><\/div>\n<p>&nbsp;<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p class=\"\" style=\"font-size: 17px; line-height: 30px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\">In this article, we delve into the world of character drivers, device numbers, and the essential kernel APIs. This knowledge is crucial for creating a seamless connection between user-space applications and kernel drivers.<\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 30px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\">Character drivers play a pivotal role in interfacing user-level applications with hardware devices. In this article, we&#8217;ll explore the intricacies of character drivers, device numbers, and the critical kernel APIs that facilitate their interaction.<\/span><\/p>\n<ul class=\"\" style=\"font-size: 17px; line-height: 30px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\">\n<li><span style=\"color: #000000;\">Character drivers are used to interact with devices in the Linux kernel.<\/span><\/li>\n<li><span style=\"color: #000000;\">Char devices refer to hardware components like memory areas, often called pseudo devices.<\/span><\/li>\n<li><span style=\"color: #000000;\">Userspace applications communicate with these devices using system calls like open, read, and write.<\/span><\/li>\n<\/ul>\n<p class=\"\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"font-size: 19px; line-height: 30px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"text-decoration: underline; color: #000080;\"><strong>User Space vs. Kernel Space (Figure 1)<\/strong><\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 30px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\">Character drivers operate in the kernel space and manage device interactions for user-space applications.<\/span><\/p>\n<figure id=\"attachment_7515\" aria-describedby=\"caption-attachment-7515\" style=\"width: 596px\" class=\"wp-caption aligncenter\"><img fetchpriority=\"high\" decoding=\"async\" class=\" wp-image-7515\" src=\"http:\/\/fastbitlab.com\/wp-content\/uploads\/2022\/02\/Figure-1-16.png\" alt=\"Figure 1. User space vs Kernel space\" width=\"596\" height=\"321\" srcset=\"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-1-16.png 789w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-1-16-300x162.png 300w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-1-16-768x414.png 768w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-1-16-600x323.png 600w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-1-16-120x65.png 120w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-1-16-500x269.png 500w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-1-16-200x108.png 200w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-1-16-400x215.png 400w\" sizes=\"(max-width: 596px) 100vw, 596px\" \/><figcaption id=\"caption-attachment-7515\" class=\"wp-caption-text\"><span style=\"color: #000000;\">Figure 1. User space vs Kernel space<\/span><\/figcaption><\/figure>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">In our case, the hardware is a simple memory area. We call it a sudo device. <\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">When the user application wants to talk to the hardware, it uses it&#8217;s a system call, such as open, read and write. <\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">When the user application uses an open system call, that open system call should connect to the open system call implementation of your driver. <\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">And when the user that application uses to read and write system calls, those system calls must connect to the read and write implementations or methods of your driver.<\/span><\/p>\n<p class=\"\">&nbsp;<\/p>\n<p class=\"\"><strong>Connecting Userspace to Driver:<\/strong><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">We have to understand how to establish such connections from userspace to the device drivers. And the drivers read and write functions or methods that will then forward data to the hardware, or it will read data from the hardware. <\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">T<\/span><span style=\"font-weight: 400; color: #000000;\">he read method of the driver will read from the hardware, and it should submit data back to the user, and the write method of the driver should accept data from the user space, it should write the data to the hardware.<\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">If you&#8217;re wondering how these a user space system calls will get connected to the driver system call&nbsp;&nbsp;<\/span><span style=\"font-weight: 400;\">implementation methods, this is taken care by the VFS (that is virtual file system) of the kernel. That means our device driver has to get registered with the VFS by using virtual files systems kernel APIs. All those APIs we are going to understand in the subsequent articles.<\/span><\/span><\/p>\n<p class=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"font-size: 20px; line-height: 30px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"text-decoration: underline; color: #000080;\"><strong>Device Number Importance<\/strong><\/span><\/p>\n<figure id=\"attachment_7516\" aria-describedby=\"caption-attachment-7516\" style=\"width: 650px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\" wp-image-7516\" src=\"http:\/\/fastbitlab.com\/wp-content\/uploads\/2022\/02\/Figure-2-19.png\" alt=\"Figure 2.One RTC device in userspace\" width=\"650\" height=\"311\" srcset=\"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-2-19.png 783w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-2-19-300x143.png 300w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-2-19-768x367.png 768w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-2-19-600x287.png 600w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-2-19-120x57.png 120w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-2-19-500x239.png 500w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-2-19-200x96.png 200w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-2-19-400x191.png 400w\" sizes=\"(max-width: 650px) 100vw, 650px\" \/><figcaption id=\"caption-attachment-7516\" class=\"wp-caption-text\"><span style=\"color: #000000;\">Figure 2. One RTC device in userspace<\/span><\/figcaption><\/figure>\n<p class=\"\" style=\"font-size: 17px; font-family: 'Roboto Slab'; font-weight: 400; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\">Device numbers are crucial for connecting device files to their respective drivers.<\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">H<\/span><\/span><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">ere, the driver which we are going to write lives in the kernel space. And it controls the hardware using its file operation methods such as open, read, write, etc. And the driver creates a device file interface in the user&#8217;s space for user-level programs to communicate with the driver. <\/span><\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">Now the question is when you use, let&#8217;s say, open system call on <\/span><b>\/dev\/rtc<\/b><span style=\"font-weight: 400;\"> device file, how does the kernel connect the open system call to intended driver&#8217;s to open method. <\/span><\/span><\/p>\n<p data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">How does that connection is established?<\/span><\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">To establish the connection, the kernel uses something called &#8220;Device number.&#8221; Let&#8217;s assign a number to the driver as 4 and also to \/dev\/rtc device file, let&#8217;s say 4:0.<\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">Here 4 denotes the number of the driver which should be used to connect this device file access to the driver&#8217;s methods. And 0 here is a device instance. There could be multiple device files, all are handled by the same driver.<\/span><\/p>\n<figure id=\"attachment_7517\" aria-describedby=\"caption-attachment-7517\" style=\"width: 691px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\" wp-image-7517\" src=\"http:\/\/fastbitlab.com\/wp-content\/uploads\/2022\/02\/Figure-3-16.png\" alt=\"Figure 3. Four RTC devices in Userspace\" width=\"691\" height=\"371\" srcset=\"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-3-16.png 876w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-3-16-300x161.png 300w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-3-16-768x413.png 768w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-3-16-600x323.png 600w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-3-16-120x65.png 120w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-3-16-500x269.png 500w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-3-16-200x108.png 200w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-3-16-400x215.png 400w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-3-16-800x430.png 800w\" sizes=\"(max-width: 691px) 100vw, 691px\" \/><figcaption id=\"caption-attachment-7517\" class=\"wp-caption-text\"><span style=\"color: #000000;\">Figure 3. Four RTC devices in Userspace<\/span><\/figcaption><\/figure>\n<p class=\"\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">For example, there could be 4 RTC devices, as shown in Figure 3. All are handled by a single RTC driver.<\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">In that case, there could be four device files. Like dev\/rtc0, rtc1, or 1, or 3, like that. In this case, the device numbers could be 4:0, 4:1, 4:2, or 4:3. <\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">Observe here this part is the same for all the devices, which refers to the driver, and this part refers to the device instances.<\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 30px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\">Device numbers consist of two parts: <\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 30px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\">The major number (identifying the driver) and the minor number (indicating the specific device file). <\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 30px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\">Major numbers are essential for driver identification, while minor numbers help the driver differentiate between various device files.<\/span><\/p>\n<figure id=\"attachment_7518\" aria-describedby=\"caption-attachment-7518\" style=\"width: 796px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-7518\" src=\"http:\/\/fastbitlab.com\/wp-content\/uploads\/2022\/02\/Figure-4-13.png\" alt=\"Figure 4. Major and Minor numbers\" width=\"796\" height=\"359\" srcset=\"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-4-13.png 796w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-4-13-300x135.png 300w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-4-13-768x346.png 768w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-4-13-600x271.png 600w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-4-13-120x54.png 120w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-4-13-500x226.png 500w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-4-13-200x90.png 200w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-4-13-400x180.png 400w\" sizes=\"(max-width: 796px) 100vw, 796px\" \/><figcaption id=\"caption-attachment-7518\" class=\"wp-caption-text\"><span style=\"color: #000000;\">Figure 4. Major and Minor numbers<\/span><\/figcaption><\/figure>\n<p data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">Collectively, number 4 is called as a device number. Left part is called a major number, and the right part is minor number. When the user-level program uses an open system, call on this device file. The system call will first handled by the virtual file system, that is, VFS in the kernel space. So, the VFS gets the device number and compares it with its driver registration list. That means this driver has to get registered with the VFS using the device number. That is what we call a character device add, known as CDEV_ADD.<\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">The VFS compares the device number of the device file with the driver&#8217;s list and picks the correct driver, and connects the user requests to the file operation methods of the selected driver.<\/span><\/p>\n<figure id=\"attachment_7519\" aria-describedby=\"caption-attachment-7519\" style=\"width: 526px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-7519\" src=\"http:\/\/fastbitlab.com\/wp-content\/uploads\/2022\/02\/Figure-5-12.png\" alt=\"Figure 5. Command ls -l\" width=\"526\" height=\"385\" srcset=\"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-5-12.png 526w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-5-12-300x220.png 300w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-5-12-120x88.png 120w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-5-12-500x366.png 500w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-5-12-200x146.png 200w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-5-12-400x293.png 400w\" sizes=\"(max-width: 526px) 100vw, 526px\" \/><figcaption id=\"caption-attachment-7519\" class=\"wp-caption-text\"><span style=\"color: #000000;\">Figure 5. Command ls -l<\/span><\/figcaption><\/figure>\n<p class=\"\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">If you just do <\/span><b>ls -l<\/b><span style=\"font-weight: 400;\"> in the \/dev directory(as shown in Figure 5), you see various device files, and here, there are two important columns. These columns signifies the device number.<\/span><\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">For example, if you consider <\/span><b>nvram<\/b><span style=\"font-weight: 400;\">.&nbsp; <\/span><\/span><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">nvram is a device file which is created to handle the nvram device of the system. This is a device number of this a device. And here, 10 is the major number which identifies the driver, and 144 is a minor number. <\/span><\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">Here there are two device files, sda, and sda1. <\/span><\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">Sda1 is a partition of my hard disk. This signifies the hard disk, and this is a partition of a hard disk. The disk partition is of type EXT3 or EXT4. And these two device files they have the same major number that is 8, and these are the minor numbers 0 and 1.<\/span><\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><span style=\"font-weight: 400;\"> The driver will use these minor numbers to distinguish on which device file the read and write operations have been issued. That&#8217;s why, minor numbers help the driver to differentiate between the device files.<\/span><\/span><\/p>\n<p class=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">From the previous article, we came to know that, first we have to a create some device numbers, and after that, the driver has to get registered with the virtual file system of the kernel to make a successful connection.<\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\"> Let&#8217;s see some of the steps for connection establishment between a device file access and the driver. First, the driver has to create a device number, it has to create device files. After that, the driver has to make a registration with the VFS, what we call as character device&nbsp; registration CDEV_ADD.<\/span><\/p>\n<p class=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">And after that, the driver has to implement the file operation methods for open, read, write, lseek&nbsp;<\/span><span style=\"font-weight: 400;\">system calls.<\/span><\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><span style=\"font-weight: 400;\"> Let&#8217;s start implementing these steps one by one. <\/span><\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">Let&#8217;s create a device number. Your driver has to ask the kernel to dynamically allocate the device number or numbers. Basically, what you should be doing here is, you should be using&nbsp; kernel APIs and kernel utilities(shown in Figure 6)&nbsp; in order to a request various services from the kernel.<\/span><\/span><\/p>\n<figure id=\"attachment_7520\" aria-describedby=\"caption-attachment-7520\" style=\"width: 567px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-7520 size-full\" src=\"http:\/\/fastbitlab.com\/wp-content\/uploads\/2022\/02\/Figure-6-9.png\" alt=\"A char driver, char device and char device number\" width=\"567\" height=\"209\" srcset=\"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-6-9.png 567w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-6-9-300x111.png 300w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-6-9-120x44.png 120w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-6-9-500x184.png 500w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-6-9-200x74.png 200w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-6-9-400x147.png 400w\" sizes=\"(max-width: 567px) 100vw, 567px\" \/><figcaption id=\"caption-attachment-7520\" class=\"wp-caption-text\"><span style=\"color: #000000;\">Figure 6. Kernel APIs and utilities to be used in driver code<\/span><\/figcaption><\/figure>\n<p data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">Now, to create a device number, you just have to use a kernel API alloc_chrdev_region(). So, you&nbsp;<\/span><span style=\"font-weight: 400;\">have to use alloc_chrdev_region(). This creates a device number. And for the registration, you can use these APIs cdev_init() and cdev_add().<\/span><\/span><\/p>\n<p class=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">And after that, the driver should create a device files. For that, you can use these kernel APIs class_create() and device_create(). The creation things we are going to do in module initialization function. <\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">Whenever you load a module, so these creation a services must be executed and your driver must be ready to accept a system calls from the user space program.<\/span><\/p>\n<figure id=\"attachment_7521\" aria-describedby=\"caption-attachment-7521\" style=\"width: 755px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-7521 size-full\" src=\"http:\/\/fastbitlab.com\/wp-content\/uploads\/2022\/02\/Figure-7-6.png\" alt=\"A char driver, char device and char device number\" width=\"755\" height=\"322\" srcset=\"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-7-6.png 755w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-7-6-300x128.png 300w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-7-6-600x256.png 600w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-7-6-120x51.png 120w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-7-6-500x213.png 500w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-7-6-200x85.png 200w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-7-6-400x171.png 400w\" sizes=\"(max-width: 755px) 100vw, 755px\" \/><figcaption id=\"caption-attachment-7521\" class=\"wp-caption-text\"><span style=\"color: #000000;\">Figure 7. Creation and Deletiion APIs<\/span><\/figcaption><\/figure>\n<p class=\"\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">That&#8217;s why it makes sense to do this creation process in the module initialization function.<\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">When you remove the module, it&#8217;s better you delete all those a resources what you requested from&nbsp;<\/span><span style=\"font-weight: 400;\">the kernel. Otherwise, it will simply consume a resources of the kernel. That&#8217;s why, let&#8217;s say, if you use unregister_chrdev_region(), it will delete the device number which is allocated for your module, that it can be reused for some other module. <\/span><\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">After that, you can use cdev_del() to delete the registration, that will free some memory. class_destroy and device_destroy will delete your device files. That&#8217;s why, all these deletion things we are going to do in module clean-up function. Because these deletion things should be executed whenever you remove the module.&nbsp;<\/span><\/span><\/p>\n<p class=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"font-size: 20px; line-height: 30px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"text-decoration: underline; color: #000080;\"><strong>Use of Kernel APIs and Header Files:<\/strong><\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">When you use all these a kernel functions and data structures, you should be using the appropriate kernel header file (as shown in Figure 8).<\/span><\/p>\n<figure id=\"attachment_7522\" aria-describedby=\"caption-attachment-7522\" style=\"width: 505px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-7522 size-full\" src=\"http:\/\/fastbitlab.com\/wp-content\/uploads\/2022\/02\/Figure-8-5.png\" alt=\"A char driver, char device and char device number\" width=\"505\" height=\"292\" srcset=\"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-8-5.png 505w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-8-5-300x173.png 300w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-8-5-120x69.png 120w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-8-5-500x289.png 500w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-8-5-200x116.png 200w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-8-5-400x231.png 400w\" sizes=\"(max-width: 505px) 100vw, 505px\" \/><figcaption id=\"caption-attachment-7522\" class=\"wp-caption-text\"><span style=\"color: #000000;\">Figure 8. Kernel header file details<\/span><\/figcaption><\/figure>\n<p class=\"\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">For example, alloc_chrdev_region() and unregister_chrdev_region(), these APIs prototypes&nbsp;<\/span><span style=\"font-weight: 400;\">are mentioned in include\/linux\/fs.h. Whenever you use any services of the VFS, then I think most probably you should be using a fs.h. <\/span><\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">Even if you are using any data structures of the VFS such as file operation structures, a cdev structures, a file structure, which will be exploring in later articles, you should be using a fs.h. Because, the structure definitions are mentioned in the fs.h. And for&nbsp; cdev_init(), cdev_add(), cdev_del(), you should be using a include\/linux\/cdev.h and for a device creation, class creation, device_destroy(), class_destroy() related APIs. <\/span><\/span><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">You should be including linux\/device.h. <\/span><\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">Basically for a character device, all these header files are very important and doesn&#8217;t matter whether you use it or not. Please include fs.h, cdev.h,device.h uaccess.h. This is basically for copy to user and copy from user a utilities, I will talk about these utilities later. And for the VFS structure definition, also you should include fs.h.<\/span><\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">In the next article, let me explain how to use alloc_chrdev_region() to <span style=\"color: #0000ff;\"><a style=\"color: #0000ff;\" href=\"http:\/\/fastbitlab.com\/dynamically-allocating-char-device-numbers\/\" target=\"_blank\" rel=\"noopener\">dynamically create char device<\/a><\/span><\/span><span style=\"font-weight: 400;\"><span style=\"color: #0000ff;\">numbers<\/span>.&nbsp;<\/span><\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 31px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><strong><span style=\"color: #000000;\"><span style=\"color: #993366;\">Get the full course on Linux Device Driver<\/span> <span style=\"color: #0000ff;\"><a style=\"color: #0000ff;\" href=\"https:\/\/www.udemy.com\/course\/linux-device-driver-programming-using-beaglebone-black\/\">Here<\/a>.<\/span><\/span><\/strong><\/p>\n<p class=\"\" style=\"border-width: 0px; font-size: 17px; line-height: 30px;\"><span style=\"color: #000080;\"><b>FastBit Embedded Brain Academy Courses<\/b><\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-size: 17px;\"><span style=\"color: #000000;\">C<span style=\"font-weight: 400;\">lick here:&nbsp;<\/span><\/span><span style=\"color: #3366ff;\"><a style=\"color: #3366ff; text-decoration: underline;\" href=\"http:\/\/fastbitlab.com\/course1\" target=\"_blank\" rel=\"noopener\"><span style=\"font-weight: 400;\">https:\/\/fastbitlab.com\/course1<\/span><\/a><\/span><\/p>\n<p class=\"\">&nbsp;<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; A char driver, char device and char device number &nbsp; &nbsp; In this article, we delve into the world of character drivers, device numbers, and the essential kernel APIs. This knowledge is crucial for creating a seamless connection between user-space applications and kernel drivers. Character drivers play a pivotal role in interfacing user-level applications [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":7515,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ocean_post_layout":"","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"0","ocean_second_sidebar":"0","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"0","ocean_custom_header_template":"0","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"0","ocean_menu_typo_font_family":"0","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"0","ocean_post_oembed":"","ocean_post_self_hosted_media":"","ocean_post_video_embed":"","ocean_link_format":"","ocean_link_format_target":"self","ocean_quote_format":"","ocean_quote_format_link":"post","ocean_gallery_link_images":"off","ocean_gallery_id":[],"footnotes":""},"categories":[8],"tags":[18],"class_list":["post-7512","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","tag-linux-device-driver-programming","entry","has-media"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>A char driver, char device and char device number | Linux Kernel Development<\/title>\n<meta name=\"description\" content=\"A char driver, char device and char device number. Explore the world of character drivers in the Linux kernel. Learn how to create a char\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/fastbitlab.com\/blog\/linux-device-driver-programming-lecture-28-a-char-driver-char-device-and-char-device-number\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"A char driver, char device and char device number | Linux Kernel Development\" \/>\n<meta property=\"og:description\" content=\"A char driver, char device and char device number. Explore the world of character drivers in the Linux kernel. Learn how to create a char\" \/>\n<meta property=\"og:url\" content=\"https:\/\/fastbitlab.com\/blog\/linux-device-driver-programming-lecture-28-a-char-driver-char-device-and-char-device-number\/\" \/>\n<meta property=\"og:site_name\" content=\"FastBit EBA\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/fastbiteba\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-02-16T10:36:17+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-09-13T09:57:40+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-1-16.png\" \/>\n\t<meta property=\"og:image:width\" content=\"789\" \/>\n\t<meta property=\"og:image:height\" content=\"425\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"FastBitLab\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@fastbiteba\" \/>\n<meta name=\"twitter:site\" content=\"@fastbiteba\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"FastBitLab\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/linux-device-driver-programming-lecture-28-a-char-driver-char-device-and-char-device-number\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/linux-device-driver-programming-lecture-28-a-char-driver-char-device-and-char-device-number\\\/\"},\"author\":{\"name\":\"FastBitLab\",\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/#\\\/schema\\\/person\\\/e32b38e733a0d76ffa7e6bc998652e5d\"},\"headline\":\"Linux Device Driver Programming Lecture 28- A char driver, char device and char device number\",\"datePublished\":\"2022-02-16T10:36:17+00:00\",\"dateModified\":\"2023-09-13T09:57:40+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/linux-device-driver-programming-lecture-28-a-char-driver-char-device-and-char-device-number\\\/\"},\"wordCount\":1792,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/linux-device-driver-programming-lecture-28-a-char-driver-char-device-and-char-device-number\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/Figure-1-16.png\",\"keywords\":[\"Linux Device Driver Programming Tutorial\"],\"articleSection\":[\"Blog\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/linux-device-driver-programming-lecture-28-a-char-driver-char-device-and-char-device-number\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/linux-device-driver-programming-lecture-28-a-char-driver-char-device-and-char-device-number\\\/\",\"url\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/linux-device-driver-programming-lecture-28-a-char-driver-char-device-and-char-device-number\\\/\",\"name\":\"A char driver, char device and char device number | Linux Kernel Development\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/linux-device-driver-programming-lecture-28-a-char-driver-char-device-and-char-device-number\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/linux-device-driver-programming-lecture-28-a-char-driver-char-device-and-char-device-number\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/Figure-1-16.png\",\"datePublished\":\"2022-02-16T10:36:17+00:00\",\"dateModified\":\"2023-09-13T09:57:40+00:00\",\"description\":\"A char driver, char device and char device number. Explore the world of character drivers in the Linux kernel. Learn how to create a char\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/linux-device-driver-programming-lecture-28-a-char-driver-char-device-and-char-device-number\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/linux-device-driver-programming-lecture-28-a-char-driver-char-device-and-char-device-number\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/linux-device-driver-programming-lecture-28-a-char-driver-char-device-and-char-device-number\\\/#primaryimage\",\"url\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/Figure-1-16.png\",\"contentUrl\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/Figure-1-16.png\",\"width\":789,\"height\":425,\"caption\":\"Figure 1. User space vs Kernel space\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/linux-device-driver-programming-lecture-28-a-char-driver-char-device-and-char-device-number\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Linux Device Driver Programming Lecture 28- A char driver, char device and char device number\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/\",\"name\":\"FastBit EBA\",\"description\":\"Your Online Academy of Embedded Systems\",\"publisher\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/#organization\",\"name\":\"FastBit EBA\",\"url\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/04\\\/logo-EzNrEnyr.png\",\"contentUrl\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/04\\\/logo-EzNrEnyr.png\",\"width\":640,\"height\":640,\"caption\":\"FastBit EBA\"},\"image\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/fastbiteba\\\/\",\"https:\\\/\\\/x.com\\\/fastbiteba\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/fastbit-embedded-brain-academy-b3167b124\\\/\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCa1REBV9hyrzGp2mjJCagBg\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/#\\\/schema\\\/person\\\/e32b38e733a0d76ffa7e6bc998652e5d\",\"name\":\"FastBitLab\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/9230d0f9bdef28b63a01e7ca274ee7b2e8ed9abe932ee564af8809caaf52a0c8?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/9230d0f9bdef28b63a01e7ca274ee7b2e8ed9abe932ee564af8809caaf52a0c8?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/9230d0f9bdef28b63a01e7ca274ee7b2e8ed9abe932ee564af8809caaf52a0c8?s=96&d=mm&r=g\",\"caption\":\"FastBitLab\"},\"description\":\"The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries.\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"A char driver, char device and char device number | Linux Kernel Development","description":"A char driver, char device and char device number. Explore the world of character drivers in the Linux kernel. Learn how to create a char","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/fastbitlab.com\/blog\/linux-device-driver-programming-lecture-28-a-char-driver-char-device-and-char-device-number\/","og_locale":"en_US","og_type":"article","og_title":"A char driver, char device and char device number | Linux Kernel Development","og_description":"A char driver, char device and char device number. Explore the world of character drivers in the Linux kernel. Learn how to create a char","og_url":"https:\/\/fastbitlab.com\/blog\/linux-device-driver-programming-lecture-28-a-char-driver-char-device-and-char-device-number\/","og_site_name":"FastBit EBA","article_publisher":"https:\/\/www.facebook.com\/fastbiteba\/","article_published_time":"2022-02-16T10:36:17+00:00","article_modified_time":"2023-09-13T09:57:40+00:00","og_image":[{"width":789,"height":425,"url":"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-1-16.png","type":"image\/png"}],"author":"FastBitLab","twitter_card":"summary_large_image","twitter_creator":"@fastbiteba","twitter_site":"@fastbiteba","twitter_misc":{"Written by":"FastBitLab","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/fastbitlab.com\/blog\/linux-device-driver-programming-lecture-28-a-char-driver-char-device-and-char-device-number\/#article","isPartOf":{"@id":"https:\/\/fastbitlab.com\/blog\/linux-device-driver-programming-lecture-28-a-char-driver-char-device-and-char-device-number\/"},"author":{"name":"FastBitLab","@id":"https:\/\/fastbitlab.com\/blog\/#\/schema\/person\/e32b38e733a0d76ffa7e6bc998652e5d"},"headline":"Linux Device Driver Programming Lecture 28- A char driver, char device and char device number","datePublished":"2022-02-16T10:36:17+00:00","dateModified":"2023-09-13T09:57:40+00:00","mainEntityOfPage":{"@id":"https:\/\/fastbitlab.com\/blog\/linux-device-driver-programming-lecture-28-a-char-driver-char-device-and-char-device-number\/"},"wordCount":1792,"commentCount":0,"publisher":{"@id":"https:\/\/fastbitlab.com\/blog\/#organization"},"image":{"@id":"https:\/\/fastbitlab.com\/blog\/linux-device-driver-programming-lecture-28-a-char-driver-char-device-and-char-device-number\/#primaryimage"},"thumbnailUrl":"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-1-16.png","keywords":["Linux Device Driver Programming Tutorial"],"articleSection":["Blog"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/fastbitlab.com\/blog\/linux-device-driver-programming-lecture-28-a-char-driver-char-device-and-char-device-number\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/fastbitlab.com\/blog\/linux-device-driver-programming-lecture-28-a-char-driver-char-device-and-char-device-number\/","url":"https:\/\/fastbitlab.com\/blog\/linux-device-driver-programming-lecture-28-a-char-driver-char-device-and-char-device-number\/","name":"A char driver, char device and char device number | Linux Kernel Development","isPartOf":{"@id":"https:\/\/fastbitlab.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/fastbitlab.com\/blog\/linux-device-driver-programming-lecture-28-a-char-driver-char-device-and-char-device-number\/#primaryimage"},"image":{"@id":"https:\/\/fastbitlab.com\/blog\/linux-device-driver-programming-lecture-28-a-char-driver-char-device-and-char-device-number\/#primaryimage"},"thumbnailUrl":"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-1-16.png","datePublished":"2022-02-16T10:36:17+00:00","dateModified":"2023-09-13T09:57:40+00:00","description":"A char driver, char device and char device number. Explore the world of character drivers in the Linux kernel. Learn how to create a char","breadcrumb":{"@id":"https:\/\/fastbitlab.com\/blog\/linux-device-driver-programming-lecture-28-a-char-driver-char-device-and-char-device-number\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/fastbitlab.com\/blog\/linux-device-driver-programming-lecture-28-a-char-driver-char-device-and-char-device-number\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/fastbitlab.com\/blog\/linux-device-driver-programming-lecture-28-a-char-driver-char-device-and-char-device-number\/#primaryimage","url":"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-1-16.png","contentUrl":"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Figure-1-16.png","width":789,"height":425,"caption":"Figure 1. User space vs Kernel space"},{"@type":"BreadcrumbList","@id":"https:\/\/fastbitlab.com\/blog\/linux-device-driver-programming-lecture-28-a-char-driver-char-device-and-char-device-number\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/fastbitlab.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Linux Device Driver Programming Lecture 28- A char driver, char device and char device number"}]},{"@type":"WebSite","@id":"https:\/\/fastbitlab.com\/blog\/#website","url":"https:\/\/fastbitlab.com\/blog\/","name":"FastBit EBA","description":"Your Online Academy of Embedded Systems","publisher":{"@id":"https:\/\/fastbitlab.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/fastbitlab.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/fastbitlab.com\/blog\/#organization","name":"FastBit EBA","url":"https:\/\/fastbitlab.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/fastbitlab.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2026\/04\/logo-EzNrEnyr.png","contentUrl":"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2026\/04\/logo-EzNrEnyr.png","width":640,"height":640,"caption":"FastBit EBA"},"image":{"@id":"https:\/\/fastbitlab.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/fastbiteba\/","https:\/\/x.com\/fastbiteba","https:\/\/www.linkedin.com\/in\/fastbit-embedded-brain-academy-b3167b124\/","https:\/\/www.youtube.com\/channel\/UCa1REBV9hyrzGp2mjJCagBg"]},{"@type":"Person","@id":"https:\/\/fastbitlab.com\/blog\/#\/schema\/person\/e32b38e733a0d76ffa7e6bc998652e5d","name":"FastBitLab","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/9230d0f9bdef28b63a01e7ca274ee7b2e8ed9abe932ee564af8809caaf52a0c8?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/9230d0f9bdef28b63a01e7ca274ee7b2e8ed9abe932ee564af8809caaf52a0c8?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/9230d0f9bdef28b63a01e7ca274ee7b2e8ed9abe932ee564af8809caaf52a0c8?s=96&d=mm&r=g","caption":"FastBitLab"},"description":"The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries."}]}},"_links":{"self":[{"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/posts\/7512","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/comments?post=7512"}],"version-history":[{"count":5,"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/posts\/7512\/revisions"}],"predecessor-version":[{"id":15918,"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/posts\/7512\/revisions\/15918"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/media\/7515"}],"wp:attachment":[{"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/media?parent=7512"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/categories?post=7512"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/tags?post=7512"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}