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>
5  *
6  * Copyright (C) 2003  Kirk Reiser.
7  *
8  * this code is specifically written as a driver for the speakup screenreview
9  * package and is not a general device driver.
10  */
11 
12 #include <linux/unistd.h>
13 #include <linux/miscdevice.h>	/* for misc_register, and MISC_DYNAMIC_MINOR */
14 #include <linux/poll.h>		/* for poll_wait() */
15 
16 /* schedule(), signal_pending(), TASK_INTERRUPTIBLE */
17 #include <linux/sched/signal.h>
18 
19 #include "spk_priv.h"
20 #include "speakup.h"
21 
22 #define DRV_VERSION "2.6"
23 #define PROCSPEECH 0x0d
24 #define CLEAR_SYNTH 0x18
25 
26 static int softsynth_probe(struct spk_synth *synth);
27 static void softsynth_release(struct spk_synth *synth);
28 static int softsynth_is_alive(struct spk_synth *synth);
29 static unsigned char get_index(struct spk_synth *synth);
30 
31 static struct miscdevice synth_device, synthu_device;
32 static int init_pos;
33 static int misc_registered;
34 
35 static struct var_t vars[] = {
36 	{ CAPS_START, .u.s = {"\x01+3p" } },
37 	{ CAPS_STOP, .u.s = {"\x01-3p" } },
38 	{ PAUSE, .u.n = {"\x01P" } },
39 	{ RATE, .u.n = {"\x01%ds", 2, 0, 9, 0, 0, NULL } },
40 	{ PITCH, .u.n = {"\x01%dp", 5, 0, 9, 0, 0, NULL } },
41 	{ INFLECTION, .u.n = {"\x01%dr", 5, 0, 9, 0, 0, NULL } },
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 } },
48 	V_LAST_VAR
49 };
50 
51 /* These attributes will appear in /sys/accessibility/speakup/soft. */
52 
53 static struct kobj_attribute caps_start_attribute =
54 	__ATTR(caps_start, 0644, spk_var_show, spk_var_store);
55 static struct kobj_attribute caps_stop_attribute =
56 	__ATTR(caps_stop, 0644, spk_var_show, spk_var_store);
57 static struct kobj_attribute freq_attribute =
58 	__ATTR(freq, 0644, spk_var_show, spk_var_store);
59 static struct kobj_attribute pitch_attribute =
60 	__ATTR(pitch, 0644, spk_var_show, spk_var_store);
61 static struct kobj_attribute inflection_attribute =
62 	__ATTR(inflection, 0644, spk_var_show, spk_var_store);
63 static struct kobj_attribute punct_attribute =
64 	__ATTR(punct, 0644, spk_var_show, spk_var_store);
65 static struct kobj_attribute rate_attribute =
66 	__ATTR(rate, 0644, spk_var_show, spk_var_store);
67 static struct kobj_attribute tone_attribute =
68 	__ATTR(tone, 0644, spk_var_show, spk_var_store);
69 static struct kobj_attribute voice_attribute =
70 	__ATTR(voice, 0644, spk_var_show, spk_var_store);
71 static struct kobj_attribute vol_attribute =
72 	__ATTR(vol, 0644, spk_var_show, spk_var_store);
73 
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 =
78  *	__ATTR(lang, 0644, spk_var_show, spk_var_store);
79  */
80 
81 static struct kobj_attribute delay_time_attribute =
82 	__ATTR(delay_time, 0644, spk_var_show, spk_var_store);
83 static struct kobj_attribute direct_attribute =
84 	__ATTR(direct, 0644, spk_var_show, spk_var_store);
85 static struct kobj_attribute full_time_attribute =
86 	__ATTR(full_time, 0644, spk_var_show, spk_var_store);
87 static struct kobj_attribute jiffy_delta_attribute =
88 	__ATTR(jiffy_delta, 0644, spk_var_show, spk_var_store);
89 static struct kobj_attribute trigger_time_attribute =
90 	__ATTR(trigger_time, 0644, spk_var_show, spk_var_store);
91 
92 /*
93  * Create a group of attributes so that we can create and destroy them all
94  * at once.
95  */
96 static struct attribute *synth_attrs[] = {
97 	&caps_start_attribute.attr,
98 	&caps_stop_attribute.attr,
99 	&freq_attribute.attr,
100 /*	&lang_attribute.attr, */
101 	&pitch_attribute.attr,
102 	&inflection_attribute.attr,
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 
116 static 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,
129 	.io_ops = NULL,
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 
151 static char *get_initstring(void)
152 {
153 	static char buf[40];
154 	char *cp;
155 	struct var_t *var;
156 	size_t len;
157 	size_t n;
158 
159 	memset(buf, 0, sizeof(buf));
160 	cp = buf;
161 	len = sizeof(buf);
162 
163 	var = synth_soft.vars;
164 	while (var->var_id != MAXVARS) {
165 		if (var->var_id != CAPS_START && var->var_id != CAPS_STOP &&
166 		    var->var_id != PAUSE && var->var_id != DIRECT) {
167 			n = scnprintf(cp, len, var->u.n.synth_fmt,
168 				      var->u.n.value);
169 			cp = cp + n;
170 			len = len - n;
171 		}
172 		var++;
173 	}
174 	cp = cp + scnprintf(cp, len, "\n");
175 	return buf;
176 }
177 
178 static int softsynth_open(struct inode *inode, struct file *fp)
179 {
180 	unsigned long flags;
181 	/*if ((fp->f_flags & O_ACCMODE) != O_RDONLY) */
182 	/*	return -EPERM; */
183 	spin_lock_irqsave(&speakup_info.spinlock, flags);
184 	if (synth_soft.alive) {
185 		spin_unlock_irqrestore(&speakup_info.spinlock, flags);
186 		return -EBUSY;
187 	}
188 	synth_soft.alive = 1;
189 	spin_unlock_irqrestore(&speakup_info.spinlock, flags);
190 	return 0;
191 }
192 
193 static int softsynth_close(struct inode *inode, struct file *fp)
194 {
195 	unsigned long flags;
196 
197 	spin_lock_irqsave(&speakup_info.spinlock, flags);
198 	synth_soft.alive = 0;
199 	init_pos = 0;
200 	spin_unlock_irqrestore(&speakup_info.spinlock, flags);
201 	/* Make sure we let applications go before leaving */
202 	speakup_start_ttys();
203 	return 0;
204 }
205 
206 static ssize_t softsynthx_read(struct file *fp, char __user *buf, size_t count,
207 			       loff_t *pos, int unicode)
208 {
209 	int chars_sent = 0;
210 	char __user *cp;
211 	char *init;
212 	size_t bytes_per_ch = unicode ? 3 : 1;
213 	u16 ch;
214 	int empty;
215 	unsigned long flags;
216 	DEFINE_WAIT(wait);
217 
218 	if (count < bytes_per_ch)
219 		return -EINVAL;
220 
221 	spin_lock_irqsave(&speakup_info.spinlock, flags);
222 	synth_soft.alive = 1;
223 	while (1) {
224 		prepare_to_wait(&speakup_event, &wait, TASK_INTERRUPTIBLE);
225 		if (synth_current() == &synth_soft) {
226 			if (!unicode)
227 				synth_buffer_skip_nonlatin1();
228 			if (!synth_buffer_empty() || speakup_info.flushing)
229 				break;
230 		}
231 		spin_unlock_irqrestore(&speakup_info.spinlock, flags);
232 		if (fp->f_flags & O_NONBLOCK) {
233 			finish_wait(&speakup_event, &wait);
234 			return -EAGAIN;
235 		}
236 		if (signal_pending(current)) {
237 			finish_wait(&speakup_event, &wait);
238 			return -ERESTARTSYS;
239 		}
240 		schedule();
241 		spin_lock_irqsave(&speakup_info.spinlock, flags);
242 	}
243 	finish_wait(&speakup_event, &wait);
244 
245 	cp = buf;
246 	init = get_initstring();
247 
248 	/* Keep 3 bytes available for a 16bit UTF-8-encoded character */
249 	while (chars_sent <= count - bytes_per_ch) {
250 		if (synth_current() != &synth_soft)
251 			break;
252 		if (speakup_info.flushing) {
253 			speakup_info.flushing = 0;
254 			ch = '\x18';
255 		} else if (init[init_pos]) {
256 			ch = init[init_pos++];
257 		} else {
258 			if (!unicode)
259 				synth_buffer_skip_nonlatin1();
260 			if (synth_buffer_empty())
261 				break;
262 			ch = synth_buffer_getc();
263 		}
264 		spin_unlock_irqrestore(&speakup_info.spinlock, flags);
265 
266 		if ((!unicode && ch < 0x100) || (unicode && ch < 0x80)) {
267 			u_char c = ch;
268 
269 			if (copy_to_user(cp, &c, 1))
270 				return -EFAULT;
271 
272 			chars_sent++;
273 			cp++;
274 		} else if (unicode && ch < 0x800) {
275 			u_char s[2] = {
276 				0xc0 | (ch >> 6),
277 				0x80 | (ch & 0x3f)
278 			};
279 
280 			if (copy_to_user(cp, s, sizeof(s)))
281 				return -EFAULT;
282 
283 			chars_sent += sizeof(s);
284 			cp += sizeof(s);
285 		} else if (unicode) {
286 			u_char s[3] = {
287 				0xe0 | (ch >> 12),
288 				0x80 | ((ch >> 6) & 0x3f),
289 				0x80 | (ch & 0x3f)
290 			};
291 
292 			if (copy_to_user(cp, s, sizeof(s)))
293 				return -EFAULT;
294 
295 			chars_sent += sizeof(s);
296 			cp += sizeof(s);
297 		}
298 
299 		spin_lock_irqsave(&speakup_info.spinlock, flags);
300 	}
301 	*pos += chars_sent;
302 	empty = synth_buffer_empty();
303 	spin_unlock_irqrestore(&speakup_info.spinlock, flags);
304 	if (empty) {
305 		speakup_start_ttys();
306 		*pos = 0;
307 	}
308 	return chars_sent;
309 }
310 
311 static ssize_t softsynth_read(struct file *fp, char __user *buf, size_t count,
312 			      loff_t *pos)
313 {
314 	return softsynthx_read(fp, buf, count, pos, 0);
315 }
316 
317 static ssize_t softsynthu_read(struct file *fp, char __user *buf, size_t count,
318 			       loff_t *pos)
319 {
320 	return softsynthx_read(fp, buf, count, pos, 1);
321 }
322 
323 static int last_index;
324 
325 static ssize_t softsynth_write(struct file *fp, const char __user *buf,
326 			       size_t count, loff_t *pos)
327 {
328 	unsigned long supplied_index = 0;
329 	int converted;
330 
331 	converted = kstrtoul_from_user(buf, count, 0, &supplied_index);
332 
333 	if (converted < 0)
334 		return converted;
335 
336 	last_index = supplied_index;
337 	return count;
338 }
339 
340 static __poll_t softsynth_poll(struct file *fp, struct poll_table_struct *wait)
341 {
342 	unsigned long flags;
343 	__poll_t ret = 0;
344 
345 	poll_wait(fp, &speakup_event, wait);
346 
347 	spin_lock_irqsave(&speakup_info.spinlock, flags);
348 	if (synth_current() == &synth_soft &&
349 	    (!synth_buffer_empty() || speakup_info.flushing))
350 		ret = EPOLLIN | EPOLLRDNORM;
351 	spin_unlock_irqrestore(&speakup_info.spinlock, flags);
352 	return ret;
353 }
354 
355 static unsigned char get_index(struct spk_synth *synth)
356 {
357 	int rv;
358 
359 	rv = last_index;
360 	last_index = 0;
361 	return rv;
362 }
363 
364 static const struct file_operations softsynth_fops = {
365 	.owner = THIS_MODULE,
366 	.poll = softsynth_poll,
367 	.read = softsynth_read,
368 	.write = softsynth_write,
369 	.open = softsynth_open,
370 	.release = softsynth_close,
371 };
372 
373 static const struct file_operations softsynthu_fops = {
374 	.owner = THIS_MODULE,
375 	.poll = softsynth_poll,
376 	.read = softsynthu_read,
377 	.write = softsynth_write,
378 	.open = softsynth_open,
379 	.release = softsynth_close,
380 };
381 
382 static int softsynth_probe(struct spk_synth *synth)
383 {
384 	if (misc_registered != 0)
385 		return 0;
386 	memset(&synth_device, 0, sizeof(synth_device));
387 	synth_device.minor = MISC_DYNAMIC_MINOR;
388 	synth_device.name = "softsynth";
389 	synth_device.fops = &softsynth_fops;
390 	if (misc_register(&synth_device)) {
391 		pr_warn("Couldn't initialize miscdevice /dev/softsynth.\n");
392 		return -ENODEV;
393 	}
394 
395 	memset(&synthu_device, 0, sizeof(synthu_device));
396 	synthu_device.minor = MISC_DYNAMIC_MINOR;
397 	synthu_device.name = "softsynthu";
398 	synthu_device.fops = &softsynthu_fops;
399 	if (misc_register(&synthu_device)) {
400 		misc_deregister(&synth_device);
401 		pr_warn("Couldn't initialize miscdevice /dev/softsynthu.\n");
402 		return -ENODEV;
403 	}
404 
405 	misc_registered = 1;
406 	pr_info("initialized device: /dev/softsynth, node (MAJOR 10, MINOR %d)\n",
407 		synth_device.minor);
408 	pr_info("initialized device: /dev/softsynthu, node (MAJOR 10, MINOR %d)\n",
409 		synthu_device.minor);
410 	return 0;
411 }
412 
413 static void softsynth_release(struct spk_synth *synth)
414 {
415 	misc_deregister(&synth_device);
416 	misc_deregister(&synthu_device);
417 	misc_registered = 0;
418 	pr_info("unregistered /dev/softsynth\n");
419 	pr_info("unregistered /dev/softsynthu\n");
420 }
421 
422 static int softsynth_is_alive(struct spk_synth *synth)
423 {
424 	if (synth_soft.alive)
425 		return 1;
426 	return 0;
427 }
428 
429 module_param_named(start, synth_soft.startup, short, 0444);
430 
431 MODULE_PARM_DESC(start, "Start the synthesizer once it is loaded.");
432 
433 module_spk_synth(synth_soft);
434 
435 MODULE_AUTHOR("Kirk Reiser <kirk@braille.uwo.ca>");
436 MODULE_DESCRIPTION("Speakup userspace software synthesizer support");
437 MODULE_LICENSE("GPL");
438 MODULE_VERSION(DRV_VERSION);
439