Linux驱动开发之uio驱动

作者

QQ群:852283276
微信:arm80x86
微信公众号:青儿创客基地
B站:主页 https://space.bilibili.com/208826118

参考

当ZYNQ遇到Linux Userspace I/O(UIO)
The Userspace I/O HOWTO
Linux 设备驱动之 UIO 机制(测试 UIO 机制)
UIO 示例
Linux UIO

ubuntu 16.04.4 unknown symbol __uio_register_device

开发了一个pcie驱动,加载时,dmesg发现unknown symbol __uio_register_device,定位原因是insmod没有自动加载uio.ko,所以先执行modprobe uio

驱动开发

struct uio_info内容如下,

/**
 * struct uio_info - UIO device capabilities
 * @uio_dev:		the UIO device this info belongs to
 * @name:		device name
 * @version:		device driver version
 * @mem:		list of mappable memory regions, size==0 for end of list
 * @port:		list of port regions, size==0 for end of list
 * @irq:		interrupt number or UIO_IRQ_CUSTOM
 * @irq_flags:		flags for request_irq()
 * @priv:		optional private data
 * @handler:		the device's irq handler
 * @mmap:		mmap operation for this uio device
 * @open:		open operation for this uio device
 * @release:		release operation for this uio device
 * @irqcontrol:		disable/enable irqs when 0/1 is written to /dev/uioX
 */
struct uio_info {
	struct uio_device	*uio_dev;
	const char		*name;
	const char		*version;
	struct uio_mem		mem[MAX_UIO_MAPS];
	struct uio_port		port[MAX_UIO_PORT_REGIONS];
	long			irq;
	unsigned long		irq_flags;
	void			*priv;
	irqreturn_t (*handler)(int irq, struct uio_info *dev_info);
	int (*mmap)(struct uio_info *info, struct vm_area_struct *vma);
	int (*open)(struct uio_info *info, struct inode *inode);
	int (*release)(struct uio_info *info, struct inode *inode);
	int (*irqcontrol)(struct uio_info *info, s32 irq_on);
};

struct uio_mem内容如下,

/**
 * struct uio_mem - description of a UIO memory region
 * @name:		name of the memory region for identification
 * @addr:		address of the device's memory (phys_addr is used since
 * 			addr can be logical, virtual, or physical & phys_addr_t
 * 			should always be large enough to handle any of the
 * 			address types)
 * @size:		size of IO
 * @memtype:		type of memory addr points to
 * @internal_addr:	ioremap-ped version of addr, for driver internal use
 * @map:		for use by the UIO core only.
 */
struct uio_mem {
	const char		*name;
	phys_addr_t		addr;
	resource_size_t		size;
	int			memtype;
	void __iomem		*internal_addr;
	struct uio_map		*map;
};

首先构造struct uio_info结构体,uio_irqhandler为中断处理函数,

uioinfo = &pdev_context->info;
uioinfo->name = "pcie_ep";
uioinfo->version = DRIVER_VERSION;
uioinfo->irq = pdev->irq;
uioinfo->irq_flags = IRQF_SHARED;
uioinfo->handler = uio_irqhandler;

struct uio_info填充struct uio_mem,下面将pcie的bar空间依次填入uio结构体,

uiomem = &uioinfo->mem[0];
for (i = 0; i < PCIE_EP_BAR_NUM; i++) {
	if (pci_resource_len(pdev, i) == 0) {
		continue;
	}
	if (uiomem >= &uioinfo->mem[MAX_UIO_MAPS]) {
		dev_warn(&pdev->dev, "device has more than "
				__stringify(MAX_UIO_MAPS)
				" I/O memory resources.\n");
		break;
	}
	uiomem->memtype = UIO_MEM_PHYS;
	uiomem->addr = pci_resource_start(pdev, i);
	uiomem->size = pci_resource_len(pdev, i);
	uiomem->name = "pcie bar";
	++uiomem;
}

注册uio设备,

ret = uio_register_device(&pdev->dev, &pdev_context->info);
if (ret)
	goto error;

加载UIO驱动

修改设备树,将compatible节点改成“generic-uio”,有两种方法加载驱动,

  1. bootargs use“uio_pdrv_genirq.of_id=generic-uio”,可以通过DTS定义。
  2. insmod uio_pdrv_genirq.ko of_id=generic-uio
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Prentice.Essential Linux Device Drivers.2008 一个老外写的,写的非常详细 Chapter 1. Introduction Evolution The GNU Copyleft Kernel.org Mailing Lists and Forums Linux Distributions Looking at the Sources Building the Kernel Loadable Modules Before Starting Chapter 2. A Peek Inside the Kernel Booting Up Kernel Mode and User Mode Process Context and Interrupt Context Kernel Timers Concurrency in the Kernel Process Filesystem Allocating Memory Looking at the Sources Chapter 3. Kernel Facilities Kernel Threads Helper Interfaces Looking at the Sources Chapter 4. Laying the Groundwork Introducing Devices and Drivers Interrupt Handling The Linux Device Model Memory Barriers Power Management Looking at the Sources Chapter 5. Character Drivers Char Driver Basics Device Example: System CMOS Sensing Data Availability Talking to the Parallel Port RTC Subsystem Pseudo Char Drivers Misc Drivers Character Caveats Looking at the Sources Chapter 6. Serial Drivers Layered Architecture UART Drivers TTY Drivers Line Disciplines Looking at the Sources Chapter 7. Input Drivers Input Event Drivers Input Device Drivers Debugging Looking at the Sources Chapter 8. The Inter-Integrated Circuit Protocol What's I2C/SMBus? I2C Core Bus Transactions Device Example: EEPROM Device Example: Real Time Clock I2C-dev Hardware Monitoring Using LM-Sensors The Serial Peripheral Interface Bus The 1-Wire Bus Debugging Looking at the Sources Chapter 9. PCMCIA and Compact Flash What's PCMCIA/CF? Linux-PCMCIA Subsystem Host Controller Drivers PCMCIA Core Driver Services Client Drivers Tying the Pieces Together PCMCIA Storage Serial PCMCIA Debugging Looking at the Sources Chapter 10. Peripheral Component Interconnect The PCI Family Addressing and Identification Accessing PCI Regions Direct Memory Access Device Example: Ethernet-Modem Card Debugging Looking at the Sources Chapter 11. Universal Serial Bus USB Architecture Linux-USB Subsystem Driver Data Structures Enumeration Device Example: Telemetry Card Class Drivers Gadget Drivers Debugging Looking at the Sources Chapter 12. Video Drivers Display Architecture Linux-Video Subsystem Display Parameters The Frame Buffer API Frame Buffer Drivers Console Drivers Debugging Looking at the Sources Chapter 13. Audio Drivers Audio Architecture Linux-Sound Subsystem Device Example: MP3 Player Debugging Looking at the Sources Chapter 14. Block Drivers Storage Technologies Linux Block I/O Layer I/O Schedulers Block Driver Data Structures and Methods Device Example: Simple Storage Controller Advanced Topics Debugging Looking at the Sources Chapter 15. Network Interface Cards Driver Data Structures Talking with Protocol Layers Buffer Management and Concurrency Control Device Example: Ethernet NIC ISA Network Drivers Asynchronous Transfer Mode Network Throughput Looking at the Sources Chapter 16. Linux Without Wires Bluetooth Infrared WiFi Cellular Networking Current Trends Chapter 17. Memory Technology Devices What's Flash Memory? Linux-MTD Subsystem Map Drivers NOR Chip Drivers NAND Chip Drivers User Modules MTD-Utils Configuring MTD eXecute In Place The Firmware Hub Debugging Looking at the Sources Chapter 18. Embedding Linux Challenges Component Selection Tool Chains Embedded Bootloaders Memory Layout Kernel Porting Embedded Drivers The Root Filesystem Test Infrastructure Debugging Chapter 19. Drivers in User Space Process Scheduling and Response Times Accessing I/O Regions Accessing Memory Regions User Mode SCSI User Mode USB User Mode I2C UIO Looking at the Sources Chapter 20. More Devices and Drivers ECC Reporting Frequency Scaling Embedded Controllers ACPI ISA and MCA FireWire Intelligent Input/Output Amateur Radio Voice over IP High-Speed Interconnects Chapter 21. Debugging Device Drivers Kernel Debuggers Kernel Probes Kexec and Kdump Profiling Tracing Linux Test Project User Mode Linux Diagnostic Tools Kernel Hacking Config Options Test Equipment Chapter 22. Maintenance and Delivery Coding Style Change Markers Version Control Consistent Checksums Build Scripts Portable Code Chapter 23. Shutting Down Checklist What Next? Appendix A. Linux Assembly Debugging Appendix B. Linux and the BIOS Real Mode Calls Protected Mode Calls BIOS and Legacy Drivers Appendix C. Seq Files The Seq File Advantage Updating the NVRAM Driver Looking at the Sources

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值