pvrusb2-ctrl.c 13 KB

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