speakup_decpc.c 15 KB

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