4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
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
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.
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
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>
28 static int pvr2_ctrl_range_check(struct pvr2_ctrl *cptr,int val)
30 if (cptr->info->check_value) {
31 if (!cptr->info->check_value(cptr,val)) return -ERANGE;
32 } else if (cptr->info->type == pvr2_ctl_enum) {
33 if (val < 0) return -ERANGE;
34 if (val >= cptr->info->def.type_enum.count) return -ERANGE;
37 lim = cptr->info->def.type_int.min_value;
38 if (cptr->info->get_min_value) {
39 cptr->info->get_min_value(cptr,&lim);
41 if (val < lim) return -ERANGE;
42 lim = cptr->info->def.type_int.max_value;
43 if (cptr->info->get_max_value) {
44 cptr->info->get_max_value(cptr,&lim);
46 if (val > lim) return -ERANGE;
52 /* Set the given control. */
53 int pvr2_ctrl_set_value(struct pvr2_ctrl *cptr,int val)
55 return pvr2_ctrl_set_mask_value(cptr,~0,val);
59 /* Set/clear specific bits of the given control. */
60 int pvr2_ctrl_set_mask_value(struct pvr2_ctrl *cptr,int mask,int val)
63 if (!cptr) return -EINVAL;
64 LOCK_TAKE(cptr->hdw->big_lock); do {
65 if (cptr->info->set_value) {
66 if (cptr->info->type == pvr2_ctl_bitmask) {
67 mask &= cptr->info->def.type_bitmask.valid_bits;
68 } else if ((cptr->info->type == pvr2_ctl_int)||
69 (cptr->info->type == pvr2_ctl_enum)) {
70 ret = pvr2_ctrl_range_check(cptr,val);
72 } else if (cptr->info->type != pvr2_ctl_bool) {
75 ret = cptr->info->set_value(cptr,mask,val);
79 } while(0); LOCK_GIVE(cptr->hdw->big_lock);
84 /* Get the current value of the given control. */
85 int pvr2_ctrl_get_value(struct pvr2_ctrl *cptr,int *valptr)
88 if (!cptr) return -EINVAL;
89 LOCK_TAKE(cptr->hdw->big_lock); do {
90 ret = cptr->info->get_value(cptr,valptr);
91 } while(0); LOCK_GIVE(cptr->hdw->big_lock);
96 /* Retrieve control's type */
97 enum pvr2_ctl_type pvr2_ctrl_get_type(struct pvr2_ctrl *cptr)
99 if (!cptr) return pvr2_ctl_int;
100 return cptr->info->type;
104 /* Retrieve control's maximum value (int type) */
105 int pvr2_ctrl_get_max(struct pvr2_ctrl *cptr)
109 LOCK_TAKE(cptr->hdw->big_lock); do {
110 if (cptr->info->get_max_value) {
111 cptr->info->get_max_value(cptr,&ret);
112 } else if (cptr->info->type == pvr2_ctl_int) {
113 ret = cptr->info->def.type_int.max_value;
115 } while(0); LOCK_GIVE(cptr->hdw->big_lock);
120 /* Retrieve control's minimum value (int type) */
121 int pvr2_ctrl_get_min(struct pvr2_ctrl *cptr)
125 LOCK_TAKE(cptr->hdw->big_lock); do {
126 if (cptr->info->get_min_value) {
127 cptr->info->get_min_value(cptr,&ret);
128 } else if (cptr->info->type == pvr2_ctl_int) {
129 ret = cptr->info->def.type_int.min_value;
131 } while(0); LOCK_GIVE(cptr->hdw->big_lock);
136 /* Retrieve control's default value (any type) */
137 int pvr2_ctrl_get_def(struct pvr2_ctrl *cptr, int *valptr)
141 LOCK_TAKE(cptr->hdw->big_lock); do {
142 if (cptr->info->type == pvr2_ctl_int) {
143 if (cptr->info->get_def_value) {
144 ret = cptr->info->get_def_value(cptr, valptr);
146 *valptr = cptr->info->default_value;
149 } while(0); LOCK_GIVE(cptr->hdw->big_lock);
154 /* Retrieve control's enumeration count (enum only) */
155 int pvr2_ctrl_get_cnt(struct pvr2_ctrl *cptr)
159 LOCK_TAKE(cptr->hdw->big_lock); do {
160 if (cptr->info->type == pvr2_ctl_enum) {
161 ret = cptr->info->def.type_enum.count;
163 } while(0); LOCK_GIVE(cptr->hdw->big_lock);
168 /* Retrieve control's valid mask bits (bit mask only) */
169 int pvr2_ctrl_get_mask(struct pvr2_ctrl *cptr)
173 LOCK_TAKE(cptr->hdw->big_lock); do {
174 if (cptr->info->type == pvr2_ctl_bitmask) {
175 ret = cptr->info->def.type_bitmask.valid_bits;
177 } while(0); LOCK_GIVE(cptr->hdw->big_lock);
182 /* Retrieve the control's name */
183 const char *pvr2_ctrl_get_name(struct pvr2_ctrl *cptr)
185 if (!cptr) return NULL;
186 return cptr->info->name;
190 /* Retrieve the control's desc */
191 const char *pvr2_ctrl_get_desc(struct pvr2_ctrl *cptr)
193 if (!cptr) return NULL;
194 return cptr->info->desc;
198 /* Retrieve a control enumeration or bit mask value */
199 int pvr2_ctrl_get_valname(struct pvr2_ctrl *cptr,int val,
200 char *bptr,unsigned int bmax,
206 LOCK_TAKE(cptr->hdw->big_lock); do {
207 if (cptr->info->type == pvr2_ctl_enum) {
209 names = cptr->info->def.type_enum.value_names;
210 if (pvr2_ctrl_range_check(cptr,val) == 0) {
220 } else if (cptr->info->type == pvr2_ctl_bitmask) {
224 names = cptr->info->def.type_bitmask.bit_names;
225 val &= cptr->info->def.type_bitmask.valid_bits;
226 for (idx = 0, msk = 1; val; idx++, msk <<= 1) {
228 *blen = scnprintf(bptr,bmax,"%s",
235 } while(0); LOCK_GIVE(cptr->hdw->big_lock);
240 /* Return V4L ID for this control or zero if none */
241 int pvr2_ctrl_get_v4lid(struct pvr2_ctrl *cptr)
244 return cptr->info->v4l_id;
248 unsigned int pvr2_ctrl_get_v4lflags(struct pvr2_ctrl *cptr)
250 unsigned int flags = 0;
252 if (cptr->info->get_v4lflags) {
253 flags = cptr->info->get_v4lflags(cptr);
256 if (cptr->info->set_value) {
257 flags &= ~V4L2_CTRL_FLAG_READ_ONLY;
259 flags |= V4L2_CTRL_FLAG_READ_ONLY;
266 /* Return true if control is writable */
267 int pvr2_ctrl_is_writable(struct pvr2_ctrl *cptr)
270 return cptr->info->set_value != NULL;
274 /* Return true if control has custom symbolic representation */
275 int pvr2_ctrl_has_custom_symbols(struct pvr2_ctrl *cptr)
278 if (!cptr->info->val_to_sym) return 0;
279 if (!cptr->info->sym_to_val) return 0;
284 /* Convert a given mask/val to a custom symbolic value */
285 int pvr2_ctrl_custom_value_to_sym(struct pvr2_ctrl *cptr,
287 char *buf,unsigned int maxlen,
290 if (!cptr) return -EINVAL;
291 if (!cptr->info->val_to_sym) return -EINVAL;
292 return cptr->info->val_to_sym(cptr,mask,val,buf,maxlen,len);
296 /* Convert a symbolic value to a mask/value pair */
297 int pvr2_ctrl_custom_sym_to_value(struct pvr2_ctrl *cptr,
298 const char *buf,unsigned int len,
299 int *maskptr,int *valptr)
301 if (!cptr) return -EINVAL;
302 if (!cptr->info->sym_to_val) return -EINVAL;
303 return cptr->info->sym_to_val(cptr,buf,len,maskptr,valptr);
307 static unsigned int gen_bitmask_string(int msk,int val,int msk_only,
309 char *ptr,unsigned int len)
320 for (idx = 0, sm = 1; msk; idx++, sm <<= 1) {
325 cnt = scnprintf(ptr,len,"%s%s%s",
328 ((val & sm) ? "+" : "-")),
330 ptr += cnt; len -= cnt; uc += cnt;
339 cnt = scnprintf(ptr,len,"%s0x%lx",
342 ptr += cnt; len -= cnt; uc += cnt;
344 } else if (um & val) {
345 cnt = scnprintf(ptr,len,"%s+0x%lx",
348 ptr += cnt; len -= cnt; uc += cnt;
350 } else if (um & ~val) {
351 cnt = scnprintf(ptr,len,"%s+0x%lx",
354 ptr += cnt; len -= cnt; uc += cnt;
362 static const char *boolNames[] = {
370 static int parse_token(const char *ptr,unsigned int len,
372 const char **names,unsigned int namecnt)
380 if (!names) namecnt = 0;
381 for (idx = 0; idx < namecnt; idx++) {
382 if (!names[idx]) continue;
383 slen = strlen(names[idx]);
384 if (slen != len) continue;
385 if (memcmp(names[idx],ptr,slen)) continue;
390 if ((*ptr == '-') || (*ptr == '+')) {
391 negfl = (*ptr == '-');
394 if (len >= sizeof(buf)) return -EINVAL;
397 *valptr = simple_strtol(buf,&p2,0);
398 if (negfl) *valptr = -(*valptr);
399 if (*p2) return -EINVAL;
404 static int parse_mtoken(const char *ptr,unsigned int len,
406 const char **names,int valid_bits)
414 for (idx = 0, msk = 1; valid_bits; idx++, msk <<= 1) {
415 if (!(msk & valid_bits)) continue;
417 if (!names[idx]) continue;
418 slen = strlen(names[idx]);
419 if (slen != len) continue;
420 if (memcmp(names[idx],ptr,slen)) continue;
424 if (len >= sizeof(buf)) return -EINVAL;
427 *valptr = simple_strtol(buf,&p2,0);
428 if (*p2) return -EINVAL;
433 static int parse_tlist(const char *ptr,unsigned int len,
434 int *maskptr,int *valptr,
435 const char **names,int valid_bits)
438 int mask,val,kv,mode,ret;
444 while ((cnt < len) &&
446 (ptr[cnt] >= 127))) cnt++;
450 if ((*ptr == '-') || (*ptr == '+')) {
451 mode = (*ptr == '-') ? -1 : 1;
457 if (ptr[cnt] <= 32) break;
458 if (ptr[cnt] >= 127) break;
462 if (parse_mtoken(ptr,cnt,&kv,names,valid_bits)) {
491 /* Convert a symbolic value to a mask/value pair */
492 int pvr2_ctrl_sym_to_value(struct pvr2_ctrl *cptr,
493 const char *ptr,unsigned int len,
494 int *maskptr,int *valptr)
503 while ((cnt < len) && ((ptr[cnt] <= 32) || (ptr[cnt] >= 127))) cnt++;
504 len -= cnt; ptr += cnt;
506 while ((cnt < len) && ((ptr[len-(cnt+1)] <= 32) ||
507 (ptr[len-(cnt+1)] >= 127))) cnt++;
510 if (!len) return -EINVAL;
512 LOCK_TAKE(cptr->hdw->big_lock); do {
513 if (cptr->info->type == pvr2_ctl_int) {
514 ret = parse_token(ptr,len,valptr,NULL,0);
516 ret = pvr2_ctrl_range_check(cptr,*valptr);
518 if (maskptr) *maskptr = ~0;
519 } else if (cptr->info->type == pvr2_ctl_bool) {
520 ret = parse_token(ptr,len,valptr,boolNames,
521 ARRAY_SIZE(boolNames));
523 *valptr = *valptr ? !0 : 0;
524 } else if (ret == 0) {
525 *valptr = (*valptr & 1) ? !0 : 0;
527 if (maskptr) *maskptr = 1;
528 } else if (cptr->info->type == pvr2_ctl_enum) {
531 cptr->info->def.type_enum.value_names,
532 cptr->info->def.type_enum.count);
534 ret = pvr2_ctrl_range_check(cptr,*valptr);
536 if (maskptr) *maskptr = ~0;
537 } else if (cptr->info->type == pvr2_ctl_bitmask) {
539 ptr,len,maskptr,valptr,
540 cptr->info->def.type_bitmask.bit_names,
541 cptr->info->def.type_bitmask.valid_bits);
543 } while(0); LOCK_GIVE(cptr->hdw->big_lock);
548 /* Convert a given mask/val to a symbolic value */
549 int pvr2_ctrl_value_to_sym_internal(struct pvr2_ctrl *cptr,
551 char *buf,unsigned int maxlen,
557 if (cptr->info->type == pvr2_ctl_int) {
558 *len = scnprintf(buf,maxlen,"%d",val);
560 } else if (cptr->info->type == pvr2_ctl_bool) {
561 *len = scnprintf(buf,maxlen,"%s",val ? "true" : "false");
563 } else if (cptr->info->type == pvr2_ctl_enum) {
565 names = cptr->info->def.type_enum.value_names;
567 (val < cptr->info->def.type_enum.count)) {
577 } else if (cptr->info->type == pvr2_ctl_bitmask) {
578 *len = gen_bitmask_string(
579 val & mask & cptr->info->def.type_bitmask.valid_bits,
581 cptr->info->def.type_bitmask.bit_names,
588 /* Convert a given mask/val to a symbolic value */
589 int pvr2_ctrl_value_to_sym(struct pvr2_ctrl *cptr,
591 char *buf,unsigned int maxlen,
595 LOCK_TAKE(cptr->hdw->big_lock); do {
596 ret = pvr2_ctrl_value_to_sym_internal(cptr,mask,val,
598 } while(0); LOCK_GIVE(cptr->hdw->big_lock);
604 Stuff for Emacs to see, in order to encourage consistent editing style:
605 *** Local Variables: ***
607 *** fill-column: 75 ***
609 *** c-basic-offset: 8 ***