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.
18 #include <linux/kernel.h>
19 #include <linux/slab.h>
20 #include "pvrusb2-context.h"
21 #include "pvrusb2-hdw.h"
23 #include "pvrusb2-debug.h"
24 #include "pvrusb2-v4l2.h"
25 #include "pvrusb2-ioread.h"
26 #include <linux/videodev2.h>
27 #include <linux/module.h>
28 #include <media/v4l2-dev.h>
29 #include <media/v4l2-device.h>
30 #include <media/v4l2-fh.h>
31 #include <media/v4l2-common.h>
32 #include <media/v4l2-ioctl.h>
38 struct pvr2_v4l2_dev
{
39 struct video_device devbase
; /* MUST be first! */
40 struct pvr2_v4l2
*v4lp
;
41 struct pvr2_context_stream
*stream
;
42 /* Information about this device: */
43 enum pvr2_config config
; /* Expected stream format */
44 int v4l_type
; /* V4L defined type for this device node */
45 enum pvr2_v4l_type minor_type
; /* pvr2-understood minor device type */
50 struct pvr2_channel channel
;
51 struct pvr2_v4l2_dev
*pdi
;
52 struct pvr2_ioread
*rhp
;
54 wait_queue_head_t wait_data
;
56 /* Map contiguous ordinal value to input id */
57 unsigned char *input_map
;
58 unsigned int input_cnt
;
62 struct pvr2_channel channel
;
64 /* streams - Note that these must be separately, individually,
65 * allocated pointers. This is because the v4l core is going to
66 * manage their deletion - separately, individually... */
67 struct pvr2_v4l2_dev
*dev_video
;
68 struct pvr2_v4l2_dev
*dev_radio
;
71 static int video_nr
[PVR_NUM
] = {[0 ... PVR_NUM
-1] = -1};
72 module_param_array(video_nr
, int, NULL
, 0444);
73 MODULE_PARM_DESC(video_nr
, "Offset for device's video dev minor");
74 static int radio_nr
[PVR_NUM
] = {[0 ... PVR_NUM
-1] = -1};
75 module_param_array(radio_nr
, int, NULL
, 0444);
76 MODULE_PARM_DESC(radio_nr
, "Offset for device's radio dev minor");
77 static int vbi_nr
[PVR_NUM
] = {[0 ... PVR_NUM
-1] = -1};
78 module_param_array(vbi_nr
, int, NULL
, 0444);
79 MODULE_PARM_DESC(vbi_nr
, "Offset for device's vbi dev minor");
81 static struct v4l2_fmtdesc pvr_fmtdesc
[] = {
84 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
85 .flags
= V4L2_FMT_FLAG_COMPRESSED
,
86 .description
= "MPEG1/2",
87 // This should really be V4L2_PIX_FMT_MPEG, but xawtv
88 // breaks when I do that.
89 .pixelformat
= 0, // V4L2_PIX_FMT_MPEG,
93 #define PVR_FORMAT_PIX 0
94 #define PVR_FORMAT_VBI 1
96 static struct v4l2_format pvr_format
[] = {
98 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
103 // This should really be V4L2_PIX_FMT_MPEG,
104 // but xawtv breaks when I do that.
105 .pixelformat
= 0, // V4L2_PIX_FMT_MPEG,
106 .field
= V4L2_FIELD_INTERLACED
,
107 .bytesperline
= 0, // doesn't make sense
109 //FIXME : Don't know what to put here...
110 .sizeimage
= (32*1024),
111 .colorspace
= 0, // doesn't make sense here
117 .type
= V4L2_BUF_TYPE_VBI_CAPTURE
,
120 .sampling_rate
= 27000000,
122 .samples_per_line
= 1443,
123 .sample_format
= V4L2_PIX_FMT_GREY
,
135 * This is part of Video 4 Linux API. These procedures handle ioctl() calls.
137 static int pvr2_querycap(struct file
*file
, void *priv
, struct v4l2_capability
*cap
)
139 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
140 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
142 strlcpy(cap
->driver
, "pvrusb2", sizeof(cap
->driver
));
143 strlcpy(cap
->bus_info
, pvr2_hdw_get_bus_info(hdw
),
144 sizeof(cap
->bus_info
));
145 strlcpy(cap
->card
, pvr2_hdw_get_desc(hdw
), sizeof(cap
->card
));
146 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_TUNER
|
147 V4L2_CAP_AUDIO
| V4L2_CAP_RADIO
|
148 V4L2_CAP_READWRITE
| V4L2_CAP_DEVICE_CAPS
;
149 switch (fh
->pdi
->devbase
.vfl_type
) {
150 case VFL_TYPE_GRABBER
:
151 cap
->device_caps
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_AUDIO
;
154 cap
->device_caps
= V4L2_CAP_RADIO
;
157 cap
->device_caps
|= V4L2_CAP_TUNER
| V4L2_CAP_READWRITE
;
161 static int pvr2_g_std(struct file
*file
, void *priv
, v4l2_std_id
*std
)
163 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
164 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
168 ret
= pvr2_ctrl_get_value(
169 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_STDCUR
), &val
);
174 static int pvr2_s_std(struct file
*file
, void *priv
, v4l2_std_id std
)
176 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
177 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
179 return pvr2_ctrl_set_value(
180 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_STDCUR
), std
);
183 static int pvr2_querystd(struct file
*file
, void *priv
, v4l2_std_id
*std
)
185 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
186 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
190 ret
= pvr2_ctrl_get_value(
191 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_STDDETECT
), &val
);
196 static int pvr2_enum_input(struct file
*file
, void *priv
, struct v4l2_input
*vi
)
198 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
199 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
200 struct pvr2_ctrl
*cptr
;
201 struct v4l2_input tmp
;
205 cptr
= pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_INPUT
);
207 memset(&tmp
, 0, sizeof(tmp
));
208 tmp
.index
= vi
->index
;
209 if (vi
->index
>= fh
->input_cnt
)
211 val
= fh
->input_map
[vi
->index
];
213 case PVR2_CVAL_INPUT_TV
:
214 case PVR2_CVAL_INPUT_DTV
:
215 case PVR2_CVAL_INPUT_RADIO
:
216 tmp
.type
= V4L2_INPUT_TYPE_TUNER
;
218 case PVR2_CVAL_INPUT_SVIDEO
:
219 case PVR2_CVAL_INPUT_COMPOSITE
:
220 tmp
.type
= V4L2_INPUT_TYPE_CAMERA
;
227 pvr2_ctrl_get_valname(cptr
, val
,
228 tmp
.name
, sizeof(tmp
.name
) - 1, &cnt
);
231 /* Don't bother with audioset, since this driver currently
232 always switches the audio whenever the video is
235 /* Handling std is a tougher problem. It doesn't make
236 sense in cases where a device might be multi-standard.
237 We could just copy out the current value for the
238 standard, but it can change over time. For now just
244 static int pvr2_g_input(struct file
*file
, void *priv
, unsigned int *i
)
246 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
247 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
249 struct pvr2_ctrl
*cptr
;
253 cptr
= pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_INPUT
);
255 ret
= pvr2_ctrl_get_value(cptr
, &val
);
257 for (idx
= 0; idx
< fh
->input_cnt
; idx
++) {
258 if (fh
->input_map
[idx
] == val
) {
266 static int pvr2_s_input(struct file
*file
, void *priv
, unsigned int inp
)
268 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
269 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
271 if (inp
>= fh
->input_cnt
)
273 return pvr2_ctrl_set_value(
274 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_INPUT
),
278 static int pvr2_enumaudio(struct file
*file
, void *priv
, struct v4l2_audio
*vin
)
280 /* pkt: FIXME: We are returning one "fake" input here
281 which could very well be called "whatever_we_like".
282 This is for apps that want to see an audio input
283 just to feel comfortable, as well as to test if
284 it can do stereo or sth. There is actually no guarantee
285 that the actual audio input cannot change behind the app's
286 back, but most applications should not mind that either.
288 Hopefully, mplayer people will work with us on this (this
289 whole mess is to support mplayer pvr://), or Hans will come
290 up with a more standard way to say "we have inputs but we
291 don 't want you to change them independent of video" which
297 strncpy(vin
->name
, "PVRUSB2 Audio", 14);
298 vin
->capability
= V4L2_AUDCAP_STEREO
;
302 static int pvr2_g_audio(struct file
*file
, void *priv
, struct v4l2_audio
*vin
)
304 /* pkt: FIXME: see above comment (VIDIOC_ENUMAUDIO) */
306 strncpy(vin
->name
, "PVRUSB2 Audio", 14);
307 vin
->capability
= V4L2_AUDCAP_STEREO
;
311 static int pvr2_s_audio(struct file
*file
, void *priv
, const struct v4l2_audio
*vout
)
318 static int pvr2_g_tuner(struct file
*file
, void *priv
, struct v4l2_tuner
*vt
)
320 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
321 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
324 return -EINVAL
; /* Only answer for the 1st tuner */
326 pvr2_hdw_execute_tuner_poll(hdw
);
327 return pvr2_hdw_get_tuner_status(hdw
, vt
);
330 static int pvr2_s_tuner(struct file
*file
, void *priv
, const struct v4l2_tuner
*vt
)
332 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
333 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
338 return pvr2_ctrl_set_value(
339 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_AUDIOMODE
),
343 static int pvr2_s_frequency(struct file
*file
, void *priv
, const struct v4l2_frequency
*vf
)
345 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
346 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
348 struct v4l2_tuner vt
;
350 struct pvr2_ctrl
*ctrlp
;
353 ret
= pvr2_hdw_get_tuner_status(hdw
, &vt
);
356 ctrlp
= pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_INPUT
);
357 ret
= pvr2_ctrl_get_value(ctrlp
, &cur_input
);
360 if (vf
->type
== V4L2_TUNER_RADIO
) {
361 if (cur_input
!= PVR2_CVAL_INPUT_RADIO
)
362 pvr2_ctrl_set_value(ctrlp
, PVR2_CVAL_INPUT_RADIO
);
364 if (cur_input
== PVR2_CVAL_INPUT_RADIO
)
365 pvr2_ctrl_set_value(ctrlp
, PVR2_CVAL_INPUT_TV
);
368 if (vt
.capability
& V4L2_TUNER_CAP_LOW
)
372 return pvr2_ctrl_set_value(
373 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_FREQUENCY
),fv
);
376 static int pvr2_g_frequency(struct file
*file
, void *priv
, struct v4l2_frequency
*vf
)
378 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
379 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
382 struct v4l2_tuner vt
;
385 ret
= pvr2_hdw_get_tuner_status(hdw
, &vt
);
388 ret
= pvr2_ctrl_get_value(
389 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_FREQUENCY
),
394 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_INPUT
),
396 if (cur_input
== PVR2_CVAL_INPUT_RADIO
)
397 vf
->type
= V4L2_TUNER_RADIO
;
399 vf
->type
= V4L2_TUNER_ANALOG_TV
;
400 if (vt
.capability
& V4L2_TUNER_CAP_LOW
)
401 val
= (val
* 2) / 125;
408 static int pvr2_enum_fmt_vid_cap(struct file
*file
, void *priv
, struct v4l2_fmtdesc
*fd
)
410 /* Only one format is supported : mpeg.*/
414 memcpy(fd
, pvr_fmtdesc
, sizeof(struct v4l2_fmtdesc
));
418 static int pvr2_g_fmt_vid_cap(struct file
*file
, void *priv
, struct v4l2_format
*vf
)
420 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
421 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
424 memcpy(vf
, &pvr_format
[PVR_FORMAT_PIX
], sizeof(struct v4l2_format
));
427 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_HRES
),
429 vf
->fmt
.pix
.width
= val
;
432 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_VRES
),
434 vf
->fmt
.pix
.height
= val
;
438 static int pvr2_try_fmt_vid_cap(struct file
*file
, void *priv
, struct v4l2_format
*vf
)
440 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
441 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
442 int lmin
, lmax
, ldef
;
443 struct pvr2_ctrl
*hcp
, *vcp
;
444 int h
= vf
->fmt
.pix
.height
;
445 int w
= vf
->fmt
.pix
.width
;
447 hcp
= pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_HRES
);
448 vcp
= pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_VRES
);
450 lmin
= pvr2_ctrl_get_min(hcp
);
451 lmax
= pvr2_ctrl_get_max(hcp
);
452 pvr2_ctrl_get_def(hcp
, &ldef
);
459 lmin
= pvr2_ctrl_get_min(vcp
);
460 lmax
= pvr2_ctrl_get_max(vcp
);
461 pvr2_ctrl_get_def(vcp
, &ldef
);
469 memcpy(vf
, &pvr_format
[PVR_FORMAT_PIX
],
470 sizeof(struct v4l2_format
));
471 vf
->fmt
.pix
.width
= w
;
472 vf
->fmt
.pix
.height
= h
;
476 static int pvr2_s_fmt_vid_cap(struct file
*file
, void *priv
, struct v4l2_format
*vf
)
478 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
479 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
480 struct pvr2_ctrl
*hcp
, *vcp
;
481 int ret
= pvr2_try_fmt_vid_cap(file
, fh
, vf
);
485 hcp
= pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_HRES
);
486 vcp
= pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_VRES
);
487 pvr2_ctrl_set_value(hcp
, vf
->fmt
.pix
.width
);
488 pvr2_ctrl_set_value(vcp
, vf
->fmt
.pix
.height
);
492 static int pvr2_streamon(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
494 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
495 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
496 struct pvr2_v4l2_dev
*pdi
= fh
->pdi
;
499 if (!fh
->pdi
->stream
) {
500 /* No stream defined for this node. This means
501 that we're not currently allowed to stream from
505 ret
= pvr2_hdw_set_stream_type(hdw
, pdi
->config
);
508 return pvr2_hdw_set_streaming(hdw
, !0);
511 static int pvr2_streamoff(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
513 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
514 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
516 if (!fh
->pdi
->stream
) {
517 /* No stream defined for this node. This means
518 that we're not currently allowed to stream from
522 return pvr2_hdw_set_streaming(hdw
, 0);
525 static int pvr2_queryctrl(struct file
*file
, void *priv
,
526 struct v4l2_queryctrl
*vc
)
528 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
529 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
530 struct pvr2_ctrl
*cptr
;
533 if (vc
->id
& V4L2_CTRL_FLAG_NEXT_CTRL
) {
534 cptr
= pvr2_hdw_get_ctrl_nextv4l(
535 hdw
, (vc
->id
& ~V4L2_CTRL_FLAG_NEXT_CTRL
));
537 vc
->id
= pvr2_ctrl_get_v4lid(cptr
);
539 cptr
= pvr2_hdw_get_ctrl_v4l(hdw
, vc
->id
);
542 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
543 "QUERYCTRL id=0x%x not implemented here",
548 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
549 "QUERYCTRL id=0x%x mapping name=%s (%s)",
550 vc
->id
, pvr2_ctrl_get_name(cptr
),
551 pvr2_ctrl_get_desc(cptr
));
552 strlcpy(vc
->name
, pvr2_ctrl_get_desc(cptr
), sizeof(vc
->name
));
553 vc
->flags
= pvr2_ctrl_get_v4lflags(cptr
);
554 pvr2_ctrl_get_def(cptr
, &val
);
555 vc
->default_value
= val
;
556 switch (pvr2_ctrl_get_type(cptr
)) {
558 vc
->type
= V4L2_CTRL_TYPE_MENU
;
560 vc
->maximum
= pvr2_ctrl_get_cnt(cptr
) - 1;
564 vc
->type
= V4L2_CTRL_TYPE_BOOLEAN
;
570 vc
->type
= V4L2_CTRL_TYPE_INTEGER
;
571 vc
->minimum
= pvr2_ctrl_get_min(cptr
);
572 vc
->maximum
= pvr2_ctrl_get_max(cptr
);
576 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
577 "QUERYCTRL id=0x%x name=%s not mappable",
578 vc
->id
, pvr2_ctrl_get_name(cptr
));
584 static int pvr2_querymenu(struct file
*file
, void *priv
, struct v4l2_querymenu
*vm
)
586 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
587 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
588 unsigned int cnt
= 0;
591 ret
= pvr2_ctrl_get_valname(pvr2_hdw_get_ctrl_v4l(hdw
, vm
->id
),
593 vm
->name
, sizeof(vm
->name
) - 1,
599 static int pvr2_g_ctrl(struct file
*file
, void *priv
, struct v4l2_control
*vc
)
601 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
602 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
606 ret
= pvr2_ctrl_get_value(pvr2_hdw_get_ctrl_v4l(hdw
, vc
->id
),
612 static int pvr2_s_ctrl(struct file
*file
, void *priv
, struct v4l2_control
*vc
)
614 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
615 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
617 return pvr2_ctrl_set_value(pvr2_hdw_get_ctrl_v4l(hdw
, vc
->id
),
621 static int pvr2_g_ext_ctrls(struct file
*file
, void *priv
,
622 struct v4l2_ext_controls
*ctls
)
624 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
625 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
626 struct v4l2_ext_control
*ctrl
;
627 struct pvr2_ctrl
*cptr
;
633 for (idx
= 0; idx
< ctls
->count
; idx
++) {
634 ctrl
= ctls
->controls
+ idx
;
635 cptr
= pvr2_hdw_get_ctrl_v4l(hdw
, ctrl
->id
);
637 if (ctls
->which
== V4L2_CTRL_WHICH_DEF_VAL
)
638 pvr2_ctrl_get_def(cptr
, &val
);
640 ret
= pvr2_ctrl_get_value(cptr
, &val
);
645 ctls
->error_idx
= idx
;
648 /* Ensure that if read as a 64 bit value, the user
649 will still get a hopefully sane value */
656 static int pvr2_s_ext_ctrls(struct file
*file
, void *priv
,
657 struct v4l2_ext_controls
*ctls
)
659 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
660 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
661 struct v4l2_ext_control
*ctrl
;
665 /* Default value cannot be changed */
666 if (ctls
->which
== V4L2_CTRL_WHICH_DEF_VAL
)
670 for (idx
= 0; idx
< ctls
->count
; idx
++) {
671 ctrl
= ctls
->controls
+ idx
;
672 ret
= pvr2_ctrl_set_value(
673 pvr2_hdw_get_ctrl_v4l(hdw
, ctrl
->id
),
676 ctls
->error_idx
= idx
;
683 static int pvr2_try_ext_ctrls(struct file
*file
, void *priv
,
684 struct v4l2_ext_controls
*ctls
)
686 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
687 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
688 struct v4l2_ext_control
*ctrl
;
689 struct pvr2_ctrl
*pctl
;
692 /* For the moment just validate that the requested control
694 for (idx
= 0; idx
< ctls
->count
; idx
++) {
695 ctrl
= ctls
->controls
+ idx
;
696 pctl
= pvr2_hdw_get_ctrl_v4l(hdw
, ctrl
->id
);
698 ctls
->error_idx
= idx
;
705 static int pvr2_cropcap(struct file
*file
, void *priv
, struct v4l2_cropcap
*cap
)
707 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
708 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
711 if (cap
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
713 ret
= pvr2_hdw_get_cropcap(hdw
, cap
);
714 cap
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
; /* paranoia */
718 static int pvr2_g_selection(struct file
*file
, void *priv
,
719 struct v4l2_selection
*sel
)
721 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
722 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
723 struct v4l2_cropcap cap
;
727 if (sel
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
730 cap
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
732 switch (sel
->target
) {
733 case V4L2_SEL_TGT_CROP
:
734 ret
= pvr2_ctrl_get_value(
735 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPL
), &val
);
739 ret
= pvr2_ctrl_get_value(
740 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPT
), &val
);
744 ret
= pvr2_ctrl_get_value(
745 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPW
), &val
);
749 ret
= pvr2_ctrl_get_value(
750 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPH
), &val
);
755 case V4L2_SEL_TGT_CROP_DEFAULT
:
756 ret
= pvr2_hdw_get_cropcap(hdw
, &cap
);
757 sel
->r
= cap
.defrect
;
759 case V4L2_SEL_TGT_CROP_BOUNDS
:
760 ret
= pvr2_hdw_get_cropcap(hdw
, &cap
);
769 static int pvr2_s_selection(struct file
*file
, void *priv
,
770 struct v4l2_selection
*sel
)
772 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
773 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
776 if (sel
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
777 sel
->target
!= V4L2_SEL_TGT_CROP
)
779 ret
= pvr2_ctrl_set_value(
780 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPL
),
784 ret
= pvr2_ctrl_set_value(
785 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPT
),
789 ret
= pvr2_ctrl_set_value(
790 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPW
),
794 ret
= pvr2_ctrl_set_value(
795 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPH
),
802 static int pvr2_log_status(struct file
*file
, void *priv
)
804 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
805 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
807 pvr2_hdw_trigger_module_log(hdw
);
811 static const struct v4l2_ioctl_ops pvr2_ioctl_ops
= {
812 .vidioc_querycap
= pvr2_querycap
,
813 .vidioc_s_audio
= pvr2_s_audio
,
814 .vidioc_g_audio
= pvr2_g_audio
,
815 .vidioc_enumaudio
= pvr2_enumaudio
,
816 .vidioc_enum_input
= pvr2_enum_input
,
817 .vidioc_cropcap
= pvr2_cropcap
,
818 .vidioc_s_selection
= pvr2_s_selection
,
819 .vidioc_g_selection
= pvr2_g_selection
,
820 .vidioc_g_input
= pvr2_g_input
,
821 .vidioc_s_input
= pvr2_s_input
,
822 .vidioc_g_frequency
= pvr2_g_frequency
,
823 .vidioc_s_frequency
= pvr2_s_frequency
,
824 .vidioc_s_tuner
= pvr2_s_tuner
,
825 .vidioc_g_tuner
= pvr2_g_tuner
,
826 .vidioc_g_std
= pvr2_g_std
,
827 .vidioc_s_std
= pvr2_s_std
,
828 .vidioc_querystd
= pvr2_querystd
,
829 .vidioc_log_status
= pvr2_log_status
,
830 .vidioc_enum_fmt_vid_cap
= pvr2_enum_fmt_vid_cap
,
831 .vidioc_g_fmt_vid_cap
= pvr2_g_fmt_vid_cap
,
832 .vidioc_s_fmt_vid_cap
= pvr2_s_fmt_vid_cap
,
833 .vidioc_try_fmt_vid_cap
= pvr2_try_fmt_vid_cap
,
834 .vidioc_streamon
= pvr2_streamon
,
835 .vidioc_streamoff
= pvr2_streamoff
,
836 .vidioc_queryctrl
= pvr2_queryctrl
,
837 .vidioc_querymenu
= pvr2_querymenu
,
838 .vidioc_g_ctrl
= pvr2_g_ctrl
,
839 .vidioc_s_ctrl
= pvr2_s_ctrl
,
840 .vidioc_g_ext_ctrls
= pvr2_g_ext_ctrls
,
841 .vidioc_s_ext_ctrls
= pvr2_s_ext_ctrls
,
842 .vidioc_try_ext_ctrls
= pvr2_try_ext_ctrls
,
845 static void pvr2_v4l2_dev_destroy(struct pvr2_v4l2_dev
*dip
)
847 struct pvr2_hdw
*hdw
= dip
->v4lp
->channel
.mc_head
->hdw
;
848 enum pvr2_config cfg
= dip
->config
;
852 /* Construct the unregistration message *before* we actually
853 perform the unregistration step. By doing it this way we don't
854 have to worry about potentially touching deleted resources. */
855 mcnt
= scnprintf(msg
, sizeof(msg
) - 1,
856 "pvrusb2: unregistered device %s [%s]",
857 video_device_node_name(&dip
->devbase
),
858 pvr2_config_get_name(cfg
));
861 pvr2_hdw_v4l_store_minor_number(hdw
,dip
->minor_type
,-1);
867 /* Actual deallocation happens later when all internal references
869 video_unregister_device(&dip
->devbase
);
871 printk(KERN_INFO
"%s\n", msg
);
876 static void pvr2_v4l2_dev_disassociate_parent(struct pvr2_v4l2_dev
*dip
)
879 if (!dip
->devbase
.v4l2_dev
->dev
) return;
880 dip
->devbase
.v4l2_dev
->dev
= NULL
;
881 device_move(&dip
->devbase
.dev
, NULL
, DPM_ORDER_NONE
);
885 static void pvr2_v4l2_destroy_no_lock(struct pvr2_v4l2
*vp
)
888 pvr2_v4l2_dev_destroy(vp
->dev_video
);
889 vp
->dev_video
= NULL
;
892 pvr2_v4l2_dev_destroy(vp
->dev_radio
);
893 vp
->dev_radio
= NULL
;
896 pvr2_trace(PVR2_TRACE_STRUCT
,"Destroying pvr2_v4l2 id=%p",vp
);
897 pvr2_channel_done(&vp
->channel
);
902 static void pvr2_video_device_release(struct video_device
*vdev
)
904 struct pvr2_v4l2_dev
*dev
;
905 dev
= container_of(vdev
,struct pvr2_v4l2_dev
,devbase
);
910 static void pvr2_v4l2_internal_check(struct pvr2_channel
*chp
)
912 struct pvr2_v4l2
*vp
;
913 vp
= container_of(chp
,struct pvr2_v4l2
,channel
);
914 if (!vp
->channel
.mc_head
->disconnect_flag
) return;
915 pvr2_v4l2_dev_disassociate_parent(vp
->dev_video
);
916 pvr2_v4l2_dev_disassociate_parent(vp
->dev_radio
);
917 if (!list_empty(&vp
->dev_video
->devbase
.fh_list
) ||
918 !list_empty(&vp
->dev_radio
->devbase
.fh_list
))
920 pvr2_v4l2_destroy_no_lock(vp
);
924 static long pvr2_v4l2_ioctl(struct file
*file
,
925 unsigned int cmd
, unsigned long arg
)
928 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
929 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
932 if (pvrusb2_debug
& PVR2_TRACE_V4LIOCTL
)
933 v4l_printk_ioctl(pvr2_hdw_get_driver_name(hdw
), cmd
);
935 if (!pvr2_hdw_dev_ok(hdw
)) {
936 pvr2_trace(PVR2_TRACE_ERROR_LEGS
,
937 "ioctl failed - bad or no context");
941 ret
= video_ioctl2(file
, cmd
, arg
);
943 pvr2_hdw_commit_ctl(hdw
);
946 if (pvrusb2_debug
& PVR2_TRACE_V4LIOCTL
) {
947 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
948 "pvr2_v4l2_do_ioctl failure, ret=%ld command was:",
950 v4l_printk_ioctl(pvr2_hdw_get_driver_name(hdw
), cmd
);
953 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
954 "pvr2_v4l2_do_ioctl complete, ret=%ld (0x%lx)",
962 static int pvr2_v4l2_release(struct file
*file
)
964 struct pvr2_v4l2_fh
*fhp
= file
->private_data
;
965 struct pvr2_v4l2
*vp
= fhp
->pdi
->v4lp
;
966 struct pvr2_hdw
*hdw
= fhp
->channel
.mc_head
->hdw
;
968 pvr2_trace(PVR2_TRACE_OPEN_CLOSE
,"pvr2_v4l2_release");
971 struct pvr2_stream
*sp
;
972 pvr2_hdw_set_streaming(hdw
,0);
973 sp
= pvr2_ioread_get_stream(fhp
->rhp
);
974 if (sp
) pvr2_stream_set_callback(sp
,NULL
,NULL
);
975 pvr2_ioread_destroy(fhp
->rhp
);
979 v4l2_fh_del(&fhp
->fh
);
980 v4l2_fh_exit(&fhp
->fh
);
981 file
->private_data
= NULL
;
983 pvr2_channel_done(&fhp
->channel
);
984 pvr2_trace(PVR2_TRACE_STRUCT
,
985 "Destroying pvr_v4l2_fh id=%p",fhp
);
986 if (fhp
->input_map
) {
987 kfree(fhp
->input_map
);
988 fhp
->input_map
= NULL
;
991 if (vp
->channel
.mc_head
->disconnect_flag
&&
992 list_empty(&vp
->dev_video
->devbase
.fh_list
) &&
993 list_empty(&vp
->dev_radio
->devbase
.fh_list
)) {
994 pvr2_v4l2_destroy_no_lock(vp
);
1000 static int pvr2_v4l2_open(struct file
*file
)
1002 struct pvr2_v4l2_dev
*dip
; /* Our own context pointer */
1003 struct pvr2_v4l2_fh
*fhp
;
1004 struct pvr2_v4l2
*vp
;
1005 struct pvr2_hdw
*hdw
;
1006 unsigned int input_mask
= 0;
1007 unsigned int input_cnt
,idx
;
1010 dip
= container_of(video_devdata(file
),struct pvr2_v4l2_dev
,devbase
);
1013 hdw
= vp
->channel
.hdw
;
1015 pvr2_trace(PVR2_TRACE_OPEN_CLOSE
,"pvr2_v4l2_open");
1017 if (!pvr2_hdw_dev_ok(hdw
)) {
1018 pvr2_trace(PVR2_TRACE_OPEN_CLOSE
,
1019 "pvr2_v4l2_open: hardware not ready");
1023 fhp
= kzalloc(sizeof(*fhp
),GFP_KERNEL
);
1028 v4l2_fh_init(&fhp
->fh
, &dip
->devbase
);
1029 init_waitqueue_head(&fhp
->wait_data
);
1032 pvr2_trace(PVR2_TRACE_STRUCT
,"Creating pvr_v4l2_fh id=%p",fhp
);
1033 pvr2_channel_init(&fhp
->channel
,vp
->channel
.mc_head
);
1035 if (dip
->v4l_type
== VFL_TYPE_RADIO
) {
1036 /* Opening device as a radio, legal input selection subset
1037 is just the radio. */
1038 input_mask
= (1 << PVR2_CVAL_INPUT_RADIO
);
1040 /* Opening the main V4L device, legal input selection
1041 subset includes all analog inputs. */
1042 input_mask
= ((1 << PVR2_CVAL_INPUT_RADIO
) |
1043 (1 << PVR2_CVAL_INPUT_TV
) |
1044 (1 << PVR2_CVAL_INPUT_COMPOSITE
) |
1045 (1 << PVR2_CVAL_INPUT_SVIDEO
));
1047 ret
= pvr2_channel_limit_inputs(&fhp
->channel
,input_mask
);
1049 pvr2_channel_done(&fhp
->channel
);
1050 pvr2_trace(PVR2_TRACE_STRUCT
,
1051 "Destroying pvr_v4l2_fh id=%p (input mask error)",
1053 v4l2_fh_exit(&fhp
->fh
);
1058 input_mask
&= pvr2_hdw_get_input_available(hdw
);
1060 for (idx
= 0; idx
< (sizeof(input_mask
) << 3); idx
++) {
1061 if (input_mask
& (1 << idx
)) input_cnt
++;
1063 fhp
->input_cnt
= input_cnt
;
1064 fhp
->input_map
= kzalloc(input_cnt
,GFP_KERNEL
);
1065 if (!fhp
->input_map
) {
1066 pvr2_channel_done(&fhp
->channel
);
1067 pvr2_trace(PVR2_TRACE_STRUCT
,
1068 "Destroying pvr_v4l2_fh id=%p (input map failure)",
1070 v4l2_fh_exit(&fhp
->fh
);
1075 for (idx
= 0; idx
< (sizeof(input_mask
) << 3); idx
++) {
1076 if (!(input_mask
& (1 << idx
))) continue;
1077 fhp
->input_map
[input_cnt
++] = idx
;
1081 file
->private_data
= fhp
;
1083 fhp
->fw_mode_flag
= pvr2_hdw_cpufw_get_enabled(hdw
);
1084 v4l2_fh_add(&fhp
->fh
);
1090 static void pvr2_v4l2_notify(struct pvr2_v4l2_fh
*fhp
)
1092 wake_up(&fhp
->wait_data
);
1095 static int pvr2_v4l2_iosetup(struct pvr2_v4l2_fh
*fh
)
1098 struct pvr2_stream
*sp
;
1099 struct pvr2_hdw
*hdw
;
1100 if (fh
->rhp
) return 0;
1102 if (!fh
->pdi
->stream
) {
1103 /* No stream defined for this node. This means that we're
1104 not currently allowed to stream from this node. */
1108 /* First read() attempt. Try to claim the stream and start
1110 if ((ret
= pvr2_channel_claim_stream(&fh
->channel
,
1111 fh
->pdi
->stream
)) != 0) {
1112 /* Someone else must already have it */
1116 fh
->rhp
= pvr2_channel_create_mpeg_stream(fh
->pdi
->stream
);
1118 pvr2_channel_claim_stream(&fh
->channel
,NULL
);
1122 hdw
= fh
->channel
.mc_head
->hdw
;
1123 sp
= fh
->pdi
->stream
->stream
;
1124 pvr2_stream_set_callback(sp
,(pvr2_stream_callback
)pvr2_v4l2_notify
,fh
);
1125 pvr2_hdw_set_stream_type(hdw
,fh
->pdi
->config
);
1126 if ((ret
= pvr2_hdw_set_streaming(hdw
,!0)) < 0) return ret
;
1127 return pvr2_ioread_set_enabled(fh
->rhp
,!0);
1131 static ssize_t
pvr2_v4l2_read(struct file
*file
,
1132 char __user
*buff
, size_t count
, loff_t
*ppos
)
1134 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
1137 if (fh
->fw_mode_flag
) {
1138 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
1142 unsigned int offs
= *ppos
;
1144 tbuf
= kmalloc(PAGE_SIZE
,GFP_KERNEL
);
1145 if (!tbuf
) return -ENOMEM
;
1149 if (c1
> PAGE_SIZE
) c1
= PAGE_SIZE
;
1150 c2
= pvr2_hdw_cpufw_get(hdw
,offs
,tbuf
,c1
);
1156 if (copy_to_user(buff
,tbuf
,c2
)) {
1171 ret
= pvr2_v4l2_iosetup(fh
);
1178 ret
= pvr2_ioread_read(fh
->rhp
,buff
,count
);
1179 if (ret
>= 0) break;
1180 if (ret
!= -EAGAIN
) break;
1181 if (file
->f_flags
& O_NONBLOCK
) break;
1182 /* Doing blocking I/O. Wait here. */
1183 ret
= wait_event_interruptible(
1185 pvr2_ioread_avail(fh
->rhp
) >= 0);
1193 static unsigned int pvr2_v4l2_poll(struct file
*file
, poll_table
*wait
)
1195 unsigned int mask
= 0;
1196 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
1199 if (fh
->fw_mode_flag
) {
1200 mask
|= POLLIN
| POLLRDNORM
;
1205 ret
= pvr2_v4l2_iosetup(fh
);
1206 if (ret
) return POLLERR
;
1209 poll_wait(file
,&fh
->wait_data
,wait
);
1211 if (pvr2_ioread_avail(fh
->rhp
) >= 0) {
1212 mask
|= POLLIN
| POLLRDNORM
;
1219 static const struct v4l2_file_operations vdev_fops
= {
1220 .owner
= THIS_MODULE
,
1221 .open
= pvr2_v4l2_open
,
1222 .release
= pvr2_v4l2_release
,
1223 .read
= pvr2_v4l2_read
,
1224 .unlocked_ioctl
= pvr2_v4l2_ioctl
,
1225 .poll
= pvr2_v4l2_poll
,
1229 static struct video_device vdev_template
= {
1234 static void pvr2_v4l2_dev_init(struct pvr2_v4l2_dev
*dip
,
1235 struct pvr2_v4l2
*vp
,
1240 struct pvr2_hdw
*hdw
;
1244 hdw
= vp
->channel
.mc_head
->hdw
;
1245 dip
->v4l_type
= v4l_type
;
1247 case VFL_TYPE_GRABBER
:
1248 dip
->stream
= &vp
->channel
.mc_head
->video_stream
;
1249 dip
->config
= pvr2_config_mpeg
;
1250 dip
->minor_type
= pvr2_v4l_type_video
;
1253 pr_err(KBUILD_MODNAME
1254 ": Failed to set up pvrusb2 v4l video dev due to missing stream instance\n");
1259 dip
->config
= pvr2_config_vbi
;
1260 dip
->minor_type
= pvr2_v4l_type_vbi
;
1263 case VFL_TYPE_RADIO
:
1264 dip
->stream
= &vp
->channel
.mc_head
->video_stream
;
1265 dip
->config
= pvr2_config_mpeg
;
1266 dip
->minor_type
= pvr2_v4l_type_radio
;
1270 /* Bail out (this should be impossible) */
1271 pr_err(KBUILD_MODNAME
": Failed to set up pvrusb2 v4l dev due to unrecognized config\n");
1275 dip
->devbase
= vdev_template
;
1276 dip
->devbase
.release
= pvr2_video_device_release
;
1277 dip
->devbase
.ioctl_ops
= &pvr2_ioctl_ops
;
1280 pvr2_ctrl_get_value(
1281 pvr2_hdw_get_ctrl_by_id(hdw
,
1282 PVR2_CID_STDAVAIL
), &val
);
1283 dip
->devbase
.tvnorms
= (v4l2_std_id
)val
;
1287 unit_number
= pvr2_hdw_get_unit_number(hdw
);
1288 if (nr_ptr
&& (unit_number
>= 0) && (unit_number
< PVR_NUM
)) {
1289 mindevnum
= nr_ptr
[unit_number
];
1291 pvr2_hdw_set_v4l2_dev(hdw
, &dip
->devbase
);
1292 if ((video_register_device(&dip
->devbase
,
1293 dip
->v4l_type
, mindevnum
) < 0) &&
1294 (video_register_device(&dip
->devbase
,
1295 dip
->v4l_type
, -1) < 0)) {
1296 pr_err(KBUILD_MODNAME
1297 ": Failed to register pvrusb2 v4l device\n");
1300 printk(KERN_INFO
"pvrusb2: registered device %s [%s]\n",
1301 video_device_node_name(&dip
->devbase
),
1302 pvr2_config_get_name(dip
->config
));
1304 pvr2_hdw_v4l_store_minor_number(hdw
,
1305 dip
->minor_type
,dip
->devbase
.minor
);
1309 struct pvr2_v4l2
*pvr2_v4l2_create(struct pvr2_context
*mnp
)
1311 struct pvr2_v4l2
*vp
;
1313 vp
= kzalloc(sizeof(*vp
),GFP_KERNEL
);
1315 pvr2_channel_init(&vp
->channel
,mnp
);
1316 pvr2_trace(PVR2_TRACE_STRUCT
,"Creating pvr2_v4l2 id=%p",vp
);
1318 vp
->channel
.check_func
= pvr2_v4l2_internal_check
;
1320 /* register streams */
1321 vp
->dev_video
= kzalloc(sizeof(*vp
->dev_video
),GFP_KERNEL
);
1322 if (!vp
->dev_video
) goto fail
;
1323 pvr2_v4l2_dev_init(vp
->dev_video
,vp
,VFL_TYPE_GRABBER
);
1324 if (pvr2_hdw_get_input_available(vp
->channel
.mc_head
->hdw
) &
1325 (1 << PVR2_CVAL_INPUT_RADIO
)) {
1326 vp
->dev_radio
= kzalloc(sizeof(*vp
->dev_radio
),GFP_KERNEL
);
1327 if (!vp
->dev_radio
) goto fail
;
1328 pvr2_v4l2_dev_init(vp
->dev_radio
,vp
,VFL_TYPE_RADIO
);
1333 pvr2_trace(PVR2_TRACE_STRUCT
,"Failure creating pvr2_v4l2 id=%p",vp
);
1334 pvr2_v4l2_destroy_no_lock(vp
);