synth.c 13 KB

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