Driver Programming class_create description

Source: Internet
Author: User

After a Linux kernel version of 2.6, devfs no longer exists, and udev becomes a replacement for devfs. Note that udev is at the application layer. Do not try to find it in the Kernel configuration options. Adding support for udev is very simple. Take the driver of a character device written by the author as an example, in the driver initialization code, call class_create to create a class for the device and call device_create to create the corresponding device for each device. The general usage is as follows:

Struct class * myclass;
Class_create (this_module, "my_device_driver ");
Device_create (myclass, null, mkdev (major_num, minor_num), null, "my_device ");

When such a module is loaded, udev daemon will automatically create the my_device file under/dev.

When we first wrote a Linux Device Driver, we often used the mknod command to manually create a device node. In fact, the Linux Kernel provides us with a set of functions, it can be used to automatically create a device node in the/dev directory when the module is loaded, and delete the node when the module is detached. Of course, the prerequisite is that the user space is transplanted with udev.

The struct class struct is defined in the kernel. As the name suggests, a struct class struct type variable corresponds to a class, and the kernel also provides class_create (...) Function, you can use it to create a class, which is stored under sysfs. Once this class is created, call device_create (...) Function to create a device node in the/dev directory. In this way, when the module is loaded, udev in the user space will automatically respond to device_create (...) Function to find the corresponding class under/sysfs to create a device node.

Note: In earlier kernel 2.6, device_create (...) The function name is class_device_create (...), Therefore, the module program compiled in the new kernel sometimes reports an error because the function names are different and the parameter settings are also changed.

Struct class and device_create (...) And device_create (...) Are defined in/include/Linux/device. H. You must include this header file when using it. Otherwise, the compiler reports an error.

In kernel 2.6.35, struct class is defined in the header file include/Linux/device. h.
/*
* Device classes
*/
Struct class {
Const char * Name;
Struct module * owner;
Struct class_attribute * class_attrs;
Struct device_attribute * dev_attrs;
Struct kobject * dev_kobj;
INT (* dev_uevent) (struct device * Dev, struct kobj_uevent_env * env );
Char * (* devnode) (struct device * Dev, mode_t * mode );
Void (* class_release) (struct class * class );
Void (* dev_release) (struct device * Dev );
INT (* suspend) (struct device * Dev, pm_message_t State );
INT (* resume) (struct device * Dev );
Const struct kobj_ns_type_operations * ns_type;
Const void * (* namespace) (struct device * Dev );
Const struct dev_pm_ops * PM;
Struct class_private * P;
};
Class_create (...) In/Drivers/base/class. C:
/**
* Class_create-create a struct Class Structure
* @ Owner: pointer to the module that is to "own" This struct class
* @ Name: pointer to a string for the name of this class.
;*
* This is used to create a struct class pointer that can then be used
* In callto device_create ().
*
* Note, the pointer created here is to be destroyed when finished
* Making a call to class_destroy ().
*/
Struct class * _ class_create (struct module * owner, const char * Name,
Struct lock_class_key * key)
{
Struct class * CLS;
Int retval;
CLS = kzarloc (sizeof (* CLs), gfp_kernel );
If (! CLs ){
Retval =-enomem;
Goto error;
}
CLS-> name = Name;
CLS-> owner = owner;
CLS-> class_release = class_create_release;
Retval = _ class_register (CLS, key );
If (retval)
Goto error;
Return CLS;
Error:
Kfree (CLS );
Return err_ptr (retval );
}

The first parameter specifies the module of the class owner and the second parameter specifies the class name.

In class. C, class_destroy (…) is also defined (...) Function, used to delete a class when the module is detached.

Device_create (...) Functions are implemented in/Drivers/base/CORE. C:
/**
* Device_create-creates a device and registers it with sysfs
* @ Class: pointer to the struct class that this device shoshould be registered
* @ Parent: pointer to the parent struct device of this new device, if any
* @ Devt: The dev_t for the char device to be added
* @ FMT: string for the device's name
*
* This function can be used by char device classes. A struct Device
* Will be created in sysfs, registered to the specified class.
*
* A "Dev" file will be created, showing the dev_t for the device, if
* The dev_t is not 0, 0.
* If a pointer to a parent struct device is passed in, the newly created
* Struct device will be a child of that device in sysfs.
* The pointer to the struct device will be returned from the call.
* Any further sysfs files that might be required can be created using this
* Pointer.
*
* Note: The struct class passed to this function must have previusly
* Been created with a call to class_create ().
*/
Struct device * device_create (struct class * class, struct device * parent, dev_t devt, void * drvdata, const char * FMT ,...)
{
Va_list vargs;
Struct device * dev;
Va_start (vargs, FMT );
Dev = device_create_vargs (class, parent, devt, drvdata, FMT, vargs );
Va_end (vargs );
Return dev;
}

The first parameter specifies the class of the device to be created. The second parameter is the parent device of the device. If not, it is null. The third parameter is the device number, the fourth parameter is the device name, and the fifth parameter is the slave device number.

The following uses a simple character device driver to demonstrate how to use these functions:
/*
* Copyright (c) 2005 farsight
*
* This program is free software; you can redistribute it and/or modify
* It under the terms of the GNU General Public License as published
* The Free Software Foundation; either version 2 of the license, or
* (At your option) any later version.
* This program is distributed in the hope that it will be useful,
* But without any warranty; without even the implied warranty
* Merchantability or fitness for a special purpose. See
* GNU General Public License for more details.
* You shoshould have written ed a copy of the GNU General Public License
* Along with this program; if not, write to the Free Software
* Foundation, inc., 51 Franklin St, th Floor, Boston, MA 02110-1301 USA
*
*/
# Include <Linux/module. h>
# Include <Linux/kernel. h>
# Include <Linux/init. h>
# Include <Linux/fs. h>
# Include <Linux/cdev. h>
# Include <ASM/uaccess. h>
# Include <Linux/device. h>
Module_license ("GPL ");
Int hello_major= 250;
Int hello_minor = 0;
Int number_of_devices = 1;
Struct cdev;
Dev_t Dev = 0;
Struct file_operations hello_fops = {
. Owner = this_module,
};
Struct class * my_class;
Static void char_reg_setup_cdev (void)
{
Int error, devno = mkdev (hello_major, hello_minor );
Cdev_init (& cdev, & hello_fops );
Cdev. Owner = this_module;
Cdev. Ops = & hello_fops;
Error = cdev_add (& cdev, devno, 1 );
If (error)
Printk (kern_notice "error % d adding char_reg_setup_cdev", error );
/* Creating your own class */
My_class = class_create (this_module, "farsight_class ");
If (is_err (my_class )){
Printk ("Err: failed in creating class. \ n ");
Return;
}
/* Register Your Own Device in sysfs, and this will cause udevd to create corresponding device node */
Device_create (my_class, null, devno, null, "hello ");
}
Static int _ init hello_2_init (void)
{
Int result;
Dev = mkdev (hello_major, hello_minor );
Result = register_chrdev_region (Dev, number_of_devices, "test ");
If (result <0 ){
Printk (kern_warning "Hello: Can't Get Major Number % d \ n", hello_major );
Return result;
}
Char_reg_setup_cdev ();
Printk (kern_info "char device registered \ n ");
Return 0;
}
Static void _ exit hello_2_exit (void)
{
Dev_t devno = mkdev (hello_major, hello_minor );
Cdev_del (& cdev );
Unregister_chrdev_region (devno, number_of_devices );
Device_destroy (my_class, devno );
Class_destroy (my_class );
}
Module_init (hello_2_init );
Module_exit (hello_2_exit );

When the module is loaded, it will be in the/dev/Hello Device File

Driver Programming class_create description

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.