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"
28 #include "pvrusb2-debug.h"
29 #include "pvrusb2-v4l2.h"
30 #include "pvrusb2-ioread.h"
31 #include <linux/videodev2.h>
32 #include <media/v4l2-dev.h>
33 #include <media/v4l2-common.h>
34 #include <media/v4l2-ioctl.h>
40 struct pvr2_v4l2_dev
{
41 struct video_device devbase
; /* MUST be first! */
42 struct pvr2_v4l2
*v4lp
;
43 struct pvr2_context_stream
*stream
;
44 /* Information about this device: */
45 enum pvr2_config config
; /* Expected stream format */
46 int v4l_type
; /* V4L defined type for this device node */
47 enum pvr2_v4l_type minor_type
; /* pvr2-understood minor device type */
51 struct pvr2_channel channel
;
52 struct pvr2_v4l2_dev
*pdi
;
53 enum v4l2_priority prio
;
54 struct pvr2_ioread
*rhp
;
56 struct pvr2_v4l2
*vhead
;
57 struct pvr2_v4l2_fh
*vnext
;
58 struct pvr2_v4l2_fh
*vprev
;
59 wait_queue_head_t wait_data
;
61 /* Map contiguous ordinal value to input id */
62 unsigned char *input_map
;
63 unsigned int input_cnt
;
67 struct pvr2_channel channel
;
68 struct pvr2_v4l2_fh
*vfirst
;
69 struct pvr2_v4l2_fh
*vlast
;
71 struct v4l2_prio_state prio
;
73 /* streams - Note that these must be separately, individually,
74 * allocated pointers. This is because the v4l core is going to
75 * manage their deletion - separately, individually... */
76 struct pvr2_v4l2_dev
*dev_video
;
77 struct pvr2_v4l2_dev
*dev_radio
;
80 static int video_nr
[PVR_NUM
] = {[0 ... PVR_NUM
-1] = -1};
81 module_param_array(video_nr
, int, NULL
, 0444);
82 MODULE_PARM_DESC(video_nr
, "Offset for device's video dev minor");
83 static int radio_nr
[PVR_NUM
] = {[0 ... PVR_NUM
-1] = -1};
84 module_param_array(radio_nr
, int, NULL
, 0444);
85 MODULE_PARM_DESC(radio_nr
, "Offset for device's radio dev minor");
86 static int vbi_nr
[PVR_NUM
] = {[0 ... PVR_NUM
-1] = -1};
87 module_param_array(vbi_nr
, int, NULL
, 0444);
88 MODULE_PARM_DESC(vbi_nr
, "Offset for device's vbi dev minor");
90 static struct v4l2_capability pvr_capability
={
92 .card
= "Hauppauge WinTV pvr-usb2",
94 .version
= LINUX_VERSION_CODE
,
95 .capabilities
= (V4L2_CAP_VIDEO_CAPTURE
|
96 V4L2_CAP_TUNER
| V4L2_CAP_AUDIO
| V4L2_CAP_RADIO
|
101 static struct v4l2_fmtdesc pvr_fmtdesc
[] = {
104 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
105 .flags
= V4L2_FMT_FLAG_COMPRESSED
,
106 .description
= "MPEG1/2",
107 // This should really be V4L2_PIX_FMT_MPEG, but xawtv
108 // breaks when I do that.
109 .pixelformat
= 0, // V4L2_PIX_FMT_MPEG,
110 .reserved
= { 0, 0, 0, 0 }
114 #define PVR_FORMAT_PIX 0
115 #define PVR_FORMAT_VBI 1
117 static struct v4l2_format pvr_format
[] = {
119 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
124 // This should really be V4L2_PIX_FMT_MPEG,
125 // but xawtv breaks when I do that.
126 .pixelformat
= 0, // V4L2_PIX_FMT_MPEG,
127 .field
= V4L2_FIELD_INTERLACED
,
128 .bytesperline
= 0, // doesn't make sense
130 //FIXME : Don't know what to put here...
131 .sizeimage
= (32*1024),
132 .colorspace
= 0, // doesn't make sense here
138 .type
= V4L2_BUF_TYPE_VBI_CAPTURE
,
141 .sampling_rate
= 27000000,
143 .samples_per_line
= 1443,
144 .sample_format
= V4L2_PIX_FMT_GREY
,
158 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
161 static long pvr2_v4l2_do_ioctl(struct file
*file
, unsigned int cmd
, void *arg
)
163 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
164 struct pvr2_v4l2
*vp
= fh
->vhead
;
165 struct pvr2_v4l2_dev
*pdi
= fh
->pdi
;
166 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
169 if (pvrusb2_debug
& PVR2_TRACE_V4LIOCTL
) {
170 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw
),cmd
);
173 if (!pvr2_hdw_dev_ok(hdw
)) {
174 pvr2_trace(PVR2_TRACE_ERROR_LEGS
,
175 "ioctl failed - bad or no context");
185 case VIDIOC_S_FREQUENCY
:
186 ret
= v4l2_prio_check(&vp
->prio
, fh
->prio
);
192 case VIDIOC_QUERYCAP
:
194 struct v4l2_capability
*cap
= arg
;
196 memcpy(cap
, &pvr_capability
, sizeof(struct v4l2_capability
));
197 strlcpy(cap
->bus_info
,pvr2_hdw_get_bus_info(hdw
),
198 sizeof(cap
->bus_info
));
199 strlcpy(cap
->card
,pvr2_hdw_get_desc(hdw
),sizeof(cap
->card
));
205 case VIDIOC_G_PRIORITY
:
207 enum v4l2_priority
*p
= arg
;
209 *p
= v4l2_prio_max(&vp
->prio
);
214 case VIDIOC_S_PRIORITY
:
216 enum v4l2_priority
*prio
= arg
;
218 ret
= v4l2_prio_change(&vp
->prio
, &fh
->prio
, *prio
);
224 struct v4l2_standard
*vs
= (struct v4l2_standard
*)arg
;
226 ret
= pvr2_hdw_get_stdenum_value(hdw
,vs
,idx
+1);
233 ret
= pvr2_ctrl_get_value(
234 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_STDCUR
),&val
);
235 *(v4l2_std_id
*)arg
= val
;
241 ret
= pvr2_ctrl_set_value(
242 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_STDCUR
),
243 *(v4l2_std_id
*)arg
);
247 case VIDIOC_ENUMINPUT
:
249 struct pvr2_ctrl
*cptr
;
250 struct v4l2_input
*vi
= (struct v4l2_input
*)arg
;
251 struct v4l2_input tmp
;
255 cptr
= pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_INPUT
);
257 memset(&tmp
,0,sizeof(tmp
));
258 tmp
.index
= vi
->index
;
260 if (vi
->index
>= fh
->input_cnt
) {
264 val
= fh
->input_map
[vi
->index
];
266 case PVR2_CVAL_INPUT_TV
:
267 case PVR2_CVAL_INPUT_DTV
:
268 case PVR2_CVAL_INPUT_RADIO
:
269 tmp
.type
= V4L2_INPUT_TYPE_TUNER
;
271 case PVR2_CVAL_INPUT_SVIDEO
:
272 case PVR2_CVAL_INPUT_COMPOSITE
:
273 tmp
.type
= V4L2_INPUT_TYPE_CAMERA
;
282 pvr2_ctrl_get_valname(cptr
,val
,
283 tmp
.name
,sizeof(tmp
.name
)-1,&cnt
);
286 /* Don't bother with audioset, since this driver currently
287 always switches the audio whenever the video is
290 /* Handling std is a tougher problem. It doesn't make
291 sense in cases where a device might be multi-standard.
292 We could just copy out the current value for the
293 standard, but it can change over time. For now just
296 memcpy(vi
, &tmp
, sizeof(tmp
));
305 struct pvr2_ctrl
*cptr
;
306 struct v4l2_input
*vi
= (struct v4l2_input
*)arg
;
308 cptr
= pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_INPUT
);
310 ret
= pvr2_ctrl_get_value(cptr
,&val
);
312 for (idx
= 0; idx
< fh
->input_cnt
; idx
++) {
313 if (fh
->input_map
[idx
] == val
) {
323 struct v4l2_input
*vi
= (struct v4l2_input
*)arg
;
324 if (vi
->index
>= fh
->input_cnt
) {
328 ret
= pvr2_ctrl_set_value(
329 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_INPUT
),
330 fh
->input_map
[vi
->index
]);
334 case VIDIOC_ENUMAUDIO
:
336 /* pkt: FIXME: We are returning one "fake" input here
337 which could very well be called "whatever_we_like".
338 This is for apps that want to see an audio input
339 just to feel comfortable, as well as to test if
340 it can do stereo or sth. There is actually no guarantee
341 that the actual audio input cannot change behind the app's
342 back, but most applications should not mind that either.
344 Hopefully, mplayer people will work with us on this (this
345 whole mess is to support mplayer pvr://), or Hans will come
346 up with a more standard way to say "we have inputs but we
347 don 't want you to change them independent of video" which
350 struct v4l2_audio
*vin
= arg
;
352 if (vin
->index
> 0) break;
353 strncpy(vin
->name
, "PVRUSB2 Audio",14);
354 vin
->capability
= V4L2_AUDCAP_STEREO
;
362 /* pkt: FIXME: see above comment (VIDIOC_ENUMAUDIO) */
363 struct v4l2_audio
*vin
= arg
;
364 memset(vin
,0,sizeof(*vin
));
366 strncpy(vin
->name
, "PVRUSB2 Audio",14);
367 vin
->capability
= V4L2_AUDCAP_STEREO
;
374 struct v4l2_tuner
*vt
= (struct v4l2_tuner
*)arg
;
376 if (vt
->index
!= 0) break; /* Only answer for the 1st tuner */
378 pvr2_hdw_execute_tuner_poll(hdw
);
379 ret
= pvr2_hdw_get_tuner_status(hdw
,vt
);
385 struct v4l2_tuner
*vt
=(struct v4l2_tuner
*)arg
;
390 ret
= pvr2_ctrl_set_value(
391 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_AUDIOMODE
),
396 case VIDIOC_S_FREQUENCY
:
398 const struct v4l2_frequency
*vf
= (struct v4l2_frequency
*)arg
;
400 struct v4l2_tuner vt
;
402 struct pvr2_ctrl
*ctrlp
;
403 ret
= pvr2_hdw_get_tuner_status(hdw
,&vt
);
405 ctrlp
= pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_INPUT
);
406 ret
= pvr2_ctrl_get_value(ctrlp
,&cur_input
);
408 if (vf
->type
== V4L2_TUNER_RADIO
) {
409 if (cur_input
!= PVR2_CVAL_INPUT_RADIO
) {
410 pvr2_ctrl_set_value(ctrlp
,
411 PVR2_CVAL_INPUT_RADIO
);
414 if (cur_input
== PVR2_CVAL_INPUT_RADIO
) {
415 pvr2_ctrl_set_value(ctrlp
,
420 if (vt
.capability
& V4L2_TUNER_CAP_LOW
) {
425 ret
= pvr2_ctrl_set_value(
426 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_FREQUENCY
),fv
);
430 case VIDIOC_G_FREQUENCY
:
432 struct v4l2_frequency
*vf
= (struct v4l2_frequency
*)arg
;
435 struct v4l2_tuner vt
;
436 ret
= pvr2_hdw_get_tuner_status(hdw
,&vt
);
438 ret
= pvr2_ctrl_get_value(
439 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_FREQUENCY
),
443 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_INPUT
),
445 if (cur_input
== PVR2_CVAL_INPUT_RADIO
) {
446 vf
->type
= V4L2_TUNER_RADIO
;
448 vf
->type
= V4L2_TUNER_ANALOG_TV
;
450 if (vt
.capability
& V4L2_TUNER_CAP_LOW
) {
451 val
= (val
* 2) / 125;
459 case VIDIOC_ENUM_FMT
:
461 struct v4l2_fmtdesc
*fd
= (struct v4l2_fmtdesc
*)arg
;
463 /* Only one format is supported : mpeg.*/
467 memcpy(fd
, pvr_fmtdesc
, sizeof(struct v4l2_fmtdesc
));
474 struct v4l2_format
*vf
= (struct v4l2_format
*)arg
;
477 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
478 memcpy(vf
, &pvr_format
[PVR_FORMAT_PIX
],
479 sizeof(struct v4l2_format
));
482 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_HRES
),
484 vf
->fmt
.pix
.width
= val
;
487 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_VRES
),
489 vf
->fmt
.pix
.height
= val
;
492 case V4L2_BUF_TYPE_VBI_CAPTURE
:
493 // ????? Still need to figure out to do VBI correctly
506 struct v4l2_format
*vf
= (struct v4l2_format
*)arg
;
510 case V4L2_BUF_TYPE_VIDEO_CAPTURE
: {
512 struct pvr2_ctrl
*hcp
,*vcp
;
513 int h
= vf
->fmt
.pix
.height
;
514 int w
= vf
->fmt
.pix
.width
;
515 hcp
= pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_HRES
);
516 vcp
= pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_VRES
);
518 lmin
= pvr2_ctrl_get_min(hcp
);
519 lmax
= pvr2_ctrl_get_max(hcp
);
520 pvr2_ctrl_get_def(hcp
, &ldef
);
523 } else if (w
< lmin
) {
525 } else if (w
> lmax
) {
528 lmin
= pvr2_ctrl_get_min(vcp
);
529 lmax
= pvr2_ctrl_get_max(vcp
);
530 pvr2_ctrl_get_def(vcp
, &ldef
);
533 } else if (h
< lmin
) {
535 } else if (h
> lmax
) {
539 memcpy(vf
, &pvr_format
[PVR_FORMAT_PIX
],
540 sizeof(struct v4l2_format
));
541 vf
->fmt
.pix
.width
= w
;
542 vf
->fmt
.pix
.height
= h
;
544 if (cmd
== VIDIOC_S_FMT
) {
545 pvr2_ctrl_set_value(hcp
,vf
->fmt
.pix
.width
);
546 pvr2_ctrl_set_value(vcp
,vf
->fmt
.pix
.height
);
549 case V4L2_BUF_TYPE_VBI_CAPTURE
:
550 // ????? Still need to figure out to do VBI correctly
560 case VIDIOC_STREAMON
:
562 if (!fh
->pdi
->stream
) {
563 /* No stream defined for this node. This means
564 that we're not currently allowed to stream from
569 ret
= pvr2_hdw_set_stream_type(hdw
,pdi
->config
);
570 if (ret
< 0) return ret
;
571 ret
= pvr2_hdw_set_streaming(hdw
,!0);
575 case VIDIOC_STREAMOFF
:
577 if (!fh
->pdi
->stream
) {
578 /* No stream defined for this node. This means
579 that we're not currently allowed to stream from
584 ret
= pvr2_hdw_set_streaming(hdw
,0);
588 case VIDIOC_QUERYCTRL
:
590 struct pvr2_ctrl
*cptr
;
592 struct v4l2_queryctrl
*vc
= (struct v4l2_queryctrl
*)arg
;
594 if (vc
->id
& V4L2_CTRL_FLAG_NEXT_CTRL
) {
595 cptr
= pvr2_hdw_get_ctrl_nextv4l(
596 hdw
,(vc
->id
& ~V4L2_CTRL_FLAG_NEXT_CTRL
));
597 if (cptr
) vc
->id
= pvr2_ctrl_get_v4lid(cptr
);
599 cptr
= pvr2_hdw_get_ctrl_v4l(hdw
,vc
->id
);
602 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
603 "QUERYCTRL id=0x%x not implemented here",
609 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
610 "QUERYCTRL id=0x%x mapping name=%s (%s)",
611 vc
->id
,pvr2_ctrl_get_name(cptr
),
612 pvr2_ctrl_get_desc(cptr
));
613 strlcpy(vc
->name
,pvr2_ctrl_get_desc(cptr
),sizeof(vc
->name
));
614 vc
->flags
= pvr2_ctrl_get_v4lflags(cptr
);
615 pvr2_ctrl_get_def(cptr
, &val
);
616 vc
->default_value
= val
;
617 switch (pvr2_ctrl_get_type(cptr
)) {
619 vc
->type
= V4L2_CTRL_TYPE_MENU
;
621 vc
->maximum
= pvr2_ctrl_get_cnt(cptr
) - 1;
625 vc
->type
= V4L2_CTRL_TYPE_BOOLEAN
;
631 vc
->type
= V4L2_CTRL_TYPE_INTEGER
;
632 vc
->minimum
= pvr2_ctrl_get_min(cptr
);
633 vc
->maximum
= pvr2_ctrl_get_max(cptr
);
637 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
638 "QUERYCTRL id=0x%x name=%s not mappable",
639 vc
->id
,pvr2_ctrl_get_name(cptr
));
646 case VIDIOC_QUERYMENU
:
648 struct v4l2_querymenu
*vm
= (struct v4l2_querymenu
*)arg
;
649 unsigned int cnt
= 0;
650 ret
= pvr2_ctrl_get_valname(pvr2_hdw_get_ctrl_v4l(hdw
,vm
->id
),
652 vm
->name
,sizeof(vm
->name
)-1,
660 struct v4l2_control
*vc
= (struct v4l2_control
*)arg
;
662 ret
= pvr2_ctrl_get_value(pvr2_hdw_get_ctrl_v4l(hdw
,vc
->id
),
670 struct v4l2_control
*vc
= (struct v4l2_control
*)arg
;
671 ret
= pvr2_ctrl_set_value(pvr2_hdw_get_ctrl_v4l(hdw
,vc
->id
),
676 case VIDIOC_G_EXT_CTRLS
:
678 struct v4l2_ext_controls
*ctls
=
679 (struct v4l2_ext_controls
*)arg
;
680 struct v4l2_ext_control
*ctrl
;
684 for (idx
= 0; idx
< ctls
->count
; idx
++) {
685 ctrl
= ctls
->controls
+ idx
;
686 ret
= pvr2_ctrl_get_value(
687 pvr2_hdw_get_ctrl_v4l(hdw
,ctrl
->id
),&val
);
689 ctls
->error_idx
= idx
;
692 /* Ensure that if read as a 64 bit value, the user
693 will still get a hopefully sane value */
700 case VIDIOC_S_EXT_CTRLS
:
702 struct v4l2_ext_controls
*ctls
=
703 (struct v4l2_ext_controls
*)arg
;
704 struct v4l2_ext_control
*ctrl
;
707 for (idx
= 0; idx
< ctls
->count
; idx
++) {
708 ctrl
= ctls
->controls
+ idx
;
709 ret
= pvr2_ctrl_set_value(
710 pvr2_hdw_get_ctrl_v4l(hdw
,ctrl
->id
),
713 ctls
->error_idx
= idx
;
720 case VIDIOC_TRY_EXT_CTRLS
:
722 struct v4l2_ext_controls
*ctls
=
723 (struct v4l2_ext_controls
*)arg
;
724 struct v4l2_ext_control
*ctrl
;
725 struct pvr2_ctrl
*pctl
;
727 /* For the moment just validate that the requested control
730 for (idx
= 0; idx
< ctls
->count
; idx
++) {
731 ctrl
= ctls
->controls
+ idx
;
732 pctl
= pvr2_hdw_get_ctrl_v4l(hdw
,ctrl
->id
);
735 ctls
->error_idx
= idx
;
744 struct v4l2_cropcap
*cap
= (struct v4l2_cropcap
*)arg
;
745 if (cap
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
749 ret
= pvr2_hdw_get_cropcap(hdw
, cap
);
750 cap
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
; /* paranoia */
755 struct v4l2_crop
*crop
= (struct v4l2_crop
*)arg
;
757 if (crop
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
761 ret
= pvr2_ctrl_get_value(
762 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPL
), &val
);
768 ret
= pvr2_ctrl_get_value(
769 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPT
), &val
);
775 ret
= pvr2_ctrl_get_value(
776 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPW
), &val
);
782 ret
= pvr2_ctrl_get_value(
783 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPH
), &val
);
788 crop
->c
.height
= val
;
792 struct v4l2_crop
*crop
= (struct v4l2_crop
*)arg
;
793 if (crop
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
797 ret
= pvr2_ctrl_set_value(
798 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPL
),
804 ret
= pvr2_ctrl_set_value(
805 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPT
),
811 ret
= pvr2_ctrl_set_value(
812 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPW
),
818 ret
= pvr2_ctrl_set_value(
819 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPH
),
826 case VIDIOC_LOG_STATUS
:
828 pvr2_hdw_trigger_module_log(hdw
);
832 #ifdef CONFIG_VIDEO_ADV_DEBUG
833 case VIDIOC_DBG_S_REGISTER
:
834 case VIDIOC_DBG_G_REGISTER
:
837 struct v4l2_dbg_register
*req
= (struct v4l2_dbg_register
*)arg
;
838 if (cmd
== VIDIOC_DBG_S_REGISTER
) val
= req
->val
;
839 ret
= pvr2_hdw_register_access(
840 hdw
, &req
->match
, req
->reg
,
841 cmd
== VIDIOC_DBG_S_REGISTER
, &val
);
842 if (cmd
== VIDIOC_DBG_G_REGISTER
) req
->val
= val
;
852 pvr2_hdw_commit_ctl(hdw
);
855 if (pvrusb2_debug
& PVR2_TRACE_V4LIOCTL
) {
856 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
857 "pvr2_v4l2_do_ioctl failure, ret=%ld", ret
);
859 if (pvrusb2_debug
& PVR2_TRACE_V4LIOCTL
) {
860 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
861 "pvr2_v4l2_do_ioctl failure, ret=%ld"
862 " command was:", ret
);
863 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw
),
868 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
869 "pvr2_v4l2_do_ioctl complete, ret=%ld (0x%lx)",
875 static void pvr2_v4l2_dev_destroy(struct pvr2_v4l2_dev
*dip
)
877 struct pvr2_hdw
*hdw
= dip
->v4lp
->channel
.mc_head
->hdw
;
878 enum pvr2_config cfg
= dip
->config
;
882 /* Construct the unregistration message *before* we actually
883 perform the unregistration step. By doing it this way we don't
884 have to worry about potentially touching deleted resources. */
885 mcnt
= scnprintf(msg
, sizeof(msg
) - 1,
886 "pvrusb2: unregistered device %s [%s]",
887 video_device_node_name(&dip
->devbase
),
888 pvr2_config_get_name(cfg
));
891 pvr2_hdw_v4l_store_minor_number(hdw
,dip
->minor_type
,-1);
897 /* Actual deallocation happens later when all internal references
899 video_unregister_device(&dip
->devbase
);
901 printk(KERN_INFO
"%s\n", msg
);
906 static void pvr2_v4l2_dev_disassociate_parent(struct pvr2_v4l2_dev
*dip
)
909 if (!dip
->devbase
.parent
) return;
910 dip
->devbase
.parent
= NULL
;
911 device_move(&dip
->devbase
.dev
, NULL
, DPM_ORDER_NONE
);
915 static void pvr2_v4l2_destroy_no_lock(struct pvr2_v4l2
*vp
)
918 pvr2_v4l2_dev_destroy(vp
->dev_video
);
919 vp
->dev_video
= NULL
;
922 pvr2_v4l2_dev_destroy(vp
->dev_radio
);
923 vp
->dev_radio
= NULL
;
926 pvr2_trace(PVR2_TRACE_STRUCT
,"Destroying pvr2_v4l2 id=%p",vp
);
927 pvr2_channel_done(&vp
->channel
);
932 static void pvr2_video_device_release(struct video_device
*vdev
)
934 struct pvr2_v4l2_dev
*dev
;
935 dev
= container_of(vdev
,struct pvr2_v4l2_dev
,devbase
);
940 static void pvr2_v4l2_internal_check(struct pvr2_channel
*chp
)
942 struct pvr2_v4l2
*vp
;
943 vp
= container_of(chp
,struct pvr2_v4l2
,channel
);
944 if (!vp
->channel
.mc_head
->disconnect_flag
) return;
945 pvr2_v4l2_dev_disassociate_parent(vp
->dev_video
);
946 pvr2_v4l2_dev_disassociate_parent(vp
->dev_radio
);
947 if (vp
->vfirst
) return;
948 pvr2_v4l2_destroy_no_lock(vp
);
952 static long pvr2_v4l2_ioctl(struct file
*file
,
953 unsigned int cmd
, unsigned long arg
)
956 return video_usercopy(file
, cmd
, arg
, pvr2_v4l2_do_ioctl
);
960 static int pvr2_v4l2_release(struct file
*file
)
962 struct pvr2_v4l2_fh
*fhp
= file
->private_data
;
963 struct pvr2_v4l2
*vp
= fhp
->vhead
;
964 struct pvr2_hdw
*hdw
= fhp
->channel
.mc_head
->hdw
;
966 pvr2_trace(PVR2_TRACE_OPEN_CLOSE
,"pvr2_v4l2_release");
969 struct pvr2_stream
*sp
;
970 pvr2_hdw_set_streaming(hdw
,0);
971 sp
= pvr2_ioread_get_stream(fhp
->rhp
);
972 if (sp
) pvr2_stream_set_callback(sp
,NULL
,NULL
);
973 pvr2_ioread_destroy(fhp
->rhp
);
977 v4l2_prio_close(&vp
->prio
, fhp
->prio
);
978 file
->private_data
= NULL
;
981 fhp
->vnext
->vprev
= fhp
->vprev
;
983 vp
->vlast
= fhp
->vprev
;
986 fhp
->vprev
->vnext
= fhp
->vnext
;
988 vp
->vfirst
= fhp
->vnext
;
993 pvr2_channel_done(&fhp
->channel
);
994 pvr2_trace(PVR2_TRACE_STRUCT
,
995 "Destroying pvr_v4l2_fh id=%p",fhp
);
996 if (fhp
->input_map
) {
997 kfree(fhp
->input_map
);
998 fhp
->input_map
= NULL
;
1001 if (vp
->channel
.mc_head
->disconnect_flag
&& !vp
->vfirst
) {
1002 pvr2_v4l2_destroy_no_lock(vp
);
1008 static int pvr2_v4l2_open(struct file
*file
)
1010 struct pvr2_v4l2_dev
*dip
; /* Our own context pointer */
1011 struct pvr2_v4l2_fh
*fhp
;
1012 struct pvr2_v4l2
*vp
;
1013 struct pvr2_hdw
*hdw
;
1014 unsigned int input_mask
= 0;
1015 unsigned int input_cnt
,idx
;
1018 dip
= container_of(video_devdata(file
),struct pvr2_v4l2_dev
,devbase
);
1021 hdw
= vp
->channel
.hdw
;
1023 pvr2_trace(PVR2_TRACE_OPEN_CLOSE
,"pvr2_v4l2_open");
1025 if (!pvr2_hdw_dev_ok(hdw
)) {
1026 pvr2_trace(PVR2_TRACE_OPEN_CLOSE
,
1027 "pvr2_v4l2_open: hardware not ready");
1031 fhp
= kzalloc(sizeof(*fhp
),GFP_KERNEL
);
1036 init_waitqueue_head(&fhp
->wait_data
);
1039 pvr2_trace(PVR2_TRACE_STRUCT
,"Creating pvr_v4l2_fh id=%p",fhp
);
1040 pvr2_channel_init(&fhp
->channel
,vp
->channel
.mc_head
);
1042 if (dip
->v4l_type
== VFL_TYPE_RADIO
) {
1043 /* Opening device as a radio, legal input selection subset
1044 is just the radio. */
1045 input_mask
= (1 << PVR2_CVAL_INPUT_RADIO
);
1047 /* Opening the main V4L device, legal input selection
1048 subset includes all analog inputs. */
1049 input_mask
= ((1 << PVR2_CVAL_INPUT_RADIO
) |
1050 (1 << PVR2_CVAL_INPUT_TV
) |
1051 (1 << PVR2_CVAL_INPUT_COMPOSITE
) |
1052 (1 << PVR2_CVAL_INPUT_SVIDEO
));
1054 ret
= pvr2_channel_limit_inputs(&fhp
->channel
,input_mask
);
1056 pvr2_channel_done(&fhp
->channel
);
1057 pvr2_trace(PVR2_TRACE_STRUCT
,
1058 "Destroying pvr_v4l2_fh id=%p (input mask error)",
1065 input_mask
&= pvr2_hdw_get_input_available(hdw
);
1067 for (idx
= 0; idx
< (sizeof(input_mask
) << 3); idx
++) {
1068 if (input_mask
& (1 << idx
)) input_cnt
++;
1070 fhp
->input_cnt
= input_cnt
;
1071 fhp
->input_map
= kzalloc(input_cnt
,GFP_KERNEL
);
1072 if (!fhp
->input_map
) {
1073 pvr2_channel_done(&fhp
->channel
);
1074 pvr2_trace(PVR2_TRACE_STRUCT
,
1075 "Destroying pvr_v4l2_fh id=%p (input map failure)",
1081 for (idx
= 0; idx
< (sizeof(input_mask
) << 3); idx
++) {
1082 if (!(input_mask
& (1 << idx
))) continue;
1083 fhp
->input_map
[input_cnt
++] = idx
;
1087 fhp
->vprev
= vp
->vlast
;
1089 vp
->vlast
->vnext
= fhp
;
1097 file
->private_data
= fhp
;
1098 v4l2_prio_open(&vp
->prio
, &fhp
->prio
);
1100 fhp
->fw_mode_flag
= pvr2_hdw_cpufw_get_enabled(hdw
);
1106 static void pvr2_v4l2_notify(struct pvr2_v4l2_fh
*fhp
)
1108 wake_up(&fhp
->wait_data
);
1111 static int pvr2_v4l2_iosetup(struct pvr2_v4l2_fh
*fh
)
1114 struct pvr2_stream
*sp
;
1115 struct pvr2_hdw
*hdw
;
1116 if (fh
->rhp
) return 0;
1118 if (!fh
->pdi
->stream
) {
1119 /* No stream defined for this node. This means that we're
1120 not currently allowed to stream from this node. */
1124 /* First read() attempt. Try to claim the stream and start
1126 if ((ret
= pvr2_channel_claim_stream(&fh
->channel
,
1127 fh
->pdi
->stream
)) != 0) {
1128 /* Someone else must already have it */
1132 fh
->rhp
= pvr2_channel_create_mpeg_stream(fh
->pdi
->stream
);
1134 pvr2_channel_claim_stream(&fh
->channel
,NULL
);
1138 hdw
= fh
->channel
.mc_head
->hdw
;
1139 sp
= fh
->pdi
->stream
->stream
;
1140 pvr2_stream_set_callback(sp
,(pvr2_stream_callback
)pvr2_v4l2_notify
,fh
);
1141 pvr2_hdw_set_stream_type(hdw
,fh
->pdi
->config
);
1142 if ((ret
= pvr2_hdw_set_streaming(hdw
,!0)) < 0) return ret
;
1143 return pvr2_ioread_set_enabled(fh
->rhp
,!0);
1147 static ssize_t
pvr2_v4l2_read(struct file
*file
,
1148 char __user
*buff
, size_t count
, loff_t
*ppos
)
1150 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
1153 if (fh
->fw_mode_flag
) {
1154 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
1158 unsigned int offs
= *ppos
;
1160 tbuf
= kmalloc(PAGE_SIZE
,GFP_KERNEL
);
1161 if (!tbuf
) return -ENOMEM
;
1165 if (c1
> PAGE_SIZE
) c1
= PAGE_SIZE
;
1166 c2
= pvr2_hdw_cpufw_get(hdw
,offs
,tbuf
,c1
);
1172 if (copy_to_user(buff
,tbuf
,c2
)) {
1187 ret
= pvr2_v4l2_iosetup(fh
);
1194 ret
= pvr2_ioread_read(fh
->rhp
,buff
,count
);
1195 if (ret
>= 0) break;
1196 if (ret
!= -EAGAIN
) break;
1197 if (file
->f_flags
& O_NONBLOCK
) break;
1198 /* Doing blocking I/O. Wait here. */
1199 ret
= wait_event_interruptible(
1201 pvr2_ioread_avail(fh
->rhp
) >= 0);
1209 static unsigned int pvr2_v4l2_poll(struct file
*file
, poll_table
*wait
)
1211 unsigned int mask
= 0;
1212 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
1215 if (fh
->fw_mode_flag
) {
1216 mask
|= POLLIN
| POLLRDNORM
;
1221 ret
= pvr2_v4l2_iosetup(fh
);
1222 if (ret
) return POLLERR
;
1225 poll_wait(file
,&fh
->wait_data
,wait
);
1227 if (pvr2_ioread_avail(fh
->rhp
) >= 0) {
1228 mask
|= POLLIN
| POLLRDNORM
;
1235 static const struct v4l2_file_operations vdev_fops
= {
1236 .owner
= THIS_MODULE
,
1237 .open
= pvr2_v4l2_open
,
1238 .release
= pvr2_v4l2_release
,
1239 .read
= pvr2_v4l2_read
,
1240 .ioctl
= pvr2_v4l2_ioctl
,
1241 .poll
= pvr2_v4l2_poll
,
1245 static struct video_device vdev_template
= {
1250 static void pvr2_v4l2_dev_init(struct pvr2_v4l2_dev
*dip
,
1251 struct pvr2_v4l2
*vp
,
1254 struct usb_device
*usbdev
;
1260 usbdev
= pvr2_hdw_get_dev(vp
->channel
.mc_head
->hdw
);
1261 dip
->v4l_type
= v4l_type
;
1263 case VFL_TYPE_GRABBER
:
1264 dip
->stream
= &vp
->channel
.mc_head
->video_stream
;
1265 dip
->config
= pvr2_config_mpeg
;
1266 dip
->minor_type
= pvr2_v4l_type_video
;
1269 pr_err(KBUILD_MODNAME
1270 ": Failed to set up pvrusb2 v4l video dev"
1271 " due to missing stream instance\n");
1276 dip
->config
= pvr2_config_vbi
;
1277 dip
->minor_type
= pvr2_v4l_type_vbi
;
1280 case VFL_TYPE_RADIO
:
1281 dip
->stream
= &vp
->channel
.mc_head
->video_stream
;
1282 dip
->config
= pvr2_config_mpeg
;
1283 dip
->minor_type
= pvr2_v4l_type_radio
;
1287 /* Bail out (this should be impossible) */
1288 pr_err(KBUILD_MODNAME
": Failed to set up pvrusb2 v4l dev"
1289 " due to unrecognized config\n");
1293 memcpy(&dip
->devbase
,&vdev_template
,sizeof(vdev_template
));
1294 dip
->devbase
.release
= pvr2_video_device_release
;
1297 unit_number
= pvr2_hdw_get_unit_number(vp
->channel
.mc_head
->hdw
);
1298 if (nr_ptr
&& (unit_number
>= 0) && (unit_number
< PVR_NUM
)) {
1299 mindevnum
= nr_ptr
[unit_number
];
1301 dip
->devbase
.parent
= &usbdev
->dev
;
1302 if ((video_register_device(&dip
->devbase
,
1303 dip
->v4l_type
, mindevnum
) < 0) &&
1304 (video_register_device(&dip
->devbase
,
1305 dip
->v4l_type
, -1) < 0)) {
1306 pr_err(KBUILD_MODNAME
1307 ": Failed to register pvrusb2 v4l device\n");
1310 printk(KERN_INFO
"pvrusb2: registered device %s [%s]\n",
1311 video_device_node_name(&dip
->devbase
),
1312 pvr2_config_get_name(dip
->config
));
1314 pvr2_hdw_v4l_store_minor_number(vp
->channel
.mc_head
->hdw
,
1315 dip
->minor_type
,dip
->devbase
.minor
);
1319 struct pvr2_v4l2
*pvr2_v4l2_create(struct pvr2_context
*mnp
)
1321 struct pvr2_v4l2
*vp
;
1323 vp
= kzalloc(sizeof(*vp
),GFP_KERNEL
);
1325 pvr2_channel_init(&vp
->channel
,mnp
);
1326 pvr2_trace(PVR2_TRACE_STRUCT
,"Creating pvr2_v4l2 id=%p",vp
);
1328 vp
->channel
.check_func
= pvr2_v4l2_internal_check
;
1330 /* register streams */
1331 vp
->dev_video
= kzalloc(sizeof(*vp
->dev_video
),GFP_KERNEL
);
1332 if (!vp
->dev_video
) goto fail
;
1333 pvr2_v4l2_dev_init(vp
->dev_video
,vp
,VFL_TYPE_GRABBER
);
1334 if (pvr2_hdw_get_input_available(vp
->channel
.mc_head
->hdw
) &
1335 (1 << PVR2_CVAL_INPUT_RADIO
)) {
1336 vp
->dev_radio
= kzalloc(sizeof(*vp
->dev_radio
),GFP_KERNEL
);
1337 if (!vp
->dev_radio
) goto fail
;
1338 pvr2_v4l2_dev_init(vp
->dev_radio
,vp
,VFL_TYPE_RADIO
);
1343 pvr2_trace(PVR2_TRACE_STRUCT
,"Failure creating pvr2_v4l2 id=%p",vp
);
1344 pvr2_v4l2_destroy_no_lock(vp
);
1349 Stuff for Emacs to see, in order to encourage consistent editing style:
1350 *** Local Variables: ***
1352 *** fill-column: 75 ***
1353 *** tab-width: 8 ***
1354 *** c-basic-offset: 8 ***