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