A preliminary study on Linux Drive model 4--Summary and supplements

Source: Internet
Author: User

A preliminary study on Linux Drive model 4--summary and supplements
1, directly on code
#include <linux/device.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/stat.h>
#include <linux/slab.h>
#include <linux/kobject.h>
#include <linux/kdev_t.h>
#include "./kf_device.h"

#define KFMAJOR 96
-----\\class Moduel
static struct class kf_class={
. Name = "Kf-class",
. Owner = This_module,
};
static int Kf_class_register (void) {
int ret =-1;
ret = Class_register (&kf_class);
Kf_class = Class_create (This_module, "Kfclass");
if (Ret < 0) {
PRINTK ("Kf-class register error!\n");
Return Kf_class_unregister (&kf_class);
}
PRINTK ("Kf-class register ok!\n");
return 0;
}

static void Kf_class_unregister (void) {
Class_unregister (&kf_class);
Class_destroy (&kf_class);
}
------\\bus Moduel
static int Kf_bus_type_match (struct device *dev, struct device_driver *drv)
{
PRINTK ("Enter%s\n", __func__);
return 1;
}
/*static int kf_bus_type_probe (struct device *dev) {
int ret =-1;
PRINTK ("Enter%s\n", __func__);
if (dev->driver->probe)
ret = Dev->driver->probe (dev);
return ret;
}*/
static void Kf_bus_release (struct device *dev) {
Need to do
PRINTK ("%s\n", __func__);
}

static struct Bus_type Kf_bus_type = {

. Name = "Kf-bus-type",
. match = Kf_bus_type_match,
. Probe = Kf_bus_type_probe,
};
static struct Device Kf_bus = {
. Init_name = "Kf-bus",
. Release = Kf_bus_release,
};

static int Kf_bus_register (void) {
int bus_ret =-1;
Kf_bus.class = &kf_class;
Bus_ret = Device_register (&kf_bus);

if (Bus_ret < 0) {
PRINTK ("Bus device Reister error!\n");
}
Bus_ret = Bus_register (&kf_bus_type);
if (Bus_ret < 0) {
PRINTK ("Bus type Reister error!\n");
}
PRINTK ("Kf_bus & Kf_bus_type Register ok\n");
return bus_ret;
}
static void Kf_bus_unregister (void) {
Bus_unregister (&kf_bus_type);
Device_unregister (&kf_bus);
}

-----\\device Module
static void Kf_device_release (struct device *dev) {
PRINTK ("%s\n", __func__);


}
static struct device kf_device={
. Init_name = "Kf-device0",
. devt = MKDEV (kfmajor,1),
. Release = Kf_device_release,
};
static int Kf_device_register (void) {

int ret =-1;
Does not point the This class
Kf_device.class = &kf_class;
Kf_device.bus = &kf_bus_type;
Kf_device.parent = &kf_bus;
ret = Device_register (&kf_device);
if (Ret < 0) {
PRINTK ("KF device Reister error!\n");
}
PRINTK ("KF device Reister ok!\n");
return ret;
}
static void Kf_device_unregister (void) {
Device_unregister (&kf_device);
}


------\\driver Module
/*
struct kf_device_driver{
Char *name;
struct Device_driver driver;
};*/
static int kf_device_driver_probe (struct device *dev) {
PRINTK ("Enter drive probe\n");
return 0;

}

static struct Device_driver kf_device_driver={
. Name = "Kf-device-driver",
. Probe = Kf_device_driver_probe,

};
static int Kf_device_dirver_register (void) {
int ret =-1;
Kf_device_driver.bus = &kf_bus_type;
ret = Driver_register (&kf_device_driver);
PRINTK ("Kf_device_driver reister ok!\n");
return ret;
}

static void Kf_device_dirver_unregister (void) {
Driver_unregister (&kf_device_driver);
}

-----\\init Func
static int __init kf_bus_init (void)
{
int Com_ret;
PRINTK (">>>kf_bus_init successed!! \ n ");
Com_ret =kf_class_register ();
Com_ret = Kf_bus_register ();
Com_ret = Kf_device_register ();
Com_ret = Kf_device_dirver_register ();
if (Com_ret < 0) {
PRINTK (">>>kf_bus_register error\n");
Kf_bus_unregister ();
}
return 0;
}

static void __exit kf_bus_exit (void)
{

PRINTK (">>>kf_bus_exit successed!! \ n ");
Kf_device_dirver_unregister ();
Kf_device_unregister ();
Kf_bus_unregister ();
Kf_class_unregister ();
}
Module_init (Kf_bus_init);
Module_exit (Kf_bus_exit);

Module_author ("Koffuxu");
Module_license ("GPL");

2, now it's insmod successful,look at the various directories of the SYS file system. Bus'smatch is successful and there will be a soft link underneath the device.At the same time, there are soft links in driver. in the bus directory:in the device directory:

In the class directory: null. Because no related bus is specified
3, Wordy, uevent moduletwo ways to inform the user layer, in the Android system, these two ways coexist. Here's enough to write a piece.int kobject_uevent_env (struct kobject *kobj, enum kobject_action Action,char *envp_ext[])Way One,/ * Send NetLink message is actually socket communication * /netlink_broadcast_filtered (uevent_sock, SKB, 0, 1, gfp_kernel,kobj_bcast_filter, kobj); @af_netlink. Cmode two,/ * Call Uevent_helper, usually only enabled during early boot invoke uevent_helper function * /Call_usermodehelper (argv[0], argv, ENV->ENVP, umh_wait_exec); @kmod. C
4,UML Read the picture (the picture is not good, since all painted, it is still affixed to it)all *_private members and their referenced structs are references to each other5,device How do I specify a class?Remember, do not assign device bus and stuct device Dev to a class, or duplicate the directory to cause the DEV register to fail! prompt error like this
6, if a class (Kf-class) is specified, the device is moved from the Sys/device directory to the/sys/device/virtual/kf-class/this is because of the operation in this function. device_add_class_symlinks (dev);device_add_attrs (dev);7, if you use Mkdev. devt = MKDEV (kfmajor,1),8,struct device devices must implement the release function, or Rmmond will error
The drive Model section has been completed. The level is limited, the time is hasty, certainly has many mistakes, if affects the reader to have any question, everybody discusses the study together. do you have a cigarette? I want to smoke one.

A preliminary study of the

Linux Driver Model 4--summary and supplements

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.