7. Character Device system

Source: Internet
Author: User

One, character device-driven structure
1. Cdev Structural Body

struct Cdev {
struct Kobject kobj; Inline Kobject Objects
struct module *owner; The module to which it belongs
const struct File_operations *ops; File Operation structure Body
struct List_head list; The kernel maintains your linked list
dev_t Dev; Device number, consisting of the primary and secondary equipment number
unsigned int count;
};

In this structure, dev_t Dev, is the device number, which consists of 32 bits, consisting of two parts, both the main device number and the secondary device number, 12 bits is the main device number, 20-bit device number can use the following macro to obtain primary and secondary device number:

MAJOR (dev_t Dev)//Get the main device number

MINOR (dev_t Dev)//Get secondary device number

If you want to combine them into a dev_t type:

MKDEV (int major, int minor); Get the device number

2, the operation of Cdev equipment

struct cdev*cdev_alloc (void);//Request Memory

void Cdev_init (struct cdev *cdev, struct file_operation * fops)//FoPs with Cdev device bindings

int Cdev_add (struct cdev *cdev,dev_t devt, unsigned num); Registration of character devices

void Cdev_del (struct cdev *cdev)//Logoff

Cdev_init function, establish cdev and file operation pointer Connection, CDEV_ADD is to register the character device to the system, Cdev_add inside a parameter is dev_t, so must first get to the device number. Cdev) _del unregisters a device, typically in a function that is unloaded by a character device.

Cdev before registering, you need to obtain the device number:

int Register_chrdev_region (dev_t from, unsigned long count, const char * name)

int alloc_chrdev_region (dev_t *dev, unsigned minor, unsigned count, const char * name)

Register_chrdev_region is when the device number from is already determined, count is the number of applications, name: name, these names, will eventually be displayed in the/proc/devices.

Alloc_chrdev_region, generally use this, because generally do not know the device number, minor: application of the smallest secondary device number, count number, name name.

To complete the registration of the device number, Logout is:

void Unregister_chrdev_region (dev_t from,unsigned count);

3. Character device operation Flow

struct Cdev *mycdev = NULL; 1. Define CDEV Equipment

static int __init xxx_init (void)//inlet

{

Mycdev = Cdev_alloc (); 2. Assigning addresses

Cdev_init (Mycdev, &fops); 3. Initialize, bind the file operation structure

ret = alloc_cdev_region (&xxx_dev_no, 0, 1, "Mycdev"); 4. Assign the device number

Mycdev->owner = This_module; 5, the rest of the parameters of the designation

Cdev_add (Mycdev, Xxx_dev_no, 1); 6, registration equipment;

}

static void __exit xxx_exit (void)

{

Unregister_chrdev_region (Xxx_dev_no, 1);

Cdev_del (Mycdev);

}

Module_init (Xxx_init);

Module_exit (Xxx_exit);

Second, the important structural body
1. File_operation Structural Body

The member function of File_operation is the interface between the character device driver and the kernel virtual file system, in general, provides the common functions such as read, write, ioctl and so on.

struct File_operations {

loff_t (*llseek) (struct file *, loff_t, int);

ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);

ssize_t (*aio_read) (struct KIOCB *, Char __user *, size_t, loff_t);

ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);

ssize_t (*aio_write) (struct KIOCB *, const char __user *, size_t, loff_t);

unsigned int (*poll) (struct file *, struct poll_table_struct *);

Int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);

Int (*open) (struct inode *, struct file *);

Int (*release) (struct inode *, struct file *);

};

ssize_t (*read) (struct file *filp, char __user * buf, size_t count, loff_t * f_pos);

File: Structure, will learn in the next position,

__user: As a special note, that BUF is the memory address of the user space, it is important to note that the address of the user space and the address of the kernel space, is not directly read and write

Count: The number of bytes to write to buf,

F_pos: The location of the file pointer,

succeeds, the number of bytes written is returned, and the failure returns EINVAL

ssize_t (*write) (struct file * filp, const char __user * buf, size_t count, loff_t *fpos);

__user: buf of user space

Count; Number of bytes written to BUF

FPOs: File pointer

The data of user space and kernel space can only be exchanged between kernel space and data space through COPY_TO_USR or COPY_FROM_USR.

Int (*open) (struct inode *, struct file *);

Inode: struct-Body pointer

File: pointer

2. File structure

When a file is opened, the system will have an associated struct file for each open file in the kernel space. She was created by the kernel when the file was opened, and the struct struct file was passed to any function to manipulate the file when it was going to do something else. The file structure contains more than N information about the files,

struct File {
Union {
struct List_head fu_list;
struct Rcu_head fu_rcuhead;
} F_u;
struct path F_path;
#define F_dentry F_path.dentry
#define F_VFSMNT F_path.mnt
const struct File_operations *f_op; File-managed operations
spinlock_t F_lock; /* F_ep_links, F_flags, no IRQ */
#ifdef CONFIG_SMP
int f_sb_list_cpu;
#endif
atomic_long_t F_count;
unsigned int f_flags; The document's logo, O_rdonly,o_nonblock,
fmode_t F_mode; Read or write mode of the file, Fmode_read,fmode_write
loff_t F_pos; Pointer to a file
struct Fown_struct f_owner;
const struct CRED *f_cred;
struct File_ra_state F_ra;

U64 f_version;
#ifdef config_security
void *f_security;
#endif
/* Needed for TTY driver, and maybe others */
void *private_data; Private data for a file

#ifdef Config_epoll
/* Used by FS/EVENTPOLL.C to link all the hooks to this file */
struct List_head f_ep_links;
#endif/* #ifdef Config_epoll */
struct Address_space *f_mapping;
#ifdef Config_debug_writecount
unsigned long f_mnt_write_state;
#endif
};

There is a lot of information in the file structure, but less commonly used, when opening files, file filled information, we can go to use, for example, we can determine whether the non-blocking mode:

if (File->flags & O_nonblock)

printf ("Nonblock \ n");

Else

printf ("BLOCK \ n");

And the private data inside file void *private_data, this pointer, generally point to save the custom device structure of the address, when read or write, and then from this private pointer, get to the automatic device structure.

3. Inode structure

When the file is saved on disk, the INODE structure holds information such as the properties of the file access, the owner, size, access time, generation time, last modified time, and so on. She is the most basic unit of the Linux managed file system.

struct Inode {
     umode_t             i_mode;        
    uid_t             I_uid;
    gid_t            I_gid;
     struct mutex        i_mutex;

    dev_t            i_rdev;                     Device number for    //file
    struct timespec        i_ atime;      //Access last access time
    struct timespec         i_mtime;     //MoD Last modification time
    struct timespec        i_ctime;    //  inode generation time, create

Union {
struct Pipe_inode_info *i_pipe;
struct Block_device *i_bdev;
struct Cdev *i_cdev; Character device
};

In the above information, the most useful for writing drivers is the dev_t I_rdev; The document's device number, struct Cdev *i_cdev, can be recorded by:

unsigned int imonir (struct inode * inode)//Get secondary device number

unsigned int imajor (struct inode * inode)//Get main device number

You can also view the status of a file by using the stat + File command:

[Email protected] 3516c]$ stat Gpio.ko
File: "Gpio.ko"
size:7757 blocks:16 IO block:4096 general file
device:801h/2049d inode:358613086 links:1
Access: (0664/-rw-rw-r--) Uid: (566/carlos) Gid: (566/carlos)
access:2016-05-07 15:29:55.000000000 +0800
modify:2016-05-06 13:31:14.000000000 +0800
change:2016-05-06 13:31:14.000000000 +0800

In fact, it also gets the inode information.

7. Character Device system

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.