pvrusb2-ctrl.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. /*
  2. *
  3. * $Id$
  4. *
  5. * Copyright (C) 2005 Mike Isely <isely@pobox.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. #include "pvrusb2-ctrl.h"
  22. #include "pvrusb2-hdw-internal.h"
  23. #include <linux/errno.h>
  24. #include <linux/string.h>
  25. #include <linux/mutex.h>
  26. /* Set the given control. */
  27. int pvr2_ctrl_set_value(struct pvr2_ctrl *cptr,int val)
  28. {
  29. return pvr2_ctrl_set_mask_value(cptr,~0,val);
  30. }
  31. /* Set/clear specific bits of the given control. */
  32. int pvr2_ctrl_set_mask_value(struct pvr2_ctrl *cptr,int mask,int val)
  33. {
  34. int ret = 0;
  35. if (!cptr) return -EINVAL;
  36. LOCK_TAKE(cptr->hdw->big_lock); do {
  37. if (cptr->info->set_value != 0) {
  38. if (cptr->info->type == pvr2_ctl_bitmask) {
  39. mask &= cptr->info->def.type_bitmask.valid_bits;
  40. } else if (cptr->info->type == pvr2_ctl_int) {
  41. if (val < cptr->info->def.type_int.min_value) {
  42. break;
  43. }
  44. if (val > cptr->info->def.type_int.max_value) {
  45. break;
  46. }
  47. } else if (cptr->info->type == pvr2_ctl_enum) {
  48. if (val >= cptr->info->def.type_enum.count) {
  49. break;
  50. }
  51. } else if (cptr->info->type != pvr2_ctl_bool) {
  52. break;
  53. }
  54. ret = cptr->info->set_value(cptr,mask,val);
  55. } else {
  56. ret = -EPERM;
  57. }
  58. } while(0); LOCK_GIVE(cptr->hdw->big_lock);
  59. return ret;
  60. }
  61. /* Get the current value of the given control. */
  62. int pvr2_ctrl_get_value(struct pvr2_ctrl *cptr,int *valptr)
  63. {
  64. int ret = 0;
  65. if (!cptr) return -EINVAL;
  66. LOCK_TAKE(cptr->hdw->big_lock); do {
  67. ret = cptr->info->get_value(cptr,valptr);
  68. } while(0); LOCK_GIVE(cptr->hdw->big_lock);
  69. return ret;
  70. }
  71. /* Retrieve control's type */
  72. enum pvr2_ctl_type pvr2_ctrl_get_type(struct pvr2_ctrl *cptr)
  73. {
  74. if (!cptr) return pvr2_ctl_int;
  75. return cptr->info->type;
  76. }
  77. /* Retrieve control's maximum value (int type) */
  78. int pvr2_ctrl_get_max(struct pvr2_ctrl *cptr)
  79. {
  80. int ret = 0;
  81. if (!cptr) return 0;
  82. LOCK_TAKE(cptr->hdw->big_lock); do {
  83. if (cptr->info->type == pvr2_ctl_int) {
  84. ret = cptr->info->def.type_int.max_value;
  85. }
  86. } while(0); LOCK_GIVE(cptr->hdw->big_lock);
  87. return ret;
  88. }
  89. /* Retrieve control's minimum value (int type) */
  90. int pvr2_ctrl_get_min(struct pvr2_ctrl *cptr)
  91. {
  92. int ret = 0;
  93. if (!cptr) return 0;
  94. LOCK_TAKE(cptr->hdw->big_lock); do {
  95. if (cptr->info->type == pvr2_ctl_int) {
  96. ret = cptr->info->def.type_int.min_value;
  97. }
  98. } while(0); LOCK_GIVE(cptr->hdw->big_lock);
  99. return ret;
  100. }
  101. /* Retrieve control's default value (any type) */
  102. int pvr2_ctrl_get_def(struct pvr2_ctrl *cptr)
  103. {
  104. int ret = 0;
  105. if (!cptr) return 0;
  106. LOCK_TAKE(cptr->hdw->big_lock); do {
  107. if (cptr->info->type == pvr2_ctl_int) {
  108. ret = cptr->info->default_value;
  109. }
  110. } while(0); LOCK_GIVE(cptr->hdw->big_lock);
  111. return ret;
  112. }
  113. /* Retrieve control's enumeration count (enum only) */
  114. int pvr2_ctrl_get_cnt(struct pvr2_ctrl *cptr)
  115. {
  116. int ret = 0;
  117. if (!cptr) return 0;
  118. LOCK_TAKE(cptr->hdw->big_lock); do {
  119. if (cptr->info->type == pvr2_ctl_enum) {
  120. ret = cptr->info->def.type_enum.count;
  121. }
  122. } while(0); LOCK_GIVE(cptr->hdw->big_lock);
  123. return ret;
  124. }
  125. /* Retrieve control's valid mask bits (bit mask only) */
  126. int pvr2_ctrl_get_mask(struct pvr2_ctrl *cptr)
  127. {
  128. int ret = 0;
  129. if (!cptr) return 0;
  130. LOCK_TAKE(cptr->hdw->big_lock); do {
  131. if (cptr->info->type == pvr2_ctl_bitmask) {
  132. ret = cptr->info->def.type_bitmask.valid_bits;
  133. }
  134. } while(0); LOCK_GIVE(cptr->hdw->big_lock);
  135. return ret;
  136. }
  137. /* Retrieve the control's name */
  138. const char *pvr2_ctrl_get_name(struct pvr2_ctrl *cptr)
  139. {
  140. if (!cptr) return 0;
  141. return cptr->info->name;
  142. }
  143. /* Retrieve the control's desc */
  144. const char *pvr2_ctrl_get_desc(struct pvr2_ctrl *cptr)
  145. {
  146. if (!cptr) return 0;
  147. return cptr->info->desc;
  148. }
  149. /* Retrieve a control enumeration or bit mask value */
  150. int pvr2_ctrl_get_valname(struct pvr2_ctrl *cptr,int val,
  151. char *bptr,unsigned int bmax,
  152. unsigned int *blen)
  153. {
  154. int ret = -EINVAL;
  155. if (!cptr) return 0;
  156. *blen = 0;
  157. LOCK_TAKE(cptr->hdw->big_lock); do {
  158. if (cptr->info->type == pvr2_ctl_enum) {
  159. const char **names;
  160. names = cptr->info->def.type_enum.value_names;
  161. if ((val >= 0) &&
  162. (val < cptr->info->def.type_enum.count)) {
  163. if (names[val]) {
  164. *blen = scnprintf(
  165. bptr,bmax,"%s",
  166. names[val]);
  167. } else {
  168. *blen = 0;
  169. }
  170. ret = 0;
  171. }
  172. } else if (cptr->info->type == pvr2_ctl_bitmask) {
  173. const char **names;
  174. unsigned int idx;
  175. int msk;
  176. names = cptr->info->def.type_bitmask.bit_names;
  177. val &= cptr->info->def.type_bitmask.valid_bits;
  178. for (idx = 0, msk = 1; val; idx++, msk <<= 1) {
  179. if (val & msk) {
  180. *blen = scnprintf(bptr,bmax,"%s",
  181. names[idx]);
  182. ret = 0;
  183. break;
  184. }
  185. }
  186. }
  187. } while(0); LOCK_GIVE(cptr->hdw->big_lock);
  188. return ret;
  189. }
  190. /* Return true if control is writable */
  191. int pvr2_ctrl_is_writable(struct pvr2_ctrl *cptr)
  192. {
  193. if (!cptr) return 0;
  194. return cptr->info->set_value != 0;
  195. }
  196. /* Return true if control has custom symbolic representation */
  197. int pvr2_ctrl_has_custom_symbols(struct pvr2_ctrl *cptr)
  198. {
  199. if (!cptr) return 0;
  200. if (!cptr->info->val_to_sym) return 0;
  201. if (!cptr->info->sym_to_val) return 0;
  202. return !0;
  203. }
  204. /* Convert a given mask/val to a custom symbolic value */
  205. int pvr2_ctrl_custom_value_to_sym(struct pvr2_ctrl *cptr,
  206. int mask,int val,
  207. char *buf,unsigned int maxlen,
  208. unsigned int *len)
  209. {
  210. if (!cptr) return -EINVAL;
  211. if (!cptr->info->val_to_sym) return -EINVAL;
  212. return cptr->info->val_to_sym(cptr,mask,val,buf,maxlen,len);
  213. }
  214. /* Convert a symbolic value to a mask/value pair */
  215. int pvr2_ctrl_custom_sym_to_value(struct pvr2_ctrl *cptr,
  216. const char *buf,unsigned int len,
  217. int *maskptr,int *valptr)
  218. {
  219. if (!cptr) return -EINVAL;
  220. if (!cptr->info->sym_to_val) return -EINVAL;
  221. return cptr->info->sym_to_val(cptr,buf,len,maskptr,valptr);
  222. }
  223. static unsigned int gen_bitmask_string(int msk,int val,int msk_only,
  224. const char **names,
  225. char *ptr,unsigned int len)
  226. {
  227. unsigned int idx;
  228. long sm,um;
  229. int spcFl;
  230. unsigned int uc,cnt;
  231. const char *idStr;
  232. spcFl = 0;
  233. uc = 0;
  234. um = 0;
  235. for (idx = 0, sm = 1; msk; idx++, sm <<= 1) {
  236. if (sm & msk) {
  237. msk &= ~sm;
  238. idStr = names[idx];
  239. if (idStr) {
  240. cnt = scnprintf(ptr,len,"%s%s%s",
  241. (spcFl ? " " : ""),
  242. (msk_only ? "" :
  243. ((val & sm) ? "+" : "-")),
  244. idStr);
  245. ptr += cnt; len -= cnt; uc += cnt;
  246. spcFl = !0;
  247. } else {
  248. um |= sm;
  249. }
  250. }
  251. }
  252. if (um) {
  253. if (msk_only) {
  254. cnt = scnprintf(ptr,len,"%s0x%lx",
  255. (spcFl ? " " : ""),
  256. um);
  257. ptr += cnt; len -= cnt; uc += cnt;
  258. spcFl = !0;
  259. } else if (um & val) {
  260. cnt = scnprintf(ptr,len,"%s+0x%lx",
  261. (spcFl ? " " : ""),
  262. um & val);
  263. ptr += cnt; len -= cnt; uc += cnt;
  264. spcFl = !0;
  265. } else if (um & ~val) {
  266. cnt = scnprintf(ptr,len,"%s+0x%lx",
  267. (spcFl ? " " : ""),
  268. um & ~val);
  269. ptr += cnt; len -= cnt; uc += cnt;
  270. spcFl = !0;
  271. }
  272. }
  273. return uc;
  274. }
  275. static const char *boolNames[] = {
  276. "false",
  277. "true",
  278. "no",
  279. "yes",
  280. };
  281. static int parse_token(const char *ptr,unsigned int len,
  282. int *valptr,
  283. const char **names,unsigned int namecnt)
  284. {
  285. char buf[33];
  286. unsigned int slen;
  287. unsigned int idx;
  288. int negfl;
  289. char *p2;
  290. *valptr = 0;
  291. if (!names) namecnt = 0;
  292. for (idx = 0; idx < namecnt; idx++) {
  293. if (!names[idx]) continue;
  294. slen = strlen(names[idx]);
  295. if (slen != len) continue;
  296. if (memcmp(names[idx],ptr,slen)) continue;
  297. *valptr = idx;
  298. return 0;
  299. }
  300. negfl = 0;
  301. if ((*ptr == '-') || (*ptr == '+')) {
  302. negfl = (*ptr == '-');
  303. ptr++; len--;
  304. }
  305. if (len >= sizeof(buf)) return -EINVAL;
  306. memcpy(buf,ptr,len);
  307. buf[len] = 0;
  308. *valptr = simple_strtol(buf,&p2,0);
  309. if (negfl) *valptr = -(*valptr);
  310. if (*p2) return -EINVAL;
  311. return 1;
  312. }
  313. static int parse_mtoken(const char *ptr,unsigned int len,
  314. int *valptr,
  315. const char **names,int valid_bits)
  316. {
  317. char buf[33];
  318. unsigned int slen;
  319. unsigned int idx;
  320. char *p2;
  321. int msk;
  322. *valptr = 0;
  323. for (idx = 0, msk = 1; valid_bits; idx++, msk <<= 1) {
  324. if (!msk & valid_bits) continue;
  325. valid_bits &= ~msk;
  326. if (!names[idx]) continue;
  327. slen = strlen(names[idx]);
  328. if (slen != len) continue;
  329. if (memcmp(names[idx],ptr,slen)) continue;
  330. *valptr = msk;
  331. return 0;
  332. }
  333. if (len >= sizeof(buf)) return -EINVAL;
  334. memcpy(buf,ptr,len);
  335. buf[len] = 0;
  336. *valptr = simple_strtol(buf,&p2,0);
  337. if (*p2) return -EINVAL;
  338. return 0;
  339. }
  340. static int parse_tlist(const char *ptr,unsigned int len,
  341. int *maskptr,int *valptr,
  342. const char **names,int valid_bits)
  343. {
  344. unsigned int cnt;
  345. int mask,val,kv,mode,ret;
  346. mask = 0;
  347. val = 0;
  348. ret = 0;
  349. while (len) {
  350. cnt = 0;
  351. while ((cnt < len) &&
  352. ((ptr[cnt] <= 32) ||
  353. (ptr[cnt] >= 127))) cnt++;
  354. ptr += cnt;
  355. len -= cnt;
  356. mode = 0;
  357. if ((*ptr == '-') || (*ptr == '+')) {
  358. mode = (*ptr == '-') ? -1 : 1;
  359. ptr++;
  360. len--;
  361. }
  362. cnt = 0;
  363. while (cnt < len) {
  364. if (ptr[cnt] <= 32) break;
  365. if (ptr[cnt] >= 127) break;
  366. cnt++;
  367. }
  368. if (!cnt) break;
  369. if (parse_mtoken(ptr,cnt,&kv,names,valid_bits)) {
  370. ret = -EINVAL;
  371. break;
  372. }
  373. ptr += cnt;
  374. len -= cnt;
  375. switch (mode) {
  376. case 0:
  377. mask = valid_bits;
  378. val |= kv;
  379. break;
  380. case -1:
  381. mask |= kv;
  382. val &= ~kv;
  383. break;
  384. case 1:
  385. mask |= kv;
  386. val |= kv;
  387. break;
  388. default:
  389. break;
  390. }
  391. }
  392. *maskptr = mask;
  393. *valptr = val;
  394. return ret;
  395. }
  396. /* Convert a symbolic value to a mask/value pair */
  397. int pvr2_ctrl_sym_to_value(struct pvr2_ctrl *cptr,
  398. const char *ptr,unsigned int len,
  399. int *maskptr,int *valptr)
  400. {
  401. int ret = -EINVAL;
  402. unsigned int cnt;
  403. *maskptr = 0;
  404. *valptr = 0;
  405. cnt = 0;
  406. while ((cnt < len) && ((ptr[cnt] <= 32) || (ptr[cnt] >= 127))) cnt++;
  407. len -= cnt; ptr += cnt;
  408. cnt = 0;
  409. while ((cnt < len) && ((ptr[len-(cnt+1)] <= 32) ||
  410. (ptr[len-(cnt+1)] >= 127))) cnt++;
  411. len -= cnt;
  412. if (!len) return -EINVAL;
  413. LOCK_TAKE(cptr->hdw->big_lock); do {
  414. if (cptr->info->type == pvr2_ctl_int) {
  415. ret = parse_token(ptr,len,valptr,0,0);
  416. if ((ret >= 0) &&
  417. ((*valptr < cptr->info->def.type_int.min_value) ||
  418. (*valptr > cptr->info->def.type_int.max_value))) {
  419. ret = -ERANGE;
  420. }
  421. if (maskptr) *maskptr = ~0;
  422. } else if (cptr->info->type == pvr2_ctl_bool) {
  423. ret = parse_token(
  424. ptr,len,valptr,boolNames,
  425. sizeof(boolNames)/sizeof(boolNames[0]));
  426. if (ret == 1) {
  427. *valptr = *valptr ? !0 : 0;
  428. } else if (ret == 0) {
  429. *valptr = (*valptr & 1) ? !0 : 0;
  430. }
  431. if (maskptr) *maskptr = 1;
  432. } else if (cptr->info->type == pvr2_ctl_enum) {
  433. ret = parse_token(
  434. ptr,len,valptr,
  435. cptr->info->def.type_enum.value_names,
  436. cptr->info->def.type_enum.count);
  437. if ((ret >= 0) &&
  438. ((*valptr < 0) ||
  439. (*valptr >= cptr->info->def.type_enum.count))) {
  440. ret = -ERANGE;
  441. }
  442. if (maskptr) *maskptr = ~0;
  443. } else if (cptr->info->type == pvr2_ctl_bitmask) {
  444. ret = parse_tlist(
  445. ptr,len,maskptr,valptr,
  446. cptr->info->def.type_bitmask.bit_names,
  447. cptr->info->def.type_bitmask.valid_bits);
  448. }
  449. } while(0); LOCK_GIVE(cptr->hdw->big_lock);
  450. return ret;
  451. }
  452. /* Convert a given mask/val to a symbolic value */
  453. int pvr2_ctrl_value_to_sym_internal(struct pvr2_ctrl *cptr,
  454. int mask,int val,
  455. char *buf,unsigned int maxlen,
  456. unsigned int *len)
  457. {
  458. int ret = -EINVAL;
  459. *len = 0;
  460. if (cptr->info->type == pvr2_ctl_int) {
  461. *len = scnprintf(buf,maxlen,"%d",val);
  462. ret = 0;
  463. } else if (cptr->info->type == pvr2_ctl_bool) {
  464. *len = scnprintf(buf,maxlen,"%s",val ? "true" : "false");
  465. ret = 0;
  466. } else if (cptr->info->type == pvr2_ctl_enum) {
  467. const char **names;
  468. names = cptr->info->def.type_enum.value_names;
  469. if ((val >= 0) &&
  470. (val < cptr->info->def.type_enum.count)) {
  471. if (names[val]) {
  472. *len = scnprintf(
  473. buf,maxlen,"%s",
  474. names[val]);
  475. } else {
  476. *len = 0;
  477. }
  478. ret = 0;
  479. }
  480. } else if (cptr->info->type == pvr2_ctl_bitmask) {
  481. *len = gen_bitmask_string(
  482. val & mask & cptr->info->def.type_bitmask.valid_bits,
  483. ~0,!0,
  484. cptr->info->def.type_bitmask.bit_names,
  485. buf,maxlen);
  486. }
  487. return ret;
  488. }
  489. /* Convert a given mask/val to a symbolic value */
  490. int pvr2_ctrl_value_to_sym(struct pvr2_ctrl *cptr,
  491. int mask,int val,
  492. char *buf,unsigned int maxlen,
  493. unsigned int *len)
  494. {
  495. int ret;
  496. LOCK_TAKE(cptr->hdw->big_lock); do {
  497. ret = pvr2_ctrl_value_to_sym_internal(cptr,mask,val,
  498. buf,maxlen,len);
  499. } while(0); LOCK_GIVE(cptr->hdw->big_lock);
  500. return ret;
  501. }
  502. /*
  503. Stuff for Emacs to see, in order to encourage consistent editing style:
  504. *** Local Variables: ***
  505. *** mode: c ***
  506. *** fill-column: 75 ***
  507. *** tab-width: 8 ***
  508. *** c-basic-offset: 8 ***
  509. *** End: ***
  510. */