Path: blob/master/drivers/accessibility/speakup/speakup_decpc.c
26282 views
// SPDX-License-Identifier: GPL-2.0+1/*2* This is the DECtalk PC speakup driver3*4* Some constants from DEC's DOS driver:5* Copyright (c) by Digital Equipment Corp.6*7* 386BSD DECtalk PC driver:8* Copyright (c) 1996 Brian Buhrow <[email protected]>9*10* Linux DECtalk PC driver:11* Copyright (c) 1997 Nicolas Pitre <[email protected]>12*13* speakup DECtalk PC Internal driver:14* Copyright (c) 2003 David Borowski <[email protected]>15*16* All rights reserved.17*/18#include <linux/jiffies.h>19#include <linux/sched.h>20#include <linux/timer.h>21#include <linux/kthread.h>2223#include "spk_priv.h"24#include "speakup.h"2526#define MODULE_init 0x0dec /* module in boot code */27#define MODULE_self_test 0x8800 /* module in self-test */28#define MODULE_reset 0xffff /* reinit the whole module */2930#define MODE_mask 0xf000 /* mode bits in high nibble */31#define MODE_null 0x000032#define MODE_test 0x2000 /* in testing mode */33#define MODE_status 0x800034#define STAT_int 0x0001 /* running in interrupt mode */35#define STAT_tr_char 0x0002 /* character data to transmit */36#define STAT_rr_char 0x0004 /* ready to receive char data */37#define STAT_cmd_ready 0x0008 /* ready to accept commands */38#define STAT_dma_ready 0x0010 /* dma command ready */39#define STAT_digitized 0x0020 /* spc in digitized mode */40#define STAT_new_index 0x0040 /* new last index ready */41#define STAT_new_status 0x0080 /* new status posted */42#define STAT_dma_state 0x0100 /* dma state toggle */43#define STAT_index_valid 0x0200 /* indexs are valid */44#define STAT_flushing 0x0400 /* flush in progress */45#define STAT_self_test 0x0800 /* module in self test */46#define MODE_ready 0xc000 /* module ready for next phase */47#define READY_boot 0x000048#define READY_kernel 0x000149#define MODE_error 0xf0005051#define CMD_mask 0xf000 /* mask for command nibble */52#define CMD_null 0x0000 /* post status */53#define CMD_control 0x1000 /* hard control command */54#define CTRL_mask 0x0F00 /* mask off control nibble */55#define CTRL_data 0x00FF /* mask to get data byte */56#define CTRL_null 0x0000 /* null control */57#define CTRL_vol_up 0x0100 /* increase volume */58#define CTRL_vol_down 0x0200 /* decrease volume */59#define CTRL_vol_set 0x0300 /* set volume */60#define CTRL_pause 0x0400 /* pause spc */61#define CTRL_resume 0x0500 /* resume spc clock */62#define CTRL_resume_spc 0x0001 /* resume spc soft pause */63#define CTRL_flush 0x0600 /* flush all buffers */64#define CTRL_int_enable 0x0700 /* enable status change ints */65#define CTRL_buff_free 0x0800 /* buffer remain count */66#define CTRL_buff_used 0x0900 /* buffer in use */67#define CTRL_speech 0x0a00 /* immediate speech change */68#define CTRL_SP_voice 0x0001 /* voice change */69#define CTRL_SP_rate 0x0002 /* rate change */70#define CTRL_SP_comma 0x0003 /* comma pause change */71#define CTRL_SP_period 0x0004 /* period pause change */72#define CTRL_SP_rate_delta 0x0005 /* delta rate change */73#define CTRL_SP_get_param 0x0006 /* return the desired parameter */74#define CTRL_last_index 0x0b00 /* get last index spoken */75#define CTRL_io_priority 0x0c00 /* change i/o priority */76#define CTRL_free_mem 0x0d00 /* get free paragraphs on module */77#define CTRL_get_lang 0x0e00 /* return bitmask of loaded languages */78#define CMD_test 0x2000 /* self-test request */79#define TEST_mask 0x0F00 /* isolate test field */80#define TEST_null 0x0000 /* no test requested */81#define TEST_isa_int 0x0100 /* assert isa irq */82#define TEST_echo 0x0200 /* make data in == data out */83#define TEST_seg 0x0300 /* set peek/poke segment */84#define TEST_off 0x0400 /* set peek/poke offset */85#define TEST_peek 0x0500 /* data out == *peek */86#define TEST_poke 0x0600 /* *peek == data in */87#define TEST_sub_code 0x00FF /* user defined test sub codes */88#define CMD_id 0x3000 /* return software id */89#define ID_null 0x0000 /* null id */90#define ID_kernel 0x0100 /* kernel code executing */91#define ID_boot 0x0200 /* boot code executing */92#define CMD_dma 0x4000 /* force a dma start */93#define CMD_reset 0x5000 /* reset module status */94#define CMD_sync 0x6000 /* kernel sync command */95#define CMD_char_in 0x7000 /* single character send */96#define CMD_char_out 0x8000 /* single character get */97#define CHAR_count_1 0x0100 /* one char in cmd_low */98#define CHAR_count_2 0x0200 /* the second in data_low */99#define CHAR_count_3 0x0300 /* the third in data_high */100#define CMD_spc_mode 0x9000 /* change spc mode */101#define CMD_spc_to_text 0x0100 /* set to text mode */102#define CMD_spc_to_digit 0x0200 /* set to digital mode */103#define CMD_spc_rate 0x0400 /* change spc data rate */104#define CMD_error 0xf000 /* severe error */105106enum { PRIMARY_DIC = 0, USER_DIC, COMMAND_DIC, ABBREV_DIC };107108#define DMA_single_in 0x01109#define DMA_single_out 0x02110#define DMA_buff_in 0x03111#define DMA_buff_out 0x04112#define DMA_control 0x05113#define DT_MEM_ALLOC 0x03114#define DT_SET_DIC 0x04115#define DT_START_TASK 0x05116#define DT_LOAD_MEM 0x06117#define DT_READ_MEM 0x07118#define DT_DIGITAL_IN 0x08119#define DMA_sync 0x06120#define DMA_sync_char 0x07121122#define DRV_VERSION "2.12"123#define PROCSPEECH 0x0b124#define SYNTH_IO_EXTENT 8125126static int synth_probe(struct spk_synth *synth);127static void dtpc_release(struct spk_synth *synth);128static const char *synth_immediate(struct spk_synth *synth, const char *buf);129static void do_catch_up(struct spk_synth *synth);130static void synth_flush(struct spk_synth *synth);131132static int synth_portlist[] = { 0x340, 0x350, 0x240, 0x250, 0 };133static int in_escape, is_flushing;134static int dt_stat, dma_state;135136137enum default_vars_id {138CAPS_START_ID = 0, CAPS_STOP_ID,139RATE_ID, PITCH_ID, INFLECTION_ID,140VOL_ID, PUNCT_ID, VOICE_ID,141DIRECT_ID, V_LAST_VAR_ID,142NB_ID,143};144145146147static struct var_t vars[NB_ID] = {148[CAPS_START_ID] = { CAPS_START, .u.s = {"[:dv ap 200]" } },149[CAPS_STOP_ID] = { CAPS_STOP, .u.s = {"[:dv ap 100]" } },150[RATE_ID] = { RATE, .u.n = {"[:ra %d]", 9, 0, 18, 150, 25, NULL } },151[PITCH_ID] = { PITCH, .u.n = {"[:dv ap %d]", 80, 0, 100, 20, 0, NULL } },152[INFLECTION_ID] = { INFLECTION, .u.n = {"[:dv pr %d] ", 100, 0, 10000, 0, 0, NULL } },153[VOL_ID] = { VOL, .u.n = {"[:vo se %d]", 5, 0, 9, 5, 10, NULL } },154[PUNCT_ID] = { PUNCT, .u.n = {"[:pu %c]", 0, 0, 2, 0, 0, "nsa" } },155[VOICE_ID] = { VOICE, .u.n = {"[:n%c]", 0, 0, 9, 0, 0, "phfdburwkv" } },156[DIRECT_ID] = { DIRECT, .u.n = {NULL, 0, 0, 1, 0, 0, NULL } },157V_LAST_VAR158};159160/*161* These attributes will appear in /sys/accessibility/speakup/decpc.162*/163static struct kobj_attribute caps_start_attribute =164__ATTR(caps_start, 0644, spk_var_show, spk_var_store);165static struct kobj_attribute caps_stop_attribute =166__ATTR(caps_stop, 0644, spk_var_show, spk_var_store);167static struct kobj_attribute pitch_attribute =168__ATTR(pitch, 0644, spk_var_show, spk_var_store);169static struct kobj_attribute inflection_attribute =170__ATTR(inflection, 0644, spk_var_show, spk_var_store);171static struct kobj_attribute punct_attribute =172__ATTR(punct, 0644, spk_var_show, spk_var_store);173static struct kobj_attribute rate_attribute =174__ATTR(rate, 0644, spk_var_show, spk_var_store);175static struct kobj_attribute voice_attribute =176__ATTR(voice, 0644, spk_var_show, spk_var_store);177static struct kobj_attribute vol_attribute =178__ATTR(vol, 0644, spk_var_show, spk_var_store);179180static struct kobj_attribute delay_time_attribute =181__ATTR(delay_time, 0644, spk_var_show, spk_var_store);182static struct kobj_attribute direct_attribute =183__ATTR(direct, 0644, spk_var_show, spk_var_store);184static struct kobj_attribute full_time_attribute =185__ATTR(full_time, 0644, spk_var_show, spk_var_store);186static struct kobj_attribute jiffy_delta_attribute =187__ATTR(jiffy_delta, 0644, spk_var_show, spk_var_store);188static struct kobj_attribute trigger_time_attribute =189__ATTR(trigger_time, 0644, spk_var_show, spk_var_store);190191/*192* Create a group of attributes so that we can create and destroy them all193* at once.194*/195static struct attribute *synth_attrs[] = {196&caps_start_attribute.attr,197&caps_stop_attribute.attr,198&pitch_attribute.attr,199&inflection_attribute.attr,200&punct_attribute.attr,201&rate_attribute.attr,202&voice_attribute.attr,203&vol_attribute.attr,204&delay_time_attribute.attr,205&direct_attribute.attr,206&full_time_attribute.attr,207&jiffy_delta_attribute.attr,208&trigger_time_attribute.attr,209NULL, /* need to NULL terminate the list of attributes */210};211212static struct spk_synth synth_dec_pc = {213.name = "decpc",214.version = DRV_VERSION,215.long_name = "Dectalk PC",216.init = "[:pe -380]",217.procspeech = PROCSPEECH,218.delay = 500,219.trigger = 50,220.jiffies = 50,221.full = 1000,222.flags = SF_DEC,223.startup = SYNTH_START,224.checkval = SYNTH_CHECK,225.vars = vars,226.io_ops = &spk_serial_io_ops,227.probe = synth_probe,228.release = dtpc_release,229.synth_immediate = synth_immediate,230.catch_up = do_catch_up,231.flush = synth_flush,232.is_alive = spk_synth_is_alive_nop,233.synth_adjust = NULL,234.read_buff_add = NULL,235.get_index = NULL,236.indexing = {237.command = NULL,238.lowindex = 0,239.highindex = 0,240.currindex = 0,241},242.attributes = {243.attrs = synth_attrs,244.name = "decpc",245},246};247248static int dt_getstatus(void)249{250dt_stat = inb_p(speakup_info.port_tts) |251(inb_p(speakup_info.port_tts + 1) << 8);252return dt_stat;253}254255static void dt_sendcmd(u_int cmd)256{257outb_p(cmd & 0xFF, speakup_info.port_tts);258outb_p((cmd >> 8) & 0xFF, speakup_info.port_tts + 1);259}260261static int dt_waitbit(int bit)262{263int timeout = 100;264265while (--timeout > 0) {266if ((dt_getstatus() & bit) == bit)267return 1;268udelay(50);269}270return 0;271}272273static int dt_wait_dma(void)274{275int timeout = 100, state = dma_state;276277if (!dt_waitbit(STAT_dma_ready))278return 0;279while (--timeout > 0) {280if ((dt_getstatus() & STAT_dma_state) == state)281return 1;282udelay(50);283}284dma_state = dt_getstatus() & STAT_dma_state;285return 1;286}287288static int dt_ctrl(u_int cmd)289{290int timeout = 10;291292if (!dt_waitbit(STAT_cmd_ready))293return -1;294outb_p(0, speakup_info.port_tts + 2);295outb_p(0, speakup_info.port_tts + 3);296dt_getstatus();297dt_sendcmd(CMD_control | cmd);298outb_p(0, speakup_info.port_tts + 6);299while (dt_getstatus() & STAT_cmd_ready) {300udelay(20);301if (--timeout == 0)302break;303}304dt_sendcmd(CMD_null);305return 0;306}307308static void synth_flush(struct spk_synth *synth)309{310int timeout = 10;311312if (is_flushing)313return;314is_flushing = 4;315in_escape = 0;316while (dt_ctrl(CTRL_flush)) {317if (--timeout == 0)318break;319udelay(50);320}321for (timeout = 0; timeout < 10; timeout++) {322if (dt_waitbit(STAT_dma_ready))323break;324udelay(50);325}326outb_p(DMA_sync, speakup_info.port_tts + 4);327outb_p(0, speakup_info.port_tts + 4);328udelay(100);329for (timeout = 0; timeout < 10; timeout++) {330if (!(dt_getstatus() & STAT_flushing))331break;332udelay(50);333}334dma_state = dt_getstatus() & STAT_dma_state;335dma_state ^= STAT_dma_state;336is_flushing = 0;337}338339static int dt_sendchar(char ch)340{341if (!dt_wait_dma())342return -1;343if (!(dt_stat & STAT_rr_char))344return -2;345outb_p(DMA_single_in, speakup_info.port_tts + 4);346outb_p(ch, speakup_info.port_tts + 4);347dma_state ^= STAT_dma_state;348return 0;349}350351static int testkernel(void)352{353int status = 0;354355if (dt_getstatus() == 0xffff) {356status = -1;357goto oops;358}359dt_sendcmd(CMD_sync);360if (!dt_waitbit(STAT_cmd_ready))361status = -2;362else if (dt_stat & 0x8000)363return 0;364else if (dt_stat == 0x0dec)365pr_warn("dec_pc at 0x%x, software not loaded\n",366speakup_info.port_tts);367status = -3;368oops: synth_release_region(speakup_info.port_tts, SYNTH_IO_EXTENT);369speakup_info.port_tts = 0;370return status;371}372373static void do_catch_up(struct spk_synth *synth)374{375u_char ch;376static u_char last;377unsigned long flags;378unsigned long jiff_max;379struct var_t *jiffy_delta;380struct var_t *delay_time;381int jiffy_delta_val;382int delay_time_val;383384jiffy_delta = spk_get_var(JIFFY);385delay_time = spk_get_var(DELAY);386spin_lock_irqsave(&speakup_info.spinlock, flags);387jiffy_delta_val = jiffy_delta->u.n.value;388spin_unlock_irqrestore(&speakup_info.spinlock, flags);389jiff_max = jiffies + jiffy_delta_val;390391while (!kthread_should_stop()) {392spin_lock_irqsave(&speakup_info.spinlock, flags);393if (speakup_info.flushing) {394speakup_info.flushing = 0;395spin_unlock_irqrestore(&speakup_info.spinlock, flags);396synth->flush(synth);397continue;398}399synth_buffer_skip_nonlatin1();400if (synth_buffer_empty()) {401spin_unlock_irqrestore(&speakup_info.spinlock, flags);402break;403}404ch = synth_buffer_peek();405set_current_state(TASK_INTERRUPTIBLE);406delay_time_val = delay_time->u.n.value;407spin_unlock_irqrestore(&speakup_info.spinlock, flags);408if (ch == '\n')409ch = 0x0D;410if (dt_sendchar(ch)) {411schedule_timeout(msecs_to_jiffies(delay_time_val));412continue;413}414set_current_state(TASK_RUNNING);415spin_lock_irqsave(&speakup_info.spinlock, flags);416synth_buffer_getc();417spin_unlock_irqrestore(&speakup_info.spinlock, flags);418if (ch == '[') {419in_escape = 1;420} else if (ch == ']') {421in_escape = 0;422} else if (ch <= SPACE) {423if (!in_escape && strchr(",.!?;:", last))424dt_sendchar(PROCSPEECH);425if (time_after_eq(jiffies, jiff_max)) {426if (!in_escape)427dt_sendchar(PROCSPEECH);428spin_lock_irqsave(&speakup_info.spinlock,429flags);430jiffy_delta_val = jiffy_delta->u.n.value;431delay_time_val = delay_time->u.n.value;432spin_unlock_irqrestore(&speakup_info.spinlock,433flags);434schedule_timeout(msecs_to_jiffies435(delay_time_val));436jiff_max = jiffies + jiffy_delta_val;437}438}439last = ch;440ch = 0;441}442if (!in_escape)443dt_sendchar(PROCSPEECH);444}445446static const char *synth_immediate(struct spk_synth *synth, const char *buf)447{448u_char ch;449450while ((ch = *buf)) {451if (ch == '\n')452ch = PROCSPEECH;453if (dt_sendchar(ch))454return buf;455buf++;456}457return NULL;458}459460static int synth_probe(struct spk_synth *synth)461{462int i = 0, failed = 0;463464pr_info("Probing for %s.\n", synth->long_name);465for (i = 0; synth_portlist[i]; i++) {466if (synth_request_region(synth_portlist[i], SYNTH_IO_EXTENT)) {467pr_warn("request_region: failed with 0x%x, %d\n",468synth_portlist[i], SYNTH_IO_EXTENT);469continue;470}471speakup_info.port_tts = synth_portlist[i];472failed = testkernel();473if (failed == 0)474break;475}476if (failed) {477pr_info("%s: not found\n", synth->long_name);478return -ENODEV;479}480pr_info("%s: %03x-%03x, Driver Version %s,\n", synth->long_name,481speakup_info.port_tts, speakup_info.port_tts + 7,482synth->version);483synth->alive = 1;484return 0;485}486487static void dtpc_release(struct spk_synth *synth)488{489spk_stop_serial_interrupt();490if (speakup_info.port_tts)491synth_release_region(speakup_info.port_tts, SYNTH_IO_EXTENT);492speakup_info.port_tts = 0;493}494495module_param_named(start, synth_dec_pc.startup, short, 0444);496module_param_named(rate, vars[RATE_ID].u.n.default_val, int, 0444);497module_param_named(pitch, vars[PITCH_ID].u.n.default_val, int, 0444);498module_param_named(inflection, vars[INFLECTION_ID].u.n.default_val, int, 0444);499module_param_named(vol, vars[VOL_ID].u.n.default_val, int, 0444);500module_param_named(punct, vars[PUNCT_ID].u.n.default_val, int, 0444);501module_param_named(voice, vars[VOICE_ID].u.n.default_val, int, 0444);502module_param_named(direct, vars[DIRECT_ID].u.n.default_val, int, 0444);503504505506507MODULE_PARM_DESC(start, "Start the synthesizer once it is loaded.");508MODULE_PARM_DESC(rate, "Set the rate variable on load.");509MODULE_PARM_DESC(pitch, "Set the pitch variable on load.");510MODULE_PARM_DESC(inflection, "Set the inflection variable on load.");511MODULE_PARM_DESC(vol, "Set the vol variable on load.");512MODULE_PARM_DESC(punct, "Set the punct variable on load.");513MODULE_PARM_DESC(voice, "Set the voice variable on load.");514MODULE_PARM_DESC(direct, "Set the direct variable on load.");515516module_spk_synth(synth_dec_pc);517518MODULE_AUTHOR("Kirk Reiser <[email protected]>");519MODULE_AUTHOR("David Borowski");520MODULE_DESCRIPTION("Speakup support for DECtalk PC synthesizers");521MODULE_LICENSE("GPL");522MODULE_VERSION(DRV_VERSION);523524525