drm_dp_aux_dev.c 8.9 KB

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