1 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2 /*
3  * Multimedia device API
4  *
5  * Copyright (C) 2010 Nokia Corporation
6  *
7  * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
8  *	     Sakari Ailus <sakari.ailus@iki.fi>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  */
19 
20 #ifndef __LINUX_MEDIA_H
21 #define __LINUX_MEDIA_H
22 
23 #include <stdint.h>
24 #include <sys/ioctl.h>
25 #include <linux/types.h>
26 
27 struct media_device_info {
28 	char driver[16];
29 	char model[32];
30 	char serial[40];
31 	char bus_info[32];
32 	uint32_t media_version;
33 	uint32_t hw_revision;
34 	uint32_t driver_version;
35 	uint32_t reserved[31];
36 };
37 
38 /*
39  * Base number ranges for entity functions
40  *
41  * NOTE: Userspace should not rely on these ranges to identify a group
42  * of function types, as newer functions can be added with any name within
43  * the full u32 range.
44  *
45  * Some older functions use the MEDIA_ENT_F_OLD_*_BASE range. Do not
46  * change this, this is for backwards compatibility. When adding new
47  * functions always use MEDIA_ENT_F_BASE.
48  */
49 #define MEDIA_ENT_F_BASE			0x00000000
50 #define MEDIA_ENT_F_OLD_BASE			0x00010000
51 #define MEDIA_ENT_F_OLD_SUBDEV_BASE		0x00020000
52 
53 /*
54  * Initial value to be used when a new entity is created
55  * Drivers should change it to something useful.
56  */
57 #define MEDIA_ENT_F_UNKNOWN			MEDIA_ENT_F_BASE
58 
59 /*
60  * Subdevs are initialized with MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN in order
61  * to preserve backward compatibility. Drivers must change to the proper
62  * subdev type before registering the entity.
63  */
64 #define MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN		MEDIA_ENT_F_OLD_SUBDEV_BASE
65 
66 /*
67  * DVB entity functions
68  */
69 #define MEDIA_ENT_F_DTV_DEMOD			(MEDIA_ENT_F_BASE + 0x00001)
70 #define MEDIA_ENT_F_TS_DEMUX			(MEDIA_ENT_F_BASE + 0x00002)
71 #define MEDIA_ENT_F_DTV_CA			(MEDIA_ENT_F_BASE + 0x00003)
72 #define MEDIA_ENT_F_DTV_NET_DECAP		(MEDIA_ENT_F_BASE + 0x00004)
73 
74 /*
75  * I/O entity functions
76  */
77 #define MEDIA_ENT_F_IO_V4L			(MEDIA_ENT_F_OLD_BASE + 1)
78 #define MEDIA_ENT_F_IO_DTV			(MEDIA_ENT_F_BASE + 0x01001)
79 #define MEDIA_ENT_F_IO_VBI			(MEDIA_ENT_F_BASE + 0x01002)
80 #define MEDIA_ENT_F_IO_SWRADIO			(MEDIA_ENT_F_BASE + 0x01003)
81 
82 /*
83  * Sensor functions
84  */
85 #define MEDIA_ENT_F_CAM_SENSOR			(MEDIA_ENT_F_OLD_SUBDEV_BASE + 1)
86 #define MEDIA_ENT_F_FLASH			(MEDIA_ENT_F_OLD_SUBDEV_BASE + 2)
87 #define MEDIA_ENT_F_LENS			(MEDIA_ENT_F_OLD_SUBDEV_BASE + 3)
88 
89 /*
90  * Digital TV, analog TV, radio and/or software defined radio tuner functions.
91  *
92  * It is a responsibility of the master/bridge drivers to add connectors
93  * and links for MEDIA_ENT_F_TUNER. Please notice that some old tuners
94  * may require the usage of separate I2C chips to decode analog TV signals,
95  * when the master/bridge chipset doesn't have its own TV standard decoder.
96  * On such cases, the IF-PLL staging is mapped via one or two entities:
97  * MEDIA_ENT_F_IF_VID_DECODER and/or MEDIA_ENT_F_IF_AUD_DECODER.
98  */
99 #define MEDIA_ENT_F_TUNER			(MEDIA_ENT_F_OLD_SUBDEV_BASE + 5)
100 
101 /*
102  * Analog TV IF-PLL decoder functions
103  *
104  * It is a responsibility of the master/bridge drivers to create links
105  * for MEDIA_ENT_F_IF_VID_DECODER and MEDIA_ENT_F_IF_AUD_DECODER.
106  */
107 #define MEDIA_ENT_F_IF_VID_DECODER		(MEDIA_ENT_F_BASE + 0x02001)
108 #define MEDIA_ENT_F_IF_AUD_DECODER		(MEDIA_ENT_F_BASE + 0x02002)
109 
110 /*
111  * Audio entity functions
112  */
113 #define MEDIA_ENT_F_AUDIO_CAPTURE		(MEDIA_ENT_F_BASE + 0x03001)
114 #define MEDIA_ENT_F_AUDIO_PLAYBACK		(MEDIA_ENT_F_BASE + 0x03002)
115 #define MEDIA_ENT_F_AUDIO_MIXER			(MEDIA_ENT_F_BASE + 0x03003)
116 
117 /*
118  * Processing entity functions
119  */
120 #define MEDIA_ENT_F_PROC_VIDEO_COMPOSER		(MEDIA_ENT_F_BASE + 0x4001)
121 #define MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER	(MEDIA_ENT_F_BASE + 0x4002)
122 #define MEDIA_ENT_F_PROC_VIDEO_PIXEL_ENC_CONV	(MEDIA_ENT_F_BASE + 0x4003)
123 #define MEDIA_ENT_F_PROC_VIDEO_LUT		(MEDIA_ENT_F_BASE + 0x4004)
124 #define MEDIA_ENT_F_PROC_VIDEO_SCALER		(MEDIA_ENT_F_BASE + 0x4005)
125 #define MEDIA_ENT_F_PROC_VIDEO_STATISTICS	(MEDIA_ENT_F_BASE + 0x4006)
126 #define MEDIA_ENT_F_PROC_VIDEO_ENCODER		(MEDIA_ENT_F_BASE + 0x4007)
127 #define MEDIA_ENT_F_PROC_VIDEO_DECODER		(MEDIA_ENT_F_BASE + 0x4008)
128 
129 /*
130  * Switch and bridge entity functions
131  */
132 #define MEDIA_ENT_F_VID_MUX			(MEDIA_ENT_F_BASE + 0x5001)
133 #define MEDIA_ENT_F_VID_IF_BRIDGE		(MEDIA_ENT_F_BASE + 0x5002)
134 
135 /*
136  * Video decoder/encoder functions
137  */
138 #define MEDIA_ENT_F_ATV_DECODER			(MEDIA_ENT_F_OLD_SUBDEV_BASE + 4)
139 #define MEDIA_ENT_F_DV_DECODER			(MEDIA_ENT_F_BASE + 0x6001)
140 #define MEDIA_ENT_F_DV_ENCODER			(MEDIA_ENT_F_BASE + 0x6002)
141 
142 /* Entity flags */
143 #define MEDIA_ENT_FL_DEFAULT			(1 << 0)
144 #define MEDIA_ENT_FL_CONNECTOR			(1 << 1)
145 
146 /* OR with the entity id value to find the next entity */
147 #define MEDIA_ENT_ID_FLAG_NEXT			(1U << 31)
148 
149 struct media_entity_desc {
150 	uint32_t id;
151 	char name[32];
152 	uint32_t type;
153 	uint32_t revision;
154 	uint32_t flags;
155 	uint32_t group_id;
156 	uint16_t pads;
157 	uint16_t links;
158 
159 	uint32_t reserved[4];
160 
161 	union {
162 		/* Node specifications */
163 		struct {
164 			uint32_t major;
165 			uint32_t minor;
166 		} dev;
167 
168 		/*
169 		 * TODO: this shouldn't have been added without
170 		 * actual drivers that use this. When the first real driver
171 		 * appears that sets this information, special attention
172 		 * should be given whether this information is 1) enough, and
173 		 * 2) can deal with udev rules that rename devices. The struct
174 		 * dev would not be sufficient for this since that does not
175 		 * contain the subdevice information. In addition, struct dev
176 		 * can only refer to a single device, and not to multiple (e.g.
177 		 * pcm and mixer devices).
178 		 */
179 		struct {
180 			uint32_t card;
181 			uint32_t device;
182 			uint32_t subdevice;
183 		} alsa;
184 
185 		/*
186 		 * DEPRECATED: previous node specifications. Kept just to
187 		 * avoid breaking compilation. Use media_entity_desc.dev
188 		 * instead.
189 		 */
190 		struct {
191 			uint32_t major;
192 			uint32_t minor;
193 		} v4l;
194 		struct {
195 			uint32_t major;
196 			uint32_t minor;
197 		} fb;
198 		int dvb;
199 
200 		/* Sub-device specifications */
201 		/* Nothing needed yet */
202 		uint8_t raw[184];
203 	};
204 };
205 
206 #define MEDIA_PAD_FL_SINK			(1 << 0)
207 #define MEDIA_PAD_FL_SOURCE			(1 << 1)
208 #define MEDIA_PAD_FL_MUST_CONNECT		(1 << 2)
209 
210 struct media_pad_desc {
211 	uint32_t entity;		/* entity ID */
212 	uint16_t index;		/* pad index */
213 	uint32_t flags;		/* pad flags */
214 	uint32_t reserved[2];
215 };
216 
217 #define MEDIA_LNK_FL_ENABLED			(1 << 0)
218 #define MEDIA_LNK_FL_IMMUTABLE			(1 << 1)
219 #define MEDIA_LNK_FL_DYNAMIC			(1 << 2)
220 
221 #define MEDIA_LNK_FL_LINK_TYPE			(0xf << 28)
222 #  define MEDIA_LNK_FL_DATA_LINK		(0 << 28)
223 #  define MEDIA_LNK_FL_INTERFACE_LINK		(1 << 28)
224 
225 struct media_link_desc {
226 	struct media_pad_desc source;
227 	struct media_pad_desc sink;
228 	uint32_t flags;
229 	uint32_t reserved[2];
230 };
231 
232 struct media_links_enum {
233 	uint32_t entity;
234 	/* Should have enough room for pads elements */
235 	struct media_pad_desc *pads;
236 	/* Should have enough room for links elements */
237 	struct media_link_desc *links;
238 	uint32_t reserved[4];
239 };
240 
241 /* Interface type ranges */
242 
243 #define MEDIA_INTF_T_DVB_BASE			0x00000100
244 #define MEDIA_INTF_T_V4L_BASE			0x00000200
245 
246 /* Interface types */
247 
248 #define MEDIA_INTF_T_DVB_FE			(MEDIA_INTF_T_DVB_BASE)
249 #define MEDIA_INTF_T_DVB_DEMUX			(MEDIA_INTF_T_DVB_BASE + 1)
250 #define MEDIA_INTF_T_DVB_DVR			(MEDIA_INTF_T_DVB_BASE + 2)
251 #define MEDIA_INTF_T_DVB_CA			(MEDIA_INTF_T_DVB_BASE + 3)
252 #define MEDIA_INTF_T_DVB_NET			(MEDIA_INTF_T_DVB_BASE + 4)
253 
254 #define MEDIA_INTF_T_V4L_VIDEO			(MEDIA_INTF_T_V4L_BASE)
255 #define MEDIA_INTF_T_V4L_VBI			(MEDIA_INTF_T_V4L_BASE + 1)
256 #define MEDIA_INTF_T_V4L_RADIO			(MEDIA_INTF_T_V4L_BASE + 2)
257 #define MEDIA_INTF_T_V4L_SUBDEV			(MEDIA_INTF_T_V4L_BASE + 3)
258 #define MEDIA_INTF_T_V4L_SWRADIO		(MEDIA_INTF_T_V4L_BASE + 4)
259 #define MEDIA_INTF_T_V4L_TOUCH			(MEDIA_INTF_T_V4L_BASE + 5)
260 
261 #define MEDIA_INTF_T_ALSA_BASE			0x00000300
262 #define MEDIA_INTF_T_ALSA_PCM_CAPTURE		(MEDIA_INTF_T_ALSA_BASE)
263 #define MEDIA_INTF_T_ALSA_PCM_PLAYBACK		(MEDIA_INTF_T_ALSA_BASE + 1)
264 #define MEDIA_INTF_T_ALSA_CONTROL		(MEDIA_INTF_T_ALSA_BASE + 2)
265 
266 
267 /*
268  * MC next gen API definitions
269  */
270 
271 /*
272  * Appeared in 4.19.0.
273  *
274  * The media_version argument comes from the media_version field in
275  * struct media_device_info.
276  */
277 #define MEDIA_V2_ENTITY_HAS_FLAGS(media_version) \
278 	((media_version) >= ((4 << 16) | (19 << 8) | 0))
279 
280 struct media_v2_entity {
281 	uint32_t id;
282 	char name[64];
283 	uint32_t function;		/* Main function of the entity */
284 	uint32_t flags;
285 	uint32_t reserved[5];
286 } __attribute__ ((packed));
287 
288 /* Should match the specific fields at media_intf_devnode */
289 struct media_v2_intf_devnode {
290 	uint32_t major;
291 	uint32_t minor;
292 } __attribute__ ((packed));
293 
294 struct media_v2_interface {
295 	uint32_t id;
296 	uint32_t intf_type;
297 	uint32_t flags;
298 	uint32_t reserved[9];
299 
300 	union {
301 		struct media_v2_intf_devnode devnode;
302 		uint32_t raw[16];
303 	};
304 } __attribute__ ((packed));
305 
306 /*
307  * Appeared in 4.19.0.
308  *
309  * The media_version argument comes from the media_version field in
310  * struct media_device_info.
311  */
312 #define MEDIA_V2_PAD_HAS_INDEX(media_version) \
313 	((media_version) >= ((4 << 16) | (19 << 8) | 0))
314 
315 struct media_v2_pad {
316 	uint32_t id;
317 	uint32_t entity_id;
318 	uint32_t flags;
319 	uint32_t index;
320 	uint32_t reserved[4];
321 } __attribute__ ((packed));
322 
323 struct media_v2_link {
324 	uint32_t id;
325 	uint32_t source_id;
326 	uint32_t sink_id;
327 	uint32_t flags;
328 	uint32_t reserved[6];
329 } __attribute__ ((packed));
330 
331 struct media_v2_topology {
332 	uint64_t topology_version;
333 
334 	uint32_t num_entities;
335 	uint32_t reserved1;
336 	uint64_t ptr_entities;
337 
338 	uint32_t num_interfaces;
339 	uint32_t reserved2;
340 	uint64_t ptr_interfaces;
341 
342 	uint32_t num_pads;
343 	uint32_t reserved3;
344 	uint64_t ptr_pads;
345 
346 	uint32_t num_links;
347 	uint32_t reserved4;
348 	uint64_t ptr_links;
349 } __attribute__ ((packed));
350 
351 /* ioctls */
352 
353 #define MEDIA_IOC_DEVICE_INFO	_IOWR('|', 0x00, struct media_device_info)
354 #define MEDIA_IOC_ENUM_ENTITIES	_IOWR('|', 0x01, struct media_entity_desc)
355 #define MEDIA_IOC_ENUM_LINKS	_IOWR('|', 0x02, struct media_links_enum)
356 #define MEDIA_IOC_SETUP_LINK	_IOWR('|', 0x03, struct media_link_desc)
357 #define MEDIA_IOC_G_TOPOLOGY	_IOWR('|', 0x04, struct media_v2_topology)
358 #define MEDIA_IOC_REQUEST_ALLOC	_IOR ('|', 0x05, int)
359 
360 /*
361  * These ioctls are called on the request file descriptor as returned
362  * by MEDIA_IOC_REQUEST_ALLOC.
363  */
364 #define MEDIA_REQUEST_IOC_QUEUE		_IO('|',  0x80)
365 #define MEDIA_REQUEST_IOC_REINIT	_IO('|',  0x81)
366 
367 
368 /*
369  * Legacy symbols used to avoid userspace compilation breakages.
370  * Do not use any of this in new applications!
371  *
372  * Those symbols map the entity function into types and should be
373  * used only on legacy programs for legacy hardware. Don't rely
374  * on those for MEDIA_IOC_G_TOPOLOGY.
375  */
376 #define MEDIA_ENT_TYPE_SHIFT			16
377 #define MEDIA_ENT_TYPE_MASK			0x00ff0000
378 #define MEDIA_ENT_SUBTYPE_MASK			0x0000ffff
379 
380 #define MEDIA_ENT_T_DEVNODE_UNKNOWN		(MEDIA_ENT_F_OLD_BASE | \
381 						 MEDIA_ENT_SUBTYPE_MASK)
382 
383 #define MEDIA_ENT_T_DEVNODE			MEDIA_ENT_F_OLD_BASE
384 #define MEDIA_ENT_T_DEVNODE_V4L			MEDIA_ENT_F_IO_V4L
385 #define MEDIA_ENT_T_DEVNODE_FB			(MEDIA_ENT_F_OLD_BASE + 2)
386 #define MEDIA_ENT_T_DEVNODE_ALSA		(MEDIA_ENT_F_OLD_BASE + 3)
387 #define MEDIA_ENT_T_DEVNODE_DVB			(MEDIA_ENT_F_OLD_BASE + 4)
388 
389 #define MEDIA_ENT_T_UNKNOWN			MEDIA_ENT_F_UNKNOWN
390 #define MEDIA_ENT_T_V4L2_VIDEO			MEDIA_ENT_F_IO_V4L
391 #define MEDIA_ENT_T_V4L2_SUBDEV			MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN
392 #define MEDIA_ENT_T_V4L2_SUBDEV_SENSOR		MEDIA_ENT_F_CAM_SENSOR
393 #define MEDIA_ENT_T_V4L2_SUBDEV_FLASH		MEDIA_ENT_F_FLASH
394 #define MEDIA_ENT_T_V4L2_SUBDEV_LENS		MEDIA_ENT_F_LENS
395 #define MEDIA_ENT_T_V4L2_SUBDEV_DECODER		MEDIA_ENT_F_ATV_DECODER
396 #define MEDIA_ENT_T_V4L2_SUBDEV_TUNER		MEDIA_ENT_F_TUNER
397 
398 #define MEDIA_ENT_F_DTV_DECODER			MEDIA_ENT_F_DV_DECODER
399 
400 /*
401  * There is still no full ALSA support in the media controller. These
402  * defines should not have been added and we leave them here only
403  * in case some application tries to use these defines.
404  *
405  * The ALSA defines that are in use have been moved into __KERNEL__
406  * scope. As support gets added to these interface types, they should
407  * be moved into __KERNEL__ scope with the code that uses them.
408  */
409 #define MEDIA_INTF_T_ALSA_COMPRESS             (MEDIA_INTF_T_ALSA_BASE + 3)
410 #define MEDIA_INTF_T_ALSA_RAWMIDI              (MEDIA_INTF_T_ALSA_BASE + 4)
411 #define MEDIA_INTF_T_ALSA_HWDEP                (MEDIA_INTF_T_ALSA_BASE + 5)
412 #define MEDIA_INTF_T_ALSA_SEQUENCER            (MEDIA_INTF_T_ALSA_BASE + 6)
413 #define MEDIA_INTF_T_ALSA_TIMER                (MEDIA_INTF_T_ALSA_BASE + 7)
414 
415 /* Obsolete symbol for media_version, no longer used in the kernel */
416 #define MEDIA_API_VERSION			((0 << 16) | (1 << 8) | 0)
417 
418 
419 #endif /* __LINUX_MEDIA_H */
420