xref: /linux-6.15/include/media/media-devnode.h (revision fe3c565e)
1 /*
2  * Media device node
3  *
4  * Copyright (C) 2010 Nokia Corporation
5  *
6  * Contacts: Laurent Pinchart <[email protected]>
7  *	     Sakari Ailus <[email protected]>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  * --
23  *
24  * Common functions for media-related drivers to register and unregister media
25  * device nodes.
26  */
27 
28 #ifndef _MEDIA_DEVNODE_H
29 #define _MEDIA_DEVNODE_H
30 
31 #include <linux/poll.h>
32 #include <linux/fs.h>
33 #include <linux/device.h>
34 #include <linux/cdev.h>
35 
36 /*
37  * Flag to mark the media_devnode struct as registered. Drivers must not touch
38  * this flag directly, it will be set and cleared by media_devnode_register and
39  * media_devnode_unregister.
40  */
41 #define MEDIA_FLAG_REGISTERED	0
42 
43 struct media_file_operations {
44 	struct module *owner;
45 	ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
46 	ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
47 	unsigned int (*poll) (struct file *, struct poll_table_struct *);
48 	long (*ioctl) (struct file *, unsigned int, unsigned long);
49 	long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
50 	int (*open) (struct file *);
51 	int (*release) (struct file *);
52 };
53 
54 /**
55  * struct media_devnode - Media device node
56  * @fops:	pointer to struct media_file_operations with media device ops
57  * @dev:	struct device pointer for the media controller device
58  * @cdev:	struct cdev pointer character device
59  * @parent:	parent device
60  * @minor:	device node minor number
61  * @flags:	flags, combination of the MEDIA_FLAG_* constants
62  * @release:	release callback called at the end of media_devnode_release()
63  *
64  * This structure represents a media-related device node.
65  *
66  * The @parent is a physical device. It must be set by core or device drivers
67  * before registering the node.
68  */
69 struct media_devnode {
70 	/* device ops */
71 	const struct media_file_operations *fops;
72 
73 	/* sysfs */
74 	struct device dev;		/* media device */
75 	struct cdev cdev;		/* character device */
76 	struct device *parent;		/* device parent */
77 
78 	/* device info */
79 	int minor;
80 	unsigned long flags;		/* Use bitops to access flags */
81 
82 	/* callbacks */
83 	void (*release)(struct media_devnode *mdev);
84 };
85 
86 /* dev to media_devnode */
87 #define to_media_devnode(cd) container_of(cd, struct media_devnode, dev)
88 
89 /**
90  * media_devnode_register - register a media device node
91  *
92  * @mdev: media device node structure we want to register
93  * @owner: should be filled with %THIS_MODULE
94  *
95  * The registration code assigns minor numbers and registers the new device node
96  * with the kernel. An error is returned if no free minor number can be found,
97  * or if the registration of the device node fails.
98  *
99  * Zero is returned on success.
100  *
101  * Note that if the media_devnode_register call fails, the release() callback of
102  * the media_devnode structure is *not* called, so the caller is responsible for
103  * freeing any data.
104  */
105 int __must_check media_devnode_register(struct media_devnode *mdev,
106 					struct module *owner);
107 
108 /**
109  * media_devnode_unregister - unregister a media device node
110  * @mdev: the device node to unregister
111  *
112  * This unregisters the passed device. Future open calls will be met with
113  * errors.
114  *
115  * This function can safely be called if the device node has never been
116  * registered or has already been unregistered.
117  */
118 void media_devnode_unregister(struct media_devnode *mdev);
119 
120 static inline struct media_devnode *media_devnode_data(struct file *filp)
121 {
122 	return filp->private_data;
123 }
124 
125 static inline int media_devnode_is_registered(struct media_devnode *mdev)
126 {
127 	return test_bit(MEDIA_FLAG_REGISTERED, &mdev->flags);
128 }
129 
130 #endif /* _MEDIA_DEVNODE_H */
131