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