blob: 61a3f7a5eb6be148acc1c9ce88c4a7cb7278c277 [file] [log] [blame]
William Hubbsc6e3fd22010-10-07 13:20:02 -05001/*
2 * Speakup kobject implementation
3 *
4 * Copyright (C) 2009 William Hubbs
5 *
6 * This code is based on kobject-example.c, which came with linux 2.6.x.
7 *
8 * Copyright (C) 2004-2007 Greg Kroah-Hartman <greg@kroah.com>
9 * Copyright (C) 2007 Novell Inc.
10 *
11 * Released under the GPL version 2 only.
12 *
13 */
14#include <linux/slab.h> /* For kmalloc. */
15#include <linux/kernel.h>
16#include <linux/kobject.h>
17#include <linux/string.h>
Andy Shevchenko576d7422013-04-30 15:27:31 -070018#include <linux/string_helpers.h>
William Hubbsc6e3fd22010-10-07 13:20:02 -050019#include <linux/sysfs.h>
20#include <linux/ctype.h>
21
22#include "speakup.h"
23#include "spk_priv.h"
24
25/*
26 * This is called when a user reads the characters or chartab sys file.
27 */
28static ssize_t chars_chartab_show(struct kobject *kobj,
29 struct kobj_attribute *attr, char *buf)
30{
31 int i;
32 int len = 0;
33 char *cp;
34 char *buf_pointer = buf;
35 size_t bufsize = PAGE_SIZE;
36 unsigned long flags;
37
William Hubbsc2d7f742013-05-13 00:02:53 -050038 spin_lock_irqsave(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -050039 *buf_pointer = '\0';
40 for (i = 0; i < 256; i++) {
41 if (bufsize <= 1)
42 break;
43 if (strcmp("characters", attr->attr.name) == 0) {
44 len = scnprintf(buf_pointer, bufsize, "%d\t%s\n",
Samuel Thibaultca2beaf2013-01-02 02:37:40 +010045 i, spk_characters[i]);
William Hubbsc6e3fd22010-10-07 13:20:02 -050046 } else { /* show chartab entry */
47 if (IS_TYPE(i, B_CTL))
48 cp = "B_CTL";
49 else if (IS_TYPE(i, WDLM))
50 cp = "WDLM";
51 else if (IS_TYPE(i, A_PUNC))
52 cp = "A_PUNC";
53 else if (IS_TYPE(i, PUNC))
54 cp = "PUNC";
55 else if (IS_TYPE(i, NUM))
56 cp = "NUM";
57 else if (IS_TYPE(i, A_CAP))
58 cp = "A_CAP";
59 else if (IS_TYPE(i, ALPHA))
60 cp = "ALPHA";
61 else if (IS_TYPE(i, B_CAPSYM))
62 cp = "B_CAPSYM";
63 else if (IS_TYPE(i, B_SYM))
64 cp = "B_SYM";
65 else
66 cp = "0";
67 len =
68 scnprintf(buf_pointer, bufsize, "%d\t%s\n", i, cp);
69 }
70 bufsize -= len;
71 buf_pointer += len;
72 }
William Hubbsc2d7f742013-05-13 00:02:53 -050073 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -050074 return buf_pointer - buf;
75}
76
77/*
78 * Print informational messages or warnings after updating
79 * character descriptions or chartab entries.
80 */
81static void report_char_chartab_status(int reset, int received, int used,
82 int rejected, int do_characters)
83{
84 char *object_type[] = {
85 "character class entries",
86 "character descriptions",
87 };
88 int len;
89 char buf[80];
90
91 if (reset) {
92 pr_info("%s reset to defaults\n", object_type[do_characters]);
William Hubbsf1c6b7f2010-10-12 11:39:30 -050093 } else if (received) {
William Hubbsc6e3fd22010-10-07 13:20:02 -050094 len = snprintf(buf, sizeof(buf),
William Hubbsf1c6b7f2010-10-12 11:39:30 -050095 " updated %d of %d %s\n",
96 used, received, object_type[do_characters]);
William Hubbsc6e3fd22010-10-07 13:20:02 -050097 if (rejected)
98 snprintf(buf + (len - 1), sizeof(buf) - (len - 1),
99 " with %d reject%s\n",
100 rejected, rejected > 1 ? "s" : "");
101 printk(buf);
102 }
103}
104
105/*
106 * This is called when a user changes the characters or chartab parameters.
107 */
108static ssize_t chars_chartab_store(struct kobject *kobj,
109 struct kobj_attribute *attr, const char *buf, size_t count)
110{
111 char *cp = (char *) buf;
112 char *end = cp + count; /* the null at the end of the buffer */
113 char *linefeed = NULL;
114 char keyword[MAX_DESC_LEN + 1];
115 char *outptr = NULL; /* Will hold keyword or desc. */
116 char *temp = NULL;
117 char *desc = NULL;
118 ssize_t retval = count;
119 unsigned long flags;
120 unsigned long index = 0;
121 int charclass = 0;
122 int received = 0;
123 int used = 0;
124 int rejected = 0;
125 int reset = 0;
126 int do_characters = !strcmp(attr->attr.name, "characters");
127 size_t desc_length = 0;
128 int i;
129
William Hubbsc2d7f742013-05-13 00:02:53 -0500130 spin_lock_irqsave(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500131 while (cp < end) {
132
133 while ((cp < end) && (*cp == ' ' || *cp == '\t'))
134 cp++;
135
136 if (cp == end)
137 break;
138 if ((*cp == '\n') || strchr("dDrR", *cp)) {
139 reset = 1;
140 break;
141 }
142 received++;
143
144 linefeed = strchr(cp, '\n');
145 if (!linefeed) {
146 rejected++;
147 break;
148 }
149
William Hubbsf1c6b7f2010-10-12 11:39:30 -0500150 if (!isdigit(*cp)) {
William Hubbsc6e3fd22010-10-07 13:20:02 -0500151 rejected++;
152 cp = linefeed + 1;
153 continue;
154 }
155
156 index = simple_strtoul(cp, &temp, 10);
157 if (index > 255) {
158 rejected++;
159 cp = linefeed + 1;
160 continue;
161 }
162
163 while ((temp < linefeed) && (*temp == ' ' || *temp == '\t'))
164 temp++;
165
166 desc_length = linefeed - temp;
167 if (desc_length > MAX_DESC_LEN) {
168 rejected++;
169 cp = linefeed + 1;
170 continue;
171 }
172 if (do_characters) {
173 desc = kmalloc(desc_length + 1, GFP_ATOMIC);
William Hubbsf1c6b7f2010-10-12 11:39:30 -0500174 if (!desc) {
William Hubbsc6e3fd22010-10-07 13:20:02 -0500175 retval = -ENOMEM;
176 reset = 1; /* just reset on error. */
177 break;
178 }
179 outptr = desc;
180 } else {
181 outptr = keyword;
182 }
183
184 for (i = 0; i < desc_length; i++)
185 outptr[i] = temp[i];
186 outptr[desc_length] = '\0';
187
188 if (do_characters) {
Samuel Thibaultca2beaf2013-01-02 02:37:40 +0100189 if (spk_characters[index] != spk_default_chars[index])
190 kfree(spk_characters[index]);
191 spk_characters[index] = desc;
William Hubbsc6e3fd22010-10-07 13:20:02 -0500192 used++;
193 } else {
Samuel Thibaultca2beaf2013-01-02 02:37:40 +0100194 charclass = spk_chartab_get_value(keyword);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500195 if (charclass == 0) {
196 rejected++;
197 cp = linefeed + 1;
198 continue;
199 }
200 if (charclass != spk_chartab[index]) {
201 spk_chartab[index] = charclass;
202 used++;
203 }
204 }
205 cp = linefeed + 1;
206 }
207
208 if (reset) {
209 if (do_characters)
Samuel Thibaultca2beaf2013-01-02 02:37:40 +0100210 spk_reset_default_chars();
William Hubbsc6e3fd22010-10-07 13:20:02 -0500211 else
Samuel Thibaultca2beaf2013-01-02 02:37:40 +0100212 spk_reset_default_chartab();
William Hubbsc6e3fd22010-10-07 13:20:02 -0500213 }
214
William Hubbsc2d7f742013-05-13 00:02:53 -0500215 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
William Hubbsf1c6b7f2010-10-12 11:39:30 -0500216 report_char_chartab_status(reset, received, used, rejected,
217 do_characters);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500218 return retval;
219}
220
221/*
222 * This is called when a user reads the keymap parameter.
223 */
224static ssize_t keymap_show(struct kobject *kobj, struct kobj_attribute *attr,
225 char *buf)
226{
227 char *cp = buf;
228 int i;
229 int n;
230 int num_keys;
231 int nstates;
232 u_char *cp1;
233 u_char ch;
234 unsigned long flags;
William Hubbsc2d7f742013-05-13 00:02:53 -0500235 spin_lock_irqsave(&speakup_info.spinlock, flags);
Samuel Thibaultca2beaf2013-01-02 02:37:40 +0100236 cp1 = spk_key_buf + SHIFT_TBL_SIZE;
William Hubbsc6e3fd22010-10-07 13:20:02 -0500237 num_keys = (int)(*cp1);
238 nstates = (int)cp1[1];
239 cp += sprintf(cp, "%d, %d, %d,\n", KEY_MAP_VER, num_keys, nstates);
240 cp1 += 2; /* now pointing at shift states */
William Hubbsf1c6b7f2010-10-12 11:39:30 -0500241 /* dump num_keys+1 as first row is shift states + flags,
242 * each subsequent row is key + states */
William Hubbsc6e3fd22010-10-07 13:20:02 -0500243 for (n = 0; n <= num_keys; n++) {
244 for (i = 0; i <= nstates; i++) {
245 ch = *cp1++;
246 cp += sprintf(cp, "%d,", (int)ch);
247 *cp++ = (i < nstates) ? SPACE : '\n';
248 }
249 }
250 cp += sprintf(cp, "0, %d\n", KEY_MAP_VER);
William Hubbsc2d7f742013-05-13 00:02:53 -0500251 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500252 return (int)(cp-buf);
253}
254
255/*
256 * This is called when a user changes the keymap parameter.
257 */
258static ssize_t keymap_store(struct kobject *kobj, struct kobj_attribute *attr,
259 const char *buf, size_t count)
260{
261 int i;
262 ssize_t ret = count;
263 char *in_buff = NULL;
264 char *cp;
265 u_char *cp1;
266 unsigned long flags;
267
William Hubbsc2d7f742013-05-13 00:02:53 -0500268 spin_lock_irqsave(&speakup_info.spinlock, flags);
Thomas Meyer1aa5eb62011-11-12 13:11:18 +0100269 in_buff = kmemdup(buf, count + 1, GFP_ATOMIC);
William Hubbsf1c6b7f2010-10-12 11:39:30 -0500270 if (!in_buff) {
William Hubbsc2d7f742013-05-13 00:02:53 -0500271 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500272 return -ENOMEM;
273 }
William Hubbsc6e3fd22010-10-07 13:20:02 -0500274 if (strchr("dDrR", *in_buff)) {
Samuel Thibaultca2beaf2013-01-02 02:37:40 +0100275 spk_set_key_info(spk_key_defaults, spk_key_buf);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500276 pr_info("keymap set to default values\n");
277 kfree(in_buff);
William Hubbsc2d7f742013-05-13 00:02:53 -0500278 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500279 return count;
280 }
281 if (in_buff[count - 1] == '\n')
William Hubbsf1c6b7f2010-10-12 11:39:30 -0500282 in_buff[count - 1] = '\0';
William Hubbsc6e3fd22010-10-07 13:20:02 -0500283 cp = in_buff;
284 cp1 = (u_char *)in_buff;
285 for (i = 0; i < 3; i++) {
Samuel Thibaultca2beaf2013-01-02 02:37:40 +0100286 cp = spk_s2uchar(cp, cp1);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500287 cp1++;
288 }
289 i = (int)cp1[-2]+1;
290 i *= (int)cp1[-1]+1;
291 i += 2; /* 0 and last map ver */
292 if (cp1[-3] != KEY_MAP_VER || cp1[-1] > 10 ||
Samuel Thibaultca2beaf2013-01-02 02:37:40 +0100293 i+SHIFT_TBL_SIZE+4 >= sizeof(spk_key_buf)) {
William Hubbsc6e3fd22010-10-07 13:20:02 -0500294 pr_warn("i %d %d %d %d\n", i,
295 (int)cp1[-3], (int)cp1[-2], (int)cp1[-1]);
296 kfree(in_buff);
William Hubbsc2d7f742013-05-13 00:02:53 -0500297 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500298 return -EINVAL;
299 }
300 while (--i >= 0) {
Samuel Thibaultca2beaf2013-01-02 02:37:40 +0100301 cp = spk_s2uchar(cp, cp1);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500302 cp1++;
303 if (!(*cp))
304 break;
305 }
306 if (i != 0 || cp1[-1] != KEY_MAP_VER || cp1[-2] != 0) {
307 ret = -EINVAL;
308 pr_warn("end %d %d %d %d\n", i,
309 (int)cp1[-3], (int)cp1[-2], (int)cp1[-1]);
310 } else {
Samuel Thibaultca2beaf2013-01-02 02:37:40 +0100311 if (spk_set_key_info(in_buff, spk_key_buf)) {
312 spk_set_key_info(spk_key_defaults, spk_key_buf);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500313 ret = -EINVAL;
314 pr_warn("set key failed\n");
315 }
316 }
317 kfree(in_buff);
William Hubbsc2d7f742013-05-13 00:02:53 -0500318 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500319 return ret;
320}
321
322/*
323 * This is called when a user changes the value of the silent parameter.
324 */
325static ssize_t silent_store(struct kobject *kobj, struct kobj_attribute *attr,
326 const char *buf, size_t count)
327{
328 int len;
329 struct vc_data *vc = vc_cons[fg_console].d;
330 char ch = 0;
331 char shut;
332 unsigned long flags;
333
334 len = strlen(buf);
roel kluine7bf3522011-01-03 11:59:48 -0800335 if (len > 0 && len < 3) {
William Hubbsc6e3fd22010-10-07 13:20:02 -0500336 ch = buf[0];
337 if (ch == '\n')
338 ch = '0';
339 }
340 if (ch < '0' || ch > '7') {
341 pr_warn("silent value '%c' not in range (0,7)\n", ch);
342 return -EINVAL;
343 }
William Hubbsc2d7f742013-05-13 00:02:53 -0500344 spin_lock_irqsave(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500345 if (ch&2) {
346 shut = 1;
Samuel Thibaultca2beaf2013-01-02 02:37:40 +0100347 spk_do_flush();
William Hubbsc6e3fd22010-10-07 13:20:02 -0500348 } else {
349 shut = 0;
350 }
351 if (ch&4)
352 shut |= 0x40;
353 if (ch&1)
354 spk_shut_up |= shut;
355 else
356 spk_shut_up &= ~shut;
William Hubbsc2d7f742013-05-13 00:02:53 -0500357 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500358 return count;
359}
360
361/*
362 * This is called when a user reads the synth setting.
363 */
364static ssize_t synth_show(struct kobject *kobj, struct kobj_attribute *attr,
365 char *buf)
366{
367 int rv;
368
369 if (synth == NULL)
370 rv = sprintf(buf, "%s\n", "none");
371 else
372 rv = sprintf(buf, "%s\n", synth->name);
373 return rv;
374}
375
376/*
377 * This is called when a user requests to change synthesizers.
378 */
379static ssize_t synth_store(struct kobject *kobj, struct kobj_attribute *attr,
380 const char *buf, size_t count)
381{
382 int len;
383 char new_synth_name[10];
384
385 len = strlen(buf);
386 if (len < 2 || len > 9)
387 return -EINVAL;
388 strncpy(new_synth_name, buf, len);
389 if (new_synth_name[len - 1] == '\n')
390 len--;
391 new_synth_name[len] = '\0';
Samuel Thibaultca2beaf2013-01-02 02:37:40 +0100392 spk_strlwr(new_synth_name);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500393 if ((synth != NULL) && (!strcmp(new_synth_name, synth->name))) {
394 pr_warn("%s already in use\n", new_synth_name);
395 } else if (synth_init(new_synth_name) != 0) {
396 pr_warn("failed to init synth %s\n", new_synth_name);
397 return -ENODEV;
398 }
399 return count;
400}
401
402/*
403 * This is called when text is sent to the synth via the synth_direct file.
404 */
William Hubbsf1c6b7f2010-10-12 11:39:30 -0500405static ssize_t synth_direct_store(struct kobject *kobj,
406 struct kobj_attribute *attr, const char *buf, size_t count)
William Hubbsc6e3fd22010-10-07 13:20:02 -0500407{
408 u_char tmp[256];
409 int len;
410 int bytes;
411 const char *ptr = buf;
412
William Hubbsf1c6b7f2010-10-12 11:39:30 -0500413 if (!synth)
William Hubbsc6e3fd22010-10-07 13:20:02 -0500414 return -EPERM;
415
416 len = strlen(buf);
417 while (len > 0) {
418 bytes = min_t(size_t, len, 250);
419 strncpy(tmp, ptr, bytes);
420 tmp[bytes] = '\0';
Andy Shevchenko576d7422013-04-30 15:27:31 -0700421 string_unescape_any_inplace(tmp);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500422 synth_printf("%s", tmp);
423 ptr += bytes;
424 len -= bytes;
425 }
426 return count;
427}
428
429/*
430 * This function is called when a user reads the version.
431 */
432static ssize_t version_show(struct kobject *kobj, struct kobj_attribute *attr,
433 char *buf)
434{
435 char *cp;
436
437 cp = buf;
438 cp += sprintf(cp, "Speakup version %s\n", SPEAKUP_VERSION);
439 if (synth)
440 cp += sprintf(cp, "%s synthesizer driver version %s\n",
441 synth->name, synth->version);
442 return cp - buf;
443}
444
445/*
446 * This is called when a user reads the punctuation settings.
447 */
448static ssize_t punc_show(struct kobject *kobj, struct kobj_attribute *attr,
449 char *buf)
450{
451 int i;
452 char *cp = buf;
453 struct st_var_header *p_header;
454 struct punc_var_t *var;
455 struct st_bits_data *pb;
456 short mask;
457 unsigned long flags;
458
Samuel Thibaultca2beaf2013-01-02 02:37:40 +0100459 p_header = spk_var_header_by_name(attr->attr.name);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500460 if (p_header == NULL) {
William Hubbsf1c6b7f2010-10-12 11:39:30 -0500461 pr_warn("p_header is null, attr->attr.name is %s\n",
462 attr->attr.name);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500463 return -EINVAL;
464 }
465
Samuel Thibaultca2beaf2013-01-02 02:37:40 +0100466 var = spk_get_punc_var(p_header->var_id);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500467 if (var == NULL) {
468 pr_warn("var is null, p_header->var_id is %i\n",
469 p_header->var_id);
470 return -EINVAL;
471 }
472
William Hubbsc2d7f742013-05-13 00:02:53 -0500473 spin_lock_irqsave(&speakup_info.spinlock, flags);
Samuel Thibaultca2beaf2013-01-02 02:37:40 +0100474 pb = (struct st_bits_data *) &spk_punc_info[var->value];
William Hubbsc6e3fd22010-10-07 13:20:02 -0500475 mask = pb->mask;
476 for (i = 33; i < 128; i++) {
477 if (!(spk_chartab[i]&mask))
478 continue;
479 *cp++ = (char)i;
480 }
William Hubbsc2d7f742013-05-13 00:02:53 -0500481 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500482 return cp-buf;
483}
484
485/*
486 * This is called when a user changes the punctuation settings.
487 */
488static ssize_t punc_store(struct kobject *kobj, struct kobj_attribute *attr,
489 const char *buf, size_t count)
490{
491 int x;
492 struct st_var_header *p_header;
493 struct punc_var_t *var;
494 char punc_buf[100];
495 unsigned long flags;
496
497 x = strlen(buf);
498 if (x < 1 || x > 99)
499 return -EINVAL;
500
Samuel Thibaultca2beaf2013-01-02 02:37:40 +0100501 p_header = spk_var_header_by_name(attr->attr.name);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500502 if (p_header == NULL) {
William Hubbsf1c6b7f2010-10-12 11:39:30 -0500503 pr_warn("p_header is null, attr->attr.name is %s\n",
504 attr->attr.name);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500505 return -EINVAL;
506 }
507
Samuel Thibaultca2beaf2013-01-02 02:37:40 +0100508 var = spk_get_punc_var(p_header->var_id);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500509 if (var == NULL) {
510 pr_warn("var is null, p_header->var_id is %i\n",
511 p_header->var_id);
512 return -EINVAL;
513 }
514
515 strncpy(punc_buf, buf, x);
516
517 while (x && punc_buf[x - 1] == '\n')
518 x--;
519 punc_buf[x] = '\0';
520
William Hubbsc2d7f742013-05-13 00:02:53 -0500521 spin_lock_irqsave(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500522
523 if (*punc_buf == 'd' || *punc_buf == 'r')
Sachin Kamataee175f2013-05-22 14:37:24 +0530524 x = spk_set_mask_bits(NULL, var->value, 3);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500525 else
Samuel Thibaultca2beaf2013-01-02 02:37:40 +0100526 x = spk_set_mask_bits(punc_buf, var->value, 3);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500527
William Hubbsc2d7f742013-05-13 00:02:53 -0500528 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500529 return count;
530}
531
532/*
533 * This function is called when a user reads one of the variable parameters.
534 */
535ssize_t spk_var_show(struct kobject *kobj, struct kobj_attribute *attr,
536 char *buf)
537{
538 int rv = 0;
539 struct st_var_header *param;
540 struct var_t *var;
541 char *cp1;
542 char *cp;
543 char ch;
544 unsigned long flags;
545
Samuel Thibaultca2beaf2013-01-02 02:37:40 +0100546 param = spk_var_header_by_name(attr->attr.name);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500547 if (param == NULL)
548 return -EINVAL;
549
William Hubbsc2d7f742013-05-13 00:02:53 -0500550 spin_lock_irqsave(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500551 var = (struct var_t *) param->data;
552 switch (param->var_type) {
553 case VAR_NUM:
554 case VAR_TIME:
555 if (var)
556 rv = sprintf(buf, "%i\n", var->u.n.value);
557 else
558 rv = sprintf(buf, "0\n");
559 break;
560 case VAR_STRING:
561 if (var) {
562 cp1 = buf;
563 *cp1++ = '"';
564 for (cp = (char *)param->p_val; (ch = *cp); cp++) {
565 if (ch >= ' ' && ch < '~')
566 *cp1++ = ch;
567 else
568 cp1 += sprintf(cp1, "\\""x%02x", ch);
569 }
570 *cp1++ = '"';
571 *cp1++ = '\n';
572 *cp1 = '\0';
573 rv = cp1-buf;
574 } else {
575 rv = sprintf(buf, "\"\"\n");
576 }
577 break;
578 default:
579 rv = sprintf(buf, "Bad parameter %s, type %i\n",
580 param->name, param->var_type);
581 break;
582 }
William Hubbsc2d7f742013-05-13 00:02:53 -0500583 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500584 return rv;
585}
586EXPORT_SYMBOL_GPL(spk_var_show);
587
588/*
Raphael S.Carvalhob71600b2013-09-02 19:20:18 -0300589 * Used to reset either default_pitch or default_vol.
590 */
591static inline void spk_reset_default_value(char *header_name,
592 int *synth_default_value, int idx)
593{
594 struct st_var_header *param;
595
596 if (synth && synth_default_value) {
597 param = spk_var_header_by_name(header_name);
598 if (param) {
599 spk_set_num_var(synth_default_value[idx],
600 param, E_NEW_DEFAULT);
601 spk_set_num_var(0, param, E_DEFAULT);
602 pr_info("%s reset to default value\n", param->name);
603 }
604 }
605}
606
607/*
William Hubbsc6e3fd22010-10-07 13:20:02 -0500608 * This function is called when a user echos a value to one of the
609 * variable parameters.
610 */
611ssize_t spk_var_store(struct kobject *kobj, struct kobj_attribute *attr,
612 const char *buf, size_t count)
613{
614 struct st_var_header *param;
615 int ret;
616 int len;
617 char *cp;
618 struct var_t *var_data;
619 int value;
620 unsigned long flags;
621
Samuel Thibaultca2beaf2013-01-02 02:37:40 +0100622 param = spk_var_header_by_name(attr->attr.name);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500623 if (param == NULL)
624 return -EINVAL;
625 if (param->data == NULL)
626 return 0;
627 ret = 0;
Andy Shevchenko576d7422013-04-30 15:27:31 -0700628 cp = (char *)buf;
629 string_unescape_any_inplace(cp);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500630
William Hubbsc2d7f742013-05-13 00:02:53 -0500631 spin_lock_irqsave(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500632 switch (param->var_type) {
633 case VAR_NUM:
634 case VAR_TIME:
635 if (*cp == 'd' || *cp == 'r' || *cp == '\0')
636 len = E_DEFAULT;
637 else if (*cp == '+' || *cp == '-')
638 len = E_INC;
639 else
640 len = E_SET;
Andy Shevchenko1627ab92013-03-28 11:02:43 +0200641 value = simple_strtol(cp, NULL, 10);
Samuel Thibaultca2beaf2013-01-02 02:37:40 +0100642 ret = spk_set_num_var(value, param, len);
Andy Shevchenko6a48f882013-03-28 11:02:44 +0200643 if (ret == -ERANGE) {
William Hubbsc6e3fd22010-10-07 13:20:02 -0500644 var_data = param->data;
645 pr_warn("value for %s out of range, expect %d to %d\n",
Raphael S.Carvalhob71600b2013-09-02 19:20:18 -0300646 param->name,
William Hubbsc6e3fd22010-10-07 13:20:02 -0500647 var_data->u.n.low, var_data->u.n.high);
648 }
Raphael S.Carvalhob71600b2013-09-02 19:20:18 -0300649
650 /*
651 * If voice was just changed, we might need to reset our default
652 * pitch and volume.
653 */
654 if (param->var_id == VOICE) {
655 spk_reset_default_value("pitch", synth->default_pitch,
656 value);
657 spk_reset_default_value("vol", synth->default_vol,
658 value);
659 }
William Hubbsc6e3fd22010-10-07 13:20:02 -0500660 break;
661 case VAR_STRING:
Raphael S.Carvalhob71600b2013-09-02 19:20:18 -0300662 len = strlen(cp);
663 if ((len >= 1) && (cp[len - 1] == '\n'))
William Hubbsc6e3fd22010-10-07 13:20:02 -0500664 --len;
Raphael S.Carvalhob71600b2013-09-02 19:20:18 -0300665 if ((len >= 2) && (cp[0] == '"') && (cp[len - 1] == '"')) {
666 ++cp;
William Hubbsc6e3fd22010-10-07 13:20:02 -0500667 len -= 2;
668 }
William Hubbsc6e3fd22010-10-07 13:20:02 -0500669 cp[len] = '\0';
Raphael S.Carvalhob71600b2013-09-02 19:20:18 -0300670 ret = spk_set_string_var(cp, param, len);
Andy Shevchenko6a48f882013-03-28 11:02:44 +0200671 if (ret == -E2BIG)
William Hubbsc6e3fd22010-10-07 13:20:02 -0500672 pr_warn("value too long for %s\n",
Raphael S.Carvalhob71600b2013-09-02 19:20:18 -0300673 param->name);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500674 break;
675 default:
676 pr_warn("%s unknown type %d\n",
677 param->name, (int)param->var_type);
678 break;
679 }
William Hubbsc2d7f742013-05-13 00:02:53 -0500680 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500681
Andy Shevchenko6a48f882013-03-28 11:02:44 +0200682 if (ret == -ERESTART)
Raphael S.Carvalhob71600b2013-09-02 19:20:18 -0300683 pr_info("%s reset to default value\n", param->name);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500684 return count;
685}
686EXPORT_SYMBOL_GPL(spk_var_store);
687
688/*
689 * Functions for reading and writing lists of i18n messages. Incomplete.
690 */
691
692static ssize_t message_show_helper(char *buf, enum msg_index_t first,
693 enum msg_index_t last)
694{
695 size_t bufsize = PAGE_SIZE;
696 char *buf_pointer = buf;
697 int printed;
698 enum msg_index_t cursor;
699 int index = 0;
700 *buf_pointer = '\0'; /* buf_pointer always looking at a NUL byte. */
701
702 for (cursor = first; cursor <= last; cursor++, index++) {
703 if (bufsize <= 1)
704 break;
705 printed = scnprintf(buf_pointer, bufsize, "%d\t%s\n",
Samuel Thibaultca2beaf2013-01-02 02:37:40 +0100706 index, spk_msg_get(cursor));
William Hubbsc6e3fd22010-10-07 13:20:02 -0500707 buf_pointer += printed;
708 bufsize -= printed;
709 }
710
711 return buf_pointer - buf;
712}
713
714static void report_msg_status(int reset, int received, int used,
715 int rejected, char *groupname)
716{
717 int len;
718 char buf[160];
719
720 if (reset) {
721 pr_info("i18n messages from group %s reset to defaults\n",
722 groupname);
William Hubbsf1c6b7f2010-10-12 11:39:30 -0500723 } else if (received) {
William Hubbsc6e3fd22010-10-07 13:20:02 -0500724 len = snprintf(buf, sizeof(buf),
725 " updated %d of %d i18n messages from group %s\n",
726 used, received, groupname);
727 if (rejected)
728 snprintf(buf + (len - 1), sizeof(buf) - (len - 1),
729 " with %d reject%s\n",
730 rejected, rejected > 1 ? "s" : "");
731 printk(buf);
732 }
733}
734
735static ssize_t message_store_helper(const char *buf, size_t count,
736 struct msg_group_t *group)
737{
738 char *cp = (char *) buf;
739 char *end = cp + count;
740 char *linefeed = NULL;
741 char *temp = NULL;
742 ssize_t msg_stored = 0;
743 ssize_t retval = count;
744 size_t desc_length = 0;
745 unsigned long index = 0;
746 int received = 0;
747 int used = 0;
748 int rejected = 0;
749 int reset = 0;
750 enum msg_index_t firstmessage = group->start;
751 enum msg_index_t lastmessage = group->end;
752 enum msg_index_t curmessage;
753
754 while (cp < end) {
755
756 while ((cp < end) && (*cp == ' ' || *cp == '\t'))
757 cp++;
758
759 if (cp == end)
760 break;
761 if (strchr("dDrR", *cp)) {
762 reset = 1;
763 break;
764 }
765 received++;
766
767 linefeed = strchr(cp, '\n');
768 if (!linefeed) {
769 rejected++;
770 break;
771 }
772
William Hubbsf1c6b7f2010-10-12 11:39:30 -0500773 if (!isdigit(*cp)) {
William Hubbsc6e3fd22010-10-07 13:20:02 -0500774 rejected++;
775 cp = linefeed + 1;
776 continue;
777 }
778
779 index = simple_strtoul(cp, &temp, 10);
780
781 while ((temp < linefeed) && (*temp == ' ' || *temp == '\t'))
782 temp++;
783
784 desc_length = linefeed - temp;
785 curmessage = firstmessage + index;
786
787 /*
788 * Note the check (curmessage < firstmessage). It is not
789 * redundant. Suppose that the user gave us an index
790 * equal to ULONG_MAX - 1. If firstmessage > 1, then
791 * firstmessage + index < firstmessage!
792 */
793
794 if ((curmessage < firstmessage) || (curmessage > lastmessage)) {
795 rejected++;
796 cp = linefeed + 1;
797 continue;
798 }
799
Samuel Thibaultca2beaf2013-01-02 02:37:40 +0100800 msg_stored = spk_msg_set(curmessage, temp, desc_length);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500801 if (msg_stored < 0) {
802 retval = msg_stored;
803 if (msg_stored == -ENOMEM)
804 reset = 1;
805 break;
806 } else {
807 used++;
808 }
809
810 cp = linefeed + 1;
811 }
812
813 if (reset)
Samuel Thibaultca2beaf2013-01-02 02:37:40 +0100814 spk_reset_msg_group(group);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500815
816 report_msg_status(reset, received, used, rejected, group->name);
817 return retval;
818}
819
820static ssize_t message_show(struct kobject *kobj,
821 struct kobj_attribute *attr, char *buf)
822{
823 ssize_t retval = 0;
Samuel Thibaultca2beaf2013-01-02 02:37:40 +0100824 struct msg_group_t *group = spk_find_msg_group(attr->attr.name);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500825 unsigned long flags;
826
William Hubbsf1c6b7f2010-10-12 11:39:30 -0500827 BUG_ON(!group);
William Hubbsc2d7f742013-05-13 00:02:53 -0500828 spin_lock_irqsave(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500829 retval = message_show_helper(buf, group->start, group->end);
William Hubbsc2d7f742013-05-13 00:02:53 -0500830 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500831 return retval;
832}
833
834static ssize_t message_store(struct kobject *kobj, struct kobj_attribute *attr,
835 const char *buf, size_t count)
836{
837 ssize_t retval = 0;
Samuel Thibaultca2beaf2013-01-02 02:37:40 +0100838 struct msg_group_t *group = spk_find_msg_group(attr->attr.name);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500839
William Hubbsf1c6b7f2010-10-12 11:39:30 -0500840 BUG_ON(!group);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500841 retval = message_store_helper(buf, count, group);
842 return retval;
843}
844
845/*
846 * Declare the attributes.
847 */
848static struct kobj_attribute keymap_attribute =
849 __ATTR(keymap, ROOT_W, keymap_show, keymap_store);
850static struct kobj_attribute silent_attribute =
851 __ATTR(silent, USER_W, NULL, silent_store);
852static struct kobj_attribute synth_attribute =
853 __ATTR(synth, USER_RW, synth_show, synth_store);
854static struct kobj_attribute synth_direct_attribute =
855 __ATTR(synth_direct, USER_W, NULL, synth_direct_store);
856static struct kobj_attribute version_attribute =
857 __ATTR_RO(version);
858
859static struct kobj_attribute delimiters_attribute =
860 __ATTR(delimiters, USER_RW, punc_show, punc_store);
861static struct kobj_attribute ex_num_attribute =
862 __ATTR(ex_num, USER_RW, punc_show, punc_store);
863static struct kobj_attribute punc_all_attribute =
864 __ATTR(punc_all, USER_RW, punc_show, punc_store);
865static struct kobj_attribute punc_most_attribute =
866 __ATTR(punc_most, USER_RW, punc_show, punc_store);
867static struct kobj_attribute punc_some_attribute =
868 __ATTR(punc_some, USER_RW, punc_show, punc_store);
869static struct kobj_attribute repeats_attribute =
870 __ATTR(repeats, USER_RW, punc_show, punc_store);
871
872static struct kobj_attribute attrib_bleep_attribute =
873 __ATTR(attrib_bleep, USER_RW, spk_var_show, spk_var_store);
874static struct kobj_attribute bell_pos_attribute =
875 __ATTR(bell_pos, USER_RW, spk_var_show, spk_var_store);
876static struct kobj_attribute bleep_time_attribute =
877 __ATTR(bleep_time, USER_RW, spk_var_show, spk_var_store);
878static struct kobj_attribute bleeps_attribute =
879 __ATTR(bleeps, USER_RW, spk_var_show, spk_var_store);
880static struct kobj_attribute cursor_time_attribute =
881 __ATTR(cursor_time, USER_RW, spk_var_show, spk_var_store);
882static struct kobj_attribute key_echo_attribute =
883 __ATTR(key_echo, USER_RW, spk_var_show, spk_var_store);
884static struct kobj_attribute no_interrupt_attribute =
885 __ATTR(no_interrupt, USER_RW, spk_var_show, spk_var_store);
886static struct kobj_attribute punc_level_attribute =
887 __ATTR(punc_level, USER_RW, spk_var_show, spk_var_store);
888static struct kobj_attribute reading_punc_attribute =
889 __ATTR(reading_punc, USER_RW, spk_var_show, spk_var_store);
890static struct kobj_attribute say_control_attribute =
891 __ATTR(say_control, USER_RW, spk_var_show, spk_var_store);
892static struct kobj_attribute say_word_ctl_attribute =
893 __ATTR(say_word_ctl, USER_RW, spk_var_show, spk_var_store);
894static struct kobj_attribute spell_delay_attribute =
895 __ATTR(spell_delay, USER_RW, spk_var_show, spk_var_store);
896
897/*
898 * These attributes are i18n related.
899 */
900static struct kobj_attribute announcements_attribute =
901 __ATTR(announcements, USER_RW, message_show, message_store);
902static struct kobj_attribute characters_attribute =
903 __ATTR(characters, USER_RW, chars_chartab_show, chars_chartab_store);
904static struct kobj_attribute chartab_attribute =
905 __ATTR(chartab, USER_RW, chars_chartab_show, chars_chartab_store);
906static struct kobj_attribute ctl_keys_attribute =
907 __ATTR(ctl_keys, USER_RW, message_show, message_store);
908static struct kobj_attribute colors_attribute =
909 __ATTR(colors, USER_RW, message_show, message_store);
910static struct kobj_attribute formatted_attribute =
911 __ATTR(formatted, USER_RW, message_show, message_store);
912static struct kobj_attribute function_names_attribute =
913 __ATTR(function_names, USER_RW, message_show, message_store);
914static struct kobj_attribute key_names_attribute =
915 __ATTR(key_names, USER_RW, message_show, message_store);
916static struct kobj_attribute states_attribute =
917 __ATTR(states, USER_RW, message_show, message_store);
918
919/*
920 * Create groups of attributes so that we can create and destroy them all
921 * at once.
922 */
923static struct attribute *main_attrs[] = {
924 &keymap_attribute.attr,
925 &silent_attribute.attr,
926 &synth_attribute.attr,
927 &synth_direct_attribute.attr,
928 &version_attribute.attr,
929 &delimiters_attribute.attr,
930 &ex_num_attribute.attr,
931 &punc_all_attribute.attr,
932 &punc_most_attribute.attr,
933 &punc_some_attribute.attr,
934 &repeats_attribute.attr,
935 &attrib_bleep_attribute.attr,
936 &bell_pos_attribute.attr,
937 &bleep_time_attribute.attr,
938 &bleeps_attribute.attr,
939 &cursor_time_attribute.attr,
940 &key_echo_attribute.attr,
941 &no_interrupt_attribute.attr,
942 &punc_level_attribute.attr,
943 &reading_punc_attribute.attr,
944 &say_control_attribute.attr,
945 &say_word_ctl_attribute.attr,
946 &spell_delay_attribute.attr,
947 NULL,
948};
949
950static struct attribute *i18n_attrs[] = {
951 &announcements_attribute.attr,
952 &characters_attribute.attr,
953 &chartab_attribute.attr,
954 &ctl_keys_attribute.attr,
955 &colors_attribute.attr,
956 &formatted_attribute.attr,
957 &function_names_attribute.attr,
958 &key_names_attribute.attr,
959 &states_attribute.attr,
960 NULL,
961};
962
963/*
964 * An unnamed attribute group will put all of the attributes directly in
965 * the kobject directory. If we specify a name, a subdirectory will be
966 * created for the attributes with the directory being the name of the
967 * attribute group.
968 */
969static struct attribute_group main_attr_group = {
970 .attrs = main_attrs,
971};
972
973static struct attribute_group i18n_attr_group = {
974 .attrs = i18n_attrs,
975 .name = "i18n",
976};
977
978static struct kobject *accessibility_kobj;
979struct kobject *speakup_kobj;
980
981int speakup_kobj_init(void)
982{
983 int retval;
984
985 /*
986 * Create a simple kobject with the name of "accessibility",
987 * located under /sys/
988 *
989 * As this is a simple directory, no uevent will be sent to
990 * userspace. That is why this function should not be used for
991 * any type of dynamic kobjects, where the name and number are
992 * not known ahead of time.
993 */
994 accessibility_kobj = kobject_create_and_add("accessibility", NULL);
William Hubbs7959d552010-12-16 13:26:58 -0600995 if (!accessibility_kobj) {
996 retval = -ENOMEM;
997 goto out;
998 }
William Hubbsc6e3fd22010-10-07 13:20:02 -0500999
1000 speakup_kobj = kobject_create_and_add("speakup", accessibility_kobj);
1001 if (!speakup_kobj) {
Vasiliy Kulikov6a564862010-10-17 18:51:53 +04001002 retval = -ENOMEM;
1003 goto err_acc;
William Hubbsc6e3fd22010-10-07 13:20:02 -05001004 }
1005
1006 /* Create the files associated with this kobject */
1007 retval = sysfs_create_group(speakup_kobj, &main_attr_group);
1008 if (retval)
Vasiliy Kulikov6a564862010-10-17 18:51:53 +04001009 goto err_speakup;
William Hubbsc6e3fd22010-10-07 13:20:02 -05001010
1011 retval = sysfs_create_group(speakup_kobj, &i18n_attr_group);
1012 if (retval)
Vasiliy Kulikov6a564862010-10-17 18:51:53 +04001013 goto err_group;
William Hubbsc6e3fd22010-10-07 13:20:02 -05001014
William Hubbs7959d552010-12-16 13:26:58 -06001015 goto out;
Vasiliy Kulikov6a564862010-10-17 18:51:53 +04001016
1017err_group:
1018 sysfs_remove_group(speakup_kobj, &main_attr_group);
1019err_speakup:
1020 kobject_put(speakup_kobj);
1021err_acc:
1022 kobject_put(accessibility_kobj);
William Hubbs7959d552010-12-16 13:26:58 -06001023out:
William Hubbsc6e3fd22010-10-07 13:20:02 -05001024 return retval;
1025}
1026
1027void speakup_kobj_exit(void)
1028{
Vasiliy Kulikov6a564862010-10-17 18:51:53 +04001029 sysfs_remove_group(speakup_kobj, &i18n_attr_group);
1030 sysfs_remove_group(speakup_kobj, &main_attr_group);
William Hubbsc6e3fd22010-10-07 13:20:02 -05001031 kobject_put(speakup_kobj);
1032 kobject_put(accessibility_kobj);
1033}