GNU Linux-libre 4.4.284-gnu1
[releases.git] / include / media / media-devnode.h
1 /*
2  * Media device node
3  *
4  * Copyright (C) 2010 Nokia Corporation
5  *
6  * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
7  *           Sakari Ailus <sakari.ailus@iki.fi>
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 struct media_device;
37
38 /*
39  * Flag to mark the media_devnode struct as registered. Drivers must not touch
40  * this flag directly, it will be set and cleared by media_devnode_register and
41  * media_devnode_unregister.
42  */
43 #define MEDIA_FLAG_REGISTERED   0
44
45 struct media_file_operations {
46         struct module *owner;
47         ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
48         ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
49         unsigned int (*poll) (struct file *, struct poll_table_struct *);
50         long (*ioctl) (struct file *, unsigned int, unsigned long);
51         long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
52         int (*open) (struct file *);
53         int (*release) (struct file *);
54 };
55
56 /**
57  * struct media_devnode - Media device node
58  * @fops:       pointer to struct media_file_operations with media device ops
59  * @dev:        struct device pointer for the media controller device
60  * @cdev:       struct cdev pointer character device
61  * @parent:     parent device
62  * @minor:      device node minor number
63  * @flags:      flags, combination of the MEDIA_FLAG_* constants
64  * @release:    release callback called at the end of media_devnode_release()
65  *
66  * This structure represents a media-related device node.
67  *
68  * The @parent is a physical device. It must be set by core or device drivers
69  * before registering the node.
70  */
71 struct media_devnode {
72         struct media_device *media_dev;
73
74         /* device ops */
75         const struct media_file_operations *fops;
76
77         /* sysfs */
78         struct device dev;              /* media device */
79         struct cdev cdev;               /* character device */
80         struct device *parent;          /* device parent */
81
82         /* device info */
83         int minor;
84         unsigned long flags;            /* Use bitops to access flags */
85
86         /* callbacks */
87         void (*release)(struct media_devnode *devnode);
88 };
89
90 /* dev to media_devnode */
91 #define to_media_devnode(cd) container_of(cd, struct media_devnode, dev)
92
93 int __must_check media_devnode_register(struct media_device *mdev,
94                                         struct media_devnode *devnode,
95                                         struct module *owner);
96
97 /**
98  * media_devnode_unregister_prepare - clear the media device node register bit
99  * @devnode: the device node to prepare for unregister
100  *
101  * This clears the passed device register bit. Future open calls will be met
102  * with errors. Should be called before media_devnode_unregister() to avoid
103  * races with unregister and device file open calls.
104  *
105  * This function can safely be called if the device node has never been
106  * registered or has already been unregistered.
107  */
108 void media_devnode_unregister_prepare(struct media_devnode *devnode);
109
110 void media_devnode_unregister(struct media_devnode *devnode);
111
112 static inline struct media_devnode *media_devnode_data(struct file *filp)
113 {
114         return filp->private_data;
115 }
116
117 static inline int media_devnode_is_registered(struct media_devnode *devnode)
118 {
119         if (!devnode)
120                 return false;
121
122         return test_bit(MEDIA_FLAG_REGISTERED, &devnode->flags);
123 }
124
125 #endif /* _MEDIA_DEVNODE_H */