Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 1 | /* |
| 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 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | * |
| 22 | * -- |
| 23 | * |
| 24 | * Common functions for media-related drivers to register and unregister media |
| 25 | * device nodes. |
| 26 | */ |
| 27 | |
| 28 | #ifndef _MEDIA_DEVNODE_H |
| 29 | #define _MEDIA_DEVNODE_H |
| 30 | |
| 31 | #include <linux/poll.h> |
| 32 | #include <linux/fs.h> |
| 33 | #include <linux/device.h> |
| 34 | #include <linux/cdev.h> |
| 35 | |
Mauro Carvalho Chehab | a087ce7 | 2016-04-27 19:28:26 -0300 | [diff] [blame] | 36 | struct media_device; |
| 37 | |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 38 | /* |
| 39 | * Flag to mark the media_devnode struct as registered. Drivers must not touch |
| 40 | * this flag directly, it will be set and cleared by media_devnode_register and |
| 41 | * media_devnode_unregister. |
| 42 | */ |
| 43 | #define MEDIA_FLAG_REGISTERED 0 |
| 44 | |
Mauro Carvalho Chehab | 75c7e29 | 2015-12-13 09:00:00 -0200 | [diff] [blame] | 45 | /** |
| 46 | * struct media_file_operations - Media device file operations |
| 47 | * |
| 48 | * @owner: should be filled with %THIS_MODULE |
| 49 | * @read: pointer to the function that implements read() syscall |
| 50 | * @write: pointer to the function that implements write() syscall |
| 51 | * @poll: pointer to the function that implements poll() syscall |
| 52 | * @ioctl: pointer to the function that implements ioctl() syscall |
| 53 | * @compat_ioctl: pointer to the function that will handle 32 bits userspace |
| 54 | * calls to the the ioctl() syscall on a Kernel compiled with 64 bits. |
| 55 | * @open: pointer to the function that implements open() syscall |
| 56 | * @release: pointer to the function that will release the resources allocated |
| 57 | * by the @open function. |
| 58 | */ |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 59 | struct media_file_operations { |
| 60 | struct module *owner; |
| 61 | ssize_t (*read) (struct file *, char __user *, size_t, loff_t *); |
| 62 | ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *); |
| 63 | unsigned int (*poll) (struct file *, struct poll_table_struct *); |
| 64 | long (*ioctl) (struct file *, unsigned int, unsigned long); |
Sakari Ailus | c6c1d50 | 2013-01-22 12:27:55 -0300 | [diff] [blame] | 65 | long (*compat_ioctl) (struct file *, unsigned int, unsigned long); |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 66 | int (*open) (struct file *); |
| 67 | int (*release) (struct file *); |
| 68 | }; |
| 69 | |
| 70 | /** |
| 71 | * struct media_devnode - Media device node |
Mauro Carvalho Chehab | 0db5c79 | 2016-06-16 08:04:40 -0300 | [diff] [blame] | 72 | * @media_dev: pointer to struct &media_device |
Mauro Carvalho Chehab | 75c7e29 | 2015-12-13 09:00:00 -0200 | [diff] [blame] | 73 | * @fops: pointer to struct &media_file_operations with media device ops |
Mauro Carvalho Chehab | 0db5c79 | 2016-06-16 08:04:40 -0300 | [diff] [blame] | 74 | * @dev: pointer to struct &device containing the media controller device |
Mauro Carvalho Chehab | ec0255c | 2015-08-22 04:45:03 -0300 | [diff] [blame] | 75 | * @cdev: struct cdev pointer character device |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 76 | * @parent: parent device |
| 77 | * @minor: device node minor number |
Mauro Carvalho Chehab | 48a7c4b | 2016-08-29 16:09:11 -0300 | [diff] [blame] | 78 | * @flags: flags, combination of the ``MEDIA_FLAG_*`` constants |
Mauro Carvalho Chehab | 82631b5 | 2016-09-08 09:57:24 -0300 | [diff] [blame] | 79 | * @release: release callback called at the end of ``media_devnode_release()`` |
| 80 | * routine at media-device.c. |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 81 | * |
| 82 | * This structure represents a media-related device node. |
| 83 | * |
| 84 | * The @parent is a physical device. It must be set by core or device drivers |
| 85 | * before registering the node. |
| 86 | */ |
| 87 | struct media_devnode { |
Mauro Carvalho Chehab | a087ce7 | 2016-04-27 19:28:26 -0300 | [diff] [blame] | 88 | struct media_device *media_dev; |
| 89 | |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 90 | /* device ops */ |
| 91 | const struct media_file_operations *fops; |
| 92 | |
| 93 | /* sysfs */ |
| 94 | struct device dev; /* media device */ |
| 95 | struct cdev cdev; /* character device */ |
| 96 | struct device *parent; /* device parent */ |
| 97 | |
| 98 | /* device info */ |
| 99 | int minor; |
| 100 | unsigned long flags; /* Use bitops to access flags */ |
| 101 | |
| 102 | /* callbacks */ |
Mauro Carvalho Chehab | 163f1e9 | 2016-03-23 11:22:57 -0300 | [diff] [blame] | 103 | void (*release)(struct media_devnode *devnode); |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | /* dev to media_devnode */ |
| 107 | #define to_media_devnode(cd) container_of(cd, struct media_devnode, dev) |
| 108 | |
Mauro Carvalho Chehab | fe3c565 | 2015-12-13 08:40:45 -0200 | [diff] [blame] | 109 | /** |
| 110 | * media_devnode_register - register a media device node |
| 111 | * |
Mauro Carvalho Chehab | 0db5c79 | 2016-06-16 08:04:40 -0300 | [diff] [blame] | 112 | * @mdev: struct media_device we want to register a device node |
Mauro Carvalho Chehab | 163f1e9 | 2016-03-23 11:22:57 -0300 | [diff] [blame] | 113 | * @devnode: media device node structure we want to register |
Mauro Carvalho Chehab | fe3c565 | 2015-12-13 08:40:45 -0200 | [diff] [blame] | 114 | * @owner: should be filled with %THIS_MODULE |
| 115 | * |
| 116 | * The registration code assigns minor numbers and registers the new device node |
| 117 | * with the kernel. An error is returned if no free minor number can be found, |
| 118 | * or if the registration of the device node fails. |
| 119 | * |
| 120 | * Zero is returned on success. |
| 121 | * |
| 122 | * Note that if the media_devnode_register call fails, the release() callback of |
| 123 | * the media_devnode structure is *not* called, so the caller is responsible for |
| 124 | * freeing any data. |
| 125 | */ |
Mauro Carvalho Chehab | a087ce7 | 2016-04-27 19:28:26 -0300 | [diff] [blame] | 126 | int __must_check media_devnode_register(struct media_device *mdev, |
| 127 | struct media_devnode *devnode, |
Sakari Ailus | 85de721 | 2013-12-12 12:38:17 -0300 | [diff] [blame] | 128 | struct module *owner); |
Mauro Carvalho Chehab | fe3c565 | 2015-12-13 08:40:45 -0200 | [diff] [blame] | 129 | |
| 130 | /** |
Shuah Khan | 6f0dd24 | 2016-06-10 14:37:23 -0300 | [diff] [blame] | 131 | * media_devnode_unregister_prepare - clear the media device node register bit |
| 132 | * @devnode: the device node to prepare for unregister |
| 133 | * |
| 134 | * This clears the passed device register bit. Future open calls will be met |
| 135 | * with errors. Should be called before media_devnode_unregister() to avoid |
| 136 | * races with unregister and device file open calls. |
| 137 | * |
| 138 | * This function can safely be called if the device node has never been |
| 139 | * registered or has already been unregistered. |
| 140 | */ |
| 141 | void media_devnode_unregister_prepare(struct media_devnode *devnode); |
| 142 | |
| 143 | /** |
Mauro Carvalho Chehab | fe3c565 | 2015-12-13 08:40:45 -0200 | [diff] [blame] | 144 | * media_devnode_unregister - unregister a media device node |
Mauro Carvalho Chehab | 163f1e9 | 2016-03-23 11:22:57 -0300 | [diff] [blame] | 145 | * @devnode: the device node to unregister |
Mauro Carvalho Chehab | fe3c565 | 2015-12-13 08:40:45 -0200 | [diff] [blame] | 146 | * |
| 147 | * This unregisters the passed device. Future open calls will be met with |
| 148 | * errors. |
| 149 | * |
Shuah Khan | 6f0dd24 | 2016-06-10 14:37:23 -0300 | [diff] [blame] | 150 | * Should be called after media_devnode_unregister_prepare() |
Mauro Carvalho Chehab | fe3c565 | 2015-12-13 08:40:45 -0200 | [diff] [blame] | 151 | */ |
Mauro Carvalho Chehab | 163f1e9 | 2016-03-23 11:22:57 -0300 | [diff] [blame] | 152 | void media_devnode_unregister(struct media_devnode *devnode); |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 153 | |
Mauro Carvalho Chehab | 75c7e29 | 2015-12-13 09:00:00 -0200 | [diff] [blame] | 154 | /** |
| 155 | * media_devnode_data - returns a pointer to the &media_devnode |
| 156 | * |
| 157 | * @filp: pointer to struct &file |
| 158 | */ |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 159 | static inline struct media_devnode *media_devnode_data(struct file *filp) |
| 160 | { |
| 161 | return filp->private_data; |
| 162 | } |
| 163 | |
Mauro Carvalho Chehab | 75c7e29 | 2015-12-13 09:00:00 -0200 | [diff] [blame] | 164 | /** |
| 165 | * media_devnode_is_registered - returns true if &media_devnode is registered; |
| 166 | * false otherwise. |
| 167 | * |
Mauro Carvalho Chehab | 163f1e9 | 2016-03-23 11:22:57 -0300 | [diff] [blame] | 168 | * @devnode: pointer to struct &media_devnode. |
Mauro Carvalho Chehab | a087ce7 | 2016-04-27 19:28:26 -0300 | [diff] [blame] | 169 | * |
| 170 | * Note: If mdev is NULL, it also returns false. |
Mauro Carvalho Chehab | 75c7e29 | 2015-12-13 09:00:00 -0200 | [diff] [blame] | 171 | */ |
Mauro Carvalho Chehab | 163f1e9 | 2016-03-23 11:22:57 -0300 | [diff] [blame] | 172 | static inline int media_devnode_is_registered(struct media_devnode *devnode) |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 173 | { |
Mauro Carvalho Chehab | a087ce7 | 2016-04-27 19:28:26 -0300 | [diff] [blame] | 174 | if (!devnode) |
| 175 | return false; |
| 176 | |
Mauro Carvalho Chehab | 163f1e9 | 2016-03-23 11:22:57 -0300 | [diff] [blame] | 177 | return test_bit(MEDIA_FLAG_REGISTERED, &devnode->flags); |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | #endif /* _MEDIA_DEVNODE_H */ |