Merge tag 'io_uring-5.11-2021-01-16' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / gpu / drm / drm_dp_aux_dev.c
blobe25181bf2c480c940e53921ebd8cf94b10dae7ad
1 /*
2 * Copyright © 2015 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
23 * Authors:
24 * Rafael Antognolli <rafael.antognolli@intel.com>
28 #include <linux/device.h>
29 #include <linux/fs.h>
30 #include <linux/init.h>
31 #include <linux/kernel.h>
32 #include <linux/module.h>
33 #include <linux/sched/signal.h>
34 #include <linux/slab.h>
35 #include <linux/uaccess.h>
36 #include <linux/uio.h>
38 #include <drm/drm_crtc.h>
39 #include <drm/drm_dp_helper.h>
40 #include <drm/drm_dp_mst_helper.h>
41 #include <drm/drm_print.h>
43 #include "drm_crtc_helper_internal.h"
45 struct drm_dp_aux_dev {
46 unsigned index;
47 struct drm_dp_aux *aux;
48 struct device *dev;
49 struct kref refcount;
50 atomic_t usecount;
53 #define DRM_AUX_MINORS 256
54 #define AUX_MAX_OFFSET (1 << 20)
55 static DEFINE_IDR(aux_idr);
56 static DEFINE_MUTEX(aux_idr_mutex);
57 static struct class *drm_dp_aux_dev_class;
58 static int drm_dev_major = -1;
60 static struct drm_dp_aux_dev *drm_dp_aux_dev_get_by_minor(unsigned index)
62 struct drm_dp_aux_dev *aux_dev = NULL;
64 mutex_lock(&aux_idr_mutex);
65 aux_dev = idr_find(&aux_idr, index);
66 if (aux_dev && !kref_get_unless_zero(&aux_dev->refcount))
67 aux_dev = NULL;
68 mutex_unlock(&aux_idr_mutex);
70 return aux_dev;
73 static struct drm_dp_aux_dev *alloc_drm_dp_aux_dev(struct drm_dp_aux *aux)
75 struct drm_dp_aux_dev *aux_dev;
76 int index;
78 aux_dev = kzalloc(sizeof(*aux_dev), GFP_KERNEL);
79 if (!aux_dev)
80 return ERR_PTR(-ENOMEM);
81 aux_dev->aux = aux;
82 atomic_set(&aux_dev->usecount, 1);
83 kref_init(&aux_dev->refcount);
85 mutex_lock(&aux_idr_mutex);
86 index = idr_alloc(&aux_idr, aux_dev, 0, DRM_AUX_MINORS, GFP_KERNEL);
87 mutex_unlock(&aux_idr_mutex);
88 if (index < 0) {
89 kfree(aux_dev);
90 return ERR_PTR(index);
92 aux_dev->index = index;
94 return aux_dev;
97 static void release_drm_dp_aux_dev(struct kref *ref)
99 struct drm_dp_aux_dev *aux_dev =
100 container_of(ref, struct drm_dp_aux_dev, refcount);
102 kfree(aux_dev);
105 static ssize_t name_show(struct device *dev,
106 struct device_attribute *attr, char *buf)
108 ssize_t res;
109 struct drm_dp_aux_dev *aux_dev =
110 drm_dp_aux_dev_get_by_minor(MINOR(dev->devt));
112 if (!aux_dev)
113 return -ENODEV;
115 res = sprintf(buf, "%s\n", aux_dev->aux->name);
116 kref_put(&aux_dev->refcount, release_drm_dp_aux_dev);
118 return res;
120 static DEVICE_ATTR_RO(name);
122 static struct attribute *drm_dp_aux_attrs[] = {
123 &dev_attr_name.attr,
124 NULL,
126 ATTRIBUTE_GROUPS(drm_dp_aux);
128 static int auxdev_open(struct inode *inode, struct file *file)
130 unsigned int minor = iminor(inode);
131 struct drm_dp_aux_dev *aux_dev;
133 aux_dev = drm_dp_aux_dev_get_by_minor(minor);
134 if (!aux_dev)
135 return -ENODEV;
137 file->private_data = aux_dev;
138 return 0;
141 static loff_t auxdev_llseek(struct file *file, loff_t offset, int whence)
143 return fixed_size_llseek(file, offset, whence, AUX_MAX_OFFSET);
146 static ssize_t auxdev_read_iter(struct kiocb *iocb, struct iov_iter *to)
148 struct drm_dp_aux_dev *aux_dev = iocb->ki_filp->private_data;
149 loff_t pos = iocb->ki_pos;
150 ssize_t res = 0;
152 if (!atomic_inc_not_zero(&aux_dev->usecount))
153 return -ENODEV;
155 iov_iter_truncate(to, AUX_MAX_OFFSET - pos);
157 while (iov_iter_count(to)) {
158 uint8_t buf[DP_AUX_MAX_PAYLOAD_BYTES];
159 ssize_t todo = min(iov_iter_count(to), sizeof(buf));
161 if (signal_pending(current)) {
162 res = -ERESTARTSYS;
163 break;
166 res = drm_dp_dpcd_read(aux_dev->aux, pos, buf, todo);
168 if (res <= 0)
169 break;
171 if (copy_to_iter(buf, res, to) != res) {
172 res = -EFAULT;
173 break;
176 pos += res;
179 if (pos != iocb->ki_pos)
180 res = pos - iocb->ki_pos;
181 iocb->ki_pos = pos;
183 if (atomic_dec_and_test(&aux_dev->usecount))
184 wake_up_var(&aux_dev->usecount);
186 return res;
189 static ssize_t auxdev_write_iter(struct kiocb *iocb, struct iov_iter *from)
191 struct drm_dp_aux_dev *aux_dev = iocb->ki_filp->private_data;
192 loff_t pos = iocb->ki_pos;
193 ssize_t res = 0;
195 if (!atomic_inc_not_zero(&aux_dev->usecount))
196 return -ENODEV;
198 iov_iter_truncate(from, AUX_MAX_OFFSET - pos);
200 while (iov_iter_count(from)) {
201 uint8_t buf[DP_AUX_MAX_PAYLOAD_BYTES];
202 ssize_t todo = min(iov_iter_count(from), sizeof(buf));
204 if (signal_pending(current)) {
205 res = -ERESTARTSYS;
206 break;
209 if (!copy_from_iter_full(buf, todo, from)) {
210 res = -EFAULT;
211 break;
214 res = drm_dp_dpcd_write(aux_dev->aux, pos, buf, todo);
216 if (res <= 0)
217 break;
219 pos += res;
222 if (pos != iocb->ki_pos)
223 res = pos - iocb->ki_pos;
224 iocb->ki_pos = pos;
226 if (atomic_dec_and_test(&aux_dev->usecount))
227 wake_up_var(&aux_dev->usecount);
229 return res;
232 static int auxdev_release(struct inode *inode, struct file *file)
234 struct drm_dp_aux_dev *aux_dev = file->private_data;
236 kref_put(&aux_dev->refcount, release_drm_dp_aux_dev);
237 return 0;
240 static const struct file_operations auxdev_fops = {
241 .owner = THIS_MODULE,
242 .llseek = auxdev_llseek,
243 .read_iter = auxdev_read_iter,
244 .write_iter = auxdev_write_iter,
245 .open = auxdev_open,
246 .release = auxdev_release,
249 #define to_auxdev(d) container_of(d, struct drm_dp_aux_dev, aux)
251 static struct drm_dp_aux_dev *drm_dp_aux_dev_get_by_aux(struct drm_dp_aux *aux)
253 struct drm_dp_aux_dev *iter, *aux_dev = NULL;
254 int id;
256 /* don't increase kref count here because this function should only be
257 * used by drm_dp_aux_unregister_devnode. Thus, it will always have at
258 * least one reference - the one that drm_dp_aux_register_devnode
259 * created
261 mutex_lock(&aux_idr_mutex);
262 idr_for_each_entry(&aux_idr, iter, id) {
263 if (iter->aux == aux) {
264 aux_dev = iter;
265 break;
268 mutex_unlock(&aux_idr_mutex);
269 return aux_dev;
272 void drm_dp_aux_unregister_devnode(struct drm_dp_aux *aux)
274 struct drm_dp_aux_dev *aux_dev;
275 unsigned int minor;
277 aux_dev = drm_dp_aux_dev_get_by_aux(aux);
278 if (!aux_dev) /* attach must have failed */
279 return;
281 mutex_lock(&aux_idr_mutex);
282 idr_remove(&aux_idr, aux_dev->index);
283 mutex_unlock(&aux_idr_mutex);
285 atomic_dec(&aux_dev->usecount);
286 wait_var_event(&aux_dev->usecount, !atomic_read(&aux_dev->usecount));
288 minor = aux_dev->index;
289 if (aux_dev->dev)
290 device_destroy(drm_dp_aux_dev_class,
291 MKDEV(drm_dev_major, minor));
293 DRM_DEBUG("drm_dp_aux_dev: aux [%s] unregistering\n", aux->name);
294 kref_put(&aux_dev->refcount, release_drm_dp_aux_dev);
297 int drm_dp_aux_register_devnode(struct drm_dp_aux *aux)
299 struct drm_dp_aux_dev *aux_dev;
300 int res;
302 aux_dev = alloc_drm_dp_aux_dev(aux);
303 if (IS_ERR(aux_dev))
304 return PTR_ERR(aux_dev);
306 aux_dev->dev = device_create(drm_dp_aux_dev_class, aux->dev,
307 MKDEV(drm_dev_major, aux_dev->index), NULL,
308 "drm_dp_aux%d", aux_dev->index);
309 if (IS_ERR(aux_dev->dev)) {
310 res = PTR_ERR(aux_dev->dev);
311 aux_dev->dev = NULL;
312 goto error;
315 DRM_DEBUG("drm_dp_aux_dev: aux [%s] registered as minor %d\n",
316 aux->name, aux_dev->index);
317 return 0;
318 error:
319 drm_dp_aux_unregister_devnode(aux);
320 return res;
323 int drm_dp_aux_dev_init(void)
325 int res;
327 drm_dp_aux_dev_class = class_create(THIS_MODULE, "drm_dp_aux_dev");
328 if (IS_ERR(drm_dp_aux_dev_class)) {
329 return PTR_ERR(drm_dp_aux_dev_class);
331 drm_dp_aux_dev_class->dev_groups = drm_dp_aux_groups;
333 res = register_chrdev(0, "aux", &auxdev_fops);
334 if (res < 0)
335 goto out;
336 drm_dev_major = res;
338 return 0;
339 out:
340 class_destroy(drm_dp_aux_dev_class);
341 return res;
344 void drm_dp_aux_dev_exit(void)
346 unregister_chrdev(drm_dev_major, "aux");
347 class_destroy(drm_dp_aux_dev_class);