18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci/* speakup_soft.c - speakup driver to register and make available
38c2ecf20Sopenharmony_ci * a user space device for software synthesizers.  written by: Kirk
48c2ecf20Sopenharmony_ci * Reiser <kirk@braille.uwo.ca>
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Copyright (C) 2003  Kirk Reiser.
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * this code is specificly written as a driver for the speakup screenreview
98c2ecf20Sopenharmony_ci * package and is not a general device driver.
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <linux/unistd.h>
138c2ecf20Sopenharmony_ci#include <linux/miscdevice.h>	/* for misc_register, and MISC_DYNAMIC_MINOR */
148c2ecf20Sopenharmony_ci#include <linux/poll.h>		/* for poll_wait() */
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci/* schedule(), signal_pending(), TASK_INTERRUPTIBLE */
178c2ecf20Sopenharmony_ci#include <linux/sched/signal.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#include "spk_priv.h"
208c2ecf20Sopenharmony_ci#include "speakup.h"
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#define DRV_VERSION "2.6"
238c2ecf20Sopenharmony_ci#define PROCSPEECH 0x0d
248c2ecf20Sopenharmony_ci#define CLEAR_SYNTH 0x18
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_cistatic int softsynth_probe(struct spk_synth *synth);
278c2ecf20Sopenharmony_cistatic void softsynth_release(void);
288c2ecf20Sopenharmony_cistatic int softsynth_is_alive(struct spk_synth *synth);
298c2ecf20Sopenharmony_cistatic unsigned char get_index(struct spk_synth *synth);
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_cistatic struct miscdevice synth_device, synthu_device;
328c2ecf20Sopenharmony_cistatic int init_pos;
338c2ecf20Sopenharmony_cistatic int misc_registered;
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_cistatic struct var_t vars[] = {
368c2ecf20Sopenharmony_ci	{ CAPS_START, .u.s = {"\x01+3p" } },
378c2ecf20Sopenharmony_ci	{ CAPS_STOP, .u.s = {"\x01-3p" } },
388c2ecf20Sopenharmony_ci	{ PAUSE, .u.n = {"\x01P" } },
398c2ecf20Sopenharmony_ci	{ RATE, .u.n = {"\x01%ds", 2, 0, 9, 0, 0, NULL } },
408c2ecf20Sopenharmony_ci	{ PITCH, .u.n = {"\x01%dp", 5, 0, 9, 0, 0, NULL } },
418c2ecf20Sopenharmony_ci	{ INFLECTION, .u.n = {"\x01%dr", 5, 0, 9, 0, 0, NULL } },
428c2ecf20Sopenharmony_ci	{ VOL, .u.n = {"\x01%dv", 5, 0, 9, 0, 0, NULL } },
438c2ecf20Sopenharmony_ci	{ TONE, .u.n = {"\x01%dx", 1, 0, 2, 0, 0, NULL } },
448c2ecf20Sopenharmony_ci	{ PUNCT, .u.n = {"\x01%db", 0, 0, 2, 0, 0, NULL } },
458c2ecf20Sopenharmony_ci	{ VOICE, .u.n = {"\x01%do", 0, 0, 7, 0, 0, NULL } },
468c2ecf20Sopenharmony_ci	{ FREQUENCY, .u.n = {"\x01%df", 5, 0, 9, 0, 0, NULL } },
478c2ecf20Sopenharmony_ci	{ DIRECT, .u.n = {NULL, 0, 0, 1, 0, 0, NULL } },
488c2ecf20Sopenharmony_ci	V_LAST_VAR
498c2ecf20Sopenharmony_ci};
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci/* These attributes will appear in /sys/accessibility/speakup/soft. */
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_cistatic struct kobj_attribute caps_start_attribute =
548c2ecf20Sopenharmony_ci	__ATTR(caps_start, 0644, spk_var_show, spk_var_store);
558c2ecf20Sopenharmony_cistatic struct kobj_attribute caps_stop_attribute =
568c2ecf20Sopenharmony_ci	__ATTR(caps_stop, 0644, spk_var_show, spk_var_store);
578c2ecf20Sopenharmony_cistatic struct kobj_attribute freq_attribute =
588c2ecf20Sopenharmony_ci	__ATTR(freq, 0644, spk_var_show, spk_var_store);
598c2ecf20Sopenharmony_cistatic struct kobj_attribute pitch_attribute =
608c2ecf20Sopenharmony_ci	__ATTR(pitch, 0644, spk_var_show, spk_var_store);
618c2ecf20Sopenharmony_cistatic struct kobj_attribute inflection_attribute =
628c2ecf20Sopenharmony_ci	__ATTR(inflection, 0644, spk_var_show, spk_var_store);
638c2ecf20Sopenharmony_cistatic struct kobj_attribute punct_attribute =
648c2ecf20Sopenharmony_ci	__ATTR(punct, 0644, spk_var_show, spk_var_store);
658c2ecf20Sopenharmony_cistatic struct kobj_attribute rate_attribute =
668c2ecf20Sopenharmony_ci	__ATTR(rate, 0644, spk_var_show, spk_var_store);
678c2ecf20Sopenharmony_cistatic struct kobj_attribute tone_attribute =
688c2ecf20Sopenharmony_ci	__ATTR(tone, 0644, spk_var_show, spk_var_store);
698c2ecf20Sopenharmony_cistatic struct kobj_attribute voice_attribute =
708c2ecf20Sopenharmony_ci	__ATTR(voice, 0644, spk_var_show, spk_var_store);
718c2ecf20Sopenharmony_cistatic struct kobj_attribute vol_attribute =
728c2ecf20Sopenharmony_ci	__ATTR(vol, 0644, spk_var_show, spk_var_store);
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci/*
758c2ecf20Sopenharmony_ci * We should uncomment the following definition, when we agree on a
768c2ecf20Sopenharmony_ci * method of passing a language designation to the software synthesizer.
778c2ecf20Sopenharmony_ci * static struct kobj_attribute lang_attribute =
788c2ecf20Sopenharmony_ci *	__ATTR(lang, 0644, spk_var_show, spk_var_store);
798c2ecf20Sopenharmony_ci */
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_cistatic struct kobj_attribute delay_time_attribute =
828c2ecf20Sopenharmony_ci	__ATTR(delay_time, 0644, spk_var_show, spk_var_store);
838c2ecf20Sopenharmony_cistatic struct kobj_attribute direct_attribute =
848c2ecf20Sopenharmony_ci	__ATTR(direct, 0644, spk_var_show, spk_var_store);
858c2ecf20Sopenharmony_cistatic struct kobj_attribute full_time_attribute =
868c2ecf20Sopenharmony_ci	__ATTR(full_time, 0644, spk_var_show, spk_var_store);
878c2ecf20Sopenharmony_cistatic struct kobj_attribute jiffy_delta_attribute =
888c2ecf20Sopenharmony_ci	__ATTR(jiffy_delta, 0644, spk_var_show, spk_var_store);
898c2ecf20Sopenharmony_cistatic struct kobj_attribute trigger_time_attribute =
908c2ecf20Sopenharmony_ci	__ATTR(trigger_time, 0644, spk_var_show, spk_var_store);
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci/*
938c2ecf20Sopenharmony_ci * Create a group of attributes so that we can create and destroy them all
948c2ecf20Sopenharmony_ci * at once.
958c2ecf20Sopenharmony_ci */
968c2ecf20Sopenharmony_cistatic struct attribute *synth_attrs[] = {
978c2ecf20Sopenharmony_ci	&caps_start_attribute.attr,
988c2ecf20Sopenharmony_ci	&caps_stop_attribute.attr,
998c2ecf20Sopenharmony_ci	&freq_attribute.attr,
1008c2ecf20Sopenharmony_ci/*	&lang_attribute.attr, */
1018c2ecf20Sopenharmony_ci	&pitch_attribute.attr,
1028c2ecf20Sopenharmony_ci	&inflection_attribute.attr,
1038c2ecf20Sopenharmony_ci	&punct_attribute.attr,
1048c2ecf20Sopenharmony_ci	&rate_attribute.attr,
1058c2ecf20Sopenharmony_ci	&tone_attribute.attr,
1068c2ecf20Sopenharmony_ci	&voice_attribute.attr,
1078c2ecf20Sopenharmony_ci	&vol_attribute.attr,
1088c2ecf20Sopenharmony_ci	&delay_time_attribute.attr,
1098c2ecf20Sopenharmony_ci	&direct_attribute.attr,
1108c2ecf20Sopenharmony_ci	&full_time_attribute.attr,
1118c2ecf20Sopenharmony_ci	&jiffy_delta_attribute.attr,
1128c2ecf20Sopenharmony_ci	&trigger_time_attribute.attr,
1138c2ecf20Sopenharmony_ci	NULL,	/* need to NULL terminate the list of attributes */
1148c2ecf20Sopenharmony_ci};
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_cistatic struct spk_synth synth_soft = {
1178c2ecf20Sopenharmony_ci	.name = "soft",
1188c2ecf20Sopenharmony_ci	.version = DRV_VERSION,
1198c2ecf20Sopenharmony_ci	.long_name = "software synth",
1208c2ecf20Sopenharmony_ci	.init = "\01@\x01\x31y\n",
1218c2ecf20Sopenharmony_ci	.procspeech = PROCSPEECH,
1228c2ecf20Sopenharmony_ci	.delay = 0,
1238c2ecf20Sopenharmony_ci	.trigger = 0,
1248c2ecf20Sopenharmony_ci	.jiffies = 0,
1258c2ecf20Sopenharmony_ci	.full = 0,
1268c2ecf20Sopenharmony_ci	.startup = SYNTH_START,
1278c2ecf20Sopenharmony_ci	.checkval = SYNTH_CHECK,
1288c2ecf20Sopenharmony_ci	.vars = vars,
1298c2ecf20Sopenharmony_ci	.io_ops = NULL,
1308c2ecf20Sopenharmony_ci	.probe = softsynth_probe,
1318c2ecf20Sopenharmony_ci	.release = softsynth_release,
1328c2ecf20Sopenharmony_ci	.synth_immediate = NULL,
1338c2ecf20Sopenharmony_ci	.catch_up = NULL,
1348c2ecf20Sopenharmony_ci	.flush = NULL,
1358c2ecf20Sopenharmony_ci	.is_alive = softsynth_is_alive,
1368c2ecf20Sopenharmony_ci	.synth_adjust = NULL,
1378c2ecf20Sopenharmony_ci	.read_buff_add = NULL,
1388c2ecf20Sopenharmony_ci	.get_index = get_index,
1398c2ecf20Sopenharmony_ci	.indexing = {
1408c2ecf20Sopenharmony_ci		.command = "\x01%di",
1418c2ecf20Sopenharmony_ci		.lowindex = 1,
1428c2ecf20Sopenharmony_ci		.highindex = 5,
1438c2ecf20Sopenharmony_ci		.currindex = 1,
1448c2ecf20Sopenharmony_ci	},
1458c2ecf20Sopenharmony_ci	.attributes = {
1468c2ecf20Sopenharmony_ci		.attrs = synth_attrs,
1478c2ecf20Sopenharmony_ci		.name = "soft",
1488c2ecf20Sopenharmony_ci	},
1498c2ecf20Sopenharmony_ci};
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_cistatic char *get_initstring(void)
1528c2ecf20Sopenharmony_ci{
1538c2ecf20Sopenharmony_ci	static char buf[40];
1548c2ecf20Sopenharmony_ci	char *cp;
1558c2ecf20Sopenharmony_ci	struct var_t *var;
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	memset(buf, 0, sizeof(buf));
1588c2ecf20Sopenharmony_ci	cp = buf;
1598c2ecf20Sopenharmony_ci	var = synth_soft.vars;
1608c2ecf20Sopenharmony_ci	while (var->var_id != MAXVARS) {
1618c2ecf20Sopenharmony_ci		if (var->var_id != CAPS_START && var->var_id != CAPS_STOP &&
1628c2ecf20Sopenharmony_ci		    var->var_id != PAUSE && var->var_id != DIRECT)
1638c2ecf20Sopenharmony_ci			cp = cp + sprintf(cp, var->u.n.synth_fmt,
1648c2ecf20Sopenharmony_ci					  var->u.n.value);
1658c2ecf20Sopenharmony_ci		var++;
1668c2ecf20Sopenharmony_ci	}
1678c2ecf20Sopenharmony_ci	cp = cp + sprintf(cp, "\n");
1688c2ecf20Sopenharmony_ci	return buf;
1698c2ecf20Sopenharmony_ci}
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_cistatic int softsynth_open(struct inode *inode, struct file *fp)
1728c2ecf20Sopenharmony_ci{
1738c2ecf20Sopenharmony_ci	unsigned long flags;
1748c2ecf20Sopenharmony_ci	/*if ((fp->f_flags & O_ACCMODE) != O_RDONLY) */
1758c2ecf20Sopenharmony_ci	/*	return -EPERM; */
1768c2ecf20Sopenharmony_ci	spin_lock_irqsave(&speakup_info.spinlock, flags);
1778c2ecf20Sopenharmony_ci	if (synth_soft.alive) {
1788c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&speakup_info.spinlock, flags);
1798c2ecf20Sopenharmony_ci		return -EBUSY;
1808c2ecf20Sopenharmony_ci	}
1818c2ecf20Sopenharmony_ci	synth_soft.alive = 1;
1828c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&speakup_info.spinlock, flags);
1838c2ecf20Sopenharmony_ci	return 0;
1848c2ecf20Sopenharmony_ci}
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_cistatic int softsynth_close(struct inode *inode, struct file *fp)
1878c2ecf20Sopenharmony_ci{
1888c2ecf20Sopenharmony_ci	unsigned long flags;
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci	spin_lock_irqsave(&speakup_info.spinlock, flags);
1918c2ecf20Sopenharmony_ci	synth_soft.alive = 0;
1928c2ecf20Sopenharmony_ci	init_pos = 0;
1938c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&speakup_info.spinlock, flags);
1948c2ecf20Sopenharmony_ci	/* Make sure we let applications go before leaving */
1958c2ecf20Sopenharmony_ci	speakup_start_ttys();
1968c2ecf20Sopenharmony_ci	return 0;
1978c2ecf20Sopenharmony_ci}
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_cistatic ssize_t softsynthx_read(struct file *fp, char __user *buf, size_t count,
2008c2ecf20Sopenharmony_ci			       loff_t *pos, int unicode)
2018c2ecf20Sopenharmony_ci{
2028c2ecf20Sopenharmony_ci	int chars_sent = 0;
2038c2ecf20Sopenharmony_ci	char __user *cp;
2048c2ecf20Sopenharmony_ci	char *init;
2058c2ecf20Sopenharmony_ci	size_t bytes_per_ch = unicode ? 3 : 1;
2068c2ecf20Sopenharmony_ci	u16 ch;
2078c2ecf20Sopenharmony_ci	int empty;
2088c2ecf20Sopenharmony_ci	unsigned long flags;
2098c2ecf20Sopenharmony_ci	DEFINE_WAIT(wait);
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	if (count < bytes_per_ch)
2128c2ecf20Sopenharmony_ci		return -EINVAL;
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	spin_lock_irqsave(&speakup_info.spinlock, flags);
2158c2ecf20Sopenharmony_ci	synth_soft.alive = 1;
2168c2ecf20Sopenharmony_ci	while (1) {
2178c2ecf20Sopenharmony_ci		prepare_to_wait(&speakup_event, &wait, TASK_INTERRUPTIBLE);
2188c2ecf20Sopenharmony_ci		if (synth_current() == &synth_soft) {
2198c2ecf20Sopenharmony_ci			if (!unicode)
2208c2ecf20Sopenharmony_ci				synth_buffer_skip_nonlatin1();
2218c2ecf20Sopenharmony_ci			if (!synth_buffer_empty() || speakup_info.flushing)
2228c2ecf20Sopenharmony_ci				break;
2238c2ecf20Sopenharmony_ci		}
2248c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&speakup_info.spinlock, flags);
2258c2ecf20Sopenharmony_ci		if (fp->f_flags & O_NONBLOCK) {
2268c2ecf20Sopenharmony_ci			finish_wait(&speakup_event, &wait);
2278c2ecf20Sopenharmony_ci			return -EAGAIN;
2288c2ecf20Sopenharmony_ci		}
2298c2ecf20Sopenharmony_ci		if (signal_pending(current)) {
2308c2ecf20Sopenharmony_ci			finish_wait(&speakup_event, &wait);
2318c2ecf20Sopenharmony_ci			return -ERESTARTSYS;
2328c2ecf20Sopenharmony_ci		}
2338c2ecf20Sopenharmony_ci		schedule();
2348c2ecf20Sopenharmony_ci		spin_lock_irqsave(&speakup_info.spinlock, flags);
2358c2ecf20Sopenharmony_ci	}
2368c2ecf20Sopenharmony_ci	finish_wait(&speakup_event, &wait);
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci	cp = buf;
2398c2ecf20Sopenharmony_ci	init = get_initstring();
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci	/* Keep 3 bytes available for a 16bit UTF-8-encoded character */
2428c2ecf20Sopenharmony_ci	while (chars_sent <= count - bytes_per_ch) {
2438c2ecf20Sopenharmony_ci		if (synth_current() != &synth_soft)
2448c2ecf20Sopenharmony_ci			break;
2458c2ecf20Sopenharmony_ci		if (speakup_info.flushing) {
2468c2ecf20Sopenharmony_ci			speakup_info.flushing = 0;
2478c2ecf20Sopenharmony_ci			ch = '\x18';
2488c2ecf20Sopenharmony_ci		} else if (init[init_pos]) {
2498c2ecf20Sopenharmony_ci			ch = init[init_pos++];
2508c2ecf20Sopenharmony_ci		} else {
2518c2ecf20Sopenharmony_ci			if (!unicode)
2528c2ecf20Sopenharmony_ci				synth_buffer_skip_nonlatin1();
2538c2ecf20Sopenharmony_ci			if (synth_buffer_empty())
2548c2ecf20Sopenharmony_ci				break;
2558c2ecf20Sopenharmony_ci			ch = synth_buffer_getc();
2568c2ecf20Sopenharmony_ci		}
2578c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&speakup_info.spinlock, flags);
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci		if ((!unicode && ch < 0x100) || (unicode && ch < 0x80)) {
2608c2ecf20Sopenharmony_ci			u_char c = ch;
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci			if (copy_to_user(cp, &c, 1))
2638c2ecf20Sopenharmony_ci				return -EFAULT;
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci			chars_sent++;
2668c2ecf20Sopenharmony_ci			cp++;
2678c2ecf20Sopenharmony_ci		} else if (unicode && ch < 0x800) {
2688c2ecf20Sopenharmony_ci			u_char s[2] = {
2698c2ecf20Sopenharmony_ci				0xc0 | (ch >> 6),
2708c2ecf20Sopenharmony_ci				0x80 | (ch & 0x3f)
2718c2ecf20Sopenharmony_ci			};
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci			if (copy_to_user(cp, s, sizeof(s)))
2748c2ecf20Sopenharmony_ci				return -EFAULT;
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci			chars_sent += sizeof(s);
2778c2ecf20Sopenharmony_ci			cp += sizeof(s);
2788c2ecf20Sopenharmony_ci		} else if (unicode) {
2798c2ecf20Sopenharmony_ci			u_char s[3] = {
2808c2ecf20Sopenharmony_ci				0xe0 | (ch >> 12),
2818c2ecf20Sopenharmony_ci				0x80 | ((ch >> 6) & 0x3f),
2828c2ecf20Sopenharmony_ci				0x80 | (ch & 0x3f)
2838c2ecf20Sopenharmony_ci			};
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci			if (copy_to_user(cp, s, sizeof(s)))
2868c2ecf20Sopenharmony_ci				return -EFAULT;
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci			chars_sent += sizeof(s);
2898c2ecf20Sopenharmony_ci			cp += sizeof(s);
2908c2ecf20Sopenharmony_ci		}
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci		spin_lock_irqsave(&speakup_info.spinlock, flags);
2938c2ecf20Sopenharmony_ci	}
2948c2ecf20Sopenharmony_ci	*pos += chars_sent;
2958c2ecf20Sopenharmony_ci	empty = synth_buffer_empty();
2968c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&speakup_info.spinlock, flags);
2978c2ecf20Sopenharmony_ci	if (empty) {
2988c2ecf20Sopenharmony_ci		speakup_start_ttys();
2998c2ecf20Sopenharmony_ci		*pos = 0;
3008c2ecf20Sopenharmony_ci	}
3018c2ecf20Sopenharmony_ci	return chars_sent;
3028c2ecf20Sopenharmony_ci}
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_cistatic ssize_t softsynth_read(struct file *fp, char __user *buf, size_t count,
3058c2ecf20Sopenharmony_ci			      loff_t *pos)
3068c2ecf20Sopenharmony_ci{
3078c2ecf20Sopenharmony_ci	return softsynthx_read(fp, buf, count, pos, 0);
3088c2ecf20Sopenharmony_ci}
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_cistatic ssize_t softsynthu_read(struct file *fp, char __user *buf, size_t count,
3118c2ecf20Sopenharmony_ci			       loff_t *pos)
3128c2ecf20Sopenharmony_ci{
3138c2ecf20Sopenharmony_ci	return softsynthx_read(fp, buf, count, pos, 1);
3148c2ecf20Sopenharmony_ci}
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_cistatic int last_index;
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_cistatic ssize_t softsynth_write(struct file *fp, const char __user *buf,
3198c2ecf20Sopenharmony_ci			       size_t count, loff_t *pos)
3208c2ecf20Sopenharmony_ci{
3218c2ecf20Sopenharmony_ci	unsigned long supplied_index = 0;
3228c2ecf20Sopenharmony_ci	int converted;
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci	converted = kstrtoul_from_user(buf, count, 0, &supplied_index);
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci	if (converted < 0)
3278c2ecf20Sopenharmony_ci		return converted;
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci	last_index = supplied_index;
3308c2ecf20Sopenharmony_ci	return count;
3318c2ecf20Sopenharmony_ci}
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_cistatic __poll_t softsynth_poll(struct file *fp, struct poll_table_struct *wait)
3348c2ecf20Sopenharmony_ci{
3358c2ecf20Sopenharmony_ci	unsigned long flags;
3368c2ecf20Sopenharmony_ci	__poll_t ret = 0;
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_ci	poll_wait(fp, &speakup_event, wait);
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	spin_lock_irqsave(&speakup_info.spinlock, flags);
3418c2ecf20Sopenharmony_ci	if (synth_current() == &synth_soft &&
3428c2ecf20Sopenharmony_ci	    (!synth_buffer_empty() || speakup_info.flushing))
3438c2ecf20Sopenharmony_ci		ret = EPOLLIN | EPOLLRDNORM;
3448c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&speakup_info.spinlock, flags);
3458c2ecf20Sopenharmony_ci	return ret;
3468c2ecf20Sopenharmony_ci}
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_cistatic unsigned char get_index(struct spk_synth *synth)
3498c2ecf20Sopenharmony_ci{
3508c2ecf20Sopenharmony_ci	int rv;
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_ci	rv = last_index;
3538c2ecf20Sopenharmony_ci	last_index = 0;
3548c2ecf20Sopenharmony_ci	return rv;
3558c2ecf20Sopenharmony_ci}
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_cistatic const struct file_operations softsynth_fops = {
3588c2ecf20Sopenharmony_ci	.owner = THIS_MODULE,
3598c2ecf20Sopenharmony_ci	.poll = softsynth_poll,
3608c2ecf20Sopenharmony_ci	.read = softsynth_read,
3618c2ecf20Sopenharmony_ci	.write = softsynth_write,
3628c2ecf20Sopenharmony_ci	.open = softsynth_open,
3638c2ecf20Sopenharmony_ci	.release = softsynth_close,
3648c2ecf20Sopenharmony_ci};
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_cistatic const struct file_operations softsynthu_fops = {
3678c2ecf20Sopenharmony_ci	.owner = THIS_MODULE,
3688c2ecf20Sopenharmony_ci	.poll = softsynth_poll,
3698c2ecf20Sopenharmony_ci	.read = softsynthu_read,
3708c2ecf20Sopenharmony_ci	.write = softsynth_write,
3718c2ecf20Sopenharmony_ci	.open = softsynth_open,
3728c2ecf20Sopenharmony_ci	.release = softsynth_close,
3738c2ecf20Sopenharmony_ci};
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_cistatic int softsynth_probe(struct spk_synth *synth)
3768c2ecf20Sopenharmony_ci{
3778c2ecf20Sopenharmony_ci	if (misc_registered != 0)
3788c2ecf20Sopenharmony_ci		return 0;
3798c2ecf20Sopenharmony_ci	memset(&synth_device, 0, sizeof(synth_device));
3808c2ecf20Sopenharmony_ci	synth_device.minor = MISC_DYNAMIC_MINOR;
3818c2ecf20Sopenharmony_ci	synth_device.name = "softsynth";
3828c2ecf20Sopenharmony_ci	synth_device.fops = &softsynth_fops;
3838c2ecf20Sopenharmony_ci	if (misc_register(&synth_device)) {
3848c2ecf20Sopenharmony_ci		pr_warn("Couldn't initialize miscdevice /dev/softsynth.\n");
3858c2ecf20Sopenharmony_ci		return -ENODEV;
3868c2ecf20Sopenharmony_ci	}
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci	memset(&synthu_device, 0, sizeof(synthu_device));
3898c2ecf20Sopenharmony_ci	synthu_device.minor = MISC_DYNAMIC_MINOR;
3908c2ecf20Sopenharmony_ci	synthu_device.name = "softsynthu";
3918c2ecf20Sopenharmony_ci	synthu_device.fops = &softsynthu_fops;
3928c2ecf20Sopenharmony_ci	if (misc_register(&synthu_device)) {
3938c2ecf20Sopenharmony_ci		pr_warn("Couldn't initialize miscdevice /dev/softsynthu.\n");
3948c2ecf20Sopenharmony_ci		return -ENODEV;
3958c2ecf20Sopenharmony_ci	}
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_ci	misc_registered = 1;
3988c2ecf20Sopenharmony_ci	pr_info("initialized device: /dev/softsynth, node (MAJOR 10, MINOR %d)\n",
3998c2ecf20Sopenharmony_ci		synth_device.minor);
4008c2ecf20Sopenharmony_ci	pr_info("initialized device: /dev/softsynthu, node (MAJOR 10, MINOR %d)\n",
4018c2ecf20Sopenharmony_ci		synthu_device.minor);
4028c2ecf20Sopenharmony_ci	return 0;
4038c2ecf20Sopenharmony_ci}
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_cistatic void softsynth_release(void)
4068c2ecf20Sopenharmony_ci{
4078c2ecf20Sopenharmony_ci	misc_deregister(&synth_device);
4088c2ecf20Sopenharmony_ci	misc_deregister(&synthu_device);
4098c2ecf20Sopenharmony_ci	misc_registered = 0;
4108c2ecf20Sopenharmony_ci	pr_info("unregistered /dev/softsynth\n");
4118c2ecf20Sopenharmony_ci	pr_info("unregistered /dev/softsynthu\n");
4128c2ecf20Sopenharmony_ci}
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_cistatic int softsynth_is_alive(struct spk_synth *synth)
4158c2ecf20Sopenharmony_ci{
4168c2ecf20Sopenharmony_ci	if (synth_soft.alive)
4178c2ecf20Sopenharmony_ci		return 1;
4188c2ecf20Sopenharmony_ci	return 0;
4198c2ecf20Sopenharmony_ci}
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_cimodule_param_named(start, synth_soft.startup, short, 0444);
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ciMODULE_PARM_DESC(start, "Start the synthesizer once it is loaded.");
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_cimodule_spk_synth(synth_soft);
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_ciMODULE_AUTHOR("Kirk Reiser <kirk@braille.uwo.ca>");
4288c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Speakup userspace software synthesizer support");
4298c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
4308c2ecf20Sopenharmony_ciMODULE_VERSION(DRV_VERSION);
431