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
24 * Rafael Antognolli <rafael.antognolli@intel.com>
28 #include <linux/device.h>
30 #include <linux/slab.h>
31 #include <linux/init.h>
32 #include <linux/kernel.h>
33 #include <linux/module.h>
34 #include <linux/uaccess.h>
35 #include <linux/uio.h>
36 #include <drm/drm_dp_helper.h>
37 #include <drm/drm_crtc.h>
40 #include "drm_crtc_helper_internal.h"
42 struct drm_dp_aux_dev
{
44 struct drm_dp_aux
*aux
;
50 #define DRM_AUX_MINORS 256
51 #define AUX_MAX_OFFSET (1 << 20)
52 static DEFINE_IDR(aux_idr
);
53 static DEFINE_MUTEX(aux_idr_mutex
);
54 static struct class *drm_dp_aux_dev_class
;
55 static int drm_dev_major
= -1;
57 static struct drm_dp_aux_dev
*drm_dp_aux_dev_get_by_minor(unsigned index
)
59 struct drm_dp_aux_dev
*aux_dev
= NULL
;
61 mutex_lock(&aux_idr_mutex
);
62 aux_dev
= idr_find(&aux_idr
, index
);
63 if (!kref_get_unless_zero(&aux_dev
->refcount
))
65 mutex_unlock(&aux_idr_mutex
);
70 static struct drm_dp_aux_dev
*alloc_drm_dp_aux_dev(struct drm_dp_aux
*aux
)
72 struct drm_dp_aux_dev
*aux_dev
;
75 aux_dev
= kzalloc(sizeof(*aux_dev
), GFP_KERNEL
);
77 return ERR_PTR(-ENOMEM
);
79 atomic_set(&aux_dev
->usecount
, 1);
80 kref_init(&aux_dev
->refcount
);
82 mutex_lock(&aux_idr_mutex
);
83 index
= idr_alloc_cyclic(&aux_idr
, aux_dev
, 0, DRM_AUX_MINORS
,
85 mutex_unlock(&aux_idr_mutex
);
88 return ERR_PTR(index
);
90 aux_dev
->index
= index
;
95 static void release_drm_dp_aux_dev(struct kref
*ref
)
97 struct drm_dp_aux_dev
*aux_dev
=
98 container_of(ref
, struct drm_dp_aux_dev
, refcount
);
103 static ssize_t
name_show(struct device
*dev
,
104 struct device_attribute
*attr
, char *buf
)
107 struct drm_dp_aux_dev
*aux_dev
=
108 drm_dp_aux_dev_get_by_minor(MINOR(dev
->devt
));
113 res
= sprintf(buf
, "%s\n", aux_dev
->aux
->name
);
114 kref_put(&aux_dev
->refcount
, release_drm_dp_aux_dev
);
118 static DEVICE_ATTR_RO(name
);
120 static struct attribute
*drm_dp_aux_attrs
[] = {
124 ATTRIBUTE_GROUPS(drm_dp_aux
);
126 static int auxdev_open(struct inode
*inode
, struct file
*file
)
128 unsigned int minor
= iminor(inode
);
129 struct drm_dp_aux_dev
*aux_dev
;
131 aux_dev
= drm_dp_aux_dev_get_by_minor(minor
);
135 file
->private_data
= aux_dev
;
139 static loff_t
auxdev_llseek(struct file
*file
, loff_t offset
, int whence
)
141 return fixed_size_llseek(file
, offset
, whence
, AUX_MAX_OFFSET
);
144 static ssize_t
auxdev_read_iter(struct kiocb
*iocb
, struct iov_iter
*to
)
146 struct drm_dp_aux_dev
*aux_dev
= iocb
->ki_filp
->private_data
;
147 loff_t pos
= iocb
->ki_pos
;
150 if (!atomic_inc_not_zero(&aux_dev
->usecount
))
153 iov_iter_truncate(to
, AUX_MAX_OFFSET
- pos
);
155 while (iov_iter_count(to
)) {
156 uint8_t buf
[DP_AUX_MAX_PAYLOAD_BYTES
];
157 ssize_t todo
= min(iov_iter_count(to
), sizeof(buf
));
159 if (signal_pending(current
)) {
164 res
= drm_dp_dpcd_read(aux_dev
->aux
, pos
, buf
, todo
);
168 if (copy_to_iter(buf
, res
, to
) != res
) {
176 if (pos
!= iocb
->ki_pos
)
177 res
= pos
- iocb
->ki_pos
;
180 if (atomic_dec_and_test(&aux_dev
->usecount
))
181 wake_up_var(&aux_dev
->usecount
);
186 static ssize_t
auxdev_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
)
188 struct drm_dp_aux_dev
*aux_dev
= iocb
->ki_filp
->private_data
;
189 loff_t pos
= iocb
->ki_pos
;
192 if (!atomic_inc_not_zero(&aux_dev
->usecount
))
195 iov_iter_truncate(from
, AUX_MAX_OFFSET
- pos
);
197 while (iov_iter_count(from
)) {
198 uint8_t buf
[DP_AUX_MAX_PAYLOAD_BYTES
];
199 ssize_t todo
= min(iov_iter_count(from
), sizeof(buf
));
201 if (signal_pending(current
)) {
206 if (!copy_from_iter_full(buf
, todo
, from
)) {
211 res
= drm_dp_dpcd_write(aux_dev
->aux
, pos
, buf
, todo
);
218 if (pos
!= iocb
->ki_pos
)
219 res
= pos
- iocb
->ki_pos
;
222 if (atomic_dec_and_test(&aux_dev
->usecount
))
223 wake_up_var(&aux_dev
->usecount
);
228 static int auxdev_release(struct inode
*inode
, struct file
*file
)
230 struct drm_dp_aux_dev
*aux_dev
= file
->private_data
;
232 kref_put(&aux_dev
->refcount
, release_drm_dp_aux_dev
);
236 static const struct file_operations auxdev_fops
= {
237 .owner
= THIS_MODULE
,
238 .llseek
= auxdev_llseek
,
239 .read_iter
= auxdev_read_iter
,
240 .write_iter
= auxdev_write_iter
,
242 .release
= auxdev_release
,
245 #define to_auxdev(d) container_of(d, struct drm_dp_aux_dev, aux)
247 static struct drm_dp_aux_dev
*drm_dp_aux_dev_get_by_aux(struct drm_dp_aux
*aux
)
249 struct drm_dp_aux_dev
*iter
, *aux_dev
= NULL
;
252 /* don't increase kref count here because this function should only be
253 * used by drm_dp_aux_unregister_devnode. Thus, it will always have at
254 * least one reference - the one that drm_dp_aux_register_devnode
257 mutex_lock(&aux_idr_mutex
);
258 idr_for_each_entry(&aux_idr
, iter
, id
) {
259 if (iter
->aux
== aux
) {
264 mutex_unlock(&aux_idr_mutex
);
268 void drm_dp_aux_unregister_devnode(struct drm_dp_aux
*aux
)
270 struct drm_dp_aux_dev
*aux_dev
;
273 aux_dev
= drm_dp_aux_dev_get_by_aux(aux
);
274 if (!aux_dev
) /* attach must have failed */
277 mutex_lock(&aux_idr_mutex
);
278 idr_remove(&aux_idr
, aux_dev
->index
);
279 mutex_unlock(&aux_idr_mutex
);
281 atomic_dec(&aux_dev
->usecount
);
282 wait_var_event(&aux_dev
->usecount
, !atomic_read(&aux_dev
->usecount
));
284 minor
= aux_dev
->index
;
286 device_destroy(drm_dp_aux_dev_class
,
287 MKDEV(drm_dev_major
, minor
));
289 DRM_DEBUG("drm_dp_aux_dev: aux [%s] unregistering\n", aux
->name
);
290 kref_put(&aux_dev
->refcount
, release_drm_dp_aux_dev
);
293 int drm_dp_aux_register_devnode(struct drm_dp_aux
*aux
)
295 struct drm_dp_aux_dev
*aux_dev
;
298 aux_dev
= alloc_drm_dp_aux_dev(aux
);
300 return PTR_ERR(aux_dev
);
302 aux_dev
->dev
= device_create(drm_dp_aux_dev_class
, aux
->dev
,
303 MKDEV(drm_dev_major
, aux_dev
->index
), NULL
,
304 "drm_dp_aux%d", aux_dev
->index
);
305 if (IS_ERR(aux_dev
->dev
)) {
306 res
= PTR_ERR(aux_dev
->dev
);
311 DRM_DEBUG("drm_dp_aux_dev: aux [%s] registered as minor %d\n",
312 aux
->name
, aux_dev
->index
);
315 drm_dp_aux_unregister_devnode(aux
);
319 int drm_dp_aux_dev_init(void)
323 drm_dp_aux_dev_class
= class_create(THIS_MODULE
, "drm_dp_aux_dev");
324 if (IS_ERR(drm_dp_aux_dev_class
)) {
325 return PTR_ERR(drm_dp_aux_dev_class
);
327 drm_dp_aux_dev_class
->dev_groups
= drm_dp_aux_groups
;
329 res
= register_chrdev(0, "aux", &auxdev_fops
);
336 class_destroy(drm_dp_aux_dev_class
);
340 void drm_dp_aux_dev_exit(void)
342 unregister_chrdev(drm_dev_major
, "aux");
343 class_destroy(drm_dp_aux_dev_class
);