Linux 4.16.11
[linux/fpc-iii.git] / drivers / staging / speakup / kobjects.c
blobf1f90222186b1ec47bcbd95e04f7ffe4d46995c6
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Speakup kobject implementation
5 * Copyright (C) 2009 William Hubbs
7 * This code is based on kobject-example.c, which came with linux 2.6.x.
9 * Copyright (C) 2004-2007 Greg Kroah-Hartman <greg@kroah.com>
10 * Copyright (C) 2007 Novell Inc.
12 * Released under the GPL version 2 only.
15 #include <linux/slab.h> /* For kmalloc. */
16 #include <linux/kernel.h>
17 #include <linux/kobject.h>
18 #include <linux/string.h>
19 #include <linux/string_helpers.h>
20 #include <linux/sysfs.h>
21 #include <linux/ctype.h>
23 #include "speakup.h"
24 #include "spk_priv.h"
27 * This is called when a user reads the characters or chartab sys file.
29 static ssize_t chars_chartab_show(struct kobject *kobj,
30 struct kobj_attribute *attr, char *buf)
32 int i;
33 int len = 0;
34 char *cp;
35 char *buf_pointer = buf;
36 size_t bufsize = PAGE_SIZE;
37 unsigned long flags;
39 spin_lock_irqsave(&speakup_info.spinlock, flags);
40 *buf_pointer = '\0';
41 for (i = 0; i < 256; i++) {
42 if (bufsize <= 1)
43 break;
44 if (strcmp("characters", attr->attr.name) == 0) {
45 len = scnprintf(buf_pointer, bufsize, "%d\t%s\n",
46 i, spk_characters[i]);
47 } else { /* show chartab entry */
48 if (IS_TYPE(i, B_CTL))
49 cp = "B_CTL";
50 else if (IS_TYPE(i, WDLM))
51 cp = "WDLM";
52 else if (IS_TYPE(i, A_PUNC))
53 cp = "A_PUNC";
54 else if (IS_TYPE(i, PUNC))
55 cp = "PUNC";
56 else if (IS_TYPE(i, NUM))
57 cp = "NUM";
58 else if (IS_TYPE(i, A_CAP))
59 cp = "A_CAP";
60 else if (IS_TYPE(i, ALPHA))
61 cp = "ALPHA";
62 else if (IS_TYPE(i, B_CAPSYM))
63 cp = "B_CAPSYM";
64 else if (IS_TYPE(i, B_SYM))
65 cp = "B_SYM";
66 else
67 cp = "0";
68 len =
69 scnprintf(buf_pointer, bufsize, "%d\t%s\n", i, cp);
71 bufsize -= len;
72 buf_pointer += len;
74 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
75 return buf_pointer - buf;
79 * Print informational messages or warnings after updating
80 * character descriptions or chartab entries.
82 static void report_char_chartab_status(int reset, int received, int used,
83 int rejected, int do_characters)
85 static char const *object_type[] = {
86 "character class entries",
87 "character descriptions",
89 int len;
90 char buf[80];
92 if (reset) {
93 pr_info("%s reset to defaults\n", object_type[do_characters]);
94 } else if (received) {
95 len = snprintf(buf, sizeof(buf),
96 " updated %d of %d %s\n",
97 used, received, object_type[do_characters]);
98 if (rejected)
99 snprintf(buf + (len - 1), sizeof(buf) - (len - 1),
100 " with %d reject%s\n",
101 rejected, rejected > 1 ? "s" : "");
102 printk(buf);
107 * This is called when a user changes the characters or chartab parameters.
109 static ssize_t chars_chartab_store(struct kobject *kobj,
110 struct kobj_attribute *attr,
111 const char *buf, size_t count)
113 char *cp = (char *)buf;
114 char *end = cp + count; /* the null at the end of the buffer */
115 char *linefeed = NULL;
116 char keyword[MAX_DESC_LEN + 1];
117 char *outptr = NULL; /* Will hold keyword or desc. */
118 char *temp = NULL;
119 char *desc = NULL;
120 ssize_t retval = count;
121 unsigned long flags;
122 unsigned long index = 0;
123 int charclass = 0;
124 int received = 0;
125 int used = 0;
126 int rejected = 0;
127 int reset = 0;
128 int do_characters = !strcmp(attr->attr.name, "characters");
129 size_t desc_length = 0;
130 int i;
132 spin_lock_irqsave(&speakup_info.spinlock, flags);
133 while (cp < end) {
134 while ((cp < end) && (*cp == ' ' || *cp == '\t'))
135 cp++;
137 if (cp == end)
138 break;
139 if ((*cp == '\n') || strchr("dDrR", *cp)) {
140 reset = 1;
141 break;
143 received++;
145 linefeed = strchr(cp, '\n');
146 if (!linefeed) {
147 rejected++;
148 break;
151 if (!isdigit(*cp)) {
152 rejected++;
153 cp = linefeed + 1;
154 continue;
157 index = simple_strtoul(cp, &temp, 10);
158 if (index > 255) {
159 rejected++;
160 cp = linefeed + 1;
161 continue;
164 while ((temp < linefeed) && (*temp == ' ' || *temp == '\t'))
165 temp++;
167 desc_length = linefeed - temp;
168 if (desc_length > MAX_DESC_LEN) {
169 rejected++;
170 cp = linefeed + 1;
171 continue;
173 if (do_characters) {
174 desc = kmalloc(desc_length + 1, GFP_ATOMIC);
175 if (!desc) {
176 retval = -ENOMEM;
177 reset = 1; /* just reset on error. */
178 break;
180 outptr = desc;
181 } else {
182 outptr = keyword;
185 for (i = 0; i < desc_length; i++)
186 outptr[i] = temp[i];
187 outptr[desc_length] = '\0';
189 if (do_characters) {
190 if (spk_characters[index] != spk_default_chars[index])
191 kfree(spk_characters[index]);
192 spk_characters[index] = desc;
193 used++;
194 } else {
195 charclass = spk_chartab_get_value(keyword);
196 if (charclass == 0) {
197 rejected++;
198 cp = linefeed + 1;
199 continue;
201 if (charclass != spk_chartab[index]) {
202 spk_chartab[index] = charclass;
203 used++;
206 cp = linefeed + 1;
209 if (reset) {
210 if (do_characters)
211 spk_reset_default_chars();
212 else
213 spk_reset_default_chartab();
216 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
217 report_char_chartab_status(reset, received, used, rejected,
218 do_characters);
219 return retval;
223 * This is called when a user reads the keymap parameter.
225 static ssize_t keymap_show(struct kobject *kobj, struct kobj_attribute *attr,
226 char *buf)
228 char *cp = buf;
229 int i;
230 int n;
231 int num_keys;
232 int nstates;
233 u_char *cp1;
234 u_char ch;
235 unsigned long flags;
237 spin_lock_irqsave(&speakup_info.spinlock, flags);
238 cp1 = spk_key_buf + SHIFT_TBL_SIZE;
239 num_keys = (int)(*cp1);
240 nstates = (int)cp1[1];
241 cp += sprintf(cp, "%d, %d, %d,\n", KEY_MAP_VER, num_keys, nstates);
242 cp1 += 2; /* now pointing at shift states */
243 /* dump num_keys+1 as first row is shift states + flags,
244 * each subsequent row is key + states
246 for (n = 0; n <= num_keys; n++) {
247 for (i = 0; i <= nstates; i++) {
248 ch = *cp1++;
249 cp += sprintf(cp, "%d,", (int)ch);
250 *cp++ = (i < nstates) ? SPACE : '\n';
253 cp += sprintf(cp, "0, %d\n", KEY_MAP_VER);
254 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
255 return (int)(cp - buf);
259 * This is called when a user changes the keymap parameter.
261 static ssize_t keymap_store(struct kobject *kobj, struct kobj_attribute *attr,
262 const char *buf, size_t count)
264 int i;
265 ssize_t ret = count;
266 char *in_buff = NULL;
267 char *cp;
268 u_char *cp1;
269 unsigned long flags;
271 spin_lock_irqsave(&speakup_info.spinlock, flags);
272 in_buff = kmemdup(buf, count + 1, GFP_ATOMIC);
273 if (!in_buff) {
274 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
275 return -ENOMEM;
277 if (strchr("dDrR", *in_buff)) {
278 spk_set_key_info(spk_key_defaults, spk_key_buf);
279 pr_info("keymap set to default values\n");
280 kfree(in_buff);
281 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
282 return count;
284 if (in_buff[count - 1] == '\n')
285 in_buff[count - 1] = '\0';
286 cp = in_buff;
287 cp1 = (u_char *)in_buff;
288 for (i = 0; i < 3; i++) {
289 cp = spk_s2uchar(cp, cp1);
290 cp1++;
292 i = (int)cp1[-2] + 1;
293 i *= (int)cp1[-1] + 1;
294 i += 2; /* 0 and last map ver */
295 if (cp1[-3] != KEY_MAP_VER || cp1[-1] > 10 ||
296 i + SHIFT_TBL_SIZE + 4 >= sizeof(spk_key_buf)) {
297 pr_warn("i %d %d %d %d\n", i,
298 (int)cp1[-3], (int)cp1[-2], (int)cp1[-1]);
299 kfree(in_buff);
300 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
301 return -EINVAL;
303 while (--i >= 0) {
304 cp = spk_s2uchar(cp, cp1);
305 cp1++;
306 if (!(*cp))
307 break;
309 if (i != 0 || cp1[-1] != KEY_MAP_VER || cp1[-2] != 0) {
310 ret = -EINVAL;
311 pr_warn("end %d %d %d %d\n", i,
312 (int)cp1[-3], (int)cp1[-2], (int)cp1[-1]);
313 } else {
314 if (spk_set_key_info(in_buff, spk_key_buf)) {
315 spk_set_key_info(spk_key_defaults, spk_key_buf);
316 ret = -EINVAL;
317 pr_warn("set key failed\n");
320 kfree(in_buff);
321 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
322 return ret;
326 * This is called when a user changes the value of the silent parameter.
328 static ssize_t silent_store(struct kobject *kobj, struct kobj_attribute *attr,
329 const char *buf, size_t count)
331 int len;
332 struct vc_data *vc = vc_cons[fg_console].d;
333 char ch = 0;
334 char shut;
335 unsigned long flags;
337 len = strlen(buf);
338 if (len > 0 && len < 3) {
339 ch = buf[0];
340 if (ch == '\n')
341 ch = '0';
343 if (ch < '0' || ch > '7') {
344 pr_warn("silent value '%c' not in range (0,7)\n", ch);
345 return -EINVAL;
347 spin_lock_irqsave(&speakup_info.spinlock, flags);
348 if (ch & 2) {
349 shut = 1;
350 spk_do_flush();
351 } else {
352 shut = 0;
354 if (ch & 4)
355 shut |= 0x40;
356 if (ch & 1)
357 spk_shut_up |= shut;
358 else
359 spk_shut_up &= ~shut;
360 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
361 return count;
365 * This is called when a user reads the synth setting.
367 static ssize_t synth_show(struct kobject *kobj, struct kobj_attribute *attr,
368 char *buf)
370 int rv;
372 if (!synth)
373 rv = sprintf(buf, "%s\n", "none");
374 else
375 rv = sprintf(buf, "%s\n", synth->name);
376 return rv;
380 * This is called when a user requests to change synthesizers.
382 static ssize_t synth_store(struct kobject *kobj, struct kobj_attribute *attr,
383 const char *buf, size_t count)
385 int len;
386 char new_synth_name[10];
388 len = strlen(buf);
389 if (len < 2 || len > 9)
390 return -EINVAL;
391 strncpy(new_synth_name, buf, len);
392 if (new_synth_name[len - 1] == '\n')
393 len--;
394 new_synth_name[len] = '\0';
395 spk_strlwr(new_synth_name);
396 if (synth && !strcmp(new_synth_name, synth->name)) {
397 pr_warn("%s already in use\n", new_synth_name);
398 } else if (synth_init(new_synth_name) != 0) {
399 pr_warn("failed to init synth %s\n", new_synth_name);
400 return -ENODEV;
402 return count;
406 * This is called when text is sent to the synth via the synth_direct file.
408 static ssize_t synth_direct_store(struct kobject *kobj,
409 struct kobj_attribute *attr,
410 const char *buf, size_t count)
412 u_char tmp[256];
413 int len;
414 int bytes;
415 const char *ptr = buf;
416 unsigned long flags;
418 if (!synth)
419 return -EPERM;
421 len = strlen(buf);
422 spin_lock_irqsave(&speakup_info.spinlock, flags);
423 while (len > 0) {
424 bytes = min_t(size_t, len, 250);
425 strncpy(tmp, ptr, bytes);
426 tmp[bytes] = '\0';
427 string_unescape_any_inplace(tmp);
428 synth_printf("%s", tmp);
429 ptr += bytes;
430 len -= bytes;
432 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
433 return count;
437 * This function is called when a user reads the version.
439 static ssize_t version_show(struct kobject *kobj, struct kobj_attribute *attr,
440 char *buf)
442 char *cp;
444 cp = buf;
445 cp += sprintf(cp, "Speakup version %s\n", SPEAKUP_VERSION);
446 if (synth)
447 cp += sprintf(cp, "%s synthesizer driver version %s\n",
448 synth->name, synth->version);
449 return cp - buf;
453 * This is called when a user reads the punctuation settings.
455 static ssize_t punc_show(struct kobject *kobj, struct kobj_attribute *attr,
456 char *buf)
458 int i;
459 char *cp = buf;
460 struct st_var_header *p_header;
461 struct punc_var_t *var;
462 struct st_bits_data *pb;
463 short mask;
464 unsigned long flags;
466 p_header = spk_var_header_by_name(attr->attr.name);
467 if (!p_header) {
468 pr_warn("p_header is null, attr->attr.name is %s\n",
469 attr->attr.name);
470 return -EINVAL;
473 var = spk_get_punc_var(p_header->var_id);
474 if (!var) {
475 pr_warn("var is null, p_header->var_id is %i\n",
476 p_header->var_id);
477 return -EINVAL;
480 spin_lock_irqsave(&speakup_info.spinlock, flags);
481 pb = (struct st_bits_data *)&spk_punc_info[var->value];
482 mask = pb->mask;
483 for (i = 33; i < 128; i++) {
484 if (!(spk_chartab[i] & mask))
485 continue;
486 *cp++ = (char)i;
488 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
489 return cp - buf;
493 * This is called when a user changes the punctuation settings.
495 static ssize_t punc_store(struct kobject *kobj, struct kobj_attribute *attr,
496 const char *buf, size_t count)
498 int x;
499 struct st_var_header *p_header;
500 struct punc_var_t *var;
501 char punc_buf[100];
502 unsigned long flags;
504 x = strlen(buf);
505 if (x < 1 || x > 99)
506 return -EINVAL;
508 p_header = spk_var_header_by_name(attr->attr.name);
509 if (!p_header) {
510 pr_warn("p_header is null, attr->attr.name is %s\n",
511 attr->attr.name);
512 return -EINVAL;
515 var = spk_get_punc_var(p_header->var_id);
516 if (!var) {
517 pr_warn("var is null, p_header->var_id is %i\n",
518 p_header->var_id);
519 return -EINVAL;
522 strncpy(punc_buf, buf, x);
524 while (x && punc_buf[x - 1] == '\n')
525 x--;
526 punc_buf[x] = '\0';
528 spin_lock_irqsave(&speakup_info.spinlock, flags);
530 if (*punc_buf == 'd' || *punc_buf == 'r')
531 x = spk_set_mask_bits(NULL, var->value, 3);
532 else
533 x = spk_set_mask_bits(punc_buf, var->value, 3);
535 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
536 return count;
540 * This function is called when a user reads one of the variable parameters.
542 ssize_t spk_var_show(struct kobject *kobj, struct kobj_attribute *attr,
543 char *buf)
545 int rv = 0;
546 struct st_var_header *param;
547 struct var_t *var;
548 char *cp1;
549 char *cp;
550 char ch;
551 unsigned long flags;
553 param = spk_var_header_by_name(attr->attr.name);
554 if (!param)
555 return -EINVAL;
557 spin_lock_irqsave(&speakup_info.spinlock, flags);
558 var = (struct var_t *)param->data;
559 switch (param->var_type) {
560 case VAR_NUM:
561 case VAR_TIME:
562 if (var)
563 rv = sprintf(buf, "%i\n", var->u.n.value);
564 else
565 rv = sprintf(buf, "0\n");
566 break;
567 case VAR_STRING:
568 if (var) {
569 cp1 = buf;
570 *cp1++ = '"';
571 for (cp = (char *)param->p_val; (ch = *cp); cp++) {
572 if (ch >= ' ' && ch < '~')
573 *cp1++ = ch;
574 else
575 cp1 += sprintf(cp1, "\\x%02x", ch);
577 *cp1++ = '"';
578 *cp1++ = '\n';
579 *cp1 = '\0';
580 rv = cp1 - buf;
581 } else {
582 rv = sprintf(buf, "\"\"\n");
584 break;
585 default:
586 rv = sprintf(buf, "Bad parameter %s, type %i\n",
587 param->name, param->var_type);
588 break;
590 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
591 return rv;
593 EXPORT_SYMBOL_GPL(spk_var_show);
596 * Used to reset either default_pitch or default_vol.
598 static inline void spk_reset_default_value(char *header_name,
599 int *synth_default_value, int idx)
601 struct st_var_header *param;
603 if (synth && synth_default_value) {
604 param = spk_var_header_by_name(header_name);
605 if (param) {
606 spk_set_num_var(synth_default_value[idx],
607 param, E_NEW_DEFAULT);
608 spk_set_num_var(0, param, E_DEFAULT);
609 pr_info("%s reset to default value\n", param->name);
615 * This function is called when a user echos a value to one of the
616 * variable parameters.
618 ssize_t spk_var_store(struct kobject *kobj, struct kobj_attribute *attr,
619 const char *buf, size_t count)
621 struct st_var_header *param;
622 int ret;
623 int len;
624 char *cp;
625 struct var_t *var_data;
626 long value;
627 unsigned long flags;
629 param = spk_var_header_by_name(attr->attr.name);
630 if (!param)
631 return -EINVAL;
632 if (!param->data)
633 return 0;
634 ret = 0;
635 cp = (char *)buf;
636 string_unescape_any_inplace(cp);
638 spin_lock_irqsave(&speakup_info.spinlock, flags);
639 switch (param->var_type) {
640 case VAR_NUM:
641 case VAR_TIME:
642 if (*cp == 'd' || *cp == 'r' || *cp == '\0')
643 len = E_DEFAULT;
644 else if (*cp == '+' || *cp == '-')
645 len = E_INC;
646 else
647 len = E_SET;
648 if (kstrtol(cp, 10, &value) == 0)
649 ret = spk_set_num_var(value, param, len);
650 else
651 pr_warn("overflow or parsing error has occurred");
652 if (ret == -ERANGE) {
653 var_data = param->data;
654 pr_warn("value for %s out of range, expect %d to %d\n",
655 param->name,
656 var_data->u.n.low, var_data->u.n.high);
660 * If voice was just changed, we might need to reset our default
661 * pitch and volume.
663 if (param->var_id == VOICE && synth &&
664 (ret == 0 || ret == -ERESTART)) {
665 var_data = param->data;
666 value = var_data->u.n.value;
667 spk_reset_default_value("pitch", synth->default_pitch,
668 value);
669 spk_reset_default_value("vol", synth->default_vol,
670 value);
672 break;
673 case VAR_STRING:
674 len = strlen(cp);
675 if ((len >= 1) && (cp[len - 1] == '\n'))
676 --len;
677 if ((len >= 2) && (cp[0] == '"') && (cp[len - 1] == '"')) {
678 ++cp;
679 len -= 2;
681 cp[len] = '\0';
682 ret = spk_set_string_var(cp, param, len);
683 if (ret == -E2BIG)
684 pr_warn("value too long for %s\n",
685 param->name);
686 break;
687 default:
688 pr_warn("%s unknown type %d\n",
689 param->name, (int)param->var_type);
690 break;
692 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
694 if (ret == -ERESTART)
695 pr_info("%s reset to default value\n", param->name);
696 return count;
698 EXPORT_SYMBOL_GPL(spk_var_store);
701 * Functions for reading and writing lists of i18n messages. Incomplete.
704 static ssize_t message_show_helper(char *buf, enum msg_index_t first,
705 enum msg_index_t last)
707 size_t bufsize = PAGE_SIZE;
708 char *buf_pointer = buf;
709 int printed;
710 enum msg_index_t cursor;
711 int index = 0;
712 *buf_pointer = '\0'; /* buf_pointer always looking at a NUL byte. */
714 for (cursor = first; cursor <= last; cursor++, index++) {
715 if (bufsize <= 1)
716 break;
717 printed = scnprintf(buf_pointer, bufsize, "%d\t%s\n",
718 index, spk_msg_get(cursor));
719 buf_pointer += printed;
720 bufsize -= printed;
723 return buf_pointer - buf;
726 static void report_msg_status(int reset, int received, int used,
727 int rejected, char *groupname)
729 int len;
730 char buf[160];
732 if (reset) {
733 pr_info("i18n messages from group %s reset to defaults\n",
734 groupname);
735 } else if (received) {
736 len = snprintf(buf, sizeof(buf),
737 " updated %d of %d i18n messages from group %s\n",
738 used, received, groupname);
739 if (rejected)
740 snprintf(buf + (len - 1), sizeof(buf) - (len - 1),
741 " with %d reject%s\n",
742 rejected, rejected > 1 ? "s" : "");
743 printk(buf);
747 static ssize_t message_store_helper(const char *buf, size_t count,
748 struct msg_group_t *group)
750 char *cp = (char *)buf;
751 char *end = cp + count;
752 char *linefeed = NULL;
753 char *temp = NULL;
754 ssize_t msg_stored = 0;
755 ssize_t retval = count;
756 size_t desc_length = 0;
757 unsigned long index = 0;
758 int received = 0;
759 int used = 0;
760 int rejected = 0;
761 int reset = 0;
762 enum msg_index_t firstmessage = group->start;
763 enum msg_index_t lastmessage = group->end;
764 enum msg_index_t curmessage;
766 while (cp < end) {
767 while ((cp < end) && (*cp == ' ' || *cp == '\t'))
768 cp++;
770 if (cp == end)
771 break;
772 if (strchr("dDrR", *cp)) {
773 reset = 1;
774 break;
776 received++;
778 linefeed = strchr(cp, '\n');
779 if (!linefeed) {
780 rejected++;
781 break;
784 if (!isdigit(*cp)) {
785 rejected++;
786 cp = linefeed + 1;
787 continue;
790 index = simple_strtoul(cp, &temp, 10);
792 while ((temp < linefeed) && (*temp == ' ' || *temp == '\t'))
793 temp++;
795 desc_length = linefeed - temp;
796 curmessage = firstmessage + index;
799 * Note the check (curmessage < firstmessage). It is not
800 * redundant. Suppose that the user gave us an index
801 * equal to ULONG_MAX - 1. If firstmessage > 1, then
802 * firstmessage + index < firstmessage!
805 if ((curmessage < firstmessage) || (curmessage > lastmessage)) {
806 rejected++;
807 cp = linefeed + 1;
808 continue;
811 msg_stored = spk_msg_set(curmessage, temp, desc_length);
812 if (msg_stored < 0) {
813 retval = msg_stored;
814 if (msg_stored == -ENOMEM)
815 reset = 1;
816 break;
819 used++;
821 cp = linefeed + 1;
824 if (reset)
825 spk_reset_msg_group(group);
827 report_msg_status(reset, received, used, rejected, group->name);
828 return retval;
831 static ssize_t message_show(struct kobject *kobj,
832 struct kobj_attribute *attr, char *buf)
834 ssize_t retval = 0;
835 struct msg_group_t *group = spk_find_msg_group(attr->attr.name);
836 unsigned long flags;
838 if (WARN_ON(!group))
839 return -EINVAL;
841 spin_lock_irqsave(&speakup_info.spinlock, flags);
842 retval = message_show_helper(buf, group->start, group->end);
843 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
844 return retval;
847 static ssize_t message_store(struct kobject *kobj, struct kobj_attribute *attr,
848 const char *buf, size_t count)
850 struct msg_group_t *group = spk_find_msg_group(attr->attr.name);
852 if (WARN_ON(!group))
853 return -EINVAL;
855 return message_store_helper(buf, count, group);
859 * Declare the attributes.
861 static struct kobj_attribute keymap_attribute =
862 __ATTR_RW(keymap);
863 static struct kobj_attribute silent_attribute =
864 __ATTR_WO(silent);
865 static struct kobj_attribute synth_attribute =
866 __ATTR_RW(synth);
867 static struct kobj_attribute synth_direct_attribute =
868 __ATTR_WO(synth_direct);
869 static struct kobj_attribute version_attribute =
870 __ATTR_RO(version);
872 static struct kobj_attribute delimiters_attribute =
873 __ATTR(delimiters, 0644, punc_show, punc_store);
874 static struct kobj_attribute ex_num_attribute =
875 __ATTR(ex_num, 0644, punc_show, punc_store);
876 static struct kobj_attribute punc_all_attribute =
877 __ATTR(punc_all, 0644, punc_show, punc_store);
878 static struct kobj_attribute punc_most_attribute =
879 __ATTR(punc_most, 0644, punc_show, punc_store);
880 static struct kobj_attribute punc_some_attribute =
881 __ATTR(punc_some, 0644, punc_show, punc_store);
882 static struct kobj_attribute repeats_attribute =
883 __ATTR(repeats, 0644, punc_show, punc_store);
885 static struct kobj_attribute attrib_bleep_attribute =
886 __ATTR(attrib_bleep, 0644, spk_var_show, spk_var_store);
887 static struct kobj_attribute bell_pos_attribute =
888 __ATTR(bell_pos, 0644, spk_var_show, spk_var_store);
889 static struct kobj_attribute bleep_time_attribute =
890 __ATTR(bleep_time, 0644, spk_var_show, spk_var_store);
891 static struct kobj_attribute bleeps_attribute =
892 __ATTR(bleeps, 0644, spk_var_show, spk_var_store);
893 static struct kobj_attribute cursor_time_attribute =
894 __ATTR(cursor_time, 0644, spk_var_show, spk_var_store);
895 static struct kobj_attribute key_echo_attribute =
896 __ATTR(key_echo, 0644, spk_var_show, spk_var_store);
897 static struct kobj_attribute no_interrupt_attribute =
898 __ATTR(no_interrupt, 0644, spk_var_show, spk_var_store);
899 static struct kobj_attribute punc_level_attribute =
900 __ATTR(punc_level, 0644, spk_var_show, spk_var_store);
901 static struct kobj_attribute reading_punc_attribute =
902 __ATTR(reading_punc, 0644, spk_var_show, spk_var_store);
903 static struct kobj_attribute say_control_attribute =
904 __ATTR(say_control, 0644, spk_var_show, spk_var_store);
905 static struct kobj_attribute say_word_ctl_attribute =
906 __ATTR(say_word_ctl, 0644, spk_var_show, spk_var_store);
907 static struct kobj_attribute spell_delay_attribute =
908 __ATTR(spell_delay, 0644, spk_var_show, spk_var_store);
911 * These attributes are i18n related.
913 static struct kobj_attribute announcements_attribute =
914 __ATTR(announcements, 0644, message_show, message_store);
915 static struct kobj_attribute characters_attribute =
916 __ATTR(characters, 0644, chars_chartab_show,
917 chars_chartab_store);
918 static struct kobj_attribute chartab_attribute =
919 __ATTR(chartab, 0644, chars_chartab_show,
920 chars_chartab_store);
921 static struct kobj_attribute ctl_keys_attribute =
922 __ATTR(ctl_keys, 0644, message_show, message_store);
923 static struct kobj_attribute colors_attribute =
924 __ATTR(colors, 0644, message_show, message_store);
925 static struct kobj_attribute formatted_attribute =
926 __ATTR(formatted, 0644, message_show, message_store);
927 static struct kobj_attribute function_names_attribute =
928 __ATTR(function_names, 0644, message_show, message_store);
929 static struct kobj_attribute key_names_attribute =
930 __ATTR(key_names, 0644, message_show, message_store);
931 static struct kobj_attribute states_attribute =
932 __ATTR(states, 0644, message_show, message_store);
935 * Create groups of attributes so that we can create and destroy them all
936 * at once.
938 static struct attribute *main_attrs[] = {
939 &keymap_attribute.attr,
940 &silent_attribute.attr,
941 &synth_attribute.attr,
942 &synth_direct_attribute.attr,
943 &version_attribute.attr,
944 &delimiters_attribute.attr,
945 &ex_num_attribute.attr,
946 &punc_all_attribute.attr,
947 &punc_most_attribute.attr,
948 &punc_some_attribute.attr,
949 &repeats_attribute.attr,
950 &attrib_bleep_attribute.attr,
951 &bell_pos_attribute.attr,
952 &bleep_time_attribute.attr,
953 &bleeps_attribute.attr,
954 &cursor_time_attribute.attr,
955 &key_echo_attribute.attr,
956 &no_interrupt_attribute.attr,
957 &punc_level_attribute.attr,
958 &reading_punc_attribute.attr,
959 &say_control_attribute.attr,
960 &say_word_ctl_attribute.attr,
961 &spell_delay_attribute.attr,
962 NULL,
965 static struct attribute *i18n_attrs[] = {
966 &announcements_attribute.attr,
967 &characters_attribute.attr,
968 &chartab_attribute.attr,
969 &ctl_keys_attribute.attr,
970 &colors_attribute.attr,
971 &formatted_attribute.attr,
972 &function_names_attribute.attr,
973 &key_names_attribute.attr,
974 &states_attribute.attr,
975 NULL,
979 * An unnamed attribute group will put all of the attributes directly in
980 * the kobject directory. If we specify a name, a subdirectory will be
981 * created for the attributes with the directory being the name of the
982 * attribute group.
984 static const struct attribute_group main_attr_group = {
985 .attrs = main_attrs,
988 static const struct attribute_group i18n_attr_group = {
989 .attrs = i18n_attrs,
990 .name = "i18n",
993 static struct kobject *accessibility_kobj;
994 struct kobject *speakup_kobj;
996 int speakup_kobj_init(void)
998 int retval;
1001 * Create a simple kobject with the name of "accessibility",
1002 * located under /sys/
1004 * As this is a simple directory, no uevent will be sent to
1005 * userspace. That is why this function should not be used for
1006 * any type of dynamic kobjects, where the name and number are
1007 * not known ahead of time.
1009 accessibility_kobj = kobject_create_and_add("accessibility", NULL);
1010 if (!accessibility_kobj) {
1011 retval = -ENOMEM;
1012 goto out;
1015 speakup_kobj = kobject_create_and_add("speakup", accessibility_kobj);
1016 if (!speakup_kobj) {
1017 retval = -ENOMEM;
1018 goto err_acc;
1021 /* Create the files associated with this kobject */
1022 retval = sysfs_create_group(speakup_kobj, &main_attr_group);
1023 if (retval)
1024 goto err_speakup;
1026 retval = sysfs_create_group(speakup_kobj, &i18n_attr_group);
1027 if (retval)
1028 goto err_group;
1030 goto out;
1032 err_group:
1033 sysfs_remove_group(speakup_kobj, &main_attr_group);
1034 err_speakup:
1035 kobject_put(speakup_kobj);
1036 err_acc:
1037 kobject_put(accessibility_kobj);
1038 out:
1039 return retval;
1042 void speakup_kobj_exit(void)
1044 sysfs_remove_group(speakup_kobj, &i18n_attr_group);
1045 sysfs_remove_group(speakup_kobj, &main_attr_group);
1046 kobject_put(speakup_kobj);
1047 kobject_put(accessibility_kobj);