驅動程式 - VxWorks - 23.09 - C/C++ - IOCTL



參考資訊:
https://zhuanlan.zhihu.com/p/597577575
https://studies.ac.upc.edu/doctorat/ENGRAP/VxWorks-device-drivers.htm
https://forums.windriver.com/t/vxworks-software-development-kit-sdk/43
https://mail.prz-rzeszow.pl/~ssamolej/vxworks/vxworks_kernel_programmers_guide_6.6.pdf
https://d13321s3lxgewa.cloudfront.net/downloads/wrsdk-vxworks7-docs/2309/README_qemu.html
https://learning.windriver.com/path/vxworks7-essentials-workbench-and-tools/vxworks-kernel-shell

main.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iosLib.h>
 
static int drvNum = 0;
static DEV_HDR hdr = {0};

static void* myOpen(struct dev_hdr *dev, const char *remainder, int access, int mode)
{
    if (remainder[0] != '\0') {
        return NULL;
    }

    printf("%s()\n", __func__);
    return dev;
}
 
static STATUS myClose(void *dev)
{
    if (dev == NULL) {
        return ERROR;
    }

    printf("%s()\n", __func__);
    return OK;
}

static long myRead(void *dev, char *buf, size_t size)
{
    if (dev == NULL) {
        return 0;
    }

    printf("%s()\n", __func__);
    return 0;
}
static long myWrite(void *dev, const char *buf, size_t size)
{
    if (dev == NULL) {
        return 0;
    }

    printf("%s()\n", __func__);
    return 0;
}

static STATUS myIOCTL(void *dev, int cmd, long arg)
{
    printf("%s(), cmd %d, arg %p\n", __func__, cmd, arg);
    return OK;
}
 
STATUS myProbe(void)
{
    drvNum = iosDrvInstall(NULL, NULL, myOpen, myClose, myRead, myWrite, myIOCTL);
    iosDevAdd(&hdr, "/dev/hello", drvNum);
    printf("Hello, world!\n");
    return OK;
}

編譯

$ wr-cc main.c -o hello -dkm

執行

-> ld < hello

-> myProbe
    Hello, world!

-> open("/dev/hello")
    myOpen()
    value = 18 = 0x12

-> ioctl(18, 100, 0x1234)
    myIOCTL(), cmd 100, arg 0x1234
    value = 0 = 0x0

-> close(18)
    myIOCTL(), cmd 82, arg 0x0
    myClose()
    value = 0 = 0x0

-> unld "hello"