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 <drm/drm_dp_helper.h>
36 #include <drm/drm_crtc.h>
39 struct drm_dp_aux_dev
{
41 struct drm_dp_aux
*aux
;
47 #define DRM_AUX_MINORS 256
48 #define AUX_MAX_OFFSET (1 << 20)
49 static DEFINE_IDR(aux_idr
);
50 static DEFINE_MUTEX(aux_idr_mutex
);
51 static struct class *drm_dp_aux_dev_class
;
52 static int drm_dev_major
= -1;
54 static struct drm_dp_aux_dev
*drm_dp_aux_dev_get_by_minor(unsigned index
)
56 struct drm_dp_aux_dev
*aux_dev
= NULL
;
58 mutex_lock(&aux_idr_mutex
);
59 aux_dev
= idr_find(&aux_idr
, index
);
60 if (!kref_get_unless_zero(&aux_dev
->refcount
))
62 mutex_unlock(&aux_idr_mutex
);
67 static struct drm_dp_aux_dev
*alloc_drm_dp_aux_dev(struct drm_dp_aux
*aux
)
69 struct drm_dp_aux_dev
*aux_dev
;
72 aux_dev
= kzalloc(sizeof(*aux_dev
), GFP_KERNEL
);
74 return ERR_PTR(-ENOMEM
);
76 atomic_set(&aux_dev
->usecount
, 1);
77 kref_init(&aux_dev
->refcount
);
79 mutex_lock(&aux_idr_mutex
);
80 index
= idr_alloc_cyclic(&aux_idr
, aux_dev
, 0, DRM_AUX_MINORS
,
82 mutex_unlock(&aux_idr_mutex
);
85 return ERR_PTR(index
);
87 aux_dev
->index
= index
;
92 static void release_drm_dp_aux_dev(struct kref
*ref
)
94 struct drm_dp_aux_dev
*aux_dev
=
95 container_of(ref
, struct drm_dp_aux_dev
, refcount
);
100 static ssize_t
name_show(struct device
*dev
,
101 struct device_attribute
*attr
, char *buf
)
104 struct drm_dp_aux_dev
*aux_dev
=
105 drm_dp_aux_dev_get_by_minor(MINOR(dev
->devt
));
110 res
= sprintf(buf
, "%s\n", aux_dev
->aux
->name
);
111 kref_put(&aux_dev
->refcount
, release_drm_dp_aux_dev
);
115 static DEVICE_ATTR_RO(name
);
117 static struct attribute
*drm_dp_aux_attrs
[] = {
121 ATTRIBUTE_GROUPS(drm_dp_aux
);
123 static int auxdev_open(struct inode
*inode
, struct file
*file
)
125 unsigned int minor
= iminor(inode
);
126 struct drm_dp_aux_dev
*aux_dev
;
128 aux_dev
= drm_dp_aux_dev_get_by_minor(minor
);
132 file
->private_data
= aux_dev
;
136 static loff_t
auxdev_llseek(struct file
*file
, loff_t offset
, int whence
)
138 return fixed_size_llseek(file
, offset
, whence
, AUX_MAX_OFFSET
);
141 static ssize_t
auxdev_read(struct file
*file
, char __user
*buf
, size_t count
,
144 size_t bytes_pending
, num_bytes_processed
= 0;
145 struct drm_dp_aux_dev
*aux_dev
= file
->private_data
;
148 if (!atomic_inc_not_zero(&aux_dev
->usecount
))
151 bytes_pending
= min((loff_t
)count
, AUX_MAX_OFFSET
- (*offset
));
153 if (!access_ok(VERIFY_WRITE
, buf
, bytes_pending
)) {
158 while (bytes_pending
> 0) {
159 uint8_t localbuf
[DP_AUX_MAX_PAYLOAD_BYTES
];
160 ssize_t todo
= min_t(size_t, bytes_pending
, sizeof(localbuf
));
162 res
= drm_dp_dpcd_read(aux_dev
->aux
, *offset
, localbuf
, todo
);
164 res
= num_bytes_processed
? num_bytes_processed
: res
;
167 if (__copy_to_user(buf
+ num_bytes_processed
, localbuf
, res
)) {
168 res
= num_bytes_processed
?
169 num_bytes_processed
: -EFAULT
;
172 bytes_pending
-= res
;
174 num_bytes_processed
+= res
;
175 res
= num_bytes_processed
;
179 atomic_dec(&aux_dev
->usecount
);
180 wake_up_atomic_t(&aux_dev
->usecount
);
184 static ssize_t
auxdev_write(struct file
*file
, const char __user
*buf
,
185 size_t count
, loff_t
*offset
)
187 size_t bytes_pending
, num_bytes_processed
= 0;
188 struct drm_dp_aux_dev
*aux_dev
= file
->private_data
;
191 if (!atomic_inc_not_zero(&aux_dev
->usecount
))
194 bytes_pending
= min((loff_t
)count
, AUX_MAX_OFFSET
- *offset
);
196 if (!access_ok(VERIFY_READ
, buf
, bytes_pending
)) {
201 while (bytes_pending
> 0) {
202 uint8_t localbuf
[DP_AUX_MAX_PAYLOAD_BYTES
];
203 ssize_t todo
= min_t(size_t, bytes_pending
, sizeof(localbuf
));
205 if (__copy_from_user(localbuf
,
206 buf
+ num_bytes_processed
, todo
)) {
207 res
= num_bytes_processed
?
208 num_bytes_processed
: -EFAULT
;
212 res
= drm_dp_dpcd_write(aux_dev
->aux
, *offset
, localbuf
, todo
);
214 res
= num_bytes_processed
? num_bytes_processed
: res
;
217 bytes_pending
-= res
;
219 num_bytes_processed
+= res
;
220 res
= num_bytes_processed
;
224 atomic_dec(&aux_dev
->usecount
);
225 wake_up_atomic_t(&aux_dev
->usecount
);
229 static int auxdev_release(struct inode
*inode
, struct file
*file
)
231 struct drm_dp_aux_dev
*aux_dev
= file
->private_data
;
233 kref_put(&aux_dev
->refcount
, release_drm_dp_aux_dev
);
237 static const struct file_operations auxdev_fops
= {
238 .owner
= THIS_MODULE
,
239 .llseek
= auxdev_llseek
,
241 .write
= auxdev_write
,
243 .release
= auxdev_release
,
246 #define to_auxdev(d) container_of(d, struct drm_dp_aux_dev, aux)
248 static struct drm_dp_aux_dev
*drm_dp_aux_dev_get_by_aux(struct drm_dp_aux
*aux
)
250 struct drm_dp_aux_dev
*iter
, *aux_dev
= NULL
;
253 /* don't increase kref count here because this function should only be
254 * used by drm_dp_aux_unregister_devnode. Thus, it will always have at
255 * least one reference - the one that drm_dp_aux_register_devnode
258 mutex_lock(&aux_idr_mutex
);
259 idr_for_each_entry(&aux_idr
, iter
, id
) {
260 if (iter
->aux
== aux
) {
265 mutex_unlock(&aux_idr_mutex
);
269 static int auxdev_wait_atomic_t(atomic_t
*p
)
275 * drm_dp_aux_unregister_devnode() - unregister a devnode for this aux channel
276 * @aux: DisplayPort AUX channel
278 * Returns 0 on success or a negative error code on failure.
280 void drm_dp_aux_unregister_devnode(struct drm_dp_aux
*aux
)
282 struct drm_dp_aux_dev
*aux_dev
;
285 aux_dev
= drm_dp_aux_dev_get_by_aux(aux
);
286 if (!aux_dev
) /* attach must have failed */
289 mutex_lock(&aux_idr_mutex
);
290 idr_remove(&aux_idr
, aux_dev
->index
);
291 mutex_unlock(&aux_idr_mutex
);
293 atomic_dec(&aux_dev
->usecount
);
294 wait_on_atomic_t(&aux_dev
->usecount
, auxdev_wait_atomic_t
,
295 TASK_UNINTERRUPTIBLE
);
297 minor
= aux_dev
->index
;
299 device_destroy(drm_dp_aux_dev_class
,
300 MKDEV(drm_dev_major
, minor
));
302 DRM_DEBUG("drm_dp_aux_dev: aux [%s] unregistering\n", aux
->name
);
303 kref_put(&aux_dev
->refcount
, release_drm_dp_aux_dev
);
305 EXPORT_SYMBOL(drm_dp_aux_unregister_devnode
);
308 * drm_dp_aux_register_devnode() - register a devnode for this aux channel
309 * @aux: DisplayPort AUX channel
311 * Returns 0 on success or a negative error code on failure.
313 int drm_dp_aux_register_devnode(struct drm_dp_aux
*aux
)
315 struct drm_dp_aux_dev
*aux_dev
;
318 aux_dev
= alloc_drm_dp_aux_dev(aux
);
320 return PTR_ERR(aux_dev
);
322 aux_dev
->dev
= device_create(drm_dp_aux_dev_class
, aux
->dev
,
323 MKDEV(drm_dev_major
, aux_dev
->index
), NULL
,
324 "drm_dp_aux%d", aux_dev
->index
);
325 if (IS_ERR(aux_dev
->dev
)) {
326 res
= PTR_ERR(aux_dev
->dev
);
331 DRM_DEBUG("drm_dp_aux_dev: aux [%s] registered as minor %d\n",
332 aux
->name
, aux_dev
->index
);
335 drm_dp_aux_unregister_devnode(aux
);
338 EXPORT_SYMBOL(drm_dp_aux_register_devnode
);
340 int drm_dp_aux_dev_init(void)
344 drm_dp_aux_dev_class
= class_create(THIS_MODULE
, "drm_dp_aux_dev");
345 if (IS_ERR(drm_dp_aux_dev_class
)) {
346 res
= PTR_ERR(drm_dp_aux_dev_class
);
349 drm_dp_aux_dev_class
->dev_groups
= drm_dp_aux_groups
;
351 res
= register_chrdev(0, "aux", &auxdev_fops
);
358 class_destroy(drm_dp_aux_dev_class
);
361 EXPORT_SYMBOL(drm_dp_aux_dev_init
);
363 void drm_dp_aux_dev_exit(void)
365 unregister_chrdev(drm_dev_major
, "aux");
366 class_destroy(drm_dp_aux_dev_class
);
368 EXPORT_SYMBOL(drm_dp_aux_dev_exit
);