当前位置 主页 > 服务器问题 > Linux/apache问题 >

    Linux 块设备驱动代码编写

    栏目:Linux/apache问题 时间:2019-11-30 07:47

    按照ldd的说法,linux的设备驱动包括了char,block,net三种设备。char设备是比较简单的,只要分配了major、minor号,就可以进行读写处理了。相对而言,block和net要稍微复杂些。net设备姑且按下不谈,我们在以后的博文中会有涉及。今天,我们可以看看一个简单的block是怎么设计的。

    为了将block和fs分开,kernel的设计者定义了request queue这一种形式。换一句话说,所有fs对block设备的请求,最终都会转变为request的形式。所以,对于block设备驱动开发的朋友来说,处理好了request queue就掌握了block设备的一半。当然,block设备很多,hd、floppy、ram都可以这么来定义,有兴趣的朋友可以在drivers/block寻找相关的代码来阅读。兴趣没有那么强的同学,可以看看我们这篇博文,基本上也能学个大概。有个基本的概念,再加上一个简单浅显的范例,对于一般的朋友来说,已经足够了。

    闲话不多说,我们看看一个ramdisk代码驱动是怎么写的,代码来自《深入linux 设备驱动程序内核机制》,

    #include <linux/module.h> 
    #include <linux/kernel.h> 
    #include <linux/init.h> 
     
    #include <linux/fs.h> 
    #include <linux/types.h> 
    #include <linux/fcntl.h> 
    #include <linux/vmalloc.h> 
    #include <linux/blkdev.h> 
    #include <linux/hdreg.h> 
     
    #define RAMHD_NAME "ramhd" 
    #define RAMHD_MAX_DEVICE 2 
    #define RAMHD_MAX_PARTITIONS 4 
     
    #define RAMHD_SECTOR_SIZE 512 
    #define RAMHD_SECTORS 16 
    #define RAMHD_HEADS 4 
    #define RAMHD_CYLINDERS 256 
     
    #define RAMHD_SECTOR_TOTAL (RAMHD_SECTORS * RAMHD_HEADS *RAMHD_CYLINDERS) 
    #define RAMHD_SIZE (RAMHD_SECTOR_SIZE * RAMHD_SECTOR_TOTAL) //8mb 
     
    typedef struct { 
      unsigned char* data; 
      struct request_queue* queue; 
      struct gendisk* gd; 
    }RAMHD_DEV; 
     
    static char* sdisk[RAMHD_MAX_DEVICE] = {NULL}; 
    static RAMHD_DEV* rdev[RAMHD_MAX_DEVICE] = {NULL}; 
     
    static dev_t ramhd_major; 
     
    static int ramhd_space_init(void) 
    { 
      int i; 
      int err = 0; 
      for(i = 0; i < RAMHD_MAX_DEVICE; i++){ 
        sdisk[i] = vmalloc(RAMHD_SIZE); 
        if(!sdisk[i]){ 
          err = -ENOMEM; 
          return err; 
        } 
         
        memset(sdisk[i], 0, RAMHD_SIZE); 
      } 
       
      return err; 
    } 
     
    static void ramhd_space_clean(void) 
    { 
      int i; 
      for(i = 0; i < RAMHD_MAX_DEVICE; i++){ 
        vfree(sdisk[i]); 
      } 
    } 
     
    static int ramhd_open(struct block_device* bdev, fmode_t mode) 
    { 
      return 0; 
    } 
     
    static int ramhd_release(struct gendisk*gd, fmode_t mode) 
    { 
      return 0; 
    } 
     
    static int ramhd_ioctl(struct block_device* bdev, fmode_t mode, unsigned int cmd, unsigned long arg) 
    { 
      int err; 
      struct hd_geometry geo; 
       
      switch(cmd) 
      { 
        case HDIO_GETGEO: 
          err = !access_ok(VERIFY_WRITE, arg, sizeof(geo)); 
          if(err) 
            return -EFAULT; 
             
          geo.cylinders = RAMHD_CYLINDERS; 
          geo.heads = RAMHD_HEADS; 
          geo.sectors = RAMHD_SECTORS; 
          geo.start = get_start_sect(bdev); 
           
          if(copy_to_user((void*)arg, &geo, sizeof(geo))) 
            return -EFAULT; 
           
          return 0; 
      } 
       
      return -ENOTTY; 
    } 
     
    static struct block_device_operations ramhd_fops = { 
      .owner = THIS_MODULE, 
      .open = ramhd_open, 
      .release = ramhd_release, 
      .ioctl = ramhd_ioctl, 
    }; 
     
    static int ramhd_make_request(struct request_queue* q, struct bio* bio) 
    { 
      char* pRHdata; 
      char* pBuffer; 
      struct bio_vec* bvec; 
      int i; 
      int err = 0; 
       
      struct block_device* bdev = bio->bi_bdev; 
      RAMHD_DEV* pdev = bdev->bd_disk->private_data; 
       
      if(((bio->bi_sector * RAMHD_SECTOR_SIZE) + bio->bi_size) > RAMHD_SIZE){ 
        err = -EIO; 
        return err; 
      } 
       
      pRHdata = pdev->data + (bio->bi_sector * RAMHD_SECTOR_SIZE); 
      bio_for_each_segment(bvec, bio, i){ 
        pBuffer = kmap(bvec->bv_page) + bvec->bv_offset; 
        switch(bio_data_dir(bio)){ 
          case READ: 
            memcpy(pBuffer, pRHdata, bvec->bv_len); 
            flush_dcache_page(bvec->bv_page); 
            break; 
             
          case WRITE: 
            flush_dcache_page(bvec->bv_page); 
            memcpy(pRHdata, pBuffer, bvec->bv_len); 
            break; 
             
          default: 
            kunmap(bvec->bv_page); 
            goto out; 
        } 
         
        kunmap(bvec->bv_page); 
        pRHdata += bvec->bv_len; 
      } 
       
    out: 
      bio_endio(bio, err); 
      return 0; 
    } 
     
    static int alloc_ramdev(void) 
    { 
      int i; 
      for(i = 0; i < RAMHD_MAX_DEVICE; i++){ 
        rdev[i] = kzalloc(sizeof(RAMHD_DEV), GFP_KERNEL); 
        if(!rdev[i]){ 
          return -ENOMEM; 
        } 
      } 
       
      return 0; 
    } 
     
    static void clean_ramdev(void) 
    { 
      int i; 
       
      for(i = 0; i < RAMHD_MAX_DEVICE; i++){ 
        if(rdev[i]) 
          kfree(rdev[i]); 
      } 
    } 
     
    static int __init ramhd_init(void) 
    { 
      int i; 
       
      ramhd_space_init(); 
      alloc_ramdev(); 
       
      ramhd_major = register_blkdev(0, RAMHD_NAME); 
       
      for(i = 0; i < RAMHD_MAX_DEVICE; i++){ 
        rdev[i]->data = sdisk[i]; 
        rdev[i]->queue = blk_alloc_queue(GFP_KERNEL); 
        blk_queue_make_request(rdev[i]->queue, ramhd_make_request); 
         
        rdev[i]->gd = alloc_disk(RAMHD_MAX_PARTITIONS); 
        rdev[i]->gd->major = ramhd_major; 
        rdev[i]->gd->first_minor = i * RAMHD_MAX_PARTITIONS; 
        rdev[i]->gd->fops = &ramhd_fops; 
        rdev[i]->gd->queue = rdev[i]->queue; 
        rdev[i]->gd->private_data = rdev[i]; 
        sprintf(rdev[i]->gd->disk_name, "ramhd%c", 'a' +i); 
        rdev[i]->gd->flags |= GENHD_FL_SUPPRESS_PARTITION_INFO; 
        set_capacity(rdev[i]->gd, RAMHD_SECTOR_TOTAL); 
        add_disk(rdev[i]->gd); 
      } 
       
      return 0; 
    } 
     
    static void __exit ramhd_exit(void) 
    { 
      int i; 
      for(i = 0; i < RAMHD_MAX_DEVICE; i++){ 
        del_gendisk(rdev[i]->gd); 
        put_disk(rdev[i]->gd); 
        blk_cleanup_queue(rdev[i]->queue); 
      } 
       
      clean_ramdev(); 
      ramhd_space_clean(); 
      unregister_blkdev(ramhd_major, RAMHD_NAME); 
    } 
     
    module_init(ramhd_init); 
    module_exit(ramhd_exit); 
     
    MODULE_AUTHOR("dennis__chen@ AMDLinuxFGL"); 
    MODULE_DESCRIPTION("The ramdisk implementation with request function"); 
    MODULE_LICENSE("GPL");