drm_dp_aux_dev.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. if (signal_pending(current)) {
  136. res = num_bytes_processed ?
  137. num_bytes_processed : -ERESTARTSYS;
  138. goto out;
  139. }
  140. res = drm_dp_dpcd_read(aux_dev->aux, *offset, localbuf, todo);
  141. if (res <= 0) {
  142. res = num_bytes_processed ? num_bytes_processed : res;
  143. goto out;
  144. }
  145. if (__copy_to_user(buf + num_bytes_processed, localbuf, res)) {
  146. res = num_bytes_processed ?
  147. num_bytes_processed : -EFAULT;
  148. goto out;
  149. }
  150. bytes_pending -= res;
  151. *offset += res;
  152. num_bytes_processed += res;
  153. res = num_bytes_processed;
  154. }
  155. out:
  156. atomic_dec(&aux_dev->usecount);
  157. wake_up_atomic_t(&aux_dev->usecount);
  158. return res;
  159. }
  160. static ssize_t auxdev_write(struct file *file, const char __user *buf,
  161. size_t count, loff_t *offset)
  162. {
  163. size_t bytes_pending, num_bytes_processed = 0;
  164. struct drm_dp_aux_dev *aux_dev = file->private_data;
  165. ssize_t res = 0;
  166. if (!atomic_inc_not_zero(&aux_dev->usecount))
  167. return -ENODEV;
  168. bytes_pending = min((loff_t)count, AUX_MAX_OFFSET - *offset);
  169. if (!access_ok(VERIFY_READ, buf, bytes_pending)) {
  170. res = -EFAULT;
  171. goto out;
  172. }
  173. while (bytes_pending > 0) {
  174. uint8_t localbuf[DP_AUX_MAX_PAYLOAD_BYTES];
  175. ssize_t todo = min_t(size_t, bytes_pending, sizeof(localbuf));
  176. if (signal_pending(current)) {
  177. res = num_bytes_processed ?
  178. num_bytes_processed : -ERESTARTSYS;
  179. goto out;
  180. }
  181. if (__copy_from_user(localbuf,
  182. buf + num_bytes_processed, todo)) {
  183. res = num_bytes_processed ?
  184. num_bytes_processed : -EFAULT;
  185. goto out;
  186. }
  187. res = drm_dp_dpcd_write(aux_dev->aux, *offset, localbuf, todo);
  188. if (res <= 0) {
  189. res = num_bytes_processed ? num_bytes_processed : res;
  190. goto out;
  191. }
  192. bytes_pending -= res;
  193. *offset += res;
  194. num_bytes_processed += res;
  195. res = num_bytes_processed;
  196. }
  197. out:
  198. atomic_dec(&aux_dev->usecount);
  199. wake_up_atomic_t(&aux_dev->usecount);
  200. return res;
  201. }
  202. static int auxdev_release(struct inode *inode, struct file *file)
  203. {
  204. struct drm_dp_aux_dev *aux_dev = file->private_data;
  205. kref_put(&aux_dev->refcount, release_drm_dp_aux_dev);
  206. return 0;
  207. }
  208. static const struct file_operations auxdev_fops = {
  209. .owner = THIS_MODULE,
  210. .llseek = auxdev_llseek,
  211. .read = auxdev_read,
  212. .write = auxdev_write,
  213. .open = auxdev_open,
  214. .release = auxdev_release,
  215. };
  216. #define to_auxdev(d) container_of(d, struct drm_dp_aux_dev, aux)
  217. static struct drm_dp_aux_dev *drm_dp_aux_dev_get_by_aux(struct drm_dp_aux *aux)
  218. {
  219. struct drm_dp_aux_dev *iter, *aux_dev = NULL;
  220. int id;
  221. /* don't increase kref count here because this function should only be
  222. * used by drm_dp_aux_unregister_devnode. Thus, it will always have at
  223. * least one reference - the one that drm_dp_aux_register_devnode
  224. * created
  225. */
  226. mutex_lock(&aux_idr_mutex);
  227. idr_for_each_entry(&aux_idr, iter, id) {
  228. if (iter->aux == aux) {
  229. aux_dev = iter;
  230. break;
  231. }
  232. }
  233. mutex_unlock(&aux_idr_mutex);
  234. return aux_dev;
  235. }
  236. static int auxdev_wait_atomic_t(atomic_t *p)
  237. {
  238. schedule();
  239. return 0;
  240. }
  241. /**
  242. * drm_dp_aux_unregister_devnode() - unregister a devnode for this aux channel
  243. * @aux: DisplayPort AUX channel
  244. *
  245. * Returns 0 on success or a negative error code on failure.
  246. */
  247. void drm_dp_aux_unregister_devnode(struct drm_dp_aux *aux)
  248. {
  249. struct drm_dp_aux_dev *aux_dev;
  250. unsigned int minor;
  251. aux_dev = drm_dp_aux_dev_get_by_aux(aux);
  252. if (!aux_dev) /* attach must have failed */
  253. return;
  254. mutex_lock(&aux_idr_mutex);
  255. idr_remove(&aux_idr, aux_dev->index);
  256. mutex_unlock(&aux_idr_mutex);
  257. atomic_dec(&aux_dev->usecount);
  258. wait_on_atomic_t(&aux_dev->usecount, auxdev_wait_atomic_t,
  259. TASK_UNINTERRUPTIBLE);
  260. minor = aux_dev->index;
  261. if (aux_dev->dev)
  262. device_destroy(drm_dp_aux_dev_class,
  263. MKDEV(drm_dev_major, minor));
  264. DRM_DEBUG("drm_dp_aux_dev: aux [%s] unregistering\n", aux->name);
  265. kref_put(&aux_dev->refcount, release_drm_dp_aux_dev);
  266. }
  267. EXPORT_SYMBOL(drm_dp_aux_unregister_devnode);
  268. /**
  269. * drm_dp_aux_register_devnode() - register a devnode for this aux channel
  270. * @aux: DisplayPort AUX channel
  271. *
  272. * Returns 0 on success or a negative error code on failure.
  273. */
  274. int drm_dp_aux_register_devnode(struct drm_dp_aux *aux)
  275. {
  276. struct drm_dp_aux_dev *aux_dev;
  277. int res;
  278. aux_dev = alloc_drm_dp_aux_dev(aux);
  279. if (IS_ERR(aux_dev))
  280. return PTR_ERR(aux_dev);
  281. aux_dev->dev = device_create(drm_dp_aux_dev_class, aux->dev,
  282. MKDEV(drm_dev_major, aux_dev->index), NULL,
  283. "drm_dp_aux%d", aux_dev->index);
  284. if (IS_ERR(aux_dev->dev)) {
  285. res = PTR_ERR(aux_dev->dev);
  286. aux_dev->dev = NULL;
  287. goto error;
  288. }
  289. DRM_DEBUG("drm_dp_aux_dev: aux [%s] registered as minor %d\n",
  290. aux->name, aux_dev->index);
  291. return 0;
  292. error:
  293. drm_dp_aux_unregister_devnode(aux);
  294. return res;
  295. }
  296. EXPORT_SYMBOL(drm_dp_aux_register_devnode);
  297. int drm_dp_aux_dev_init(void)
  298. {
  299. int res;
  300. drm_dp_aux_dev_class = class_create(THIS_MODULE, "drm_dp_aux_dev");
  301. if (IS_ERR(drm_dp_aux_dev_class)) {
  302. res = PTR_ERR(drm_dp_aux_dev_class);
  303. goto out;
  304. }
  305. drm_dp_aux_dev_class->dev_groups = drm_dp_aux_groups;
  306. res = register_chrdev(0, "aux", &auxdev_fops);
  307. if (res < 0)
  308. goto out;
  309. drm_dev_major = res;
  310. return 0;
  311. out:
  312. class_destroy(drm_dp_aux_dev_class);
  313. return res;
  314. }
  315. EXPORT_SYMBOL(drm_dp_aux_dev_init);
  316. void drm_dp_aux_dev_exit(void)
  317. {
  318. unregister_chrdev(drm_dev_major, "aux");
  319. class_destroy(drm_dp_aux_dev_class);
  320. }
  321. EXPORT_SYMBOL(drm_dp_aux_dev_exit);