pvrusb2-ctrl.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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 -EINVAL;
  124. LOCK_TAKE(cptr->hdw->big_lock); do {
  125. if (cptr->info->get_def_value) {
  126. ret = cptr->info->get_def_value(cptr, valptr);
  127. } else {
  128. *valptr = cptr->info->default_value;
  129. }
  130. } while(0); LOCK_GIVE(cptr->hdw->big_lock);
  131. return ret;
  132. }
  133. /* Retrieve control's enumeration count (enum only) */
  134. int pvr2_ctrl_get_cnt(struct pvr2_ctrl *cptr)
  135. {
  136. int ret = 0;
  137. if (!cptr) return 0;
  138. LOCK_TAKE(cptr->hdw->big_lock); do {
  139. if (cptr->info->type == pvr2_ctl_enum) {
  140. ret = cptr->info->def.type_enum.count;
  141. }
  142. } while(0); LOCK_GIVE(cptr->hdw->big_lock);
  143. return ret;
  144. }
  145. /* Retrieve control's valid mask bits (bit mask only) */
  146. int pvr2_ctrl_get_mask(struct pvr2_ctrl *cptr)
  147. {
  148. int ret = 0;
  149. if (!cptr) return 0;
  150. LOCK_TAKE(cptr->hdw->big_lock); do {
  151. if (cptr->info->type == pvr2_ctl_bitmask) {
  152. ret = cptr->info->def.type_bitmask.valid_bits;
  153. }
  154. } while(0); LOCK_GIVE(cptr->hdw->big_lock);
  155. return ret;
  156. }
  157. /* Retrieve the control's name */
  158. const char *pvr2_ctrl_get_name(struct pvr2_ctrl *cptr)
  159. {
  160. if (!cptr) return NULL;
  161. return cptr->info->name;
  162. }
  163. /* Retrieve the control's desc */
  164. const char *pvr2_ctrl_get_desc(struct pvr2_ctrl *cptr)
  165. {
  166. if (!cptr) return NULL;
  167. return cptr->info->desc;
  168. }
  169. /* Retrieve a control enumeration or bit mask value */
  170. int pvr2_ctrl_get_valname(struct pvr2_ctrl *cptr,int val,
  171. char *bptr,unsigned int bmax,
  172. unsigned int *blen)
  173. {
  174. int ret = -EINVAL;
  175. if (!cptr) return 0;
  176. *blen = 0;
  177. LOCK_TAKE(cptr->hdw->big_lock); do {
  178. if (cptr->info->type == pvr2_ctl_enum) {
  179. const char * const *names;
  180. names = cptr->info->def.type_enum.value_names;
  181. if (pvr2_ctrl_range_check(cptr,val) == 0) {
  182. if (names[val]) {
  183. *blen = scnprintf(
  184. bptr,bmax,"%s",
  185. names[val]);
  186. } else {
  187. *blen = 0;
  188. }
  189. ret = 0;
  190. }
  191. } else if (cptr->info->type == pvr2_ctl_bitmask) {
  192. const char **names;
  193. unsigned int idx;
  194. int msk;
  195. names = cptr->info->def.type_bitmask.bit_names;
  196. val &= cptr->info->def.type_bitmask.valid_bits;
  197. for (idx = 0, msk = 1; val; idx++, msk <<= 1) {
  198. if (val & msk) {
  199. *blen = scnprintf(bptr,bmax,"%s",
  200. names[idx]);
  201. ret = 0;
  202. break;
  203. }
  204. }
  205. }
  206. } while(0); LOCK_GIVE(cptr->hdw->big_lock);
  207. return ret;
  208. }
  209. /* Return V4L ID for this control or zero if none */
  210. int pvr2_ctrl_get_v4lid(struct pvr2_ctrl *cptr)
  211. {
  212. if (!cptr) return 0;
  213. return cptr->info->v4l_id;
  214. }
  215. unsigned int pvr2_ctrl_get_v4lflags(struct pvr2_ctrl *cptr)
  216. {
  217. unsigned int flags = 0;
  218. if (cptr->info->get_v4lflags) {
  219. flags = cptr->info->get_v4lflags(cptr);
  220. }
  221. if (cptr->info->set_value) {
  222. flags &= ~V4L2_CTRL_FLAG_READ_ONLY;
  223. } else {
  224. flags |= V4L2_CTRL_FLAG_READ_ONLY;
  225. }
  226. return flags;
  227. }
  228. /* Return true if control is writable */
  229. int pvr2_ctrl_is_writable(struct pvr2_ctrl *cptr)
  230. {
  231. if (!cptr) return 0;
  232. return cptr->info->set_value != NULL;
  233. }
  234. /* Return true if control has custom symbolic representation */
  235. int pvr2_ctrl_has_custom_symbols(struct pvr2_ctrl *cptr)
  236. {
  237. if (!cptr) return 0;
  238. if (!cptr->info->val_to_sym) return 0;
  239. if (!cptr->info->sym_to_val) return 0;
  240. return !0;
  241. }
  242. /* Convert a given mask/val to a custom symbolic value */
  243. int pvr2_ctrl_custom_value_to_sym(struct pvr2_ctrl *cptr,
  244. int mask,int val,
  245. char *buf,unsigned int maxlen,
  246. unsigned int *len)
  247. {
  248. if (!cptr) return -EINVAL;
  249. if (!cptr->info->val_to_sym) return -EINVAL;
  250. return cptr->info->val_to_sym(cptr,mask,val,buf,maxlen,len);
  251. }
  252. /* Convert a symbolic value to a mask/value pair */
  253. int pvr2_ctrl_custom_sym_to_value(struct pvr2_ctrl *cptr,
  254. const char *buf,unsigned int len,
  255. int *maskptr,int *valptr)
  256. {
  257. if (!cptr) return -EINVAL;
  258. if (!cptr->info->sym_to_val) return -EINVAL;
  259. return cptr->info->sym_to_val(cptr,buf,len,maskptr,valptr);
  260. }
  261. static unsigned int gen_bitmask_string(int msk,int val,int msk_only,
  262. const char **names,
  263. char *ptr,unsigned int len)
  264. {
  265. unsigned int idx;
  266. long sm,um;
  267. int spcFl;
  268. unsigned int uc,cnt;
  269. const char *idStr;
  270. spcFl = 0;
  271. uc = 0;
  272. um = 0;
  273. for (idx = 0, sm = 1; msk; idx++, sm <<= 1) {
  274. if (sm & msk) {
  275. msk &= ~sm;
  276. idStr = names[idx];
  277. if (idStr) {
  278. cnt = scnprintf(ptr,len,"%s%s%s",
  279. (spcFl ? " " : ""),
  280. (msk_only ? "" :
  281. ((val & sm) ? "+" : "-")),
  282. idStr);
  283. ptr += cnt; len -= cnt; uc += cnt;
  284. spcFl = !0;
  285. } else {
  286. um |= sm;
  287. }
  288. }
  289. }
  290. if (um) {
  291. if (msk_only) {
  292. cnt = scnprintf(ptr,len,"%s0x%lx",
  293. (spcFl ? " " : ""),
  294. um);
  295. ptr += cnt; len -= cnt; uc += cnt;
  296. spcFl = !0;
  297. } else if (um & val) {
  298. cnt = scnprintf(ptr,len,"%s+0x%lx",
  299. (spcFl ? " " : ""),
  300. um & val);
  301. ptr += cnt; len -= cnt; uc += cnt;
  302. spcFl = !0;
  303. } else if (um & ~val) {
  304. cnt = scnprintf(ptr,len,"%s+0x%lx",
  305. (spcFl ? " " : ""),
  306. um & ~val);
  307. ptr += cnt; len -= cnt; uc += cnt;
  308. spcFl = !0;
  309. }
  310. }
  311. return uc;
  312. }
  313. static const char *boolNames[] = {
  314. "false",
  315. "true",
  316. "no",
  317. "yes",
  318. };
  319. static int parse_token(const char *ptr,unsigned int len,
  320. int *valptr,
  321. const char * const *names, unsigned int namecnt)
  322. {
  323. char buf[33];
  324. unsigned int slen;
  325. unsigned int idx;
  326. int negfl;
  327. char *p2;
  328. *valptr = 0;
  329. if (!names) namecnt = 0;
  330. for (idx = 0; idx < namecnt; idx++) {
  331. if (!names[idx]) continue;
  332. slen = strlen(names[idx]);
  333. if (slen != len) continue;
  334. if (memcmp(names[idx],ptr,slen)) continue;
  335. *valptr = idx;
  336. return 0;
  337. }
  338. negfl = 0;
  339. if ((*ptr == '-') || (*ptr == '+')) {
  340. negfl = (*ptr == '-');
  341. ptr++; len--;
  342. }
  343. if (len >= sizeof(buf)) return -EINVAL;
  344. memcpy(buf,ptr,len);
  345. buf[len] = 0;
  346. *valptr = simple_strtol(buf,&p2,0);
  347. if (negfl) *valptr = -(*valptr);
  348. if (*p2) return -EINVAL;
  349. return 1;
  350. }
  351. static int parse_mtoken(const char *ptr,unsigned int len,
  352. int *valptr,
  353. const char **names,int valid_bits)
  354. {
  355. char buf[33];
  356. unsigned int slen;
  357. unsigned int idx;
  358. char *p2;
  359. int msk;
  360. *valptr = 0;
  361. for (idx = 0, msk = 1; valid_bits; idx++, msk <<= 1) {
  362. if (!(msk & valid_bits)) continue;
  363. valid_bits &= ~msk;
  364. if (!names[idx]) continue;
  365. slen = strlen(names[idx]);
  366. if (slen != len) continue;
  367. if (memcmp(names[idx],ptr,slen)) continue;
  368. *valptr = msk;
  369. return 0;
  370. }
  371. if (len >= sizeof(buf)) return -EINVAL;
  372. memcpy(buf,ptr,len);
  373. buf[len] = 0;
  374. *valptr = simple_strtol(buf,&p2,0);
  375. if (*p2) return -EINVAL;
  376. return 0;
  377. }
  378. static int parse_tlist(const char *ptr,unsigned int len,
  379. int *maskptr,int *valptr,
  380. const char **names,int valid_bits)
  381. {
  382. unsigned int cnt;
  383. int mask,val,kv,mode,ret;
  384. mask = 0;
  385. val = 0;
  386. ret = 0;
  387. while (len) {
  388. cnt = 0;
  389. while ((cnt < len) &&
  390. ((ptr[cnt] <= 32) ||
  391. (ptr[cnt] >= 127))) cnt++;
  392. ptr += cnt;
  393. len -= cnt;
  394. mode = 0;
  395. if ((*ptr == '-') || (*ptr == '+')) {
  396. mode = (*ptr == '-') ? -1 : 1;
  397. ptr++;
  398. len--;
  399. }
  400. cnt = 0;
  401. while (cnt < len) {
  402. if (ptr[cnt] <= 32) break;
  403. if (ptr[cnt] >= 127) break;
  404. cnt++;
  405. }
  406. if (!cnt) break;
  407. if (parse_mtoken(ptr,cnt,&kv,names,valid_bits)) {
  408. ret = -EINVAL;
  409. break;
  410. }
  411. ptr += cnt;
  412. len -= cnt;
  413. switch (mode) {
  414. case 0:
  415. mask = valid_bits;
  416. val |= kv;
  417. break;
  418. case -1:
  419. mask |= kv;
  420. val &= ~kv;
  421. break;
  422. case 1:
  423. mask |= kv;
  424. val |= kv;
  425. break;
  426. default:
  427. break;
  428. }
  429. }
  430. *maskptr = mask;
  431. *valptr = val;
  432. return ret;
  433. }
  434. /* Convert a symbolic value to a mask/value pair */
  435. int pvr2_ctrl_sym_to_value(struct pvr2_ctrl *cptr,
  436. const char *ptr,unsigned int len,
  437. int *maskptr,int *valptr)
  438. {
  439. int ret = -EINVAL;
  440. unsigned int cnt;
  441. *maskptr = 0;
  442. *valptr = 0;
  443. cnt = 0;
  444. while ((cnt < len) && ((ptr[cnt] <= 32) || (ptr[cnt] >= 127))) cnt++;
  445. len -= cnt; ptr += cnt;
  446. cnt = 0;
  447. while ((cnt < len) && ((ptr[len-(cnt+1)] <= 32) ||
  448. (ptr[len-(cnt+1)] >= 127))) cnt++;
  449. len -= cnt;
  450. if (!len) return -EINVAL;
  451. LOCK_TAKE(cptr->hdw->big_lock); do {
  452. if (cptr->info->type == pvr2_ctl_int) {
  453. ret = parse_token(ptr,len,valptr,NULL,0);
  454. if (ret >= 0) {
  455. ret = pvr2_ctrl_range_check(cptr,*valptr);
  456. }
  457. *maskptr = ~0;
  458. } else if (cptr->info->type == pvr2_ctl_bool) {
  459. ret = parse_token(ptr,len,valptr,boolNames,
  460. ARRAY_SIZE(boolNames));
  461. if (ret == 1) {
  462. *valptr = *valptr ? !0 : 0;
  463. } else if (ret == 0) {
  464. *valptr = (*valptr & 1) ? !0 : 0;
  465. }
  466. *maskptr = 1;
  467. } else if (cptr->info->type == pvr2_ctl_enum) {
  468. ret = parse_token(
  469. ptr,len,valptr,
  470. cptr->info->def.type_enum.value_names,
  471. cptr->info->def.type_enum.count);
  472. if (ret >= 0) {
  473. ret = pvr2_ctrl_range_check(cptr,*valptr);
  474. }
  475. *maskptr = ~0;
  476. } else if (cptr->info->type == pvr2_ctl_bitmask) {
  477. ret = parse_tlist(
  478. ptr,len,maskptr,valptr,
  479. cptr->info->def.type_bitmask.bit_names,
  480. cptr->info->def.type_bitmask.valid_bits);
  481. }
  482. } while(0); LOCK_GIVE(cptr->hdw->big_lock);
  483. return ret;
  484. }
  485. /* Convert a given mask/val to a symbolic value */
  486. int pvr2_ctrl_value_to_sym_internal(struct pvr2_ctrl *cptr,
  487. int mask,int val,
  488. char *buf,unsigned int maxlen,
  489. unsigned int *len)
  490. {
  491. int ret = -EINVAL;
  492. *len = 0;
  493. if (cptr->info->type == pvr2_ctl_int) {
  494. *len = scnprintf(buf,maxlen,"%d",val);
  495. ret = 0;
  496. } else if (cptr->info->type == pvr2_ctl_bool) {
  497. *len = scnprintf(buf,maxlen,"%s",val ? "true" : "false");
  498. ret = 0;
  499. } else if (cptr->info->type == pvr2_ctl_enum) {
  500. const char * const *names;
  501. names = cptr->info->def.type_enum.value_names;
  502. if ((val >= 0) &&
  503. (val < cptr->info->def.type_enum.count)) {
  504. if (names[val]) {
  505. *len = scnprintf(
  506. buf,maxlen,"%s",
  507. names[val]);
  508. } else {
  509. *len = 0;
  510. }
  511. ret = 0;
  512. }
  513. } else if (cptr->info->type == pvr2_ctl_bitmask) {
  514. *len = gen_bitmask_string(
  515. val & mask & cptr->info->def.type_bitmask.valid_bits,
  516. ~0,!0,
  517. cptr->info->def.type_bitmask.bit_names,
  518. buf,maxlen);
  519. }
  520. return ret;
  521. }
  522. /* Convert a given mask/val to a symbolic value */
  523. int pvr2_ctrl_value_to_sym(struct pvr2_ctrl *cptr,
  524. int mask,int val,
  525. char *buf,unsigned int maxlen,
  526. unsigned int *len)
  527. {
  528. int ret;
  529. LOCK_TAKE(cptr->hdw->big_lock); do {
  530. ret = pvr2_ctrl_value_to_sym_internal(cptr,mask,val,
  531. buf,maxlen,len);
  532. } while(0); LOCK_GIVE(cptr->hdw->big_lock);
  533. return ret;
  534. }