speakup_decpc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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 <[email protected]>
  10. *
  11. * Linux DECtalk PC driver:
  12. * Copyright (c) 1997 Nicolas Pitre <[email protected]>
  13. *
  14. * speakup DECtalk PC Internal driver:
  15. * Copyright (c) 2003 David Borowski <[email protected]>
  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. static struct var_t vars[] = {
  128. { CAPS_START, .u.s = {"[:dv ap 200]" } },
  129. { CAPS_STOP, .u.s = {"[:dv ap 100]" } },
  130. { RATE, .u.n = {"[:ra %d]", 9, 0, 18, 150, 25, NULL } },
  131. { PITCH, .u.n = {"[:dv ap %d]", 80, 0, 100, 20, 0, NULL } },
  132. { INFLECTION, .u.n = {"[:dv pr %d] ", 100, 0, 10000, 0, 0, NULL } },
  133. { VOL, .u.n = {"[:vo se %d]", 5, 0, 9, 5, 10, NULL } },
  134. { PUNCT, .u.n = {"[:pu %c]", 0, 0, 2, 0, 0, "nsa" } },
  135. { VOICE, .u.n = {"[:n%c]", 0, 0, 9, 0, 0, "phfdburwkv" } },
  136. { DIRECT, .u.n = {NULL, 0, 0, 1, 0, 0, NULL } },
  137. V_LAST_VAR
  138. };
  139. /*
  140. * These attributes will appear in /sys/accessibility/speakup/decpc.
  141. */
  142. static struct kobj_attribute caps_start_attribute =
  143. __ATTR(caps_start, 0644, spk_var_show, spk_var_store);
  144. static struct kobj_attribute caps_stop_attribute =
  145. __ATTR(caps_stop, 0644, spk_var_show, spk_var_store);
  146. static struct kobj_attribute pitch_attribute =
  147. __ATTR(pitch, 0644, spk_var_show, spk_var_store);
  148. static struct kobj_attribute inflection_attribute =
  149. __ATTR(inflection, 0644, spk_var_show, spk_var_store);
  150. static struct kobj_attribute punct_attribute =
  151. __ATTR(punct, 0644, spk_var_show, spk_var_store);
  152. static struct kobj_attribute rate_attribute =
  153. __ATTR(rate, 0644, spk_var_show, spk_var_store);
  154. static struct kobj_attribute voice_attribute =
  155. __ATTR(voice, 0644, spk_var_show, spk_var_store);
  156. static struct kobj_attribute vol_attribute =
  157. __ATTR(vol, 0644, spk_var_show, spk_var_store);
  158. static struct kobj_attribute delay_time_attribute =
  159. __ATTR(delay_time, 0644, spk_var_show, spk_var_store);
  160. static struct kobj_attribute direct_attribute =
  161. __ATTR(direct, 0644, spk_var_show, spk_var_store);
  162. static struct kobj_attribute full_time_attribute =
  163. __ATTR(full_time, 0644, spk_var_show, spk_var_store);
  164. static struct kobj_attribute jiffy_delta_attribute =
  165. __ATTR(jiffy_delta, 0644, spk_var_show, spk_var_store);
  166. static struct kobj_attribute trigger_time_attribute =
  167. __ATTR(trigger_time, 0644, spk_var_show, spk_var_store);
  168. /*
  169. * Create a group of attributes so that we can create and destroy them all
  170. * at once.
  171. */
  172. static struct attribute *synth_attrs[] = {
  173. &caps_start_attribute.attr,
  174. &caps_stop_attribute.attr,
  175. &pitch_attribute.attr,
  176. &inflection_attribute.attr,
  177. &punct_attribute.attr,
  178. &rate_attribute.attr,
  179. &voice_attribute.attr,
  180. &vol_attribute.attr,
  181. &delay_time_attribute.attr,
  182. &direct_attribute.attr,
  183. &full_time_attribute.attr,
  184. &jiffy_delta_attribute.attr,
  185. &trigger_time_attribute.attr,
  186. NULL, /* need to NULL terminate the list of attributes */
  187. };
  188. static struct spk_synth synth_dec_pc = {
  189. .name = "decpc",
  190. .version = DRV_VERSION,
  191. .long_name = "Dectalk PC",
  192. .init = "[:pe -380]",
  193. .procspeech = PROCSPEECH,
  194. .delay = 500,
  195. .trigger = 50,
  196. .jiffies = 50,
  197. .full = 1000,
  198. .flags = SF_DEC,
  199. .startup = SYNTH_START,
  200. .checkval = SYNTH_CHECK,
  201. .vars = vars,
  202. .io_ops = &spk_serial_io_ops,
  203. .probe = synth_probe,
  204. .release = dtpc_release,
  205. .synth_immediate = synth_immediate,
  206. .catch_up = do_catch_up,
  207. .flush = synth_flush,
  208. .is_alive = spk_synth_is_alive_nop,
  209. .synth_adjust = NULL,
  210. .read_buff_add = NULL,
  211. .get_index = NULL,
  212. .indexing = {
  213. .command = NULL,
  214. .lowindex = 0,
  215. .highindex = 0,
  216. .currindex = 0,
  217. },
  218. .attributes = {
  219. .attrs = synth_attrs,
  220. .name = "decpc",
  221. },
  222. };
  223. static int dt_getstatus(void)
  224. {
  225. dt_stat = inb_p(speakup_info.port_tts) |
  226. (inb_p(speakup_info.port_tts + 1) << 8);
  227. return dt_stat;
  228. }
  229. static void dt_sendcmd(u_int cmd)
  230. {
  231. outb_p(cmd & 0xFF, speakup_info.port_tts);
  232. outb_p((cmd >> 8) & 0xFF, speakup_info.port_tts + 1);
  233. }
  234. static int dt_waitbit(int bit)
  235. {
  236. int timeout = 100;
  237. while (--timeout > 0) {
  238. if ((dt_getstatus() & bit) == bit)
  239. return 1;
  240. udelay(50);
  241. }
  242. return 0;
  243. }
  244. static int dt_wait_dma(void)
  245. {
  246. int timeout = 100, state = dma_state;
  247. if (!dt_waitbit(STAT_dma_ready))
  248. return 0;
  249. while (--timeout > 0) {
  250. if ((dt_getstatus() & STAT_dma_state) == state)
  251. return 1;
  252. udelay(50);
  253. }
  254. dma_state = dt_getstatus() & STAT_dma_state;
  255. return 1;
  256. }
  257. static int dt_ctrl(u_int cmd)
  258. {
  259. int timeout = 10;
  260. if (!dt_waitbit(STAT_cmd_ready))
  261. return -1;
  262. outb_p(0, speakup_info.port_tts + 2);
  263. outb_p(0, speakup_info.port_tts + 3);
  264. dt_getstatus();
  265. dt_sendcmd(CMD_control | cmd);
  266. outb_p(0, speakup_info.port_tts + 6);
  267. while (dt_getstatus() & STAT_cmd_ready) {
  268. udelay(20);
  269. if (--timeout == 0)
  270. break;
  271. }
  272. dt_sendcmd(CMD_null);
  273. return 0;
  274. }
  275. static void synth_flush(struct spk_synth *synth)
  276. {
  277. int timeout = 10;
  278. if (is_flushing)
  279. return;
  280. is_flushing = 4;
  281. in_escape = 0;
  282. while (dt_ctrl(CTRL_flush)) {
  283. if (--timeout == 0)
  284. break;
  285. udelay(50);
  286. }
  287. for (timeout = 0; timeout < 10; timeout++) {
  288. if (dt_waitbit(STAT_dma_ready))
  289. break;
  290. udelay(50);
  291. }
  292. outb_p(DMA_sync, speakup_info.port_tts + 4);
  293. outb_p(0, speakup_info.port_tts + 4);
  294. udelay(100);
  295. for (timeout = 0; timeout < 10; timeout++) {
  296. if (!(dt_getstatus() & STAT_flushing))
  297. break;
  298. udelay(50);
  299. }
  300. dma_state = dt_getstatus() & STAT_dma_state;
  301. dma_state ^= STAT_dma_state;
  302. is_flushing = 0;
  303. }
  304. static int dt_sendchar(char ch)
  305. {
  306. if (!dt_wait_dma())
  307. return -1;
  308. if (!(dt_stat & STAT_rr_char))
  309. return -2;
  310. outb_p(DMA_single_in, speakup_info.port_tts + 4);
  311. outb_p(ch, speakup_info.port_tts + 4);
  312. dma_state ^= STAT_dma_state;
  313. return 0;
  314. }
  315. static int testkernel(void)
  316. {
  317. int status = 0;
  318. if (dt_getstatus() == 0xffff) {
  319. status = -1;
  320. goto oops;
  321. }
  322. dt_sendcmd(CMD_sync);
  323. if (!dt_waitbit(STAT_cmd_ready))
  324. status = -2;
  325. else if (dt_stat & 0x8000)
  326. return 0;
  327. else if (dt_stat == 0x0dec)
  328. pr_warn("dec_pc at 0x%x, software not loaded\n",
  329. speakup_info.port_tts);
  330. status = -3;
  331. oops: synth_release_region(speakup_info.port_tts, SYNTH_IO_EXTENT);
  332. speakup_info.port_tts = 0;
  333. return status;
  334. }
  335. static void do_catch_up(struct spk_synth *synth)
  336. {
  337. u_char ch;
  338. static u_char last;
  339. unsigned long flags;
  340. unsigned long jiff_max;
  341. struct var_t *jiffy_delta;
  342. struct var_t *delay_time;
  343. int jiffy_delta_val;
  344. int delay_time_val;
  345. jiffy_delta = spk_get_var(JIFFY);
  346. delay_time = spk_get_var(DELAY);
  347. spin_lock_irqsave(&speakup_info.spinlock, flags);
  348. jiffy_delta_val = jiffy_delta->u.n.value;
  349. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  350. jiff_max = jiffies + jiffy_delta_val;
  351. while (!kthread_should_stop()) {
  352. spin_lock_irqsave(&speakup_info.spinlock, flags);
  353. if (speakup_info.flushing) {
  354. speakup_info.flushing = 0;
  355. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  356. synth->flush(synth);
  357. continue;
  358. }
  359. synth_buffer_skip_nonlatin1();
  360. if (synth_buffer_empty()) {
  361. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  362. break;
  363. }
  364. ch = synth_buffer_peek();
  365. set_current_state(TASK_INTERRUPTIBLE);
  366. delay_time_val = delay_time->u.n.value;
  367. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  368. if (ch == '\n')
  369. ch = 0x0D;
  370. if (dt_sendchar(ch)) {
  371. schedule_timeout(msecs_to_jiffies(delay_time_val));
  372. continue;
  373. }
  374. set_current_state(TASK_RUNNING);
  375. spin_lock_irqsave(&speakup_info.spinlock, flags);
  376. synth_buffer_getc();
  377. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  378. if (ch == '[') {
  379. in_escape = 1;
  380. } else if (ch == ']') {
  381. in_escape = 0;
  382. } else if (ch <= SPACE) {
  383. if (!in_escape && strchr(",.!?;:", last))
  384. dt_sendchar(PROCSPEECH);
  385. if (time_after_eq(jiffies, jiff_max)) {
  386. if (!in_escape)
  387. dt_sendchar(PROCSPEECH);
  388. spin_lock_irqsave(&speakup_info.spinlock,
  389. flags);
  390. jiffy_delta_val = jiffy_delta->u.n.value;
  391. delay_time_val = delay_time->u.n.value;
  392. spin_unlock_irqrestore(&speakup_info.spinlock,
  393. flags);
  394. schedule_timeout(msecs_to_jiffies
  395. (delay_time_val));
  396. jiff_max = jiffies + jiffy_delta_val;
  397. }
  398. }
  399. last = ch;
  400. ch = 0;
  401. }
  402. if (!in_escape)
  403. dt_sendchar(PROCSPEECH);
  404. }
  405. static const char *synth_immediate(struct spk_synth *synth, const char *buf)
  406. {
  407. u_char ch;
  408. while ((ch = *buf)) {
  409. if (ch == '\n')
  410. ch = PROCSPEECH;
  411. if (dt_sendchar(ch))
  412. return buf;
  413. buf++;
  414. }
  415. return NULL;
  416. }
  417. static int synth_probe(struct spk_synth *synth)
  418. {
  419. int i = 0, failed = 0;
  420. pr_info("Probing for %s.\n", synth->long_name);
  421. for (i = 0; synth_portlist[i]; i++) {
  422. if (synth_request_region(synth_portlist[i], SYNTH_IO_EXTENT)) {
  423. pr_warn("request_region: failed with 0x%x, %d\n",
  424. synth_portlist[i], SYNTH_IO_EXTENT);
  425. continue;
  426. }
  427. speakup_info.port_tts = synth_portlist[i];
  428. failed = testkernel();
  429. if (failed == 0)
  430. break;
  431. }
  432. if (failed) {
  433. pr_info("%s: not found\n", synth->long_name);
  434. return -ENODEV;
  435. }
  436. pr_info("%s: %03x-%03x, Driver Version %s,\n", synth->long_name,
  437. speakup_info.port_tts, speakup_info.port_tts + 7,
  438. synth->version);
  439. synth->alive = 1;
  440. return 0;
  441. }
  442. static void dtpc_release(struct spk_synth *synth)
  443. {
  444. spk_stop_serial_interrupt();
  445. if (speakup_info.port_tts)
  446. synth_release_region(speakup_info.port_tts, SYNTH_IO_EXTENT);
  447. speakup_info.port_tts = 0;
  448. }
  449. module_param_named(start, synth_dec_pc.startup, short, 0444);
  450. MODULE_PARM_DESC(start, "Start the synthesizer once it is loaded.");
  451. module_spk_synth(synth_dec_pc);
  452. MODULE_AUTHOR("Kirk Reiser <[email protected]>");
  453. MODULE_AUTHOR("David Borowski");
  454. MODULE_DESCRIPTION("Speakup support for DECtalk PC synthesizers");
  455. MODULE_LICENSE("GPL");
  456. MODULE_VERSION(DRV_VERSION);