5 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
6 * Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/kernel.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>
39 struct pvr2_v4l2_dev
{
40 struct video_device devbase
; /* MUST be first! */
41 struct pvr2_v4l2
*v4lp
;
42 struct pvr2_context_stream
*stream
;
43 /* Information about this device: */
44 enum pvr2_config config
; /* Expected stream format */
45 int v4l_type
; /* V4L defined type for this device node */
46 enum pvr2_v4l_type minor_type
; /* pvr2-understood minor device type */
50 struct pvr2_channel channel
;
51 struct pvr2_v4l2_dev
*dev_info
;
52 enum v4l2_priority prio
;
53 struct pvr2_ioread
*rhp
;
55 struct pvr2_v4l2
*vhead
;
56 struct pvr2_v4l2_fh
*vnext
;
57 struct pvr2_v4l2_fh
*vprev
;
58 wait_queue_head_t wait_data
;
64 struct pvr2_channel channel
;
65 struct pvr2_v4l2_fh
*vfirst
;
66 struct pvr2_v4l2_fh
*vlast
;
68 struct v4l2_prio_state prio
;
70 /* streams - Note that these must be separately, individually,
71 * allocated pointers. This is because the v4l core is going to
72 * manage their deletion - separately, individually... */
73 struct pvr2_v4l2_dev
*dev_video
;
74 struct pvr2_v4l2_dev
*dev_radio
;
77 static int video_nr
[PVR_NUM
] = {[0 ... PVR_NUM
-1] = -1};
78 module_param_array(video_nr
, int, NULL
, 0444);
79 MODULE_PARM_DESC(video_nr
, "Offset for device's video dev minor");
80 static int radio_nr
[PVR_NUM
] = {[0 ... PVR_NUM
-1] = -1};
81 module_param_array(radio_nr
, int, NULL
, 0444);
82 MODULE_PARM_DESC(radio_nr
, "Offset for device's radio dev minor");
83 static int vbi_nr
[PVR_NUM
] = {[0 ... PVR_NUM
-1] = -1};
84 module_param_array(vbi_nr
, int, NULL
, 0444);
85 MODULE_PARM_DESC(vbi_nr
, "Offset for device's vbi dev minor");
87 static struct v4l2_capability pvr_capability
={
89 .card
= "Hauppauge WinTV pvr-usb2",
91 .version
= KERNEL_VERSION(0,8,0),
92 .capabilities
= (V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_VBI_CAPTURE
|
93 V4L2_CAP_TUNER
| V4L2_CAP_AUDIO
| V4L2_CAP_RADIO
|
98 static struct v4l2_fmtdesc pvr_fmtdesc
[] = {
101 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
102 .flags
= V4L2_FMT_FLAG_COMPRESSED
,
103 .description
= "MPEG1/2",
104 // This should really be V4L2_PIX_FMT_MPEG, but xawtv
105 // breaks when I do that.
106 .pixelformat
= 0, // V4L2_PIX_FMT_MPEG,
107 .reserved
= { 0, 0, 0, 0 }
111 #define PVR_FORMAT_PIX 0
112 #define PVR_FORMAT_VBI 1
114 static struct v4l2_format pvr_format
[] = {
116 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
121 // This should really be V4L2_PIX_FMT_MPEG,
122 // but xawtv breaks when I do that.
123 .pixelformat
= 0, // V4L2_PIX_FMT_MPEG,
124 .field
= V4L2_FIELD_INTERLACED
,
125 .bytesperline
= 0, // doesn't make sense
127 //FIXME : Don't know what to put here...
128 .sizeimage
= (32*1024),
129 .colorspace
= 0, // doesn't make sense here
135 .type
= V4L2_BUF_TYPE_VBI_CAPTURE
,
138 .sampling_rate
= 27000000,
140 .samples_per_line
= 1443,
141 .sample_format
= V4L2_PIX_FMT_GREY
,
152 static const char *get_v4l_name(int v4l_type
)
155 case VFL_TYPE_GRABBER
: return "video";
156 case VFL_TYPE_RADIO
: return "radio";
157 case VFL_TYPE_VBI
: return "vbi";
166 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
169 static int pvr2_v4l2_do_ioctl(struct inode
*inode
, struct file
*file
,
170 unsigned int cmd
, void *arg
)
172 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
173 struct pvr2_v4l2
*vp
= fh
->vhead
;
174 struct pvr2_v4l2_dev
*dev_info
= fh
->dev_info
;
175 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
178 if (pvrusb2_debug
& PVR2_TRACE_V4LIOCTL
) {
179 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw
),cmd
);
182 if (!pvr2_hdw_dev_ok(hdw
)) {
183 pvr2_trace(PVR2_TRACE_ERROR_LEGS
,
184 "ioctl failed - bad or no context");
194 case VIDIOC_S_FREQUENCY
:
195 ret
= v4l2_prio_check(&vp
->prio
, &fh
->prio
);
201 case VIDIOC_QUERYCAP
:
203 struct v4l2_capability
*cap
= arg
;
205 memcpy(cap
, &pvr_capability
, sizeof(struct v4l2_capability
));
206 strlcpy(cap
->bus_info
,pvr2_hdw_get_bus_info(hdw
),
207 sizeof(cap
->bus_info
));
208 strlcpy(cap
->card
,pvr2_hdw_get_desc(hdw
),sizeof(cap
->card
));
214 case VIDIOC_G_PRIORITY
:
216 enum v4l2_priority
*p
= arg
;
218 *p
= v4l2_prio_max(&vp
->prio
);
223 case VIDIOC_S_PRIORITY
:
225 enum v4l2_priority
*prio
= arg
;
227 ret
= v4l2_prio_change(&vp
->prio
, &fh
->prio
, *prio
);
233 struct v4l2_standard
*vs
= (struct v4l2_standard
*)arg
;
235 ret
= pvr2_hdw_get_stdenum_value(hdw
,vs
,idx
+1);
242 ret
= pvr2_ctrl_get_value(
243 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_STDCUR
),&val
);
244 *(v4l2_std_id
*)arg
= val
;
250 ret
= pvr2_ctrl_set_value(
251 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_STDCUR
),
252 *(v4l2_std_id
*)arg
);
256 case VIDIOC_ENUMINPUT
:
258 struct pvr2_ctrl
*cptr
;
259 struct v4l2_input
*vi
= (struct v4l2_input
*)arg
;
260 struct v4l2_input tmp
;
263 cptr
= pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_INPUT
);
265 memset(&tmp
,0,sizeof(tmp
));
266 tmp
.index
= vi
->index
;
269 case PVR2_CVAL_INPUT_TV
:
270 case PVR2_CVAL_INPUT_RADIO
:
271 tmp
.type
= V4L2_INPUT_TYPE_TUNER
;
273 case PVR2_CVAL_INPUT_SVIDEO
:
274 case PVR2_CVAL_INPUT_COMPOSITE
:
275 tmp
.type
= V4L2_INPUT_TYPE_CAMERA
;
284 pvr2_ctrl_get_valname(cptr
,vi
->index
,
285 tmp
.name
,sizeof(tmp
.name
)-1,&cnt
);
288 /* Don't bother with audioset, since this driver currently
289 always switches the audio whenever the video is
292 /* Handling std is a tougher problem. It doesn't make
293 sense in cases where a device might be multi-standard.
294 We could just copy out the current value for the
295 standard, but it can change over time. For now just
298 memcpy(vi
, &tmp
, sizeof(tmp
));
306 struct pvr2_ctrl
*cptr
;
307 struct v4l2_input
*vi
= (struct v4l2_input
*)arg
;
309 cptr
= pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_INPUT
);
311 ret
= pvr2_ctrl_get_value(cptr
,&val
);
318 struct v4l2_input
*vi
= (struct v4l2_input
*)arg
;
319 ret
= pvr2_ctrl_set_value(
320 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_INPUT
),
325 case VIDIOC_ENUMAUDIO
:
327 /* pkt: FIXME: We are returning one "fake" input here
328 which could very well be called "whatever_we_like".
329 This is for apps that want to see an audio input
330 just to feel comfortable, as well as to test if
331 it can do stereo or sth. There is actually no guarantee
332 that the actual audio input cannot change behind the app's
333 back, but most applications should not mind that either.
335 Hopefully, mplayer people will work with us on this (this
336 whole mess is to support mplayer pvr://), or Hans will come
337 up with a more standard way to say "we have inputs but we
338 don 't want you to change them independent of video" which
341 struct v4l2_audio
*vin
= arg
;
343 if (vin
->index
> 0) break;
344 strncpy(vin
->name
, "PVRUSB2 Audio",14);
345 vin
->capability
= V4L2_AUDCAP_STEREO
;
353 /* pkt: FIXME: see above comment (VIDIOC_ENUMAUDIO) */
354 struct v4l2_audio
*vin
= arg
;
355 memset(vin
,0,sizeof(*vin
));
357 strncpy(vin
->name
, "PVRUSB2 Audio",14);
358 vin
->capability
= V4L2_AUDCAP_STEREO
;
370 struct v4l2_tuner
*vt
= (struct v4l2_tuner
*)arg
;
372 if (vt
->index
!= 0) break; /* Only answer for the 1st tuner */
374 pvr2_hdw_execute_tuner_poll(hdw
);
375 ret
= pvr2_hdw_get_tuner_status(hdw
,vt
);
381 struct v4l2_tuner
*vt
=(struct v4l2_tuner
*)arg
;
386 ret
= pvr2_ctrl_set_value(
387 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_AUDIOMODE
),
392 case VIDIOC_S_FREQUENCY
:
394 const struct v4l2_frequency
*vf
= (struct v4l2_frequency
*)arg
;
396 struct v4l2_tuner vt
;
398 struct pvr2_ctrl
*ctrlp
;
399 ret
= pvr2_hdw_get_tuner_status(hdw
,&vt
);
401 ctrlp
= pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_INPUT
);
402 ret
= pvr2_ctrl_get_value(ctrlp
,&cur_input
);
404 if (vf
->type
== V4L2_TUNER_RADIO
) {
405 if (cur_input
!= PVR2_CVAL_INPUT_RADIO
) {
406 pvr2_ctrl_set_value(ctrlp
,
407 PVR2_CVAL_INPUT_RADIO
);
410 if (cur_input
== PVR2_CVAL_INPUT_RADIO
) {
411 pvr2_ctrl_set_value(ctrlp
,
416 if (vt
.capability
& V4L2_TUNER_CAP_LOW
) {
421 ret
= pvr2_ctrl_set_value(
422 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_FREQUENCY
),fv
);
426 case VIDIOC_G_FREQUENCY
:
428 struct v4l2_frequency
*vf
= (struct v4l2_frequency
*)arg
;
431 struct v4l2_tuner vt
;
432 ret
= pvr2_hdw_get_tuner_status(hdw
,&vt
);
434 ret
= pvr2_ctrl_get_value(
435 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_FREQUENCY
),
439 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_INPUT
),
441 if (cur_input
== PVR2_CVAL_INPUT_RADIO
) {
442 vf
->type
= V4L2_TUNER_RADIO
;
444 vf
->type
= V4L2_TUNER_ANALOG_TV
;
446 if (vt
.capability
& V4L2_TUNER_CAP_LOW
) {
447 val
= (val
* 2) / 125;
455 case VIDIOC_ENUM_FMT
:
457 struct v4l2_fmtdesc
*fd
= (struct v4l2_fmtdesc
*)arg
;
459 /* Only one format is supported : mpeg.*/
463 memcpy(fd
, pvr_fmtdesc
, sizeof(struct v4l2_fmtdesc
));
470 struct v4l2_format
*vf
= (struct v4l2_format
*)arg
;
473 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
474 memcpy(vf
, &pvr_format
[PVR_FORMAT_PIX
],
475 sizeof(struct v4l2_format
));
478 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_HRES
),
480 vf
->fmt
.pix
.width
= val
;
483 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_VRES
),
485 vf
->fmt
.pix
.height
= val
;
488 case V4L2_BUF_TYPE_VBI_CAPTURE
:
489 // ????? Still need to figure out to do VBI correctly
502 struct v4l2_format
*vf
= (struct v4l2_format
*)arg
;
506 case V4L2_BUF_TYPE_VIDEO_CAPTURE
: {
508 struct pvr2_ctrl
*hcp
,*vcp
;
509 int h
= vf
->fmt
.pix
.height
;
510 int w
= vf
->fmt
.pix
.width
;
511 hcp
= pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_HRES
);
512 vcp
= pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_VRES
);
514 lmin
= pvr2_ctrl_get_min(hcp
);
515 lmax
= pvr2_ctrl_get_max(hcp
);
516 ldef
= pvr2_ctrl_get_def(hcp
);
519 } else if (w
< lmin
) {
521 } else if (w
> lmax
) {
524 lmin
= pvr2_ctrl_get_min(vcp
);
525 lmax
= pvr2_ctrl_get_max(vcp
);
526 ldef
= pvr2_ctrl_get_def(vcp
);
529 } else if (h
< lmin
) {
531 } else if (h
> lmax
) {
535 memcpy(vf
, &pvr_format
[PVR_FORMAT_PIX
],
536 sizeof(struct v4l2_format
));
537 vf
->fmt
.pix
.width
= w
;
538 vf
->fmt
.pix
.height
= h
;
540 if (cmd
== VIDIOC_S_FMT
) {
541 pvr2_ctrl_set_value(hcp
,vf
->fmt
.pix
.width
);
542 pvr2_ctrl_set_value(vcp
,vf
->fmt
.pix
.height
);
545 case V4L2_BUF_TYPE_VBI_CAPTURE
:
546 // ????? Still need to figure out to do VBI correctly
556 case VIDIOC_STREAMON
:
558 if (!fh
->dev_info
->stream
) {
559 /* No stream defined for this node. This means
560 that we're not currently allowed to stream from
565 ret
= pvr2_hdw_set_stream_type(hdw
,dev_info
->config
);
566 if (ret
< 0) return ret
;
567 ret
= pvr2_hdw_set_streaming(hdw
,!0);
571 case VIDIOC_STREAMOFF
:
573 if (!fh
->dev_info
->stream
) {
574 /* No stream defined for this node. This means
575 that we're not currently allowed to stream from
580 ret
= pvr2_hdw_set_streaming(hdw
,0);
584 case VIDIOC_QUERYCTRL
:
586 struct pvr2_ctrl
*cptr
;
587 struct v4l2_queryctrl
*vc
= (struct v4l2_queryctrl
*)arg
;
589 if (vc
->id
& V4L2_CTRL_FLAG_NEXT_CTRL
) {
590 cptr
= pvr2_hdw_get_ctrl_nextv4l(
591 hdw
,(vc
->id
& ~V4L2_CTRL_FLAG_NEXT_CTRL
));
592 if (cptr
) vc
->id
= pvr2_ctrl_get_v4lid(cptr
);
594 cptr
= pvr2_hdw_get_ctrl_v4l(hdw
,vc
->id
);
597 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
598 "QUERYCTRL id=0x%x not implemented here",
604 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
605 "QUERYCTRL id=0x%x mapping name=%s (%s)",
606 vc
->id
,pvr2_ctrl_get_name(cptr
),
607 pvr2_ctrl_get_desc(cptr
));
608 strlcpy(vc
->name
,pvr2_ctrl_get_desc(cptr
),sizeof(vc
->name
));
609 vc
->flags
= pvr2_ctrl_get_v4lflags(cptr
);
610 vc
->default_value
= pvr2_ctrl_get_def(cptr
);
611 switch (pvr2_ctrl_get_type(cptr
)) {
613 vc
->type
= V4L2_CTRL_TYPE_MENU
;
615 vc
->maximum
= pvr2_ctrl_get_cnt(cptr
) - 1;
619 vc
->type
= V4L2_CTRL_TYPE_BOOLEAN
;
625 vc
->type
= V4L2_CTRL_TYPE_INTEGER
;
626 vc
->minimum
= pvr2_ctrl_get_min(cptr
);
627 vc
->maximum
= pvr2_ctrl_get_max(cptr
);
631 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
632 "QUERYCTRL id=0x%x name=%s not mappable",
633 vc
->id
,pvr2_ctrl_get_name(cptr
));
640 case VIDIOC_QUERYMENU
:
642 struct v4l2_querymenu
*vm
= (struct v4l2_querymenu
*)arg
;
643 unsigned int cnt
= 0;
644 ret
= pvr2_ctrl_get_valname(pvr2_hdw_get_ctrl_v4l(hdw
,vm
->id
),
646 vm
->name
,sizeof(vm
->name
)-1,
654 struct v4l2_control
*vc
= (struct v4l2_control
*)arg
;
656 ret
= pvr2_ctrl_get_value(pvr2_hdw_get_ctrl_v4l(hdw
,vc
->id
),
664 struct v4l2_control
*vc
= (struct v4l2_control
*)arg
;
665 ret
= pvr2_ctrl_set_value(pvr2_hdw_get_ctrl_v4l(hdw
,vc
->id
),
670 case VIDIOC_G_EXT_CTRLS
:
672 struct v4l2_ext_controls
*ctls
=
673 (struct v4l2_ext_controls
*)arg
;
674 struct v4l2_ext_control
*ctrl
;
678 for (idx
= 0; idx
< ctls
->count
; idx
++) {
679 ctrl
= ctls
->controls
+ idx
;
680 ret
= pvr2_ctrl_get_value(
681 pvr2_hdw_get_ctrl_v4l(hdw
,ctrl
->id
),&val
);
683 ctls
->error_idx
= idx
;
686 /* Ensure that if read as a 64 bit value, the user
687 will still get a hopefully sane value */
694 case VIDIOC_S_EXT_CTRLS
:
696 struct v4l2_ext_controls
*ctls
=
697 (struct v4l2_ext_controls
*)arg
;
698 struct v4l2_ext_control
*ctrl
;
701 for (idx
= 0; idx
< ctls
->count
; idx
++) {
702 ctrl
= ctls
->controls
+ idx
;
703 ret
= pvr2_ctrl_set_value(
704 pvr2_hdw_get_ctrl_v4l(hdw
,ctrl
->id
),
707 ctls
->error_idx
= idx
;
714 case VIDIOC_TRY_EXT_CTRLS
:
716 struct v4l2_ext_controls
*ctls
=
717 (struct v4l2_ext_controls
*)arg
;
718 struct v4l2_ext_control
*ctrl
;
719 struct pvr2_ctrl
*pctl
;
721 /* For the moment just validate that the requested control
724 for (idx
= 0; idx
< ctls
->count
; idx
++) {
725 ctrl
= ctls
->controls
+ idx
;
726 pctl
= pvr2_hdw_get_ctrl_v4l(hdw
,ctrl
->id
);
729 ctls
->error_idx
= idx
;
736 case VIDIOC_LOG_STATUS
:
738 pvr2_hdw_trigger_module_log(hdw
);
742 #ifdef CONFIG_VIDEO_ADV_DEBUG
743 case VIDIOC_DBG_S_REGISTER
:
744 case VIDIOC_DBG_G_REGISTER
:
747 struct v4l2_register
*req
= (struct v4l2_register
*)arg
;
748 if (cmd
== VIDIOC_DBG_S_REGISTER
) val
= req
->val
;
749 ret
= pvr2_hdw_register_access(
750 hdw
,req
->match_type
,req
->match_chip
,req
->reg
,
751 cmd
== VIDIOC_DBG_S_REGISTER
,&val
);
752 if (cmd
== VIDIOC_DBG_G_REGISTER
) req
->val
= val
;
758 ret
= v4l_compat_translate_ioctl(inode
,file
,cmd
,
759 arg
,pvr2_v4l2_do_ioctl
);
762 pvr2_hdw_commit_ctl(hdw
);
765 if (pvrusb2_debug
& PVR2_TRACE_V4LIOCTL
) {
766 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
767 "pvr2_v4l2_do_ioctl failure, ret=%d",ret
);
769 if (pvrusb2_debug
& PVR2_TRACE_V4LIOCTL
) {
770 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
771 "pvr2_v4l2_do_ioctl failure, ret=%d"
772 " command was:",ret
);
773 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw
),
778 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
779 "pvr2_v4l2_do_ioctl complete, ret=%d (0x%x)",
786 static void pvr2_v4l2_dev_destroy(struct pvr2_v4l2_dev
*dip
)
788 int minor_id
= dip
->devbase
.minor
;
789 struct pvr2_hdw
*hdw
= dip
->v4lp
->channel
.mc_head
->hdw
;
790 enum pvr2_config cfg
= dip
->config
;
791 int v4l_type
= dip
->v4l_type
;
793 pvr2_hdw_v4l_store_minor_number(hdw
,dip
->minor_type
,-1);
799 /* Actual deallocation happens later when all internal references
801 video_unregister_device(&dip
->devbase
);
803 printk(KERN_INFO
"pvrusb2: unregistered device %s%u [%s]\n",
804 get_v4l_name(v4l_type
),minor_id
& 0x1f,
805 pvr2_config_get_name(cfg
));
810 static void pvr2_v4l2_destroy_no_lock(struct pvr2_v4l2
*vp
)
813 pvr2_v4l2_dev_destroy(vp
->dev_video
);
814 vp
->dev_video
= NULL
;
817 pvr2_v4l2_dev_destroy(vp
->dev_radio
);
818 vp
->dev_radio
= NULL
;
821 pvr2_trace(PVR2_TRACE_STRUCT
,"Destroying pvr2_v4l2 id=%p",vp
);
822 pvr2_channel_done(&vp
->channel
);
827 static void pvr2_video_device_release(struct video_device
*vdev
)
829 struct pvr2_v4l2_dev
*dev
;
830 dev
= container_of(vdev
,struct pvr2_v4l2_dev
,devbase
);
835 static void pvr2_v4l2_internal_check(struct pvr2_channel
*chp
)
837 struct pvr2_v4l2
*vp
;
838 vp
= container_of(chp
,struct pvr2_v4l2
,channel
);
839 if (!vp
->channel
.mc_head
->disconnect_flag
) return;
840 if (vp
->vfirst
) return;
841 pvr2_v4l2_destroy_no_lock(vp
);
845 static int pvr2_v4l2_ioctl(struct inode
*inode
, struct file
*file
,
846 unsigned int cmd
, unsigned long arg
)
849 /* Temporary hack : use ivtv api until a v4l2 one is available. */
850 #define IVTV_IOC_G_CODEC 0xFFEE7703
851 #define IVTV_IOC_S_CODEC 0xFFEE7704
852 if (cmd
== IVTV_IOC_G_CODEC
|| cmd
== IVTV_IOC_S_CODEC
) return 0;
853 return video_usercopy(inode
, file
, cmd
, arg
, pvr2_v4l2_do_ioctl
);
857 static int pvr2_v4l2_release(struct inode
*inode
, struct file
*file
)
859 struct pvr2_v4l2_fh
*fhp
= file
->private_data
;
860 struct pvr2_v4l2
*vp
= fhp
->vhead
;
861 struct pvr2_context
*mp
= fhp
->vhead
->channel
.mc_head
;
862 struct pvr2_hdw
*hdw
= fhp
->channel
.mc_head
->hdw
;
864 pvr2_trace(PVR2_TRACE_OPEN_CLOSE
,"pvr2_v4l2_release");
867 struct pvr2_stream
*sp
;
868 pvr2_hdw_set_streaming(hdw
,0);
869 sp
= pvr2_ioread_get_stream(fhp
->rhp
);
870 if (sp
) pvr2_stream_set_callback(sp
,NULL
,NULL
);
871 pvr2_ioread_destroy(fhp
->rhp
);
875 v4l2_prio_close(&vp
->prio
, &fhp
->prio
);
876 file
->private_data
= NULL
;
878 pvr2_context_enter(mp
); do {
879 /* Restore the previous input selection, if it makes sense
881 if (fhp
->dev_info
->v4l_type
== VFL_TYPE_RADIO
) {
882 struct pvr2_ctrl
*cp
;
884 cp
= pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_INPUT
);
885 pvr2_ctrl_get_value(cp
,&pval
);
886 /* Only restore if we're still selecting the radio */
887 if (pval
== PVR2_CVAL_INPUT_RADIO
) {
888 pvr2_ctrl_set_value(cp
,fhp
->prev_input_val
);
889 pvr2_hdw_commit_ctl(hdw
);
894 fhp
->vnext
->vprev
= fhp
->vprev
;
896 vp
->vlast
= fhp
->vprev
;
899 fhp
->vprev
->vnext
= fhp
->vnext
;
901 vp
->vfirst
= fhp
->vnext
;
906 pvr2_channel_done(&fhp
->channel
);
907 pvr2_trace(PVR2_TRACE_STRUCT
,
908 "Destroying pvr_v4l2_fh id=%p",fhp
);
910 if (vp
->channel
.mc_head
->disconnect_flag
&& !vp
->vfirst
) {
911 pvr2_v4l2_destroy_no_lock(vp
);
913 } while (0); pvr2_context_exit(mp
);
918 static int pvr2_v4l2_open(struct inode
*inode
, struct file
*file
)
920 struct pvr2_v4l2_dev
*dip
; /* Our own context pointer */
921 struct pvr2_v4l2_fh
*fhp
;
922 struct pvr2_v4l2
*vp
;
923 struct pvr2_hdw
*hdw
;
925 dip
= container_of(video_devdata(file
),struct pvr2_v4l2_dev
,devbase
);
928 hdw
= vp
->channel
.hdw
;
930 pvr2_trace(PVR2_TRACE_OPEN_CLOSE
,"pvr2_v4l2_open");
932 if (!pvr2_hdw_dev_ok(hdw
)) {
933 pvr2_trace(PVR2_TRACE_OPEN_CLOSE
,
934 "pvr2_v4l2_open: hardware not ready");
938 fhp
= kzalloc(sizeof(*fhp
),GFP_KERNEL
);
943 init_waitqueue_head(&fhp
->wait_data
);
946 pvr2_context_enter(vp
->channel
.mc_head
); do {
947 pvr2_trace(PVR2_TRACE_STRUCT
,"Creating pvr_v4l2_fh id=%p",fhp
);
948 pvr2_channel_init(&fhp
->channel
,vp
->channel
.mc_head
);
951 fhp
->vprev
= vp
->vlast
;
953 vp
->vlast
->vnext
= fhp
;
960 /* Opening the /dev/radioX device implies a mode switch.
961 So execute that here. Note that you can get the
962 IDENTICAL effect merely by opening the normal video
963 device and setting the input appropriately. */
964 if (dip
->v4l_type
== VFL_TYPE_RADIO
) {
965 struct pvr2_ctrl
*cp
;
966 cp
= pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_INPUT
);
967 pvr2_ctrl_get_value(cp
,&fhp
->prev_input_val
);
968 pvr2_ctrl_set_value(cp
,PVR2_CVAL_INPUT_RADIO
);
969 pvr2_hdw_commit_ctl(hdw
);
971 } while (0); pvr2_context_exit(vp
->channel
.mc_head
);
974 file
->private_data
= fhp
;
975 v4l2_prio_open(&vp
->prio
,&fhp
->prio
);
977 fhp
->fw_mode_flag
= pvr2_hdw_cpufw_get_enabled(hdw
);
983 static void pvr2_v4l2_notify(struct pvr2_v4l2_fh
*fhp
)
985 wake_up(&fhp
->wait_data
);
988 static int pvr2_v4l2_iosetup(struct pvr2_v4l2_fh
*fh
)
991 struct pvr2_stream
*sp
;
992 struct pvr2_hdw
*hdw
;
993 if (fh
->rhp
) return 0;
995 if (!fh
->dev_info
->stream
) {
996 /* No stream defined for this node. This means that we're
997 not currently allowed to stream from this node. */
1001 /* First read() attempt. Try to claim the stream and start
1003 if ((ret
= pvr2_channel_claim_stream(&fh
->channel
,
1004 fh
->dev_info
->stream
)) != 0) {
1005 /* Someone else must already have it */
1009 fh
->rhp
= pvr2_channel_create_mpeg_stream(fh
->dev_info
->stream
);
1011 pvr2_channel_claim_stream(&fh
->channel
,NULL
);
1015 hdw
= fh
->channel
.mc_head
->hdw
;
1016 sp
= fh
->dev_info
->stream
->stream
;
1017 pvr2_stream_set_callback(sp
,(pvr2_stream_callback
)pvr2_v4l2_notify
,fh
);
1018 pvr2_hdw_set_stream_type(hdw
,fh
->dev_info
->config
);
1019 if ((ret
= pvr2_hdw_set_streaming(hdw
,!0)) < 0) return ret
;
1020 return pvr2_ioread_set_enabled(fh
->rhp
,!0);
1024 static ssize_t
pvr2_v4l2_read(struct file
*file
,
1025 char __user
*buff
, size_t count
, loff_t
*ppos
)
1027 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
1030 if (fh
->fw_mode_flag
) {
1031 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
1035 unsigned int offs
= *ppos
;
1037 tbuf
= kmalloc(PAGE_SIZE
,GFP_KERNEL
);
1038 if (!tbuf
) return -ENOMEM
;
1042 if (c1
> PAGE_SIZE
) c1
= PAGE_SIZE
;
1043 c2
= pvr2_hdw_cpufw_get(hdw
,offs
,tbuf
,c1
);
1049 if (copy_to_user(buff
,tbuf
,c2
)) {
1064 ret
= pvr2_v4l2_iosetup(fh
);
1071 ret
= pvr2_ioread_read(fh
->rhp
,buff
,count
);
1072 if (ret
>= 0) break;
1073 if (ret
!= -EAGAIN
) break;
1074 if (file
->f_flags
& O_NONBLOCK
) break;
1075 /* Doing blocking I/O. Wait here. */
1076 ret
= wait_event_interruptible(
1078 pvr2_ioread_avail(fh
->rhp
) >= 0);
1086 static unsigned int pvr2_v4l2_poll(struct file
*file
, poll_table
*wait
)
1088 unsigned int mask
= 0;
1089 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
1092 if (fh
->fw_mode_flag
) {
1093 mask
|= POLLIN
| POLLRDNORM
;
1098 ret
= pvr2_v4l2_iosetup(fh
);
1099 if (ret
) return POLLERR
;
1102 poll_wait(file
,&fh
->wait_data
,wait
);
1104 if (pvr2_ioread_avail(fh
->rhp
) >= 0) {
1105 mask
|= POLLIN
| POLLRDNORM
;
1112 static const struct file_operations vdev_fops
= {
1113 .owner
= THIS_MODULE
,
1114 .open
= pvr2_v4l2_open
,
1115 .release
= pvr2_v4l2_release
,
1116 .read
= pvr2_v4l2_read
,
1117 .ioctl
= pvr2_v4l2_ioctl
,
1118 .llseek
= no_llseek
,
1119 .poll
= pvr2_v4l2_poll
,
1123 static struct video_device vdev_template
= {
1124 .owner
= THIS_MODULE
,
1125 .type
= VID_TYPE_CAPTURE
| VID_TYPE_TUNER
,
1126 .type2
= (V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_VBI_CAPTURE
1127 | V4L2_CAP_TUNER
| V4L2_CAP_AUDIO
1128 | V4L2_CAP_READWRITE
),
1133 static void pvr2_v4l2_dev_init(struct pvr2_v4l2_dev
*dip
,
1134 struct pvr2_v4l2
*vp
,
1143 dip
->v4l_type
= v4l_type
;
1145 case VFL_TYPE_GRABBER
:
1146 dip
->stream
= &vp
->channel
.mc_head
->video_stream
;
1147 dip
->config
= pvr2_config_mpeg
;
1148 dip
->minor_type
= pvr2_v4l_type_video
;
1151 err("Failed to set up pvrusb2 v4l video dev"
1152 " due to missing stream instance");
1157 dip
->config
= pvr2_config_vbi
;
1158 dip
->minor_type
= pvr2_v4l_type_vbi
;
1161 case VFL_TYPE_RADIO
:
1162 dip
->stream
= &vp
->channel
.mc_head
->video_stream
;
1163 dip
->config
= pvr2_config_mpeg
;
1164 dip
->minor_type
= pvr2_v4l_type_radio
;
1168 /* Bail out (this should be impossible) */
1169 err("Failed to set up pvrusb2 v4l dev"
1170 " due to unrecognized config");
1174 memcpy(&dip
->devbase
,&vdev_template
,sizeof(vdev_template
));
1175 dip
->devbase
.release
= pvr2_video_device_release
;
1178 unit_number
= pvr2_hdw_get_unit_number(vp
->channel
.mc_head
->hdw
);
1179 if (nr_ptr
&& (unit_number
>= 0) && (unit_number
< PVR_NUM
)) {
1180 mindevnum
= nr_ptr
[unit_number
];
1182 if ((video_register_device(&dip
->devbase
,
1183 dip
->v4l_type
, mindevnum
) < 0) &&
1184 (video_register_device(&dip
->devbase
,
1185 dip
->v4l_type
, -1) < 0)) {
1186 err("Failed to register pvrusb2 v4l device");
1189 printk(KERN_INFO
"pvrusb2: registered device %s%u [%s]\n",
1190 get_v4l_name(dip
->v4l_type
),dip
->devbase
.minor
& 0x1f,
1191 pvr2_config_get_name(dip
->config
));
1193 pvr2_hdw_v4l_store_minor_number(vp
->channel
.mc_head
->hdw
,
1194 dip
->minor_type
,dip
->devbase
.minor
);
1198 struct pvr2_v4l2
*pvr2_v4l2_create(struct pvr2_context
*mnp
)
1200 struct pvr2_v4l2
*vp
;
1202 vp
= kzalloc(sizeof(*vp
),GFP_KERNEL
);
1204 vp
->dev_video
= kzalloc(sizeof(*vp
->dev_video
),GFP_KERNEL
);
1205 vp
->dev_radio
= kzalloc(sizeof(*vp
->dev_radio
),GFP_KERNEL
);
1206 if (!(vp
->dev_video
&& vp
->dev_radio
)) {
1207 kfree(vp
->dev_video
);
1208 kfree(vp
->dev_radio
);
1212 pvr2_channel_init(&vp
->channel
,mnp
);
1213 pvr2_trace(PVR2_TRACE_STRUCT
,"Creating pvr2_v4l2 id=%p",vp
);
1215 vp
->channel
.check_func
= pvr2_v4l2_internal_check
;
1217 /* register streams */
1218 pvr2_v4l2_dev_init(vp
->dev_video
,vp
,VFL_TYPE_GRABBER
);
1219 pvr2_v4l2_dev_init(vp
->dev_radio
,vp
,VFL_TYPE_RADIO
);
1225 Stuff for Emacs to see, in order to encourage consistent editing style:
1226 *** Local Variables: ***
1228 *** fill-column: 75 ***
1229 *** tab-width: 8 ***
1230 *** c-basic-offset: 8 ***