Home IT Linux Windows Database Network Programming Server Mobile  
           
  Home \ Linux \ Linux character device - user mode and kernel mode data transfer data     - php for Linux the MySQL extension module installation and configuration (Database)

- Oracle database with test data insertion speed (Database)

- Find details block device with Linux blkid command (Linux)

- File sharing and fork function (Programming)

- CentOS 7 How to connect to a wireless network (Linux)

- Good wireless network security information spread in the air (Linux)

- Linux cron job (crontab) Examples (Linux)

- Ubuntu 14.04 compile, install, configure, the latest development version GoldenDict (Linux)

- Vim highlight lookup operation (Linux)

- Upgrade installation manual CentOS6.5 GCC4.8.2 (Linux)

- C ++ Supplements - malloc free and new delete the same and different (Programming)

- VMware11 virtual machine Ubuntu14.10 system partition sda1 disk expansion (Linux)

- Git commands (Linux)

- Linux Proc File System Experiment (Linux)

- See Shell Script Linux Server network traffic (Server)

- System with Windows Remote Desktop to connect Ubuntu 15.04 (Linux)

- MySQL performance view and configure finishing Daquan (Database)

- Puppet 3.x installed on Debian 7 (Server)

- Oracle and MySQL difference between the jdbc (Database)

- Use small network command to check whether PC Security (Linux)

 
         
  Linux character device - user mode and kernel mode data transfer data
     
  Add Date : 2018-11-21      
         
       
         
  Linux character device - user mode and kernel mode data is data to each other

_IO, _IOR, _IOW Meaning and _IORW

For the system to support the device ioctl number, you can find in / usr / include the following header file for your device, if you want to use ioctl interface, you need to define your own ioctl numbers. 2.4 in the previous problem is that everyone just define your own ioctl number, resulting in a high probability of repeatability. A disadvantage is difficult to manage, the other is likely to cause an error, such as if a user had hoped to open a serial device, results open open network port, if the serial number is just an ioctl closing operation of the network interface, this will cause error. 2.6 inside, you define your own ioctl number is best defined using _IO, _IOR, _IOW and _IORW, these macros into account the magic number for the third argument length, equipment, and the direction of the operation, etc., to avoid 2.4 problems

: _IO (Type, nr) (no arguments to commands),

_IOR (Type, nre, datatype) (to read the data from the drive),

_IOW (Type, nr, datatype) (to write data)

_IOWR (Type, nr, datatype) (for bi-directional transmission).

type and number members are passed as parameters,

Members and size parameters by applying sizeof to the datatype obtained

int ioctl (int fd, int request, ... / * void * arg * /) Comments

The third parameter is always a pointer, but it depends on the type of request pointer parameters. We can and network-related requests are divided into six categories:

Socket operation

File Operations

Interface Operation

ARP cache operations

Routing Table Operation

Flow System

Write a kernel mode and user mode data examples and data to each other APP

Manual installation steps:

Insmod my_char_dev.ko

You do not need to install the device node

Then test app

./my_char_dev_app 1

#include < linux / module.h >
#include < linux / init.h >
#include < linux / io.h >
#include < linux / fs.h >
#include < asm / device.h > // The following three header files are dynamically created due to the need to increase the
#include < linux / device.h >
#include < linux / cdev.h >
#include "my_cdev.h"
#include < asm / uaccess.h >


struct cdev cdev;
dev_t devno; // here is the dynamic allocation device number and dynamically create device nodes need to use
struct class * cdev_class;
int param;
int my_cdev_open (struct inode * node, struct file * filp)
{
    printk ( "! my_cdev_open sucess \ n");
    return 0;
}

long my_cdev_ioctl (struct file * filp, unsigned int cmd, unsigned long arg)
{
    int rc = -1;

    switch (cmd)
    {
        case LED_ON:
            rc = copy_from_user (& param, (int __user *) arg, 4);
            if (0! = rc)
            {
                printk ( ". copy_from_user failed \ n");
                break;
            }
            printk ( "Param is% d \ n.", param);
            printk ( "CMD test: LED_ON is set \ n!");
            return 0;
        case LED_OFF:
            printk ( "CMD test: LED_OFF is set \ n!");
            param = 1000;
            rc = copy_to_user ((int __user *) arg, & param, 4);
            if (0! = rc)
            {
                printk ( ". copy_to_user failed \ n");
            }
            param = 0;
            return 0;
        default:
            return -EINVAL;
    }
}

struct file_operations my_cdev_fops =
{
    .open = my_cdev_open,
    .unlocked_ioctl = my_cdev_ioctl,

};

static int my_cdev_init (void)
{
    int ret;
    / ** Dynamically allocated device number * /
    ret = alloc_chrdev_region (& devno, 0,1, "my_chardev");
    if (ret)
    {
        printk ( "alloc_chrdev_region fail \ n!");
        unregister_chrdev_region (devno, 1);
        return ret;
    }
    else
    {
        printk ( "! alloc_chrdev_region sucess \ n");
    }
    / ** * Initialize the description of the structure /
    cdev_init (& cdev, & my_cdev_fops);
    / ** * Register description of the structure /
    ret = cdev_add (& cdev, devno, 1);
    if (ret)
    {
        printk ( ". cdev add fail \ n");
        unregister_chrdev_region (devno, 1);
        return ret;
    }
    else
    {
        printk ( "cdev add sucess \ n!");
    }

    cdev_class = class_create (THIS_MODULE, "my_chardev");
    if (IS_ERR (cdev_class))
    {
        printk ( "! Create class fail \ n");
        unregister_chrdev_region (devno, 1);
        return -1;
    }
    else
    {
        printk ( "! Create class sucess \ n");
    }

    device_create (cdev_class, NULL, devno, 0, "my_chardev");
    
    return 0;
}
static void my_cdev_exit (void)
{
    device_destroy (cdev_class, devno);
    class_destroy (cdev_class);
    cdev_del (& cdev);
    unregister_chrdev_region (devno, 1);
    printk ( "! my_cdev_exit sucess \ n");
}
module_init (my_cdev_init);
module_exit (my_cdev_exit);
MODULE_LICENSE ( "GPL");
MODULE_AUTHOR ( "YEFEI");
MODULE_DESCRIPTION ( "YEFEI Driver");

--------------------------------------------

#ifndef __MY_CDEV_H__
#define __MY_CDEV_H__

#define LED_MAGIC 'L'
#define LED_ON _IOW (LED_MAGIC, 0, int)
#define LED_OFF _IOR (LED_MAGIC, 1, int *)

#endif

--------------------------------------------

#include < sys / stat.h >
#include < sys / types.h >
#include < sys / ioctl.h >
#include < fcntl.h >
#include < stdio.h >
#include "my_cdev.h"

int main (int argc, char * argv [])
{
    int fd;
    int cmd;
    long ret = 0;
    unsigned long param = 0;
    if (argc <2)
    {
        printf ( "Please enter secend param \ n!");
        return 0;
    }
    cmd = atoi (argv [1]);
    fd = open ( "/ dev / my_chardev", O_RDWR);
    if (fd <0)
    {
        printf ( "! Open dev / my_chardev fail \ n");
        close (fd);
        return 0;
    }
    switch (cmd)
    {
        case 1:
            param = 500;
            ret = ioctl (fd, LED_ON, & param);
            break;
        case 2:
            ret = ioctl (fd, LED_OFF, & param);
            printf ( "ret is% d read data is% d \ n..", ret, param);
            break;
        default:
            break;
    }
    close (fd);
    return 0;
}

--------------------------------------------

1 obj-m: = my_char_dev.o
2 KDIR: = / home / win / dn377org / trunk / bcm7252 / linux /
3 all:
4 make -C $ (KDIR) M = $ (PWD) modules CROSS_COMPILE = arm-linux- ARCH = arm
5 clean:
6 rm -f * .ko * .o * .mod.o * .mod.c * .symvers * .bak * .order
     
         
       
         
  More:      
 
- MySQL 5.6.26 source install (Database)
- Linux find and xargs (Linux)
- Commentary Apache + Tomcat + JK implement Tomcat clustering and load (Server)
- How to monitor Nginx (Database)
- PL / SQL how to make the program every few seconds to insert a data (Database)
- Hadoop 2.6.0 standalone configuration and pseudo-distributed configuration under Ubuntu 14.04 (Server)
- Internal class broadcasting needs public and static (Programming)
- Network Security: SYN attacks against under linux (Linux)
- Spark and Hadoop comparison (Server)
- Linux Desktop allows exceptionally different launch applications (Linux)
- Five Linux user space debugging tool (Linux)
- TOAST function in PostgreSQL (Database)
- Linux Workstation Security Checklist - from the Linux Foundation Internal (Linux)
- Clojure programming languages: take full advantage of the Clojure plug-in Eclipse (Programming)
- Python Django model within the class meta Detailed (Programming)
- MultiWriter: while the ISO image concurrent writes 20 USB Startup Disk (Linux)
- Linux command line to put on your coat GUI (Linux)
- How to use awk command in Linux (Linux)
- Debian 7.6 install Nvidia graphics driver (Linux)
- Oracle RMAN repair logical bad blocks (Database)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.