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 <linux/module.h>
33 #include <media/v4l2-dev.h>
34 #include <media/v4l2-common.h>
35 #include <media/v4l2-ioctl.h>
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 */
52 struct pvr2_channel channel
;
53 struct pvr2_v4l2_dev
*pdi
;
54 enum v4l2_priority prio
;
55 struct pvr2_ioread
*rhp
;
57 struct pvr2_v4l2
*vhead
;
58 struct pvr2_v4l2_fh
*vnext
;
59 struct pvr2_v4l2_fh
*vprev
;
60 wait_queue_head_t wait_data
;
62 /* Map contiguous ordinal value to input id */
63 unsigned char *input_map
;
64 unsigned int input_cnt
;
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
={
93 .card
= "Hauppauge WinTV pvr-usb2",
95 .version
= LINUX_VERSION_CODE
,
96 .capabilities
= (V4L2_CAP_VIDEO_CAPTURE
|
97 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,
113 #define PVR_FORMAT_PIX 0
114 #define PVR_FORMAT_VBI 1
116 static struct v4l2_format pvr_format
[] = {
118 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
123 // This should really be V4L2_PIX_FMT_MPEG,
124 // but xawtv breaks when I do that.
125 .pixelformat
= 0, // V4L2_PIX_FMT_MPEG,
126 .field
= V4L2_FIELD_INTERLACED
,
127 .bytesperline
= 0, // doesn't make sense
129 //FIXME : Don't know what to put here...
130 .sizeimage
= (32*1024),
131 .colorspace
= 0, // doesn't make sense here
137 .type
= V4L2_BUF_TYPE_VBI_CAPTURE
,
140 .sampling_rate
= 27000000,
142 .samples_per_line
= 1443,
143 .sample_format
= V4L2_PIX_FMT_GREY
,
155 * This is part of Video 4 Linux API. These procedures handle ioctl() calls.
157 static int pvr2_querycap(struct file
*file
, void *priv
, struct v4l2_capability
*cap
)
159 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
160 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
162 memcpy(cap
, &pvr_capability
, sizeof(struct v4l2_capability
));
163 strlcpy(cap
->bus_info
, pvr2_hdw_get_bus_info(hdw
),
164 sizeof(cap
->bus_info
));
165 strlcpy(cap
->card
, pvr2_hdw_get_desc(hdw
), sizeof(cap
->card
));
169 static int pvr2_g_priority(struct file
*file
, void *priv
, enum v4l2_priority
*p
)
171 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
172 struct pvr2_v4l2
*vp
= fh
->vhead
;
174 *p
= v4l2_prio_max(&vp
->prio
);
178 static int pvr2_s_priority(struct file
*file
, void *priv
, enum v4l2_priority prio
)
180 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
181 struct pvr2_v4l2
*vp
= fh
->vhead
;
183 return v4l2_prio_change(&vp
->prio
, &fh
->prio
, prio
);
186 static int pvr2_g_std(struct file
*file
, void *priv
, v4l2_std_id
*std
)
188 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
189 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
193 ret
= pvr2_ctrl_get_value(
194 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_STDCUR
), &val
);
199 static int pvr2_s_std(struct file
*file
, void *priv
, v4l2_std_id std
)
201 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
202 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
204 return pvr2_ctrl_set_value(
205 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_STDCUR
), std
);
208 static int pvr2_querystd(struct file
*file
, void *priv
, v4l2_std_id
*std
)
210 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
211 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
215 ret
= pvr2_ctrl_get_value(
216 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_STDDETECT
), &val
);
221 static int pvr2_enum_input(struct file
*file
, void *priv
, struct v4l2_input
*vi
)
223 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
224 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
225 struct pvr2_ctrl
*cptr
;
226 struct v4l2_input tmp
;
230 cptr
= pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_INPUT
);
232 memset(&tmp
, 0, sizeof(tmp
));
233 tmp
.index
= vi
->index
;
234 if (vi
->index
>= fh
->input_cnt
)
236 val
= fh
->input_map
[vi
->index
];
238 case PVR2_CVAL_INPUT_TV
:
239 case PVR2_CVAL_INPUT_DTV
:
240 case PVR2_CVAL_INPUT_RADIO
:
241 tmp
.type
= V4L2_INPUT_TYPE_TUNER
;
243 case PVR2_CVAL_INPUT_SVIDEO
:
244 case PVR2_CVAL_INPUT_COMPOSITE
:
245 tmp
.type
= V4L2_INPUT_TYPE_CAMERA
;
252 pvr2_ctrl_get_valname(cptr
, val
,
253 tmp
.name
, sizeof(tmp
.name
) - 1, &cnt
);
256 /* Don't bother with audioset, since this driver currently
257 always switches the audio whenever the video is
260 /* Handling std is a tougher problem. It doesn't make
261 sense in cases where a device might be multi-standard.
262 We could just copy out the current value for the
263 standard, but it can change over time. For now just
269 static int pvr2_g_input(struct file
*file
, void *priv
, unsigned int *i
)
271 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
272 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
274 struct pvr2_ctrl
*cptr
;
278 cptr
= pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_INPUT
);
280 ret
= pvr2_ctrl_get_value(cptr
, &val
);
282 for (idx
= 0; idx
< fh
->input_cnt
; idx
++) {
283 if (fh
->input_map
[idx
] == val
) {
291 static int pvr2_s_input(struct file
*file
, void *priv
, unsigned int inp
)
293 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
294 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
296 if (inp
>= fh
->input_cnt
)
298 return pvr2_ctrl_set_value(
299 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_INPUT
),
303 static int pvr2_enumaudio(struct file
*file
, void *priv
, struct v4l2_audio
*vin
)
305 /* pkt: FIXME: We are returning one "fake" input here
306 which could very well be called "whatever_we_like".
307 This is for apps that want to see an audio input
308 just to feel comfortable, as well as to test if
309 it can do stereo or sth. There is actually no guarantee
310 that the actual audio input cannot change behind the app's
311 back, but most applications should not mind that either.
313 Hopefully, mplayer people will work with us on this (this
314 whole mess is to support mplayer pvr://), or Hans will come
315 up with a more standard way to say "we have inputs but we
316 don 't want you to change them independent of video" which
322 strncpy(vin
->name
, "PVRUSB2 Audio", 14);
323 vin
->capability
= V4L2_AUDCAP_STEREO
;
327 static int pvr2_g_audio(struct file
*file
, void *priv
, struct v4l2_audio
*vin
)
329 /* pkt: FIXME: see above comment (VIDIOC_ENUMAUDIO) */
331 strncpy(vin
->name
, "PVRUSB2 Audio", 14);
332 vin
->capability
= V4L2_AUDCAP_STEREO
;
336 static int pvr2_s_audio(struct file
*file
, void *priv
, const struct v4l2_audio
*vout
)
343 static int pvr2_g_tuner(struct file
*file
, void *priv
, struct v4l2_tuner
*vt
)
345 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
346 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
349 return -EINVAL
; /* Only answer for the 1st tuner */
351 pvr2_hdw_execute_tuner_poll(hdw
);
352 return pvr2_hdw_get_tuner_status(hdw
, vt
);
355 static int pvr2_s_tuner(struct file
*file
, void *priv
, const struct v4l2_tuner
*vt
)
357 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
358 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
363 return pvr2_ctrl_set_value(
364 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_AUDIOMODE
),
368 static int pvr2_s_frequency(struct file
*file
, void *priv
, const struct v4l2_frequency
*vf
)
370 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
371 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
373 struct v4l2_tuner vt
;
375 struct pvr2_ctrl
*ctrlp
;
378 ret
= pvr2_hdw_get_tuner_status(hdw
, &vt
);
381 ctrlp
= pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_INPUT
);
382 ret
= pvr2_ctrl_get_value(ctrlp
, &cur_input
);
385 if (vf
->type
== V4L2_TUNER_RADIO
) {
386 if (cur_input
!= PVR2_CVAL_INPUT_RADIO
)
387 pvr2_ctrl_set_value(ctrlp
, PVR2_CVAL_INPUT_RADIO
);
389 if (cur_input
== PVR2_CVAL_INPUT_RADIO
)
390 pvr2_ctrl_set_value(ctrlp
, PVR2_CVAL_INPUT_TV
);
393 if (vt
.capability
& V4L2_TUNER_CAP_LOW
)
397 return pvr2_ctrl_set_value(
398 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_FREQUENCY
),fv
);
401 static int pvr2_g_frequency(struct file
*file
, void *priv
, struct v4l2_frequency
*vf
)
403 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
404 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
407 struct v4l2_tuner vt
;
410 ret
= pvr2_hdw_get_tuner_status(hdw
, &vt
);
413 ret
= pvr2_ctrl_get_value(
414 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_FREQUENCY
),
419 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_INPUT
),
421 if (cur_input
== PVR2_CVAL_INPUT_RADIO
)
422 vf
->type
= V4L2_TUNER_RADIO
;
424 vf
->type
= V4L2_TUNER_ANALOG_TV
;
425 if (vt
.capability
& V4L2_TUNER_CAP_LOW
)
426 val
= (val
* 2) / 125;
433 static int pvr2_enum_fmt_vid_cap(struct file
*file
, void *priv
, struct v4l2_fmtdesc
*fd
)
435 /* Only one format is supported : mpeg.*/
439 memcpy(fd
, pvr_fmtdesc
, sizeof(struct v4l2_fmtdesc
));
443 static int pvr2_g_fmt_vid_cap(struct file
*file
, void *priv
, struct v4l2_format
*vf
)
445 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
446 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
449 memcpy(vf
, &pvr_format
[PVR_FORMAT_PIX
], sizeof(struct v4l2_format
));
452 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_HRES
),
454 vf
->fmt
.pix
.width
= val
;
457 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_VRES
),
459 vf
->fmt
.pix
.height
= val
;
463 static int pvr2_try_fmt_vid_cap(struct file
*file
, void *priv
, struct v4l2_format
*vf
)
465 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
466 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
467 int lmin
, lmax
, ldef
;
468 struct pvr2_ctrl
*hcp
, *vcp
;
469 int h
= vf
->fmt
.pix
.height
;
470 int w
= vf
->fmt
.pix
.width
;
472 hcp
= pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_HRES
);
473 vcp
= pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_VRES
);
475 lmin
= pvr2_ctrl_get_min(hcp
);
476 lmax
= pvr2_ctrl_get_max(hcp
);
477 pvr2_ctrl_get_def(hcp
, &ldef
);
484 lmin
= pvr2_ctrl_get_min(vcp
);
485 lmax
= pvr2_ctrl_get_max(vcp
);
486 pvr2_ctrl_get_def(vcp
, &ldef
);
494 memcpy(vf
, &pvr_format
[PVR_FORMAT_PIX
],
495 sizeof(struct v4l2_format
));
496 vf
->fmt
.pix
.width
= w
;
497 vf
->fmt
.pix
.height
= h
;
501 static int pvr2_s_fmt_vid_cap(struct file
*file
, void *priv
, struct v4l2_format
*vf
)
503 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
504 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
505 struct pvr2_ctrl
*hcp
, *vcp
;
506 int ret
= pvr2_try_fmt_vid_cap(file
, fh
, vf
);
510 hcp
= pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_HRES
);
511 vcp
= pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_VRES
);
512 pvr2_ctrl_set_value(hcp
, vf
->fmt
.pix
.width
);
513 pvr2_ctrl_set_value(vcp
, vf
->fmt
.pix
.height
);
517 static int pvr2_streamon(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
519 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
520 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
521 struct pvr2_v4l2_dev
*pdi
= fh
->pdi
;
524 if (!fh
->pdi
->stream
) {
525 /* No stream defined for this node. This means
526 that we're not currently allowed to stream from
530 ret
= pvr2_hdw_set_stream_type(hdw
, pdi
->config
);
533 return pvr2_hdw_set_streaming(hdw
, !0);
536 static int pvr2_streamoff(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
538 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
539 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
541 if (!fh
->pdi
->stream
) {
542 /* No stream defined for this node. This means
543 that we're not currently allowed to stream from
547 return pvr2_hdw_set_streaming(hdw
, 0);
550 static int pvr2_queryctrl(struct file
*file
, void *priv
,
551 struct v4l2_queryctrl
*vc
)
553 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
554 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
555 struct pvr2_ctrl
*cptr
;
558 if (vc
->id
& V4L2_CTRL_FLAG_NEXT_CTRL
) {
559 cptr
= pvr2_hdw_get_ctrl_nextv4l(
560 hdw
, (vc
->id
& ~V4L2_CTRL_FLAG_NEXT_CTRL
));
562 vc
->id
= pvr2_ctrl_get_v4lid(cptr
);
564 cptr
= pvr2_hdw_get_ctrl_v4l(hdw
, vc
->id
);
567 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
568 "QUERYCTRL id=0x%x not implemented here",
573 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
574 "QUERYCTRL id=0x%x mapping name=%s (%s)",
575 vc
->id
, pvr2_ctrl_get_name(cptr
),
576 pvr2_ctrl_get_desc(cptr
));
577 strlcpy(vc
->name
, pvr2_ctrl_get_desc(cptr
), sizeof(vc
->name
));
578 vc
->flags
= pvr2_ctrl_get_v4lflags(cptr
);
579 pvr2_ctrl_get_def(cptr
, &val
);
580 vc
->default_value
= val
;
581 switch (pvr2_ctrl_get_type(cptr
)) {
583 vc
->type
= V4L2_CTRL_TYPE_MENU
;
585 vc
->maximum
= pvr2_ctrl_get_cnt(cptr
) - 1;
589 vc
->type
= V4L2_CTRL_TYPE_BOOLEAN
;
595 vc
->type
= V4L2_CTRL_TYPE_INTEGER
;
596 vc
->minimum
= pvr2_ctrl_get_min(cptr
);
597 vc
->maximum
= pvr2_ctrl_get_max(cptr
);
601 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
602 "QUERYCTRL id=0x%x name=%s not mappable",
603 vc
->id
, pvr2_ctrl_get_name(cptr
));
609 static int pvr2_querymenu(struct file
*file
, void *priv
, struct v4l2_querymenu
*vm
)
611 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
612 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
613 unsigned int cnt
= 0;
616 ret
= pvr2_ctrl_get_valname(pvr2_hdw_get_ctrl_v4l(hdw
, vm
->id
),
618 vm
->name
, sizeof(vm
->name
) - 1,
624 static int pvr2_g_ctrl(struct file
*file
, void *priv
, struct v4l2_control
*vc
)
626 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
627 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
631 ret
= pvr2_ctrl_get_value(pvr2_hdw_get_ctrl_v4l(hdw
, vc
->id
),
637 static int pvr2_s_ctrl(struct file
*file
, void *priv
, struct v4l2_control
*vc
)
639 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
640 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
642 return pvr2_ctrl_set_value(pvr2_hdw_get_ctrl_v4l(hdw
, vc
->id
),
646 static int pvr2_g_ext_ctrls(struct file
*file
, void *priv
,
647 struct v4l2_ext_controls
*ctls
)
649 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
650 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
651 struct v4l2_ext_control
*ctrl
;
657 for (idx
= 0; idx
< ctls
->count
; idx
++) {
658 ctrl
= ctls
->controls
+ idx
;
659 ret
= pvr2_ctrl_get_value(
660 pvr2_hdw_get_ctrl_v4l(hdw
, ctrl
->id
), &val
);
662 ctls
->error_idx
= idx
;
665 /* Ensure that if read as a 64 bit value, the user
666 will still get a hopefully sane value */
673 static int pvr2_s_ext_ctrls(struct file
*file
, void *priv
,
674 struct v4l2_ext_controls
*ctls
)
676 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
677 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
678 struct v4l2_ext_control
*ctrl
;
683 for (idx
= 0; idx
< ctls
->count
; idx
++) {
684 ctrl
= ctls
->controls
+ idx
;
685 ret
= pvr2_ctrl_set_value(
686 pvr2_hdw_get_ctrl_v4l(hdw
, ctrl
->id
),
689 ctls
->error_idx
= idx
;
696 static int pvr2_try_ext_ctrls(struct file
*file
, void *priv
,
697 struct v4l2_ext_controls
*ctls
)
699 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
700 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
701 struct v4l2_ext_control
*ctrl
;
702 struct pvr2_ctrl
*pctl
;
705 /* For the moment just validate that the requested control
707 for (idx
= 0; idx
< ctls
->count
; idx
++) {
708 ctrl
= ctls
->controls
+ idx
;
709 pctl
= pvr2_hdw_get_ctrl_v4l(hdw
, ctrl
->id
);
711 ctls
->error_idx
= idx
;
718 static int pvr2_cropcap(struct file
*file
, void *priv
, struct v4l2_cropcap
*cap
)
720 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
721 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
724 if (cap
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
726 ret
= pvr2_hdw_get_cropcap(hdw
, cap
);
727 cap
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
; /* paranoia */
731 static int pvr2_g_crop(struct file
*file
, void *priv
, struct v4l2_crop
*crop
)
733 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
734 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
738 if (crop
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
740 ret
= pvr2_ctrl_get_value(
741 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPL
), &val
);
745 ret
= pvr2_ctrl_get_value(
746 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPT
), &val
);
750 ret
= pvr2_ctrl_get_value(
751 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPW
), &val
);
755 ret
= pvr2_ctrl_get_value(
756 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPH
), &val
);
759 crop
->c
.height
= val
;
763 static int pvr2_s_crop(struct file
*file
, void *priv
, const struct v4l2_crop
*crop
)
765 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
766 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
769 if (crop
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
771 ret
= pvr2_ctrl_set_value(
772 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPL
),
776 ret
= pvr2_ctrl_set_value(
777 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPT
),
781 ret
= pvr2_ctrl_set_value(
782 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPW
),
786 ret
= pvr2_ctrl_set_value(
787 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPH
),
794 static int pvr2_log_status(struct file
*file
, void *priv
)
796 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
797 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
799 pvr2_hdw_trigger_module_log(hdw
);
803 static const struct v4l2_ioctl_ops pvr2_ioctl_ops
= {
804 .vidioc_querycap
= pvr2_querycap
,
805 .vidioc_g_priority
= pvr2_g_priority
,
806 .vidioc_s_priority
= pvr2_s_priority
,
807 .vidioc_s_audio
= pvr2_s_audio
,
808 .vidioc_g_audio
= pvr2_g_audio
,
809 .vidioc_enumaudio
= pvr2_enumaudio
,
810 .vidioc_enum_input
= pvr2_enum_input
,
811 .vidioc_cropcap
= pvr2_cropcap
,
812 .vidioc_s_crop
= pvr2_s_crop
,
813 .vidioc_g_crop
= pvr2_g_crop
,
814 .vidioc_g_input
= pvr2_g_input
,
815 .vidioc_s_input
= pvr2_s_input
,
816 .vidioc_g_frequency
= pvr2_g_frequency
,
817 .vidioc_s_frequency
= pvr2_s_frequency
,
818 .vidioc_s_tuner
= pvr2_s_tuner
,
819 .vidioc_g_tuner
= pvr2_g_tuner
,
820 .vidioc_g_std
= pvr2_g_std
,
821 .vidioc_s_std
= pvr2_s_std
,
822 .vidioc_querystd
= pvr2_querystd
,
823 .vidioc_log_status
= pvr2_log_status
,
824 .vidioc_enum_fmt_vid_cap
= pvr2_enum_fmt_vid_cap
,
825 .vidioc_g_fmt_vid_cap
= pvr2_g_fmt_vid_cap
,
826 .vidioc_s_fmt_vid_cap
= pvr2_s_fmt_vid_cap
,
827 .vidioc_try_fmt_vid_cap
= pvr2_try_fmt_vid_cap
,
828 .vidioc_streamon
= pvr2_streamon
,
829 .vidioc_streamoff
= pvr2_streamoff
,
830 .vidioc_queryctrl
= pvr2_queryctrl
,
831 .vidioc_querymenu
= pvr2_querymenu
,
832 .vidioc_g_ctrl
= pvr2_g_ctrl
,
833 .vidioc_s_ctrl
= pvr2_s_ctrl
,
834 .vidioc_g_ext_ctrls
= pvr2_g_ext_ctrls
,
835 .vidioc_s_ext_ctrls
= pvr2_s_ext_ctrls
,
836 .vidioc_try_ext_ctrls
= pvr2_try_ext_ctrls
,
839 static void pvr2_v4l2_dev_destroy(struct pvr2_v4l2_dev
*dip
)
841 struct pvr2_hdw
*hdw
= dip
->v4lp
->channel
.mc_head
->hdw
;
842 enum pvr2_config cfg
= dip
->config
;
846 /* Construct the unregistration message *before* we actually
847 perform the unregistration step. By doing it this way we don't
848 have to worry about potentially touching deleted resources. */
849 mcnt
= scnprintf(msg
, sizeof(msg
) - 1,
850 "pvrusb2: unregistered device %s [%s]",
851 video_device_node_name(&dip
->devbase
),
852 pvr2_config_get_name(cfg
));
855 pvr2_hdw_v4l_store_minor_number(hdw
,dip
->minor_type
,-1);
861 /* Actual deallocation happens later when all internal references
863 video_unregister_device(&dip
->devbase
);
865 printk(KERN_INFO
"%s\n", msg
);
870 static void pvr2_v4l2_dev_disassociate_parent(struct pvr2_v4l2_dev
*dip
)
873 if (!dip
->devbase
.parent
) return;
874 dip
->devbase
.parent
= NULL
;
875 device_move(&dip
->devbase
.dev
, NULL
, DPM_ORDER_NONE
);
879 static void pvr2_v4l2_destroy_no_lock(struct pvr2_v4l2
*vp
)
882 pvr2_v4l2_dev_destroy(vp
->dev_video
);
883 vp
->dev_video
= NULL
;
886 pvr2_v4l2_dev_destroy(vp
->dev_radio
);
887 vp
->dev_radio
= NULL
;
890 pvr2_trace(PVR2_TRACE_STRUCT
,"Destroying pvr2_v4l2 id=%p",vp
);
891 pvr2_channel_done(&vp
->channel
);
896 static void pvr2_video_device_release(struct video_device
*vdev
)
898 struct pvr2_v4l2_dev
*dev
;
899 dev
= container_of(vdev
,struct pvr2_v4l2_dev
,devbase
);
904 static void pvr2_v4l2_internal_check(struct pvr2_channel
*chp
)
906 struct pvr2_v4l2
*vp
;
907 vp
= container_of(chp
,struct pvr2_v4l2
,channel
);
908 if (!vp
->channel
.mc_head
->disconnect_flag
) return;
909 pvr2_v4l2_dev_disassociate_parent(vp
->dev_video
);
910 pvr2_v4l2_dev_disassociate_parent(vp
->dev_radio
);
911 if (vp
->vfirst
) return;
912 pvr2_v4l2_destroy_no_lock(vp
);
916 static long pvr2_v4l2_ioctl(struct file
*file
,
917 unsigned int cmd
, unsigned long arg
)
920 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
921 struct pvr2_v4l2
*vp
= fh
->vhead
;
922 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
925 if (pvrusb2_debug
& PVR2_TRACE_V4LIOCTL
)
926 v4l_printk_ioctl(pvr2_hdw_get_driver_name(hdw
), cmd
);
928 if (!pvr2_hdw_dev_ok(hdw
)) {
929 pvr2_trace(PVR2_TRACE_ERROR_LEGS
,
930 "ioctl failed - bad or no context");
940 case VIDIOC_S_FREQUENCY
:
941 ret
= v4l2_prio_check(&vp
->prio
, fh
->prio
);
946 ret
= video_ioctl2(file
, cmd
, arg
);
948 pvr2_hdw_commit_ctl(hdw
);
951 if (pvrusb2_debug
& PVR2_TRACE_V4LIOCTL
) {
952 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
953 "pvr2_v4l2_do_ioctl failure, ret=%ld", ret
);
955 if (pvrusb2_debug
& PVR2_TRACE_V4LIOCTL
) {
956 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
957 "pvr2_v4l2_do_ioctl failure, ret=%ld"
958 " command was:", ret
);
959 v4l_printk_ioctl(pvr2_hdw_get_driver_name(hdw
),
964 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
965 "pvr2_v4l2_do_ioctl complete, ret=%ld (0x%lx)",
973 static int pvr2_v4l2_release(struct file
*file
)
975 struct pvr2_v4l2_fh
*fhp
= file
->private_data
;
976 struct pvr2_v4l2
*vp
= fhp
->vhead
;
977 struct pvr2_hdw
*hdw
= fhp
->channel
.mc_head
->hdw
;
979 pvr2_trace(PVR2_TRACE_OPEN_CLOSE
,"pvr2_v4l2_release");
982 struct pvr2_stream
*sp
;
983 pvr2_hdw_set_streaming(hdw
,0);
984 sp
= pvr2_ioread_get_stream(fhp
->rhp
);
985 if (sp
) pvr2_stream_set_callback(sp
,NULL
,NULL
);
986 pvr2_ioread_destroy(fhp
->rhp
);
990 v4l2_prio_close(&vp
->prio
, fhp
->prio
);
991 file
->private_data
= NULL
;
994 fhp
->vnext
->vprev
= fhp
->vprev
;
996 vp
->vlast
= fhp
->vprev
;
999 fhp
->vprev
->vnext
= fhp
->vnext
;
1001 vp
->vfirst
= fhp
->vnext
;
1006 pvr2_channel_done(&fhp
->channel
);
1007 pvr2_trace(PVR2_TRACE_STRUCT
,
1008 "Destroying pvr_v4l2_fh id=%p",fhp
);
1009 if (fhp
->input_map
) {
1010 kfree(fhp
->input_map
);
1011 fhp
->input_map
= NULL
;
1014 if (vp
->channel
.mc_head
->disconnect_flag
&& !vp
->vfirst
) {
1015 pvr2_v4l2_destroy_no_lock(vp
);
1021 static int pvr2_v4l2_open(struct file
*file
)
1023 struct pvr2_v4l2_dev
*dip
; /* Our own context pointer */
1024 struct pvr2_v4l2_fh
*fhp
;
1025 struct pvr2_v4l2
*vp
;
1026 struct pvr2_hdw
*hdw
;
1027 unsigned int input_mask
= 0;
1028 unsigned int input_cnt
,idx
;
1031 dip
= container_of(video_devdata(file
),struct pvr2_v4l2_dev
,devbase
);
1034 hdw
= vp
->channel
.hdw
;
1036 pvr2_trace(PVR2_TRACE_OPEN_CLOSE
,"pvr2_v4l2_open");
1038 if (!pvr2_hdw_dev_ok(hdw
)) {
1039 pvr2_trace(PVR2_TRACE_OPEN_CLOSE
,
1040 "pvr2_v4l2_open: hardware not ready");
1044 fhp
= kzalloc(sizeof(*fhp
),GFP_KERNEL
);
1049 init_waitqueue_head(&fhp
->wait_data
);
1052 pvr2_trace(PVR2_TRACE_STRUCT
,"Creating pvr_v4l2_fh id=%p",fhp
);
1053 pvr2_channel_init(&fhp
->channel
,vp
->channel
.mc_head
);
1055 if (dip
->v4l_type
== VFL_TYPE_RADIO
) {
1056 /* Opening device as a radio, legal input selection subset
1057 is just the radio. */
1058 input_mask
= (1 << PVR2_CVAL_INPUT_RADIO
);
1060 /* Opening the main V4L device, legal input selection
1061 subset includes all analog inputs. */
1062 input_mask
= ((1 << PVR2_CVAL_INPUT_RADIO
) |
1063 (1 << PVR2_CVAL_INPUT_TV
) |
1064 (1 << PVR2_CVAL_INPUT_COMPOSITE
) |
1065 (1 << PVR2_CVAL_INPUT_SVIDEO
));
1067 ret
= pvr2_channel_limit_inputs(&fhp
->channel
,input_mask
);
1069 pvr2_channel_done(&fhp
->channel
);
1070 pvr2_trace(PVR2_TRACE_STRUCT
,
1071 "Destroying pvr_v4l2_fh id=%p (input mask error)",
1078 input_mask
&= pvr2_hdw_get_input_available(hdw
);
1080 for (idx
= 0; idx
< (sizeof(input_mask
) << 3); idx
++) {
1081 if (input_mask
& (1 << idx
)) input_cnt
++;
1083 fhp
->input_cnt
= input_cnt
;
1084 fhp
->input_map
= kzalloc(input_cnt
,GFP_KERNEL
);
1085 if (!fhp
->input_map
) {
1086 pvr2_channel_done(&fhp
->channel
);
1087 pvr2_trace(PVR2_TRACE_STRUCT
,
1088 "Destroying pvr_v4l2_fh id=%p (input map failure)",
1094 for (idx
= 0; idx
< (sizeof(input_mask
) << 3); idx
++) {
1095 if (!(input_mask
& (1 << idx
))) continue;
1096 fhp
->input_map
[input_cnt
++] = idx
;
1100 fhp
->vprev
= vp
->vlast
;
1102 vp
->vlast
->vnext
= fhp
;
1110 file
->private_data
= fhp
;
1111 v4l2_prio_open(&vp
->prio
, &fhp
->prio
);
1113 fhp
->fw_mode_flag
= pvr2_hdw_cpufw_get_enabled(hdw
);
1119 static void pvr2_v4l2_notify(struct pvr2_v4l2_fh
*fhp
)
1121 wake_up(&fhp
->wait_data
);
1124 static int pvr2_v4l2_iosetup(struct pvr2_v4l2_fh
*fh
)
1127 struct pvr2_stream
*sp
;
1128 struct pvr2_hdw
*hdw
;
1129 if (fh
->rhp
) return 0;
1131 if (!fh
->pdi
->stream
) {
1132 /* No stream defined for this node. This means that we're
1133 not currently allowed to stream from this node. */
1137 /* First read() attempt. Try to claim the stream and start
1139 if ((ret
= pvr2_channel_claim_stream(&fh
->channel
,
1140 fh
->pdi
->stream
)) != 0) {
1141 /* Someone else must already have it */
1145 fh
->rhp
= pvr2_channel_create_mpeg_stream(fh
->pdi
->stream
);
1147 pvr2_channel_claim_stream(&fh
->channel
,NULL
);
1151 hdw
= fh
->channel
.mc_head
->hdw
;
1152 sp
= fh
->pdi
->stream
->stream
;
1153 pvr2_stream_set_callback(sp
,(pvr2_stream_callback
)pvr2_v4l2_notify
,fh
);
1154 pvr2_hdw_set_stream_type(hdw
,fh
->pdi
->config
);
1155 if ((ret
= pvr2_hdw_set_streaming(hdw
,!0)) < 0) return ret
;
1156 return pvr2_ioread_set_enabled(fh
->rhp
,!0);
1160 static ssize_t
pvr2_v4l2_read(struct file
*file
,
1161 char __user
*buff
, size_t count
, loff_t
*ppos
)
1163 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
1166 if (fh
->fw_mode_flag
) {
1167 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
1171 unsigned int offs
= *ppos
;
1173 tbuf
= kmalloc(PAGE_SIZE
,GFP_KERNEL
);
1174 if (!tbuf
) return -ENOMEM
;
1178 if (c1
> PAGE_SIZE
) c1
= PAGE_SIZE
;
1179 c2
= pvr2_hdw_cpufw_get(hdw
,offs
,tbuf
,c1
);
1185 if (copy_to_user(buff
,tbuf
,c2
)) {
1200 ret
= pvr2_v4l2_iosetup(fh
);
1207 ret
= pvr2_ioread_read(fh
->rhp
,buff
,count
);
1208 if (ret
>= 0) break;
1209 if (ret
!= -EAGAIN
) break;
1210 if (file
->f_flags
& O_NONBLOCK
) break;
1211 /* Doing blocking I/O. Wait here. */
1212 ret
= wait_event_interruptible(
1214 pvr2_ioread_avail(fh
->rhp
) >= 0);
1222 static unsigned int pvr2_v4l2_poll(struct file
*file
, poll_table
*wait
)
1224 unsigned int mask
= 0;
1225 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
1228 if (fh
->fw_mode_flag
) {
1229 mask
|= POLLIN
| POLLRDNORM
;
1234 ret
= pvr2_v4l2_iosetup(fh
);
1235 if (ret
) return POLLERR
;
1238 poll_wait(file
,&fh
->wait_data
,wait
);
1240 if (pvr2_ioread_avail(fh
->rhp
) >= 0) {
1241 mask
|= POLLIN
| POLLRDNORM
;
1248 static const struct v4l2_file_operations vdev_fops
= {
1249 .owner
= THIS_MODULE
,
1250 .open
= pvr2_v4l2_open
,
1251 .release
= pvr2_v4l2_release
,
1252 .read
= pvr2_v4l2_read
,
1253 .ioctl
= pvr2_v4l2_ioctl
,
1254 .poll
= pvr2_v4l2_poll
,
1258 static struct video_device vdev_template
= {
1263 static void pvr2_v4l2_dev_init(struct pvr2_v4l2_dev
*dip
,
1264 struct pvr2_v4l2
*vp
,
1267 struct usb_device
*usbdev
;
1270 struct pvr2_hdw
*hdw
;
1274 hdw
= vp
->channel
.mc_head
->hdw
;
1275 usbdev
= pvr2_hdw_get_dev(hdw
);
1276 dip
->v4l_type
= v4l_type
;
1278 case VFL_TYPE_GRABBER
:
1279 dip
->stream
= &vp
->channel
.mc_head
->video_stream
;
1280 dip
->config
= pvr2_config_mpeg
;
1281 dip
->minor_type
= pvr2_v4l_type_video
;
1284 pr_err(KBUILD_MODNAME
1285 ": Failed to set up pvrusb2 v4l video dev"
1286 " due to missing stream instance\n");
1291 dip
->config
= pvr2_config_vbi
;
1292 dip
->minor_type
= pvr2_v4l_type_vbi
;
1295 case VFL_TYPE_RADIO
:
1296 dip
->stream
= &vp
->channel
.mc_head
->video_stream
;
1297 dip
->config
= pvr2_config_mpeg
;
1298 dip
->minor_type
= pvr2_v4l_type_radio
;
1302 /* Bail out (this should be impossible) */
1303 pr_err(KBUILD_MODNAME
": Failed to set up pvrusb2 v4l dev"
1304 " due to unrecognized config\n");
1308 dip
->devbase
= vdev_template
;
1309 dip
->devbase
.release
= pvr2_video_device_release
;
1310 dip
->devbase
.ioctl_ops
= &pvr2_ioctl_ops
;
1313 pvr2_ctrl_get_value(
1314 pvr2_hdw_get_ctrl_by_id(hdw
,
1315 PVR2_CID_STDAVAIL
), &val
);
1316 dip
->devbase
.tvnorms
= (v4l2_std_id
)val
;
1320 unit_number
= pvr2_hdw_get_unit_number(hdw
);
1321 if (nr_ptr
&& (unit_number
>= 0) && (unit_number
< PVR_NUM
)) {
1322 mindevnum
= nr_ptr
[unit_number
];
1324 dip
->devbase
.parent
= &usbdev
->dev
;
1325 if ((video_register_device(&dip
->devbase
,
1326 dip
->v4l_type
, mindevnum
) < 0) &&
1327 (video_register_device(&dip
->devbase
,
1328 dip
->v4l_type
, -1) < 0)) {
1329 pr_err(KBUILD_MODNAME
1330 ": Failed to register pvrusb2 v4l device\n");
1333 printk(KERN_INFO
"pvrusb2: registered device %s [%s]\n",
1334 video_device_node_name(&dip
->devbase
),
1335 pvr2_config_get_name(dip
->config
));
1337 pvr2_hdw_v4l_store_minor_number(hdw
,
1338 dip
->minor_type
,dip
->devbase
.minor
);
1342 struct pvr2_v4l2
*pvr2_v4l2_create(struct pvr2_context
*mnp
)
1344 struct pvr2_v4l2
*vp
;
1346 vp
= kzalloc(sizeof(*vp
),GFP_KERNEL
);
1348 pvr2_channel_init(&vp
->channel
,mnp
);
1349 pvr2_trace(PVR2_TRACE_STRUCT
,"Creating pvr2_v4l2 id=%p",vp
);
1351 vp
->channel
.check_func
= pvr2_v4l2_internal_check
;
1353 /* register streams */
1354 vp
->dev_video
= kzalloc(sizeof(*vp
->dev_video
),GFP_KERNEL
);
1355 if (!vp
->dev_video
) goto fail
;
1356 pvr2_v4l2_dev_init(vp
->dev_video
,vp
,VFL_TYPE_GRABBER
);
1357 if (pvr2_hdw_get_input_available(vp
->channel
.mc_head
->hdw
) &
1358 (1 << PVR2_CVAL_INPUT_RADIO
)) {
1359 vp
->dev_radio
= kzalloc(sizeof(*vp
->dev_radio
),GFP_KERNEL
);
1360 if (!vp
->dev_radio
) goto fail
;
1361 pvr2_v4l2_dev_init(vp
->dev_radio
,vp
,VFL_TYPE_RADIO
);
1366 pvr2_trace(PVR2_TRACE_STRUCT
,"Failure creating pvr2_v4l2 id=%p",vp
);
1367 pvr2_v4l2_destroy_no_lock(vp
);
1372 Stuff for Emacs to see, in order to encourage consistent editing style:
1373 *** Local Variables: ***
1375 *** fill-column: 75 ***
1376 *** tab-width: 8 ***
1377 *** c-basic-offset: 8 ***