spk_ttyio.c 8.8 KB

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