162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Media device node
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (C) 2010 Nokia Corporation
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
862306a36Sopenharmony_ci *	     Sakari Ailus <sakari.ailus@iki.fi>
962306a36Sopenharmony_ci *
1062306a36Sopenharmony_ci * --
1162306a36Sopenharmony_ci *
1262306a36Sopenharmony_ci * Common functions for media-related drivers to register and unregister media
1362306a36Sopenharmony_ci * device nodes.
1462306a36Sopenharmony_ci */
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci#ifndef _MEDIA_DEVNODE_H
1762306a36Sopenharmony_ci#define _MEDIA_DEVNODE_H
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ci#include <linux/poll.h>
2062306a36Sopenharmony_ci#include <linux/fs.h>
2162306a36Sopenharmony_ci#include <linux/device.h>
2262306a36Sopenharmony_ci#include <linux/cdev.h>
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_cistruct media_device;
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci/*
2762306a36Sopenharmony_ci * Flag to mark the media_devnode struct as registered. Drivers must not touch
2862306a36Sopenharmony_ci * this flag directly, it will be set and cleared by media_devnode_register and
2962306a36Sopenharmony_ci * media_devnode_unregister.
3062306a36Sopenharmony_ci */
3162306a36Sopenharmony_ci#define MEDIA_FLAG_REGISTERED	0
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci/**
3462306a36Sopenharmony_ci * struct media_file_operations - Media device file operations
3562306a36Sopenharmony_ci *
3662306a36Sopenharmony_ci * @owner: should be filled with %THIS_MODULE
3762306a36Sopenharmony_ci * @read: pointer to the function that implements read() syscall
3862306a36Sopenharmony_ci * @write: pointer to the function that implements write() syscall
3962306a36Sopenharmony_ci * @poll: pointer to the function that implements poll() syscall
4062306a36Sopenharmony_ci * @ioctl: pointer to the function that implements ioctl() syscall
4162306a36Sopenharmony_ci * @compat_ioctl: pointer to the function that will handle 32 bits userspace
4262306a36Sopenharmony_ci *	calls to the ioctl() syscall on a Kernel compiled with 64 bits.
4362306a36Sopenharmony_ci * @open: pointer to the function that implements open() syscall
4462306a36Sopenharmony_ci * @release: pointer to the function that will release the resources allocated
4562306a36Sopenharmony_ci *	by the @open function.
4662306a36Sopenharmony_ci */
4762306a36Sopenharmony_cistruct media_file_operations {
4862306a36Sopenharmony_ci	struct module *owner;
4962306a36Sopenharmony_ci	ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
5062306a36Sopenharmony_ci	ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
5162306a36Sopenharmony_ci	__poll_t (*poll) (struct file *, struct poll_table_struct *);
5262306a36Sopenharmony_ci	long (*ioctl) (struct file *, unsigned int, unsigned long);
5362306a36Sopenharmony_ci	long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
5462306a36Sopenharmony_ci	int (*open) (struct file *);
5562306a36Sopenharmony_ci	int (*release) (struct file *);
5662306a36Sopenharmony_ci};
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci/**
5962306a36Sopenharmony_ci * struct media_devnode - Media device node
6062306a36Sopenharmony_ci * @media_dev:	pointer to struct &media_device
6162306a36Sopenharmony_ci * @fops:	pointer to struct &media_file_operations with media device ops
6262306a36Sopenharmony_ci * @dev:	pointer to struct &device containing the media controller device
6362306a36Sopenharmony_ci * @cdev:	struct cdev pointer character device
6462306a36Sopenharmony_ci * @parent:	parent device
6562306a36Sopenharmony_ci * @minor:	device node minor number
6662306a36Sopenharmony_ci * @flags:	flags, combination of the ``MEDIA_FLAG_*`` constants
6762306a36Sopenharmony_ci * @release:	release callback called at the end of ``media_devnode_release()``
6862306a36Sopenharmony_ci *		routine at media-device.c.
6962306a36Sopenharmony_ci *
7062306a36Sopenharmony_ci * This structure represents a media-related device node.
7162306a36Sopenharmony_ci *
7262306a36Sopenharmony_ci * The @parent is a physical device. It must be set by core or device drivers
7362306a36Sopenharmony_ci * before registering the node.
7462306a36Sopenharmony_ci */
7562306a36Sopenharmony_cistruct media_devnode {
7662306a36Sopenharmony_ci	struct media_device *media_dev;
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_ci	/* device ops */
7962306a36Sopenharmony_ci	const struct media_file_operations *fops;
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ci	/* sysfs */
8262306a36Sopenharmony_ci	struct device dev;		/* media device */
8362306a36Sopenharmony_ci	struct cdev cdev;		/* character device */
8462306a36Sopenharmony_ci	struct device *parent;		/* device parent */
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ci	/* device info */
8762306a36Sopenharmony_ci	int minor;
8862306a36Sopenharmony_ci	unsigned long flags;		/* Use bitops to access flags */
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci	/* callbacks */
9162306a36Sopenharmony_ci	void (*release)(struct media_devnode *devnode);
9262306a36Sopenharmony_ci};
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci/* dev to media_devnode */
9562306a36Sopenharmony_ci#define to_media_devnode(cd) container_of(cd, struct media_devnode, dev)
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ci/**
9862306a36Sopenharmony_ci * media_devnode_register - register a media device node
9962306a36Sopenharmony_ci *
10062306a36Sopenharmony_ci * @mdev: struct media_device we want to register a device node
10162306a36Sopenharmony_ci * @devnode: media device node structure we want to register
10262306a36Sopenharmony_ci * @owner: should be filled with %THIS_MODULE
10362306a36Sopenharmony_ci *
10462306a36Sopenharmony_ci * The registration code assigns minor numbers and registers the new device node
10562306a36Sopenharmony_ci * with the kernel. An error is returned if no free minor number can be found,
10662306a36Sopenharmony_ci * or if the registration of the device node fails.
10762306a36Sopenharmony_ci *
10862306a36Sopenharmony_ci * Zero is returned on success.
10962306a36Sopenharmony_ci *
11062306a36Sopenharmony_ci * Note that if the media_devnode_register call fails, the release() callback of
11162306a36Sopenharmony_ci * the media_devnode structure is *not* called, so the caller is responsible for
11262306a36Sopenharmony_ci * freeing any data.
11362306a36Sopenharmony_ci */
11462306a36Sopenharmony_ciint __must_check media_devnode_register(struct media_device *mdev,
11562306a36Sopenharmony_ci					struct media_devnode *devnode,
11662306a36Sopenharmony_ci					struct module *owner);
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_ci/**
11962306a36Sopenharmony_ci * media_devnode_unregister_prepare - clear the media device node register bit
12062306a36Sopenharmony_ci * @devnode: the device node to prepare for unregister
12162306a36Sopenharmony_ci *
12262306a36Sopenharmony_ci * This clears the passed device register bit. Future open calls will be met
12362306a36Sopenharmony_ci * with errors. Should be called before media_devnode_unregister() to avoid
12462306a36Sopenharmony_ci * races with unregister and device file open calls.
12562306a36Sopenharmony_ci *
12662306a36Sopenharmony_ci * This function can safely be called if the device node has never been
12762306a36Sopenharmony_ci * registered or has already been unregistered.
12862306a36Sopenharmony_ci */
12962306a36Sopenharmony_civoid media_devnode_unregister_prepare(struct media_devnode *devnode);
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_ci/**
13262306a36Sopenharmony_ci * media_devnode_unregister - unregister a media device node
13362306a36Sopenharmony_ci * @devnode: the device node to unregister
13462306a36Sopenharmony_ci *
13562306a36Sopenharmony_ci * This unregisters the passed device. Future open calls will be met with
13662306a36Sopenharmony_ci * errors.
13762306a36Sopenharmony_ci *
13862306a36Sopenharmony_ci * Should be called after media_devnode_unregister_prepare()
13962306a36Sopenharmony_ci */
14062306a36Sopenharmony_civoid media_devnode_unregister(struct media_devnode *devnode);
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_ci/**
14362306a36Sopenharmony_ci * media_devnode_data - returns a pointer to the &media_devnode
14462306a36Sopenharmony_ci *
14562306a36Sopenharmony_ci * @filp: pointer to struct &file
14662306a36Sopenharmony_ci */
14762306a36Sopenharmony_cistatic inline struct media_devnode *media_devnode_data(struct file *filp)
14862306a36Sopenharmony_ci{
14962306a36Sopenharmony_ci	return filp->private_data;
15062306a36Sopenharmony_ci}
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_ci/**
15362306a36Sopenharmony_ci * media_devnode_is_registered - returns true if &media_devnode is registered;
15462306a36Sopenharmony_ci *	false otherwise.
15562306a36Sopenharmony_ci *
15662306a36Sopenharmony_ci * @devnode: pointer to struct &media_devnode.
15762306a36Sopenharmony_ci *
15862306a36Sopenharmony_ci * Note: If mdev is NULL, it also returns false.
15962306a36Sopenharmony_ci */
16062306a36Sopenharmony_cistatic inline int media_devnode_is_registered(struct media_devnode *devnode)
16162306a36Sopenharmony_ci{
16262306a36Sopenharmony_ci	if (!devnode)
16362306a36Sopenharmony_ci		return false;
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ci	return test_bit(MEDIA_FLAG_REGISTERED, &devnode->flags);
16662306a36Sopenharmony_ci}
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_ci#endif /* _MEDIA_DEVNODE_H */
169