speakup_keypc.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * written by David Borowski
  4. *
  5. * Copyright (C) 2003 David Borowski.
  6. *
  7. * specifically written as a driver for the speakup screenreview
  8. * package it's not a general device driver.
  9. * This driver is for the Keynote Gold internal synthesizer.
  10. */
  11. #include <linux/jiffies.h>
  12. #include <linux/sched.h>
  13. #include <linux/timer.h>
  14. #include <linux/kthread.h>
  15. #include <linux/serial_reg.h>
  16. #include "spk_priv.h"
  17. #include "speakup.h"
  18. #define DRV_VERSION "2.10"
  19. #define SYNTH_IO_EXTENT 0x04
  20. #define SWAIT udelay(70)
  21. #define PROCSPEECH 0x1f
  22. #define SYNTH_CLEAR 0x03
  23. static int synth_probe(struct spk_synth *synth);
  24. static void keynote_release(struct spk_synth *synth);
  25. static const char *synth_immediate(struct spk_synth *synth, const char *buf);
  26. static void do_catch_up(struct spk_synth *synth);
  27. static void synth_flush(struct spk_synth *synth);
  28. static int synth_port;
  29. static int port_forced;
  30. static unsigned int synth_portlist[] = { 0x2a8, 0 };
  31. enum default_vars_id {
  32. CAPS_START_ID = 0, CAPS_STOP_ID,
  33. RATE_ID, PITCH_ID,
  34. DIRECT_ID, V_LAST_VAR_ID,
  35. NB_ID
  36. };
  37. static struct var_t vars[NB_ID] = {
  38. [CAPS_START_ID] = { CAPS_START, .u.s = {"[f130]" } },
  39. [CAPS_STOP_ID] = { CAPS_STOP, .u.s = {"[f90]" } },
  40. [RATE_ID] = { RATE, .u.n = {"\04%c ", 8, 0, 10, 81, -8, NULL } },
  41. [PITCH_ID] = { PITCH, .u.n = {"[f%d]", 5, 0, 9, 40, 10, NULL } },
  42. [DIRECT_ID] = { DIRECT, .u.n = {NULL, 0, 0, 1, 0, 0, NULL } },
  43. V_LAST_VAR
  44. };
  45. /*
  46. * These attributes will appear in /sys/accessibility/speakup/keypc.
  47. */
  48. static struct kobj_attribute caps_start_attribute =
  49. __ATTR(caps_start, 0644, spk_var_show, spk_var_store);
  50. static struct kobj_attribute caps_stop_attribute =
  51. __ATTR(caps_stop, 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 rate_attribute =
  55. __ATTR(rate, 0644, spk_var_show, spk_var_store);
  56. static struct kobj_attribute delay_time_attribute =
  57. __ATTR(delay_time, 0644, spk_var_show, spk_var_store);
  58. static struct kobj_attribute direct_attribute =
  59. __ATTR(direct, 0644, spk_var_show, spk_var_store);
  60. static struct kobj_attribute full_time_attribute =
  61. __ATTR(full_time, 0644, spk_var_show, spk_var_store);
  62. static struct kobj_attribute jiffy_delta_attribute =
  63. __ATTR(jiffy_delta, 0644, spk_var_show, spk_var_store);
  64. static struct kobj_attribute trigger_time_attribute =
  65. __ATTR(trigger_time, 0644, spk_var_show, spk_var_store);
  66. /*
  67. * Create a group of attributes so that we can create and destroy them all
  68. * at once.
  69. */
  70. static struct attribute *synth_attrs[] = {
  71. &caps_start_attribute.attr,
  72. &caps_stop_attribute.attr,
  73. &pitch_attribute.attr,
  74. &rate_attribute.attr,
  75. &delay_time_attribute.attr,
  76. &direct_attribute.attr,
  77. &full_time_attribute.attr,
  78. &jiffy_delta_attribute.attr,
  79. &trigger_time_attribute.attr,
  80. NULL, /* need to NULL terminate the list of attributes */
  81. };
  82. static struct spk_synth synth_keypc = {
  83. .name = "keypc",
  84. .version = DRV_VERSION,
  85. .long_name = "Keynote PC",
  86. .init = "[t][n7,1][n8,0]",
  87. .procspeech = PROCSPEECH,
  88. .clear = SYNTH_CLEAR,
  89. .delay = 500,
  90. .trigger = 50,
  91. .jiffies = 50,
  92. .full = 1000,
  93. .startup = SYNTH_START,
  94. .checkval = SYNTH_CHECK,
  95. .vars = vars,
  96. .io_ops = &spk_serial_io_ops,
  97. .probe = synth_probe,
  98. .release = keynote_release,
  99. .synth_immediate = synth_immediate,
  100. .catch_up = do_catch_up,
  101. .flush = synth_flush,
  102. .is_alive = spk_synth_is_alive_nop,
  103. .synth_adjust = NULL,
  104. .read_buff_add = NULL,
  105. .get_index = NULL,
  106. .indexing = {
  107. .command = NULL,
  108. .lowindex = 0,
  109. .highindex = 0,
  110. .currindex = 0,
  111. },
  112. .attributes = {
  113. .attrs = synth_attrs,
  114. .name = "keypc",
  115. },
  116. };
  117. static inline bool synth_writable(void)
  118. {
  119. return (inb_p(synth_port + UART_RX) & 0x10) != 0;
  120. }
  121. static inline bool synth_full(void)
  122. {
  123. return (inb_p(synth_port + UART_RX) & 0x80) == 0;
  124. }
  125. static char *oops(void)
  126. {
  127. int s1, s2, s3, s4;
  128. s1 = inb_p(synth_port);
  129. s2 = inb_p(synth_port + 1);
  130. s3 = inb_p(synth_port + 2);
  131. s4 = inb_p(synth_port + 3);
  132. pr_warn("synth timeout %d %d %d %d\n", s1, s2, s3, s4);
  133. return NULL;
  134. }
  135. static const char *synth_immediate(struct spk_synth *synth, const char *buf)
  136. {
  137. u_char ch;
  138. int timeout;
  139. while ((ch = *buf)) {
  140. if (ch == '\n')
  141. ch = PROCSPEECH;
  142. if (synth_full())
  143. return buf;
  144. timeout = 1000;
  145. while (synth_writable())
  146. if (--timeout <= 0)
  147. return oops();
  148. outb_p(ch, synth_port);
  149. udelay(70);
  150. buf++;
  151. }
  152. return NULL;
  153. }
  154. static void do_catch_up(struct spk_synth *synth)
  155. {
  156. u_char ch;
  157. int timeout;
  158. unsigned long flags;
  159. unsigned long jiff_max;
  160. struct var_t *jiffy_delta;
  161. struct var_t *delay_time;
  162. struct var_t *full_time;
  163. int delay_time_val;
  164. int full_time_val;
  165. int jiffy_delta_val;
  166. jiffy_delta = spk_get_var(JIFFY);
  167. delay_time = spk_get_var(DELAY);
  168. full_time = spk_get_var(FULL);
  169. spin_lock_irqsave(&speakup_info.spinlock, flags);
  170. jiffy_delta_val = jiffy_delta->u.n.value;
  171. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  172. jiff_max = jiffies + jiffy_delta_val;
  173. while (!kthread_should_stop()) {
  174. spin_lock_irqsave(&speakup_info.spinlock, flags);
  175. if (speakup_info.flushing) {
  176. speakup_info.flushing = 0;
  177. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  178. synth->flush(synth);
  179. continue;
  180. }
  181. synth_buffer_skip_nonlatin1();
  182. if (synth_buffer_empty()) {
  183. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  184. break;
  185. }
  186. set_current_state(TASK_INTERRUPTIBLE);
  187. full_time_val = full_time->u.n.value;
  188. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  189. if (synth_full()) {
  190. schedule_timeout(msecs_to_jiffies(full_time_val));
  191. continue;
  192. }
  193. set_current_state(TASK_RUNNING);
  194. timeout = 1000;
  195. while (synth_writable())
  196. if (--timeout <= 0)
  197. break;
  198. if (timeout <= 0) {
  199. oops();
  200. break;
  201. }
  202. spin_lock_irqsave(&speakup_info.spinlock, flags);
  203. ch = synth_buffer_getc();
  204. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  205. if (ch == '\n')
  206. ch = PROCSPEECH;
  207. outb_p(ch, synth_port);
  208. SWAIT;
  209. if (time_after_eq(jiffies, jiff_max) && (ch == SPACE)) {
  210. timeout = 1000;
  211. while (synth_writable())
  212. if (--timeout <= 0)
  213. break;
  214. if (timeout <= 0) {
  215. oops();
  216. break;
  217. }
  218. outb_p(PROCSPEECH, synth_port);
  219. spin_lock_irqsave(&speakup_info.spinlock, flags);
  220. jiffy_delta_val = jiffy_delta->u.n.value;
  221. delay_time_val = delay_time->u.n.value;
  222. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  223. schedule_timeout(msecs_to_jiffies(delay_time_val));
  224. jiff_max = jiffies + jiffy_delta_val;
  225. }
  226. }
  227. timeout = 1000;
  228. while (synth_writable())
  229. if (--timeout <= 0)
  230. break;
  231. if (timeout <= 0)
  232. oops();
  233. else
  234. outb_p(PROCSPEECH, synth_port);
  235. }
  236. static void synth_flush(struct spk_synth *synth)
  237. {
  238. outb_p(SYNTH_CLEAR, synth_port);
  239. }
  240. static int synth_probe(struct spk_synth *synth)
  241. {
  242. unsigned int port_val = 0;
  243. int i;
  244. pr_info("Probing for %s.\n", synth->long_name);
  245. if (port_forced) {
  246. synth_port = port_forced;
  247. pr_info("probe forced to %x by kernel command line\n",
  248. synth_port);
  249. if (synth_request_region(synth_port - 1, SYNTH_IO_EXTENT)) {
  250. pr_warn("sorry, port already reserved\n");
  251. return -EBUSY;
  252. }
  253. port_val = inb(synth_port);
  254. } else {
  255. for (i = 0; synth_portlist[i]; i++) {
  256. if (synth_request_region(synth_portlist[i],
  257. SYNTH_IO_EXTENT)) {
  258. pr_warn
  259. ("request_region: failed with 0x%x, %d\n",
  260. synth_portlist[i], SYNTH_IO_EXTENT);
  261. continue;
  262. }
  263. port_val = inb(synth_portlist[i]);
  264. if (port_val == 0x80) {
  265. synth_port = synth_portlist[i];
  266. break;
  267. }
  268. }
  269. }
  270. if (port_val != 0x80) {
  271. pr_info("%s: not found\n", synth->long_name);
  272. synth_release_region(synth_port, SYNTH_IO_EXTENT);
  273. synth_port = 0;
  274. return -ENODEV;
  275. }
  276. pr_info("%s: %03x-%03x, driver version %s,\n", synth->long_name,
  277. synth_port, synth_port + SYNTH_IO_EXTENT - 1,
  278. synth->version);
  279. synth->alive = 1;
  280. return 0;
  281. }
  282. static void keynote_release(struct spk_synth *synth)
  283. {
  284. spk_stop_serial_interrupt();
  285. if (synth_port)
  286. synth_release_region(synth_port, SYNTH_IO_EXTENT);
  287. synth_port = 0;
  288. }
  289. module_param_hw_named(port, port_forced, int, ioport, 0444);
  290. module_param_named(start, synth_keypc.startup, short, 0444);
  291. module_param_named(rate, vars[RATE_ID].u.n.default_val, int, 0444);
  292. module_param_named(pitch, vars[PITCH_ID].u.n.default_val, int, 0444);
  293. module_param_named(direct, vars[DIRECT_ID].u.n.default_val, int, 0444);
  294. MODULE_PARM_DESC(port, "Set the port for the synthesizer (override probing).");
  295. MODULE_PARM_DESC(start, "Start the synthesizer once it is loaded.");
  296. MODULE_PARM_DESC(rate, "Set the rate variable on load.");
  297. MODULE_PARM_DESC(pitch, "Set the pitch variable on load.");
  298. MODULE_PARM_DESC(direct, "Set the direct variable on load.");
  299. module_spk_synth(synth_keypc);
  300. MODULE_AUTHOR("David Borowski");
  301. MODULE_DESCRIPTION("Speakup support for Keynote Gold PC synthesizers");
  302. MODULE_LICENSE("GPL");
  303. MODULE_VERSION(DRV_VERSION);