Skip to content

Let's code a Linux Driver - 21 Easy device files with miscellaneous devices

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.

Answers

Questions about this video

What is the main advantage of using the miscellaneous device API?

The miscellaneous device API allows you to easily create device files with minimal code compared to traditional character device drivers, making it ideal for simple devices.

How does a miscellaneous device differ from a traditional character device?

Miscellaneous devices use a reserved major device number (10) and typically dynamic minor numbers, whereas character devices require manually choosing free major and minor numbers.

When should I choose a miscellaneous device driver over a character device driver?

For very simple devices, miscellaneous device drivers are recommended due to their simplicity. For more complex devices, a full character device driver might be more appropriate.

Full Transcript — Download SRT & Markdown

00:01
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.
00:15
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.
00:29
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.
00:44
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.
00:52
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
01:07
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
01:19
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.
01:33
Speaker A
Okay. So let's navigate in here and let's take a look what we have in here.
01:37
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.
01:52
Speaker A
application. And we have a read me giving you some more information if you're viewing this online.
01:58
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
02:09
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
02:27
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.
02:43
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
02:59
Speaker A
Okay. So, let's navigate in here and let's take a look at what we have in here.
03:11
Speaker A
will also put a link into the description down below so you can watch it again if you want to.
03:16
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
03:30
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.
03:44
Speaker A
application. And we have a readme giving you some more information if you're viewing this online.
04:00
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
04:14
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.
04:27
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
04:44
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
05:00
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
05:18
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
05:32
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.
05:47
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
06:15
Speaker A
and I will print out this message and I will return with the error code here.
06:22
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
06:51
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.
07:01
Speaker A
will also put a link into the description down below so you can watch it again if you want to.
07:21
Speaker A
Okay, the compilation worked. So let's try to load our driver. So in here I want to follow the kernel lock.
07:34
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.
07:55
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.
08:03
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.
08:27
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.
08:41
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.
09:03
Speaker A
Okay. So, and if I remove the module again, now the my MISK file is no longer available here.
09:16
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
09:35
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
09:52
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
10:05
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
10:22
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
10:30
Speaker A
So, thanks for watching and goodbye.
Topics:Linux device drivermiscellaneous devicecharacter deviceLinux kerneldevice fileread write callbacksdriver tutorialRaspberry Pidevice registrationLinux kernel programming

Get More with the SozAI App

Transcribe recordings, audio files, and YouTube videos — with AI summaries, speaker detection, and unlimited transcriptions.

Or transcribe another YouTube video here →