Linux-libre 4.15.7-gnu
[librecmc/linux-libre.git] / drivers / staging / speakup / speakup_soft.c
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/signal.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 SOFTSYNTHU_MINOR 27 /* might as well give it one more than /dev/synth */
33 #define PROCSPEECH 0x0d
34 #define CLEAR_SYNTH 0x18
35
36 static int softsynth_probe(struct spk_synth *synth);
37 static void softsynth_release(void);
38 static int softsynth_is_alive(struct spk_synth *synth);
39 static unsigned char get_index(struct spk_synth *synth);
40
41 static struct miscdevice synth_device, synthu_device;
42 static int init_pos;
43 static int misc_registered;
44
45 static struct var_t vars[] = {
46         { CAPS_START, .u.s = {"\x01+3p" } },
47         { CAPS_STOP, .u.s = {"\x01-3p" } },
48         { RATE, .u.n = {"\x01%ds", 2, 0, 9, 0, 0, NULL } },
49         { PITCH, .u.n = {"\x01%dp", 5, 0, 9, 0, 0, NULL } },
50         { VOL, .u.n = {"\x01%dv", 5, 0, 9, 0, 0, NULL } },
51         { TONE, .u.n = {"\x01%dx", 1, 0, 2, 0, 0, NULL } },
52         { PUNCT, .u.n = {"\x01%db", 0, 0, 2, 0, 0, NULL } },
53         { VOICE, .u.n = {"\x01%do", 0, 0, 7, 0, 0, NULL } },
54         { FREQUENCY, .u.n = {"\x01%df", 5, 0, 9, 0, 0, NULL } },
55         { DIRECT, .u.n = {NULL, 0, 0, 1, 0, 0, NULL } },
56         V_LAST_VAR
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, 0644, spk_var_show, spk_var_store);
63 static struct kobj_attribute caps_stop_attribute =
64         __ATTR(caps_stop, 0644, spk_var_show, spk_var_store);
65 static struct kobj_attribute freq_attribute =
66         __ATTR(freq, 0644, spk_var_show, spk_var_store);
67 static struct kobj_attribute pitch_attribute =
68         __ATTR(pitch, 0644, spk_var_show, spk_var_store);
69 static struct kobj_attribute punct_attribute =
70         __ATTR(punct, 0644, spk_var_show, spk_var_store);
71 static struct kobj_attribute rate_attribute =
72         __ATTR(rate, 0644, spk_var_show, spk_var_store);
73 static struct kobj_attribute tone_attribute =
74         __ATTR(tone, 0644, spk_var_show, spk_var_store);
75 static struct kobj_attribute voice_attribute =
76         __ATTR(voice, 0644, spk_var_show, spk_var_store);
77 static struct kobj_attribute vol_attribute =
78         __ATTR(vol, 0644, 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, 0644, spk_var_show, spk_var_store);
85  */
86
87 static struct kobj_attribute delay_time_attribute =
88         __ATTR(delay_time, 0644, spk_var_show, spk_var_store);
89 static struct kobj_attribute direct_attribute =
90         __ATTR(direct, 0644, spk_var_show, spk_var_store);
91 static struct kobj_attribute full_time_attribute =
92         __ATTR(full_time, 0644, spk_var_show, spk_var_store);
93 static struct kobj_attribute jiffy_delta_attribute =
94         __ATTR(jiffy_delta, 0644, spk_var_show, spk_var_store);
95 static struct kobj_attribute trigger_time_attribute =
96         __ATTR(trigger_time, 0644, 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         .io_ops = NULL,
135         .probe = softsynth_probe,
136         .release = softsynth_release,
137         .synth_immediate = NULL,
138         .catch_up = NULL,
139         .flush = NULL,
140         .is_alive = softsynth_is_alive,
141         .synth_adjust = NULL,
142         .read_buff_add = NULL,
143         .get_index = get_index,
144         .indexing = {
145                 .command = "\x01%di",
146                 .lowindex = 1,
147                 .highindex = 5,
148                 .currindex = 1,
149         },
150         .attributes = {
151                 .attrs = synth_attrs,
152                 .name = "soft",
153         },
154 };
155
156 static char *get_initstring(void)
157 {
158         static char buf[40];
159         char *cp;
160         struct var_t *var;
161
162         memset(buf, 0, sizeof(buf));
163         cp = buf;
164         var = synth_soft.vars;
165         while (var->var_id != MAXVARS) {
166                 if (var->var_id != CAPS_START && var->var_id != CAPS_STOP &&
167                     var->var_id != DIRECT)
168                         cp = cp + sprintf(cp, var->u.n.synth_fmt,
169                                           var->u.n.value);
170                 var++;
171         }
172         cp = cp + sprintf(cp, "\n");
173         return buf;
174 }
175
176 static int softsynth_open(struct inode *inode, struct file *fp)
177 {
178         unsigned long flags;
179         /*if ((fp->f_flags & O_ACCMODE) != O_RDONLY) */
180         /*      return -EPERM; */
181         spin_lock_irqsave(&speakup_info.spinlock, flags);
182         if (synth_soft.alive) {
183                 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
184                 return -EBUSY;
185         }
186         synth_soft.alive = 1;
187         spin_unlock_irqrestore(&speakup_info.spinlock, flags);
188         return 0;
189 }
190
191 static int softsynth_close(struct inode *inode, struct file *fp)
192 {
193         unsigned long flags;
194
195         spin_lock_irqsave(&speakup_info.spinlock, flags);
196         synth_soft.alive = 0;
197         init_pos = 0;
198         spin_unlock_irqrestore(&speakup_info.spinlock, flags);
199         /* Make sure we let applications go before leaving */
200         speakup_start_ttys();
201         return 0;
202 }
203
204 static ssize_t softsynthx_read(struct file *fp, char __user *buf, size_t count,
205                                loff_t *pos, int unicode)
206 {
207         int chars_sent = 0;
208         char __user *cp;
209         char *init;
210         u16 ch;
211         int empty;
212         unsigned long flags;
213         DEFINE_WAIT(wait);
214
215         spin_lock_irqsave(&speakup_info.spinlock, flags);
216         while (1) {
217                 prepare_to_wait(&speakup_event, &wait, TASK_INTERRUPTIBLE);
218                 if (!unicode)
219                         synth_buffer_skip_nonlatin1();
220                 if (!synth_buffer_empty() || speakup_info.flushing)
221                         break;
222                 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
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();
232                 spin_lock_irqsave(&speakup_info.spinlock, flags);
233         }
234         finish_wait(&speakup_event, &wait);
235
236         cp = buf;
237         init = get_initstring();
238
239         /* Keep 3 bytes available for a 16bit UTF-8-encoded character */
240         while (chars_sent <= count - 3) {
241                 if (speakup_info.flushing) {
242                         speakup_info.flushing = 0;
243                         ch = '\x18';
244                 } else if (init[init_pos]) {
245                         ch = init[init_pos++];
246                 } else {
247                         if (!unicode)
248                                 synth_buffer_skip_nonlatin1();
249                         if (synth_buffer_empty())
250                                 break;
251                         ch = synth_buffer_getc();
252                 }
253                 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
254
255                 if ((!unicode && ch < 0x100) || (unicode && ch < 0x80)) {
256                         u_char c = ch;
257
258                         if (copy_to_user(cp, &c, 1))
259                                 return -EFAULT;
260
261                         chars_sent++;
262                         cp++;
263                 } else if (unicode && ch < 0x800) {
264                         u_char s[2] = {
265                                 0xc0 | (ch >> 6),
266                                 0x80 | (ch & 0x3f)
267                         };
268
269                         if (copy_to_user(cp, s, sizeof(s)))
270                                 return -EFAULT;
271
272                         chars_sent += sizeof(s);
273                         cp += sizeof(s);
274                 } else if (unicode) {
275                         u_char s[3] = {
276                                 0xe0 | (ch >> 12),
277                                 0x80 | ((ch >> 6) & 0x3f),
278                                 0x80 | (ch & 0x3f)
279                         };
280
281                         if (copy_to_user(cp, s, sizeof(s)))
282                                 return -EFAULT;
283
284                         chars_sent += sizeof(s);
285                         cp += sizeof(s);
286                 }
287
288                 spin_lock_irqsave(&speakup_info.spinlock, flags);
289         }
290         *pos += chars_sent;
291         empty = synth_buffer_empty();
292         spin_unlock_irqrestore(&speakup_info.spinlock, flags);
293         if (empty) {
294                 speakup_start_ttys();
295                 *pos = 0;
296         }
297         return chars_sent;
298 }
299
300 static ssize_t softsynth_read(struct file *fp, char __user *buf, size_t count,
301                               loff_t *pos)
302 {
303         return softsynthx_read(fp, buf, count, pos, 0);
304 }
305
306 static ssize_t softsynthu_read(struct file *fp, char __user *buf, size_t count,
307                                loff_t *pos)
308 {
309         return softsynthx_read(fp, buf, count, pos, 1);
310 }
311
312 static int last_index;
313
314 static ssize_t softsynth_write(struct file *fp, const char __user *buf,
315                                size_t count, loff_t *pos)
316 {
317         unsigned long supplied_index = 0;
318         int converted;
319
320         converted = kstrtoul_from_user(buf, count, 0, &supplied_index);
321
322         if (converted < 0)
323                 return converted;
324
325         last_index = supplied_index;
326         return count;
327 }
328
329 static unsigned int softsynth_poll(struct file *fp, struct poll_table_struct *wait)
330 {
331         unsigned long flags;
332         int ret = 0;
333
334         poll_wait(fp, &speakup_event, wait);
335
336         spin_lock_irqsave(&speakup_info.spinlock, flags);
337         if (!synth_buffer_empty() || speakup_info.flushing)
338                 ret = POLLIN | POLLRDNORM;
339         spin_unlock_irqrestore(&speakup_info.spinlock, flags);
340         return ret;
341 }
342
343 static unsigned char get_index(struct spk_synth *synth)
344 {
345         int rv;
346
347         rv = last_index;
348         last_index = 0;
349         return rv;
350 }
351
352 static const struct file_operations softsynth_fops = {
353         .owner = THIS_MODULE,
354         .poll = softsynth_poll,
355         .read = softsynth_read,
356         .write = softsynth_write,
357         .open = softsynth_open,
358         .release = softsynth_close,
359 };
360
361 static const struct file_operations softsynthu_fops = {
362         .owner = THIS_MODULE,
363         .poll = softsynth_poll,
364         .read = softsynthu_read,
365         .write = softsynth_write,
366         .open = softsynth_open,
367         .release = softsynth_close,
368 };
369
370 static int softsynth_probe(struct spk_synth *synth)
371 {
372         if (misc_registered != 0)
373                 return 0;
374         memset(&synth_device, 0, sizeof(synth_device));
375         synth_device.minor = SOFTSYNTH_MINOR;
376         synth_device.name = "softsynth";
377         synth_device.fops = &softsynth_fops;
378         if (misc_register(&synth_device)) {
379                 pr_warn("Couldn't initialize miscdevice /dev/softsynth.\n");
380                 return -ENODEV;
381         }
382
383         memset(&synthu_device, 0, sizeof(synthu_device));
384         synthu_device.minor = SOFTSYNTHU_MINOR;
385         synthu_device.name = "softsynthu";
386         synthu_device.fops = &softsynthu_fops;
387         if (misc_register(&synthu_device)) {
388                 pr_warn("Couldn't initialize miscdevice /dev/softsynth.\n");
389                 return -ENODEV;
390         }
391
392         misc_registered = 1;
393         pr_info("initialized device: /dev/softsynth, node (MAJOR 10, MINOR 26)\n");
394         pr_info("initialized device: /dev/softsynthu, node (MAJOR 10, MINOR 27)\n");
395         return 0;
396 }
397
398 static void softsynth_release(void)
399 {
400         misc_deregister(&synth_device);
401         misc_deregister(&synthu_device);
402         misc_registered = 0;
403         pr_info("unregistered /dev/softsynth\n");
404         pr_info("unregistered /dev/softsynthu\n");
405 }
406
407 static int softsynth_is_alive(struct spk_synth *synth)
408 {
409         if (synth_soft.alive)
410                 return 1;
411         return 0;
412 }
413
414 module_param_named(start, synth_soft.startup, short, 0444);
415
416 MODULE_PARM_DESC(start, "Start the synthesizer once it is loaded.");
417
418 module_spk_synth(synth_soft);
419
420 MODULE_AUTHOR("Kirk Reiser <kirk@braille.uwo.ca>");
421 MODULE_DESCRIPTION("Speakup userspace software synthesizer support");
422 MODULE_LICENSE("GPL");
423 MODULE_VERSION(DRV_VERSION);