11bb76ff1Sjsg /*
21bb76ff1Sjsg * Copyright © 2015 Intel Corporation
31bb76ff1Sjsg *
41bb76ff1Sjsg * Permission is hereby granted, free of charge, to any person obtaining a
51bb76ff1Sjsg * copy of this software and associated documentation files (the "Software"),
61bb76ff1Sjsg * to deal in the Software without restriction, including without limitation
71bb76ff1Sjsg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
81bb76ff1Sjsg * and/or sell copies of the Software, and to permit persons to whom the
91bb76ff1Sjsg * Software is furnished to do so, subject to the following conditions:
101bb76ff1Sjsg *
111bb76ff1Sjsg * The above copyright notice and this permission notice (including the next
121bb76ff1Sjsg * paragraph) shall be included in all copies or substantial portions of the
131bb76ff1Sjsg * Software.
141bb76ff1Sjsg *
151bb76ff1Sjsg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
161bb76ff1Sjsg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
171bb76ff1Sjsg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
181bb76ff1Sjsg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
191bb76ff1Sjsg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
201bb76ff1Sjsg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
211bb76ff1Sjsg * IN THE SOFTWARE.
221bb76ff1Sjsg *
231bb76ff1Sjsg * Authors:
241bb76ff1Sjsg * Rafael Antognolli <rafael.antognolli@intel.com>
251bb76ff1Sjsg *
261bb76ff1Sjsg */
271bb76ff1Sjsg
281bb76ff1Sjsg #include <linux/device.h>
291bb76ff1Sjsg #include <linux/fs.h>
301bb76ff1Sjsg #include <linux/init.h>
311bb76ff1Sjsg #include <linux/kernel.h>
321bb76ff1Sjsg #include <linux/module.h>
331bb76ff1Sjsg #include <linux/sched/signal.h>
341bb76ff1Sjsg #include <linux/slab.h>
351bb76ff1Sjsg #include <linux/uaccess.h>
361bb76ff1Sjsg #include <linux/uio.h>
371bb76ff1Sjsg
381bb76ff1Sjsg #include <drm/display/drm_dp_helper.h>
391bb76ff1Sjsg #include <drm/display/drm_dp_mst_helper.h>
401bb76ff1Sjsg #include <drm/drm_crtc.h>
411bb76ff1Sjsg #include <drm/drm_print.h>
421bb76ff1Sjsg
431bb76ff1Sjsg #include "drm_dp_helper_internal.h"
441bb76ff1Sjsg
451bb76ff1Sjsg struct drm_dp_aux_dev {
461bb76ff1Sjsg unsigned index;
471bb76ff1Sjsg struct drm_dp_aux *aux;
481bb76ff1Sjsg struct device *dev;
491bb76ff1Sjsg struct kref refcount;
501bb76ff1Sjsg atomic_t usecount;
511bb76ff1Sjsg };
521bb76ff1Sjsg
531bb76ff1Sjsg #define DRM_AUX_MINORS 256
541bb76ff1Sjsg #define AUX_MAX_OFFSET (1 << 20)
551bb76ff1Sjsg static DEFINE_IDR(aux_idr);
561bb76ff1Sjsg static DEFINE_MUTEX(aux_idr_mutex);
571bb76ff1Sjsg static struct class *drm_dp_aux_dev_class;
581bb76ff1Sjsg static int drm_dev_major = -1;
591bb76ff1Sjsg
drm_dp_aux_dev_get_by_minor(unsigned index)601bb76ff1Sjsg static struct drm_dp_aux_dev *drm_dp_aux_dev_get_by_minor(unsigned index)
611bb76ff1Sjsg {
621bb76ff1Sjsg struct drm_dp_aux_dev *aux_dev = NULL;
631bb76ff1Sjsg
641bb76ff1Sjsg mutex_lock(&aux_idr_mutex);
651bb76ff1Sjsg aux_dev = idr_find(&aux_idr, index);
661bb76ff1Sjsg if (aux_dev && !kref_get_unless_zero(&aux_dev->refcount))
671bb76ff1Sjsg aux_dev = NULL;
681bb76ff1Sjsg mutex_unlock(&aux_idr_mutex);
691bb76ff1Sjsg
701bb76ff1Sjsg return aux_dev;
711bb76ff1Sjsg }
721bb76ff1Sjsg
alloc_drm_dp_aux_dev(struct drm_dp_aux * aux)731bb76ff1Sjsg static struct drm_dp_aux_dev *alloc_drm_dp_aux_dev(struct drm_dp_aux *aux)
741bb76ff1Sjsg {
751bb76ff1Sjsg struct drm_dp_aux_dev *aux_dev;
761bb76ff1Sjsg int index;
771bb76ff1Sjsg
781bb76ff1Sjsg aux_dev = kzalloc(sizeof(*aux_dev), GFP_KERNEL);
791bb76ff1Sjsg if (!aux_dev)
801bb76ff1Sjsg return ERR_PTR(-ENOMEM);
811bb76ff1Sjsg aux_dev->aux = aux;
821bb76ff1Sjsg atomic_set(&aux_dev->usecount, 1);
831bb76ff1Sjsg kref_init(&aux_dev->refcount);
841bb76ff1Sjsg
851bb76ff1Sjsg mutex_lock(&aux_idr_mutex);
861bb76ff1Sjsg index = idr_alloc(&aux_idr, aux_dev, 0, DRM_AUX_MINORS, GFP_KERNEL);
871bb76ff1Sjsg mutex_unlock(&aux_idr_mutex);
881bb76ff1Sjsg if (index < 0) {
891bb76ff1Sjsg kfree(aux_dev);
901bb76ff1Sjsg return ERR_PTR(index);
911bb76ff1Sjsg }
921bb76ff1Sjsg aux_dev->index = index;
931bb76ff1Sjsg
941bb76ff1Sjsg return aux_dev;
951bb76ff1Sjsg }
961bb76ff1Sjsg
release_drm_dp_aux_dev(struct kref * ref)971bb76ff1Sjsg static void release_drm_dp_aux_dev(struct kref *ref)
981bb76ff1Sjsg {
991bb76ff1Sjsg struct drm_dp_aux_dev *aux_dev =
1001bb76ff1Sjsg container_of(ref, struct drm_dp_aux_dev, refcount);
1011bb76ff1Sjsg
1021bb76ff1Sjsg kfree(aux_dev);
1031bb76ff1Sjsg }
1041bb76ff1Sjsg
name_show(struct device * dev,struct device_attribute * attr,char * buf)1051bb76ff1Sjsg static ssize_t name_show(struct device *dev,
1061bb76ff1Sjsg struct device_attribute *attr, char *buf)
1071bb76ff1Sjsg {
1081bb76ff1Sjsg ssize_t res;
1091bb76ff1Sjsg struct drm_dp_aux_dev *aux_dev =
1101bb76ff1Sjsg drm_dp_aux_dev_get_by_minor(MINOR(dev->devt));
1111bb76ff1Sjsg
1121bb76ff1Sjsg if (!aux_dev)
1131bb76ff1Sjsg return -ENODEV;
1141bb76ff1Sjsg
1151bb76ff1Sjsg res = sprintf(buf, "%s\n", aux_dev->aux->name);
1161bb76ff1Sjsg kref_put(&aux_dev->refcount, release_drm_dp_aux_dev);
1171bb76ff1Sjsg
1181bb76ff1Sjsg return res;
1191bb76ff1Sjsg }
1201bb76ff1Sjsg static DEVICE_ATTR_RO(name);
1211bb76ff1Sjsg
1221bb76ff1Sjsg static struct attribute *drm_dp_aux_attrs[] = {
1231bb76ff1Sjsg &dev_attr_name.attr,
1241bb76ff1Sjsg NULL,
1251bb76ff1Sjsg };
1261bb76ff1Sjsg ATTRIBUTE_GROUPS(drm_dp_aux);
1271bb76ff1Sjsg
auxdev_open(struct inode * inode,struct file * file)1281bb76ff1Sjsg static int auxdev_open(struct inode *inode, struct file *file)
1291bb76ff1Sjsg {
1301bb76ff1Sjsg unsigned int minor = iminor(inode);
1311bb76ff1Sjsg struct drm_dp_aux_dev *aux_dev;
1321bb76ff1Sjsg
1331bb76ff1Sjsg aux_dev = drm_dp_aux_dev_get_by_minor(minor);
1341bb76ff1Sjsg if (!aux_dev)
1351bb76ff1Sjsg return -ENODEV;
1361bb76ff1Sjsg
1371bb76ff1Sjsg file->private_data = aux_dev;
1381bb76ff1Sjsg return 0;
1391bb76ff1Sjsg }
1401bb76ff1Sjsg
auxdev_llseek(struct file * file,loff_t offset,int whence)1411bb76ff1Sjsg static loff_t auxdev_llseek(struct file *file, loff_t offset, int whence)
1421bb76ff1Sjsg {
1431bb76ff1Sjsg return fixed_size_llseek(file, offset, whence, AUX_MAX_OFFSET);
1441bb76ff1Sjsg }
1451bb76ff1Sjsg
auxdev_read_iter(struct kiocb * iocb,struct iov_iter * to)1461bb76ff1Sjsg static ssize_t auxdev_read_iter(struct kiocb *iocb, struct iov_iter *to)
1471bb76ff1Sjsg {
1481bb76ff1Sjsg struct drm_dp_aux_dev *aux_dev = iocb->ki_filp->private_data;
1491bb76ff1Sjsg loff_t pos = iocb->ki_pos;
1501bb76ff1Sjsg ssize_t res = 0;
1511bb76ff1Sjsg
1521bb76ff1Sjsg if (!atomic_inc_not_zero(&aux_dev->usecount))
1531bb76ff1Sjsg return -ENODEV;
1541bb76ff1Sjsg
1551bb76ff1Sjsg iov_iter_truncate(to, AUX_MAX_OFFSET - pos);
1561bb76ff1Sjsg
1571bb76ff1Sjsg while (iov_iter_count(to)) {
1581bb76ff1Sjsg uint8_t buf[DP_AUX_MAX_PAYLOAD_BYTES];
1591bb76ff1Sjsg ssize_t todo = min(iov_iter_count(to), sizeof(buf));
1601bb76ff1Sjsg
1611bb76ff1Sjsg if (signal_pending(current)) {
1621bb76ff1Sjsg res = -ERESTARTSYS;
1631bb76ff1Sjsg break;
1641bb76ff1Sjsg }
1651bb76ff1Sjsg
1661bb76ff1Sjsg res = drm_dp_dpcd_read(aux_dev->aux, pos, buf, todo);
1671bb76ff1Sjsg
1681bb76ff1Sjsg if (res <= 0)
1691bb76ff1Sjsg break;
1701bb76ff1Sjsg
1711bb76ff1Sjsg if (copy_to_iter(buf, res, to) != res) {
1721bb76ff1Sjsg res = -EFAULT;
1731bb76ff1Sjsg break;
1741bb76ff1Sjsg }
1751bb76ff1Sjsg
1761bb76ff1Sjsg pos += res;
1771bb76ff1Sjsg }
1781bb76ff1Sjsg
1791bb76ff1Sjsg if (pos != iocb->ki_pos)
1801bb76ff1Sjsg res = pos - iocb->ki_pos;
1811bb76ff1Sjsg iocb->ki_pos = pos;
1821bb76ff1Sjsg
1831bb76ff1Sjsg if (atomic_dec_and_test(&aux_dev->usecount))
1841bb76ff1Sjsg wake_up_var(&aux_dev->usecount);
1851bb76ff1Sjsg
1861bb76ff1Sjsg return res;
1871bb76ff1Sjsg }
1881bb76ff1Sjsg
auxdev_write_iter(struct kiocb * iocb,struct iov_iter * from)1891bb76ff1Sjsg static ssize_t auxdev_write_iter(struct kiocb *iocb, struct iov_iter *from)
1901bb76ff1Sjsg {
1911bb76ff1Sjsg struct drm_dp_aux_dev *aux_dev = iocb->ki_filp->private_data;
1921bb76ff1Sjsg loff_t pos = iocb->ki_pos;
1931bb76ff1Sjsg ssize_t res = 0;
1941bb76ff1Sjsg
1951bb76ff1Sjsg if (!atomic_inc_not_zero(&aux_dev->usecount))
1961bb76ff1Sjsg return -ENODEV;
1971bb76ff1Sjsg
1981bb76ff1Sjsg iov_iter_truncate(from, AUX_MAX_OFFSET - pos);
1991bb76ff1Sjsg
2001bb76ff1Sjsg while (iov_iter_count(from)) {
2011bb76ff1Sjsg uint8_t buf[DP_AUX_MAX_PAYLOAD_BYTES];
2021bb76ff1Sjsg ssize_t todo = min(iov_iter_count(from), sizeof(buf));
2031bb76ff1Sjsg
2041bb76ff1Sjsg if (signal_pending(current)) {
2051bb76ff1Sjsg res = -ERESTARTSYS;
2061bb76ff1Sjsg break;
2071bb76ff1Sjsg }
2081bb76ff1Sjsg
2091bb76ff1Sjsg if (!copy_from_iter_full(buf, todo, from)) {
2101bb76ff1Sjsg res = -EFAULT;
2111bb76ff1Sjsg break;
2121bb76ff1Sjsg }
2131bb76ff1Sjsg
2141bb76ff1Sjsg res = drm_dp_dpcd_write(aux_dev->aux, pos, buf, todo);
2151bb76ff1Sjsg
2161bb76ff1Sjsg if (res <= 0)
2171bb76ff1Sjsg break;
2181bb76ff1Sjsg
2191bb76ff1Sjsg pos += res;
2201bb76ff1Sjsg }
2211bb76ff1Sjsg
2221bb76ff1Sjsg if (pos != iocb->ki_pos)
2231bb76ff1Sjsg res = pos - iocb->ki_pos;
2241bb76ff1Sjsg iocb->ki_pos = pos;
2251bb76ff1Sjsg
2261bb76ff1Sjsg if (atomic_dec_and_test(&aux_dev->usecount))
2271bb76ff1Sjsg wake_up_var(&aux_dev->usecount);
2281bb76ff1Sjsg
2291bb76ff1Sjsg return res;
2301bb76ff1Sjsg }
2311bb76ff1Sjsg
auxdev_release(struct inode * inode,struct file * file)2321bb76ff1Sjsg static int auxdev_release(struct inode *inode, struct file *file)
2331bb76ff1Sjsg {
2341bb76ff1Sjsg struct drm_dp_aux_dev *aux_dev = file->private_data;
2351bb76ff1Sjsg
2361bb76ff1Sjsg kref_put(&aux_dev->refcount, release_drm_dp_aux_dev);
2371bb76ff1Sjsg return 0;
2381bb76ff1Sjsg }
2391bb76ff1Sjsg
2401bb76ff1Sjsg static const struct file_operations auxdev_fops = {
2411bb76ff1Sjsg .owner = THIS_MODULE,
2421bb76ff1Sjsg .llseek = auxdev_llseek,
2431bb76ff1Sjsg .read_iter = auxdev_read_iter,
2441bb76ff1Sjsg .write_iter = auxdev_write_iter,
2451bb76ff1Sjsg .open = auxdev_open,
2461bb76ff1Sjsg .release = auxdev_release,
2471bb76ff1Sjsg };
2481bb76ff1Sjsg
2491bb76ff1Sjsg #define to_auxdev(d) container_of(d, struct drm_dp_aux_dev, aux)
2501bb76ff1Sjsg
drm_dp_aux_dev_get_by_aux(struct drm_dp_aux * aux)2511bb76ff1Sjsg static struct drm_dp_aux_dev *drm_dp_aux_dev_get_by_aux(struct drm_dp_aux *aux)
2521bb76ff1Sjsg {
2531bb76ff1Sjsg struct drm_dp_aux_dev *iter, *aux_dev = NULL;
2541bb76ff1Sjsg int id;
2551bb76ff1Sjsg
2561bb76ff1Sjsg /* don't increase kref count here because this function should only be
2571bb76ff1Sjsg * used by drm_dp_aux_unregister_devnode. Thus, it will always have at
2581bb76ff1Sjsg * least one reference - the one that drm_dp_aux_register_devnode
2591bb76ff1Sjsg * created
2601bb76ff1Sjsg */
2611bb76ff1Sjsg mutex_lock(&aux_idr_mutex);
2621bb76ff1Sjsg idr_for_each_entry(&aux_idr, iter, id) {
2631bb76ff1Sjsg if (iter->aux == aux) {
2641bb76ff1Sjsg aux_dev = iter;
2651bb76ff1Sjsg break;
2661bb76ff1Sjsg }
2671bb76ff1Sjsg }
2681bb76ff1Sjsg mutex_unlock(&aux_idr_mutex);
2691bb76ff1Sjsg return aux_dev;
2701bb76ff1Sjsg }
2711bb76ff1Sjsg
drm_dp_aux_unregister_devnode(struct drm_dp_aux * aux)2721bb76ff1Sjsg void drm_dp_aux_unregister_devnode(struct drm_dp_aux *aux)
2731bb76ff1Sjsg {
2741bb76ff1Sjsg struct drm_dp_aux_dev *aux_dev;
2751bb76ff1Sjsg unsigned int minor;
2761bb76ff1Sjsg
2771bb76ff1Sjsg aux_dev = drm_dp_aux_dev_get_by_aux(aux);
2781bb76ff1Sjsg if (!aux_dev) /* attach must have failed */
2791bb76ff1Sjsg return;
2801bb76ff1Sjsg
2811bb76ff1Sjsg /*
2821bb76ff1Sjsg * As some AUX adapters may exist as platform devices which outlive their respective DRM
2831bb76ff1Sjsg * devices, we clear drm_dev to ensure that we never accidentally reference a stale pointer
2841bb76ff1Sjsg */
2851bb76ff1Sjsg aux->drm_dev = NULL;
2861bb76ff1Sjsg
2871bb76ff1Sjsg mutex_lock(&aux_idr_mutex);
2881bb76ff1Sjsg idr_remove(&aux_idr, aux_dev->index);
2891bb76ff1Sjsg mutex_unlock(&aux_idr_mutex);
2901bb76ff1Sjsg
2911bb76ff1Sjsg atomic_dec(&aux_dev->usecount);
2921bb76ff1Sjsg wait_var_event(&aux_dev->usecount, !atomic_read(&aux_dev->usecount));
2931bb76ff1Sjsg
2941bb76ff1Sjsg minor = aux_dev->index;
2951bb76ff1Sjsg if (aux_dev->dev)
2961bb76ff1Sjsg device_destroy(drm_dp_aux_dev_class,
2971bb76ff1Sjsg MKDEV(drm_dev_major, minor));
2981bb76ff1Sjsg
2991bb76ff1Sjsg DRM_DEBUG("drm_dp_aux_dev: aux [%s] unregistering\n", aux->name);
3001bb76ff1Sjsg kref_put(&aux_dev->refcount, release_drm_dp_aux_dev);
3011bb76ff1Sjsg }
3021bb76ff1Sjsg
drm_dp_aux_register_devnode(struct drm_dp_aux * aux)3031bb76ff1Sjsg int drm_dp_aux_register_devnode(struct drm_dp_aux *aux)
3041bb76ff1Sjsg {
3051bb76ff1Sjsg struct drm_dp_aux_dev *aux_dev;
3061bb76ff1Sjsg int res;
3071bb76ff1Sjsg
3081bb76ff1Sjsg aux_dev = alloc_drm_dp_aux_dev(aux);
3091bb76ff1Sjsg if (IS_ERR(aux_dev))
3101bb76ff1Sjsg return PTR_ERR(aux_dev);
3111bb76ff1Sjsg
3121bb76ff1Sjsg aux_dev->dev = device_create(drm_dp_aux_dev_class, aux->dev,
3131bb76ff1Sjsg MKDEV(drm_dev_major, aux_dev->index), NULL,
3141bb76ff1Sjsg "drm_dp_aux%d", aux_dev->index);
3151bb76ff1Sjsg if (IS_ERR(aux_dev->dev)) {
3161bb76ff1Sjsg res = PTR_ERR(aux_dev->dev);
3171bb76ff1Sjsg aux_dev->dev = NULL;
3181bb76ff1Sjsg goto error;
3191bb76ff1Sjsg }
3201bb76ff1Sjsg
3211bb76ff1Sjsg DRM_DEBUG("drm_dp_aux_dev: aux [%s] registered as minor %d\n",
3221bb76ff1Sjsg aux->name, aux_dev->index);
3231bb76ff1Sjsg return 0;
3241bb76ff1Sjsg error:
3251bb76ff1Sjsg drm_dp_aux_unregister_devnode(aux);
3261bb76ff1Sjsg return res;
3271bb76ff1Sjsg }
3281bb76ff1Sjsg
drm_dp_aux_dev_init(void)3291bb76ff1Sjsg int drm_dp_aux_dev_init(void)
3301bb76ff1Sjsg {
3311bb76ff1Sjsg int res;
3321bb76ff1Sjsg
333*f005ef32Sjsg drm_dp_aux_dev_class = class_create("drm_dp_aux_dev");
3341bb76ff1Sjsg if (IS_ERR(drm_dp_aux_dev_class)) {
3351bb76ff1Sjsg return PTR_ERR(drm_dp_aux_dev_class);
3361bb76ff1Sjsg }
3371bb76ff1Sjsg drm_dp_aux_dev_class->dev_groups = drm_dp_aux_groups;
3381bb76ff1Sjsg
3391bb76ff1Sjsg res = register_chrdev(0, "aux", &auxdev_fops);
3401bb76ff1Sjsg if (res < 0)
3411bb76ff1Sjsg goto out;
3421bb76ff1Sjsg drm_dev_major = res;
3431bb76ff1Sjsg
3441bb76ff1Sjsg return 0;
3451bb76ff1Sjsg out:
3461bb76ff1Sjsg class_destroy(drm_dp_aux_dev_class);
3471bb76ff1Sjsg return res;
3481bb76ff1Sjsg }
3491bb76ff1Sjsg
drm_dp_aux_dev_exit(void)3501bb76ff1Sjsg void drm_dp_aux_dev_exit(void)
3511bb76ff1Sjsg {
3521bb76ff1Sjsg unregister_chrdev(drm_dev_major, "aux");
3531bb76ff1Sjsg class_destroy(drm_dp_aux_dev_class);
3541bb76ff1Sjsg }
355