speakup_dtlk.c 11 KB

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