1/* SPDX-License-Identifier: GPL-2.0-only */
2/*
3 * include/linux/uio_driver.h
4 *
5 * Copyright(C) 2005, Benedikt Spranger <b.spranger@linutronix.de>
6 * Copyright(C) 2005, Thomas Gleixner <tglx@linutronix.de>
7 * Copyright(C) 2006, Hans J. Koch <hjk@hansjkoch.de>
8 * Copyright(C) 2006, Greg Kroah-Hartman <greg@kroah.com>
9 *
10 * Userspace IO driver.
11 */
12
13#ifndef _UIO_DRIVER_H_
14#define _UIO_DRIVER_H_
15
16#include <linux/device.h>
17#include <linux/fs.h>
18#include <linux/interrupt.h>
19
20struct module;
21struct uio_map;
22
23/**
24 * struct uio_mem - description of a UIO memory region
25 * @name:		name of the memory region for identification
26 * @addr:               address of the device's memory rounded to page
27 *			size (phys_addr is used since addr can be
28 *			logical, virtual, or physical & phys_addr_t
29 *			should always be large enough to handle any of
30 *			the address types)
31 * @dma_addr:		DMA handle set by dma_alloc_coherent, used with
32 *			UIO_MEM_DMA_COHERENT only (@addr should be the
33 *			void * returned from the same dma_alloc_coherent call)
34 * @offs:               offset of device memory within the page
35 * @size:		size of IO (multiple of page size)
36 * @memtype:		type of memory addr points to
37 * @internal_addr:	ioremap-ped version of addr, for driver internal use
38 * @dma_device:		device struct that was passed to dma_alloc_coherent,
39 *			used with UIO_MEM_DMA_COHERENT only
40 * @map:		for use by the UIO core only.
41 */
42struct uio_mem {
43	const char		*name;
44	phys_addr_t		addr;
45	dma_addr_t		dma_addr;
46	unsigned long		offs;
47	resource_size_t		size;
48	int			memtype;
49	void __iomem		*internal_addr;
50	struct device		*dma_device;
51	struct uio_map		*map;
52};
53
54#define MAX_UIO_MAPS	5
55
56struct uio_portio;
57
58/**
59 * struct uio_port - description of a UIO port region
60 * @name:		name of the port region for identification
61 * @start:		start of port region
62 * @size:		size of port region
63 * @porttype:		type of port (see UIO_PORT_* below)
64 * @portio:		for use by the UIO core only.
65 */
66struct uio_port {
67	const char		*name;
68	unsigned long		start;
69	unsigned long		size;
70	int			porttype;
71	struct uio_portio	*portio;
72};
73
74#define MAX_UIO_PORT_REGIONS	5
75
76struct uio_device {
77	struct module           *owner;
78	struct device		dev;
79	int                     minor;
80	atomic_t                event;
81	struct fasync_struct    *async_queue;
82	wait_queue_head_t       wait;
83	struct uio_info         *info;
84	struct mutex		info_lock;
85	struct kobject          *map_dir;
86	struct kobject          *portio_dir;
87};
88
89/**
90 * struct uio_info - UIO device capabilities
91 * @uio_dev:		the UIO device this info belongs to
92 * @name:		device name
93 * @version:		device driver version
94 * @mem:		list of mappable memory regions, size==0 for end of list
95 * @port:		list of port regions, size==0 for end of list
96 * @irq:		interrupt number or UIO_IRQ_CUSTOM
97 * @irq_flags:		flags for request_irq()
98 * @priv:		optional private data
99 * @handler:		the device's irq handler
100 * @mmap:		mmap operation for this uio device
101 * @open:		open operation for this uio device
102 * @release:		release operation for this uio device
103 * @irqcontrol:		disable/enable irqs when 0/1 is written to /dev/uioX
104 */
105struct uio_info {
106	struct uio_device	*uio_dev;
107	const char		*name;
108	const char		*version;
109	struct uio_mem		mem[MAX_UIO_MAPS];
110	struct uio_port		port[MAX_UIO_PORT_REGIONS];
111	long			irq;
112	unsigned long		irq_flags;
113	void			*priv;
114	irqreturn_t (*handler)(int irq, struct uio_info *dev_info);
115	int (*mmap)(struct uio_info *info, struct vm_area_struct *vma);
116	int (*open)(struct uio_info *info, struct inode *inode);
117	int (*release)(struct uio_info *info, struct inode *inode);
118	int (*irqcontrol)(struct uio_info *info, s32 irq_on);
119};
120
121extern int __must_check
122	__uio_register_device(struct module *owner,
123			      struct device *parent,
124			      struct uio_info *info);
125
126/* use a define to avoid include chaining to get THIS_MODULE */
127
128/**
129 * uio_register_device - register a new userspace IO device
130 * @parent:	parent device
131 * @info:	UIO device capabilities
132 *
133 * returns zero on success or a negative error code.
134 */
135#define uio_register_device(parent, info) \
136	__uio_register_device(THIS_MODULE, parent, info)
137
138extern void uio_unregister_device(struct uio_info *info);
139extern void uio_event_notify(struct uio_info *info);
140
141extern int __must_check
142	__devm_uio_register_device(struct module *owner,
143				   struct device *parent,
144				   struct uio_info *info);
145
146/* use a define to avoid include chaining to get THIS_MODULE */
147
148/**
149 * devm_uio_register_device - Resource managed uio_register_device()
150 * @parent:	parent device
151 * @info:	UIO device capabilities
152 *
153 * returns zero on success or a negative error code.
154 */
155#define devm_uio_register_device(parent, info) \
156	__devm_uio_register_device(THIS_MODULE, parent, info)
157
158/* defines for uio_info->irq */
159#define UIO_IRQ_CUSTOM	-1
160#define UIO_IRQ_NONE	0
161
162/* defines for uio_mem->memtype */
163#define UIO_MEM_NONE	0
164#define UIO_MEM_PHYS	1
165#define UIO_MEM_LOGICAL	2
166#define UIO_MEM_VIRTUAL 3
167#define UIO_MEM_IOVA	4
168/*
169 * UIO_MEM_DMA_COHERENT exists for legacy drivers that had been getting by with
170 * improperly mapping DMA coherent allocations through the other modes.
171 * Do not use in new drivers.
172 */
173#define UIO_MEM_DMA_COHERENT	5
174
175/* defines for uio_port->porttype */
176#define UIO_PORT_NONE	0
177#define UIO_PORT_X86	1
178#define UIO_PORT_GPIO	2
179#define UIO_PORT_OTHER	3
180
181#endif /* _LINUX_UIO_DRIVER_H_ */
182