/drivers/staging/speakup/kobjects.c

https://bitbucket.org/cyanogenmod/android_kernel_asus_tf300t · C · 1025 lines · 922 code · 48 blank · 55 comment · 133 complexity · 0a0e6af27ebea0f95c002cb9ee761020 MD5 · raw file

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