kobjects.c 25 KB

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