Linux Device driver character device

Source: Internet
Author: User

Can no longer be streamlined, suitable for getting started.

1#include <linux/module.h>2#include <linux/init.h>3 4#include <asm/uaccess.h>5#include <linux/types.h>6#include <linux/fs.h>7#include <linux/cdev.h>8 structmengc_dev{9         Chardata[ -];Ten         structCdev Cdev; One }; A intMengmajor; - structMengc_dev Meng_device; -  the voidExit_mengc_dev (void) - { -dev_t Devno = MKDEV (Mengmajor,1); -Cdev_del (&Meng_device.cdev); +Unregister_chrdev_region (Devno,1); - } + intMeng_open (structInode*inode,structFile *Filep) A { at         return 0; - } - intMeng_release (structInode*inode,structFile *Filep) - { -                 return 0; - } in  -ssize_t Meng_read (structFile*filep,Char__user*buf,size_t count,loff_t*f_ops) to { +         if(*f_ops>=strlen (meng_device.data)) -                 return 0; the         if(Copy_to_user (Buf,meng_device.data,strlen (meng_device.data))) *         { $                 return-1;Panax Notoginseng         } -*f_ops+=strlen (meng_device.data); the         returnstrlen (meng_device.data); + } A  thessize_t Meng_write (structFile*filep,Const Char__user*buf,size_t count,loff_t*f_ops) + { -         if(count> the) $Count= the; $         if(Copy_from_user (meng_device.data,buf,count)) -         { -                 return-Efault; the         } -         returncount;Wuyi } the  -  Wu structFile_operations meng_fops= - { About. read=Meng_read, $. write=Meng_write, -. open=Meng_open, -. release=Meng_release - }; A intInit_mengc_dev (void) + { the         intre; -dev_t dev=0; $Re=alloc_chrdev_region (&dev,1,1,"Mengcdev"); the         if(re<0) the         { thePrintk"MENG_DEVC Register failed.\n"); the                 returnre; -         } inMengmajor=MAJOR (dev); thememset (&meng_device,0,sizeof(Meng_device)); theCdev_init (&meng_device.cdev,&meng_fops); AboutMeng_device.cdev.owner=This_module; themeng_device.cdev.ops=&Meng_fops; theRe=cdev_add (&meng_device.cdev,dev,1); the         if(RE) +         { -Printk"MENGDEVC Add faild.\n"); the         }Bayi         return 0; the } the Module_init (Init_mengc_dev); -Module_exit (Exit_mengc_dev);

Save the above file to MENGCDEV.C

Then create a makefile file in the same directory, the file content is as follows (only this line):

Obj-m: =MENGCDEV.O

Then execute make-c/lib/modules/' uname-r '/build/m= ' pwd ' in this directory the command executed successfully the day before yesterday is installed the source code package, specifically can refer to other materials, Ubuntu just apt-get install several packages, The Redhat series requires only a few yuminstall. Of course, there is a development environment, that is, GCC and the like.

Once the compilation is complete, the Mengcdev.ko module can be generated.

Insert this module with root insmod Mengcdev.ko

Then go to the/proc/device and find the appropriate device number.

Cat/proc/devices |grep Meng

If the output is as follows:

248 Mengcdev

The execution

Mknod/dev/mengcdev C 248 1, pay attention to replace 248 with the actual output value

And then you can test it. Echo Hello >/dev/mengcdev

Cat/dev/mengcdev

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.