speakup_soft.c 12 KB

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