162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+ 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * written by: Kirk Reiser <kirk@braille.uwo.ca> 462306a36Sopenharmony_ci * this version considerably modified by David Borowski, david575@rogers.com 562306a36Sopenharmony_ci * 662306a36Sopenharmony_ci * Copyright (C) 1998-99 Kirk Reiser. 762306a36Sopenharmony_ci * Copyright (C) 2003 David Borowski. 862306a36Sopenharmony_ci * 962306a36Sopenharmony_ci * this code is specifically written as a driver for the speakup screenreview 1062306a36Sopenharmony_ci * package and is not a general device driver. 1162306a36Sopenharmony_ci * This driver is for the Aicom Acent PC internal synthesizer. 1262306a36Sopenharmony_ci */ 1362306a36Sopenharmony_ci 1462306a36Sopenharmony_ci#include <linux/jiffies.h> 1562306a36Sopenharmony_ci#include <linux/sched.h> 1662306a36Sopenharmony_ci#include <linux/timer.h> 1762306a36Sopenharmony_ci#include <linux/kthread.h> 1862306a36Sopenharmony_ci 1962306a36Sopenharmony_ci#include "spk_priv.h" 2062306a36Sopenharmony_ci#include "serialio.h" 2162306a36Sopenharmony_ci#include "speakup.h" 2262306a36Sopenharmony_ci#include "speakup_acnt.h" /* local header file for Accent values */ 2362306a36Sopenharmony_ci 2462306a36Sopenharmony_ci#define DRV_VERSION "2.10" 2562306a36Sopenharmony_ci#define PROCSPEECH '\r' 2662306a36Sopenharmony_ci 2762306a36Sopenharmony_cistatic int synth_probe(struct spk_synth *synth); 2862306a36Sopenharmony_cistatic void accent_release(struct spk_synth *synth); 2962306a36Sopenharmony_cistatic const char *synth_immediate(struct spk_synth *synth, const char *buf); 3062306a36Sopenharmony_cistatic void do_catch_up(struct spk_synth *synth); 3162306a36Sopenharmony_cistatic void synth_flush(struct spk_synth *synth); 3262306a36Sopenharmony_ci 3362306a36Sopenharmony_cistatic int synth_port_control; 3462306a36Sopenharmony_cistatic int port_forced; 3562306a36Sopenharmony_cistatic unsigned int synth_portlist[] = { 0x2a8, 0 }; 3662306a36Sopenharmony_ci 3762306a36Sopenharmony_cienum default_vars_id { 3862306a36Sopenharmony_ci CAPS_START_ID = 0, CAPS_STOP_ID, 3962306a36Sopenharmony_ci RATE_ID, PITCH_ID, 4062306a36Sopenharmony_ci VOL_ID, TONE_ID, 4162306a36Sopenharmony_ci DIRECT_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 [CAPS_START_ID] = { CAPS_START, .u.s = {"\033P8" } }, 4862306a36Sopenharmony_ci [CAPS_STOP_ID] = { CAPS_STOP, .u.s = {"\033P5" } }, 4962306a36Sopenharmony_ci [RATE_ID] = { RATE, .u.n = {"\033R%c", 9, 0, 17, 0, 0, "0123456789abcdefgh" } }, 5062306a36Sopenharmony_ci [PITCH_ID] = { PITCH, .u.n = {"\033P%d", 5, 0, 9, 0, 0, NULL } }, 5162306a36Sopenharmony_ci [VOL_ID] = { VOL, .u.n = {"\033A%d", 5, 0, 9, 0, 0, NULL } }, 5262306a36Sopenharmony_ci [TONE_ID] = { TONE, .u.n = {"\033V%d", 5, 0, 9, 0, 0, NULL } }, 5362306a36Sopenharmony_ci [DIRECT_ID] = { DIRECT, .u.n = {NULL, 0, 0, 1, 0, 0, NULL } }, 5462306a36Sopenharmony_ci V_LAST_VAR 5562306a36Sopenharmony_ci}; 5662306a36Sopenharmony_ci 5762306a36Sopenharmony_ci/* 5862306a36Sopenharmony_ci * These attributes will appear in /sys/accessibility/speakup/acntpc. 5962306a36Sopenharmony_ci */ 6062306a36Sopenharmony_cistatic struct kobj_attribute caps_start_attribute = 6162306a36Sopenharmony_ci __ATTR(caps_start, 0644, spk_var_show, spk_var_store); 6262306a36Sopenharmony_cistatic struct kobj_attribute caps_stop_attribute = 6362306a36Sopenharmony_ci __ATTR(caps_stop, 0644, spk_var_show, spk_var_store); 6462306a36Sopenharmony_cistatic struct kobj_attribute pitch_attribute = 6562306a36Sopenharmony_ci __ATTR(pitch, 0644, spk_var_show, spk_var_store); 6662306a36Sopenharmony_cistatic struct kobj_attribute rate_attribute = 6762306a36Sopenharmony_ci __ATTR(rate, 0644, spk_var_show, spk_var_store); 6862306a36Sopenharmony_cistatic struct kobj_attribute tone_attribute = 6962306a36Sopenharmony_ci __ATTR(tone, 0644, spk_var_show, spk_var_store); 7062306a36Sopenharmony_cistatic struct kobj_attribute vol_attribute = 7162306a36Sopenharmony_ci __ATTR(vol, 0644, spk_var_show, spk_var_store); 7262306a36Sopenharmony_ci 7362306a36Sopenharmony_cistatic struct kobj_attribute delay_time_attribute = 7462306a36Sopenharmony_ci __ATTR(delay_time, 0644, spk_var_show, spk_var_store); 7562306a36Sopenharmony_cistatic struct kobj_attribute direct_attribute = 7662306a36Sopenharmony_ci __ATTR(direct, 0644, spk_var_show, spk_var_store); 7762306a36Sopenharmony_cistatic struct kobj_attribute full_time_attribute = 7862306a36Sopenharmony_ci __ATTR(full_time, 0644, spk_var_show, spk_var_store); 7962306a36Sopenharmony_cistatic struct kobj_attribute jiffy_delta_attribute = 8062306a36Sopenharmony_ci __ATTR(jiffy_delta, 0644, spk_var_show, spk_var_store); 8162306a36Sopenharmony_cistatic struct kobj_attribute trigger_time_attribute = 8262306a36Sopenharmony_ci __ATTR(trigger_time, 0644, spk_var_show, spk_var_store); 8362306a36Sopenharmony_ci 8462306a36Sopenharmony_ci/* 8562306a36Sopenharmony_ci * Create a group of attributes so that we can create and destroy them all 8662306a36Sopenharmony_ci * at once. 8762306a36Sopenharmony_ci */ 8862306a36Sopenharmony_cistatic struct attribute *synth_attrs[] = { 8962306a36Sopenharmony_ci &caps_start_attribute.attr, 9062306a36Sopenharmony_ci &caps_stop_attribute.attr, 9162306a36Sopenharmony_ci &pitch_attribute.attr, 9262306a36Sopenharmony_ci &rate_attribute.attr, 9362306a36Sopenharmony_ci &tone_attribute.attr, 9462306a36Sopenharmony_ci &vol_attribute.attr, 9562306a36Sopenharmony_ci &delay_time_attribute.attr, 9662306a36Sopenharmony_ci &direct_attribute.attr, 9762306a36Sopenharmony_ci &full_time_attribute.attr, 9862306a36Sopenharmony_ci &jiffy_delta_attribute.attr, 9962306a36Sopenharmony_ci &trigger_time_attribute.attr, 10062306a36Sopenharmony_ci NULL, /* need to NULL terminate the list of attributes */ 10162306a36Sopenharmony_ci}; 10262306a36Sopenharmony_ci 10362306a36Sopenharmony_cistatic struct spk_synth synth_acntpc = { 10462306a36Sopenharmony_ci .name = "acntpc", 10562306a36Sopenharmony_ci .version = DRV_VERSION, 10662306a36Sopenharmony_ci .long_name = "Accent PC", 10762306a36Sopenharmony_ci .init = "\033=X \033Oi\033T2\033=M\033N1\n", 10862306a36Sopenharmony_ci .procspeech = PROCSPEECH, 10962306a36Sopenharmony_ci .clear = SYNTH_CLEAR, 11062306a36Sopenharmony_ci .delay = 500, 11162306a36Sopenharmony_ci .trigger = 50, 11262306a36Sopenharmony_ci .jiffies = 50, 11362306a36Sopenharmony_ci .full = 1000, 11462306a36Sopenharmony_ci .startup = SYNTH_START, 11562306a36Sopenharmony_ci .checkval = SYNTH_CHECK, 11662306a36Sopenharmony_ci .vars = vars, 11762306a36Sopenharmony_ci .io_ops = &spk_serial_io_ops, 11862306a36Sopenharmony_ci .probe = synth_probe, 11962306a36Sopenharmony_ci .release = accent_release, 12062306a36Sopenharmony_ci .synth_immediate = synth_immediate, 12162306a36Sopenharmony_ci .catch_up = do_catch_up, 12262306a36Sopenharmony_ci .flush = synth_flush, 12362306a36Sopenharmony_ci .is_alive = spk_synth_is_alive_nop, 12462306a36Sopenharmony_ci .synth_adjust = NULL, 12562306a36Sopenharmony_ci .read_buff_add = NULL, 12662306a36Sopenharmony_ci .get_index = NULL, 12762306a36Sopenharmony_ci .indexing = { 12862306a36Sopenharmony_ci .command = NULL, 12962306a36Sopenharmony_ci .lowindex = 0, 13062306a36Sopenharmony_ci .highindex = 0, 13162306a36Sopenharmony_ci .currindex = 0, 13262306a36Sopenharmony_ci }, 13362306a36Sopenharmony_ci .attributes = { 13462306a36Sopenharmony_ci .attrs = synth_attrs, 13562306a36Sopenharmony_ci .name = "acntpc", 13662306a36Sopenharmony_ci }, 13762306a36Sopenharmony_ci}; 13862306a36Sopenharmony_ci 13962306a36Sopenharmony_cistatic inline bool synth_writable(void) 14062306a36Sopenharmony_ci{ 14162306a36Sopenharmony_ci return inb_p(synth_port_control) & SYNTH_WRITABLE; 14262306a36Sopenharmony_ci} 14362306a36Sopenharmony_ci 14462306a36Sopenharmony_cistatic inline bool synth_full(void) 14562306a36Sopenharmony_ci{ 14662306a36Sopenharmony_ci return inb_p(speakup_info.port_tts + UART_RX) == 'F'; 14762306a36Sopenharmony_ci} 14862306a36Sopenharmony_ci 14962306a36Sopenharmony_cistatic const char *synth_immediate(struct spk_synth *synth, const char *buf) 15062306a36Sopenharmony_ci{ 15162306a36Sopenharmony_ci u_char ch; 15262306a36Sopenharmony_ci 15362306a36Sopenharmony_ci while ((ch = *buf)) { 15462306a36Sopenharmony_ci int timeout = SPK_XMITR_TIMEOUT; 15562306a36Sopenharmony_ci 15662306a36Sopenharmony_ci if (ch == '\n') 15762306a36Sopenharmony_ci ch = PROCSPEECH; 15862306a36Sopenharmony_ci if (synth_full()) 15962306a36Sopenharmony_ci return buf; 16062306a36Sopenharmony_ci while (synth_writable()) { 16162306a36Sopenharmony_ci if (!--timeout) 16262306a36Sopenharmony_ci return buf; 16362306a36Sopenharmony_ci udelay(1); 16462306a36Sopenharmony_ci } 16562306a36Sopenharmony_ci outb_p(ch, speakup_info.port_tts); 16662306a36Sopenharmony_ci buf++; 16762306a36Sopenharmony_ci } 16862306a36Sopenharmony_ci return NULL; 16962306a36Sopenharmony_ci} 17062306a36Sopenharmony_ci 17162306a36Sopenharmony_cistatic void do_catch_up(struct spk_synth *synth) 17262306a36Sopenharmony_ci{ 17362306a36Sopenharmony_ci u_char ch; 17462306a36Sopenharmony_ci unsigned long flags; 17562306a36Sopenharmony_ci unsigned long jiff_max; 17662306a36Sopenharmony_ci int timeout; 17762306a36Sopenharmony_ci int delay_time_val; 17862306a36Sopenharmony_ci int jiffy_delta_val; 17962306a36Sopenharmony_ci int full_time_val; 18062306a36Sopenharmony_ci struct var_t *delay_time; 18162306a36Sopenharmony_ci struct var_t *full_time; 18262306a36Sopenharmony_ci struct var_t *jiffy_delta; 18362306a36Sopenharmony_ci 18462306a36Sopenharmony_ci jiffy_delta = spk_get_var(JIFFY); 18562306a36Sopenharmony_ci delay_time = spk_get_var(DELAY); 18662306a36Sopenharmony_ci full_time = spk_get_var(FULL); 18762306a36Sopenharmony_ci 18862306a36Sopenharmony_ci spin_lock_irqsave(&speakup_info.spinlock, flags); 18962306a36Sopenharmony_ci jiffy_delta_val = jiffy_delta->u.n.value; 19062306a36Sopenharmony_ci spin_unlock_irqrestore(&speakup_info.spinlock, flags); 19162306a36Sopenharmony_ci 19262306a36Sopenharmony_ci jiff_max = jiffies + jiffy_delta_val; 19362306a36Sopenharmony_ci while (!kthread_should_stop()) { 19462306a36Sopenharmony_ci spin_lock_irqsave(&speakup_info.spinlock, flags); 19562306a36Sopenharmony_ci if (speakup_info.flushing) { 19662306a36Sopenharmony_ci speakup_info.flushing = 0; 19762306a36Sopenharmony_ci spin_unlock_irqrestore(&speakup_info.spinlock, flags); 19862306a36Sopenharmony_ci synth->flush(synth); 19962306a36Sopenharmony_ci continue; 20062306a36Sopenharmony_ci } 20162306a36Sopenharmony_ci synth_buffer_skip_nonlatin1(); 20262306a36Sopenharmony_ci if (synth_buffer_empty()) { 20362306a36Sopenharmony_ci spin_unlock_irqrestore(&speakup_info.spinlock, flags); 20462306a36Sopenharmony_ci break; 20562306a36Sopenharmony_ci } 20662306a36Sopenharmony_ci set_current_state(TASK_INTERRUPTIBLE); 20762306a36Sopenharmony_ci full_time_val = full_time->u.n.value; 20862306a36Sopenharmony_ci spin_unlock_irqrestore(&speakup_info.spinlock, flags); 20962306a36Sopenharmony_ci if (synth_full()) { 21062306a36Sopenharmony_ci schedule_timeout(msecs_to_jiffies(full_time_val)); 21162306a36Sopenharmony_ci continue; 21262306a36Sopenharmony_ci } 21362306a36Sopenharmony_ci set_current_state(TASK_RUNNING); 21462306a36Sopenharmony_ci timeout = SPK_XMITR_TIMEOUT; 21562306a36Sopenharmony_ci while (synth_writable()) { 21662306a36Sopenharmony_ci if (!--timeout) 21762306a36Sopenharmony_ci break; 21862306a36Sopenharmony_ci udelay(1); 21962306a36Sopenharmony_ci } 22062306a36Sopenharmony_ci spin_lock_irqsave(&speakup_info.spinlock, flags); 22162306a36Sopenharmony_ci ch = synth_buffer_getc(); 22262306a36Sopenharmony_ci spin_unlock_irqrestore(&speakup_info.spinlock, flags); 22362306a36Sopenharmony_ci if (ch == '\n') 22462306a36Sopenharmony_ci ch = PROCSPEECH; 22562306a36Sopenharmony_ci outb_p(ch, speakup_info.port_tts); 22662306a36Sopenharmony_ci if (time_after_eq(jiffies, jiff_max) && ch == SPACE) { 22762306a36Sopenharmony_ci timeout = SPK_XMITR_TIMEOUT; 22862306a36Sopenharmony_ci while (synth_writable()) { 22962306a36Sopenharmony_ci if (!--timeout) 23062306a36Sopenharmony_ci break; 23162306a36Sopenharmony_ci udelay(1); 23262306a36Sopenharmony_ci } 23362306a36Sopenharmony_ci outb_p(PROCSPEECH, speakup_info.port_tts); 23462306a36Sopenharmony_ci spin_lock_irqsave(&speakup_info.spinlock, flags); 23562306a36Sopenharmony_ci jiffy_delta_val = jiffy_delta->u.n.value; 23662306a36Sopenharmony_ci delay_time_val = delay_time->u.n.value; 23762306a36Sopenharmony_ci spin_unlock_irqrestore(&speakup_info.spinlock, flags); 23862306a36Sopenharmony_ci schedule_timeout(msecs_to_jiffies(delay_time_val)); 23962306a36Sopenharmony_ci jiff_max = jiffies + jiffy_delta_val; 24062306a36Sopenharmony_ci } 24162306a36Sopenharmony_ci } 24262306a36Sopenharmony_ci timeout = SPK_XMITR_TIMEOUT; 24362306a36Sopenharmony_ci while (synth_writable()) { 24462306a36Sopenharmony_ci if (!--timeout) 24562306a36Sopenharmony_ci break; 24662306a36Sopenharmony_ci udelay(1); 24762306a36Sopenharmony_ci } 24862306a36Sopenharmony_ci outb_p(PROCSPEECH, speakup_info.port_tts); 24962306a36Sopenharmony_ci} 25062306a36Sopenharmony_ci 25162306a36Sopenharmony_cistatic void synth_flush(struct spk_synth *synth) 25262306a36Sopenharmony_ci{ 25362306a36Sopenharmony_ci outb_p(SYNTH_CLEAR, speakup_info.port_tts); 25462306a36Sopenharmony_ci} 25562306a36Sopenharmony_ci 25662306a36Sopenharmony_cistatic int synth_probe(struct spk_synth *synth) 25762306a36Sopenharmony_ci{ 25862306a36Sopenharmony_ci unsigned int port_val = 0; 25962306a36Sopenharmony_ci int i; 26062306a36Sopenharmony_ci 26162306a36Sopenharmony_ci pr_info("Probing for %s.\n", synth->long_name); 26262306a36Sopenharmony_ci if (port_forced) { 26362306a36Sopenharmony_ci speakup_info.port_tts = port_forced; 26462306a36Sopenharmony_ci pr_info("probe forced to %x by kernel command line\n", 26562306a36Sopenharmony_ci speakup_info.port_tts); 26662306a36Sopenharmony_ci if (synth_request_region(speakup_info.port_tts - 1, 26762306a36Sopenharmony_ci SYNTH_IO_EXTENT)) { 26862306a36Sopenharmony_ci pr_warn("sorry, port already reserved\n"); 26962306a36Sopenharmony_ci return -EBUSY; 27062306a36Sopenharmony_ci } 27162306a36Sopenharmony_ci port_val = inw(speakup_info.port_tts - 1); 27262306a36Sopenharmony_ci synth_port_control = speakup_info.port_tts - 1; 27362306a36Sopenharmony_ci } else { 27462306a36Sopenharmony_ci for (i = 0; synth_portlist[i]; i++) { 27562306a36Sopenharmony_ci if (synth_request_region(synth_portlist[i], 27662306a36Sopenharmony_ci SYNTH_IO_EXTENT)) { 27762306a36Sopenharmony_ci pr_warn 27862306a36Sopenharmony_ci ("request_region: failed with 0x%x, %d\n", 27962306a36Sopenharmony_ci synth_portlist[i], SYNTH_IO_EXTENT); 28062306a36Sopenharmony_ci continue; 28162306a36Sopenharmony_ci } 28262306a36Sopenharmony_ci port_val = inw(synth_portlist[i]) & 0xfffc; 28362306a36Sopenharmony_ci if (port_val == 0x53fc) { 28462306a36Sopenharmony_ci /* 'S' and out&input bits */ 28562306a36Sopenharmony_ci synth_port_control = synth_portlist[i]; 28662306a36Sopenharmony_ci speakup_info.port_tts = synth_port_control + 1; 28762306a36Sopenharmony_ci break; 28862306a36Sopenharmony_ci } 28962306a36Sopenharmony_ci } 29062306a36Sopenharmony_ci } 29162306a36Sopenharmony_ci port_val &= 0xfffc; 29262306a36Sopenharmony_ci if (port_val != 0x53fc) { 29362306a36Sopenharmony_ci /* 'S' and out&input bits */ 29462306a36Sopenharmony_ci pr_info("%s: not found\n", synth->long_name); 29562306a36Sopenharmony_ci synth_release_region(synth_port_control, SYNTH_IO_EXTENT); 29662306a36Sopenharmony_ci synth_port_control = 0; 29762306a36Sopenharmony_ci return -ENODEV; 29862306a36Sopenharmony_ci } 29962306a36Sopenharmony_ci pr_info("%s: %03x-%03x, driver version %s,\n", synth->long_name, 30062306a36Sopenharmony_ci synth_port_control, synth_port_control + SYNTH_IO_EXTENT - 1, 30162306a36Sopenharmony_ci synth->version); 30262306a36Sopenharmony_ci synth->alive = 1; 30362306a36Sopenharmony_ci return 0; 30462306a36Sopenharmony_ci} 30562306a36Sopenharmony_ci 30662306a36Sopenharmony_cistatic void accent_release(struct spk_synth *synth) 30762306a36Sopenharmony_ci{ 30862306a36Sopenharmony_ci spk_stop_serial_interrupt(); 30962306a36Sopenharmony_ci if (speakup_info.port_tts) 31062306a36Sopenharmony_ci synth_release_region(speakup_info.port_tts - 1, 31162306a36Sopenharmony_ci SYNTH_IO_EXTENT); 31262306a36Sopenharmony_ci speakup_info.port_tts = 0; 31362306a36Sopenharmony_ci} 31462306a36Sopenharmony_ci 31562306a36Sopenharmony_cimodule_param_hw_named(port, port_forced, int, ioport, 0444); 31662306a36Sopenharmony_cimodule_param_named(start, synth_acntpc.startup, short, 0444); 31762306a36Sopenharmony_cimodule_param_named(rate, vars[RATE_ID].u.n.default_val, int, 0444); 31862306a36Sopenharmony_cimodule_param_named(pitch, vars[PITCH_ID].u.n.default_val, int, 0444); 31962306a36Sopenharmony_cimodule_param_named(vol, vars[VOL_ID].u.n.default_val, int, 0444); 32062306a36Sopenharmony_cimodule_param_named(tone, vars[TONE_ID].u.n.default_val, int, 0444); 32162306a36Sopenharmony_cimodule_param_named(direct, vars[DIRECT_ID].u.n.default_val, int, 0444); 32262306a36Sopenharmony_ci 32362306a36Sopenharmony_ci 32462306a36Sopenharmony_ci 32562306a36Sopenharmony_ciMODULE_PARM_DESC(port, "Set the port for the synthesizer (override probing)."); 32662306a36Sopenharmony_ciMODULE_PARM_DESC(start, "Start the synthesizer once it is loaded."); 32762306a36Sopenharmony_ciMODULE_PARM_DESC(rate, "Set the rate variable on load."); 32862306a36Sopenharmony_ciMODULE_PARM_DESC(pitch, "Set the pitch variable on load."); 32962306a36Sopenharmony_ciMODULE_PARM_DESC(vol, "Set the vol variable on load."); 33062306a36Sopenharmony_ciMODULE_PARM_DESC(tone, "Set the tone variable on load."); 33162306a36Sopenharmony_ciMODULE_PARM_DESC(direct, "Set the direct variable on load."); 33262306a36Sopenharmony_ci 33362306a36Sopenharmony_ci 33462306a36Sopenharmony_cimodule_spk_synth(synth_acntpc); 33562306a36Sopenharmony_ci 33662306a36Sopenharmony_ciMODULE_AUTHOR("Kirk Reiser <kirk@braille.uwo.ca>"); 33762306a36Sopenharmony_ciMODULE_AUTHOR("David Borowski"); 33862306a36Sopenharmony_ciMODULE_DESCRIPTION("Speakup support for Accent PC synthesizer"); 33962306a36Sopenharmony_ciMODULE_LICENSE("GPL"); 34062306a36Sopenharmony_ciMODULE_VERSION(DRV_VERSION); 34162306a36Sopenharmony_ci 342