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