1 /* speakup_soft.c - speakup driver to register and make available
2 * a user space device for software synthesizers. written by: Kirk
3 * Reiser <kirk@braille.uwo.ca>
4 *
5 * Copyright (C) 2003 Kirk Reiser.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 *
18 * this code is specificly written as a driver for the speakup screenreview
19 * package and is not a general device driver.
20 */
21
22 #include <linux/unistd.h>
23 #include <linux/miscdevice.h> /* for misc_register, and SYNTH_MINOR */
24 #include <linux/poll.h> /* for poll_wait() */
25 #include <linux/sched.h> /* schedule(), signal_pending(), TASK_INTERRUPTIBLE */
26
27 #include "spk_priv.h"
28 #include "speakup.h"
29
30 #define DRV_VERSION "2.6"
31 #define SOFTSYNTH_MINOR 26 /* might as well give it one more than /dev/synth */
32 #define PROCSPEECH 0x0d
33 #define CLEAR_SYNTH 0x18
34
35 static int softsynth_probe(struct spk_synth *synth);
36 static void softsynth_release(void);
37 static int softsynth_is_alive(struct spk_synth *synth);
38 static unsigned char get_index(void);
39
40 static struct miscdevice synth_device;
41 static int init_pos;
42 static int misc_registered;
43
44 static struct var_t vars[] = {
45 { CAPS_START, .u.s = {"\x01+3p" } },
46 { CAPS_STOP, .u.s = {"\x01-3p" } },
47 { RATE, .u.n = {"\x01%ds", 2, 0, 9, 0, 0, NULL } },
48 { PITCH, .u.n = {"\x01%dp", 5, 0, 9, 0, 0, NULL } },
49 { VOL, .u.n = {"\x01%dv", 5, 0, 9, 0, 0, NULL } },
50 { TONE, .u.n = {"\x01%dx", 1, 0, 2, 0, 0, NULL } },
51 { PUNCT, .u.n = {"\x01%db", 0, 0, 2, 0, 0, NULL } },
52 { VOICE, .u.n = {"\x01%do", 0, 0, 7, 0, 0, NULL } },
53 { FREQUENCY, .u.n = {"\x01%df", 5, 0, 9, 0, 0, NULL } },
54 { DIRECT, .u.n = {NULL, 0, 0, 1, 0, 0, NULL } },
55 V_LAST_VAR
56 };
57
58 /*
59 * These attributes will appear in /sys/accessibility/speakup/soft.
60 */
61 static struct kobj_attribute caps_start_attribute =
62 __ATTR(caps_start, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
63 static struct kobj_attribute caps_stop_attribute =
64 __ATTR(caps_stop, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
65 static struct kobj_attribute freq_attribute =
66 __ATTR(freq, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
67 static struct kobj_attribute pitch_attribute =
68 __ATTR(pitch, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
69 static struct kobj_attribute punct_attribute =
70 __ATTR(punct, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
71 static struct kobj_attribute rate_attribute =
72 __ATTR(rate, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
73 static struct kobj_attribute tone_attribute =
74 __ATTR(tone, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
75 static struct kobj_attribute voice_attribute =
76 __ATTR(voice, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
77 static struct kobj_attribute vol_attribute =
78 __ATTR(vol, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
79
80 /*
81 * We should uncomment the following definition, when we agree on a
82 * method of passing a language designation to the software synthesizer.
83 * static struct kobj_attribute lang_attribute =
84 * __ATTR(lang, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
85 */
86
87 static struct kobj_attribute delay_time_attribute =
88 __ATTR(delay_time, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
89 static struct kobj_attribute direct_attribute =
90 __ATTR(direct, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
91 static struct kobj_attribute full_time_attribute =
92 __ATTR(full_time, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
93 static struct kobj_attribute jiffy_delta_attribute =
94 __ATTR(jiffy_delta, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
95 static struct kobj_attribute trigger_time_attribute =
96 __ATTR(trigger_time, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
97
98 /*
99 * Create a group of attributes so that we can create and destroy them all
100 * at once.
101 */
102 static struct attribute *synth_attrs[] = {
103 &caps_start_attribute.attr,
104 &caps_stop_attribute.attr,
105 &freq_attribute.attr,
106 /* &lang_attribute.attr, */
107 &pitch_attribute.attr,
108 &punct_attribute.attr,
109 &rate_attribute.attr,
110 &tone_attribute.attr,
111 &voice_attribute.attr,
112 &vol_attribute.attr,
113 &delay_time_attribute.attr,
114 &direct_attribute.attr,
115 &full_time_attribute.attr,
116 &jiffy_delta_attribute.attr,
117 &trigger_time_attribute.attr,
118 NULL, /* need to NULL terminate the list of attributes */
119 };
120
121 static struct spk_synth synth_soft = {
122 .name = "soft",
123 .version = DRV_VERSION,
124 .long_name = "software synth",
125 .init = "\01@\x01\x31y\n",
126 .procspeech = PROCSPEECH,
127 .delay = 0,
128 .trigger = 0,
129 .jiffies = 0,
130 .full = 0,
131 .startup = SYNTH_START,
132 .checkval = SYNTH_CHECK,
133 .vars = vars,
134 .probe = softsynth_probe,
135 .release = softsynth_release,
136 .synth_immediate = NULL,
137 .catch_up = NULL,
138 .flush = NULL,
139 .is_alive = softsynth_is_alive,
140 .synth_adjust = NULL,
141 .read_buff_add = NULL,
142 .get_index = get_index,
143 .indexing = {
144 .command = "\x01%di",
145 .lowindex = 1,
146 .highindex = 5,
147 .currindex = 1,
148 },
149 .attributes = {
150 .attrs = synth_attrs,
151 .name = "soft",
152 },
153 };
154
get_initstring(void)155 static char *get_initstring(void)
156 {
157 static char buf[40];
158 char *cp;
159 struct var_t *var;
160
161 memset(buf, 0, sizeof(buf));
162 cp = buf;
163 var = synth_soft.vars;
164 while (var->var_id != MAXVARS) {
165 if (var->var_id != CAPS_START && var->var_id != CAPS_STOP
166 && var->var_id != DIRECT)
167 cp = cp + sprintf(cp, var->u.n.synth_fmt,
168 var->u.n.value);
169 var++;
170 }
171 cp = cp + sprintf(cp, "\n");
172 return buf;
173 }
174
softsynth_open(struct inode * inode,struct file * fp)175 static int softsynth_open(struct inode *inode, struct file *fp)
176 {
177 unsigned long flags;
178 /*if ((fp->f_flags & O_ACCMODE) != O_RDONLY) */
179 /* return -EPERM; */
180 spin_lock_irqsave(&speakup_info.spinlock, flags);
181 if (synth_soft.alive) {
182 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
183 return -EBUSY;
184 }
185 synth_soft.alive = 1;
186 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
187 return 0;
188 }
189
softsynth_close(struct inode * inode,struct file * fp)190 static int softsynth_close(struct inode *inode, struct file *fp)
191 {
192 unsigned long flags;
193
194 spin_lock_irqsave(&speakup_info.spinlock, flags);
195 synth_soft.alive = 0;
196 init_pos = 0;
197 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
198 /* Make sure we let applications go before leaving */
199 speakup_start_ttys();
200 return 0;
201 }
202
softsynth_read(struct file * fp,char __user * buf,size_t count,loff_t * pos)203 static ssize_t softsynth_read(struct file *fp, char __user *buf, size_t count,
204 loff_t *pos)
205 {
206 int chars_sent = 0;
207 char __user *cp;
208 char *init;
209 char ch;
210 int empty;
211 unsigned long flags;
212 DEFINE_WAIT(wait);
213
214 spin_lock_irqsave(&speakup_info.spinlock, flags);
215 while (1) {
216 prepare_to_wait(&speakup_event, &wait, TASK_INTERRUPTIBLE);
217 if (!synth_buffer_empty() || speakup_info.flushing)
218 break;
219 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
220 if (fp->f_flags & O_NONBLOCK) {
221 finish_wait(&speakup_event, &wait);
222 return -EAGAIN;
223 }
224 if (signal_pending(current)) {
225 finish_wait(&speakup_event, &wait);
226 return -ERESTARTSYS;
227 }
228 schedule();
229 spin_lock_irqsave(&speakup_info.spinlock, flags);
230 }
231 finish_wait(&speakup_event, &wait);
232
233 cp = buf;
234 init = get_initstring();
235 while (chars_sent < count) {
236 if (speakup_info.flushing) {
237 speakup_info.flushing = 0;
238 ch = '\x18';
239 } else if (synth_buffer_empty()) {
240 break;
241 } else if (init[init_pos]) {
242 ch = init[init_pos++];
243 } else {
244 ch = synth_buffer_getc();
245 }
246 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
247 if (copy_to_user(cp, &ch, 1))
248 return -EFAULT;
249 spin_lock_irqsave(&speakup_info.spinlock, flags);
250 chars_sent++;
251 cp++;
252 }
253 *pos += chars_sent;
254 empty = synth_buffer_empty();
255 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
256 if (empty) {
257 speakup_start_ttys();
258 *pos = 0;
259 }
260 return chars_sent;
261 }
262
263 static int last_index;
264
softsynth_write(struct file * fp,const char __user * buf,size_t count,loff_t * pos)265 static ssize_t softsynth_write(struct file *fp, const char __user *buf,
266 size_t count, loff_t *pos)
267 {
268 unsigned long supplied_index = 0;
269 int converted;
270
271 converted = kstrtoul_from_user(buf, count, 0, &supplied_index);
272
273 if (converted < 0)
274 return converted;
275
276 last_index = supplied_index;
277 return count;
278 }
279
softsynth_poll(struct file * fp,struct poll_table_struct * wait)280 static unsigned int softsynth_poll(struct file *fp,
281 struct poll_table_struct *wait)
282 {
283 unsigned long flags;
284 int ret = 0;
285
286 poll_wait(fp, &speakup_event, wait);
287
288 spin_lock_irqsave(&speakup_info.spinlock, flags);
289 if (!synth_buffer_empty() || speakup_info.flushing)
290 ret = POLLIN | POLLRDNORM;
291 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
292 return ret;
293 }
294
get_index(void)295 static unsigned char get_index(void)
296 {
297 int rv;
298
299 rv = last_index;
300 last_index = 0;
301 return rv;
302 }
303
304 static const struct file_operations softsynth_fops = {
305 .owner = THIS_MODULE,
306 .poll = softsynth_poll,
307 .read = softsynth_read,
308 .write = softsynth_write,
309 .open = softsynth_open,
310 .release = softsynth_close,
311 };
312
313
softsynth_probe(struct spk_synth * synth)314 static int softsynth_probe(struct spk_synth *synth)
315 {
316
317 if (misc_registered != 0)
318 return 0;
319 memset(&synth_device, 0, sizeof(synth_device));
320 synth_device.minor = SOFTSYNTH_MINOR;
321 synth_device.name = "softsynth";
322 synth_device.fops = &softsynth_fops;
323 if (misc_register(&synth_device)) {
324 pr_warn("Couldn't initialize miscdevice /dev/softsynth.\n");
325 return -ENODEV;
326 }
327
328 misc_registered = 1;
329 pr_info("initialized device: /dev/softsynth, node (MAJOR 10, MINOR 26)\n");
330 return 0;
331 }
332
softsynth_release(void)333 static void softsynth_release(void)
334 {
335 misc_deregister(&synth_device);
336 misc_registered = 0;
337 pr_info("unregistered /dev/softsynth\n");
338 }
339
softsynth_is_alive(struct spk_synth * synth)340 static int softsynth_is_alive(struct spk_synth *synth)
341 {
342 if (synth_soft.alive)
343 return 1;
344 return 0;
345 }
346
347 module_param_named(start, synth_soft.startup, short, S_IRUGO);
348
349 MODULE_PARM_DESC(start, "Start the synthesizer once it is loaded.");
350
351 module_spk_synth(synth_soft);
352
353 MODULE_AUTHOR("Kirk Reiser <kirk@braille.uwo.ca>");
354 MODULE_DESCRIPTION("Speakup userspace software synthesizer support");
355 MODULE_LICENSE("GPL");
356 MODULE_VERSION(DRV_VERSION);
357