]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - include/media/v4l2-dev.h
Merge tag 'linux-watchdog-5.4-rc1' of git://www.linux-watchdog.org/linux-watchdog
[mirror_ubuntu-focal-kernel.git] / include / media / v4l2-dev.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
401998fa
MCC
2/*
3 *
4 * V 4 L 2 D R I V E R H E L P E R A P I
5 *
6 * Moved from videodev2.h
7 *
8 * Some commonly needed functions for drivers (v4l2-common.o module)
9 */
10#ifndef _V4L2_DEV_H
11#define _V4L2_DEV_H
12
401998fa
MCC
13#include <linux/poll.h>
14#include <linux/fs.h>
15#include <linux/device.h>
7f8ecfab 16#include <linux/cdev.h>
401998fa 17#include <linux/mutex.h>
401998fa 18#include <linux/videodev2.h>
401998fa 19
2c0ab67b
LP
20#include <media/media-entity.h>
21
401998fa 22#define VIDEO_MAJOR 81
bfa8a273 23
4839c58f
MCC
24/**
25 * enum vfl_devnode_type - type of V4L2 device node
26 *
27 * @VFL_TYPE_GRABBER: for video input/output devices
28 * @VFL_TYPE_VBI: for vertical blank data (i.e. closed captions, teletext)
29 * @VFL_TYPE_RADIO: for radio tuners
30 * @VFL_TYPE_SUBDEV: for V4L2 subdevices
31 * @VFL_TYPE_SDR: for Software Defined Radio tuners
32 * @VFL_TYPE_TOUCH: for touch sensors
5bc3744c 33 * @VFL_TYPE_MAX: number of VFL types, must always be last in the enum
4839c58f
MCC
34 */
35enum vfl_devnode_type {
36 VFL_TYPE_GRABBER = 0,
a95845ba
MCC
37 VFL_TYPE_VBI,
38 VFL_TYPE_RADIO,
39 VFL_TYPE_SUBDEV,
40 VFL_TYPE_SDR,
41 VFL_TYPE_TOUCH,
42 VFL_TYPE_MAX /* Shall be the last one */
4839c58f 43};
401998fa 44
468fde0b
MCC
45/**
46 * enum vfl_direction - Identifies if a &struct video_device corresponds
47 * to a receiver, a transmitter or a mem-to-mem device.
48 *
49 * @VFL_DIR_RX: device is a receiver.
50 * @VFL_DIR_TX: device is a transmitter.
51 * @VFL_DIR_M2M: device is a memory to memory device.
52 *
53 * Note: Ignored if &enum vfl_devnode_type is %VFL_TYPE_SUBDEV.
54 */
55enum vfl_devnode_direction {
56 VFL_DIR_RX,
57 VFL_DIR_TX,
58 VFL_DIR_M2M,
59};
5c77879f 60
a399810c 61struct v4l2_ioctl_callbacks;
bec43661 62struct video_device;
9bea3514 63struct v4l2_device;
0996517c 64struct v4l2_ctrl_handler;
a399810c 65
63b31ffd
MCC
66/**
67 * enum v4l2_video_device_flags - Flags used by &struct video_device
68 *
69 * @V4L2_FL_REGISTERED:
4a3fad70 70 * indicates that a &struct video_device is registered.
63b31ffd
MCC
71 * Drivers can clear this flag if they want to block all future
72 * device access. It is cleared by video_unregister_device.
73 * @V4L2_FL_USES_V4L2_FH:
74 * indicates that file->private_data points to &struct v4l2_fh.
75 * This flag is set by the core when v4l2_fh_init() is called.
76 * All new drivers should use it.
8cbd94bd
HV
77 * @V4L2_FL_QUIRK_INVERTED_CROP:
78 * some old M2M drivers use g/s_crop/cropcap incorrectly: crop and
79 * compose are swapped. If this flag is set, then the selection
80 * targets are swapped in the g/s_crop/cropcap functions in v4l2-ioctl.c.
81 * This allows those drivers to correctly implement the selection API,
82 * but the old crop API will still work as expected in order to preserve
83 * backwards compatibility.
84 * Never set this flag for new drivers.
63b31ffd
MCC
85 */
86enum v4l2_video_device_flags {
8cbd94bd
HV
87 V4L2_FL_REGISTERED = 0,
88 V4L2_FL_USES_V4L2_FH = 1,
89 V4L2_FL_QUIRK_INVERTED_CROP = 2,
63b31ffd 90};
dc93a70c 91
02265493
HV
92/* Priority helper functions */
93
d9d3d176
MCC
94/**
95 * struct v4l2_prio_state - stores the priority states
96 *
97 * @prios: array with elements to store the array priorities
98 *
99 *
100 * .. note::
101 * The size of @prios array matches the number of priority types defined
ffa0441e 102 * by enum &v4l2_priority.
d9d3d176 103 */
02265493
HV
104struct v4l2_prio_state {
105 atomic_t prios[4];
106};
107
d9d3d176
MCC
108/**
109 * v4l2_prio_init - initializes a struct v4l2_prio_state
110 *
111 * @global: pointer to &struct v4l2_prio_state
112 */
02265493 113void v4l2_prio_init(struct v4l2_prio_state *global);
d9d3d176
MCC
114
115/**
116 * v4l2_prio_change - changes the v4l2 file handler priority
117 *
118 * @global: pointer to the &struct v4l2_prio_state of the device node.
ffa0441e
MCC
119 * @local: pointer to the desired priority, as defined by enum &v4l2_priority
120 * @new: Priority type requested, as defined by enum &v4l2_priority.
d9d3d176
MCC
121 *
122 * .. note::
123 * This function should be used only by the V4L2 core.
124 */
02265493
HV
125int v4l2_prio_change(struct v4l2_prio_state *global, enum v4l2_priority *local,
126 enum v4l2_priority new);
d9d3d176
MCC
127
128/**
129 * v4l2_prio_open - Implements the priority logic for a file handler open
130 *
131 * @global: pointer to the &struct v4l2_prio_state of the device node.
ffa0441e 132 * @local: pointer to the desired priority, as defined by enum &v4l2_priority
d9d3d176
MCC
133 *
134 * .. note::
135 * This function should be used only by the V4L2 core.
136 */
02265493 137void v4l2_prio_open(struct v4l2_prio_state *global, enum v4l2_priority *local);
d9d3d176
MCC
138
139/**
140 * v4l2_prio_close - Implements the priority logic for a file handler close
141 *
142 * @global: pointer to the &struct v4l2_prio_state of the device node.
ffa0441e 143 * @local: priority to be released, as defined by enum &v4l2_priority
d9d3d176
MCC
144 *
145 * .. note::
146 * This function should be used only by the V4L2 core.
147 */
02265493 148void v4l2_prio_close(struct v4l2_prio_state *global, enum v4l2_priority local);
d9d3d176
MCC
149
150/**
151 * v4l2_prio_max - Return the maximum priority, as stored at the @global array.
152 *
153 * @global: pointer to the &struct v4l2_prio_state of the device node.
154 *
155 * .. note::
156 * This function should be used only by the V4L2 core.
157 */
02265493 158enum v4l2_priority v4l2_prio_max(struct v4l2_prio_state *global);
02265493 159
d9d3d176 160/**
e383ce07 161 * v4l2_prio_check - Implements the priority logic for a file handler close
d9d3d176
MCC
162 *
163 * @global: pointer to the &struct v4l2_prio_state of the device node.
ffa0441e 164 * @local: desired priority, as defined by enum &v4l2_priority local
d9d3d176
MCC
165 *
166 * .. note::
167 * This function should be used only by the V4L2 core.
168 */
169int v4l2_prio_check(struct v4l2_prio_state *global, enum v4l2_priority local);
02265493 170
d9d3d176
MCC
171/**
172 * struct v4l2_file_operations - fs operations used by a V4L2 device
173 *
174 * @owner: pointer to struct module
175 * @read: operations needed to implement the read() syscall
176 * @write: operations needed to implement the write() syscall
177 * @poll: operations needed to implement the poll() syscall
178 * @unlocked_ioctl: operations needed to implement the ioctl() syscall
179 * @compat_ioctl32: operations needed to implement the ioctl() syscall for
180 * the special case where the Kernel uses 64 bits instructions, but
181 * the userspace uses 32 bits.
182 * @get_unmapped_area: called by the mmap() syscall, used when %!CONFIG_MMU
183 * @mmap: operations needed to implement the mmap() syscall
184 * @open: operations needed to implement the open() syscall
185 * @release: operations needed to implement the release() syscall
186 *
187 * .. note::
188 *
189 * Those operations are used to implemente the fs struct file_operations
190 * at the V4L2 drivers. The V4L2 core overrides the fs ops with some
191 * extra logic needed by the subsystem.
192 */
bec43661
HV
193struct v4l2_file_operations {
194 struct module *owner;
195 ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
196 ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
a3f8683b 197 __poll_t (*poll) (struct file *, struct poll_table_struct *);
bec43661 198 long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
b9d0aa6e
LP
199#ifdef CONFIG_COMPAT
200 long (*compat_ioctl32) (struct file *, unsigned int, unsigned long);
201#endif
ecc6517d
BL
202 unsigned long (*get_unmapped_area) (struct file *, unsigned long,
203 unsigned long, unsigned long, unsigned long);
bec43661
HV
204 int (*mmap) (struct file *, struct vm_area_struct *);
205 int (*open) (struct file *);
206 int (*release) (struct file *);
207};
208
401998fa
MCC
209/*
210 * Newer version of video_device, handled by videodev2.c
4a3fad70 211 * This version moves redundant code from video device code to
401998fa
MCC
212 * the common handler
213 */
401998fa 214
d9d3d176
MCC
215/**
216 * struct video_device - Structure used to create and manage the V4L2 device
217 * nodes.
218 *
219 * @entity: &struct media_entity
220 * @intf_devnode: pointer to &struct media_intf_devnode
221 * @pipe: &struct media_pipeline
222 * @fops: pointer to &struct v4l2_file_operations for the video device
223 * @device_caps: device capabilities as used in v4l2_capabilities
224 * @dev: &struct device for the video device
225 * @cdev: character device
226 * @v4l2_dev: pointer to &struct v4l2_device parent
227 * @dev_parent: pointer to &struct device parent
228 * @ctrl_handler: Control handler associated with this device node.
229 * May be NULL.
230 * @queue: &struct vb2_queue associated with this device node. May be NULL.
231 * @prio: pointer to &struct v4l2_prio_state with device's Priority state.
232 * If NULL, then v4l2_dev->prio will be used.
233 * @name: video device name
4839c58f 234 * @vfl_type: V4L device type, as defined by &enum vfl_devnode_type
d9d3d176
MCC
235 * @vfl_dir: V4L receiver, transmitter or m2m
236 * @minor: device node 'minor'. It is set to -1 if the registration failed
237 * @num: number of the video device node
63b31ffd
MCC
238 * @flags: video device flags. Use bitops to set/clear/test flags.
239 * Contains a set of &enum v4l2_video_device_flags.
d9d3d176
MCC
240 * @index: attribute to differentiate multiple indices on one physical device
241 * @fh_lock: Lock for all v4l2_fhs
242 * @fh_list: List of &struct v4l2_fh
243 * @dev_debug: Internal device debug flags, not for use by drivers
244 * @tvnorms: Supported tv norms
245 *
246 * @release: video device release() callback
247 * @ioctl_ops: pointer to &struct v4l2_ioctl_ops with ioctl callbacks
248 *
249 * @valid_ioctls: bitmap with the valid ioctls for this device
d9d3d176
MCC
250 * @lock: pointer to &struct mutex serialization lock
251 *
252 * .. note::
253 * Only set @dev_parent if that can't be deduced from @v4l2_dev.
254 */
255
401998fa
MCC
256struct video_device
257{
2c0ab67b
LP
258#if defined(CONFIG_MEDIA_CONTROLLER)
259 struct media_entity entity;
d9c21e3e 260 struct media_intf_devnode *intf_devnode;
d0a164f5 261 struct media_pipeline pipe;
2c0ab67b 262#endif
bec43661 263 const struct v4l2_file_operations *fops;
401998fa 264
7bbe7813
HV
265 u32 device_caps;
266
54bd5b66 267 /* sysfs */
d9d3d176
MCC
268 struct device dev;
269 struct cdev *cdev;
9bea3514 270
d9d3d176
MCC
271 struct v4l2_device *v4l2_dev;
272 struct device *dev_parent;
54bd5b66 273
0996517c
HV
274 struct v4l2_ctrl_handler *ctrl_handler;
275
5a5adf6b
HV
276 struct vb2_queue *queue;
277
0f62fd6a
HV
278 struct v4l2_prio_state *prio;
279
401998fa 280 /* device info */
401998fa 281 char name[32];
4839c58f 282 enum vfl_devnode_type vfl_type;
468fde0b 283 enum vfl_devnode_direction vfl_dir;
401998fa 284 int minor;
dd89601d 285 u16 num;
dc93a70c 286 unsigned long flags;
539a7555 287 int index;
401998fa 288
1babcb46 289 /* V4L2 file handles */
d9d3d176
MCC
290 spinlock_t fh_lock;
291 struct list_head fh_list;
1babcb46 292
17028cdb 293 int dev_debug;
401998fa 294
d9d3d176 295 v4l2_std_id tvnorms;
401998fa
MCC
296
297 /* callbacks */
dc93a70c 298 void (*release)(struct video_device *vdev);
a399810c 299 const struct v4l2_ioctl_ops *ioctl_ops;
48ea0be0 300 DECLARE_BITMAP(valid_ioctls, BASE_VIDIOC_PRIVATE);
ee6869af 301
ee6869af 302 struct mutex *lock;
401998fa
MCC
303};
304
69b925c5
MCC
305/**
306 * media_entity_to_video_device - Returns a &struct video_device from
307 * the &struct media_entity embedded on it.
308 *
7b73ce9d 309 * @__entity: pointer to &struct media_entity
69b925c5 310 */
7b73ce9d
NS
311#define media_entity_to_video_device(__entity) \
312 container_of(__entity, struct video_device, entity)
69b925c5
MCC
313
314/**
315 * to_video_device - Returns a &struct video_device from the
316 * &struct device embedded on it.
317 *
318 * @cd: pointer to &struct device
319 */
22a04f10 320#define to_video_device(cd) container_of(cd, struct video_device, dev)
e90ff923 321
d9d3d176
MCC
322/**
323 * __video_register_device - register video4linux devices
324 *
325 * @vdev: struct video_device to register
4839c58f 326 * @type: type of device to register, as defined by &enum vfl_devnode_type
d9d3d176 327 * @nr: which device node number is desired:
4a3fad70 328 * (0 == /dev/video0, 1 == /dev/video1, ..., -1 == first free)
d9d3d176
MCC
329 * @warn_if_nr_in_use: warn if the desired device node number
330 * was already in use and another number was chosen instead.
331 * @owner: module that owns the video device node
332 *
333 * The registration code assigns minor numbers and device node numbers
334 * based on the requested type and registers the new device node with
335 * the kernel.
336 *
337 * This function assumes that struct video_device was zeroed when it
338 * was allocated and does not contain any stale date.
339 *
340 * An error is returned if no free minor or device node number could be
341 * found, or if the registration of the device node failed.
342 *
343 * Returns 0 on success.
344 *
d9d3d176
MCC
345 * .. note::
346 *
347 * This function is meant to be used only inside the V4L2 core.
348 * Drivers should use video_register_device() or
349 * video_register_device_no_warn().
350 */
4839c58f
MCC
351int __must_check __video_register_device(struct video_device *vdev,
352 enum vfl_devnode_type type,
353 int nr, int warn_if_nr_in_use,
354 struct module *owner);
2096a5dc 355
d9d3d176
MCC
356/**
357 * video_register_device - register video4linux devices
358 *
359 * @vdev: struct video_device to register
4839c58f 360 * @type: type of device to register, as defined by &enum vfl_devnode_type
d9d3d176 361 * @nr: which device node number is desired:
4a3fad70 362 * (0 == /dev/video0, 1 == /dev/video1, ..., -1 == first free)
d9d3d176
MCC
363 *
364 * Internally, it calls __video_register_device(). Please see its
365 * documentation for more details.
366 *
367 * .. note::
4a3fad70 368 * if video_register_device fails, the release() callback of
d9d3d176
MCC
369 * &struct video_device structure is *not* called, so the caller
370 * is responsible for freeing any data. Usually that means that
371 * you video_device_release() should be called on failure.
372 */
2096a5dc 373static inline int __must_check video_register_device(struct video_device *vdev,
4839c58f
MCC
374 enum vfl_devnode_type type,
375 int nr)
2096a5dc
LP
376{
377 return __video_register_device(vdev, type, nr, 1, vdev->fops->owner);
378}
dc93a70c 379
d9d3d176
MCC
380/**
381 * video_register_device_no_warn - register video4linux devices
382 *
383 * @vdev: struct video_device to register
4839c58f 384 * @type: type of device to register, as defined by &enum vfl_devnode_type
d9d3d176 385 * @nr: which device node number is desired:
4a3fad70 386 * (0 == /dev/video0, 1 == /dev/video1, ..., -1 == first free)
d9d3d176
MCC
387 *
388 * This function is identical to video_register_device() except that no
389 * warning is issued if the desired device node number was already in use.
390 *
391 * Internally, it calls __video_register_device(). Please see its
392 * documentation for more details.
393 *
394 * .. note::
4a3fad70 395 * if video_register_device fails, the release() callback of
d9d3d176
MCC
396 * &struct video_device structure is *not* called, so the caller
397 * is responsible for freeing any data. Usually that means that
398 * you video_device_release() should be called on failure.
399 */
4839c58f
MCC
400static inline int __must_check
401video_register_device_no_warn(struct video_device *vdev,
402 enum vfl_devnode_type type, int nr)
2096a5dc
LP
403{
404 return __video_register_device(vdev, type, nr, 0, vdev->fops->owner);
405}
6b5270d2 406
d9d3d176
MCC
407/**
408 * video_unregister_device - Unregister video devices.
409 *
410 * @vdev: &struct video_device to register
411 *
412 * Does nothing if vdev == NULL or if video_is_registered() returns false.
413 */
dc93a70c 414void video_unregister_device(struct video_device *vdev);
401998fa 415
d9d3d176
MCC
416/**
417 * video_device_alloc - helper function to alloc &struct video_device
418 *
419 * Returns NULL if %-ENOMEM or a &struct video_device on success.
420 */
e138c592 421struct video_device * __must_check video_device_alloc(void);
bfa8a273 422
d9d3d176
MCC
423/**
424 * video_device_release - helper function to release &struct video_device
425 *
426 * @vdev: pointer to &struct video_device
427 *
564aaf69 428 * Can also be used for video_device->release\(\).
d9d3d176 429 */
dc93a70c 430void video_device_release(struct video_device *vdev);
bfa8a273 431
d9d3d176
MCC
432/**
433 * video_device_release_empty - helper function to implement the
4a3fad70 434 * video_device->release\(\) callback.
d9d3d176
MCC
435 *
436 * @vdev: pointer to &struct video_device
437 *
438 * This release function does nothing.
439 *
440 * It should be used when the video_device is a static global struct.
441 *
442 * .. note::
443 * Having a static video_device is a dubious construction at best.
444 */
dc93a70c 445void video_device_release_empty(struct video_device *vdev);
401998fa 446
d9d3d176
MCC
447/**
448 * v4l2_disable_ioctl- mark that a given command isn't implemented.
449 * shouldn't use core locking
450 *
451 * @vdev: pointer to &struct video_device
452 * @cmd: ioctl command
453 *
454 * This function allows drivers to provide just one v4l2_ioctl_ops struct, but
455 * disable ioctls based on the specific card that is actually found.
456 *
457 * .. note::
458 *
459 * This must be called before video_register_device.
460 * See also the comments for determine_valid_ioctls().
461 */
462static inline void v4l2_disable_ioctl(struct video_device *vdev,
463 unsigned int cmd)
48ea0be0
HV
464{
465 if (_IOC_NR(cmd) < BASE_VIDIOC_PRIVATE)
466 set_bit(_IOC_NR(cmd), vdev->valid_ioctls);
467}
468
d9d3d176
MCC
469/**
470 * video_get_drvdata - gets private data from &struct video_device.
471 *
472 * @vdev: pointer to &struct video_device
473 *
474 * returns a pointer to the private data
475 */
dc93a70c 476static inline void *video_get_drvdata(struct video_device *vdev)
401998fa 477{
dc93a70c 478 return dev_get_drvdata(&vdev->dev);
401998fa
MCC
479}
480
d9d3d176
MCC
481/**
482 * video_set_drvdata - sets private data from &struct video_device.
483 *
484 * @vdev: pointer to &struct video_device
485 * @data: private data pointer
486 */
dc93a70c 487static inline void video_set_drvdata(struct video_device *vdev, void *data)
401998fa 488{
dc93a70c 489 dev_set_drvdata(&vdev->dev, data);
401998fa 490}
38ee04f0 491
d9d3d176
MCC
492/**
493 * video_devdata - gets &struct video_device from struct file.
494 *
495 * @file: pointer to struct file
496 */
bfa8a273
HV
497struct video_device *video_devdata(struct file *file);
498
d9d3d176
MCC
499/**
500 * video_drvdata - gets private data from &struct video_device using the
501 * struct file.
502 *
503 * @file: pointer to struct file
504 *
505 * This is function combines both video_get_drvdata() and video_devdata()
506 * as this is used very often.
507 */
bfa8a273
HV
508static inline void *video_drvdata(struct file *file)
509{
510 return video_get_drvdata(video_devdata(file));
511}
401998fa 512
d9d3d176
MCC
513/**
514 * video_device_node_name - returns the video device name
515 *
516 * @vdev: pointer to &struct video_device
517 *
518 * Returns the device name string
519 */
eac8ea53
LP
520static inline const char *video_device_node_name(struct video_device *vdev)
521{
522 return dev_name(&vdev->dev);
523}
524
d9d3d176
MCC
525/**
526 * video_is_registered - returns true if the &struct video_device is registered.
527 *
528 *
529 * @vdev: pointer to &struct video_device
530 */
957b4aa9 531static inline int video_is_registered(struct video_device *vdev)
dc93a70c 532{
957b4aa9 533 return test_bit(V4L2_FL_REGISTERED, &vdev->flags);
dc93a70c
HV
534}
535
401998fa 536#endif /* _V4L2_DEV_H */