Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7687657
  • 博文数量: 961
  • 博客积分: 15795
  • 博客等级: 上将
  • 技术积分: 16612
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-07 14:23
文章分类

全部博文(961)

文章存档

2016年(1)

2015年(61)

2014年(41)

2013年(51)

2012年(235)

2011年(391)

2010年(181)

分类: 嵌入式

2011-01-18 19:26:17

  1. #ifndef _MEMDEV_H_
  2. #define _MEMDEV_H_

  3. #ifndef MEMDEV_MAJOR
  4. #define MEMDEV_MAJOR 0 /*预设的mem的主设备号*/
  5. #endif

  6. #ifndef MEMDEV_NR_DEVS
  7. #define MEMDEV_NR_DEVS 2 /*设备数*/
  8. #endif

  9. #ifndef MEMDEV_SIZE
  10. #define MEMDEV_SIZE 4096
  11. #endif

  12. /*mem设备描述结构体*/
  13. struct mem_dev
  14. {
  15.   char *data;
  16.   unsigned long size;
  17.   wait_queue_head_t inq;
  18. };

  19. #endif /* _MEMDEV_H_ */
  1. #include <linux/module.h>
  2. #include <linux/types.h>
  3. #include <linux/fs.h>
  4. #include <linux/errno.h>
  5. #include <linux/mm.h>
  6. #include <linux/sched.h>
  7. #include <linux/init.h>
  8. #include <linux/cdev.h>
  9. #include <asm/io.h>
  10. #include <asm/system.h>
  11. #include <asm/uaccess.h>

  12. #include "memdev.h"

  13. static mem_major = MEMDEV_MAJOR;
  14. bool have_data = false; /*表明设备有足够数据可供读*/

  15. module_param(mem_major, int, S_IRUGO);

  16. struct mem_dev *mem_devp; /*设备结构体指针*/

  17. struct cdev cdev;

  18. /*文件打开函数*/
  19. int mem_open(struct inode *inode, struct file *filp)
  20. {
  21.     struct mem_dev *dev;
  22.     
  23.     /*获取次设备号*/
  24.     int num = MINOR(inode->i_rdev);

  25.     if (num >= MEMDEV_NR_DEVS)
  26.             return -ENODEV;
  27.     dev = &mem_devp[num];
  28.     
  29.     /*将设备描述结构指针赋值给文件私有数据指针*/
  30.     filp->private_data = dev;
  31.     
  32.     return 0;
  33. }

  34. /*文件释放函数*/
  35. int mem_release(struct inode *inode, struct file *filp)
  36. {
  37.   return 0;
  38. }

  39. /*读函数*/
  40. static ssize_t mem_read(struct file *filp, char __user *buf, size_t size, loff_t *ppos)
  41. {
  42.   unsigned long p = *ppos;
  43.   unsigned int count = size;
  44.   int ret = 0;
  45.   struct mem_dev *dev = filp->private_data; /*获得设备结构体指针*/

  46.   /*判断读位置是否有效*/
  47.   if (p >= MEMDEV_SIZE)
  48.     return 0;
  49.   if (count > MEMDEV_SIZE - p)
  50.     count = MEMDEV_SIZE - p;
  51.     
  52. while (!have_data) /* 没有数据可读,考虑为什么不用if,而用while,中断信号唤醒 */
  53. {
  54.         if (filp->f_flags & O_NONBLOCK)
  55.             return -EAGAIN;
  56.     
  57.     wait_event_interruptible(dev->inq,have_data);
  58. }


  59.   /*读数据到用户空间*/
  60.   if (copy_to_user(buf, (void*)(dev->data + p), count))
  61.   {
  62.     ret = - EFAULT;
  63.   }
  64.   else
  65.   {
  66.     *ppos += count;
  67.     ret = count;
  68.    
  69.     printk(KERN_INFO "read %d bytes(s) from %d\n", count, p);
  70.   }
  71.   
  72.   have_data = false; /* 表明不再有数据可读 */
  73.   return ret;
  74. }

  75. /*写函数*/
  76. static ssize_t mem_write(struct file *filp, const char __user *buf, size_t size, loff_t *ppos)
  77. {
  78.   unsigned long p = *ppos;
  79.   unsigned int count = size;
  80.   int ret = 0;
  81.   struct mem_dev *dev = filp->private_data; /*获得设备结构体指针*/
  82.   
  83.   /*分析和获取有效的写长度*/
  84.   if (p >= MEMDEV_SIZE)
  85.     return 0;
  86.   if (count > MEMDEV_SIZE - p)
  87.     count = MEMDEV_SIZE - p;
  88.     
  89.   /*从用户空间写入数据*/
  90.   if (copy_from_user(dev->data + p, buf, count))
  91.     ret = - EFAULT;
  92.   else
  93.   {
  94.     *ppos += count;
  95.     ret = count;
  96.     
  97.     printk(KERN_INFO "written %d bytes(s) from %d\n", count, p);
  98.   }
  99.   
  100.   have_data = true; /* 有新的数据可读 */
  101.     
  102.     /* 唤醒读进程 */
  103.     wake_up(&(dev->inq));

  104.   return ret;
  105. }

  106. /* seek文件定位函数 */
  107. static loff_t mem_llseek(struct file *filp, loff_t offset, int whence)
  108. {
  109.     loff_t newpos;

  110.     switch(whence) {
  111.       case 0: /* SEEK_SET */
  112.         newpos = offset;
  113.         break;

  114.       case 1: /* SEEK_CUR */
  115.         newpos = filp->f_pos + offset;
  116.         break;

  117.       case 2: /* SEEK_END */
  118.         newpos = MEMDEV_SIZE -1 + offset;
  119.         break;

  120.       default: /* can't happen */
  121.         return -EINVAL;
  122.     }
  123.     if ((newpos<0) || (newpos>MEMDEV_SIZE))
  124.         return -EINVAL;
  125.         
  126.     filp->f_pos = newpos;
  127.     return newpos;

  128. }

  129. /*文件操作结构体*/
  130. static const struct file_operations mem_fops =
  131. {
  132.   .owner = THIS_MODULE,
  133.   .llseek = mem_llseek,
  134.   .read = mem_read,
  135.   .write = mem_write,
  136.   .open = mem_open,
  137.   .release = mem_release,
  138. };

  139. /*设备驱动模块加载函数*/
  140. static int memdev_init(void)
  141. {
  142.   int result;
  143.   int i;

  144.   dev_t devno = MKDEV(mem_major, 0);

  145.   /* 静态申请设备号*/
  146.   if (mem_major)
  147.     result = register_chrdev_region(devno, 2, "memdev");
  148.   else /* 动态分配设备号 */
  149.   {
  150.     result = alloc_chrdev_region(&devno, 0, 2, "memdev");
  151.     mem_major = MAJOR(devno);
  152.   }
  153.   
  154.   if (result < 0)
  155.     return result;

  156.   /*初始化cdev结构*/
  157.   cdev_init(&cdev, &mem_fops);
  158.   cdev.owner = THIS_MODULE;
  159.   cdev.ops = &mem_fops;
  160.   
  161.   /* 注册字符设备 */
  162.   cdev_add(&cdev, MKDEV(mem_major, 0), MEMDEV_NR_DEVS);
  163.    
  164.   /* 为设备描述结构分配内存*/
  165.   mem_devp = kmalloc(MEMDEV_NR_DEVS * sizeof(struct mem_dev), GFP_KERNEL);
  166.   if (!mem_devp) /*申请失败*/
  167.   {
  168.     result = - ENOMEM;
  169.     goto fail_malloc;
  170.   }
  171.   memset(mem_devp, 0, sizeof(struct mem_dev));
  172.   
  173.   /*为设备分配内存*/
  174.   for (i=0; i < MEMDEV_NR_DEVS; i++)
  175.   {
  176.         mem_devp[i].size = MEMDEV_SIZE;
  177.         mem_devp[i].data = kmalloc(MEMDEV_SIZE, GFP_KERNEL);
  178.         memset(mem_devp[i].data, 0, MEMDEV_SIZE);
  179.   
  180.       /*初始化等待队列*/
  181.      init_waitqueue_head(&(mem_devp[i].inq));
  182.   }
  183.    
  184.   return 0;

  185.   fail_malloc:
  186.   unregister_chrdev_region(devno, 1);
  187.   
  188.   return result;
  189. }

  190. /*模块卸载函数*/
  191. static void memdev_exit(void)
  192. {
  193.   cdev_del(&cdev); /*注销设备*/
  194.   kfree(mem_devp); /*释放设备结构体内存*/
  195.   unregister_chrdev_region(MKDEV(mem_major, 0), 2); /*释放设备号*/
  196. }

  197. MODULE_AUTHOR("David Xie");
  198. MODULE_LICENSE("GPL");

  199. module_init(memdev_init);
  200. module_exit(memdev_exit);
  1. #include <stdio.h>

  2. int main()
  3. {
  4.     FILE *fp = NULL;
  5.     char Buf[128];
  6.     
  7.     
  8.     /*打开设备文件*/
  9.     fp = fopen("/dev/memdev0","r+");
  10.     if (fp == NULL)
  11.     {
  12.         printf("Open Dev memdev0 Error!\n");
  13.         return -1;
  14.     }
  15.     
  16.     /*写入设备*/
  17.     strcpy(Buf,"memdev is char dev!");
  18.     printf("Write BUF: %s\n",Buf);
  19.     fwrite(Buf, sizeof(Buf), 1, fp);
  20.     
  21.     sleep(5);
  22.     fclose(fp);
  23.     
  24.     return 0;    

  25. }
  1. #include <stdio.h>

  2. int main()
  3. {
  4.     FILE *fp = NULL;
  5.     char Buf[128];
  6.     
  7.     /*初始化Buf*/
  8.     strcpy(Buf,"memdev is char dev!");
  9.     printf("BUF: %s\n",Buf);
  10.     
  11.     /*打开设备文件*/
  12.     fp = fopen("/dev/memdev0","r+");
  13.     if (fp == NULL)
  14.     {
  15.         printf("Open memdev0 Error!\n");
  16.         return -1;
  17.     }
  18.     
  19.     /*清除Buf*/
  20.     strcpy(Buf,"Buf is NULL!");
  21.     printf("Read BUF1: %s\n",Buf);
  22.     
  23.     /*读出数据*/
  24.     fread(Buf, sizeof(Buf), 1, fp);
  25.     
  26.     /*检测结果*/
  27.     printf("Read BUF2: %s\n",Buf);
  28.     
  29.     fclose(fp);
  30.     
  31.     return 0;    

  32. }
阅读(2554) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~