kobjects.c 26 KB

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