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 <[email protected]>
  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. u_char tmp[256];
  389. int len;
  390. int bytes;
  391. const char *ptr = buf;
  392. unsigned long flags;
  393. if (!synth)
  394. return -EPERM;
  395. len = strlen(buf);
  396. spin_lock_irqsave(&speakup_info.spinlock, flags);
  397. while (len > 0) {
  398. bytes = min_t(size_t, len, 250);
  399. strncpy(tmp, ptr, bytes);
  400. tmp[bytes] = '\0';
  401. string_unescape_any_inplace(tmp);
  402. synth_printf("%s", tmp);
  403. ptr += bytes;
  404. len -= bytes;
  405. }
  406. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  407. return count;
  408. }
  409. /*
  410. * This function is called when a user reads the version.
  411. */
  412. static ssize_t version_show(struct kobject *kobj, struct kobj_attribute *attr,
  413. char *buf)
  414. {
  415. char *cp;
  416. cp = buf;
  417. cp += sprintf(cp, "Speakup version %s\n", SPEAKUP_VERSION);
  418. if (synth)
  419. cp += sprintf(cp, "%s synthesizer driver version %s\n",
  420. synth->name, synth->version);
  421. return cp - buf;
  422. }
  423. /*
  424. * This is called when a user reads the punctuation settings.
  425. */
  426. static ssize_t punc_show(struct kobject *kobj, struct kobj_attribute *attr,
  427. char *buf)
  428. {
  429. int i;
  430. char *cp = buf;
  431. struct st_var_header *p_header;
  432. struct punc_var_t *var;
  433. struct st_bits_data *pb;
  434. short mask;
  435. unsigned long flags;
  436. p_header = spk_var_header_by_name(attr->attr.name);
  437. if (!p_header) {
  438. pr_warn("p_header is null, attr->attr.name is %s\n",
  439. attr->attr.name);
  440. return -EINVAL;
  441. }
  442. var = spk_get_punc_var(p_header->var_id);
  443. if (!var) {
  444. pr_warn("var is null, p_header->var_id is %i\n",
  445. p_header->var_id);
  446. return -EINVAL;
  447. }
  448. spin_lock_irqsave(&speakup_info.spinlock, flags);
  449. pb = (struct st_bits_data *)&spk_punc_info[var->value];
  450. mask = pb->mask;
  451. for (i = 33; i < 128; i++) {
  452. if (!(spk_chartab[i] & mask))
  453. continue;
  454. *cp++ = (char)i;
  455. }
  456. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  457. return cp - buf;
  458. }
  459. /*
  460. * This is called when a user changes the punctuation settings.
  461. */
  462. static ssize_t punc_store(struct kobject *kobj, struct kobj_attribute *attr,
  463. const char *buf, size_t count)
  464. {
  465. int x;
  466. struct st_var_header *p_header;
  467. struct punc_var_t *var;
  468. char punc_buf[100];
  469. unsigned long flags;
  470. x = strlen(buf);
  471. if (x < 1 || x > 99)
  472. return -EINVAL;
  473. p_header = spk_var_header_by_name(attr->attr.name);
  474. if (!p_header) {
  475. pr_warn("p_header is null, attr->attr.name is %s\n",
  476. attr->attr.name);
  477. return -EINVAL;
  478. }
  479. var = spk_get_punc_var(p_header->var_id);
  480. if (!var) {
  481. pr_warn("var is null, p_header->var_id is %i\n",
  482. p_header->var_id);
  483. return -EINVAL;
  484. }
  485. memcpy(punc_buf, buf, x);
  486. while (x && punc_buf[x - 1] == '\n')
  487. x--;
  488. punc_buf[x] = '\0';
  489. spin_lock_irqsave(&speakup_info.spinlock, flags);
  490. if (*punc_buf == 'd' || *punc_buf == 'r')
  491. x = spk_set_mask_bits(NULL, var->value, 3);
  492. else
  493. x = spk_set_mask_bits(punc_buf, var->value, 3);
  494. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  495. return count;
  496. }
  497. /*
  498. * This function is called when a user reads one of the variable parameters.
  499. */
  500. ssize_t spk_var_show(struct kobject *kobj, struct kobj_attribute *attr,
  501. char *buf)
  502. {
  503. int rv = 0;
  504. struct st_var_header *param;
  505. struct var_t *var;
  506. char *cp1;
  507. char *cp;
  508. char ch;
  509. unsigned long flags;
  510. param = spk_var_header_by_name(attr->attr.name);
  511. if (!param)
  512. return -EINVAL;
  513. spin_lock_irqsave(&speakup_info.spinlock, flags);
  514. var = (struct var_t *)param->data;
  515. switch (param->var_type) {
  516. case VAR_NUM:
  517. case VAR_TIME:
  518. if (var)
  519. rv = sprintf(buf, "%i\n", var->u.n.value);
  520. else
  521. rv = sprintf(buf, "0\n");
  522. break;
  523. case VAR_STRING:
  524. if (var) {
  525. cp1 = buf;
  526. *cp1++ = '"';
  527. for (cp = (char *)param->p_val; (ch = *cp); cp++) {
  528. if (ch >= ' ' && ch < '~')
  529. *cp1++ = ch;
  530. else
  531. cp1 += sprintf(cp1, "\\x%02x", ch);
  532. }
  533. *cp1++ = '"';
  534. *cp1++ = '\n';
  535. *cp1 = '\0';
  536. rv = cp1 - buf;
  537. } else {
  538. rv = sprintf(buf, "\"\"\n");
  539. }
  540. break;
  541. default:
  542. rv = sprintf(buf, "Bad parameter %s, type %i\n",
  543. param->name, param->var_type);
  544. break;
  545. }
  546. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  547. return rv;
  548. }
  549. EXPORT_SYMBOL_GPL(spk_var_show);
  550. /*
  551. * Used to reset either default_pitch or default_vol.
  552. */
  553. static inline void spk_reset_default_value(char *header_name,
  554. int *synth_default_value, int idx)
  555. {
  556. struct st_var_header *param;
  557. if (synth && synth_default_value) {
  558. param = spk_var_header_by_name(header_name);
  559. if (param) {
  560. spk_set_num_var(synth_default_value[idx],
  561. param, E_NEW_DEFAULT);
  562. spk_set_num_var(0, param, E_DEFAULT);
  563. pr_info("%s reset to default value\n", param->name);
  564. }
  565. }
  566. }
  567. /*
  568. * This function is called when a user echos a value to one of the
  569. * variable parameters.
  570. */
  571. ssize_t spk_var_store(struct kobject *kobj, struct kobj_attribute *attr,
  572. const char *buf, size_t count)
  573. {
  574. struct st_var_header *param;
  575. int ret;
  576. int len;
  577. char *cp;
  578. struct var_t *var_data;
  579. long value;
  580. unsigned long flags;
  581. param = spk_var_header_by_name(attr->attr.name);
  582. if (!param)
  583. return -EINVAL;
  584. if (!param->data)
  585. return 0;
  586. ret = 0;
  587. cp = (char *)buf;
  588. string_unescape_any_inplace(cp);
  589. spin_lock_irqsave(&speakup_info.spinlock, flags);
  590. switch (param->var_type) {
  591. case VAR_NUM:
  592. case VAR_TIME:
  593. if (*cp == 'd' || *cp == 'r' || *cp == '\0')
  594. len = E_DEFAULT;
  595. else if (*cp == '+' || *cp == '-')
  596. len = E_INC;
  597. else
  598. len = E_SET;
  599. if (kstrtol(cp, 10, &value) == 0)
  600. ret = spk_set_num_var(value, param, len);
  601. else
  602. pr_warn("overflow or parsing error has occurred");
  603. if (ret == -ERANGE) {
  604. var_data = param->data;
  605. pr_warn("value for %s out of range, expect %d to %d\n",
  606. param->name,
  607. var_data->u.n.low, var_data->u.n.high);
  608. }
  609. /*
  610. * If voice was just changed, we might need to reset our default
  611. * pitch and volume.
  612. */
  613. if (param->var_id == VOICE && synth &&
  614. (ret == 0 || ret == -ERESTART)) {
  615. var_data = param->data;
  616. value = var_data->u.n.value;
  617. spk_reset_default_value("pitch", synth->default_pitch,
  618. value);
  619. spk_reset_default_value("vol", synth->default_vol,
  620. value);
  621. }
  622. break;
  623. case VAR_STRING:
  624. len = strlen(cp);
  625. if ((len >= 1) && (cp[len - 1] == '\n'))
  626. --len;
  627. if ((len >= 2) && (cp[0] == '"') && (cp[len - 1] == '"')) {
  628. ++cp;
  629. len -= 2;
  630. }
  631. cp[len] = '\0';
  632. ret = spk_set_string_var(cp, param, len);
  633. if (ret == -E2BIG)
  634. pr_warn("value too long for %s\n",
  635. param->name);
  636. break;
  637. default:
  638. pr_warn("%s unknown type %d\n",
  639. param->name, (int)param->var_type);
  640. break;
  641. }
  642. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  643. if (ret == -ERESTART)
  644. pr_info("%s reset to default value\n", param->name);
  645. return count;
  646. }
  647. EXPORT_SYMBOL_GPL(spk_var_store);
  648. /*
  649. * Functions for reading and writing lists of i18n messages. Incomplete.
  650. */
  651. static ssize_t message_show_helper(char *buf, enum msg_index_t first,
  652. enum msg_index_t last)
  653. {
  654. size_t bufsize = PAGE_SIZE;
  655. char *buf_pointer = buf;
  656. int printed;
  657. enum msg_index_t cursor;
  658. int index = 0;
  659. *buf_pointer = '\0'; /* buf_pointer always looking at a NUL byte. */
  660. for (cursor = first; cursor <= last; cursor++, index++) {
  661. if (bufsize <= 1)
  662. break;
  663. printed = scnprintf(buf_pointer, bufsize, "%d\t%s\n",
  664. index, spk_msg_get(cursor));
  665. buf_pointer += printed;
  666. bufsize -= printed;
  667. }
  668. return buf_pointer - buf;
  669. }
  670. static void report_msg_status(int reset, int received, int used,
  671. int rejected, char *groupname)
  672. {
  673. int len;
  674. char buf[160];
  675. if (reset) {
  676. pr_info("i18n messages from group %s reset to defaults\n",
  677. groupname);
  678. } else if (received) {
  679. len = snprintf(buf, sizeof(buf),
  680. " updated %d of %d i18n messages from group %s\n",
  681. used, received, groupname);
  682. if (rejected)
  683. snprintf(buf + (len - 1), sizeof(buf) - (len - 1),
  684. " with %d reject%s\n",
  685. rejected, rejected > 1 ? "s" : "");
  686. pr_info("%s", buf);
  687. }
  688. }
  689. static ssize_t message_store_helper(const char *buf, size_t count,
  690. struct msg_group_t *group)
  691. {
  692. char *cp = (char *)buf;
  693. char *end = cp + count;
  694. char *linefeed = NULL;
  695. char *temp = NULL;
  696. ssize_t msg_stored = 0;
  697. ssize_t retval = count;
  698. size_t desc_length = 0;
  699. unsigned long index = 0;
  700. int received = 0;
  701. int used = 0;
  702. int rejected = 0;
  703. int reset = 0;
  704. enum msg_index_t firstmessage = group->start;
  705. enum msg_index_t lastmessage = group->end;
  706. enum msg_index_t curmessage;
  707. while (cp < end) {
  708. while ((cp < end) && (*cp == ' ' || *cp == '\t'))
  709. cp++;
  710. if (cp == end)
  711. break;
  712. if (strchr("dDrR", *cp)) {
  713. reset = 1;
  714. break;
  715. }
  716. received++;
  717. linefeed = strchr(cp, '\n');
  718. if (!linefeed) {
  719. rejected++;
  720. break;
  721. }
  722. if (!isdigit(*cp)) {
  723. rejected++;
  724. cp = linefeed + 1;
  725. continue;
  726. }
  727. /*
  728. * Do not replace with kstrtoul:
  729. * here we need temp to be updated
  730. */
  731. index = simple_strtoul(cp, &temp, 10);
  732. while ((temp < linefeed) && (*temp == ' ' || *temp == '\t'))
  733. temp++;
  734. desc_length = linefeed - temp;
  735. curmessage = firstmessage + index;
  736. /*
  737. * Note the check (curmessage < firstmessage). It is not
  738. * redundant. Suppose that the user gave us an index
  739. * equal to ULONG_MAX - 1. If firstmessage > 1, then
  740. * firstmessage + index < firstmessage!
  741. */
  742. if ((curmessage < firstmessage) || (curmessage > lastmessage)) {
  743. rejected++;
  744. cp = linefeed + 1;
  745. continue;
  746. }
  747. msg_stored = spk_msg_set(curmessage, temp, desc_length);
  748. if (msg_stored < 0) {
  749. retval = msg_stored;
  750. if (msg_stored == -ENOMEM)
  751. reset = 1;
  752. break;
  753. }
  754. used++;
  755. cp = linefeed + 1;
  756. }
  757. if (reset)
  758. spk_reset_msg_group(group);
  759. report_msg_status(reset, received, used, rejected, group->name);
  760. return retval;
  761. }
  762. static ssize_t message_show(struct kobject *kobj,
  763. struct kobj_attribute *attr, char *buf)
  764. {
  765. ssize_t retval = 0;
  766. struct msg_group_t *group = spk_find_msg_group(attr->attr.name);
  767. unsigned long flags;
  768. if (WARN_ON(!group))
  769. return -EINVAL;
  770. spin_lock_irqsave(&speakup_info.spinlock, flags);
  771. retval = message_show_helper(buf, group->start, group->end);
  772. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  773. return retval;
  774. }
  775. static ssize_t message_store(struct kobject *kobj, struct kobj_attribute *attr,
  776. const char *buf, size_t count)
  777. {
  778. struct msg_group_t *group = spk_find_msg_group(attr->attr.name);
  779. if (WARN_ON(!group))
  780. return -EINVAL;
  781. return message_store_helper(buf, count, group);
  782. }
  783. /*
  784. * Declare the attributes.
  785. */
  786. static struct kobj_attribute keymap_attribute =
  787. __ATTR_RW(keymap);
  788. static struct kobj_attribute silent_attribute =
  789. __ATTR_WO(silent);
  790. static struct kobj_attribute synth_attribute =
  791. __ATTR_RW(synth);
  792. static struct kobj_attribute synth_direct_attribute =
  793. __ATTR_WO(synth_direct);
  794. static struct kobj_attribute version_attribute =
  795. __ATTR_RO(version);
  796. static struct kobj_attribute delimiters_attribute =
  797. __ATTR(delimiters, 0644, punc_show, punc_store);
  798. static struct kobj_attribute ex_num_attribute =
  799. __ATTR(ex_num, 0644, punc_show, punc_store);
  800. static struct kobj_attribute punc_all_attribute =
  801. __ATTR(punc_all, 0644, punc_show, punc_store);
  802. static struct kobj_attribute punc_most_attribute =
  803. __ATTR(punc_most, 0644, punc_show, punc_store);
  804. static struct kobj_attribute punc_some_attribute =
  805. __ATTR(punc_some, 0644, punc_show, punc_store);
  806. static struct kobj_attribute repeats_attribute =
  807. __ATTR(repeats, 0644, punc_show, punc_store);
  808. static struct kobj_attribute attrib_bleep_attribute =
  809. __ATTR(attrib_bleep, 0644, spk_var_show, spk_var_store);
  810. static struct kobj_attribute bell_pos_attribute =
  811. __ATTR(bell_pos, 0644, spk_var_show, spk_var_store);
  812. static struct kobj_attribute bleep_time_attribute =
  813. __ATTR(bleep_time, 0644, spk_var_show, spk_var_store);
  814. static struct kobj_attribute bleeps_attribute =
  815. __ATTR(bleeps, 0644, spk_var_show, spk_var_store);
  816. static struct kobj_attribute cursor_time_attribute =
  817. __ATTR(cursor_time, 0644, spk_var_show, spk_var_store);
  818. static struct kobj_attribute key_echo_attribute =
  819. __ATTR(key_echo, 0644, spk_var_show, spk_var_store);
  820. static struct kobj_attribute no_interrupt_attribute =
  821. __ATTR(no_interrupt, 0644, spk_var_show, spk_var_store);
  822. static struct kobj_attribute punc_level_attribute =
  823. __ATTR(punc_level, 0644, spk_var_show, spk_var_store);
  824. static struct kobj_attribute reading_punc_attribute =
  825. __ATTR(reading_punc, 0644, spk_var_show, spk_var_store);
  826. static struct kobj_attribute say_control_attribute =
  827. __ATTR(say_control, 0644, spk_var_show, spk_var_store);
  828. static struct kobj_attribute say_word_ctl_attribute =
  829. __ATTR(say_word_ctl, 0644, spk_var_show, spk_var_store);
  830. static struct kobj_attribute spell_delay_attribute =
  831. __ATTR(spell_delay, 0644, spk_var_show, spk_var_store);
  832. /*
  833. * These attributes are i18n related.
  834. */
  835. static struct kobj_attribute announcements_attribute =
  836. __ATTR(announcements, 0644, message_show, message_store);
  837. static struct kobj_attribute characters_attribute =
  838. __ATTR(characters, 0644, chars_chartab_show,
  839. chars_chartab_store);
  840. static struct kobj_attribute chartab_attribute =
  841. __ATTR(chartab, 0644, chars_chartab_show,
  842. chars_chartab_store);
  843. static struct kobj_attribute ctl_keys_attribute =
  844. __ATTR(ctl_keys, 0644, message_show, message_store);
  845. static struct kobj_attribute colors_attribute =
  846. __ATTR(colors, 0644, message_show, message_store);
  847. static struct kobj_attribute formatted_attribute =
  848. __ATTR(formatted, 0644, message_show, message_store);
  849. static struct kobj_attribute function_names_attribute =
  850. __ATTR(function_names, 0644, message_show, message_store);
  851. static struct kobj_attribute key_names_attribute =
  852. __ATTR(key_names, 0644, message_show, message_store);
  853. static struct kobj_attribute states_attribute =
  854. __ATTR(states, 0644, message_show, message_store);
  855. /*
  856. * Create groups of attributes so that we can create and destroy them all
  857. * at once.
  858. */
  859. static struct attribute *main_attrs[] = {
  860. &keymap_attribute.attr,
  861. &silent_attribute.attr,
  862. &synth_attribute.attr,
  863. &synth_direct_attribute.attr,
  864. &version_attribute.attr,
  865. &delimiters_attribute.attr,
  866. &ex_num_attribute.attr,
  867. &punc_all_attribute.attr,
  868. &punc_most_attribute.attr,
  869. &punc_some_attribute.attr,
  870. &repeats_attribute.attr,
  871. &attrib_bleep_attribute.attr,
  872. &bell_pos_attribute.attr,
  873. &bleep_time_attribute.attr,
  874. &bleeps_attribute.attr,
  875. &cursor_time_attribute.attr,
  876. &key_echo_attribute.attr,
  877. &no_interrupt_attribute.attr,
  878. &punc_level_attribute.attr,
  879. &reading_punc_attribute.attr,
  880. &say_control_attribute.attr,
  881. &say_word_ctl_attribute.attr,
  882. &spell_delay_attribute.attr,
  883. NULL,
  884. };
  885. static struct attribute *i18n_attrs[] = {
  886. &announcements_attribute.attr,
  887. &characters_attribute.attr,
  888. &chartab_attribute.attr,
  889. &ctl_keys_attribute.attr,
  890. &colors_attribute.attr,
  891. &formatted_attribute.attr,
  892. &function_names_attribute.attr,
  893. &key_names_attribute.attr,
  894. &states_attribute.attr,
  895. NULL,
  896. };
  897. /*
  898. * An unnamed attribute group will put all of the attributes directly in
  899. * the kobject directory. If we specify a name, a subdirectory will be
  900. * created for the attributes with the directory being the name of the
  901. * attribute group.
  902. */
  903. static const struct attribute_group main_attr_group = {
  904. .attrs = main_attrs,
  905. };
  906. static const struct attribute_group i18n_attr_group = {
  907. .attrs = i18n_attrs,
  908. .name = "i18n",
  909. };
  910. static struct kobject *accessibility_kobj;
  911. struct kobject *speakup_kobj;
  912. int speakup_kobj_init(void)
  913. {
  914. int retval;
  915. /*
  916. * Create a simple kobject with the name of "accessibility",
  917. * located under /sys/
  918. *
  919. * As this is a simple directory, no uevent will be sent to
  920. * userspace. That is why this function should not be used for
  921. * any type of dynamic kobjects, where the name and number are
  922. * not known ahead of time.
  923. */
  924. accessibility_kobj = kobject_create_and_add("accessibility", NULL);
  925. if (!accessibility_kobj) {
  926. retval = -ENOMEM;
  927. goto out;
  928. }
  929. speakup_kobj = kobject_create_and_add("speakup", accessibility_kobj);
  930. if (!speakup_kobj) {
  931. retval = -ENOMEM;
  932. goto err_acc;
  933. }
  934. /* Create the files associated with this kobject */
  935. retval = sysfs_create_group(speakup_kobj, &main_attr_group);
  936. if (retval)
  937. goto err_speakup;
  938. retval = sysfs_create_group(speakup_kobj, &i18n_attr_group);
  939. if (retval)
  940. goto err_group;
  941. goto out;
  942. err_group:
  943. sysfs_remove_group(speakup_kobj, &main_attr_group);
  944. err_speakup:
  945. kobject_put(speakup_kobj);
  946. err_acc:
  947. kobject_put(accessibility_kobj);
  948. out:
  949. return retval;
  950. }
  951. void speakup_kobj_exit(void)
  952. {
  953. sysfs_remove_group(speakup_kobj, &i18n_attr_group);
  954. sysfs_remove_group(speakup_kobj, &main_attr_group);
  955. kobject_put(speakup_kobj);
  956. kobject_put(accessibility_kobj);
  957. }