2012-11-24 20 views
3

我想在我的char驱动程序的DMAble存储器上实现mmap方法。 起初我想用DMA API来实现它,然后用于PCI设备。使用device_create为dma_alloc_coherent创建struct device

dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle, gfp_t gfp) 

dma_alloc_coherent的第一个参数是指向结构设备的指针。 对于ISA或EISA它可以是NULL,但我需要支持DMA的有效设备。 以下代码应该完成工作,但分配失败。

/* register device in sysfs */ 
mmrmp->udev = device_create(mmrmp->class, NULL, mmrmp->major, NULL, DEVICE_NAME); 
if (mmrmp->udev == NULL) 
{ 
    goto devcreate_failed; 
} 
printk(KERN_INFO "%s: Char driver initialized and added to the system at addr %p", __func__, mmrmp->udev); 
if(dma_supported(mmrmp->udev,DMA_BIT_MASK(32))) 
    printk(KERN_INFO "device supports DMA"); 
else 
    printk(KERN_INFO "device does not support DMA"); 

if(is_device_dma_capable(mmrmp->udev)) 
    printk(KERN_INFO "is_device_dma_capable: device supports DMA"); 
else 
    printk(KERN_INFO "is_device_dma_capable: device does not support DMA"); 

if (!dma_set_mask(mmrmp->udev, dma_get_required_mask(mmrmp->udev))) 
    printk(KERN_INFO "set DMA mask failed"); 

if ((mmrmp->dma_handle & dma_get_required_mask(mmrmp->udev)) == mmrmp->dma_handle) 
    printk(KERN_INFO "Allocated physical address is within DMA region"); 
mmrmp->data = dma_alloc_coherent(mmrmp->udev, PAGE_SIZE, &mmrmp->dma_handle, GFP_KERNEL); 

为什么dma_supported返回1并且is_dma_capable_returns 0? 我应该如何创建我的设备来支持dma_alloc_coherent?

回答

0

确保mmrmp->udev->dma_mask pointer不为NULL。初始化如下:

static const u64 dmamask = DMA_BIT_MASK(32); 
mmrmp->udev->dma_mask = (u64 *)&dmamask; 
mmrmp->udev->coherent_dma_mask = DMA_BIT_MASK(32); 
相关问题