pvrusb2-ctrl.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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 {
  31. int lim;
  32. lim = cptr->info->def.type_int.min_value;
  33. if (cptr->info->get_min_value) {
  34. cptr->info->get_min_value(cptr,&lim);
  35. }
  36. if (val < lim) return -ERANGE;
  37. lim = cptr->info->def.type_int.max_value;
  38. if (cptr->info->get_max_value) {
  39. cptr->info->get_max_value(cptr,&lim);
  40. }
  41. if (val > lim) return -ERANGE;
  42. }
  43. return 0;
  44. }
  45. /* Set the given control. */
  46. int pvr2_ctrl_set_value(struct pvr2_ctrl *cptr,int val)
  47. {
  48. return pvr2_ctrl_set_mask_value(cptr,~0,val);
  49. }
  50. /* Set/clear specific bits of the given control. */
  51. int pvr2_ctrl_set_mask_value(struct pvr2_ctrl *cptr,int mask,int val)
  52. {
  53. int ret = 0;
  54. if (!cptr) return -EINVAL;
  55. LOCK_TAKE(cptr->hdw->big_lock); do {
  56. if (cptr->info->set_value != 0) {
  57. if (cptr->info->type == pvr2_ctl_bitmask) {
  58. mask &= cptr->info->def.type_bitmask.valid_bits;
  59. } else if (cptr->info->type == pvr2_ctl_int) {
  60. ret = pvr2_ctrl_range_check(cptr,val);
  61. if (ret < 0) break;
  62. } else if (cptr->info->type == pvr2_ctl_enum) {
  63. if (val >= cptr->info->def.type_enum.count) {
  64. break;
  65. }
  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 ((val >= 0) &&
  181. (val < cptr->info->def.type_enum.count)) {
  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 != 0;
  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 **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. if (maskptr) *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. if (maskptr) *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. ((*valptr < 0) ||
  474. (*valptr >= cptr->info->def.type_enum.count))) {
  475. ret = -ERANGE;
  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. */