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-device.h>
35 #include <media/v4l2-common.h>
36 #include <media/v4l2-ioctl.h>
42 struct pvr2_v4l2_dev
{
43 struct video_device devbase
; /* MUST be first! */
44 struct pvr2_v4l2
*v4lp
;
45 struct pvr2_context_stream
*stream
;
46 /* Information about this device: */
47 enum pvr2_config config
; /* Expected stream format */
48 int v4l_type
; /* V4L defined type for this device node */
49 enum pvr2_v4l_type minor_type
; /* pvr2-understood minor device type */
53 struct pvr2_channel channel
;
54 struct pvr2_v4l2_dev
*pdi
;
55 enum v4l2_priority prio
;
56 struct pvr2_ioread
*rhp
;
58 struct pvr2_v4l2
*vhead
;
59 struct pvr2_v4l2_fh
*vnext
;
60 struct pvr2_v4l2_fh
*vprev
;
61 wait_queue_head_t wait_data
;
63 /* Map contiguous ordinal value to input id */
64 unsigned char *input_map
;
65 unsigned int input_cnt
;
69 struct pvr2_channel channel
;
70 struct pvr2_v4l2_fh
*vfirst
;
71 struct pvr2_v4l2_fh
*vlast
;
73 struct v4l2_prio_state prio
;
75 /* streams - Note that these must be separately, individually,
76 * allocated pointers. This is because the v4l core is going to
77 * manage their deletion - separately, individually... */
78 struct pvr2_v4l2_dev
*dev_video
;
79 struct pvr2_v4l2_dev
*dev_radio
;
82 static int video_nr
[PVR_NUM
] = {[0 ... PVR_NUM
-1] = -1};
83 module_param_array(video_nr
, int, NULL
, 0444);
84 MODULE_PARM_DESC(video_nr
, "Offset for device's video dev minor");
85 static int radio_nr
[PVR_NUM
] = {[0 ... PVR_NUM
-1] = -1};
86 module_param_array(radio_nr
, int, NULL
, 0444);
87 MODULE_PARM_DESC(radio_nr
, "Offset for device's radio dev minor");
88 static int vbi_nr
[PVR_NUM
] = {[0 ... PVR_NUM
-1] = -1};
89 module_param_array(vbi_nr
, int, NULL
, 0444);
90 MODULE_PARM_DESC(vbi_nr
, "Offset for device's vbi dev minor");
92 static struct v4l2_capability pvr_capability
={
94 .card
= "Hauppauge WinTV pvr-usb2",
96 .version
= LINUX_VERSION_CODE
,
97 .capabilities
= (V4L2_CAP_VIDEO_CAPTURE
|
98 V4L2_CAP_TUNER
| V4L2_CAP_AUDIO
| V4L2_CAP_RADIO
|
102 static struct v4l2_fmtdesc pvr_fmtdesc
[] = {
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,
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
,
156 * This is part of Video 4 Linux API. These procedures handle ioctl() calls.
158 static int pvr2_querycap(struct file
*file
, void *priv
, struct v4l2_capability
*cap
)
160 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
161 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
163 memcpy(cap
, &pvr_capability
, sizeof(struct v4l2_capability
));
164 strlcpy(cap
->bus_info
, pvr2_hdw_get_bus_info(hdw
),
165 sizeof(cap
->bus_info
));
166 strlcpy(cap
->card
, pvr2_hdw_get_desc(hdw
), sizeof(cap
->card
));
170 static int pvr2_g_priority(struct file
*file
, void *priv
, enum v4l2_priority
*p
)
172 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
173 struct pvr2_v4l2
*vp
= fh
->vhead
;
175 *p
= v4l2_prio_max(&vp
->prio
);
179 static int pvr2_s_priority(struct file
*file
, void *priv
, enum v4l2_priority prio
)
181 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
182 struct pvr2_v4l2
*vp
= fh
->vhead
;
184 return v4l2_prio_change(&vp
->prio
, &fh
->prio
, prio
);
187 static int pvr2_g_std(struct file
*file
, void *priv
, v4l2_std_id
*std
)
189 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
190 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
194 ret
= pvr2_ctrl_get_value(
195 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_STDCUR
), &val
);
200 static int pvr2_s_std(struct file
*file
, void *priv
, v4l2_std_id std
)
202 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
203 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
205 return pvr2_ctrl_set_value(
206 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_STDCUR
), std
);
209 static int pvr2_querystd(struct file
*file
, void *priv
, v4l2_std_id
*std
)
211 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
212 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
216 ret
= pvr2_ctrl_get_value(
217 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_STDDETECT
), &val
);
222 static int pvr2_enum_input(struct file
*file
, void *priv
, struct v4l2_input
*vi
)
224 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
225 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
226 struct pvr2_ctrl
*cptr
;
227 struct v4l2_input tmp
;
231 cptr
= pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_INPUT
);
233 memset(&tmp
, 0, sizeof(tmp
));
234 tmp
.index
= vi
->index
;
235 if (vi
->index
>= fh
->input_cnt
)
237 val
= fh
->input_map
[vi
->index
];
239 case PVR2_CVAL_INPUT_TV
:
240 case PVR2_CVAL_INPUT_DTV
:
241 case PVR2_CVAL_INPUT_RADIO
:
242 tmp
.type
= V4L2_INPUT_TYPE_TUNER
;
244 case PVR2_CVAL_INPUT_SVIDEO
:
245 case PVR2_CVAL_INPUT_COMPOSITE
:
246 tmp
.type
= V4L2_INPUT_TYPE_CAMERA
;
253 pvr2_ctrl_get_valname(cptr
, val
,
254 tmp
.name
, sizeof(tmp
.name
) - 1, &cnt
);
257 /* Don't bother with audioset, since this driver currently
258 always switches the audio whenever the video is
261 /* Handling std is a tougher problem. It doesn't make
262 sense in cases where a device might be multi-standard.
263 We could just copy out the current value for the
264 standard, but it can change over time. For now just
270 static int pvr2_g_input(struct file
*file
, void *priv
, unsigned int *i
)
272 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
273 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
275 struct pvr2_ctrl
*cptr
;
279 cptr
= pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_INPUT
);
281 ret
= pvr2_ctrl_get_value(cptr
, &val
);
283 for (idx
= 0; idx
< fh
->input_cnt
; idx
++) {
284 if (fh
->input_map
[idx
] == val
) {
292 static int pvr2_s_input(struct file
*file
, void *priv
, unsigned int inp
)
294 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
295 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
297 if (inp
>= fh
->input_cnt
)
299 return pvr2_ctrl_set_value(
300 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_INPUT
),
304 static int pvr2_enumaudio(struct file
*file
, void *priv
, struct v4l2_audio
*vin
)
306 /* pkt: FIXME: We are returning one "fake" input here
307 which could very well be called "whatever_we_like".
308 This is for apps that want to see an audio input
309 just to feel comfortable, as well as to test if
310 it can do stereo or sth. There is actually no guarantee
311 that the actual audio input cannot change behind the app's
312 back, but most applications should not mind that either.
314 Hopefully, mplayer people will work with us on this (this
315 whole mess is to support mplayer pvr://), or Hans will come
316 up with a more standard way to say "we have inputs but we
317 don 't want you to change them independent of video" which
323 strncpy(vin
->name
, "PVRUSB2 Audio", 14);
324 vin
->capability
= V4L2_AUDCAP_STEREO
;
328 static int pvr2_g_audio(struct file
*file
, void *priv
, struct v4l2_audio
*vin
)
330 /* pkt: FIXME: see above comment (VIDIOC_ENUMAUDIO) */
332 strncpy(vin
->name
, "PVRUSB2 Audio", 14);
333 vin
->capability
= V4L2_AUDCAP_STEREO
;
337 static int pvr2_s_audio(struct file
*file
, void *priv
, const struct v4l2_audio
*vout
)
344 static int pvr2_g_tuner(struct file
*file
, void *priv
, struct v4l2_tuner
*vt
)
346 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
347 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
350 return -EINVAL
; /* Only answer for the 1st tuner */
352 pvr2_hdw_execute_tuner_poll(hdw
);
353 return pvr2_hdw_get_tuner_status(hdw
, vt
);
356 static int pvr2_s_tuner(struct file
*file
, void *priv
, const struct v4l2_tuner
*vt
)
358 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
359 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
364 return pvr2_ctrl_set_value(
365 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_AUDIOMODE
),
369 static int pvr2_s_frequency(struct file
*file
, void *priv
, const struct v4l2_frequency
*vf
)
371 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
372 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
374 struct v4l2_tuner vt
;
376 struct pvr2_ctrl
*ctrlp
;
379 ret
= pvr2_hdw_get_tuner_status(hdw
, &vt
);
382 ctrlp
= pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_INPUT
);
383 ret
= pvr2_ctrl_get_value(ctrlp
, &cur_input
);
386 if (vf
->type
== V4L2_TUNER_RADIO
) {
387 if (cur_input
!= PVR2_CVAL_INPUT_RADIO
)
388 pvr2_ctrl_set_value(ctrlp
, PVR2_CVAL_INPUT_RADIO
);
390 if (cur_input
== PVR2_CVAL_INPUT_RADIO
)
391 pvr2_ctrl_set_value(ctrlp
, PVR2_CVAL_INPUT_TV
);
394 if (vt
.capability
& V4L2_TUNER_CAP_LOW
)
398 return pvr2_ctrl_set_value(
399 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_FREQUENCY
),fv
);
402 static int pvr2_g_frequency(struct file
*file
, void *priv
, struct v4l2_frequency
*vf
)
404 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
405 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
408 struct v4l2_tuner vt
;
411 ret
= pvr2_hdw_get_tuner_status(hdw
, &vt
);
414 ret
= pvr2_ctrl_get_value(
415 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_FREQUENCY
),
420 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_INPUT
),
422 if (cur_input
== PVR2_CVAL_INPUT_RADIO
)
423 vf
->type
= V4L2_TUNER_RADIO
;
425 vf
->type
= V4L2_TUNER_ANALOG_TV
;
426 if (vt
.capability
& V4L2_TUNER_CAP_LOW
)
427 val
= (val
* 2) / 125;
434 static int pvr2_enum_fmt_vid_cap(struct file
*file
, void *priv
, struct v4l2_fmtdesc
*fd
)
436 /* Only one format is supported : mpeg.*/
440 memcpy(fd
, pvr_fmtdesc
, sizeof(struct v4l2_fmtdesc
));
444 static int pvr2_g_fmt_vid_cap(struct file
*file
, void *priv
, struct v4l2_format
*vf
)
446 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
447 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
450 memcpy(vf
, &pvr_format
[PVR_FORMAT_PIX
], sizeof(struct v4l2_format
));
453 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_HRES
),
455 vf
->fmt
.pix
.width
= val
;
458 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_VRES
),
460 vf
->fmt
.pix
.height
= val
;
464 static int pvr2_try_fmt_vid_cap(struct file
*file
, void *priv
, struct v4l2_format
*vf
)
466 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
467 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
468 int lmin
, lmax
, ldef
;
469 struct pvr2_ctrl
*hcp
, *vcp
;
470 int h
= vf
->fmt
.pix
.height
;
471 int w
= vf
->fmt
.pix
.width
;
473 hcp
= pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_HRES
);
474 vcp
= pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_VRES
);
476 lmin
= pvr2_ctrl_get_min(hcp
);
477 lmax
= pvr2_ctrl_get_max(hcp
);
478 pvr2_ctrl_get_def(hcp
, &ldef
);
485 lmin
= pvr2_ctrl_get_min(vcp
);
486 lmax
= pvr2_ctrl_get_max(vcp
);
487 pvr2_ctrl_get_def(vcp
, &ldef
);
495 memcpy(vf
, &pvr_format
[PVR_FORMAT_PIX
],
496 sizeof(struct v4l2_format
));
497 vf
->fmt
.pix
.width
= w
;
498 vf
->fmt
.pix
.height
= h
;
502 static int pvr2_s_fmt_vid_cap(struct file
*file
, void *priv
, struct v4l2_format
*vf
)
504 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
505 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
506 struct pvr2_ctrl
*hcp
, *vcp
;
507 int ret
= pvr2_try_fmt_vid_cap(file
, fh
, vf
);
511 hcp
= pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_HRES
);
512 vcp
= pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_VRES
);
513 pvr2_ctrl_set_value(hcp
, vf
->fmt
.pix
.width
);
514 pvr2_ctrl_set_value(vcp
, vf
->fmt
.pix
.height
);
518 static int pvr2_streamon(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
520 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
521 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
522 struct pvr2_v4l2_dev
*pdi
= fh
->pdi
;
525 if (!fh
->pdi
->stream
) {
526 /* No stream defined for this node. This means
527 that we're not currently allowed to stream from
531 ret
= pvr2_hdw_set_stream_type(hdw
, pdi
->config
);
534 return pvr2_hdw_set_streaming(hdw
, !0);
537 static int pvr2_streamoff(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
539 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
540 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
542 if (!fh
->pdi
->stream
) {
543 /* No stream defined for this node. This means
544 that we're not currently allowed to stream from
548 return pvr2_hdw_set_streaming(hdw
, 0);
551 static int pvr2_queryctrl(struct file
*file
, void *priv
,
552 struct v4l2_queryctrl
*vc
)
554 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
555 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
556 struct pvr2_ctrl
*cptr
;
559 if (vc
->id
& V4L2_CTRL_FLAG_NEXT_CTRL
) {
560 cptr
= pvr2_hdw_get_ctrl_nextv4l(
561 hdw
, (vc
->id
& ~V4L2_CTRL_FLAG_NEXT_CTRL
));
563 vc
->id
= pvr2_ctrl_get_v4lid(cptr
);
565 cptr
= pvr2_hdw_get_ctrl_v4l(hdw
, vc
->id
);
568 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
569 "QUERYCTRL id=0x%x not implemented here",
574 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
575 "QUERYCTRL id=0x%x mapping name=%s (%s)",
576 vc
->id
, pvr2_ctrl_get_name(cptr
),
577 pvr2_ctrl_get_desc(cptr
));
578 strlcpy(vc
->name
, pvr2_ctrl_get_desc(cptr
), sizeof(vc
->name
));
579 vc
->flags
= pvr2_ctrl_get_v4lflags(cptr
);
580 pvr2_ctrl_get_def(cptr
, &val
);
581 vc
->default_value
= val
;
582 switch (pvr2_ctrl_get_type(cptr
)) {
584 vc
->type
= V4L2_CTRL_TYPE_MENU
;
586 vc
->maximum
= pvr2_ctrl_get_cnt(cptr
) - 1;
590 vc
->type
= V4L2_CTRL_TYPE_BOOLEAN
;
596 vc
->type
= V4L2_CTRL_TYPE_INTEGER
;
597 vc
->minimum
= pvr2_ctrl_get_min(cptr
);
598 vc
->maximum
= pvr2_ctrl_get_max(cptr
);
602 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
603 "QUERYCTRL id=0x%x name=%s not mappable",
604 vc
->id
, pvr2_ctrl_get_name(cptr
));
610 static int pvr2_querymenu(struct file
*file
, void *priv
, struct v4l2_querymenu
*vm
)
612 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
613 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
614 unsigned int cnt
= 0;
617 ret
= pvr2_ctrl_get_valname(pvr2_hdw_get_ctrl_v4l(hdw
, vm
->id
),
619 vm
->name
, sizeof(vm
->name
) - 1,
625 static int pvr2_g_ctrl(struct file
*file
, void *priv
, struct v4l2_control
*vc
)
627 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
628 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
632 ret
= pvr2_ctrl_get_value(pvr2_hdw_get_ctrl_v4l(hdw
, vc
->id
),
638 static int pvr2_s_ctrl(struct file
*file
, void *priv
, struct v4l2_control
*vc
)
640 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
641 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
643 return pvr2_ctrl_set_value(pvr2_hdw_get_ctrl_v4l(hdw
, vc
->id
),
647 static int pvr2_g_ext_ctrls(struct file
*file
, void *priv
,
648 struct v4l2_ext_controls
*ctls
)
650 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
651 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
652 struct v4l2_ext_control
*ctrl
;
658 for (idx
= 0; idx
< ctls
->count
; idx
++) {
659 ctrl
= ctls
->controls
+ idx
;
660 ret
= pvr2_ctrl_get_value(
661 pvr2_hdw_get_ctrl_v4l(hdw
, ctrl
->id
), &val
);
663 ctls
->error_idx
= idx
;
666 /* Ensure that if read as a 64 bit value, the user
667 will still get a hopefully sane value */
674 static int pvr2_s_ext_ctrls(struct file
*file
, void *priv
,
675 struct v4l2_ext_controls
*ctls
)
677 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
678 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
679 struct v4l2_ext_control
*ctrl
;
684 for (idx
= 0; idx
< ctls
->count
; idx
++) {
685 ctrl
= ctls
->controls
+ idx
;
686 ret
= pvr2_ctrl_set_value(
687 pvr2_hdw_get_ctrl_v4l(hdw
, ctrl
->id
),
690 ctls
->error_idx
= idx
;
697 static int pvr2_try_ext_ctrls(struct file
*file
, void *priv
,
698 struct v4l2_ext_controls
*ctls
)
700 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
701 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
702 struct v4l2_ext_control
*ctrl
;
703 struct pvr2_ctrl
*pctl
;
706 /* For the moment just validate that the requested control
708 for (idx
= 0; idx
< ctls
->count
; idx
++) {
709 ctrl
= ctls
->controls
+ idx
;
710 pctl
= pvr2_hdw_get_ctrl_v4l(hdw
, ctrl
->id
);
712 ctls
->error_idx
= idx
;
719 static int pvr2_cropcap(struct file
*file
, void *priv
, struct v4l2_cropcap
*cap
)
721 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
722 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
725 if (cap
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
727 ret
= pvr2_hdw_get_cropcap(hdw
, cap
);
728 cap
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
; /* paranoia */
732 static int pvr2_g_crop(struct file
*file
, void *priv
, struct v4l2_crop
*crop
)
734 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
735 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
739 if (crop
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
741 ret
= pvr2_ctrl_get_value(
742 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPL
), &val
);
746 ret
= pvr2_ctrl_get_value(
747 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPT
), &val
);
751 ret
= pvr2_ctrl_get_value(
752 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPW
), &val
);
756 ret
= pvr2_ctrl_get_value(
757 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPH
), &val
);
760 crop
->c
.height
= val
;
764 static int pvr2_s_crop(struct file
*file
, void *priv
, const struct v4l2_crop
*crop
)
766 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
767 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
770 if (crop
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
772 ret
= pvr2_ctrl_set_value(
773 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPL
),
777 ret
= pvr2_ctrl_set_value(
778 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPT
),
782 ret
= pvr2_ctrl_set_value(
783 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPW
),
787 ret
= pvr2_ctrl_set_value(
788 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPH
),
795 static int pvr2_log_status(struct file
*file
, void *priv
)
797 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
798 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
800 pvr2_hdw_trigger_module_log(hdw
);
804 static const struct v4l2_ioctl_ops pvr2_ioctl_ops
= {
805 .vidioc_querycap
= pvr2_querycap
,
806 .vidioc_g_priority
= pvr2_g_priority
,
807 .vidioc_s_priority
= pvr2_s_priority
,
808 .vidioc_s_audio
= pvr2_s_audio
,
809 .vidioc_g_audio
= pvr2_g_audio
,
810 .vidioc_enumaudio
= pvr2_enumaudio
,
811 .vidioc_enum_input
= pvr2_enum_input
,
812 .vidioc_cropcap
= pvr2_cropcap
,
813 .vidioc_s_crop
= pvr2_s_crop
,
814 .vidioc_g_crop
= pvr2_g_crop
,
815 .vidioc_g_input
= pvr2_g_input
,
816 .vidioc_s_input
= pvr2_s_input
,
817 .vidioc_g_frequency
= pvr2_g_frequency
,
818 .vidioc_s_frequency
= pvr2_s_frequency
,
819 .vidioc_s_tuner
= pvr2_s_tuner
,
820 .vidioc_g_tuner
= pvr2_g_tuner
,
821 .vidioc_g_std
= pvr2_g_std
,
822 .vidioc_s_std
= pvr2_s_std
,
823 .vidioc_querystd
= pvr2_querystd
,
824 .vidioc_log_status
= pvr2_log_status
,
825 .vidioc_enum_fmt_vid_cap
= pvr2_enum_fmt_vid_cap
,
826 .vidioc_g_fmt_vid_cap
= pvr2_g_fmt_vid_cap
,
827 .vidioc_s_fmt_vid_cap
= pvr2_s_fmt_vid_cap
,
828 .vidioc_try_fmt_vid_cap
= pvr2_try_fmt_vid_cap
,
829 .vidioc_streamon
= pvr2_streamon
,
830 .vidioc_streamoff
= pvr2_streamoff
,
831 .vidioc_queryctrl
= pvr2_queryctrl
,
832 .vidioc_querymenu
= pvr2_querymenu
,
833 .vidioc_g_ctrl
= pvr2_g_ctrl
,
834 .vidioc_s_ctrl
= pvr2_s_ctrl
,
835 .vidioc_g_ext_ctrls
= pvr2_g_ext_ctrls
,
836 .vidioc_s_ext_ctrls
= pvr2_s_ext_ctrls
,
837 .vidioc_try_ext_ctrls
= pvr2_try_ext_ctrls
,
840 static void pvr2_v4l2_dev_destroy(struct pvr2_v4l2_dev
*dip
)
842 struct pvr2_hdw
*hdw
= dip
->v4lp
->channel
.mc_head
->hdw
;
843 enum pvr2_config cfg
= dip
->config
;
847 /* Construct the unregistration message *before* we actually
848 perform the unregistration step. By doing it this way we don't
849 have to worry about potentially touching deleted resources. */
850 mcnt
= scnprintf(msg
, sizeof(msg
) - 1,
851 "pvrusb2: unregistered device %s [%s]",
852 video_device_node_name(&dip
->devbase
),
853 pvr2_config_get_name(cfg
));
856 pvr2_hdw_v4l_store_minor_number(hdw
,dip
->minor_type
,-1);
862 /* Actual deallocation happens later when all internal references
864 video_unregister_device(&dip
->devbase
);
866 printk(KERN_INFO
"%s\n", msg
);
871 static void pvr2_v4l2_dev_disassociate_parent(struct pvr2_v4l2_dev
*dip
)
874 if (!dip
->devbase
.v4l2_dev
->dev
) return;
875 dip
->devbase
.v4l2_dev
->dev
= NULL
;
876 device_move(&dip
->devbase
.dev
, NULL
, DPM_ORDER_NONE
);
880 static void pvr2_v4l2_destroy_no_lock(struct pvr2_v4l2
*vp
)
883 pvr2_v4l2_dev_destroy(vp
->dev_video
);
884 vp
->dev_video
= NULL
;
887 pvr2_v4l2_dev_destroy(vp
->dev_radio
);
888 vp
->dev_radio
= NULL
;
891 pvr2_trace(PVR2_TRACE_STRUCT
,"Destroying pvr2_v4l2 id=%p",vp
);
892 pvr2_channel_done(&vp
->channel
);
897 static void pvr2_video_device_release(struct video_device
*vdev
)
899 struct pvr2_v4l2_dev
*dev
;
900 dev
= container_of(vdev
,struct pvr2_v4l2_dev
,devbase
);
905 static void pvr2_v4l2_internal_check(struct pvr2_channel
*chp
)
907 struct pvr2_v4l2
*vp
;
908 vp
= container_of(chp
,struct pvr2_v4l2
,channel
);
909 if (!vp
->channel
.mc_head
->disconnect_flag
) return;
910 pvr2_v4l2_dev_disassociate_parent(vp
->dev_video
);
911 pvr2_v4l2_dev_disassociate_parent(vp
->dev_radio
);
912 if (vp
->vfirst
) return;
913 pvr2_v4l2_destroy_no_lock(vp
);
917 static long pvr2_v4l2_ioctl(struct file
*file
,
918 unsigned int cmd
, unsigned long arg
)
921 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
922 struct pvr2_v4l2
*vp
= fh
->vhead
;
923 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
926 if (pvrusb2_debug
& PVR2_TRACE_V4LIOCTL
)
927 v4l_printk_ioctl(pvr2_hdw_get_driver_name(hdw
), cmd
);
929 if (!pvr2_hdw_dev_ok(hdw
)) {
930 pvr2_trace(PVR2_TRACE_ERROR_LEGS
,
931 "ioctl failed - bad or no context");
941 case VIDIOC_S_FREQUENCY
:
942 ret
= v4l2_prio_check(&vp
->prio
, fh
->prio
);
947 ret
= video_ioctl2(file
, cmd
, arg
);
949 pvr2_hdw_commit_ctl(hdw
);
952 if (pvrusb2_debug
& PVR2_TRACE_V4LIOCTL
) {
953 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
954 "pvr2_v4l2_do_ioctl failure, ret=%ld", ret
);
956 if (pvrusb2_debug
& PVR2_TRACE_V4LIOCTL
) {
957 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
958 "pvr2_v4l2_do_ioctl failure, ret=%ld"
959 " command was:", ret
);
960 v4l_printk_ioctl(pvr2_hdw_get_driver_name(hdw
),
965 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
966 "pvr2_v4l2_do_ioctl complete, ret=%ld (0x%lx)",
974 static int pvr2_v4l2_release(struct file
*file
)
976 struct pvr2_v4l2_fh
*fhp
= file
->private_data
;
977 struct pvr2_v4l2
*vp
= fhp
->vhead
;
978 struct pvr2_hdw
*hdw
= fhp
->channel
.mc_head
->hdw
;
980 pvr2_trace(PVR2_TRACE_OPEN_CLOSE
,"pvr2_v4l2_release");
983 struct pvr2_stream
*sp
;
984 pvr2_hdw_set_streaming(hdw
,0);
985 sp
= pvr2_ioread_get_stream(fhp
->rhp
);
986 if (sp
) pvr2_stream_set_callback(sp
,NULL
,NULL
);
987 pvr2_ioread_destroy(fhp
->rhp
);
991 v4l2_prio_close(&vp
->prio
, fhp
->prio
);
992 file
->private_data
= NULL
;
995 fhp
->vnext
->vprev
= fhp
->vprev
;
997 vp
->vlast
= fhp
->vprev
;
1000 fhp
->vprev
->vnext
= fhp
->vnext
;
1002 vp
->vfirst
= fhp
->vnext
;
1007 pvr2_channel_done(&fhp
->channel
);
1008 pvr2_trace(PVR2_TRACE_STRUCT
,
1009 "Destroying pvr_v4l2_fh id=%p",fhp
);
1010 if (fhp
->input_map
) {
1011 kfree(fhp
->input_map
);
1012 fhp
->input_map
= NULL
;
1015 if (vp
->channel
.mc_head
->disconnect_flag
&& !vp
->vfirst
) {
1016 pvr2_v4l2_destroy_no_lock(vp
);
1022 static int pvr2_v4l2_open(struct file
*file
)
1024 struct pvr2_v4l2_dev
*dip
; /* Our own context pointer */
1025 struct pvr2_v4l2_fh
*fhp
;
1026 struct pvr2_v4l2
*vp
;
1027 struct pvr2_hdw
*hdw
;
1028 unsigned int input_mask
= 0;
1029 unsigned int input_cnt
,idx
;
1032 dip
= container_of(video_devdata(file
),struct pvr2_v4l2_dev
,devbase
);
1035 hdw
= vp
->channel
.hdw
;
1037 pvr2_trace(PVR2_TRACE_OPEN_CLOSE
,"pvr2_v4l2_open");
1039 if (!pvr2_hdw_dev_ok(hdw
)) {
1040 pvr2_trace(PVR2_TRACE_OPEN_CLOSE
,
1041 "pvr2_v4l2_open: hardware not ready");
1045 fhp
= kzalloc(sizeof(*fhp
),GFP_KERNEL
);
1050 init_waitqueue_head(&fhp
->wait_data
);
1053 pvr2_trace(PVR2_TRACE_STRUCT
,"Creating pvr_v4l2_fh id=%p",fhp
);
1054 pvr2_channel_init(&fhp
->channel
,vp
->channel
.mc_head
);
1056 if (dip
->v4l_type
== VFL_TYPE_RADIO
) {
1057 /* Opening device as a radio, legal input selection subset
1058 is just the radio. */
1059 input_mask
= (1 << PVR2_CVAL_INPUT_RADIO
);
1061 /* Opening the main V4L device, legal input selection
1062 subset includes all analog inputs. */
1063 input_mask
= ((1 << PVR2_CVAL_INPUT_RADIO
) |
1064 (1 << PVR2_CVAL_INPUT_TV
) |
1065 (1 << PVR2_CVAL_INPUT_COMPOSITE
) |
1066 (1 << PVR2_CVAL_INPUT_SVIDEO
));
1068 ret
= pvr2_channel_limit_inputs(&fhp
->channel
,input_mask
);
1070 pvr2_channel_done(&fhp
->channel
);
1071 pvr2_trace(PVR2_TRACE_STRUCT
,
1072 "Destroying pvr_v4l2_fh id=%p (input mask error)",
1079 input_mask
&= pvr2_hdw_get_input_available(hdw
);
1081 for (idx
= 0; idx
< (sizeof(input_mask
) << 3); idx
++) {
1082 if (input_mask
& (1 << idx
)) input_cnt
++;
1084 fhp
->input_cnt
= input_cnt
;
1085 fhp
->input_map
= kzalloc(input_cnt
,GFP_KERNEL
);
1086 if (!fhp
->input_map
) {
1087 pvr2_channel_done(&fhp
->channel
);
1088 pvr2_trace(PVR2_TRACE_STRUCT
,
1089 "Destroying pvr_v4l2_fh id=%p (input map failure)",
1095 for (idx
= 0; idx
< (sizeof(input_mask
) << 3); idx
++) {
1096 if (!(input_mask
& (1 << idx
))) continue;
1097 fhp
->input_map
[input_cnt
++] = idx
;
1101 fhp
->vprev
= vp
->vlast
;
1103 vp
->vlast
->vnext
= fhp
;
1111 file
->private_data
= fhp
;
1112 v4l2_prio_open(&vp
->prio
, &fhp
->prio
);
1114 fhp
->fw_mode_flag
= pvr2_hdw_cpufw_get_enabled(hdw
);
1120 static void pvr2_v4l2_notify(struct pvr2_v4l2_fh
*fhp
)
1122 wake_up(&fhp
->wait_data
);
1125 static int pvr2_v4l2_iosetup(struct pvr2_v4l2_fh
*fh
)
1128 struct pvr2_stream
*sp
;
1129 struct pvr2_hdw
*hdw
;
1130 if (fh
->rhp
) return 0;
1132 if (!fh
->pdi
->stream
) {
1133 /* No stream defined for this node. This means that we're
1134 not currently allowed to stream from this node. */
1138 /* First read() attempt. Try to claim the stream and start
1140 if ((ret
= pvr2_channel_claim_stream(&fh
->channel
,
1141 fh
->pdi
->stream
)) != 0) {
1142 /* Someone else must already have it */
1146 fh
->rhp
= pvr2_channel_create_mpeg_stream(fh
->pdi
->stream
);
1148 pvr2_channel_claim_stream(&fh
->channel
,NULL
);
1152 hdw
= fh
->channel
.mc_head
->hdw
;
1153 sp
= fh
->pdi
->stream
->stream
;
1154 pvr2_stream_set_callback(sp
,(pvr2_stream_callback
)pvr2_v4l2_notify
,fh
);
1155 pvr2_hdw_set_stream_type(hdw
,fh
->pdi
->config
);
1156 if ((ret
= pvr2_hdw_set_streaming(hdw
,!0)) < 0) return ret
;
1157 return pvr2_ioread_set_enabled(fh
->rhp
,!0);
1161 static ssize_t
pvr2_v4l2_read(struct file
*file
,
1162 char __user
*buff
, size_t count
, loff_t
*ppos
)
1164 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
1167 if (fh
->fw_mode_flag
) {
1168 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
1172 unsigned int offs
= *ppos
;
1174 tbuf
= kmalloc(PAGE_SIZE
,GFP_KERNEL
);
1175 if (!tbuf
) return -ENOMEM
;
1179 if (c1
> PAGE_SIZE
) c1
= PAGE_SIZE
;
1180 c2
= pvr2_hdw_cpufw_get(hdw
,offs
,tbuf
,c1
);
1186 if (copy_to_user(buff
,tbuf
,c2
)) {
1201 ret
= pvr2_v4l2_iosetup(fh
);
1208 ret
= pvr2_ioread_read(fh
->rhp
,buff
,count
);
1209 if (ret
>= 0) break;
1210 if (ret
!= -EAGAIN
) break;
1211 if (file
->f_flags
& O_NONBLOCK
) break;
1212 /* Doing blocking I/O. Wait here. */
1213 ret
= wait_event_interruptible(
1215 pvr2_ioread_avail(fh
->rhp
) >= 0);
1223 static unsigned int pvr2_v4l2_poll(struct file
*file
, poll_table
*wait
)
1225 unsigned int mask
= 0;
1226 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
1229 if (fh
->fw_mode_flag
) {
1230 mask
|= POLLIN
| POLLRDNORM
;
1235 ret
= pvr2_v4l2_iosetup(fh
);
1236 if (ret
) return POLLERR
;
1239 poll_wait(file
,&fh
->wait_data
,wait
);
1241 if (pvr2_ioread_avail(fh
->rhp
) >= 0) {
1242 mask
|= POLLIN
| POLLRDNORM
;
1249 static const struct v4l2_file_operations vdev_fops
= {
1250 .owner
= THIS_MODULE
,
1251 .open
= pvr2_v4l2_open
,
1252 .release
= pvr2_v4l2_release
,
1253 .read
= pvr2_v4l2_read
,
1254 .ioctl
= pvr2_v4l2_ioctl
,
1255 .poll
= pvr2_v4l2_poll
,
1259 static struct video_device vdev_template
= {
1264 static void pvr2_v4l2_dev_init(struct pvr2_v4l2_dev
*dip
,
1265 struct pvr2_v4l2
*vp
,
1270 struct pvr2_hdw
*hdw
;
1274 hdw
= vp
->channel
.mc_head
->hdw
;
1275 dip
->v4l_type
= v4l_type
;
1277 case VFL_TYPE_GRABBER
:
1278 dip
->stream
= &vp
->channel
.mc_head
->video_stream
;
1279 dip
->config
= pvr2_config_mpeg
;
1280 dip
->minor_type
= pvr2_v4l_type_video
;
1283 pr_err(KBUILD_MODNAME
1284 ": Failed to set up pvrusb2 v4l video dev"
1285 " due to missing stream instance\n");
1290 dip
->config
= pvr2_config_vbi
;
1291 dip
->minor_type
= pvr2_v4l_type_vbi
;
1294 case VFL_TYPE_RADIO
:
1295 dip
->stream
= &vp
->channel
.mc_head
->video_stream
;
1296 dip
->config
= pvr2_config_mpeg
;
1297 dip
->minor_type
= pvr2_v4l_type_radio
;
1301 /* Bail out (this should be impossible) */
1302 pr_err(KBUILD_MODNAME
": Failed to set up pvrusb2 v4l dev"
1303 " due to unrecognized config\n");
1307 dip
->devbase
= vdev_template
;
1308 dip
->devbase
.release
= pvr2_video_device_release
;
1309 dip
->devbase
.ioctl_ops
= &pvr2_ioctl_ops
;
1312 pvr2_ctrl_get_value(
1313 pvr2_hdw_get_ctrl_by_id(hdw
,
1314 PVR2_CID_STDAVAIL
), &val
);
1315 dip
->devbase
.tvnorms
= (v4l2_std_id
)val
;
1319 unit_number
= pvr2_hdw_get_unit_number(hdw
);
1320 if (nr_ptr
&& (unit_number
>= 0) && (unit_number
< PVR_NUM
)) {
1321 mindevnum
= nr_ptr
[unit_number
];
1323 pvr2_hdw_set_v4l2_dev(hdw
, &dip
->devbase
);
1324 if ((video_register_device(&dip
->devbase
,
1325 dip
->v4l_type
, mindevnum
) < 0) &&
1326 (video_register_device(&dip
->devbase
,
1327 dip
->v4l_type
, -1) < 0)) {
1328 pr_err(KBUILD_MODNAME
1329 ": Failed to register pvrusb2 v4l device\n");
1332 printk(KERN_INFO
"pvrusb2: registered device %s [%s]\n",
1333 video_device_node_name(&dip
->devbase
),
1334 pvr2_config_get_name(dip
->config
));
1336 pvr2_hdw_v4l_store_minor_number(hdw
,
1337 dip
->minor_type
,dip
->devbase
.minor
);
1341 struct pvr2_v4l2
*pvr2_v4l2_create(struct pvr2_context
*mnp
)
1343 struct pvr2_v4l2
*vp
;
1345 vp
= kzalloc(sizeof(*vp
),GFP_KERNEL
);
1347 pvr2_channel_init(&vp
->channel
,mnp
);
1348 pvr2_trace(PVR2_TRACE_STRUCT
,"Creating pvr2_v4l2 id=%p",vp
);
1350 vp
->channel
.check_func
= pvr2_v4l2_internal_check
;
1352 /* register streams */
1353 vp
->dev_video
= kzalloc(sizeof(*vp
->dev_video
),GFP_KERNEL
);
1354 if (!vp
->dev_video
) goto fail
;
1355 pvr2_v4l2_dev_init(vp
->dev_video
,vp
,VFL_TYPE_GRABBER
);
1356 if (pvr2_hdw_get_input_available(vp
->channel
.mc_head
->hdw
) &
1357 (1 << PVR2_CVAL_INPUT_RADIO
)) {
1358 vp
->dev_radio
= kzalloc(sizeof(*vp
->dev_radio
),GFP_KERNEL
);
1359 if (!vp
->dev_radio
) goto fail
;
1360 pvr2_v4l2_dev_init(vp
->dev_radio
,vp
,VFL_TYPE_RADIO
);
1365 pvr2_trace(PVR2_TRACE_STRUCT
,"Failure creating pvr2_v4l2 id=%p",vp
);
1366 pvr2_v4l2_destroy_no_lock(vp
);
1371 Stuff for Emacs to see, in order to encourage consistent editing style:
1372 *** Local Variables: ***
1374 *** fill-column: 75 ***
1375 *** tab-width: 8 ***
1376 *** c-basic-offset: 8 ***