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