Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7687983
  • 博文数量: 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-16 10:03:56

头文件
  1. #ifndef _MEMDEV_H_
  2. #define _MEMDEV_H_

  3. #ifndef MEMDEV_MAJOR
  4. #define MEMDEV_MAJOR 251 /*预设的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. };

  18. #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. module_param(mem_major, int, S_IRUGO);

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

  16. struct cdev cdev;

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

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

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

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

  45.   /*判断读位置是否有效*/
  46.   if (p >= MEMDEV_SIZE)
  47.     return 0;
  48.   if (count > MEMDEV_SIZE - p)
  49.     count = MEMDEV_SIZE - p;

  50.   /*读数据到用户空间*/
  51.   if (copy_to_user(buf, (void*)(dev->data + p), count))
  52.   {
  53.     ret = - EFAULT;
  54.   }
  55.   else
  56.   {
  57.     *ppos += count;
  58.     ret = count;
  59.     
  60.     printk(KERN_INFO "read %d bytes(s) from %d\n", count, p);
  61.   }

  62.   return ret;
  63. }

  64. /*写函数*/
  65. static ssize_t mem_write(struct file *filp, const char __user *buf, size_t size, loff_t *ppos)
  66. {
  67.   unsigned long p = *ppos;
  68.   unsigned int count = size;
  69.   int ret = 0;
  70.   struct mem_dev *dev = filp->private_data; /*获得设备结构体指针*/
  71.   
  72.   /*分析和获取有效的写长度*/
  73.   if (p >= MEMDEV_SIZE)
  74.     return 0;
  75.   if (count > MEMDEV_SIZE - p)
  76.     count = MEMDEV_SIZE - p;
  77.     
  78.   /*从用户空间写入数据*/
  79.   if (copy_from_user(dev->data + p, buf, count))
  80.     ret = - EFAULT;
  81.   else
  82.   {
  83.     *ppos += count;
  84.     ret = count;
  85.     
  86.     printk(KERN_INFO "written %d bytes(s) from %d\n", count, p);
  87.   }

  88.   return ret;
  89. }

  90. /* seek文件定位函数 */
  91. static loff_t mem_llseek(struct file *filp, loff_t offset, int whence)
  92. {
  93.     loff_t newpos;

  94.     switch(whence) {
  95.       case 0: /* SEEK_SET */
  96.         newpos = offset;
  97.         break;

  98.       case 1: /* SEEK_CUR */
  99.         newpos = filp->f_pos + offset;
  100.         break;

  101.       case 2: /* SEEK_END */
  102.         newpos = MEMDEV_SIZE -1 + offset;
  103.         break;

  104.       default: /* can't happen */
  105.         return -EINVAL;
  106.     }
  107.     if ((newpos<0) || (newpos>MEMDEV_SIZE))
  108.         return -EINVAL;
  109.         
  110.     filp->f_pos = newpos;
  111.     return newpos;

  112. }

  113. /*文件操作结构体*/
  114. static const struct file_operations mem_fops =
  115. {
  116.   .owner = THIS_MODULE,
  117.   .llseek = mem_llseek,
  118.   .read = mem_read,
  119.   .write = mem_write,
  120.   .open = mem_open,
  121.   .release = mem_release,
  122. };

  123. /*设备驱动模块加载函数*/
  124. static int memdev_init(void)
  125. {
  126.   int result;
  127.   int i;

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

  129.   /* 静态申请设备号*/
  130.   if (mem_major)
  131.     result = register_chrdev_region(devno, 2, "memdev");
  132.   else /* 动态分配设备号 */
  133.   {
  134.     result = alloc_chrdev_region(&devno, 0, 2, "memdev");
  135.     mem_major = MAJOR(devno);
  136.   }
  137.   
  138.   if (result < 0)
  139.     return result;

  140.   /*初始化cdev结构*/
  141.   cdev_init(&cdev, &mem_fops);
  142.   cdev.owner = THIS_MODULE;
  143.   cdev.ops = &mem_fops;
  144.   
  145.   /* 注册字符设备 */
  146.   cdev_add(&cdev, MKDEV(mem_major, 0), MEMDEV_NR_DEVS);
  147.    
  148.   /* 为设备描述结构分配内存*/
  149.   mem_devp = kmalloc(MEMDEV_NR_DEVS * sizeof(struct mem_dev), GFP_KERNEL);
  150.   if (!mem_devp) /*申请失败*/
  151.   {
  152.     result = - ENOMEM;
  153.     goto fail_malloc;
  154.   }
  155.   memset(mem_devp, 0, sizeof(struct mem_dev));
  156.   
  157.   /*为设备分配内存*/
  158.   for (i=0; i < MEMDEV_NR_DEVS; i++)
  159.   {
  160.         mem_devp[i].size = MEMDEV_SIZE;
  161.         mem_devp[i].data = kmalloc(MEMDEV_SIZE, GFP_KERNEL);
  162.         memset(mem_devp[i].data, 0, MEMDEV_SIZE);
  163.   }
  164.     
  165.   return 0;

  166.   fail_malloc:
  167.   unregister_chrdev_region(devno, 1);
  168.   
  169.   return result;
  170. }

  171. /*模块卸载函数*/
  172. static void memdev_exit(void)
  173. {
  174.   cdev_del(&cdev); /*注销设备*/
  175.   kfree(mem_devp); /*释放设备结构体内存*/
  176.   unregister_chrdev_region(MKDEV(mem_major, 0), 2); /*释放设备号*/
  177. }

  178. MODULE_AUTHOR("David Xie");
  179. MODULE_LICENSE("GPL");

  180. module_init(memdev_init);
  181. module_exit(memdev_exit);
测试应用程序
  1. #include <stdio.h>

  2. int main()
  3. {
  4.     FILE *fp0 = NULL;
  5.     char Buf[4096];
  6.     
  7.     /*初始化Buf*/
  8.     strcpy(Buf,"Mem is char dev!");
  9.     printf("BUF: %s\n",Buf);
  10.     
  11.     /*打开设备文件*/
  12.     fp0 = fopen("/dev/memdev0","r+");
  13.     if (fp0 == NULL)
  14.     {
  15.         printf("Open Memdev0 Error!\n");
  16.         return -1;
  17.     }
  18.     
  19.     /*写入设备*/
  20.     fwrite(Buf, sizeof(Buf), 1, fp0);
  21.     
  22.     /*重新定位文件位置(思考没有该指令,会有何后果)*/
  23.     fseek(fp0,0,SEEK_SET);
  24.     
  25.     /*清除Buf*/
  26.     strcpy(Buf,"Buf is NULL!");
  27.     printf("BUF: %s\n",Buf);
  28.     
  29.     
  30.     /*读出设备*/
  31.     fread(Buf, sizeof(Buf), 1, fp0);
  32.     
  33.     /*检测结果*/
  34.     printf("BUF: %s\n",Buf);
  35.     
  36.     return 0;    

  37. }
阅读(1421) | 评论(0) | 转发(3) |
0

上一篇:智能控制电机

下一篇:串口通信程序

给主人留下些什么吧!~~