"Linux development" Linux Device Drive inductive summary (eight): 3. Equipment management layering and object-oriented thinking __php

Source: Internet
Author: User

Linux Device driver inductive summary (eight): 3. The layering and object-oriented thought of equipment management


Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

The previous section describes the relationships and operations of the bus, device, and driver functions. Starting from this section, we introduce the layered thinking and object-oriented thinking in equipment management (the name was fabricated by myself, "LDD" refers to the structure of the body embedded). Understandably, an excess of platform-class devices (platform).

Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx


One, the equipment management stratification


Recall that before the device and driver functions were registered, they were the bus they were assigned to. However, kernel developers feel that this approach is not good and should be provided by the bus to provide device and driver registration functions. When the device and the driver need to register to the specified bus, it must use the registration function that the bus provides for the device and the driver.


So, revise the bus.c of the previous section as follows:

/*8th_devmodule_3/1st/bus.c*/

21/* The device registration function provided by the bus * *

int usb_device_register (struct device *dev)

23 {

Dev->bus = &usb_bus; The bus for the device device is Usb_bus

Device_register (Dev); Register this device

26}

usb_device_unregister void (struct device *dev)

28 {

Device_unregister (Dev);

30}

Export_symbol (Usb_device_register);

Export_symbol (Usb_device_unregister);

33/* Bus-provided driver registration function * *

int Usb_driver_register (struct device_driver *drv)

35 {

Drv->bus = &usb_bus; Set the driver bus to Usb_bus

Panax Notoginseng return Driver_register (DRV); Register this driver

38}

Usb_driver_unregister (struct device_driver *drv)

40 {

Driver_unregister (DRV);

42}

Export_symbol (Usb_driver_register);

Export_symbol (Usb_driver_unregister);

Write a bus.h that allows the device and the driver function to contain the header file to use the bus-supplied driver function.

/*8th_devmodule_3/1st/bus.h*/

1 #ifndef _bus_h

2 #define _bus_h

3

4 int usb_device_register (struct device *dev);

5 void Usb_device_unregister (struct device *dev);

6

7 int usb_driver_register (struct device_driver *drv);

8 void Usb_driver_unregister (struct device_driver *drv);

9 #endif/* _bus_h * *

The above program can see, actually also did not do anything, just by the bus to encapsulate and to the device and drive functions to provide registration functions, easy to manage. Unlike before, devices and drivers can register to the specified bus as long as they know the name of the bus.

Again, modify the code for the device and the driver function:

/*8th_devmodule_3/1st/device.c*/

11/* Structure body does not need to specify the bus members, to Usb_device_register to complete * *

struct device Usb_device = {

bus_id = "Usb_mouse",

Release = Usb_dev_release,//must have a release function, or the uninstall will be wrong

15};

16

The static int __init usb_device_init (void)

18 {

int ret;

20

ret = Usb_device_register (&usb_device);

(ret) {

PRINTK ("Device register failed!\n");

return ret;

25}

26

PRINTK ("USB device init\n");

return 0;

29}

30

to static void __exit usb_device_exit (void)

32 {

Usb_device_unregister (&usb_device);

PRINTK ("USB device bye!\n");

35}


/*8th_devmodule_3/1st/driver.c*/

24/* Structure body does not need to specify the bus members, to Usb_device_register to complete * *

struct Device_driver usb_driver = {

Name = "Usb_mouse",//driver directory names in/sys/

Probe = Usb_driver_probe,

remove = Usb_driver_remove,

29};

30

-Static int __init usb_driver_init (void)

32 {

int ret;

34/* Driver registration, after the successful registration in the/sys/bus/usb/driver directory to create a directory usb_mouse*/

ret = Usb_driver_register (&usb_driver);

if (ret) {

Panax Notoginseng PRINTK ("Driver Register failed!\n");

return to RET;

39}

PRINTK ("USB driver init\n");

0;

42}

43

The static void __exit usb_driver_exit (void)

45 {

Usb_driver_unregister (&usb_driver);

PRINTK ("USB driver bye!\n");

48}

Modified, verified, the effect and the same as before, I do not explain in detail:

[root:1st]# insmod Bus.ko

USB Bus Init

[root:1st]# insmod Device.ko

USB Device Init

[root:1st]# insmod Driver.ko

Match success

Match success

Init USB Mouse

USB Driver Init

[root:1st]# rmmod Device

Remove Mouse Driver

Release

USB Device bye!

[root:1st]# rmmod Driver

USB Driver bye!

[root:1st]# Rmmod Bus

USB Bus bye!


Second, object-oriented thought--structure embedding


The device structure contains the basic information of the equipment model respectively. Most subsystems then also record information that is outside the structure but related to the device. Therefore, simply using a device structure to represent the device is very rare, but the device structure of the body embedded into other structures. Of course, Device_driver is the same.


Next, I encapsulate the structure of the device and the driver function:

/*8th_devmodule_3/2nd/bus.h*/

4 struct usb_device{//containing device structure in Usb_device

5 unsigned long phys, virt; Storage of device and physical address and corresponding virtual address

6 int IRQ; The interrupt number of the storage device

7 int VendorID, DeviceID; Manufacturer number and unit number of the storage equipment

8

9 struct device dev;

10};

11

struct usb_driver{//containing device_driver structure in Usb_driver

int VendorID, DeviceID;

14

<

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.