speakup_dectlk.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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/unistd.h>
  13. #include <linux/proc_fs.h>
  14. #include <linux/jiffies.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/sched.h>
  17. #include <linux/timer.h>
  18. #include <linux/kthread.h>
  19. #include "speakup.h"
  20. #include "spk_priv.h"
  21. #define DRV_VERSION "2.20"
  22. #define SYNTH_CLEAR 0x03
  23. #define PROCSPEECH 0x0b
  24. static int xoff;
  25. static inline int synth_full(void)
  26. {
  27. return xoff;
  28. }
  29. static void do_catch_up(struct spk_synth *synth);
  30. static void synth_flush(struct spk_synth *synth);
  31. static void read_buff_add(u_char c);
  32. static unsigned char get_index(struct spk_synth *synth);
  33. static int in_escape;
  34. static int is_flushing;
  35. static DEFINE_SPINLOCK(flush_lock);
  36. static DECLARE_WAIT_QUEUE_HEAD(flush);
  37. enum default_vars_id {
  38. CAPS_START_ID = 0, CAPS_STOP_ID,
  39. RATE_ID, PITCH_ID, INFLECTION_ID,
  40. VOL_ID, PUNCT_ID, VOICE_ID,
  41. DIRECT_ID, V_LAST_VAR_ID,
  42. NB_ID,
  43. };
  44. static struct var_t vars[NB_ID] = {
  45. [CAPS_START_ID] = { CAPS_START, .u.s = {"[:dv ap 160] " } },
  46. [CAPS_STOP_ID] = { CAPS_STOP, .u.s = {"[:dv ap 100 ] " } },
  47. [RATE_ID] = { RATE, .u.n = {"[:ra %d] ", 180, 75, 650, 0, 0, NULL } },
  48. [PITCH_ID] = { PITCH, .u.n = {"[:dv ap %d] ", 122, 50, 350, 0, 0, NULL } },
  49. [INFLECTION_ID] = { INFLECTION, .u.n = {"[:dv pr %d] ", 100, 0, 10000, 0, 0, NULL } },
  50. [VOL_ID] = { VOL, .u.n = {"[:dv g5 %d] ", 86, 60, 86, 0, 0, NULL } },
  51. [PUNCT_ID] = { PUNCT, .u.n = {"[:pu %c] ", 0, 0, 2, 0, 0, "nsa" } },
  52. [VOICE_ID] = { VOICE, .u.n = {"[:n%c] ", 0, 0, 9, 0, 0, "phfdburwkv" } },
  53. [DIRECT_ID] = { DIRECT, .u.n = {NULL, 0, 0, 1, 0, 0, NULL } },
  54. V_LAST_VAR
  55. };
  56. /*
  57. * These attributes will appear in /sys/accessibility/speakup/dectlk.
  58. */
  59. static struct kobj_attribute caps_start_attribute =
  60. __ATTR(caps_start, 0644, spk_var_show, spk_var_store);
  61. static struct kobj_attribute caps_stop_attribute =
  62. __ATTR(caps_stop, 0644, spk_var_show, spk_var_store);
  63. static struct kobj_attribute pitch_attribute =
  64. __ATTR(pitch, 0644, spk_var_show, spk_var_store);
  65. static struct kobj_attribute inflection_attribute =
  66. __ATTR(inflection, 0644, spk_var_show, spk_var_store);
  67. static struct kobj_attribute punct_attribute =
  68. __ATTR(punct, 0644, spk_var_show, spk_var_store);
  69. static struct kobj_attribute rate_attribute =
  70. __ATTR(rate, 0644, spk_var_show, spk_var_store);
  71. static struct kobj_attribute voice_attribute =
  72. __ATTR(voice, 0644, spk_var_show, spk_var_store);
  73. static struct kobj_attribute vol_attribute =
  74. __ATTR(vol, 0644, spk_var_show, spk_var_store);
  75. static struct kobj_attribute delay_time_attribute =
  76. __ATTR(delay_time, 0644, spk_var_show, spk_var_store);
  77. static struct kobj_attribute direct_attribute =
  78. __ATTR(direct, 0644, spk_var_show, spk_var_store);
  79. static struct kobj_attribute full_time_attribute =
  80. __ATTR(full_time, 0644, spk_var_show, spk_var_store);
  81. static struct kobj_attribute flush_time_attribute =
  82. __ATTR(flush_time, 0644, spk_var_show, spk_var_store);
  83. static struct kobj_attribute jiffy_delta_attribute =
  84. __ATTR(jiffy_delta, 0644, spk_var_show, spk_var_store);
  85. static struct kobj_attribute trigger_time_attribute =
  86. __ATTR(trigger_time, 0644, spk_var_show, spk_var_store);
  87. /*
  88. * Create a group of attributes so that we can create and destroy them all
  89. * at once.
  90. */
  91. static struct attribute *synth_attrs[] = {
  92. &caps_start_attribute.attr,
  93. &caps_stop_attribute.attr,
  94. &pitch_attribute.attr,
  95. &inflection_attribute.attr,
  96. &punct_attribute.attr,
  97. &rate_attribute.attr,
  98. &voice_attribute.attr,
  99. &vol_attribute.attr,
  100. &delay_time_attribute.attr,
  101. &direct_attribute.attr,
  102. &full_time_attribute.attr,
  103. &flush_time_attribute.attr,
  104. &jiffy_delta_attribute.attr,
  105. &trigger_time_attribute.attr,
  106. NULL, /* need to NULL terminate the list of attributes */
  107. };
  108. static int ap_defaults[] = {122, 89, 155, 110, 208, 240, 200, 106, 306};
  109. static int g5_defaults[] = {86, 81, 86, 84, 81, 80, 83, 83, 73};
  110. static struct spk_synth synth_dectlk = {
  111. .name = "dectlk",
  112. .version = DRV_VERSION,
  113. .long_name = "Dectalk Express",
  114. .init = "[:error sp :name paul :rate 180 :tsr off] ",
  115. .procspeech = PROCSPEECH,
  116. .clear = SYNTH_CLEAR,
  117. .delay = 500,
  118. .trigger = 50,
  119. .jiffies = 50,
  120. .full = 40000,
  121. .flush_time = 4000,
  122. .dev_name = SYNTH_DEFAULT_DEV,
  123. .startup = SYNTH_START,
  124. .checkval = SYNTH_CHECK,
  125. .vars = vars,
  126. .default_pitch = ap_defaults,
  127. .default_vol = g5_defaults,
  128. .io_ops = &spk_ttyio_ops,
  129. .probe = spk_ttyio_synth_probe,
  130. .release = spk_ttyio_release,
  131. .synth_immediate = spk_ttyio_synth_immediate,
  132. .catch_up = do_catch_up,
  133. .flush = synth_flush,
  134. .is_alive = spk_synth_is_alive_restart,
  135. .synth_adjust = NULL,
  136. .read_buff_add = read_buff_add,
  137. .get_index = get_index,
  138. .indexing = {
  139. .command = "[:in re %d ] ",
  140. .lowindex = 1,
  141. .highindex = 8,
  142. .currindex = 1,
  143. },
  144. .attributes = {
  145. .attrs = synth_attrs,
  146. .name = "dectlk",
  147. },
  148. };
  149. static int is_indnum(u_char *ch)
  150. {
  151. if ((*ch >= '0') && (*ch <= '9')) {
  152. *ch = *ch - '0';
  153. return 1;
  154. }
  155. return 0;
  156. }
  157. static u_char lastind;
  158. static unsigned char get_index(struct spk_synth *synth)
  159. {
  160. u_char rv;
  161. rv = lastind;
  162. lastind = 0;
  163. return rv;
  164. }
  165. static void read_buff_add(u_char c)
  166. {
  167. static int ind = -1;
  168. if (c == 0x01) {
  169. unsigned long flags;
  170. spin_lock_irqsave(&flush_lock, flags);
  171. is_flushing = 0;
  172. wake_up_interruptible(&flush);
  173. spin_unlock_irqrestore(&flush_lock, flags);
  174. } else if (c == 0x13) {
  175. xoff = 1;
  176. } else if (c == 0x11) {
  177. xoff = 0;
  178. } else if (is_indnum(&c)) {
  179. if (ind == -1)
  180. ind = c;
  181. else
  182. ind = ind * 10 + c;
  183. } else if ((c > 31) && (c < 127)) {
  184. if (ind != -1)
  185. lastind = (u_char)ind;
  186. ind = -1;
  187. }
  188. }
  189. static void do_catch_up(struct spk_synth *synth)
  190. {
  191. int synth_full_val = 0;
  192. static u_char ch;
  193. static u_char last = '\0';
  194. unsigned long flags;
  195. unsigned long jiff_max;
  196. unsigned long timeout;
  197. DEFINE_WAIT(wait);
  198. struct var_t *jiffy_delta;
  199. struct var_t *delay_time;
  200. struct var_t *flush_time;
  201. int jiffy_delta_val;
  202. int delay_time_val;
  203. int timeout_val;
  204. jiffy_delta = spk_get_var(JIFFY);
  205. delay_time = spk_get_var(DELAY);
  206. flush_time = spk_get_var(FLUSH);
  207. spin_lock_irqsave(&speakup_info.spinlock, flags);
  208. jiffy_delta_val = jiffy_delta->u.n.value;
  209. timeout_val = flush_time->u.n.value;
  210. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  211. timeout = msecs_to_jiffies(timeout_val);
  212. jiff_max = jiffies + jiffy_delta_val;
  213. while (!kthread_should_stop()) {
  214. /* if no ctl-a in 4, send data anyway */
  215. spin_lock_irqsave(&flush_lock, flags);
  216. while (is_flushing && timeout) {
  217. prepare_to_wait(&flush, &wait, TASK_INTERRUPTIBLE);
  218. spin_unlock_irqrestore(&flush_lock, flags);
  219. timeout = schedule_timeout(timeout);
  220. spin_lock_irqsave(&flush_lock, flags);
  221. }
  222. finish_wait(&flush, &wait);
  223. is_flushing = 0;
  224. spin_unlock_irqrestore(&flush_lock, flags);
  225. spin_lock_irqsave(&speakup_info.spinlock, flags);
  226. if (speakup_info.flushing) {
  227. speakup_info.flushing = 0;
  228. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  229. synth->flush(synth);
  230. continue;
  231. }
  232. synth_buffer_skip_nonlatin1();
  233. if (synth_buffer_empty()) {
  234. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  235. break;
  236. }
  237. ch = synth_buffer_peek();
  238. set_current_state(TASK_INTERRUPTIBLE);
  239. delay_time_val = delay_time->u.n.value;
  240. synth_full_val = synth_full();
  241. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  242. if (ch == '\n')
  243. ch = 0x0D;
  244. if (synth_full_val || !synth->io_ops->synth_out(synth, ch)) {
  245. schedule_timeout(msecs_to_jiffies(delay_time_val));
  246. continue;
  247. }
  248. set_current_state(TASK_RUNNING);
  249. spin_lock_irqsave(&speakup_info.spinlock, flags);
  250. synth_buffer_getc();
  251. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  252. if (ch == '[') {
  253. in_escape = 1;
  254. } else if (ch == ']') {
  255. in_escape = 0;
  256. } else if (ch <= SPACE) {
  257. if (!in_escape && strchr(",.!?;:", last))
  258. synth->io_ops->synth_out(synth, PROCSPEECH);
  259. if (time_after_eq(jiffies, jiff_max)) {
  260. if (!in_escape)
  261. synth->io_ops->synth_out(synth,
  262. PROCSPEECH);
  263. spin_lock_irqsave(&speakup_info.spinlock,
  264. flags);
  265. jiffy_delta_val = jiffy_delta->u.n.value;
  266. delay_time_val = delay_time->u.n.value;
  267. spin_unlock_irqrestore(&speakup_info.spinlock,
  268. flags);
  269. schedule_timeout(msecs_to_jiffies
  270. (delay_time_val));
  271. jiff_max = jiffies + jiffy_delta_val;
  272. }
  273. }
  274. last = ch;
  275. }
  276. if (!in_escape)
  277. synth->io_ops->synth_out(synth, PROCSPEECH);
  278. }
  279. static void synth_flush(struct spk_synth *synth)
  280. {
  281. if (in_escape)
  282. /* if in command output ']' so we don't get an error */
  283. synth->io_ops->synth_out(synth, ']');
  284. in_escape = 0;
  285. is_flushing = 1;
  286. synth->io_ops->flush_buffer(synth);
  287. synth->io_ops->synth_out(synth, SYNTH_CLEAR);
  288. }
  289. module_param_named(ser, synth_dectlk.ser, int, 0444);
  290. module_param_named(dev, synth_dectlk.dev_name, charp, 0444);
  291. module_param_named(start, synth_dectlk.startup, short, 0444);
  292. module_param_named(rate, vars[RATE_ID].u.n.default_val, int, 0444);
  293. module_param_named(pitch, vars[PITCH_ID].u.n.default_val, int, 0444);
  294. module_param_named(inflection, vars[INFLECTION_ID].u.n.default_val, int, 0444);
  295. module_param_named(vol, vars[VOL_ID].u.n.default_val, int, 0444);
  296. module_param_named(punct, vars[PUNCT_ID].u.n.default_val, int, 0444);
  297. module_param_named(voice, vars[VOICE_ID].u.n.default_val, int, 0444);
  298. module_param_named(direct, vars[DIRECT_ID].u.n.default_val, int, 0444);
  299. MODULE_PARM_DESC(ser, "Set the serial port for the synthesizer (0-based).");
  300. MODULE_PARM_DESC(dev, "Set the device e.g. ttyUSB0, for the synthesizer.");
  301. MODULE_PARM_DESC(start, "Start the synthesizer once it is loaded.");
  302. MODULE_PARM_DESC(rate, "Set the rate variable on load.");
  303. MODULE_PARM_DESC(pitch, "Set the pitch variable on load.");
  304. MODULE_PARM_DESC(inflection, "Set the inflection variable on load.");
  305. MODULE_PARM_DESC(vol, "Set the vol variable on load.");
  306. MODULE_PARM_DESC(punct, "Set the punct variable on load.");
  307. MODULE_PARM_DESC(voice, "Set the voice variable on load.");
  308. MODULE_PARM_DESC(direct, "Set the direct variable on load.");
  309. module_spk_synth(synth_dectlk);
  310. MODULE_AUTHOR("Kirk Reiser <kirk@braille.uwo.ca>");
  311. MODULE_AUTHOR("David Borowski");
  312. MODULE_DESCRIPTION("Speakup support for DECtalk Express synthesizers");
  313. MODULE_LICENSE("GPL");
  314. MODULE_VERSION(DRV_VERSION);