speakup_dectlk.c 9.1 KB

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