Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
at v4.17 498 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 */ 55static void _spk_do_catch_up(struct spk_synth *synth, int unicode) 56{ 57 u16 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 int ret; 67 68 jiffy_delta = spk_get_var(JIFFY); 69 full_time = spk_get_var(FULL); 70 delay_time = spk_get_var(DELAY); 71 72 spin_lock_irqsave(&speakup_info.spinlock, flags); 73 jiffy_delta_val = jiffy_delta->u.n.value; 74 spin_unlock_irqrestore(&speakup_info.spinlock, flags); 75 76 jiff_max = jiffies + jiffy_delta_val; 77 while (!kthread_should_stop()) { 78 spin_lock_irqsave(&speakup_info.spinlock, flags); 79 if (speakup_info.flushing) { 80 speakup_info.flushing = 0; 81 spin_unlock_irqrestore(&speakup_info.spinlock, flags); 82 synth->flush(synth); 83 continue; 84 } 85 if (!unicode) 86 synth_buffer_skip_nonlatin1(); 87 if (synth_buffer_empty()) { 88 spin_unlock_irqrestore(&speakup_info.spinlock, flags); 89 break; 90 } 91 ch = synth_buffer_peek(); 92 set_current_state(TASK_INTERRUPTIBLE); 93 full_time_val = full_time->u.n.value; 94 spin_unlock_irqrestore(&speakup_info.spinlock, flags); 95 if (ch == '\n') 96 ch = synth->procspeech; 97 if (unicode) 98 ret = synth->io_ops->synth_out_unicode(synth, ch); 99 else 100 ret = synth->io_ops->synth_out(synth, ch); 101 if (!ret) { 102 schedule_timeout(msecs_to_jiffies(full_time_val)); 103 continue; 104 } 105 if (time_after_eq(jiffies, jiff_max) && (ch == SPACE)) { 106 spin_lock_irqsave(&speakup_info.spinlock, flags); 107 jiffy_delta_val = jiffy_delta->u.n.value; 108 delay_time_val = delay_time->u.n.value; 109 full_time_val = full_time->u.n.value; 110 spin_unlock_irqrestore(&speakup_info.spinlock, flags); 111 if (synth->io_ops->synth_out(synth, synth->procspeech)) 112 schedule_timeout( 113 msecs_to_jiffies(delay_time_val)); 114 else 115 schedule_timeout( 116 msecs_to_jiffies(full_time_val)); 117 jiff_max = jiffies + jiffy_delta_val; 118 } 119 set_current_state(TASK_RUNNING); 120 spin_lock_irqsave(&speakup_info.spinlock, flags); 121 synth_buffer_getc(); 122 spin_unlock_irqrestore(&speakup_info.spinlock, flags); 123 } 124 synth->io_ops->synth_out(synth, synth->procspeech); 125} 126 127void spk_do_catch_up(struct spk_synth *synth) 128{ 129 _spk_do_catch_up(synth, 0); 130} 131EXPORT_SYMBOL_GPL(spk_do_catch_up); 132 133void spk_do_catch_up_unicode(struct spk_synth *synth) 134{ 135 _spk_do_catch_up(synth, 1); 136} 137EXPORT_SYMBOL_GPL(spk_do_catch_up_unicode); 138 139void spk_synth_flush(struct spk_synth *synth) 140{ 141 synth->io_ops->flush_buffer(); 142 synth->io_ops->synth_out(synth, synth->clear); 143} 144EXPORT_SYMBOL_GPL(spk_synth_flush); 145 146unsigned char spk_synth_get_index(struct spk_synth *synth) 147{ 148 return synth->io_ops->synth_in_nowait(); 149} 150EXPORT_SYMBOL_GPL(spk_synth_get_index); 151 152int spk_synth_is_alive_nop(struct spk_synth *synth) 153{ 154 synth->alive = 1; 155 return 1; 156} 157EXPORT_SYMBOL_GPL(spk_synth_is_alive_nop); 158 159int spk_synth_is_alive_restart(struct spk_synth *synth) 160{ 161 if (synth->alive) 162 return 1; 163 if (spk_wait_for_xmitr(synth) > 0) { 164 /* restart */ 165 synth->alive = 1; 166 synth_printf("%s", synth->init); 167 return 2; /* reenabled */ 168 } 169 pr_warn("%s: can't restart synth\n", synth->long_name); 170 return 0; 171} 172EXPORT_SYMBOL_GPL(spk_synth_is_alive_restart); 173 174static void thread_wake_up(struct timer_list *unused) 175{ 176 wake_up_interruptible_all(&speakup_event); 177} 178 179static DEFINE_TIMER(thread_timer, thread_wake_up); 180 181void synth_start(void) 182{ 183 struct var_t *trigger_time; 184 185 if (!synth->alive) { 186 synth_buffer_clear(); 187 return; 188 } 189 trigger_time = spk_get_var(TRIGGER); 190 if (!timer_pending(&thread_timer)) 191 mod_timer(&thread_timer, jiffies + 192 msecs_to_jiffies(trigger_time->u.n.value)); 193} 194 195void spk_do_flush(void) 196{ 197 if (!synth) 198 return; 199 200 speakup_info.flushing = 1; 201 synth_buffer_clear(); 202 if (synth->alive) { 203 if (spk_pitch_shift) { 204 synth_printf("%s", spk_pitch_buff); 205 spk_pitch_shift = 0; 206 } 207 } 208 wake_up_interruptible_all(&speakup_event); 209 wake_up_process(speakup_task); 210} 211 212void synth_write(const char *buf, size_t count) 213{ 214 while (count--) 215 synth_buffer_add(*buf++); 216 synth_start(); 217} 218 219void synth_printf(const char *fmt, ...) 220{ 221 va_list args; 222 unsigned char buf[160], *p; 223 int r; 224 225 va_start(args, fmt); 226 r = vsnprintf(buf, sizeof(buf), fmt, args); 227 va_end(args); 228 if (r > sizeof(buf) - 1) 229 r = sizeof(buf) - 1; 230 231 p = buf; 232 while (r--) 233 synth_buffer_add(*p++); 234 synth_start(); 235} 236EXPORT_SYMBOL_GPL(synth_printf); 237 238void synth_putwc(u16 wc) 239{ 240 synth_buffer_add(wc); 241} 242EXPORT_SYMBOL_GPL(synth_putwc); 243 244void synth_putwc_s(u16 wc) 245{ 246 synth_buffer_add(wc); 247 synth_start(); 248} 249EXPORT_SYMBOL_GPL(synth_putwc_s); 250 251void synth_putws(const u16 *buf) 252{ 253 const u16 *p; 254 255 for (p = buf; *p; p++) 256 synth_buffer_add(*p); 257} 258EXPORT_SYMBOL_GPL(synth_putws); 259 260void synth_putws_s(const u16 *buf) 261{ 262 synth_putws(buf); 263 synth_start(); 264} 265EXPORT_SYMBOL_GPL(synth_putws_s); 266 267static int index_count; 268static int sentence_count; 269 270void spk_reset_index_count(int sc) 271{ 272 static int first = 1; 273 274 if (first) 275 first = 0; 276 else 277 synth->get_index(synth); 278 index_count = 0; 279 sentence_count = sc; 280} 281 282int synth_supports_indexing(void) 283{ 284 if (synth->get_index) 285 return 1; 286 return 0; 287} 288 289void synth_insert_next_index(int sent_num) 290{ 291 int out; 292 293 if (synth->alive) { 294 if (sent_num == 0) { 295 synth->indexing.currindex++; 296 index_count++; 297 if (synth->indexing.currindex > 298 synth->indexing.highindex) 299 synth->indexing.currindex = 300 synth->indexing.lowindex; 301 } 302 303 out = synth->indexing.currindex * 10 + sent_num; 304 synth_printf(synth->indexing.command, out, out); 305 } 306} 307 308void spk_get_index_count(int *linecount, int *sentcount) 309{ 310 int ind = synth->get_index(synth); 311 312 if (ind) { 313 sentence_count = ind % 10; 314 315 if ((ind / 10) <= synth->indexing.currindex) 316 index_count = synth->indexing.currindex - (ind / 10); 317 else 318 index_count = synth->indexing.currindex 319 - synth->indexing.lowindex 320 + synth->indexing.highindex - (ind / 10) + 1; 321 } 322 *sentcount = sentence_count; 323 *linecount = index_count; 324} 325 326static struct resource synth_res; 327 328int synth_request_region(unsigned long start, unsigned long n) 329{ 330 struct resource *parent = &ioport_resource; 331 332 memset(&synth_res, 0, sizeof(synth_res)); 333 synth_res.name = synth->name; 334 synth_res.start = start; 335 synth_res.end = start + n - 1; 336 synth_res.flags = IORESOURCE_BUSY; 337 return request_resource(parent, &synth_res); 338} 339EXPORT_SYMBOL_GPL(synth_request_region); 340 341int synth_release_region(unsigned long start, unsigned long n) 342{ 343 return release_resource(&synth_res); 344} 345EXPORT_SYMBOL_GPL(synth_release_region); 346 347struct var_t synth_time_vars[] = { 348 { DELAY, .u.n = {NULL, 100, 100, 2000, 0, 0, NULL } }, 349 { TRIGGER, .u.n = {NULL, 20, 10, 2000, 0, 0, NULL } }, 350 { JIFFY, .u.n = {NULL, 50, 20, 200, 0, 0, NULL } }, 351 { FULL, .u.n = {NULL, 400, 200, 60000, 0, 0, NULL } }, 352 V_LAST_VAR 353}; 354 355/* called by: speakup_init() */ 356int synth_init(char *synth_name) 357{ 358 int i; 359 int ret = 0; 360 struct spk_synth *synth = NULL; 361 362 if (!synth_name) 363 return 0; 364 365 if (strcmp(synth_name, "none") == 0) { 366 mutex_lock(&spk_mutex); 367 synth_release(); 368 mutex_unlock(&spk_mutex); 369 return 0; 370 } 371 372 mutex_lock(&spk_mutex); 373 /* First, check if we already have it loaded. */ 374 for (i = 0; i < MAXSYNTHS && synths[i]; i++) 375 if (strcmp(synths[i]->name, synth_name) == 0) 376 synth = synths[i]; 377 378 /* If we got one, initialize it now. */ 379 if (synth) 380 ret = do_synth_init(synth); 381 else 382 ret = -ENODEV; 383 mutex_unlock(&spk_mutex); 384 385 return ret; 386} 387 388/* called by: synth_add() */ 389static int do_synth_init(struct spk_synth *in_synth) 390{ 391 struct var_t *var; 392 393 synth_release(); 394 if (in_synth->checkval != SYNTH_CHECK) 395 return -EINVAL; 396 synth = in_synth; 397 synth->alive = 0; 398 pr_warn("synth probe\n"); 399 if (synth->probe(synth) < 0) { 400 pr_warn("%s: device probe failed\n", in_synth->name); 401 synth = NULL; 402 return -ENODEV; 403 } 404 synth_time_vars[0].u.n.value = 405 synth_time_vars[0].u.n.default_val = synth->delay; 406 synth_time_vars[1].u.n.value = 407 synth_time_vars[1].u.n.default_val = synth->trigger; 408 synth_time_vars[2].u.n.value = 409 synth_time_vars[2].u.n.default_val = synth->jiffies; 410 synth_time_vars[3].u.n.value = 411 synth_time_vars[3].u.n.default_val = synth->full; 412 synth_printf("%s", synth->init); 413 for (var = synth->vars; 414 (var->var_id >= 0) && (var->var_id < MAXVARS); var++) 415 speakup_register_var(var); 416 if (!spk_quiet_boot) 417 synth_printf("%s found\n", synth->long_name); 418 if (synth->attributes.name && 419 sysfs_create_group(speakup_kobj, &synth->attributes) < 0) 420 return -ENOMEM; 421 synth_flags = synth->flags; 422 wake_up_interruptible_all(&speakup_event); 423 if (speakup_task) 424 wake_up_process(speakup_task); 425 return 0; 426} 427 428void synth_release(void) 429{ 430 struct var_t *var; 431 unsigned long flags; 432 433 if (!synth) 434 return; 435 spin_lock_irqsave(&speakup_info.spinlock, flags); 436 pr_info("releasing synth %s\n", synth->name); 437 synth->alive = 0; 438 del_timer(&thread_timer); 439 spin_unlock_irqrestore(&speakup_info.spinlock, flags); 440 if (synth->attributes.name) 441 sysfs_remove_group(speakup_kobj, &synth->attributes); 442 for (var = synth->vars; var->var_id != MAXVARS; var++) 443 speakup_unregister_var(var->var_id); 444 synth->release(); 445 synth = NULL; 446} 447 448/* called by: all_driver_init() */ 449int synth_add(struct spk_synth *in_synth) 450{ 451 int i; 452 int status = 0; 453 454 mutex_lock(&spk_mutex); 455 for (i = 0; i < MAXSYNTHS && synths[i]; i++) 456 /* synth_remove() is responsible for rotating the array down */ 457 if (in_synth == synths[i]) { 458 mutex_unlock(&spk_mutex); 459 return 0; 460 } 461 if (i == MAXSYNTHS) { 462 pr_warn("Error: attempting to add a synth past end of array\n"); 463 mutex_unlock(&spk_mutex); 464 return -1; 465 } 466 467 if (in_synth->startup) 468 status = do_synth_init(in_synth); 469 470 if (!status) { 471 synths[i++] = in_synth; 472 synths[i] = NULL; 473 } 474 475 mutex_unlock(&spk_mutex); 476 return status; 477} 478EXPORT_SYMBOL_GPL(synth_add); 479 480void synth_remove(struct spk_synth *in_synth) 481{ 482 int i; 483 484 mutex_lock(&spk_mutex); 485 if (synth == in_synth) 486 synth_release(); 487 for (i = 0; synths[i]; i++) { 488 if (in_synth == synths[i]) 489 break; 490 } 491 for ( ; synths[i]; i++) /* compress table */ 492 synths[i] = synths[i + 1]; 493 module_status = 0; 494 mutex_unlock(&spk_mutex); 495} 496EXPORT_SYMBOL_GPL(synth_remove); 497 498short spk_punc_masks[] = { 0, SOME, MOST, PUNC, PUNC | B_SYM };