Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Linux device driver development small example --LED lights     - Ubuntu 14.04 Trusty Tahr User How to install Banshee 2.9.1 (Linux)

- Linux Getting Started tutorial: Experience Xen Virtual Machine chapter (Linux)

- KVM QEMU virtual machine installation configuration under CentOS (Linux)

- Ubuntu 10.04 to Ubuntu 10.10 Upgrade (Linux)

- To use Java arrays implement the order form (Programming)

- Linux installation skynet issue summary (Linux)

- CentOS 6.5 using Virtualenv under development environment to build Python3 (Linux)

- Ubuntu Linux use MAC binding against ARP attacks (Linux)

- awk variables (Linux)

- Linux startup and logon security settings (Linux)

- Linux kernel programming parameter passing between modules and function calls (Programming)

- Java, on the dfile.encoding Systemproperty (Programming)

- Servlet life cycle code examples (Programming)

- Linux Security Raiders SSH tools SCP uses examples to explain in detail (Linux)

- CentOS6.6 ordinary users to use sudo command to borrow root user privileges (Linux)

- 10 useful Linux command line tips (Linux)

- To create a secure network firewall with iptables Under Linux (Linux)

- Open log in Hibernate (Programming)

- Linux network monitoring strategy (Linux)

- systemd Power Management (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:      
 
- Compile Android libwebcore.so error occurs when solving (Programming)
- Zypper command for SUSE Linux package management (Linux)
- C ++, overloading, cover, hide (Programming)
- Using the Linux VNC service (Server)
- Android Fragment everything you need to know (Programming)
- Depth understanding of DB2 table space (Tablespace) (Database)
- Linux System Getting Started Learning: rename multiple files in Linux (Linux)
- C # function (Programming)
- OpenGL shadow map (Programming)
- Linux network security backdoor technology and advanced skill practice (Linux)
- Ubuntu install Scala 2.10.x version (Linux)
- Use PuTTY key authentication mechanism for remote login Linux (Linux)
- About ORA-02391 solution (Database)
- Linux terminal program running in the background (Linux)
- CentOS / Linux kernel upgrade (Linux)
- ThinkPad X201i installation notes Debian 7.5 (Linux)
- How to use the Linux terminal Git commands (Linux)
- Use PXE with kickstart to install Linux automatically (Linux)
- Grub2: Save Your bootloader (Linux)
- ElasticSearch basic usage and cluster structures (Server)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.