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
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() */
16 /* schedule(), signal_pending(), TASK_INTERRUPTIBLE */
17 #include <linux/sched/signal.h>
22 #define DRV_VERSION "2.6"
23 #define SOFTSYNTH_MINOR 26 /* might as well give it one more than /dev/synth */
24 #define SOFTSYNTHU_MINOR 27 /* might as well give it one more than /dev/synth */
25 #define PROCSPEECH 0x0d
26 #define CLEAR_SYNTH 0x18
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);
31 static unsigned char get_index(struct spk_synth *synth);
33 static struct miscdevice synth_device, synthu_device;
35 static int misc_registered;
37 static struct var_t vars[] = {
38 { CAPS_START, .u.s = {"\x01+3p" } },
39 { CAPS_STOP, .u.s = {"\x01-3p" } },
40 { PAUSE, .u.n = {"\x01P" } },
41 { RATE, .u.n = {"\x01%ds", 2, 0, 9, 0, 0, NULL } },
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 } },
52 /* These attributes will appear in /sys/accessibility/speakup/soft. */
54 static struct kobj_attribute caps_start_attribute =
55 __ATTR(caps_start, 0644, spk_var_show, spk_var_store);
56 static struct kobj_attribute caps_stop_attribute =
57 __ATTR(caps_stop, 0644, spk_var_show, spk_var_store);
58 static struct kobj_attribute freq_attribute =
59 __ATTR(freq, 0644, spk_var_show, spk_var_store);
60 static struct kobj_attribute pitch_attribute =
61 __ATTR(pitch, 0644, spk_var_show, spk_var_store);
62 static struct kobj_attribute punct_attribute =
63 __ATTR(punct, 0644, spk_var_show, spk_var_store);
64 static struct kobj_attribute rate_attribute =
65 __ATTR(rate, 0644, spk_var_show, spk_var_store);
66 static struct kobj_attribute tone_attribute =
67 __ATTR(tone, 0644, spk_var_show, spk_var_store);
68 static struct kobj_attribute voice_attribute =
69 __ATTR(voice, 0644, spk_var_show, spk_var_store);
70 static struct kobj_attribute vol_attribute =
71 __ATTR(vol, 0644, spk_var_show, spk_var_store);
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 =
77 * __ATTR(lang, 0644, spk_var_show, spk_var_store);
80 static struct kobj_attribute delay_time_attribute =
81 __ATTR(delay_time, 0644, spk_var_show, spk_var_store);
82 static struct kobj_attribute direct_attribute =
83 __ATTR(direct, 0644, spk_var_show, spk_var_store);
84 static struct kobj_attribute full_time_attribute =
85 __ATTR(full_time, 0644, spk_var_show, spk_var_store);
86 static struct kobj_attribute jiffy_delta_attribute =
87 __ATTR(jiffy_delta, 0644, spk_var_show, spk_var_store);
88 static struct kobj_attribute trigger_time_attribute =
89 __ATTR(trigger_time, 0644, spk_var_show, spk_var_store);
92 * Create a group of attributes so that we can create and destroy them all
95 static struct attribute *synth_attrs[] = {
96 &caps_start_attribute.attr,
97 &caps_stop_attribute.attr,
99 /* &lang_attribute.attr, */
100 &pitch_attribute.attr,
101 &punct_attribute.attr,
102 &rate_attribute.attr,
103 &tone_attribute.attr,
104 &voice_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 */
114 static struct spk_synth synth_soft = {
116 .version = DRV_VERSION,
117 .long_name = "software synth",
118 .init = "\01@\x01\x31y\n",
119 .procspeech = PROCSPEECH,
124 .startup = SYNTH_START,
125 .checkval = SYNTH_CHECK,
128 .probe = softsynth_probe,
129 .release = softsynth_release,
130 .synth_immediate = NULL,
133 .is_alive = softsynth_is_alive,
134 .synth_adjust = NULL,
135 .read_buff_add = NULL,
136 .get_index = get_index,
138 .command = "\x01%di",
144 .attrs = synth_attrs,
149 static char *get_initstring(void)
155 memset(buf, 0, sizeof(buf));
157 var = synth_soft.vars;
158 while (var->var_id != MAXVARS) {
159 if (var->var_id != CAPS_START && var->var_id != CAPS_STOP &&
160 var->var_id != PAUSE && var->var_id != DIRECT)
161 cp = cp + sprintf(cp, var->u.n.synth_fmt,
165 cp = cp + sprintf(cp, "\n");
169 static int softsynth_open(struct inode *inode, struct file *fp)
172 /*if ((fp->f_flags & O_ACCMODE) != O_RDONLY) */
174 spin_lock_irqsave(&speakup_info.spinlock, flags);
175 if (synth_soft.alive) {
176 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
179 synth_soft.alive = 1;
180 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
184 static int softsynth_close(struct inode *inode, struct file *fp)
188 spin_lock_irqsave(&speakup_info.spinlock, flags);
189 synth_soft.alive = 0;
191 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
192 /* Make sure we let applications go before leaving */
193 speakup_start_ttys();
197 static ssize_t softsynthx_read(struct file *fp, char __user *buf, size_t count,
198 loff_t *pos, int unicode)
203 size_t bytes_per_ch = unicode ? 3 : 1;
209 if (count < bytes_per_ch)
212 spin_lock_irqsave(&speakup_info.spinlock, flags);
214 prepare_to_wait(&speakup_event, &wait, TASK_INTERRUPTIBLE);
216 synth_buffer_skip_nonlatin1();
217 if (!synth_buffer_empty() || speakup_info.flushing)
219 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
220 if (fp->f_flags & O_NONBLOCK) {
221 finish_wait(&speakup_event, &wait);
224 if (signal_pending(current)) {
225 finish_wait(&speakup_event, &wait);
229 spin_lock_irqsave(&speakup_info.spinlock, flags);
231 finish_wait(&speakup_event, &wait);
234 init = get_initstring();
236 /* Keep 3 bytes available for a 16bit UTF-8-encoded character */
237 while (chars_sent <= count - bytes_per_ch) {
238 if (speakup_info.flushing) {
239 speakup_info.flushing = 0;
241 } else if (init[init_pos]) {
242 ch = init[init_pos++];
245 synth_buffer_skip_nonlatin1();
246 if (synth_buffer_empty())
248 ch = synth_buffer_getc();
250 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
252 if ((!unicode && ch < 0x100) || (unicode && ch < 0x80)) {
255 if (copy_to_user(cp, &c, 1))
260 } else if (unicode && ch < 0x800) {
266 if (copy_to_user(cp, s, sizeof(s)))
269 chars_sent += sizeof(s);
271 } else if (unicode) {
274 0x80 | ((ch >> 6) & 0x3f),
278 if (copy_to_user(cp, s, sizeof(s)))
281 chars_sent += sizeof(s);
285 spin_lock_irqsave(&speakup_info.spinlock, flags);
288 empty = synth_buffer_empty();
289 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
291 speakup_start_ttys();
297 static ssize_t softsynth_read(struct file *fp, char __user *buf, size_t count,
300 return softsynthx_read(fp, buf, count, pos, 0);
303 static ssize_t softsynthu_read(struct file *fp, char __user *buf, size_t count,
306 return softsynthx_read(fp, buf, count, pos, 1);
309 static int last_index;
311 static ssize_t softsynth_write(struct file *fp, const char __user *buf,
312 size_t count, loff_t *pos)
314 unsigned long supplied_index = 0;
317 converted = kstrtoul_from_user(buf, count, 0, &supplied_index);
322 last_index = supplied_index;
326 static __poll_t softsynth_poll(struct file *fp, struct poll_table_struct *wait)
331 poll_wait(fp, &speakup_event, wait);
333 spin_lock_irqsave(&speakup_info.spinlock, flags);
334 if (!synth_buffer_empty() || speakup_info.flushing)
335 ret = EPOLLIN | EPOLLRDNORM;
336 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
340 static unsigned char get_index(struct spk_synth *synth)
349 static const struct file_operations softsynth_fops = {
350 .owner = THIS_MODULE,
351 .poll = softsynth_poll,
352 .read = softsynth_read,
353 .write = softsynth_write,
354 .open = softsynth_open,
355 .release = softsynth_close,
358 static const struct file_operations softsynthu_fops = {
359 .owner = THIS_MODULE,
360 .poll = softsynth_poll,
361 .read = softsynthu_read,
362 .write = softsynth_write,
363 .open = softsynth_open,
364 .release = softsynth_close,
367 static int softsynth_probe(struct spk_synth *synth)
369 if (misc_registered != 0)
371 memset(&synth_device, 0, sizeof(synth_device));
372 synth_device.minor = SOFTSYNTH_MINOR;
373 synth_device.name = "softsynth";
374 synth_device.fops = &softsynth_fops;
375 if (misc_register(&synth_device)) {
376 pr_warn("Couldn't initialize miscdevice /dev/softsynth.\n");
380 memset(&synthu_device, 0, sizeof(synthu_device));
381 synthu_device.minor = SOFTSYNTHU_MINOR;
382 synthu_device.name = "softsynthu";
383 synthu_device.fops = &softsynthu_fops;
384 if (misc_register(&synthu_device)) {
385 pr_warn("Couldn't initialize miscdevice /dev/softsynth.\n");
390 pr_info("initialized device: /dev/softsynth, node (MAJOR 10, MINOR 26)\n");
391 pr_info("initialized device: /dev/softsynthu, node (MAJOR 10, MINOR 27)\n");
395 static void softsynth_release(void)
397 misc_deregister(&synth_device);
398 misc_deregister(&synthu_device);
400 pr_info("unregistered /dev/softsynth\n");
401 pr_info("unregistered /dev/softsynthu\n");
404 static int softsynth_is_alive(struct spk_synth *synth)
406 if (synth_soft.alive)
411 module_param_named(start, synth_soft.startup, short, 0444);
413 MODULE_PARM_DESC(start, "Start the synthesizer once it is loaded.");
415 module_spk_synth(synth_soft);
418 MODULE_DESCRIPTION("Speakup userspace software synthesizer support");
419 MODULE_LICENSE("GPL");
420 MODULE_VERSION(DRV_VERSION);