Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Linux device driver development small example --LED lights     - How to query the role of Linux services (Linux)

- Volatile keyword in C language understanding (Programming)

- Linux System Getting Started Learning: Join cron job in Linux (Linux)

- Python format string (Programming)

- Configuring xdebug debugging environment in Ubuntu 14.04 under the PhpStorm (Linux)

- Android WebView use layman (Programming)

- Embedded Linux to solve the problem in the time zone (Linux)

- How to install Linux on a MacBook Pro Retina (Linux)

- Python kills corresponding process according to pid (Programming)

- How to Install Telegram instant messaging software on Ubuntu (Linux)

- Linux basic articles of the boot process (Linux)

- Can not empty the Recycle Bin to repair problems in Ubuntu 14.04 (Linux)

- MySQL primary and secondary replicate data inconsistencies (Database)

- Linux formatted partition error Could not stat / dev / sda No such file or directory Solution (Linux)

- Using Python to find a particular file extension directory (Programming)

- Linux iptables firewall settings (Linux)

- How to Install Xombrero 1.6.4 (minimalist Web browser) on Ubuntu and Archlinux (Linux)

- Use of the storage-level replication technology will quickly clone a ASM database to the target environment (Database)

- Ubuntu Learning Advanced article - to teach you to further enhance system security (Linux)

- Use SecureCRT to transfer files between Linux and Windows (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:      
 
- How to Start a Linux application running in the background using the terminal mode (Linux)
- To disable the function by pressing Ctrl-Alt-Del to restart the system in RHEL / CentOS 5/6 (Linux)
- Oracle metadata Reconstruction experiments (Database)
- Use the top command (Linux)
- Overall Physical Migration of Oracle Database with (Database)
- ASP.NET 5 is connected with the Redis server on the Linux platform (Server)
- Let MySQL 5.6 support Emoji expression (Database)
- Empty password Linux operating system (Linux)
- Oracle Migration partition table (Database)
- How to set up FTP server on Linux (Server)
- openSUSE 13.1 / 13.2 installation SoundConverter 2.1.2 (Linux)
- Python image processing library (PIL) to install and simple to use (Linux)
- To see the Linux device tree (Linux)
- How to Install Telegram instant messaging software on Ubuntu (Linux)
- AFNetworking + Nginx HTTPS communication server + (Server)
- The best known for archiving / compression tool under linux (Linux)
- CentOS 7.0 Enable iptables firewall (Linux)
- To update Python version under CentOS system (Linux)
- What factors affect the performance of Java calls (Programming)
- Linux Services Comments (Linux)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.