Learn how to create simple Linux device drivers using the miscellaneous device API for easy device file creation.
Key Takeaways
- The miscellaneous device API simplifies device file creation with minimal code.
- Miscellaneous devices use a reserved major device number (10) and dynamic minor numbers by default.
- File operations for miscellaneous devices are similar to those for character devices.
- For simple devices, miscellaneous device drivers are recommended over traditional character devices.
- More complex devices may still require full character device implementations.
What the video covers
- Introduction to writing simple Linux device drivers for basic devices.
- Challenges of creating device files with traditional character device drivers.
- Introduction to the Linux kernel's miscellaneous device API as an easier alternative.
- Step-by-step implementation of a miscellaneous device driver using example code.
- Comparison of miscellaneous devices versus traditional character devices.
- Explanation of file operations, read/write callbacks, and device registration.
- Details on dynamic versus fixed minor device numbers for miscellaneous devices.
- Instructions on adapting existing character device code to use miscellaneous devices.
- Demonstration of compiling, loading, and testing the miscellaneous device driver.
- Summary of when to prefer miscellaneous devices over character devices based on device complexity.
Chapters
- 00:00Introduction to simple Linux device drivers
- 00:29Challenges with traditional device file creation
- 00:52Introducing the miscellaneous device API
- 01:19Overview of today's tutorial and API benefits
- 01:37Setup: Navigating tutorial files and templates
- 02:27Reusing read/write callbacks from previous lecture
- 02:59Examining source files and project structure
- 04:14Renaming and adapting code for miscellaneous devices
- 04:44Implementing read/write callbacks and file operations
- 05:47Registering character device vs miscellaneous device
Full Transcript — Download SRT & Markdown
Speaker A
Welcome to a new episode of my Linux driver tutorials. So, let's say you have a very simple device and you want to write a simple Linux device driver for it. You need an interface, a user space to communicate with the device driver.
Speaker A
But now things are getting difficult because if you remember the video where we have created a device file from within our Linux driver, we needed quite a bit of code to create a device file.
Speaker A
But now things are getting difficult because if you remember the video where we created a device file from within our Linux driver, we needed quite a bit of code to create a device file.
Speaker A
allows you to easily create a device file for a simple device. In today's video, we will take a look at this API.
Speaker A
And maybe you're wondering if there is an easier API available to just get a device file I can use for my simple device driver. And of course, the answer is yes. There is the miscellaneous device API in the Linux kernel which
Speaker A
And the first thing I will do is I will navigate into my Linux driver tutorials folder. Here I can see all the lectures I already did. And as a template for today's video, I will use um lection 8
Speaker A
allows you to easily create a device file for a simple device. In today's video, we will take a look at this API.
Speaker A
Okay. So let's navigate in here and let's take a look what we have in here.
Speaker A
And later we will also discuss some advantages and disadvantages of a miscellaneous device compared to a more traditional character device. So, let's start. As you can see here, I'm connected to my Raspberry Pi over SSH.
Speaker A
application. And we have a read me giving you some more information if you're viewing this online.
Speaker A
And the first thing I will do is I will navigate into my Linux driver tutorials folder. Here I can see all the lectures I already did. And as a template for today's video, I will use um lecture 8
Speaker A
And of course, I also have to adapt this here in the Mac file. Okay. Now, let's take a look at the driver source and let's implement a miscellaneous device here. So, what do we have here? This example basically
Speaker A
read write character device because I will recycle the read and the write callback from this um lecture and I will create a new folder. I will call it MISK device for miscellaneous device.
Speaker A
can write this string from user space. Down here we are defining the file operations our character device supports and down here we are registering a character device. So this function will allocate a device number for us and um
Speaker A
Okay. So, let's navigate in here and let's take a look at what we have in here.
Speaker A
will also put a link into the description down below so you can watch it again if you want to.
Speaker A
So hello CDEV.c is the Linux kernel driver C source file. Then test.c is a user space application we can use for this driver. The makefile will compile both the Linux driver and the user space
Speaker A
And we also don't need the static in major variable here. So let me also delete it. Okay. So if you want to use miscellaneous devices, we have to include the header Linux/mis device.h.
Speaker A
application. And we have a readme giving you some more information if you're viewing this online.
Speaker A
And here we are defining our miscellaneous devices. So the first thing we will do is we will assign the file operation the miscellaneous device supports because basically the miscellaneous devices supports the same file operation as a normal character
Speaker A
But as we don't have a traditional character device here, but a miscellaneous device instead, let me rename hello CDEV.c to hello misdev.c.
Speaker A
is and this is a difference between a character device and a MISK device. By a character device we can choose a major and a minor device number. Of course both must be free but else we can use any number we want to. But a
Speaker A
And of course, I also have to adapt this here in the makefile. Okay. Now, let's take a look at the driver source and let's implement a miscellaneous device here. So, what do we have here? This example basically
Speaker A
have to set it to MIS dynamic minor and if we want to use a fixed minor device number okay we could set this to 12 for example for device number 12 but no I want to assign dynamical device number here
Speaker A
shows you how to implement a read and write callback for a character device file. So, we have a global string up here and with my read we can read this string from the user space and with my write we
Speaker A
Okay. And now let's register our miscellaneous device. And for doing so all we have to do is we have to call the function MISK register.
Speaker A
can write this string from user space. Down here we are defining the file operations our character device supports and down here we are registering a character device. So this function will allocate a device number for us and um
Speaker A
and I will print out this message and I will return with the error code here.
Speaker A
create a character device for us. This doesn't create a device file for us. We will take a look at the steps which are needed for this later, but of course I already made a video about this which I
Speaker A
all we need. And here in the exit function, all we have to do is we have to call the function um miskd register.
Speaker A
will also put a link into the description down below so you can watch it again if you want to.
Speaker A
Okay, the compilation worked. So let's try to load our driver. So in here I want to follow the kernel lock.
Speaker A
Okay, so I think we don't need this code here. So let's delete it because we don't want to um create a character device. We want to use a miscellaneous device.
Speaker A
can see the major device number is 10. The minor device number is as we can see here in the kernel lock 121.
Speaker A
And we also don't need the static int major variable here. So let me also delete it. Okay. So if you want to use miscellaneous devices, we have to include the header linux/miscdevice.h.
Speaker A
write from this file and if we take a look at def proc devices we can see major device number 10 is uh is reserved for MISK devices.
Speaker A
And after the definition of the file operations, let's create a second global variable from the type struct miscdevice which I will call my MISK device.
Speaker A
Okay. So, and if I remove the module again, now the my MISK file is no longer available here.
Speaker A
And here we are defining our miscellaneous device. So the first thing we will do is we will assign the file operations the miscellaneous device supports because basically the miscellaneous device supports the same file operations as a normal character
Speaker A
for registering or creating a device file with a miscellaneous device is extremely easy. We only need very few lines of code. If we compare it to create the creation of a device file by using a character device, you see it's a lot more code. More code
Speaker A
device. Then we have to give the miscellaneous device a name. I will choose my MISK dev and this will also be the name of the device file which will be created by this. And the last thing we have to do
Speaker A
So yeah, you have to know what you prefer. In general, if you have a very simple device, a miscellaneous device driver is the way to go. If it's getting more complicated, maybe a device, a proper character device is better. So I
Speaker A
is, and this is a difference between a character device and a MISK device, by a character device we can choose a major and a minor device number. Of course, both must be free but else we can use any number we want to. But a
Speaker A
So, thanks for watching and goodbye.
Topics:Linux device drivermiscellaneous devicecharacter deviceLinux kerneldevice fileread write callbacksdriver tutorialRaspberry Pidevice registrationLinux kernel programming











