Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Linux device driver development small example --LED lights     - MongoDB query statistics grouping remove duplicate records (Database)

- Linux Shell Scripting (Programming)

- C # mobile side and PC-side data exchange (Database)

- MySQL monitoring tool -Innotop (Database)

- Android recyclerview cardview (Programming)

- Select helps secure the system network management tools (Linux)

- Mutt - an email client that sends messages through the terminal (Linux)

- Android Application Development: an argument between Activity (Programming)

- Two kinds of agents of Spring AOP (Programming)

- ORA-12154 TNS could not resolve the specified identifier (Database)

- Installation of Python2.7.10 under CentOS 6.4 (Linux)

- The difference between IPython and Python (Linux)

- Installation image conversion tool on your Ubuntu Converseen 0.8.1 (Linux)

- Docker ecosystem security is gradually maturing (Server)

- jdbc Oracle database connection string writing pluggable (Database)

- MySQL Online DDL tools of pt-online-schema-change (Database)

- CentOS 6 Install Xen4.2 Virtualization Practice (Linux)

- Linux Network Programming --TCP and UDP datagram type Explanation (Programming)

- Grub2 Boots the openSUSE installation image (Linux)

- Nginx logging client ip (Server)

 
         
  Linux device driver development small example --LED lights
     
  Add Date : 2018-11-21      
         
         
         
  Led Linux device drivers
#ifndef __KERNEL__
# define __KERNEL__
#endif
#ifndef MODULE
# define MODULE
#endif
#include
#include
#include
#include
#include / * printk () * /
#include / * kmalloc () * /
#include / * everything ... * /
#include / * error codes * /
#include / * size_t * /
#include
#include / * O_ACCMODE * /
#include / * cOPY_TO_USER * /
#include / * cli (), * _flags * /
#include
#define DEVICE_NAME "demo"
#define led_MAJOR 212
#define led_MINOR 0
static int MAX_BUF_LEN = 1024;
static char drv_buf [1024];
static int WRI_LENGTH = 0;
static char combuf [2];
char base = 0x70;
char off = 0x07;
/ ****** ************************************************** ***************************** /
static ssize_t led_write (struct file * filp, const char * buffer, size_t count, loff_t * ppos )
{
copy_from_user (drv_buf, buffer, count);
combuf [0] = drv_buf [0];
combuf [1] = drv_buf [1];
WRI_LENGTH = count;
printk ( "user write data to driver n");
IIC_WriteSerial (base, off, combuf, 2);
return count;
}
/ ****** ************************************************** ***************************** /
static ssize_t led_read (struct file * filp, char * buffer, size_t count, loff_t * ppos)
{
return count;
}
/ ***************************** ************************************************** ****** /
static int led_ioctl (struct inode * inode, struct file * file,
unsigned int cmd, unsigned long arg)
{
printk ( "ioctl runing n" );
switch (cmd) {
case 1: printk ( "runing command 1 n"); break;
case 2: printk ( "runing command 2 n"); break;
default:
printk ( "error cmd number n"); break;
}
return 0;
} / **************** ************************************************** ******************* /
static int led_open (struct inode * inode, struct file * file)
{
sprintf (drv_buf, "device ! open sucess n ");
printk (" device open sucess n ");!
return 0;
}
/ ************* ************************************************** ********************** /
static int led_release (struct inode * inode, struct file * filp)
{
MOD_DEC_USE_COUNT; < BR> printk ( "device release n");
return 0;
}
/ ************************ ************************************************** *********** /
static struct file_operations demo_fops = {
owner: THIS_MODULE,
write: led_write,
read: led_read,
ioctl: led_ioctl, < BR> open: led_open,
release: led_release,
};

/ **************************** ************************************************** ******* /
#ifdef CONFIG_DEVFS_FS
static devfs_handle_t devfs_demo_dir, devfs_demoraw;
#endif
/ ****************** ************************************************** ***************** /
static int __init led_init (void)
{
int result;
SET_MODULE_OWNER (& demo_fops);
result = register_chrdev (led_MAJOR, "demo", & demo_fops);
if (result < 0) return result;
printk (DEVICE_NAME "initialized n");
return 0;
}
/ ******************************************** ***************************************** /
static void __exit led_exit ( void)
{
unregister_chrdev (led_MAJOR, "demo");
// kfree (demo_devices);
printk (DEVICE_NAME "unloaded n");
}
/ * ************************************************** ********************************** /
module_init (led_init);
module_exit (led_exit);
////////////////////////////////////////////// ////////////////////////////////

The following is a description of the procedures, are not part of the code:
important data structure
struct file data structure definitions at
include / fs.h
struct file structure associated with the drive member
read and write permissions mode_t f_mode ID file
loff_t f_pos current write position
unsigned int_f_flag file mark, mainly for blocking / non-blocking operation inspection
struct file_operation * f_op file operations structure pointer
void * private_data driver will generally point it has allocated data
struct dentry * f_dentry directory entry corresponding to the file structure

device driver Interface (struct file_operations), marking methods:

static struct file_operations demo_fops = {

owner: THIS_MODULE,

write: demo_write,

read: demo_read,

ioctl: demo_ioctl,

open: demo_open,

release: demo_release,

};

Device Driver Interface (struct file_operations)

commonly referred to as a device driver interface means struct file_operations {}, its definition is located in include / linux / fs.h in.

in the development of embedded systems, usually just a few interface functions to achieve the following functions of the system will be able to complete the required

When init load the driver (insmod), the kernel automatically call

read from the device to read data

write write data to a character device

ioctl control equipment, and other control commands other than read and write operations

open to open the device and initialize

release off the device and release resources

exit uninstall the driver (rmmod), the kernel automatically call

Driver registration process (dynamically assigned major number)

insmod module_name; the driver is loaded, run init function (register_chrdev (dev_Major, "module_name", * fs))

Check / proc / devices

mknod / dev / module_name c / b major device number minor device number

rmmod module_name; uninstall the driver, run the exit function (unregister_chrdev (dev_Major, "module_name", * fs))

     
         
         
         
  More:      
 
- Kubernetes resolve application deployment model (Server)
- ORA-600 [kcbz_check_objd_typ] Error Handling (Database)
- How to use the Linux command compress JPEG images (Linux)
- DataGuard the MRP can not start to analyze and solve problems (Database)
- To install and deploy Apache under the CentOS (Server)
- Present Situation and Development Trend of firewall products (Linux)
- Hadoop 2.6.0 standalone configuration and pseudo-distributed configuration under Ubuntu 14.04 (Server)
- Linux iptables firewall settings whitelist (RHEL 6 and CentOS 7) (Linux)
- Use XtraBackup be physical standby database MySQL (Database)
- After reloading the cinder-volume OpenStack not start properly (Server)
- CentOS x86 64bit upgrade to 2.7 Python2.6 (Linux)
- Linux Basics Tutorial: create your own Vim IDE (Linux)
- shell script error dirname: invalid option - b (Database)
- Modular JavaScript (Programming)
- 10 Regulation of painless SQL Schema (Database)
- zBackup: A versatile tool to remove duplicate backup (Linux)
- To execute the cp command prompt type skip folder under CentOS (Linux)
- Chromium Install Flash Official Guide (Linux)
- RPM package management tools under Linux (Linux)
- To create and manage virtual machines on Ubuntu Redhat (Linux)
     
           
     
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.