2 * Speakup kobject implementation
4 * Copyright (C) 2009 William Hubbs
6 * This code is based on kobject-example.c, which came with linux 2.6.x.
8 * Copyright (C) 2004-2007 Greg Kroah-Hartman <greg@kroah.com>
9 * Copyright (C) 2007 Novell Inc.
11 * Released under the GPL version 2 only.
14 #include <linux/slab.h> /* For kmalloc. */
15 #include <linux/kernel.h>
16 #include <linux/kobject.h>
17 #include <linux/string.h>
18 #include <linux/string_helpers.h>
19 #include <linux/sysfs.h>
20 #include <linux/ctype.h>
26 * This is called when a user reads the characters or chartab sys file.
28 static ssize_t chars_chartab_show(struct kobject *kobj,
29 struct kobj_attribute *attr, char *buf)
34 char *buf_pointer = buf;
35 size_t bufsize = PAGE_SIZE;
38 spin_lock_irqsave(&speakup_info.spinlock, flags);
40 for (i = 0; i < 256; i++) {
43 if (strcmp("characters", attr->attr.name) == 0) {
44 len = scnprintf(buf_pointer, bufsize, "%d\t%s\n",
45 i, spk_characters[i]);
46 } else { /* show chartab entry */
47 if (IS_TYPE(i, B_CTL))
49 else if (IS_TYPE(i, WDLM))
51 else if (IS_TYPE(i, A_PUNC))
53 else if (IS_TYPE(i, PUNC))
55 else if (IS_TYPE(i, NUM))
57 else if (IS_TYPE(i, A_CAP))
59 else if (IS_TYPE(i, ALPHA))
61 else if (IS_TYPE(i, B_CAPSYM))
63 else if (IS_TYPE(i, B_SYM))
68 scnprintf(buf_pointer, bufsize, "%d\t%s\n", i, cp);
73 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
74 return buf_pointer - buf;
78 * Print informational messages or warnings after updating
79 * character descriptions or chartab entries.
81 static void report_char_chartab_status(int reset, int received, int used,
82 int rejected, int do_characters)
84 static char const *object_type[] = {
85 "character class entries",
86 "character descriptions",
92 pr_info("%s reset to defaults\n", object_type[do_characters]);
93 } else if (received) {
94 len = snprintf(buf, sizeof(buf),
95 " updated %d of %d %s\n",
96 used, received, object_type[do_characters]);
98 snprintf(buf + (len - 1), sizeof(buf) - (len - 1),
99 " with %d reject%s\n",
100 rejected, rejected > 1 ? "s" : "");
106 * This is called when a user changes the characters or chartab parameters.
108 static ssize_t chars_chartab_store(struct kobject *kobj,
109 struct kobj_attribute *attr, const char *buf, size_t count)
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. */
118 ssize_t retval = count;
120 unsigned long index = 0;
126 int do_characters = !strcmp(attr->attr.name, "characters");
127 size_t desc_length = 0;
130 spin_lock_irqsave(&speakup_info.spinlock, flags);
133 while ((cp < end) && (*cp == ' ' || *cp == '\t'))
138 if ((*cp == '\n') || strchr("dDrR", *cp)) {
144 linefeed = strchr(cp, '\n');
156 index = simple_strtoul(cp, &temp, 10);
163 while ((temp < linefeed) && (*temp == ' ' || *temp == '\t'))
166 desc_length = linefeed - temp;
167 if (desc_length > MAX_DESC_LEN) {
173 desc = kmalloc(desc_length + 1, GFP_ATOMIC);
176 reset = 1; /* just reset on error. */
184 for (i = 0; i < desc_length; i++)
186 outptr[desc_length] = '\0';
189 if (spk_characters[index] != spk_default_chars[index])
190 kfree(spk_characters[index]);
191 spk_characters[index] = desc;
194 charclass = spk_chartab_get_value(keyword);
195 if (charclass == 0) {
200 if (charclass != spk_chartab[index]) {
201 spk_chartab[index] = charclass;
210 spk_reset_default_chars();
212 spk_reset_default_chartab();
215 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
216 report_char_chartab_status(reset, received, used, rejected,
222 * This is called when a user reads the keymap parameter.
224 static ssize_t keymap_show(struct kobject *kobj, struct kobj_attribute *attr,
236 spin_lock_irqsave(&speakup_info.spinlock, flags);
237 cp1 = spk_key_buf + SHIFT_TBL_SIZE;
238 num_keys = (int)(*cp1);
239 nstates = (int)cp1[1];
240 cp += sprintf(cp, "%d, %d, %d,\n", KEY_MAP_VER, num_keys, nstates);
241 cp1 += 2; /* now pointing at shift states */
242 /* dump num_keys+1 as first row is shift states + flags,
243 * each subsequent row is key + states
245 for (n = 0; n <= num_keys; n++) {
246 for (i = 0; i <= nstates; i++) {
248 cp += sprintf(cp, "%d,", (int)ch);
249 *cp++ = (i < nstates) ? SPACE : '\n';
252 cp += sprintf(cp, "0, %d\n", KEY_MAP_VER);
253 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
254 return (int)(cp - buf);
258 * This is called when a user changes the keymap parameter.
260 static ssize_t keymap_store(struct kobject *kobj, struct kobj_attribute *attr,
261 const char *buf, size_t count)
265 char *in_buff = NULL;
270 spin_lock_irqsave(&speakup_info.spinlock, flags);
271 in_buff = kmemdup(buf, count + 1, GFP_ATOMIC);
273 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
276 if (strchr("dDrR", *in_buff)) {
277 spk_set_key_info(spk_key_defaults, spk_key_buf);
278 pr_info("keymap set to default values\n");
280 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
283 if (in_buff[count - 1] == '\n')
284 in_buff[count - 1] = '\0';
286 cp1 = (u_char *)in_buff;
287 for (i = 0; i < 3; i++) {
288 cp = spk_s2uchar(cp, cp1);
291 i = (int)cp1[-2] + 1;
292 i *= (int)cp1[-1] + 1;
293 i += 2; /* 0 and last map ver */
294 if (cp1[-3] != KEY_MAP_VER || cp1[-1] > 10 ||
295 i+SHIFT_TBL_SIZE+4 >= sizeof(spk_key_buf)) {
296 pr_warn("i %d %d %d %d\n", i,
297 (int)cp1[-3], (int)cp1[-2], (int)cp1[-1]);
299 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
303 cp = spk_s2uchar(cp, cp1);
308 if (i != 0 || cp1[-1] != KEY_MAP_VER || cp1[-2] != 0) {
310 pr_warn("end %d %d %d %d\n", i,
311 (int)cp1[-3], (int)cp1[-2], (int)cp1[-1]);
313 if (spk_set_key_info(in_buff, spk_key_buf)) {
314 spk_set_key_info(spk_key_defaults, spk_key_buf);
316 pr_warn("set key failed\n");
320 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
325 * This is called when a user changes the value of the silent parameter.
327 static ssize_t silent_store(struct kobject *kobj, struct kobj_attribute *attr,
328 const char *buf, size_t count)
331 struct vc_data *vc = vc_cons[fg_console].d;
337 if (len > 0 && len < 3) {
342 if (ch < '0' || ch > '7') {
343 pr_warn("silent value '%c' not in range (0,7)\n", ch);
346 spin_lock_irqsave(&speakup_info.spinlock, flags);
358 spk_shut_up &= ~shut;
359 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
364 * This is called when a user reads the synth setting.
366 static ssize_t synth_show(struct kobject *kobj, struct kobj_attribute *attr,
372 rv = sprintf(buf, "%s\n", "none");
374 rv = sprintf(buf, "%s\n", synth->name);
379 * This is called when a user requests to change synthesizers.
381 static ssize_t synth_store(struct kobject *kobj, struct kobj_attribute *attr,
382 const char *buf, size_t count)
385 char new_synth_name[10];
388 if (len < 2 || len > 9)
390 strncpy(new_synth_name, buf, len);
391 if (new_synth_name[len - 1] == '\n')
393 new_synth_name[len] = '\0';
394 spk_strlwr(new_synth_name);
395 if ((synth != NULL) && (!strcmp(new_synth_name, synth->name))) {
396 pr_warn("%s already in use\n", new_synth_name);
397 } else if (synth_init(new_synth_name) != 0) {
398 pr_warn("failed to init synth %s\n", new_synth_name);
405 * This is called when text is sent to the synth via the synth_direct file.
407 static ssize_t synth_direct_store(struct kobject *kobj,
408 struct kobj_attribute *attr, const char *buf, size_t count)
413 const char *ptr = buf;
420 spin_lock_irqsave(&speakup_info.spinlock, flags);
422 bytes = min_t(size_t, len, 250);
423 strncpy(tmp, ptr, bytes);
425 string_unescape_any_inplace(tmp);
426 synth_printf("%s", tmp);
430 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
435 * This function is called when a user reads the version.
437 static ssize_t version_show(struct kobject *kobj, struct kobj_attribute *attr,
443 cp += sprintf(cp, "Speakup version %s\n", SPEAKUP_VERSION);
445 cp += sprintf(cp, "%s synthesizer driver version %s\n",
446 synth->name, synth->version);
451 * This is called when a user reads the punctuation settings.
453 static ssize_t punc_show(struct kobject *kobj, struct kobj_attribute *attr,
458 struct st_var_header *p_header;
459 struct punc_var_t *var;
460 struct st_bits_data *pb;
464 p_header = spk_var_header_by_name(attr->attr.name);
466 pr_warn("p_header is null, attr->attr.name is %s\n",
471 var = spk_get_punc_var(p_header->var_id);
473 pr_warn("var is null, p_header->var_id is %i\n",
478 spin_lock_irqsave(&speakup_info.spinlock, flags);
479 pb = (struct st_bits_data *) &spk_punc_info[var->value];
481 for (i = 33; i < 128; i++) {
482 if (!(spk_chartab[i]&mask))
486 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
491 * This is called when a user changes the punctuation settings.
493 static ssize_t punc_store(struct kobject *kobj, struct kobj_attribute *attr,
494 const char *buf, size_t count)
497 struct st_var_header *p_header;
498 struct punc_var_t *var;
506 p_header = spk_var_header_by_name(attr->attr.name);
508 pr_warn("p_header is null, attr->attr.name is %s\n",
513 var = spk_get_punc_var(p_header->var_id);
515 pr_warn("var is null, p_header->var_id is %i\n",
520 strncpy(punc_buf, buf, x);
522 while (x && punc_buf[x - 1] == '\n')
526 spin_lock_irqsave(&speakup_info.spinlock, flags);
528 if (*punc_buf == 'd' || *punc_buf == 'r')
529 x = spk_set_mask_bits(NULL, var->value, 3);
531 x = spk_set_mask_bits(punc_buf, var->value, 3);
533 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
538 * This function is called when a user reads one of the variable parameters.
540 ssize_t spk_var_show(struct kobject *kobj, struct kobj_attribute *attr,
544 struct st_var_header *param;
551 param = spk_var_header_by_name(attr->attr.name);
555 spin_lock_irqsave(&speakup_info.spinlock, flags);
556 var = (struct var_t *) param->data;
557 switch (param->var_type) {
561 rv = sprintf(buf, "%i\n", var->u.n.value);
563 rv = sprintf(buf, "0\n");
569 for (cp = (char *)param->p_val; (ch = *cp); cp++) {
570 if (ch >= ' ' && ch < '~')
573 cp1 += sprintf(cp1, "\\x%02x", ch);
580 rv = sprintf(buf, "\"\"\n");
584 rv = sprintf(buf, "Bad parameter %s, type %i\n",
585 param->name, param->var_type);
588 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
591 EXPORT_SYMBOL_GPL(spk_var_show);
594 * Used to reset either default_pitch or default_vol.
596 static inline void spk_reset_default_value(char *header_name,
597 int *synth_default_value, int idx)
599 struct st_var_header *param;
601 if (synth && synth_default_value) {
602 param = spk_var_header_by_name(header_name);
604 spk_set_num_var(synth_default_value[idx],
605 param, E_NEW_DEFAULT);
606 spk_set_num_var(0, param, E_DEFAULT);
607 pr_info("%s reset to default value\n", param->name);
613 * This function is called when a user echos a value to one of the
614 * variable parameters.
616 ssize_t spk_var_store(struct kobject *kobj, struct kobj_attribute *attr,
617 const char *buf, size_t count)
619 struct st_var_header *param;
623 struct var_t *var_data;
627 param = spk_var_header_by_name(attr->attr.name);
634 string_unescape_any_inplace(cp);
636 spin_lock_irqsave(&speakup_info.spinlock, flags);
637 switch (param->var_type) {
640 if (*cp == 'd' || *cp == 'r' || *cp == '\0')
642 else if (*cp == '+' || *cp == '-')
646 if (kstrtol(cp, 10, &value) == 0)
647 ret = spk_set_num_var(value, param, len);
649 pr_warn("overflow or parsing error has occurred");
650 if (ret == -ERANGE) {
651 var_data = param->data;
652 pr_warn("value for %s out of range, expect %d to %d\n",
654 var_data->u.n.low, var_data->u.n.high);
658 * If voice was just changed, we might need to reset our default
661 if (param->var_id == VOICE && synth &&
662 (ret == 0 || ret == -ERESTART)) {
663 var_data = param->data;
664 value = var_data->u.n.value;
665 spk_reset_default_value("pitch", synth->default_pitch,
667 spk_reset_default_value("vol", synth->default_vol,
673 if ((len >= 1) && (cp[len - 1] == '\n'))
675 if ((len >= 2) && (cp[0] == '"') && (cp[len - 1] == '"')) {
680 ret = spk_set_string_var(cp, param, len);
682 pr_warn("value too long for %s\n",
686 pr_warn("%s unknown type %d\n",
687 param->name, (int)param->var_type);
690 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
692 if (ret == -ERESTART)
693 pr_info("%s reset to default value\n", param->name);
696 EXPORT_SYMBOL_GPL(spk_var_store);
699 * Functions for reading and writing lists of i18n messages. Incomplete.
702 static ssize_t message_show_helper(char *buf, enum msg_index_t first,
703 enum msg_index_t last)
705 size_t bufsize = PAGE_SIZE;
706 char *buf_pointer = buf;
708 enum msg_index_t cursor;
710 *buf_pointer = '\0'; /* buf_pointer always looking at a NUL byte. */
712 for (cursor = first; cursor <= last; cursor++, index++) {
715 printed = scnprintf(buf_pointer, bufsize, "%d\t%s\n",
716 index, spk_msg_get(cursor));
717 buf_pointer += printed;
721 return buf_pointer - buf;
724 static void report_msg_status(int reset, int received, int used,
725 int rejected, char *groupname)
731 pr_info("i18n messages from group %s reset to defaults\n",
733 } else if (received) {
734 len = snprintf(buf, sizeof(buf),
735 " updated %d of %d i18n messages from group %s\n",
736 used, received, groupname);
738 snprintf(buf + (len - 1), sizeof(buf) - (len - 1),
739 " with %d reject%s\n",
740 rejected, rejected > 1 ? "s" : "");
745 static ssize_t message_store_helper(const char *buf, size_t count,
746 struct msg_group_t *group)
748 char *cp = (char *) buf;
749 char *end = cp + count;
750 char *linefeed = NULL;
752 ssize_t msg_stored = 0;
753 ssize_t retval = count;
754 size_t desc_length = 0;
755 unsigned long index = 0;
760 enum msg_index_t firstmessage = group->start;
761 enum msg_index_t lastmessage = group->end;
762 enum msg_index_t curmessage;
766 while ((cp < end) && (*cp == ' ' || *cp == '\t'))
771 if (strchr("dDrR", *cp)) {
777 linefeed = strchr(cp, '\n');
789 index = simple_strtoul(cp, &temp, 10);
791 while ((temp < linefeed) && (*temp == ' ' || *temp == '\t'))
794 desc_length = linefeed - temp;
795 curmessage = firstmessage + index;
798 * Note the check (curmessage < firstmessage). It is not
799 * redundant. Suppose that the user gave us an index
800 * equal to ULONG_MAX - 1. If firstmessage > 1, then
801 * firstmessage + index < firstmessage!
804 if ((curmessage < firstmessage) || (curmessage > lastmessage)) {
810 msg_stored = spk_msg_set(curmessage, temp, desc_length);
811 if (msg_stored < 0) {
813 if (msg_stored == -ENOMEM)
824 spk_reset_msg_group(group);
826 report_msg_status(reset, received, used, rejected, group->name);
830 static ssize_t message_show(struct kobject *kobj,
831 struct kobj_attribute *attr, char *buf)
834 struct msg_group_t *group = spk_find_msg_group(attr->attr.name);
838 spin_lock_irqsave(&speakup_info.spinlock, flags);
839 retval = message_show_helper(buf, group->start, group->end);
840 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
844 static ssize_t message_store(struct kobject *kobj, struct kobj_attribute *attr,
845 const char *buf, size_t count)
847 struct msg_group_t *group = spk_find_msg_group(attr->attr.name);
850 return message_store_helper(buf, count, group);
854 * Declare the attributes.
856 static struct kobj_attribute keymap_attribute =
858 static struct kobj_attribute silent_attribute =
860 static struct kobj_attribute synth_attribute =
862 static struct kobj_attribute synth_direct_attribute =
863 __ATTR_WO(synth_direct);
864 static struct kobj_attribute version_attribute =
867 static struct kobj_attribute delimiters_attribute =
868 __ATTR(delimiters, 0644, punc_show, punc_store);
869 static struct kobj_attribute ex_num_attribute =
870 __ATTR(ex_num, 0644, punc_show, punc_store);
871 static struct kobj_attribute punc_all_attribute =
872 __ATTR(punc_all, 0644, punc_show, punc_store);
873 static struct kobj_attribute punc_most_attribute =
874 __ATTR(punc_most, 0644, punc_show, punc_store);
875 static struct kobj_attribute punc_some_attribute =
876 __ATTR(punc_some, 0644, punc_show, punc_store);
877 static struct kobj_attribute repeats_attribute =
878 __ATTR(repeats, 0644, punc_show, punc_store);
880 static struct kobj_attribute attrib_bleep_attribute =
881 __ATTR(attrib_bleep, 0644, spk_var_show, spk_var_store);
882 static struct kobj_attribute bell_pos_attribute =
883 __ATTR(bell_pos, 0644, spk_var_show, spk_var_store);
884 static struct kobj_attribute bleep_time_attribute =
885 __ATTR(bleep_time, 0644, spk_var_show, spk_var_store);
886 static struct kobj_attribute bleeps_attribute =
887 __ATTR(bleeps, 0644, spk_var_show, spk_var_store);
888 static struct kobj_attribute cursor_time_attribute =
889 __ATTR(cursor_time, 0644, spk_var_show, spk_var_store);
890 static struct kobj_attribute key_echo_attribute =
891 __ATTR(key_echo, 0644, spk_var_show, spk_var_store);
892 static struct kobj_attribute no_interrupt_attribute =
893 __ATTR(no_interrupt, 0644, spk_var_show, spk_var_store);
894 static struct kobj_attribute punc_level_attribute =
895 __ATTR(punc_level, 0644, spk_var_show, spk_var_store);
896 static struct kobj_attribute reading_punc_attribute =
897 __ATTR(reading_punc, 0644, spk_var_show, spk_var_store);
898 static struct kobj_attribute say_control_attribute =
899 __ATTR(say_control, 0644, spk_var_show, spk_var_store);
900 static struct kobj_attribute say_word_ctl_attribute =
901 __ATTR(say_word_ctl, 0644, spk_var_show, spk_var_store);
902 static struct kobj_attribute spell_delay_attribute =
903 __ATTR(spell_delay, 0644, spk_var_show, spk_var_store);
906 * These attributes are i18n related.
908 static struct kobj_attribute announcements_attribute =
909 __ATTR(announcements, 0644, message_show, message_store);
910 static struct kobj_attribute characters_attribute =
911 __ATTR(characters, 0644, chars_chartab_show,
912 chars_chartab_store);
913 static struct kobj_attribute chartab_attribute =
914 __ATTR(chartab, 0644, chars_chartab_show,
915 chars_chartab_store);
916 static struct kobj_attribute ctl_keys_attribute =
917 __ATTR(ctl_keys, 0644, message_show, message_store);
918 static struct kobj_attribute colors_attribute =
919 __ATTR(colors, 0644, message_show, message_store);
920 static struct kobj_attribute formatted_attribute =
921 __ATTR(formatted, 0644, message_show, message_store);
922 static struct kobj_attribute function_names_attribute =
923 __ATTR(function_names, 0644, message_show, message_store);
924 static struct kobj_attribute key_names_attribute =
925 __ATTR(key_names, 0644, message_show, message_store);
926 static struct kobj_attribute states_attribute =
927 __ATTR(states, 0644, message_show, message_store);
930 * Create groups of attributes so that we can create and destroy them all
933 static struct attribute *main_attrs[] = {
934 &keymap_attribute.attr,
935 &silent_attribute.attr,
936 &synth_attribute.attr,
937 &synth_direct_attribute.attr,
938 &version_attribute.attr,
939 &delimiters_attribute.attr,
940 &ex_num_attribute.attr,
941 &punc_all_attribute.attr,
942 &punc_most_attribute.attr,
943 &punc_some_attribute.attr,
944 &repeats_attribute.attr,
945 &attrib_bleep_attribute.attr,
946 &bell_pos_attribute.attr,
947 &bleep_time_attribute.attr,
948 &bleeps_attribute.attr,
949 &cursor_time_attribute.attr,
950 &key_echo_attribute.attr,
951 &no_interrupt_attribute.attr,
952 &punc_level_attribute.attr,
953 &reading_punc_attribute.attr,
954 &say_control_attribute.attr,
955 &say_word_ctl_attribute.attr,
956 &spell_delay_attribute.attr,
960 static struct attribute *i18n_attrs[] = {
961 &announcements_attribute.attr,
962 &characters_attribute.attr,
963 &chartab_attribute.attr,
964 &ctl_keys_attribute.attr,
965 &colors_attribute.attr,
966 &formatted_attribute.attr,
967 &function_names_attribute.attr,
968 &key_names_attribute.attr,
969 &states_attribute.attr,
974 * An unnamed attribute group will put all of the attributes directly in
975 * the kobject directory. If we specify a name, a subdirectory will be
976 * created for the attributes with the directory being the name of the
979 static const struct attribute_group main_attr_group = {
983 static const struct attribute_group i18n_attr_group = {
988 static struct kobject *accessibility_kobj;
989 struct kobject *speakup_kobj;
991 int speakup_kobj_init(void)
996 * Create a simple kobject with the name of "accessibility",
997 * located under /sys/
999 * As this is a simple directory, no uevent will be sent to
1000 * userspace. That is why this function should not be used for
1001 * any type of dynamic kobjects, where the name and number are
1002 * not known ahead of time.
1004 accessibility_kobj = kobject_create_and_add("accessibility", NULL);
1005 if (!accessibility_kobj) {
1010 speakup_kobj = kobject_create_and_add("speakup", accessibility_kobj);
1011 if (!speakup_kobj) {
1016 /* Create the files associated with this kobject */
1017 retval = sysfs_create_group(speakup_kobj, &main_attr_group);
1021 retval = sysfs_create_group(speakup_kobj, &i18n_attr_group);
1028 sysfs_remove_group(speakup_kobj, &main_attr_group);
1030 kobject_put(speakup_kobj);
1032 kobject_put(accessibility_kobj);
1037 void speakup_kobj_exit(void)
1039 sysfs_remove_group(speakup_kobj, &i18n_attr_group);
1040 sysfs_remove_group(speakup_kobj, &main_attr_group);
1041 kobject_put(speakup_kobj);
1042 kobject_put(accessibility_kobj);