pvrusb2-ctrl.c 14 KB

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