Linux system driven Basic learning notes

Source: Internet
Author: User

Linux drivers:

Roles: Applications

Api
Operating system
Driven

Actual hardware


Function:
1. Initialize and release the device
2. Transfer data from the kernel to the hardware and from the hardware to read the data
3. Detection and handling of equipment errors

Linux Driver Type:
Character devices: Managed by the file system (accessed through device files)
Block devices: Managed by the file system
Network devices: Managed by the protocol stack (accessed via socket)


viewing system device Files
Ls-l/dev
Cat/proc/devices
Cat/sys/power/state

Properties: File type main device number secondary device number
Device number:<linux/types.h>
dev_t 32-bit, 12-main device number, 20-bit secondary device number
Actual use of:<linux/kdev_t.h>
dev_t---main device number, secondary device number
MAJOR (dev_t Dev);
MINOR (dev_t Dev);
Main device number, secondary device number---(dev_t)
MKDEV (int major,int minor)


Device-driven components:
1. Configure initialization, register the device, and detect if the hardware device is working properly.
2. Sub-programs serving I/O requests
3. Interrupt Service sub-procedures

1. System runtime compile and load, unload module: insmod,rmmod
2.Linux kernel source code compiled into modules, after starting with Insmod,rmmod
or load the kernel directly into the source code.

Output: Kernel programming cannot use printf () in user-State C library functions
Only use PRINTK (). Log information or a warning prompt for the logging mechanism. (var/log/messages)

Log level: Default:default_message_loglevel   4
<linux/kernel.h>
  #define kern_emerg      <0>     /* system is unusable */
#define kern_alert     "<1> "    /* action must be taken immediately */
#define kern_crit    " <2> "     /* Critical conditions */
#define kern_err      <3>      /* Error conditions */
#define kern_warning  "<4>"     /* WARNING conditions */
#define kern_notice   <5>     /* Normal but significant */
#define Kern_info      "<6>"     /* Informational */
#define kern_debug    " <7> "    /* debug-level messages */

When the priority is less than the INT console_loglevel variable, the message is printed to the console.
If the syslogd and Klogd daemons are running, the message can only be added to the Var/log/messages file
KLOGD only processes kernel information, SYSLOGD handles other system information, such as applications


Differences between drivers and applications:
1. Drive without main, with module initialization function as Portal
2. The driver is no longer running after initialization is complete, waiting for system calls
3. Driver cannot use standard C library


* Compile driver to kernel template:
1. While configuring Kconfig, open Linux-2.6.32.2/drivers/char/kconfig,
Find Mini2440_hello_module, copy a paragraph down, modify it into:
Config My_hello
TriState "My Hello module"
Depends on mach_mini2440
Default m if mach_mini2440
Help
Mini2440 Module Samples
2. In make menuconfig, you can see my hello module in device drivers-> Character devices
Select as M

3. Open Linux-2.6.32.2/drivers/char/makefile
Increase under Config_mini2440_hello_module:
Obj-s (Config_my_hello) +=hello.o

4. Go back to the root directory, execute: Make modules
Build: Hello.ko


The basic steps of compiling the template after the system is run:
1. Compiling
Make
That is equal to: make modules

In makefile:
First see Kernelrelease is in the Linux kernel source code in the main makefile is defined, if defined, the build target file
If undefined: definition: Host: Kerneldir? =/lib/modules/$ (Shell uname-r)/build
Development Board: Kerneldir? =/mnt/hgfs/linuxshared/linux-2.6.32/linux-2.6.32.2
That is, Kerneldir is the Linux kernel source path that the makefile relies on, and when cross-compiling, it takes the source path running on the development Board.

Make: $ (make)-C $ (Kerneldir) m=$ (PWD) modules
To the directory where the Linux source code executes the main makefile and the current path to the main makefile, tell the main makefile to return to the current directory after execution, execute makefile


2. Installing the module
Insmod Hello.ko
Modprobe-r filename Loads the driver and loads the other modules that the module depends on.

INSMOD Module Name Parameter name = parameter value

Define parameters:
Module_param (parameter name, parameter type, parameter read-write property)
static char *string_test = "This is a test";
static num_test = 1000;
Module_param (Num_test,int,s_irugo);
Module_param (Steing_test,charp,s_itugo);

3. Display Module
Lsmod |grep Hello
4. Deleting a module
Rmmod Hello

5. Get module information
Modinfo Hello.ko
6. Clear
Make clean

---character driven---

Operation Steps:
Detection and initialization of devices

************************************************************
static __init int myinit (void)
{
Device detection-related
1. Device's Common attribute assignment--device name, device number
2. Registration of the device

Device initialization
1. A series of spatial allocations used in the drive
2. Initialization of device-dependent resources-definition of GPIO port (attributes, Pins)
return 0;
}
static __exit void Myexit (void)
{
Logout device
Freeing resources
}
Implementation of IO interface
Processing of interrupts
***********************************************************
<linux/fs.h>
struct file_operations
{
struct module *owner;
loff_t (*llseek) (struct file *, loff_t, int);
ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
ssize_t (*aio_read) (struct KIOCB *, const struct IOVEC *, unsigned long, loff_t);
ssize_t (*aio_write) (struct KIOCB *, const struct IOVEC *, unsigned long, loff_t);
Int (*readdir) (struct file *, void *, filldir_t);
unsigned int (*poll) (struct file *, struct poll_table_struct *);
Int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
Long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
Long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
Int (*mmap) (struct file *, struct vm_area_struct *);
Int (*open) (struct inode *, struct file *);
Int (*flush) (struct file *, fl_owner_t ID);
Int (*release) (struct inode *, struct file *);
Int (*fsync) (struct file *, struct dentry *, int datasync);
Int (*aio_fsync) (struct KIOCB *, int datasync);
Int (*fasync) (int, struct file *, int);
Int (*lock) (struct file *, int, struct file_lock *);
ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
unsigned long (*get_unmapped_area) (struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
Int (*check_flags) (int);
Int (*flock) (struct file *, int, struct file_lock *);
ssize_t (*splice_write) (struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
ssize_t (*splice_read) (struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
Int (*setlease) (struct file *, long, struct file_lock * *);
};


struct file
{
 /*
  * Fu_list becomes invalid after File_free is called and queued via
  ; * Fu_rcuhead for RCU freeing
  */
 union
 {
  struct list_head fu_list;
  struct Rcu_head  fu_rcuhead;
 } F_u;
 struct path  f_path;
#define F_DENTRY&NBSP;F_PATH.DENTRY
#define F_VFSMNT&NBSP;F_PATH.MNT
 const struct file_operations  *f_op;
 spinlock_t  f_lock; /* f_ep_links, F_flags, no IRQ */
 atomic_long_t  f_ Count
 unsigned int   f_flags;
 fmode_t   f_mode;
 loff_t   f_pos;
 struct fown_struct f_owner;
 const struct cred *f_cred;
 struct File_ra_state f_ra;

U64 f_version;
#ifdef config_security
void *f_security;
#endif
/* Needed for TTY driver, and maybe others */
void *private_data;

#ifdef Config_epoll
/* Used by FS/EVENTPOLL.C to link all the hooks to this file */
struct List_head f_ep_links;
#endif/* #ifdef Config_epoll */
struct Address_space *f_mapping;
#ifdef Config_debug_writecount
unsigned long f_mnt_write_state;
#endif
};

FILE *FP;

<asm/uaccess.h>

unsigned long copy_from_user (void *to, const void *from, unsigned long n);

unsigned long copy_to_user (void * To, void * from, unsigned long len);


Globvar_driver.c after the build of the Ko file after loading, through the lsmod or cat/proc/devices can see that the device has been loaded in the system, but there is a build device file under/dev, you need to manually create the node
[Email protected] driver_2]# Mknod/dev/globalvar C 230 0
[Email protected] driver_2]# Ls/dev

---Linux2.6 kernel character driver---

<linux/cdev.h>
struct Cdev {
struct Kobject kobj; Each cdev is a kobject
struct module *owner; Point to the implementation-driven module
const struct File_operations *ops; How to manipulate this character device file
struct List_head list; Inode->i_devices of the Cdev corresponding to the character device file
dev_t Dev; Start Device number
unsigned int count; Device Range Number Size
};


Defines an object of type struct Cdev to represent the current device driver
Allocating space to Cdev objects
Initialize Cdev:cdev_init
Assigned device number: Regist_chrdev_region or alloc_chrdev_region
Device driver added to system: Cdev_add

Interface implementation

Logout: Cdev_del
Unregister_chrdev_region

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.