speakup_soft.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /* speakup_soft.c - speakup driver to register and make available
  3. * a user space device for software synthesizers. written by: Kirk
  4. * Reiser <kirk@braille.uwo.ca>
  5. *
  6. * Copyright (C) 2003 Kirk Reiser.
  7. *
  8. * this code is specifically written as a driver for the speakup screenreview
  9. * package and is not a general device driver.
  10. */
  11. #include <linux/unistd.h>
  12. #include <linux/miscdevice.h> /* for misc_register, and MISC_DYNAMIC_MINOR */
  13. #include <linux/poll.h> /* for poll_wait() */
  14. /* schedule(), signal_pending(), TASK_INTERRUPTIBLE */
  15. #include <linux/sched/signal.h>
  16. #include "spk_priv.h"
  17. #include "speakup.h"
  18. #define DRV_VERSION "2.6"
  19. #define PROCSPEECH 0x0d
  20. #define CLEAR_SYNTH 0x18
  21. static int softsynth_probe(struct spk_synth *synth);
  22. static void softsynth_release(struct spk_synth *synth);
  23. static int softsynth_is_alive(struct spk_synth *synth);
  24. static int softsynth_adjust(struct spk_synth *synth, struct st_var_header *var);
  25. static unsigned char get_index(struct spk_synth *synth);
  26. static struct miscdevice synth_device, synthu_device;
  27. static int init_pos;
  28. static int misc_registered;
  29. enum default_vars_id {
  30. DIRECT_ID = 0, CAPS_START_ID, CAPS_STOP_ID,
  31. PAUSE_ID, RATE_ID, PITCH_ID, INFLECTION_ID,
  32. VOL_ID, TONE_ID, PUNCT_ID, VOICE_ID,
  33. FREQUENCY_ID, V_LAST_VAR_ID,
  34. NB_ID
  35. };
  36. static struct var_t vars[NB_ID] = {
  37. [DIRECT_ID] = { DIRECT, .u.n = {NULL, 0, 0, 1, 0, 0, NULL } },
  38. [CAPS_START_ID] = { CAPS_START, .u.s = {"\x01+3p" } },
  39. [CAPS_STOP_ID] = { CAPS_STOP, .u.s = {"\x01-3p" } },
  40. [PAUSE_ID] = { PAUSE, .u.n = {"\x01P" } },
  41. [RATE_ID] = { RATE, .u.n = {"\x01%ds", 2, 0, 9, 0, 0, NULL } },
  42. [PITCH_ID] = { PITCH, .u.n = {"\x01%dp", 5, 0, 9, 0, 0, NULL } },
  43. [INFLECTION_ID] = { INFLECTION, .u.n = {"\x01%dr", 5, 0, 9, 0, 0, NULL } },
  44. [VOL_ID] = { VOL, .u.n = {"\x01%dv", 5, 0, 9, 0, 0, NULL } },
  45. [TONE_ID] = { TONE, .u.n = {"\x01%dx", 1, 0, 2, 0, 0, NULL } },
  46. [PUNCT_ID] = { PUNCT, .u.n = {"\x01%db", 0, 0, 3, 0, 0, NULL } },
  47. [VOICE_ID] = { VOICE, .u.n = {"\x01%do", 0, 0, 7, 0, 0, NULL } },
  48. [FREQUENCY_ID] = { FREQUENCY, .u.n = {"\x01%df", 5, 0, 9, 0, 0, NULL } },
  49. V_LAST_VAR
  50. };
  51. /* These attributes will appear in /sys/accessibility/speakup/soft. */
  52. static struct kobj_attribute caps_start_attribute =
  53. __ATTR(caps_start, 0644, spk_var_show, spk_var_store);
  54. static struct kobj_attribute caps_stop_attribute =
  55. __ATTR(caps_stop, 0644, spk_var_show, spk_var_store);
  56. static struct kobj_attribute freq_attribute =
  57. __ATTR(freq, 0644, spk_var_show, spk_var_store);
  58. static struct kobj_attribute pitch_attribute =
  59. __ATTR(pitch, 0644, spk_var_show, spk_var_store);
  60. static struct kobj_attribute inflection_attribute =
  61. __ATTR(inflection, 0644, spk_var_show, spk_var_store);
  62. static struct kobj_attribute punct_attribute =
  63. __ATTR(punct, 0644, spk_var_show, spk_var_store);
  64. static struct kobj_attribute rate_attribute =
  65. __ATTR(rate, 0644, spk_var_show, spk_var_store);
  66. static struct kobj_attribute tone_attribute =
  67. __ATTR(tone, 0644, spk_var_show, spk_var_store);
  68. static struct kobj_attribute voice_attribute =
  69. __ATTR(voice, 0644, spk_var_show, spk_var_store);
  70. static struct kobj_attribute vol_attribute =
  71. __ATTR(vol, 0644, spk_var_show, spk_var_store);
  72. /*
  73. * We should uncomment the following definition, when we agree on a
  74. * method of passing a language designation to the software synthesizer.
  75. * static struct kobj_attribute lang_attribute =
  76. * __ATTR(lang, 0644, spk_var_show, spk_var_store);
  77. */
  78. static struct kobj_attribute delay_time_attribute =
  79. __ATTR(delay_time, 0644, spk_var_show, spk_var_store);
  80. static struct kobj_attribute direct_attribute =
  81. __ATTR(direct, 0644, spk_var_show, spk_var_store);
  82. static struct kobj_attribute full_time_attribute =
  83. __ATTR(full_time, 0644, spk_var_show, spk_var_store);
  84. static struct kobj_attribute jiffy_delta_attribute =
  85. __ATTR(jiffy_delta, 0644, spk_var_show, spk_var_store);
  86. static struct kobj_attribute trigger_time_attribute =
  87. __ATTR(trigger_time, 0644, spk_var_show, spk_var_store);
  88. /*
  89. * Create a group of attributes so that we can create and destroy them all
  90. * at once.
  91. */
  92. static struct attribute *synth_attrs[] = {
  93. &caps_start_attribute.attr,
  94. &caps_stop_attribute.attr,
  95. &freq_attribute.attr,
  96. /* &lang_attribute.attr, */
  97. &pitch_attribute.attr,
  98. &inflection_attribute.attr,
  99. &punct_attribute.attr,
  100. &rate_attribute.attr,
  101. &tone_attribute.attr,
  102. &voice_attribute.attr,
  103. &vol_attribute.attr,
  104. &delay_time_attribute.attr,
  105. &direct_attribute.attr,
  106. &full_time_attribute.attr,
  107. &jiffy_delta_attribute.attr,
  108. &trigger_time_attribute.attr,
  109. NULL, /* need to NULL terminate the list of attributes */
  110. };
  111. static struct spk_synth synth_soft = {
  112. .name = "soft",
  113. .version = DRV_VERSION,
  114. .long_name = "software synth",
  115. .init = "\01@\x01\x31y\n",
  116. .procspeech = PROCSPEECH,
  117. .delay = 0,
  118. .trigger = 0,
  119. .jiffies = 0,
  120. .full = 0,
  121. .startup = SYNTH_START,
  122. .checkval = SYNTH_CHECK,
  123. .vars = vars,
  124. .io_ops = NULL,
  125. .probe = softsynth_probe,
  126. .release = softsynth_release,
  127. .synth_immediate = NULL,
  128. .catch_up = NULL,
  129. .flush = NULL,
  130. .is_alive = softsynth_is_alive,
  131. .synth_adjust = softsynth_adjust,
  132. .read_buff_add = NULL,
  133. .get_index = get_index,
  134. .indexing = {
  135. .command = "\x01%di",
  136. .lowindex = 1,
  137. .highindex = 5,
  138. .currindex = 1,
  139. },
  140. .attributes = {
  141. .attrs = synth_attrs,
  142. .name = "soft",
  143. },
  144. };
  145. static char *get_initstring(void)
  146. {
  147. static char buf[40];
  148. char *cp;
  149. struct var_t *var;
  150. size_t len;
  151. size_t n;
  152. memset(buf, 0, sizeof(buf));
  153. cp = buf;
  154. len = sizeof(buf);
  155. var = synth_soft.vars;
  156. while (var->var_id != MAXVARS) {
  157. if (var->var_id != CAPS_START && var->var_id != CAPS_STOP &&
  158. var->var_id != PAUSE && var->var_id != DIRECT) {
  159. n = scnprintf(cp, len, var->u.n.synth_fmt,
  160. var->u.n.value);
  161. cp = cp + n;
  162. len = len - n;
  163. }
  164. var++;
  165. }
  166. cp = cp + scnprintf(cp, len, "\n");
  167. return buf;
  168. }
  169. static int softsynth_open(struct inode *inode, struct file *fp)
  170. {
  171. unsigned long flags;
  172. /*if ((fp->f_flags & O_ACCMODE) != O_RDONLY) */
  173. /* return -EPERM; */
  174. spin_lock_irqsave(&speakup_info.spinlock, flags);
  175. if (synth_soft.alive) {
  176. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  177. return -EBUSY;
  178. }
  179. synth_soft.alive = 1;
  180. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  181. return 0;
  182. }
  183. static int softsynth_close(struct inode *inode, struct file *fp)
  184. {
  185. unsigned long flags;
  186. spin_lock_irqsave(&speakup_info.spinlock, flags);
  187. synth_soft.alive = 0;
  188. init_pos = 0;
  189. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  190. /* Make sure we let applications go before leaving */
  191. speakup_start_ttys();
  192. return 0;
  193. }
  194. static ssize_t softsynthx_read(struct file *fp, char __user *buf, size_t count,
  195. loff_t *pos, int unicode)
  196. {
  197. int chars_sent = 0;
  198. char __user *cp;
  199. char *init;
  200. size_t bytes_per_ch = unicode ? 3 : 1;
  201. u16 ch;
  202. int empty;
  203. unsigned long flags;
  204. DEFINE_WAIT(wait);
  205. if (count < bytes_per_ch)
  206. return -EINVAL;
  207. spin_lock_irqsave(&speakup_info.spinlock, flags);
  208. synth_soft.alive = 1;
  209. while (1) {
  210. prepare_to_wait(&speakup_event, &wait, TASK_INTERRUPTIBLE);
  211. if (synth_current() == &synth_soft) {
  212. if (!unicode)
  213. synth_buffer_skip_nonlatin1();
  214. if (!synth_buffer_empty() || speakup_info.flushing)
  215. break;
  216. }
  217. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  218. if (fp->f_flags & O_NONBLOCK) {
  219. finish_wait(&speakup_event, &wait);
  220. return -EAGAIN;
  221. }
  222. if (signal_pending(current)) {
  223. finish_wait(&speakup_event, &wait);
  224. return -ERESTARTSYS;
  225. }
  226. schedule();
  227. spin_lock_irqsave(&speakup_info.spinlock, flags);
  228. }
  229. finish_wait(&speakup_event, &wait);
  230. cp = buf;
  231. init = get_initstring();
  232. /* Keep 3 bytes available for a 16bit UTF-8-encoded character */
  233. while (chars_sent <= count - bytes_per_ch) {
  234. if (synth_current() != &synth_soft)
  235. break;
  236. if (speakup_info.flushing) {
  237. speakup_info.flushing = 0;
  238. ch = '\x18';
  239. } else if (init[init_pos]) {
  240. ch = init[init_pos++];
  241. } else {
  242. if (!unicode)
  243. synth_buffer_skip_nonlatin1();
  244. if (synth_buffer_empty())
  245. break;
  246. ch = synth_buffer_getc();
  247. }
  248. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  249. if ((!unicode && ch < 0x100) || (unicode && ch < 0x80)) {
  250. u_char c = ch;
  251. if (copy_to_user(cp, &c, 1))
  252. return -EFAULT;
  253. chars_sent++;
  254. cp++;
  255. } else if (unicode && ch < 0x800) {
  256. u_char s[2] = {
  257. 0xc0 | (ch >> 6),
  258. 0x80 | (ch & 0x3f)
  259. };
  260. if (copy_to_user(cp, s, sizeof(s)))
  261. return -EFAULT;
  262. chars_sent += sizeof(s);
  263. cp += sizeof(s);
  264. } else if (unicode) {
  265. u_char s[3] = {
  266. 0xe0 | (ch >> 12),
  267. 0x80 | ((ch >> 6) & 0x3f),
  268. 0x80 | (ch & 0x3f)
  269. };
  270. if (copy_to_user(cp, s, sizeof(s)))
  271. return -EFAULT;
  272. chars_sent += sizeof(s);
  273. cp += sizeof(s);
  274. }
  275. spin_lock_irqsave(&speakup_info.spinlock, flags);
  276. }
  277. *pos += chars_sent;
  278. empty = synth_buffer_empty();
  279. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  280. if (empty) {
  281. speakup_start_ttys();
  282. *pos = 0;
  283. }
  284. return chars_sent;
  285. }
  286. static ssize_t softsynth_read(struct file *fp, char __user *buf, size_t count,
  287. loff_t *pos)
  288. {
  289. return softsynthx_read(fp, buf, count, pos, 0);
  290. }
  291. static ssize_t softsynthu_read(struct file *fp, char __user *buf, size_t count,
  292. loff_t *pos)
  293. {
  294. return softsynthx_read(fp, buf, count, pos, 1);
  295. }
  296. static int last_index;
  297. static ssize_t softsynth_write(struct file *fp, const char __user *buf,
  298. size_t count, loff_t *pos)
  299. {
  300. unsigned long supplied_index = 0;
  301. int converted;
  302. converted = kstrtoul_from_user(buf, count, 0, &supplied_index);
  303. if (converted < 0)
  304. return converted;
  305. last_index = supplied_index;
  306. return count;
  307. }
  308. static __poll_t softsynth_poll(struct file *fp, struct poll_table_struct *wait)
  309. {
  310. unsigned long flags;
  311. __poll_t ret = 0;
  312. poll_wait(fp, &speakup_event, wait);
  313. spin_lock_irqsave(&speakup_info.spinlock, flags);
  314. if (synth_current() == &synth_soft &&
  315. (!synth_buffer_empty() || speakup_info.flushing))
  316. ret = EPOLLIN | EPOLLRDNORM;
  317. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  318. return ret;
  319. }
  320. static unsigned char get_index(struct spk_synth *synth)
  321. {
  322. int rv;
  323. rv = last_index;
  324. last_index = 0;
  325. return rv;
  326. }
  327. static const struct file_operations softsynth_fops = {
  328. .owner = THIS_MODULE,
  329. .poll = softsynth_poll,
  330. .read = softsynth_read,
  331. .write = softsynth_write,
  332. .open = softsynth_open,
  333. .release = softsynth_close,
  334. };
  335. static const struct file_operations softsynthu_fops = {
  336. .owner = THIS_MODULE,
  337. .poll = softsynth_poll,
  338. .read = softsynthu_read,
  339. .write = softsynth_write,
  340. .open = softsynth_open,
  341. .release = softsynth_close,
  342. };
  343. static int softsynth_probe(struct spk_synth *synth)
  344. {
  345. if (misc_registered != 0)
  346. return 0;
  347. memset(&synth_device, 0, sizeof(synth_device));
  348. synth_device.minor = MISC_DYNAMIC_MINOR;
  349. synth_device.name = "softsynth";
  350. synth_device.fops = &softsynth_fops;
  351. if (misc_register(&synth_device)) {
  352. pr_warn("Couldn't initialize miscdevice /dev/softsynth.\n");
  353. return -ENODEV;
  354. }
  355. memset(&synthu_device, 0, sizeof(synthu_device));
  356. synthu_device.minor = MISC_DYNAMIC_MINOR;
  357. synthu_device.name = "softsynthu";
  358. synthu_device.fops = &softsynthu_fops;
  359. if (misc_register(&synthu_device)) {
  360. misc_deregister(&synth_device);
  361. pr_warn("Couldn't initialize miscdevice /dev/softsynthu.\n");
  362. return -ENODEV;
  363. }
  364. misc_registered = 1;
  365. pr_info("initialized device: /dev/softsynth, node (MAJOR 10, MINOR %d)\n",
  366. synth_device.minor);
  367. pr_info("initialized device: /dev/softsynthu, node (MAJOR 10, MINOR %d)\n",
  368. synthu_device.minor);
  369. return 0;
  370. }
  371. static void softsynth_release(struct spk_synth *synth)
  372. {
  373. misc_deregister(&synth_device);
  374. misc_deregister(&synthu_device);
  375. misc_registered = 0;
  376. pr_info("unregistered /dev/softsynth\n");
  377. pr_info("unregistered /dev/softsynthu\n");
  378. }
  379. static int softsynth_is_alive(struct spk_synth *synth)
  380. {
  381. if (synth_soft.alive)
  382. return 1;
  383. return 0;
  384. }
  385. static int softsynth_adjust(struct spk_synth *synth, struct st_var_header *var)
  386. {
  387. struct st_var_header *punc_level_var;
  388. struct var_t *var_data;
  389. if (var->var_id != PUNC_LEVEL)
  390. return 0;
  391. /* We want to set the the speech synthesis punctuation level
  392. * accordingly, so it properly tunes speaking A_PUNC characters */
  393. var_data = var->data;
  394. if (!var_data)
  395. return 0;
  396. punc_level_var = spk_get_var_header(PUNCT);
  397. if (!punc_level_var)
  398. return 0;
  399. spk_set_num_var(var_data->u.n.value, punc_level_var, E_SET);
  400. return 1;
  401. }
  402. module_param_named(start, synth_soft.startup, short, 0444);
  403. module_param_named(direct, vars[DIRECT_ID].u.n.default_val, int, 0444);
  404. module_param_named(rate, vars[RATE_ID].u.n.default_val, int, 0444);
  405. module_param_named(pitch, vars[PITCH_ID].u.n.default_val, int, 0444);
  406. module_param_named(inflection, vars[INFLECTION_ID].u.n.default_val, int, 0444);
  407. module_param_named(vol, vars[VOL_ID].u.n.default_val, int, 0444);
  408. module_param_named(tone, vars[TONE_ID].u.n.default_val, int, 0444);
  409. module_param_named(punct, vars[PUNCT_ID].u.n.default_val, int, 0444);
  410. module_param_named(voice, vars[VOICE_ID].u.n.default_val, int, 0444);
  411. module_param_named(frequency, vars[FREQUENCY_ID].u.n.default_val, int, 0444);
  412. MODULE_PARM_DESC(start, "Start the synthesizer once it is loaded.");
  413. MODULE_PARM_DESC(direct, "Set the direct variable on load.");
  414. MODULE_PARM_DESC(rate, "Sets the rate of the synthesizer.");
  415. MODULE_PARM_DESC(pitch, "Sets the pitch of the synthesizer.");
  416. MODULE_PARM_DESC(inflection, "Sets the inflection of the synthesizer.");
  417. MODULE_PARM_DESC(vol, "Sets the volume of the speech synthesizer.");
  418. MODULE_PARM_DESC(tone, "Sets the tone of the speech synthesizer.");
  419. MODULE_PARM_DESC(punct, "Sets the amount of punctuation spoken by the synthesizer.");
  420. MODULE_PARM_DESC(voice, "Sets the voice used by the synthesizer.");
  421. MODULE_PARM_DESC(frequency, "Sets the frequency of speech synthesizer.");
  422. module_spk_synth(synth_soft);
  423. MODULE_AUTHOR("Kirk Reiser <kirk@braille.uwo.ca>");
  424. MODULE_DESCRIPTION("Speakup userspace software synthesizer support");
  425. MODULE_LICENSE("GPL");
  426. MODULE_VERSION(DRV_VERSION);