1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
5 * Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
8 #include <linux/kernel.h>
9 #include <linux/slab.h>
10 #include "pvrusb2-context.h"
11 #include "pvrusb2-hdw.h"
13 #include "pvrusb2-debug.h"
14 #include "pvrusb2-v4l2.h"
15 #include "pvrusb2-ioread.h"
16 #include <linux/videodev2.h>
17 #include <linux/module.h>
18 #include <media/v4l2-dev.h>
19 #include <media/v4l2-device.h>
20 #include <media/v4l2-fh.h>
21 #include <media/v4l2-common.h>
22 #include <media/v4l2-ioctl.h>
28 struct pvr2_v4l2_dev
{
29 struct video_device devbase
; /* MUST be first! */
30 struct pvr2_v4l2
*v4lp
;
31 struct pvr2_context_stream
*stream
;
32 /* Information about this device: */
33 enum pvr2_config config
; /* Expected stream format */
34 int v4l_type
; /* V4L defined type for this device node */
35 enum pvr2_v4l_type minor_type
; /* pvr2-understood minor device type */
40 struct pvr2_channel channel
;
41 struct pvr2_v4l2_dev
*pdi
;
42 struct pvr2_ioread
*rhp
;
44 wait_queue_head_t wait_data
;
46 /* Map contiguous ordinal value to input id */
47 unsigned char *input_map
;
48 unsigned int input_cnt
;
52 struct pvr2_channel channel
;
54 /* streams - Note that these must be separately, individually,
55 * allocated pointers. This is because the v4l core is going to
56 * manage their deletion - separately, individually... */
57 struct pvr2_v4l2_dev
*dev_video
;
58 struct pvr2_v4l2_dev
*dev_radio
;
61 static int video_nr
[PVR_NUM
] = {[0 ... PVR_NUM
-1] = -1};
62 module_param_array(video_nr
, int, NULL
, 0444);
63 MODULE_PARM_DESC(video_nr
, "Offset for device's video dev minor");
64 static int radio_nr
[PVR_NUM
] = {[0 ... PVR_NUM
-1] = -1};
65 module_param_array(radio_nr
, int, NULL
, 0444);
66 MODULE_PARM_DESC(radio_nr
, "Offset for device's radio dev minor");
67 static int vbi_nr
[PVR_NUM
] = {[0 ... PVR_NUM
-1] = -1};
68 module_param_array(vbi_nr
, int, NULL
, 0444);
69 MODULE_PARM_DESC(vbi_nr
, "Offset for device's vbi dev minor");
71 #define PVR_FORMAT_PIX 0
72 #define PVR_FORMAT_VBI 1
74 static struct v4l2_format pvr_format
[] = {
76 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
81 .pixelformat
= V4L2_PIX_FMT_MPEG
,
82 .field
= V4L2_FIELD_INTERLACED
,
83 /* FIXME : Don't know what to put here... */
84 .sizeimage
= 32 * 1024,
89 .type
= V4L2_BUF_TYPE_VBI_CAPTURE
,
92 .sampling_rate
= 27000000,
94 .samples_per_line
= 1443,
95 .sample_format
= V4L2_PIX_FMT_GREY
,
107 * This is part of Video 4 Linux API. These procedures handle ioctl() calls.
109 static int pvr2_querycap(struct file
*file
, void *priv
, struct v4l2_capability
*cap
)
111 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
112 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
114 strscpy(cap
->driver
, "pvrusb2", sizeof(cap
->driver
));
115 strscpy(cap
->bus_info
, pvr2_hdw_get_bus_info(hdw
),
116 sizeof(cap
->bus_info
));
117 strscpy(cap
->card
, pvr2_hdw_get_desc(hdw
), sizeof(cap
->card
));
118 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_TUNER
|
119 V4L2_CAP_AUDIO
| V4L2_CAP_RADIO
|
120 V4L2_CAP_READWRITE
| V4L2_CAP_DEVICE_CAPS
;
124 static int pvr2_g_std(struct file
*file
, void *priv
, v4l2_std_id
*std
)
126 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
127 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
131 ret
= pvr2_ctrl_get_value(
132 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_STDCUR
), &val
);
137 static int pvr2_s_std(struct file
*file
, void *priv
, v4l2_std_id std
)
139 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
140 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
143 ret
= pvr2_ctrl_set_value(
144 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_STDCUR
), std
);
145 pvr2_hdw_commit_ctl(hdw
);
149 static int pvr2_querystd(struct file
*file
, void *priv
, v4l2_std_id
*std
)
151 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
152 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
156 ret
= pvr2_ctrl_get_value(
157 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_STDDETECT
), &val
);
162 static int pvr2_enum_input(struct file
*file
, void *priv
, struct v4l2_input
*vi
)
164 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
165 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
166 struct pvr2_ctrl
*cptr
;
167 struct v4l2_input tmp
;
171 cptr
= pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_INPUT
);
173 memset(&tmp
, 0, sizeof(tmp
));
174 tmp
.index
= vi
->index
;
175 if (vi
->index
>= fh
->input_cnt
)
177 val
= fh
->input_map
[vi
->index
];
179 case PVR2_CVAL_INPUT_TV
:
180 case PVR2_CVAL_INPUT_DTV
:
181 case PVR2_CVAL_INPUT_RADIO
:
182 tmp
.type
= V4L2_INPUT_TYPE_TUNER
;
184 case PVR2_CVAL_INPUT_SVIDEO
:
185 case PVR2_CVAL_INPUT_COMPOSITE
:
186 tmp
.type
= V4L2_INPUT_TYPE_CAMERA
;
193 pvr2_ctrl_get_valname(cptr
, val
,
194 tmp
.name
, sizeof(tmp
.name
) - 1, &cnt
);
197 /* Don't bother with audioset, since this driver currently
198 always switches the audio whenever the video is
201 /* Handling std is a tougher problem. It doesn't make
202 sense in cases where a device might be multi-standard.
203 We could just copy out the current value for the
204 standard, but it can change over time. For now just
210 static int pvr2_g_input(struct file
*file
, void *priv
, unsigned int *i
)
212 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
213 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
215 struct pvr2_ctrl
*cptr
;
219 cptr
= pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_INPUT
);
221 ret
= pvr2_ctrl_get_value(cptr
, &val
);
223 for (idx
= 0; idx
< fh
->input_cnt
; idx
++) {
224 if (fh
->input_map
[idx
] == val
) {
232 static int pvr2_s_input(struct file
*file
, void *priv
, unsigned int inp
)
234 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
235 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
238 if (inp
>= fh
->input_cnt
)
240 ret
= pvr2_ctrl_set_value(
241 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_INPUT
),
243 pvr2_hdw_commit_ctl(hdw
);
247 static int pvr2_enumaudio(struct file
*file
, void *priv
, struct v4l2_audio
*vin
)
249 /* pkt: FIXME: We are returning one "fake" input here
250 which could very well be called "whatever_we_like".
251 This is for apps that want to see an audio input
252 just to feel comfortable, as well as to test if
253 it can do stereo or sth. There is actually no guarantee
254 that the actual audio input cannot change behind the app's
255 back, but most applications should not mind that either.
257 Hopefully, mplayer people will work with us on this (this
258 whole mess is to support mplayer pvr://), or Hans will come
259 up with a more standard way to say "we have inputs but we
260 don 't want you to change them independent of video" which
266 strscpy(vin
->name
, "PVRUSB2 Audio", sizeof(vin
->name
));
267 vin
->capability
= V4L2_AUDCAP_STEREO
;
271 static int pvr2_g_audio(struct file
*file
, void *priv
, struct v4l2_audio
*vin
)
273 /* pkt: FIXME: see above comment (VIDIOC_ENUMAUDIO) */
275 strscpy(vin
->name
, "PVRUSB2 Audio", sizeof(vin
->name
));
276 vin
->capability
= V4L2_AUDCAP_STEREO
;
280 static int pvr2_s_audio(struct file
*file
, void *priv
, const struct v4l2_audio
*vout
)
287 static int pvr2_g_tuner(struct file
*file
, void *priv
, struct v4l2_tuner
*vt
)
289 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
290 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
293 return -EINVAL
; /* Only answer for the 1st tuner */
295 pvr2_hdw_execute_tuner_poll(hdw
);
296 return pvr2_hdw_get_tuner_status(hdw
, vt
);
299 static int pvr2_s_tuner(struct file
*file
, void *priv
, const struct v4l2_tuner
*vt
)
301 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
302 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
308 ret
= pvr2_ctrl_set_value(
309 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_AUDIOMODE
),
311 pvr2_hdw_commit_ctl(hdw
);
315 static int pvr2_s_frequency(struct file
*file
, void *priv
, const struct v4l2_frequency
*vf
)
317 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
318 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
320 struct v4l2_tuner vt
;
322 struct pvr2_ctrl
*ctrlp
;
325 ret
= pvr2_hdw_get_tuner_status(hdw
, &vt
);
328 ctrlp
= pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_INPUT
);
329 ret
= pvr2_ctrl_get_value(ctrlp
, &cur_input
);
332 if (vf
->type
== V4L2_TUNER_RADIO
) {
333 if (cur_input
!= PVR2_CVAL_INPUT_RADIO
)
334 pvr2_ctrl_set_value(ctrlp
, PVR2_CVAL_INPUT_RADIO
);
336 if (cur_input
== PVR2_CVAL_INPUT_RADIO
)
337 pvr2_ctrl_set_value(ctrlp
, PVR2_CVAL_INPUT_TV
);
340 if (vt
.capability
& V4L2_TUNER_CAP_LOW
)
344 ret
= pvr2_ctrl_set_value(
345 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_FREQUENCY
),fv
);
346 pvr2_hdw_commit_ctl(hdw
);
350 static int pvr2_g_frequency(struct file
*file
, void *priv
, struct v4l2_frequency
*vf
)
352 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
353 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
356 struct v4l2_tuner vt
;
359 ret
= pvr2_hdw_get_tuner_status(hdw
, &vt
);
362 ret
= pvr2_ctrl_get_value(
363 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_FREQUENCY
),
368 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_INPUT
),
370 if (cur_input
== PVR2_CVAL_INPUT_RADIO
)
371 vf
->type
= V4L2_TUNER_RADIO
;
373 vf
->type
= V4L2_TUNER_ANALOG_TV
;
374 if (vt
.capability
& V4L2_TUNER_CAP_LOW
)
375 val
= (val
* 2) / 125;
382 static int pvr2_enum_fmt_vid_cap(struct file
*file
, void *priv
, struct v4l2_fmtdesc
*fd
)
384 /* Only one format is supported: MPEG. */
388 fd
->pixelformat
= V4L2_PIX_FMT_MPEG
;
392 static int pvr2_g_fmt_vid_cap(struct file
*file
, void *priv
, struct v4l2_format
*vf
)
394 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
395 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
398 memcpy(vf
, &pvr_format
[PVR_FORMAT_PIX
], sizeof(struct v4l2_format
));
401 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_HRES
),
403 vf
->fmt
.pix
.width
= val
;
406 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_VRES
),
408 vf
->fmt
.pix
.height
= val
;
412 static int pvr2_try_fmt_vid_cap(struct file
*file
, void *priv
, struct v4l2_format
*vf
)
414 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
415 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
416 int lmin
, lmax
, ldef
;
417 struct pvr2_ctrl
*hcp
, *vcp
;
418 int h
= vf
->fmt
.pix
.height
;
419 int w
= vf
->fmt
.pix
.width
;
421 hcp
= pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_HRES
);
422 vcp
= pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_VRES
);
424 lmin
= pvr2_ctrl_get_min(hcp
);
425 lmax
= pvr2_ctrl_get_max(hcp
);
426 pvr2_ctrl_get_def(hcp
, &ldef
);
433 lmin
= pvr2_ctrl_get_min(vcp
);
434 lmax
= pvr2_ctrl_get_max(vcp
);
435 pvr2_ctrl_get_def(vcp
, &ldef
);
443 memcpy(vf
, &pvr_format
[PVR_FORMAT_PIX
],
444 sizeof(struct v4l2_format
));
445 vf
->fmt
.pix
.width
= w
;
446 vf
->fmt
.pix
.height
= h
;
450 static int pvr2_s_fmt_vid_cap(struct file
*file
, void *priv
, struct v4l2_format
*vf
)
452 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
453 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
454 struct pvr2_ctrl
*hcp
, *vcp
;
455 int ret
= pvr2_try_fmt_vid_cap(file
, fh
, vf
);
459 hcp
= pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_HRES
);
460 vcp
= pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_VRES
);
461 pvr2_ctrl_set_value(hcp
, vf
->fmt
.pix
.width
);
462 pvr2_ctrl_set_value(vcp
, vf
->fmt
.pix
.height
);
463 pvr2_hdw_commit_ctl(hdw
);
467 static int pvr2_streamon(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
469 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
470 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
471 struct pvr2_v4l2_dev
*pdi
= fh
->pdi
;
474 if (!fh
->pdi
->stream
) {
475 /* No stream defined for this node. This means
476 that we're not currently allowed to stream from
480 ret
= pvr2_hdw_set_stream_type(hdw
, pdi
->config
);
483 return pvr2_hdw_set_streaming(hdw
, !0);
486 static int pvr2_streamoff(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
488 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
489 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
491 if (!fh
->pdi
->stream
) {
492 /* No stream defined for this node. This means
493 that we're not currently allowed to stream from
497 return pvr2_hdw_set_streaming(hdw
, 0);
500 static int pvr2_queryctrl(struct file
*file
, void *priv
,
501 struct v4l2_queryctrl
*vc
)
503 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
504 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
505 struct pvr2_ctrl
*cptr
;
508 if (vc
->id
& V4L2_CTRL_FLAG_NEXT_CTRL
) {
509 cptr
= pvr2_hdw_get_ctrl_nextv4l(
510 hdw
, (vc
->id
& ~V4L2_CTRL_FLAG_NEXT_CTRL
));
512 vc
->id
= pvr2_ctrl_get_v4lid(cptr
);
514 cptr
= pvr2_hdw_get_ctrl_v4l(hdw
, vc
->id
);
517 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
518 "QUERYCTRL id=0x%x not implemented here",
523 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
524 "QUERYCTRL id=0x%x mapping name=%s (%s)",
525 vc
->id
, pvr2_ctrl_get_name(cptr
),
526 pvr2_ctrl_get_desc(cptr
));
527 strscpy(vc
->name
, pvr2_ctrl_get_desc(cptr
), sizeof(vc
->name
));
528 vc
->flags
= pvr2_ctrl_get_v4lflags(cptr
);
529 pvr2_ctrl_get_def(cptr
, &val
);
530 vc
->default_value
= val
;
531 switch (pvr2_ctrl_get_type(cptr
)) {
533 vc
->type
= V4L2_CTRL_TYPE_MENU
;
535 vc
->maximum
= pvr2_ctrl_get_cnt(cptr
) - 1;
539 vc
->type
= V4L2_CTRL_TYPE_BOOLEAN
;
545 vc
->type
= V4L2_CTRL_TYPE_INTEGER
;
546 vc
->minimum
= pvr2_ctrl_get_min(cptr
);
547 vc
->maximum
= pvr2_ctrl_get_max(cptr
);
551 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
552 "QUERYCTRL id=0x%x name=%s not mappable",
553 vc
->id
, pvr2_ctrl_get_name(cptr
));
559 static int pvr2_querymenu(struct file
*file
, void *priv
, struct v4l2_querymenu
*vm
)
561 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
562 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
563 unsigned int cnt
= 0;
566 ret
= pvr2_ctrl_get_valname(pvr2_hdw_get_ctrl_v4l(hdw
, vm
->id
),
568 vm
->name
, sizeof(vm
->name
) - 1,
574 static int pvr2_g_ctrl(struct file
*file
, void *priv
, struct v4l2_control
*vc
)
576 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
577 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
581 ret
= pvr2_ctrl_get_value(pvr2_hdw_get_ctrl_v4l(hdw
, vc
->id
),
587 static int pvr2_s_ctrl(struct file
*file
, void *priv
, struct v4l2_control
*vc
)
589 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
590 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
593 ret
= pvr2_ctrl_set_value(pvr2_hdw_get_ctrl_v4l(hdw
, vc
->id
),
595 pvr2_hdw_commit_ctl(hdw
);
599 static int pvr2_g_ext_ctrls(struct file
*file
, void *priv
,
600 struct v4l2_ext_controls
*ctls
)
602 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
603 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
604 struct v4l2_ext_control
*ctrl
;
605 struct pvr2_ctrl
*cptr
;
611 for (idx
= 0; idx
< ctls
->count
; idx
++) {
612 ctrl
= ctls
->controls
+ idx
;
613 cptr
= pvr2_hdw_get_ctrl_v4l(hdw
, ctrl
->id
);
615 if (ctls
->which
== V4L2_CTRL_WHICH_DEF_VAL
)
616 pvr2_ctrl_get_def(cptr
, &val
);
618 ret
= pvr2_ctrl_get_value(cptr
, &val
);
623 ctls
->error_idx
= idx
;
626 /* Ensure that if read as a 64 bit value, the user
627 will still get a hopefully sane value */
634 static int pvr2_s_ext_ctrls(struct file
*file
, void *priv
,
635 struct v4l2_ext_controls
*ctls
)
637 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
638 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
639 struct v4l2_ext_control
*ctrl
;
643 /* Default value cannot be changed */
644 if (ctls
->which
== V4L2_CTRL_WHICH_DEF_VAL
)
648 for (idx
= 0; idx
< ctls
->count
; idx
++) {
649 ctrl
= ctls
->controls
+ idx
;
650 ret
= pvr2_ctrl_set_value(
651 pvr2_hdw_get_ctrl_v4l(hdw
, ctrl
->id
),
654 ctls
->error_idx
= idx
;
659 pvr2_hdw_commit_ctl(hdw
);
663 static int pvr2_try_ext_ctrls(struct file
*file
, void *priv
,
664 struct v4l2_ext_controls
*ctls
)
666 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
667 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
668 struct v4l2_ext_control
*ctrl
;
669 struct pvr2_ctrl
*pctl
;
672 /* For the moment just validate that the requested control
674 for (idx
= 0; idx
< ctls
->count
; idx
++) {
675 ctrl
= ctls
->controls
+ idx
;
676 pctl
= pvr2_hdw_get_ctrl_v4l(hdw
, ctrl
->id
);
678 ctls
->error_idx
= idx
;
685 static int pvr2_g_pixelaspect(struct file
*file
, void *priv
,
686 int type
, struct v4l2_fract
*f
)
688 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
689 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
690 struct v4l2_cropcap cap
= { .type
= type
};
693 if (type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
695 ret
= pvr2_hdw_get_cropcap(hdw
, &cap
);
697 *f
= cap
.pixelaspect
;
701 static int pvr2_g_selection(struct file
*file
, void *priv
,
702 struct v4l2_selection
*sel
)
704 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
705 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
706 struct v4l2_cropcap cap
;
710 if (sel
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
713 cap
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
715 switch (sel
->target
) {
716 case V4L2_SEL_TGT_CROP
:
717 ret
= pvr2_ctrl_get_value(
718 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPL
), &val
);
722 ret
= pvr2_ctrl_get_value(
723 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPT
), &val
);
727 ret
= pvr2_ctrl_get_value(
728 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPW
), &val
);
732 ret
= pvr2_ctrl_get_value(
733 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPH
), &val
);
738 case V4L2_SEL_TGT_CROP_DEFAULT
:
739 ret
= pvr2_hdw_get_cropcap(hdw
, &cap
);
740 sel
->r
= cap
.defrect
;
742 case V4L2_SEL_TGT_CROP_BOUNDS
:
743 ret
= pvr2_hdw_get_cropcap(hdw
, &cap
);
752 static int pvr2_s_selection(struct file
*file
, void *priv
,
753 struct v4l2_selection
*sel
)
755 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
756 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
759 if (sel
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
760 sel
->target
!= V4L2_SEL_TGT_CROP
)
762 ret
= pvr2_ctrl_set_value(
763 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPL
),
767 ret
= pvr2_ctrl_set_value(
768 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPT
),
772 ret
= pvr2_ctrl_set_value(
773 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPW
),
777 ret
= pvr2_ctrl_set_value(
778 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPH
),
781 pvr2_hdw_commit_ctl(hdw
);
785 static int pvr2_log_status(struct file
*file
, void *priv
)
787 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
788 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
790 pvr2_hdw_trigger_module_log(hdw
);
794 static const struct v4l2_ioctl_ops pvr2_ioctl_ops
= {
795 .vidioc_querycap
= pvr2_querycap
,
796 .vidioc_s_audio
= pvr2_s_audio
,
797 .vidioc_g_audio
= pvr2_g_audio
,
798 .vidioc_enumaudio
= pvr2_enumaudio
,
799 .vidioc_enum_input
= pvr2_enum_input
,
800 .vidioc_g_pixelaspect
= pvr2_g_pixelaspect
,
801 .vidioc_s_selection
= pvr2_s_selection
,
802 .vidioc_g_selection
= pvr2_g_selection
,
803 .vidioc_g_input
= pvr2_g_input
,
804 .vidioc_s_input
= pvr2_s_input
,
805 .vidioc_g_frequency
= pvr2_g_frequency
,
806 .vidioc_s_frequency
= pvr2_s_frequency
,
807 .vidioc_s_tuner
= pvr2_s_tuner
,
808 .vidioc_g_tuner
= pvr2_g_tuner
,
809 .vidioc_g_std
= pvr2_g_std
,
810 .vidioc_s_std
= pvr2_s_std
,
811 .vidioc_querystd
= pvr2_querystd
,
812 .vidioc_log_status
= pvr2_log_status
,
813 .vidioc_enum_fmt_vid_cap
= pvr2_enum_fmt_vid_cap
,
814 .vidioc_g_fmt_vid_cap
= pvr2_g_fmt_vid_cap
,
815 .vidioc_s_fmt_vid_cap
= pvr2_s_fmt_vid_cap
,
816 .vidioc_try_fmt_vid_cap
= pvr2_try_fmt_vid_cap
,
817 .vidioc_streamon
= pvr2_streamon
,
818 .vidioc_streamoff
= pvr2_streamoff
,
819 .vidioc_queryctrl
= pvr2_queryctrl
,
820 .vidioc_querymenu
= pvr2_querymenu
,
821 .vidioc_g_ctrl
= pvr2_g_ctrl
,
822 .vidioc_s_ctrl
= pvr2_s_ctrl
,
823 .vidioc_g_ext_ctrls
= pvr2_g_ext_ctrls
,
824 .vidioc_s_ext_ctrls
= pvr2_s_ext_ctrls
,
825 .vidioc_try_ext_ctrls
= pvr2_try_ext_ctrls
,
828 static void pvr2_v4l2_dev_destroy(struct pvr2_v4l2_dev
*dip
)
830 struct pvr2_hdw
*hdw
= dip
->v4lp
->channel
.mc_head
->hdw
;
831 enum pvr2_config cfg
= dip
->config
;
835 /* Construct the unregistration message *before* we actually
836 perform the unregistration step. By doing it this way we don't
837 have to worry about potentially touching deleted resources. */
838 mcnt
= scnprintf(msg
, sizeof(msg
) - 1,
839 "pvrusb2: unregistered device %s [%s]",
840 video_device_node_name(&dip
->devbase
),
841 pvr2_config_get_name(cfg
));
844 pvr2_hdw_v4l_store_minor_number(hdw
,dip
->minor_type
,-1);
850 /* Actual deallocation happens later when all internal references
852 video_unregister_device(&dip
->devbase
);
854 pr_info("%s\n", msg
);
859 static void pvr2_v4l2_dev_disassociate_parent(struct pvr2_v4l2_dev
*dip
)
862 if (!dip
->devbase
.v4l2_dev
->dev
) return;
863 dip
->devbase
.v4l2_dev
->dev
= NULL
;
864 device_move(&dip
->devbase
.dev
, NULL
, DPM_ORDER_NONE
);
868 static void pvr2_v4l2_destroy_no_lock(struct pvr2_v4l2
*vp
)
871 pvr2_v4l2_dev_destroy(vp
->dev_video
);
872 vp
->dev_video
= NULL
;
875 pvr2_v4l2_dev_destroy(vp
->dev_radio
);
876 vp
->dev_radio
= NULL
;
879 pvr2_trace(PVR2_TRACE_STRUCT
,"Destroying pvr2_v4l2 id=%p",vp
);
880 pvr2_channel_done(&vp
->channel
);
885 static void pvr2_video_device_release(struct video_device
*vdev
)
887 struct pvr2_v4l2_dev
*dev
;
888 dev
= container_of(vdev
,struct pvr2_v4l2_dev
,devbase
);
893 static void pvr2_v4l2_internal_check(struct pvr2_channel
*chp
)
895 struct pvr2_v4l2
*vp
;
896 vp
= container_of(chp
,struct pvr2_v4l2
,channel
);
897 if (!vp
->channel
.mc_head
->disconnect_flag
) return;
898 pvr2_v4l2_dev_disassociate_parent(vp
->dev_video
);
899 pvr2_v4l2_dev_disassociate_parent(vp
->dev_radio
);
900 if (!list_empty(&vp
->dev_video
->devbase
.fh_list
) ||
902 !list_empty(&vp
->dev_radio
->devbase
.fh_list
))) {
903 pvr2_trace(PVR2_TRACE_STRUCT
,
904 "pvr2_v4l2 internal_check exit-empty id=%p", vp
);
907 pvr2_v4l2_destroy_no_lock(vp
);
911 static int pvr2_v4l2_release(struct file
*file
)
913 struct pvr2_v4l2_fh
*fhp
= file
->private_data
;
914 struct pvr2_v4l2
*vp
= fhp
->pdi
->v4lp
;
915 struct pvr2_hdw
*hdw
= fhp
->channel
.mc_head
->hdw
;
917 pvr2_trace(PVR2_TRACE_OPEN_CLOSE
,"pvr2_v4l2_release");
920 struct pvr2_stream
*sp
;
921 pvr2_hdw_set_streaming(hdw
,0);
922 sp
= pvr2_ioread_get_stream(fhp
->rhp
);
923 if (sp
) pvr2_stream_set_callback(sp
,NULL
,NULL
);
924 pvr2_ioread_destroy(fhp
->rhp
);
928 v4l2_fh_del(&fhp
->fh
);
929 v4l2_fh_exit(&fhp
->fh
);
930 file
->private_data
= NULL
;
932 pvr2_channel_done(&fhp
->channel
);
933 pvr2_trace(PVR2_TRACE_STRUCT
,
934 "Destroying pvr_v4l2_fh id=%p",fhp
);
935 if (fhp
->input_map
) {
936 kfree(fhp
->input_map
);
937 fhp
->input_map
= NULL
;
940 if (vp
->channel
.mc_head
->disconnect_flag
&&
941 list_empty(&vp
->dev_video
->devbase
.fh_list
) &&
943 list_empty(&vp
->dev_radio
->devbase
.fh_list
))) {
944 pvr2_v4l2_destroy_no_lock(vp
);
950 static int pvr2_v4l2_open(struct file
*file
)
952 struct pvr2_v4l2_dev
*dip
; /* Our own context pointer */
953 struct pvr2_v4l2_fh
*fhp
;
954 struct pvr2_v4l2
*vp
;
955 struct pvr2_hdw
*hdw
;
956 unsigned int input_mask
= 0;
957 unsigned int input_cnt
,idx
;
960 dip
= container_of(video_devdata(file
),struct pvr2_v4l2_dev
,devbase
);
963 hdw
= vp
->channel
.hdw
;
965 pvr2_trace(PVR2_TRACE_OPEN_CLOSE
,"pvr2_v4l2_open");
967 if (!pvr2_hdw_dev_ok(hdw
)) {
968 pvr2_trace(PVR2_TRACE_OPEN_CLOSE
,
969 "pvr2_v4l2_open: hardware not ready");
973 fhp
= kzalloc(sizeof(*fhp
),GFP_KERNEL
);
978 v4l2_fh_init(&fhp
->fh
, &dip
->devbase
);
979 init_waitqueue_head(&fhp
->wait_data
);
982 pvr2_trace(PVR2_TRACE_STRUCT
,"Creating pvr_v4l2_fh id=%p",fhp
);
983 pvr2_channel_init(&fhp
->channel
,vp
->channel
.mc_head
);
985 if (dip
->v4l_type
== VFL_TYPE_RADIO
) {
986 /* Opening device as a radio, legal input selection subset
987 is just the radio. */
988 input_mask
= (1 << PVR2_CVAL_INPUT_RADIO
);
990 /* Opening the main V4L device, legal input selection
991 subset includes all analog inputs. */
992 input_mask
= ((1 << PVR2_CVAL_INPUT_RADIO
) |
993 (1 << PVR2_CVAL_INPUT_TV
) |
994 (1 << PVR2_CVAL_INPUT_COMPOSITE
) |
995 (1 << PVR2_CVAL_INPUT_SVIDEO
));
997 ret
= pvr2_channel_limit_inputs(&fhp
->channel
,input_mask
);
999 pvr2_channel_done(&fhp
->channel
);
1000 pvr2_trace(PVR2_TRACE_STRUCT
,
1001 "Destroying pvr_v4l2_fh id=%p (input mask error)",
1003 v4l2_fh_exit(&fhp
->fh
);
1008 input_mask
&= pvr2_hdw_get_input_available(hdw
);
1010 for (idx
= 0; idx
< (sizeof(input_mask
) << 3); idx
++) {
1011 if (input_mask
& (1UL << idx
)) input_cnt
++;
1013 fhp
->input_cnt
= input_cnt
;
1014 fhp
->input_map
= kzalloc(input_cnt
,GFP_KERNEL
);
1015 if (!fhp
->input_map
) {
1016 pvr2_channel_done(&fhp
->channel
);
1017 pvr2_trace(PVR2_TRACE_STRUCT
,
1018 "Destroying pvr_v4l2_fh id=%p (input map failure)",
1020 v4l2_fh_exit(&fhp
->fh
);
1025 for (idx
= 0; idx
< (sizeof(input_mask
) << 3); idx
++) {
1026 if (!(input_mask
& (1UL << idx
))) continue;
1027 fhp
->input_map
[input_cnt
++] = idx
;
1031 file
->private_data
= fhp
;
1033 fhp
->fw_mode_flag
= pvr2_hdw_cpufw_get_enabled(hdw
);
1034 v4l2_fh_add(&fhp
->fh
);
1040 static void pvr2_v4l2_notify(struct pvr2_v4l2_fh
*fhp
)
1042 wake_up(&fhp
->wait_data
);
1045 static int pvr2_v4l2_iosetup(struct pvr2_v4l2_fh
*fh
)
1048 struct pvr2_stream
*sp
;
1049 struct pvr2_hdw
*hdw
;
1050 if (fh
->rhp
) return 0;
1052 if (!fh
->pdi
->stream
) {
1053 /* No stream defined for this node. This means that we're
1054 not currently allowed to stream from this node. */
1058 /* First read() attempt. Try to claim the stream and start
1060 if ((ret
= pvr2_channel_claim_stream(&fh
->channel
,
1061 fh
->pdi
->stream
)) != 0) {
1062 /* Someone else must already have it */
1066 fh
->rhp
= pvr2_channel_create_mpeg_stream(fh
->pdi
->stream
);
1068 pvr2_channel_claim_stream(&fh
->channel
,NULL
);
1072 hdw
= fh
->channel
.mc_head
->hdw
;
1073 sp
= fh
->pdi
->stream
->stream
;
1074 pvr2_stream_set_callback(sp
,(pvr2_stream_callback
)pvr2_v4l2_notify
,fh
);
1075 pvr2_hdw_set_stream_type(hdw
,fh
->pdi
->config
);
1076 if ((ret
= pvr2_hdw_set_streaming(hdw
,!0)) < 0) return ret
;
1077 return pvr2_ioread_set_enabled(fh
->rhp
,!0);
1081 static ssize_t
pvr2_v4l2_read(struct file
*file
,
1082 char __user
*buff
, size_t count
, loff_t
*ppos
)
1084 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
1087 if (fh
->fw_mode_flag
) {
1088 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
1092 unsigned int offs
= *ppos
;
1094 tbuf
= kmalloc(PAGE_SIZE
,GFP_KERNEL
);
1095 if (!tbuf
) return -ENOMEM
;
1099 if (c1
> PAGE_SIZE
) c1
= PAGE_SIZE
;
1100 c2
= pvr2_hdw_cpufw_get(hdw
,offs
,tbuf
,c1
);
1106 if (copy_to_user(buff
,tbuf
,c2
)) {
1121 ret
= pvr2_v4l2_iosetup(fh
);
1128 ret
= pvr2_ioread_read(fh
->rhp
,buff
,count
);
1129 if (ret
>= 0) break;
1130 if (ret
!= -EAGAIN
) break;
1131 if (file
->f_flags
& O_NONBLOCK
) break;
1132 /* Doing blocking I/O. Wait here. */
1133 ret
= wait_event_interruptible(
1135 pvr2_ioread_avail(fh
->rhp
) >= 0);
1143 static __poll_t
pvr2_v4l2_poll(struct file
*file
, poll_table
*wait
)
1146 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
1149 if (fh
->fw_mode_flag
) {
1150 mask
|= EPOLLIN
| EPOLLRDNORM
;
1155 ret
= pvr2_v4l2_iosetup(fh
);
1156 if (ret
) return EPOLLERR
;
1159 poll_wait(file
,&fh
->wait_data
,wait
);
1161 if (pvr2_ioread_avail(fh
->rhp
) >= 0) {
1162 mask
|= EPOLLIN
| EPOLLRDNORM
;
1169 static const struct v4l2_file_operations vdev_fops
= {
1170 .owner
= THIS_MODULE
,
1171 .open
= pvr2_v4l2_open
,
1172 .release
= pvr2_v4l2_release
,
1173 .read
= pvr2_v4l2_read
,
1174 .unlocked_ioctl
= video_ioctl2
,
1175 .poll
= pvr2_v4l2_poll
,
1179 static const struct video_device vdev_template
= {
1184 static void pvr2_v4l2_dev_init(struct pvr2_v4l2_dev
*dip
,
1185 struct pvr2_v4l2
*vp
,
1190 struct pvr2_hdw
*hdw
;
1192 u32 caps
= V4L2_CAP_TUNER
| V4L2_CAP_READWRITE
;
1196 hdw
= vp
->channel
.mc_head
->hdw
;
1197 dip
->v4l_type
= v4l_type
;
1199 case VFL_TYPE_GRABBER
:
1200 dip
->stream
= &vp
->channel
.mc_head
->video_stream
;
1201 dip
->config
= pvr2_config_mpeg
;
1202 dip
->minor_type
= pvr2_v4l_type_video
;
1204 caps
|= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_AUDIO
;
1206 pr_err(KBUILD_MODNAME
1207 ": Failed to set up pvrusb2 v4l video dev due to missing stream instance\n");
1212 dip
->config
= pvr2_config_vbi
;
1213 dip
->minor_type
= pvr2_v4l_type_vbi
;
1215 caps
|= V4L2_CAP_VBI_CAPTURE
;
1217 case VFL_TYPE_RADIO
:
1218 dip
->stream
= &vp
->channel
.mc_head
->video_stream
;
1219 dip
->config
= pvr2_config_mpeg
;
1220 dip
->minor_type
= pvr2_v4l_type_radio
;
1222 caps
|= V4L2_CAP_RADIO
;
1225 /* Bail out (this should be impossible) */
1226 pr_err(KBUILD_MODNAME
": Failed to set up pvrusb2 v4l dev due to unrecognized config\n");
1230 dip
->devbase
= vdev_template
;
1231 dip
->devbase
.release
= pvr2_video_device_release
;
1232 dip
->devbase
.ioctl_ops
= &pvr2_ioctl_ops
;
1233 dip
->devbase
.device_caps
= caps
;
1236 pvr2_ctrl_get_value(
1237 pvr2_hdw_get_ctrl_by_id(hdw
,
1238 PVR2_CID_STDAVAIL
), &val
);
1239 dip
->devbase
.tvnorms
= (v4l2_std_id
)val
;
1243 unit_number
= pvr2_hdw_get_unit_number(hdw
);
1244 if (nr_ptr
&& (unit_number
>= 0) && (unit_number
< PVR_NUM
)) {
1245 mindevnum
= nr_ptr
[unit_number
];
1247 pvr2_hdw_set_v4l2_dev(hdw
, &dip
->devbase
);
1248 if ((video_register_device(&dip
->devbase
,
1249 dip
->v4l_type
, mindevnum
) < 0) &&
1250 (video_register_device(&dip
->devbase
,
1251 dip
->v4l_type
, -1) < 0)) {
1252 pr_err(KBUILD_MODNAME
1253 ": Failed to register pvrusb2 v4l device\n");
1256 pr_info("pvrusb2: registered device %s [%s]\n",
1257 video_device_node_name(&dip
->devbase
),
1258 pvr2_config_get_name(dip
->config
));
1260 pvr2_hdw_v4l_store_minor_number(hdw
,
1261 dip
->minor_type
,dip
->devbase
.minor
);
1265 struct pvr2_v4l2
*pvr2_v4l2_create(struct pvr2_context
*mnp
)
1267 struct pvr2_v4l2
*vp
;
1269 vp
= kzalloc(sizeof(*vp
),GFP_KERNEL
);
1271 pvr2_channel_init(&vp
->channel
,mnp
);
1272 pvr2_trace(PVR2_TRACE_STRUCT
,"Creating pvr2_v4l2 id=%p",vp
);
1274 vp
->channel
.check_func
= pvr2_v4l2_internal_check
;
1276 /* register streams */
1277 vp
->dev_video
= kzalloc(sizeof(*vp
->dev_video
),GFP_KERNEL
);
1278 if (!vp
->dev_video
) goto fail
;
1279 pvr2_v4l2_dev_init(vp
->dev_video
,vp
,VFL_TYPE_GRABBER
);
1280 if (pvr2_hdw_get_input_available(vp
->channel
.mc_head
->hdw
) &
1281 (1 << PVR2_CVAL_INPUT_RADIO
)) {
1282 vp
->dev_radio
= kzalloc(sizeof(*vp
->dev_radio
),GFP_KERNEL
);
1283 if (!vp
->dev_radio
) goto fail
;
1284 pvr2_v4l2_dev_init(vp
->dev_radio
,vp
,VFL_TYPE_RADIO
);
1289 pvr2_trace(PVR2_TRACE_STRUCT
,"Failure creating pvr2_v4l2 id=%p",vp
);
1290 pvr2_v4l2_destroy_no_lock(vp
);