1 /* SPDX-License-Identifier: GPL-2.0-only */ << 2 /* 1 /* 3 * Media device node 2 * Media device node 4 * 3 * 5 * Copyright (C) 2010 Nokia Corporation 4 * Copyright (C) 2010 Nokia Corporation 6 * 5 * 7 * Contacts: Laurent Pinchart <laurent.pinchar 6 * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com> 8 * Sakari Ailus <sakari.ailus@iki.fi 7 * Sakari Ailus <sakari.ailus@iki.fi> 9 * 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 * 10 * -- 18 * -- 11 * 19 * 12 * Common functions for media-related drivers 20 * Common functions for media-related drivers to register and unregister media 13 * device nodes. 21 * device nodes. 14 */ 22 */ 15 23 16 #ifndef _MEDIA_DEVNODE_H 24 #ifndef _MEDIA_DEVNODE_H 17 #define _MEDIA_DEVNODE_H 25 #define _MEDIA_DEVNODE_H 18 26 19 #include <linux/poll.h> 27 #include <linux/poll.h> 20 #include <linux/fs.h> 28 #include <linux/fs.h> 21 #include <linux/device.h> 29 #include <linux/device.h> 22 #include <linux/cdev.h> 30 #include <linux/cdev.h> 23 31 24 struct media_device; 32 struct media_device; 25 33 26 /* 34 /* 27 * Flag to mark the media_devnode struct as re 35 * Flag to mark the media_devnode struct as registered. Drivers must not touch 28 * this flag directly, it will be set and clea 36 * this flag directly, it will be set and cleared by media_devnode_register and 29 * media_devnode_unregister. 37 * media_devnode_unregister. 30 */ 38 */ 31 #define MEDIA_FLAG_REGISTERED 0 39 #define MEDIA_FLAG_REGISTERED 0 32 40 33 /** 41 /** 34 * struct media_file_operations - Media device 42 * struct media_file_operations - Media device file operations 35 * 43 * 36 * @owner: should be filled with %THIS_MODULE 44 * @owner: should be filled with %THIS_MODULE 37 * @read: pointer to the function that impleme 45 * @read: pointer to the function that implements read() syscall 38 * @write: pointer to the function that implem 46 * @write: pointer to the function that implements write() syscall 39 * @poll: pointer to the function that impleme 47 * @poll: pointer to the function that implements poll() syscall 40 * @ioctl: pointer to the function that implem 48 * @ioctl: pointer to the function that implements ioctl() syscall 41 * @compat_ioctl: pointer to the function that 49 * @compat_ioctl: pointer to the function that will handle 32 bits userspace 42 * calls to the ioctl() syscall on a Kern !! 50 * calls to the the ioctl() syscall on a Kernel compiled with 64 bits. 43 * @open: pointer to the function that impleme 51 * @open: pointer to the function that implements open() syscall 44 * @release: pointer to the function that will 52 * @release: pointer to the function that will release the resources allocated 45 * by the @open function. 53 * by the @open function. 46 */ 54 */ 47 struct media_file_operations { 55 struct media_file_operations { 48 struct module *owner; 56 struct module *owner; 49 ssize_t (*read) (struct file *, char _ 57 ssize_t (*read) (struct file *, char __user *, size_t, loff_t *); 50 ssize_t (*write) (struct file *, const 58 ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *); 51 __poll_t (*poll) (struct file *, struc 59 __poll_t (*poll) (struct file *, struct poll_table_struct *); 52 long (*ioctl) (struct file *, unsigned 60 long (*ioctl) (struct file *, unsigned int, unsigned long); 53 long (*compat_ioctl) (struct file *, u 61 long (*compat_ioctl) (struct file *, unsigned int, unsigned long); 54 int (*open) (struct file *); 62 int (*open) (struct file *); 55 int (*release) (struct file *); 63 int (*release) (struct file *); 56 }; 64 }; 57 65 58 /** 66 /** 59 * struct media_devnode - Media device node 67 * struct media_devnode - Media device node 60 * @media_dev: pointer to struct &media_devic 68 * @media_dev: pointer to struct &media_device 61 * @fops: pointer to struct &media_file_ 69 * @fops: pointer to struct &media_file_operations with media device ops 62 * @dev: pointer to struct &device cont 70 * @dev: pointer to struct &device containing the media controller device 63 * @cdev: struct cdev pointer character 71 * @cdev: struct cdev pointer character device 64 * @parent: parent device 72 * @parent: parent device 65 * @minor: device node minor number 73 * @minor: device node minor number 66 * @flags: flags, combination of the ``ME 74 * @flags: flags, combination of the ``MEDIA_FLAG_*`` constants 67 * @release: release callback called at the 75 * @release: release callback called at the end of ``media_devnode_release()`` 68 * routine at media-device.c. 76 * routine at media-device.c. 69 * 77 * 70 * This structure represents a media-related d 78 * This structure represents a media-related device node. 71 * 79 * 72 * The @parent is a physical device. It must b 80 * The @parent is a physical device. It must be set by core or device drivers 73 * before registering the node. 81 * before registering the node. 74 */ 82 */ 75 struct media_devnode { 83 struct media_devnode { 76 struct media_device *media_dev; 84 struct media_device *media_dev; 77 85 78 /* device ops */ 86 /* device ops */ 79 const struct media_file_operations *fo 87 const struct media_file_operations *fops; 80 88 81 /* sysfs */ 89 /* sysfs */ 82 struct device dev; /* med 90 struct device dev; /* media device */ 83 struct cdev cdev; /* cha 91 struct cdev cdev; /* character device */ 84 struct device *parent; /* dev 92 struct device *parent; /* device parent */ 85 93 86 /* device info */ 94 /* device info */ 87 int minor; 95 int minor; 88 unsigned long flags; /* Use 96 unsigned long flags; /* Use bitops to access flags */ 89 97 90 /* callbacks */ 98 /* callbacks */ 91 void (*release)(struct media_devnode * 99 void (*release)(struct media_devnode *devnode); 92 }; 100 }; 93 101 94 /* dev to media_devnode */ 102 /* dev to media_devnode */ 95 #define to_media_devnode(cd) container_of(cd, 103 #define to_media_devnode(cd) container_of(cd, struct media_devnode, dev) 96 104 97 /** 105 /** 98 * media_devnode_register - register a media d 106 * media_devnode_register - register a media device node 99 * 107 * 100 * @mdev: struct media_device we want to regis 108 * @mdev: struct media_device we want to register a device node 101 * @devnode: media device node structure we wa 109 * @devnode: media device node structure we want to register 102 * @owner: should be filled with %THIS_MODULE 110 * @owner: should be filled with %THIS_MODULE 103 * 111 * 104 * The registration code assigns minor numbers 112 * The registration code assigns minor numbers and registers the new device node 105 * with the kernel. An error is returned if no 113 * with the kernel. An error is returned if no free minor number can be found, 106 * or if the registration of the device node f 114 * or if the registration of the device node fails. 107 * 115 * 108 * Zero is returned on success. 116 * Zero is returned on success. 109 * 117 * 110 * Note that if the media_devnode_register cal 118 * Note that if the media_devnode_register call fails, the release() callback of 111 * the media_devnode structure is *not* called 119 * the media_devnode structure is *not* called, so the caller is responsible for 112 * freeing any data. 120 * freeing any data. 113 */ 121 */ 114 int __must_check media_devnode_register(struct 122 int __must_check media_devnode_register(struct media_device *mdev, 115 struct 123 struct media_devnode *devnode, 116 struct 124 struct module *owner); 117 125 118 /** 126 /** 119 * media_devnode_unregister_prepare - clear th 127 * media_devnode_unregister_prepare - clear the media device node register bit 120 * @devnode: the device node to prepare for un 128 * @devnode: the device node to prepare for unregister 121 * 129 * 122 * This clears the passed device register bit. 130 * This clears the passed device register bit. Future open calls will be met 123 * with errors. Should be called before media_ 131 * with errors. Should be called before media_devnode_unregister() to avoid 124 * races with unregister and device file open 132 * races with unregister and device file open calls. 125 * 133 * 126 * This function can safely be called if the d 134 * This function can safely be called if the device node has never been 127 * registered or has already been unregistered 135 * registered or has already been unregistered. 128 */ 136 */ 129 void media_devnode_unregister_prepare(struct m 137 void media_devnode_unregister_prepare(struct media_devnode *devnode); 130 138 131 /** 139 /** 132 * media_devnode_unregister - unregister a med 140 * media_devnode_unregister - unregister a media device node 133 * @devnode: the device node to unregister 141 * @devnode: the device node to unregister 134 * 142 * 135 * This unregisters the passed device. Future 143 * This unregisters the passed device. Future open calls will be met with 136 * errors. 144 * errors. 137 * 145 * 138 * Should be called after media_devnode_unregi 146 * Should be called after media_devnode_unregister_prepare() 139 */ 147 */ 140 void media_devnode_unregister(struct media_dev 148 void media_devnode_unregister(struct media_devnode *devnode); 141 149 142 /** 150 /** 143 * media_devnode_data - returns a pointer to t 151 * media_devnode_data - returns a pointer to the &media_devnode 144 * 152 * 145 * @filp: pointer to struct &file 153 * @filp: pointer to struct &file 146 */ 154 */ 147 static inline struct media_devnode *media_devn 155 static inline struct media_devnode *media_devnode_data(struct file *filp) 148 { 156 { 149 return filp->private_data; 157 return filp->private_data; 150 } 158 } 151 159 152 /** 160 /** 153 * media_devnode_is_registered - returns true 161 * media_devnode_is_registered - returns true if &media_devnode is registered; 154 * false otherwise. 162 * false otherwise. 155 * 163 * 156 * @devnode: pointer to struct &media_devnode. 164 * @devnode: pointer to struct &media_devnode. 157 * 165 * 158 * Note: If mdev is NULL, it also returns fals 166 * Note: If mdev is NULL, it also returns false. 159 */ 167 */ 160 static inline int media_devnode_is_registered( 168 static inline int media_devnode_is_registered(struct media_devnode *devnode) 161 { 169 { 162 if (!devnode) 170 if (!devnode) 163 return false; 171 return false; 164 172 165 return test_bit(MEDIA_FLAG_REGISTERED, 173 return test_bit(MEDIA_FLAG_REGISTERED, &devnode->flags); 166 } 174 } 167 175 168 #endif /* _MEDIA_DEVNODE_H */ 176 #endif /* _MEDIA_DEVNODE_H */ 169 177
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.