blob: dc2f64e1b08f0a55253afca656a3faffd6610ae8 [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 *
Laurent Pinchartcf4b9212009-12-09 08:39:56 -030018 * --
19 *
20 * Common functions for media-related drivers to register and unregister media
21 * device nodes.
22 */
23
24#ifndef _MEDIA_DEVNODE_H
25#define _MEDIA_DEVNODE_H
26
27#include <linux/poll.h>
28#include <linux/fs.h>
29#include <linux/device.h>
30#include <linux/cdev.h>
31
Mauro Carvalho Chehaba087ce72016-04-27 19:28:26 -030032struct media_device;
33
Laurent Pinchartcf4b9212009-12-09 08:39:56 -030034/*
35 * Flag to mark the media_devnode struct as registered. Drivers must not touch
36 * this flag directly, it will be set and cleared by media_devnode_register and
37 * media_devnode_unregister.
38 */
39#define MEDIA_FLAG_REGISTERED 0
40
Mauro Carvalho Chehab75c7e292015-12-13 09:00:00 -020041/**
42 * struct media_file_operations - Media device file operations
43 *
44 * @owner: should be filled with %THIS_MODULE
45 * @read: pointer to the function that implements read() syscall
46 * @write: pointer to the function that implements write() syscall
47 * @poll: pointer to the function that implements poll() syscall
48 * @ioctl: pointer to the function that implements ioctl() syscall
49 * @compat_ioctl: pointer to the function that will handle 32 bits userspace
50 * calls to the the ioctl() syscall on a Kernel compiled with 64 bits.
51 * @open: pointer to the function that implements open() syscall
52 * @release: pointer to the function that will release the resources allocated
53 * by the @open function.
54 */
Laurent Pinchartcf4b9212009-12-09 08:39:56 -030055struct media_file_operations {
56 struct module *owner;
57 ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
58 ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
Al Viroa3f86832017-07-02 22:22:01 -040059 __poll_t (*poll) (struct file *, struct poll_table_struct *);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -030060 long (*ioctl) (struct file *, unsigned int, unsigned long);
Sakari Ailusc6c1d502013-01-22 12:27:55 -030061 long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -030062 int (*open) (struct file *);
63 int (*release) (struct file *);
64};
65
66/**
67 * struct media_devnode - Media device node
Mauro Carvalho Chehab0db5c792016-06-16 08:04:40 -030068 * @media_dev: pointer to struct &media_device
Mauro Carvalho Chehab75c7e292015-12-13 09:00:00 -020069 * @fops: pointer to struct &media_file_operations with media device ops
Mauro Carvalho Chehab0db5c792016-06-16 08:04:40 -030070 * @dev: pointer to struct &device containing the media controller device
Mauro Carvalho Chehabec0255c2015-08-22 04:45:03 -030071 * @cdev: struct cdev pointer character device
Laurent Pinchartcf4b9212009-12-09 08:39:56 -030072 * @parent: parent device
73 * @minor: device node minor number
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -030074 * @flags: flags, combination of the ``MEDIA_FLAG_*`` constants
Mauro Carvalho Chehab82631b52016-09-08 09:57:24 -030075 * @release: release callback called at the end of ``media_devnode_release()``
76 * routine at media-device.c.
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 {
Mauro Carvalho Chehaba087ce72016-04-27 19:28:26 -030084 struct media_device *media_dev;
85
Laurent Pinchartcf4b9212009-12-09 08:39:56 -030086 /* device ops */
87 const struct media_file_operations *fops;
88
89 /* sysfs */
90 struct device dev; /* media device */
91 struct cdev cdev; /* character device */
92 struct device *parent; /* device parent */
93
94 /* device info */
95 int minor;
96 unsigned long flags; /* Use bitops to access flags */
97
98 /* callbacks */
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -030099 void (*release)(struct media_devnode *devnode);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300100};
101
102/* dev to media_devnode */
103#define to_media_devnode(cd) container_of(cd, struct media_devnode, dev)
104
Mauro Carvalho Chehabfe3c5652015-12-13 08:40:45 -0200105/**
106 * media_devnode_register - register a media device node
107 *
Mauro Carvalho Chehab0db5c792016-06-16 08:04:40 -0300108 * @mdev: struct media_device we want to register a device node
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300109 * @devnode: media device node structure we want to register
Mauro Carvalho Chehabfe3c5652015-12-13 08:40:45 -0200110 * @owner: should be filled with %THIS_MODULE
111 *
112 * The registration code assigns minor numbers and registers the new device node
113 * with the kernel. An error is returned if no free minor number can be found,
114 * or if the registration of the device node fails.
115 *
116 * Zero is returned on success.
117 *
118 * Note that if the media_devnode_register call fails, the release() callback of
119 * the media_devnode structure is *not* called, so the caller is responsible for
120 * freeing any data.
121 */
Mauro Carvalho Chehaba087ce72016-04-27 19:28:26 -0300122int __must_check media_devnode_register(struct media_device *mdev,
123 struct media_devnode *devnode,
Sakari Ailus85de7212013-12-12 12:38:17 -0300124 struct module *owner);
Mauro Carvalho Chehabfe3c5652015-12-13 08:40:45 -0200125
126/**
Shuah Khan6f0dd242016-06-10 14:37:23 -0300127 * media_devnode_unregister_prepare - clear the media device node register bit
128 * @devnode: the device node to prepare for unregister
129 *
130 * This clears the passed device register bit. Future open calls will be met
131 * with errors. Should be called before media_devnode_unregister() to avoid
132 * races with unregister and device file open calls.
133 *
134 * This function can safely be called if the device node has never been
135 * registered or has already been unregistered.
136 */
137void media_devnode_unregister_prepare(struct media_devnode *devnode);
138
139/**
Mauro Carvalho Chehabfe3c5652015-12-13 08:40:45 -0200140 * media_devnode_unregister - unregister a media device node
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300141 * @devnode: the device node to unregister
Mauro Carvalho Chehabfe3c5652015-12-13 08:40:45 -0200142 *
143 * This unregisters the passed device. Future open calls will be met with
144 * errors.
145 *
Shuah Khan6f0dd242016-06-10 14:37:23 -0300146 * Should be called after media_devnode_unregister_prepare()
Mauro Carvalho Chehabfe3c5652015-12-13 08:40:45 -0200147 */
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300148void media_devnode_unregister(struct media_devnode *devnode);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300149
Mauro Carvalho Chehab75c7e292015-12-13 09:00:00 -0200150/**
151 * media_devnode_data - returns a pointer to the &media_devnode
152 *
153 * @filp: pointer to struct &file
154 */
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300155static inline struct media_devnode *media_devnode_data(struct file *filp)
156{
157 return filp->private_data;
158}
159
Mauro Carvalho Chehab75c7e292015-12-13 09:00:00 -0200160/**
161 * media_devnode_is_registered - returns true if &media_devnode is registered;
162 * false otherwise.
163 *
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300164 * @devnode: pointer to struct &media_devnode.
Mauro Carvalho Chehaba087ce72016-04-27 19:28:26 -0300165 *
166 * Note: If mdev is NULL, it also returns false.
Mauro Carvalho Chehab75c7e292015-12-13 09:00:00 -0200167 */
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300168static inline int media_devnode_is_registered(struct media_devnode *devnode)
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300169{
Mauro Carvalho Chehaba087ce72016-04-27 19:28:26 -0300170 if (!devnode)
171 return false;
172
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300173 return test_bit(MEDIA_FLAG_REGISTERED, &devnode->flags);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300174}
175
176#endif /* _MEDIA_DEVNODE_H */