pvrusb2-ctrl.c 14 KB

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