Operation of encapsulating the driver using the file_operations struct

Source: Internet
Author: User
I recently learned the Linux driver section and found that all the operation methods of the device are encapsulated into the file_operations structure, this struct provides a unified operation function interface for all device files. Then, combine the struct with the device's master device number and name (useless) and use the register_chrdev (0, "first_drv", & first_drv_fops) function) register the XX item of the character device array chrdev from the driver module to the kernel. The specific registration and usage are exactly the same as that of bare metal LCD and LCD controller! They all use the idea of Object-oriented Thinking and structured programming. 1. the driver is used as follows:
1/* 2 2018-10-18 3 features: the first driver; 4 */5 6 # include <Linux/module. h> 7 # include <Linux/kernel. h> 8 # include <Linux/Fs. h> 9 # include <Linux/init. h> 10 # include <Linux/delay. h> 11 # include <ASM/uaccess. h> 12 # include <ASM/IRQ. h> 13 # include <ASM/Io. h> 14 # include <ASM/ARCH/regs-gpio.h> 15 # include <ASM/hardware. h> 16 17 static int first_drv_open (struct inode * inode, struct file * file) 18 {19 printk ("first_drv_open \ n "); 20} 21 22 static ssize_t first_drv_write (struct file * file, const char _ USR * Buf, size_t count, loff_t * PPOs) 23 {24 printk ("first_drv_write \ n "); 25} 26 27 static struct file_operations first_drv_fops = 28 {29. owner = this_module, 30. open = first_drv_open, 31. write = first_drv_write32}; 33 34 int Major; 35 static int first_drv_init (void) 36 {37 major = register_chrdev (0, "first_drv", & first_drv_fops ); 38 firstdrv_class = class_create (this_module, "firstdrv"); 39 first_class_dev = class_device_create (firstdrv_class, null, mkdev (Major, 0), null, "XYZ "); 40 gpfcon = (volatile unsigned long x) ioremap (0x56000050, 16); 41 gpfdat = gpfcon + 1; 42 return 0; 43} 44 45 static void first_drv_exit (void) 46 {47 values (Major, "first_drv"); 48 values (values); 49 class_destroy (firstdrv_class); 50} 51 52 module_init (first_drv_init); 53 module_exit (first_drv_exit ); 54 module_license ("GPL ");

2. The file LCD. H is as follows:

1 # ifndef _ LCD _h 2 # DEFINE _ LCD _h 3 4 5 Enum {6 normal = 0, 7 invert = 1, 8}; 9 10/* normal: Normal Polarity 11 * invert: invert polarity 12 */13 typedef struct pins_polarity {14 int de;/* normal: data can be transmitted at high power usage */15 int pwren;/* normal: high Level valid */16 int vclk;/* normal: Get data on the descent edge */17 int RGB;/* normal: the high level indicates 1 */18 int hsync;/* normal: high pulse */19 int vsync;/* normal: High pulse */20} pins_polarity, * p_pins_polarity; 21 22 typedef struct time_sequence {23/* vertical direction */24 int TVP; /* vysnc pulse width */25 int TVB;/* black box above, vertical back porch */26 int tvf;/* black box below, vertical front porch */27 28/* horizontal direction */29 int THP;/* hsync pulse width */30 int THB;/* black box on the left, horizontal back porch */31 int THF;/* black box on the right, horizontal front porch */32 33 int vclk; 34} time_sequence, * p_time_sequence; 35 36 37 typedef struct LCD _params {38 char * Name; 39 40/* pin polarity */41 pins_polarity pins_pol; 42 43/* Timing */44 time_sequence time_seq; 45 46/* Resolution, BPP */47 int xres; 48 int yres; 49 int BPP; 50 51/* framebuffer address */52 unsigned int fb_base; 53} LCD _params, * p_ LCD _params; 54 55 void get_ LCD _params (unsigned int * fb_base, int * xres, int * yres, int * bpp); 56 57 # endif/* _ LCD _h */

3. The file LCD _4.3.c is as follows:

# Include "LCD. H "# define LCD _fb_base 0x33c00000lcd_params LCD _4_3_params = {. name = "LCD _4.3 ",. pins_pol = {. de = normal,/* normal: data can be transmitted during high-power periods */. pwren = normal,/* normal: valid at the High Level */. vclk = normal,/* normal: Get data on the descent edge */. RGB = normal,/* normal: The height indicates 1 */. hsync = invert,/* normal: High pulse */. vsync = invert,/* normal: High pulse */},. time_seq = {/* vertical direction */. TVP = 10,/* vysnc pulse width */. TVB = 2,/* black box above, vertical back porch */. tvf = 2,/* black box below, vertical front porch * // * horizontal direction */. THP = 41,/* hsync pulse width */. THB = 2,/* black box on the left, horizontal back porch */. THF = 2,/* black box on the right, horizontal front porch */. vclk = 9,/* MHz */},. xres = 480 ,. yres = 272 ,. bpp = 32,/* 16, no 24bpp */. fb_base = LCD _fb_base,}; void LCD _4_3_add (void) {register_ LCD (& LCD _4_3_params );}

 

Link: Coding Daquan recommends initialization when defining variables, but many people, especially newcomers, do not initialize struct or struct array definitions, or do not know how to initialize them.
1. Initialization
Copy the Code as follows:
Typedef struct _ test_t {
Int I;
Char C [10];
} Test_t;
Test_t GST = {1, "12345"}; // It can be initialized. set I to 1, and s to a string.
Test_t GST = {1}; // when the number of initialization tasks is less than the actual number, only the previous members are initialized.
Test_tgst = {. c = "12345"}; // select an initialization member.
2. Compound literal quantity.
GST = (test_t) {122, "1256"}; // This is a value assignment statement and can also be used as initialization. It can appear anywhere in the program.
Of course, you can also use the composite literal to initialize:
GST = (test_t) {. I = 122,. c = "123 "};
3. struct Array
It can be enclosed in multiple braces:
Test_t GST [10] = {{},{},{},{}}
You can also initialize one of the elements:
Test_t GST [10] = {[2] = {}, [3] = {}}
You can also use the composite literal:
Test_t GST [10] = {[2]. I = 0, [3]. I = {}}
Why initialize:
1. Local variable initialization can prevent the occurrence of random values.
2. You can tell the compiler that the global variable Initialization is a definition rather than a declaration. (If two C instances have the same global variable definition and are not initialized, the compiler considers the second one declaration rather than definition .)

Operation of encapsulating the driver using the file_operations struct

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.