first drvier note

來源:互聯網
上載者:User

the code is from  the baohua book

/*======================================================================    A globalmem driver as an example of char device drivers         The initial developer of the original code is Baohua Song    <author@linuxdriver.cn>. All Rights Reserved.======================================================================*/#include <linux/module.h>#include <linux/types.h>#include <linux/fs.h>#include <linux/errno.h>#include <linux/mm.h>#include <linux/sched.h>#include <linux/slab.h>#include <linux/init.h>#include <linux/cdev.h>#include <asm/io.h>#include <asm/system.h>#include <asm/uaccess.h>#define GLOBALMEM_SIZE0x1000/*全域記憶體最大4K位元組*/#define MEM_CLEAR 0x1  /*清0全域記憶體*/#define GLOBALMEM_MAJOR 0    /*預設的globalmem的主裝置號*/static int globalmem_major = GLOBALMEM_MAJOR;/*globalmem裝置結構體*/struct globalmem_dev                                     {                                                          struct cdev cdev; /*cdev結構體*/                         unsigned char mem[GLOBALMEM_SIZE]; /*全域記憶體*/        };struct globalmem_dev *globalmem_devp; /*裝置結構體指標*//*檔案開啟函數*/int globalmem_open(struct inode *inode, struct file *filp){  /*將裝置結構體指標賦值給檔案私人資料指標*/  filp->private_data = globalmem_devp;  return 0;}/*檔案釋放函數*/int globalmem_release(struct inode *inode, struct file *filp){  return 0;}/* ioctl裝置控制函數 */static int globalmem_ioctl(struct inode *inodep, struct file *filp, unsigned  int cmd, unsigned long arg){  struct globalmem_dev *dev = filp->private_data;/*獲得裝置結構體指標*/  switch (cmd)  {    case MEM_CLEAR:      memset(dev->mem, 0, GLOBALMEM_SIZE);            printk(KERN_INFO "globalmem is set to zero\n");      break;    default:      return  - EINVAL;  }  return 0;}/*讀函數*/static ssize_t globalmem_read(struct file *filp, char __user *buf, size_t size,  loff_t *ppos){  unsigned long p =  *ppos;  unsigned int count = size;  int ret = 0;  struct globalmem_dev *dev = filp->private_data; /*獲得裝置結構體指標*/  /*分析和擷取有效寫長度*/  if (p >= GLOBALMEM_SIZE)    return count ?  - ENXIO: 0;  if (count > GLOBALMEM_SIZE - p)    count = GLOBALMEM_SIZE - p;  /*核心空間->使用者空間*/  if (copy_to_user(buf, (void*)(dev->mem + p), count))  {    ret =  - EFAULT;  }  else  {    *ppos += count;    ret = count;        printk(KERN_INFO "read %d bytes(s) from %d\n", count, p);  }  return ret;}/*寫函數*/static ssize_t globalmem_write(struct file *filp, const char __user *buf,  size_t size, loff_t *ppos){  unsigned long p =  *ppos;  unsigned int count = size;  int ret = 0;  struct globalmem_dev *dev = filp->private_data; /*獲得裝置結構體指標*/    /*分析和擷取有效寫長度*/  if (p >= GLOBALMEM_SIZE)    return count ?  - ENXIO: 0;  if (count > GLOBALMEM_SIZE - p)    count = GLOBALMEM_SIZE - p;      /*使用者空間->核心空間*/  if (copy_from_user(dev->mem + p, buf, count))    ret =  - EFAULT;  else  {    *ppos += count;    ret = count;        printk(KERN_INFO "written %d bytes(s) from %d\n", count, p);  }  return ret;}/* seek檔案定位函數 */static loff_t globalmem_llseek(struct file *filp, loff_t offset, int orig){  loff_t ret = 0;  switch (orig)  {    case 0:   /*相對檔案開始位置位移*/      if (offset < 0)      {        ret =  - EINVAL;        break;      }      if ((unsigned int)offset > GLOBALMEM_SIZE)      {        ret =  - EINVAL;        break;      }      filp->f_pos = (unsigned int)offset;      ret = filp->f_pos;      break;    case 1:   /*相對檔案當前位置位移*/      if ((filp->f_pos + offset) > GLOBALMEM_SIZE)      {        ret =  - EINVAL;        break;      }      if ((filp->f_pos + offset) < 0)      {        ret =  - EINVAL;        break;      }      filp->f_pos += offset;      ret = filp->f_pos;      break;    default:      ret =  - EINVAL;      break;  }  return ret;}/*檔案操作結構體*/static const struct file_operations globalmem_fops ={  .owner = THIS_MODULE,  .llseek = globalmem_llseek,  .read = globalmem_read,  .write = globalmem_write,  .ioctl = globalmem_ioctl,  .open = globalmem_open,  .release = globalmem_release,};/*初始化並註冊cdev*/static void globalmem_setup_cdev(struct globalmem_dev *dev, int index){  int err, devno = MKDEV(globalmem_major, index);  cdev_init(&dev->cdev, &globalmem_fops);  dev->cdev.owner = THIS_MODULE;  dev->cdev.ops = &globalmem_fops;  err = cdev_add(&dev->cdev, devno, 1);  if (err)    printk(KERN_NOTICE "Error %d adding LED%d", err, index);}/*裝置驅動模組載入函數*/int globalmem_init(void){  int result;  dev_t devno = MKDEV(globalmem_major, 0);  /* 申請裝置號*/  if (globalmem_major)    result = register_chrdev_region(devno, 1, "globalmem");  else  /* 動態申請裝置號 */  {    result = alloc_chrdev_region(&devno, 0, 1, "globalmem");    globalmem_major = MAJOR(devno);  }    if (result < 0)    return result;      /* 動態申請裝置結構體的記憶體*/  globalmem_devp = kmalloc(sizeof(struct globalmem_dev), GFP_KERNEL);  if (!globalmem_devp)    /*申請失敗*/  {    result =  - ENOMEM;    goto fail_malloc;  }  memset(globalmem_devp, 0, sizeof(struct globalmem_dev));    globalmem_setup_cdev(globalmem_devp, 0);  return 0;  fail_malloc: unregister_chrdev_region(devno, 1);  return result;}/*模組卸載函數*/void globalmem_exit(void){  cdev_del(&globalmem_devp->cdev);   /*登出cdev*/  kfree(globalmem_devp);     /*釋放裝置結構體記憶體*/  unregister_chrdev_region(MKDEV(globalmem_major, 0), 1); /*釋放裝置號*/}MODULE_AUTHOR("Song Baohua");MODULE_LICENSE("Dual BSD/GPL");module_param(globalmem_major, int, S_IRUGO);module_init(globalmem_init);module_exit(globalmem_exit);

 the test code  as below:

#include <sys/types.h>#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <fcntl.h>#include <sys/types.h>#include <sys/stat.h>main(){  int fd;  int size;  int i;  char boot_buf[512] = {0x33};  char boot_buf2[512];  fd=open("/dev/gloablmem",O_RDWR | O_NOCTTY | O_NONBLOCK);  write(fd,boot_buf,511);    read(fd,boot_buf2,233);  close(fd); i=0;for( i =0; i<strlen(boot_buf2);i++)       printf("fork NO! %d \n",&boot_buf2[i]);         pid_t pid;   if(pid=fork()==-1)    {      printf("fork wrong!\n");      exit(1);    }  if(pid==0)    {                }  return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.