^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0-only */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Media device node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2010 Nokia Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Sakari Ailus <sakari.ailus@iki.fi>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * --
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * Common functions for media-related drivers to register and unregister media
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * device nodes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #ifndef _MEDIA_DEVNODE_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #define _MEDIA_DEVNODE_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/cdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct media_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * Flag to mark the media_devnode struct as registered. Drivers must not touch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * this flag directly, it will be set and cleared by media_devnode_register and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * media_devnode_unregister.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #define MEDIA_FLAG_REGISTERED 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * struct media_file_operations - Media device file operations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * @owner: should be filled with %THIS_MODULE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * @read: pointer to the function that implements read() syscall
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * @write: pointer to the function that implements write() syscall
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * @poll: pointer to the function that implements poll() syscall
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * @ioctl: pointer to the function that implements ioctl() syscall
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * @compat_ioctl: pointer to the function that will handle 32 bits userspace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * calls to the ioctl() syscall on a Kernel compiled with 64 bits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * @open: pointer to the function that implements open() syscall
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * @release: pointer to the function that will release the resources allocated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * by the @open function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct media_file_operations {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct module *owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) __poll_t (*poll) (struct file *, struct poll_table_struct *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) long (*ioctl) (struct file *, unsigned int, unsigned long);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) int (*open) (struct file *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) int (*release) (struct file *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * struct media_devnode - Media device node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * @media_dev: pointer to struct &media_device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * @fops: pointer to struct &media_file_operations with media device ops
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * @dev: pointer to struct &device containing the media controller device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * @cdev: struct cdev pointer character device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * @parent: parent device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * @minor: device node minor number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * @flags: flags, combination of the ``MEDIA_FLAG_*`` constants
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * @release: release callback called at the end of ``media_devnode_release()``
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * routine at media-device.c.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * This structure represents a media-related device node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * The @parent is a physical device. It must be set by core or device drivers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * before registering the node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) struct media_devnode {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) struct media_device *media_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) /* device ops */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) const struct media_file_operations *fops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) /* sysfs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) struct device dev; /* media device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) struct cdev cdev; /* character device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) struct device *parent; /* device parent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) /* device info */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) int minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) unsigned long flags; /* Use bitops to access flags */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) /* callbacks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) void (*release)(struct media_devnode *devnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) /* dev to media_devnode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) #define to_media_devnode(cd) container_of(cd, struct media_devnode, dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) * media_devnode_register - register a media device node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * @mdev: struct media_device we want to register a device node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * @devnode: media device node structure we want to register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) * @owner: should be filled with %THIS_MODULE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * The registration code assigns minor numbers and registers the new device node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) * with the kernel. An error is returned if no free minor number can be found,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) * or if the registration of the device node fails.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) * Zero is returned on success.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) * Note that if the media_devnode_register call fails, the release() callback of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) * the media_devnode structure is *not* called, so the caller is responsible for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) * freeing any data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) int __must_check media_devnode_register(struct media_device *mdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) struct media_devnode *devnode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) struct module *owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * media_devnode_unregister_prepare - clear the media device node register bit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) * @devnode: the device node to prepare for unregister
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) * This clears the passed device register bit. Future open calls will be met
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) * with errors. Should be called before media_devnode_unregister() to avoid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) * races with unregister and device file open calls.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) * This function can safely be called if the device node has never been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) * registered or has already been unregistered.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) void media_devnode_unregister_prepare(struct media_devnode *devnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) * media_devnode_unregister - unregister a media device node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) * @devnode: the device node to unregister
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) * This unregisters the passed device. Future open calls will be met with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) * errors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) * Should be called after media_devnode_unregister_prepare()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) void media_devnode_unregister(struct media_devnode *devnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) * media_devnode_data - returns a pointer to the &media_devnode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) * @filp: pointer to struct &file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) static inline struct media_devnode *media_devnode_data(struct file *filp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) return filp->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) * media_devnode_is_registered - returns true if &media_devnode is registered;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) * false otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) * @devnode: pointer to struct &media_devnode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) * Note: If mdev is NULL, it also returns false.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) static inline int media_devnode_is_registered(struct media_devnode *devnode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) if (!devnode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) return test_bit(MEDIA_FLAG_REGISTERED, &devnode->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) #endif /* _MEDIA_DEVNODE_H */