1 /* SPDX-License-Identifier: GPL-2.0 */
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
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>
19
20 #include <media/media-entity.h>
21
22 #define VIDEO_MAJOR 81
23
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
33 * @VFL_TYPE_MAX: number of VFL types, must always be last in the enum
34 */
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 */
43 };
44
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 */
55 enum vfl_devnode_direction {
56 VFL_DIR_RX,
57 VFL_DIR_TX,
58 VFL_DIR_M2M,
59 };
60
61 struct v4l2_ioctl_callbacks;
62 struct video_device;
63 struct v4l2_device;
64 struct v4l2_ctrl_handler;
65
66 /**
67 * enum v4l2_video_device_flags - Flags used by &struct video_device
68 *
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.
85 */
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,
90 };
91
92 /* Priority helper functions */
93
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
102 * by enum &v4l2_priority.
103 */
104 struct v4l2_prio_state {
105 atomic_t prios[4];
106 };
107
108 /**
109 * v4l2_prio_init - initializes a struct v4l2_prio_state
110 *
111 * @global: pointer to &struct v4l2_prio_state
112 */
113 void v4l2_prio_init(struct v4l2_prio_state *global);
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.
119 * @local: pointer to the desired priority, as defined by enum &v4l2_priority
120 * @new: Priority type requested, as defined by enum &v4l2_priority.
121 *
122 * .. note::
123 * This function should be used only by the V4L2 core.
124 */
125 int v4l2_prio_change(struct v4l2_prio_state *global, enum v4l2_priority *local,
126 enum v4l2_priority new);
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.
132 * @local: pointer to the desired priority, as defined by enum &v4l2_priority
133 *
134 * .. note::
135 * This function should be used only by the V4L2 core.
136 */
137 void v4l2_prio_open(struct v4l2_prio_state *global, enum v4l2_priority *local);
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.
143 * @local: priority to be released, as defined by enum &v4l2_priority
144 *
145 * .. note::
146 * This function should be used only by the V4L2 core.
147 */
148 void v4l2_prio_close(struct v4l2_prio_state *global, enum v4l2_priority local);
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 */
158 enum v4l2_priority v4l2_prio_max(struct v4l2_prio_state *global);
159
160 /**
161 * v4l2_prio_check - Implements the priority logic for a file handler close
162 *
163 * @global: pointer to the &struct v4l2_prio_state of the device node.
164 * @local: desired priority, as defined by enum &v4l2_priority local
165 *
166 * .. note::
167 * This function should be used only by the V4L2 core.
168 */
169 int v4l2_prio_check(struct v4l2_prio_state *global, enum v4l2_priority local);
170
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 */
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 *);
207 };
208
209 /*
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
213 */
214
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
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
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
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
256 struct video_device
257 {
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;
264
265 u32 device_caps;
266
267 /* sysfs */
268 struct device dev;
269 struct cdev *cdev;
270
271 struct v4l2_device *v4l2_dev;
272 struct device *dev_parent;
273
274 struct v4l2_ctrl_handler *ctrl_handler;
275
276 struct vb2_queue *queue;
277
278 struct v4l2_prio_state *prio;
279
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;
288
289 /* V4L2 file handles */
290 spinlock_t fh_lock;
291 struct list_head fh_list;
292
293 int dev_debug;
294
295 v4l2_std_id tvnorms;
296
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);
301
302 struct mutex *lock;
303 };
304
305 /**
306 * media_entity_to_video_device - Returns a &struct video_device from
307 * the &struct media_entity embedded on it.
308 *
309 * @__entity: pointer to &struct media_entity
310 */
311 #define media_entity_to_video_device(__entity) \
312 container_of(__entity, struct video_device, entity)
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 */
320 #define to_video_device(cd) container_of(cd, struct video_device, dev)
321
322 /**
323 * __video_register_device - register video4linux devices
324 *
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
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 *
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 */
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);
355
356 /**
357 * video_register_device - register video4linux devices
358 *
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)
363 *
364 * Internally, it calls __video_register_device(). Please see its
365 * documentation for more details.
366 *
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.
372 */
video_register_device(struct video_device * vdev,enum vfl_devnode_type type,int nr)373 static inline int __must_check video_register_device(struct video_device *vdev,
374 enum vfl_devnode_type type,
375 int nr)
376 {
377 return __video_register_device(vdev, type, nr, 1, vdev->fops->owner);
378 }
379
380 /**
381 * video_register_device_no_warn - register video4linux devices
382 *
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)
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::
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.
399 */
400 static inline int __must_check
video_register_device_no_warn(struct video_device * vdev,enum vfl_devnode_type type,int nr)401 video_register_device_no_warn(struct video_device *vdev,
402 enum vfl_devnode_type type, int nr)
403 {
404 return __video_register_device(vdev, type, nr, 0, vdev->fops->owner);
405 }
406
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 */
414 void video_unregister_device(struct video_device *vdev);
415
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 */
421 struct video_device * __must_check video_device_alloc(void);
422
423 /**
424 * video_device_release - helper function to release &struct video_device
425 *
426 * @vdev: pointer to &struct video_device
427 *
428 * Can also be used for video_device->release\(\).
429 */
430 void video_device_release(struct video_device *vdev);
431
432 /**
433 * video_device_release_empty - helper function to implement the
434 * video_device->release\(\) callback.
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 */
445 void video_device_release_empty(struct video_device *vdev);
446
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 */
v4l2_disable_ioctl(struct video_device * vdev,unsigned int cmd)462 static inline void v4l2_disable_ioctl(struct video_device *vdev,
463 unsigned int cmd)
464 {
465 if (_IOC_NR(cmd) < BASE_VIDIOC_PRIVATE)
466 set_bit(_IOC_NR(cmd), vdev->valid_ioctls);
467 }
468
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 */
video_get_drvdata(struct video_device * vdev)476 static inline void *video_get_drvdata(struct video_device *vdev)
477 {
478 return dev_get_drvdata(&vdev->dev);
479 }
480
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 */
video_set_drvdata(struct video_device * vdev,void * data)487 static inline void video_set_drvdata(struct video_device *vdev, void *data)
488 {
489 dev_set_drvdata(&vdev->dev, data);
490 }
491
492 /**
493 * video_devdata - gets &struct video_device from struct file.
494 *
495 * @file: pointer to struct file
496 */
497 struct video_device *video_devdata(struct file *file);
498
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 */
video_drvdata(struct file * file)508 static inline void *video_drvdata(struct file *file)
509 {
510 return video_get_drvdata(video_devdata(file));
511 }
512
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 */
video_device_node_name(struct video_device * vdev)520 static inline const char *video_device_node_name(struct video_device *vdev)
521 {
522 return dev_name(&vdev->dev);
523 }
524
525 /**
526 * video_is_registered - returns true if the &struct video_device is registered.
527 *
528 *
529 * @vdev: pointer to &struct video_device
530 */
video_is_registered(struct video_device * vdev)531 static inline int video_is_registered(struct video_device *vdev)
532 {
533 return test_bit(V4L2_FL_REGISTERED, &vdev->flags);
534 }
535
536 #endif /* _V4L2_DEV_H */
537