synth.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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. #include "spk_priv.h"
  16. #include "speakup.h"
  17. #include "serialio.h"
  18. #define MAXSYNTHS 16 /* Max number of synths in array. */
  19. static struct spk_synth *synths[MAXSYNTHS];
  20. struct spk_synth *synth;
  21. char pitch_buff[32] = "";
  22. static int module_status;
  23. int quiet_boot;
  24. struct speakup_info_t speakup_info = {
  25. .spinlock = __SPIN_LOCK_UNLOCKED(speakup_info.spinlock),
  26. .flushing = 0,
  27. };
  28. EXPORT_SYMBOL_GPL(speakup_info);
  29. static int do_synth_init(struct spk_synth *in_synth);
  30. int serial_synth_probe(struct spk_synth *synth)
  31. {
  32. struct serial_state *ser;
  33. int failed = 0;
  34. if ((synth->ser >= SPK_LO_TTY) && (synth->ser <= SPK_HI_TTY)) {
  35. ser = spk_serial_init(synth->ser);
  36. if (ser == NULL) {
  37. failed = -1;
  38. } else {
  39. outb_p(0, ser->port);
  40. mdelay(1);
  41. outb_p('\r', ser->port);
  42. }
  43. } else {
  44. failed = -1;
  45. pr_warn("ttyS%i is an invalid port\n", synth->ser);
  46. }
  47. if (failed) {
  48. pr_info("%s: not found\n", synth->long_name);
  49. return -ENODEV;
  50. }
  51. pr_info("%s: ttyS%i, Driver Version %s\n",
  52. synth->long_name, synth->ser, synth->version);
  53. synth->alive = 1;
  54. return 0;
  55. }
  56. EXPORT_SYMBOL_GPL(serial_synth_probe);
  57. /* Main loop of the progression thread: keep eating from the buffer
  58. * and push to the serial port, waiting as needed
  59. *
  60. * For devices that have a "full" notification mecanism, the driver can
  61. * adapt the loop the way they prefer.
  62. */
  63. void spk_do_catch_up(struct spk_synth *synth)
  64. {
  65. u_char ch;
  66. unsigned long flags;
  67. unsigned long jiff_max;
  68. struct var_t *delay_time;
  69. struct var_t *full_time;
  70. struct var_t *jiffy_delta;
  71. int jiffy_delta_val;
  72. int delay_time_val;
  73. int full_time_val;
  74. jiffy_delta = get_var(JIFFY);
  75. full_time = get_var(FULL);
  76. delay_time = get_var(DELAY);
  77. spk_lock(flags);
  78. jiffy_delta_val = jiffy_delta->u.n.value;
  79. spk_unlock(flags);
  80. jiff_max = jiffies + jiffy_delta_val;
  81. while (!kthread_should_stop()) {
  82. spk_lock(flags);
  83. if (speakup_info.flushing) {
  84. speakup_info.flushing = 0;
  85. spk_unlock(flags);
  86. synth->flush(synth);
  87. continue;
  88. }
  89. if (synth_buffer_empty()) {
  90. spk_unlock(flags);
  91. break;
  92. }
  93. ch = synth_buffer_peek();
  94. set_current_state(TASK_INTERRUPTIBLE);
  95. full_time_val = full_time->u.n.value;
  96. spk_unlock(flags);
  97. if (ch == '\n')
  98. ch = synth->procspeech;
  99. if (!spk_serial_out(ch)) {
  100. schedule_timeout(msecs_to_jiffies(full_time_val));
  101. continue;
  102. }
  103. if ((jiffies >= jiff_max) && (ch == SPACE)) {
  104. spk_lock(flags);
  105. jiffy_delta_val = jiffy_delta->u.n.value;
  106. delay_time_val = delay_time->u.n.value;
  107. full_time_val = full_time->u.n.value;
  108. spk_unlock(flags);
  109. if (spk_serial_out(synth->procspeech))
  110. schedule_timeout(
  111. msecs_to_jiffies(delay_time_val));
  112. else
  113. schedule_timeout(
  114. msecs_to_jiffies(full_time_val));
  115. jiff_max = jiffies + jiffy_delta_val;
  116. }
  117. set_current_state(TASK_RUNNING);
  118. spk_lock(flags);
  119. synth_buffer_getc();
  120. spk_unlock(flags);
  121. }
  122. spk_serial_out(synth->procspeech);
  123. }
  124. EXPORT_SYMBOL_GPL(spk_do_catch_up);
  125. const char *spk_synth_immediate(struct spk_synth *synth, const char *buff)
  126. {
  127. u_char ch;
  128. while ((ch = *buff)) {
  129. if (ch == '\n')
  130. ch = synth->procspeech;
  131. if (wait_for_xmitr())
  132. outb(ch, speakup_info.port_tts);
  133. else
  134. return buff;
  135. buff++;
  136. }
  137. return 0;
  138. }
  139. EXPORT_SYMBOL_GPL(spk_synth_immediate);
  140. void spk_synth_flush(struct spk_synth *synth)
  141. {
  142. spk_serial_out(synth->clear);
  143. }
  144. EXPORT_SYMBOL_GPL(spk_synth_flush);
  145. int spk_synth_is_alive_nop(struct spk_synth *synth)
  146. {
  147. synth->alive = 1;
  148. return 1;
  149. }
  150. EXPORT_SYMBOL_GPL(spk_synth_is_alive_nop);
  151. int spk_synth_is_alive_restart(struct spk_synth *synth)
  152. {
  153. if (synth->alive)
  154. return 1;
  155. if (!synth->alive && wait_for_xmitr() > 0) {
  156. /* restart */
  157. synth->alive = 1;
  158. synth_printf("%s", synth->init);
  159. return 2; /* reenabled */
  160. }
  161. pr_warn("%s: can't restart synth\n", synth->long_name);
  162. return 0;
  163. }
  164. EXPORT_SYMBOL_GPL(spk_synth_is_alive_restart);
  165. static void thread_wake_up(u_long data)
  166. {
  167. wake_up_interruptible_all(&speakup_event);
  168. }
  169. static DEFINE_TIMER(thread_timer, thread_wake_up, 0, 0);
  170. void synth_start(void)
  171. {
  172. struct var_t *trigger_time;
  173. if (!synth->alive) {
  174. synth_buffer_clear();
  175. return;
  176. }
  177. trigger_time = get_var(TRIGGER);
  178. if (!timer_pending(&thread_timer))
  179. mod_timer(&thread_timer, jiffies +
  180. msecs_to_jiffies(trigger_time->u.n.value));
  181. }
  182. void do_flush(void)
  183. {
  184. speakup_info.flushing = 1;
  185. synth_buffer_clear();
  186. if (synth->alive) {
  187. if (pitch_shift) {
  188. synth_printf("%s", pitch_buff);
  189. pitch_shift = 0;
  190. }
  191. }
  192. wake_up_interruptible_all(&speakup_event);
  193. wake_up_process(speakup_task);
  194. }
  195. void synth_write(const char *buf, size_t count)
  196. {
  197. while (count--)
  198. synth_buffer_add(*buf++);
  199. synth_start();
  200. }
  201. void synth_printf(const char *fmt, ...)
  202. {
  203. va_list args;
  204. unsigned char buf[160], *p;
  205. int r;
  206. va_start(args, fmt);
  207. r = vsnprintf(buf, sizeof(buf), fmt, args);
  208. va_end(args);
  209. if (r > sizeof(buf) - 1)
  210. r = sizeof(buf) - 1;
  211. p = buf;
  212. while (r--)
  213. synth_buffer_add(*p++);
  214. synth_start();
  215. }
  216. EXPORT_SYMBOL_GPL(synth_printf);
  217. static int index_count;
  218. static int sentence_count;
  219. void reset_index_count(int sc)
  220. {
  221. static int first = 1;
  222. if (first)
  223. first = 0;
  224. else
  225. synth->get_index();
  226. index_count = 0;
  227. sentence_count = sc;
  228. }
  229. int synth_supports_indexing(void)
  230. {
  231. if (synth->get_index != NULL)
  232. return 1;
  233. return 0;
  234. }
  235. void synth_insert_next_index(int sent_num)
  236. {
  237. int out;
  238. if (synth->alive) {
  239. if (sent_num == 0) {
  240. synth->indexing.currindex++;
  241. index_count++;
  242. if (synth->indexing.currindex >
  243. synth->indexing.highindex)
  244. synth->indexing.currindex =
  245. synth->indexing.lowindex;
  246. }
  247. out = synth->indexing.currindex * 10 + sent_num;
  248. synth_printf(synth->indexing.command, out, out);
  249. }
  250. }
  251. void get_index_count(int *linecount, int *sentcount)
  252. {
  253. int ind = synth->get_index();
  254. if (ind) {
  255. sentence_count = ind % 10;
  256. if ((ind / 10) <= synth->indexing.currindex)
  257. index_count = synth->indexing.currindex-(ind/10);
  258. else
  259. index_count = synth->indexing.currindex
  260. -synth->indexing.lowindex
  261. + synth->indexing.highindex-(ind/10)+1;
  262. }
  263. *sentcount = sentence_count;
  264. *linecount = index_count;
  265. }
  266. static struct resource synth_res;
  267. int synth_request_region(unsigned long start, unsigned long n)
  268. {
  269. struct resource *parent = &ioport_resource;
  270. memset(&synth_res, 0, sizeof(synth_res));
  271. synth_res.name = synth->name;
  272. synth_res.start = start;
  273. synth_res.end = start + n - 1;
  274. synth_res.flags = IORESOURCE_BUSY;
  275. return request_resource(parent, &synth_res);
  276. }
  277. EXPORT_SYMBOL_GPL(synth_request_region);
  278. int synth_release_region(unsigned long start, unsigned long n)
  279. {
  280. return release_resource(&synth_res);
  281. }
  282. EXPORT_SYMBOL_GPL(synth_release_region);
  283. struct var_t synth_time_vars[] = {
  284. { DELAY, .u.n = {NULL, 100, 100, 2000, 0, 0, NULL } },
  285. { TRIGGER, .u.n = {NULL, 20, 10, 2000, 0, 0, NULL } },
  286. { JIFFY, .u.n = {NULL, 50, 20, 200, 0, 0, NULL } },
  287. { FULL, .u.n = {NULL, 400, 200, 60000, 0, 0, NULL } },
  288. V_LAST_VAR
  289. };
  290. /* called by: speakup_init() */
  291. int synth_init(char *synth_name)
  292. {
  293. int i;
  294. int ret = 0;
  295. struct spk_synth *synth = NULL;
  296. if (synth_name == NULL)
  297. return 0;
  298. if (strcmp(synth_name, "none") == 0) {
  299. mutex_lock(&spk_mutex);
  300. synth_release();
  301. mutex_unlock(&spk_mutex);
  302. return 0;
  303. }
  304. mutex_lock(&spk_mutex);
  305. /* First, check if we already have it loaded. */
  306. for (i = 0; synths[i] != NULL && i < MAXSYNTHS; i++)
  307. if (strcmp(synths[i]->name, synth_name) == 0)
  308. synth = synths[i];
  309. /* If we got one, initialize it now. */
  310. if (synth)
  311. ret = do_synth_init(synth);
  312. else
  313. ret = -ENODEV;
  314. mutex_unlock(&spk_mutex);
  315. return ret;
  316. }
  317. /* called by: synth_add() */
  318. static int do_synth_init(struct spk_synth *in_synth)
  319. {
  320. struct var_t *var;
  321. synth_release();
  322. if (in_synth->checkval != SYNTH_CHECK)
  323. return -EINVAL;
  324. synth = in_synth;
  325. synth->alive = 0;
  326. pr_warn("synth probe\n");
  327. if (synth->probe(synth) < 0) {
  328. pr_warn("%s: device probe failed\n", in_synth->name);
  329. synth = NULL;
  330. return -ENODEV;
  331. }
  332. synth_time_vars[0].u.n.value =
  333. synth_time_vars[0].u.n.default_val = synth->delay;
  334. synth_time_vars[1].u.n.value =
  335. synth_time_vars[1].u.n.default_val = synth->trigger;
  336. synth_time_vars[2].u.n.value =
  337. synth_time_vars[2].u.n.default_val = synth->jiffies;
  338. synth_time_vars[3].u.n.value =
  339. synth_time_vars[3].u.n.default_val = synth->full;
  340. synth_printf("%s", synth->init);
  341. for (var = synth->vars;
  342. (var->var_id >= 0) && (var->var_id < MAXVARS); var++)
  343. speakup_register_var(var);
  344. if (!quiet_boot)
  345. synth_printf("%s found\n", synth->long_name);
  346. if (synth->attributes.name
  347. && sysfs_create_group(speakup_kobj, &(synth->attributes)) < 0)
  348. return -ENOMEM;
  349. synth_flags = synth->flags;
  350. wake_up_interruptible_all(&speakup_event);
  351. if (speakup_task)
  352. wake_up_process(speakup_task);
  353. return 0;
  354. }
  355. void synth_release(void)
  356. {
  357. struct var_t *var;
  358. unsigned long flags;
  359. if (synth == NULL)
  360. return;
  361. spk_lock(flags);
  362. pr_info("releasing synth %s\n", synth->name);
  363. synth->alive = 0;
  364. del_timer(&thread_timer);
  365. spk_unlock(flags);
  366. if (synth->attributes.name)
  367. sysfs_remove_group(speakup_kobj, &(synth->attributes));
  368. for (var = synth->vars; var->var_id != MAXVARS; var++)
  369. speakup_unregister_var(var->var_id);
  370. stop_serial_interrupt();
  371. synth->release();
  372. synth = NULL;
  373. }
  374. /* called by: all_driver_init() */
  375. int synth_add(struct spk_synth *in_synth)
  376. {
  377. int i;
  378. int status = 0;
  379. mutex_lock(&spk_mutex);
  380. for (i = 0; synths[i] != NULL && i < MAXSYNTHS; i++)
  381. /* synth_remove() is responsible for rotating the array down */
  382. if (in_synth == synths[i]) {
  383. mutex_unlock(&spk_mutex);
  384. return 0;
  385. }
  386. if (i == MAXSYNTHS) {
  387. pr_warn("Error: attempting to add a synth past end of array\n");
  388. mutex_unlock(&spk_mutex);
  389. return -1;
  390. }
  391. synths[i++] = in_synth;
  392. synths[i] = NULL;
  393. if (in_synth->startup)
  394. status = do_synth_init(in_synth);
  395. mutex_unlock(&spk_mutex);
  396. return status;
  397. }
  398. EXPORT_SYMBOL_GPL(synth_add);
  399. void synth_remove(struct spk_synth *in_synth)
  400. {
  401. int i;
  402. mutex_lock(&spk_mutex);
  403. if (synth == in_synth)
  404. synth_release();
  405. for (i = 0; synths[i] != NULL; i++) {
  406. if (in_synth == synths[i])
  407. break;
  408. }
  409. for ( ; synths[i] != NULL; i++) /* compress table */
  410. synths[i] = synths[i+1];
  411. module_status = 0;
  412. mutex_unlock(&spk_mutex);
  413. }
  414. EXPORT_SYMBOL_GPL(synth_remove);
  415. short punc_masks[] = { 0, SOME, MOST, PUNC, PUNC|B_SYM };