不使用 mmap 写入 /dev/mem

不使用 mmap 写入 /dev/mem

不使用 mmap 可以在 /dev/mem 上写入吗?

我在 LKM 内的 Raspberry Pi 上启用上拉电阻,但该功能void *mmap (caddr_t addr, size_t len, int prot, int flags, int fd, off_t offset)不存在。

我尝试使用open(稍后将其转换为filp_open),但它什么也没做:

#include <stdio.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <time.h>
#include <errno.h>

// From https://github.com/RPi-Distro/raspi-gpio/blob/master/raspi-gpio.c


#define PULL_UNSET  -1
#define PULL_NONE   0
#define PULL_DOWN   1
#define PULL_UP     2

#define GPIO_BASE_OFFSET 0x00200000
#define GPPUD       37
#define GPPUDCLK0   38
#define BASE_READ   0x1000
#define BASE_SIZE   (BASE_READ/sizeof(uint32_t))

uint32_t getGpioRegBase(void) {
    const char *revision_file = "/proc/device-tree/system/linux,revision";
    uint8_t revision[4] = { 0 };
    uint32_t cpu = 0;
    FILE *fd;

    if ((fd = fopen(revision_file, "rb")) == NULL) {
        printf("Can't open '%s'\n", revision_file);
        exit(EXIT_FAILURE);
    }
    else {
        if (fread(revision, 1, sizeof(revision), fd) == 4) cpu = (revision[2] >> 4) & 0xf;
        else {
            printf("Revision data too short\n");
            exit(EXIT_FAILURE);
        }

        fclose(fd);
    }

    printf("CPU: %d\n", cpu);
    switch (cpu) {
        case 0: // BCM2835 [Pi 1 A; Pi 1 B; Pi 1 B+; Pi Zero; Pi Zero W]
            //chip = &gpio_chip_2835;
            return 0x20000000 + GPIO_BASE_OFFSET;
        case 1: // BCM2836 [Pi 2 B]
        case 2: // BCM2837 [Pi 3 B; Pi 3 B+; Pi 3 A+]
            //chip = &gpio_chip_2835;
            return 0x3f000000 + GPIO_BASE_OFFSET;
        case 3: // BCM2711 [Pi 4 B]
            //chip = &gpio_chip_2711;
            return 0xfe000000 + GPIO_BASE_OFFSET;
        default:
            printf("Unrecognised revision code\n");
            exit(1);
    }
}

int writeBase(uint32_t reg_base, uint32_t offset, uint32_t data) {
    int fd;
    if ((fd = open("/dev/mem", O_RDWR | O_SYNC | O_CLOEXEC) ) < 0) return -1;
    
    if (lseek(fd, reg_base+offset, SEEK_SET) == -1) return -2;
    if (write(fd, (void*)&data, sizeof(uint32_t)) != sizeof(uint32_t)) return -3;
    if (close(fd) == -1) return -4;
    return 0;
}

int setPull(unsigned int gpio, int pull) {
    int r;
    int clkreg = GPPUDCLK0 + (gpio / 32);
    int clkbit = 1 << (gpio % 32);
    uint32_t reg_base = getGpioRegBase();

    r = writeBase(reg_base, GPPUD, pull); // base[GPPUD] = pull
    if (r < 0) return r;
    usleep(10);
    r = writeBase(reg_base, clkreg, clkbit); // base[clkreg] = clkbit
    if (r < 0) return r;
    usleep(10);
    r = writeBase(reg_base, GPPUD, 0); // base[GPPUD] = 0
    if (r < 0) return r;
    usleep(10);
    r = writeBase(reg_base, clkreg, 0); // base[clkreg] = 0
    usleep(10);
    return r;
}

int main(int argc, char *argv[]) {
    int gpio, r;
    
    if (argc!=2) {
        printf("GPIO pin needed!\n");
        return 1;
    }
    
    gpio = atoi(argv[1]);
    printf("Enabling pull-up on GPIO%d...\n", gpio);
    r = setPull(gpio, PULL_UP);
    printf("Return value: %d\n", r);
    if (r != 0) printf("%s\n", strerror(errno));
    return r;
}

raspi-gpio这是我想要的片段:

#include <stdio.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <time.h>

// From https://github.com/RPi-Distro/raspi-gpio/blob/master/raspi-gpio.c


#define PULL_UNSET  -1
#define PULL_NONE    0
#define PULL_DOWN    1
#define PULL_UP      2

#define GPIO_BASE_OFFSET 0x00200000
#define GPPUD        37
#define GPPUDCLK0    38

uint32_t getGpioRegBase(void) {
    const char *revision_file = "/proc/device-tree/system/linux,revision";
    uint8_t revision[4] = { 0 };
    uint32_t cpu = 0;
    FILE *fd;

    if ((fd = fopen(revision_file, "rb")) == NULL)
    {
        printf("Can't open '%s'\n", revision_file);
    }
    else
    {
        if (fread(revision, 1, sizeof(revision), fd) == 4)
            cpu = (revision[2] >> 4) & 0xf;
        else
            printf("Revision data too short\n");

        fclose(fd);
    }

    printf("CPU: %d\n", cpu);
    switch (cpu) {
        case 0: // BCM2835 [Pi 1 A; Pi 1 B; Pi 1 B+; Pi Zero; Pi Zero W]
            return 0x20000000 + GPIO_BASE_OFFSET;
        case 1: // BCM2836 [Pi 2 B]
        case 2: // BCM2837 [Pi 3 B; Pi 3 B+; Pi 3 A+]
            return 0x3f000000 + GPIO_BASE_OFFSET;
        case 3: // BCM2711 [Pi 4 B]
            return 0xfe000000 + GPIO_BASE_OFFSET;
        default:
            printf("Unrecognised revision code\n");
            exit(1);
    }
}

volatile uint32_t *getBase(uint32_t reg_base) {
    int fd;
    if ((fd = open ("/dev/mem", O_RDWR | O_SYNC | O_CLOEXEC) ) < 0) return NULL;
    return (uint32_t *)mmap(0, /*chip->reg_size*/ 0x1000,
                                  PROT_READ|PROT_WRITE, MAP_SHARED,
                                  fd, reg_base);
}

void setPull(volatile uint32_t *base, unsigned int gpio, int pull) {
    int clkreg = GPPUDCLK0 + (gpio / 32);
    int clkbit = 1 << (gpio % 32);

    base[GPPUD] = pull;
    usleep(10);
    base[clkreg] = clkbit;
    usleep(10);
    base[GPPUD] = 0;
    usleep(10);
    base[clkreg] = 0;
    usleep(10);
}

int main(int argc, char *argv[]) {
    if (argc!=2) {
        printf("GPIO pin needed!\n");
        return 1;
    }

    uint32_t reg_base = getGpioRegBase();
    volatile uint32_t *base = getBase(reg_base);
    if (base == NULL || base == (uint32_t *)-1) {
        printf("Base error");
        return 1;
    }
    printf("Base: %p\n", base);
    setPull(base, atoi(argv[1]), PULL_UP);
    return 0;
}

这是启用上拉的 KML 片段(我需要删除该mmap部分):

#include <linux/types.h>    // uint_32
#include <linux/fs.h>       // filp_open/filp_close
#include <linux/delay.h>    // udelay

#define PULL_DOWN    1
#define PULL_UP      2

#define GPIO_BASE_OFFSET 0x00200000
#define GPPUD        37
#define GPPUDCLK0    38

static uint32_t getGpioRegBase(bool *error) {
    uint8_t revision[4] = { 0 };
    uint32_t cpu = 0;
    struct file *fd;
    ssize_t rc = 0;

    if (IS_ERR(( fd = filp_open("/proc/device-tree/system/linux,revision", O_RDONLY | O_SYNC | O_CLOEXEC, 0) ))) {
        *error = true;
        return 0;
    }
    
    if ((rc = kernel_read(fd, revision, sizeof(revision), 0)) == 4) cpu = (revision[2] >> 4) & 0xf;
    else {
        *error = true;
        return 0;
    }

    filp_close(fd, NULL);

    *error = false;
    switch (cpu) {
        case 0: // BCM2835 [Pi 1 A; Pi 1 B; Pi 1 B+; Pi Zero; Pi Zero W]
            return 0x20000000 + GPIO_BASE_OFFSET;
        case 1: // BCM2836 [Pi 2 B]
        case 2: // BCM2837 [Pi 3 B; Pi 3 B+; Pi 3 A+]
            return 0x3f000000 + GPIO_BASE_OFFSET;
        case 3: // BCM2711 [Pi 4 B]
            return 0xfe000000 + GPIO_BASE_OFFSET;
        default:
            *error = true;
            return 0;
    }
}

static volatile uint32_t *getBase(uint32_t reg_base) {
    struct file *fd;
    volatile uint32_t *r;
    
    if (IS_ERR(( fd = filp_open("/dev/mem", O_RDWR | O_SYNC | O_CLOEXEC, 0) ))) return NULL;
    r = (uint32_t*)mmap(0, 0x1000, PROT_READ|PROT_WRITE, MAP_SHARED, fd, reg_base);
    filp_close(fd, NULL); // TODO the original didn't have this
    
    return r;
}

static void setPull(volatile uint32_t *base, uint32_t gpio, int pull) {
    int clkreg = GPPUDCLK0 + (gpio / 32);
    int clkbit = 1 << (gpio % 32);

    base[GPPUD] = pull;
    udelay(10);
    base[clkreg] = clkbit;
    udelay(10);
    base[GPPUD] = 0;
    udelay(10);
    base[clkreg] = 0;
    udelay(10);
}

/**
 * Equivalent to 'raspi-gpio set <gpio> <pu/pd>'
 * @param gpio Valid GPIO pin
 * @param pull PULL_DOWN/PULL_UP
 */
static int setGpioPull(uint32_t gpio, int pull) {
    bool error;
    uint32_t reg_base;
    volatile uint32_t *base;
    
    reg_base = getGpioRegBase(&error);
    if (error) return -1;
    base = getBase(reg_base);
    if (base == NULL || base == (uint32_t*)-1) return -1;
    setPull(base, gpio, pull);
    
    return 0;
}```

答案1

/proc和设备节点/dev用于用户空间;内核不需要它们,内核模块也不应该使用它们。

相反,要访问 GPIO,您应该使用ioremap以及各种ioread...iowrite...函数:ioremap获取与您要查找的物理地址相对应的地址,以及执行 IO 的其他函数。

我不知道如何立即检索您从中获取的设备树信息/proc,但应该有内核函数可以做到这一点。

答案2

也许还有另一种方式:

在我的 RPi 3B+/bulleye 上:

$ ls -l /dev | grep mem
crw-rw---- 1 root gpio    247,   0 Jan 26 00:06 gpiomem
crw-r----- 1 root kmem      1,   1 Jan 26 00:06 mem

群组成员gpio可以访问gpiomem,并且默认(通常是仅有的RPi OS (née raspbian) 中的用户pi是该组的成员。

相关内容