⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dev_mem.c

📁 基于Linux的ffmepg decoder
💻 C
字号:
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>

#include "fmp4.h"

static int mem_fd = -1;
static int mp4_fd = -1;

// --------------------------------------------------------------------
//	return
//		-1 ==> failure
//		other ==> mapped virtual address
// --------------------------------------------------------------------
void *ioremap(unsigned long physaddr, unsigned size)
{
    unsigned long page_addr, ofs_addr, reg, pgmask;
    unsigned long reg_mem = 0;

    /*
     * looks like mmap wants aligned addresses?
     */
    pgmask = getpagesize()-1;
    page_addr = physaddr & ~pgmask;
    ofs_addr  = physaddr & pgmask;

    /*
     * Don't forget O_SYNC, esp. if address is in RAM region.
     * Note: if you do know you'll access in Read Only mode,
     *    pass O_RDONLY to open, and PROT_READ only to mmap
     */
    if (mem_fd < 0) {
        mem_fd = open("/dev/mem", O_RDWR|O_SYNC);
        if (mem_fd < 0) {
			perror("ioremap: can't open /dev/mem");
            return (void *)-1;
        }
    }

    /* memory map */
    reg_mem = (unsigned long)mmap(
        (caddr_t)reg_mem,
        size+ofs_addr,
        PROT_READ|PROT_WRITE,
        MAP_SHARED,
        mem_fd,
        page_addr
    );
    if (reg_mem == (unsigned long)MAP_FAILED) {
        perror("ioremap: mmap error");
		return (void *)-1;
    }

    reg = reg_mem + ofs_addr;
    return (void *)reg;
}


int iounmap(void *start, size_t length)
{
    unsigned long ofs_addr;
    ofs_addr = (unsigned long)start & (getpagesize()-1);

    /* do some cleanup when you're done with it */
    return munmap((void*)start-ofs_addr, length+ofs_addr);
}



// --------------------------------------------------------------------
//	return
//		0 ==> success
//		-1 ==> failure
// --------------------------------------------------------------------
int dma_malloc(int size, unsigned int *phys, unsigned int *virt)
{
//	unsigned int ret_virt;

    if (mp4_fd == -1) {
        mp4_fd = open("/dev/mp4", O_RDWR);
        if (mp4_fd < 0) {
			perror("dma_malloc: can't open /dev/mp4");
            return -1;
        }
    }
printf("phys = 0x%x\n", (int)phys);
{
 int tsize;
for (tsize = 0x100000; tsize != 0; tsize --);
}
	*phys = ioctl(mp4_fd, MP4_IOCTL_MALLOC, size);
//	*virt = ret_virt = ioremap(*phys, size);
	*virt = (unsigned int *)ioremap(*phys, size);
	return 0;
}


void dma_free(int size, unsigned int virt)
{
 	struct virt_size tvs;

	tvs.virt = virt;
	tvs.size = size;
	iounmap(virt, size);
	ioctl(mp4_fd, MP4_IOCTL_FREE, &tvs);
}


///#define TEST_MMAP

#if defined(TEST_MMAP)

#define CPE_MPEG4_BASE		0x90e00000
#define CPE_MPEG4_SIZE		0x100000

int main()
{
	unsigned char *base_address;
	int i;
	unsigned long phys;
	unsigned int *virt;

	base_address = ioremap(CPE_MPEG4_BASE, CPE_MPEG4_SIZE);
	if (base_address == (void *)-1)
	{
		printf("map failure\n");
	}
	for (i=0; i<0x100; ++i)
	{
		printf("%02x ", base_address[i]);
		if (i%16==15)
		{
			printf("\n");
		}
		base_address[i] += 1;
	}
	iounmap(base_address, CPE_MPEG4_SIZE);
	printf("end 1\n");

	dma_malloc(1024, &phys, (void **)&virt);
	printf("phys: 0x%x\n", (unsigned int)phys);
	printf("virt: 0x%x\n", (unsigned int)virt);
	for (i=0; i<16; ++i)
	{
		printf("%02x ", virt[i]);
		if (i%16==15)
		{
			printf("\n");
		}
		virt[i] = i;
	}
	dma_free(1024, phys, virt);
	

	return 0;
}

#endif // TEST_MMAP

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -