5 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
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
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.
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
22 #include "pvrusb2-ctrl.h"
23 #include "pvrusb2-hdw-internal.h"
24 #include <linux/errno.h>
25 #include <linux/string.h>
26 #include <linux/mutex.h>
29 /* Set the given control. */
30 int pvr2_ctrl_set_value(struct pvr2_ctrl *cptr,int val)
32 return pvr2_ctrl_set_mask_value(cptr,~0,val);
36 /* Set/clear specific bits of the given control. */
37 int pvr2_ctrl_set_mask_value(struct pvr2_ctrl *cptr,int mask,int val)
40 if (!cptr) return -EINVAL;
41 LOCK_TAKE(cptr->hdw->big_lock); do {
42 if (cptr->info->set_value != 0) {
43 if (cptr->info->type == pvr2_ctl_bitmask) {
44 mask &= cptr->info->def.type_bitmask.valid_bits;
45 } else if (cptr->info->type == pvr2_ctl_int) {
47 lim = cptr->info->def.type_int.min_value;
48 if (cptr->info->get_min_value) {
49 cptr->info->get_min_value(cptr,&lim);
52 lim = cptr->info->def.type_int.max_value;
53 if (cptr->info->get_max_value) {
54 cptr->info->get_max_value(cptr,&lim);
57 } else if (cptr->info->type == pvr2_ctl_enum) {
58 if (val >= cptr->info->def.type_enum.count) {
61 } else if (cptr->info->type != pvr2_ctl_bool) {
64 ret = cptr->info->set_value(cptr,mask,val);
68 } while(0); LOCK_GIVE(cptr->hdw->big_lock);
73 /* Get the current value of the given control. */
74 int pvr2_ctrl_get_value(struct pvr2_ctrl *cptr,int *valptr)
77 if (!cptr) return -EINVAL;
78 LOCK_TAKE(cptr->hdw->big_lock); do {
79 ret = cptr->info->get_value(cptr,valptr);
80 } while(0); LOCK_GIVE(cptr->hdw->big_lock);
85 /* Retrieve control's type */
86 enum pvr2_ctl_type pvr2_ctrl_get_type(struct pvr2_ctrl *cptr)
88 if (!cptr) return pvr2_ctl_int;
89 return cptr->info->type;
93 /* Retrieve control's maximum value (int type) */
94 int pvr2_ctrl_get_max(struct pvr2_ctrl *cptr)
98 LOCK_TAKE(cptr->hdw->big_lock); do {
99 if (cptr->info->get_max_value) {
100 cptr->info->get_max_value(cptr,&ret);
101 } else if (cptr->info->type == pvr2_ctl_int) {
102 ret = cptr->info->def.type_int.max_value;
104 } while(0); LOCK_GIVE(cptr->hdw->big_lock);
109 /* Retrieve control's minimum value (int type) */
110 int pvr2_ctrl_get_min(struct pvr2_ctrl *cptr)
114 LOCK_TAKE(cptr->hdw->big_lock); do {
115 if (cptr->info->get_min_value) {
116 cptr->info->get_min_value(cptr,&ret);
117 } else if (cptr->info->type == pvr2_ctl_int) {
118 ret = cptr->info->def.type_int.min_value;
120 } while(0); LOCK_GIVE(cptr->hdw->big_lock);
125 /* Retrieve control's default value (any type) */
126 int pvr2_ctrl_get_def(struct pvr2_ctrl *cptr)
130 LOCK_TAKE(cptr->hdw->big_lock); do {
131 if (cptr->info->type == pvr2_ctl_int) {
132 ret = cptr->info->default_value;
134 } while(0); LOCK_GIVE(cptr->hdw->big_lock);
139 /* Retrieve control's enumeration count (enum only) */
140 int pvr2_ctrl_get_cnt(struct pvr2_ctrl *cptr)
144 LOCK_TAKE(cptr->hdw->big_lock); do {
145 if (cptr->info->type == pvr2_ctl_enum) {
146 ret = cptr->info->def.type_enum.count;
148 } while(0); LOCK_GIVE(cptr->hdw->big_lock);
153 /* Retrieve control's valid mask bits (bit mask only) */
154 int pvr2_ctrl_get_mask(struct pvr2_ctrl *cptr)
158 LOCK_TAKE(cptr->hdw->big_lock); do {
159 if (cptr->info->type == pvr2_ctl_bitmask) {
160 ret = cptr->info->def.type_bitmask.valid_bits;
162 } while(0); LOCK_GIVE(cptr->hdw->big_lock);
167 /* Retrieve the control's name */
168 const char *pvr2_ctrl_get_name(struct pvr2_ctrl *cptr)
170 if (!cptr) return NULL;
171 return cptr->info->name;
175 /* Retrieve the control's desc */
176 const char *pvr2_ctrl_get_desc(struct pvr2_ctrl *cptr)
178 if (!cptr) return NULL;
179 return cptr->info->desc;
183 /* Retrieve a control enumeration or bit mask value */
184 int pvr2_ctrl_get_valname(struct pvr2_ctrl *cptr,int val,
185 char *bptr,unsigned int bmax,
191 LOCK_TAKE(cptr->hdw->big_lock); do {
192 if (cptr->info->type == pvr2_ctl_enum) {
194 names = cptr->info->def.type_enum.value_names;
196 (val < cptr->info->def.type_enum.count)) {
206 } else if (cptr->info->type == pvr2_ctl_bitmask) {
210 names = cptr->info->def.type_bitmask.bit_names;
211 val &= cptr->info->def.type_bitmask.valid_bits;
212 for (idx = 0, msk = 1; val; idx++, msk <<= 1) {
214 *blen = scnprintf(bptr,bmax,"%s",
221 } while(0); LOCK_GIVE(cptr->hdw->big_lock);
226 /* Return V4L ID for this control or zero if none */
227 int pvr2_ctrl_get_v4lid(struct pvr2_ctrl *cptr)
230 return cptr->info->v4l_id;
234 unsigned int pvr2_ctrl_get_v4lflags(struct pvr2_ctrl *cptr)
236 unsigned int flags = 0;
238 if (cptr->info->get_v4lflags) {
239 flags = cptr->info->get_v4lflags(cptr);
242 if (cptr->info->set_value) {
243 flags &= ~V4L2_CTRL_FLAG_READ_ONLY;
245 flags |= V4L2_CTRL_FLAG_READ_ONLY;
252 /* Return true if control is writable */
253 int pvr2_ctrl_is_writable(struct pvr2_ctrl *cptr)
256 return cptr->info->set_value != 0;
260 /* Return true if control has custom symbolic representation */
261 int pvr2_ctrl_has_custom_symbols(struct pvr2_ctrl *cptr)
264 if (!cptr->info->val_to_sym) return 0;
265 if (!cptr->info->sym_to_val) return 0;
270 /* Convert a given mask/val to a custom symbolic value */
271 int pvr2_ctrl_custom_value_to_sym(struct pvr2_ctrl *cptr,
273 char *buf,unsigned int maxlen,
276 if (!cptr) return -EINVAL;
277 if (!cptr->info->val_to_sym) return -EINVAL;
278 return cptr->info->val_to_sym(cptr,mask,val,buf,maxlen,len);
282 /* Convert a symbolic value to a mask/value pair */
283 int pvr2_ctrl_custom_sym_to_value(struct pvr2_ctrl *cptr,
284 const char *buf,unsigned int len,
285 int *maskptr,int *valptr)
287 if (!cptr) return -EINVAL;
288 if (!cptr->info->sym_to_val) return -EINVAL;
289 return cptr->info->sym_to_val(cptr,buf,len,maskptr,valptr);
293 static unsigned int gen_bitmask_string(int msk,int val,int msk_only,
295 char *ptr,unsigned int len)
306 for (idx = 0, sm = 1; msk; idx++, sm <<= 1) {
311 cnt = scnprintf(ptr,len,"%s%s%s",
314 ((val & sm) ? "+" : "-")),
316 ptr += cnt; len -= cnt; uc += cnt;
325 cnt = scnprintf(ptr,len,"%s0x%lx",
328 ptr += cnt; len -= cnt; uc += cnt;
330 } else if (um & val) {
331 cnt = scnprintf(ptr,len,"%s+0x%lx",
334 ptr += cnt; len -= cnt; uc += cnt;
336 } else if (um & ~val) {
337 cnt = scnprintf(ptr,len,"%s+0x%lx",
340 ptr += cnt; len -= cnt; uc += cnt;
348 static const char *boolNames[] = {
356 static int parse_token(const char *ptr,unsigned int len,
358 const char **names,unsigned int namecnt)
366 if (!names) namecnt = 0;
367 for (idx = 0; idx < namecnt; idx++) {
368 if (!names[idx]) continue;
369 slen = strlen(names[idx]);
370 if (slen != len) continue;
371 if (memcmp(names[idx],ptr,slen)) continue;
376 if ((*ptr == '-') || (*ptr == '+')) {
377 negfl = (*ptr == '-');
380 if (len >= sizeof(buf)) return -EINVAL;
383 *valptr = simple_strtol(buf,&p2,0);
384 if (negfl) *valptr = -(*valptr);
385 if (*p2) return -EINVAL;
390 static int parse_mtoken(const char *ptr,unsigned int len,
392 const char **names,int valid_bits)
400 for (idx = 0, msk = 1; valid_bits; idx++, msk <<= 1) {
401 if (!msk & valid_bits) continue;
403 if (!names[idx]) continue;
404 slen = strlen(names[idx]);
405 if (slen != len) continue;
406 if (memcmp(names[idx],ptr,slen)) continue;
410 if (len >= sizeof(buf)) return -EINVAL;
413 *valptr = simple_strtol(buf,&p2,0);
414 if (*p2) return -EINVAL;
419 static int parse_tlist(const char *ptr,unsigned int len,
420 int *maskptr,int *valptr,
421 const char **names,int valid_bits)
424 int mask,val,kv,mode,ret;
430 while ((cnt < len) &&
432 (ptr[cnt] >= 127))) cnt++;
436 if ((*ptr == '-') || (*ptr == '+')) {
437 mode = (*ptr == '-') ? -1 : 1;
443 if (ptr[cnt] <= 32) break;
444 if (ptr[cnt] >= 127) break;
448 if (parse_mtoken(ptr,cnt,&kv,names,valid_bits)) {
477 /* Convert a symbolic value to a mask/value pair */
478 int pvr2_ctrl_sym_to_value(struct pvr2_ctrl *cptr,
479 const char *ptr,unsigned int len,
480 int *maskptr,int *valptr)
489 while ((cnt < len) && ((ptr[cnt] <= 32) || (ptr[cnt] >= 127))) cnt++;
490 len -= cnt; ptr += cnt;
492 while ((cnt < len) && ((ptr[len-(cnt+1)] <= 32) ||
493 (ptr[len-(cnt+1)] >= 127))) cnt++;
496 if (!len) return -EINVAL;
498 LOCK_TAKE(cptr->hdw->big_lock); do {
499 if (cptr->info->type == pvr2_ctl_int) {
500 ret = parse_token(ptr,len,valptr,NULL,0);
502 ((*valptr < cptr->info->def.type_int.min_value) ||
503 (*valptr > cptr->info->def.type_int.max_value))) {
506 if (maskptr) *maskptr = ~0;
507 } else if (cptr->info->type == pvr2_ctl_bool) {
509 ptr,len,valptr,boolNames,
510 sizeof(boolNames)/sizeof(boolNames[0]));
512 *valptr = *valptr ? !0 : 0;
513 } else if (ret == 0) {
514 *valptr = (*valptr & 1) ? !0 : 0;
516 if (maskptr) *maskptr = 1;
517 } else if (cptr->info->type == pvr2_ctl_enum) {
520 cptr->info->def.type_enum.value_names,
521 cptr->info->def.type_enum.count);
524 (*valptr >= cptr->info->def.type_enum.count))) {
527 if (maskptr) *maskptr = ~0;
528 } else if (cptr->info->type == pvr2_ctl_bitmask) {
530 ptr,len,maskptr,valptr,
531 cptr->info->def.type_bitmask.bit_names,
532 cptr->info->def.type_bitmask.valid_bits);
534 } while(0); LOCK_GIVE(cptr->hdw->big_lock);
539 /* Convert a given mask/val to a symbolic value */
540 int pvr2_ctrl_value_to_sym_internal(struct pvr2_ctrl *cptr,
542 char *buf,unsigned int maxlen,
548 if (cptr->info->type == pvr2_ctl_int) {
549 *len = scnprintf(buf,maxlen,"%d",val);
551 } else if (cptr->info->type == pvr2_ctl_bool) {
552 *len = scnprintf(buf,maxlen,"%s",val ? "true" : "false");
554 } else if (cptr->info->type == pvr2_ctl_enum) {
556 names = cptr->info->def.type_enum.value_names;
558 (val < cptr->info->def.type_enum.count)) {
568 } else if (cptr->info->type == pvr2_ctl_bitmask) {
569 *len = gen_bitmask_string(
570 val & mask & cptr->info->def.type_bitmask.valid_bits,
572 cptr->info->def.type_bitmask.bit_names,
579 /* Convert a given mask/val to a symbolic value */
580 int pvr2_ctrl_value_to_sym(struct pvr2_ctrl *cptr,
582 char *buf,unsigned int maxlen,
586 LOCK_TAKE(cptr->hdw->big_lock); do {
587 ret = pvr2_ctrl_value_to_sym_internal(cptr,mask,val,
589 } while(0); LOCK_GIVE(cptr->hdw->big_lock);
595 Stuff for Emacs to see, in order to encourage consistent editing style:
596 *** Local Variables: ***
598 *** fill-column: 75 ***
600 *** c-basic-offset: 8 ***