首 页 | 新 闻 | 文 档 | 代 码 | 工 具 | 论 坛

Mp4Tech 首页  >  文 档  >   操作系统
 
 

移植嵌入式Linux到ARM处理器:设备驱动(16)



内容简介:
============================
Linux下的设备驱动程序被组织为一组完成不同任务的函数的集合,通过这些函数使得Windows的设备操作犹如文件一般.在应用程序看来,硬件设备 只是一个设备文件,应用程序可以象操作普通文件一样对硬件设备进行操作,如open ()、close ()、read ()、write () 等。

设备驱动程序是操作系统内核和机器硬件之间的接口,它为应用程序屏蔽硬件的细节,一般来说,Linux的设备驱动程序需要完成如下功能:
设备初始化、释放;
   ·提供各类设备服务;
   ·负责内核和设备之间的数据交换;
   ·检测和处理设备工作过程中出现的错误。
============================

   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)();
};



    随着内核功能的加强,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

作者:宋宝华   更新日期:2006-11-21
来源:dev.yesky.com

 


联系我们
便携式多媒体技术中心
All Rights Reserved