Commit | Line | Data |
---|---|---|
1802d0be | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
cf4b9211 LP |
2 | /* |
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 | * | |
cf4b9211 LP |
10 | * -- |
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 | ||
a087ce70 MCC |
24 | struct media_device; |
25 | ||
cf4b9211 LP |
26 | /* |
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 | ||
75c7e295 MCC |
33 | /** |
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 | */ | |
cf4b9211 LP |
47 | struct 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 *); | |
a3f8683b | 51 | __poll_t (*poll) (struct file *, struct poll_table_struct *); |
cf4b9211 | 52 | long (*ioctl) (struct file *, unsigned int, unsigned long); |
c6c1d50b | 53 | long (*compat_ioctl) (struct file *, unsigned int, unsigned long); |
cf4b9211 LP |
54 | int (*open) (struct file *); |
55 | int (*release) (struct file *); | |
56 | }; | |
57 | ||
58 | /** | |
59 | * struct media_devnode - Media device node | |
0db5c799 | 60 | * @media_dev: pointer to struct &media_device |
75c7e295 | 61 | * @fops: pointer to struct &media_file_operations with media device ops |
0db5c799 | 62 | * @dev: pointer to struct &device containing the media controller device |
ec0255ca | 63 | * @cdev: struct cdev pointer character device |
cf4b9211 LP |
64 | * @parent: parent device |
65 | * @minor: device node minor number | |
48a7c4ba | 66 | * @flags: flags, combination of the ``MEDIA_FLAG_*`` constants |
82631b5b MCC |
67 | * @release: release callback called at the end of ``media_devnode_release()`` |
68 | * routine at media-device.c. | |
cf4b9211 LP |
69 | * |
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 | */ | |
75 | struct media_devnode { | |
a087ce70 MCC |
76 | struct media_device *media_dev; |
77 | ||
cf4b9211 LP |
78 | /* 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 */ | |
163f1e93 | 91 | void (*release)(struct media_devnode *devnode); |
cf4b9211 LP |
92 | }; |
93 | ||
94 | /* dev to media_devnode */ | |
95 | #define to_media_devnode(cd) container_of(cd, struct media_devnode, dev) | |
96 | ||
fe3c565e MCC |
97 | /** |
98 | * media_devnode_register - register a media device node | |
99 | * | |
0db5c799 | 100 | * @mdev: struct media_device we want to register a device node |
163f1e93 | 101 | * @devnode: media device node structure we want to register |
fe3c565e MCC |
102 | * @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 | */ | |
a087ce70 MCC |
114 | int __must_check media_devnode_register(struct media_device *mdev, |
115 | struct media_devnode *devnode, | |
85de721c | 116 | struct module *owner); |
fe3c565e | 117 | |
6f0dd24a SK |
118 | /** |
119 | * 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 | */ | |
129 | void media_devnode_unregister_prepare(struct media_devnode *devnode); | |
130 | ||
fe3c565e MCC |
131 | /** |
132 | * media_devnode_unregister - unregister a media device node | |
163f1e93 | 133 | * @devnode: the device node to unregister |
fe3c565e MCC |
134 | * |
135 | * This unregisters the passed device. Future open calls will be met with | |
136 | * errors. | |
137 | * | |
6f0dd24a | 138 | * Should be called after media_devnode_unregister_prepare() |
fe3c565e | 139 | */ |
163f1e93 | 140 | void media_devnode_unregister(struct media_devnode *devnode); |
cf4b9211 | 141 | |
75c7e295 MCC |
142 | /** |
143 | * media_devnode_data - returns a pointer to the &media_devnode | |
144 | * | |
145 | * @filp: pointer to struct &file | |
146 | */ | |
cf4b9211 LP |
147 | static inline struct media_devnode *media_devnode_data(struct file *filp) |
148 | { | |
149 | return filp->private_data; | |
150 | } | |
151 | ||
75c7e295 MCC |
152 | /** |
153 | * media_devnode_is_registered - returns true if &media_devnode is registered; | |
154 | * false otherwise. | |
155 | * | |
163f1e93 | 156 | * @devnode: pointer to struct &media_devnode. |
a087ce70 MCC |
157 | * |
158 | * Note: If mdev is NULL, it also returns false. | |
75c7e295 | 159 | */ |
163f1e93 | 160 | static inline int media_devnode_is_registered(struct media_devnode *devnode) |
cf4b9211 | 161 | { |
a087ce70 MCC |
162 | if (!devnode) |
163 | return false; | |
164 | ||
163f1e93 | 165 | return test_bit(MEDIA_FLAG_REGISTERED, &devnode->flags); |
cf4b9211 LP |
166 | } |
167 | ||
168 | #endif /* _MEDIA_DEVNODE_H */ |