/drivers/staging/speakup/speakup_decpc.c

https://bitbucket.org/slukk/jb-tsm-kernel-4.2 · C · 504 lines · 431 code · 34 blank · 39 comment · 57 complexity · ed7c7aadb1ec186f2ccddadf56210b14 MD5 · raw file

  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 /* madk 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. #define CMD_test 0x2000 /* self-test request */
  90. #define TEST_mask 0x0F00 /* isolate test field */
  91. #define TEST_null 0x0000 /* no test requested */
  92. #define TEST_isa_int 0x0100 /* assert isa irq */
  93. #define TEST_echo 0x0200 /* make data in == data out */
  94. #define TEST_seg 0x0300 /* set peek/poke segment */
  95. #define TEST_off 0x0400 /* set peek/poke offset */
  96. #define TEST_peek 0x0500 /* data out == *peek */
  97. #define TEST_poke 0x0600 /* *peek == data in */
  98. #define TEST_sub_code 0x00FF /* user defined test sub codes */
  99. #define CMD_id 0x3000 /* return software id */
  100. #define ID_null 0x0000 /* null id */
  101. #define ID_kernel 0x0100 /* kernel code executing */
  102. #define ID_boot 0x0200 /* boot code executing */
  103. #define CMD_dma 0x4000 /* force a dma start */
  104. #define CMD_reset 0x5000 /* reset module status */
  105. #define CMD_sync 0x6000 /* kernel sync command */
  106. #define CMD_char_in 0x7000 /* single character send */
  107. #define CMD_char_out 0x8000 /* single character get */
  108. #define CHAR_count_1 0x0100 /* one char in cmd_low */
  109. #define CHAR_count_2 0x0200 /* the second in data_low */
  110. #define CHAR_count_3 0x0300 /* the third in data_high */
  111. #define CMD_spc_mode 0x9000 /* change spc mode */
  112. #define CMD_spc_to_text 0x0100 /* set to text mode */
  113. #define CMD_spc_to_digit 0x0200 /* set to digital mode */
  114. #define CMD_spc_rate 0x0400 /* change spc data rate */
  115. #define CMD_error 0xf000 /* severe error */
  116. enum { PRIMARY_DIC = 0, USER_DIC, COMMAND_DIC, ABBREV_DIC };
  117. #define DMA_single_in 0x01
  118. #define DMA_single_out 0x02
  119. #define DMA_buff_in 0x03
  120. #define DMA_buff_out 0x04
  121. #define DMA_control 0x05
  122. #define DT_MEM_ALLOC 0x03
  123. #define DT_SET_DIC 0x04
  124. #define DT_START_TASK 0x05
  125. #define DT_LOAD_MEM 0x06
  126. #define DT_READ_MEM 0x07
  127. #define DT_DIGITAL_IN 0x08
  128. #define DMA_sync 0x06
  129. #define DMA_sync_char 0x07
  130. #define DRV_VERSION "2.12"
  131. #define PROCSPEECH 0x0b
  132. #define SYNTH_IO_EXTENT 8
  133. static int synth_probe(struct spk_synth *synth);
  134. static void dtpc_release(void);
  135. static const char *synth_immediate(struct spk_synth *synth, const char *buf);
  136. static void do_catch_up(struct spk_synth *synth);
  137. static void synth_flush(struct spk_synth *synth);
  138. static int synth_portlist[] = { 0x340, 0x350, 0x240, 0x250, 0 };
  139. static int in_escape, is_flushing;
  140. static int dt_stat, dma_state;
  141. static struct var_t vars[] = {
  142. { CAPS_START, .u.s = {"[:dv ap 200]" } },
  143. { CAPS_STOP, .u.s = {"[:dv ap 100]" } },
  144. { RATE, .u.n = {"[:ra %d]", 9, 0, 18, 150, 25, NULL } },
  145. { PITCH, .u.n = {"[:dv ap %d]", 80, 0, 100, 20, 0, NULL } },
  146. { VOL, .u.n = {"[:vo se %d]", 5, 0, 9, 5, 10, NULL } },
  147. { PUNCT, .u.n = {"[:pu %c]", 0, 0, 2, 0, 0, "nsa" } },
  148. { VOICE, .u.n = {"[:n%c]", 0, 0, 9, 0, 0, "phfdburwkv" } },
  149. { DIRECT, .u.n = {NULL, 0, 0, 1, 0, 0, NULL } },
  150. V_LAST_VAR
  151. };
  152. /*
  153. * These attributes will appear in /sys/accessibility/speakup/decpc.
  154. */
  155. static struct kobj_attribute caps_start_attribute =
  156. __ATTR(caps_start, USER_RW, spk_var_show, spk_var_store);
  157. static struct kobj_attribute caps_stop_attribute =
  158. __ATTR(caps_stop, USER_RW, spk_var_show, spk_var_store);
  159. static struct kobj_attribute pitch_attribute =
  160. __ATTR(pitch, USER_RW, spk_var_show, spk_var_store);
  161. static struct kobj_attribute punct_attribute =
  162. __ATTR(punct, USER_RW, spk_var_show, spk_var_store);
  163. static struct kobj_attribute rate_attribute =
  164. __ATTR(rate, USER_RW, spk_var_show, spk_var_store);
  165. static struct kobj_attribute voice_attribute =
  166. __ATTR(voice, USER_RW, spk_var_show, spk_var_store);
  167. static struct kobj_attribute vol_attribute =
  168. __ATTR(vol, USER_RW, spk_var_show, spk_var_store);
  169. static struct kobj_attribute delay_time_attribute =
  170. __ATTR(delay_time, ROOT_W, spk_var_show, spk_var_store);
  171. static struct kobj_attribute direct_attribute =
  172. __ATTR(direct, USER_RW, spk_var_show, spk_var_store);
  173. static struct kobj_attribute full_time_attribute =
  174. __ATTR(full_time, ROOT_W, spk_var_show, spk_var_store);
  175. static struct kobj_attribute jiffy_delta_attribute =
  176. __ATTR(jiffy_delta, ROOT_W, spk_var_show, spk_var_store);
  177. static struct kobj_attribute trigger_time_attribute =
  178. __ATTR(trigger_time, ROOT_W, spk_var_show, spk_var_store);
  179. /*
  180. * Create a group of attributes so that we can create and destroy them all
  181. * at once.
  182. */
  183. static struct attribute *synth_attrs[] = {
  184. &caps_start_attribute.attr,
  185. &caps_stop_attribute.attr,
  186. &pitch_attribute.attr,
  187. &punct_attribute.attr,
  188. &rate_attribute.attr,
  189. &voice_attribute.attr,
  190. &vol_attribute.attr,
  191. &delay_time_attribute.attr,
  192. &direct_attribute.attr,
  193. &full_time_attribute.attr,
  194. &jiffy_delta_attribute.attr,
  195. &trigger_time_attribute.attr,
  196. NULL, /* need to NULL terminate the list of attributes */
  197. };
  198. static struct spk_synth synth_dec_pc = {
  199. .name = "decpc",
  200. .version = DRV_VERSION,
  201. .long_name = "Dectalk PC",
  202. .init = "[:pe -380]",
  203. .procspeech = PROCSPEECH,
  204. .delay = 500,
  205. .trigger = 50,
  206. .jiffies = 50,
  207. .full = 1000,
  208. .flags = SF_DEC,
  209. .startup = SYNTH_START,
  210. .checkval = SYNTH_CHECK,
  211. .vars = vars,
  212. .probe = synth_probe,
  213. .release = dtpc_release,
  214. .synth_immediate = synth_immediate,
  215. .catch_up = do_catch_up,
  216. .flush = synth_flush,
  217. .is_alive = spk_synth_is_alive_nop,
  218. .synth_adjust = NULL,
  219. .read_buff_add = NULL,
  220. .get_index = NULL,
  221. .indexing = {
  222. .command = NULL,
  223. .lowindex = 0,
  224. .highindex = 0,
  225. .currindex = 0,
  226. },
  227. .attributes = {
  228. .attrs = synth_attrs,
  229. .name = "decpc",
  230. },
  231. };
  232. static int dt_getstatus(void)
  233. {
  234. dt_stat = inb_p(speakup_info.port_tts) |
  235. (inb_p(speakup_info.port_tts + 1) << 8);
  236. return dt_stat;
  237. }
  238. static void dt_sendcmd(u_int cmd)
  239. {
  240. outb_p(cmd & 0xFF, speakup_info.port_tts);
  241. outb_p((cmd >> 8) & 0xFF, speakup_info.port_tts+1);
  242. }
  243. static int dt_waitbit(int bit)
  244. {
  245. int timeout = 100;
  246. while (--timeout > 0) {
  247. if ((dt_getstatus() & bit) == bit)
  248. return 1;
  249. udelay(50);
  250. }
  251. return 0;
  252. }
  253. static int dt_wait_dma(void)
  254. {
  255. int timeout = 100, state = dma_state;
  256. if (!dt_waitbit(STAT_dma_ready))
  257. return 0;
  258. while (--timeout > 0) {
  259. if ((dt_getstatus()&STAT_dma_state) == state)
  260. return 1;
  261. udelay(50);
  262. }
  263. dma_state = dt_getstatus() & STAT_dma_state;
  264. return 1;
  265. }
  266. static int dt_ctrl(u_int cmd)
  267. {
  268. int timeout = 10;
  269. if (!dt_waitbit(STAT_cmd_ready))
  270. return -1;
  271. outb_p(0, speakup_info.port_tts+2);
  272. outb_p(0, speakup_info.port_tts+3);
  273. dt_getstatus();
  274. dt_sendcmd(CMD_control|cmd);
  275. outb_p(0, speakup_info.port_tts+6);
  276. while (dt_getstatus() & STAT_cmd_ready) {
  277. udelay(20);
  278. if (--timeout == 0)
  279. break;
  280. }
  281. dt_sendcmd(CMD_null);
  282. return 0;
  283. }
  284. static void synth_flush(struct spk_synth *synth)
  285. {
  286. int timeout = 10;
  287. if (is_flushing)
  288. return;
  289. is_flushing = 4;
  290. in_escape = 0;
  291. while (dt_ctrl(CTRL_flush)) {
  292. if (--timeout == 0)
  293. break;
  294. udelay(50);
  295. }
  296. for (timeout = 0; timeout < 10; timeout++) {
  297. if (dt_waitbit(STAT_dma_ready))
  298. break;
  299. udelay(50);
  300. }
  301. outb_p(DMA_sync, speakup_info.port_tts+4);
  302. outb_p(0, speakup_info.port_tts+4);
  303. udelay(100);
  304. for (timeout = 0; timeout < 10; timeout++) {
  305. if (!(dt_getstatus() & STAT_flushing))
  306. break;
  307. udelay(50);
  308. }
  309. dma_state = dt_getstatus() & STAT_dma_state;
  310. dma_state ^= STAT_dma_state;
  311. is_flushing = 0;
  312. }
  313. static int dt_sendchar(char ch)
  314. {
  315. if (!dt_wait_dma())
  316. return -1;
  317. if (!(dt_stat & STAT_rr_char))
  318. return -2;
  319. outb_p(DMA_single_in, speakup_info.port_tts+4);
  320. outb_p(ch, speakup_info.port_tts+4);
  321. dma_state ^= STAT_dma_state;
  322. return 0;
  323. }
  324. static int testkernel(void)
  325. {
  326. int status = 0;
  327. if (dt_getstatus() == 0xffff) {
  328. status = -1;
  329. goto oops;
  330. }
  331. dt_sendcmd(CMD_sync);
  332. if (!dt_waitbit(STAT_cmd_ready))
  333. status = -2;
  334. else if (dt_stat&0x8000)
  335. return 0;
  336. else if (dt_stat == 0x0dec)
  337. pr_warn("dec_pc at 0x%x, software not loaded\n",
  338. speakup_info.port_tts);
  339. status = -3;
  340. oops: synth_release_region(speakup_info.port_tts, SYNTH_IO_EXTENT);
  341. speakup_info.port_tts = 0;
  342. return status;
  343. }
  344. static void do_catch_up(struct spk_synth *synth)
  345. {
  346. u_char ch;
  347. static u_char last;
  348. unsigned long flags;
  349. unsigned long jiff_max;
  350. struct var_t *jiffy_delta;
  351. struct var_t *delay_time;
  352. int jiffy_delta_val;
  353. int delay_time_val;
  354. jiffy_delta = get_var(JIFFY);
  355. delay_time = get_var(DELAY);
  356. spk_lock(flags);
  357. jiffy_delta_val = jiffy_delta->u.n.value;
  358. spk_unlock(flags);
  359. jiff_max = jiffies + jiffy_delta_val;
  360. while (!kthread_should_stop()) {
  361. spk_lock(flags);
  362. if (speakup_info.flushing) {
  363. speakup_info.flushing = 0;
  364. spk_unlock(flags);
  365. synth->flush(synth);
  366. continue;
  367. }
  368. if (synth_buffer_empty()) {
  369. spk_unlock(flags);
  370. break;
  371. }
  372. ch = synth_buffer_peek();
  373. set_current_state(TASK_INTERRUPTIBLE);
  374. delay_time_val = delay_time->u.n.value;
  375. spk_unlock(flags);
  376. if (ch == '\n')
  377. ch = 0x0D;
  378. if (dt_sendchar(ch)) {
  379. schedule_timeout(msecs_to_jiffies(delay_time_val));
  380. continue;
  381. }
  382. set_current_state(TASK_RUNNING);
  383. spk_lock(flags);
  384. synth_buffer_getc();
  385. spk_unlock(flags);
  386. if (ch == '[')
  387. in_escape = 1;
  388. else if (ch == ']')
  389. in_escape = 0;
  390. else if (ch <= SPACE) {
  391. if (!in_escape && strchr(",.!?;:", last))
  392. dt_sendchar(PROCSPEECH);
  393. if (jiffies >= jiff_max) {
  394. if (!in_escape)
  395. dt_sendchar(PROCSPEECH);
  396. spk_lock(flags);
  397. jiffy_delta_val = jiffy_delta->u.n.value;
  398. delay_time_val = delay_time->u.n.value;
  399. spk_unlock(flags);
  400. schedule_timeout(msecs_to_jiffies
  401. (delay_time_val));
  402. jiff_max = jiffies + jiffy_delta_val;
  403. }
  404. }
  405. last = ch;
  406. ch = 0;
  407. }
  408. if (!in_escape)
  409. dt_sendchar(PROCSPEECH);
  410. }
  411. static const char *synth_immediate(struct spk_synth *synth, const char *buf)
  412. {
  413. u_char ch;
  414. while ((ch = *buf)) {
  415. if (ch == '\n')
  416. ch = PROCSPEECH;
  417. if (dt_sendchar(ch))
  418. return buf;
  419. buf++;
  420. }
  421. return 0;
  422. }
  423. static int synth_probe(struct spk_synth *synth)
  424. {
  425. int i = 0, failed = 0;
  426. pr_info("Probing for %s.\n", synth->long_name);
  427. for (i = 0; synth_portlist[i]; i++) {
  428. if (synth_request_region(synth_portlist[i], SYNTH_IO_EXTENT)) {
  429. pr_warn("request_region: failed with 0x%x, %d\n",
  430. synth_portlist[i], SYNTH_IO_EXTENT);
  431. continue;
  432. }
  433. speakup_info.port_tts = synth_portlist[i];
  434. failed = testkernel();
  435. if (failed == 0)
  436. break;
  437. }
  438. if (failed) {
  439. pr_info("%s: not found\n", synth->long_name);
  440. return -ENODEV;
  441. }
  442. pr_info("%s: %03x-%03x, Driver Version %s,\n", synth->long_name,
  443. speakup_info.port_tts, speakup_info.port_tts + 7,
  444. synth->version);
  445. synth->alive = 1;
  446. return 0;
  447. }
  448. static void dtpc_release(void)
  449. {
  450. if (speakup_info.port_tts)
  451. synth_release_region(speakup_info.port_tts, SYNTH_IO_EXTENT);
  452. speakup_info.port_tts = 0;
  453. }
  454. module_param_named(start, synth_dec_pc.startup, short, S_IRUGO);
  455. MODULE_PARM_DESC(start, "Start the synthesizer once it is loaded.");
  456. static int __init decpc_init(void)
  457. {
  458. return synth_add(&synth_dec_pc);
  459. }
  460. static void __exit decpc_exit(void)
  461. {
  462. synth_remove(&synth_dec_pc);
  463. }
  464. module_init(decpc_init);
  465. module_exit(decpc_exit);
  466. MODULE_AUTHOR("Kirk Reiser <kirk@braille.uwo.ca>");
  467. MODULE_AUTHOR("David Borowski");
  468. MODULE_DESCRIPTION("Speakup support for DECtalk PC synthesizers");
  469. MODULE_LICENSE("GPL");
  470. MODULE_VERSION(DRV_VERSION);