Merge branch 'stable' of git://git.kernel.org/pub/scm/linux/kernel/git/cmetcalf/linux...
[firefly-linux-kernel-4.4.55.git] / drivers / staging / speakup / kobjects.c
1 /*
2  * Speakup kobject implementation
3  *
4  * Copyright (C) 2009 William Hubbs
5  *
6  * This code is based on kobject-example.c, which came with linux 2.6.x.
7  *
8  * Copyright (C) 2004-2007 Greg Kroah-Hartman <greg@kroah.com>
9  * Copyright (C) 2007 Novell Inc.
10  *
11  * Released under the GPL version 2 only.
12  *
13  */
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>
21
22 #include "speakup.h"
23 #include "spk_priv.h"
24
25 /*
26  * This is called when a user reads the characters or chartab sys file.
27  */
28 static ssize_t chars_chartab_show(struct kobject *kobj,
29         struct kobj_attribute *attr, char *buf)
30 {
31         int i;
32         int len = 0;
33         char *cp;
34         char *buf_pointer = buf;
35         size_t bufsize = PAGE_SIZE;
36         unsigned long flags;
37
38         spk_lock(flags);
39         *buf_pointer = '\0';
40         for (i = 0; i < 256; i++) {
41                 if (bufsize <= 1)
42                         break;
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))
48                                 cp = "B_CTL";
49                         else if (IS_TYPE(i, WDLM))
50                                 cp = "WDLM";
51                         else if (IS_TYPE(i, A_PUNC))
52                                 cp = "A_PUNC";
53                         else if (IS_TYPE(i, PUNC))
54                                 cp = "PUNC";
55                         else if (IS_TYPE(i, NUM))
56                                 cp = "NUM";
57                         else if (IS_TYPE(i, A_CAP))
58                                 cp = "A_CAP";
59                         else if (IS_TYPE(i, ALPHA))
60                                 cp = "ALPHA";
61                         else if (IS_TYPE(i, B_CAPSYM))
62                                 cp = "B_CAPSYM";
63                         else if (IS_TYPE(i, B_SYM))
64                                 cp = "B_SYM";
65                         else
66                                 cp = "0";
67                         len =
68                             scnprintf(buf_pointer, bufsize, "%d\t%s\n", i, cp);
69                 }
70                 bufsize -= len;
71                 buf_pointer += len;
72         }
73         spk_unlock(flags);
74         return buf_pointer - buf;
75 }
76
77 /*
78  * Print informational messages or warnings after updating
79  * character descriptions or chartab entries.
80  */
81 static void report_char_chartab_status(int reset, int received, int used,
82         int rejected, int do_characters)
83 {
84         char *object_type[] = {
85                 "character class entries",
86                 "character descriptions",
87         };
88         int len;
89         char buf[80];
90
91         if (reset) {
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]);
97                 if (rejected)
98                         snprintf(buf + (len - 1), sizeof(buf) - (len - 1),
99                                  " with %d reject%s\n",
100                                  rejected, rejected > 1 ? "s" : "");
101                 printk(buf);
102         }
103 }
104
105 /*
106  * This is called when a user changes the characters or chartab parameters.
107  */
108 static ssize_t chars_chartab_store(struct kobject *kobj,
109         struct kobj_attribute *attr, const char *buf, size_t count)
110 {
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. */
116         char *temp = NULL;
117         char *desc = NULL;
118         ssize_t retval = count;
119         unsigned long flags;
120         unsigned long index = 0;
121         int charclass = 0;
122         int received = 0;
123         int used = 0;
124         int rejected = 0;
125         int reset = 0;
126         int do_characters = !strcmp(attr->attr.name, "characters");
127         size_t desc_length = 0;
128         int i;
129
130         spk_lock(flags);
131         while (cp < end) {
132
133                 while ((cp < end) && (*cp == ' ' || *cp == '\t'))
134                         cp++;
135
136                 if (cp == end)
137                         break;
138                 if ((*cp == '\n') || strchr("dDrR", *cp)) {
139                         reset = 1;
140                         break;
141                 }
142                 received++;
143
144                 linefeed = strchr(cp, '\n');
145                 if (!linefeed) {
146                         rejected++;
147                         break;
148                 }
149
150                 if (!isdigit(*cp)) {
151                         rejected++;
152                         cp = linefeed + 1;
153                         continue;
154                 }
155
156                 index = simple_strtoul(cp, &temp, 10);
157                 if (index > 255) {
158                         rejected++;
159                         cp = linefeed + 1;
160                         continue;
161                 }
162
163                 while ((temp < linefeed) && (*temp == ' ' || *temp == '\t'))
164                         temp++;
165
166                 desc_length = linefeed - temp;
167                 if (desc_length > MAX_DESC_LEN) {
168                         rejected++;
169                         cp = linefeed + 1;
170                         continue;
171                 }
172                 if (do_characters) {
173                         desc = kmalloc(desc_length + 1, GFP_ATOMIC);
174                         if (!desc) {
175                                 retval = -ENOMEM;
176                                 reset = 1;      /* just reset on error. */
177                                 break;
178                         }
179                         outptr = desc;
180                 } else {
181                         outptr = keyword;
182                 }
183
184                 for (i = 0; i < desc_length; i++)
185                         outptr[i] = temp[i];
186                 outptr[desc_length] = '\0';
187
188                 if (do_characters) {
189                         if (spk_characters[index] != spk_default_chars[index])
190                                 kfree(spk_characters[index]);
191                         spk_characters[index] = desc;
192                         used++;
193                 } else {
194                         charclass = spk_chartab_get_value(keyword);
195                         if (charclass == 0) {
196                                 rejected++;
197                                 cp = linefeed + 1;
198                                 continue;
199                         }
200                         if (charclass != spk_chartab[index]) {
201                                 spk_chartab[index] = charclass;
202                                 used++;
203                         }
204                 }
205                 cp = linefeed + 1;
206         }
207
208         if (reset) {
209                 if (do_characters)
210                         spk_reset_default_chars();
211                 else
212                         spk_reset_default_chartab();
213         }
214
215         spk_unlock(flags);
216         report_char_chartab_status(reset, received, used, rejected,
217                 do_characters);
218         return retval;
219 }
220
221 /*
222  * This is called when a user reads the keymap parameter.
223  */
224 static ssize_t keymap_show(struct kobject *kobj, struct kobj_attribute *attr,
225         char *buf)
226 {
227         char *cp = buf;
228         int i;
229         int n;
230         int num_keys;
231         int nstates;
232         u_char *cp1;
233         u_char ch;
234         unsigned long flags;
235         spk_lock(flags);
236         cp1 = spk_key_buf + SHIFT_TBL_SIZE;
237         num_keys = (int)(*cp1);
238         nstates = (int)cp1[1];
239         cp += sprintf(cp, "%d, %d, %d,\n", KEY_MAP_VER, num_keys, nstates);
240         cp1 += 2; /* now pointing at shift states */
241         /* dump num_keys+1 as first row is shift states + flags,
242          * each subsequent row is key + states */
243         for (n = 0; n <= num_keys; n++) {
244                 for (i = 0; i <= nstates; i++) {
245                         ch = *cp1++;
246                         cp += sprintf(cp, "%d,", (int)ch);
247                         *cp++ = (i < nstates) ? SPACE : '\n';
248                 }
249         }
250         cp += sprintf(cp, "0, %d\n", KEY_MAP_VER);
251         spk_unlock(flags);
252         return (int)(cp-buf);
253 }
254
255 /*
256  * This is called when a user changes the keymap parameter.
257  */
258 static ssize_t keymap_store(struct kobject *kobj, struct kobj_attribute *attr,
259         const char *buf, size_t count)
260 {
261         int i;
262         ssize_t ret = count;
263         char *in_buff = NULL;
264         char *cp;
265         u_char *cp1;
266         unsigned long flags;
267
268         spk_lock(flags);
269         in_buff = kmemdup(buf, count + 1, GFP_ATOMIC);
270         if (!in_buff) {
271                 spk_unlock(flags);
272                 return -ENOMEM;
273         }
274         if (strchr("dDrR", *in_buff)) {
275                 spk_set_key_info(spk_key_defaults, spk_key_buf);
276                 pr_info("keymap set to default values\n");
277                 kfree(in_buff);
278                 spk_unlock(flags);
279                 return count;
280         }
281         if (in_buff[count - 1] == '\n')
282                 in_buff[count - 1] = '\0';
283         cp = in_buff;
284         cp1 = (u_char *)in_buff;
285         for (i = 0; i < 3; i++) {
286                 cp = spk_s2uchar(cp, cp1);
287                 cp1++;
288         }
289         i = (int)cp1[-2]+1;
290         i *= (int)cp1[-1]+1;
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(spk_key_buf)) {
294                 pr_warn("i %d %d %d %d\n", i,
295                                 (int)cp1[-3], (int)cp1[-2], (int)cp1[-1]);
296                 kfree(in_buff);
297                 spk_unlock(flags);
298                 return -EINVAL;
299         }
300         while (--i >= 0) {
301                 cp = spk_s2uchar(cp, cp1);
302                 cp1++;
303                 if (!(*cp))
304                         break;
305         }
306         if (i != 0 || cp1[-1] != KEY_MAP_VER || cp1[-2] != 0) {
307                 ret = -EINVAL;
308                 pr_warn("end %d %d %d %d\n", i,
309                                 (int)cp1[-3], (int)cp1[-2], (int)cp1[-1]);
310         } else {
311                 if (spk_set_key_info(in_buff, spk_key_buf)) {
312                         spk_set_key_info(spk_key_defaults, spk_key_buf);
313                         ret = -EINVAL;
314                         pr_warn("set key failed\n");
315                 }
316         }
317         kfree(in_buff);
318         spk_unlock(flags);
319         return ret;
320 }
321
322 /*
323  * This is called when a user changes the value of the silent parameter.
324  */
325 static ssize_t silent_store(struct kobject *kobj, struct kobj_attribute *attr,
326         const char *buf, size_t count)
327 {
328         int len;
329         struct vc_data *vc = vc_cons[fg_console].d;
330         char ch = 0;
331         char shut;
332         unsigned long flags;
333
334         len = strlen(buf);
335         if (len > 0 && len < 3) {
336                 ch = buf[0];
337                 if (ch == '\n')
338                         ch = '0';
339         }
340         if (ch < '0' || ch > '7') {
341                 pr_warn("silent value '%c' not in range (0,7)\n", ch);
342                 return -EINVAL;
343         }
344         spk_lock(flags);
345         if (ch&2) {
346                 shut = 1;
347                 spk_do_flush();
348         } else {
349                 shut = 0;
350         }
351         if (ch&4)
352                 shut |= 0x40;
353         if (ch&1)
354                 spk_shut_up |= shut;
355         else
356                 spk_shut_up &= ~shut;
357         spk_unlock(flags);
358         return count;
359 }
360
361 /*
362  * This is called when a user reads the synth setting.
363  */
364 static ssize_t synth_show(struct kobject *kobj, struct kobj_attribute *attr,
365         char *buf)
366 {
367         int rv;
368
369         if (synth == NULL)
370                 rv = sprintf(buf, "%s\n", "none");
371         else
372                 rv = sprintf(buf, "%s\n", synth->name);
373         return rv;
374 }
375
376 /*
377  * This is called when a user requests to change synthesizers.
378  */
379 static ssize_t synth_store(struct kobject *kobj, struct kobj_attribute *attr,
380         const char *buf, size_t count)
381 {
382         int len;
383         char new_synth_name[10];
384
385         len = strlen(buf);
386         if (len < 2 || len > 9)
387                 return -EINVAL;
388         strncpy(new_synth_name, buf, len);
389         if (new_synth_name[len - 1] == '\n')
390                 len--;
391         new_synth_name[len] = '\0';
392         spk_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);
397                 return -ENODEV;
398         }
399         return count;
400 }
401
402 /*
403  * This is called when text is sent to the synth via the synth_direct file.
404  */
405 static ssize_t synth_direct_store(struct kobject *kobj,
406         struct kobj_attribute *attr, const char *buf, size_t count)
407 {
408         u_char tmp[256];
409         int len;
410         int bytes;
411         const char *ptr = buf;
412
413         if (!synth)
414                 return -EPERM;
415
416         len = strlen(buf);
417         while (len > 0) {
418                 bytes = min_t(size_t, len, 250);
419                 strncpy(tmp, ptr, bytes);
420                 tmp[bytes] = '\0';
421                 string_unescape_any_inplace(tmp);
422                 synth_printf("%s", tmp);
423                 ptr += bytes;
424                 len -= bytes;
425         }
426         return count;
427 }
428
429 /*
430  * This function is called when a user reads the version.
431  */
432 static ssize_t version_show(struct kobject *kobj, struct kobj_attribute *attr,
433         char *buf)
434 {
435         char *cp;
436
437         cp = buf;
438         cp += sprintf(cp, "Speakup version %s\n", SPEAKUP_VERSION);
439         if (synth)
440                 cp += sprintf(cp, "%s synthesizer driver version %s\n",
441                 synth->name, synth->version);
442         return cp - buf;
443 }
444
445 /*
446  * This is called when a user reads the punctuation settings.
447  */
448 static ssize_t punc_show(struct kobject *kobj, struct kobj_attribute *attr,
449         char *buf)
450 {
451         int i;
452         char *cp = buf;
453         struct st_var_header *p_header;
454         struct punc_var_t *var;
455         struct st_bits_data *pb;
456         short mask;
457         unsigned long flags;
458
459         p_header = spk_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",
462                         attr->attr.name);
463                 return -EINVAL;
464         }
465
466         var = spk_get_punc_var(p_header->var_id);
467         if (var == NULL) {
468                 pr_warn("var is null, p_header->var_id is %i\n",
469                                 p_header->var_id);
470                 return -EINVAL;
471         }
472
473         spk_lock(flags);
474         pb = (struct st_bits_data *) &spk_punc_info[var->value];
475         mask = pb->mask;
476         for (i = 33; i < 128; i++) {
477                 if (!(spk_chartab[i]&mask))
478                         continue;
479                 *cp++ = (char)i;
480         }
481         spk_unlock(flags);
482         return cp-buf;
483 }
484
485 /*
486  * This is called when a user changes the punctuation settings.
487  */
488 static ssize_t punc_store(struct kobject *kobj, struct kobj_attribute *attr,
489                          const char *buf, size_t count)
490 {
491         int x;
492         struct st_var_header *p_header;
493         struct punc_var_t *var;
494         char punc_buf[100];
495         unsigned long flags;
496
497         x = strlen(buf);
498         if (x < 1 || x > 99)
499                 return -EINVAL;
500
501         p_header = spk_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",
504                         attr->attr.name);
505                 return -EINVAL;
506         }
507
508         var = spk_get_punc_var(p_header->var_id);
509         if (var == NULL) {
510                 pr_warn("var is null, p_header->var_id is %i\n",
511                                 p_header->var_id);
512                 return -EINVAL;
513         }
514
515         strncpy(punc_buf, buf, x);
516
517         while (x && punc_buf[x - 1] == '\n')
518                 x--;
519         punc_buf[x] = '\0';
520
521         spk_lock(flags);
522
523         if (*punc_buf == 'd' || *punc_buf == 'r')
524                 x = spk_set_mask_bits(0, var->value, 3);
525         else
526                 x = spk_set_mask_bits(punc_buf, var->value, 3);
527
528         spk_unlock(flags);
529         return count;
530 }
531
532 /*
533  * This function is called when a user reads one of the variable parameters.
534  */
535 ssize_t spk_var_show(struct kobject *kobj, struct kobj_attribute *attr,
536         char *buf)
537 {
538         int rv = 0;
539         struct st_var_header *param;
540         struct var_t *var;
541                 char *cp1;
542         char *cp;
543         char ch;
544         unsigned long flags;
545
546         param = spk_var_header_by_name(attr->attr.name);
547         if (param == NULL)
548                 return -EINVAL;
549
550         spk_lock(flags);
551         var = (struct var_t *) param->data;
552         switch (param->var_type) {
553         case VAR_NUM:
554         case VAR_TIME:
555                 if (var)
556                         rv = sprintf(buf, "%i\n", var->u.n.value);
557                 else
558                         rv = sprintf(buf, "0\n");
559                 break;
560         case VAR_STRING:
561                 if (var) {
562                         cp1 = buf;
563                         *cp1++ = '"';
564                         for (cp = (char *)param->p_val; (ch = *cp); cp++) {
565                                 if (ch >= ' ' && ch < '~')
566                                         *cp1++ = ch;
567                                 else
568                                         cp1 += sprintf(cp1, "\\""x%02x", ch);
569                         }
570                         *cp1++ = '"';
571                         *cp1++ = '\n';
572                         *cp1 = '\0';
573                         rv = cp1-buf;
574                 } else {
575                         rv = sprintf(buf, "\"\"\n");
576                 }
577                 break;
578         default:
579                 rv = sprintf(buf, "Bad parameter  %s, type %i\n",
580                         param->name, param->var_type);
581                 break;
582         }
583         spk_unlock(flags);
584         return rv;
585 }
586 EXPORT_SYMBOL_GPL(spk_var_show);
587
588 /*
589  * This function is called when a user echos a value to one of the
590  * variable parameters.
591  */
592 ssize_t spk_var_store(struct kobject *kobj, struct kobj_attribute *attr,
593                          const char *buf, size_t count)
594 {
595         struct st_var_header *param;
596         int ret;
597         int len;
598         char *cp;
599         struct var_t *var_data;
600         int value;
601         unsigned long flags;
602
603         param = spk_var_header_by_name(attr->attr.name);
604         if (param == NULL)
605                 return -EINVAL;
606         if (param->data == NULL)
607                 return 0;
608         ret = 0;
609         cp = (char *)buf;
610         string_unescape_any_inplace(cp);
611
612         spk_lock(flags);
613         switch (param->var_type) {
614         case VAR_NUM:
615         case VAR_TIME:
616                 if (*cp == 'd' || *cp == 'r' || *cp == '\0')
617                         len = E_DEFAULT;
618                 else if (*cp == '+' || *cp == '-')
619                         len = E_INC;
620                 else
621                         len = E_SET;
622                 value = simple_strtol(cp, NULL, 10);
623                 ret = spk_set_num_var(value, param, len);
624                 if (ret == -ERANGE) {
625                         var_data = param->data;
626                         pr_warn("value for %s out of range, expect %d to %d\n",
627                                 attr->attr.name,
628                                 var_data->u.n.low, var_data->u.n.high);
629                 }
630                 break;
631         case VAR_STRING:
632                 len = strlen(buf);
633                 if ((len >= 1) && (buf[len - 1] == '\n'))
634                         --len;
635                 if ((len >= 2) && (buf[0] == '"') && (buf[len - 1] == '"')) {
636                         ++buf;
637                         len -= 2;
638                 }
639                 cp = (char *) buf;
640                 cp[len] = '\0';
641                 ret = spk_set_string_var(buf, param, len);
642                 if (ret == -E2BIG)
643                         pr_warn("value too long for %s\n",
644                                         attr->attr.name);
645                 break;
646         default:
647                 pr_warn("%s unknown type %d\n",
648                         param->name, (int)param->var_type);
649         break;
650         }
651         /*
652          * If voice was just changed, we might need to reset our default
653          * pitch and volume.
654          */
655         if (strcmp(attr->attr.name, "voice") == 0) {
656                 if (synth && synth->default_pitch) {
657                         param = spk_var_header_by_name("pitch");
658                         if (param)  {
659                                 spk_set_num_var(synth->default_pitch[value],
660                                                 param, E_NEW_DEFAULT);
661                                 spk_set_num_var(0, param, E_DEFAULT);
662                         }
663                 }
664                 if (synth && synth->default_vol) {
665                         param = spk_var_header_by_name("vol");
666                         if (param)  {
667                                 spk_set_num_var(synth->default_vol[value],
668                                                 param, E_NEW_DEFAULT);
669                                 spk_set_num_var(0, param, E_DEFAULT);
670                         }
671                 }
672         }
673         spk_unlock(flags);
674
675         if (ret == -ERESTART)
676                 pr_info("%s reset to default value\n", attr->attr.name);
677         return count;
678 }
679 EXPORT_SYMBOL_GPL(spk_var_store);
680
681 /*
682  * Functions for reading and writing lists of i18n messages.  Incomplete.
683  */
684
685 static ssize_t message_show_helper(char *buf, enum msg_index_t first,
686         enum msg_index_t last)
687 {
688         size_t bufsize = PAGE_SIZE;
689         char *buf_pointer = buf;
690         int printed;
691         enum msg_index_t cursor;
692         int index = 0;
693         *buf_pointer = '\0'; /* buf_pointer always looking at a NUL byte. */
694
695         for (cursor = first; cursor <= last; cursor++, index++) {
696                 if (bufsize <= 1)
697                         break;
698                 printed = scnprintf(buf_pointer, bufsize, "%d\t%s\n",
699                         index, spk_msg_get(cursor));
700                 buf_pointer += printed;
701                 bufsize -= printed;
702         }
703
704         return buf_pointer - buf;
705 }
706
707 static void report_msg_status(int reset, int received, int used,
708         int rejected, char *groupname)
709 {
710         int len;
711         char buf[160];
712
713         if (reset) {
714                 pr_info("i18n messages from group %s reset to defaults\n",
715                         groupname);
716         } else if (received) {
717                 len = snprintf(buf, sizeof(buf),
718                                " updated %d of %d i18n messages from group %s\n",
719                                        used, received, groupname);
720                 if (rejected)
721                         snprintf(buf + (len - 1), sizeof(buf) - (len - 1),
722                                  " with %d reject%s\n",
723                                  rejected, rejected > 1 ? "s" : "");
724                 printk(buf);
725         }
726 }
727
728 static ssize_t message_store_helper(const char *buf, size_t count,
729         struct msg_group_t *group)
730 {
731         char *cp = (char *) buf;
732         char *end = cp + count;
733         char *linefeed = NULL;
734         char *temp = NULL;
735         ssize_t msg_stored = 0;
736         ssize_t retval = count;
737         size_t desc_length = 0;
738         unsigned long index = 0;
739         int received = 0;
740         int used = 0;
741         int rejected = 0;
742         int reset = 0;
743         enum msg_index_t firstmessage = group->start;
744         enum msg_index_t lastmessage = group->end;
745         enum msg_index_t curmessage;
746
747         while (cp < end) {
748
749                 while ((cp < end) && (*cp == ' ' || *cp == '\t'))
750                         cp++;
751
752                 if (cp == end)
753                         break;
754                 if (strchr("dDrR", *cp)) {
755                         reset = 1;
756                         break;
757                 }
758                 received++;
759
760                 linefeed = strchr(cp, '\n');
761                 if (!linefeed) {
762                         rejected++;
763                         break;
764                 }
765
766                 if (!isdigit(*cp)) {
767                         rejected++;
768                         cp = linefeed + 1;
769                         continue;
770                 }
771
772                 index = simple_strtoul(cp, &temp, 10);
773
774                 while ((temp < linefeed) && (*temp == ' ' || *temp == '\t'))
775                         temp++;
776
777                 desc_length = linefeed - temp;
778                 curmessage = firstmessage + index;
779
780                 /*
781                  * Note the check (curmessage < firstmessage).  It is not
782                  * redundant.  Suppose that the user gave us an index
783                  * equal to ULONG_MAX - 1.  If firstmessage > 1, then
784                  * firstmessage + index < firstmessage!
785                  */
786
787                 if ((curmessage < firstmessage) || (curmessage > lastmessage)) {
788                         rejected++;
789                         cp = linefeed + 1;
790                         continue;
791                 }
792
793                 msg_stored = spk_msg_set(curmessage, temp, desc_length);
794                 if (msg_stored < 0) {
795                         retval = msg_stored;
796                         if (msg_stored == -ENOMEM)
797                                 reset = 1;
798                         break;
799                 } else {
800                         used++;
801                 }
802
803                 cp = linefeed + 1;
804         }
805
806         if (reset)
807                 spk_reset_msg_group(group);
808
809         report_msg_status(reset, received, used, rejected, group->name);
810         return retval;
811 }
812
813 static ssize_t message_show(struct kobject *kobj,
814         struct kobj_attribute *attr, char *buf)
815 {
816         ssize_t retval = 0;
817         struct msg_group_t *group = spk_find_msg_group(attr->attr.name);
818         unsigned long flags;
819
820         BUG_ON(!group);
821         spk_lock(flags);
822         retval = message_show_helper(buf, group->start, group->end);
823         spk_unlock(flags);
824         return retval;
825 }
826
827 static ssize_t message_store(struct kobject *kobj, struct kobj_attribute *attr,
828         const char *buf, size_t count)
829 {
830         ssize_t retval = 0;
831         struct msg_group_t *group = spk_find_msg_group(attr->attr.name);
832
833         BUG_ON(!group);
834         retval = message_store_helper(buf, count, group);
835         return retval;
836 }
837
838 /*
839  * Declare the attributes.
840  */
841 static struct kobj_attribute keymap_attribute =
842         __ATTR(keymap, ROOT_W, keymap_show, keymap_store);
843 static struct kobj_attribute silent_attribute =
844         __ATTR(silent, USER_W, NULL, silent_store);
845 static struct kobj_attribute synth_attribute =
846         __ATTR(synth, USER_RW, synth_show, synth_store);
847 static struct kobj_attribute synth_direct_attribute =
848         __ATTR(synth_direct, USER_W, NULL, synth_direct_store);
849 static struct kobj_attribute version_attribute =
850         __ATTR_RO(version);
851
852 static struct kobj_attribute delimiters_attribute =
853         __ATTR(delimiters, USER_RW, punc_show, punc_store);
854 static struct kobj_attribute ex_num_attribute =
855         __ATTR(ex_num, USER_RW, punc_show, punc_store);
856 static struct kobj_attribute punc_all_attribute =
857         __ATTR(punc_all, USER_RW, punc_show, punc_store);
858 static struct kobj_attribute punc_most_attribute =
859         __ATTR(punc_most, USER_RW, punc_show, punc_store);
860 static struct kobj_attribute punc_some_attribute =
861         __ATTR(punc_some, USER_RW, punc_show, punc_store);
862 static struct kobj_attribute repeats_attribute =
863         __ATTR(repeats, USER_RW, punc_show, punc_store);
864
865 static struct kobj_attribute attrib_bleep_attribute =
866         __ATTR(attrib_bleep, USER_RW, spk_var_show, spk_var_store);
867 static struct kobj_attribute bell_pos_attribute =
868         __ATTR(bell_pos, USER_RW, spk_var_show, spk_var_store);
869 static struct kobj_attribute bleep_time_attribute =
870         __ATTR(bleep_time, USER_RW, spk_var_show, spk_var_store);
871 static struct kobj_attribute bleeps_attribute =
872         __ATTR(bleeps, USER_RW, spk_var_show, spk_var_store);
873 static struct kobj_attribute cursor_time_attribute =
874         __ATTR(cursor_time, USER_RW, spk_var_show, spk_var_store);
875 static struct kobj_attribute key_echo_attribute =
876         __ATTR(key_echo, USER_RW, spk_var_show, spk_var_store);
877 static struct kobj_attribute no_interrupt_attribute =
878         __ATTR(no_interrupt, USER_RW, spk_var_show, spk_var_store);
879 static struct kobj_attribute punc_level_attribute =
880         __ATTR(punc_level, USER_RW, spk_var_show, spk_var_store);
881 static struct kobj_attribute reading_punc_attribute =
882         __ATTR(reading_punc, USER_RW, spk_var_show, spk_var_store);
883 static struct kobj_attribute say_control_attribute =
884         __ATTR(say_control, USER_RW, spk_var_show, spk_var_store);
885 static struct kobj_attribute say_word_ctl_attribute =
886         __ATTR(say_word_ctl, USER_RW, spk_var_show, spk_var_store);
887 static struct kobj_attribute spell_delay_attribute =
888         __ATTR(spell_delay, USER_RW, spk_var_show, spk_var_store);
889
890 /*
891  * These attributes are i18n related.
892  */
893 static struct kobj_attribute announcements_attribute =
894         __ATTR(announcements, USER_RW, message_show, message_store);
895 static struct kobj_attribute characters_attribute =
896         __ATTR(characters, USER_RW, chars_chartab_show, chars_chartab_store);
897 static struct kobj_attribute chartab_attribute =
898         __ATTR(chartab, USER_RW, chars_chartab_show, chars_chartab_store);
899 static struct kobj_attribute ctl_keys_attribute =
900         __ATTR(ctl_keys, USER_RW, message_show, message_store);
901 static struct kobj_attribute colors_attribute =
902         __ATTR(colors, USER_RW, message_show, message_store);
903 static struct kobj_attribute formatted_attribute =
904         __ATTR(formatted, USER_RW, message_show, message_store);
905 static struct kobj_attribute function_names_attribute =
906         __ATTR(function_names, USER_RW, message_show, message_store);
907 static struct kobj_attribute key_names_attribute =
908         __ATTR(key_names, USER_RW, message_show, message_store);
909 static struct kobj_attribute states_attribute =
910         __ATTR(states, USER_RW, message_show, message_store);
911
912 /*
913  * Create groups of attributes so that we can create and destroy them all
914  * at once.
915  */
916 static struct attribute *main_attrs[] = {
917         &keymap_attribute.attr,
918         &silent_attribute.attr,
919         &synth_attribute.attr,
920         &synth_direct_attribute.attr,
921         &version_attribute.attr,
922         &delimiters_attribute.attr,
923         &ex_num_attribute.attr,
924         &punc_all_attribute.attr,
925         &punc_most_attribute.attr,
926         &punc_some_attribute.attr,
927         &repeats_attribute.attr,
928         &attrib_bleep_attribute.attr,
929         &bell_pos_attribute.attr,
930         &bleep_time_attribute.attr,
931         &bleeps_attribute.attr,
932         &cursor_time_attribute.attr,
933         &key_echo_attribute.attr,
934         &no_interrupt_attribute.attr,
935         &punc_level_attribute.attr,
936         &reading_punc_attribute.attr,
937         &say_control_attribute.attr,
938         &say_word_ctl_attribute.attr,
939         &spell_delay_attribute.attr,
940         NULL,
941 };
942
943 static struct attribute *i18n_attrs[] = {
944         &announcements_attribute.attr,
945         &characters_attribute.attr,
946         &chartab_attribute.attr,
947         &ctl_keys_attribute.attr,
948         &colors_attribute.attr,
949         &formatted_attribute.attr,
950         &function_names_attribute.attr,
951         &key_names_attribute.attr,
952         &states_attribute.attr,
953         NULL,
954 };
955
956 /*
957  * An unnamed attribute group will put all of the attributes directly in
958  * the kobject directory.  If we specify a name, a subdirectory will be
959  * created for the attributes with the directory being the name of the
960  * attribute group.
961  */
962 static struct attribute_group main_attr_group = {
963         .attrs = main_attrs,
964 };
965
966 static struct attribute_group i18n_attr_group = {
967         .attrs = i18n_attrs,
968         .name = "i18n",
969 };
970
971 static struct kobject *accessibility_kobj;
972 struct kobject *speakup_kobj;
973
974 int speakup_kobj_init(void)
975 {
976         int retval;
977
978         /*
979          * Create a simple kobject with the name of "accessibility",
980          * located under /sys/
981          *
982          * As this is a simple directory, no uevent will be sent to
983          * userspace.  That is why this function should not be used for
984          * any type of dynamic kobjects, where the name and number are
985          * not known ahead of time.
986          */
987         accessibility_kobj = kobject_create_and_add("accessibility", NULL);
988         if (!accessibility_kobj) {
989                 retval = -ENOMEM;
990                 goto out;
991         }
992
993         speakup_kobj = kobject_create_and_add("speakup", accessibility_kobj);
994         if (!speakup_kobj) {
995                 retval = -ENOMEM;
996                 goto err_acc;
997         }
998
999         /* Create the files associated with this kobject */
1000         retval = sysfs_create_group(speakup_kobj, &main_attr_group);
1001         if (retval)
1002                 goto err_speakup;
1003
1004         retval = sysfs_create_group(speakup_kobj, &i18n_attr_group);
1005         if (retval)
1006                 goto err_group;
1007
1008         goto out;
1009
1010 err_group:
1011         sysfs_remove_group(speakup_kobj, &main_attr_group);
1012 err_speakup:
1013         kobject_put(speakup_kobj);
1014 err_acc:
1015         kobject_put(accessibility_kobj);
1016 out:
1017         return retval;
1018 }
1019
1020 void speakup_kobj_exit(void)
1021 {
1022         sysfs_remove_group(speakup_kobj, &i18n_attr_group);
1023         sysfs_remove_group(speakup_kobj, &main_attr_group);
1024         kobject_put(speakup_kobj);
1025         kobject_put(accessibility_kobj);
1026 }