Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Linux device driver development small example --LED lights     - Use SecureCRT to transfer files between local and remote hosts (Linux)

- Fragment Android developers learning to resolve (Programming)

- Log analysis is done with Grafana Elasticsearch (Server)

- How to use nmap command in Linux (Linux)

- Scala REPL Shell call (Programming)

- Shell scripts to copy all directories under the current directory of a certain type of file to the same directory (Linux)

- How to adjust the system time CentOS (Linux)

- The Linux-based security settings Ipchains Firewall (Linux)

- Ubuntu Linux use ufw or iptables firewall configuration (Linux)

- Getting Started with Linux system to learn: how to install USB webcams come in raspberry (Linux)

- ctop: monitor container performance Linux command line artifact (Linux)

- Linux desktop system using the remote server in clear text ssh password (Server)

- PL / SQL in forall simple test (Database)

- Realize screen recording and playback via Linux command (Linux)

- Java inheritance initialization problem (Programming)

- The most common and most effective security settings under linux (Linux)

- CentOS use wget (Linux)

- Ora-00020: maximum number of processes (500) exceeded (Database)

- Forwarding module with Apache reverse proxy server (Server)

- Security Configuration SQL Server 2000 database tutorial (Linux)

 
         
  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:      
 
- jQuery get value drop-down list and select text (Programming)
- Ubuntu install Oracle 10g process and problem solution (Linux)
- How to handle special characters in JSON (Programming)
- Linux process management related content (Linux)
- Linux firewall to prevent external network attacks (Linux)
- Kickstart automated installation and deployment RHEL 7.0 (Linux)
- php for Linux the MySQL extension module installation and configuration (Database)
- Linux system monitoring tool set cpu (Linux)
- Apache site default home page settings (Server)
- Shell script on the variables with double quotation marks grep small problem (Programming)
- Beautiful start Ubuntu installation tool Duck Launcher 0.64.5 (Linux)
- Openfire achieve load balancing cluster by Nginx (Server)
- Access clipboard content across multiple vim instances in a terminal (Linux)
- Linux LVM - File system extension (Linux)
- Free compiler install MySQL-5.6.14 (Database)
- Linux top command to get started (Linux)
- Jetty JNDI Development combat (Linux)
- Linux Security Module (LSM) Introduction (Linux)
- CentOS 6.5 Telnet SecureCRT use management tools (Linux)
- Using packet capture libpcap be reconciliation package in Ubuntu 14.04 64 bits (Linux)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.