162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
262306a36Sopenharmony_ci/* speakup_soft.c - speakup driver to register and make available
362306a36Sopenharmony_ci * a user space device for software synthesizers.  written by: Kirk
462306a36Sopenharmony_ci * Reiser <kirk@braille.uwo.ca>
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci * Copyright (C) 2003  Kirk Reiser.
762306a36Sopenharmony_ci *
862306a36Sopenharmony_ci * this code is specifically written as a driver for the speakup screenreview
962306a36Sopenharmony_ci * package and is not a general device driver.
1062306a36Sopenharmony_ci */
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci#include <linux/unistd.h>
1362306a36Sopenharmony_ci#include <linux/miscdevice.h>	/* for misc_register, and MISC_DYNAMIC_MINOR */
1462306a36Sopenharmony_ci#include <linux/poll.h>		/* for poll_wait() */
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci/* schedule(), signal_pending(), TASK_INTERRUPTIBLE */
1762306a36Sopenharmony_ci#include <linux/sched/signal.h>
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ci#include "spk_priv.h"
2062306a36Sopenharmony_ci#include "speakup.h"
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci#define DRV_VERSION "2.6"
2362306a36Sopenharmony_ci#define PROCSPEECH 0x0d
2462306a36Sopenharmony_ci#define CLEAR_SYNTH 0x18
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_cistatic int softsynth_probe(struct spk_synth *synth);
2762306a36Sopenharmony_cistatic void softsynth_release(struct spk_synth *synth);
2862306a36Sopenharmony_cistatic int softsynth_is_alive(struct spk_synth *synth);
2962306a36Sopenharmony_cistatic int softsynth_adjust(struct spk_synth *synth, struct st_var_header *var);
3062306a36Sopenharmony_cistatic unsigned char get_index(struct spk_synth *synth);
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_cistatic struct miscdevice synth_device, synthu_device;
3362306a36Sopenharmony_cistatic int init_pos;
3462306a36Sopenharmony_cistatic int misc_registered;
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_cienum default_vars_id {
3862306a36Sopenharmony_ci	DIRECT_ID = 0, CAPS_START_ID, CAPS_STOP_ID,
3962306a36Sopenharmony_ci	PAUSE_ID, RATE_ID, PITCH_ID, INFLECTION_ID,
4062306a36Sopenharmony_ci	VOL_ID, TONE_ID, PUNCT_ID, VOICE_ID,
4162306a36Sopenharmony_ci	FREQUENCY_ID, V_LAST_VAR_ID,
4262306a36Sopenharmony_ci	 NB_ID
4362306a36Sopenharmony_ci};
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_cistatic struct var_t vars[NB_ID] = {
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ci	[DIRECT_ID]  = { DIRECT, .u.n = {NULL, 0, 0, 1, 0, 0, NULL } },
4962306a36Sopenharmony_ci	[CAPS_START_ID] = { CAPS_START, .u.s = {"\x01+3p" } },
5062306a36Sopenharmony_ci	[CAPS_STOP_ID]  = { CAPS_STOP, .u.s = {"\x01-3p" } },
5162306a36Sopenharmony_ci	[PAUSE_ID]  = { PAUSE, .u.n = {"\x01P" } },
5262306a36Sopenharmony_ci	[RATE_ID]  = { RATE, .u.n = {"\x01%ds", 2, 0, 9, 0, 0, NULL } },
5362306a36Sopenharmony_ci	[PITCH_ID]  = { PITCH, .u.n = {"\x01%dp", 5, 0, 9, 0, 0, NULL } },
5462306a36Sopenharmony_ci	[INFLECTION_ID]  = { INFLECTION, .u.n = {"\x01%dr", 5, 0, 9, 0, 0, NULL } },
5562306a36Sopenharmony_ci	[VOL_ID]  = { VOL, .u.n = {"\x01%dv", 5, 0, 9, 0, 0, NULL } },
5662306a36Sopenharmony_ci	[TONE_ID]  = { TONE, .u.n = {"\x01%dx", 1, 0, 2, 0, 0, NULL } },
5762306a36Sopenharmony_ci	[PUNCT_ID]  = { PUNCT, .u.n = {"\x01%db", 0, 0, 3, 0, 0, NULL } },
5862306a36Sopenharmony_ci	[VOICE_ID]  = { VOICE, .u.n = {"\x01%do", 0, 0, 7, 0, 0, NULL } },
5962306a36Sopenharmony_ci	[FREQUENCY_ID]  = { FREQUENCY, .u.n = {"\x01%df", 5, 0, 9, 0, 0, NULL } },
6062306a36Sopenharmony_ci	V_LAST_VAR
6162306a36Sopenharmony_ci};
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci/* These attributes will appear in /sys/accessibility/speakup/soft. */
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_cistatic struct kobj_attribute caps_start_attribute =
6662306a36Sopenharmony_ci	__ATTR(caps_start, 0644, spk_var_show, spk_var_store);
6762306a36Sopenharmony_cistatic struct kobj_attribute caps_stop_attribute =
6862306a36Sopenharmony_ci	__ATTR(caps_stop, 0644, spk_var_show, spk_var_store);
6962306a36Sopenharmony_cistatic struct kobj_attribute freq_attribute =
7062306a36Sopenharmony_ci	__ATTR(freq, 0644, spk_var_show, spk_var_store);
7162306a36Sopenharmony_cistatic struct kobj_attribute pitch_attribute =
7262306a36Sopenharmony_ci	__ATTR(pitch, 0644, spk_var_show, spk_var_store);
7362306a36Sopenharmony_cistatic struct kobj_attribute inflection_attribute =
7462306a36Sopenharmony_ci	__ATTR(inflection, 0644, spk_var_show, spk_var_store);
7562306a36Sopenharmony_cistatic struct kobj_attribute punct_attribute =
7662306a36Sopenharmony_ci	__ATTR(punct, 0644, spk_var_show, spk_var_store);
7762306a36Sopenharmony_cistatic struct kobj_attribute rate_attribute =
7862306a36Sopenharmony_ci	__ATTR(rate, 0644, spk_var_show, spk_var_store);
7962306a36Sopenharmony_cistatic struct kobj_attribute tone_attribute =
8062306a36Sopenharmony_ci	__ATTR(tone, 0644, spk_var_show, spk_var_store);
8162306a36Sopenharmony_cistatic struct kobj_attribute voice_attribute =
8262306a36Sopenharmony_ci	__ATTR(voice, 0644, spk_var_show, spk_var_store);
8362306a36Sopenharmony_cistatic struct kobj_attribute vol_attribute =
8462306a36Sopenharmony_ci	__ATTR(vol, 0644, spk_var_show, spk_var_store);
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ci/*
8762306a36Sopenharmony_ci * We should uncomment the following definition, when we agree on a
8862306a36Sopenharmony_ci * method of passing a language designation to the software synthesizer.
8962306a36Sopenharmony_ci * static struct kobj_attribute lang_attribute =
9062306a36Sopenharmony_ci *	__ATTR(lang, 0644, spk_var_show, spk_var_store);
9162306a36Sopenharmony_ci */
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_cistatic struct kobj_attribute delay_time_attribute =
9462306a36Sopenharmony_ci	__ATTR(delay_time, 0644, spk_var_show, spk_var_store);
9562306a36Sopenharmony_cistatic struct kobj_attribute direct_attribute =
9662306a36Sopenharmony_ci	__ATTR(direct, 0644, spk_var_show, spk_var_store);
9762306a36Sopenharmony_cistatic struct kobj_attribute full_time_attribute =
9862306a36Sopenharmony_ci	__ATTR(full_time, 0644, spk_var_show, spk_var_store);
9962306a36Sopenharmony_cistatic struct kobj_attribute jiffy_delta_attribute =
10062306a36Sopenharmony_ci	__ATTR(jiffy_delta, 0644, spk_var_show, spk_var_store);
10162306a36Sopenharmony_cistatic struct kobj_attribute trigger_time_attribute =
10262306a36Sopenharmony_ci	__ATTR(trigger_time, 0644, spk_var_show, spk_var_store);
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ci/*
10562306a36Sopenharmony_ci * Create a group of attributes so that we can create and destroy them all
10662306a36Sopenharmony_ci * at once.
10762306a36Sopenharmony_ci */
10862306a36Sopenharmony_cistatic struct attribute *synth_attrs[] = {
10962306a36Sopenharmony_ci	&caps_start_attribute.attr,
11062306a36Sopenharmony_ci	&caps_stop_attribute.attr,
11162306a36Sopenharmony_ci	&freq_attribute.attr,
11262306a36Sopenharmony_ci/*	&lang_attribute.attr, */
11362306a36Sopenharmony_ci	&pitch_attribute.attr,
11462306a36Sopenharmony_ci	&inflection_attribute.attr,
11562306a36Sopenharmony_ci	&punct_attribute.attr,
11662306a36Sopenharmony_ci	&rate_attribute.attr,
11762306a36Sopenharmony_ci	&tone_attribute.attr,
11862306a36Sopenharmony_ci	&voice_attribute.attr,
11962306a36Sopenharmony_ci	&vol_attribute.attr,
12062306a36Sopenharmony_ci	&delay_time_attribute.attr,
12162306a36Sopenharmony_ci	&direct_attribute.attr,
12262306a36Sopenharmony_ci	&full_time_attribute.attr,
12362306a36Sopenharmony_ci	&jiffy_delta_attribute.attr,
12462306a36Sopenharmony_ci	&trigger_time_attribute.attr,
12562306a36Sopenharmony_ci	NULL,	/* need to NULL terminate the list of attributes */
12662306a36Sopenharmony_ci};
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_cistatic struct spk_synth synth_soft = {
12962306a36Sopenharmony_ci	.name = "soft",
13062306a36Sopenharmony_ci	.version = DRV_VERSION,
13162306a36Sopenharmony_ci	.long_name = "software synth",
13262306a36Sopenharmony_ci	.init = "\01@\x01\x31y\n",
13362306a36Sopenharmony_ci	.procspeech = PROCSPEECH,
13462306a36Sopenharmony_ci	.delay = 0,
13562306a36Sopenharmony_ci	.trigger = 0,
13662306a36Sopenharmony_ci	.jiffies = 0,
13762306a36Sopenharmony_ci	.full = 0,
13862306a36Sopenharmony_ci	.startup = SYNTH_START,
13962306a36Sopenharmony_ci	.checkval = SYNTH_CHECK,
14062306a36Sopenharmony_ci	.vars = vars,
14162306a36Sopenharmony_ci	.io_ops = NULL,
14262306a36Sopenharmony_ci	.probe = softsynth_probe,
14362306a36Sopenharmony_ci	.release = softsynth_release,
14462306a36Sopenharmony_ci	.synth_immediate = NULL,
14562306a36Sopenharmony_ci	.catch_up = NULL,
14662306a36Sopenharmony_ci	.flush = NULL,
14762306a36Sopenharmony_ci	.is_alive = softsynth_is_alive,
14862306a36Sopenharmony_ci	.synth_adjust = softsynth_adjust,
14962306a36Sopenharmony_ci	.read_buff_add = NULL,
15062306a36Sopenharmony_ci	.get_index = get_index,
15162306a36Sopenharmony_ci	.indexing = {
15262306a36Sopenharmony_ci		.command = "\x01%di",
15362306a36Sopenharmony_ci		.lowindex = 1,
15462306a36Sopenharmony_ci		.highindex = 5,
15562306a36Sopenharmony_ci		.currindex = 1,
15662306a36Sopenharmony_ci	},
15762306a36Sopenharmony_ci	.attributes = {
15862306a36Sopenharmony_ci		.attrs = synth_attrs,
15962306a36Sopenharmony_ci		.name = "soft",
16062306a36Sopenharmony_ci	},
16162306a36Sopenharmony_ci};
16262306a36Sopenharmony_ci
16362306a36Sopenharmony_cistatic char *get_initstring(void)
16462306a36Sopenharmony_ci{
16562306a36Sopenharmony_ci	static char buf[40];
16662306a36Sopenharmony_ci	char *cp;
16762306a36Sopenharmony_ci	struct var_t *var;
16862306a36Sopenharmony_ci	size_t len;
16962306a36Sopenharmony_ci	size_t n;
17062306a36Sopenharmony_ci
17162306a36Sopenharmony_ci	memset(buf, 0, sizeof(buf));
17262306a36Sopenharmony_ci	cp = buf;
17362306a36Sopenharmony_ci	len = sizeof(buf);
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_ci	var = synth_soft.vars;
17662306a36Sopenharmony_ci	while (var->var_id != MAXVARS) {
17762306a36Sopenharmony_ci		if (var->var_id != CAPS_START && var->var_id != CAPS_STOP &&
17862306a36Sopenharmony_ci		    var->var_id != PAUSE && var->var_id != DIRECT) {
17962306a36Sopenharmony_ci			n = scnprintf(cp, len, var->u.n.synth_fmt,
18062306a36Sopenharmony_ci				      var->u.n.value);
18162306a36Sopenharmony_ci			cp = cp + n;
18262306a36Sopenharmony_ci			len = len - n;
18362306a36Sopenharmony_ci		}
18462306a36Sopenharmony_ci		var++;
18562306a36Sopenharmony_ci	}
18662306a36Sopenharmony_ci	cp = cp + scnprintf(cp, len, "\n");
18762306a36Sopenharmony_ci	return buf;
18862306a36Sopenharmony_ci}
18962306a36Sopenharmony_ci
19062306a36Sopenharmony_cistatic int softsynth_open(struct inode *inode, struct file *fp)
19162306a36Sopenharmony_ci{
19262306a36Sopenharmony_ci	unsigned long flags;
19362306a36Sopenharmony_ci	/*if ((fp->f_flags & O_ACCMODE) != O_RDONLY) */
19462306a36Sopenharmony_ci	/*	return -EPERM; */
19562306a36Sopenharmony_ci	spin_lock_irqsave(&speakup_info.spinlock, flags);
19662306a36Sopenharmony_ci	if (synth_soft.alive) {
19762306a36Sopenharmony_ci		spin_unlock_irqrestore(&speakup_info.spinlock, flags);
19862306a36Sopenharmony_ci		return -EBUSY;
19962306a36Sopenharmony_ci	}
20062306a36Sopenharmony_ci	synth_soft.alive = 1;
20162306a36Sopenharmony_ci	spin_unlock_irqrestore(&speakup_info.spinlock, flags);
20262306a36Sopenharmony_ci	return 0;
20362306a36Sopenharmony_ci}
20462306a36Sopenharmony_ci
20562306a36Sopenharmony_cistatic int softsynth_close(struct inode *inode, struct file *fp)
20662306a36Sopenharmony_ci{
20762306a36Sopenharmony_ci	unsigned long flags;
20862306a36Sopenharmony_ci
20962306a36Sopenharmony_ci	spin_lock_irqsave(&speakup_info.spinlock, flags);
21062306a36Sopenharmony_ci	synth_soft.alive = 0;
21162306a36Sopenharmony_ci	init_pos = 0;
21262306a36Sopenharmony_ci	spin_unlock_irqrestore(&speakup_info.spinlock, flags);
21362306a36Sopenharmony_ci	/* Make sure we let applications go before leaving */
21462306a36Sopenharmony_ci	speakup_start_ttys();
21562306a36Sopenharmony_ci	return 0;
21662306a36Sopenharmony_ci}
21762306a36Sopenharmony_ci
21862306a36Sopenharmony_cistatic ssize_t softsynthx_read(struct file *fp, char __user *buf, size_t count,
21962306a36Sopenharmony_ci			       loff_t *pos, int unicode)
22062306a36Sopenharmony_ci{
22162306a36Sopenharmony_ci	int chars_sent = 0;
22262306a36Sopenharmony_ci	char __user *cp;
22362306a36Sopenharmony_ci	char *init;
22462306a36Sopenharmony_ci	size_t bytes_per_ch = unicode ? 3 : 1;
22562306a36Sopenharmony_ci	u16 ch;
22662306a36Sopenharmony_ci	int empty;
22762306a36Sopenharmony_ci	unsigned long flags;
22862306a36Sopenharmony_ci	DEFINE_WAIT(wait);
22962306a36Sopenharmony_ci
23062306a36Sopenharmony_ci	if (count < bytes_per_ch)
23162306a36Sopenharmony_ci		return -EINVAL;
23262306a36Sopenharmony_ci
23362306a36Sopenharmony_ci	spin_lock_irqsave(&speakup_info.spinlock, flags);
23462306a36Sopenharmony_ci	synth_soft.alive = 1;
23562306a36Sopenharmony_ci	while (1) {
23662306a36Sopenharmony_ci		prepare_to_wait(&speakup_event, &wait, TASK_INTERRUPTIBLE);
23762306a36Sopenharmony_ci		if (synth_current() == &synth_soft) {
23862306a36Sopenharmony_ci			if (!unicode)
23962306a36Sopenharmony_ci				synth_buffer_skip_nonlatin1();
24062306a36Sopenharmony_ci			if (!synth_buffer_empty() || speakup_info.flushing)
24162306a36Sopenharmony_ci				break;
24262306a36Sopenharmony_ci		}
24362306a36Sopenharmony_ci		spin_unlock_irqrestore(&speakup_info.spinlock, flags);
24462306a36Sopenharmony_ci		if (fp->f_flags & O_NONBLOCK) {
24562306a36Sopenharmony_ci			finish_wait(&speakup_event, &wait);
24662306a36Sopenharmony_ci			return -EAGAIN;
24762306a36Sopenharmony_ci		}
24862306a36Sopenharmony_ci		if (signal_pending(current)) {
24962306a36Sopenharmony_ci			finish_wait(&speakup_event, &wait);
25062306a36Sopenharmony_ci			return -ERESTARTSYS;
25162306a36Sopenharmony_ci		}
25262306a36Sopenharmony_ci		schedule();
25362306a36Sopenharmony_ci		spin_lock_irqsave(&speakup_info.spinlock, flags);
25462306a36Sopenharmony_ci	}
25562306a36Sopenharmony_ci	finish_wait(&speakup_event, &wait);
25662306a36Sopenharmony_ci
25762306a36Sopenharmony_ci	cp = buf;
25862306a36Sopenharmony_ci	init = get_initstring();
25962306a36Sopenharmony_ci
26062306a36Sopenharmony_ci	/* Keep 3 bytes available for a 16bit UTF-8-encoded character */
26162306a36Sopenharmony_ci	while (chars_sent <= count - bytes_per_ch) {
26262306a36Sopenharmony_ci		if (synth_current() != &synth_soft)
26362306a36Sopenharmony_ci			break;
26462306a36Sopenharmony_ci		if (speakup_info.flushing) {
26562306a36Sopenharmony_ci			speakup_info.flushing = 0;
26662306a36Sopenharmony_ci			ch = '\x18';
26762306a36Sopenharmony_ci		} else if (init[init_pos]) {
26862306a36Sopenharmony_ci			ch = init[init_pos++];
26962306a36Sopenharmony_ci		} else {
27062306a36Sopenharmony_ci			if (!unicode)
27162306a36Sopenharmony_ci				synth_buffer_skip_nonlatin1();
27262306a36Sopenharmony_ci			if (synth_buffer_empty())
27362306a36Sopenharmony_ci				break;
27462306a36Sopenharmony_ci			ch = synth_buffer_getc();
27562306a36Sopenharmony_ci		}
27662306a36Sopenharmony_ci		spin_unlock_irqrestore(&speakup_info.spinlock, flags);
27762306a36Sopenharmony_ci
27862306a36Sopenharmony_ci		if ((!unicode && ch < 0x100) || (unicode && ch < 0x80)) {
27962306a36Sopenharmony_ci			u_char c = ch;
28062306a36Sopenharmony_ci
28162306a36Sopenharmony_ci			if (copy_to_user(cp, &c, 1))
28262306a36Sopenharmony_ci				return -EFAULT;
28362306a36Sopenharmony_ci
28462306a36Sopenharmony_ci			chars_sent++;
28562306a36Sopenharmony_ci			cp++;
28662306a36Sopenharmony_ci		} else if (unicode && ch < 0x800) {
28762306a36Sopenharmony_ci			u_char s[2] = {
28862306a36Sopenharmony_ci				0xc0 | (ch >> 6),
28962306a36Sopenharmony_ci				0x80 | (ch & 0x3f)
29062306a36Sopenharmony_ci			};
29162306a36Sopenharmony_ci
29262306a36Sopenharmony_ci			if (copy_to_user(cp, s, sizeof(s)))
29362306a36Sopenharmony_ci				return -EFAULT;
29462306a36Sopenharmony_ci
29562306a36Sopenharmony_ci			chars_sent += sizeof(s);
29662306a36Sopenharmony_ci			cp += sizeof(s);
29762306a36Sopenharmony_ci		} else if (unicode) {
29862306a36Sopenharmony_ci			u_char s[3] = {
29962306a36Sopenharmony_ci				0xe0 | (ch >> 12),
30062306a36Sopenharmony_ci				0x80 | ((ch >> 6) & 0x3f),
30162306a36Sopenharmony_ci				0x80 | (ch & 0x3f)
30262306a36Sopenharmony_ci			};
30362306a36Sopenharmony_ci
30462306a36Sopenharmony_ci			if (copy_to_user(cp, s, sizeof(s)))
30562306a36Sopenharmony_ci				return -EFAULT;
30662306a36Sopenharmony_ci
30762306a36Sopenharmony_ci			chars_sent += sizeof(s);
30862306a36Sopenharmony_ci			cp += sizeof(s);
30962306a36Sopenharmony_ci		}
31062306a36Sopenharmony_ci
31162306a36Sopenharmony_ci		spin_lock_irqsave(&speakup_info.spinlock, flags);
31262306a36Sopenharmony_ci	}
31362306a36Sopenharmony_ci	*pos += chars_sent;
31462306a36Sopenharmony_ci	empty = synth_buffer_empty();
31562306a36Sopenharmony_ci	spin_unlock_irqrestore(&speakup_info.spinlock, flags);
31662306a36Sopenharmony_ci	if (empty) {
31762306a36Sopenharmony_ci		speakup_start_ttys();
31862306a36Sopenharmony_ci		*pos = 0;
31962306a36Sopenharmony_ci	}
32062306a36Sopenharmony_ci	return chars_sent;
32162306a36Sopenharmony_ci}
32262306a36Sopenharmony_ci
32362306a36Sopenharmony_cistatic ssize_t softsynth_read(struct file *fp, char __user *buf, size_t count,
32462306a36Sopenharmony_ci			      loff_t *pos)
32562306a36Sopenharmony_ci{
32662306a36Sopenharmony_ci	return softsynthx_read(fp, buf, count, pos, 0);
32762306a36Sopenharmony_ci}
32862306a36Sopenharmony_ci
32962306a36Sopenharmony_cistatic ssize_t softsynthu_read(struct file *fp, char __user *buf, size_t count,
33062306a36Sopenharmony_ci			       loff_t *pos)
33162306a36Sopenharmony_ci{
33262306a36Sopenharmony_ci	return softsynthx_read(fp, buf, count, pos, 1);
33362306a36Sopenharmony_ci}
33462306a36Sopenharmony_ci
33562306a36Sopenharmony_cistatic int last_index;
33662306a36Sopenharmony_ci
33762306a36Sopenharmony_cistatic ssize_t softsynth_write(struct file *fp, const char __user *buf,
33862306a36Sopenharmony_ci			       size_t count, loff_t *pos)
33962306a36Sopenharmony_ci{
34062306a36Sopenharmony_ci	unsigned long supplied_index = 0;
34162306a36Sopenharmony_ci	int converted;
34262306a36Sopenharmony_ci
34362306a36Sopenharmony_ci	converted = kstrtoul_from_user(buf, count, 0, &supplied_index);
34462306a36Sopenharmony_ci
34562306a36Sopenharmony_ci	if (converted < 0)
34662306a36Sopenharmony_ci		return converted;
34762306a36Sopenharmony_ci
34862306a36Sopenharmony_ci	last_index = supplied_index;
34962306a36Sopenharmony_ci	return count;
35062306a36Sopenharmony_ci}
35162306a36Sopenharmony_ci
35262306a36Sopenharmony_cistatic __poll_t softsynth_poll(struct file *fp, struct poll_table_struct *wait)
35362306a36Sopenharmony_ci{
35462306a36Sopenharmony_ci	unsigned long flags;
35562306a36Sopenharmony_ci	__poll_t ret = 0;
35662306a36Sopenharmony_ci
35762306a36Sopenharmony_ci	poll_wait(fp, &speakup_event, wait);
35862306a36Sopenharmony_ci
35962306a36Sopenharmony_ci	spin_lock_irqsave(&speakup_info.spinlock, flags);
36062306a36Sopenharmony_ci	if (synth_current() == &synth_soft &&
36162306a36Sopenharmony_ci	    (!synth_buffer_empty() || speakup_info.flushing))
36262306a36Sopenharmony_ci		ret = EPOLLIN | EPOLLRDNORM;
36362306a36Sopenharmony_ci	spin_unlock_irqrestore(&speakup_info.spinlock, flags);
36462306a36Sopenharmony_ci	return ret;
36562306a36Sopenharmony_ci}
36662306a36Sopenharmony_ci
36762306a36Sopenharmony_cistatic unsigned char get_index(struct spk_synth *synth)
36862306a36Sopenharmony_ci{
36962306a36Sopenharmony_ci	int rv;
37062306a36Sopenharmony_ci
37162306a36Sopenharmony_ci	rv = last_index;
37262306a36Sopenharmony_ci	last_index = 0;
37362306a36Sopenharmony_ci	return rv;
37462306a36Sopenharmony_ci}
37562306a36Sopenharmony_ci
37662306a36Sopenharmony_cistatic const struct file_operations softsynth_fops = {
37762306a36Sopenharmony_ci	.owner = THIS_MODULE,
37862306a36Sopenharmony_ci	.poll = softsynth_poll,
37962306a36Sopenharmony_ci	.read = softsynth_read,
38062306a36Sopenharmony_ci	.write = softsynth_write,
38162306a36Sopenharmony_ci	.open = softsynth_open,
38262306a36Sopenharmony_ci	.release = softsynth_close,
38362306a36Sopenharmony_ci};
38462306a36Sopenharmony_ci
38562306a36Sopenharmony_cistatic const struct file_operations softsynthu_fops = {
38662306a36Sopenharmony_ci	.owner = THIS_MODULE,
38762306a36Sopenharmony_ci	.poll = softsynth_poll,
38862306a36Sopenharmony_ci	.read = softsynthu_read,
38962306a36Sopenharmony_ci	.write = softsynth_write,
39062306a36Sopenharmony_ci	.open = softsynth_open,
39162306a36Sopenharmony_ci	.release = softsynth_close,
39262306a36Sopenharmony_ci};
39362306a36Sopenharmony_ci
39462306a36Sopenharmony_cistatic int softsynth_probe(struct spk_synth *synth)
39562306a36Sopenharmony_ci{
39662306a36Sopenharmony_ci	if (misc_registered != 0)
39762306a36Sopenharmony_ci		return 0;
39862306a36Sopenharmony_ci	memset(&synth_device, 0, sizeof(synth_device));
39962306a36Sopenharmony_ci	synth_device.minor = MISC_DYNAMIC_MINOR;
40062306a36Sopenharmony_ci	synth_device.name = "softsynth";
40162306a36Sopenharmony_ci	synth_device.fops = &softsynth_fops;
40262306a36Sopenharmony_ci	if (misc_register(&synth_device)) {
40362306a36Sopenharmony_ci		pr_warn("Couldn't initialize miscdevice /dev/softsynth.\n");
40462306a36Sopenharmony_ci		return -ENODEV;
40562306a36Sopenharmony_ci	}
40662306a36Sopenharmony_ci
40762306a36Sopenharmony_ci	memset(&synthu_device, 0, sizeof(synthu_device));
40862306a36Sopenharmony_ci	synthu_device.minor = MISC_DYNAMIC_MINOR;
40962306a36Sopenharmony_ci	synthu_device.name = "softsynthu";
41062306a36Sopenharmony_ci	synthu_device.fops = &softsynthu_fops;
41162306a36Sopenharmony_ci	if (misc_register(&synthu_device)) {
41262306a36Sopenharmony_ci		misc_deregister(&synth_device);
41362306a36Sopenharmony_ci		pr_warn("Couldn't initialize miscdevice /dev/softsynthu.\n");
41462306a36Sopenharmony_ci		return -ENODEV;
41562306a36Sopenharmony_ci	}
41662306a36Sopenharmony_ci
41762306a36Sopenharmony_ci	misc_registered = 1;
41862306a36Sopenharmony_ci	pr_info("initialized device: /dev/softsynth, node (MAJOR 10, MINOR %d)\n",
41962306a36Sopenharmony_ci		synth_device.minor);
42062306a36Sopenharmony_ci	pr_info("initialized device: /dev/softsynthu, node (MAJOR 10, MINOR %d)\n",
42162306a36Sopenharmony_ci		synthu_device.minor);
42262306a36Sopenharmony_ci	return 0;
42362306a36Sopenharmony_ci}
42462306a36Sopenharmony_ci
42562306a36Sopenharmony_cistatic void softsynth_release(struct spk_synth *synth)
42662306a36Sopenharmony_ci{
42762306a36Sopenharmony_ci	misc_deregister(&synth_device);
42862306a36Sopenharmony_ci	misc_deregister(&synthu_device);
42962306a36Sopenharmony_ci	misc_registered = 0;
43062306a36Sopenharmony_ci	pr_info("unregistered /dev/softsynth\n");
43162306a36Sopenharmony_ci	pr_info("unregistered /dev/softsynthu\n");
43262306a36Sopenharmony_ci}
43362306a36Sopenharmony_ci
43462306a36Sopenharmony_cistatic int softsynth_is_alive(struct spk_synth *synth)
43562306a36Sopenharmony_ci{
43662306a36Sopenharmony_ci	if (synth_soft.alive)
43762306a36Sopenharmony_ci		return 1;
43862306a36Sopenharmony_ci	return 0;
43962306a36Sopenharmony_ci}
44062306a36Sopenharmony_ci
44162306a36Sopenharmony_cistatic int softsynth_adjust(struct spk_synth *synth, struct st_var_header *var)
44262306a36Sopenharmony_ci{
44362306a36Sopenharmony_ci	struct st_var_header *punc_level_var;
44462306a36Sopenharmony_ci	struct var_t *var_data;
44562306a36Sopenharmony_ci
44662306a36Sopenharmony_ci	if (var->var_id != PUNC_LEVEL)
44762306a36Sopenharmony_ci		return 0;
44862306a36Sopenharmony_ci
44962306a36Sopenharmony_ci	/* We want to set the the speech synthesis punctuation level
45062306a36Sopenharmony_ci	 * accordingly, so it properly tunes speaking A_PUNC characters */
45162306a36Sopenharmony_ci	var_data = var->data;
45262306a36Sopenharmony_ci	if (!var_data)
45362306a36Sopenharmony_ci		return 0;
45462306a36Sopenharmony_ci	punc_level_var = spk_get_var_header(PUNCT);
45562306a36Sopenharmony_ci	if (!punc_level_var)
45662306a36Sopenharmony_ci		return 0;
45762306a36Sopenharmony_ci	spk_set_num_var(var_data->u.n.value, punc_level_var, E_SET);
45862306a36Sopenharmony_ci
45962306a36Sopenharmony_ci	return 1;
46062306a36Sopenharmony_ci}
46162306a36Sopenharmony_ci
46262306a36Sopenharmony_cimodule_param_named(start, synth_soft.startup, short, 0444);
46362306a36Sopenharmony_cimodule_param_named(direct, vars[DIRECT_ID].u.n.default_val, int, 0444);
46462306a36Sopenharmony_cimodule_param_named(rate, vars[RATE_ID].u.n.default_val, int, 0444);
46562306a36Sopenharmony_cimodule_param_named(pitch, vars[PITCH_ID].u.n.default_val, int, 0444);
46662306a36Sopenharmony_cimodule_param_named(inflection, vars[INFLECTION_ID].u.n.default_val, int, 0444);
46762306a36Sopenharmony_cimodule_param_named(vol, vars[VOL_ID].u.n.default_val, int, 0444);
46862306a36Sopenharmony_cimodule_param_named(tone, vars[TONE_ID].u.n.default_val, int, 0444);
46962306a36Sopenharmony_cimodule_param_named(punct, vars[PUNCT_ID].u.n.default_val, int, 0444);
47062306a36Sopenharmony_cimodule_param_named(voice, vars[VOICE_ID].u.n.default_val, int, 0444);
47162306a36Sopenharmony_cimodule_param_named(frequency, vars[FREQUENCY_ID].u.n.default_val, int, 0444);
47262306a36Sopenharmony_ci
47362306a36Sopenharmony_ci
47462306a36Sopenharmony_ci
47562306a36Sopenharmony_ciMODULE_PARM_DESC(start, "Start the synthesizer once it is loaded.");
47662306a36Sopenharmony_ciMODULE_PARM_DESC(direct, "Set the direct variable on load.");
47762306a36Sopenharmony_ciMODULE_PARM_DESC(rate, "Sets the rate of the synthesizer.");
47862306a36Sopenharmony_ciMODULE_PARM_DESC(pitch, "Sets the pitch of the synthesizer.");
47962306a36Sopenharmony_ciMODULE_PARM_DESC(inflection, "Sets the inflection of the synthesizer.");
48062306a36Sopenharmony_ciMODULE_PARM_DESC(vol, "Sets the volume of the speech synthesizer.");
48162306a36Sopenharmony_ciMODULE_PARM_DESC(tone, "Sets the tone of the speech synthesizer.");
48262306a36Sopenharmony_ciMODULE_PARM_DESC(punct, "Sets the amount of punctuation spoken by the synthesizer.");
48362306a36Sopenharmony_ciMODULE_PARM_DESC(voice, "Sets the voice used by the synthesizer.");
48462306a36Sopenharmony_ciMODULE_PARM_DESC(frequency, "Sets the frequency of speech synthesizer.");
48562306a36Sopenharmony_ci
48662306a36Sopenharmony_cimodule_spk_synth(synth_soft);
48762306a36Sopenharmony_ci
48862306a36Sopenharmony_ciMODULE_AUTHOR("Kirk Reiser <kirk@braille.uwo.ca>");
48962306a36Sopenharmony_ciMODULE_DESCRIPTION("Speakup userspace software synthesizer support");
49062306a36Sopenharmony_ciMODULE_LICENSE("GPL");
49162306a36Sopenharmony_ciMODULE_VERSION(DRV_VERSION);
492