pvrusb2-ctrl.c 14 KB

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