GNU Linux-libre 4.9-gnu1
[releases.git] / drivers / staging / speakup / speakup_decpc.c
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 #include <linux/jiffies.h>
29 #include <linux/sched.h>
30 #include <linux/timer.h>
31 #include <linux/kthread.h>
32
33 #include "spk_priv.h"
34 #include "speakup.h"
35
36 #define MODULE_init             0x0dec          /* module in boot code */
37 #define MODULE_self_test        0x8800          /* module in self-test */
38 #define MODULE_reset            0xffff          /* reinit the whole module */
39
40 #define MODE_mask               0xf000          /* mode bits in high nibble */
41 #define MODE_null               0x0000
42 #define MODE_test               0x2000          /* in testing mode */
43 #define MODE_status             0x8000
44 #define STAT_int                0x0001          /* running in interrupt mode */
45 #define STAT_tr_char            0x0002  /* character data to transmit */
46 #define STAT_rr_char            0x0004  /* ready to receive char data */
47 #define STAT_cmd_ready          0x0008  /* ready to accept commands */
48 #define STAT_dma_ready          0x0010  /* dma command ready */
49 #define STAT_digitized          0x0020  /* spc in digitized mode */
50 #define STAT_new_index          0x0040  /* new last index ready */
51 #define STAT_new_status         0x0080  /* new status posted */
52 #define STAT_dma_state          0x0100  /* dma state toggle */
53 #define STAT_index_valid        0x0200  /* indexs are valid */
54 #define STAT_flushing           0x0400  /* flush in progress */
55 #define STAT_self_test          0x0800  /* module in self test */
56 #define MODE_ready              0xc000  /* module ready for next phase */
57 #define READY_boot              0x0000
58 #define READY_kernel            0x0001
59 #define MODE_error              0xf000
60
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
118 enum {  PRIMARY_DIC     = 0, USER_DIC, COMMAND_DIC, ABBREV_DIC };
119
120 #define DMA_single_in           0x01
121 #define DMA_single_out          0x02
122 #define DMA_buff_in             0x03
123 #define DMA_buff_out            0x04
124 #define DMA_control             0x05
125 #define DT_MEM_ALLOC            0x03
126 #define DT_SET_DIC              0x04
127 #define DT_START_TASK           0x05
128 #define DT_LOAD_MEM             0x06
129 #define DT_READ_MEM             0x07
130 #define DT_DIGITAL_IN           0x08
131 #define DMA_sync                0x06
132 #define DMA_sync_char           0x07
133
134 #define DRV_VERSION "2.12"
135 #define PROCSPEECH 0x0b
136 #define SYNTH_IO_EXTENT 8
137
138 static int synth_probe(struct spk_synth *synth);
139 static void dtpc_release(void);
140 static const char *synth_immediate(struct spk_synth *synth, const char *buf);
141 static void do_catch_up(struct spk_synth *synth);
142 static void synth_flush(struct spk_synth *synth);
143
144 static int synth_portlist[] = { 0x340, 0x350, 0x240, 0x250, 0 };
145 static int in_escape, is_flushing;
146 static int dt_stat, dma_state;
147
148 static struct var_t vars[] = {
149         { CAPS_START, .u.s = {"[:dv ap 200]" } },
150         { CAPS_STOP, .u.s = {"[:dv ap 100]" } },
151         { RATE, .u.n = {"[:ra %d]", 9, 0, 18, 150, 25, NULL } },
152         { PITCH, .u.n = {"[:dv ap %d]", 80, 0, 100, 20, 0, NULL } },
153         { VOL, .u.n = {"[:vo se %d]", 5, 0, 9, 5, 10, NULL } },
154         { PUNCT, .u.n = {"[:pu %c]", 0, 0, 2, 0, 0, "nsa" } },
155         { VOICE, .u.n = {"[:n%c]", 0, 0, 9, 0, 0, "phfdburwkv" } },
156         { DIRECT, .u.n = {NULL, 0, 0, 1, 0, 0, NULL } },
157         V_LAST_VAR
158 };
159
160 /*
161  * These attributes will appear in /sys/accessibility/speakup/decpc.
162  */
163 static struct kobj_attribute caps_start_attribute =
164         __ATTR(caps_start, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
165 static struct kobj_attribute caps_stop_attribute =
166         __ATTR(caps_stop, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
167 static struct kobj_attribute pitch_attribute =
168         __ATTR(pitch, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
169 static struct kobj_attribute punct_attribute =
170         __ATTR(punct, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
171 static struct kobj_attribute rate_attribute =
172         __ATTR(rate, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
173 static struct kobj_attribute voice_attribute =
174         __ATTR(voice, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
175 static struct kobj_attribute vol_attribute =
176         __ATTR(vol, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
177
178 static struct kobj_attribute delay_time_attribute =
179         __ATTR(delay_time, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
180 static struct kobj_attribute direct_attribute =
181         __ATTR(direct, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
182 static struct kobj_attribute full_time_attribute =
183         __ATTR(full_time, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
184 static struct kobj_attribute jiffy_delta_attribute =
185         __ATTR(jiffy_delta, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
186 static struct kobj_attribute trigger_time_attribute =
187         __ATTR(trigger_time, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
188
189 /*
190  * Create a group of attributes so that we can create and destroy them all
191  * at once.
192  */
193 static struct attribute *synth_attrs[] = {
194         &caps_start_attribute.attr,
195         &caps_stop_attribute.attr,
196         &pitch_attribute.attr,
197         &punct_attribute.attr,
198         &rate_attribute.attr,
199         &voice_attribute.attr,
200         &vol_attribute.attr,
201         &delay_time_attribute.attr,
202         &direct_attribute.attr,
203         &full_time_attribute.attr,
204         &jiffy_delta_attribute.attr,
205         &trigger_time_attribute.attr,
206         NULL,   /* need to NULL terminate the list of attributes */
207 };
208
209 static struct spk_synth synth_dec_pc = {
210         .name = "decpc",
211         .version = DRV_VERSION,
212         .long_name = "Dectalk PC",
213         .init = "[:pe -380]",
214         .procspeech = PROCSPEECH,
215         .delay = 500,
216         .trigger = 50,
217         .jiffies = 50,
218         .full = 1000,
219         .flags = SF_DEC,
220         .startup = SYNTH_START,
221         .checkval = SYNTH_CHECK,
222         .vars = vars,
223         .probe = synth_probe,
224         .release = dtpc_release,
225         .synth_immediate = synth_immediate,
226         .catch_up = do_catch_up,
227         .flush = synth_flush,
228         .is_alive = spk_synth_is_alive_nop,
229         .synth_adjust = NULL,
230         .read_buff_add = NULL,
231         .get_index = NULL,
232         .indexing = {
233                 .command = NULL,
234                 .lowindex = 0,
235                 .highindex = 0,
236                 .currindex = 0,
237         },
238         .attributes = {
239                 .attrs = synth_attrs,
240                 .name = "decpc",
241         },
242 };
243
244 static int dt_getstatus(void)
245 {
246         dt_stat = inb_p(speakup_info.port_tts) |
247                  (inb_p(speakup_info.port_tts + 1) << 8);
248         return dt_stat;
249 }
250
251 static void dt_sendcmd(u_int cmd)
252 {
253         outb_p(cmd & 0xFF, speakup_info.port_tts);
254         outb_p((cmd >> 8) & 0xFF, speakup_info.port_tts+1);
255 }
256
257 static int dt_waitbit(int bit)
258 {
259         int timeout = 100;
260
261         while (--timeout > 0) {
262                 if ((dt_getstatus() & bit) == bit)
263                         return 1;
264                 udelay(50);
265         }
266         return 0;
267 }
268
269 static int dt_wait_dma(void)
270 {
271         int timeout = 100, state = dma_state;
272
273         if (!dt_waitbit(STAT_dma_ready))
274                 return 0;
275         while (--timeout > 0) {
276                 if ((dt_getstatus()&STAT_dma_state) == state)
277                         return 1;
278                 udelay(50);
279         }
280         dma_state = dt_getstatus() & STAT_dma_state;
281         return 1;
282 }
283
284 static int dt_ctrl(u_int cmd)
285 {
286         int timeout = 10;
287
288         if (!dt_waitbit(STAT_cmd_ready))
289                 return -1;
290         outb_p(0, speakup_info.port_tts+2);
291         outb_p(0, speakup_info.port_tts+3);
292         dt_getstatus();
293         dt_sendcmd(CMD_control|cmd);
294         outb_p(0, speakup_info.port_tts+6);
295         while (dt_getstatus() & STAT_cmd_ready) {
296                 udelay(20);
297                 if (--timeout == 0)
298                         break;
299         }
300         dt_sendcmd(CMD_null);
301         return 0;
302 }
303
304 static void synth_flush(struct spk_synth *synth)
305 {
306         int timeout = 10;
307
308         if (is_flushing)
309                 return;
310         is_flushing = 4;
311         in_escape = 0;
312         while (dt_ctrl(CTRL_flush)) {
313                 if (--timeout == 0)
314                         break;
315 udelay(50);
316         }
317         for (timeout = 0; timeout < 10; timeout++) {
318                 if (dt_waitbit(STAT_dma_ready))
319                         break;
320 udelay(50);
321         }
322         outb_p(DMA_sync, speakup_info.port_tts+4);
323         outb_p(0, speakup_info.port_tts+4);
324         udelay(100);
325         for (timeout = 0; timeout < 10; timeout++) {
326                 if (!(dt_getstatus() & STAT_flushing))
327                         break;
328 udelay(50);
329         }
330         dma_state = dt_getstatus() & STAT_dma_state;
331         dma_state ^= STAT_dma_state;
332         is_flushing = 0;
333 }
334
335 static int dt_sendchar(char ch)
336 {
337         if (!dt_wait_dma())
338                 return -1;
339         if (!(dt_stat & STAT_rr_char))
340                 return -2;
341         outb_p(DMA_single_in, speakup_info.port_tts+4);
342         outb_p(ch, speakup_info.port_tts+4);
343         dma_state ^= STAT_dma_state;
344         return 0;
345 }
346
347 static int testkernel(void)
348 {
349         int status = 0;
350
351         if (dt_getstatus() == 0xffff) {
352                 status = -1;
353                 goto oops;
354         }
355         dt_sendcmd(CMD_sync);
356         if (!dt_waitbit(STAT_cmd_ready))
357                 status = -2;
358         else if (dt_stat&0x8000)
359                 return 0;
360         else if (dt_stat == 0x0dec)
361                 pr_warn("dec_pc at 0x%x, software not loaded\n",
362                                 speakup_info.port_tts);
363         status = -3;
364 oops:   synth_release_region(speakup_info.port_tts, SYNTH_IO_EXTENT);
365         speakup_info.port_tts = 0;
366         return status;
367 }
368
369 static void do_catch_up(struct spk_synth *synth)
370 {
371         u_char ch;
372         static u_char last;
373         unsigned long flags;
374         unsigned long jiff_max;
375         struct var_t *jiffy_delta;
376         struct var_t *delay_time;
377         int jiffy_delta_val;
378         int delay_time_val;
379
380         jiffy_delta = spk_get_var(JIFFY);
381         delay_time = spk_get_var(DELAY);
382         spin_lock_irqsave(&speakup_info.spinlock, flags);
383         jiffy_delta_val = jiffy_delta->u.n.value;
384         spin_unlock_irqrestore(&speakup_info.spinlock, flags);
385         jiff_max = jiffies + jiffy_delta_val;
386
387         while (!kthread_should_stop()) {
388                 spin_lock_irqsave(&speakup_info.spinlock, flags);
389                 if (speakup_info.flushing) {
390                         speakup_info.flushing = 0;
391                         spin_unlock_irqrestore(&speakup_info.spinlock, flags);
392                         synth->flush(synth);
393                         continue;
394                 }
395                 if (synth_buffer_empty()) {
396                         spin_unlock_irqrestore(&speakup_info.spinlock, flags);
397                         break;
398                 }
399                 ch = synth_buffer_peek();
400                 set_current_state(TASK_INTERRUPTIBLE);
401                 delay_time_val = delay_time->u.n.value;
402                 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
403                 if (ch == '\n')
404                         ch = 0x0D;
405                 if (dt_sendchar(ch)) {
406                         schedule_timeout(msecs_to_jiffies(delay_time_val));
407                         continue;
408                 }
409                 set_current_state(TASK_RUNNING);
410                 spin_lock_irqsave(&speakup_info.spinlock, flags);
411                 synth_buffer_getc();
412                 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
413                 if (ch == '[')
414                         in_escape = 1;
415                 else if (ch == ']')
416                         in_escape = 0;
417                 else if (ch <= SPACE) {
418                         if (!in_escape && strchr(",.!?;:", last))
419                                 dt_sendchar(PROCSPEECH);
420                         if (time_after_eq(jiffies, jiff_max)) {
421                                 if (!in_escape)
422                                         dt_sendchar(PROCSPEECH);
423                                 spin_lock_irqsave(&speakup_info.spinlock,
424                                                         flags);
425                                 jiffy_delta_val = jiffy_delta->u.n.value;
426                                 delay_time_val = delay_time->u.n.value;
427                                 spin_unlock_irqrestore(&speakup_info.spinlock,
428                                                         flags);
429                                 schedule_timeout(msecs_to_jiffies
430                                                  (delay_time_val));
431                                 jiff_max = jiffies + jiffy_delta_val;
432                         }
433                 }
434                 last = ch;
435                 ch = 0;
436         }
437         if (!in_escape)
438                 dt_sendchar(PROCSPEECH);
439 }
440
441 static const char *synth_immediate(struct spk_synth *synth, const char *buf)
442 {
443         u_char ch;
444
445         while ((ch = *buf)) {
446                 if (ch == '\n')
447                         ch = PROCSPEECH;
448                 if (dt_sendchar(ch))
449                         return buf;
450                 buf++;
451         }
452         return NULL;
453 }
454
455 static int synth_probe(struct spk_synth *synth)
456 {
457         int i = 0, failed = 0;
458
459         pr_info("Probing for %s.\n", synth->long_name);
460         for (i = 0; synth_portlist[i]; i++) {
461                 if (synth_request_region(synth_portlist[i], SYNTH_IO_EXTENT)) {
462                         pr_warn("request_region: failed with 0x%x, %d\n",
463                                 synth_portlist[i], SYNTH_IO_EXTENT);
464                         continue;
465                 }
466                 speakup_info.port_tts = synth_portlist[i];
467                 failed = testkernel();
468                 if (failed == 0)
469                         break;
470         }
471         if (failed) {
472                 pr_info("%s: not found\n", synth->long_name);
473                 return -ENODEV;
474         }
475         pr_info("%s: %03x-%03x, Driver Version %s,\n", synth->long_name,
476                 speakup_info.port_tts, speakup_info.port_tts + 7,
477                 synth->version);
478         synth->alive = 1;
479         return 0;
480 }
481
482 static void dtpc_release(void)
483 {
484         if (speakup_info.port_tts)
485                 synth_release_region(speakup_info.port_tts, SYNTH_IO_EXTENT);
486         speakup_info.port_tts = 0;
487 }
488
489 module_param_named(start, synth_dec_pc.startup, short, S_IRUGO);
490
491 MODULE_PARM_DESC(start, "Start the synthesizer once it is loaded.");
492
493 module_spk_synth(synth_dec_pc);
494
495 MODULE_AUTHOR("Kirk Reiser <kirk@braille.uwo.ca>");
496 MODULE_AUTHOR("David Borowski");
497 MODULE_DESCRIPTION("Speakup support for DECtalk PC synthesizers");
498 MODULE_LICENSE("GPL");
499 MODULE_VERSION(DRV_VERSION);