blob: 6393842c6b2188cbf1174508e9d390518dd2b4f5 [file] [log] [blame]
Thomas Gleixner1802d0b2019-05-27 08:55:21 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Laurent Pinchartcf4b9212009-12-09 08:39:56 -03002/*
3 * Media device node
4 *
5 * Copyright (C) 2010 Nokia Corporation
6 *
7 * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
8 * Sakari Ailus <sakari.ailus@iki.fi>
9 *
Laurent Pinchartcf4b9212009-12-09 08:39:56 -030010 * --
11 *
12 * Common functions for media-related drivers to register and unregister media
13 * device nodes.
14 */
15
16#ifndef _MEDIA_DEVNODE_H
17#define _MEDIA_DEVNODE_H
18
19#include <linux/poll.h>
20#include <linux/fs.h>
21#include <linux/device.h>
22#include <linux/cdev.h>
23
Mauro Carvalho Chehaba087ce72016-04-27 19:28:26 -030024struct media_device;
25
Laurent Pinchartcf4b9212009-12-09 08:39:56 -030026/*
27 * Flag to mark the media_devnode struct as registered. Drivers must not touch
28 * this flag directly, it will be set and cleared by media_devnode_register and
29 * media_devnode_unregister.
30 */
31#define MEDIA_FLAG_REGISTERED 0
32
Mauro Carvalho Chehab75c7e292015-12-13 09:00:00 -020033/**
34 * struct media_file_operations - Media device file operations
35 *
36 * @owner: should be filled with %THIS_MODULE
37 * @read: pointer to the function that implements read() syscall
38 * @write: pointer to the function that implements write() syscall
39 * @poll: pointer to the function that implements poll() syscall
40 * @ioctl: pointer to the function that implements ioctl() syscall
41 * @compat_ioctl: pointer to the function that will handle 32 bits userspace
42 * calls to the the ioctl() syscall on a Kernel compiled with 64 bits.
43 * @open: pointer to the function that implements open() syscall
44 * @release: pointer to the function that will release the resources allocated
45 * by the @open function.
46 */
Laurent Pinchartcf4b9212009-12-09 08:39:56 -030047struct media_file_operations {
48 struct module *owner;
49 ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
50 ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
Al Viroa3f86832017-07-02 22:22:01 -040051 __poll_t (*poll) (struct file *, struct poll_table_struct *);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -030052 long (*ioctl) (struct file *, unsigned int, unsigned long);
Sakari Ailusc6c1d502013-01-22 12:27:55 -030053 long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -030054 int (*open) (struct file *);
55 int (*release) (struct file *);
56};
57
58/**
59 * struct media_devnode - Media device node
Mauro Carvalho Chehab0db5c792016-06-16 08:04:40 -030060 * @media_dev: pointer to struct &media_device
Mauro Carvalho Chehab75c7e292015-12-13 09:00:00 -020061 * @fops: pointer to struct &media_file_operations with media device ops
Mauro Carvalho Chehab0db5c792016-06-16 08:04:40 -030062 * @dev: pointer to struct &device containing the media controller device
Mauro Carvalho Chehabec0255c2015-08-22 04:45:03 -030063 * @cdev: struct cdev pointer character device
Laurent Pinchartcf4b9212009-12-09 08:39:56 -030064 * @parent: parent device
65 * @minor: device node minor number
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -030066 * @flags: flags, combination of the ``MEDIA_FLAG_*`` constants
Mauro Carvalho Chehab82631b52016-09-08 09:57:24 -030067 * @release: release callback called at the end of ``media_devnode_release()``
68 * routine at media-device.c.
Laurent Pinchartcf4b9212009-12-09 08:39:56 -030069 *
70 * This structure represents a media-related device node.
71 *
72 * The @parent is a physical device. It must be set by core or device drivers
73 * before registering the node.
74 */
75struct media_devnode {
Mauro Carvalho Chehaba087ce72016-04-27 19:28:26 -030076 struct media_device *media_dev;
77
Laurent Pinchartcf4b9212009-12-09 08:39:56 -030078 /* device ops */
79 const struct media_file_operations *fops;
80
81 /* sysfs */
82 struct device dev; /* media device */
83 struct cdev cdev; /* character device */
84 struct device *parent; /* device parent */
85
86 /* device info */
87 int minor;
88 unsigned long flags; /* Use bitops to access flags */
89
90 /* callbacks */
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -030091 void (*release)(struct media_devnode *devnode);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -030092};
93
94/* dev to media_devnode */
95#define to_media_devnode(cd) container_of(cd, struct media_devnode, dev)
96
Mauro Carvalho Chehabfe3c5652015-12-13 08:40:45 -020097/**
98 * media_devnode_register - register a media device node
99 *
Mauro Carvalho Chehab0db5c792016-06-16 08:04:40 -0300100 * @mdev: struct media_device we want to register a device node
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300101 * @devnode: media device node structure we want to register
Mauro Carvalho Chehabfe3c5652015-12-13 08:40:45 -0200102 * @owner: should be filled with %THIS_MODULE
103 *
104 * The registration code assigns minor numbers and registers the new device node
105 * with the kernel. An error is returned if no free minor number can be found,
106 * or if the registration of the device node fails.
107 *
108 * Zero is returned on success.
109 *
110 * Note that if the media_devnode_register call fails, the release() callback of
111 * the media_devnode structure is *not* called, so the caller is responsible for
112 * freeing any data.
113 */
Mauro Carvalho Chehaba087ce72016-04-27 19:28:26 -0300114int __must_check media_devnode_register(struct media_device *mdev,
115 struct media_devnode *devnode,
Sakari Ailus85de7212013-12-12 12:38:17 -0300116 struct module *owner);
Mauro Carvalho Chehabfe3c5652015-12-13 08:40:45 -0200117
118/**
Shuah Khan6f0dd242016-06-10 14:37:23 -0300119 * media_devnode_unregister_prepare - clear the media device node register bit
120 * @devnode: the device node to prepare for unregister
121 *
122 * This clears the passed device register bit. Future open calls will be met
123 * with errors. Should be called before media_devnode_unregister() to avoid
124 * races with unregister and device file open calls.
125 *
126 * This function can safely be called if the device node has never been
127 * registered or has already been unregistered.
128 */
129void media_devnode_unregister_prepare(struct media_devnode *devnode);
130
131/**
Mauro Carvalho Chehabfe3c5652015-12-13 08:40:45 -0200132 * media_devnode_unregister - unregister a media device node
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300133 * @devnode: the device node to unregister
Mauro Carvalho Chehabfe3c5652015-12-13 08:40:45 -0200134 *
135 * This unregisters the passed device. Future open calls will be met with
136 * errors.
137 *
Shuah Khan6f0dd242016-06-10 14:37:23 -0300138 * Should be called after media_devnode_unregister_prepare()
Mauro Carvalho Chehabfe3c5652015-12-13 08:40:45 -0200139 */
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300140void media_devnode_unregister(struct media_devnode *devnode);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300141
Mauro Carvalho Chehab75c7e292015-12-13 09:00:00 -0200142/**
143 * media_devnode_data - returns a pointer to the &media_devnode
144 *
145 * @filp: pointer to struct &file
146 */
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300147static inline struct media_devnode *media_devnode_data(struct file *filp)
148{
149 return filp->private_data;
150}
151
Mauro Carvalho Chehab75c7e292015-12-13 09:00:00 -0200152/**
153 * media_devnode_is_registered - returns true if &media_devnode is registered;
154 * false otherwise.
155 *
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300156 * @devnode: pointer to struct &media_devnode.
Mauro Carvalho Chehaba087ce72016-04-27 19:28:26 -0300157 *
158 * Note: If mdev is NULL, it also returns false.
Mauro Carvalho Chehab75c7e292015-12-13 09:00:00 -0200159 */
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300160static inline int media_devnode_is_registered(struct media_devnode *devnode)
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300161{
Mauro Carvalho Chehaba087ce72016-04-27 19:28:26 -0300162 if (!devnode)
163 return false;
164
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300165 return test_bit(MEDIA_FLAG_REGISTERED, &devnode->flags);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300166}
167
168#endif /* _MEDIA_DEVNODE_H */