kobjects.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  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. static char const *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. */
  225. for (n = 0; n <= num_keys; n++) {
  226. for (i = 0; i <= nstates; i++) {
  227. ch = *cp1++;
  228. cp += sprintf(cp, "%d,", (int)ch);
  229. *cp++ = (i < nstates) ? SPACE : '\n';
  230. }
  231. }
  232. cp += sprintf(cp, "0, %d\n", KEY_MAP_VER);
  233. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  234. return (int)(cp-buf);
  235. }
  236. /*
  237. * This is called when a user changes the keymap parameter.
  238. */
  239. static ssize_t keymap_store(struct kobject *kobj, struct kobj_attribute *attr,
  240. const char *buf, size_t count)
  241. {
  242. int i;
  243. ssize_t ret = count;
  244. char *in_buff = NULL;
  245. char *cp;
  246. u_char *cp1;
  247. unsigned long flags;
  248. spin_lock_irqsave(&speakup_info.spinlock, flags);
  249. in_buff = kmemdup(buf, count + 1, GFP_ATOMIC);
  250. if (!in_buff) {
  251. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  252. return -ENOMEM;
  253. }
  254. if (strchr("dDrR", *in_buff)) {
  255. spk_set_key_info(spk_key_defaults, spk_key_buf);
  256. pr_info("keymap set to default values\n");
  257. kfree(in_buff);
  258. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  259. return count;
  260. }
  261. if (in_buff[count - 1] == '\n')
  262. in_buff[count - 1] = '\0';
  263. cp = in_buff;
  264. cp1 = (u_char *)in_buff;
  265. for (i = 0; i < 3; i++) {
  266. cp = spk_s2uchar(cp, cp1);
  267. cp1++;
  268. }
  269. i = (int)cp1[-2]+1;
  270. i *= (int)cp1[-1]+1;
  271. i += 2; /* 0 and last map ver */
  272. if (cp1[-3] != KEY_MAP_VER || cp1[-1] > 10 ||
  273. i+SHIFT_TBL_SIZE+4 >= sizeof(spk_key_buf)) {
  274. pr_warn("i %d %d %d %d\n", i,
  275. (int)cp1[-3], (int)cp1[-2], (int)cp1[-1]);
  276. kfree(in_buff);
  277. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  278. return -EINVAL;
  279. }
  280. while (--i >= 0) {
  281. cp = spk_s2uchar(cp, cp1);
  282. cp1++;
  283. if (!(*cp))
  284. break;
  285. }
  286. if (i != 0 || cp1[-1] != KEY_MAP_VER || cp1[-2] != 0) {
  287. ret = -EINVAL;
  288. pr_warn("end %d %d %d %d\n", i,
  289. (int)cp1[-3], (int)cp1[-2], (int)cp1[-1]);
  290. } else {
  291. if (spk_set_key_info(in_buff, spk_key_buf)) {
  292. spk_set_key_info(spk_key_defaults, spk_key_buf);
  293. ret = -EINVAL;
  294. pr_warn("set key failed\n");
  295. }
  296. }
  297. kfree(in_buff);
  298. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  299. return ret;
  300. }
  301. /*
  302. * This is called when a user changes the value of the silent parameter.
  303. */
  304. static ssize_t silent_store(struct kobject *kobj, struct kobj_attribute *attr,
  305. const char *buf, size_t count)
  306. {
  307. int len;
  308. struct vc_data *vc = vc_cons[fg_console].d;
  309. char ch = 0;
  310. char shut;
  311. unsigned long flags;
  312. len = strlen(buf);
  313. if (len > 0 && len < 3) {
  314. ch = buf[0];
  315. if (ch == '\n')
  316. ch = '0';
  317. }
  318. if (ch < '0' || ch > '7') {
  319. pr_warn("silent value '%c' not in range (0,7)\n", ch);
  320. return -EINVAL;
  321. }
  322. spin_lock_irqsave(&speakup_info.spinlock, flags);
  323. if (ch&2) {
  324. shut = 1;
  325. spk_do_flush();
  326. } else {
  327. shut = 0;
  328. }
  329. if (ch&4)
  330. shut |= 0x40;
  331. if (ch&1)
  332. spk_shut_up |= shut;
  333. else
  334. spk_shut_up &= ~shut;
  335. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  336. return count;
  337. }
  338. /*
  339. * This is called when a user reads the synth setting.
  340. */
  341. static ssize_t synth_show(struct kobject *kobj, struct kobj_attribute *attr,
  342. char *buf)
  343. {
  344. int rv;
  345. if (!synth)
  346. rv = sprintf(buf, "%s\n", "none");
  347. else
  348. rv = sprintf(buf, "%s\n", synth->name);
  349. return rv;
  350. }
  351. /*
  352. * This is called when a user requests to change synthesizers.
  353. */
  354. static ssize_t synth_store(struct kobject *kobj, struct kobj_attribute *attr,
  355. const char *buf, size_t count)
  356. {
  357. int len;
  358. char new_synth_name[10];
  359. len = strlen(buf);
  360. if (len < 2 || len > 9)
  361. return -EINVAL;
  362. memcpy(new_synth_name, buf, len);
  363. if (new_synth_name[len - 1] == '\n')
  364. len--;
  365. new_synth_name[len] = '\0';
  366. spk_strlwr(new_synth_name);
  367. if ((synth != NULL) && (!strcmp(new_synth_name, synth->name))) {
  368. pr_warn("%s already in use\n", new_synth_name);
  369. } else if (synth_init(new_synth_name) != 0) {
  370. pr_warn("failed to init synth %s\n", new_synth_name);
  371. return -ENODEV;
  372. }
  373. return count;
  374. }
  375. /*
  376. * This is called when text is sent to the synth via the synth_direct file.
  377. */
  378. static ssize_t synth_direct_store(struct kobject *kobj,
  379. struct kobj_attribute *attr, const char *buf, size_t count)
  380. {
  381. u_char tmp[256];
  382. int len;
  383. int bytes;
  384. const char *ptr = buf;
  385. if (!synth)
  386. return -EPERM;
  387. len = strlen(buf);
  388. while (len > 0) {
  389. bytes = min_t(size_t, len, 250);
  390. strncpy(tmp, ptr, bytes);
  391. tmp[bytes] = '\0';
  392. string_unescape_any_inplace(tmp);
  393. synth_printf("%s", tmp);
  394. ptr += bytes;
  395. len -= bytes;
  396. }
  397. return count;
  398. }
  399. /*
  400. * This function is called when a user reads the version.
  401. */
  402. static ssize_t version_show(struct kobject *kobj, struct kobj_attribute *attr,
  403. char *buf)
  404. {
  405. char *cp;
  406. cp = buf;
  407. cp += sprintf(cp, "Speakup version %s\n", SPEAKUP_VERSION);
  408. if (synth)
  409. cp += sprintf(cp, "%s synthesizer driver version %s\n",
  410. synth->name, synth->version);
  411. return cp - buf;
  412. }
  413. /*
  414. * This is called when a user reads the punctuation settings.
  415. */
  416. static ssize_t punc_show(struct kobject *kobj, struct kobj_attribute *attr,
  417. char *buf)
  418. {
  419. int i;
  420. char *cp = buf;
  421. struct st_var_header *p_header;
  422. struct punc_var_t *var;
  423. struct st_bits_data *pb;
  424. short mask;
  425. unsigned long flags;
  426. p_header = spk_var_header_by_name(attr->attr.name);
  427. if (!p_header) {
  428. pr_warn("p_header is null, attr->attr.name is %s\n",
  429. attr->attr.name);
  430. return -EINVAL;
  431. }
  432. var = spk_get_punc_var(p_header->var_id);
  433. if (!var) {
  434. pr_warn("var is null, p_header->var_id is %i\n",
  435. p_header->var_id);
  436. return -EINVAL;
  437. }
  438. spin_lock_irqsave(&speakup_info.spinlock, flags);
  439. pb = (struct st_bits_data *) &spk_punc_info[var->value];
  440. mask = pb->mask;
  441. for (i = 33; i < 128; i++) {
  442. if (!(spk_chartab[i]&mask))
  443. continue;
  444. *cp++ = (char)i;
  445. }
  446. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  447. return cp-buf;
  448. }
  449. /*
  450. * This is called when a user changes the punctuation settings.
  451. */
  452. static ssize_t punc_store(struct kobject *kobj, struct kobj_attribute *attr,
  453. const char *buf, size_t count)
  454. {
  455. int x;
  456. struct st_var_header *p_header;
  457. struct punc_var_t *var;
  458. char punc_buf[100];
  459. unsigned long flags;
  460. x = strlen(buf);
  461. if (x < 1 || x > 99)
  462. return -EINVAL;
  463. p_header = spk_var_header_by_name(attr->attr.name);
  464. if (!p_header) {
  465. pr_warn("p_header is null, attr->attr.name is %s\n",
  466. attr->attr.name);
  467. return -EINVAL;
  468. }
  469. var = spk_get_punc_var(p_header->var_id);
  470. if (!var) {
  471. pr_warn("var is null, p_header->var_id is %i\n",
  472. p_header->var_id);
  473. return -EINVAL;
  474. }
  475. memcpy(punc_buf, buf, x);
  476. while (x && punc_buf[x - 1] == '\n')
  477. x--;
  478. punc_buf[x] = '\0';
  479. spin_lock_irqsave(&speakup_info.spinlock, flags);
  480. if (*punc_buf == 'd' || *punc_buf == 'r')
  481. x = spk_set_mask_bits(NULL, var->value, 3);
  482. else
  483. x = spk_set_mask_bits(punc_buf, var->value, 3);
  484. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  485. return count;
  486. }
  487. /*
  488. * This function is called when a user reads one of the variable parameters.
  489. */
  490. ssize_t spk_var_show(struct kobject *kobj, struct kobj_attribute *attr,
  491. char *buf)
  492. {
  493. int rv = 0;
  494. struct st_var_header *param;
  495. struct var_t *var;
  496. char *cp1;
  497. char *cp;
  498. char ch;
  499. unsigned long flags;
  500. param = spk_var_header_by_name(attr->attr.name);
  501. if (!param)
  502. return -EINVAL;
  503. spin_lock_irqsave(&speakup_info.spinlock, flags);
  504. var = (struct var_t *) param->data;
  505. switch (param->var_type) {
  506. case VAR_NUM:
  507. case VAR_TIME:
  508. if (var)
  509. rv = sprintf(buf, "%i\n", var->u.n.value);
  510. else
  511. rv = sprintf(buf, "0\n");
  512. break;
  513. case VAR_STRING:
  514. if (var) {
  515. cp1 = buf;
  516. *cp1++ = '"';
  517. for (cp = (char *)param->p_val; (ch = *cp); cp++) {
  518. if (ch >= ' ' && ch < '~')
  519. *cp1++ = ch;
  520. else
  521. cp1 += sprintf(cp1, "\\x%02x", ch);
  522. }
  523. *cp1++ = '"';
  524. *cp1++ = '\n';
  525. *cp1 = '\0';
  526. rv = cp1-buf;
  527. } else {
  528. rv = sprintf(buf, "\"\"\n");
  529. }
  530. break;
  531. default:
  532. rv = sprintf(buf, "Bad parameter %s, type %i\n",
  533. param->name, param->var_type);
  534. break;
  535. }
  536. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  537. return rv;
  538. }
  539. EXPORT_SYMBOL_GPL(spk_var_show);
  540. /*
  541. * Used to reset either default_pitch or default_vol.
  542. */
  543. static inline void spk_reset_default_value(char *header_name,
  544. int *synth_default_value, int idx)
  545. {
  546. struct st_var_header *param;
  547. if (synth && synth_default_value) {
  548. param = spk_var_header_by_name(header_name);
  549. if (param) {
  550. spk_set_num_var(synth_default_value[idx],
  551. param, E_NEW_DEFAULT);
  552. spk_set_num_var(0, param, E_DEFAULT);
  553. pr_info("%s reset to default value\n", param->name);
  554. }
  555. }
  556. }
  557. /*
  558. * This function is called when a user echos a value to one of the
  559. * variable parameters.
  560. */
  561. ssize_t spk_var_store(struct kobject *kobj, struct kobj_attribute *attr,
  562. const char *buf, size_t count)
  563. {
  564. struct st_var_header *param;
  565. int ret;
  566. int len;
  567. char *cp;
  568. struct var_t *var_data;
  569. long value;
  570. unsigned long flags;
  571. param = spk_var_header_by_name(attr->attr.name);
  572. if (!param)
  573. return -EINVAL;
  574. if (!param->data)
  575. return 0;
  576. ret = 0;
  577. cp = (char *)buf;
  578. string_unescape_any_inplace(cp);
  579. spin_lock_irqsave(&speakup_info.spinlock, flags);
  580. switch (param->var_type) {
  581. case VAR_NUM:
  582. case VAR_TIME:
  583. if (*cp == 'd' || *cp == 'r' || *cp == '\0')
  584. len = E_DEFAULT;
  585. else if (*cp == '+' || *cp == '-')
  586. len = E_INC;
  587. else
  588. len = E_SET;
  589. if (kstrtol(cp, 10, &value) == 0)
  590. ret = spk_set_num_var(value, param, len);
  591. else
  592. pr_warn("overflow or parsing error has occurred");
  593. if (ret == -ERANGE) {
  594. var_data = param->data;
  595. pr_warn("value for %s out of range, expect %d to %d\n",
  596. param->name,
  597. var_data->u.n.low, var_data->u.n.high);
  598. }
  599. /*
  600. * If voice was just changed, we might need to reset our default
  601. * pitch and volume.
  602. */
  603. if (param->var_id == VOICE && synth &&
  604. (ret == 0 || ret == -ERESTART)) {
  605. var_data = param->data;
  606. value = var_data->u.n.value;
  607. spk_reset_default_value("pitch", synth->default_pitch,
  608. value);
  609. spk_reset_default_value("vol", synth->default_vol,
  610. value);
  611. }
  612. break;
  613. case VAR_STRING:
  614. len = strlen(cp);
  615. if ((len >= 1) && (cp[len - 1] == '\n'))
  616. --len;
  617. if ((len >= 2) && (cp[0] == '"') && (cp[len - 1] == '"')) {
  618. ++cp;
  619. len -= 2;
  620. }
  621. cp[len] = '\0';
  622. ret = spk_set_string_var(cp, param, len);
  623. if (ret == -E2BIG)
  624. pr_warn("value too long for %s\n",
  625. param->name);
  626. break;
  627. default:
  628. pr_warn("%s unknown type %d\n",
  629. param->name, (int)param->var_type);
  630. break;
  631. }
  632. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  633. if (ret == -ERESTART)
  634. pr_info("%s reset to default value\n", param->name);
  635. return count;
  636. }
  637. EXPORT_SYMBOL_GPL(spk_var_store);
  638. /*
  639. * Functions for reading and writing lists of i18n messages. Incomplete.
  640. */
  641. static ssize_t message_show_helper(char *buf, enum msg_index_t first,
  642. enum msg_index_t last)
  643. {
  644. size_t bufsize = PAGE_SIZE;
  645. char *buf_pointer = buf;
  646. int printed;
  647. enum msg_index_t cursor;
  648. int index = 0;
  649. *buf_pointer = '\0'; /* buf_pointer always looking at a NUL byte. */
  650. for (cursor = first; cursor <= last; cursor++, index++) {
  651. if (bufsize <= 1)
  652. break;
  653. printed = scnprintf(buf_pointer, bufsize, "%d\t%s\n",
  654. index, spk_msg_get(cursor));
  655. buf_pointer += printed;
  656. bufsize -= printed;
  657. }
  658. return buf_pointer - buf;
  659. }
  660. static void report_msg_status(int reset, int received, int used,
  661. int rejected, char *groupname)
  662. {
  663. int len;
  664. char buf[160];
  665. if (reset) {
  666. pr_info("i18n messages from group %s reset to defaults\n",
  667. groupname);
  668. } else if (received) {
  669. len = snprintf(buf, sizeof(buf),
  670. " updated %d of %d i18n messages from group %s\n",
  671. used, received, groupname);
  672. if (rejected)
  673. snprintf(buf + (len - 1), sizeof(buf) - (len - 1),
  674. " with %d reject%s\n",
  675. rejected, rejected > 1 ? "s" : "");
  676. printk(buf);
  677. }
  678. }
  679. static ssize_t message_store_helper(const char *buf, size_t count,
  680. struct msg_group_t *group)
  681. {
  682. char *cp = (char *) buf;
  683. char *end = cp + count;
  684. char *linefeed = NULL;
  685. char *temp = NULL;
  686. ssize_t msg_stored = 0;
  687. ssize_t retval = count;
  688. size_t desc_length = 0;
  689. unsigned long index = 0;
  690. int received = 0;
  691. int used = 0;
  692. int rejected = 0;
  693. int reset = 0;
  694. enum msg_index_t firstmessage = group->start;
  695. enum msg_index_t lastmessage = group->end;
  696. enum msg_index_t curmessage;
  697. while (cp < end) {
  698. while ((cp < end) && (*cp == ' ' || *cp == '\t'))
  699. cp++;
  700. if (cp == end)
  701. break;
  702. if (strchr("dDrR", *cp)) {
  703. reset = 1;
  704. break;
  705. }
  706. received++;
  707. linefeed = strchr(cp, '\n');
  708. if (!linefeed) {
  709. rejected++;
  710. break;
  711. }
  712. if (!isdigit(*cp)) {
  713. rejected++;
  714. cp = linefeed + 1;
  715. continue;
  716. }
  717. index = simple_strtoul(cp, &temp, 10);
  718. while ((temp < linefeed) && (*temp == ' ' || *temp == '\t'))
  719. temp++;
  720. desc_length = linefeed - temp;
  721. curmessage = firstmessage + index;
  722. /*
  723. * Note the check (curmessage < firstmessage). It is not
  724. * redundant. Suppose that the user gave us an index
  725. * equal to ULONG_MAX - 1. If firstmessage > 1, then
  726. * firstmessage + index < firstmessage!
  727. */
  728. if ((curmessage < firstmessage) || (curmessage > lastmessage)) {
  729. rejected++;
  730. cp = linefeed + 1;
  731. continue;
  732. }
  733. msg_stored = spk_msg_set(curmessage, temp, desc_length);
  734. if (msg_stored < 0) {
  735. retval = msg_stored;
  736. if (msg_stored == -ENOMEM)
  737. reset = 1;
  738. break;
  739. }
  740. used++;
  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. if (WARN_ON(!group))
  755. return -EINVAL;
  756. spin_lock_irqsave(&speakup_info.spinlock, flags);
  757. retval = message_show_helper(buf, group->start, group->end);
  758. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  759. return retval;
  760. }
  761. static ssize_t message_store(struct kobject *kobj, struct kobj_attribute *attr,
  762. const char *buf, size_t count)
  763. {
  764. struct msg_group_t *group = spk_find_msg_group(attr->attr.name);
  765. if (WARN_ON(!group))
  766. return -EINVAL;
  767. return message_store_helper(buf, count, group);
  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. }