blob: fe42f08e72bdc5e58cb2639b3dfb1ca4de434f0c [file] [log] [blame]
Laurent Pinchartcf4b9212009-12-09 08:39:56 -03001/*
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/*
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
Mauro Carvalho Chehab75c7e292015-12-13 09:00:00 -020043/**
44 * struct media_file_operations - Media device file operations
45 *
46 * @owner: should be filled with %THIS_MODULE
47 * @read: pointer to the function that implements read() syscall
48 * @write: pointer to the function that implements write() syscall
49 * @poll: pointer to the function that implements poll() syscall
50 * @ioctl: pointer to the function that implements ioctl() syscall
51 * @compat_ioctl: pointer to the function that will handle 32 bits userspace
52 * calls to the the ioctl() syscall on a Kernel compiled with 64 bits.
53 * @open: pointer to the function that implements open() syscall
54 * @release: pointer to the function that will release the resources allocated
55 * by the @open function.
56 */
Laurent Pinchartcf4b9212009-12-09 08:39:56 -030057struct media_file_operations {
58 struct module *owner;
59 ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
60 ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
61 unsigned int (*poll) (struct file *, struct poll_table_struct *);
62 long (*ioctl) (struct file *, unsigned int, unsigned long);
Sakari Ailusc6c1d502013-01-22 12:27:55 -030063 long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -030064 int (*open) (struct file *);
65 int (*release) (struct file *);
66};
67
68/**
69 * struct media_devnode - Media device node
Mauro Carvalho Chehab75c7e292015-12-13 09:00:00 -020070 * @fops: pointer to struct &media_file_operations with media device ops
Mauro Carvalho Chehabec0255c2015-08-22 04:45:03 -030071 * @dev: struct device pointer for the media controller device
72 * @cdev: struct cdev pointer character device
Laurent Pinchartcf4b9212009-12-09 08:39:56 -030073 * @parent: parent device
74 * @minor: device node minor number
75 * @flags: flags, combination of the MEDIA_FLAG_* constants
Mauro Carvalho Chehabec0255c2015-08-22 04:45:03 -030076 * @release: release callback called at the end of media_devnode_release()
Laurent Pinchartcf4b9212009-12-09 08:39:56 -030077 *
78 * This structure represents a media-related device node.
79 *
80 * The @parent is a physical device. It must be set by core or device drivers
81 * before registering the node.
82 */
83struct media_devnode {
84 /* device ops */
85 const struct media_file_operations *fops;
86
87 /* sysfs */
88 struct device dev; /* media device */
89 struct cdev cdev; /* character device */
90 struct device *parent; /* device parent */
91
92 /* device info */
93 int minor;
94 unsigned long flags; /* Use bitops to access flags */
95
96 /* callbacks */
97 void (*release)(struct media_devnode *mdev);
98};
99
100/* dev to media_devnode */
101#define to_media_devnode(cd) container_of(cd, struct media_devnode, dev)
102
Mauro Carvalho Chehabfe3c5652015-12-13 08:40:45 -0200103/**
104 * media_devnode_register - register a media device node
105 *
106 * @mdev: media device node structure we want to register
107 * @owner: should be filled with %THIS_MODULE
108 *
109 * The registration code assigns minor numbers and registers the new device node
110 * with the kernel. An error is returned if no free minor number can be found,
111 * or if the registration of the device node fails.
112 *
113 * Zero is returned on success.
114 *
115 * Note that if the media_devnode_register call fails, the release() callback of
116 * the media_devnode structure is *not* called, so the caller is responsible for
117 * freeing any data.
118 */
Sakari Ailus85de7212013-12-12 12:38:17 -0300119int __must_check media_devnode_register(struct media_devnode *mdev,
120 struct module *owner);
Mauro Carvalho Chehabfe3c5652015-12-13 08:40:45 -0200121
122/**
123 * media_devnode_unregister - unregister a media device node
124 * @mdev: the device node to unregister
125 *
126 * This unregisters the passed device. Future open calls will be met with
127 * errors.
128 *
129 * This function can safely be called if the device node has never been
130 * registered or has already been unregistered.
131 */
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300132void media_devnode_unregister(struct media_devnode *mdev);
133
Mauro Carvalho Chehab75c7e292015-12-13 09:00:00 -0200134/**
135 * media_devnode_data - returns a pointer to the &media_devnode
136 *
137 * @filp: pointer to struct &file
138 */
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300139static inline struct media_devnode *media_devnode_data(struct file *filp)
140{
141 return filp->private_data;
142}
143
Mauro Carvalho Chehab75c7e292015-12-13 09:00:00 -0200144/**
145 * media_devnode_is_registered - returns true if &media_devnode is registered;
146 * false otherwise.
147 *
148 * @mdev: pointer to struct &media_devnode.
149 */
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300150static inline int media_devnode_is_registered(struct media_devnode *mdev)
151{
152 return test_bit(MEDIA_FLAG_REGISTERED, &mdev->flags);
153}
154
155#endif /* _MEDIA_DEVNODE_H */