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>
6 * Copyright (C) 2003 Kirk Reiser.
8 * this code is specificly written as a driver for the speakup screenreview
9 * package and is not a general device driver.
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 */
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
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);
31 static struct miscdevice synth_device, synthu_device;
33 static int misc_registered;
35 static struct var_t vars[] = {
36 { CAPS_START, .u.s = {"\x01+3p" } },
37 { CAPS_STOP, .u.s = {"\x01-3p" } },
38 { RATE, .u.n = {"\x01%ds", 2, 0, 9, 0, 0, NULL } },
39 { PITCH, .u.n = {"\x01%dp", 5, 0, 9, 0, 0, NULL } },
40 { VOL, .u.n = {"\x01%dv", 5, 0, 9, 0, 0, NULL } },
41 { TONE, .u.n = {"\x01%dx", 1, 0, 2, 0, 0, NULL } },
42 { PUNCT, .u.n = {"\x01%db", 0, 0, 2, 0, 0, NULL } },
43 { VOICE, .u.n = {"\x01%do", 0, 0, 7, 0, 0, NULL } },
44 { FREQUENCY, .u.n = {"\x01%df", 5, 0, 9, 0, 0, NULL } },
45 { DIRECT, .u.n = {NULL, 0, 0, 1, 0, 0, NULL } },
49 /* These attributes will appear in /sys/accessibility/speakup/soft. */
51 static struct kobj_attribute caps_start_attribute =
52 __ATTR(caps_start, 0644, spk_var_show, spk_var_store);
53 static struct kobj_attribute caps_stop_attribute =
54 __ATTR(caps_stop, 0644, spk_var_show, spk_var_store);
55 static struct kobj_attribute freq_attribute =
56 __ATTR(freq, 0644, spk_var_show, spk_var_store);
57 static struct kobj_attribute pitch_attribute =
58 __ATTR(pitch, 0644, spk_var_show, spk_var_store);
59 static struct kobj_attribute punct_attribute =
60 __ATTR(punct, 0644, spk_var_show, spk_var_store);
61 static struct kobj_attribute rate_attribute =
62 __ATTR(rate, 0644, spk_var_show, spk_var_store);
63 static struct kobj_attribute tone_attribute =
64 __ATTR(tone, 0644, spk_var_show, spk_var_store);
65 static struct kobj_attribute voice_attribute =
66 __ATTR(voice, 0644, spk_var_show, spk_var_store);
67 static struct kobj_attribute vol_attribute =
68 __ATTR(vol, 0644, spk_var_show, spk_var_store);
71 * We should uncomment the following definition, when we agree on a
72 * method of passing a language designation to the software synthesizer.
73 * static struct kobj_attribute lang_attribute =
74 * __ATTR(lang, 0644, spk_var_show, spk_var_store);
77 static struct kobj_attribute delay_time_attribute =
78 __ATTR(delay_time, 0644, spk_var_show, spk_var_store);
79 static struct kobj_attribute direct_attribute =
80 __ATTR(direct, 0644, spk_var_show, spk_var_store);
81 static struct kobj_attribute full_time_attribute =
82 __ATTR(full_time, 0644, spk_var_show, spk_var_store);
83 static struct kobj_attribute jiffy_delta_attribute =
84 __ATTR(jiffy_delta, 0644, spk_var_show, spk_var_store);
85 static struct kobj_attribute trigger_time_attribute =
86 __ATTR(trigger_time, 0644, spk_var_show, spk_var_store);
89 * Create a group of attributes so that we can create and destroy them all
92 static struct attribute *synth_attrs[] = {
93 &caps_start_attribute.attr,
94 &caps_stop_attribute.attr,
96 /* &lang_attribute.attr, */
97 &pitch_attribute.attr,
98 &punct_attribute.attr,
100 &tone_attribute.attr,
101 &voice_attribute.attr,
103 &delay_time_attribute.attr,
104 &direct_attribute.attr,
105 &full_time_attribute.attr,
106 &jiffy_delta_attribute.attr,
107 &trigger_time_attribute.attr,
108 NULL, /* need to NULL terminate the list of attributes */
111 static struct spk_synth synth_soft = {
113 .version = DRV_VERSION,
114 .long_name = "software synth",
115 .init = "\01@\x01\x31y\n",
116 .procspeech = PROCSPEECH,
121 .startup = SYNTH_START,
122 .checkval = SYNTH_CHECK,
125 .probe = softsynth_probe,
126 .release = softsynth_release,
127 .synth_immediate = NULL,
130 .is_alive = softsynth_is_alive,
131 .synth_adjust = NULL,
132 .read_buff_add = NULL,
133 .get_index = get_index,
135 .command = "\x01%di",
141 .attrs = synth_attrs,
146 static char *get_initstring(void)
152 memset(buf, 0, sizeof(buf));
154 var = synth_soft.vars;
155 while (var->var_id != MAXVARS) {
156 if (var->var_id != CAPS_START && var->var_id != CAPS_STOP &&
157 var->var_id != DIRECT)
158 cp = cp + sprintf(cp, var->u.n.synth_fmt,
162 cp = cp + sprintf(cp, "\n");
166 static int softsynth_open(struct inode *inode, struct file *fp)
169 /*if ((fp->f_flags & O_ACCMODE) != O_RDONLY) */
171 spin_lock_irqsave(&speakup_info.spinlock, flags);
172 if (synth_soft.alive) {
173 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
176 synth_soft.alive = 1;
177 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
181 static int softsynth_close(struct inode *inode, struct file *fp)
185 spin_lock_irqsave(&speakup_info.spinlock, flags);
186 synth_soft.alive = 0;
188 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
189 /* Make sure we let applications go before leaving */
190 speakup_start_ttys();
194 static ssize_t softsynthx_read(struct file *fp, char __user *buf, size_t count,
195 loff_t *pos, int unicode)
205 spin_lock_irqsave(&speakup_info.spinlock, flags);
207 prepare_to_wait(&speakup_event, &wait, TASK_INTERRUPTIBLE);
209 synth_buffer_skip_nonlatin1();
210 if (!synth_buffer_empty() || speakup_info.flushing)
212 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
213 if (fp->f_flags & O_NONBLOCK) {
214 finish_wait(&speakup_event, &wait);
217 if (signal_pending(current)) {
218 finish_wait(&speakup_event, &wait);
222 spin_lock_irqsave(&speakup_info.spinlock, flags);
224 finish_wait(&speakup_event, &wait);
227 init = get_initstring();
229 /* Keep 3 bytes available for a 16bit UTF-8-encoded character */
230 while (chars_sent <= count - 3) {
231 if (speakup_info.flushing) {
232 speakup_info.flushing = 0;
234 } else if (init[init_pos]) {
235 ch = init[init_pos++];
238 synth_buffer_skip_nonlatin1();
239 if (synth_buffer_empty())
241 ch = synth_buffer_getc();
243 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
245 if ((!unicode && ch < 0x100) || (unicode && ch < 0x80)) {
248 if (copy_to_user(cp, &c, 1))
253 } else if (unicode && ch < 0x800) {
259 if (copy_to_user(cp, s, sizeof(s)))
262 chars_sent += sizeof(s);
264 } else if (unicode) {
267 0x80 | ((ch >> 6) & 0x3f),
271 if (copy_to_user(cp, s, sizeof(s)))
274 chars_sent += sizeof(s);
278 spin_lock_irqsave(&speakup_info.spinlock, flags);
281 empty = synth_buffer_empty();
282 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
284 speakup_start_ttys();
290 static ssize_t softsynth_read(struct file *fp, char __user *buf, size_t count,
293 return softsynthx_read(fp, buf, count, pos, 0);
296 static ssize_t softsynthu_read(struct file *fp, char __user *buf, size_t count,
299 return softsynthx_read(fp, buf, count, pos, 1);
302 static int last_index;
304 static ssize_t softsynth_write(struct file *fp, const char __user *buf,
305 size_t count, loff_t *pos)
307 unsigned long supplied_index = 0;
310 converted = kstrtoul_from_user(buf, count, 0, &supplied_index);
315 last_index = supplied_index;
319 static __poll_t softsynth_poll(struct file *fp, struct poll_table_struct *wait)
324 poll_wait(fp, &speakup_event, wait);
326 spin_lock_irqsave(&speakup_info.spinlock, flags);
327 if (!synth_buffer_empty() || speakup_info.flushing)
328 ret = POLLIN | POLLRDNORM;
329 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
333 static unsigned char get_index(struct spk_synth *synth)
342 static const struct file_operations softsynth_fops = {
343 .owner = THIS_MODULE,
344 .poll = softsynth_poll,
345 .read = softsynth_read,
346 .write = softsynth_write,
347 .open = softsynth_open,
348 .release = softsynth_close,
351 static const struct file_operations softsynthu_fops = {
352 .owner = THIS_MODULE,
353 .poll = softsynth_poll,
354 .read = softsynthu_read,
355 .write = softsynth_write,
356 .open = softsynth_open,
357 .release = softsynth_close,
360 static int softsynth_probe(struct spk_synth *synth)
362 if (misc_registered != 0)
364 memset(&synth_device, 0, sizeof(synth_device));
365 synth_device.minor = SOFTSYNTH_MINOR;
366 synth_device.name = "softsynth";
367 synth_device.fops = &softsynth_fops;
368 if (misc_register(&synth_device)) {
369 pr_warn("Couldn't initialize miscdevice /dev/softsynth.\n");
373 memset(&synthu_device, 0, sizeof(synthu_device));
374 synthu_device.minor = SOFTSYNTHU_MINOR;
375 synthu_device.name = "softsynthu";
376 synthu_device.fops = &softsynthu_fops;
377 if (misc_register(&synthu_device)) {
378 pr_warn("Couldn't initialize miscdevice /dev/softsynth.\n");
383 pr_info("initialized device: /dev/softsynth, node (MAJOR 10, MINOR 26)\n");
384 pr_info("initialized device: /dev/softsynthu, node (MAJOR 10, MINOR 27)\n");
388 static void softsynth_release(void)
390 misc_deregister(&synth_device);
391 misc_deregister(&synthu_device);
393 pr_info("unregistered /dev/softsynth\n");
394 pr_info("unregistered /dev/softsynthu\n");
397 static int softsynth_is_alive(struct spk_synth *synth)
399 if (synth_soft.alive)
404 module_param_named(start, synth_soft.startup, short, 0444);
406 MODULE_PARM_DESC(start, "Start the synthesizer once it is loaded.");
408 module_spk_synth(synth_soft);
410 MODULE_AUTHOR("Kirk Reiser <kirk@braille.uwo.ca>");
411 MODULE_DESCRIPTION("Speakup userspace software synthesizer support");
412 MODULE_LICENSE("GPL");
413 MODULE_VERSION(DRV_VERSION);