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/sysfs.h>
19 #include <linux/ctype.h>
25 * This is called when a user reads the characters or chartab sys file.
27 static ssize_t chars_chartab_show(struct kobject *kobj,
28 struct kobj_attribute *attr, char *buf)
33 char *buf_pointer = buf;
34 size_t bufsize = PAGE_SIZE;
39 for (i = 0; i < 256; i++) {
42 if (strcmp("characters", attr->attr.name) == 0) {
43 len = scnprintf(buf_pointer, bufsize, "%d\t%s\n",
44 i, spk_characters[i]);
45 } else { /* show chartab entry */
46 if (IS_TYPE(i, B_CTL))
48 else if (IS_TYPE(i, WDLM))
50 else if (IS_TYPE(i, A_PUNC))
52 else if (IS_TYPE(i, PUNC))
54 else if (IS_TYPE(i, NUM))
56 else if (IS_TYPE(i, A_CAP))
58 else if (IS_TYPE(i, ALPHA))
60 else if (IS_TYPE(i, B_CAPSYM))
62 else if (IS_TYPE(i, B_SYM))
67 scnprintf(buf_pointer, bufsize, "%d\t%s\n", i, cp);
73 return buf_pointer - buf;
77 * Print informational messages or warnings after updating
78 * character descriptions or chartab entries.
80 static void report_char_chartab_status(int reset, int received, int used,
81 int rejected, int do_characters)
83 char *object_type[] = {
84 "character class entries",
85 "character descriptions",
91 pr_info("%s reset to defaults\n", object_type[do_characters]);
92 } else if (received) {
93 len = snprintf(buf, sizeof(buf),
94 " updated %d of %d %s\n",
95 used, received, object_type[do_characters]);
97 snprintf(buf + (len - 1), sizeof(buf) - (len - 1),
98 " with %d reject%s\n",
99 rejected, rejected > 1 ? "s" : "");
105 * This is called when a user changes the characters or chartab parameters.
107 static ssize_t chars_chartab_store(struct kobject *kobj,
108 struct kobj_attribute *attr, const char *buf, size_t count)
110 char *cp = (char *) buf;
111 char *end = cp + count; /* the null at the end of the buffer */
112 char *linefeed = NULL;
113 char keyword[MAX_DESC_LEN + 1];
114 char *outptr = NULL; /* Will hold keyword or desc. */
117 ssize_t retval = count;
119 unsigned long index = 0;
125 int do_characters = !strcmp(attr->attr.name, "characters");
126 size_t desc_length = 0;
132 while ((cp < end) && (*cp == ' ' || *cp == '\t'))
137 if ((*cp == '\n') || strchr("dDrR", *cp)) {
143 linefeed = strchr(cp, '\n');
155 index = simple_strtoul(cp, &temp, 10);
162 while ((temp < linefeed) && (*temp == ' ' || *temp == '\t'))
165 desc_length = linefeed - temp;
166 if (desc_length > MAX_DESC_LEN) {
172 desc = kmalloc(desc_length + 1, GFP_ATOMIC);
175 reset = 1; /* just reset on error. */
183 for (i = 0; i < desc_length; i++)
185 outptr[desc_length] = '\0';
188 if (spk_characters[index] != spk_default_chars[index])
189 kfree(spk_characters[index]);
190 spk_characters[index] = desc;
193 charclass = spk_chartab_get_value(keyword);
194 if (charclass == 0) {
199 if (charclass != spk_chartab[index]) {
200 spk_chartab[index] = charclass;
209 spk_reset_default_chars();
211 spk_reset_default_chartab();
215 report_char_chartab_status(reset, received, used, rejected,
221 * This is called when a user reads the keymap parameter.
223 static ssize_t keymap_show(struct kobject *kobj, struct kobj_attribute *attr,
235 cp1 = spk_key_buf + SHIFT_TBL_SIZE;
236 num_keys = (int)(*cp1);
237 nstates = (int)cp1[1];
238 cp += sprintf(cp, "%d, %d, %d,\n", KEY_MAP_VER, num_keys, nstates);
239 cp1 += 2; /* now pointing at shift states */
240 /* dump num_keys+1 as first row is shift states + flags,
241 * each subsequent row is key + states */
242 for (n = 0; n <= num_keys; n++) {
243 for (i = 0; i <= nstates; i++) {
245 cp += sprintf(cp, "%d,", (int)ch);
246 *cp++ = (i < nstates) ? SPACE : '\n';
249 cp += sprintf(cp, "0, %d\n", KEY_MAP_VER);
251 return (int)(cp-buf);
255 * This is called when a user changes the keymap parameter.
257 static ssize_t keymap_store(struct kobject *kobj, struct kobj_attribute *attr,
258 const char *buf, size_t count)
262 char *in_buff = NULL;
268 in_buff = kmemdup(buf, count + 1, GFP_ATOMIC);
273 if (strchr("dDrR", *in_buff)) {
274 spk_set_key_info(spk_key_defaults, spk_key_buf);
275 pr_info("keymap set to default values\n");
280 if (in_buff[count - 1] == '\n')
281 in_buff[count - 1] = '\0';
283 cp1 = (u_char *)in_buff;
284 for (i = 0; i < 3; i++) {
285 cp = spk_s2uchar(cp, cp1);
290 i += 2; /* 0 and last map ver */
291 if (cp1[-3] != KEY_MAP_VER || cp1[-1] > 10 ||
292 i+SHIFT_TBL_SIZE+4 >= sizeof(spk_key_buf)) {
293 pr_warn("i %d %d %d %d\n", i,
294 (int)cp1[-3], (int)cp1[-2], (int)cp1[-1]);
300 cp = spk_s2uchar(cp, cp1);
305 if (i != 0 || cp1[-1] != KEY_MAP_VER || cp1[-2] != 0) {
307 pr_warn("end %d %d %d %d\n", i,
308 (int)cp1[-3], (int)cp1[-2], (int)cp1[-1]);
310 if (spk_set_key_info(in_buff, spk_key_buf)) {
311 spk_set_key_info(spk_key_defaults, spk_key_buf);
313 pr_warn("set key failed\n");
322 * This is called when a user changes the value of the silent parameter.
324 static ssize_t silent_store(struct kobject *kobj, struct kobj_attribute *attr,
325 const char *buf, size_t count)
328 struct vc_data *vc = vc_cons[fg_console].d;
334 if (len > 0 && len < 3) {
339 if (ch < '0' || ch > '7') {
340 pr_warn("silent value '%c' not in range (0,7)\n", ch);
355 spk_shut_up &= ~shut;
361 * This is called when a user reads the synth setting.
363 static ssize_t synth_show(struct kobject *kobj, struct kobj_attribute *attr,
369 rv = sprintf(buf, "%s\n", "none");
371 rv = sprintf(buf, "%s\n", synth->name);
376 * This is called when a user requests to change synthesizers.
378 static ssize_t synth_store(struct kobject *kobj, struct kobj_attribute *attr,
379 const char *buf, size_t count)
382 char new_synth_name[10];
385 if (len < 2 || len > 9)
387 strncpy(new_synth_name, buf, len);
388 if (new_synth_name[len - 1] == '\n')
390 new_synth_name[len] = '\0';
391 spk_strlwr(new_synth_name);
392 if ((synth != NULL) && (!strcmp(new_synth_name, synth->name))) {
393 pr_warn("%s already in use\n", new_synth_name);
394 } else if (synth_init(new_synth_name) != 0) {
395 pr_warn("failed to init synth %s\n", new_synth_name);
402 * This is called when text is sent to the synth via the synth_direct file.
404 static ssize_t synth_direct_store(struct kobject *kobj,
405 struct kobj_attribute *attr, const char *buf, size_t count)
410 const char *ptr = buf;
417 bytes = min_t(size_t, len, 250);
418 strncpy(tmp, ptr, bytes);
421 synth_printf("%s", tmp);
429 * This function is called when a user reads the version.
431 static ssize_t version_show(struct kobject *kobj, struct kobj_attribute *attr,
437 cp += sprintf(cp, "Speakup version %s\n", SPEAKUP_VERSION);
439 cp += sprintf(cp, "%s synthesizer driver version %s\n",
440 synth->name, synth->version);
445 * This is called when a user reads the punctuation settings.
447 static ssize_t punc_show(struct kobject *kobj, struct kobj_attribute *attr,
452 struct st_var_header *p_header;
453 struct punc_var_t *var;
454 struct st_bits_data *pb;
458 p_header = spk_var_header_by_name(attr->attr.name);
459 if (p_header == NULL) {
460 pr_warn("p_header is null, attr->attr.name is %s\n",
465 var = spk_get_punc_var(p_header->var_id);
467 pr_warn("var is null, p_header->var_id is %i\n",
473 pb = (struct st_bits_data *) &spk_punc_info[var->value];
475 for (i = 33; i < 128; i++) {
476 if (!(spk_chartab[i]&mask))
485 * This is called when a user changes the punctuation settings.
487 static ssize_t punc_store(struct kobject *kobj, struct kobj_attribute *attr,
488 const char *buf, size_t count)
491 struct st_var_header *p_header;
492 struct punc_var_t *var;
500 p_header = spk_var_header_by_name(attr->attr.name);
501 if (p_header == NULL) {
502 pr_warn("p_header is null, attr->attr.name is %s\n",
507 var = spk_get_punc_var(p_header->var_id);
509 pr_warn("var is null, p_header->var_id is %i\n",
514 strncpy(punc_buf, buf, x);
516 while (x && punc_buf[x - 1] == '\n')
522 if (*punc_buf == 'd' || *punc_buf == 'r')
523 x = spk_set_mask_bits(0, var->value, 3);
525 x = spk_set_mask_bits(punc_buf, var->value, 3);
532 * This function is called when a user reads one of the variable parameters.
534 ssize_t spk_var_show(struct kobject *kobj, struct kobj_attribute *attr,
538 struct st_var_header *param;
545 param = spk_var_header_by_name(attr->attr.name);
550 var = (struct var_t *) param->data;
551 switch (param->var_type) {
555 rv = sprintf(buf, "%i\n", var->u.n.value);
557 rv = sprintf(buf, "0\n");
563 for (cp = (char *)param->p_val; (ch = *cp); cp++) {
564 if (ch >= ' ' && ch < '~')
567 cp1 += sprintf(cp1, "\\""x%02x", ch);
574 rv = sprintf(buf, "\"\"\n");
578 rv = sprintf(buf, "Bad parameter %s, type %i\n",
579 param->name, param->var_type);
585 EXPORT_SYMBOL_GPL(spk_var_show);
588 * This function is called when a user echos a value to one of the
589 * variable parameters.
591 ssize_t spk_var_store(struct kobject *kobj, struct kobj_attribute *attr,
592 const char *buf, size_t count)
594 struct st_var_header *param;
598 struct var_t *var_data;
602 param = spk_var_header_by_name(attr->attr.name);
605 if (param->data == NULL)
608 cp = spk_xlate((char *) buf);
611 switch (param->var_type) {
614 if (*cp == 'd' || *cp == 'r' || *cp == '\0')
616 else if (*cp == '+' || *cp == '-')
620 value = simple_strtol(cp, NULL, 10);
621 ret = spk_set_num_var(value, param, len);
622 if (ret == -ERANGE) {
623 var_data = param->data;
624 pr_warn("value for %s out of range, expect %d to %d\n",
626 var_data->u.n.low, var_data->u.n.high);
631 if ((len >= 1) && (buf[len - 1] == '\n'))
633 if ((len >= 2) && (buf[0] == '"') && (buf[len - 1] == '"')) {
639 ret = spk_set_string_var(buf, param, len);
641 pr_warn("value too long for %s\n",
645 pr_warn("%s unknown type %d\n",
646 param->name, (int)param->var_type);
650 * If voice was just changed, we might need to reset our default
653 if (strcmp(attr->attr.name, "voice") == 0) {
654 if (synth && synth->default_pitch) {
655 param = spk_var_header_by_name("pitch");
657 spk_set_num_var(synth->default_pitch[value],
658 param, E_NEW_DEFAULT);
659 spk_set_num_var(0, param, E_DEFAULT);
662 if (synth && synth->default_vol) {
663 param = spk_var_header_by_name("vol");
665 spk_set_num_var(synth->default_vol[value],
666 param, E_NEW_DEFAULT);
667 spk_set_num_var(0, param, E_DEFAULT);
673 if (ret == -ERESTART)
674 pr_info("%s reset to default value\n", attr->attr.name);
677 EXPORT_SYMBOL_GPL(spk_var_store);
680 * Functions for reading and writing lists of i18n messages. Incomplete.
683 static ssize_t message_show_helper(char *buf, enum msg_index_t first,
684 enum msg_index_t last)
686 size_t bufsize = PAGE_SIZE;
687 char *buf_pointer = buf;
689 enum msg_index_t cursor;
691 *buf_pointer = '\0'; /* buf_pointer always looking at a NUL byte. */
693 for (cursor = first; cursor <= last; cursor++, index++) {
696 printed = scnprintf(buf_pointer, bufsize, "%d\t%s\n",
697 index, spk_msg_get(cursor));
698 buf_pointer += printed;
702 return buf_pointer - buf;
705 static void report_msg_status(int reset, int received, int used,
706 int rejected, char *groupname)
712 pr_info("i18n messages from group %s reset to defaults\n",
714 } else if (received) {
715 len = snprintf(buf, sizeof(buf),
716 " updated %d of %d i18n messages from group %s\n",
717 used, received, groupname);
719 snprintf(buf + (len - 1), sizeof(buf) - (len - 1),
720 " with %d reject%s\n",
721 rejected, rejected > 1 ? "s" : "");
726 static ssize_t message_store_helper(const char *buf, size_t count,
727 struct msg_group_t *group)
729 char *cp = (char *) buf;
730 char *end = cp + count;
731 char *linefeed = NULL;
733 ssize_t msg_stored = 0;
734 ssize_t retval = count;
735 size_t desc_length = 0;
736 unsigned long index = 0;
741 enum msg_index_t firstmessage = group->start;
742 enum msg_index_t lastmessage = group->end;
743 enum msg_index_t curmessage;
747 while ((cp < end) && (*cp == ' ' || *cp == '\t'))
752 if (strchr("dDrR", *cp)) {
758 linefeed = strchr(cp, '\n');
770 index = simple_strtoul(cp, &temp, 10);
772 while ((temp < linefeed) && (*temp == ' ' || *temp == '\t'))
775 desc_length = linefeed - temp;
776 curmessage = firstmessage + index;
779 * Note the check (curmessage < firstmessage). It is not
780 * redundant. Suppose that the user gave us an index
781 * equal to ULONG_MAX - 1. If firstmessage > 1, then
782 * firstmessage + index < firstmessage!
785 if ((curmessage < firstmessage) || (curmessage > lastmessage)) {
791 msg_stored = spk_msg_set(curmessage, temp, desc_length);
792 if (msg_stored < 0) {
794 if (msg_stored == -ENOMEM)
805 spk_reset_msg_group(group);
807 report_msg_status(reset, received, used, rejected, group->name);
811 static ssize_t message_show(struct kobject *kobj,
812 struct kobj_attribute *attr, char *buf)
815 struct msg_group_t *group = spk_find_msg_group(attr->attr.name);
820 retval = message_show_helper(buf, group->start, group->end);
825 static ssize_t message_store(struct kobject *kobj, struct kobj_attribute *attr,
826 const char *buf, size_t count)
829 struct msg_group_t *group = spk_find_msg_group(attr->attr.name);
832 retval = message_store_helper(buf, count, group);
837 * Declare the attributes.
839 static struct kobj_attribute keymap_attribute =
840 __ATTR(keymap, ROOT_W, keymap_show, keymap_store);
841 static struct kobj_attribute silent_attribute =
842 __ATTR(silent, USER_W, NULL, silent_store);
843 static struct kobj_attribute synth_attribute =
844 __ATTR(synth, USER_RW, synth_show, synth_store);
845 static struct kobj_attribute synth_direct_attribute =
846 __ATTR(synth_direct, USER_W, NULL, synth_direct_store);
847 static struct kobj_attribute version_attribute =
850 static struct kobj_attribute delimiters_attribute =
851 __ATTR(delimiters, USER_RW, punc_show, punc_store);
852 static struct kobj_attribute ex_num_attribute =
853 __ATTR(ex_num, USER_RW, punc_show, punc_store);
854 static struct kobj_attribute punc_all_attribute =
855 __ATTR(punc_all, USER_RW, punc_show, punc_store);
856 static struct kobj_attribute punc_most_attribute =
857 __ATTR(punc_most, USER_RW, punc_show, punc_store);
858 static struct kobj_attribute punc_some_attribute =
859 __ATTR(punc_some, USER_RW, punc_show, punc_store);
860 static struct kobj_attribute repeats_attribute =
861 __ATTR(repeats, USER_RW, punc_show, punc_store);
863 static struct kobj_attribute attrib_bleep_attribute =
864 __ATTR(attrib_bleep, USER_RW, spk_var_show, spk_var_store);
865 static struct kobj_attribute bell_pos_attribute =
866 __ATTR(bell_pos, USER_RW, spk_var_show, spk_var_store);
867 static struct kobj_attribute bleep_time_attribute =
868 __ATTR(bleep_time, USER_RW, spk_var_show, spk_var_store);
869 static struct kobj_attribute bleeps_attribute =
870 __ATTR(bleeps, USER_RW, spk_var_show, spk_var_store);
871 static struct kobj_attribute cursor_time_attribute =
872 __ATTR(cursor_time, USER_RW, spk_var_show, spk_var_store);
873 static struct kobj_attribute key_echo_attribute =
874 __ATTR(key_echo, USER_RW, spk_var_show, spk_var_store);
875 static struct kobj_attribute no_interrupt_attribute =
876 __ATTR(no_interrupt, USER_RW, spk_var_show, spk_var_store);
877 static struct kobj_attribute punc_level_attribute =
878 __ATTR(punc_level, USER_RW, spk_var_show, spk_var_store);
879 static struct kobj_attribute reading_punc_attribute =
880 __ATTR(reading_punc, USER_RW, spk_var_show, spk_var_store);
881 static struct kobj_attribute say_control_attribute =
882 __ATTR(say_control, USER_RW, spk_var_show, spk_var_store);
883 static struct kobj_attribute say_word_ctl_attribute =
884 __ATTR(say_word_ctl, USER_RW, spk_var_show, spk_var_store);
885 static struct kobj_attribute spell_delay_attribute =
886 __ATTR(spell_delay, USER_RW, spk_var_show, spk_var_store);
889 * These attributes are i18n related.
891 static struct kobj_attribute announcements_attribute =
892 __ATTR(announcements, USER_RW, message_show, message_store);
893 static struct kobj_attribute characters_attribute =
894 __ATTR(characters, USER_RW, chars_chartab_show, chars_chartab_store);
895 static struct kobj_attribute chartab_attribute =
896 __ATTR(chartab, USER_RW, chars_chartab_show, chars_chartab_store);
897 static struct kobj_attribute ctl_keys_attribute =
898 __ATTR(ctl_keys, USER_RW, message_show, message_store);
899 static struct kobj_attribute colors_attribute =
900 __ATTR(colors, USER_RW, message_show, message_store);
901 static struct kobj_attribute formatted_attribute =
902 __ATTR(formatted, USER_RW, message_show, message_store);
903 static struct kobj_attribute function_names_attribute =
904 __ATTR(function_names, USER_RW, message_show, message_store);
905 static struct kobj_attribute key_names_attribute =
906 __ATTR(key_names, USER_RW, message_show, message_store);
907 static struct kobj_attribute states_attribute =
908 __ATTR(states, USER_RW, message_show, message_store);
911 * Create groups of attributes so that we can create and destroy them all
914 static struct attribute *main_attrs[] = {
915 &keymap_attribute.attr,
916 &silent_attribute.attr,
917 &synth_attribute.attr,
918 &synth_direct_attribute.attr,
919 &version_attribute.attr,
920 &delimiters_attribute.attr,
921 &ex_num_attribute.attr,
922 &punc_all_attribute.attr,
923 &punc_most_attribute.attr,
924 &punc_some_attribute.attr,
925 &repeats_attribute.attr,
926 &attrib_bleep_attribute.attr,
927 &bell_pos_attribute.attr,
928 &bleep_time_attribute.attr,
929 &bleeps_attribute.attr,
930 &cursor_time_attribute.attr,
931 &key_echo_attribute.attr,
932 &no_interrupt_attribute.attr,
933 &punc_level_attribute.attr,
934 &reading_punc_attribute.attr,
935 &say_control_attribute.attr,
936 &say_word_ctl_attribute.attr,
937 &spell_delay_attribute.attr,
941 static struct attribute *i18n_attrs[] = {
942 &announcements_attribute.attr,
943 &characters_attribute.attr,
944 &chartab_attribute.attr,
945 &ctl_keys_attribute.attr,
946 &colors_attribute.attr,
947 &formatted_attribute.attr,
948 &function_names_attribute.attr,
949 &key_names_attribute.attr,
950 &states_attribute.attr,
955 * An unnamed attribute group will put all of the attributes directly in
956 * the kobject directory. If we specify a name, a subdirectory will be
957 * created for the attributes with the directory being the name of the
960 static struct attribute_group main_attr_group = {
964 static struct attribute_group i18n_attr_group = {
969 static struct kobject *accessibility_kobj;
970 struct kobject *speakup_kobj;
972 int speakup_kobj_init(void)
977 * Create a simple kobject with the name of "accessibility",
978 * located under /sys/
980 * As this is a simple directory, no uevent will be sent to
981 * userspace. That is why this function should not be used for
982 * any type of dynamic kobjects, where the name and number are
983 * not known ahead of time.
985 accessibility_kobj = kobject_create_and_add("accessibility", NULL);
986 if (!accessibility_kobj) {
991 speakup_kobj = kobject_create_and_add("speakup", accessibility_kobj);
997 /* Create the files associated with this kobject */
998 retval = sysfs_create_group(speakup_kobj, &main_attr_group);
1002 retval = sysfs_create_group(speakup_kobj, &i18n_attr_group);
1009 sysfs_remove_group(speakup_kobj, &main_attr_group);
1011 kobject_put(speakup_kobj);
1013 kobject_put(accessibility_kobj);
1018 void speakup_kobj_exit(void)
1020 sysfs_remove_group(speakup_kobj, &i18n_attr_group);
1021 sysfs_remove_group(speakup_kobj, &main_attr_group);
1022 kobject_put(speakup_kobj);
1023 kobject_put(accessibility_kobj);