spk_ttyio.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/types.h>
  3. #include <linux/tty.h>
  4. #include <linux/tty_flip.h>
  5. #include <linux/slab.h>
  6. #include "speakup.h"
  7. #include "spk_types.h"
  8. #include "spk_priv.h"
  9. struct spk_ldisc_data {
  10. char buf;
  11. struct completion completion;
  12. bool buf_free;
  13. struct spk_synth *synth;
  14. };
  15. /*
  16. * This allows to catch within spk_ttyio_ldisc_open whether it is getting set
  17. * on for a speakup-driven device.
  18. */
  19. static struct tty_struct *speakup_tty;
  20. /* This mutex serializes the use of such global speakup_tty variable */
  21. static DEFINE_MUTEX(speakup_tty_mutex);
  22. static int ser_to_dev(int ser, dev_t *dev_no)
  23. {
  24. if (ser < 0 || ser > (255 - 64)) {
  25. pr_err("speakup: Invalid ser param. Must be between 0 and 191 inclusive.\n");
  26. return -EINVAL;
  27. }
  28. *dev_no = MKDEV(4, (64 + ser));
  29. return 0;
  30. }
  31. static int get_dev_to_use(struct spk_synth *synth, dev_t *dev_no)
  32. {
  33. /* use ser only when dev is not specified */
  34. if (strcmp(synth->dev_name, SYNTH_DEFAULT_DEV) ||
  35. synth->ser == SYNTH_DEFAULT_SER)
  36. return tty_dev_name_to_number(synth->dev_name, dev_no);
  37. return ser_to_dev(synth->ser, dev_no);
  38. }
  39. static int spk_ttyio_ldisc_open(struct tty_struct *tty)
  40. {
  41. struct spk_ldisc_data *ldisc_data;
  42. if (tty != speakup_tty)
  43. /* Somebody tried to use this line discipline outside speakup */
  44. return -ENODEV;
  45. if (!tty->ops->write)
  46. return -EOPNOTSUPP;
  47. ldisc_data = kmalloc(sizeof(*ldisc_data), GFP_KERNEL);
  48. if (!ldisc_data)
  49. return -ENOMEM;
  50. init_completion(&ldisc_data->completion);
  51. ldisc_data->buf_free = true;
  52. tty->disc_data = ldisc_data;
  53. return 0;
  54. }
  55. static void spk_ttyio_ldisc_close(struct tty_struct *tty)
  56. {
  57. kfree(tty->disc_data);
  58. }
  59. static int spk_ttyio_receive_buf2(struct tty_struct *tty,
  60. const unsigned char *cp,
  61. const char *fp, int count)
  62. {
  63. struct spk_ldisc_data *ldisc_data = tty->disc_data;
  64. struct spk_synth *synth = ldisc_data->synth;
  65. if (synth->read_buff_add) {
  66. int i;
  67. for (i = 0; i < count; i++)
  68. synth->read_buff_add(cp[i]);
  69. return count;
  70. }
  71. if (!ldisc_data->buf_free)
  72. /* ttyio_in will tty_flip_buffer_push */
  73. return 0;
  74. /* Make sure the consumer has read buf before we have seen
  75. * buf_free == true and overwrite buf
  76. */
  77. mb();
  78. ldisc_data->buf = cp[0];
  79. ldisc_data->buf_free = false;
  80. complete(&ldisc_data->completion);
  81. return 1;
  82. }
  83. static struct tty_ldisc_ops spk_ttyio_ldisc_ops = {
  84. .owner = THIS_MODULE,
  85. .num = N_SPEAKUP,
  86. .name = "speakup_ldisc",
  87. .open = spk_ttyio_ldisc_open,
  88. .close = spk_ttyio_ldisc_close,
  89. .receive_buf2 = spk_ttyio_receive_buf2,
  90. };
  91. static int spk_ttyio_out(struct spk_synth *in_synth, const char ch);
  92. static int spk_ttyio_out_unicode(struct spk_synth *in_synth, u16 ch);
  93. static void spk_ttyio_send_xchar(struct spk_synth *in_synth, char ch);
  94. static void spk_ttyio_tiocmset(struct spk_synth *in_synth, unsigned int set, unsigned int clear);
  95. static unsigned char spk_ttyio_in(struct spk_synth *in_synth);
  96. static unsigned char spk_ttyio_in_nowait(struct spk_synth *in_synth);
  97. static void spk_ttyio_flush_buffer(struct spk_synth *in_synth);
  98. static int spk_ttyio_wait_for_xmitr(struct spk_synth *in_synth);
  99. struct spk_io_ops spk_ttyio_ops = {
  100. .synth_out = spk_ttyio_out,
  101. .synth_out_unicode = spk_ttyio_out_unicode,
  102. .send_xchar = spk_ttyio_send_xchar,
  103. .tiocmset = spk_ttyio_tiocmset,
  104. .synth_in = spk_ttyio_in,
  105. .synth_in_nowait = spk_ttyio_in_nowait,
  106. .flush_buffer = spk_ttyio_flush_buffer,
  107. .wait_for_xmitr = spk_ttyio_wait_for_xmitr,
  108. };
  109. EXPORT_SYMBOL_GPL(spk_ttyio_ops);
  110. static inline void get_termios(struct tty_struct *tty,
  111. struct ktermios *out_termios)
  112. {
  113. down_read(&tty->termios_rwsem);
  114. *out_termios = tty->termios;
  115. up_read(&tty->termios_rwsem);
  116. }
  117. static int spk_ttyio_initialise_ldisc(struct spk_synth *synth)
  118. {
  119. int ret = 0;
  120. struct tty_struct *tty;
  121. struct ktermios tmp_termios;
  122. dev_t dev;
  123. ret = get_dev_to_use(synth, &dev);
  124. if (ret)
  125. return ret;
  126. tty = tty_kopen_exclusive(dev);
  127. if (IS_ERR(tty))
  128. return PTR_ERR(tty);
  129. if (tty->ops->open)
  130. ret = tty->ops->open(tty, NULL);
  131. else
  132. ret = -ENODEV;
  133. if (ret) {
  134. tty_unlock(tty);
  135. return ret;
  136. }
  137. clear_bit(TTY_HUPPED, &tty->flags);
  138. /* ensure hardware flow control is enabled */
  139. get_termios(tty, &tmp_termios);
  140. if (!(tmp_termios.c_cflag & CRTSCTS)) {
  141. tmp_termios.c_cflag |= CRTSCTS;
  142. tty_set_termios(tty, &tmp_termios);
  143. /*
  144. * check c_cflag to see if it's updated as tty_set_termios
  145. * may not return error even when no tty bits are
  146. * changed by the request.
  147. */
  148. get_termios(tty, &tmp_termios);
  149. if (!(tmp_termios.c_cflag & CRTSCTS))
  150. pr_warn("speakup: Failed to set hardware flow control\n");
  151. }
  152. tty_unlock(tty);
  153. mutex_lock(&speakup_tty_mutex);
  154. speakup_tty = tty;
  155. ret = tty_set_ldisc(tty, N_SPEAKUP);
  156. speakup_tty = NULL;
  157. mutex_unlock(&speakup_tty_mutex);
  158. if (!ret) {
  159. /* Success */
  160. struct spk_ldisc_data *ldisc_data = tty->disc_data;
  161. ldisc_data->synth = synth;
  162. synth->dev = tty;
  163. return 0;
  164. }
  165. pr_err("speakup: Failed to set N_SPEAKUP on tty\n");
  166. tty_lock(tty);
  167. if (tty->ops->close)
  168. tty->ops->close(tty, NULL);
  169. tty_unlock(tty);
  170. tty_kclose(tty);
  171. return ret;
  172. }
  173. void spk_ttyio_register_ldisc(void)
  174. {
  175. if (tty_register_ldisc(&spk_ttyio_ldisc_ops))
  176. pr_warn("speakup: Error registering line discipline. Most synths won't work.\n");
  177. }
  178. void spk_ttyio_unregister_ldisc(void)
  179. {
  180. tty_unregister_ldisc(&spk_ttyio_ldisc_ops);
  181. }
  182. static int spk_ttyio_out(struct spk_synth *in_synth, const char ch)
  183. {
  184. struct tty_struct *tty = in_synth->dev;
  185. int ret;
  186. if (!in_synth->alive || !tty->ops->write)
  187. return 0;
  188. ret = tty->ops->write(tty, &ch, 1);
  189. if (ret == 0)
  190. /* No room */
  191. return 0;
  192. if (ret > 0)
  193. /* Success */
  194. return 1;
  195. pr_warn("%s: I/O error, deactivating speakup\n",
  196. in_synth->long_name);
  197. /* No synth any more, so nobody will restart TTYs,
  198. * and we thus need to do it ourselves. Now that there
  199. * is no synth we can let application flood anyway
  200. */
  201. in_synth->alive = 0;
  202. speakup_start_ttys();
  203. return 0;
  204. }
  205. static int spk_ttyio_out_unicode(struct spk_synth *in_synth, u16 ch)
  206. {
  207. int ret;
  208. if (ch < 0x80) {
  209. ret = spk_ttyio_out(in_synth, ch);
  210. } else if (ch < 0x800) {
  211. ret = spk_ttyio_out(in_synth, 0xc0 | (ch >> 6));
  212. ret &= spk_ttyio_out(in_synth, 0x80 | (ch & 0x3f));
  213. } else {
  214. ret = spk_ttyio_out(in_synth, 0xe0 | (ch >> 12));
  215. ret &= spk_ttyio_out(in_synth, 0x80 | ((ch >> 6) & 0x3f));
  216. ret &= spk_ttyio_out(in_synth, 0x80 | (ch & 0x3f));
  217. }
  218. return ret;
  219. }
  220. static void spk_ttyio_send_xchar(struct spk_synth *in_synth, char ch)
  221. {
  222. struct tty_struct *tty = in_synth->dev;
  223. if (tty->ops->send_xchar)
  224. tty->ops->send_xchar(tty, ch);
  225. }
  226. static void spk_ttyio_tiocmset(struct spk_synth *in_synth, unsigned int set, unsigned int clear)
  227. {
  228. struct tty_struct *tty = in_synth->dev;
  229. if (tty->ops->tiocmset)
  230. tty->ops->tiocmset(tty, set, clear);
  231. }
  232. static int spk_ttyio_wait_for_xmitr(struct spk_synth *in_synth)
  233. {
  234. return 1;
  235. }
  236. static unsigned char ttyio_in(struct spk_synth *in_synth, int timeout)
  237. {
  238. struct tty_struct *tty = in_synth->dev;
  239. struct spk_ldisc_data *ldisc_data = tty->disc_data;
  240. char rv;
  241. if (!timeout) {
  242. if (!try_wait_for_completion(&ldisc_data->completion))
  243. return 0xff;
  244. } else if (wait_for_completion_timeout(&ldisc_data->completion,
  245. usecs_to_jiffies(timeout)) == 0) {
  246. pr_warn("spk_ttyio: timeout (%d) while waiting for input\n",
  247. timeout);
  248. return 0xff;
  249. }
  250. rv = ldisc_data->buf;
  251. /* Make sure we have read buf before we set buf_free to let
  252. * the producer overwrite it
  253. */
  254. mb();
  255. ldisc_data->buf_free = true;
  256. /* Let TTY push more characters */
  257. tty_flip_buffer_push(tty->port);
  258. return rv;
  259. }
  260. static unsigned char spk_ttyio_in(struct spk_synth *in_synth)
  261. {
  262. return ttyio_in(in_synth, SPK_SYNTH_TIMEOUT);
  263. }
  264. static unsigned char spk_ttyio_in_nowait(struct spk_synth *in_synth)
  265. {
  266. u8 rv = ttyio_in(in_synth, 0);
  267. return (rv == 0xff) ? 0 : rv;
  268. }
  269. static void spk_ttyio_flush_buffer(struct spk_synth *in_synth)
  270. {
  271. struct tty_struct *tty = in_synth->dev;
  272. if (tty->ops->flush_buffer)
  273. tty->ops->flush_buffer(tty);
  274. }
  275. int spk_ttyio_synth_probe(struct spk_synth *synth)
  276. {
  277. int rv = spk_ttyio_initialise_ldisc(synth);
  278. if (rv)
  279. return rv;
  280. synth->alive = 1;
  281. return 0;
  282. }
  283. EXPORT_SYMBOL_GPL(spk_ttyio_synth_probe);
  284. void spk_ttyio_release(struct spk_synth *in_synth)
  285. {
  286. struct tty_struct *tty = in_synth->dev;
  287. if (tty == NULL)
  288. return;
  289. tty_lock(tty);
  290. if (tty->ops->close)
  291. tty->ops->close(tty, NULL);
  292. tty_ldisc_flush(tty);
  293. tty_unlock(tty);
  294. tty_kclose(tty);
  295. in_synth->dev = NULL;
  296. }
  297. EXPORT_SYMBOL_GPL(spk_ttyio_release);
  298. const char *spk_ttyio_synth_immediate(struct spk_synth *in_synth, const char *buff)
  299. {
  300. struct tty_struct *tty = in_synth->dev;
  301. u_char ch;
  302. while ((ch = *buff)) {
  303. if (ch == '\n')
  304. ch = in_synth->procspeech;
  305. if (tty_write_room(tty) < 1 ||
  306. !in_synth->io_ops->synth_out(in_synth, ch))
  307. return buff;
  308. buff++;
  309. }
  310. return NULL;
  311. }
  312. EXPORT_SYMBOL_GPL(spk_ttyio_synth_immediate);