speakup_decpc.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * This is the DECtalk PC speakup driver
  4. *
  5. * Some constants from DEC's DOS driver:
  6. * Copyright (c) by Digital Equipment Corp.
  7. *
  8. * 386BSD DECtalk PC driver:
  9. * Copyright (c) 1996 Brian Buhrow <buhrow@lothlorien.nfbcal.org>
  10. *
  11. * Linux DECtalk PC driver:
  12. * Copyright (c) 1997 Nicolas Pitre <nico@cam.org>
  13. *
  14. * speakup DECtalk PC Internal driver:
  15. * Copyright (c) 2003 David Borowski <david575@golden.net>
  16. *
  17. * All rights reserved.
  18. */
  19. #include <linux/jiffies.h>
  20. #include <linux/sched.h>
  21. #include <linux/timer.h>
  22. #include <linux/kthread.h>
  23. #include "spk_priv.h"
  24. #include "speakup.h"
  25. #define MODULE_init 0x0dec /* module in boot code */
  26. #define MODULE_self_test 0x8800 /* module in self-test */
  27. #define MODULE_reset 0xffff /* reinit the whole module */
  28. #define MODE_mask 0xf000 /* mode bits in high nibble */
  29. #define MODE_null 0x0000
  30. #define MODE_test 0x2000 /* in testing mode */
  31. #define MODE_status 0x8000
  32. #define STAT_int 0x0001 /* running in interrupt mode */
  33. #define STAT_tr_char 0x0002 /* character data to transmit */
  34. #define STAT_rr_char 0x0004 /* ready to receive char data */
  35. #define STAT_cmd_ready 0x0008 /* ready to accept commands */
  36. #define STAT_dma_ready 0x0010 /* dma command ready */
  37. #define STAT_digitized 0x0020 /* spc in digitized mode */
  38. #define STAT_new_index 0x0040 /* new last index ready */
  39. #define STAT_new_status 0x0080 /* new status posted */
  40. #define STAT_dma_state 0x0100 /* dma state toggle */
  41. #define STAT_index_valid 0x0200 /* indexs are valid */
  42. #define STAT_flushing 0x0400 /* flush in progress */
  43. #define STAT_self_test 0x0800 /* module in self test */
  44. #define MODE_ready 0xc000 /* module ready for next phase */
  45. #define READY_boot 0x0000
  46. #define READY_kernel 0x0001
  47. #define MODE_error 0xf000
  48. #define CMD_mask 0xf000 /* mask for command nibble */
  49. #define CMD_null 0x0000 /* post status */
  50. #define CMD_control 0x1000 /* hard control command */
  51. #define CTRL_mask 0x0F00 /* mask off control nibble */
  52. #define CTRL_data 0x00FF /* mask to get data byte */
  53. #define CTRL_null 0x0000 /* null control */
  54. #define CTRL_vol_up 0x0100 /* increase volume */
  55. #define CTRL_vol_down 0x0200 /* decrease volume */
  56. #define CTRL_vol_set 0x0300 /* set volume */
  57. #define CTRL_pause 0x0400 /* pause spc */
  58. #define CTRL_resume 0x0500 /* resume spc clock */
  59. #define CTRL_resume_spc 0x0001 /* resume spc soft pause */
  60. #define CTRL_flush 0x0600 /* flush all buffers */
  61. #define CTRL_int_enable 0x0700 /* enable status change ints */
  62. #define CTRL_buff_free 0x0800 /* buffer remain count */
  63. #define CTRL_buff_used 0x0900 /* buffer in use */
  64. #define CTRL_speech 0x0a00 /* immediate speech change */
  65. #define CTRL_SP_voice 0x0001 /* voice change */
  66. #define CTRL_SP_rate 0x0002 /* rate change */
  67. #define CTRL_SP_comma 0x0003 /* comma pause change */
  68. #define CTRL_SP_period 0x0004 /* period pause change */
  69. #define CTRL_SP_rate_delta 0x0005 /* delta rate change */
  70. #define CTRL_SP_get_param 0x0006 /* return the desired parameter */
  71. #define CTRL_last_index 0x0b00 /* get last index spoken */
  72. #define CTRL_io_priority 0x0c00 /* change i/o priority */
  73. #define CTRL_free_mem 0x0d00 /* get free paragraphs on module */
  74. #define CTRL_get_lang 0x0e00 /* return bitmask of loaded languages */
  75. #define CMD_test 0x2000 /* self-test request */
  76. #define TEST_mask 0x0F00 /* isolate test field */
  77. #define TEST_null 0x0000 /* no test requested */
  78. #define TEST_isa_int 0x0100 /* assert isa irq */
  79. #define TEST_echo 0x0200 /* make data in == data out */
  80. #define TEST_seg 0x0300 /* set peek/poke segment */
  81. #define TEST_off 0x0400 /* set peek/poke offset */
  82. #define TEST_peek 0x0500 /* data out == *peek */
  83. #define TEST_poke 0x0600 /* *peek == data in */
  84. #define TEST_sub_code 0x00FF /* user defined test sub codes */
  85. #define CMD_id 0x3000 /* return software id */
  86. #define ID_null 0x0000 /* null id */
  87. #define ID_kernel 0x0100 /* kernel code executing */
  88. #define ID_boot 0x0200 /* boot code executing */
  89. #define CMD_dma 0x4000 /* force a dma start */
  90. #define CMD_reset 0x5000 /* reset module status */
  91. #define CMD_sync 0x6000 /* kernel sync command */
  92. #define CMD_char_in 0x7000 /* single character send */
  93. #define CMD_char_out 0x8000 /* single character get */
  94. #define CHAR_count_1 0x0100 /* one char in cmd_low */
  95. #define CHAR_count_2 0x0200 /* the second in data_low */
  96. #define CHAR_count_3 0x0300 /* the third in data_high */
  97. #define CMD_spc_mode 0x9000 /* change spc mode */
  98. #define CMD_spc_to_text 0x0100 /* set to text mode */
  99. #define CMD_spc_to_digit 0x0200 /* set to digital mode */
  100. #define CMD_spc_rate 0x0400 /* change spc data rate */
  101. #define CMD_error 0xf000 /* severe error */
  102. enum { PRIMARY_DIC = 0, USER_DIC, COMMAND_DIC, ABBREV_DIC };
  103. #define DMA_single_in 0x01
  104. #define DMA_single_out 0x02
  105. #define DMA_buff_in 0x03
  106. #define DMA_buff_out 0x04
  107. #define DMA_control 0x05
  108. #define DT_MEM_ALLOC 0x03
  109. #define DT_SET_DIC 0x04
  110. #define DT_START_TASK 0x05
  111. #define DT_LOAD_MEM 0x06
  112. #define DT_READ_MEM 0x07
  113. #define DT_DIGITAL_IN 0x08
  114. #define DMA_sync 0x06
  115. #define DMA_sync_char 0x07
  116. #define DRV_VERSION "2.12"
  117. #define PROCSPEECH 0x0b
  118. #define SYNTH_IO_EXTENT 8
  119. static int synth_probe(struct spk_synth *synth);
  120. static void dtpc_release(struct spk_synth *synth);
  121. static const char *synth_immediate(struct spk_synth *synth, const char *buf);
  122. static void do_catch_up(struct spk_synth *synth);
  123. static void synth_flush(struct spk_synth *synth);
  124. static int synth_portlist[] = { 0x340, 0x350, 0x240, 0x250, 0 };
  125. static int in_escape, is_flushing;
  126. static int dt_stat, dma_state;
  127. enum default_vars_id {
  128. CAPS_START_ID = 0, CAPS_STOP_ID,
  129. RATE_ID, PITCH_ID, INFLECTION_ID,
  130. VOL_ID, PUNCT_ID, VOICE_ID,
  131. DIRECT_ID, V_LAST_VAR_ID,
  132. NB_ID,
  133. };
  134. static struct var_t vars[NB_ID] = {
  135. [CAPS_START_ID] = { CAPS_START, .u.s = {"[:dv ap 200]" } },
  136. [CAPS_STOP_ID] = { CAPS_STOP, .u.s = {"[:dv ap 100]" } },
  137. [RATE_ID] = { RATE, .u.n = {"[:ra %d]", 9, 0, 18, 150, 25, NULL } },
  138. [PITCH_ID] = { PITCH, .u.n = {"[:dv ap %d]", 80, 0, 100, 20, 0, NULL } },
  139. [INFLECTION_ID] = { INFLECTION, .u.n = {"[:dv pr %d] ", 100, 0, 10000, 0, 0, NULL } },
  140. [VOL_ID] = { VOL, .u.n = {"[:vo se %d]", 5, 0, 9, 5, 10, NULL } },
  141. [PUNCT_ID] = { PUNCT, .u.n = {"[:pu %c]", 0, 0, 2, 0, 0, "nsa" } },
  142. [VOICE_ID] = { VOICE, .u.n = {"[:n%c]", 0, 0, 9, 0, 0, "phfdburwkv" } },
  143. [DIRECT_ID] = { DIRECT, .u.n = {NULL, 0, 0, 1, 0, 0, NULL } },
  144. V_LAST_VAR
  145. };
  146. /*
  147. * These attributes will appear in /sys/accessibility/speakup/decpc.
  148. */
  149. static struct kobj_attribute caps_start_attribute =
  150. __ATTR(caps_start, 0644, spk_var_show, spk_var_store);
  151. static struct kobj_attribute caps_stop_attribute =
  152. __ATTR(caps_stop, 0644, spk_var_show, spk_var_store);
  153. static struct kobj_attribute pitch_attribute =
  154. __ATTR(pitch, 0644, spk_var_show, spk_var_store);
  155. static struct kobj_attribute inflection_attribute =
  156. __ATTR(inflection, 0644, spk_var_show, spk_var_store);
  157. static struct kobj_attribute punct_attribute =
  158. __ATTR(punct, 0644, spk_var_show, spk_var_store);
  159. static struct kobj_attribute rate_attribute =
  160. __ATTR(rate, 0644, spk_var_show, spk_var_store);
  161. static struct kobj_attribute voice_attribute =
  162. __ATTR(voice, 0644, spk_var_show, spk_var_store);
  163. static struct kobj_attribute vol_attribute =
  164. __ATTR(vol, 0644, spk_var_show, spk_var_store);
  165. static struct kobj_attribute delay_time_attribute =
  166. __ATTR(delay_time, 0644, spk_var_show, spk_var_store);
  167. static struct kobj_attribute direct_attribute =
  168. __ATTR(direct, 0644, spk_var_show, spk_var_store);
  169. static struct kobj_attribute full_time_attribute =
  170. __ATTR(full_time, 0644, spk_var_show, spk_var_store);
  171. static struct kobj_attribute jiffy_delta_attribute =
  172. __ATTR(jiffy_delta, 0644, spk_var_show, spk_var_store);
  173. static struct kobj_attribute trigger_time_attribute =
  174. __ATTR(trigger_time, 0644, spk_var_show, spk_var_store);
  175. /*
  176. * Create a group of attributes so that we can create and destroy them all
  177. * at once.
  178. */
  179. static struct attribute *synth_attrs[] = {
  180. &caps_start_attribute.attr,
  181. &caps_stop_attribute.attr,
  182. &pitch_attribute.attr,
  183. &inflection_attribute.attr,
  184. &punct_attribute.attr,
  185. &rate_attribute.attr,
  186. &voice_attribute.attr,
  187. &vol_attribute.attr,
  188. &delay_time_attribute.attr,
  189. &direct_attribute.attr,
  190. &full_time_attribute.attr,
  191. &jiffy_delta_attribute.attr,
  192. &trigger_time_attribute.attr,
  193. NULL, /* need to NULL terminate the list of attributes */
  194. };
  195. static struct spk_synth synth_dec_pc = {
  196. .name = "decpc",
  197. .version = DRV_VERSION,
  198. .long_name = "Dectalk PC",
  199. .init = "[:pe -380]",
  200. .procspeech = PROCSPEECH,
  201. .delay = 500,
  202. .trigger = 50,
  203. .jiffies = 50,
  204. .full = 1000,
  205. .flags = SF_DEC,
  206. .startup = SYNTH_START,
  207. .checkval = SYNTH_CHECK,
  208. .vars = vars,
  209. .io_ops = &spk_serial_io_ops,
  210. .probe = synth_probe,
  211. .release = dtpc_release,
  212. .synth_immediate = synth_immediate,
  213. .catch_up = do_catch_up,
  214. .flush = synth_flush,
  215. .is_alive = spk_synth_is_alive_nop,
  216. .synth_adjust = NULL,
  217. .read_buff_add = NULL,
  218. .get_index = NULL,
  219. .indexing = {
  220. .command = NULL,
  221. .lowindex = 0,
  222. .highindex = 0,
  223. .currindex = 0,
  224. },
  225. .attributes = {
  226. .attrs = synth_attrs,
  227. .name = "decpc",
  228. },
  229. };
  230. static int dt_getstatus(void)
  231. {
  232. dt_stat = inb_p(speakup_info.port_tts) |
  233. (inb_p(speakup_info.port_tts + 1) << 8);
  234. return dt_stat;
  235. }
  236. static void dt_sendcmd(u_int cmd)
  237. {
  238. outb_p(cmd & 0xFF, speakup_info.port_tts);
  239. outb_p((cmd >> 8) & 0xFF, speakup_info.port_tts + 1);
  240. }
  241. static int dt_waitbit(int bit)
  242. {
  243. int timeout = 100;
  244. while (--timeout > 0) {
  245. if ((dt_getstatus() & bit) == bit)
  246. return 1;
  247. udelay(50);
  248. }
  249. return 0;
  250. }
  251. static int dt_wait_dma(void)
  252. {
  253. int timeout = 100, state = dma_state;
  254. if (!dt_waitbit(STAT_dma_ready))
  255. return 0;
  256. while (--timeout > 0) {
  257. if ((dt_getstatus() & STAT_dma_state) == state)
  258. return 1;
  259. udelay(50);
  260. }
  261. dma_state = dt_getstatus() & STAT_dma_state;
  262. return 1;
  263. }
  264. static int dt_ctrl(u_int cmd)
  265. {
  266. int timeout = 10;
  267. if (!dt_waitbit(STAT_cmd_ready))
  268. return -1;
  269. outb_p(0, speakup_info.port_tts + 2);
  270. outb_p(0, speakup_info.port_tts + 3);
  271. dt_getstatus();
  272. dt_sendcmd(CMD_control | cmd);
  273. outb_p(0, speakup_info.port_tts + 6);
  274. while (dt_getstatus() & STAT_cmd_ready) {
  275. udelay(20);
  276. if (--timeout == 0)
  277. break;
  278. }
  279. dt_sendcmd(CMD_null);
  280. return 0;
  281. }
  282. static void synth_flush(struct spk_synth *synth)
  283. {
  284. int timeout = 10;
  285. if (is_flushing)
  286. return;
  287. is_flushing = 4;
  288. in_escape = 0;
  289. while (dt_ctrl(CTRL_flush)) {
  290. if (--timeout == 0)
  291. break;
  292. udelay(50);
  293. }
  294. for (timeout = 0; timeout < 10; timeout++) {
  295. if (dt_waitbit(STAT_dma_ready))
  296. break;
  297. udelay(50);
  298. }
  299. outb_p(DMA_sync, speakup_info.port_tts + 4);
  300. outb_p(0, speakup_info.port_tts + 4);
  301. udelay(100);
  302. for (timeout = 0; timeout < 10; timeout++) {
  303. if (!(dt_getstatus() & STAT_flushing))
  304. break;
  305. udelay(50);
  306. }
  307. dma_state = dt_getstatus() & STAT_dma_state;
  308. dma_state ^= STAT_dma_state;
  309. is_flushing = 0;
  310. }
  311. static int dt_sendchar(char ch)
  312. {
  313. if (!dt_wait_dma())
  314. return -1;
  315. if (!(dt_stat & STAT_rr_char))
  316. return -2;
  317. outb_p(DMA_single_in, speakup_info.port_tts + 4);
  318. outb_p(ch, speakup_info.port_tts + 4);
  319. dma_state ^= STAT_dma_state;
  320. return 0;
  321. }
  322. static int testkernel(void)
  323. {
  324. int status = 0;
  325. if (dt_getstatus() == 0xffff) {
  326. status = -1;
  327. goto oops;
  328. }
  329. dt_sendcmd(CMD_sync);
  330. if (!dt_waitbit(STAT_cmd_ready))
  331. status = -2;
  332. else if (dt_stat & 0x8000)
  333. return 0;
  334. else if (dt_stat == 0x0dec)
  335. pr_warn("dec_pc at 0x%x, software not loaded\n",
  336. speakup_info.port_tts);
  337. status = -3;
  338. oops: synth_release_region(speakup_info.port_tts, SYNTH_IO_EXTENT);
  339. speakup_info.port_tts = 0;
  340. return status;
  341. }
  342. static void do_catch_up(struct spk_synth *synth)
  343. {
  344. u_char ch;
  345. static u_char last;
  346. unsigned long flags;
  347. unsigned long jiff_max;
  348. struct var_t *jiffy_delta;
  349. struct var_t *delay_time;
  350. int jiffy_delta_val;
  351. int delay_time_val;
  352. jiffy_delta = spk_get_var(JIFFY);
  353. delay_time = spk_get_var(DELAY);
  354. spin_lock_irqsave(&speakup_info.spinlock, flags);
  355. jiffy_delta_val = jiffy_delta->u.n.value;
  356. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  357. jiff_max = jiffies + jiffy_delta_val;
  358. while (!kthread_should_stop()) {
  359. spin_lock_irqsave(&speakup_info.spinlock, flags);
  360. if (speakup_info.flushing) {
  361. speakup_info.flushing = 0;
  362. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  363. synth->flush(synth);
  364. continue;
  365. }
  366. synth_buffer_skip_nonlatin1();
  367. if (synth_buffer_empty()) {
  368. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  369. break;
  370. }
  371. ch = synth_buffer_peek();
  372. set_current_state(TASK_INTERRUPTIBLE);
  373. delay_time_val = delay_time->u.n.value;
  374. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  375. if (ch == '\n')
  376. ch = 0x0D;
  377. if (dt_sendchar(ch)) {
  378. schedule_timeout(msecs_to_jiffies(delay_time_val));
  379. continue;
  380. }
  381. set_current_state(TASK_RUNNING);
  382. spin_lock_irqsave(&speakup_info.spinlock, flags);
  383. synth_buffer_getc();
  384. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  385. if (ch == '[') {
  386. in_escape = 1;
  387. } else if (ch == ']') {
  388. in_escape = 0;
  389. } else if (ch <= SPACE) {
  390. if (!in_escape && strchr(",.!?;:", last))
  391. dt_sendchar(PROCSPEECH);
  392. if (time_after_eq(jiffies, jiff_max)) {
  393. if (!in_escape)
  394. dt_sendchar(PROCSPEECH);
  395. spin_lock_irqsave(&speakup_info.spinlock,
  396. flags);
  397. jiffy_delta_val = jiffy_delta->u.n.value;
  398. delay_time_val = delay_time->u.n.value;
  399. spin_unlock_irqrestore(&speakup_info.spinlock,
  400. flags);
  401. schedule_timeout(msecs_to_jiffies
  402. (delay_time_val));
  403. jiff_max = jiffies + jiffy_delta_val;
  404. }
  405. }
  406. last = ch;
  407. ch = 0;
  408. }
  409. if (!in_escape)
  410. dt_sendchar(PROCSPEECH);
  411. }
  412. static const char *synth_immediate(struct spk_synth *synth, const char *buf)
  413. {
  414. u_char ch;
  415. while ((ch = *buf)) {
  416. if (ch == '\n')
  417. ch = PROCSPEECH;
  418. if (dt_sendchar(ch))
  419. return buf;
  420. buf++;
  421. }
  422. return NULL;
  423. }
  424. static int synth_probe(struct spk_synth *synth)
  425. {
  426. int i = 0, failed = 0;
  427. pr_info("Probing for %s.\n", synth->long_name);
  428. for (i = 0; synth_portlist[i]; i++) {
  429. if (synth_request_region(synth_portlist[i], SYNTH_IO_EXTENT)) {
  430. pr_warn("request_region: failed with 0x%x, %d\n",
  431. synth_portlist[i], SYNTH_IO_EXTENT);
  432. continue;
  433. }
  434. speakup_info.port_tts = synth_portlist[i];
  435. failed = testkernel();
  436. if (failed == 0)
  437. break;
  438. }
  439. if (failed) {
  440. pr_info("%s: not found\n", synth->long_name);
  441. return -ENODEV;
  442. }
  443. pr_info("%s: %03x-%03x, Driver Version %s,\n", synth->long_name,
  444. speakup_info.port_tts, speakup_info.port_tts + 7,
  445. synth->version);
  446. synth->alive = 1;
  447. return 0;
  448. }
  449. static void dtpc_release(struct spk_synth *synth)
  450. {
  451. spk_stop_serial_interrupt();
  452. if (speakup_info.port_tts)
  453. synth_release_region(speakup_info.port_tts, SYNTH_IO_EXTENT);
  454. speakup_info.port_tts = 0;
  455. }
  456. module_param_named(start, synth_dec_pc.startup, short, 0444);
  457. module_param_named(rate, vars[RATE_ID].u.n.default_val, int, 0444);
  458. module_param_named(pitch, vars[PITCH_ID].u.n.default_val, int, 0444);
  459. module_param_named(inflection, vars[INFLECTION_ID].u.n.default_val, int, 0444);
  460. module_param_named(vol, vars[VOL_ID].u.n.default_val, int, 0444);
  461. module_param_named(punct, vars[PUNCT_ID].u.n.default_val, int, 0444);
  462. module_param_named(voice, vars[VOICE_ID].u.n.default_val, int, 0444);
  463. module_param_named(direct, vars[DIRECT_ID].u.n.default_val, int, 0444);
  464. MODULE_PARM_DESC(start, "Start the synthesizer once it is loaded.");
  465. MODULE_PARM_DESC(rate, "Set the rate variable on load.");
  466. MODULE_PARM_DESC(pitch, "Set the pitch variable on load.");
  467. MODULE_PARM_DESC(inflection, "Set the inflection variable on load.");
  468. MODULE_PARM_DESC(vol, "Set the vol variable on load.");
  469. MODULE_PARM_DESC(punct, "Set the punct variable on load.");
  470. MODULE_PARM_DESC(voice, "Set the voice variable on load.");
  471. MODULE_PARM_DESC(direct, "Set the direct variable on load.");
  472. module_spk_synth(synth_dec_pc);
  473. MODULE_AUTHOR("Kirk Reiser <kirk@braille.uwo.ca>");
  474. MODULE_AUTHOR("David Borowski");
  475. MODULE_DESCRIPTION("Speakup support for DECtalk PC synthesizers");
  476. MODULE_LICENSE("GPL");
  477. MODULE_VERSION(DRV_VERSION);