speakup_soft.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /* speakup_soft.c - speakup driver to register and make available
  2. * a user space device for software synthesizers. written by: Kirk
  3. * Reiser <kirk@braille.uwo.ca>
  4. *
  5. * Copyright (C) 2003 Kirk Reiser.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. * this code is specificly written as a driver for the speakup screenreview
  22. * package and is not a general device driver.
  23. */
  24. #include <linux/unistd.h>
  25. #include <linux/miscdevice.h> /* for misc_register, and SYNTH_MINOR */
  26. #include <linux/poll.h> /* for poll_wait() */
  27. #include <linux/sched.h> /* schedule(), signal_pending(), TASK_INTERRUPTIBLE */
  28. #include "spk_priv.h"
  29. #include "speakup.h"
  30. #define DRV_VERSION "2.6"
  31. #define SOFTSYNTH_MINOR 26 /* might as well give it one more than /dev/synth */
  32. #define PROCSPEECH 0x0d
  33. #define CLEAR_SYNTH 0x18
  34. static int softsynth_probe(struct spk_synth *synth);
  35. static void softsynth_release(void);
  36. static int softsynth_is_alive(struct spk_synth *synth);
  37. static unsigned char get_index(void);
  38. static struct miscdevice synth_device;
  39. static int init_pos;
  40. static int misc_registered;
  41. static struct var_t vars[] = {
  42. { CAPS_START, .u.s = {"\x01+3p" } },
  43. { CAPS_STOP, .u.s = {"\x01-3p" } },
  44. { RATE, .u.n = {"\x01%ds", 2, 0, 9, 0, 0, NULL } },
  45. { PITCH, .u.n = {"\x01%dp", 5, 0, 9, 0, 0, NULL } },
  46. { VOL, .u.n = {"\x01%dv", 5, 0, 9, 0, 0, NULL } },
  47. { TONE, .u.n = {"\x01%dx", 1, 0, 2, 0, 0, NULL } },
  48. { PUNCT, .u.n = {"\x01%db", 0, 0, 2, 0, 0, NULL } },
  49. { VOICE, .u.n = {"\x01%do", 0, 0, 7, 0, 0, NULL } },
  50. { FREQUENCY, .u.n = {"\x01%df", 5, 0, 9, 0, 0, NULL } },
  51. { DIRECT, .u.n = {NULL, 0, 0, 1, 0, 0, NULL } },
  52. V_LAST_VAR
  53. };
  54. /*
  55. * These attributes will appear in /sys/accessibility/speakup/soft.
  56. */
  57. static struct kobj_attribute caps_start_attribute =
  58. __ATTR(caps_start, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
  59. static struct kobj_attribute caps_stop_attribute =
  60. __ATTR(caps_stop, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
  61. static struct kobj_attribute freq_attribute =
  62. __ATTR(freq, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
  63. static struct kobj_attribute pitch_attribute =
  64. __ATTR(pitch, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
  65. static struct kobj_attribute punct_attribute =
  66. __ATTR(punct, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
  67. static struct kobj_attribute rate_attribute =
  68. __ATTR(rate, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
  69. static struct kobj_attribute tone_attribute =
  70. __ATTR(tone, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
  71. static struct kobj_attribute voice_attribute =
  72. __ATTR(voice, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
  73. static struct kobj_attribute vol_attribute =
  74. __ATTR(vol, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
  75. /*
  76. * We should uncomment the following definition, when we agree on a
  77. * method of passing a language designation to the software synthesizer.
  78. * static struct kobj_attribute lang_attribute =
  79. * __ATTR(lang, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
  80. */
  81. static struct kobj_attribute delay_time_attribute =
  82. __ATTR(delay_time, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
  83. static struct kobj_attribute direct_attribute =
  84. __ATTR(direct, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
  85. static struct kobj_attribute full_time_attribute =
  86. __ATTR(full_time, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
  87. static struct kobj_attribute jiffy_delta_attribute =
  88. __ATTR(jiffy_delta, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
  89. static struct kobj_attribute trigger_time_attribute =
  90. __ATTR(trigger_time, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
  91. /*
  92. * Create a group of attributes so that we can create and destroy them all
  93. * at once.
  94. */
  95. static struct attribute *synth_attrs[] = {
  96. &caps_start_attribute.attr,
  97. &caps_stop_attribute.attr,
  98. &freq_attribute.attr,
  99. /* &lang_attribute.attr, */
  100. &pitch_attribute.attr,
  101. &punct_attribute.attr,
  102. &rate_attribute.attr,
  103. &tone_attribute.attr,
  104. &voice_attribute.attr,
  105. &vol_attribute.attr,
  106. &delay_time_attribute.attr,
  107. &direct_attribute.attr,
  108. &full_time_attribute.attr,
  109. &jiffy_delta_attribute.attr,
  110. &trigger_time_attribute.attr,
  111. NULL, /* need to NULL terminate the list of attributes */
  112. };
  113. static struct spk_synth synth_soft = {
  114. .name = "soft",
  115. .version = DRV_VERSION,
  116. .long_name = "software synth",
  117. .init = "\01@\x01\x31y\n",
  118. .procspeech = PROCSPEECH,
  119. .delay = 0,
  120. .trigger = 0,
  121. .jiffies = 0,
  122. .full = 0,
  123. .startup = SYNTH_START,
  124. .checkval = SYNTH_CHECK,
  125. .vars = vars,
  126. .probe = softsynth_probe,
  127. .release = softsynth_release,
  128. .synth_immediate = NULL,
  129. .catch_up = NULL,
  130. .flush = NULL,
  131. .is_alive = softsynth_is_alive,
  132. .synth_adjust = NULL,
  133. .read_buff_add = NULL,
  134. .get_index = get_index,
  135. .indexing = {
  136. .command = "\x01%di",
  137. .lowindex = 1,
  138. .highindex = 5,
  139. .currindex = 1,
  140. },
  141. .attributes = {
  142. .attrs = synth_attrs,
  143. .name = "soft",
  144. },
  145. };
  146. static char *get_initstring(void)
  147. {
  148. static char buf[40];
  149. char *cp;
  150. struct var_t *var;
  151. memset(buf, 0, sizeof(buf));
  152. cp = buf;
  153. var = synth_soft.vars;
  154. while (var->var_id != MAXVARS) {
  155. if (var->var_id != CAPS_START && var->var_id != CAPS_STOP
  156. && var->var_id != DIRECT)
  157. cp = cp + sprintf(cp, var->u.n.synth_fmt,
  158. var->u.n.value);
  159. var++;
  160. }
  161. cp = cp + sprintf(cp, "\n");
  162. return buf;
  163. }
  164. static int softsynth_open(struct inode *inode, struct file *fp)
  165. {
  166. unsigned long flags;
  167. /*if ((fp->f_flags & O_ACCMODE) != O_RDONLY) */
  168. /* return -EPERM; */
  169. spin_lock_irqsave(&speakup_info.spinlock, flags);
  170. if (synth_soft.alive) {
  171. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  172. return -EBUSY;
  173. }
  174. synth_soft.alive = 1;
  175. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  176. return 0;
  177. }
  178. static int softsynth_close(struct inode *inode, struct file *fp)
  179. {
  180. unsigned long flags;
  181. spin_lock_irqsave(&speakup_info.spinlock, flags);
  182. synth_soft.alive = 0;
  183. init_pos = 0;
  184. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  185. /* Make sure we let applications go before leaving */
  186. speakup_start_ttys();
  187. return 0;
  188. }
  189. static ssize_t softsynth_read(struct file *fp, char __user *buf, size_t count,
  190. loff_t *pos)
  191. {
  192. int chars_sent = 0;
  193. char __user *cp;
  194. char *init;
  195. char ch;
  196. int empty;
  197. unsigned long flags;
  198. DEFINE_WAIT(wait);
  199. spin_lock_irqsave(&speakup_info.spinlock, flags);
  200. while (1) {
  201. prepare_to_wait(&speakup_event, &wait, TASK_INTERRUPTIBLE);
  202. if (!synth_buffer_empty() || speakup_info.flushing)
  203. break;
  204. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  205. if (fp->f_flags & O_NONBLOCK) {
  206. finish_wait(&speakup_event, &wait);
  207. return -EAGAIN;
  208. }
  209. if (signal_pending(current)) {
  210. finish_wait(&speakup_event, &wait);
  211. return -ERESTARTSYS;
  212. }
  213. schedule();
  214. spin_lock_irqsave(&speakup_info.spinlock, flags);
  215. }
  216. finish_wait(&speakup_event, &wait);
  217. cp = buf;
  218. init = get_initstring();
  219. while (chars_sent < count) {
  220. if (speakup_info.flushing) {
  221. speakup_info.flushing = 0;
  222. ch = '\x18';
  223. } else if (synth_buffer_empty()) {
  224. break;
  225. } else if (init[init_pos]) {
  226. ch = init[init_pos++];
  227. } else {
  228. ch = synth_buffer_getc();
  229. }
  230. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  231. if (copy_to_user(cp, &ch, 1))
  232. return -EFAULT;
  233. spin_lock_irqsave(&speakup_info.spinlock, flags);
  234. chars_sent++;
  235. cp++;
  236. }
  237. *pos += chars_sent;
  238. empty = synth_buffer_empty();
  239. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  240. if (empty) {
  241. speakup_start_ttys();
  242. *pos = 0;
  243. }
  244. return chars_sent;
  245. }
  246. static int last_index;
  247. static ssize_t softsynth_write(struct file *fp, const char __user *buf,
  248. size_t count, loff_t *pos)
  249. {
  250. unsigned long supplied_index = 0;
  251. int converted;
  252. converted = kstrtoul_from_user(buf, count, 0, &supplied_index);
  253. if (converted < 0)
  254. return converted;
  255. last_index = supplied_index;
  256. return count;
  257. }
  258. static unsigned int softsynth_poll(struct file *fp,
  259. struct poll_table_struct *wait)
  260. {
  261. unsigned long flags;
  262. int ret = 0;
  263. poll_wait(fp, &speakup_event, wait);
  264. spin_lock_irqsave(&speakup_info.spinlock, flags);
  265. if (!synth_buffer_empty() || speakup_info.flushing)
  266. ret = POLLIN | POLLRDNORM;
  267. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  268. return ret;
  269. }
  270. static unsigned char get_index(void)
  271. {
  272. int rv;
  273. rv = last_index;
  274. last_index = 0;
  275. return rv;
  276. }
  277. static const struct file_operations softsynth_fops = {
  278. .owner = THIS_MODULE,
  279. .poll = softsynth_poll,
  280. .read = softsynth_read,
  281. .write = softsynth_write,
  282. .open = softsynth_open,
  283. .release = softsynth_close,
  284. };
  285. static int softsynth_probe(struct spk_synth *synth)
  286. {
  287. if (misc_registered != 0)
  288. return 0;
  289. memset(&synth_device, 0, sizeof(synth_device));
  290. synth_device.minor = SOFTSYNTH_MINOR;
  291. synth_device.name = "softsynth";
  292. synth_device.fops = &softsynth_fops;
  293. if (misc_register(&synth_device)) {
  294. pr_warn("Couldn't initialize miscdevice /dev/softsynth.\n");
  295. return -ENODEV;
  296. }
  297. misc_registered = 1;
  298. pr_info("initialized device: /dev/softsynth, node (MAJOR 10, MINOR 26)\n");
  299. return 0;
  300. }
  301. static void softsynth_release(void)
  302. {
  303. misc_deregister(&synth_device);
  304. misc_registered = 0;
  305. pr_info("unregistered /dev/softsynth\n");
  306. }
  307. static int softsynth_is_alive(struct spk_synth *synth)
  308. {
  309. if (synth_soft.alive)
  310. return 1;
  311. return 0;
  312. }
  313. module_param_named(start, synth_soft.startup, short, S_IRUGO);
  314. MODULE_PARM_DESC(start, "Start the synthesizer once it is loaded.");
  315. module_spk_synth(synth_soft);
  316. MODULE_AUTHOR("Kirk Reiser <kirk@braille.uwo.ca>");
  317. MODULE_DESCRIPTION("Speakup userspace software synthesizer support");
  318. MODULE_LICENSE("GPL");
  319. MODULE_VERSION(DRV_VERSION);