synth.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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. while (count--)
  193. synth_buffer_add(*buf++);
  194. synth_start();
  195. }
  196. void synth_printf(const char *fmt, ...)
  197. {
  198. va_list args;
  199. unsigned char buf[160], *p;
  200. int r;
  201. va_start(args, fmt);
  202. r = vsnprintf(buf, sizeof(buf), fmt, args);
  203. va_end(args);
  204. if (r > sizeof(buf) - 1)
  205. r = sizeof(buf) - 1;
  206. p = buf;
  207. while (r--)
  208. synth_buffer_add(*p++);
  209. synth_start();
  210. }
  211. EXPORT_SYMBOL_GPL(synth_printf);
  212. void synth_putwc(u16 wc)
  213. {
  214. synth_buffer_add(wc);
  215. }
  216. EXPORT_SYMBOL_GPL(synth_putwc);
  217. void synth_putwc_s(u16 wc)
  218. {
  219. synth_buffer_add(wc);
  220. synth_start();
  221. }
  222. EXPORT_SYMBOL_GPL(synth_putwc_s);
  223. void synth_putws(const u16 *buf)
  224. {
  225. const u16 *p;
  226. for (p = buf; *p; p++)
  227. synth_buffer_add(*p);
  228. }
  229. EXPORT_SYMBOL_GPL(synth_putws);
  230. void synth_putws_s(const u16 *buf)
  231. {
  232. synth_putws(buf);
  233. synth_start();
  234. }
  235. EXPORT_SYMBOL_GPL(synth_putws_s);
  236. static int index_count;
  237. static int sentence_count;
  238. void spk_reset_index_count(int sc)
  239. {
  240. static int first = 1;
  241. if (first)
  242. first = 0;
  243. else
  244. synth->get_index(synth);
  245. index_count = 0;
  246. sentence_count = sc;
  247. }
  248. int synth_supports_indexing(void)
  249. {
  250. if (synth->get_index)
  251. return 1;
  252. return 0;
  253. }
  254. void synth_insert_next_index(int sent_num)
  255. {
  256. int out;
  257. if (synth->alive) {
  258. if (sent_num == 0) {
  259. synth->indexing.currindex++;
  260. index_count++;
  261. if (synth->indexing.currindex >
  262. synth->indexing.highindex)
  263. synth->indexing.currindex =
  264. synth->indexing.lowindex;
  265. }
  266. out = synth->indexing.currindex * 10 + sent_num;
  267. synth_printf(synth->indexing.command, out, out);
  268. }
  269. }
  270. void spk_get_index_count(int *linecount, int *sentcount)
  271. {
  272. int ind = synth->get_index(synth);
  273. if (ind) {
  274. sentence_count = ind % 10;
  275. if ((ind / 10) <= synth->indexing.currindex)
  276. index_count = synth->indexing.currindex - (ind / 10);
  277. else
  278. index_count = synth->indexing.currindex
  279. - synth->indexing.lowindex
  280. + synth->indexing.highindex - (ind / 10) + 1;
  281. }
  282. *sentcount = sentence_count;
  283. *linecount = index_count;
  284. }
  285. static struct resource synth_res;
  286. int synth_request_region(unsigned long start, unsigned long n)
  287. {
  288. struct resource *parent = &ioport_resource;
  289. memset(&synth_res, 0, sizeof(synth_res));
  290. synth_res.name = synth->name;
  291. synth_res.start = start;
  292. synth_res.end = start + n - 1;
  293. synth_res.flags = IORESOURCE_BUSY;
  294. return request_resource(parent, &synth_res);
  295. }
  296. EXPORT_SYMBOL_GPL(synth_request_region);
  297. int synth_release_region(unsigned long start, unsigned long n)
  298. {
  299. return release_resource(&synth_res);
  300. }
  301. EXPORT_SYMBOL_GPL(synth_release_region);
  302. struct var_t synth_time_vars[] = {
  303. { DELAY, .u.n = {NULL, 100, 100, 2000, 0, 0, NULL } },
  304. { TRIGGER, .u.n = {NULL, 20, 10, 2000, 0, 0, NULL } },
  305. { JIFFY, .u.n = {NULL, 50, 20, 200, 0, 0, NULL } },
  306. { FULL, .u.n = {NULL, 400, 200, 60000, 0, 0, NULL } },
  307. { FLUSH, .u.n = {NULL, 4000, 10, 4000, 0, 0, NULL } },
  308. V_LAST_VAR
  309. };
  310. /* called by: speakup_init() */
  311. int synth_init(char *synth_name)
  312. {
  313. int ret = 0;
  314. struct spk_synth *tmp, *synth = NULL;
  315. if (!synth_name)
  316. return 0;
  317. if (strcmp(synth_name, "none") == 0) {
  318. mutex_lock(&spk_mutex);
  319. synth_release();
  320. mutex_unlock(&spk_mutex);
  321. return 0;
  322. }
  323. mutex_lock(&spk_mutex);
  324. /* First, check if we already have it loaded. */
  325. list_for_each_entry(tmp, &synths, node) {
  326. if (strcmp(tmp->name, synth_name) == 0)
  327. synth = tmp;
  328. }
  329. /* If we got one, initialize it now. */
  330. if (synth)
  331. ret = do_synth_init(synth);
  332. else
  333. ret = -ENODEV;
  334. mutex_unlock(&spk_mutex);
  335. return ret;
  336. }
  337. /* called by: synth_add() */
  338. static int do_synth_init(struct spk_synth *in_synth)
  339. {
  340. struct var_t *var;
  341. synth_release();
  342. if (in_synth->checkval != SYNTH_CHECK)
  343. return -EINVAL;
  344. synth = in_synth;
  345. synth->alive = 0;
  346. pr_warn("synth probe\n");
  347. if (synth->probe(synth) < 0) {
  348. pr_warn("%s: device probe failed\n", in_synth->name);
  349. synth = NULL;
  350. return -ENODEV;
  351. }
  352. synth_time_vars[0].u.n.value =
  353. synth_time_vars[0].u.n.default_val = synth->delay;
  354. synth_time_vars[1].u.n.value =
  355. synth_time_vars[1].u.n.default_val = synth->trigger;
  356. synth_time_vars[2].u.n.value =
  357. synth_time_vars[2].u.n.default_val = synth->jiffies;
  358. synth_time_vars[3].u.n.value =
  359. synth_time_vars[3].u.n.default_val = synth->full;
  360. synth_time_vars[4].u.n.value =
  361. synth_time_vars[4].u.n.default_val = synth->flush_time;
  362. synth_printf("%s", synth->init);
  363. for (var = synth->vars;
  364. (var->var_id >= 0) && (var->var_id < MAXVARS); var++)
  365. speakup_register_var(var);
  366. if (!spk_quiet_boot)
  367. synth_printf("%s found\n", synth->long_name);
  368. if (synth->attributes.name &&
  369. sysfs_create_group(speakup_kobj, &synth->attributes) < 0)
  370. return -ENOMEM;
  371. synth_flags = synth->flags;
  372. wake_up_interruptible_all(&speakup_event);
  373. if (speakup_task)
  374. wake_up_process(speakup_task);
  375. return 0;
  376. }
  377. void synth_release(void)
  378. {
  379. struct var_t *var;
  380. unsigned long flags;
  381. if (!synth)
  382. return;
  383. spin_lock_irqsave(&speakup_info.spinlock, flags);
  384. pr_info("releasing synth %s\n", synth->name);
  385. synth->alive = 0;
  386. del_timer(&thread_timer);
  387. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  388. if (synth->attributes.name)
  389. sysfs_remove_group(speakup_kobj, &synth->attributes);
  390. for (var = synth->vars; var->var_id != MAXVARS; var++)
  391. speakup_unregister_var(var->var_id);
  392. synth->release(synth);
  393. synth = NULL;
  394. }
  395. /* called by: all_driver_init() */
  396. int synth_add(struct spk_synth *in_synth)
  397. {
  398. int status = 0;
  399. struct spk_synth *tmp;
  400. mutex_lock(&spk_mutex);
  401. list_for_each_entry(tmp, &synths, node) {
  402. if (tmp == in_synth) {
  403. mutex_unlock(&spk_mutex);
  404. return 0;
  405. }
  406. }
  407. if (in_synth->startup)
  408. status = do_synth_init(in_synth);
  409. if (!status)
  410. list_add_tail(&in_synth->node, &synths);
  411. mutex_unlock(&spk_mutex);
  412. return status;
  413. }
  414. EXPORT_SYMBOL_GPL(synth_add);
  415. void synth_remove(struct spk_synth *in_synth)
  416. {
  417. mutex_lock(&spk_mutex);
  418. if (synth == in_synth)
  419. synth_release();
  420. list_del(&in_synth->node);
  421. module_status = 0;
  422. mutex_unlock(&spk_mutex);
  423. }
  424. EXPORT_SYMBOL_GPL(synth_remove);
  425. struct spk_synth *synth_current(void)
  426. {
  427. return synth;
  428. }
  429. EXPORT_SYMBOL_GPL(synth_current);
  430. short spk_punc_masks[] = { 0, SOME, MOST, PUNC, PUNC | B_SYM };