Add linux-next specific files for 20110831
[linux-2.6/next.git] / drivers / media / video / pvrusb2 / pvrusb2-v4l2.c
blob6811bb91ee276a2e94956cdede5800a1f39410bd
1 /*
4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
5 * Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
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 <linux/kernel.h>
23 #include <linux/slab.h>
24 #include <linux/version.h>
25 #include "pvrusb2-context.h"
26 #include "pvrusb2-hdw.h"
27 #include "pvrusb2.h"
28 #include "pvrusb2-debug.h"
29 #include "pvrusb2-v4l2.h"
30 #include "pvrusb2-ioread.h"
31 #include <linux/videodev2.h>
32 #include <linux/module.h>
33 #include <media/v4l2-dev.h>
34 #include <media/v4l2-common.h>
35 #include <media/v4l2-ioctl.h>
37 struct pvr2_v4l2_dev;
38 struct pvr2_v4l2_fh;
39 struct pvr2_v4l2;
41 struct pvr2_v4l2_dev {
42 struct video_device devbase; /* MUST be first! */
43 struct pvr2_v4l2 *v4lp;
44 struct pvr2_context_stream *stream;
45 /* Information about this device: */
46 enum pvr2_config config; /* Expected stream format */
47 int v4l_type; /* V4L defined type for this device node */
48 enum pvr2_v4l_type minor_type; /* pvr2-understood minor device type */
51 struct pvr2_v4l2_fh {
52 struct pvr2_channel channel;
53 struct pvr2_v4l2_dev *pdi;
54 enum v4l2_priority prio;
55 struct pvr2_ioread *rhp;
56 struct file *file;
57 struct pvr2_v4l2 *vhead;
58 struct pvr2_v4l2_fh *vnext;
59 struct pvr2_v4l2_fh *vprev;
60 wait_queue_head_t wait_data;
61 int fw_mode_flag;
62 /* Map contiguous ordinal value to input id */
63 unsigned char *input_map;
64 unsigned int input_cnt;
67 struct pvr2_v4l2 {
68 struct pvr2_channel channel;
69 struct pvr2_v4l2_fh *vfirst;
70 struct pvr2_v4l2_fh *vlast;
72 struct v4l2_prio_state prio;
74 /* streams - Note that these must be separately, individually,
75 * allocated pointers. This is because the v4l core is going to
76 * manage their deletion - separately, individually... */
77 struct pvr2_v4l2_dev *dev_video;
78 struct pvr2_v4l2_dev *dev_radio;
81 static int video_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
82 module_param_array(video_nr, int, NULL, 0444);
83 MODULE_PARM_DESC(video_nr, "Offset for device's video dev minor");
84 static int radio_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
85 module_param_array(radio_nr, int, NULL, 0444);
86 MODULE_PARM_DESC(radio_nr, "Offset for device's radio dev minor");
87 static int vbi_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
88 module_param_array(vbi_nr, int, NULL, 0444);
89 MODULE_PARM_DESC(vbi_nr, "Offset for device's vbi dev minor");
91 static struct v4l2_capability pvr_capability ={
92 .driver = "pvrusb2",
93 .card = "Hauppauge WinTV pvr-usb2",
94 .bus_info = "usb",
95 .version = LINUX_VERSION_CODE,
96 .capabilities = (V4L2_CAP_VIDEO_CAPTURE |
97 V4L2_CAP_TUNER | V4L2_CAP_AUDIO | V4L2_CAP_RADIO |
98 V4L2_CAP_READWRITE),
99 .reserved = {0,0,0,0}
102 static struct v4l2_fmtdesc pvr_fmtdesc [] = {
104 .index = 0,
105 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
106 .flags = V4L2_FMT_FLAG_COMPRESSED,
107 .description = "MPEG1/2",
108 // This should really be V4L2_PIX_FMT_MPEG, but xawtv
109 // breaks when I do that.
110 .pixelformat = 0, // V4L2_PIX_FMT_MPEG,
111 .reserved = { 0, 0, 0, 0 }
115 #define PVR_FORMAT_PIX 0
116 #define PVR_FORMAT_VBI 1
118 static struct v4l2_format pvr_format [] = {
119 [PVR_FORMAT_PIX] = {
120 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
121 .fmt = {
122 .pix = {
123 .width = 720,
124 .height = 576,
125 // This should really be V4L2_PIX_FMT_MPEG,
126 // but xawtv breaks when I do that.
127 .pixelformat = 0, // V4L2_PIX_FMT_MPEG,
128 .field = V4L2_FIELD_INTERLACED,
129 .bytesperline = 0, // doesn't make sense
130 // here
131 //FIXME : Don't know what to put here...
132 .sizeimage = (32*1024),
133 .colorspace = 0, // doesn't make sense here
134 .priv = 0
138 [PVR_FORMAT_VBI] = {
139 .type = V4L2_BUF_TYPE_VBI_CAPTURE,
140 .fmt = {
141 .vbi = {
142 .sampling_rate = 27000000,
143 .offset = 248,
144 .samples_per_line = 1443,
145 .sample_format = V4L2_PIX_FMT_GREY,
146 .start = { 0, 0 },
147 .count = { 0, 0 },
148 .flags = 0,
149 .reserved = { 0, 0 }
157 * pvr_ioctl()
159 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
162 static long pvr2_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
164 struct pvr2_v4l2_fh *fh = file->private_data;
165 struct pvr2_v4l2 *vp = fh->vhead;
166 struct pvr2_v4l2_dev *pdi = fh->pdi;
167 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
168 long ret = -EINVAL;
170 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
171 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),cmd);
174 if (!pvr2_hdw_dev_ok(hdw)) {
175 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
176 "ioctl failed - bad or no context");
177 return -EFAULT;
180 /* check priority */
181 switch (cmd) {
182 case VIDIOC_S_CTRL:
183 case VIDIOC_S_STD:
184 case VIDIOC_S_INPUT:
185 case VIDIOC_S_TUNER:
186 case VIDIOC_S_FREQUENCY:
187 ret = v4l2_prio_check(&vp->prio, fh->prio);
188 if (ret)
189 return ret;
192 switch (cmd) {
193 case VIDIOC_QUERYCAP:
195 struct v4l2_capability *cap = arg;
197 memcpy(cap, &pvr_capability, sizeof(struct v4l2_capability));
198 strlcpy(cap->bus_info,pvr2_hdw_get_bus_info(hdw),
199 sizeof(cap->bus_info));
200 strlcpy(cap->card,pvr2_hdw_get_desc(hdw),sizeof(cap->card));
202 ret = 0;
203 break;
206 case VIDIOC_G_PRIORITY:
208 enum v4l2_priority *p = arg;
210 *p = v4l2_prio_max(&vp->prio);
211 ret = 0;
212 break;
215 case VIDIOC_S_PRIORITY:
217 enum v4l2_priority *prio = arg;
219 ret = v4l2_prio_change(&vp->prio, &fh->prio, *prio);
220 break;
223 case VIDIOC_ENUMSTD:
225 struct v4l2_standard *vs = (struct v4l2_standard *)arg;
226 int idx = vs->index;
227 ret = pvr2_hdw_get_stdenum_value(hdw,vs,idx+1);
228 break;
231 case VIDIOC_G_STD:
233 int val = 0;
234 ret = pvr2_ctrl_get_value(
235 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR),&val);
236 *(v4l2_std_id *)arg = val;
237 break;
240 case VIDIOC_S_STD:
242 ret = pvr2_ctrl_set_value(
243 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR),
244 *(v4l2_std_id *)arg);
245 break;
248 case VIDIOC_ENUMINPUT:
250 struct pvr2_ctrl *cptr;
251 struct v4l2_input *vi = (struct v4l2_input *)arg;
252 struct v4l2_input tmp;
253 unsigned int cnt;
254 int val;
256 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
258 memset(&tmp,0,sizeof(tmp));
259 tmp.index = vi->index;
260 ret = 0;
261 if (vi->index >= fh->input_cnt) {
262 ret = -EINVAL;
263 break;
265 val = fh->input_map[vi->index];
266 switch (val) {
267 case PVR2_CVAL_INPUT_TV:
268 case PVR2_CVAL_INPUT_DTV:
269 case PVR2_CVAL_INPUT_RADIO:
270 tmp.type = V4L2_INPUT_TYPE_TUNER;
271 break;
272 case PVR2_CVAL_INPUT_SVIDEO:
273 case PVR2_CVAL_INPUT_COMPOSITE:
274 tmp.type = V4L2_INPUT_TYPE_CAMERA;
275 break;
276 default:
277 ret = -EINVAL;
278 break;
280 if (ret < 0) break;
282 cnt = 0;
283 pvr2_ctrl_get_valname(cptr,val,
284 tmp.name,sizeof(tmp.name)-1,&cnt);
285 tmp.name[cnt] = 0;
287 /* Don't bother with audioset, since this driver currently
288 always switches the audio whenever the video is
289 switched. */
291 /* Handling std is a tougher problem. It doesn't make
292 sense in cases where a device might be multi-standard.
293 We could just copy out the current value for the
294 standard, but it can change over time. For now just
295 leave it zero. */
297 memcpy(vi, &tmp, sizeof(tmp));
299 ret = 0;
300 break;
303 case VIDIOC_G_INPUT:
305 unsigned int idx;
306 struct pvr2_ctrl *cptr;
307 struct v4l2_input *vi = (struct v4l2_input *)arg;
308 int val;
309 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
310 val = 0;
311 ret = pvr2_ctrl_get_value(cptr,&val);
312 vi->index = 0;
313 for (idx = 0; idx < fh->input_cnt; idx++) {
314 if (fh->input_map[idx] == val) {
315 vi->index = idx;
316 break;
319 break;
322 case VIDIOC_S_INPUT:
324 struct v4l2_input *vi = (struct v4l2_input *)arg;
325 if (vi->index >= fh->input_cnt) {
326 ret = -ERANGE;
327 break;
329 ret = pvr2_ctrl_set_value(
330 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
331 fh->input_map[vi->index]);
332 break;
335 case VIDIOC_ENUMAUDIO:
337 /* pkt: FIXME: We are returning one "fake" input here
338 which could very well be called "whatever_we_like".
339 This is for apps that want to see an audio input
340 just to feel comfortable, as well as to test if
341 it can do stereo or sth. There is actually no guarantee
342 that the actual audio input cannot change behind the app's
343 back, but most applications should not mind that either.
345 Hopefully, mplayer people will work with us on this (this
346 whole mess is to support mplayer pvr://), or Hans will come
347 up with a more standard way to say "we have inputs but we
348 don 't want you to change them independent of video" which
349 will sort this mess.
351 struct v4l2_audio *vin = arg;
352 ret = -EINVAL;
353 if (vin->index > 0) break;
354 strncpy(vin->name, "PVRUSB2 Audio",14);
355 vin->capability = V4L2_AUDCAP_STEREO;
356 ret = 0;
357 break;
358 break;
361 case VIDIOC_G_AUDIO:
363 /* pkt: FIXME: see above comment (VIDIOC_ENUMAUDIO) */
364 struct v4l2_audio *vin = arg;
365 memset(vin,0,sizeof(*vin));
366 vin->index = 0;
367 strncpy(vin->name, "PVRUSB2 Audio",14);
368 vin->capability = V4L2_AUDCAP_STEREO;
369 ret = 0;
370 break;
373 case VIDIOC_G_TUNER:
375 struct v4l2_tuner *vt = (struct v4l2_tuner *)arg;
377 if (vt->index != 0) break; /* Only answer for the 1st tuner */
379 pvr2_hdw_execute_tuner_poll(hdw);
380 ret = pvr2_hdw_get_tuner_status(hdw,vt);
381 break;
384 case VIDIOC_S_TUNER:
386 struct v4l2_tuner *vt=(struct v4l2_tuner *)arg;
388 if (vt->index != 0)
389 break;
391 ret = pvr2_ctrl_set_value(
392 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_AUDIOMODE),
393 vt->audmode);
394 break;
397 case VIDIOC_S_FREQUENCY:
399 const struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
400 unsigned long fv;
401 struct v4l2_tuner vt;
402 int cur_input;
403 struct pvr2_ctrl *ctrlp;
404 ret = pvr2_hdw_get_tuner_status(hdw,&vt);
405 if (ret != 0) break;
406 ctrlp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
407 ret = pvr2_ctrl_get_value(ctrlp,&cur_input);
408 if (ret != 0) break;
409 if (vf->type == V4L2_TUNER_RADIO) {
410 if (cur_input != PVR2_CVAL_INPUT_RADIO) {
411 pvr2_ctrl_set_value(ctrlp,
412 PVR2_CVAL_INPUT_RADIO);
414 } else {
415 if (cur_input == PVR2_CVAL_INPUT_RADIO) {
416 pvr2_ctrl_set_value(ctrlp,
417 PVR2_CVAL_INPUT_TV);
420 fv = vf->frequency;
421 if (vt.capability & V4L2_TUNER_CAP_LOW) {
422 fv = (fv * 125) / 2;
423 } else {
424 fv = fv * 62500;
426 ret = pvr2_ctrl_set_value(
427 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),fv);
428 break;
431 case VIDIOC_G_FREQUENCY:
433 struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
434 int val = 0;
435 int cur_input;
436 struct v4l2_tuner vt;
437 ret = pvr2_hdw_get_tuner_status(hdw,&vt);
438 if (ret != 0) break;
439 ret = pvr2_ctrl_get_value(
440 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),
441 &val);
442 if (ret != 0) break;
443 pvr2_ctrl_get_value(
444 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
445 &cur_input);
446 if (cur_input == PVR2_CVAL_INPUT_RADIO) {
447 vf->type = V4L2_TUNER_RADIO;
448 } else {
449 vf->type = V4L2_TUNER_ANALOG_TV;
451 if (vt.capability & V4L2_TUNER_CAP_LOW) {
452 val = (val * 2) / 125;
453 } else {
454 val /= 62500;
456 vf->frequency = val;
457 break;
460 case VIDIOC_ENUM_FMT:
462 struct v4l2_fmtdesc *fd = (struct v4l2_fmtdesc *)arg;
464 /* Only one format is supported : mpeg.*/
465 if (fd->index != 0)
466 break;
468 memcpy(fd, pvr_fmtdesc, sizeof(struct v4l2_fmtdesc));
469 ret = 0;
470 break;
473 case VIDIOC_G_FMT:
475 struct v4l2_format *vf = (struct v4l2_format *)arg;
476 int val;
477 switch(vf->type) {
478 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
479 memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
480 sizeof(struct v4l2_format));
481 val = 0;
482 pvr2_ctrl_get_value(
483 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES),
484 &val);
485 vf->fmt.pix.width = val;
486 val = 0;
487 pvr2_ctrl_get_value(
488 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES),
489 &val);
490 vf->fmt.pix.height = val;
491 ret = 0;
492 break;
493 case V4L2_BUF_TYPE_VBI_CAPTURE:
494 // ????? Still need to figure out to do VBI correctly
495 ret = -EINVAL;
496 break;
497 default:
498 ret = -EINVAL;
499 break;
501 break;
504 case VIDIOC_TRY_FMT:
505 case VIDIOC_S_FMT:
507 struct v4l2_format *vf = (struct v4l2_format *)arg;
509 ret = 0;
510 switch(vf->type) {
511 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
512 int lmin,lmax,ldef;
513 struct pvr2_ctrl *hcp,*vcp;
514 int h = vf->fmt.pix.height;
515 int w = vf->fmt.pix.width;
516 hcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES);
517 vcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES);
519 lmin = pvr2_ctrl_get_min(hcp);
520 lmax = pvr2_ctrl_get_max(hcp);
521 pvr2_ctrl_get_def(hcp, &ldef);
522 if (w == -1) {
523 w = ldef;
524 } else if (w < lmin) {
525 w = lmin;
526 } else if (w > lmax) {
527 w = lmax;
529 lmin = pvr2_ctrl_get_min(vcp);
530 lmax = pvr2_ctrl_get_max(vcp);
531 pvr2_ctrl_get_def(vcp, &ldef);
532 if (h == -1) {
533 h = ldef;
534 } else if (h < lmin) {
535 h = lmin;
536 } else if (h > lmax) {
537 h = lmax;
540 memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
541 sizeof(struct v4l2_format));
542 vf->fmt.pix.width = w;
543 vf->fmt.pix.height = h;
545 if (cmd == VIDIOC_S_FMT) {
546 pvr2_ctrl_set_value(hcp,vf->fmt.pix.width);
547 pvr2_ctrl_set_value(vcp,vf->fmt.pix.height);
549 } break;
550 case V4L2_BUF_TYPE_VBI_CAPTURE:
551 // ????? Still need to figure out to do VBI correctly
552 ret = -EINVAL;
553 break;
554 default:
555 ret = -EINVAL;
556 break;
558 break;
561 case VIDIOC_STREAMON:
563 if (!fh->pdi->stream) {
564 /* No stream defined for this node. This means
565 that we're not currently allowed to stream from
566 this node. */
567 ret = -EPERM;
568 break;
570 ret = pvr2_hdw_set_stream_type(hdw,pdi->config);
571 if (ret < 0) return ret;
572 ret = pvr2_hdw_set_streaming(hdw,!0);
573 break;
576 case VIDIOC_STREAMOFF:
578 if (!fh->pdi->stream) {
579 /* No stream defined for this node. This means
580 that we're not currently allowed to stream from
581 this node. */
582 ret = -EPERM;
583 break;
585 ret = pvr2_hdw_set_streaming(hdw,0);
586 break;
589 case VIDIOC_QUERYCTRL:
591 struct pvr2_ctrl *cptr;
592 int val;
593 struct v4l2_queryctrl *vc = (struct v4l2_queryctrl *)arg;
594 ret = 0;
595 if (vc->id & V4L2_CTRL_FLAG_NEXT_CTRL) {
596 cptr = pvr2_hdw_get_ctrl_nextv4l(
597 hdw,(vc->id & ~V4L2_CTRL_FLAG_NEXT_CTRL));
598 if (cptr) vc->id = pvr2_ctrl_get_v4lid(cptr);
599 } else {
600 cptr = pvr2_hdw_get_ctrl_v4l(hdw,vc->id);
602 if (!cptr) {
603 pvr2_trace(PVR2_TRACE_V4LIOCTL,
604 "QUERYCTRL id=0x%x not implemented here",
605 vc->id);
606 ret = -EINVAL;
607 break;
610 pvr2_trace(PVR2_TRACE_V4LIOCTL,
611 "QUERYCTRL id=0x%x mapping name=%s (%s)",
612 vc->id,pvr2_ctrl_get_name(cptr),
613 pvr2_ctrl_get_desc(cptr));
614 strlcpy(vc->name,pvr2_ctrl_get_desc(cptr),sizeof(vc->name));
615 vc->flags = pvr2_ctrl_get_v4lflags(cptr);
616 pvr2_ctrl_get_def(cptr, &val);
617 vc->default_value = val;
618 switch (pvr2_ctrl_get_type(cptr)) {
619 case pvr2_ctl_enum:
620 vc->type = V4L2_CTRL_TYPE_MENU;
621 vc->minimum = 0;
622 vc->maximum = pvr2_ctrl_get_cnt(cptr) - 1;
623 vc->step = 1;
624 break;
625 case pvr2_ctl_bool:
626 vc->type = V4L2_CTRL_TYPE_BOOLEAN;
627 vc->minimum = 0;
628 vc->maximum = 1;
629 vc->step = 1;
630 break;
631 case pvr2_ctl_int:
632 vc->type = V4L2_CTRL_TYPE_INTEGER;
633 vc->minimum = pvr2_ctrl_get_min(cptr);
634 vc->maximum = pvr2_ctrl_get_max(cptr);
635 vc->step = 1;
636 break;
637 default:
638 pvr2_trace(PVR2_TRACE_V4LIOCTL,
639 "QUERYCTRL id=0x%x name=%s not mappable",
640 vc->id,pvr2_ctrl_get_name(cptr));
641 ret = -EINVAL;
642 break;
644 break;
647 case VIDIOC_QUERYMENU:
649 struct v4l2_querymenu *vm = (struct v4l2_querymenu *)arg;
650 unsigned int cnt = 0;
651 ret = pvr2_ctrl_get_valname(pvr2_hdw_get_ctrl_v4l(hdw,vm->id),
652 vm->index,
653 vm->name,sizeof(vm->name)-1,
654 &cnt);
655 vm->name[cnt] = 0;
656 break;
659 case VIDIOC_G_CTRL:
661 struct v4l2_control *vc = (struct v4l2_control *)arg;
662 int val = 0;
663 ret = pvr2_ctrl_get_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
664 &val);
665 vc->value = val;
666 break;
669 case VIDIOC_S_CTRL:
671 struct v4l2_control *vc = (struct v4l2_control *)arg;
672 ret = pvr2_ctrl_set_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
673 vc->value);
674 break;
677 case VIDIOC_G_EXT_CTRLS:
679 struct v4l2_ext_controls *ctls =
680 (struct v4l2_ext_controls *)arg;
681 struct v4l2_ext_control *ctrl;
682 unsigned int idx;
683 int val;
684 ret = 0;
685 for (idx = 0; idx < ctls->count; idx++) {
686 ctrl = ctls->controls + idx;
687 ret = pvr2_ctrl_get_value(
688 pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),&val);
689 if (ret) {
690 ctls->error_idx = idx;
691 break;
693 /* Ensure that if read as a 64 bit value, the user
694 will still get a hopefully sane value */
695 ctrl->value64 = 0;
696 ctrl->value = val;
698 break;
701 case VIDIOC_S_EXT_CTRLS:
703 struct v4l2_ext_controls *ctls =
704 (struct v4l2_ext_controls *)arg;
705 struct v4l2_ext_control *ctrl;
706 unsigned int idx;
707 ret = 0;
708 for (idx = 0; idx < ctls->count; idx++) {
709 ctrl = ctls->controls + idx;
710 ret = pvr2_ctrl_set_value(
711 pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),
712 ctrl->value);
713 if (ret) {
714 ctls->error_idx = idx;
715 break;
718 break;
721 case VIDIOC_TRY_EXT_CTRLS:
723 struct v4l2_ext_controls *ctls =
724 (struct v4l2_ext_controls *)arg;
725 struct v4l2_ext_control *ctrl;
726 struct pvr2_ctrl *pctl;
727 unsigned int idx;
728 /* For the moment just validate that the requested control
729 actually exists. */
730 ret = 0;
731 for (idx = 0; idx < ctls->count; idx++) {
732 ctrl = ctls->controls + idx;
733 pctl = pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id);
734 if (!pctl) {
735 ret = -EINVAL;
736 ctls->error_idx = idx;
737 break;
740 break;
743 case VIDIOC_CROPCAP:
745 struct v4l2_cropcap *cap = (struct v4l2_cropcap *)arg;
746 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
747 ret = -EINVAL;
748 break;
750 ret = pvr2_hdw_get_cropcap(hdw, cap);
751 cap->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; /* paranoia */
752 break;
754 case VIDIOC_G_CROP:
756 struct v4l2_crop *crop = (struct v4l2_crop *)arg;
757 int val = 0;
758 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
759 ret = -EINVAL;
760 break;
762 ret = pvr2_ctrl_get_value(
763 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPL), &val);
764 if (ret != 0) {
765 ret = -EINVAL;
766 break;
768 crop->c.left = val;
769 ret = pvr2_ctrl_get_value(
770 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPT), &val);
771 if (ret != 0) {
772 ret = -EINVAL;
773 break;
775 crop->c.top = val;
776 ret = pvr2_ctrl_get_value(
777 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPW), &val);
778 if (ret != 0) {
779 ret = -EINVAL;
780 break;
782 crop->c.width = val;
783 ret = pvr2_ctrl_get_value(
784 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPH), &val);
785 if (ret != 0) {
786 ret = -EINVAL;
787 break;
789 crop->c.height = val;
791 case VIDIOC_S_CROP:
793 struct v4l2_crop *crop = (struct v4l2_crop *)arg;
794 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
795 ret = -EINVAL;
796 break;
798 ret = pvr2_ctrl_set_value(
799 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPL),
800 crop->c.left);
801 if (ret != 0) {
802 ret = -EINVAL;
803 break;
805 ret = pvr2_ctrl_set_value(
806 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPT),
807 crop->c.top);
808 if (ret != 0) {
809 ret = -EINVAL;
810 break;
812 ret = pvr2_ctrl_set_value(
813 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPW),
814 crop->c.width);
815 if (ret != 0) {
816 ret = -EINVAL;
817 break;
819 ret = pvr2_ctrl_set_value(
820 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPH),
821 crop->c.height);
822 if (ret != 0) {
823 ret = -EINVAL;
824 break;
827 case VIDIOC_LOG_STATUS:
829 pvr2_hdw_trigger_module_log(hdw);
830 ret = 0;
831 break;
833 #ifdef CONFIG_VIDEO_ADV_DEBUG
834 case VIDIOC_DBG_S_REGISTER:
835 case VIDIOC_DBG_G_REGISTER:
837 u64 val;
838 struct v4l2_dbg_register *req = (struct v4l2_dbg_register *)arg;
839 if (cmd == VIDIOC_DBG_S_REGISTER) val = req->val;
840 ret = pvr2_hdw_register_access(
841 hdw, &req->match, req->reg,
842 cmd == VIDIOC_DBG_S_REGISTER, &val);
843 if (cmd == VIDIOC_DBG_G_REGISTER) req->val = val;
844 break;
846 #endif
848 default :
849 ret = -ENOTTY;
850 break;
853 pvr2_hdw_commit_ctl(hdw);
855 if (ret < 0) {
856 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
857 pvr2_trace(PVR2_TRACE_V4LIOCTL,
858 "pvr2_v4l2_do_ioctl failure, ret=%ld", ret);
859 } else {
860 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
861 pvr2_trace(PVR2_TRACE_V4LIOCTL,
862 "pvr2_v4l2_do_ioctl failure, ret=%ld"
863 " command was:", ret);
864 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),
865 cmd);
868 } else {
869 pvr2_trace(PVR2_TRACE_V4LIOCTL,
870 "pvr2_v4l2_do_ioctl complete, ret=%ld (0x%lx)",
871 ret, ret);
873 return ret;
876 static void pvr2_v4l2_dev_destroy(struct pvr2_v4l2_dev *dip)
878 struct pvr2_hdw *hdw = dip->v4lp->channel.mc_head->hdw;
879 enum pvr2_config cfg = dip->config;
880 char msg[80];
881 unsigned int mcnt;
883 /* Construct the unregistration message *before* we actually
884 perform the unregistration step. By doing it this way we don't
885 have to worry about potentially touching deleted resources. */
886 mcnt = scnprintf(msg, sizeof(msg) - 1,
887 "pvrusb2: unregistered device %s [%s]",
888 video_device_node_name(&dip->devbase),
889 pvr2_config_get_name(cfg));
890 msg[mcnt] = 0;
892 pvr2_hdw_v4l_store_minor_number(hdw,dip->minor_type,-1);
894 /* Paranoia */
895 dip->v4lp = NULL;
896 dip->stream = NULL;
898 /* Actual deallocation happens later when all internal references
899 are gone. */
900 video_unregister_device(&dip->devbase);
902 printk(KERN_INFO "%s\n", msg);
907 static void pvr2_v4l2_dev_disassociate_parent(struct pvr2_v4l2_dev *dip)
909 if (!dip) return;
910 if (!dip->devbase.parent) return;
911 dip->devbase.parent = NULL;
912 device_move(&dip->devbase.dev, NULL, DPM_ORDER_NONE);
916 static void pvr2_v4l2_destroy_no_lock(struct pvr2_v4l2 *vp)
918 if (vp->dev_video) {
919 pvr2_v4l2_dev_destroy(vp->dev_video);
920 vp->dev_video = NULL;
922 if (vp->dev_radio) {
923 pvr2_v4l2_dev_destroy(vp->dev_radio);
924 vp->dev_radio = NULL;
927 pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr2_v4l2 id=%p",vp);
928 pvr2_channel_done(&vp->channel);
929 kfree(vp);
933 static void pvr2_video_device_release(struct video_device *vdev)
935 struct pvr2_v4l2_dev *dev;
936 dev = container_of(vdev,struct pvr2_v4l2_dev,devbase);
937 kfree(dev);
941 static void pvr2_v4l2_internal_check(struct pvr2_channel *chp)
943 struct pvr2_v4l2 *vp;
944 vp = container_of(chp,struct pvr2_v4l2,channel);
945 if (!vp->channel.mc_head->disconnect_flag) return;
946 pvr2_v4l2_dev_disassociate_parent(vp->dev_video);
947 pvr2_v4l2_dev_disassociate_parent(vp->dev_radio);
948 if (vp->vfirst) return;
949 pvr2_v4l2_destroy_no_lock(vp);
953 static long pvr2_v4l2_ioctl(struct file *file,
954 unsigned int cmd, unsigned long arg)
957 return video_usercopy(file, cmd, arg, pvr2_v4l2_do_ioctl);
961 static int pvr2_v4l2_release(struct file *file)
963 struct pvr2_v4l2_fh *fhp = file->private_data;
964 struct pvr2_v4l2 *vp = fhp->vhead;
965 struct pvr2_hdw *hdw = fhp->channel.mc_head->hdw;
967 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_release");
969 if (fhp->rhp) {
970 struct pvr2_stream *sp;
971 pvr2_hdw_set_streaming(hdw,0);
972 sp = pvr2_ioread_get_stream(fhp->rhp);
973 if (sp) pvr2_stream_set_callback(sp,NULL,NULL);
974 pvr2_ioread_destroy(fhp->rhp);
975 fhp->rhp = NULL;
978 v4l2_prio_close(&vp->prio, fhp->prio);
979 file->private_data = NULL;
981 if (fhp->vnext) {
982 fhp->vnext->vprev = fhp->vprev;
983 } else {
984 vp->vlast = fhp->vprev;
986 if (fhp->vprev) {
987 fhp->vprev->vnext = fhp->vnext;
988 } else {
989 vp->vfirst = fhp->vnext;
991 fhp->vnext = NULL;
992 fhp->vprev = NULL;
993 fhp->vhead = NULL;
994 pvr2_channel_done(&fhp->channel);
995 pvr2_trace(PVR2_TRACE_STRUCT,
996 "Destroying pvr_v4l2_fh id=%p",fhp);
997 if (fhp->input_map) {
998 kfree(fhp->input_map);
999 fhp->input_map = NULL;
1001 kfree(fhp);
1002 if (vp->channel.mc_head->disconnect_flag && !vp->vfirst) {
1003 pvr2_v4l2_destroy_no_lock(vp);
1005 return 0;
1009 static int pvr2_v4l2_open(struct file *file)
1011 struct pvr2_v4l2_dev *dip; /* Our own context pointer */
1012 struct pvr2_v4l2_fh *fhp;
1013 struct pvr2_v4l2 *vp;
1014 struct pvr2_hdw *hdw;
1015 unsigned int input_mask = 0;
1016 unsigned int input_cnt,idx;
1017 int ret = 0;
1019 dip = container_of(video_devdata(file),struct pvr2_v4l2_dev,devbase);
1021 vp = dip->v4lp;
1022 hdw = vp->channel.hdw;
1024 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_open");
1026 if (!pvr2_hdw_dev_ok(hdw)) {
1027 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,
1028 "pvr2_v4l2_open: hardware not ready");
1029 return -EIO;
1032 fhp = kzalloc(sizeof(*fhp),GFP_KERNEL);
1033 if (!fhp) {
1034 return -ENOMEM;
1037 init_waitqueue_head(&fhp->wait_data);
1038 fhp->pdi = dip;
1040 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr_v4l2_fh id=%p",fhp);
1041 pvr2_channel_init(&fhp->channel,vp->channel.mc_head);
1043 if (dip->v4l_type == VFL_TYPE_RADIO) {
1044 /* Opening device as a radio, legal input selection subset
1045 is just the radio. */
1046 input_mask = (1 << PVR2_CVAL_INPUT_RADIO);
1047 } else {
1048 /* Opening the main V4L device, legal input selection
1049 subset includes all analog inputs. */
1050 input_mask = ((1 << PVR2_CVAL_INPUT_RADIO) |
1051 (1 << PVR2_CVAL_INPUT_TV) |
1052 (1 << PVR2_CVAL_INPUT_COMPOSITE) |
1053 (1 << PVR2_CVAL_INPUT_SVIDEO));
1055 ret = pvr2_channel_limit_inputs(&fhp->channel,input_mask);
1056 if (ret) {
1057 pvr2_channel_done(&fhp->channel);
1058 pvr2_trace(PVR2_TRACE_STRUCT,
1059 "Destroying pvr_v4l2_fh id=%p (input mask error)",
1060 fhp);
1062 kfree(fhp);
1063 return ret;
1066 input_mask &= pvr2_hdw_get_input_available(hdw);
1067 input_cnt = 0;
1068 for (idx = 0; idx < (sizeof(input_mask) << 3); idx++) {
1069 if (input_mask & (1 << idx)) input_cnt++;
1071 fhp->input_cnt = input_cnt;
1072 fhp->input_map = kzalloc(input_cnt,GFP_KERNEL);
1073 if (!fhp->input_map) {
1074 pvr2_channel_done(&fhp->channel);
1075 pvr2_trace(PVR2_TRACE_STRUCT,
1076 "Destroying pvr_v4l2_fh id=%p (input map failure)",
1077 fhp);
1078 kfree(fhp);
1079 return -ENOMEM;
1081 input_cnt = 0;
1082 for (idx = 0; idx < (sizeof(input_mask) << 3); idx++) {
1083 if (!(input_mask & (1 << idx))) continue;
1084 fhp->input_map[input_cnt++] = idx;
1087 fhp->vnext = NULL;
1088 fhp->vprev = vp->vlast;
1089 if (vp->vlast) {
1090 vp->vlast->vnext = fhp;
1091 } else {
1092 vp->vfirst = fhp;
1094 vp->vlast = fhp;
1095 fhp->vhead = vp;
1097 fhp->file = file;
1098 file->private_data = fhp;
1099 v4l2_prio_open(&vp->prio, &fhp->prio);
1101 fhp->fw_mode_flag = pvr2_hdw_cpufw_get_enabled(hdw);
1103 return 0;
1107 static void pvr2_v4l2_notify(struct pvr2_v4l2_fh *fhp)
1109 wake_up(&fhp->wait_data);
1112 static int pvr2_v4l2_iosetup(struct pvr2_v4l2_fh *fh)
1114 int ret;
1115 struct pvr2_stream *sp;
1116 struct pvr2_hdw *hdw;
1117 if (fh->rhp) return 0;
1119 if (!fh->pdi->stream) {
1120 /* No stream defined for this node. This means that we're
1121 not currently allowed to stream from this node. */
1122 return -EPERM;
1125 /* First read() attempt. Try to claim the stream and start
1126 it... */
1127 if ((ret = pvr2_channel_claim_stream(&fh->channel,
1128 fh->pdi->stream)) != 0) {
1129 /* Someone else must already have it */
1130 return ret;
1133 fh->rhp = pvr2_channel_create_mpeg_stream(fh->pdi->stream);
1134 if (!fh->rhp) {
1135 pvr2_channel_claim_stream(&fh->channel,NULL);
1136 return -ENOMEM;
1139 hdw = fh->channel.mc_head->hdw;
1140 sp = fh->pdi->stream->stream;
1141 pvr2_stream_set_callback(sp,(pvr2_stream_callback)pvr2_v4l2_notify,fh);
1142 pvr2_hdw_set_stream_type(hdw,fh->pdi->config);
1143 if ((ret = pvr2_hdw_set_streaming(hdw,!0)) < 0) return ret;
1144 return pvr2_ioread_set_enabled(fh->rhp,!0);
1148 static ssize_t pvr2_v4l2_read(struct file *file,
1149 char __user *buff, size_t count, loff_t *ppos)
1151 struct pvr2_v4l2_fh *fh = file->private_data;
1152 int ret;
1154 if (fh->fw_mode_flag) {
1155 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
1156 char *tbuf;
1157 int c1,c2;
1158 int tcnt = 0;
1159 unsigned int offs = *ppos;
1161 tbuf = kmalloc(PAGE_SIZE,GFP_KERNEL);
1162 if (!tbuf) return -ENOMEM;
1164 while (count) {
1165 c1 = count;
1166 if (c1 > PAGE_SIZE) c1 = PAGE_SIZE;
1167 c2 = pvr2_hdw_cpufw_get(hdw,offs,tbuf,c1);
1168 if (c2 < 0) {
1169 tcnt = c2;
1170 break;
1172 if (!c2) break;
1173 if (copy_to_user(buff,tbuf,c2)) {
1174 tcnt = -EFAULT;
1175 break;
1177 offs += c2;
1178 tcnt += c2;
1179 buff += c2;
1180 count -= c2;
1181 *ppos += c2;
1183 kfree(tbuf);
1184 return tcnt;
1187 if (!fh->rhp) {
1188 ret = pvr2_v4l2_iosetup(fh);
1189 if (ret) {
1190 return ret;
1194 for (;;) {
1195 ret = pvr2_ioread_read(fh->rhp,buff,count);
1196 if (ret >= 0) break;
1197 if (ret != -EAGAIN) break;
1198 if (file->f_flags & O_NONBLOCK) break;
1199 /* Doing blocking I/O. Wait here. */
1200 ret = wait_event_interruptible(
1201 fh->wait_data,
1202 pvr2_ioread_avail(fh->rhp) >= 0);
1203 if (ret < 0) break;
1206 return ret;
1210 static unsigned int pvr2_v4l2_poll(struct file *file, poll_table *wait)
1212 unsigned int mask = 0;
1213 struct pvr2_v4l2_fh *fh = file->private_data;
1214 int ret;
1216 if (fh->fw_mode_flag) {
1217 mask |= POLLIN | POLLRDNORM;
1218 return mask;
1221 if (!fh->rhp) {
1222 ret = pvr2_v4l2_iosetup(fh);
1223 if (ret) return POLLERR;
1226 poll_wait(file,&fh->wait_data,wait);
1228 if (pvr2_ioread_avail(fh->rhp) >= 0) {
1229 mask |= POLLIN | POLLRDNORM;
1232 return mask;
1236 static const struct v4l2_file_operations vdev_fops = {
1237 .owner = THIS_MODULE,
1238 .open = pvr2_v4l2_open,
1239 .release = pvr2_v4l2_release,
1240 .read = pvr2_v4l2_read,
1241 .ioctl = pvr2_v4l2_ioctl,
1242 .poll = pvr2_v4l2_poll,
1246 static struct video_device vdev_template = {
1247 .fops = &vdev_fops,
1251 static void pvr2_v4l2_dev_init(struct pvr2_v4l2_dev *dip,
1252 struct pvr2_v4l2 *vp,
1253 int v4l_type)
1255 struct usb_device *usbdev;
1256 int mindevnum;
1257 int unit_number;
1258 int *nr_ptr = NULL;
1259 dip->v4lp = vp;
1261 usbdev = pvr2_hdw_get_dev(vp->channel.mc_head->hdw);
1262 dip->v4l_type = v4l_type;
1263 switch (v4l_type) {
1264 case VFL_TYPE_GRABBER:
1265 dip->stream = &vp->channel.mc_head->video_stream;
1266 dip->config = pvr2_config_mpeg;
1267 dip->minor_type = pvr2_v4l_type_video;
1268 nr_ptr = video_nr;
1269 if (!dip->stream) {
1270 pr_err(KBUILD_MODNAME
1271 ": Failed to set up pvrusb2 v4l video dev"
1272 " due to missing stream instance\n");
1273 return;
1275 break;
1276 case VFL_TYPE_VBI:
1277 dip->config = pvr2_config_vbi;
1278 dip->minor_type = pvr2_v4l_type_vbi;
1279 nr_ptr = vbi_nr;
1280 break;
1281 case VFL_TYPE_RADIO:
1282 dip->stream = &vp->channel.mc_head->video_stream;
1283 dip->config = pvr2_config_mpeg;
1284 dip->minor_type = pvr2_v4l_type_radio;
1285 nr_ptr = radio_nr;
1286 break;
1287 default:
1288 /* Bail out (this should be impossible) */
1289 pr_err(KBUILD_MODNAME ": Failed to set up pvrusb2 v4l dev"
1290 " due to unrecognized config\n");
1291 return;
1294 memcpy(&dip->devbase,&vdev_template,sizeof(vdev_template));
1295 dip->devbase.release = pvr2_video_device_release;
1297 mindevnum = -1;
1298 unit_number = pvr2_hdw_get_unit_number(vp->channel.mc_head->hdw);
1299 if (nr_ptr && (unit_number >= 0) && (unit_number < PVR_NUM)) {
1300 mindevnum = nr_ptr[unit_number];
1302 dip->devbase.parent = &usbdev->dev;
1303 if ((video_register_device(&dip->devbase,
1304 dip->v4l_type, mindevnum) < 0) &&
1305 (video_register_device(&dip->devbase,
1306 dip->v4l_type, -1) < 0)) {
1307 pr_err(KBUILD_MODNAME
1308 ": Failed to register pvrusb2 v4l device\n");
1311 printk(KERN_INFO "pvrusb2: registered device %s [%s]\n",
1312 video_device_node_name(&dip->devbase),
1313 pvr2_config_get_name(dip->config));
1315 pvr2_hdw_v4l_store_minor_number(vp->channel.mc_head->hdw,
1316 dip->minor_type,dip->devbase.minor);
1320 struct pvr2_v4l2 *pvr2_v4l2_create(struct pvr2_context *mnp)
1322 struct pvr2_v4l2 *vp;
1324 vp = kzalloc(sizeof(*vp),GFP_KERNEL);
1325 if (!vp) return vp;
1326 pvr2_channel_init(&vp->channel,mnp);
1327 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr2_v4l2 id=%p",vp);
1329 vp->channel.check_func = pvr2_v4l2_internal_check;
1331 /* register streams */
1332 vp->dev_video = kzalloc(sizeof(*vp->dev_video),GFP_KERNEL);
1333 if (!vp->dev_video) goto fail;
1334 pvr2_v4l2_dev_init(vp->dev_video,vp,VFL_TYPE_GRABBER);
1335 if (pvr2_hdw_get_input_available(vp->channel.mc_head->hdw) &
1336 (1 << PVR2_CVAL_INPUT_RADIO)) {
1337 vp->dev_radio = kzalloc(sizeof(*vp->dev_radio),GFP_KERNEL);
1338 if (!vp->dev_radio) goto fail;
1339 pvr2_v4l2_dev_init(vp->dev_radio,vp,VFL_TYPE_RADIO);
1342 return vp;
1343 fail:
1344 pvr2_trace(PVR2_TRACE_STRUCT,"Failure creating pvr2_v4l2 id=%p",vp);
1345 pvr2_v4l2_destroy_no_lock(vp);
1346 return NULL;
1350 Stuff for Emacs to see, in order to encourage consistent editing style:
1351 *** Local Variables: ***
1352 *** mode: c ***
1353 *** fill-column: 75 ***
1354 *** tab-width: 8 ***
1355 *** c-basic-offset: 8 ***
1356 *** End: ***