linux/drivers/staging/speakup/speakup_soft.c
<<
>>
Prefs
   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 specificly 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 SYNTH_MINOR */
  14#include <linux/poll.h>         /* for poll_wait() */
  15#include <linux/sched/signal.h> /* schedule(), signal_pending(), TASK_INTERRUPTIBLE */
  16
  17#include "spk_priv.h"
  18#include "speakup.h"
  19
  20#define DRV_VERSION "2.6"
  21#define SOFTSYNTH_MINOR 26 /* might as well give it one more than /dev/synth */
  22#define SOFTSYNTHU_MINOR 27 /* might as well give it one more than /dev/synth */
  23#define PROCSPEECH 0x0d
  24#define CLEAR_SYNTH 0x18
  25
  26static int softsynth_probe(struct spk_synth *synth);
  27static void softsynth_release(void);
  28static int softsynth_is_alive(struct spk_synth *synth);
  29static unsigned char get_index(struct spk_synth *synth);
  30
  31static struct miscdevice synth_device, synthu_device;
  32static int init_pos;
  33static int misc_registered;
  34
  35static struct var_t vars[] = {
  36        { CAPS_START, .u.s = {"\x01+3p" } },
  37        { CAPS_STOP, .u.s = {"\x01-3p" } },
  38        { RATE, .u.n = {"\x01%ds", 2, 0, 9, 0, 0, NULL } },
  39        { PITCH, .u.n = {"\x01%dp", 5, 0, 9, 0, 0, NULL } },
  40        { VOL, .u.n = {"\x01%dv", 5, 0, 9, 0, 0, NULL } },
  41        { TONE, .u.n = {"\x01%dx", 1, 0, 2, 0, 0, NULL } },
  42        { PUNCT, .u.n = {"\x01%db", 0, 0, 2, 0, 0, NULL } },
  43        { VOICE, .u.n = {"\x01%do", 0, 0, 7, 0, 0, NULL } },
  44        { FREQUENCY, .u.n = {"\x01%df", 5, 0, 9, 0, 0, NULL } },
  45        { DIRECT, .u.n = {NULL, 0, 0, 1, 0, 0, NULL } },
  46        V_LAST_VAR
  47};
  48
  49/* These attributes will appear in /sys/accessibility/speakup/soft. */
  50
  51static struct kobj_attribute caps_start_attribute =
  52        __ATTR(caps_start, 0644, spk_var_show, spk_var_store);
  53static struct kobj_attribute caps_stop_attribute =
  54        __ATTR(caps_stop, 0644, spk_var_show, spk_var_store);
  55static struct kobj_attribute freq_attribute =
  56        __ATTR(freq, 0644, spk_var_show, spk_var_store);
  57static struct kobj_attribute pitch_attribute =
  58        __ATTR(pitch, 0644, spk_var_show, spk_var_store);
  59static struct kobj_attribute punct_attribute =
  60        __ATTR(punct, 0644, spk_var_show, spk_var_store);
  61static struct kobj_attribute rate_attribute =
  62        __ATTR(rate, 0644, spk_var_show, spk_var_store);
  63static struct kobj_attribute tone_attribute =
  64        __ATTR(tone, 0644, spk_var_show, spk_var_store);
  65static struct kobj_attribute voice_attribute =
  66        __ATTR(voice, 0644, spk_var_show, spk_var_store);
  67static struct kobj_attribute vol_attribute =
  68        __ATTR(vol, 0644, spk_var_show, spk_var_store);
  69
  70/*
  71 * We should uncomment the following definition, when we agree on a
  72 * method of passing a language designation to the software synthesizer.
  73 * static struct kobj_attribute lang_attribute =
  74 *      __ATTR(lang, 0644, spk_var_show, spk_var_store);
  75 */
  76
  77static struct kobj_attribute delay_time_attribute =
  78        __ATTR(delay_time, 0644, spk_var_show, spk_var_store);
  79static struct kobj_attribute direct_attribute =
  80        __ATTR(direct, 0644, spk_var_show, spk_var_store);
  81static struct kobj_attribute full_time_attribute =
  82        __ATTR(full_time, 0644, spk_var_show, spk_var_store);
  83static struct kobj_attribute jiffy_delta_attribute =
  84        __ATTR(jiffy_delta, 0644, spk_var_show, spk_var_store);
  85static struct kobj_attribute trigger_time_attribute =
  86        __ATTR(trigger_time, 0644, spk_var_show, spk_var_store);
  87
  88/*
  89 * Create a group of attributes so that we can create and destroy them all
  90 * at once.
  91 */
  92static struct attribute *synth_attrs[] = {
  93        &caps_start_attribute.attr,
  94        &caps_stop_attribute.attr,
  95        &freq_attribute.attr,
  96/*      &lang_attribute.attr, */
  97        &pitch_attribute.attr,
  98        &punct_attribute.attr,
  99        &rate_attribute.attr,
 100        &tone_attribute.attr,
 101        &voice_attribute.attr,
 102        &vol_attribute.attr,
 103        &delay_time_attribute.attr,
 104        &direct_attribute.attr,
 105        &full_time_attribute.attr,
 106        &jiffy_delta_attribute.attr,
 107        &trigger_time_attribute.attr,
 108        NULL,   /* need to NULL terminate the list of attributes */
 109};
 110
 111static struct spk_synth synth_soft = {
 112        .name = "soft",
 113        .version = DRV_VERSION,
 114        .long_name = "software synth",
 115        .init = "\01@\x01\x31y\n",
 116        .procspeech = PROCSPEECH,
 117        .delay = 0,
 118        .trigger = 0,
 119        .jiffies = 0,
 120        .full = 0,
 121        .startup = SYNTH_START,
 122        .checkval = SYNTH_CHECK,
 123        .vars = vars,
 124        .io_ops = NULL,
 125        .probe = softsynth_probe,
 126        .release = softsynth_release,
 127        .synth_immediate = NULL,
 128        .catch_up = NULL,
 129        .flush = NULL,
 130        .is_alive = softsynth_is_alive,
 131        .synth_adjust = NULL,
 132        .read_buff_add = NULL,
 133        .get_index = get_index,
 134        .indexing = {
 135                .command = "\x01%di",
 136                .lowindex = 1,
 137                .highindex = 5,
 138                .currindex = 1,
 139        },
 140        .attributes = {
 141                .attrs = synth_attrs,
 142                .name = "soft",
 143        },
 144};
 145
 146static char *get_initstring(void)
 147{
 148        static char buf[40];
 149        char *cp;
 150        struct var_t *var;
 151
 152        memset(buf, 0, sizeof(buf));
 153        cp = buf;
 154        var = synth_soft.vars;
 155        while (var->var_id != MAXVARS) {
 156                if (var->var_id != CAPS_START && var->var_id != CAPS_STOP &&
 157                    var->var_id != DIRECT)
 158                        cp = cp + sprintf(cp, var->u.n.synth_fmt,
 159                                          var->u.n.value);
 160                var++;
 161        }
 162        cp = cp + sprintf(cp, "\n");
 163        return buf;
 164}
 165
 166static int softsynth_open(struct inode *inode, struct file *fp)
 167{
 168        unsigned long flags;
 169        /*if ((fp->f_flags & O_ACCMODE) != O_RDONLY) */
 170        /*      return -EPERM; */
 171        spin_lock_irqsave(&speakup_info.spinlock, flags);
 172        if (synth_soft.alive) {
 173                spin_unlock_irqrestore(&speakup_info.spinlock, flags);
 174                return -EBUSY;
 175        }
 176        synth_soft.alive = 1;
 177        spin_unlock_irqrestore(&speakup_info.spinlock, flags);
 178        return 0;
 179}
 180
 181static int softsynth_close(struct inode *inode, struct file *fp)
 182{
 183        unsigned long flags;
 184
 185        spin_lock_irqsave(&speakup_info.spinlock, flags);
 186        synth_soft.alive = 0;
 187        init_pos = 0;
 188        spin_unlock_irqrestore(&speakup_info.spinlock, flags);
 189        /* Make sure we let applications go before leaving */
 190        speakup_start_ttys();
 191        return 0;
 192}
 193
 194static ssize_t softsynthx_read(struct file *fp, char __user *buf, size_t count,
 195                               loff_t *pos, int unicode)
 196{
 197        int chars_sent = 0;
 198        char __user *cp;
 199        char *init;
 200        u16 ch;
 201        int empty;
 202        unsigned long flags;
 203        DEFINE_WAIT(wait);
 204
 205        spin_lock_irqsave(&speakup_info.spinlock, flags);
 206        while (1) {
 207                prepare_to_wait(&speakup_event, &wait, TASK_INTERRUPTIBLE);
 208                if (!unicode)
 209                        synth_buffer_skip_nonlatin1();
 210                if (!synth_buffer_empty() || speakup_info.flushing)
 211                        break;
 212                spin_unlock_irqrestore(&speakup_info.spinlock, flags);
 213                if (fp->f_flags & O_NONBLOCK) {
 214                        finish_wait(&speakup_event, &wait);
 215                        return -EAGAIN;
 216                }
 217                if (signal_pending(current)) {
 218                        finish_wait(&speakup_event, &wait);
 219                        return -ERESTARTSYS;
 220                }
 221                schedule();
 222                spin_lock_irqsave(&speakup_info.spinlock, flags);
 223        }
 224        finish_wait(&speakup_event, &wait);
 225
 226        cp = buf;
 227        init = get_initstring();
 228
 229        /* Keep 3 bytes available for a 16bit UTF-8-encoded character */
 230        while (chars_sent <= count - 3) {
 231                if (speakup_info.flushing) {
 232                        speakup_info.flushing = 0;
 233                        ch = '\x18';
 234                } else if (init[init_pos]) {
 235                        ch = init[init_pos++];
 236                } else {
 237                        if (!unicode)
 238                                synth_buffer_skip_nonlatin1();
 239                        if (synth_buffer_empty())
 240                                break;
 241                        ch = synth_buffer_getc();
 242                }
 243                spin_unlock_irqrestore(&speakup_info.spinlock, flags);
 244
 245                if ((!unicode && ch < 0x100) || (unicode && ch < 0x80)) {
 246                        u_char c = ch;
 247
 248                        if (copy_to_user(cp, &c, 1))
 249                                return -EFAULT;
 250
 251                        chars_sent++;
 252                        cp++;
 253                } else if (unicode && ch < 0x800) {
 254                        u_char s[2] = {
 255                                0xc0 | (ch >> 6),
 256                                0x80 | (ch & 0x3f)
 257                        };
 258
 259                        if (copy_to_user(cp, s, sizeof(s)))
 260                                return -EFAULT;
 261
 262                        chars_sent += sizeof(s);
 263                        cp += sizeof(s);
 264                } else if (unicode) {
 265                        u_char s[3] = {
 266                                0xe0 | (ch >> 12),
 267                                0x80 | ((ch >> 6) & 0x3f),
 268                                0x80 | (ch & 0x3f)
 269                        };
 270
 271                        if (copy_to_user(cp, s, sizeof(s)))
 272                                return -EFAULT;
 273
 274                        chars_sent += sizeof(s);
 275                        cp += sizeof(s);
 276                }
 277
 278                spin_lock_irqsave(&speakup_info.spinlock, flags);
 279        }
 280        *pos += chars_sent;
 281        empty = synth_buffer_empty();
 282        spin_unlock_irqrestore(&speakup_info.spinlock, flags);
 283        if (empty) {
 284                speakup_start_ttys();
 285                *pos = 0;
 286        }
 287        return chars_sent;
 288}
 289
 290static ssize_t softsynth_read(struct file *fp, char __user *buf, size_t count,
 291                              loff_t *pos)
 292{
 293        return softsynthx_read(fp, buf, count, pos, 0);
 294}
 295
 296static ssize_t softsynthu_read(struct file *fp, char __user *buf, size_t count,
 297                               loff_t *pos)
 298{
 299        return softsynthx_read(fp, buf, count, pos, 1);
 300}
 301
 302static int last_index;
 303
 304static ssize_t softsynth_write(struct file *fp, const char __user *buf,
 305                               size_t count, loff_t *pos)
 306{
 307        unsigned long supplied_index = 0;
 308        int converted;
 309
 310        converted = kstrtoul_from_user(buf, count, 0, &supplied_index);
 311
 312        if (converted < 0)
 313                return converted;
 314
 315        last_index = supplied_index;
 316        return count;
 317}
 318
 319static __poll_t softsynth_poll(struct file *fp, struct poll_table_struct *wait)
 320{
 321        unsigned long flags;
 322        __poll_t ret = 0;
 323
 324        poll_wait(fp, &speakup_event, wait);
 325
 326        spin_lock_irqsave(&speakup_info.spinlock, flags);
 327        if (!synth_buffer_empty() || speakup_info.flushing)
 328                ret = EPOLLIN | EPOLLRDNORM;
 329        spin_unlock_irqrestore(&speakup_info.spinlock, flags);
 330        return ret;
 331}
 332
 333static unsigned char get_index(struct spk_synth *synth)
 334{
 335        int rv;
 336
 337        rv = last_index;
 338        last_index = 0;
 339        return rv;
 340}
 341
 342static const struct file_operations softsynth_fops = {
 343        .owner = THIS_MODULE,
 344        .poll = softsynth_poll,
 345        .read = softsynth_read,
 346        .write = softsynth_write,
 347        .open = softsynth_open,
 348        .release = softsynth_close,
 349};
 350
 351static const struct file_operations softsynthu_fops = {
 352        .owner = THIS_MODULE,
 353        .poll = softsynth_poll,
 354        .read = softsynthu_read,
 355        .write = softsynth_write,
 356        .open = softsynth_open,
 357        .release = softsynth_close,
 358};
 359
 360static int softsynth_probe(struct spk_synth *synth)
 361{
 362        if (misc_registered != 0)
 363                return 0;
 364        memset(&synth_device, 0, sizeof(synth_device));
 365        synth_device.minor = SOFTSYNTH_MINOR;
 366        synth_device.name = "softsynth";
 367        synth_device.fops = &softsynth_fops;
 368        if (misc_register(&synth_device)) {
 369                pr_warn("Couldn't initialize miscdevice /dev/softsynth.\n");
 370                return -ENODEV;
 371        }
 372
 373        memset(&synthu_device, 0, sizeof(synthu_device));
 374        synthu_device.minor = SOFTSYNTHU_MINOR;
 375        synthu_device.name = "softsynthu";
 376        synthu_device.fops = &softsynthu_fops;
 377        if (misc_register(&synthu_device)) {
 378                pr_warn("Couldn't initialize miscdevice /dev/softsynth.\n");
 379                return -ENODEV;
 380        }
 381
 382        misc_registered = 1;
 383        pr_info("initialized device: /dev/softsynth, node (MAJOR 10, MINOR 26)\n");
 384        pr_info("initialized device: /dev/softsynthu, node (MAJOR 10, MINOR 27)\n");
 385        return 0;
 386}
 387
 388static void softsynth_release(void)
 389{
 390        misc_deregister(&synth_device);
 391        misc_deregister(&synthu_device);
 392        misc_registered = 0;
 393        pr_info("unregistered /dev/softsynth\n");
 394        pr_info("unregistered /dev/softsynthu\n");
 395}
 396
 397static int softsynth_is_alive(struct spk_synth *synth)
 398{
 399        if (synth_soft.alive)
 400                return 1;
 401        return 0;
 402}
 403
 404module_param_named(start, synth_soft.startup, short, 0444);
 405
 406MODULE_PARM_DESC(start, "Start the synthesizer once it is loaded.");
 407
 408module_spk_synth(synth_soft);
 409
 410MODULE_AUTHOR("Kirk Reiser <kirk@braille.uwo.ca>");
 411MODULE_DESCRIPTION("Speakup userspace software synthesizer support");
 412MODULE_LICENSE("GPL");
 413MODULE_VERSION(DRV_VERSION);
 414