Path: blob/master/drivers/gpu/drm/display/drm_dp_aux_dev.c
26494 views
/*1* Copyright © 2015 Intel Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*22* Authors:23* Rafael Antognolli <[email protected]>24*25*/2627#include <linux/device.h>28#include <linux/fs.h>29#include <linux/init.h>30#include <linux/kernel.h>31#include <linux/module.h>32#include <linux/sched/signal.h>33#include <linux/slab.h>34#include <linux/uaccess.h>35#include <linux/uio.h>3637#include <drm/display/drm_dp_helper.h>38#include <drm/display/drm_dp_mst_helper.h>39#include <drm/drm_crtc.h>40#include <drm/drm_print.h>4142#include "drm_dp_helper_internal.h"4344struct drm_dp_aux_dev {45unsigned index;46struct drm_dp_aux *aux;47struct device *dev;48struct kref refcount;49atomic_t usecount;50};5152#define DRM_AUX_MINORS 25653#define AUX_MAX_OFFSET (1 << 20)54static DEFINE_IDR(aux_idr);55static DEFINE_MUTEX(aux_idr_mutex);56static struct class *drm_dp_aux_dev_class;57static int drm_dev_major = -1;5859static struct drm_dp_aux_dev *drm_dp_aux_dev_get_by_minor(unsigned index)60{61struct drm_dp_aux_dev *aux_dev = NULL;6263mutex_lock(&aux_idr_mutex);64aux_dev = idr_find(&aux_idr, index);65if (aux_dev && !kref_get_unless_zero(&aux_dev->refcount))66aux_dev = NULL;67mutex_unlock(&aux_idr_mutex);6869return aux_dev;70}7172static struct drm_dp_aux_dev *alloc_drm_dp_aux_dev(struct drm_dp_aux *aux)73{74struct drm_dp_aux_dev *aux_dev;75int index;7677aux_dev = kzalloc(sizeof(*aux_dev), GFP_KERNEL);78if (!aux_dev)79return ERR_PTR(-ENOMEM);80aux_dev->aux = aux;81atomic_set(&aux_dev->usecount, 1);82kref_init(&aux_dev->refcount);8384mutex_lock(&aux_idr_mutex);85index = idr_alloc(&aux_idr, aux_dev, 0, DRM_AUX_MINORS, GFP_KERNEL);86mutex_unlock(&aux_idr_mutex);87if (index < 0) {88kfree(aux_dev);89return ERR_PTR(index);90}91aux_dev->index = index;9293return aux_dev;94}9596static void release_drm_dp_aux_dev(struct kref *ref)97{98struct drm_dp_aux_dev *aux_dev =99container_of(ref, struct drm_dp_aux_dev, refcount);100101kfree(aux_dev);102}103104static ssize_t name_show(struct device *dev,105struct device_attribute *attr, char *buf)106{107ssize_t res;108struct drm_dp_aux_dev *aux_dev =109drm_dp_aux_dev_get_by_minor(MINOR(dev->devt));110111if (!aux_dev)112return -ENODEV;113114res = sprintf(buf, "%s\n", aux_dev->aux->name);115kref_put(&aux_dev->refcount, release_drm_dp_aux_dev);116117return res;118}119static DEVICE_ATTR_RO(name);120121static struct attribute *drm_dp_aux_attrs[] = {122&dev_attr_name.attr,123NULL,124};125ATTRIBUTE_GROUPS(drm_dp_aux);126127static int auxdev_open(struct inode *inode, struct file *file)128{129unsigned int minor = iminor(inode);130struct drm_dp_aux_dev *aux_dev;131132aux_dev = drm_dp_aux_dev_get_by_minor(minor);133if (!aux_dev)134return -ENODEV;135136file->private_data = aux_dev;137return 0;138}139140static loff_t auxdev_llseek(struct file *file, loff_t offset, int whence)141{142return fixed_size_llseek(file, offset, whence, AUX_MAX_OFFSET);143}144145static ssize_t auxdev_read_iter(struct kiocb *iocb, struct iov_iter *to)146{147struct drm_dp_aux_dev *aux_dev = iocb->ki_filp->private_data;148loff_t pos = iocb->ki_pos;149ssize_t res = 0;150151if (!atomic_inc_not_zero(&aux_dev->usecount))152return -ENODEV;153154iov_iter_truncate(to, AUX_MAX_OFFSET - pos);155156while (iov_iter_count(to)) {157uint8_t buf[DP_AUX_MAX_PAYLOAD_BYTES];158ssize_t todo = min(iov_iter_count(to), sizeof(buf));159160if (signal_pending(current)) {161res = -ERESTARTSYS;162break;163}164165res = drm_dp_dpcd_read(aux_dev->aux, pos, buf, todo);166167if (res <= 0)168break;169170if (copy_to_iter(buf, res, to) != res) {171res = -EFAULT;172break;173}174175pos += res;176}177178if (pos != iocb->ki_pos)179res = pos - iocb->ki_pos;180iocb->ki_pos = pos;181182if (atomic_dec_and_test(&aux_dev->usecount))183wake_up_var(&aux_dev->usecount);184185return res;186}187188static ssize_t auxdev_write_iter(struct kiocb *iocb, struct iov_iter *from)189{190struct drm_dp_aux_dev *aux_dev = iocb->ki_filp->private_data;191loff_t pos = iocb->ki_pos;192ssize_t res = 0;193194if (!atomic_inc_not_zero(&aux_dev->usecount))195return -ENODEV;196197iov_iter_truncate(from, AUX_MAX_OFFSET - pos);198199while (iov_iter_count(from)) {200uint8_t buf[DP_AUX_MAX_PAYLOAD_BYTES];201ssize_t todo = min(iov_iter_count(from), sizeof(buf));202203if (signal_pending(current)) {204res = -ERESTARTSYS;205break;206}207208if (!copy_from_iter_full(buf, todo, from)) {209res = -EFAULT;210break;211}212213res = drm_dp_dpcd_write(aux_dev->aux, pos, buf, todo);214215if (res <= 0)216break;217218pos += res;219}220221if (pos != iocb->ki_pos)222res = pos - iocb->ki_pos;223iocb->ki_pos = pos;224225if (atomic_dec_and_test(&aux_dev->usecount))226wake_up_var(&aux_dev->usecount);227228return res;229}230231static int auxdev_release(struct inode *inode, struct file *file)232{233struct drm_dp_aux_dev *aux_dev = file->private_data;234235kref_put(&aux_dev->refcount, release_drm_dp_aux_dev);236return 0;237}238239static const struct file_operations auxdev_fops = {240.owner = THIS_MODULE,241.llseek = auxdev_llseek,242.read_iter = auxdev_read_iter,243.write_iter = auxdev_write_iter,244.open = auxdev_open,245.release = auxdev_release,246};247248#define to_auxdev(d) container_of(d, struct drm_dp_aux_dev, aux)249250static struct drm_dp_aux_dev *drm_dp_aux_dev_get_by_aux(struct drm_dp_aux *aux)251{252struct drm_dp_aux_dev *iter, *aux_dev = NULL;253int id;254255/* don't increase kref count here because this function should only be256* used by drm_dp_aux_unregister_devnode. Thus, it will always have at257* least one reference - the one that drm_dp_aux_register_devnode258* created259*/260mutex_lock(&aux_idr_mutex);261idr_for_each_entry(&aux_idr, iter, id) {262if (iter->aux == aux) {263aux_dev = iter;264break;265}266}267mutex_unlock(&aux_idr_mutex);268return aux_dev;269}270271void drm_dp_aux_unregister_devnode(struct drm_dp_aux *aux)272{273struct drm_dp_aux_dev *aux_dev;274unsigned int minor;275276aux_dev = drm_dp_aux_dev_get_by_aux(aux);277if (!aux_dev) /* attach must have failed */278return;279280/*281* As some AUX adapters may exist as platform devices which outlive their respective DRM282* devices, we clear drm_dev to ensure that we never accidentally reference a stale pointer283*/284aux->drm_dev = NULL;285286mutex_lock(&aux_idr_mutex);287idr_remove(&aux_idr, aux_dev->index);288mutex_unlock(&aux_idr_mutex);289290atomic_dec(&aux_dev->usecount);291wait_var_event(&aux_dev->usecount, !atomic_read(&aux_dev->usecount));292293minor = aux_dev->index;294if (aux_dev->dev)295device_destroy(drm_dp_aux_dev_class,296MKDEV(drm_dev_major, minor));297298DRM_DEBUG("drm_dp_aux_dev: aux [%s] unregistering\n", aux->name);299kref_put(&aux_dev->refcount, release_drm_dp_aux_dev);300}301302int drm_dp_aux_register_devnode(struct drm_dp_aux *aux)303{304struct drm_dp_aux_dev *aux_dev;305int res;306307aux_dev = alloc_drm_dp_aux_dev(aux);308if (IS_ERR(aux_dev))309return PTR_ERR(aux_dev);310311aux_dev->dev = device_create(drm_dp_aux_dev_class, aux->dev,312MKDEV(drm_dev_major, aux_dev->index), NULL,313"drm_dp_aux%d", aux_dev->index);314if (IS_ERR(aux_dev->dev)) {315res = PTR_ERR(aux_dev->dev);316aux_dev->dev = NULL;317goto error;318}319320DRM_DEBUG("drm_dp_aux_dev: aux [%s] registered as minor %d\n",321aux->name, aux_dev->index);322return 0;323error:324drm_dp_aux_unregister_devnode(aux);325return res;326}327328int drm_dp_aux_dev_init(void)329{330int res;331332drm_dp_aux_dev_class = class_create("drm_dp_aux_dev");333if (IS_ERR(drm_dp_aux_dev_class)) {334return PTR_ERR(drm_dp_aux_dev_class);335}336drm_dp_aux_dev_class->dev_groups = drm_dp_aux_groups;337338res = register_chrdev(0, "aux", &auxdev_fops);339if (res < 0)340goto out;341drm_dev_major = res;342343return 0;344out:345class_destroy(drm_dp_aux_dev_class);346return res;347}348349void drm_dp_aux_dev_exit(void)350{351unregister_chrdev(drm_dev_major, "aux");352class_destroy(drm_dp_aux_dev_class);353}354355356