drm_dp_aux_dev.c 8.2 KB

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