xref: /dragonfly/sys/dev/drm/drm_dp_aux_dev.c (revision 3f2dd94a569761201b5b0a18b2f697f97fe1b9dc)
1 /*
2  * Copyright © 2015 Intel Corporation
3  *
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:
10  *
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.
14  *
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.
22  *
23  * Authors:
24  *    Rafael Antognolli <rafael.antognolli@intel.com>
25  *
26  */
27 
28 #include <linux/device.h>
29 #include <linux/fs.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>
38 #include <drm/drmP.h>
39 
40 #include "drm_crtc_helper_internal.h"
41 
42 struct drm_dp_aux_dev {
43           unsigned index;
44           struct drm_dp_aux *aux;
45           struct device *dev;
46           struct kref refcount;
47           atomic_t usecount;
48 };
49 
50 #define DRM_AUX_MINORS        256
51 #define AUX_MAX_OFFSET        (1 << 20)
52 #if 0
53 static DEFINE_IDR(aux_idr);
54 static DEFINE_MUTEX(aux_idr_mutex);
55 static struct class *drm_dp_aux_dev_class;
56 static int drm_dev_major = -1;
57 
58 static struct drm_dp_aux_dev *drm_dp_aux_dev_get_by_minor(unsigned index)
59 {
60           struct drm_dp_aux_dev *aux_dev = NULL;
61 
62           mutex_lock(&aux_idr_mutex);
63           aux_dev = idr_find(&aux_idr, index);
64           if (!kref_get_unless_zero(&aux_dev->refcount))
65                     aux_dev = NULL;
66           mutex_unlock(&aux_idr_mutex);
67 
68           return aux_dev;
69 }
70 
71 static struct drm_dp_aux_dev *alloc_drm_dp_aux_dev(struct drm_dp_aux *aux)
72 {
73           struct drm_dp_aux_dev *aux_dev;
74           int index;
75 
76           aux_dev = kzalloc(sizeof(*aux_dev), GFP_KERNEL);
77           if (!aux_dev)
78                     return ERR_PTR(-ENOMEM);
79           aux_dev->aux = aux;
80           atomic_set(&aux_dev->usecount, 1);
81           kref_init(&aux_dev->refcount);
82 
83           mutex_lock(&aux_idr_mutex);
84           index = idr_alloc_cyclic(&aux_idr, aux_dev, 0, DRM_AUX_MINORS,
85                                          GFP_KERNEL);
86           mutex_unlock(&aux_idr_mutex);
87           if (index < 0) {
88                     kfree(aux_dev);
89                     return ERR_PTR(index);
90           }
91           aux_dev->index = index;
92 
93           return aux_dev;
94 }
95 
96 static void release_drm_dp_aux_dev(struct kref *ref)
97 {
98           struct drm_dp_aux_dev *aux_dev =
99                     container_of(ref, struct drm_dp_aux_dev, refcount);
100 
101           kfree(aux_dev);
102 }
103 
104 static ssize_t name_show(struct device *dev,
105                                struct device_attribute *attr, char *buf)
106 {
107           ssize_t res;
108           struct drm_dp_aux_dev *aux_dev =
109                     drm_dp_aux_dev_get_by_minor(MINOR(dev->devt));
110 
111           if (!aux_dev)
112                     return -ENODEV;
113 
114           res = sprintf(buf, "%s\n", aux_dev->aux->name);
115           kref_put(&aux_dev->refcount, release_drm_dp_aux_dev);
116 
117           return res;
118 }
119 static DEVICE_ATTR_RO(name);
120 
121 static struct attribute *drm_dp_aux_attrs[] = {
122           &dev_attr_name.attr,
123           NULL,
124 };
125 ATTRIBUTE_GROUPS(drm_dp_aux);
126 
127 static int auxdev_open(struct inode *inode, struct file *file)
128 {
129           unsigned int minor = iminor(inode);
130           struct drm_dp_aux_dev *aux_dev;
131 
132           aux_dev = drm_dp_aux_dev_get_by_minor(minor);
133           if (!aux_dev)
134                     return -ENODEV;
135 
136           file->private_data = aux_dev;
137           return 0;
138 }
139 
140 static loff_t auxdev_llseek(struct file *file, loff_t offset, int whence)
141 {
142           return fixed_size_llseek(file, offset, whence, AUX_MAX_OFFSET);
143 }
144 
145 static ssize_t auxdev_read_iter(struct kiocb *iocb, struct iov_iter *to)
146 {
147           struct drm_dp_aux_dev *aux_dev = iocb->ki_filp->private_data;
148           loff_t pos = iocb->ki_pos;
149           ssize_t res = 0;
150 
151           if (!atomic_inc_not_zero(&aux_dev->usecount))
152                     return -ENODEV;
153 
154           iov_iter_truncate(to, AUX_MAX_OFFSET - pos);
155 
156           while (iov_iter_count(to)) {
157                     uint8_t buf[DP_AUX_MAX_PAYLOAD_BYTES];
158                     ssize_t todo = min(iov_iter_count(to), sizeof(buf));
159 
160                     if (signal_pending(current)) {
161                               res = -ERESTARTSYS;
162                               break;
163                     }
164 
165                     res = drm_dp_dpcd_read(aux_dev->aux, pos, buf, todo);
166                     if (res <= 0)
167                               break;
168 
169                     if (copy_to_iter(buf, res, to) != res) {
170                               res = -EFAULT;
171                               break;
172                     }
173 
174                     pos += res;
175           }
176 
177           if (pos != iocb->ki_pos)
178                     res = pos - iocb->ki_pos;
179           iocb->ki_pos = pos;
180 
181           atomic_dec(&aux_dev->usecount);
182           wake_up_atomic_t(&aux_dev->usecount);
183           return res;
184 }
185 
186 static ssize_t auxdev_write_iter(struct kiocb *iocb, struct iov_iter *from)
187 {
188           struct drm_dp_aux_dev *aux_dev = iocb->ki_filp->private_data;
189           loff_t pos = iocb->ki_pos;
190           ssize_t res = 0;
191 
192           if (!atomic_inc_not_zero(&aux_dev->usecount))
193                     return -ENODEV;
194 
195           iov_iter_truncate(from, AUX_MAX_OFFSET - pos);
196 
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));
200 
201                     if (signal_pending(current)) {
202                               res = -ERESTARTSYS;
203                               break;
204                     }
205 
206                     if (!copy_from_iter_full(buf, todo, from)) {
207                               res = -EFAULT;
208                               break;
209                     }
210 
211                     res = drm_dp_dpcd_write(aux_dev->aux, pos, buf, todo);
212                     if (res <= 0)
213                               break;
214 
215                     pos += res;
216           }
217 
218           if (pos != iocb->ki_pos)
219                     res = pos - iocb->ki_pos;
220           iocb->ki_pos = pos;
221 
222           atomic_dec(&aux_dev->usecount);
223           wake_up_atomic_t(&aux_dev->usecount);
224           return res;
225 }
226 
227 static int auxdev_release(struct inode *inode, struct file *file)
228 {
229           struct drm_dp_aux_dev *aux_dev = file->private_data;
230 
231           kref_put(&aux_dev->refcount, release_drm_dp_aux_dev);
232           return 0;
233 }
234 
235 static const struct file_operations auxdev_fops = {
236           .owner              = THIS_MODULE,
237           .llseek             = auxdev_llseek,
238           .read_iter          = auxdev_read_iter,
239           .write_iter         = auxdev_write_iter,
240           .open               = auxdev_open,
241           .release  = auxdev_release,
242 };
243 
244 #define to_auxdev(d) container_of(d, struct drm_dp_aux_dev, aux)
245 
246 static struct drm_dp_aux_dev *drm_dp_aux_dev_get_by_aux(struct drm_dp_aux *aux)
247 {
248           struct drm_dp_aux_dev *iter, *aux_dev = NULL;
249           int id;
250 
251           /* don't increase kref count here because this function should only be
252            * used by drm_dp_aux_unregister_devnode. Thus, it will always have at
253            * least one reference - the one that drm_dp_aux_register_devnode
254            * created
255            */
256           mutex_lock(&aux_idr_mutex);
257           idr_for_each_entry(&aux_idr, iter, id) {
258                     if (iter->aux == aux) {
259                               aux_dev = iter;
260                               break;
261                     }
262           }
263           mutex_unlock(&aux_idr_mutex);
264           return aux_dev;
265 }
266 
267 void drm_dp_aux_unregister_devnode(struct drm_dp_aux *aux)
268 {
269           struct drm_dp_aux_dev *aux_dev;
270           unsigned int minor;
271 
272           aux_dev = drm_dp_aux_dev_get_by_aux(aux);
273           if (!aux_dev) /* attach must have failed */
274                     return;
275 
276           mutex_lock(&aux_idr_mutex);
277           idr_remove(&aux_idr, aux_dev->index);
278           mutex_unlock(&aux_idr_mutex);
279 
280           atomic_dec(&aux_dev->usecount);
281           wait_on_atomic_t(&aux_dev->usecount, atomic_t_wait,
282                                TASK_UNINTERRUPTIBLE);
283 
284           minor = aux_dev->index;
285           if (aux_dev->dev)
286                     device_destroy(drm_dp_aux_dev_class,
287                                      MKDEV(drm_dev_major, minor));
288 
289           DRM_DEBUG("drm_dp_aux_dev: aux [%s] unregistering\n", aux->name);
290           kref_put(&aux_dev->refcount, release_drm_dp_aux_dev);
291 }
292 
293 int drm_dp_aux_register_devnode(struct drm_dp_aux *aux)
294 {
295           struct drm_dp_aux_dev *aux_dev;
296           int res;
297 
298           aux_dev = alloc_drm_dp_aux_dev(aux);
299           if (IS_ERR(aux_dev))
300                     return PTR_ERR(aux_dev);
301 
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);
307                     aux_dev->dev = NULL;
308                     goto error;
309           }
310 
311           DRM_DEBUG("drm_dp_aux_dev: aux [%s] registered as minor %d\n",
312                       aux->name, aux_dev->index);
313           return 0;
314 error:
315           drm_dp_aux_unregister_devnode(aux);
316           return res;
317 }
318 
319 int drm_dp_aux_dev_init(void)
320 {
321           int res;
322 
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);
326           }
327           drm_dp_aux_dev_class->dev_groups = drm_dp_aux_groups;
328 
329           res = register_chrdev(0, "aux", &auxdev_fops);
330           if (res < 0)
331                     goto out;
332           drm_dev_major = res;
333 
334           return 0;
335 out:
336           class_destroy(drm_dp_aux_dev_class);
337           return res;
338 }
339 
340 void drm_dp_aux_dev_exit(void)
341 {
342           unregister_chrdev(drm_dev_major, "aux");
343           class_destroy(drm_dp_aux_dev_class);
344 }
345 #endif    /* 0 */
346