pvrusb2-ctrl.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. *
  4. * Copyright (C) 2005 Mike Isely <[email protected]>
  5. */
  6. #include "pvrusb2-ctrl.h"
  7. #include "pvrusb2-hdw-internal.h"
  8. #include <linux/errno.h>
  9. #include <linux/string.h>
  10. #include <linux/mutex.h>
  11. static int pvr2_ctrl_range_check(struct pvr2_ctrl *cptr,int val)
  12. {
  13. if (cptr->info->check_value) {
  14. if (!cptr->info->check_value(cptr,val)) return -ERANGE;
  15. } else if (cptr->info->type == pvr2_ctl_enum) {
  16. if (val < 0) return -ERANGE;
  17. if (val >= cptr->info->def.type_enum.count) return -ERANGE;
  18. } else {
  19. int lim;
  20. lim = cptr->info->def.type_int.min_value;
  21. if (cptr->info->get_min_value) {
  22. cptr->info->get_min_value(cptr,&lim);
  23. }
  24. if (val < lim) return -ERANGE;
  25. lim = cptr->info->def.type_int.max_value;
  26. if (cptr->info->get_max_value) {
  27. cptr->info->get_max_value(cptr,&lim);
  28. }
  29. if (val > lim) return -ERANGE;
  30. }
  31. return 0;
  32. }
  33. /* Set the given control. */
  34. int pvr2_ctrl_set_value(struct pvr2_ctrl *cptr,int val)
  35. {
  36. return pvr2_ctrl_set_mask_value(cptr,~0,val);
  37. }
  38. /* Set/clear specific bits of the given control. */
  39. int pvr2_ctrl_set_mask_value(struct pvr2_ctrl *cptr,int mask,int val)
  40. {
  41. int ret = 0;
  42. if (!cptr) return -EINVAL;
  43. LOCK_TAKE(cptr->hdw->big_lock); do {
  44. if (cptr->info->set_value) {
  45. if (cptr->info->type == pvr2_ctl_bitmask) {
  46. mask &= cptr->info->def.type_bitmask.valid_bits;
  47. } else if ((cptr->info->type == pvr2_ctl_int)||
  48. (cptr->info->type == pvr2_ctl_enum)) {
  49. ret = pvr2_ctrl_range_check(cptr,val);
  50. if (ret < 0) break;
  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->get_max_value) {
  84. cptr->info->get_max_value(cptr,&ret);
  85. } else if (cptr->info->type == pvr2_ctl_int) {
  86. ret = cptr->info->def.type_int.max_value;
  87. }
  88. } while(0); LOCK_GIVE(cptr->hdw->big_lock);
  89. return ret;
  90. }
  91. /* Retrieve control's minimum value (int type) */
  92. int pvr2_ctrl_get_min(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_min_value) {
  98. cptr->info->get_min_value(cptr,&ret);
  99. } else if (cptr->info->type == pvr2_ctl_int) {
  100. ret = cptr->info->def.type_int.min_value;
  101. }
  102. } while(0); LOCK_GIVE(cptr->hdw->big_lock);
  103. return ret;
  104. }
  105. /* Retrieve control's default value (any type) */
  106. int pvr2_ctrl_get_def(struct pvr2_ctrl *cptr, int *valptr)
  107. {
  108. int ret = 0;
  109. if (!cptr) return -EINVAL;
  110. LOCK_TAKE(cptr->hdw->big_lock); do {
  111. if (cptr->info->get_def_value) {
  112. ret = cptr->info->get_def_value(cptr, valptr);
  113. } else {
  114. *valptr = cptr->info->default_value;
  115. }
  116. } while(0); LOCK_GIVE(cptr->hdw->big_lock);
  117. return ret;
  118. }
  119. /* Retrieve control's enumeration count (enum only) */
  120. int pvr2_ctrl_get_cnt(struct pvr2_ctrl *cptr)
  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_enum) {
  126. ret = cptr->info->def.type_enum.count;
  127. }
  128. } while(0); LOCK_GIVE(cptr->hdw->big_lock);
  129. return ret;
  130. }
  131. /* Retrieve control's valid mask bits (bit mask only) */
  132. int pvr2_ctrl_get_mask(struct pvr2_ctrl *cptr)
  133. {
  134. int ret = 0;
  135. if (!cptr) return 0;
  136. LOCK_TAKE(cptr->hdw->big_lock); do {
  137. if (cptr->info->type == pvr2_ctl_bitmask) {
  138. ret = cptr->info->def.type_bitmask.valid_bits;
  139. }
  140. } while(0); LOCK_GIVE(cptr->hdw->big_lock);
  141. return ret;
  142. }
  143. /* Retrieve the control's name */
  144. const char *pvr2_ctrl_get_name(struct pvr2_ctrl *cptr)
  145. {
  146. if (!cptr) return NULL;
  147. return cptr->info->name;
  148. }
  149. /* Retrieve the control's desc */
  150. const char *pvr2_ctrl_get_desc(struct pvr2_ctrl *cptr)
  151. {
  152. if (!cptr) return NULL;
  153. return cptr->info->desc;
  154. }
  155. /* Retrieve a control enumeration or bit mask value */
  156. int pvr2_ctrl_get_valname(struct pvr2_ctrl *cptr,int val,
  157. char *bptr,unsigned int bmax,
  158. unsigned int *blen)
  159. {
  160. int ret = -EINVAL;
  161. if (!cptr) return 0;
  162. *blen = 0;
  163. LOCK_TAKE(cptr->hdw->big_lock); do {
  164. if (cptr->info->type == pvr2_ctl_enum) {
  165. const char * const *names;
  166. names = cptr->info->def.type_enum.value_names;
  167. if (pvr2_ctrl_range_check(cptr,val) == 0) {
  168. if (names[val]) {
  169. *blen = scnprintf(
  170. bptr,bmax,"%s",
  171. names[val]);
  172. } else {
  173. *blen = 0;
  174. }
  175. ret = 0;
  176. }
  177. } else if (cptr->info->type == pvr2_ctl_bitmask) {
  178. const char **names;
  179. unsigned int idx;
  180. int msk;
  181. names = cptr->info->def.type_bitmask.bit_names;
  182. val &= cptr->info->def.type_bitmask.valid_bits;
  183. for (idx = 0, msk = 1; val; idx++, msk <<= 1) {
  184. if (val & msk) {
  185. *blen = scnprintf(bptr,bmax,"%s",
  186. names[idx]);
  187. ret = 0;
  188. break;
  189. }
  190. }
  191. }
  192. } while(0); LOCK_GIVE(cptr->hdw->big_lock);
  193. return ret;
  194. }
  195. /* Return V4L ID for this control or zero if none */
  196. int pvr2_ctrl_get_v4lid(struct pvr2_ctrl *cptr)
  197. {
  198. if (!cptr) return 0;
  199. return cptr->info->v4l_id;
  200. }
  201. unsigned int pvr2_ctrl_get_v4lflags(struct pvr2_ctrl *cptr)
  202. {
  203. unsigned int flags = 0;
  204. if (cptr->info->get_v4lflags) {
  205. flags = cptr->info->get_v4lflags(cptr);
  206. }
  207. if (cptr->info->set_value) {
  208. flags &= ~V4L2_CTRL_FLAG_READ_ONLY;
  209. } else {
  210. flags |= V4L2_CTRL_FLAG_READ_ONLY;
  211. }
  212. return flags;
  213. }
  214. /* Return true if control is writable */
  215. int pvr2_ctrl_is_writable(struct pvr2_ctrl *cptr)
  216. {
  217. if (!cptr) return 0;
  218. return cptr->info->set_value != NULL;
  219. }
  220. /* Return true if control has custom symbolic representation */
  221. int pvr2_ctrl_has_custom_symbols(struct pvr2_ctrl *cptr)
  222. {
  223. if (!cptr) return 0;
  224. if (!cptr->info->val_to_sym) return 0;
  225. if (!cptr->info->sym_to_val) return 0;
  226. return !0;
  227. }
  228. /* Convert a given mask/val to a custom symbolic value */
  229. int pvr2_ctrl_custom_value_to_sym(struct pvr2_ctrl *cptr,
  230. int mask,int val,
  231. char *buf,unsigned int maxlen,
  232. unsigned int *len)
  233. {
  234. if (!cptr) return -EINVAL;
  235. if (!cptr->info->val_to_sym) return -EINVAL;
  236. return cptr->info->val_to_sym(cptr,mask,val,buf,maxlen,len);
  237. }
  238. /* Convert a symbolic value to a mask/value pair */
  239. int pvr2_ctrl_custom_sym_to_value(struct pvr2_ctrl *cptr,
  240. const char *buf,unsigned int len,
  241. int *maskptr,int *valptr)
  242. {
  243. if (!cptr) return -EINVAL;
  244. if (!cptr->info->sym_to_val) return -EINVAL;
  245. return cptr->info->sym_to_val(cptr,buf,len,maskptr,valptr);
  246. }
  247. static unsigned int gen_bitmask_string(int msk,int val,int msk_only,
  248. const char **names,
  249. char *ptr,unsigned int len)
  250. {
  251. unsigned int idx;
  252. long sm,um;
  253. int spcFl;
  254. unsigned int uc,cnt;
  255. const char *idStr;
  256. spcFl = 0;
  257. uc = 0;
  258. um = 0;
  259. for (idx = 0, sm = 1; msk; idx++, sm <<= 1) {
  260. if (sm & msk) {
  261. msk &= ~sm;
  262. idStr = names[idx];
  263. if (idStr) {
  264. cnt = scnprintf(ptr,len,"%s%s%s",
  265. (spcFl ? " " : ""),
  266. (msk_only ? "" :
  267. ((val & sm) ? "+" : "-")),
  268. idStr);
  269. ptr += cnt; len -= cnt; uc += cnt;
  270. spcFl = !0;
  271. } else {
  272. um |= sm;
  273. }
  274. }
  275. }
  276. if (um) {
  277. if (msk_only) {
  278. cnt = scnprintf(ptr,len,"%s0x%lx",
  279. (spcFl ? " " : ""),
  280. um);
  281. ptr += cnt; len -= cnt; uc += cnt;
  282. spcFl = !0;
  283. } else if (um & val) {
  284. cnt = scnprintf(ptr,len,"%s+0x%lx",
  285. (spcFl ? " " : ""),
  286. um & val);
  287. ptr += cnt; len -= cnt; uc += cnt;
  288. spcFl = !0;
  289. } else if (um & ~val) {
  290. cnt = scnprintf(ptr,len,"%s+0x%lx",
  291. (spcFl ? " " : ""),
  292. um & ~val);
  293. ptr += cnt; len -= cnt; uc += cnt;
  294. spcFl = !0;
  295. }
  296. }
  297. return uc;
  298. }
  299. static const char *boolNames[] = {
  300. "false",
  301. "true",
  302. "no",
  303. "yes",
  304. };
  305. static int parse_token(const char *ptr,unsigned int len,
  306. int *valptr,
  307. const char * const *names, unsigned int namecnt)
  308. {
  309. unsigned int slen;
  310. unsigned int idx;
  311. *valptr = 0;
  312. if (!names) namecnt = 0;
  313. for (idx = 0; idx < namecnt; idx++) {
  314. if (!names[idx]) continue;
  315. slen = strlen(names[idx]);
  316. if (slen != len) continue;
  317. if (memcmp(names[idx],ptr,slen)) continue;
  318. *valptr = idx;
  319. return 0;
  320. }
  321. return kstrtoint(ptr, 0, valptr) ? -EINVAL : 1;
  322. }
  323. static int parse_mtoken(const char *ptr,unsigned int len,
  324. int *valptr,
  325. const char **names,int valid_bits)
  326. {
  327. unsigned int slen;
  328. unsigned int idx;
  329. int msk;
  330. *valptr = 0;
  331. for (idx = 0, msk = 1; valid_bits; idx++, msk <<= 1) {
  332. if (!(msk & valid_bits)) continue;
  333. valid_bits &= ~msk;
  334. if (!names[idx]) continue;
  335. slen = strlen(names[idx]);
  336. if (slen != len) continue;
  337. if (memcmp(names[idx],ptr,slen)) continue;
  338. *valptr = msk;
  339. return 0;
  340. }
  341. return kstrtoint(ptr, 0, valptr);
  342. }
  343. static int parse_tlist(const char *ptr,unsigned int len,
  344. int *maskptr,int *valptr,
  345. const char **names,int valid_bits)
  346. {
  347. unsigned int cnt;
  348. int mask,val,kv,mode,ret;
  349. mask = 0;
  350. val = 0;
  351. ret = 0;
  352. while (len) {
  353. cnt = 0;
  354. while ((cnt < len) &&
  355. ((ptr[cnt] <= 32) ||
  356. (ptr[cnt] >= 127))) cnt++;
  357. ptr += cnt;
  358. len -= cnt;
  359. mode = 0;
  360. if ((*ptr == '-') || (*ptr == '+')) {
  361. mode = (*ptr == '-') ? -1 : 1;
  362. ptr++;
  363. len--;
  364. }
  365. cnt = 0;
  366. while (cnt < len) {
  367. if (ptr[cnt] <= 32) break;
  368. if (ptr[cnt] >= 127) break;
  369. cnt++;
  370. }
  371. if (!cnt) break;
  372. if (parse_mtoken(ptr,cnt,&kv,names,valid_bits)) {
  373. ret = -EINVAL;
  374. break;
  375. }
  376. ptr += cnt;
  377. len -= cnt;
  378. switch (mode) {
  379. case 0:
  380. mask = valid_bits;
  381. val |= kv;
  382. break;
  383. case -1:
  384. mask |= kv;
  385. val &= ~kv;
  386. break;
  387. case 1:
  388. mask |= kv;
  389. val |= kv;
  390. break;
  391. default:
  392. break;
  393. }
  394. }
  395. *maskptr = mask;
  396. *valptr = val;
  397. return ret;
  398. }
  399. /* Convert a symbolic value to a mask/value pair */
  400. int pvr2_ctrl_sym_to_value(struct pvr2_ctrl *cptr,
  401. const char *ptr,unsigned int len,
  402. int *maskptr,int *valptr)
  403. {
  404. int ret = -EINVAL;
  405. unsigned int cnt;
  406. *maskptr = 0;
  407. *valptr = 0;
  408. cnt = 0;
  409. while ((cnt < len) && ((ptr[cnt] <= 32) || (ptr[cnt] >= 127))) cnt++;
  410. len -= cnt; ptr += cnt;
  411. cnt = 0;
  412. while ((cnt < len) && ((ptr[len-(cnt+1)] <= 32) ||
  413. (ptr[len-(cnt+1)] >= 127))) cnt++;
  414. len -= cnt;
  415. if (!len) return -EINVAL;
  416. LOCK_TAKE(cptr->hdw->big_lock); do {
  417. if (cptr->info->type == pvr2_ctl_int) {
  418. ret = parse_token(ptr,len,valptr,NULL,0);
  419. if (ret >= 0) {
  420. ret = pvr2_ctrl_range_check(cptr,*valptr);
  421. }
  422. *maskptr = ~0;
  423. } else if (cptr->info->type == pvr2_ctl_bool) {
  424. ret = parse_token(ptr,len,valptr,boolNames,
  425. ARRAY_SIZE(boolNames));
  426. if (ret == 1) {
  427. *valptr = *valptr ? !0 : 0;
  428. } else if (ret == 0) {
  429. *valptr = (*valptr & 1) ? !0 : 0;
  430. }
  431. *maskptr = 1;
  432. } else if (cptr->info->type == pvr2_ctl_enum) {
  433. ret = parse_token(
  434. ptr,len,valptr,
  435. cptr->info->def.type_enum.value_names,
  436. cptr->info->def.type_enum.count);
  437. if (ret >= 0) {
  438. ret = pvr2_ctrl_range_check(cptr,*valptr);
  439. }
  440. *maskptr = ~0;
  441. } else if (cptr->info->type == pvr2_ctl_bitmask) {
  442. ret = parse_tlist(
  443. ptr,len,maskptr,valptr,
  444. cptr->info->def.type_bitmask.bit_names,
  445. cptr->info->def.type_bitmask.valid_bits);
  446. }
  447. } while(0); LOCK_GIVE(cptr->hdw->big_lock);
  448. return ret;
  449. }
  450. /* Convert a given mask/val to a symbolic value */
  451. int pvr2_ctrl_value_to_sym_internal(struct pvr2_ctrl *cptr,
  452. int mask,int val,
  453. char *buf,unsigned int maxlen,
  454. unsigned int *len)
  455. {
  456. int ret = -EINVAL;
  457. *len = 0;
  458. if (cptr->info->type == pvr2_ctl_int) {
  459. *len = scnprintf(buf,maxlen,"%d",val);
  460. ret = 0;
  461. } else if (cptr->info->type == pvr2_ctl_bool) {
  462. *len = scnprintf(buf,maxlen,"%s",val ? "true" : "false");
  463. ret = 0;
  464. } else if (cptr->info->type == pvr2_ctl_enum) {
  465. const char * const *names;
  466. names = cptr->info->def.type_enum.value_names;
  467. if ((val >= 0) &&
  468. (val < cptr->info->def.type_enum.count)) {
  469. if (names[val]) {
  470. *len = scnprintf(
  471. buf,maxlen,"%s",
  472. names[val]);
  473. } else {
  474. *len = 0;
  475. }
  476. ret = 0;
  477. }
  478. } else if (cptr->info->type == pvr2_ctl_bitmask) {
  479. *len = gen_bitmask_string(
  480. val & mask & cptr->info->def.type_bitmask.valid_bits,
  481. ~0,!0,
  482. cptr->info->def.type_bitmask.bit_names,
  483. buf,maxlen);
  484. }
  485. return ret;
  486. }
  487. /* Convert a given mask/val to a symbolic value */
  488. int pvr2_ctrl_value_to_sym(struct pvr2_ctrl *cptr,
  489. int mask,int val,
  490. char *buf,unsigned int maxlen,
  491. unsigned int *len)
  492. {
  493. int ret;
  494. LOCK_TAKE(cptr->hdw->big_lock); do {
  495. ret = pvr2_ctrl_value_to_sym_internal(cptr,mask,val,
  496. buf,maxlen,len);
  497. } while(0); LOCK_GIVE(cptr->hdw->big_lock);
  498. return ret;
  499. }