speakup_decext.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * originally written by: Kirk Reiser <kirk@braille.uwo.ca>
  4. * this version considerably modified by David Borowski, david575@rogers.com
  5. *
  6. * Copyright (C) 1998-99 Kirk Reiser.
  7. * Copyright (C) 2003 David Borowski.
  8. *
  9. * specifically written as a driver for the speakup screenreview
  10. * s not a general device driver.
  11. */
  12. #include <linux/jiffies.h>
  13. #include <linux/sched.h>
  14. #include <linux/timer.h>
  15. #include <linux/kthread.h>
  16. #include "spk_priv.h"
  17. #include "speakup.h"
  18. #define DRV_VERSION "2.14"
  19. #define SYNTH_CLEAR 0x03
  20. #define PROCSPEECH 0x0b
  21. static volatile unsigned char last_char;
  22. static void read_buff_add(u_char ch)
  23. {
  24. last_char = ch;
  25. }
  26. static inline bool synth_full(void)
  27. {
  28. return last_char == 0x13;
  29. }
  30. static void do_catch_up(struct spk_synth *synth);
  31. static void synth_flush(struct spk_synth *synth);
  32. static int in_escape;
  33. enum default_vars_id {
  34. CAPS_START_ID = 0, CAPS_STOP_ID,
  35. RATE_ID, PITCH_ID, INFLECTION_ID,
  36. VOL_ID, PUNCT_ID, VOICE_ID,
  37. DIRECT_ID, V_LAST_ID,
  38. NB_ID,
  39. };
  40. static struct var_t vars[NB_ID] = {
  41. [CAPS_START_ID] = { CAPS_START, .u.s = {"[:dv ap 222]" } },
  42. [CAPS_STOP_ID] = { CAPS_STOP, .u.s = {"[:dv ap 100]" } },
  43. [RATE_ID] = { RATE, .u.n = {"[:ra %d]", 7, 0, 9, 150, 25, NULL } },
  44. [PITCH_ID] = { PITCH, .u.n = {"[:dv ap %d]", 100, 0, 100, 0, 0, NULL } },
  45. [INFLECTION_ID] = { INFLECTION, .u.n = {"[:dv pr %d] ", 100, 0, 10000, 0, 0, NULL } },
  46. [VOL_ID] = { VOL, .u.n = {"[:dv gv %d]", 13, 0, 16, 0, 5, NULL } },
  47. [PUNCT_ID] = { PUNCT, .u.n = {"[:pu %c]", 0, 0, 2, 0, 0, "nsa" } },
  48. [VOICE_ID] = { VOICE, .u.n = {"[:n%c]", 0, 0, 9, 0, 0, "phfdburwkv" } },
  49. [DIRECT_ID] = { DIRECT, .u.n = {NULL, 0, 0, 1, 0, 0, NULL } },
  50. V_LAST_VAR
  51. };
  52. /*
  53. * These attributes will appear in /sys/accessibility/speakup/decext.
  54. */
  55. static struct kobj_attribute caps_start_attribute =
  56. __ATTR(caps_start, 0644, spk_var_show, spk_var_store);
  57. static struct kobj_attribute caps_stop_attribute =
  58. __ATTR(caps_stop, 0644, spk_var_show, spk_var_store);
  59. static struct kobj_attribute pitch_attribute =
  60. __ATTR(pitch, 0644, spk_var_show, spk_var_store);
  61. static struct kobj_attribute inflection_attribute =
  62. __ATTR(inflection, 0644, spk_var_show, spk_var_store);
  63. static struct kobj_attribute punct_attribute =
  64. __ATTR(punct, 0644, spk_var_show, spk_var_store);
  65. static struct kobj_attribute rate_attribute =
  66. __ATTR(rate, 0644, spk_var_show, spk_var_store);
  67. static struct kobj_attribute voice_attribute =
  68. __ATTR(voice, 0644, spk_var_show, spk_var_store);
  69. static struct kobj_attribute vol_attribute =
  70. __ATTR(vol, 0644, spk_var_show, spk_var_store);
  71. static struct kobj_attribute delay_time_attribute =
  72. __ATTR(delay_time, 0644, spk_var_show, spk_var_store);
  73. static struct kobj_attribute direct_attribute =
  74. __ATTR(direct, 0644, spk_var_show, spk_var_store);
  75. static struct kobj_attribute full_time_attribute =
  76. __ATTR(full_time, 0644, spk_var_show, spk_var_store);
  77. static struct kobj_attribute jiffy_delta_attribute =
  78. __ATTR(jiffy_delta, 0644, spk_var_show, spk_var_store);
  79. static struct kobj_attribute trigger_time_attribute =
  80. __ATTR(trigger_time, 0644, spk_var_show, spk_var_store);
  81. /*
  82. * Create a group of attributes so that we can create and destroy them all
  83. * at once.
  84. */
  85. static struct attribute *synth_attrs[] = {
  86. &caps_start_attribute.attr,
  87. &caps_stop_attribute.attr,
  88. &pitch_attribute.attr,
  89. &inflection_attribute.attr,
  90. &punct_attribute.attr,
  91. &rate_attribute.attr,
  92. &voice_attribute.attr,
  93. &vol_attribute.attr,
  94. &delay_time_attribute.attr,
  95. &direct_attribute.attr,
  96. &full_time_attribute.attr,
  97. &jiffy_delta_attribute.attr,
  98. &trigger_time_attribute.attr,
  99. NULL, /* need to NULL terminate the list of attributes */
  100. };
  101. static struct spk_synth synth_decext = {
  102. .name = "decext",
  103. .version = DRV_VERSION,
  104. .long_name = "Dectalk External",
  105. .init = "[:pe -380]",
  106. .procspeech = PROCSPEECH,
  107. .clear = SYNTH_CLEAR,
  108. .delay = 500,
  109. .trigger = 50,
  110. .jiffies = 50,
  111. .full = 40000,
  112. .flags = SF_DEC,
  113. .dev_name = SYNTH_DEFAULT_DEV,
  114. .startup = SYNTH_START,
  115. .checkval = SYNTH_CHECK,
  116. .vars = vars,
  117. .io_ops = &spk_ttyio_ops,
  118. .probe = spk_ttyio_synth_probe,
  119. .release = spk_ttyio_release,
  120. .synth_immediate = spk_ttyio_synth_immediate,
  121. .catch_up = do_catch_up,
  122. .flush = synth_flush,
  123. .is_alive = spk_synth_is_alive_restart,
  124. .synth_adjust = NULL,
  125. .read_buff_add = read_buff_add,
  126. .get_index = NULL,
  127. .indexing = {
  128. .command = NULL,
  129. .lowindex = 0,
  130. .highindex = 0,
  131. .currindex = 0,
  132. },
  133. .attributes = {
  134. .attrs = synth_attrs,
  135. .name = "decext",
  136. },
  137. };
  138. static void do_catch_up(struct spk_synth *synth)
  139. {
  140. u_char ch;
  141. static u_char last = '\0';
  142. unsigned long flags;
  143. unsigned long jiff_max;
  144. struct var_t *jiffy_delta;
  145. struct var_t *delay_time;
  146. int jiffy_delta_val = 0;
  147. int delay_time_val = 0;
  148. jiffy_delta = spk_get_var(JIFFY);
  149. delay_time = spk_get_var(DELAY);
  150. spin_lock_irqsave(&speakup_info.spinlock, flags);
  151. jiffy_delta_val = jiffy_delta->u.n.value;
  152. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  153. jiff_max = jiffies + jiffy_delta_val;
  154. while (!kthread_should_stop()) {
  155. spin_lock_irqsave(&speakup_info.spinlock, flags);
  156. if (speakup_info.flushing) {
  157. speakup_info.flushing = 0;
  158. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  159. synth->flush(synth);
  160. continue;
  161. }
  162. synth_buffer_skip_nonlatin1();
  163. if (synth_buffer_empty()) {
  164. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  165. break;
  166. }
  167. ch = synth_buffer_peek();
  168. set_current_state(TASK_INTERRUPTIBLE);
  169. delay_time_val = delay_time->u.n.value;
  170. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  171. if (ch == '\n')
  172. ch = 0x0D;
  173. if (synth_full() || !synth->io_ops->synth_out(synth, ch)) {
  174. schedule_timeout(msecs_to_jiffies(delay_time_val));
  175. continue;
  176. }
  177. set_current_state(TASK_RUNNING);
  178. spin_lock_irqsave(&speakup_info.spinlock, flags);
  179. synth_buffer_getc();
  180. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  181. if (ch == '[') {
  182. in_escape = 1;
  183. } else if (ch == ']') {
  184. in_escape = 0;
  185. } else if (ch <= SPACE) {
  186. if (!in_escape && strchr(",.!?;:", last))
  187. synth->io_ops->synth_out(synth, PROCSPEECH);
  188. if (time_after_eq(jiffies, jiff_max)) {
  189. if (!in_escape)
  190. synth->io_ops->synth_out(synth,
  191. PROCSPEECH);
  192. spin_lock_irqsave(&speakup_info.spinlock,
  193. flags);
  194. jiffy_delta_val = jiffy_delta->u.n.value;
  195. delay_time_val = delay_time->u.n.value;
  196. spin_unlock_irqrestore(&speakup_info.spinlock,
  197. flags);
  198. schedule_timeout(msecs_to_jiffies
  199. (delay_time_val));
  200. jiff_max = jiffies + jiffy_delta_val;
  201. }
  202. }
  203. last = ch;
  204. }
  205. if (!in_escape)
  206. synth->io_ops->synth_out(synth, PROCSPEECH);
  207. }
  208. static void synth_flush(struct spk_synth *synth)
  209. {
  210. in_escape = 0;
  211. synth->io_ops->flush_buffer(synth);
  212. synth->synth_immediate(synth, "\033P;10z\033\\");
  213. }
  214. module_param_named(ser, synth_decext.ser, int, 0444);
  215. module_param_named(dev, synth_decext.dev_name, charp, 0444);
  216. module_param_named(start, synth_decext.startup, short, 0444);
  217. module_param_named(rate, vars[RATE_ID].u.n.default_val, int, 0444);
  218. module_param_named(pitch, vars[PITCH_ID].u.n.default_val, int, 0444);
  219. module_param_named(inflection, vars[INFLECTION_ID].u.n.default_val, int, 0444);
  220. module_param_named(vol, vars[VOL_ID].u.n.default_val, int, 0444);
  221. module_param_named(punct, vars[PUNCT_ID].u.n.default_val, int, 0444);
  222. module_param_named(voice, vars[VOICE_ID].u.n.default_val, int, 0444);
  223. module_param_named(direct, vars[DIRECT_ID].u.n.default_val, int, 0444);
  224. MODULE_PARM_DESC(ser, "Set the serial port for the synthesizer (0-based).");
  225. MODULE_PARM_DESC(dev, "Set the device e.g. ttyUSB0, for the synthesizer.");
  226. MODULE_PARM_DESC(start, "Start the synthesizer once it is loaded.");
  227. MODULE_PARM_DESC(rate, "Set the rate variable on load.");
  228. MODULE_PARM_DESC(pitch, "Set the pitch variable on load.");
  229. MODULE_PARM_DESC(inflection, "Set the inflection variable on load.");
  230. MODULE_PARM_DESC(vol, "Set the vol variable on load.");
  231. MODULE_PARM_DESC(punct, "Set the punct variable on load.");
  232. MODULE_PARM_DESC(voice, "Set the voice variable on load.");
  233. MODULE_PARM_DESC(direct, "Set the direct variable on load.");
  234. module_spk_synth(synth_decext);
  235. MODULE_AUTHOR("Kirk Reiser <kirk@braille.uwo.ca>");
  236. MODULE_AUTHOR("David Borowski");
  237. MODULE_DESCRIPTION("Speakup support for DECtalk External synthesizers");
  238. MODULE_LICENSE("GPL");
  239. MODULE_VERSION(DRV_VERSION);