0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028 #include <linux/device.h>
0029 #include <linux/fs.h>
0030 #include <linux/init.h>
0031 #include <linux/kernel.h>
0032 #include <linux/module.h>
0033 #include <linux/sched/signal.h>
0034 #include <linux/slab.h>
0035 #include <linux/uaccess.h>
0036 #include <linux/uio.h>
0037
0038 #include <drm/display/drm_dp_helper.h>
0039 #include <drm/display/drm_dp_mst_helper.h>
0040 #include <drm/drm_crtc.h>
0041 #include <drm/drm_print.h>
0042
0043 #include "drm_dp_helper_internal.h"
0044
0045 struct drm_dp_aux_dev {
0046 unsigned index;
0047 struct drm_dp_aux *aux;
0048 struct device *dev;
0049 struct kref refcount;
0050 atomic_t usecount;
0051 };
0052
0053 #define DRM_AUX_MINORS 256
0054 #define AUX_MAX_OFFSET (1 << 20)
0055 static DEFINE_IDR(aux_idr);
0056 static DEFINE_MUTEX(aux_idr_mutex);
0057 static struct class *drm_dp_aux_dev_class;
0058 static int drm_dev_major = -1;
0059
0060 static struct drm_dp_aux_dev *drm_dp_aux_dev_get_by_minor(unsigned index)
0061 {
0062 struct drm_dp_aux_dev *aux_dev = NULL;
0063
0064 mutex_lock(&aux_idr_mutex);
0065 aux_dev = idr_find(&aux_idr, index);
0066 if (aux_dev && !kref_get_unless_zero(&aux_dev->refcount))
0067 aux_dev = NULL;
0068 mutex_unlock(&aux_idr_mutex);
0069
0070 return aux_dev;
0071 }
0072
0073 static struct drm_dp_aux_dev *alloc_drm_dp_aux_dev(struct drm_dp_aux *aux)
0074 {
0075 struct drm_dp_aux_dev *aux_dev;
0076 int index;
0077
0078 aux_dev = kzalloc(sizeof(*aux_dev), GFP_KERNEL);
0079 if (!aux_dev)
0080 return ERR_PTR(-ENOMEM);
0081 aux_dev->aux = aux;
0082 atomic_set(&aux_dev->usecount, 1);
0083 kref_init(&aux_dev->refcount);
0084
0085 mutex_lock(&aux_idr_mutex);
0086 index = idr_alloc(&aux_idr, aux_dev, 0, DRM_AUX_MINORS, GFP_KERNEL);
0087 mutex_unlock(&aux_idr_mutex);
0088 if (index < 0) {
0089 kfree(aux_dev);
0090 return ERR_PTR(index);
0091 }
0092 aux_dev->index = index;
0093
0094 return aux_dev;
0095 }
0096
0097 static void release_drm_dp_aux_dev(struct kref *ref)
0098 {
0099 struct drm_dp_aux_dev *aux_dev =
0100 container_of(ref, struct drm_dp_aux_dev, refcount);
0101
0102 kfree(aux_dev);
0103 }
0104
0105 static ssize_t name_show(struct device *dev,
0106 struct device_attribute *attr, char *buf)
0107 {
0108 ssize_t res;
0109 struct drm_dp_aux_dev *aux_dev =
0110 drm_dp_aux_dev_get_by_minor(MINOR(dev->devt));
0111
0112 if (!aux_dev)
0113 return -ENODEV;
0114
0115 res = sprintf(buf, "%s\n", aux_dev->aux->name);
0116 kref_put(&aux_dev->refcount, release_drm_dp_aux_dev);
0117
0118 return res;
0119 }
0120 static DEVICE_ATTR_RO(name);
0121
0122 static struct attribute *drm_dp_aux_attrs[] = {
0123 &dev_attr_name.attr,
0124 NULL,
0125 };
0126 ATTRIBUTE_GROUPS(drm_dp_aux);
0127
0128 static int auxdev_open(struct inode *inode, struct file *file)
0129 {
0130 unsigned int minor = iminor(inode);
0131 struct drm_dp_aux_dev *aux_dev;
0132
0133 aux_dev = drm_dp_aux_dev_get_by_minor(minor);
0134 if (!aux_dev)
0135 return -ENODEV;
0136
0137 file->private_data = aux_dev;
0138 return 0;
0139 }
0140
0141 static loff_t auxdev_llseek(struct file *file, loff_t offset, int whence)
0142 {
0143 return fixed_size_llseek(file, offset, whence, AUX_MAX_OFFSET);
0144 }
0145
0146 static ssize_t auxdev_read_iter(struct kiocb *iocb, struct iov_iter *to)
0147 {
0148 struct drm_dp_aux_dev *aux_dev = iocb->ki_filp->private_data;
0149 loff_t pos = iocb->ki_pos;
0150 ssize_t res = 0;
0151
0152 if (!atomic_inc_not_zero(&aux_dev->usecount))
0153 return -ENODEV;
0154
0155 iov_iter_truncate(to, AUX_MAX_OFFSET - pos);
0156
0157 while (iov_iter_count(to)) {
0158 uint8_t buf[DP_AUX_MAX_PAYLOAD_BYTES];
0159 ssize_t todo = min(iov_iter_count(to), sizeof(buf));
0160
0161 if (signal_pending(current)) {
0162 res = -ERESTARTSYS;
0163 break;
0164 }
0165
0166 res = drm_dp_dpcd_read(aux_dev->aux, pos, buf, todo);
0167
0168 if (res <= 0)
0169 break;
0170
0171 if (copy_to_iter(buf, res, to) != res) {
0172 res = -EFAULT;
0173 break;
0174 }
0175
0176 pos += res;
0177 }
0178
0179 if (pos != iocb->ki_pos)
0180 res = pos - iocb->ki_pos;
0181 iocb->ki_pos = pos;
0182
0183 if (atomic_dec_and_test(&aux_dev->usecount))
0184 wake_up_var(&aux_dev->usecount);
0185
0186 return res;
0187 }
0188
0189 static ssize_t auxdev_write_iter(struct kiocb *iocb, struct iov_iter *from)
0190 {
0191 struct drm_dp_aux_dev *aux_dev = iocb->ki_filp->private_data;
0192 loff_t pos = iocb->ki_pos;
0193 ssize_t res = 0;
0194
0195 if (!atomic_inc_not_zero(&aux_dev->usecount))
0196 return -ENODEV;
0197
0198 iov_iter_truncate(from, AUX_MAX_OFFSET - pos);
0199
0200 while (iov_iter_count(from)) {
0201 uint8_t buf[DP_AUX_MAX_PAYLOAD_BYTES];
0202 ssize_t todo = min(iov_iter_count(from), sizeof(buf));
0203
0204 if (signal_pending(current)) {
0205 res = -ERESTARTSYS;
0206 break;
0207 }
0208
0209 if (!copy_from_iter_full(buf, todo, from)) {
0210 res = -EFAULT;
0211 break;
0212 }
0213
0214 res = drm_dp_dpcd_write(aux_dev->aux, pos, buf, todo);
0215
0216 if (res <= 0)
0217 break;
0218
0219 pos += res;
0220 }
0221
0222 if (pos != iocb->ki_pos)
0223 res = pos - iocb->ki_pos;
0224 iocb->ki_pos = pos;
0225
0226 if (atomic_dec_and_test(&aux_dev->usecount))
0227 wake_up_var(&aux_dev->usecount);
0228
0229 return res;
0230 }
0231
0232 static int auxdev_release(struct inode *inode, struct file *file)
0233 {
0234 struct drm_dp_aux_dev *aux_dev = file->private_data;
0235
0236 kref_put(&aux_dev->refcount, release_drm_dp_aux_dev);
0237 return 0;
0238 }
0239
0240 static const struct file_operations auxdev_fops = {
0241 .owner = THIS_MODULE,
0242 .llseek = auxdev_llseek,
0243 .read_iter = auxdev_read_iter,
0244 .write_iter = auxdev_write_iter,
0245 .open = auxdev_open,
0246 .release = auxdev_release,
0247 };
0248
0249 #define to_auxdev(d) container_of(d, struct drm_dp_aux_dev, aux)
0250
0251 static struct drm_dp_aux_dev *drm_dp_aux_dev_get_by_aux(struct drm_dp_aux *aux)
0252 {
0253 struct drm_dp_aux_dev *iter, *aux_dev = NULL;
0254 int id;
0255
0256
0257
0258
0259
0260
0261 mutex_lock(&aux_idr_mutex);
0262 idr_for_each_entry(&aux_idr, iter, id) {
0263 if (iter->aux == aux) {
0264 aux_dev = iter;
0265 break;
0266 }
0267 }
0268 mutex_unlock(&aux_idr_mutex);
0269 return aux_dev;
0270 }
0271
0272 void drm_dp_aux_unregister_devnode(struct drm_dp_aux *aux)
0273 {
0274 struct drm_dp_aux_dev *aux_dev;
0275 unsigned int minor;
0276
0277 aux_dev = drm_dp_aux_dev_get_by_aux(aux);
0278 if (!aux_dev)
0279 return;
0280
0281
0282
0283
0284
0285 aux->drm_dev = NULL;
0286
0287 mutex_lock(&aux_idr_mutex);
0288 idr_remove(&aux_idr, aux_dev->index);
0289 mutex_unlock(&aux_idr_mutex);
0290
0291 atomic_dec(&aux_dev->usecount);
0292 wait_var_event(&aux_dev->usecount, !atomic_read(&aux_dev->usecount));
0293
0294 minor = aux_dev->index;
0295 if (aux_dev->dev)
0296 device_destroy(drm_dp_aux_dev_class,
0297 MKDEV(drm_dev_major, minor));
0298
0299 DRM_DEBUG("drm_dp_aux_dev: aux [%s] unregistering\n", aux->name);
0300 kref_put(&aux_dev->refcount, release_drm_dp_aux_dev);
0301 }
0302
0303 int drm_dp_aux_register_devnode(struct drm_dp_aux *aux)
0304 {
0305 struct drm_dp_aux_dev *aux_dev;
0306 int res;
0307
0308 aux_dev = alloc_drm_dp_aux_dev(aux);
0309 if (IS_ERR(aux_dev))
0310 return PTR_ERR(aux_dev);
0311
0312 aux_dev->dev = device_create(drm_dp_aux_dev_class, aux->dev,
0313 MKDEV(drm_dev_major, aux_dev->index), NULL,
0314 "drm_dp_aux%d", aux_dev->index);
0315 if (IS_ERR(aux_dev->dev)) {
0316 res = PTR_ERR(aux_dev->dev);
0317 aux_dev->dev = NULL;
0318 goto error;
0319 }
0320
0321 DRM_DEBUG("drm_dp_aux_dev: aux [%s] registered as minor %d\n",
0322 aux->name, aux_dev->index);
0323 return 0;
0324 error:
0325 drm_dp_aux_unregister_devnode(aux);
0326 return res;
0327 }
0328
0329 int drm_dp_aux_dev_init(void)
0330 {
0331 int res;
0332
0333 drm_dp_aux_dev_class = class_create(THIS_MODULE, "drm_dp_aux_dev");
0334 if (IS_ERR(drm_dp_aux_dev_class)) {
0335 return PTR_ERR(drm_dp_aux_dev_class);
0336 }
0337 drm_dp_aux_dev_class->dev_groups = drm_dp_aux_groups;
0338
0339 res = register_chrdev(0, "aux", &auxdev_fops);
0340 if (res < 0)
0341 goto out;
0342 drm_dev_major = res;
0343
0344 return 0;
0345 out:
0346 class_destroy(drm_dp_aux_dev_class);
0347 return res;
0348 }
0349
0350 void drm_dp_aux_dev_exit(void)
0351 {
0352 unregister_chrdev(drm_dev_major, "aux");
0353 class_destroy(drm_dp_aux_dev_class);
0354 }