selection.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/slab.h> /* for kmalloc */
  3. #include <linux/consolemap.h>
  4. #include <linux/interrupt.h>
  5. #include <linux/sched.h>
  6. #include <linux/device.h> /* for dev_warn */
  7. #include <linux/selection.h>
  8. #include <linux/workqueue.h>
  9. #include <linux/tty.h>
  10. #include <linux/tty_flip.h>
  11. #include <linux/atomic.h>
  12. #include <linux/console.h>
  13. #include "speakup.h"
  14. unsigned short spk_xs, spk_ys, spk_xe, spk_ye; /* our region points */
  15. struct vc_data *spk_sel_cons;
  16. struct speakup_selection_work {
  17. struct work_struct work;
  18. struct tiocl_selection sel;
  19. struct tty_struct *tty;
  20. };
  21. static void __speakup_set_selection(struct work_struct *work)
  22. {
  23. struct speakup_selection_work *ssw =
  24. container_of(work, struct speakup_selection_work, work);
  25. struct tty_struct *tty;
  26. struct tiocl_selection sel;
  27. sel = ssw->sel;
  28. /* this ensures we copy sel before releasing the lock below */
  29. rmb();
  30. /* release the lock by setting tty of the struct to NULL */
  31. tty = xchg(&ssw->tty, NULL);
  32. if (spk_sel_cons != vc_cons[fg_console].d) {
  33. spk_sel_cons = vc_cons[fg_console].d;
  34. pr_warn("Selection: mark console not the same as cut\n");
  35. goto unref;
  36. }
  37. console_lock();
  38. clear_selection();
  39. console_unlock();
  40. set_selection_kernel(&sel, tty);
  41. unref:
  42. tty_kref_put(tty);
  43. }
  44. static struct speakup_selection_work speakup_sel_work = {
  45. .work = __WORK_INITIALIZER(speakup_sel_work.work,
  46. __speakup_set_selection)
  47. };
  48. int speakup_set_selection(struct tty_struct *tty)
  49. {
  50. /* we get kref here first in order to avoid a subtle race when
  51. * cancelling selection work. getting kref first establishes the
  52. * invariant that if speakup_sel_work.tty is not NULL when
  53. * speakup_cancel_selection() is called, it must be the case that a put
  54. * kref is pending.
  55. */
  56. tty_kref_get(tty);
  57. if (cmpxchg(&speakup_sel_work.tty, NULL, tty)) {
  58. tty_kref_put(tty);
  59. return -EBUSY;
  60. }
  61. /* now we have the 'lock' by setting tty member of
  62. * speakup_selection_work. wmb() ensures that writes to
  63. * speakup_sel_work don't happen before cmpxchg() above.
  64. */
  65. wmb();
  66. speakup_sel_work.sel.xs = spk_xs + 1;
  67. speakup_sel_work.sel.ys = spk_ys + 1;
  68. speakup_sel_work.sel.xe = spk_xe + 1;
  69. speakup_sel_work.sel.ye = spk_ye + 1;
  70. speakup_sel_work.sel.sel_mode = TIOCL_SELCHAR;
  71. schedule_work_on(WORK_CPU_UNBOUND, &speakup_sel_work.work);
  72. return 0;
  73. }
  74. void speakup_cancel_selection(void)
  75. {
  76. struct tty_struct *tty;
  77. cancel_work_sync(&speakup_sel_work.work);
  78. /* setting to null so that if work fails to run and we cancel it,
  79. * we can run it again without getting EBUSY forever from there on.
  80. * we need to use xchg here to avoid race with speakup_set_selection()
  81. */
  82. tty = xchg(&speakup_sel_work.tty, NULL);
  83. if (tty)
  84. tty_kref_put(tty);
  85. }
  86. static void __speakup_paste_selection(struct work_struct *work)
  87. {
  88. struct speakup_selection_work *ssw =
  89. container_of(work, struct speakup_selection_work, work);
  90. struct tty_struct *tty = xchg(&ssw->tty, NULL);
  91. paste_selection(tty);
  92. tty_kref_put(tty);
  93. }
  94. static struct speakup_selection_work speakup_paste_work = {
  95. .work = __WORK_INITIALIZER(speakup_paste_work.work,
  96. __speakup_paste_selection)
  97. };
  98. int speakup_paste_selection(struct tty_struct *tty)
  99. {
  100. tty_kref_get(tty);
  101. if (cmpxchg(&speakup_paste_work.tty, NULL, tty)) {
  102. tty_kref_put(tty);
  103. return -EBUSY;
  104. }
  105. schedule_work_on(WORK_CPU_UNBOUND, &speakup_paste_work.work);
  106. return 0;
  107. }
  108. void speakup_cancel_paste(void)
  109. {
  110. struct tty_struct *tty;
  111. cancel_work_sync(&speakup_paste_work.work);
  112. tty = xchg(&speakup_paste_work.tty, NULL);
  113. if (tty)
  114. tty_kref_put(tty);
  115. }