Home IT Linux Windows Database Network Programming Server Mobile  
           
  Home \ Linux \ Linux character device - user mode and kernel mode data transfer data     - Oracle create user authorization and in PLSQL (Database)

- About Linux iptables firewall interview questions and answers (Linux)

- Getting Started with Linux system to learn: how to use tcpdump to capture TCP SYN, ACK and FIN packets (Linux)

- Easy to install CentOS 6.6 desktop environment (Linux)

- Windows Ubuntu dual system a key Ghost, grub rescue prompt solution (Linux)

- The Java way to stop a thread of execution (Programming)

- CentOS 7 install Hadoop-cdh-2.5 on Mesos (Server)

- Linux package management (Linux)

- Memory leak analysis using Android studio (Programming)

- Gentoo: startx problem appears Failed to load module (Linux)

- RHEL7 unattended automatic installation DHCP + TFTP + SYSLINUX + TFTP + Kickstart (Linux)

- Ubuntu 14.04 to install file editor KKEdit 0.1.5 version (Linux)

- Linux System Getting Started Tutorial: How do you know Shell which is currently being used (Linux)

- Java interface (Programming)

- LMMS 1.03 install on Ubuntu 14.04 (Linux)

- To build Spring RestTemplate use HttpClient4 (Programming)

- Linux System Getting Started Learning: hard disk partition, and to deal with traps (Linux)

- ntop monitoring software configuration and installation (Linux)

- JavaScript prototype and prototype chain and project combat (Programming)

- Red Hat Linux security settings document (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.7.1 High Availability Setup Deployment (Server)
- Syncthing: Private Security Synchronization tool to synchronize files between computers / folder (Linux)
- Use of the storage-level replication technology will quickly clone a ASM database to the target environment (Database)
- Mistakenly deleted redo log file group being given the lead to start the database ORA-03113 (Database)
- Linux Programming memory mapping (Programming)
- Tab set to four spaces in Vim (Linux)
- Git commands (Linux)
- RocketMQ Message Queuing simple deployment (Linux)
- Using Android Studio integrated development environment to build Android (Linux)
- Update GAMIT10.6 command (Linux)
- Docker Basic and Advanced (Linux)
- Using packet capture libpcap be reconciliation package in Ubuntu 14.04 64 bits (Linux)
- Oracle database physical file backup / restore (Database)
- Several start-up mode of Tomcat (Server)
- OpenCV cvFindCornerSubPix () to find sub-pixel Corner (Programming)
- Linux server Php injection prevention (Linux)
- How to protect the Apache website under Linux system (Linux)
- Java interface (Programming)
- Postfix mail service system principle and configuration (Linux)
- How to add a new resolution VirtualBox (Linux)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.