你好,游客 登录 注册 搜索
背景:
阅读新闻

移植Linux的外围设备驱动到QNX系统中

[日期:2013-04-11] 来源:Linux社区  作者:newzzorg [字体:大 中 小]

为了将原有的Linux驱动移植到QNX上,需要改动read/write以及ioctl的函数接口参数。并将与Linux内核相关的函数去掉(通常不会在QNX里面用到)以read函数代码为例:
 

size_t rtc_read(char *buf, size_t len, int *ppos){
    VR_TIME *tm = (VR_TIME *)buf;
    static unsigned char tmp = 0;
    tmp = i2c_rtime(0x00) & 0x7f;    tm->second = BCD2DEC(tmp);
    tmp = i2c_rtime(0x10) & 0x7f;      tm->minute = BCD2DEC(tmp);
    tmp = i2c_rtime(0x20) & 0x3f;      tm->hour  = BCD2DEC(tmp);
    tmp = i2c_rtime(0x40) & 0x3f;      tm->day    = BCD2DEC(tmp);
    tmp = i2c_rtime(0x50) & 0x1f;      tm->month  = BCD2DEC(tmp);
    tmp = i2c_rtime(0x60);            tm->year  = BCD2DEC(tmp)+ 1920;
    return 0;
 }
 
结构体VR_TIME是自定义的。
 经过多次驱动的编写和调整,我发现还是Linux的那种回调机制在移植的时候代码改动最方便。因此,我自己也定义了一个结构体,用于模仿Linux的驱动框架,整合两个系统的差异。
 
struct driver_interface ;
 struct driver_interface get_driver_interface(){
    struct driver_interface drv;
    drv.drv_init  = rtc_init;
    drv.drv_read  = rtc_read;
    drv.drv_write = rtc_write;
    drv.drv_ioctl = rtc_ioctl;
    return drv;
 }
 这个框架差异整合的结构体为:
 
struct driver_interface{
    void (*drv_init)(void);
    size_t (*drv_read)(char *buf, size_t len, int *ppos);
    size_t (*drv_write)( const char *buf, size_t len, int *ppos);
    int (*drv_ioctl)(unsigned int cmd, unsigned long arg);
 };
 
struct driver_interface get_driver_interface();
 然后我们来看看QNX的驱动框架,根据这个框架,我们将自己的drv整合进去,于是便有了下面的代码:
 文件名为:driver_main.c
 

/*
  *  driver_main.c
  *
  *  This module contains the source code for the /dev/rtc device.
  *
  *  This module contains all of the functions necessary.
  *
  */
 
#include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 #include <sys/iofunc.h>
 #include <sys/dispatch.h>
 #include <sys/neutrino.h>
 #include <sys/resmgr.h>
 

#include <arm/atmel-at91sam9260.h>
 #include "linux2qnx_drv.h"
 
//#include "MMA8452Q.h" 
 #include "rtc.h"
 

#define EXAMPLE_NAME "/dev/rtc"
 

struct driver_interface drv;
 
/* change to something else if sharing a target */
 // #define EXAMPLE_NAME "/dev/dagexample"
 
void options (int argc, char *argv[]);
 
/*
  *  these prototypes are needed since we are using their names in main ()
  */
 
int io_open (resmgr_context_t *ctp, io_open_t  *msg, RESMGR_HANDLE_T *handle, void *extra);
 int io_read (resmgr_context_t *ctp, io_read_t  *msg, RESMGR_OCB_T *ocb);
 int io_write(resmgr_context_t *ctp, io_write_t *msg, RESMGR_OCB_T *ocb);
 int    io_devctl(resmgr_context_t *ctp, io_devctl_t *msg, RESMGR_OCB_T *ocb);
 
/*
  *  our connect and I/O functions
  */
 
resmgr_connect_funcs_t  connect_funcs;
 resmgr_io_funcs_t      io_funcs;
 
/*
  *  our dispatch, resource manager and iofunc variables
  */
 
dispatch_t              *dpp;
 resmgr_attr_t          rattr;
 dispatch_context_t      *ctp;
 iofunc_attr_t          ioattr;
 
char    *progname = "rtc_driver";
 int    optv;                              // -v for verbose operation
 
int main (int argc, char *argv[]){
    printf ("%s:  starting\n", progname);
    int pathID;
    options (argc, argv);
 
    drv = get_driver_interface();
    drv.drv_init();
 
    /*
      * allocate and initialize a dispatch structure for use by our
      * main loop
      */
    dpp = dispatch_create ();
    if (dpp == NULL) {
        fprintf (stderr, "%s:  couldn't dispatch_create: %s\n",
                  progname, strerror (errno));
        exit (1);
    }
    /*
      * set up the resource manager attributes structure, we'll
      * use this as a way of passing information to resmgr_attach().
      * For now, we just use defaults.
      */
    memset (&rattr, 0, sizeof (rattr)); /* using the defaults for rattr */
    /*
      * initialize the connect functions and I/O functions tables to
      * their defaults by calling iofunc_func_init().
      *
      * connect_funcs, and io_funcs variables are already declared.
      *
      */
    iofunc_func_init (_RESMGR_CONNECT_NFUNCS, &connect_funcs,
                      _RESMGR_IO_NFUNCS, &io_funcs);
    /* over-ride the connect_funcs handler for open with our io_open,
      * and over-ride the io_funcs handlers for read and write with our
      * io_read and io_write handlers
      */
    connect_funcs.open = io_open;
    io_funcs.read = io_read;
    io_funcs.write = io_write;
    io_funcs.devctl = io_devctl;
    /* initialize our device description structure
      */
    iofunc_attr_init (&ioattr, S_IFCHR | 0666, NULL, NULL);
    /*
      *  call resmgr_attach to register our prefix with the
      *  process manager, and also to let it know about our connect
      *  and I/O functions.
      *
      *  On error, returns -1 and errno is set.
      */
    pathID = resmgr_attach (dpp, &rattr, EXAMPLE_NAME, _FTYPE_ANY, 0,
                            &connect_funcs, &io_funcs, &ioattr);
    if (pathID == -1) {
        fprintf (stderr, "%s:  couldn't attach pathname: %s\n",
                  progname, strerror (errno));
        exit (1);
    }
 
    ctp = dispatch_context_alloc (dpp);
    while (1) {
        if ((ctp = dispatch_block (ctp)) == NULL) {
            fprintf (stderr, "%s:  dispatch_block failed: %s\n",
                      progname, strerror (errno));
            exit (1);
        }
        dispatch_handler (ctp);
    }
 }
 
/*
  *  io_open
  *
  *  we are called here when the client does an open.
  *  It is up to us to establish a context (in this
  *  case NULL will do just fine), and return a status
  *  code.
  */
 int io_open (resmgr_context_t *ctp, io_open_t *msg,
              RESMGR_HANDLE_T *handle, void *extra){
    if (optv) {
        printf ("%s:  in io_open\n", progname);
    }
 
    return (iofunc_open_default (ctp, msg, handle, extra));
 }
 
/*
  *  io_read
  *
  *  At this point, the client has called their library "read"
  *  function, and expects zero or more bytes.  Currently our
  *  /dev/example resource manager returns zero bytes to
  *  indicate EOF -- no more bytes expected.
  *
  *  After our exercises, it will return some data.
  */
 int io_read (resmgr_context_t *ctp, io_read_t *msg, RESMGR_OCB_T *ocb){
    int status;
    int nb = sizeof(VR_TIME);
    static VR_TIME data;
 
    if (optv) {
        printf ("%s:  in io_read\n", progname);
    }
 
    if ((status = iofunc_read_verify(ctp, msg, ocb, NULL)) != EOK) {
        if (optv) printf("read failed because of error %d\n", status );
        return status;
    }
 
    // No special xtypes
    if ((msg->i.xtype & _IO_XTYPE_MASK) != _IO_XTYPE_NONE) {
        return ENOSYS;  // causes MsgError( ctp->rcvid, ENOSYS );
    }
 
    drv.drv_read((char*)&data, sizeof(data), 0);
 
    nb = min( nb, msg->i.nbytes );
 
    _IO_SET_READ_NBYTES (ctp, nb); // ctp->status = nb
    SETIOV( ctp->iov, &data, nb );
 
    if (nb > 0)
        ocb->attr->flags |= IOFUNC_ATTR_ATIME;
 
    return _RESMGR_NPARTS (1); // causes MsgReplyv( ctp->rcvid, ctp->status, ctp->iov, 1 );
 }
 
/*
  *  io_write
  *
  *  At this point, the client has called their library "write"
  *  function, and expects that our resource manager will write
  *  the number of bytes that they have specified to some device.
  *
  *  Currently, for /dev/example, all of the clients writes always
  *  work -- they just go into Deep Outer Space.
  *
  *  After our updates, they will be displayed on standard out.
  */
 int io_write (resmgr_context_t *ctp, io_write_t *msg, RESMGR_OCB_T *ocb){
    int status;
    int nb;
    VR_TIME *buf;
 
    if (optv) {
        printf ("\n\n\n%s:  in io_write, of %d bytes\n\n\n", progname, msg->i.nbytes);
    }
 
    if ((status = iofunc_write_verify(ctp, msg, ocb, NULL)) != EOK)
        return status;
 
    // No special xtypes
    if ((msg->i.xtype & _IO_XTYPE_MASK) != _IO_XTYPE_NONE) {
        return ENOSYS;
    }
 
    /* first process any data already in the receive buff */
 
    // skip the io_write header to get to the data
    buf = (VR_TIME *)(msg+1);
    // calculate number of bytes of client data in receive buffer
    nb = ctp->info.msglen - (ctp->offset + sizeof(*msg) );
    status = drv.drv_write( (char*)buf, sizeof(*buf), 0 );
 
    _IO_SET_WRITE_NBYTES (ctp, nb);
 
    // if we actually handled any data, mark that a write was done for
    // time updates (POSIX stuff)
    if (nb > 0)
        ocb->attr->flags |= IOFUNC_ATTR_MTIME | IOFUNC_ATTR_CTIME;
 
    return _RESMGR_NPARTS (0);
 }
 
int    io_devctl(resmgr_context_t *ctp, io_devctl_t *msg, RESMGR_OCB_T *ocb){
    int    nbytes, status;
 
    union {  /* See note 1 */
        XYZunion8  arg_info_data;
        int    data32;
        /*  other devctl types you can receive */
    } *rx_data;
 
    /*
      Let common code handle DCMD_ALL_* cases.
      You can do this before or after you intercept devctls, depending
      on your intentions.  Here we aren't using any predefined values,
      so let the system ones be handled first. See note 2.
    */
    if ((status = iofunc_devctl_default(ctp, msg, ocb)) !=
          _RESMGR_DEFAULT) {
        return(status);
    }
    status = nbytes = 0;
 
    /*
      Note this assumes that you can fit the entire data portion of
      the devctl into one message.  In reality you should probably
      perform a MsgReadv() once you know the type of message you
      have received to get all of the data, rather than assume
      it all fits in the message.  We have set in our main routine
      that we'll accept a total message size of up to 2 KB, so we
      don't worry about it in this example where we deal with ints.
    */
 
    /* Get the data from the message. See Note 3. */
    rx_data = _DEVCTL_DATA(msg->i);
    /*
      Three examples of devctl operations:
      SET: Set a value (int) in the server
      GET: Get a value (int) from the server
      SETGET: Set a new value and return the previous value
    */
    _Int32t linux_cmd = (msg->i.dcmd);
 
    if((linux_cmd & SETVAL) && ((GETVAL & linux_cmd))){  // SETGET is SETVAL|GETVAL == 0x800000000|0x40000000
        linux_cmd &= ~SETGET;
        linux_cmd -= (sizeof(XYZunion8)<<16);
        //printf("SG = 0x%0x", SETGET);
 
        drv.drv_ioctl(linux_cmd, (unsigned long)&(rx_data->arg_info_data) );
        nbytes = sizeof( rx_data->arg_info_data );
 
    }else if(linux_cmd & GETVAL){
        linux_cmd &= ~GETVAL;
        linux_cmd -= (sizeof(int)<<16);
        printf("G = 0x%x", GETVAL);
        printf(", linux_cmd=%d, data address =%d\n", linux_cmd, (int)&(rx_data->data32));
        drv.drv_ioctl(linux_cmd, (unsigned long )&(rx_data->data32));
        nbytes = sizeof(&rx_data->data32);
 
    }else if(linux_cmd & SETVAL ){
        linux_cmd &= ~SETVAL;
        linux_cmd -= (sizeof(int)<<16);
        printf("S = 0x%x", SETVAL);
        printf(", linux_cmd=%d, data=%d\n", linux_cmd, rx_data->data32);
 
        drv.drv_ioctl(linux_cmd, (int)(rx_data->data32) );
    }else{
        printf("undefined functionality\n");
        //TODO
          return -1;
    }
 
    /* Clear the return message. Note that we saved our data past
        this location in the message. */
    memset(&msg->o, 0, sizeof(msg->o));
    /*
      If you wanted to pass something different to the return
      field of the devctl() you could do it through this member.
      See note 5.
    */
    msg->o.ret_val = status;
 
    /* Indicate the number of bytes and return the message */
    msg->o.nbytes = nbytes;
    return(_RESMGR_PTR(ctp, &msg->o, sizeof(msg->o) + nbytes));
 }
 
/*
  *  options
  *
  *  This routine handles the command line options.
  *  For our simple /dev/example, we support:
  *      -v      verbose operation
  */
 void options (int argc, char *argv[]){
    optv = 0;
    int    opt;
 
    while ((opt = getopt (argc, argv, "v")) != -1) {
        if( opt == 'v' ){
            optv++;
        }
    }
 }
 这样,我们的Linux驱动就能基本原封不动的移植到QNX里面。

linux
相关资讯       Linux移植  QNX  QNX系统 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款