ºìÁªLinuxÃÅ»§
Linux°ïÖú

linux×Ö·ûÉ豸Çý¶¯Ö®ÎÒ¼ûÓëÒÉ»ó

·¢²¼Ê±¼ä:2011-04-08 16:55:21À´Ô´:ºìÁª×÷Õß:stephenyong
#include
#include
#include
#include //ÕâЩ¶¼ÊÇÐèÒªµÄÍ·Îļþ£¬ÎÒÒ²²»ÖªµÀΪʲôÐèÒªÕâЩ£¬´ó¸ÅÊDZ༭ģ¿éʱºòÐèÒªµÄ£¬¼ÇÏÂÀ´¾ÍºÃÁË°É

MODULE_LICENSE("GPL");//ÉùÃ÷Ö¤Ê飬±ØÐëÓÐÒ²¿ÉÒÔÓÃÆäËûµÄ£¬±ÈÈçDAULµÈ

#define MAJOR_NUM 222 //¶¨ÒåÖ÷É豸ºÅΪ222£¬ÕâÀïÒ²¿ÉÒÔÈÃÄÚºË×Ô¶¯¶¨Ò壬ÏÖÔÚÊÇÈ˹¤¶¨Ò壬ÐèÒª²é¿´ÒÑÓеÄÉ豸ºÅ£¬
//¿ÉÒÔÓÃcat /proc/device²é¿´£¬²»ÒªÖظ´¾ÍºÃ

static ssize_t chardev_read(struct file *,char *,size_t,loff_t *); //ÕâÁ½¾äÊÇÉùÃ÷´ý»áÓõ½µÄfile_operations½á¹¹ÌåÖеĺ¯Êý£¬¶Áд
static ssize_t chardev_write(struct file *,const char *,size_t,loff_t *);

struct file_operations chardev_fops=
{
read: chardev_read,
write: chardev_write,
};//ÕâÊÇÒ»¸ö½á¹¹Ì壬¶¨Òåchardev_fopsΪһ¸öfile_operationsµÄ½á¹¹Ì壬Õâ¸öÊDZØÐëµÄ£¬Õâ¸ö½á¹¹Ì彫ÓëÏÂÃæµÄÖ÷É豸ºÅ£¬É豸ÃûÁ¬½Ó³ÉΪһ¸öÕûÌå

static int chardev_var = 0;//Ëæ±ã¶¨ÒåÁËÒ»¸ö¾²Ì¬±äÁ¿£¬ÓÃÓÚ²âÊÔ¶øÒÑ

static int __init chardev_init(void) //ÕâÊÇÄ£¿éµÄ³õʼ»¯º¯Êý
{
int ret;

ret = register_chrdev(MAJOR_NUM,"chardev",&chardev_fops); //×¢²áÉ豸£¬¾ÍÊÇÉÏÃæ½²µÄ½«MAJOR_NUM£¬É豸Ãûchardev£¬»¹ÓÐchardev_fops½áºÏ

if(ret)
printk("chardev register faild...\n");
printk("chardev register succes...\n");

return ret;
}


static void __exit chardev_exit(void) //×¢ÏúÉ豸
{
unregister_chrdev(MAJOR_NUM,"chardev");//×¢Ò⣬´Ëº¯ÊýûÓзµ»ØÖµ
}

static ssize_t chardev_read(struct file *filp,char *buf,size_t len,loff_t *off) //Õâ¾ÍÊDzâÊÔ³ÌÐòÀïÃæµÄreadº¯ÊýÔÚÄÚºËÀïÕæÕýµÄÖ´ÐÐÌ壬
//ËüµÄ×÷ÓÃÊǽ«ÄÚºËÊý¾Ýдµ½Óû§¿Õ¼ä
{
if(copy_to_user(buf,&chardev_var,sizeof(int))) //×¢ÒâÇø·Öcopy_to_userºÍcopy_from_userµÄÇø±ð£¬¶¼ÊÇÒÔÓû§¿Õ¼äΪÖ÷ÌåµÄ£¬ÕâÀïµÄº¯ÊýÀïÃæµÄ²ÎÊý¾ßÌåûÓп´Ã÷°×
//ÕâÀïÃæµÄbuf£¬ÊDz»ÊÇÄں˿ռäµÄÒ»¸öÊý¾Ý»º³åÇø£¿ËûÊDz»ÊÇÓëchardevÕâ¸öÉ豸ÎļþÓÐʲôÁªÏµ£¿»¹ÊÇËû¾ÍÊÇÓû§¿Õ¼änumµÄÐÎʽ²ÎÊý¶øÒÑ£¿
return -EFAULT;
return sizeof(int);
}


static ssize_t chardev_write(struct file *filp,const char *buf,size_t len,loff_t *off)
{
if(copy_from_user(&chardev_var,buf,sizeof(int)))
return -EFAULT;
return sizeof(int);
}


module_init(chardev_init);
module_exit(chardev_exit);






//²âÊÔ³ÌÐòteat1.c
#include
#include
#include
#include

main()
{
int fd,num;

fd = open("/dev/chardev1",O_RDWR,S_IRUSR | S_IWUSR);

if(fd!=-1)
{
printf("open the ¡®/dev/chardev1¡¯ success...\n");
read(fd,&num,sizeof(int));//ÕâÀïÎÒÊÇÕâÑùÀí½âµÄ£¬fdÊÇ´ò¿ªµÄÉ豸ÎļþµÄÃèÊö·û£¬ÊDz»ÊÇ´ÓÕâ¸öÎļþ´«µÝÊý¾Ý¸øÓû§¿Õ¼äµÄnum£¿Èç¹ûÊÇ£¬ÄÇôÕâ¸öÉ豸ÎļþµÄÊý¾ÝÓÖÊǴӺζøÀ´£¿
printf("The chardev is %d\n",num);

printf("input the num you want to write...\n");
scanf("%d",&num);
write(fd,&num,sizeof(int));

read(fd,&num,sizeof(int));
printf("The chardev is %d\n",num);

close(fd);
}

else
printf("Device open faild...\n");
}


/*
ÒÉÎÊ1
ΪʲôÔÚfile_operations½á¹¹ÌåÀïÃæread=chardev_read£¿µ«ÊÇÁ½ÕߵIJÎÊý¸öÊý²»Ò»Ñù°¡£¬ÈçºÎ´«µÝµÄ£¿²âÊÔ³ÌÐòÀïÃæÖ»ÓÐ3¸ö²ÎÊý£¬¶øchardev_readÀïÃæÐèÒª4¸ö²ÎÊý°¡£¿

ÒÉÎÊ2
readº¯ÊýÓëchardev_readº¯Êý¸÷¸ö²ÎÊýµÄº¬ÒåÊÇʲô£¿ÓÃÓÚʲôÓÃ;£¿
*/
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 8 ÌõÆÀÂÛ

  1. gilee2012 ÓÚ 2013-04-26 21:57:47·¢±í:

    read: chardev_read,
    ÕâÀïµÄread²¢²»ÊÇÄã²âÊÔ³ÌÐòÀïÃæÓõ½µÄread£¬Öм侭¹ýÁ˼¸µÀת»»µÄ£¡

  2. cargo ÓÚ 2013-04-12 12:20:37·¢±í:

    ¹þ¹þŶ

  3. ½ºÍ·µÎ¹Ü3 ÓÚ 2012-09-04 18:24:32·¢±í:

    dingqi

  4. hangyixiao ÓÚ 2012-09-04 15:38:18·¢±í:

    ͬÑùÒÉ»óÖС£¡£¡£Ï£Íû¸ßÊÖÖ¸µãһϰ¡

  5. zl870213 ÓÚ 2011-08-10 14:48:08·¢±í:

    ¶¥

  6. stephenyong ÓÚ 2011-04-10 16:25:53·¢±í:

    4# shenhao0129


    лл

  7. nxzcc ÓÚ 2011-04-09 08:54:29·¢±í:

    °ï¶¥ÉÏ

  8. stephenyong ÓÚ 2011-04-08 17:00:18·¢±í:

    Çë¸ßÊÖÖ¸µã£¬Ð¡µÜ¸Õѧ£¬¶àл¶àл£¡{:2_98:}