ARM的嵌入式Linux移植體驗之設備驅動

 
設備驅動程序是操作系統內核和機器硬件之間的接口,它爲應用程序屏蔽硬件的細節,一般來說,Linux的設備驅動程序需要完成如下功能:

  ·設備初始化、釋放;

  ·提供各類設備服務;

  ·負責內核和設備之間的數據交換;

  ·檢測和處理設備工作過程中出現的錯誤。

  Linux下的設備驅動程序被組織爲一組完成不同任務的函數的集合,通過這些函數使得Windows的設備操作猶如文件一般。在應用程序看來,硬件設備只是一個設備文件,應用程序可以象操作普通文件一樣對硬件設備進行操作,如open ()、close ()、read ()、write () 等。

  Linux主要將設備分爲二類:字符設備和塊設備。字符設備是指設備發送和接收數據以字符的形式進行;而塊設備則以整個數據緩衝區的形式進行。在對字符設備發出讀/寫請求時,實際的硬件I/O一般就緊接着發生了;而塊設備則不然,它利用一塊系統內存作緩衝區,當用戶進程對設備請求能滿足用戶的要求,就返回請求的數據,如果不能,就調用請求函數來進行實際的I/O操作。塊設備主要針對磁盤等慢速設備。

  1.內存分配

  由於Linux驅動程序在內核中運行,因此在設備驅動程序需要申請/釋放內存時,不能使用用戶級的malloc/free函數,而需由內核級的函數kmalloc/kfree () 來實現,kmalloc()函數的原型爲:

void kmalloc (size_t size ,int priority);
  參數size爲申請分配內存的字節數,kmalloc最多隻能開闢128k的內存;參數priority說明若kmalloc()不能馬上分配內存時用戶進程要採用的動作:GFP_KERNEL 表示等待,即等kmalloc()函數將一些內存安排到交換區來滿足你的內存需要,GFP_ATOMIC 表示不等待,如不能立即分配到內存則返回0 值;函數的返回值指向已分配內存的起始地址,出錯時,返回0。

  kmalloc ()分配的內存需用kfree()函數來釋放,kfree ()被定義爲:

# define kfree (n) kfree_s( (n) ,0)
  其中kfree_s () 函數原型爲:

void kfree_s (void * ptr ,int size);
  參數ptr爲kmalloc()返回的已分配內存的指針,size是要釋放內存的字節數,若爲0 時,由內核自動確定內存的大小。

  2.中斷

  許多設備涉及到中斷操作,因此,在這樣的設備的驅動程序中需要對硬件產生的中斷請求提供中斷服務程序。與註冊基本入口點一樣,驅動程序也要請求內核將特定的中斷請求和中斷服務程序聯繫在一起。在Linux中,用request_irq()函數來實現請求:

int request_irq (unsigned int irq ,void( * handler) int ,unsigned long type ,char * name);
  參數irq爲要中斷請求號,參數handler爲指向中斷服務程序的指針,參數type 用來確定是正常中斷還是快速中斷(正常中斷指中斷服務子程序返回後,內核可以執行調度程序來確定將運行哪一個進程;而快速中斷是指中斷服務子程序返回後,立即執行被中斷程序,正常中斷type 取值爲0 ,快速中斷type 取值爲SA_INTERRUPT),參數name是設備驅動程序的名稱。 3.字符設備驅動

  我們必須爲字符設備提供一個初始化函數,該函數用來完成對所控設備的初始化工作,並調用register_chrdev() 函數註冊字符設備。假設有一字符設備"exampledev",則其init 函數爲:


void exampledev_init(void)
{
if (register_chrdev(MAJOR_NUM, " exampledev ", &exampledev_fops))
TRACE_TXT("Device exampledev driver registered error");
else
TRACE_TXT("Device exampledev driver registered successfully");
…//設備初始化
}
  其中,register_chrdev函數中的參數MAJOR_NUM爲主設備號,"exampledev"爲設備名,exampledev_fops爲包含基本函數入口點的結構體,類型爲file_operations。當執行exampledev_init時,它將調用內核函數register_chrdev,把驅動程序的基本入口點指針存放在內核的字符設備地址表中,在用戶進程對該設備執行系統調用時提供入口地址。

  較早版本內核的file_operations結構體定義爲(代碼及圖示):

struct file_operations
{
 int (*lseek)();
 int (*read)();
 int (*write)();
 int (*readdir)();
 int (*select)();
 int (*ioctl)();
 int (*mmap)();
 int (*open)();
 void(*release)();
 int (*fsync)();
 int (*fasync)();
 int (*check_media_change)();
 void(*revalidate)();
};
ARM的嵌入式Linux移植體驗之設備驅動(圖一)


  隨着內核功能的加強,file_operations結構體也變得更加龐大。但是大多數的驅動程序只是利用了其中的一部分,對於驅動程序中無需提供的功能,只需要把相應位置的值設爲NULL。對於字符設備來說,要提供的主要入口有:open ()、release ()、read ()、write ()、ioctl ()等。

  open()函數 對設備特殊文件進行open()系統調用時,將調用驅動程序的open () 函數:

int (*open)(struct inode * inode,struct file *filp);
  其中參數inode爲設備特殊文件的inode (索引結點) 結構的指針,參數filp是指向這一設備的文件結構的指針。open()的主要任務是確定硬件處在就緒狀態、驗證次設備號的合法性(次設備號可以用MINOR(inode-> i_rdev) 取得)、控制使用設備的進程數、根據執行情況返回狀態碼(0表示成功,負數表示存在錯誤) 等;

  release()函數 當最後一個打開設備的用戶進程執行close ()系統調用時,內核將調用驅動程序的release () 函數:

void (*release) (struct inode * inode,struct file *filp) ;
  release 函數的主要任務是清理未結束的輸入/輸出操作、釋放資源、用戶自定義排他標誌的復位等。

  read()函數 當對設備特殊文件進行read() 系統調用時,將調用驅動程序read() 函數:


ssize_t (*read) (struct file * filp, char * buf, size_t count, loff_t * offp);
  參數buf是指向用戶空間緩衝區的指針,由用戶進程給出,count 爲用戶進程要求讀取的字節數,也由用戶給出。

  read() 函數的功能就是從硬設備或內核內存中讀取或複製count個字節到buf 指定的緩衝區中。在複製數據時要注意,驅動程序運行在內核中,而buf指定的緩衝區在用戶內存區中,是不能直接在內核中訪問使用的,因此,必須使用特殊的複製函數來完成複製工作,這些函數在include/asm/uaccess.h中被聲明:

unsigned long copy_to_user (void * to, void * from, unsigned long len);
  此外,put_user()函數用於內核空間和用戶空間的單值交互(如char、int、long)。

  write( ) 函數 當設備特殊文件進行write () 系統調用時,將調用驅動程序的write () 函數:

ssize_t (*write) (struct file *, const char *, size_t, loff_t *);
  write ()的功能是將參數buf 指定的緩衝區中的count 個字節內容複製到硬件或內核內存中,和read() 一樣,複製工作也需要由特殊函數來完成:

unsigned long copy_from_user(void *to, const void *from, unsigned long n);
  此外,get_user()函數用於內核空間和用戶空間的單值交互(如char、int、long)。

  ioctl() 函數 該函數是特殊的控制函數,可以通過它向設備傳遞控制信息或從設備取得狀態信息,函數原型爲:

int (*ioctl) (struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg);
  參數cmd爲設備驅動程序要執行的命令的代碼,由用戶自定義,參數arg 爲相應的命令提供參數,類型可以是整型、指針等。

  同樣,在驅動程序中,這些函數的定義也必須符合命名規則,按照本文約定,設備"exampledev"的驅動程序的這些函數應分別命名爲exampledev_open、exampledev_ release、exampledev_read、exampledev_write、exampledev_ioctl,因此設備"exampledev"的基本入口點結構變量exampledev_fops 賦值如下(對較早版本的內核):

struct file_operations exampledev_fops {
 NULL ,
 exampledev_read ,
 exampledev_write ,
 NULL ,
 NULL ,
 exampledev_ioctl ,
 NULL ,
 exampledev_open ,
 exampledev_release ,
 NULL ,
 NULL ,
 NULL ,
 NULL
} ;
  就目前而言,由於file_operations結構體已經很龐大,我們更適合用GNU擴展的C語法來初始化exampledev_fops:

struct file_operations exampledev_fops = {
 read: exampledev _read,
 write: exampledev _write,
ioctl: exampledev_ioctl ,
 open: exampledev_open ,
 release : exampledev_release ,
};
  看看第一章電路板硬件原理圖,板上包含四個用戶可編程的發光二極管(LED),這些LED連接在ARM處理器的可編程I/O口(GPIO)上,現在來編寫這些LED的驅動:

#include <linux/config.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/miscdevice.h>
#include <linux/sched.h>
#include <linux/delay.h>
#include <asm/hardware.h>
#define DEVICE_NAME "leds" /*定義led 設備的名字*/
#define LED_MAJOR 231 /*定義led 設備的主設備號*/
static unsigned long led_table[] =
{
 /*I/O 方式led 設備對應的硬件資源*/
 GPIO_B10, GPIO_B8, GPIO_B5, GPIO_B6,
};
/*使用ioctl 控制led*/
static int leds_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
unsigned long arg)
{
 switch (cmd)
 {
  case 0:
  case 1:
   if (arg > 4)
   {
    return -EINVAL;
   }
   write_gpio_bit(led_table[arg], !cmd);
  default:
   return -EINVAL;
 }
}
static struct file_operations leds_fops =
{
 owner: THIS_MODULE, ioctl: leds_ioctl,
};
static devfs_handle_t devfs_handle;
static int __init leds_init(void)
{
 int ret;
 int i;
 /*在內核中註冊設備*/
 ret = register_chrdev(LED_MAJOR, DEVICE_NAME, &leds_fops);
 if (ret < 0)
 {
  printk(DEVICE_NAME " can't register major number\n");
  return ret;
 }
 devfs_handle = devfs_register(NULL, DEVICE_NAME, DEVFS_FL_DEFAULT, LED_MAJOR,
0, S_IFCHR S_IRUSR S_IWUSR, &leds_fops, NULL);
 /*使用宏進行端口初始化,set_gpio_ctrl 和write_gpio_bit 均爲宏定義*/
 for (i = 0; i < 8; i++)
 {
  set_gpio_ctrl(led_table[i] GPIO_PULLUP_EN GPIO_MODE_OUT);

  write_gpio_bit(led_table[i], 1);
 }
 printk(DEVICE_NAME " initialized\n");
 return 0;
}

static void __exit leds_exit(void)
{
 devfs_unregister(devfs_handle);
 unregister_chrdev(LED_MAJOR, DEVICE_NAME);
}

module_init(leds_init);
module_exit(leds_exit);
  使用命令方式編譯led 驅動模塊:

#arm-linux-gcc -D__KERNEL__ -I/arm/kernel/include
-DKBUILD_BASENAME=leds -DMODULE -c -o leds.o leds.c
  以上命令將生成leds.o 文件,把該文件複製到板子的/lib目錄下,使用以下命令就可以安裝leds驅動模塊:

#insmod /lib/ leds.o
  刪除該模塊的命令是:

#rmmod leds 4.塊設備驅動

  塊設備驅動程序的編寫是一個浩繁的工程,其難度遠超過字符設備,上千行的代碼往往只能搞定一個簡單的塊設備,而數十行代碼就可能搞定一個字符設備。因此,非得有相當的基本功才能完成此項工作。下面先給出一個實例,即mtdblock塊設備的驅動。我們通過分析此實例中的代碼來說明塊設備驅動程序的寫法(由於篇幅的關係,大量的代碼被省略,只保留了必要的主幹):

#include <linux/config.h>
#include <linux/devfs_fs_kernel.h>
static void mtd_notify_add(struct mtd_info* mtd);
static void mtd_notify_remove(struct mtd_info* mtd);
static struct mtd_notifier notifier = {
 mtd_notify_add,
 mtd_notify_remove,
 NULL
};
static devfs_handle_t devfs_dir_handle = NULL;
static devfs_handle_t devfs_rw_handle[MAX_MTD_DEVICES];

static struct mtdblk_dev {
 struct mtd_info *mtd; /* Locked */
 int count;
 struct semaphore cache_sem;
 unsigned char *cache_data;
 unsigned long cache_offset;
 unsigned int cache_size;
 enum { STATE_EMPTY, STATE_CLEAN, STATE_DIRTY } cache_state;
} *mtdblks[MAX_MTD_DEVICES];

static spinlock_t mtdblks_lock;
/* this lock is used just in kernels >= 2.5.x */
static spinlock_t mtdblock_lock;

static int mtd_sizes[MAX_MTD_DEVICES];
static int mtd_blksizes[MAX_MTD_DEVICES];

static void erase_callback(struct erase_info *done)
{
 wait_queue_head_t *wait_q = (wait_queue_head_t *)done->priv;
 wake_up(wait_q);
}

static int erase_write (struct mtd_info *mtd, unsigned long pos,
int len, const char *buf)
{
 struct erase_info erase;
 DECLARE_WAITQUEUE(wait, current);
 wait_queue_head_t wait_q;
 size_t retlen;
 int ret;

 /*
 * First, let's erase the flash block.
 */

 init_waitqueue_head(&wait_q);
 erase.mtd = mtd;
 erase.callback = erase_callback;
 erase.addr = pos;
 erase.len = len;
 erase.priv = (u_long)&wait_q;

 set_current_state(TASK_INTERRUPTIBLE);
 add_wait_queue(&wait_q, &wait);

 ret = MTD_ERASE(mtd, &erase);
 if (ret) {
  set_current_state(TASK_RUNNING);
  remove_wait_queue(&wait_q, &wait);
  printk (KERN_WARNING "mtdblock: erase of region [0x%lx, 0x%x] " "on \"%s\" failed\n",
pos, len, mtd->name);
  return ret;
 }

 schedule(); /* Wait for erase to finish. */
 remove_wait_queue(&wait_q, &wait);

 /*
 * Next, writhe data to flash.
 */

 ret = MTD_WRITE (mtd, pos, len, &retlen, buf);
 if (ret)
  return ret;
 if (retlen != len)
  return -EIO;
 return 0;
}

static int write_cached_data (struct mtdblk_dev *mtdblk)
{
 struct mtd_info *mtd = mtdblk->mtd;
 int ret;

 if (mtdblk->cache_state != STATE_DIRTY)
  return 0;

 DEBUG(MTD_DEBUG_LEVEL2, "mtdblock: writing cached data for \"%s\" "
"at 0x%lx, size 0x%x\n", mtd->name,
mtdblk->cache_offset, mtdblk->cache_size);
 ret = erase_write (mtd, mtdblk->cache_offset,
mtdblk->cache_size, mtdblk->cache_data);
 if (ret)
  return ret;

 mtdblk->cache_state = STATE_EMPTY;
 return 0;
}

static int do_cached_write (struct mtdblk_dev *mtdblk, unsigned long pos,
int len, const char *buf)
{
 …
}

static int do_cached_read (struct mtdblk_dev *mtdblk, unsigned long pos,
int len, char *buf)
{
 …
}

static int mtdblock_open(struct inode *inode, struct file *file)
{
 …
}

static release_t mtdblock_release(struct inode *inode, struct file *file)
{
 int dev;
 struct mtdblk_dev *mtdblk;
 DEBUG(MTD_DEBUG_LEVEL1, "mtdblock_release\n");

 if (inode == NULL)
  release_return(-ENODEV);

 dev = minor(inode->i_rdev);
 mtdblk = mtdblks[dev];

 down(&mtdblk->cache_sem);
 write_cached_data(mtdblk);
 up(&mtdblk->cache_sem);

 spin_lock(&mtdblks_lock);
 if (!--mtdblk->count) {
  /* It was the last usage. Free the device */
  mtdblks[dev] = NULL;
  spin_unlock(&mtdblks_lock);
  if (mtdblk->mtd->sync)
   mtdblk->mtd->sync(mtdblk->mtd);
   put_mtd_device(mtdblk->mtd);
   vfree(mtdblk->cache_data);
   kfree(mtdblk);
 } else {
  spin_unlock(&mtdblks_lock);
 }

 DEBUG(MTD_DEBUG_LEVEL1, "ok\n");
 
 BLK_DEC_USE_COUNT;
 release_return(0);
}

/*
* This is a special request_fn because it is executed in a process context
* to be able to sleep independently of the caller. The
* io_request_lock (for <2.5) or queue_lock (for >=2.5) is held upon entry


* and exit. The head of our request queue is considered active so there is
* no need to dequeue requests before we are done.
*/
static void handle_mtdblock_request(void)
{
 struct request *req;
 struct mtdblk_dev *mtdblk;
 unsigned int res;

 for (;;) {
  INIT_REQUEST;
  req = CURRENT;
  spin_unlock_irq(QUEUE_LOCK(QUEUE));
  mtdblk = mtdblks[minor(req->rq_dev)];
  res = 0;

  if (minor(req->rq_dev) >= MAX_MTD_DEVICES)
   panic("%s : minor out of bound", __FUNCTION__);

  if (!IS_REQ_CMD(req))
   goto end_req;

  if ((req->sector + req->current_nr_sectors) > (mtdblk->mtd->size >> 9))
   goto end_req;

  // Handle the request
  switch (rq_data_dir(req))
  {
   int err;

   case READ:
    down(&mtdblk->cache_sem);
    err = do_cached_read (mtdblk, req->sector << 9,
req->current_nr_sectors << 9,
req->buffer);
    up(&mtdblk->cache_sem);
    if (!err)
     res = 1;
    break;

   case WRITE:
    // Read only device
    if ( !(mtdblk->mtd->flags & MTD_WRITEABLE) )
     break;

    // Do the write
    down(&mtdblk->cache_sem);
    err = do_cached_write (mtdblk, req->sector << 9,req->current_nr_sectors << 9, req->buffer);
    up(&mtdblk->cache_sem);
    if (!err)
     res = 1;
    break;
  }

 end_req:
 spin_lock_irq(QUEUE_LOCK(QUEUE));
 end_request(res);
}
}

static volatile int leaving = 0;
static DECLARE_MUTEX_LOCKED(thread_sem);
static DECLARE_WAIT_QUEUE_HEAD(thr_wq);

int mtdblock_thread(void *dummy)
{
 …
}

#define RQFUNC_ARG request_queue_t *q

static void mtdblock_request(RQFUNC_ARG)
{
 /* Don't do anything, except wake the thread if necessary */
 wake_up(&thr_wq);
}

static int mtdblock_ioctl(struct inode * inode, struct file * file,
unsigned int cmd, unsigned long arg)
{
 struct mtdblk_dev *mtdblk;
 mtdblk = mtdblks[minor(inode->i_rdev)];
 switch (cmd) {
  case BLKGETSIZE: /* Return device size */
   return put_user((mtdblk->mtd->size >> 9), (unsigned long *) arg);

  case BLKFLSBUF:
   if(!capable(CAP_SYS_ADMIN))
    return -EACCES;
   fsync_dev(inode->i_rdev);
   invalidate_buffers(inode->i_rdev);
   down(&mtdblk->cache_sem);
   write_cached_data(mtdblk);
   up(&mtdblk->cache_sem);
   if (mtdblk->mtd->sync)
    mtdblk->mtd->sync(mtdblk->mtd);
    return 0;
  default:
   return -EINVAL;
 }
}

static struct block_device_operations mtd_fops =
{
 owner: THIS_MODULE,
 open: mtdblock_open,
 release: mtdblock_release,
 ioctl: mtdblock_ioctl
};

static void mtd_notify_add(struct mtd_info* mtd)
{
 …
}

static void mtd_notify_remove(struct mtd_info* mtd)
{
 if (!mtd mtd->type == MTD_ABSENT)
  return;

 devfs_unregister(devfs_rw_handle[mtd->index]);
}

int __init init_mtdblock(void)
{
 int i;

 spin_lock_init(&mtdblks_lock);
 /* this lock is used just in kernels >= 2.5.x */
 spin_lock_init(&mtdblock_lock);

 #ifdef CONFIG_DEVFS_FS

 if (devfs_register_blkdev(MTD_BLOCK_MAJOR, DEVICE_NAME, &mtd_fops))
 {
  printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n",
MTD_BLOCK_MAJOR);
  return -EAGAIN;
 }

 devfs_dir_handle = devfs_mk_dir(NULL, DEVICE_NAME, NULL);
 register_mtd_user(&notifier);
 #else
  if (register_blkdev(MAJOR_NR,DEVICE_NAME,&mtd_fops)) {
   printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n",
MTD_BLOCK_MAJOR);
  return -EAGAIN;
 }
 #endif

/* We fill it in at open() time. */
for (i=0; i< MAX_MTD_DEVICES; i++) {
 mtd_sizes[i] = 0;
 mtd_blksizes[i] = BLOCK_SIZE;
}
init_waitqueue_head(&thr_wq);
/* Allow the block size to default to BLOCK_SIZE. */
blksize_size[MAJOR_NR] = mtd_blksizes;
blk_size[MAJOR_NR] = mtd_sizes;

BLK_INIT_QUEUE(BLK_DEFAULT_QUEUE(MAJOR_NR), &mtdblock_request, &mtdblock_lock);

kernel_thread (mtdblock_thread, NULL, CLONE_FSCLONE_FILESCLONE_SIGHAND);
return 0;
}

static void __exit cleanup_mtdblock(void)
{
 leaving = 1;
 wake_up(&thr_wq);
 down(&thread_sem);
 #ifdef CONFIG_DEVFS_FS
  unregister_mtd_user(&notifier);
  devfs_unregister(devfs_dir_handle);
  devfs_unregister_blkdev(MTD_BLOCK_MAJOR, DEVICE_NAME);
 #else
  unregister_blkdev(MAJOR_NR,DEVICE_NAME);
 #endif
 blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR));
 blksize_size[MAJOR_NR] = NULL;
 blk_size[MAJOR_NR] = NULL;
}

module_init(init_mtdblock);
module_exit(cleanup_mtdblock);
  從上述源代碼中我們發現,塊設備也以與字符設備register_chrdev、unregister_ chrdev 函數類似的方法進行設備的註冊與釋放:


int register_blkdev(unsigned int major, const char *name, struct block_device_operations *bdops);
int unregister_blkdev(unsigned int major, const char *name);
  但是,register_chrdev使用一個向 file_operations 結構的指針,而register_blkdev 則使用 block_device_operations 結構的指針,其中定義的open、release 和 ioctl 方法和字符設備的對應方法相同,但未定義 read 或者 write 操作。這是因爲,所有涉及到塊設備的 I/O 通常由系統進行緩衝處理。

  塊驅動程序最終必須提供完成實際塊 I/O 操作的機制,在 Linux 當中,用於這些 I/O 操作的方法稱爲"request(請求)"。在塊設備的註冊過程中,需要初始化request隊列,這一動作通過blk_init_queue來完成,blk_init_queue函數建立隊列,並將該驅動程序的 request 函數關聯到隊列。在模塊的清除階段,應調用 blk_cleanup_queue 函數。

  本例中相關的代碼爲:

BLK_INIT_QUEUE(BLK_DEFAULT_QUEUE(MAJOR_NR), &mtdblock_request, &mtdblock_lock);
blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR));
  每個設備有一個默認使用的請求隊列,必要時,可使用 BLK_DEFAULT_QUEUE(major) 宏得到該默認隊列。這個宏在 blk_dev_struct 結構形成的全局數組(該數組名爲 blk_dev)中搜索得到對應的默認隊列。blk_dev 數組由內核維護,並可通過主設備號索引。blk_dev_struct 接口定義如下:

struct blk_dev_struct {
 /*
 * queue_proc has to be atomic
 */
 request_queue_t request_queue;
 queue_proc *queue;
 void *data;
};
  request_queue 成員包含了初始化之後的 I/O 請求隊列,data 成員可由驅動程序使用,以便保存一些私有數據。

  request_queue定義爲:

struct request_queue
{
 /*
 * the queue request freelist, one for reads and one for writes
 */
 struct request_list rq[2];

 /*
 * Together with queue_head for cacheline sharing
 */
 struct list_head queue_head;
 elevator_t elevator;

 request_fn_proc * request_fn;
 merge_request_fn * back_merge_fn;
 merge_request_fn * front_merge_fn;
 merge_requests_fn * merge_requests_fn;
 make_request_fn * make_request_fn;
 plug_device_fn * plug_device_fn;

 /*
 * The queue owner gets to use this for whatever they like.
 * ll_rw_blk doesn't touch it.
 */
 void * queuedata;

 /*
 * This is used to remove the plug when tq_disk runs.
 */
 struct tq_struct plug_tq;

 /*
 * Boolean that indicates whether this queue is plugged or not.
 */
 char plugged;

 /*
 * Boolean that indicates whether current_request is active or
 * not.
 */
 char head_active;

 /*
 * Is meant to protect the queue in the future instead of
 * io_request_lock
 */
 spinlock_t queue_lock;

 /*
 * Tasks wait here for free request
 */
 wait_queue_head_t wait_for_request;
};
  下圖表徵了blk_dev、blk_dev_struct和request_queue的關係:

ARM的嵌入式Linux移植體驗之設備驅動(圖二)

  下圖則表徵了塊設備的註冊和釋放過程:

ARM的嵌入式Linux移植體驗之設備驅動(圖三)

  5.小結

  本章講述了Linux設備驅動程序的入口函數及驅動程序中的內存申請、中斷等,並分別以實例講述了字符設備及塊設備的驅動開發方法。

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章