speakup_dtlk.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. * package it's not a general device driver.
  11. * This driver is for the RC Systems DoubleTalk PC internal synthesizer.
  12. */
  13. #include <linux/jiffies.h>
  14. #include <linux/sched.h>
  15. #include <linux/timer.h>
  16. #include <linux/kthread.h>
  17. #include "spk_priv.h"
  18. #include "serialio.h"
  19. #include "speakup_dtlk.h" /* local header file for DoubleTalk values */
  20. #include "speakup.h"
  21. #define DRV_VERSION "2.10"
  22. #define PROCSPEECH 0x00
  23. static int synth_probe(struct spk_synth *synth);
  24. static void dtlk_release(struct spk_synth *synth);
  25. static const char *synth_immediate(struct spk_synth *synth, const char *buf);
  26. static void do_catch_up(struct spk_synth *synth);
  27. static void synth_flush(struct spk_synth *synth);
  28. static int synth_lpc;
  29. static int port_forced;
  30. static unsigned int synth_portlist[] = {
  31. 0x25e, 0x29e, 0x2de, 0x31e, 0x35e, 0x39e, 0
  32. };
  33. static u_char synth_status;
  34. enum default_vars_id {
  35. CAPS_START_ID = 0, CAPS_STOP_ID,
  36. RATE_ID, PITCH_ID,
  37. VOL_ID, TONE_ID, PUNCT_ID,
  38. VOICE_ID, FREQUENCY_ID,
  39. DIRECT_ID, V_LAST_VAR_ID,
  40. NB_ID,
  41. };
  42. static struct var_t vars[NB_ID] = {
  43. [CAPS_START_ID] = { CAPS_START, .u.s = {"\x01+35p" } },
  44. [CAPS_STOP_ID] = { CAPS_STOP, .u.s = {"\x01-35p" } },
  45. [RATE_ID] = { RATE, .u.n = {"\x01%ds", 8, 0, 9, 0, 0, NULL } },
  46. [PITCH_ID] = { PITCH, .u.n = {"\x01%dp", 50, 0, 99, 0, 0, NULL } },
  47. [VOL_ID] = { VOL, .u.n = {"\x01%dv", 5, 0, 9, 0, 0, NULL } },
  48. [TONE_ID] = { TONE, .u.n = {"\x01%dx", 1, 0, 2, 0, 0, NULL } },
  49. [PUNCT_ID] = { PUNCT, .u.n = {"\x01%db", 7, 0, 15, 0, 0, NULL } },
  50. [VOICE_ID] = { VOICE, .u.n = {"\x01%do", 0, 0, 7, 0, 0, NULL } },
  51. [FREQUENCY_ID] = { FREQUENCY, .u.n = {"\x01%df", 5, 0, 9, 0, 0, NULL } },
  52. [DIRECT_ID] = { DIRECT, .u.n = {NULL, 0, 0, 1, 0, 0, NULL } },
  53. V_LAST_VAR
  54. };
  55. /*
  56. * These attributes will appear in /sys/accessibility/speakup/dtlk.
  57. */
  58. static struct kobj_attribute caps_start_attribute =
  59. __ATTR(caps_start, 0644, spk_var_show, spk_var_store);
  60. static struct kobj_attribute caps_stop_attribute =
  61. __ATTR(caps_stop, 0644, spk_var_show, spk_var_store);
  62. static struct kobj_attribute freq_attribute =
  63. __ATTR(freq, 0644, spk_var_show, spk_var_store);
  64. static struct kobj_attribute pitch_attribute =
  65. __ATTR(pitch, 0644, spk_var_show, spk_var_store);
  66. static struct kobj_attribute punct_attribute =
  67. __ATTR(punct, 0644, spk_var_show, spk_var_store);
  68. static struct kobj_attribute rate_attribute =
  69. __ATTR(rate, 0644, spk_var_show, spk_var_store);
  70. static struct kobj_attribute tone_attribute =
  71. __ATTR(tone, 0644, spk_var_show, spk_var_store);
  72. static struct kobj_attribute voice_attribute =
  73. __ATTR(voice, 0644, spk_var_show, spk_var_store);
  74. static struct kobj_attribute vol_attribute =
  75. __ATTR(vol, 0644, spk_var_show, spk_var_store);
  76. static struct kobj_attribute delay_time_attribute =
  77. __ATTR(delay_time, 0644, spk_var_show, spk_var_store);
  78. static struct kobj_attribute direct_attribute =
  79. __ATTR(direct, 0644, spk_var_show, spk_var_store);
  80. static struct kobj_attribute full_time_attribute =
  81. __ATTR(full_time, 0644, spk_var_show, spk_var_store);
  82. static struct kobj_attribute jiffy_delta_attribute =
  83. __ATTR(jiffy_delta, 0644, spk_var_show, spk_var_store);
  84. static struct kobj_attribute trigger_time_attribute =
  85. __ATTR(trigger_time, 0644, spk_var_show, spk_var_store);
  86. /*
  87. * Create a group of attributes so that we can create and destroy them all
  88. * at once.
  89. */
  90. static struct attribute *synth_attrs[] = {
  91. &caps_start_attribute.attr,
  92. &caps_stop_attribute.attr,
  93. &freq_attribute.attr,
  94. &pitch_attribute.attr,
  95. &punct_attribute.attr,
  96. &rate_attribute.attr,
  97. &tone_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. &jiffy_delta_attribute.attr,
  104. &trigger_time_attribute.attr,
  105. NULL, /* need to NULL terminate the list of attributes */
  106. };
  107. static struct spk_synth synth_dtlk = {
  108. .name = "dtlk",
  109. .version = DRV_VERSION,
  110. .long_name = "DoubleTalk PC",
  111. .init = "\x01@\x01\x31y",
  112. .procspeech = PROCSPEECH,
  113. .clear = SYNTH_CLEAR,
  114. .delay = 500,
  115. .trigger = 30,
  116. .jiffies = 50,
  117. .full = 1000,
  118. .startup = SYNTH_START,
  119. .checkval = SYNTH_CHECK,
  120. .vars = vars,
  121. .io_ops = &spk_serial_io_ops,
  122. .probe = synth_probe,
  123. .release = dtlk_release,
  124. .synth_immediate = synth_immediate,
  125. .catch_up = do_catch_up,
  126. .flush = synth_flush,
  127. .is_alive = spk_synth_is_alive_nop,
  128. .synth_adjust = NULL,
  129. .read_buff_add = NULL,
  130. .get_index = spk_synth_get_index,
  131. .indexing = {
  132. .command = "\x01%di",
  133. .lowindex = 1,
  134. .highindex = 5,
  135. .currindex = 1,
  136. },
  137. .attributes = {
  138. .attrs = synth_attrs,
  139. .name = "dtlk",
  140. },
  141. };
  142. static inline bool synth_readable(void)
  143. {
  144. synth_status = inb_p(speakup_info.port_tts + UART_RX);
  145. return (synth_status & TTS_READABLE) != 0;
  146. }
  147. static inline bool synth_writable(void)
  148. {
  149. synth_status = inb_p(speakup_info.port_tts + UART_RX);
  150. return (synth_status & TTS_WRITABLE) != 0;
  151. }
  152. static inline bool synth_full(void)
  153. {
  154. synth_status = inb_p(speakup_info.port_tts + UART_RX);
  155. return (synth_status & TTS_ALMOST_FULL) != 0;
  156. }
  157. static void spk_out(const char ch)
  158. {
  159. int timeout = SPK_XMITR_TIMEOUT;
  160. while (!synth_writable()) {
  161. if (!--timeout)
  162. break;
  163. udelay(1);
  164. }
  165. outb_p(ch, speakup_info.port_tts);
  166. timeout = SPK_XMITR_TIMEOUT;
  167. while (synth_writable()) {
  168. if (!--timeout)
  169. break;
  170. udelay(1);
  171. }
  172. }
  173. static void do_catch_up(struct spk_synth *synth)
  174. {
  175. u_char ch;
  176. unsigned long flags;
  177. unsigned long jiff_max;
  178. struct var_t *jiffy_delta;
  179. struct var_t *delay_time;
  180. int jiffy_delta_val;
  181. int delay_time_val;
  182. jiffy_delta = spk_get_var(JIFFY);
  183. delay_time = spk_get_var(DELAY);
  184. spin_lock_irqsave(&speakup_info.spinlock, flags);
  185. jiffy_delta_val = jiffy_delta->u.n.value;
  186. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  187. jiff_max = jiffies + jiffy_delta_val;
  188. while (!kthread_should_stop()) {
  189. spin_lock_irqsave(&speakup_info.spinlock, flags);
  190. if (speakup_info.flushing) {
  191. speakup_info.flushing = 0;
  192. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  193. synth->flush(synth);
  194. continue;
  195. }
  196. synth_buffer_skip_nonlatin1();
  197. if (synth_buffer_empty()) {
  198. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  199. break;
  200. }
  201. set_current_state(TASK_INTERRUPTIBLE);
  202. delay_time_val = delay_time->u.n.value;
  203. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  204. if (synth_full()) {
  205. schedule_timeout(msecs_to_jiffies(delay_time_val));
  206. continue;
  207. }
  208. set_current_state(TASK_RUNNING);
  209. spin_lock_irqsave(&speakup_info.spinlock, flags);
  210. ch = synth_buffer_getc();
  211. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  212. if (ch == '\n')
  213. ch = PROCSPEECH;
  214. spk_out(ch);
  215. if (time_after_eq(jiffies, jiff_max) && (ch == SPACE)) {
  216. spk_out(PROCSPEECH);
  217. spin_lock_irqsave(&speakup_info.spinlock, flags);
  218. delay_time_val = delay_time->u.n.value;
  219. jiffy_delta_val = jiffy_delta->u.n.value;
  220. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  221. schedule_timeout(msecs_to_jiffies(delay_time_val));
  222. jiff_max = jiffies + jiffy_delta_val;
  223. }
  224. }
  225. spk_out(PROCSPEECH);
  226. }
  227. static const char *synth_immediate(struct spk_synth *synth, const char *buf)
  228. {
  229. u_char ch;
  230. while ((ch = (u_char)*buf)) {
  231. if (synth_full())
  232. return buf;
  233. if (ch == '\n')
  234. ch = PROCSPEECH;
  235. spk_out(ch);
  236. buf++;
  237. }
  238. return NULL;
  239. }
  240. static void synth_flush(struct spk_synth *synth)
  241. {
  242. outb_p(SYNTH_CLEAR, speakup_info.port_tts);
  243. while (synth_writable())
  244. cpu_relax();
  245. }
  246. static char synth_read_tts(void)
  247. {
  248. u_char ch;
  249. while (!synth_readable())
  250. cpu_relax();
  251. ch = synth_status & 0x7f;
  252. outb_p(ch, speakup_info.port_tts);
  253. while (synth_readable())
  254. cpu_relax();
  255. return (char)ch;
  256. }
  257. /* interrogate the DoubleTalk PC and return its settings */
  258. static struct synth_settings *synth_interrogate(struct spk_synth *synth)
  259. {
  260. u_char *t;
  261. static char buf[sizeof(struct synth_settings) + 1];
  262. int total, i;
  263. static struct synth_settings status;
  264. synth_immediate(synth, "\x18\x01?");
  265. for (total = 0, i = 0; i < 50; i++) {
  266. buf[total] = synth_read_tts();
  267. if (total > 2 && buf[total] == 0x7f)
  268. break;
  269. if (total < sizeof(struct synth_settings))
  270. total++;
  271. }
  272. t = buf;
  273. /* serial number is little endian */
  274. status.serial_number = t[0] + t[1] * 256;
  275. t += 2;
  276. for (i = 0; *t != '\r'; t++) {
  277. status.rom_version[i] = *t;
  278. if (i < sizeof(status.rom_version) - 1)
  279. i++;
  280. }
  281. status.rom_version[i] = 0;
  282. t++;
  283. status.mode = *t++;
  284. status.punc_level = *t++;
  285. status.formant_freq = *t++;
  286. status.pitch = *t++;
  287. status.speed = *t++;
  288. status.volume = *t++;
  289. status.tone = *t++;
  290. status.expression = *t++;
  291. status.ext_dict_loaded = *t++;
  292. status.ext_dict_status = *t++;
  293. status.free_ram = *t++;
  294. status.articulation = *t++;
  295. status.reverb = *t++;
  296. status.eob = *t++;
  297. return &status;
  298. }
  299. static int synth_probe(struct spk_synth *synth)
  300. {
  301. unsigned int port_val = 0;
  302. int i;
  303. struct synth_settings *sp;
  304. pr_info("Probing for DoubleTalk.\n");
  305. if (port_forced) {
  306. speakup_info.port_tts = port_forced;
  307. pr_info("probe forced to %x by kernel command line\n",
  308. speakup_info.port_tts);
  309. if ((port_forced & 0xf) != 0xf)
  310. pr_info("warning: port base should probably end with f\n");
  311. if (synth_request_region(speakup_info.port_tts - 1,
  312. SYNTH_IO_EXTENT)) {
  313. pr_warn("sorry, port already reserved\n");
  314. return -EBUSY;
  315. }
  316. port_val = inw(speakup_info.port_tts - 1);
  317. synth_lpc = speakup_info.port_tts - 1;
  318. } else {
  319. for (i = 0; synth_portlist[i]; i++) {
  320. if (synth_request_region(synth_portlist[i],
  321. SYNTH_IO_EXTENT))
  322. continue;
  323. port_val = inw(synth_portlist[i]) & 0xfbff;
  324. if (port_val == 0x107f) {
  325. synth_lpc = synth_portlist[i];
  326. speakup_info.port_tts = synth_lpc + 1;
  327. break;
  328. }
  329. synth_release_region(synth_portlist[i],
  330. SYNTH_IO_EXTENT);
  331. }
  332. }
  333. port_val &= 0xfbff;
  334. if (port_val != 0x107f) {
  335. pr_info("DoubleTalk PC: not found\n");
  336. if (synth_lpc)
  337. synth_release_region(synth_lpc, SYNTH_IO_EXTENT);
  338. return -ENODEV;
  339. }
  340. while (inw_p(synth_lpc) != 0x147f)
  341. cpu_relax(); /* wait until it's ready */
  342. sp = synth_interrogate(synth);
  343. pr_info("%s: %03x-%03x, ROM ver %s, s/n %u, driver: %s\n",
  344. synth->long_name, synth_lpc, synth_lpc + SYNTH_IO_EXTENT - 1,
  345. sp->rom_version, sp->serial_number, synth->version);
  346. synth->alive = 1;
  347. return 0;
  348. }
  349. static void dtlk_release(struct spk_synth *synth)
  350. {
  351. spk_stop_serial_interrupt();
  352. if (speakup_info.port_tts)
  353. synth_release_region(speakup_info.port_tts - 1,
  354. SYNTH_IO_EXTENT);
  355. speakup_info.port_tts = 0;
  356. }
  357. module_param_hw_named(port, port_forced, int, ioport, 0444);
  358. module_param_named(start, synth_dtlk.startup, short, 0444);
  359. module_param_named(rate, vars[RATE_ID].u.n.default_val, int, 0444);
  360. module_param_named(pitch, vars[PITCH_ID].u.n.default_val, int, 0444);
  361. module_param_named(vol, vars[VOL_ID].u.n.default_val, int, 0444);
  362. module_param_named(tone, vars[TONE_ID].u.n.default_val, int, 0444);
  363. module_param_named(punct, vars[PUNCT_ID].u.n.default_val, int, 0444);
  364. module_param_named(voice, vars[VOICE_ID].u.n.default_val, int, 0444);
  365. module_param_named(frequency, vars[FREQUENCY_ID].u.n.default_val, int, 0444);
  366. module_param_named(direct, vars[DIRECT_ID].u.n.default_val, int, 0444);
  367. MODULE_PARM_DESC(port, "Set the port for the synthesizer (override probing).");
  368. MODULE_PARM_DESC(start, "Start the synthesizer once it is loaded.");
  369. MODULE_PARM_DESC(rate, "Set the rate variable on load.");
  370. MODULE_PARM_DESC(pitch, "Set the pitch variable on load.");
  371. MODULE_PARM_DESC(vol, "Set the vol variable on load.");
  372. MODULE_PARM_DESC(tone, "Set the tone variable on load.");
  373. MODULE_PARM_DESC(punct, "Set the punct variable on load.");
  374. MODULE_PARM_DESC(voice, "Set the voice variable on load.");
  375. MODULE_PARM_DESC(frequency, "Set the frequency variable on load.");
  376. MODULE_PARM_DESC(direct, "Set the direct variable on load.");
  377. module_spk_synth(synth_dtlk);
  378. MODULE_AUTHOR("Kirk Reiser <kirk@braille.uwo.ca>");
  379. MODULE_AUTHOR("David Borowski");
  380. MODULE_DESCRIPTION("Speakup support for DoubleTalk PC synthesizers");
  381. MODULE_LICENSE("GPL");
  382. MODULE_VERSION(DRV_VERSION);