Linux 2.6 driver design Quick Start!

xiaoxiao2021-03-05  18

Author ray

Source www.rtems.net, author ray @ rtems

I don't want to talk nonsense in Linux 2.6 and 2.4, generally 2.6 is more functional, but the resource consumption is more.

Since the 2.6 kernel is on the driving frame, the underlying calls and 2.4 kernel have a lot of differences, this article is mainly guided by the programmer 2.4 to 2.6 migration.

2.6 and 2.4 mainly differentiated

• The API changes in the kernel have increased many new features (such as MEM POOL)

• Provide sysfs for describing equipment trees

• The drive module is from .o to .ko

Transplant hello word

Here is the simplest 2.4 driver:

#DEfine module #include #include int init_module (void) {printk (kern_info "hello, world / n"); return 0;} void cleanup_module (void) {printk (Kern_Info "Goodbye Cruel World / N);

2.6 Hello World version!

#include #include #include module_license ("gpl"); // New, otherwise there is Waring, remove #define module, automatic definition Static int hello_init (void) {printk (kern_lert "Hello, World / N"); return 0;} static void hello_exit (void) {printk (kern_alert "Goodbye, Cruel World / N");} Module_init (Hello_init); / / have to! ! Module_exit (Hello_exit); // must! !

Note that Module_init in 2.4 is not necessary, as long as the driven initialization function and the ditch function name the default init_module and cleanup_module

Compilation generation:

2.4

GCC -D__kernel__ -dmodule -i / usr / src / linux- 2.4.27 / include -o2 -c testmod.c

2.6

Obj-M in Makefile: = Hello.o

then:

Make -c / usr / src / linux- 2.6.11 subdirs = $ PWD MODULES (of course simple make can also be)

Haha is simple enough! !

Other differences:

counter:

Previous 2.4 kernel Use mod_inc_Use_count to increase counts, for example:

Void

INC_MOD (VOID)

{

MOD_INC_USE_COUNT;

} / * End inc_mod * /

/ ************************************************** **********************

* Calculator Dec

*********************************************************** ********************* /

Void

DEC_MOD (VOID)

{

MOD_DEC_USE_COUNT;} / * end Dec_mod * /

This is too time now !!

2.6, user function To load the module, use:

INT try_module_get (& module);

Function uninstall module

Module_put ()

And there is no need to display the definition INC and DEC in the drive

No kdev_t

Nowadays, DEV_T is now, and is 32-bit

Structural initialization

Old release:

STATIC STRUCT SOME_STRUCTURE = {

Field1: Value,

Field2: Value

}

Now popular:

STATIC STRUCT SOME_STRUCTURE = {

.field1 = value,

.field2 = value,

...

}

Malloc.h

Do you want to use a nuclear memory? Use well,

Memory pool

Memory management changes, adding Memory pool * (# include ). (Now everything is pool, memory thread ....) This is provided for block devices, using Mempool's biggest advantage is that allocation memory is not wrong, nor will it wait (how this is also RTEMS). There are still some use of Embed devices. Standard call mode:

Mempool_t * mempool_create (int min_nr, mempool_alloc_t * alloc_fn, mempool_free_t * free_fn, void * pool_data);

Use Mempool's function:

Mempool_alloc_t / mempool_free_t

Mempool_alloc_slab / mempool_free_slab

Mempool_alloc / mempool_free

Mempool_Resize

Structural body assignment

Previously, the structural body assignment of the drive was used:

STATIC STRUCT SOME_STRUCTURE = {

Field1: Value,

Field2: Value

}

Now use:

STATIC STRUCT SOME_STRUCTURE = {

.field1 = value,

.field2 = value,

...

}

E.g :

Static struct file_operations yourDev_file_ops = {

.open = yourDev_open,

.read = yourDev_read_file,

.write = yourDev_write_file,

}

min (), max ()

Many programmers define their MIN and MAX macros, everyone knows that the macro is not safe, Linux defines the MIN and MAX functions (note that the template function is required, you need to set the comparison type!)

Prototype

Call_usermodehelper ()

Request_module ()

The prototype of the function changes, use it quickly.!!

DEVFS

Hey, there is no use, it's the technology, this is Linux. Not our fluent, it is a world change

Character device

2.6 Callover from the device number is no longer limited to the original 8bit

Register_chrdev () can be upgraded to:

INT register_chrdev_region (dev_t from, // device number unsigned count, // registration number char * name); // Name This function dynamic version (do not know when the main device number)

Int Alloc_chrdev_region (dev_t * dev, unsigned baseminor,

Unsigned count, char * name);

New file_operations

Register_chrdev_region does not use the register_chrdev_region parameter because the device is now using Struct CDEV to define him in .

Struct cdev {

Struct kobject kobj;

Struct Module * Owner;

Struct File_Operations * OPS;

Struct list_head list;

DEV_T DEV;

Unsigned int count;

}

First need to be assigned space: struct cdev * cdev_alloc (void);

Then initialize it:

Void CDEV_INIT (STRUCT CDEV * CDEV, STRUCT FILE_OPERATIONS * FOPS);

CDEV uses a kObject_set_name setting name: for example:

Struct cdev * my_cdev = cdev_alloc (); kobject_set_name (& CDEV-> Kobj, "MY_CDEV% D", Devnum);

You can use int CDEV_ADD (Struct CDEV * CDEV, DEV_T dev, unsigned count); add CDEV to your system.

Delete CDEV

Void CDEV_DEL (Struct CDEV * CDEV);

For CDEVs that do not use CDEV_ADD, use KObject_put (& CDEV-> kobj); deletion is also used: struct kobject * cdev_get (struct cdev * cdev); void CDEV_PUT (Struct CDEV * CDEV);

Taiji chain;

Struct Inode -> i_cdev -> CDEV

This will be linked from inode to CDEV

Drive system:

/ dev to / sys

/ dev is too late, the device files are running to / sys!

# CD / SYS

# ls

Block bus class defices firmware

转载请注明原文地址:https://www.9cbs.com/read-38818.html

New Post(0)