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 #define PVR_FORMAT_PIX 0
82 #define PVR_FORMAT_VBI 1
84 static struct v4l2_format pvr_format
[] = {
86 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
91 .pixelformat
= V4L2_PIX_FMT_MPEG
,
92 .field
= V4L2_FIELD_INTERLACED
,
93 /* FIXME : Don't know what to put here... */
94 .sizeimage
= 32 * 1024,
99 .type
= V4L2_BUF_TYPE_VBI_CAPTURE
,
102 .sampling_rate
= 27000000,
104 .samples_per_line
= 1443,
105 .sample_format
= V4L2_PIX_FMT_GREY
,
117 * This is part of Video 4 Linux API. These procedures handle ioctl() calls.
119 static int pvr2_querycap(struct file
*file
, void *priv
, struct v4l2_capability
*cap
)
121 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
122 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
124 strlcpy(cap
->driver
, "pvrusb2", sizeof(cap
->driver
));
125 strlcpy(cap
->bus_info
, pvr2_hdw_get_bus_info(hdw
),
126 sizeof(cap
->bus_info
));
127 strlcpy(cap
->card
, pvr2_hdw_get_desc(hdw
), sizeof(cap
->card
));
128 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_TUNER
|
129 V4L2_CAP_AUDIO
| V4L2_CAP_RADIO
|
130 V4L2_CAP_READWRITE
| V4L2_CAP_DEVICE_CAPS
;
131 switch (fh
->pdi
->devbase
.vfl_type
) {
132 case VFL_TYPE_GRABBER
:
133 cap
->device_caps
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_AUDIO
;
136 cap
->device_caps
= V4L2_CAP_RADIO
;
141 cap
->device_caps
|= V4L2_CAP_TUNER
| V4L2_CAP_READWRITE
;
145 static int pvr2_g_std(struct file
*file
, void *priv
, v4l2_std_id
*std
)
147 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
148 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
152 ret
= pvr2_ctrl_get_value(
153 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_STDCUR
), &val
);
158 static int pvr2_s_std(struct file
*file
, void *priv
, v4l2_std_id std
)
160 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
161 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
163 return pvr2_ctrl_set_value(
164 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_STDCUR
), std
);
167 static int pvr2_querystd(struct file
*file
, void *priv
, v4l2_std_id
*std
)
169 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
170 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
174 ret
= pvr2_ctrl_get_value(
175 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_STDDETECT
), &val
);
180 static int pvr2_enum_input(struct file
*file
, void *priv
, struct v4l2_input
*vi
)
182 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
183 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
184 struct pvr2_ctrl
*cptr
;
185 struct v4l2_input tmp
;
189 cptr
= pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_INPUT
);
191 memset(&tmp
, 0, sizeof(tmp
));
192 tmp
.index
= vi
->index
;
193 if (vi
->index
>= fh
->input_cnt
)
195 val
= fh
->input_map
[vi
->index
];
197 case PVR2_CVAL_INPUT_TV
:
198 case PVR2_CVAL_INPUT_DTV
:
199 case PVR2_CVAL_INPUT_RADIO
:
200 tmp
.type
= V4L2_INPUT_TYPE_TUNER
;
202 case PVR2_CVAL_INPUT_SVIDEO
:
203 case PVR2_CVAL_INPUT_COMPOSITE
:
204 tmp
.type
= V4L2_INPUT_TYPE_CAMERA
;
211 pvr2_ctrl_get_valname(cptr
, val
,
212 tmp
.name
, sizeof(tmp
.name
) - 1, &cnt
);
215 /* Don't bother with audioset, since this driver currently
216 always switches the audio whenever the video is
219 /* Handling std is a tougher problem. It doesn't make
220 sense in cases where a device might be multi-standard.
221 We could just copy out the current value for the
222 standard, but it can change over time. For now just
228 static int pvr2_g_input(struct file
*file
, void *priv
, unsigned int *i
)
230 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
231 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
233 struct pvr2_ctrl
*cptr
;
237 cptr
= pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_INPUT
);
239 ret
= pvr2_ctrl_get_value(cptr
, &val
);
241 for (idx
= 0; idx
< fh
->input_cnt
; idx
++) {
242 if (fh
->input_map
[idx
] == val
) {
250 static int pvr2_s_input(struct file
*file
, void *priv
, unsigned int inp
)
252 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
253 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
255 if (inp
>= fh
->input_cnt
)
257 return pvr2_ctrl_set_value(
258 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_INPUT
),
262 static int pvr2_enumaudio(struct file
*file
, void *priv
, struct v4l2_audio
*vin
)
264 /* pkt: FIXME: We are returning one "fake" input here
265 which could very well be called "whatever_we_like".
266 This is for apps that want to see an audio input
267 just to feel comfortable, as well as to test if
268 it can do stereo or sth. There is actually no guarantee
269 that the actual audio input cannot change behind the app's
270 back, but most applications should not mind that either.
272 Hopefully, mplayer people will work with us on this (this
273 whole mess is to support mplayer pvr://), or Hans will come
274 up with a more standard way to say "we have inputs but we
275 don 't want you to change them independent of video" which
281 strncpy(vin
->name
, "PVRUSB2 Audio", 14);
282 vin
->capability
= V4L2_AUDCAP_STEREO
;
286 static int pvr2_g_audio(struct file
*file
, void *priv
, struct v4l2_audio
*vin
)
288 /* pkt: FIXME: see above comment (VIDIOC_ENUMAUDIO) */
290 strncpy(vin
->name
, "PVRUSB2 Audio", 14);
291 vin
->capability
= V4L2_AUDCAP_STEREO
;
295 static int pvr2_s_audio(struct file
*file
, void *priv
, const struct v4l2_audio
*vout
)
302 static int pvr2_g_tuner(struct file
*file
, void *priv
, struct v4l2_tuner
*vt
)
304 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
305 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
308 return -EINVAL
; /* Only answer for the 1st tuner */
310 pvr2_hdw_execute_tuner_poll(hdw
);
311 return pvr2_hdw_get_tuner_status(hdw
, vt
);
314 static int pvr2_s_tuner(struct file
*file
, void *priv
, const struct v4l2_tuner
*vt
)
316 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
317 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
322 return pvr2_ctrl_set_value(
323 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_AUDIOMODE
),
327 static int pvr2_s_frequency(struct file
*file
, void *priv
, const struct v4l2_frequency
*vf
)
329 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
330 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
332 struct v4l2_tuner vt
;
334 struct pvr2_ctrl
*ctrlp
;
337 ret
= pvr2_hdw_get_tuner_status(hdw
, &vt
);
340 ctrlp
= pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_INPUT
);
341 ret
= pvr2_ctrl_get_value(ctrlp
, &cur_input
);
344 if (vf
->type
== V4L2_TUNER_RADIO
) {
345 if (cur_input
!= PVR2_CVAL_INPUT_RADIO
)
346 pvr2_ctrl_set_value(ctrlp
, PVR2_CVAL_INPUT_RADIO
);
348 if (cur_input
== PVR2_CVAL_INPUT_RADIO
)
349 pvr2_ctrl_set_value(ctrlp
, PVR2_CVAL_INPUT_TV
);
352 if (vt
.capability
& V4L2_TUNER_CAP_LOW
)
356 return pvr2_ctrl_set_value(
357 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_FREQUENCY
),fv
);
360 static int pvr2_g_frequency(struct file
*file
, void *priv
, struct v4l2_frequency
*vf
)
362 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
363 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
366 struct v4l2_tuner vt
;
369 ret
= pvr2_hdw_get_tuner_status(hdw
, &vt
);
372 ret
= pvr2_ctrl_get_value(
373 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_FREQUENCY
),
378 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_INPUT
),
380 if (cur_input
== PVR2_CVAL_INPUT_RADIO
)
381 vf
->type
= V4L2_TUNER_RADIO
;
383 vf
->type
= V4L2_TUNER_ANALOG_TV
;
384 if (vt
.capability
& V4L2_TUNER_CAP_LOW
)
385 val
= (val
* 2) / 125;
392 static int pvr2_enum_fmt_vid_cap(struct file
*file
, void *priv
, struct v4l2_fmtdesc
*fd
)
394 /* Only one format is supported: MPEG. */
398 fd
->pixelformat
= V4L2_PIX_FMT_MPEG
;
402 static int pvr2_g_fmt_vid_cap(struct file
*file
, void *priv
, struct v4l2_format
*vf
)
404 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
405 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
408 memcpy(vf
, &pvr_format
[PVR_FORMAT_PIX
], sizeof(struct v4l2_format
));
411 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_HRES
),
413 vf
->fmt
.pix
.width
= val
;
416 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_VRES
),
418 vf
->fmt
.pix
.height
= val
;
422 static int pvr2_try_fmt_vid_cap(struct file
*file
, void *priv
, struct v4l2_format
*vf
)
424 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
425 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
426 int lmin
, lmax
, ldef
;
427 struct pvr2_ctrl
*hcp
, *vcp
;
428 int h
= vf
->fmt
.pix
.height
;
429 int w
= vf
->fmt
.pix
.width
;
431 hcp
= pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_HRES
);
432 vcp
= pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_VRES
);
434 lmin
= pvr2_ctrl_get_min(hcp
);
435 lmax
= pvr2_ctrl_get_max(hcp
);
436 pvr2_ctrl_get_def(hcp
, &ldef
);
443 lmin
= pvr2_ctrl_get_min(vcp
);
444 lmax
= pvr2_ctrl_get_max(vcp
);
445 pvr2_ctrl_get_def(vcp
, &ldef
);
453 memcpy(vf
, &pvr_format
[PVR_FORMAT_PIX
],
454 sizeof(struct v4l2_format
));
455 vf
->fmt
.pix
.width
= w
;
456 vf
->fmt
.pix
.height
= h
;
460 static int pvr2_s_fmt_vid_cap(struct file
*file
, void *priv
, struct v4l2_format
*vf
)
462 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
463 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
464 struct pvr2_ctrl
*hcp
, *vcp
;
465 int ret
= pvr2_try_fmt_vid_cap(file
, fh
, vf
);
469 hcp
= pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_HRES
);
470 vcp
= pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_VRES
);
471 pvr2_ctrl_set_value(hcp
, vf
->fmt
.pix
.width
);
472 pvr2_ctrl_set_value(vcp
, vf
->fmt
.pix
.height
);
476 static int pvr2_streamon(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
478 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
479 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
480 struct pvr2_v4l2_dev
*pdi
= fh
->pdi
;
483 if (!fh
->pdi
->stream
) {
484 /* No stream defined for this node. This means
485 that we're not currently allowed to stream from
489 ret
= pvr2_hdw_set_stream_type(hdw
, pdi
->config
);
492 return pvr2_hdw_set_streaming(hdw
, !0);
495 static int pvr2_streamoff(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
497 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
498 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
500 if (!fh
->pdi
->stream
) {
501 /* No stream defined for this node. This means
502 that we're not currently allowed to stream from
506 return pvr2_hdw_set_streaming(hdw
, 0);
509 static int pvr2_queryctrl(struct file
*file
, void *priv
,
510 struct v4l2_queryctrl
*vc
)
512 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
513 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
514 struct pvr2_ctrl
*cptr
;
517 if (vc
->id
& V4L2_CTRL_FLAG_NEXT_CTRL
) {
518 cptr
= pvr2_hdw_get_ctrl_nextv4l(
519 hdw
, (vc
->id
& ~V4L2_CTRL_FLAG_NEXT_CTRL
));
521 vc
->id
= pvr2_ctrl_get_v4lid(cptr
);
523 cptr
= pvr2_hdw_get_ctrl_v4l(hdw
, vc
->id
);
526 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
527 "QUERYCTRL id=0x%x not implemented here",
532 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
533 "QUERYCTRL id=0x%x mapping name=%s (%s)",
534 vc
->id
, pvr2_ctrl_get_name(cptr
),
535 pvr2_ctrl_get_desc(cptr
));
536 strlcpy(vc
->name
, pvr2_ctrl_get_desc(cptr
), sizeof(vc
->name
));
537 vc
->flags
= pvr2_ctrl_get_v4lflags(cptr
);
538 pvr2_ctrl_get_def(cptr
, &val
);
539 vc
->default_value
= val
;
540 switch (pvr2_ctrl_get_type(cptr
)) {
542 vc
->type
= V4L2_CTRL_TYPE_MENU
;
544 vc
->maximum
= pvr2_ctrl_get_cnt(cptr
) - 1;
548 vc
->type
= V4L2_CTRL_TYPE_BOOLEAN
;
554 vc
->type
= V4L2_CTRL_TYPE_INTEGER
;
555 vc
->minimum
= pvr2_ctrl_get_min(cptr
);
556 vc
->maximum
= pvr2_ctrl_get_max(cptr
);
560 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
561 "QUERYCTRL id=0x%x name=%s not mappable",
562 vc
->id
, pvr2_ctrl_get_name(cptr
));
568 static int pvr2_querymenu(struct file
*file
, void *priv
, struct v4l2_querymenu
*vm
)
570 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
571 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
572 unsigned int cnt
= 0;
575 ret
= pvr2_ctrl_get_valname(pvr2_hdw_get_ctrl_v4l(hdw
, vm
->id
),
577 vm
->name
, sizeof(vm
->name
) - 1,
583 static int pvr2_g_ctrl(struct file
*file
, void *priv
, struct v4l2_control
*vc
)
585 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
586 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
590 ret
= pvr2_ctrl_get_value(pvr2_hdw_get_ctrl_v4l(hdw
, vc
->id
),
596 static int pvr2_s_ctrl(struct file
*file
, void *priv
, struct v4l2_control
*vc
)
598 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
599 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
601 return pvr2_ctrl_set_value(pvr2_hdw_get_ctrl_v4l(hdw
, vc
->id
),
605 static int pvr2_g_ext_ctrls(struct file
*file
, void *priv
,
606 struct v4l2_ext_controls
*ctls
)
608 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
609 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
610 struct v4l2_ext_control
*ctrl
;
611 struct pvr2_ctrl
*cptr
;
617 for (idx
= 0; idx
< ctls
->count
; idx
++) {
618 ctrl
= ctls
->controls
+ idx
;
619 cptr
= pvr2_hdw_get_ctrl_v4l(hdw
, ctrl
->id
);
621 if (ctls
->which
== V4L2_CTRL_WHICH_DEF_VAL
)
622 pvr2_ctrl_get_def(cptr
, &val
);
624 ret
= pvr2_ctrl_get_value(cptr
, &val
);
629 ctls
->error_idx
= idx
;
632 /* Ensure that if read as a 64 bit value, the user
633 will still get a hopefully sane value */
640 static int pvr2_s_ext_ctrls(struct file
*file
, void *priv
,
641 struct v4l2_ext_controls
*ctls
)
643 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
644 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
645 struct v4l2_ext_control
*ctrl
;
649 /* Default value cannot be changed */
650 if (ctls
->which
== V4L2_CTRL_WHICH_DEF_VAL
)
654 for (idx
= 0; idx
< ctls
->count
; idx
++) {
655 ctrl
= ctls
->controls
+ idx
;
656 ret
= pvr2_ctrl_set_value(
657 pvr2_hdw_get_ctrl_v4l(hdw
, ctrl
->id
),
660 ctls
->error_idx
= idx
;
667 static int pvr2_try_ext_ctrls(struct file
*file
, void *priv
,
668 struct v4l2_ext_controls
*ctls
)
670 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
671 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
672 struct v4l2_ext_control
*ctrl
;
673 struct pvr2_ctrl
*pctl
;
676 /* For the moment just validate that the requested control
678 for (idx
= 0; idx
< ctls
->count
; idx
++) {
679 ctrl
= ctls
->controls
+ idx
;
680 pctl
= pvr2_hdw_get_ctrl_v4l(hdw
, ctrl
->id
);
682 ctls
->error_idx
= idx
;
689 static int pvr2_cropcap(struct file
*file
, void *priv
, struct v4l2_cropcap
*cap
)
691 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
692 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
695 if (cap
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
697 ret
= pvr2_hdw_get_cropcap(hdw
, cap
);
698 cap
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
; /* paranoia */
702 static int pvr2_g_selection(struct file
*file
, void *priv
,
703 struct v4l2_selection
*sel
)
705 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
706 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
707 struct v4l2_cropcap cap
;
711 if (sel
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
714 cap
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
716 switch (sel
->target
) {
717 case V4L2_SEL_TGT_CROP
:
718 ret
= pvr2_ctrl_get_value(
719 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPL
), &val
);
723 ret
= pvr2_ctrl_get_value(
724 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPT
), &val
);
728 ret
= pvr2_ctrl_get_value(
729 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPW
), &val
);
733 ret
= pvr2_ctrl_get_value(
734 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPH
), &val
);
739 case V4L2_SEL_TGT_CROP_DEFAULT
:
740 ret
= pvr2_hdw_get_cropcap(hdw
, &cap
);
741 sel
->r
= cap
.defrect
;
743 case V4L2_SEL_TGT_CROP_BOUNDS
:
744 ret
= pvr2_hdw_get_cropcap(hdw
, &cap
);
753 static int pvr2_s_selection(struct file
*file
, void *priv
,
754 struct v4l2_selection
*sel
)
756 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
757 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
760 if (sel
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
761 sel
->target
!= V4L2_SEL_TGT_CROP
)
763 ret
= pvr2_ctrl_set_value(
764 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPL
),
768 ret
= pvr2_ctrl_set_value(
769 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPT
),
773 ret
= pvr2_ctrl_set_value(
774 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPW
),
778 ret
= pvr2_ctrl_set_value(
779 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPH
),
786 static int pvr2_log_status(struct file
*file
, void *priv
)
788 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
789 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
791 pvr2_hdw_trigger_module_log(hdw
);
795 static const struct v4l2_ioctl_ops pvr2_ioctl_ops
= {
796 .vidioc_querycap
= pvr2_querycap
,
797 .vidioc_s_audio
= pvr2_s_audio
,
798 .vidioc_g_audio
= pvr2_g_audio
,
799 .vidioc_enumaudio
= pvr2_enumaudio
,
800 .vidioc_enum_input
= pvr2_enum_input
,
801 .vidioc_cropcap
= pvr2_cropcap
,
802 .vidioc_s_selection
= pvr2_s_selection
,
803 .vidioc_g_selection
= pvr2_g_selection
,
804 .vidioc_g_input
= pvr2_g_input
,
805 .vidioc_s_input
= pvr2_s_input
,
806 .vidioc_g_frequency
= pvr2_g_frequency
,
807 .vidioc_s_frequency
= pvr2_s_frequency
,
808 .vidioc_s_tuner
= pvr2_s_tuner
,
809 .vidioc_g_tuner
= pvr2_g_tuner
,
810 .vidioc_g_std
= pvr2_g_std
,
811 .vidioc_s_std
= pvr2_s_std
,
812 .vidioc_querystd
= pvr2_querystd
,
813 .vidioc_log_status
= pvr2_log_status
,
814 .vidioc_enum_fmt_vid_cap
= pvr2_enum_fmt_vid_cap
,
815 .vidioc_g_fmt_vid_cap
= pvr2_g_fmt_vid_cap
,
816 .vidioc_s_fmt_vid_cap
= pvr2_s_fmt_vid_cap
,
817 .vidioc_try_fmt_vid_cap
= pvr2_try_fmt_vid_cap
,
818 .vidioc_streamon
= pvr2_streamon
,
819 .vidioc_streamoff
= pvr2_streamoff
,
820 .vidioc_queryctrl
= pvr2_queryctrl
,
821 .vidioc_querymenu
= pvr2_querymenu
,
822 .vidioc_g_ctrl
= pvr2_g_ctrl
,
823 .vidioc_s_ctrl
= pvr2_s_ctrl
,
824 .vidioc_g_ext_ctrls
= pvr2_g_ext_ctrls
,
825 .vidioc_s_ext_ctrls
= pvr2_s_ext_ctrls
,
826 .vidioc_try_ext_ctrls
= pvr2_try_ext_ctrls
,
829 static void pvr2_v4l2_dev_destroy(struct pvr2_v4l2_dev
*dip
)
831 struct pvr2_hdw
*hdw
= dip
->v4lp
->channel
.mc_head
->hdw
;
832 enum pvr2_config cfg
= dip
->config
;
836 /* Construct the unregistration message *before* we actually
837 perform the unregistration step. By doing it this way we don't
838 have to worry about potentially touching deleted resources. */
839 mcnt
= scnprintf(msg
, sizeof(msg
) - 1,
840 "pvrusb2: unregistered device %s [%s]",
841 video_device_node_name(&dip
->devbase
),
842 pvr2_config_get_name(cfg
));
845 pvr2_hdw_v4l_store_minor_number(hdw
,dip
->minor_type
,-1);
851 /* Actual deallocation happens later when all internal references
853 video_unregister_device(&dip
->devbase
);
855 printk(KERN_INFO
"%s\n", msg
);
860 static void pvr2_v4l2_dev_disassociate_parent(struct pvr2_v4l2_dev
*dip
)
863 if (!dip
->devbase
.v4l2_dev
->dev
) return;
864 dip
->devbase
.v4l2_dev
->dev
= NULL
;
865 device_move(&dip
->devbase
.dev
, NULL
, DPM_ORDER_NONE
);
869 static void pvr2_v4l2_destroy_no_lock(struct pvr2_v4l2
*vp
)
872 pvr2_v4l2_dev_destroy(vp
->dev_video
);
873 vp
->dev_video
= NULL
;
876 pvr2_v4l2_dev_destroy(vp
->dev_radio
);
877 vp
->dev_radio
= NULL
;
880 pvr2_trace(PVR2_TRACE_STRUCT
,"Destroying pvr2_v4l2 id=%p",vp
);
881 pvr2_channel_done(&vp
->channel
);
886 static void pvr2_video_device_release(struct video_device
*vdev
)
888 struct pvr2_v4l2_dev
*dev
;
889 dev
= container_of(vdev
,struct pvr2_v4l2_dev
,devbase
);
894 static void pvr2_v4l2_internal_check(struct pvr2_channel
*chp
)
896 struct pvr2_v4l2
*vp
;
897 vp
= container_of(chp
,struct pvr2_v4l2
,channel
);
898 if (!vp
->channel
.mc_head
->disconnect_flag
) return;
899 pvr2_v4l2_dev_disassociate_parent(vp
->dev_video
);
900 pvr2_v4l2_dev_disassociate_parent(vp
->dev_radio
);
901 if (!list_empty(&vp
->dev_video
->devbase
.fh_list
) ||
902 !list_empty(&vp
->dev_radio
->devbase
.fh_list
))
904 pvr2_v4l2_destroy_no_lock(vp
);
908 static long pvr2_v4l2_ioctl(struct file
*file
,
909 unsigned int cmd
, unsigned long arg
)
912 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
913 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
916 if (pvrusb2_debug
& PVR2_TRACE_V4LIOCTL
)
917 v4l_printk_ioctl(pvr2_hdw_get_driver_name(hdw
), cmd
);
919 if (!pvr2_hdw_dev_ok(hdw
)) {
920 pvr2_trace(PVR2_TRACE_ERROR_LEGS
,
921 "ioctl failed - bad or no context");
925 ret
= video_ioctl2(file
, cmd
, arg
);
927 pvr2_hdw_commit_ctl(hdw
);
930 if (pvrusb2_debug
& PVR2_TRACE_V4LIOCTL
) {
931 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
932 "pvr2_v4l2_do_ioctl failure, ret=%ld command was:",
934 v4l_printk_ioctl(pvr2_hdw_get_driver_name(hdw
), cmd
);
937 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
938 "pvr2_v4l2_do_ioctl complete, ret=%ld (0x%lx)",
946 static int pvr2_v4l2_release(struct file
*file
)
948 struct pvr2_v4l2_fh
*fhp
= file
->private_data
;
949 struct pvr2_v4l2
*vp
= fhp
->pdi
->v4lp
;
950 struct pvr2_hdw
*hdw
= fhp
->channel
.mc_head
->hdw
;
952 pvr2_trace(PVR2_TRACE_OPEN_CLOSE
,"pvr2_v4l2_release");
955 struct pvr2_stream
*sp
;
956 pvr2_hdw_set_streaming(hdw
,0);
957 sp
= pvr2_ioread_get_stream(fhp
->rhp
);
958 if (sp
) pvr2_stream_set_callback(sp
,NULL
,NULL
);
959 pvr2_ioread_destroy(fhp
->rhp
);
963 v4l2_fh_del(&fhp
->fh
);
964 v4l2_fh_exit(&fhp
->fh
);
965 file
->private_data
= NULL
;
967 pvr2_channel_done(&fhp
->channel
);
968 pvr2_trace(PVR2_TRACE_STRUCT
,
969 "Destroying pvr_v4l2_fh id=%p",fhp
);
970 if (fhp
->input_map
) {
971 kfree(fhp
->input_map
);
972 fhp
->input_map
= NULL
;
975 if (vp
->channel
.mc_head
->disconnect_flag
&&
976 list_empty(&vp
->dev_video
->devbase
.fh_list
) &&
977 list_empty(&vp
->dev_radio
->devbase
.fh_list
)) {
978 pvr2_v4l2_destroy_no_lock(vp
);
984 static int pvr2_v4l2_open(struct file
*file
)
986 struct pvr2_v4l2_dev
*dip
; /* Our own context pointer */
987 struct pvr2_v4l2_fh
*fhp
;
988 struct pvr2_v4l2
*vp
;
989 struct pvr2_hdw
*hdw
;
990 unsigned int input_mask
= 0;
991 unsigned int input_cnt
,idx
;
994 dip
= container_of(video_devdata(file
),struct pvr2_v4l2_dev
,devbase
);
997 hdw
= vp
->channel
.hdw
;
999 pvr2_trace(PVR2_TRACE_OPEN_CLOSE
,"pvr2_v4l2_open");
1001 if (!pvr2_hdw_dev_ok(hdw
)) {
1002 pvr2_trace(PVR2_TRACE_OPEN_CLOSE
,
1003 "pvr2_v4l2_open: hardware not ready");
1007 fhp
= kzalloc(sizeof(*fhp
),GFP_KERNEL
);
1012 v4l2_fh_init(&fhp
->fh
, &dip
->devbase
);
1013 init_waitqueue_head(&fhp
->wait_data
);
1016 pvr2_trace(PVR2_TRACE_STRUCT
,"Creating pvr_v4l2_fh id=%p",fhp
);
1017 pvr2_channel_init(&fhp
->channel
,vp
->channel
.mc_head
);
1019 if (dip
->v4l_type
== VFL_TYPE_RADIO
) {
1020 /* Opening device as a radio, legal input selection subset
1021 is just the radio. */
1022 input_mask
= (1 << PVR2_CVAL_INPUT_RADIO
);
1024 /* Opening the main V4L device, legal input selection
1025 subset includes all analog inputs. */
1026 input_mask
= ((1 << PVR2_CVAL_INPUT_RADIO
) |
1027 (1 << PVR2_CVAL_INPUT_TV
) |
1028 (1 << PVR2_CVAL_INPUT_COMPOSITE
) |
1029 (1 << PVR2_CVAL_INPUT_SVIDEO
));
1031 ret
= pvr2_channel_limit_inputs(&fhp
->channel
,input_mask
);
1033 pvr2_channel_done(&fhp
->channel
);
1034 pvr2_trace(PVR2_TRACE_STRUCT
,
1035 "Destroying pvr_v4l2_fh id=%p (input mask error)",
1037 v4l2_fh_exit(&fhp
->fh
);
1042 input_mask
&= pvr2_hdw_get_input_available(hdw
);
1044 for (idx
= 0; idx
< (sizeof(input_mask
) << 3); idx
++) {
1045 if (input_mask
& (1 << idx
)) input_cnt
++;
1047 fhp
->input_cnt
= input_cnt
;
1048 fhp
->input_map
= kzalloc(input_cnt
,GFP_KERNEL
);
1049 if (!fhp
->input_map
) {
1050 pvr2_channel_done(&fhp
->channel
);
1051 pvr2_trace(PVR2_TRACE_STRUCT
,
1052 "Destroying pvr_v4l2_fh id=%p (input map failure)",
1054 v4l2_fh_exit(&fhp
->fh
);
1059 for (idx
= 0; idx
< (sizeof(input_mask
) << 3); idx
++) {
1060 if (!(input_mask
& (1 << idx
))) continue;
1061 fhp
->input_map
[input_cnt
++] = idx
;
1065 file
->private_data
= fhp
;
1067 fhp
->fw_mode_flag
= pvr2_hdw_cpufw_get_enabled(hdw
);
1068 v4l2_fh_add(&fhp
->fh
);
1074 static void pvr2_v4l2_notify(struct pvr2_v4l2_fh
*fhp
)
1076 wake_up(&fhp
->wait_data
);
1079 static int pvr2_v4l2_iosetup(struct pvr2_v4l2_fh
*fh
)
1082 struct pvr2_stream
*sp
;
1083 struct pvr2_hdw
*hdw
;
1084 if (fh
->rhp
) return 0;
1086 if (!fh
->pdi
->stream
) {
1087 /* No stream defined for this node. This means that we're
1088 not currently allowed to stream from this node. */
1092 /* First read() attempt. Try to claim the stream and start
1094 if ((ret
= pvr2_channel_claim_stream(&fh
->channel
,
1095 fh
->pdi
->stream
)) != 0) {
1096 /* Someone else must already have it */
1100 fh
->rhp
= pvr2_channel_create_mpeg_stream(fh
->pdi
->stream
);
1102 pvr2_channel_claim_stream(&fh
->channel
,NULL
);
1106 hdw
= fh
->channel
.mc_head
->hdw
;
1107 sp
= fh
->pdi
->stream
->stream
;
1108 pvr2_stream_set_callback(sp
,(pvr2_stream_callback
)pvr2_v4l2_notify
,fh
);
1109 pvr2_hdw_set_stream_type(hdw
,fh
->pdi
->config
);
1110 if ((ret
= pvr2_hdw_set_streaming(hdw
,!0)) < 0) return ret
;
1111 return pvr2_ioread_set_enabled(fh
->rhp
,!0);
1115 static ssize_t
pvr2_v4l2_read(struct file
*file
,
1116 char __user
*buff
, size_t count
, loff_t
*ppos
)
1118 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
1121 if (fh
->fw_mode_flag
) {
1122 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
1126 unsigned int offs
= *ppos
;
1128 tbuf
= kmalloc(PAGE_SIZE
,GFP_KERNEL
);
1129 if (!tbuf
) return -ENOMEM
;
1133 if (c1
> PAGE_SIZE
) c1
= PAGE_SIZE
;
1134 c2
= pvr2_hdw_cpufw_get(hdw
,offs
,tbuf
,c1
);
1140 if (copy_to_user(buff
,tbuf
,c2
)) {
1155 ret
= pvr2_v4l2_iosetup(fh
);
1162 ret
= pvr2_ioread_read(fh
->rhp
,buff
,count
);
1163 if (ret
>= 0) break;
1164 if (ret
!= -EAGAIN
) break;
1165 if (file
->f_flags
& O_NONBLOCK
) break;
1166 /* Doing blocking I/O. Wait here. */
1167 ret
= wait_event_interruptible(
1169 pvr2_ioread_avail(fh
->rhp
) >= 0);
1177 static __poll_t
pvr2_v4l2_poll(struct file
*file
, poll_table
*wait
)
1180 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
1183 if (fh
->fw_mode_flag
) {
1184 mask
|= EPOLLIN
| EPOLLRDNORM
;
1189 ret
= pvr2_v4l2_iosetup(fh
);
1190 if (ret
) return EPOLLERR
;
1193 poll_wait(file
,&fh
->wait_data
,wait
);
1195 if (pvr2_ioread_avail(fh
->rhp
) >= 0) {
1196 mask
|= EPOLLIN
| EPOLLRDNORM
;
1203 static const struct v4l2_file_operations vdev_fops
= {
1204 .owner
= THIS_MODULE
,
1205 .open
= pvr2_v4l2_open
,
1206 .release
= pvr2_v4l2_release
,
1207 .read
= pvr2_v4l2_read
,
1208 .unlocked_ioctl
= pvr2_v4l2_ioctl
,
1209 .poll
= pvr2_v4l2_poll
,
1213 static const struct video_device vdev_template
= {
1218 static void pvr2_v4l2_dev_init(struct pvr2_v4l2_dev
*dip
,
1219 struct pvr2_v4l2
*vp
,
1224 struct pvr2_hdw
*hdw
;
1228 hdw
= vp
->channel
.mc_head
->hdw
;
1229 dip
->v4l_type
= v4l_type
;
1231 case VFL_TYPE_GRABBER
:
1232 dip
->stream
= &vp
->channel
.mc_head
->video_stream
;
1233 dip
->config
= pvr2_config_mpeg
;
1234 dip
->minor_type
= pvr2_v4l_type_video
;
1237 pr_err(KBUILD_MODNAME
1238 ": Failed to set up pvrusb2 v4l video dev due to missing stream instance\n");
1243 dip
->config
= pvr2_config_vbi
;
1244 dip
->minor_type
= pvr2_v4l_type_vbi
;
1247 case VFL_TYPE_RADIO
:
1248 dip
->stream
= &vp
->channel
.mc_head
->video_stream
;
1249 dip
->config
= pvr2_config_mpeg
;
1250 dip
->minor_type
= pvr2_v4l_type_radio
;
1254 /* Bail out (this should be impossible) */
1255 pr_err(KBUILD_MODNAME
": Failed to set up pvrusb2 v4l dev due to unrecognized config\n");
1259 dip
->devbase
= vdev_template
;
1260 dip
->devbase
.release
= pvr2_video_device_release
;
1261 dip
->devbase
.ioctl_ops
= &pvr2_ioctl_ops
;
1264 pvr2_ctrl_get_value(
1265 pvr2_hdw_get_ctrl_by_id(hdw
,
1266 PVR2_CID_STDAVAIL
), &val
);
1267 dip
->devbase
.tvnorms
= (v4l2_std_id
)val
;
1271 unit_number
= pvr2_hdw_get_unit_number(hdw
);
1272 if (nr_ptr
&& (unit_number
>= 0) && (unit_number
< PVR_NUM
)) {
1273 mindevnum
= nr_ptr
[unit_number
];
1275 pvr2_hdw_set_v4l2_dev(hdw
, &dip
->devbase
);
1276 if ((video_register_device(&dip
->devbase
,
1277 dip
->v4l_type
, mindevnum
) < 0) &&
1278 (video_register_device(&dip
->devbase
,
1279 dip
->v4l_type
, -1) < 0)) {
1280 pr_err(KBUILD_MODNAME
1281 ": Failed to register pvrusb2 v4l device\n");
1284 printk(KERN_INFO
"pvrusb2: registered device %s [%s]\n",
1285 video_device_node_name(&dip
->devbase
),
1286 pvr2_config_get_name(dip
->config
));
1288 pvr2_hdw_v4l_store_minor_number(hdw
,
1289 dip
->minor_type
,dip
->devbase
.minor
);
1293 struct pvr2_v4l2
*pvr2_v4l2_create(struct pvr2_context
*mnp
)
1295 struct pvr2_v4l2
*vp
;
1297 vp
= kzalloc(sizeof(*vp
),GFP_KERNEL
);
1299 pvr2_channel_init(&vp
->channel
,mnp
);
1300 pvr2_trace(PVR2_TRACE_STRUCT
,"Creating pvr2_v4l2 id=%p",vp
);
1302 vp
->channel
.check_func
= pvr2_v4l2_internal_check
;
1304 /* register streams */
1305 vp
->dev_video
= kzalloc(sizeof(*vp
->dev_video
),GFP_KERNEL
);
1306 if (!vp
->dev_video
) goto fail
;
1307 pvr2_v4l2_dev_init(vp
->dev_video
,vp
,VFL_TYPE_GRABBER
);
1308 if (pvr2_hdw_get_input_available(vp
->channel
.mc_head
->hdw
) &
1309 (1 << PVR2_CVAL_INPUT_RADIO
)) {
1310 vp
->dev_radio
= kzalloc(sizeof(*vp
->dev_radio
),GFP_KERNEL
);
1311 if (!vp
->dev_radio
) goto fail
;
1312 pvr2_v4l2_dev_init(vp
->dev_radio
,vp
,VFL_TYPE_RADIO
);
1317 pvr2_trace(PVR2_TRACE_STRUCT
,"Failure creating pvr2_v4l2 id=%p",vp
);
1318 pvr2_v4l2_destroy_no_lock(vp
);