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
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
28 #include <linux/device.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_print.h>
42 #include "drm_crtc_helper_internal.h"
44 struct drm_dp_aux_dev {
46 struct drm_dp_aux *aux;
52 #define DRM_AUX_MINORS 256
53 #define AUX_MAX_OFFSET (1 << 20)
54 static DEFINE_IDR(aux_idr);
55 static DEFINE_MUTEX(aux_idr_mutex);
56 static struct class *drm_dp_aux_dev_class;
57 static int drm_dev_major = -1;
59 static struct drm_dp_aux_dev *drm_dp_aux_dev_get_by_minor(unsigned index)
61 struct drm_dp_aux_dev *aux_dev = NULL;
63 mutex_lock(&aux_idr_mutex);
64 aux_dev = idr_find(&aux_idr, index);
65 if (!kref_get_unless_zero(&aux_dev->refcount))
67 mutex_unlock(&aux_idr_mutex);
72 static struct drm_dp_aux_dev *alloc_drm_dp_aux_dev(struct drm_dp_aux *aux)
74 struct drm_dp_aux_dev *aux_dev;
77 aux_dev = kzalloc(sizeof(*aux_dev), GFP_KERNEL);
79 return ERR_PTR(-ENOMEM);
81 atomic_set(&aux_dev->usecount, 1);
82 kref_init(&aux_dev->refcount);
84 mutex_lock(&aux_idr_mutex);
85 index = idr_alloc_cyclic(&aux_idr, aux_dev, 0, DRM_AUX_MINORS,
87 mutex_unlock(&aux_idr_mutex);
90 return ERR_PTR(index);
92 aux_dev->index = index;
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);
105 static ssize_t name_show(struct device *dev,
106 struct device_attribute *attr, char *buf)
109 struct drm_dp_aux_dev *aux_dev =
110 drm_dp_aux_dev_get_by_minor(MINOR(dev->devt));
115 res = sprintf(buf, "%s\n", aux_dev->aux->name);
116 kref_put(&aux_dev->refcount, release_drm_dp_aux_dev);
120 static DEVICE_ATTR_RO(name);
122 static struct attribute *drm_dp_aux_attrs[] = {
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);
137 file->private_data = aux_dev;
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;
152 if (!atomic_inc_not_zero(&aux_dev->usecount))
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)) {
166 res = drm_dp_dpcd_read(aux_dev->aux, pos, buf, todo);
170 if (copy_to_iter(buf, res, to) != res) {
178 if (pos != iocb->ki_pos)
179 res = pos - iocb->ki_pos;
182 if (atomic_dec_and_test(&aux_dev->usecount))
183 wake_up_var(&aux_dev->usecount);
188 static ssize_t auxdev_write_iter(struct kiocb *iocb, struct iov_iter *from)
190 struct drm_dp_aux_dev *aux_dev = iocb->ki_filp->private_data;
191 loff_t pos = iocb->ki_pos;
194 if (!atomic_inc_not_zero(&aux_dev->usecount))
197 iov_iter_truncate(from, AUX_MAX_OFFSET - pos);
199 while (iov_iter_count(from)) {
200 uint8_t buf[DP_AUX_MAX_PAYLOAD_BYTES];
201 ssize_t todo = min(iov_iter_count(from), sizeof(buf));
203 if (signal_pending(current)) {
208 if (!copy_from_iter_full(buf, todo, from)) {
213 res = drm_dp_dpcd_write(aux_dev->aux, pos, buf, todo);
220 if (pos != iocb->ki_pos)
221 res = pos - iocb->ki_pos;
224 if (atomic_dec_and_test(&aux_dev->usecount))
225 wake_up_var(&aux_dev->usecount);
230 static int auxdev_release(struct inode *inode, struct file *file)
232 struct drm_dp_aux_dev *aux_dev = file->private_data;
234 kref_put(&aux_dev->refcount, release_drm_dp_aux_dev);
238 static const struct file_operations auxdev_fops = {
239 .owner = THIS_MODULE,
240 .llseek = auxdev_llseek,
241 .read_iter = auxdev_read_iter,
242 .write_iter = auxdev_write_iter,
244 .release = auxdev_release,
247 #define to_auxdev(d) container_of(d, struct drm_dp_aux_dev, aux)
249 static struct drm_dp_aux_dev *drm_dp_aux_dev_get_by_aux(struct drm_dp_aux *aux)
251 struct drm_dp_aux_dev *iter, *aux_dev = NULL;
254 /* don't increase kref count here because this function should only be
255 * used by drm_dp_aux_unregister_devnode. Thus, it will always have at
256 * least one reference - the one that drm_dp_aux_register_devnode
259 mutex_lock(&aux_idr_mutex);
260 idr_for_each_entry(&aux_idr, iter, id) {
261 if (iter->aux == aux) {
266 mutex_unlock(&aux_idr_mutex);
270 void drm_dp_aux_unregister_devnode(struct drm_dp_aux *aux)
272 struct drm_dp_aux_dev *aux_dev;
275 aux_dev = drm_dp_aux_dev_get_by_aux(aux);
276 if (!aux_dev) /* attach must have failed */
279 mutex_lock(&aux_idr_mutex);
280 idr_remove(&aux_idr, aux_dev->index);
281 mutex_unlock(&aux_idr_mutex);
283 atomic_dec(&aux_dev->usecount);
284 wait_var_event(&aux_dev->usecount, !atomic_read(&aux_dev->usecount));
286 minor = aux_dev->index;
288 device_destroy(drm_dp_aux_dev_class,
289 MKDEV(drm_dev_major, minor));
291 DRM_DEBUG("drm_dp_aux_dev: aux [%s] unregistering\n", aux->name);
292 kref_put(&aux_dev->refcount, release_drm_dp_aux_dev);
295 int drm_dp_aux_register_devnode(struct drm_dp_aux *aux)
297 struct drm_dp_aux_dev *aux_dev;
300 aux_dev = alloc_drm_dp_aux_dev(aux);
302 return PTR_ERR(aux_dev);
304 aux_dev->dev = device_create(drm_dp_aux_dev_class, aux->dev,
305 MKDEV(drm_dev_major, aux_dev->index), NULL,
306 "drm_dp_aux%d", aux_dev->index);
307 if (IS_ERR(aux_dev->dev)) {
308 res = PTR_ERR(aux_dev->dev);
313 DRM_DEBUG("drm_dp_aux_dev: aux [%s] registered as minor %d\n",
314 aux->name, aux_dev->index);
317 drm_dp_aux_unregister_devnode(aux);
321 int drm_dp_aux_dev_init(void)
325 drm_dp_aux_dev_class = class_create(THIS_MODULE, "drm_dp_aux_dev");
326 if (IS_ERR(drm_dp_aux_dev_class)) {
327 return PTR_ERR(drm_dp_aux_dev_class);
329 drm_dp_aux_dev_class->dev_groups = drm_dp_aux_groups;
331 res = register_chrdev(0, "aux", &auxdev_fops);
338 class_destroy(drm_dp_aux_dev_class);
342 void drm_dp_aux_dev_exit(void)
344 unregister_chrdev(drm_dev_major, "aux");
345 class_destroy(drm_dp_aux_dev_class);