Kernel module programming (3): Get (assign or register) the device number

Source: Internet
Author: User

This article is one of the Reading Notes of char drivers in chapter 3 of Linux device drivers.

We can view the device node in/dev. Each device has a primary number (Major) and a secondary number (Minor). Generally, a major number corresponds to a certain device, although Linux allows multiple devices to share a major number. The minor number is used for the specific device correspondence of the kernel. The kernel does not know the specific application of the device following the minor.

In a kenrel Module Program, we need to map our program to one or more device nodes. In this scull example, a char-type device node is used, which is a part of the memory (so that no special device is needed ). These devices may already have a fixed primary number, or they may not be automatically assigned by the system. Use the following example to learn.

  

The header file scull. H is as follows: if major is not 0, it is the specified major number. If it is 0, it is dynamically allocated by the system. Our module consists of four devices, with the minor number ranging from 0 to 3.


# Ifndef _ WEI_SCULL_H

# Define _ WEI_SCULL_H

# Define SCULL_MAJOR 0

# Define SCULL_MINOR_MIN 0

# Define SCULL_DEV_NUM 4

# Endif

The source file scull. C is as follows. We will learn three functions:

Int register_chrdev_region (dev_t first, unsigned int count, char * Name );

Int alloc_chrdev_region (dev_t * Dev, unsigned int firstminor, unisgned int count, char * Name );

Void unregister_chrdev_region (dev_t first, unsigned int count );


# Include <linux/init. h>

# Include <linux/module. h>

# Include <linux/fs. h>

# Include "scull. h"

MODULE_LICENSE ("Dual BSD/GPL ");

Dev_t dev;

Int is_get_dev =-1;

Static int scull_major = SCULL_MAJOR;

Static int _ init scull_init (void)

{

Printk ("Scull module init enter/n ");

If (scull_major) {/* obtain dev based on the major and minor numbers */

Dev =MKDEV (Scull_major, SCULL_MINOR_MIN

);

Is_get_dev =Register_chrdev_region

(Dev, SCULL_DEV_NUM, "scull"); // register

} Else {/* dynamic system allocation */

Is_get_dev =Alloc_chrdev_region

(& Dev, SCULL_MINOR_MIN, SCULL_DEV_NUM, "scull ");

Scull_major =MAJOR (Dev

);

// Obtain the primary number. MINOR () can obtain the minor number.

}

If (is_get_dev <0 ){

Printk (KERN_WARNING "scull: can't get device major number % d/n", scull_major );

}

Return is_get_dev;

}


/* When the module ends, the registered resources should be released */

Static void _ exit scull_exit (void)

{

If (is_get_dev <0 ){

Return;

} Else {

Unregister_chrdev_region (Dev, SCULL_DEV_NUM

);

Printk ("scull module exit/N ");

}

}

Module_init (scull_init );

Module_exit (scull_exit );

If these device nodes already exist in the system, you can view them in/dev, or you can view the corresponding major number and device name in/proc/devices. We manually go to mknod our device node. In this case, the major number is the master number we manually configured in scull. h macro definition, or we can import it as a parameter when loading a module. If a device node is dynamically allocated by the system, you need to wait for the system to allocate the device node. We can use scripts to complete this task. The following is the script for Loading modules and automatically creating nodes: scull_load.


[Wei @ Wei ~] $ Cat scull_load

#! /Bin/sh

Module = "scull"

Device = "scull"

Mode = "644"

# Load the node. If the load fails (it may have been loaded), exit.

/Sbin/insmod./$ module. Ko $ * | Exit 1

# If/dev contains the previously created nodes and deletes them, we can also determine whether the major number is consistent with the assigned number to decide whether to retain or delete it.

Rm-F/dev/$ {Device} [0-3]

# This is the process of obtaining the major number, the use of awk, can refer to the document: http://www.ibm.com/developerworks/cn/linux/shell/awk/awk-1/
 

Use/proc/devices as the input and input by line. If the second parameter matches $ module, print the first parameter, that is, assign the value to Major.

Major = $ (awk "/$2 =/" $ module/"{print/$1}"/proc/devices)

Echo major =$ {major}


# Mknod four device nodes

Mknod/dev/$ {Device} 0 C $ major 0

Mknod/dev/$ {Device} 1 C $ major 1

Mknod/dev/$ {Device} 2 C $ major 2

Mknod/dev/$ {Device} 3 C $ major 3

# In general, the Operation permission of the kernel module is root, but sometimes we want some users to have the read permission and need to modify the permission of the device node. The following operations can be performed on the group Wei. If the group Wei does not exist, the group is moblin.

Group = "wei"

Grep-Q '^ Wei:'/etc/group | group = "moblin"

Chgrp $ group/dev/$ {Device} [0-3]

Chmod $ mode/dev/$ {Device} [0-3]

The following is the script scull_unload to uninstall. We need to delete the created device node.

[Wei @ Wei ~] $ Cat scull_unload

#! /Bin/sh

Module = "scull"

Device = "scull"

# If the module is not loaded, uninstall the module and delete the relevant nodes.

/Sbin/lsmod | grep $ module | Exit 1


/Sbin/rmmod $ Module

Rm-f/dev/$ {device} [0-3]

Link: My articles related to the kernel module


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.