Major and Minor Numbers and registration and Revocation

Source: Internet
Author: User

Char
Devices are accessed

Through





Names in the filesystem. Those names are
Called special files or device files or simply nodes of
Filesystem tree; they are conventionally located in
/Dev


Directory. Special files
For char drivers are identified by
"C" in the first column of
Output of ls-l
. Block devices appear in
/Dev
As well, but they are identified by
"B." The focus of this chapter is
On char devices, but much of the following information applies
Block devices as well.

 


If you
Issue the ls-l
Command, you'll
SeeTwo numbers

(Separated by a comma) in the device file entries
Before the date of the last modification, where the file length
Normally appears. These numbers are the major and minor device number
For the participating device. The following listing shows a few devices
As they appear on a typical system. Their major numbers are 1, 4, 7,
While the minors are 1, 3, 5, 64, 65, and 129.

 crw-rw-rw-    1 root     root       1,   3 Apr 11  2002 null

crw------- 1 root root 10, 1 Apr 11 2002 psaux

crw------- 1 root root 4, 1 Oct 28 03:04 tty1

crw-rw-rw- 1 root tty 4, 64 Apr 11 2002 ttys0

crw-rw---- 1 root uucp 4, 65 Apr 11 2002 ttyS1

crw--w---- 1 vcsa tty 7, 1 Apr 11 2002 vcs1

crw--w---- 1 vcsa tty 7, 129 Apr 11 2002 vcsa1

crw-rw-rw- 1 root root 1, 5 Apr 11 2002 zero

 

Traditionally,Major number identifies the driver associated
The device.

For example,/Dev/null
And
/Dev/zero
Are both managed by driver 1, whereas
Virtual LEs and serial terminals are managed by driver 4;
Similarly, bothVcs1
And
Vcsa1
Devices are managed by driver 7. Modern
Linux kernels allow multiple drivers to share major numbers, but most
Devices that you will see are still organized on
One-major-one-driver principle
.



The minor number is used by
Kernel to determine exactly which device is being referred


.
Depending on how your driver is written (as we will see below), you
Can either get a direct pointer to your device from the kernel, or
You can use the minor number yourself as an index into a local array
Of devices. Either way, the kernel itself knows almost nothing about
Minor numbers beyond the fact that they refer to devices implemented
By your driver.

3.2.1. The Internal Representation of Device Numbers

Within the kernel,Dev_t
Type (defined in
<Linux/types. h>
)
Is
Used to hold device numbers â € "both
The major and

Minor parts. As
Version 2.6.0 of the kernel,Dev_t
Is a 32-bit
Quantity with 12 bits set aside for

Major number and 20 for
Minor number. Your code shocould, of course, never make any assumptions
About the internal organization of device numbers; it shocould,
Instead, make use of a set

Macros found
In<Linux/kdev_t.h>
. To obtain the major
Or minor parts ofDev_t
, Use:

MAJOR



(dev_t dev);




MINOR



(dev_t dev);

 

If, instead, you have the major and minor numbers and need to turn
Them intoDev_t
, Use:

MKDEV



(int major, int minor);

 

Note that the 2.6 kernel can accommodate a vast number of devices,
While previous kernel versions were limited to 255 major and 255
Minor numbers. One assumes that the wider range will be sufficient
For quite some time, but the computing field is littered
Erroneous assumptions of that nature. So you should have CT that
FormatDev_t
Cocould change again in the future;
If you write your drivers carefully, however, these changes will not
Be A Problem.

3.2.2. Allocating and Freeing Device Numbers

One of the first things your
Driver



Will need to do

When setting up a char
Device is to obtain one or more device numbers to work with.
Necessary function for this task is
Register_chrdev_region
, Which is declared in
<Linux/fs. h>
:

int register_chrdev_region



(dev_t first, unsigned int count,




char *name);

 

Here,First
Is the beginning

Device Number of the range you wowould
Like to allocate. The minor number portion
First
Is often0
, But there is
No requirement to that effect.Count
Is the total
Number of contiguous device numbers you are requesting. Note that, if
Count
Is large, the range you request cocould spill
Over to the next major number; but everything will still work
Properly as long as the number range you request is available.
Finally,Name
Is the name of the device that
Shocould be associated with this number range; it will appear in
/Proc/devices
And sysfs.

As with most kernel functions, the return value from
Register_chrdev_region
Will be
0
If the allocation was successfully saved med. In
Case of error, a negative error code will be returned, and you will
Not have access to the requested region.

Register_chrdev_region
Works well if you know
Ahead of time exactly which device numbers you want. Often, however,
You will not know which major numbers your device will use; there is
A constant effort within the Linux kernel development community
Move over to the use of dynamicly-allocated
Device numbers.
Kernel will happily allocate a major number for you on the fly,
You must request this allocation by using a different function:

int alloc_chrdev_region

(dev_t *dev, unsigned int firstminor,

unsigned int count, char *name);

After successful, the device Number of the device will be displayed in the/proc/device file.



 

With this function,Dev
Is an output-only
Parameter that will, on successful completion, hold the first number
In your allocated range.Firstminor
Shocould be
Requested first minor number to use; it is usually
0
.Count
And
Name
Parameters work like those given
Request_chrdev_region
.

Regardless of how you allocate your device numbers, you shoshould free
Them when they are no longer in use. device numbers are freed:

void unregister_chrdev_region

(dev_t first, unsigned int count);

 

The usual place to call unregister_chrdev_region
Wocould be in your module's cleanup function
.

 

The above functions allocate device numbers for your
Driver's use, but they do not tell the kernel
Anything about what you will actually do with those numbers. Before
User-space program can access one of those device numbers, your
Driver needs to connect them to its internal functions that implement
The device's operations. We will describe how this
Connection is accomplished shortly, but there are a couple
Necessary digressions to take care of first.

 

When a device file is accessed, the kernel uses the major number of the file to determine
Which driver shoshould be used to handle the access. This means that the kernel doesn't really need to use or even know
About the minor number. The driver itself is the only thing that cares about the minor number. It uses the minor
Number to distinguish between different pieces of hardware.

Kernel knows the major device number and uses this major device number to find the driver. The driver itself is used to differentiate specific hardware based on the minor device number.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.