I'm reading about misc drivers in Linux, and I'm a little confused about the differences between them and char drivers. One source, the Linux journal, writes:
Alessandro tells us how to register a small device needing a single entry point with the misc driver.
Sometimes people need to write “small” device drivers, to support custom hacks—either hardware or software ones. To this end, as well as to host some real drivers, the Linux kernel exports an interface to allow modules to register their own small drivers. The misc driver was designed for this purpose.
Ok, so from this I get that there's a simple driver (in this case with a single entry point), that's a misc driver. Then another source, Essential Linux Device Drivers, states:
Misc (or miscellaneous) drivers are simple char drivers that share certain common characteristics. Because misc drivers are char drivers, the earlier discussion on char driver entry points hold for misc drivers, too.
Now this seems to say that misc drivers are just char drivers, but perhaps a subset of functions, and char drivers can have more than one entry point (such as an ioctl()
or an open()
or a read()
call)
So, what, in Linux C coding terms, are the differences between a char and misc device driver? (Besides the obvious major number assignment (10) for all misc drivers). Is there a difference in supported entry points? Is my assumption correct that misc device drivers only have a subset of what you can get in a full char device driver?
Misc (or miscellaneous) drivers are simple char drivers that share certain common characteristics. The kernel abstracts these commonalities into an API (implemented in drivers/char/misc. c), and this simplifies the way these drivers are initialized.
Character device drivers normally perform I/O in a byte stream. Examples of devices using character drivers include tape drives and serial ports. Character device drivers can also provide additional interfaces not present in block drivers, such as I/O control (ioctl) commands, memory mapping, and device polling.
The block devices access the disk using the system's normal buffering mechanism. The character devices provide for direct transmission between the disk and the user's read or write buffer.
The misc driver exports two functions for user modules to register and unregister their own minor number: #include <linux/miscdevice.
Edit: I thought you were talking about drivers/misc
drivers, but I see you're refering to character drivers using misc_register
(and all the API in drivers/char/misc.c
). You should specify this in your question.
In this case, the misc
API seems to make your life easier when you're writing a small character driver and do not want to need to allocate a new major number only to use one minor number, for example. It simplifies things, but all the file operations are still available using the fops
member of struct miscdevice
. The basic difference is you only get one minor number per misc
device.
My previous, unrelated answer was, for the record:
Have a quick look at drivers/misc
: you won't find any "misc
core" out there. This means: misc
is not a device class; it's just a bunch of drivers that don't fit in any other category. Things like barometers, DACs, test suites and other strange things.
Look at the top of drivers/misc/Kconfig
:
#
# Misc strange devices
#
menu "Misc devices"
All the items in this Kconfig do not depend on any "misc
core", but on other cores (i2c
, pci
, tty
, etc.). Usually, when a driver is really using a driver core, you will see it in its Kconfig. For instance, pretty much all leds
drivers (drivers/leds
) depend on the leds
class core and have this in their Kconfig node:
depends on LEDS_CLASS
Maybe misc
drivers are all character drivers (I didn't check all of them), but something else would still work there, although it would probably be at the wrong place. I believe that lots of misc
drivers could be moved to better places now... a veteran kernel hacker could confirm this.
So, to answer your question: misc
drivers do not have to be character drivers, so the two categories are completely unrelated. A misc
driver brings nothing more than a character driver because a misc
driver is, again, nothing special.
Update: the Honeywell compass driver is a great example. It is small and straightforward.
It communicates with the actual compass using I²C. This device won't show up as a character device, so forget about major number 10. It will, however, appear somewhere in Sysfs, under /sys/bus/i2c/devices
, like all I²C devices do. And you will see the Sysfs attributes it adds to its group, like heading0_input
that will show the current compass direction when read.
So here you have it: a misc
driver that's not a character driver.
Now this seems to say that misc drivers are just char drivers, but perhaps a subset of functions, and char drivers can have more than one entry point (such as an ioctl() or an open() or a read() call)
Yes,it just Charater driver, and Misc driver also have multiple entry point read(), write(), ioctl() (because in miscdevice 's structure already have filefile_operations structure)
in my understanding, when we need to write a small driver (only have one entry point or some more (2,3,... entry points) <<< mean small driver) we should use misc driver. it will prevent waste of RAM if we register new Major Number.
Now Since the kernel keeps a static table of device drivers, frivolous allocation of major numbers is rather wasteful of RAM. The Linux kernel, therefore, offers a simplified interface for simple drivers—those that will register a single entry point. Note that, in general, allocating the whole name space of a major number to every device is beneficial. This allows the handling of multiple terminals, multiple serial ports and several disk partitions without any overhead in the kernel proper: a single driver takes care of all of them, and uses the minor number to differentiate.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With