treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / include / media / v4l2-dev.h
blob48531e57cc5a86d2c95bda85108d1b44c93f6c29
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
4 * V 4 L 2 D R I V E R H E L P E R A P I
6 * Moved from videodev2.h
8 * Some commonly needed functions for drivers (v4l2-common.o module)
9 */
10 #ifndef _V4L2_DEV_H
11 #define _V4L2_DEV_H
13 #include <linux/poll.h>
14 #include <linux/fs.h>
15 #include <linux/device.h>
16 #include <linux/cdev.h>
17 #include <linux/mutex.h>
18 #include <linux/videodev2.h>
20 #include <media/media-entity.h>
22 #define VIDEO_MAJOR 81
24 /**
25 * enum vfl_devnode_type - type of V4L2 device node
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
33 * @VFL_TYPE_MAX: number of VFL types, must always be last in the enum
35 enum vfl_devnode_type {
36 VFL_TYPE_GRABBER = 0,
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 */
45 /**
46 * enum vfl_direction - Identifies if a &struct video_device corresponds
47 * to a receiver, a transmitter or a mem-to-mem device.
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.
53 * Note: Ignored if &enum vfl_devnode_type is %VFL_TYPE_SUBDEV.
55 enum vfl_devnode_direction {
56 VFL_DIR_RX,
57 VFL_DIR_TX,
58 VFL_DIR_M2M,
61 struct v4l2_ioctl_callbacks;
62 struct video_device;
63 struct v4l2_device;
64 struct v4l2_ctrl_handler;
66 /**
67 * enum v4l2_video_device_flags - Flags used by &struct video_device
69 * @V4L2_FL_REGISTERED:
70 * indicates that a &struct video_device is registered.
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.
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.
86 enum v4l2_video_device_flags {
87 V4L2_FL_REGISTERED = 0,
88 V4L2_FL_USES_V4L2_FH = 1,
89 V4L2_FL_QUIRK_INVERTED_CROP = 2,
92 /* Priority helper functions */
94 /**
95 * struct v4l2_prio_state - stores the priority states
97 * @prios: array with elements to store the array priorities
100 * .. note::
101 * The size of @prios array matches the number of priority types defined
102 * by enum &v4l2_priority.
104 struct v4l2_prio_state {
105 atomic_t prios[4];
109 * v4l2_prio_init - initializes a struct v4l2_prio_state
111 * @global: pointer to &struct v4l2_prio_state
113 void v4l2_prio_init(struct v4l2_prio_state *global);
116 * v4l2_prio_change - changes the v4l2 file handler priority
118 * @global: pointer to the &struct v4l2_prio_state of the device node.
119 * @local: pointer to the desired priority, as defined by enum &v4l2_priority
120 * @new: Priority type requested, as defined by enum &v4l2_priority.
122 * .. note::
123 * This function should be used only by the V4L2 core.
125 int v4l2_prio_change(struct v4l2_prio_state *global, enum v4l2_priority *local,
126 enum v4l2_priority new);
129 * v4l2_prio_open - Implements the priority logic for a file handler open
131 * @global: pointer to the &struct v4l2_prio_state of the device node.
132 * @local: pointer to the desired priority, as defined by enum &v4l2_priority
134 * .. note::
135 * This function should be used only by the V4L2 core.
137 void v4l2_prio_open(struct v4l2_prio_state *global, enum v4l2_priority *local);
140 * v4l2_prio_close - Implements the priority logic for a file handler close
142 * @global: pointer to the &struct v4l2_prio_state of the device node.
143 * @local: priority to be released, as defined by enum &v4l2_priority
145 * .. note::
146 * This function should be used only by the V4L2 core.
148 void v4l2_prio_close(struct v4l2_prio_state *global, enum v4l2_priority local);
151 * v4l2_prio_max - Return the maximum priority, as stored at the @global array.
153 * @global: pointer to the &struct v4l2_prio_state of the device node.
155 * .. note::
156 * This function should be used only by the V4L2 core.
158 enum v4l2_priority v4l2_prio_max(struct v4l2_prio_state *global);
161 * v4l2_prio_check - Implements the priority logic for a file handler close
163 * @global: pointer to the &struct v4l2_prio_state of the device node.
164 * @local: desired priority, as defined by enum &v4l2_priority local
166 * .. note::
167 * This function should be used only by the V4L2 core.
169 int v4l2_prio_check(struct v4l2_prio_state *global, enum v4l2_priority local);
172 * struct v4l2_file_operations - fs operations used by a V4L2 device
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
187 * .. note::
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.
193 struct 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 *);
197 __poll_t (*poll) (struct file *, struct poll_table_struct *);
198 long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
199 #ifdef CONFIG_COMPAT
200 long (*compat_ioctl32) (struct file *, unsigned int, unsigned long);
201 #endif
202 unsigned long (*get_unmapped_area) (struct file *, unsigned long,
203 unsigned long, unsigned long, unsigned long);
204 int (*mmap) (struct file *, struct vm_area_struct *);
205 int (*open) (struct file *);
206 int (*release) (struct file *);
210 * Newer version of video_device, handled by videodev2.c
211 * This version moves redundant code from video device code to
212 * the common handler
216 * struct video_device - Structure used to create and manage the V4L2 device
217 * nodes.
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
234 * @vfl_type: V4L device type, as defined by &enum vfl_devnode_type
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
238 * @flags: video device flags. Use bitops to set/clear/test flags.
239 * Contains a set of &enum v4l2_video_device_flags.
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
246 * @release: video device release() callback
247 * @ioctl_ops: pointer to &struct v4l2_ioctl_ops with ioctl callbacks
249 * @valid_ioctls: bitmap with the valid ioctls for this device
250 * @lock: pointer to &struct mutex serialization lock
252 * .. note::
253 * Only set @dev_parent if that can't be deduced from @v4l2_dev.
256 struct video_device
258 #if defined(CONFIG_MEDIA_CONTROLLER)
259 struct media_entity entity;
260 struct media_intf_devnode *intf_devnode;
261 struct media_pipeline pipe;
262 #endif
263 const struct v4l2_file_operations *fops;
265 u32 device_caps;
267 /* sysfs */
268 struct device dev;
269 struct cdev *cdev;
271 struct v4l2_device *v4l2_dev;
272 struct device *dev_parent;
274 struct v4l2_ctrl_handler *ctrl_handler;
276 struct vb2_queue *queue;
278 struct v4l2_prio_state *prio;
280 /* device info */
281 char name[32];
282 enum vfl_devnode_type vfl_type;
283 enum vfl_devnode_direction vfl_dir;
284 int minor;
285 u16 num;
286 unsigned long flags;
287 int index;
289 /* V4L2 file handles */
290 spinlock_t fh_lock;
291 struct list_head fh_list;
293 int dev_debug;
295 v4l2_std_id tvnorms;
297 /* callbacks */
298 void (*release)(struct video_device *vdev);
299 const struct v4l2_ioctl_ops *ioctl_ops;
300 DECLARE_BITMAP(valid_ioctls, BASE_VIDIOC_PRIVATE);
302 struct mutex *lock;
306 * media_entity_to_video_device - Returns a &struct video_device from
307 * the &struct media_entity embedded on it.
309 * @__entity: pointer to &struct media_entity
311 #define media_entity_to_video_device(__entity) \
312 container_of(__entity, struct video_device, entity)
315 * to_video_device - Returns a &struct video_device from the
316 * &struct device embedded on it.
318 * @cd: pointer to &struct device
320 #define to_video_device(cd) container_of(cd, struct video_device, dev)
323 * __video_register_device - register video4linux devices
325 * @vdev: struct video_device to register
326 * @type: type of device to register, as defined by &enum vfl_devnode_type
327 * @nr: which device node number is desired:
328 * (0 == /dev/video0, 1 == /dev/video1, ..., -1 == first free)
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
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.
337 * This function assumes that struct video_device was zeroed when it
338 * was allocated and does not contain any stale date.
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.
343 * Returns 0 on success.
345 * .. note::
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().
351 int __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);
357 * video_register_device - register video4linux devices
359 * @vdev: struct video_device to register
360 * @type: type of device to register, as defined by &enum vfl_devnode_type
361 * @nr: which device node number is desired:
362 * (0 == /dev/video0, 1 == /dev/video1, ..., -1 == first free)
364 * Internally, it calls __video_register_device(). Please see its
365 * documentation for more details.
367 * .. note::
368 * if video_register_device fails, the release() callback of
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.
373 static inline int __must_check video_register_device(struct video_device *vdev,
374 enum vfl_devnode_type type,
375 int nr)
377 return __video_register_device(vdev, type, nr, 1, vdev->fops->owner);
381 * video_register_device_no_warn - register video4linux devices
383 * @vdev: struct video_device to register
384 * @type: type of device to register, as defined by &enum vfl_devnode_type
385 * @nr: which device node number is desired:
386 * (0 == /dev/video0, 1 == /dev/video1, ..., -1 == first free)
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.
391 * Internally, it calls __video_register_device(). Please see its
392 * documentation for more details.
394 * .. note::
395 * if video_register_device fails, the release() callback of
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.
400 static inline int __must_check
401 video_register_device_no_warn(struct video_device *vdev,
402 enum vfl_devnode_type type, int nr)
404 return __video_register_device(vdev, type, nr, 0, vdev->fops->owner);
408 * video_unregister_device - Unregister video devices.
410 * @vdev: &struct video_device to register
412 * Does nothing if vdev == NULL or if video_is_registered() returns false.
414 void video_unregister_device(struct video_device *vdev);
417 * video_device_alloc - helper function to alloc &struct video_device
419 * Returns NULL if %-ENOMEM or a &struct video_device on success.
421 struct video_device * __must_check video_device_alloc(void);
424 * video_device_release - helper function to release &struct video_device
426 * @vdev: pointer to &struct video_device
428 * Can also be used for video_device->release\(\).
430 void video_device_release(struct video_device *vdev);
433 * video_device_release_empty - helper function to implement the
434 * video_device->release\(\) callback.
436 * @vdev: pointer to &struct video_device
438 * This release function does nothing.
440 * It should be used when the video_device is a static global struct.
442 * .. note::
443 * Having a static video_device is a dubious construction at best.
445 void video_device_release_empty(struct video_device *vdev);
448 * v4l2_disable_ioctl- mark that a given command isn't implemented.
449 * shouldn't use core locking
451 * @vdev: pointer to &struct video_device
452 * @cmd: ioctl command
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.
457 * .. note::
459 * This must be called before video_register_device.
460 * See also the comments for determine_valid_ioctls().
462 static inline void v4l2_disable_ioctl(struct video_device *vdev,
463 unsigned int cmd)
465 if (_IOC_NR(cmd) < BASE_VIDIOC_PRIVATE)
466 set_bit(_IOC_NR(cmd), vdev->valid_ioctls);
470 * video_get_drvdata - gets private data from &struct video_device.
472 * @vdev: pointer to &struct video_device
474 * returns a pointer to the private data
476 static inline void *video_get_drvdata(struct video_device *vdev)
478 return dev_get_drvdata(&vdev->dev);
482 * video_set_drvdata - sets private data from &struct video_device.
484 * @vdev: pointer to &struct video_device
485 * @data: private data pointer
487 static inline void video_set_drvdata(struct video_device *vdev, void *data)
489 dev_set_drvdata(&vdev->dev, data);
493 * video_devdata - gets &struct video_device from struct file.
495 * @file: pointer to struct file
497 struct video_device *video_devdata(struct file *file);
500 * video_drvdata - gets private data from &struct video_device using the
501 * struct file.
503 * @file: pointer to struct file
505 * This is function combines both video_get_drvdata() and video_devdata()
506 * as this is used very often.
508 static inline void *video_drvdata(struct file *file)
510 return video_get_drvdata(video_devdata(file));
514 * video_device_node_name - returns the video device name
516 * @vdev: pointer to &struct video_device
518 * Returns the device name string
520 static inline const char *video_device_node_name(struct video_device *vdev)
522 return dev_name(&vdev->dev);
526 * video_is_registered - returns true if the &struct video_device is registered.
529 * @vdev: pointer to &struct video_device
531 static inline int video_is_registered(struct video_device *vdev)
533 return test_bit(V4L2_FL_REGISTERED, &vdev->flags);
536 #endif /* _V4L2_DEV_H */