drm_dp_aux_dev.c 9.2 KB

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