IOCTL in Linux device drivers

Source: Internet
Author: User

Memdev.h

#ifndef _memdev_h#define _memdev_h#define mem_magic ' m ' #define Mem_restart _io (mem_magic, 0)//Use the kernel-provided macros to generate commands #define MEM _set     _iow (mem_magic, 1, int)//change command to pass a shaping parameter to the kernel #endif

Driver MEMDEV.C

#include <linux/module.h> #include <linux/init.h> #include <linux/cdev.h> #include <linux/fs.h > #include <asm/uaccess.h> #include "memdev.h" struct Cdev mdev;dev_t devno;static long mem_ioctl (struct file* FILP, unsigned int cmd, unsigned long arg) {switch (cmd) {case MEM_RESTART:PRINTK ("Memdev restart.\n"); Break;case Mem_set: PRINTK ("Arg is%ld\n", arg);//Print the parameters passed down by the user to see if they are consistent break;default:return-einval;break;} return 0;} struct File_operations memfops = {. Llseek = Mem_lseek,.unlocked_ioctl = mem_ioctl,// The function parameters also change};static __init int memdev_init (void) {Cdev_init (&mdev, 2.6.36) after the kernel is ioctl,2.6.36 after the unlocked_ioctl. &memfops); alloc_chrdev_region (&devno, 0, 2, "Memdev"), Cdev_add (&mdev, Devno, 2);p RINTK ("Memdev_init Success\n "); return 0;} static void __exit memdev_exit (void) {Cdev_del (&mdev); Unregister_chrdev_region (Devno, 2);p RINTK ("Memdev_exit Success\n ");} Module_init (Memdev_init); Module_exit (Memdev_exit); Module_license ("GPL"); Module_author ("Liuwei"); Module_dEscription ("char driver"); 

Application MEM_READ.C

#include <stdio.h> #include <sys/ioctl.h> #include <sys/types.h> #include <sys/stat.h># Include <fcntl.h> #include "memdev.h" int main (int argc, char *argv[]) {int FD = open ("/dev/memdev", O_RDWR); if (FD ==< c0/>-1) {perror ("open"); return-1;} IOCTL (FD, Mem_restart);//Send two control commands, one without parameters, one with an integer parameter of the IOCTL (FD, Mem_set, x); close (FD); return 0;}
The device number is dynamically assigned in the driver and the device number assigned by the system to Memdev is viewed through cat/proc/devices.

Use the command Mknod/dev/memdev C 252 0 To create a device node. Where 252 system assigned the device number.

Drive a simple makefile

Ifeq ($ (kernelrelease),) PWD: = $ (Shell PWD) kerneldir? =/home/farsight/samba/linux-2.6.36installdir? =/nfs/rootnfs/ modules:$ (make)-C $ (Kerneldir) m=$ (PWD) modulesinstall:cp *.ko/nfs/rootnfs/clean:rm-rf *.o *.ko *.mod.c. *.cmd modules. Order module.symvers. Tmp_versionselseobj-m: = MEMDEV.O endif



IOCTL in Linux device drivers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.