Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
at v4.16 481 lines 12 kB view raw
1// SPDX-License-Identifier: GPL-2.0 2#include <linux/types.h> 3#include <linux/ctype.h> /* for isdigit() and friends */ 4#include <linux/fs.h> 5#include <linux/mm.h> /* for verify_area */ 6#include <linux/errno.h> /* for -EBUSY */ 7#include <linux/ioport.h> /* for check_region, request_region */ 8#include <linux/interrupt.h> 9#include <linux/delay.h> /* for loops_per_sec */ 10#include <linux/kmod.h> 11#include <linux/jiffies.h> 12#include <linux/uaccess.h> /* for copy_from_user */ 13#include <linux/sched.h> 14#include <linux/timer.h> 15#include <linux/kthread.h> 16 17#include "spk_priv.h" 18#include "speakup.h" 19#include "serialio.h" 20 21#define MAXSYNTHS 16 /* Max number of synths in array. */ 22static struct spk_synth *synths[MAXSYNTHS + 1]; 23struct spk_synth *synth; 24char spk_pitch_buff[32] = ""; 25static int module_status; 26bool spk_quiet_boot; 27 28struct speakup_info_t speakup_info = { 29 /* 30 * This spinlock is used to protect the entire speakup machinery, and 31 * must be taken at each kernel->speakup transition and released at 32 * each corresponding speakup->kernel transition. 33 * 34 * The progression thread only interferes with the speakup machinery 35 * through the synth buffer, so only needs to take the lock 36 * while tinkering with the buffer. 37 * 38 * We use spin_lock/trylock_irqsave and spin_unlock_irqrestore with this 39 * spinlock because speakup needs to disable the keyboard IRQ. 40 */ 41 .spinlock = __SPIN_LOCK_UNLOCKED(speakup_info.spinlock), 42 .flushing = 0, 43}; 44EXPORT_SYMBOL_GPL(speakup_info); 45 46static int do_synth_init(struct spk_synth *in_synth); 47 48/* 49 * Main loop of the progression thread: keep eating from the buffer 50 * and push to the serial port, waiting as needed 51 * 52 * For devices that have a "full" notification mechanism, the driver can 53 * adapt the loop the way they prefer. 54 */ 55void spk_do_catch_up(struct spk_synth *synth) 56{ 57 u_char ch; 58 unsigned long flags; 59 unsigned long jiff_max; 60 struct var_t *delay_time; 61 struct var_t *full_time; 62 struct var_t *jiffy_delta; 63 int jiffy_delta_val; 64 int delay_time_val; 65 int full_time_val; 66 67 jiffy_delta = spk_get_var(JIFFY); 68 full_time = spk_get_var(FULL); 69 delay_time = spk_get_var(DELAY); 70 71 spin_lock_irqsave(&speakup_info.spinlock, flags); 72 jiffy_delta_val = jiffy_delta->u.n.value; 73 spin_unlock_irqrestore(&speakup_info.spinlock, flags); 74 75 jiff_max = jiffies + jiffy_delta_val; 76 while (!kthread_should_stop()) { 77 spin_lock_irqsave(&speakup_info.spinlock, flags); 78 if (speakup_info.flushing) { 79 speakup_info.flushing = 0; 80 spin_unlock_irqrestore(&speakup_info.spinlock, flags); 81 synth->flush(synth); 82 continue; 83 } 84 synth_buffer_skip_nonlatin1(); 85 if (synth_buffer_empty()) { 86 spin_unlock_irqrestore(&speakup_info.spinlock, flags); 87 break; 88 } 89 ch = synth_buffer_peek(); 90 set_current_state(TASK_INTERRUPTIBLE); 91 full_time_val = full_time->u.n.value; 92 spin_unlock_irqrestore(&speakup_info.spinlock, flags); 93 if (ch == '\n') 94 ch = synth->procspeech; 95 if (!synth->io_ops->synth_out(synth, ch)) { 96 schedule_timeout(msecs_to_jiffies(full_time_val)); 97 continue; 98 } 99 if (time_after_eq(jiffies, jiff_max) && (ch == SPACE)) { 100 spin_lock_irqsave(&speakup_info.spinlock, flags); 101 jiffy_delta_val = jiffy_delta->u.n.value; 102 delay_time_val = delay_time->u.n.value; 103 full_time_val = full_time->u.n.value; 104 spin_unlock_irqrestore(&speakup_info.spinlock, flags); 105 if (synth->io_ops->synth_out(synth, synth->procspeech)) 106 schedule_timeout( 107 msecs_to_jiffies(delay_time_val)); 108 else 109 schedule_timeout( 110 msecs_to_jiffies(full_time_val)); 111 jiff_max = jiffies + jiffy_delta_val; 112 } 113 set_current_state(TASK_RUNNING); 114 spin_lock_irqsave(&speakup_info.spinlock, flags); 115 synth_buffer_getc(); 116 spin_unlock_irqrestore(&speakup_info.spinlock, flags); 117 } 118 synth->io_ops->synth_out(synth, synth->procspeech); 119} 120EXPORT_SYMBOL_GPL(spk_do_catch_up); 121 122void spk_synth_flush(struct spk_synth *synth) 123{ 124 synth->io_ops->flush_buffer(); 125 synth->io_ops->synth_out(synth, synth->clear); 126} 127EXPORT_SYMBOL_GPL(spk_synth_flush); 128 129unsigned char spk_synth_get_index(struct spk_synth *synth) 130{ 131 return synth->io_ops->synth_in_nowait(); 132} 133EXPORT_SYMBOL_GPL(spk_synth_get_index); 134 135int spk_synth_is_alive_nop(struct spk_synth *synth) 136{ 137 synth->alive = 1; 138 return 1; 139} 140EXPORT_SYMBOL_GPL(spk_synth_is_alive_nop); 141 142int spk_synth_is_alive_restart(struct spk_synth *synth) 143{ 144 if (synth->alive) 145 return 1; 146 if (spk_wait_for_xmitr(synth) > 0) { 147 /* restart */ 148 synth->alive = 1; 149 synth_printf("%s", synth->init); 150 return 2; /* reenabled */ 151 } 152 pr_warn("%s: can't restart synth\n", synth->long_name); 153 return 0; 154} 155EXPORT_SYMBOL_GPL(spk_synth_is_alive_restart); 156 157static void thread_wake_up(struct timer_list *unused) 158{ 159 wake_up_interruptible_all(&speakup_event); 160} 161 162static DEFINE_TIMER(thread_timer, thread_wake_up); 163 164void synth_start(void) 165{ 166 struct var_t *trigger_time; 167 168 if (!synth->alive) { 169 synth_buffer_clear(); 170 return; 171 } 172 trigger_time = spk_get_var(TRIGGER); 173 if (!timer_pending(&thread_timer)) 174 mod_timer(&thread_timer, jiffies + 175 msecs_to_jiffies(trigger_time->u.n.value)); 176} 177 178void spk_do_flush(void) 179{ 180 if (!synth) 181 return; 182 183 speakup_info.flushing = 1; 184 synth_buffer_clear(); 185 if (synth->alive) { 186 if (spk_pitch_shift) { 187 synth_printf("%s", spk_pitch_buff); 188 spk_pitch_shift = 0; 189 } 190 } 191 wake_up_interruptible_all(&speakup_event); 192 wake_up_process(speakup_task); 193} 194 195void synth_write(const char *buf, size_t count) 196{ 197 while (count--) 198 synth_buffer_add(*buf++); 199 synth_start(); 200} 201 202void synth_printf(const char *fmt, ...) 203{ 204 va_list args; 205 unsigned char buf[160], *p; 206 int r; 207 208 va_start(args, fmt); 209 r = vsnprintf(buf, sizeof(buf), fmt, args); 210 va_end(args); 211 if (r > sizeof(buf) - 1) 212 r = sizeof(buf) - 1; 213 214 p = buf; 215 while (r--) 216 synth_buffer_add(*p++); 217 synth_start(); 218} 219EXPORT_SYMBOL_GPL(synth_printf); 220 221void synth_putwc(u16 wc) 222{ 223 synth_buffer_add(wc); 224} 225EXPORT_SYMBOL_GPL(synth_putwc); 226 227void synth_putwc_s(u16 wc) 228{ 229 synth_buffer_add(wc); 230 synth_start(); 231} 232EXPORT_SYMBOL_GPL(synth_putwc_s); 233 234void synth_putws(const u16 *buf) 235{ 236 const u16 *p; 237 238 for (p = buf; *p; p++) 239 synth_buffer_add(*p); 240} 241EXPORT_SYMBOL_GPL(synth_putws); 242 243void synth_putws_s(const u16 *buf) 244{ 245 synth_putws(buf); 246 synth_start(); 247} 248EXPORT_SYMBOL_GPL(synth_putws_s); 249 250static int index_count; 251static int sentence_count; 252 253void spk_reset_index_count(int sc) 254{ 255 static int first = 1; 256 257 if (first) 258 first = 0; 259 else 260 synth->get_index(synth); 261 index_count = 0; 262 sentence_count = sc; 263} 264 265int synth_supports_indexing(void) 266{ 267 if (synth->get_index) 268 return 1; 269 return 0; 270} 271 272void synth_insert_next_index(int sent_num) 273{ 274 int out; 275 276 if (synth->alive) { 277 if (sent_num == 0) { 278 synth->indexing.currindex++; 279 index_count++; 280 if (synth->indexing.currindex > 281 synth->indexing.highindex) 282 synth->indexing.currindex = 283 synth->indexing.lowindex; 284 } 285 286 out = synth->indexing.currindex * 10 + sent_num; 287 synth_printf(synth->indexing.command, out, out); 288 } 289} 290 291void spk_get_index_count(int *linecount, int *sentcount) 292{ 293 int ind = synth->get_index(synth); 294 295 if (ind) { 296 sentence_count = ind % 10; 297 298 if ((ind / 10) <= synth->indexing.currindex) 299 index_count = synth->indexing.currindex - (ind / 10); 300 else 301 index_count = synth->indexing.currindex 302 - synth->indexing.lowindex 303 + synth->indexing.highindex - (ind / 10) + 1; 304 } 305 *sentcount = sentence_count; 306 *linecount = index_count; 307} 308 309static struct resource synth_res; 310 311int synth_request_region(unsigned long start, unsigned long n) 312{ 313 struct resource *parent = &ioport_resource; 314 315 memset(&synth_res, 0, sizeof(synth_res)); 316 synth_res.name = synth->name; 317 synth_res.start = start; 318 synth_res.end = start + n - 1; 319 synth_res.flags = IORESOURCE_BUSY; 320 return request_resource(parent, &synth_res); 321} 322EXPORT_SYMBOL_GPL(synth_request_region); 323 324int synth_release_region(unsigned long start, unsigned long n) 325{ 326 return release_resource(&synth_res); 327} 328EXPORT_SYMBOL_GPL(synth_release_region); 329 330struct var_t synth_time_vars[] = { 331 { DELAY, .u.n = {NULL, 100, 100, 2000, 0, 0, NULL } }, 332 { TRIGGER, .u.n = {NULL, 20, 10, 2000, 0, 0, NULL } }, 333 { JIFFY, .u.n = {NULL, 50, 20, 200, 0, 0, NULL } }, 334 { FULL, .u.n = {NULL, 400, 200, 60000, 0, 0, NULL } }, 335 V_LAST_VAR 336}; 337 338/* called by: speakup_init() */ 339int synth_init(char *synth_name) 340{ 341 int i; 342 int ret = 0; 343 struct spk_synth *synth = NULL; 344 345 if (!synth_name) 346 return 0; 347 348 if (strcmp(synth_name, "none") == 0) { 349 mutex_lock(&spk_mutex); 350 synth_release(); 351 mutex_unlock(&spk_mutex); 352 return 0; 353 } 354 355 mutex_lock(&spk_mutex); 356 /* First, check if we already have it loaded. */ 357 for (i = 0; i < MAXSYNTHS && synths[i]; i++) 358 if (strcmp(synths[i]->name, synth_name) == 0) 359 synth = synths[i]; 360 361 /* If we got one, initialize it now. */ 362 if (synth) 363 ret = do_synth_init(synth); 364 else 365 ret = -ENODEV; 366 mutex_unlock(&spk_mutex); 367 368 return ret; 369} 370 371/* called by: synth_add() */ 372static int do_synth_init(struct spk_synth *in_synth) 373{ 374 struct var_t *var; 375 376 synth_release(); 377 if (in_synth->checkval != SYNTH_CHECK) 378 return -EINVAL; 379 synth = in_synth; 380 synth->alive = 0; 381 pr_warn("synth probe\n"); 382 if (synth->probe(synth) < 0) { 383 pr_warn("%s: device probe failed\n", in_synth->name); 384 synth = NULL; 385 return -ENODEV; 386 } 387 synth_time_vars[0].u.n.value = 388 synth_time_vars[0].u.n.default_val = synth->delay; 389 synth_time_vars[1].u.n.value = 390 synth_time_vars[1].u.n.default_val = synth->trigger; 391 synth_time_vars[2].u.n.value = 392 synth_time_vars[2].u.n.default_val = synth->jiffies; 393 synth_time_vars[3].u.n.value = 394 synth_time_vars[3].u.n.default_val = synth->full; 395 synth_printf("%s", synth->init); 396 for (var = synth->vars; 397 (var->var_id >= 0) && (var->var_id < MAXVARS); var++) 398 speakup_register_var(var); 399 if (!spk_quiet_boot) 400 synth_printf("%s found\n", synth->long_name); 401 if (synth->attributes.name && 402 sysfs_create_group(speakup_kobj, &synth->attributes) < 0) 403 return -ENOMEM; 404 synth_flags = synth->flags; 405 wake_up_interruptible_all(&speakup_event); 406 if (speakup_task) 407 wake_up_process(speakup_task); 408 return 0; 409} 410 411void synth_release(void) 412{ 413 struct var_t *var; 414 unsigned long flags; 415 416 if (!synth) 417 return; 418 spin_lock_irqsave(&speakup_info.spinlock, flags); 419 pr_info("releasing synth %s\n", synth->name); 420 synth->alive = 0; 421 del_timer(&thread_timer); 422 spin_unlock_irqrestore(&speakup_info.spinlock, flags); 423 if (synth->attributes.name) 424 sysfs_remove_group(speakup_kobj, &synth->attributes); 425 for (var = synth->vars; var->var_id != MAXVARS; var++) 426 speakup_unregister_var(var->var_id); 427 synth->release(); 428 synth = NULL; 429} 430 431/* called by: all_driver_init() */ 432int synth_add(struct spk_synth *in_synth) 433{ 434 int i; 435 int status = 0; 436 437 mutex_lock(&spk_mutex); 438 for (i = 0; i < MAXSYNTHS && synths[i]; i++) 439 /* synth_remove() is responsible for rotating the array down */ 440 if (in_synth == synths[i]) { 441 mutex_unlock(&spk_mutex); 442 return 0; 443 } 444 if (i == MAXSYNTHS) { 445 pr_warn("Error: attempting to add a synth past end of array\n"); 446 mutex_unlock(&spk_mutex); 447 return -1; 448 } 449 450 if (in_synth->startup) 451 status = do_synth_init(in_synth); 452 453 if (!status) { 454 synths[i++] = in_synth; 455 synths[i] = NULL; 456 } 457 458 mutex_unlock(&spk_mutex); 459 return status; 460} 461EXPORT_SYMBOL_GPL(synth_add); 462 463void synth_remove(struct spk_synth *in_synth) 464{ 465 int i; 466 467 mutex_lock(&spk_mutex); 468 if (synth == in_synth) 469 synth_release(); 470 for (i = 0; synths[i]; i++) { 471 if (in_synth == synths[i]) 472 break; 473 } 474 for ( ; synths[i]; i++) /* compress table */ 475 synths[i] = synths[i + 1]; 476 module_status = 0; 477 mutex_unlock(&spk_mutex); 478} 479EXPORT_SYMBOL_GPL(synth_remove); 480 481short spk_punc_masks[] = { 0, SOME, MOST, PUNC, PUNC | B_SYM };