kobjects.c 25.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
 * Speakup kobject implementation
 *
 * Copyright (C) 2009 William Hubbs
 *
 * This code is based on kobject-example.c, which came with linux 2.6.x.
 *
 * Copyright (C) 2004-2007 Greg Kroah-Hartman <greg@kroah.com>
 * Copyright (C) 2007 Novell Inc.
 *
 * Released under the GPL version 2 only.
 *
 */
#include <linux/slab.h>		/* For kmalloc. */
#include <linux/kernel.h>
#include <linux/kobject.h>
#include <linux/string.h>
18
#include <linux/string_helpers.h>
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <linux/sysfs.h>
#include <linux/ctype.h>

#include "speakup.h"
#include "spk_priv.h"

/*
 * This is called when a user reads the characters or chartab sys file.
 */
static ssize_t chars_chartab_show(struct kobject *kobj,
	struct kobj_attribute *attr, char *buf)
{
	int i;
	int len = 0;
	char *cp;
	char *buf_pointer = buf;
	size_t bufsize = PAGE_SIZE;
	unsigned long flags;

38
	spin_lock_irqsave(&speakup_info.spinlock, flags);
39
40
41
42
43
44
	*buf_pointer = '\0';
	for (i = 0; i < 256; i++) {
		if (bufsize <= 1)
			break;
		if (strcmp("characters", attr->attr.name) == 0) {
			len = scnprintf(buf_pointer, bufsize, "%d\t%s\n",
45
					i, spk_characters[i]);
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
		} else {	/* show chartab entry */
			if (IS_TYPE(i, B_CTL))
				cp = "B_CTL";
			else if (IS_TYPE(i, WDLM))
				cp = "WDLM";
			else if (IS_TYPE(i, A_PUNC))
				cp = "A_PUNC";
			else if (IS_TYPE(i, PUNC))
				cp = "PUNC";
			else if (IS_TYPE(i, NUM))
				cp = "NUM";
			else if (IS_TYPE(i, A_CAP))
				cp = "A_CAP";
			else if (IS_TYPE(i, ALPHA))
				cp = "ALPHA";
			else if (IS_TYPE(i, B_CAPSYM))
				cp = "B_CAPSYM";
			else if (IS_TYPE(i, B_SYM))
				cp = "B_SYM";
			else
				cp = "0";
			len =
			    scnprintf(buf_pointer, bufsize, "%d\t%s\n", i, cp);
		}
		bufsize -= len;
		buf_pointer += len;
	}
73
	spin_unlock_irqrestore(&speakup_info.spinlock, flags);
74
75
76
77
78
79
80
81
82
83
	return buf_pointer - buf;
}

/*
 * Print informational messages or warnings after updating
 * character descriptions or chartab entries.
 */
static void report_char_chartab_status(int reset, int received, int used,
	int rejected, int do_characters)
{
84
	static char const *object_type[] = {
85
86
87
88
89
90
91
92
		"character class entries",
		"character descriptions",
	};
	int len;
	char buf[80];

	if (reset) {
		pr_info("%s reset to defaults\n", object_type[do_characters]);
93
	} else if (received) {
94
		len = snprintf(buf, sizeof(buf),
95
96
				" updated %d of %d %s\n",
				used, received, object_type[do_characters]);
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
		if (rejected)
			snprintf(buf + (len - 1), sizeof(buf) - (len - 1),
				 " with %d reject%s\n",
				 rejected, rejected > 1 ? "s" : "");
		printk(buf);
	}
}

/*
 * This is called when a user changes the characters or chartab parameters.
 */
static ssize_t chars_chartab_store(struct kobject *kobj,
	struct kobj_attribute *attr, const char *buf, size_t count)
{
	char *cp = (char *) buf;
	char *end = cp + count; /* the null at the end of the buffer */
	char *linefeed = NULL;
	char keyword[MAX_DESC_LEN + 1];
	char *outptr = NULL;	/* Will hold keyword or desc. */
	char *temp = NULL;
	char *desc = NULL;
	ssize_t retval = count;
	unsigned long flags;
	unsigned long index = 0;
	int charclass = 0;
	int received = 0;
	int used = 0;
	int rejected = 0;
	int reset = 0;
	int do_characters = !strcmp(attr->attr.name, "characters");
	size_t desc_length = 0;
	int i;

130
	spin_lock_irqsave(&speakup_info.spinlock, flags);
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
	while (cp < end) {

		while ((cp < end) && (*cp == ' ' || *cp == '\t'))
			cp++;

		if (cp == end)
			break;
		if ((*cp == '\n') || strchr("dDrR", *cp)) {
			reset = 1;
			break;
		}
		received++;

		linefeed = strchr(cp, '\n');
		if (!linefeed) {
			rejected++;
			break;
		}

150
		if (!isdigit(*cp)) {
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
			rejected++;
			cp = linefeed + 1;
			continue;
		}

		index = simple_strtoul(cp, &temp, 10);
		if (index > 255) {
			rejected++;
			cp = linefeed + 1;
			continue;
		}

		while ((temp < linefeed) && (*temp == ' ' || *temp == '\t'))
			temp++;

		desc_length = linefeed - temp;
		if (desc_length > MAX_DESC_LEN) {
			rejected++;
			cp = linefeed + 1;
			continue;
		}
		if (do_characters) {
			desc = kmalloc(desc_length + 1, GFP_ATOMIC);
174
			if (!desc) {
175
176
177
178
179
180
181
182
183
184
185
186
187
188
				retval = -ENOMEM;
				reset = 1;	/* just reset on error. */
				break;
			}
			outptr = desc;
		} else {
			outptr = keyword;
		}

		for (i = 0; i < desc_length; i++)
			outptr[i] = temp[i];
		outptr[desc_length] = '\0';

		if (do_characters) {
189
190
191
			if (spk_characters[index] != spk_default_chars[index])
				kfree(spk_characters[index]);
			spk_characters[index] = desc;
192
193
			used++;
		} else {
194
			charclass = spk_chartab_get_value(keyword);
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
			if (charclass == 0) {
				rejected++;
				cp = linefeed + 1;
				continue;
			}
			if (charclass != spk_chartab[index]) {
				spk_chartab[index] = charclass;
				used++;
			}
		}
		cp = linefeed + 1;
	}

	if (reset) {
		if (do_characters)
210
			spk_reset_default_chars();
211
		else
212
			spk_reset_default_chartab();
213
214
	}

215
	spin_unlock_irqrestore(&speakup_info.spinlock, flags);
216
217
	report_char_chartab_status(reset, received, used, rejected,
		do_characters);
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
	return retval;
}

/*
 * This is called when a user reads the keymap parameter.
 */
static ssize_t keymap_show(struct kobject *kobj, struct kobj_attribute *attr,
	char *buf)
{
	char *cp = buf;
	int i;
	int n;
	int num_keys;
	int nstates;
	u_char *cp1;
	u_char ch;
	unsigned long flags;
235

236
	spin_lock_irqsave(&speakup_info.spinlock, flags);
237
	cp1 = spk_key_buf + SHIFT_TBL_SIZE;
238
239
240
241
	num_keys = (int)(*cp1);
	nstates = (int)cp1[1];
	cp += sprintf(cp, "%d, %d, %d,\n", KEY_MAP_VER, num_keys, nstates);
	cp1 += 2; /* now pointing at shift states */
242
	/* dump num_keys+1 as first row is shift states + flags,
243
244
	 * each subsequent row is key + states
	 */
245
246
247
248
249
250
251
252
	for (n = 0; n <= num_keys; n++) {
		for (i = 0; i <= nstates; i++) {
			ch = *cp1++;
			cp += sprintf(cp, "%d,", (int)ch);
			*cp++ = (i < nstates) ? SPACE : '\n';
		}
	}
	cp += sprintf(cp, "0, %d\n", KEY_MAP_VER);
253
	spin_unlock_irqrestore(&speakup_info.spinlock, flags);
254
	return (int)(cp - buf);
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
}

/*
 * This is called when a user changes the keymap parameter.
 */
static ssize_t keymap_store(struct kobject *kobj, struct kobj_attribute *attr,
	const char *buf, size_t count)
{
	int i;
	ssize_t ret = count;
	char *in_buff = NULL;
	char *cp;
	u_char *cp1;
	unsigned long flags;

270
	spin_lock_irqsave(&speakup_info.spinlock, flags);
271
	in_buff = kmemdup(buf, count + 1, GFP_ATOMIC);
272
	if (!in_buff) {
273
		spin_unlock_irqrestore(&speakup_info.spinlock, flags);
274
275
276
		return -ENOMEM;
	}
	if (strchr("dDrR", *in_buff)) {
277
		spk_set_key_info(spk_key_defaults, spk_key_buf);
278
279
		pr_info("keymap set to default values\n");
		kfree(in_buff);
280
		spin_unlock_irqrestore(&speakup_info.spinlock, flags);
281
282
283
		return count;
	}
	if (in_buff[count - 1] == '\n')
284
		in_buff[count - 1] = '\0';
285
286
287
	cp = in_buff;
	cp1 = (u_char *)in_buff;
	for (i = 0; i < 3; i++) {
288
		cp = spk_s2uchar(cp, cp1);
289
290
		cp1++;
	}
291
292
	i = (int)cp1[-2] + 1;
	i *= (int)cp1[-1] + 1;
293
294
	i += 2; /* 0 and last map ver */
	if (cp1[-3] != KEY_MAP_VER || cp1[-1] > 10 ||
295
			i+SHIFT_TBL_SIZE+4 >= sizeof(spk_key_buf)) {
296
297
298
		pr_warn("i %d %d %d %d\n", i,
				(int)cp1[-3], (int)cp1[-2], (int)cp1[-1]);
		kfree(in_buff);
299
		spin_unlock_irqrestore(&speakup_info.spinlock, flags);
300
301
302
		return -EINVAL;
	}
	while (--i >= 0) {
303
		cp = spk_s2uchar(cp, cp1);
304
305
306
307
308
309
310
311
312
		cp1++;
		if (!(*cp))
			break;
	}
	if (i != 0 || cp1[-1] != KEY_MAP_VER || cp1[-2] != 0) {
		ret = -EINVAL;
		pr_warn("end %d %d %d %d\n", i,
				(int)cp1[-3], (int)cp1[-2], (int)cp1[-1]);
	} else {
313
314
		if (spk_set_key_info(in_buff, spk_key_buf)) {
			spk_set_key_info(spk_key_defaults, spk_key_buf);
315
316
317
318
319
			ret = -EINVAL;
			pr_warn("set key failed\n");
		}
	}
	kfree(in_buff);
320
	spin_unlock_irqrestore(&speakup_info.spinlock, flags);
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
	return ret;
}

/*
 * This is called when a user changes the value of the silent parameter.
 */
static ssize_t silent_store(struct kobject *kobj, struct kobj_attribute *attr,
	const char *buf, size_t count)
{
	int len;
	struct vc_data *vc = vc_cons[fg_console].d;
	char ch = 0;
	char shut;
	unsigned long flags;

	len = strlen(buf);
337
	if (len > 0 && len < 3) {
338
339
340
341
342
343
344
345
		ch = buf[0];
		if (ch == '\n')
			ch = '0';
	}
	if (ch < '0' || ch > '7') {
		pr_warn("silent value '%c' not in range (0,7)\n", ch);
		return -EINVAL;
	}
346
	spin_lock_irqsave(&speakup_info.spinlock, flags);
347
348
	if (ch&2) {
		shut = 1;
349
		spk_do_flush();
350
351
352
	} else {
		shut = 0;
	}
353
	if (ch & 4)
354
		shut |= 0x40;
355
	if (ch & 1)
356
357
358
		spk_shut_up |= shut;
	else
		spk_shut_up &= ~shut;
359
	spin_unlock_irqrestore(&speakup_info.spinlock, flags);
360
361
362
363
364
365
366
367
368
369
370
	return count;
}

/*
 * This is called when a user reads the synth setting.
 */
static ssize_t synth_show(struct kobject *kobj, struct kobj_attribute *attr,
	char *buf)
{
	int rv;

371
	if (!synth)
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
		rv = sprintf(buf, "%s\n", "none");
	else
		rv = sprintf(buf, "%s\n", synth->name);
	return rv;
}

/*
 * This is called when a user requests to change synthesizers.
 */
static ssize_t synth_store(struct kobject *kobj, struct kobj_attribute *attr,
	const char *buf, size_t count)
{
	int len;
	char new_synth_name[10];

	len = strlen(buf);
	if (len < 2 || len > 9)
		return -EINVAL;
	strncpy(new_synth_name, buf, len);
	if (new_synth_name[len - 1] == '\n')
		len--;
	new_synth_name[len] = '\0';
394
	spk_strlwr(new_synth_name);
395
396
397
398
399
400
401
402
403
404
405
406
	if ((synth != NULL) && (!strcmp(new_synth_name, synth->name))) {
		pr_warn("%s already in use\n", new_synth_name);
	} else if (synth_init(new_synth_name) != 0) {
		pr_warn("failed to init synth %s\n", new_synth_name);
		return -ENODEV;
	}
	return count;
}

/*
 * This is called when text is sent to the synth via the synth_direct file.
 */
407
408
static ssize_t synth_direct_store(struct kobject *kobj,
	struct kobj_attribute *attr, const char *buf, size_t count)
409
410
411
412
413
{
	u_char tmp[256];
	int len;
	int bytes;
	const char *ptr = buf;
414
	unsigned long flags;
415

416
	if (!synth)
417
418
419
		return -EPERM;

	len = strlen(buf);
420
	spin_lock_irqsave(&speakup_info.spinlock, flags);
421
422
423
424
	while (len > 0) {
		bytes = min_t(size_t, len, 250);
		strncpy(tmp, ptr, bytes);
		tmp[bytes] = '\0';
425
		string_unescape_any_inplace(tmp);
426
427
428
429
		synth_printf("%s", tmp);
		ptr += bytes;
		len -= bytes;
	}
430
	spin_unlock_irqrestore(&speakup_info.spinlock, flags);
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
	return count;
}

/*
 * This function is called when a user reads the version.
 */
static ssize_t version_show(struct kobject *kobj, struct kobj_attribute *attr,
	char *buf)
{
	char *cp;

	cp = buf;
	cp += sprintf(cp, "Speakup version %s\n", SPEAKUP_VERSION);
	if (synth)
		cp += sprintf(cp, "%s synthesizer driver version %s\n",
		synth->name, synth->version);
	return cp - buf;
}

/*
 * This is called when a user reads the punctuation settings.
 */
static ssize_t punc_show(struct kobject *kobj, struct kobj_attribute *attr,
	char *buf)
{
	int i;
	char *cp = buf;
	struct st_var_header *p_header;
	struct punc_var_t *var;
	struct st_bits_data *pb;
	short mask;
	unsigned long flags;

464
	p_header = spk_var_header_by_name(attr->attr.name);
465
	if (!p_header) {
466
467
		pr_warn("p_header is null, attr->attr.name is %s\n",
			attr->attr.name);
468
469
470
		return -EINVAL;
	}

471
	var = spk_get_punc_var(p_header->var_id);
472
	if (!var) {
473
474
475
476
477
		pr_warn("var is null, p_header->var_id is %i\n",
				p_header->var_id);
		return -EINVAL;
	}

478
	spin_lock_irqsave(&speakup_info.spinlock, flags);
479
	pb = (struct st_bits_data *) &spk_punc_info[var->value];
480
481
482
483
484
485
	mask = pb->mask;
	for (i = 33; i < 128; i++) {
		if (!(spk_chartab[i]&mask))
			continue;
		*cp++ = (char)i;
	}
486
	spin_unlock_irqrestore(&speakup_info.spinlock, flags);
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
	return cp-buf;
}

/*
 * This is called when a user changes the punctuation settings.
 */
static ssize_t punc_store(struct kobject *kobj, struct kobj_attribute *attr,
			 const char *buf, size_t count)
{
	int x;
	struct st_var_header *p_header;
	struct punc_var_t *var;
	char punc_buf[100];
	unsigned long flags;

	x = strlen(buf);
	if (x < 1 || x > 99)
		return -EINVAL;

506
	p_header = spk_var_header_by_name(attr->attr.name);
507
	if (!p_header) {
508
509
		pr_warn("p_header is null, attr->attr.name is %s\n",
			attr->attr.name);
510
511
512
		return -EINVAL;
	}

513
	var = spk_get_punc_var(p_header->var_id);
514
	if (!var) {
515
516
517
518
519
520
521
522
523
524
525
		pr_warn("var is null, p_header->var_id is %i\n",
				p_header->var_id);
		return -EINVAL;
	}

	strncpy(punc_buf, buf, x);

	while (x && punc_buf[x - 1] == '\n')
		x--;
	punc_buf[x] = '\0';

526
	spin_lock_irqsave(&speakup_info.spinlock, flags);
527
528

	if (*punc_buf == 'd' || *punc_buf == 'r')
529
		x = spk_set_mask_bits(NULL, var->value, 3);
530
	else
531
		x = spk_set_mask_bits(punc_buf, var->value, 3);
532

533
	spin_unlock_irqrestore(&speakup_info.spinlock, flags);
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
	return count;
}

/*
 * This function is called when a user reads one of the variable parameters.
 */
ssize_t spk_var_show(struct kobject *kobj, struct kobj_attribute *attr,
	char *buf)
{
	int rv = 0;
	struct st_var_header *param;
	struct var_t *var;
		char *cp1;
	char *cp;
	char ch;
	unsigned long flags;

551
	param = spk_var_header_by_name(attr->attr.name);
552
	if (!param)
553
554
		return -EINVAL;

555
	spin_lock_irqsave(&speakup_info.spinlock, flags);
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
	var = (struct var_t *) param->data;
	switch (param->var_type) {
	case VAR_NUM:
	case VAR_TIME:
		if (var)
			rv = sprintf(buf, "%i\n", var->u.n.value);
		else
			rv = sprintf(buf, "0\n");
		break;
	case VAR_STRING:
		if (var) {
			cp1 = buf;
			*cp1++ = '"';
			for (cp = (char *)param->p_val; (ch = *cp); cp++) {
				if (ch >= ' ' && ch < '~')
					*cp1++ = ch;
				else
573
					cp1 += sprintf(cp1, "\\x%02x", ch);
574
575
576
577
578
579
580
581
582
583
584
585
586
587
			}
			*cp1++ = '"';
			*cp1++ = '\n';
			*cp1 = '\0';
			rv = cp1-buf;
		} else {
			rv = sprintf(buf, "\"\"\n");
		}
		break;
	default:
		rv = sprintf(buf, "Bad parameter  %s, type %i\n",
			param->name, param->var_type);
		break;
	}
588
	spin_unlock_irqrestore(&speakup_info.spinlock, flags);
589
590
591
592
	return rv;
}
EXPORT_SYMBOL_GPL(spk_var_show);

593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
/*
 * Used to reset either default_pitch or default_vol.
 */
static inline void spk_reset_default_value(char *header_name,
					int *synth_default_value, int idx)
{
	struct st_var_header *param;

	if (synth && synth_default_value) {
		param = spk_var_header_by_name(header_name);
		if (param)  {
			spk_set_num_var(synth_default_value[idx],
					param, E_NEW_DEFAULT);
			spk_set_num_var(0, param, E_DEFAULT);
			pr_info("%s reset to default value\n", param->name);
		}
	}
}

612
613
614
615
616
617
618
619
620
621
622
623
/*
 * This function is called when a user echos a value to one of the
 * variable parameters.
 */
ssize_t spk_var_store(struct kobject *kobj, struct kobj_attribute *attr,
			 const char *buf, size_t count)
{
	struct st_var_header *param;
	int ret;
	int len;
	char *cp;
	struct var_t *var_data;
624
	long value;
625
626
	unsigned long flags;

627
	param = spk_var_header_by_name(attr->attr.name);
628
	if (!param)
629
		return -EINVAL;
630
	if (!param->data)
631
632
		return 0;
	ret = 0;
633
634
	cp = (char *)buf;
	string_unescape_any_inplace(cp);
635

636
	spin_lock_irqsave(&speakup_info.spinlock, flags);
637
638
639
640
641
642
643
644
645
	switch (param->var_type) {
	case VAR_NUM:
	case VAR_TIME:
		if (*cp == 'd' || *cp == 'r' || *cp == '\0')
			len = E_DEFAULT;
		else if (*cp == '+' || *cp == '-')
			len = E_INC;
		else
			len = E_SET;
646
647
648
		if (kstrtol(cp, 10, &value) == 0)
			ret = spk_set_num_var(value, param, len);
		else
649
			pr_warn("overflow or parsing error has occurred");
650
		if (ret == -ERANGE) {
651
652
			var_data = param->data;
			pr_warn("value for %s out of range, expect %d to %d\n",
653
				param->name,
654
655
				var_data->u.n.low, var_data->u.n.high);
		}
656
657
658
659
660

	       /*
		* If voice was just changed, we might need to reset our default
		* pitch and volume.
		*/
661
662
663
664
		if (param->var_id == VOICE && synth &&
		    (ret == 0 || ret == -ERESTART)) {
			var_data = param->data;
			value = var_data->u.n.value;
665
666
667
668
669
			spk_reset_default_value("pitch", synth->default_pitch,
				value);
			spk_reset_default_value("vol", synth->default_vol,
				value);
		}
670
671
		break;
	case VAR_STRING:
672
673
		len = strlen(cp);
		if ((len >= 1) && (cp[len - 1] == '\n'))
674
			--len;
675
676
		if ((len >= 2) && (cp[0] == '"') && (cp[len - 1] == '"')) {
			++cp;
677
678
679
			len -= 2;
		}
		cp[len] = '\0';
680
		ret = spk_set_string_var(cp, param, len);
681
		if (ret == -E2BIG)
682
			pr_warn("value too long for %s\n",
683
					param->name);
684
685
686
687
688
689
		break;
	default:
		pr_warn("%s unknown type %d\n",
			param->name, (int)param->var_type);
	break;
	}
690
	spin_unlock_irqrestore(&speakup_info.spinlock, flags);
691

692
	if (ret == -ERESTART)
693
		pr_info("%s reset to default value\n", param->name);
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
	return count;
}
EXPORT_SYMBOL_GPL(spk_var_store);

/*
 * Functions for reading and writing lists of i18n messages.  Incomplete.
 */

static ssize_t message_show_helper(char *buf, enum msg_index_t first,
	enum msg_index_t last)
{
	size_t bufsize = PAGE_SIZE;
	char *buf_pointer = buf;
	int printed;
	enum msg_index_t cursor;
	int index = 0;
	*buf_pointer = '\0'; /* buf_pointer always looking at a NUL byte. */

	for (cursor = first; cursor <= last; cursor++, index++) {
		if (bufsize <= 1)
			break;
		printed = scnprintf(buf_pointer, bufsize, "%d\t%s\n",
716
			index, spk_msg_get(cursor));
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
		buf_pointer += printed;
		bufsize -= printed;
	}

	return buf_pointer - buf;
}

static void report_msg_status(int reset, int received, int used,
	int rejected, char *groupname)
{
	int len;
	char buf[160];

	if (reset) {
		pr_info("i18n messages from group %s reset to defaults\n",
			groupname);
733
	} else if (received) {
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
		len = snprintf(buf, sizeof(buf),
			       " updated %d of %d i18n messages from group %s\n",
				       used, received, groupname);
		if (rejected)
			snprintf(buf + (len - 1), sizeof(buf) - (len - 1),
				 " with %d reject%s\n",
				 rejected, rejected > 1 ? "s" : "");
		printk(buf);
	}
}

static ssize_t message_store_helper(const char *buf, size_t count,
	struct msg_group_t *group)
{
	char *cp = (char *) buf;
	char *end = cp + count;
	char *linefeed = NULL;
	char *temp = NULL;
	ssize_t msg_stored = 0;
	ssize_t retval = count;
	size_t desc_length = 0;
	unsigned long index = 0;
	int received = 0;
	int used = 0;
	int rejected = 0;
	int reset = 0;
	enum msg_index_t firstmessage = group->start;
	enum msg_index_t lastmessage = group->end;
	enum msg_index_t curmessage;

	while (cp < end) {

		while ((cp < end) && (*cp == ' ' || *cp == '\t'))
			cp++;

		if (cp == end)
			break;
		if (strchr("dDrR", *cp)) {
			reset = 1;
			break;
		}
		received++;

		linefeed = strchr(cp, '\n');
		if (!linefeed) {
			rejected++;
			break;
		}

783
		if (!isdigit(*cp)) {
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
			rejected++;
			cp = linefeed + 1;
			continue;
		}

		index = simple_strtoul(cp, &temp, 10);

		while ((temp < linefeed) && (*temp == ' ' || *temp == '\t'))
			temp++;

		desc_length = linefeed - temp;
		curmessage = firstmessage + index;

		/*
		 * Note the check (curmessage < firstmessage).  It is not
		 * redundant.  Suppose that the user gave us an index
		 * equal to ULONG_MAX - 1.  If firstmessage > 1, then
		 * firstmessage + index < firstmessage!
		 */

		if ((curmessage < firstmessage) || (curmessage > lastmessage)) {
			rejected++;
			cp = linefeed + 1;
			continue;
		}

810
		msg_stored = spk_msg_set(curmessage, temp, desc_length);
811
812
813
814
815
816
817
		if (msg_stored < 0) {
			retval = msg_stored;
			if (msg_stored == -ENOMEM)
				reset = 1;
			break;
		}

818
819
		used++;

820
821
822
823
		cp = linefeed + 1;
	}

	if (reset)
824
		spk_reset_msg_group(group);
825
826
827
828
829
830
831
832
833

	report_msg_status(reset, received, used, rejected, group->name);
	return retval;
}

static ssize_t message_show(struct kobject *kobj,
	struct kobj_attribute *attr, char *buf)
{
	ssize_t retval = 0;
834
	struct msg_group_t *group = spk_find_msg_group(attr->attr.name);
835
836
	unsigned long flags;

837
	BUG_ON(!group);
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);
841
842
843
844
845
846
	return retval;
}

static ssize_t message_store(struct kobject *kobj, struct kobj_attribute *attr,
	const char *buf, size_t count)
{
847
	struct msg_group_t *group = spk_find_msg_group(attr->attr.name);
848

849
	BUG_ON(!group);
850
	return message_store_helper(buf, count, group);
851
852
853
854
855
856
}

/*
 * Declare the attributes.
 */
static struct kobj_attribute keymap_attribute =
857
	__ATTR_RW(keymap);
858
static struct kobj_attribute silent_attribute =
859
	__ATTR_WO(silent);
860
static struct kobj_attribute synth_attribute =
861
	__ATTR_RW(synth);
862
static struct kobj_attribute synth_direct_attribute =
863
	__ATTR_WO(synth_direct);
864
865
866
867
static struct kobj_attribute version_attribute =
	__ATTR_RO(version);

static struct kobj_attribute delimiters_attribute =
868
	__ATTR(delimiters, S_IWUSR | S_IRUGO, punc_show, punc_store);
869
static struct kobj_attribute ex_num_attribute =
870
	__ATTR(ex_num, S_IWUSR | S_IRUGO, punc_show, punc_store);
871
static struct kobj_attribute punc_all_attribute =
872
	__ATTR(punc_all, S_IWUSR | S_IRUGO, punc_show, punc_store);
873
static struct kobj_attribute punc_most_attribute =
874
	__ATTR(punc_most, S_IWUSR | S_IRUGO, punc_show, punc_store);
875
static struct kobj_attribute punc_some_attribute =
876
	__ATTR(punc_some, S_IWUSR | S_IRUGO, punc_show, punc_store);
877
static struct kobj_attribute repeats_attribute =
878
	__ATTR(repeats, S_IWUSR | S_IRUGO, punc_show, punc_store);
879
880

static struct kobj_attribute attrib_bleep_attribute =
881
	__ATTR(attrib_bleep, S_IWUSR | S_IRUGO, spk_var_show, spk_var_store);
882
static struct kobj_attribute bell_pos_attribute =
883
	__ATTR(bell_pos, S_IWUSR | S_IRUGO, spk_var_show, spk_var_store);
884
static struct kobj_attribute bleep_time_attribute =
885
	__ATTR(bleep_time, S_IWUSR | S_IRUGO, spk_var_show, spk_var_store);
886
static struct kobj_attribute bleeps_attribute =
887
	__ATTR(bleeps, S_IWUSR | S_IRUGO, spk_var_show, spk_var_store);
888
static struct kobj_attribute cursor_time_attribute =
889
	__ATTR(cursor_time, S_IWUSR | S_IRUGO, spk_var_show, spk_var_store);
890
static struct kobj_attribute key_echo_attribute =
891
	__ATTR(key_echo, S_IWUSR | S_IRUGO, spk_var_show, spk_var_store);
892
static struct kobj_attribute no_interrupt_attribute =
893
	__ATTR(no_interrupt, S_IWUSR | S_IRUGO, spk_var_show, spk_var_store);
894
static struct kobj_attribute punc_level_attribute =
895
	__ATTR(punc_level, S_IWUSR | S_IRUGO, spk_var_show, spk_var_store);
896
static struct kobj_attribute reading_punc_attribute =
897
	__ATTR(reading_punc, S_IWUSR | S_IRUGO, spk_var_show, spk_var_store);
898
static struct kobj_attribute say_control_attribute =
899
	__ATTR(say_control, S_IWUSR | S_IRUGO, spk_var_show, spk_var_store);
900
static struct kobj_attribute say_word_ctl_attribute =
901
	__ATTR(say_word_ctl, S_IWUSR | S_IRUGO, spk_var_show, spk_var_store);
902
static struct kobj_attribute spell_delay_attribute =
903
	__ATTR(spell_delay, S_IWUSR | S_IRUGO, spk_var_show, spk_var_store);
904
905
906
907
908

/*
 * These attributes are i18n related.
 */
static struct kobj_attribute announcements_attribute =
909
	__ATTR(announcements, S_IWUSR | S_IRUGO, message_show, message_store);
910
static struct kobj_attribute characters_attribute =
911
	__ATTR(characters, S_IWUSR | S_IRUGO, chars_chartab_show,
912
	       chars_chartab_store);
913
static struct kobj_attribute chartab_attribute =
914
	__ATTR(chartab, S_IWUSR | S_IRUGO, chars_chartab_show,
915
	       chars_chartab_store);
916
static struct kobj_attribute ctl_keys_attribute =
917
	__ATTR(ctl_keys, S_IWUSR | S_IRUGO, message_show, message_store);
918
static struct kobj_attribute colors_attribute =
919
	__ATTR(colors, S_IWUSR | S_IRUGO, message_show, message_store);
920
static struct kobj_attribute formatted_attribute =
921
	__ATTR(formatted, S_IWUSR | S_IRUGO, message_show, message_store);
922
static struct kobj_attribute function_names_attribute =
923
	__ATTR(function_names, S_IWUSR | S_IRUGO, message_show, message_store);
924
static struct kobj_attribute key_names_attribute =
925
	__ATTR(key_names, S_IWUSR | S_IRUGO, message_show, message_store);
926
static struct kobj_attribute states_attribute =
927
	__ATTR(states, S_IWUSR | S_IRUGO, message_show, message_store);
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004

/*
 * Create groups of attributes so that we can create and destroy them all
 * at once.
 */
static struct attribute *main_attrs[] = {
	&keymap_attribute.attr,
	&silent_attribute.attr,
	&synth_attribute.attr,
	&synth_direct_attribute.attr,
	&version_attribute.attr,
	&delimiters_attribute.attr,
	&ex_num_attribute.attr,
	&punc_all_attribute.attr,
	&punc_most_attribute.attr,
	&punc_some_attribute.attr,
	&repeats_attribute.attr,
	&attrib_bleep_attribute.attr,
	&bell_pos_attribute.attr,
	&bleep_time_attribute.attr,
	&bleeps_attribute.attr,
	&cursor_time_attribute.attr,
	&key_echo_attribute.attr,
	&no_interrupt_attribute.attr,
	&punc_level_attribute.attr,
	&reading_punc_attribute.attr,
	&say_control_attribute.attr,
	&say_word_ctl_attribute.attr,
	&spell_delay_attribute.attr,
	NULL,
};

static struct attribute *i18n_attrs[] = {
	&announcements_attribute.attr,
	&characters_attribute.attr,
	&chartab_attribute.attr,
	&ctl_keys_attribute.attr,
	&colors_attribute.attr,
	&formatted_attribute.attr,
	&function_names_attribute.attr,
	&key_names_attribute.attr,
	&states_attribute.attr,
	NULL,
};

/*
 * An unnamed attribute group will put all of the attributes directly in
 * the kobject directory.  If we specify a name, a subdirectory will be
 * created for the attributes with the directory being the name of the
 * attribute group.
 */
static struct attribute_group main_attr_group = {
	.attrs = main_attrs,
};

static struct attribute_group i18n_attr_group = {
	.attrs = i18n_attrs,
	.name = "i18n",
};

static struct kobject *accessibility_kobj;
struct kobject *speakup_kobj;

int speakup_kobj_init(void)
{
	int retval;

	/*
	 * Create a simple kobject with the name of "accessibility",
	 * located under /sys/
	 *
	 * As this is a simple directory, no uevent will be sent to
	 * userspace.  That is why this function should not be used for
	 * any type of dynamic kobjects, where the name and number are
	 * not known ahead of time.
	 */
	accessibility_kobj = kobject_create_and_add("accessibility", NULL);
1005
1006
1007
1008
	if (!accessibility_kobj) {
		retval = -ENOMEM;
		goto out;
	}
1009
1010
1011

	speakup_kobj = kobject_create_and_add("speakup", accessibility_kobj);
	if (!speakup_kobj) {
1012
1013
		retval = -ENOMEM;
		goto err_acc;
1014
1015
1016
1017
1018
	}

	/* Create the files associated with this kobject */
	retval = sysfs_create_group(speakup_kobj, &main_attr_group);
	if (retval)
1019
		goto err_speakup;
1020
1021
1022

	retval = sysfs_create_group(speakup_kobj, &i18n_attr_group);
	if (retval)
1023
1024
		goto err_group;

1025
	goto out;
1026

1027
1028
1029
1030
1031
1032
err_group:
	sysfs_remove_group(speakup_kobj, &main_attr_group);
err_speakup:
	kobject_put(speakup_kobj);
err_acc:
	kobject_put(accessibility_kobj);
1033
out:
1034
1035
1036
1037
1038
	return retval;
}

void speakup_kobj_exit(void)
{
1039
1040
	sysfs_remove_group(speakup_kobj, &i18n_attr_group);
	sysfs_remove_group(speakup_kobj, &main_attr_group);
1041
1042
1043
	kobject_put(speakup_kobj);
	kobject_put(accessibility_kobj);
}