kobjects.c 25 KB

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