Home IT Linux Windows Database Network Programming Server Mobile  
           
  Home \ Linux \ Linux character device - user mode and kernel mode data transfer data     - Fatal NI connect error 12170 error in Alert Log (Database)

- Python Flask environment to build (Linux)

- Ubuntu 10.04 to Ubuntu 10.10 Upgrade (Linux)

- Ubuntu 10.10 install Oracle 10g graphic tutorials (Database)

- How to use Aptik to backup and restore Apps/PPAs under ubuntu (Linux)

- Ubuntu 14.10 Server configuration wireless Internet access (Server)

- Linux platform NTOP Installation and Configuration (Linux)

- Apache2.4.7 make an error [exports.lo] Error 1 Solution (Server)

- pureftpd basis: Install, configure, implement, anonymous logon (Linux)

- Python MySQL database connection (Database)

- Linux System Getting Started Learning: Linux command in w (Linux)

- NIC configuration parameters under Linux (Linux)

- How to install Bugzilla 4.4 on Ubuntu / CentOS 6.x (Linux)

- Using the Linux VNC service (Server)

- How to view information about the installed version of CentOS (Linux)

- xCAT Installation Kit (Linux)

- Linux operating system must know the security command (Linux)

- Linux server is how to do after the invasion (Linux)

- Python Django direct implementation of sql statement (Programming)

- Linux kernel RCU (Read Copy Update) lock Brief (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:      
 
- Hadoop 2.0 Detailed Configuration Tutorial (Server)
- Oracle DataGuard principles and basic configuration (Database)
- Ubuntu 14.04 kernel after the restart boot black screen to solve (Linux)
- Ubuntu 14.04 to install file editor KKEdit 0.1.5 version (Linux)
- Linux kernel programming parameter passing between modules and function calls (Programming)
- OpenGL shadow map (Programming)
- Linux script to copy the folder to all folders with the same name (Linux)
- Android Notification (Programming)
- C ++ based foundation: the difference between C and C ++ (Programming)
- Oracle JDK installation under Ubuntu Linux (Linux)
- Oracle 11g maintenance partitions - Adding Partitions (Database)
- Nginx multi-domain certificate HTTPS (Server)
- Linux data recovery software efficiently practical application extundelete (Linux)
- Windows environment Android Studio v1.0 Installation Guide (Linux)
- Bash environment is automatically install and initialize oh-my-zsh & autojump zsh (Linux)
- Python closure and function objects (Programming)
- Java regular expression syntax (Programming)
- Github Getting Started Basic Course (Linux)
- JDK tools jstat (Linux)
- HA-Federation-HDFS + Yarn cluster deployment (Server)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.