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",
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 (characters
[index
] != default_chars
[index
])
189 kfree(characters
[index
]);
190 characters
[index
] = desc
;
193 charclass
= chartab_get_value(keyword
);
194 if (charclass
== 0) {
199 if (charclass
!= spk_chartab
[index
]) {
200 spk_chartab
[index
] = charclass
;
209 reset_default_chars();
211 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
= 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
= kmalloc(count
+ 1, GFP_ATOMIC
);
273 memcpy(in_buff
, buf
, count
+ 1);
274 if (strchr("dDrR", *in_buff
)) {
275 set_key_info(key_defaults
, key_buf
);
276 pr_info("keymap set to default values\n");
281 if (in_buff
[count
- 1] == '\n')
282 in_buff
[count
- 1] = '\0';
284 cp1
= (u_char
*)in_buff
;
285 for (i
= 0; i
< 3; i
++) {
286 cp
= s2uchar(cp
, cp1
);
291 i
+= 2; /* 0 and last map ver */
292 if (cp1
[-3] != KEY_MAP_VER
|| cp1
[-1] > 10 ||
293 i
+SHIFT_TBL_SIZE
+4 >= sizeof(key_buf
)) {
294 pr_warn("i %d %d %d %d\n", i
,
295 (int)cp1
[-3], (int)cp1
[-2], (int)cp1
[-1]);
301 cp
= s2uchar(cp
, cp1
);
306 if (i
!= 0 || cp1
[-1] != KEY_MAP_VER
|| cp1
[-2] != 0) {
308 pr_warn("end %d %d %d %d\n", i
,
309 (int)cp1
[-3], (int)cp1
[-2], (int)cp1
[-1]);
311 if (set_key_info(in_buff
, key_buf
)) {
312 set_key_info(key_defaults
, key_buf
);
314 pr_warn("set key failed\n");
323 * This is called when a user changes the value of the silent parameter.
325 static ssize_t
silent_store(struct kobject
*kobj
, struct kobj_attribute
*attr
,
326 const char *buf
, size_t count
)
329 struct vc_data
*vc
= vc_cons
[fg_console
].d
;
335 if (len
> 0 && len
< 3) {
340 if (ch
< '0' || ch
> '7') {
341 pr_warn("silent value '%c' not in range (0,7)\n", ch
);
356 spk_shut_up
&= ~shut
;
362 * This is called when a user reads the synth setting.
364 static ssize_t
synth_show(struct kobject
*kobj
, struct kobj_attribute
*attr
,
370 rv
= sprintf(buf
, "%s\n", "none");
372 rv
= sprintf(buf
, "%s\n", synth
->name
);
377 * This is called when a user requests to change synthesizers.
379 static ssize_t
synth_store(struct kobject
*kobj
, struct kobj_attribute
*attr
,
380 const char *buf
, size_t count
)
383 char new_synth_name
[10];
386 if (len
< 2 || len
> 9)
388 strncpy(new_synth_name
, buf
, len
);
389 if (new_synth_name
[len
- 1] == '\n')
391 new_synth_name
[len
] = '\0';
392 strlwr(new_synth_name
);
393 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
);
403 * This is called when text is sent to the synth via the synth_direct file.
405 static ssize_t
synth_direct_store(struct kobject
*kobj
,
406 struct kobj_attribute
*attr
, const char *buf
, size_t count
)
411 const char *ptr
= buf
;
418 bytes
= min_t(size_t, len
, 250);
419 strncpy(tmp
, ptr
, bytes
);
422 synth_printf("%s", tmp
);
430 * This function is called when a user reads the version.
432 static ssize_t
version_show(struct kobject
*kobj
, struct kobj_attribute
*attr
,
438 cp
+= sprintf(cp
, "Speakup version %s\n", SPEAKUP_VERSION
);
440 cp
+= sprintf(cp
, "%s synthesizer driver version %s\n",
441 synth
->name
, synth
->version
);
446 * This is called when a user reads the punctuation settings.
448 static ssize_t
punc_show(struct kobject
*kobj
, struct kobj_attribute
*attr
,
453 struct st_var_header
*p_header
;
454 struct punc_var_t
*var
;
455 struct st_bits_data
*pb
;
459 p_header
= var_header_by_name(attr
->attr
.name
);
460 if (p_header
== NULL
) {
461 pr_warn("p_header is null, attr->attr.name is %s\n",
466 var
= get_punc_var(p_header
->var_id
);
468 pr_warn("var is null, p_header->var_id is %i\n",
474 pb
= (struct st_bits_data
*) &punc_info
[var
->value
];
476 for (i
= 33; i
< 128; i
++) {
477 if (!(spk_chartab
[i
]&mask
))
486 * This is called when a user changes the punctuation settings.
488 static ssize_t
punc_store(struct kobject
*kobj
, struct kobj_attribute
*attr
,
489 const char *buf
, size_t count
)
492 struct st_var_header
*p_header
;
493 struct punc_var_t
*var
;
501 p_header
= var_header_by_name(attr
->attr
.name
);
502 if (p_header
== NULL
) {
503 pr_warn("p_header is null, attr->attr.name is %s\n",
508 var
= get_punc_var(p_header
->var_id
);
510 pr_warn("var is null, p_header->var_id is %i\n",
515 strncpy(punc_buf
, buf
, x
);
517 while (x
&& punc_buf
[x
- 1] == '\n')
523 if (*punc_buf
== 'd' || *punc_buf
== 'r')
524 x
= set_mask_bits(0, var
->value
, 3);
526 x
= set_mask_bits(punc_buf
, var
->value
, 3);
533 * This function is called when a user reads one of the variable parameters.
535 ssize_t
spk_var_show(struct kobject
*kobj
, struct kobj_attribute
*attr
,
539 struct st_var_header
*param
;
546 param
= var_header_by_name(attr
->attr
.name
);
551 var
= (struct var_t
*) param
->data
;
552 switch (param
->var_type
) {
556 rv
= sprintf(buf
, "%i\n", var
->u
.n
.value
);
558 rv
= sprintf(buf
, "0\n");
564 for (cp
= (char *)param
->p_val
; (ch
= *cp
); cp
++) {
565 if (ch
>= ' ' && ch
< '~')
568 cp1
+= sprintf(cp1
, "\\""x%02x", ch
);
575 rv
= sprintf(buf
, "\"\"\n");
579 rv
= sprintf(buf
, "Bad parameter %s, type %i\n",
580 param
->name
, param
->var_type
);
586 EXPORT_SYMBOL_GPL(spk_var_show
);
589 * This function is called when a user echos a value to one of the
590 * variable parameters.
592 ssize_t
spk_var_store(struct kobject
*kobj
, struct kobj_attribute
*attr
,
593 const char *buf
, size_t count
)
595 struct st_var_header
*param
;
599 struct var_t
*var_data
;
603 param
= var_header_by_name(attr
->attr
.name
);
606 if (param
->data
== NULL
)
609 cp
= xlate((char *) buf
);
612 switch (param
->var_type
) {
615 if (*cp
== 'd' || *cp
== 'r' || *cp
== '\0')
617 else if (*cp
== '+' || *cp
== '-')
621 speakup_s2i(cp
, &value
);
622 ret
= set_num_var(value
, param
, len
);
623 if (ret
== E_RANGE
) {
624 var_data
= param
->data
;
625 pr_warn("value for %s out of range, expect %d to %d\n",
627 var_data
->u
.n
.low
, var_data
->u
.n
.high
);
632 if ((len
>= 1) && (buf
[len
- 1] == '\n'))
634 if ((len
>= 2) && (buf
[0] == '"') && (buf
[len
- 1] == '"')) {
640 ret
= set_string_var(buf
, param
, len
);
641 if (ret
== E_TOOLONG
)
642 pr_warn("value too long for %s\n",
646 pr_warn("%s unknown type %d\n",
647 param
->name
, (int)param
->var_type
);
651 * If voice was just changed, we might need to reset our default
654 if (strcmp(attr
->attr
.name
, "voice") == 0) {
655 if (synth
&& synth
->default_pitch
) {
656 param
= var_header_by_name("pitch");
658 set_num_var(synth
->default_pitch
[value
], param
,
660 set_num_var(0, param
, E_DEFAULT
);
663 if (synth
&& synth
->default_vol
) {
664 param
= var_header_by_name("vol");
666 set_num_var(synth
->default_vol
[value
], param
,
668 set_num_var(0, param
, E_DEFAULT
);
674 if (ret
== SET_DEFAULT
)
675 pr_info("%s reset to default value\n", attr
->attr
.name
);
678 EXPORT_SYMBOL_GPL(spk_var_store
);
681 * Functions for reading and writing lists of i18n messages. Incomplete.
684 static ssize_t
message_show_helper(char *buf
, enum msg_index_t first
,
685 enum msg_index_t last
)
687 size_t bufsize
= PAGE_SIZE
;
688 char *buf_pointer
= buf
;
690 enum msg_index_t cursor
;
692 *buf_pointer
= '\0'; /* buf_pointer always looking at a NUL byte. */
694 for (cursor
= first
; cursor
<= last
; cursor
++, index
++) {
697 printed
= scnprintf(buf_pointer
, bufsize
, "%d\t%s\n",
698 index
, msg_get(cursor
));
699 buf_pointer
+= printed
;
703 return buf_pointer
- buf
;
706 static void report_msg_status(int reset
, int received
, int used
,
707 int rejected
, char *groupname
)
713 pr_info("i18n messages from group %s reset to defaults\n",
715 } else if (received
) {
716 len
= snprintf(buf
, sizeof(buf
),
717 " updated %d of %d i18n messages from group %s\n",
718 used
, received
, groupname
);
720 snprintf(buf
+ (len
- 1), sizeof(buf
) - (len
- 1),
721 " with %d reject%s\n",
722 rejected
, rejected
> 1 ? "s" : "");
727 static ssize_t
message_store_helper(const char *buf
, size_t count
,
728 struct msg_group_t
*group
)
730 char *cp
= (char *) buf
;
731 char *end
= cp
+ count
;
732 char *linefeed
= NULL
;
734 ssize_t msg_stored
= 0;
735 ssize_t retval
= count
;
736 size_t desc_length
= 0;
737 unsigned long index
= 0;
742 enum msg_index_t firstmessage
= group
->start
;
743 enum msg_index_t lastmessage
= group
->end
;
744 enum msg_index_t curmessage
;
748 while ((cp
< end
) && (*cp
== ' ' || *cp
== '\t'))
753 if (strchr("dDrR", *cp
)) {
759 linefeed
= strchr(cp
, '\n');
771 index
= simple_strtoul(cp
, &temp
, 10);
773 while ((temp
< linefeed
) && (*temp
== ' ' || *temp
== '\t'))
776 desc_length
= linefeed
- temp
;
777 curmessage
= firstmessage
+ index
;
780 * Note the check (curmessage < firstmessage). It is not
781 * redundant. Suppose that the user gave us an index
782 * equal to ULONG_MAX - 1. If firstmessage > 1, then
783 * firstmessage + index < firstmessage!
786 if ((curmessage
< firstmessage
) || (curmessage
> lastmessage
)) {
792 msg_stored
= msg_set(curmessage
, temp
, desc_length
);
793 if (msg_stored
< 0) {
795 if (msg_stored
== -ENOMEM
)
806 reset_msg_group(group
);
808 report_msg_status(reset
, received
, used
, rejected
, group
->name
);
812 static ssize_t
message_show(struct kobject
*kobj
,
813 struct kobj_attribute
*attr
, char *buf
)
816 struct msg_group_t
*group
= find_msg_group(attr
->attr
.name
);
821 retval
= message_show_helper(buf
, group
->start
, group
->end
);
826 static ssize_t
message_store(struct kobject
*kobj
, struct kobj_attribute
*attr
,
827 const char *buf
, size_t count
)
830 struct msg_group_t
*group
= find_msg_group(attr
->attr
.name
);
833 retval
= message_store_helper(buf
, count
, group
);
838 * Declare the attributes.
840 static struct kobj_attribute keymap_attribute
=
841 __ATTR(keymap
, ROOT_W
, keymap_show
, keymap_store
);
842 static struct kobj_attribute silent_attribute
=
843 __ATTR(silent
, USER_W
, NULL
, silent_store
);
844 static struct kobj_attribute synth_attribute
=
845 __ATTR(synth
, USER_RW
, synth_show
, synth_store
);
846 static struct kobj_attribute synth_direct_attribute
=
847 __ATTR(synth_direct
, USER_W
, NULL
, synth_direct_store
);
848 static struct kobj_attribute version_attribute
=
851 static struct kobj_attribute delimiters_attribute
=
852 __ATTR(delimiters
, USER_RW
, punc_show
, punc_store
);
853 static struct kobj_attribute ex_num_attribute
=
854 __ATTR(ex_num
, USER_RW
, punc_show
, punc_store
);
855 static struct kobj_attribute punc_all_attribute
=
856 __ATTR(punc_all
, USER_RW
, punc_show
, punc_store
);
857 static struct kobj_attribute punc_most_attribute
=
858 __ATTR(punc_most
, USER_RW
, punc_show
, punc_store
);
859 static struct kobj_attribute punc_some_attribute
=
860 __ATTR(punc_some
, USER_RW
, punc_show
, punc_store
);
861 static struct kobj_attribute repeats_attribute
=
862 __ATTR(repeats
, USER_RW
, punc_show
, punc_store
);
864 static struct kobj_attribute attrib_bleep_attribute
=
865 __ATTR(attrib_bleep
, USER_RW
, spk_var_show
, spk_var_store
);
866 static struct kobj_attribute bell_pos_attribute
=
867 __ATTR(bell_pos
, USER_RW
, spk_var_show
, spk_var_store
);
868 static struct kobj_attribute bleep_time_attribute
=
869 __ATTR(bleep_time
, USER_RW
, spk_var_show
, spk_var_store
);
870 static struct kobj_attribute bleeps_attribute
=
871 __ATTR(bleeps
, USER_RW
, spk_var_show
, spk_var_store
);
872 static struct kobj_attribute cursor_time_attribute
=
873 __ATTR(cursor_time
, USER_RW
, spk_var_show
, spk_var_store
);
874 static struct kobj_attribute key_echo_attribute
=
875 __ATTR(key_echo
, USER_RW
, spk_var_show
, spk_var_store
);
876 static struct kobj_attribute no_interrupt_attribute
=
877 __ATTR(no_interrupt
, USER_RW
, spk_var_show
, spk_var_store
);
878 static struct kobj_attribute punc_level_attribute
=
879 __ATTR(punc_level
, USER_RW
, spk_var_show
, spk_var_store
);
880 static struct kobj_attribute reading_punc_attribute
=
881 __ATTR(reading_punc
, USER_RW
, spk_var_show
, spk_var_store
);
882 static struct kobj_attribute say_control_attribute
=
883 __ATTR(say_control
, USER_RW
, spk_var_show
, spk_var_store
);
884 static struct kobj_attribute say_word_ctl_attribute
=
885 __ATTR(say_word_ctl
, USER_RW
, spk_var_show
, spk_var_store
);
886 static struct kobj_attribute spell_delay_attribute
=
887 __ATTR(spell_delay
, USER_RW
, spk_var_show
, spk_var_store
);
890 * These attributes are i18n related.
892 static struct kobj_attribute announcements_attribute
=
893 __ATTR(announcements
, USER_RW
, message_show
, message_store
);
894 static struct kobj_attribute characters_attribute
=
895 __ATTR(characters
, USER_RW
, chars_chartab_show
, chars_chartab_store
);
896 static struct kobj_attribute chartab_attribute
=
897 __ATTR(chartab
, USER_RW
, chars_chartab_show
, chars_chartab_store
);
898 static struct kobj_attribute ctl_keys_attribute
=
899 __ATTR(ctl_keys
, USER_RW
, message_show
, message_store
);
900 static struct kobj_attribute colors_attribute
=
901 __ATTR(colors
, USER_RW
, message_show
, message_store
);
902 static struct kobj_attribute formatted_attribute
=
903 __ATTR(formatted
, USER_RW
, message_show
, message_store
);
904 static struct kobj_attribute function_names_attribute
=
905 __ATTR(function_names
, USER_RW
, message_show
, message_store
);
906 static struct kobj_attribute key_names_attribute
=
907 __ATTR(key_names
, USER_RW
, message_show
, message_store
);
908 static struct kobj_attribute states_attribute
=
909 __ATTR(states
, USER_RW
, message_show
, message_store
);
912 * Create groups of attributes so that we can create and destroy them all
915 static struct attribute
*main_attrs
[] = {
916 &keymap_attribute
.attr
,
917 &silent_attribute
.attr
,
918 &synth_attribute
.attr
,
919 &synth_direct_attribute
.attr
,
920 &version_attribute
.attr
,
921 &delimiters_attribute
.attr
,
922 &ex_num_attribute
.attr
,
923 &punc_all_attribute
.attr
,
924 &punc_most_attribute
.attr
,
925 &punc_some_attribute
.attr
,
926 &repeats_attribute
.attr
,
927 &attrib_bleep_attribute
.attr
,
928 &bell_pos_attribute
.attr
,
929 &bleep_time_attribute
.attr
,
930 &bleeps_attribute
.attr
,
931 &cursor_time_attribute
.attr
,
932 &key_echo_attribute
.attr
,
933 &no_interrupt_attribute
.attr
,
934 &punc_level_attribute
.attr
,
935 &reading_punc_attribute
.attr
,
936 &say_control_attribute
.attr
,
937 &say_word_ctl_attribute
.attr
,
938 &spell_delay_attribute
.attr
,
942 static struct attribute
*i18n_attrs
[] = {
943 &announcements_attribute
.attr
,
944 &characters_attribute
.attr
,
945 &chartab_attribute
.attr
,
946 &ctl_keys_attribute
.attr
,
947 &colors_attribute
.attr
,
948 &formatted_attribute
.attr
,
949 &function_names_attribute
.attr
,
950 &key_names_attribute
.attr
,
951 &states_attribute
.attr
,
956 * An unnamed attribute group will put all of the attributes directly in
957 * the kobject directory. If we specify a name, a subdirectory will be
958 * created for the attributes with the directory being the name of the
961 static struct attribute_group main_attr_group
= {
965 static struct attribute_group i18n_attr_group
= {
970 static struct kobject
*accessibility_kobj
;
971 struct kobject
*speakup_kobj
;
973 int speakup_kobj_init(void)
978 * Create a simple kobject with the name of "accessibility",
979 * located under /sys/
981 * As this is a simple directory, no uevent will be sent to
982 * userspace. That is why this function should not be used for
983 * any type of dynamic kobjects, where the name and number are
984 * not known ahead of time.
986 accessibility_kobj
= kobject_create_and_add("accessibility", NULL
);
987 if (!accessibility_kobj
) {
992 speakup_kobj
= kobject_create_and_add("speakup", accessibility_kobj
);
998 /* Create the files associated with this kobject */
999 retval
= sysfs_create_group(speakup_kobj
, &main_attr_group
);
1003 retval
= sysfs_create_group(speakup_kobj
, &i18n_attr_group
);
1010 sysfs_remove_group(speakup_kobj
, &main_attr_group
);
1012 kobject_put(speakup_kobj
);
1014 kobject_put(accessibility_kobj
);
1019 void speakup_kobj_exit(void)
1021 sysfs_remove_group(speakup_kobj
, &i18n_attr_group
);
1022 sysfs_remove_group(speakup_kobj
, &main_attr_group
);
1023 kobject_put(speakup_kobj
);
1024 kobject_put(accessibility_kobj
);