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