Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /* speakup_soft.c - speakup driver to register and make available
0003  * a user space device for software synthesizers.  written by: Kirk
0004  * Reiser <kirk@braille.uwo.ca>
0005  *
0006  * Copyright (C) 2003  Kirk Reiser.
0007  *
0008  * this code is specifically written as a driver for the speakup screenreview
0009  * package and is not a general device driver.
0010  */
0011 
0012 #include <linux/unistd.h>
0013 #include <linux/miscdevice.h>   /* for misc_register, and MISC_DYNAMIC_MINOR */
0014 #include <linux/poll.h>     /* for poll_wait() */
0015 
0016 /* schedule(), signal_pending(), TASK_INTERRUPTIBLE */
0017 #include <linux/sched/signal.h>
0018 
0019 #include "spk_priv.h"
0020 #include "speakup.h"
0021 
0022 #define DRV_VERSION "2.6"
0023 #define PROCSPEECH 0x0d
0024 #define CLEAR_SYNTH 0x18
0025 
0026 static int softsynth_probe(struct spk_synth *synth);
0027 static void softsynth_release(struct spk_synth *synth);
0028 static int softsynth_is_alive(struct spk_synth *synth);
0029 static unsigned char get_index(struct spk_synth *synth);
0030 
0031 static struct miscdevice synth_device, synthu_device;
0032 static int init_pos;
0033 static int misc_registered;
0034 
0035 static struct var_t vars[] = {
0036     { CAPS_START, .u.s = {"\x01+3p" } },
0037     { CAPS_STOP, .u.s = {"\x01-3p" } },
0038     { PAUSE, .u.n = {"\x01P" } },
0039     { RATE, .u.n = {"\x01%ds", 2, 0, 9, 0, 0, NULL } },
0040     { PITCH, .u.n = {"\x01%dp", 5, 0, 9, 0, 0, NULL } },
0041     { INFLECTION, .u.n = {"\x01%dr", 5, 0, 9, 0, 0, NULL } },
0042     { VOL, .u.n = {"\x01%dv", 5, 0, 9, 0, 0, NULL } },
0043     { TONE, .u.n = {"\x01%dx", 1, 0, 2, 0, 0, NULL } },
0044     { PUNCT, .u.n = {"\x01%db", 0, 0, 2, 0, 0, NULL } },
0045     { VOICE, .u.n = {"\x01%do", 0, 0, 7, 0, 0, NULL } },
0046     { FREQUENCY, .u.n = {"\x01%df", 5, 0, 9, 0, 0, NULL } },
0047     { DIRECT, .u.n = {NULL, 0, 0, 1, 0, 0, NULL } },
0048     V_LAST_VAR
0049 };
0050 
0051 /* These attributes will appear in /sys/accessibility/speakup/soft. */
0052 
0053 static struct kobj_attribute caps_start_attribute =
0054     __ATTR(caps_start, 0644, spk_var_show, spk_var_store);
0055 static struct kobj_attribute caps_stop_attribute =
0056     __ATTR(caps_stop, 0644, spk_var_show, spk_var_store);
0057 static struct kobj_attribute freq_attribute =
0058     __ATTR(freq, 0644, spk_var_show, spk_var_store);
0059 static struct kobj_attribute pitch_attribute =
0060     __ATTR(pitch, 0644, spk_var_show, spk_var_store);
0061 static struct kobj_attribute inflection_attribute =
0062     __ATTR(inflection, 0644, spk_var_show, spk_var_store);
0063 static struct kobj_attribute punct_attribute =
0064     __ATTR(punct, 0644, spk_var_show, spk_var_store);
0065 static struct kobj_attribute rate_attribute =
0066     __ATTR(rate, 0644, spk_var_show, spk_var_store);
0067 static struct kobj_attribute tone_attribute =
0068     __ATTR(tone, 0644, spk_var_show, spk_var_store);
0069 static struct kobj_attribute voice_attribute =
0070     __ATTR(voice, 0644, spk_var_show, spk_var_store);
0071 static struct kobj_attribute vol_attribute =
0072     __ATTR(vol, 0644, spk_var_show, spk_var_store);
0073 
0074 /*
0075  * We should uncomment the following definition, when we agree on a
0076  * method of passing a language designation to the software synthesizer.
0077  * static struct kobj_attribute lang_attribute =
0078  *  __ATTR(lang, 0644, spk_var_show, spk_var_store);
0079  */
0080 
0081 static struct kobj_attribute delay_time_attribute =
0082     __ATTR(delay_time, 0644, spk_var_show, spk_var_store);
0083 static struct kobj_attribute direct_attribute =
0084     __ATTR(direct, 0644, spk_var_show, spk_var_store);
0085 static struct kobj_attribute full_time_attribute =
0086     __ATTR(full_time, 0644, spk_var_show, spk_var_store);
0087 static struct kobj_attribute jiffy_delta_attribute =
0088     __ATTR(jiffy_delta, 0644, spk_var_show, spk_var_store);
0089 static struct kobj_attribute trigger_time_attribute =
0090     __ATTR(trigger_time, 0644, spk_var_show, spk_var_store);
0091 
0092 /*
0093  * Create a group of attributes so that we can create and destroy them all
0094  * at once.
0095  */
0096 static struct attribute *synth_attrs[] = {
0097     &caps_start_attribute.attr,
0098     &caps_stop_attribute.attr,
0099     &freq_attribute.attr,
0100 /*  &lang_attribute.attr, */
0101     &pitch_attribute.attr,
0102     &inflection_attribute.attr,
0103     &punct_attribute.attr,
0104     &rate_attribute.attr,
0105     &tone_attribute.attr,
0106     &voice_attribute.attr,
0107     &vol_attribute.attr,
0108     &delay_time_attribute.attr,
0109     &direct_attribute.attr,
0110     &full_time_attribute.attr,
0111     &jiffy_delta_attribute.attr,
0112     &trigger_time_attribute.attr,
0113     NULL,   /* need to NULL terminate the list of attributes */
0114 };
0115 
0116 static struct spk_synth synth_soft = {
0117     .name = "soft",
0118     .version = DRV_VERSION,
0119     .long_name = "software synth",
0120     .init = "\01@\x01\x31y\n",
0121     .procspeech = PROCSPEECH,
0122     .delay = 0,
0123     .trigger = 0,
0124     .jiffies = 0,
0125     .full = 0,
0126     .startup = SYNTH_START,
0127     .checkval = SYNTH_CHECK,
0128     .vars = vars,
0129     .io_ops = NULL,
0130     .probe = softsynth_probe,
0131     .release = softsynth_release,
0132     .synth_immediate = NULL,
0133     .catch_up = NULL,
0134     .flush = NULL,
0135     .is_alive = softsynth_is_alive,
0136     .synth_adjust = NULL,
0137     .read_buff_add = NULL,
0138     .get_index = get_index,
0139     .indexing = {
0140         .command = "\x01%di",
0141         .lowindex = 1,
0142         .highindex = 5,
0143         .currindex = 1,
0144     },
0145     .attributes = {
0146         .attrs = synth_attrs,
0147         .name = "soft",
0148     },
0149 };
0150 
0151 static char *get_initstring(void)
0152 {
0153     static char buf[40];
0154     char *cp;
0155     struct var_t *var;
0156     size_t len;
0157     size_t n;
0158 
0159     memset(buf, 0, sizeof(buf));
0160     cp = buf;
0161     len = sizeof(buf);
0162 
0163     var = synth_soft.vars;
0164     while (var->var_id != MAXVARS) {
0165         if (var->var_id != CAPS_START && var->var_id != CAPS_STOP &&
0166             var->var_id != PAUSE && var->var_id != DIRECT) {
0167             n = scnprintf(cp, len, var->u.n.synth_fmt,
0168                       var->u.n.value);
0169             cp = cp + n;
0170             len = len - n;
0171         }
0172         var++;
0173     }
0174     cp = cp + scnprintf(cp, len, "\n");
0175     return buf;
0176 }
0177 
0178 static int softsynth_open(struct inode *inode, struct file *fp)
0179 {
0180     unsigned long flags;
0181     /*if ((fp->f_flags & O_ACCMODE) != O_RDONLY) */
0182     /*  return -EPERM; */
0183     spin_lock_irqsave(&speakup_info.spinlock, flags);
0184     if (synth_soft.alive) {
0185         spin_unlock_irqrestore(&speakup_info.spinlock, flags);
0186         return -EBUSY;
0187     }
0188     synth_soft.alive = 1;
0189     spin_unlock_irqrestore(&speakup_info.spinlock, flags);
0190     return 0;
0191 }
0192 
0193 static int softsynth_close(struct inode *inode, struct file *fp)
0194 {
0195     unsigned long flags;
0196 
0197     spin_lock_irqsave(&speakup_info.spinlock, flags);
0198     synth_soft.alive = 0;
0199     init_pos = 0;
0200     spin_unlock_irqrestore(&speakup_info.spinlock, flags);
0201     /* Make sure we let applications go before leaving */
0202     speakup_start_ttys();
0203     return 0;
0204 }
0205 
0206 static ssize_t softsynthx_read(struct file *fp, char __user *buf, size_t count,
0207                    loff_t *pos, int unicode)
0208 {
0209     int chars_sent = 0;
0210     char __user *cp;
0211     char *init;
0212     size_t bytes_per_ch = unicode ? 3 : 1;
0213     u16 ch;
0214     int empty;
0215     unsigned long flags;
0216     DEFINE_WAIT(wait);
0217 
0218     if (count < bytes_per_ch)
0219         return -EINVAL;
0220 
0221     spin_lock_irqsave(&speakup_info.spinlock, flags);
0222     synth_soft.alive = 1;
0223     while (1) {
0224         prepare_to_wait(&speakup_event, &wait, TASK_INTERRUPTIBLE);
0225         if (synth_current() == &synth_soft) {
0226             if (!unicode)
0227                 synth_buffer_skip_nonlatin1();
0228             if (!synth_buffer_empty() || speakup_info.flushing)
0229                 break;
0230         }
0231         spin_unlock_irqrestore(&speakup_info.spinlock, flags);
0232         if (fp->f_flags & O_NONBLOCK) {
0233             finish_wait(&speakup_event, &wait);
0234             return -EAGAIN;
0235         }
0236         if (signal_pending(current)) {
0237             finish_wait(&speakup_event, &wait);
0238             return -ERESTARTSYS;
0239         }
0240         schedule();
0241         spin_lock_irqsave(&speakup_info.spinlock, flags);
0242     }
0243     finish_wait(&speakup_event, &wait);
0244 
0245     cp = buf;
0246     init = get_initstring();
0247 
0248     /* Keep 3 bytes available for a 16bit UTF-8-encoded character */
0249     while (chars_sent <= count - bytes_per_ch) {
0250         if (synth_current() != &synth_soft)
0251             break;
0252         if (speakup_info.flushing) {
0253             speakup_info.flushing = 0;
0254             ch = '\x18';
0255         } else if (init[init_pos]) {
0256             ch = init[init_pos++];
0257         } else {
0258             if (!unicode)
0259                 synth_buffer_skip_nonlatin1();
0260             if (synth_buffer_empty())
0261                 break;
0262             ch = synth_buffer_getc();
0263         }
0264         spin_unlock_irqrestore(&speakup_info.spinlock, flags);
0265 
0266         if ((!unicode && ch < 0x100) || (unicode && ch < 0x80)) {
0267             u_char c = ch;
0268 
0269             if (copy_to_user(cp, &c, 1))
0270                 return -EFAULT;
0271 
0272             chars_sent++;
0273             cp++;
0274         } else if (unicode && ch < 0x800) {
0275             u_char s[2] = {
0276                 0xc0 | (ch >> 6),
0277                 0x80 | (ch & 0x3f)
0278             };
0279 
0280             if (copy_to_user(cp, s, sizeof(s)))
0281                 return -EFAULT;
0282 
0283             chars_sent += sizeof(s);
0284             cp += sizeof(s);
0285         } else if (unicode) {
0286             u_char s[3] = {
0287                 0xe0 | (ch >> 12),
0288                 0x80 | ((ch >> 6) & 0x3f),
0289                 0x80 | (ch & 0x3f)
0290             };
0291 
0292             if (copy_to_user(cp, s, sizeof(s)))
0293                 return -EFAULT;
0294 
0295             chars_sent += sizeof(s);
0296             cp += sizeof(s);
0297         }
0298 
0299         spin_lock_irqsave(&speakup_info.spinlock, flags);
0300     }
0301     *pos += chars_sent;
0302     empty = synth_buffer_empty();
0303     spin_unlock_irqrestore(&speakup_info.spinlock, flags);
0304     if (empty) {
0305         speakup_start_ttys();
0306         *pos = 0;
0307     }
0308     return chars_sent;
0309 }
0310 
0311 static ssize_t softsynth_read(struct file *fp, char __user *buf, size_t count,
0312                   loff_t *pos)
0313 {
0314     return softsynthx_read(fp, buf, count, pos, 0);
0315 }
0316 
0317 static ssize_t softsynthu_read(struct file *fp, char __user *buf, size_t count,
0318                    loff_t *pos)
0319 {
0320     return softsynthx_read(fp, buf, count, pos, 1);
0321 }
0322 
0323 static int last_index;
0324 
0325 static ssize_t softsynth_write(struct file *fp, const char __user *buf,
0326                    size_t count, loff_t *pos)
0327 {
0328     unsigned long supplied_index = 0;
0329     int converted;
0330 
0331     converted = kstrtoul_from_user(buf, count, 0, &supplied_index);
0332 
0333     if (converted < 0)
0334         return converted;
0335 
0336     last_index = supplied_index;
0337     return count;
0338 }
0339 
0340 static __poll_t softsynth_poll(struct file *fp, struct poll_table_struct *wait)
0341 {
0342     unsigned long flags;
0343     __poll_t ret = 0;
0344 
0345     poll_wait(fp, &speakup_event, wait);
0346 
0347     spin_lock_irqsave(&speakup_info.spinlock, flags);
0348     if (synth_current() == &synth_soft &&
0349         (!synth_buffer_empty() || speakup_info.flushing))
0350         ret = EPOLLIN | EPOLLRDNORM;
0351     spin_unlock_irqrestore(&speakup_info.spinlock, flags);
0352     return ret;
0353 }
0354 
0355 static unsigned char get_index(struct spk_synth *synth)
0356 {
0357     int rv;
0358 
0359     rv = last_index;
0360     last_index = 0;
0361     return rv;
0362 }
0363 
0364 static const struct file_operations softsynth_fops = {
0365     .owner = THIS_MODULE,
0366     .poll = softsynth_poll,
0367     .read = softsynth_read,
0368     .write = softsynth_write,
0369     .open = softsynth_open,
0370     .release = softsynth_close,
0371 };
0372 
0373 static const struct file_operations softsynthu_fops = {
0374     .owner = THIS_MODULE,
0375     .poll = softsynth_poll,
0376     .read = softsynthu_read,
0377     .write = softsynth_write,
0378     .open = softsynth_open,
0379     .release = softsynth_close,
0380 };
0381 
0382 static int softsynth_probe(struct spk_synth *synth)
0383 {
0384     if (misc_registered != 0)
0385         return 0;
0386     memset(&synth_device, 0, sizeof(synth_device));
0387     synth_device.minor = MISC_DYNAMIC_MINOR;
0388     synth_device.name = "softsynth";
0389     synth_device.fops = &softsynth_fops;
0390     if (misc_register(&synth_device)) {
0391         pr_warn("Couldn't initialize miscdevice /dev/softsynth.\n");
0392         return -ENODEV;
0393     }
0394 
0395     memset(&synthu_device, 0, sizeof(synthu_device));
0396     synthu_device.minor = MISC_DYNAMIC_MINOR;
0397     synthu_device.name = "softsynthu";
0398     synthu_device.fops = &softsynthu_fops;
0399     if (misc_register(&synthu_device)) {
0400         misc_deregister(&synth_device);
0401         pr_warn("Couldn't initialize miscdevice /dev/softsynthu.\n");
0402         return -ENODEV;
0403     }
0404 
0405     misc_registered = 1;
0406     pr_info("initialized device: /dev/softsynth, node (MAJOR 10, MINOR %d)\n",
0407         synth_device.minor);
0408     pr_info("initialized device: /dev/softsynthu, node (MAJOR 10, MINOR %d)\n",
0409         synthu_device.minor);
0410     return 0;
0411 }
0412 
0413 static void softsynth_release(struct spk_synth *synth)
0414 {
0415     misc_deregister(&synth_device);
0416     misc_deregister(&synthu_device);
0417     misc_registered = 0;
0418     pr_info("unregistered /dev/softsynth\n");
0419     pr_info("unregistered /dev/softsynthu\n");
0420 }
0421 
0422 static int softsynth_is_alive(struct spk_synth *synth)
0423 {
0424     if (synth_soft.alive)
0425         return 1;
0426     return 0;
0427 }
0428 
0429 module_param_named(start, synth_soft.startup, short, 0444);
0430 
0431 MODULE_PARM_DESC(start, "Start the synthesizer once it is loaded.");
0432 
0433 module_spk_synth(synth_soft);
0434 
0435 MODULE_AUTHOR("Kirk Reiser <kirk@braille.uwo.ca>");
0436 MODULE_DESCRIPTION("Speakup userspace software synthesizer support");
0437 MODULE_LICENSE("GPL");
0438 MODULE_VERSION(DRV_VERSION);