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