1 // SPDX-License-Identifier: GPL-2.0
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>
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)
35 char *buf_pointer = buf;
36 size_t bufsize = PAGE_SIZE;
39 spin_lock_irqsave(&speakup_info.spinlock, flags);
41 for (i = 0; i < 256; i++) {
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))
50 else if (IS_TYPE(i, WDLM))
52 else if (IS_TYPE(i, A_PUNC))
54 else if (IS_TYPE(i, PUNC))
56 else if (IS_TYPE(i, NUM))
58 else if (IS_TYPE(i, A_CAP))
60 else if (IS_TYPE(i, ALPHA))
62 else if (IS_TYPE(i, B_CAPSYM))
64 else if (IS_TYPE(i, B_SYM))
69 scnprintf(buf_pointer, bufsize, "%d\t%s\n", i, cp);
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",
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]);
99 snprintf(buf + (len - 1), sizeof(buf) - (len - 1),
100 " with %d reject%s\n",
101 rejected, rejected > 1 ? "s" : "");
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. */
120 ssize_t retval = count;
122 unsigned long index = 0;
128 int do_characters = !strcmp(attr->attr.name, "characters");
129 size_t desc_length = 0;
132 spin_lock_irqsave(&speakup_info.spinlock, flags);
134 while ((cp < end) && (*cp == ' ' || *cp == '\t'))
139 if ((*cp == '\n') || strchr("dDrR", *cp)) {
145 linefeed = strchr(cp, '\n');
157 index = simple_strtoul(cp, &temp, 10);
164 while ((temp < linefeed) && (*temp == ' ' || *temp == '\t'))
167 desc_length = linefeed - temp;
168 if (desc_length > MAX_DESC_LEN) {
174 desc = kmalloc(desc_length + 1, GFP_ATOMIC);
177 reset = 1; /* just reset on error. */
185 for (i = 0; i < desc_length; i++)
187 outptr[desc_length] = '\0';
190 if (spk_characters[index] != spk_default_chars[index])
191 kfree(spk_characters[index]);
192 spk_characters[index] = desc;
195 charclass = spk_chartab_get_value(keyword);
196 if (charclass == 0) {
201 if (charclass != spk_chartab[index]) {
202 spk_chartab[index] = charclass;
211 spk_reset_default_chars();
213 spk_reset_default_chartab();
216 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
217 report_char_chartab_status(reset, received, used, rejected,
223 * This is called when a user reads the keymap parameter.
225 static ssize_t keymap_show(struct kobject *kobj, struct kobj_attribute *attr,
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++) {
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)
266 char *in_buff = NULL;
271 spin_lock_irqsave(&speakup_info.spinlock, flags);
272 in_buff = kmemdup(buf, count + 1, GFP_ATOMIC);
274 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
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");
281 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
284 if (in_buff[count - 1] == '\n')
285 in_buff[count - 1] = '\0';
287 cp1 = (u_char *)in_buff;
288 for (i = 0; i < 3; i++) {
289 cp = spk_s2uchar(cp, 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]);
300 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
304 cp = spk_s2uchar(cp, cp1);
309 if (i != 0 || cp1[-1] != KEY_MAP_VER || cp1[-2] != 0) {
311 pr_warn("end %d %d %d %d\n", i,
312 (int)cp1[-3], (int)cp1[-2], (int)cp1[-1]);
314 if (spk_set_key_info(in_buff, spk_key_buf)) {
315 spk_set_key_info(spk_key_defaults, spk_key_buf);
317 pr_warn("set key failed\n");
321 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
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)
332 struct vc_data *vc = vc_cons[fg_console].d;
338 if (len > 0 && len < 3) {
343 if (ch < '0' || ch > '7') {
344 pr_warn("silent value '%c' not in range (0,7)\n", ch);
347 spin_lock_irqsave(&speakup_info.spinlock, flags);
359 spk_shut_up &= ~shut;
360 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
365 * This is called when a user reads the synth setting.
367 static ssize_t synth_show(struct kobject *kobj, struct kobj_attribute *attr,
373 rv = sprintf(buf, "%s\n", "none");
375 rv = sprintf(buf, "%s\n", synth->name);
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)
386 char new_synth_name[10];
389 if (len < 2 || len > 9)
391 memcpy(new_synth_name, buf, len);
392 if (new_synth_name[len - 1] == '\n')
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);
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)
415 const char *ptr = buf;
422 spin_lock_irqsave(&speakup_info.spinlock, flags);
424 bytes = min_t(size_t, len, 250);
425 strncpy(tmp, ptr, bytes);
427 string_unescape_any_inplace(tmp);
428 synth_printf("%s", tmp);
432 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
437 * This function is called when a user reads the version.
439 static ssize_t version_show(struct kobject *kobj, struct kobj_attribute *attr,
445 cp += sprintf(cp, "Speakup version %s\n", SPEAKUP_VERSION);
447 cp += sprintf(cp, "%s synthesizer driver version %s\n",
448 synth->name, synth->version);
453 * This is called when a user reads the punctuation settings.
455 static ssize_t punc_show(struct kobject *kobj, struct kobj_attribute *attr,
460 struct st_var_header *p_header;
461 struct punc_var_t *var;
462 struct st_bits_data *pb;
466 p_header = spk_var_header_by_name(attr->attr.name);
468 pr_warn("p_header is null, attr->attr.name is %s\n",
473 var = spk_get_punc_var(p_header->var_id);
475 pr_warn("var is null, p_header->var_id is %i\n",
480 spin_lock_irqsave(&speakup_info.spinlock, flags);
481 pb = (struct st_bits_data *)&spk_punc_info[var->value];
483 for (i = 33; i < 128; i++) {
484 if (!(spk_chartab[i] & mask))
488 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
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)
499 struct st_var_header *p_header;
500 struct punc_var_t *var;
508 p_header = spk_var_header_by_name(attr->attr.name);
510 pr_warn("p_header is null, attr->attr.name is %s\n",
515 var = spk_get_punc_var(p_header->var_id);
517 pr_warn("var is null, p_header->var_id is %i\n",
522 memcpy(punc_buf, buf, x);
524 while (x && punc_buf[x - 1] == '\n')
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);
533 x = spk_set_mask_bits(punc_buf, var->value, 3);
535 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
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,
546 struct st_var_header *param;
553 param = spk_var_header_by_name(attr->attr.name);
557 spin_lock_irqsave(&speakup_info.spinlock, flags);
558 var = (struct var_t *)param->data;
559 switch (param->var_type) {
563 rv = sprintf(buf, "%i\n", var->u.n.value);
565 rv = sprintf(buf, "0\n");
571 for (cp = (char *)param->p_val; (ch = *cp); cp++) {
572 if (ch >= ' ' && ch < '~')
575 cp1 += sprintf(cp1, "\\x%02x", ch);
582 rv = sprintf(buf, "\"\"\n");
586 rv = sprintf(buf, "Bad parameter %s, type %i\n",
587 param->name, param->var_type);
590 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
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);
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;
625 struct var_t *var_data;
629 param = spk_var_header_by_name(attr->attr.name);
636 string_unescape_any_inplace(cp);
638 spin_lock_irqsave(&speakup_info.spinlock, flags);
639 switch (param->var_type) {
642 if (*cp == 'd' || *cp == 'r' || *cp == '\0')
644 else if (*cp == '+' || *cp == '-')
648 if (kstrtol(cp, 10, &value) == 0)
649 ret = spk_set_num_var(value, param, len);
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",
656 var_data->u.n.low, var_data->u.n.high);
660 * If voice was just changed, we might need to reset our default
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,
669 spk_reset_default_value("vol", synth->default_vol,
675 if ((len >= 1) && (cp[len - 1] == '\n'))
677 if ((len >= 2) && (cp[0] == '"') && (cp[len - 1] == '"')) {
682 ret = spk_set_string_var(cp, param, len);
684 pr_warn("value too long for %s\n",
688 pr_warn("%s unknown type %d\n",
689 param->name, (int)param->var_type);
692 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
694 if (ret == -ERESTART)
695 pr_info("%s reset to default value\n", param->name);
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;
710 enum msg_index_t cursor;
712 *buf_pointer = '\0'; /* buf_pointer always looking at a NUL byte. */
714 for (cursor = first; cursor <= last; cursor++, index++) {
717 printed = scnprintf(buf_pointer, bufsize, "%d\t%s\n",
718 index, spk_msg_get(cursor));
719 buf_pointer += printed;
723 return buf_pointer - buf;
726 static void report_msg_status(int reset, int received, int used,
727 int rejected, char *groupname)
733 pr_info("i18n messages from group %s reset to defaults\n",
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);
740 snprintf(buf + (len - 1), sizeof(buf) - (len - 1),
741 " with %d reject%s\n",
742 rejected, rejected > 1 ? "s" : "");
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;
754 ssize_t msg_stored = 0;
755 ssize_t retval = count;
756 size_t desc_length = 0;
757 unsigned long index = 0;
762 enum msg_index_t firstmessage = group->start;
763 enum msg_index_t lastmessage = group->end;
764 enum msg_index_t curmessage;
767 while ((cp < end) && (*cp == ' ' || *cp == '\t'))
772 if (strchr("dDrR", *cp)) {
778 linefeed = strchr(cp, '\n');
790 index = simple_strtoul(cp, &temp, 10);
792 while ((temp < linefeed) && (*temp == ' ' || *temp == '\t'))
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)) {
811 msg_stored = spk_msg_set(curmessage, temp, desc_length);
812 if (msg_stored < 0) {
814 if (msg_stored == -ENOMEM)
825 spk_reset_msg_group(group);
827 report_msg_status(reset, received, used, rejected, group->name);
831 static ssize_t message_show(struct kobject *kobj,
832 struct kobj_attribute *attr, char *buf)
835 struct msg_group_t *group = spk_find_msg_group(attr->attr.name);
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);
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);
855 return message_store_helper(buf, count, group);
859 * Declare the attributes.
861 static struct kobj_attribute keymap_attribute =
863 static struct kobj_attribute silent_attribute =
865 static struct kobj_attribute synth_attribute =
867 static struct kobj_attribute synth_direct_attribute =
868 __ATTR_WO(synth_direct);
869 static struct kobj_attribute version_attribute =
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
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,
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,
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
984 static const struct attribute_group main_attr_group = {
988 static const struct attribute_group i18n_attr_group = {
993 static struct kobject *accessibility_kobj;
994 struct kobject *speakup_kobj;
996 int speakup_kobj_init(void)
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) {
1015 speakup_kobj = kobject_create_and_add("speakup", accessibility_kobj);
1016 if (!speakup_kobj) {
1021 /* Create the files associated with this kobject */
1022 retval = sysfs_create_group(speakup_kobj, &main_attr_group);
1026 retval = sysfs_create_group(speakup_kobj, &i18n_attr_group);
1033 sysfs_remove_group(speakup_kobj, &main_attr_group);
1035 kobject_put(speakup_kobj);
1037 kobject_put(accessibility_kobj);
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);