/* SPDX-License-Identifier: GPL-2.0-only */1/*2* Media device node3*4* Copyright (C) 2010 Nokia Corporation5*6* Contacts: Laurent Pinchart <[email protected]>7* Sakari Ailus <[email protected]>8*9* --10*11* Common functions for media-related drivers to register and unregister media12* device nodes.13*/1415#ifndef _MEDIA_DEVNODE_H16#define _MEDIA_DEVNODE_H1718#include <linux/poll.h>19#include <linux/fs.h>20#include <linux/device.h>21#include <linux/cdev.h>2223struct media_device;2425/*26* Flag to mark the media_devnode struct as registered. Drivers must not touch27* this flag directly, it will be set and cleared by media_devnode_register and28* media_devnode_unregister.29*/30#define MEDIA_FLAG_REGISTERED 03132/**33* struct media_file_operations - Media device file operations34*35* @owner: should be filled with %THIS_MODULE36* @read: pointer to the function that implements read() syscall37* @write: pointer to the function that implements write() syscall38* @poll: pointer to the function that implements poll() syscall39* @ioctl: pointer to the function that implements ioctl() syscall40* @compat_ioctl: pointer to the function that will handle 32 bits userspace41* calls to the ioctl() syscall on a Kernel compiled with 64 bits.42* @open: pointer to the function that implements open() syscall43* @release: pointer to the function that will release the resources allocated44* by the @open function.45*/46struct media_file_operations {47struct module *owner;48ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);49ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);50__poll_t (*poll) (struct file *, struct poll_table_struct *);51long (*ioctl) (struct file *, unsigned int, unsigned long);52long (*compat_ioctl) (struct file *, unsigned int, unsigned long);53int (*open) (struct file *);54int (*release) (struct file *);55};5657/**58* struct media_devnode - Media device node59* @media_dev: pointer to struct &media_device60* @fops: pointer to struct &media_file_operations with media device ops61* @dev: pointer to struct &device containing the media controller device62* @cdev: struct cdev pointer character device63* @parent: parent device64* @minor: device node minor number65* @flags: flags, combination of the ``MEDIA_FLAG_*`` constants66* @release: release callback called at the end of ``media_devnode_release()``67* routine at media-device.c.68*69* This structure represents a media-related device node.70*71* The @parent is a physical device. It must be set by core or device drivers72* before registering the node.73*/74struct media_devnode {75struct media_device *media_dev;7677/* device ops */78const struct media_file_operations *fops;7980/* sysfs */81struct device dev; /* media device */82struct cdev cdev; /* character device */83struct device *parent; /* device parent */8485/* device info */86int minor;87unsigned long flags; /* Use bitops to access flags */8889/* callbacks */90void (*release)(struct media_devnode *devnode);91};9293/* dev to media_devnode */94#define to_media_devnode(cd) container_of(cd, struct media_devnode, dev)9596/**97* media_devnode_register - register a media device node98*99* @mdev: struct media_device we want to register a device node100* @devnode: media device node structure we want to register101* @owner: should be filled with %THIS_MODULE102*103* The registration code assigns minor numbers and registers the new device node104* with the kernel. An error is returned if no free minor number can be found,105* or if the registration of the device node fails.106*107* Zero is returned on success.108*109* Note that if the media_devnode_register call fails, the release() callback of110* the media_devnode structure is *not* called, so the caller is responsible for111* freeing any data.112*/113int __must_check media_devnode_register(struct media_device *mdev,114struct media_devnode *devnode,115struct module *owner);116117/**118* media_devnode_unregister_prepare - clear the media device node register bit119* @devnode: the device node to prepare for unregister120*121* This clears the passed device register bit. Future open calls will be met122* with errors. Should be called before media_devnode_unregister() to avoid123* races with unregister and device file open calls.124*125* This function can safely be called if the device node has never been126* registered or has already been unregistered.127*/128void media_devnode_unregister_prepare(struct media_devnode *devnode);129130/**131* media_devnode_unregister - unregister a media device node132* @devnode: the device node to unregister133*134* This unregisters the passed device. Future open calls will be met with135* errors.136*137* Should be called after media_devnode_unregister_prepare()138*/139void media_devnode_unregister(struct media_devnode *devnode);140141/**142* media_devnode_data - returns a pointer to the &media_devnode143*144* @filp: pointer to struct &file145*/146static inline struct media_devnode *media_devnode_data(struct file *filp)147{148return filp->private_data;149}150151/**152* media_devnode_is_registered - returns true if &media_devnode is registered;153* false otherwise.154*155* @devnode: pointer to struct &media_devnode.156*157* Note: If mdev is NULL, it also returns false.158*/159static inline int media_devnode_is_registered(struct media_devnode *devnode)160{161if (!devnode)162return false;163164return test_bit(MEDIA_FLAG_REGISTERED, &devnode->flags);165}166167#endif /* _MEDIA_DEVNODE_H */168169170