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