4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
5 * Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/kernel.h>
23 #include <linux/slab.h>
24 #include "pvrusb2-context.h"
25 #include "pvrusb2-hdw.h"
27 #include "pvrusb2-debug.h"
28 #include "pvrusb2-v4l2.h"
29 #include "pvrusb2-ioread.h"
30 #include <linux/videodev2.h>
31 #include <linux/module.h>
32 #include <media/v4l2-dev.h>
33 #include <media/v4l2-device.h>
34 #include <media/v4l2-fh.h>
35 #include <media/v4l2-common.h>
36 #include <media/v4l2-ioctl.h>
42 struct pvr2_v4l2_dev
{
43 struct video_device devbase
; /* MUST be first! */
44 struct pvr2_v4l2
*v4lp
;
45 struct pvr2_context_stream
*stream
;
46 /* Information about this device: */
47 enum pvr2_config config
; /* Expected stream format */
48 int v4l_type
; /* V4L defined type for this device node */
49 enum pvr2_v4l_type minor_type
; /* pvr2-understood minor device type */
54 struct pvr2_channel channel
;
55 struct pvr2_v4l2_dev
*pdi
;
56 struct pvr2_ioread
*rhp
;
58 wait_queue_head_t wait_data
;
60 /* Map contiguous ordinal value to input id */
61 unsigned char *input_map
;
62 unsigned int input_cnt
;
66 struct pvr2_channel channel
;
68 /* streams - Note that these must be separately, individually,
69 * allocated pointers. This is because the v4l core is going to
70 * manage their deletion - separately, individually... */
71 struct pvr2_v4l2_dev
*dev_video
;
72 struct pvr2_v4l2_dev
*dev_radio
;
75 static int video_nr
[PVR_NUM
] = {[0 ... PVR_NUM
-1] = -1};
76 module_param_array(video_nr
, int, NULL
, 0444);
77 MODULE_PARM_DESC(video_nr
, "Offset for device's video dev minor");
78 static int radio_nr
[PVR_NUM
] = {[0 ... PVR_NUM
-1] = -1};
79 module_param_array(radio_nr
, int, NULL
, 0444);
80 MODULE_PARM_DESC(radio_nr
, "Offset for device's radio dev minor");
81 static int vbi_nr
[PVR_NUM
] = {[0 ... PVR_NUM
-1] = -1};
82 module_param_array(vbi_nr
, int, NULL
, 0444);
83 MODULE_PARM_DESC(vbi_nr
, "Offset for device's vbi dev minor");
85 static struct v4l2_fmtdesc pvr_fmtdesc
[] = {
88 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
89 .flags
= V4L2_FMT_FLAG_COMPRESSED
,
90 .description
= "MPEG1/2",
91 // This should really be V4L2_PIX_FMT_MPEG, but xawtv
92 // breaks when I do that.
93 .pixelformat
= 0, // V4L2_PIX_FMT_MPEG,
97 #define PVR_FORMAT_PIX 0
98 #define PVR_FORMAT_VBI 1
100 static struct v4l2_format pvr_format
[] = {
102 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
107 // This should really be V4L2_PIX_FMT_MPEG,
108 // but xawtv breaks when I do that.
109 .pixelformat
= 0, // V4L2_PIX_FMT_MPEG,
110 .field
= V4L2_FIELD_INTERLACED
,
111 .bytesperline
= 0, // doesn't make sense
113 //FIXME : Don't know what to put here...
114 .sizeimage
= (32*1024),
115 .colorspace
= 0, // doesn't make sense here
121 .type
= V4L2_BUF_TYPE_VBI_CAPTURE
,
124 .sampling_rate
= 27000000,
126 .samples_per_line
= 1443,
127 .sample_format
= V4L2_PIX_FMT_GREY
,
139 * This is part of Video 4 Linux API. These procedures handle ioctl() calls.
141 static int pvr2_querycap(struct file
*file
, void *priv
, struct v4l2_capability
*cap
)
143 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
144 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
146 strlcpy(cap
->driver
, "pvrusb2", sizeof(cap
->driver
));
147 strlcpy(cap
->bus_info
, pvr2_hdw_get_bus_info(hdw
),
148 sizeof(cap
->bus_info
));
149 strlcpy(cap
->card
, pvr2_hdw_get_desc(hdw
), sizeof(cap
->card
));
150 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_TUNER
|
151 V4L2_CAP_AUDIO
| V4L2_CAP_RADIO
|
152 V4L2_CAP_READWRITE
| V4L2_CAP_DEVICE_CAPS
;
153 switch (fh
->pdi
->devbase
.vfl_type
) {
154 case VFL_TYPE_GRABBER
:
155 cap
->device_caps
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_AUDIO
;
158 cap
->device_caps
= V4L2_CAP_RADIO
;
161 cap
->device_caps
|= V4L2_CAP_TUNER
| V4L2_CAP_READWRITE
;
165 static int pvr2_g_std(struct file
*file
, void *priv
, v4l2_std_id
*std
)
167 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
168 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
172 ret
= pvr2_ctrl_get_value(
173 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_STDCUR
), &val
);
178 static int pvr2_s_std(struct file
*file
, void *priv
, v4l2_std_id std
)
180 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
181 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
183 return pvr2_ctrl_set_value(
184 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_STDCUR
), std
);
187 static int pvr2_querystd(struct file
*file
, void *priv
, v4l2_std_id
*std
)
189 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
190 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
194 ret
= pvr2_ctrl_get_value(
195 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_STDDETECT
), &val
);
200 static int pvr2_enum_input(struct file
*file
, void *priv
, struct v4l2_input
*vi
)
202 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
203 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
204 struct pvr2_ctrl
*cptr
;
205 struct v4l2_input tmp
;
209 cptr
= pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_INPUT
);
211 memset(&tmp
, 0, sizeof(tmp
));
212 tmp
.index
= vi
->index
;
213 if (vi
->index
>= fh
->input_cnt
)
215 val
= fh
->input_map
[vi
->index
];
217 case PVR2_CVAL_INPUT_TV
:
218 case PVR2_CVAL_INPUT_DTV
:
219 case PVR2_CVAL_INPUT_RADIO
:
220 tmp
.type
= V4L2_INPUT_TYPE_TUNER
;
222 case PVR2_CVAL_INPUT_SVIDEO
:
223 case PVR2_CVAL_INPUT_COMPOSITE
:
224 tmp
.type
= V4L2_INPUT_TYPE_CAMERA
;
231 pvr2_ctrl_get_valname(cptr
, val
,
232 tmp
.name
, sizeof(tmp
.name
) - 1, &cnt
);
235 /* Don't bother with audioset, since this driver currently
236 always switches the audio whenever the video is
239 /* Handling std is a tougher problem. It doesn't make
240 sense in cases where a device might be multi-standard.
241 We could just copy out the current value for the
242 standard, but it can change over time. For now just
248 static int pvr2_g_input(struct file
*file
, void *priv
, unsigned int *i
)
250 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
251 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
253 struct pvr2_ctrl
*cptr
;
257 cptr
= pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_INPUT
);
259 ret
= pvr2_ctrl_get_value(cptr
, &val
);
261 for (idx
= 0; idx
< fh
->input_cnt
; idx
++) {
262 if (fh
->input_map
[idx
] == val
) {
270 static int pvr2_s_input(struct file
*file
, void *priv
, unsigned int inp
)
272 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
273 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
275 if (inp
>= fh
->input_cnt
)
277 return pvr2_ctrl_set_value(
278 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_INPUT
),
282 static int pvr2_enumaudio(struct file
*file
, void *priv
, struct v4l2_audio
*vin
)
284 /* pkt: FIXME: We are returning one "fake" input here
285 which could very well be called "whatever_we_like".
286 This is for apps that want to see an audio input
287 just to feel comfortable, as well as to test if
288 it can do stereo or sth. There is actually no guarantee
289 that the actual audio input cannot change behind the app's
290 back, but most applications should not mind that either.
292 Hopefully, mplayer people will work with us on this (this
293 whole mess is to support mplayer pvr://), or Hans will come
294 up with a more standard way to say "we have inputs but we
295 don 't want you to change them independent of video" which
301 strncpy(vin
->name
, "PVRUSB2 Audio", 14);
302 vin
->capability
= V4L2_AUDCAP_STEREO
;
306 static int pvr2_g_audio(struct file
*file
, void *priv
, struct v4l2_audio
*vin
)
308 /* pkt: FIXME: see above comment (VIDIOC_ENUMAUDIO) */
310 strncpy(vin
->name
, "PVRUSB2 Audio", 14);
311 vin
->capability
= V4L2_AUDCAP_STEREO
;
315 static int pvr2_s_audio(struct file
*file
, void *priv
, const struct v4l2_audio
*vout
)
322 static int pvr2_g_tuner(struct file
*file
, void *priv
, struct v4l2_tuner
*vt
)
324 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
325 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
328 return -EINVAL
; /* Only answer for the 1st tuner */
330 pvr2_hdw_execute_tuner_poll(hdw
);
331 return pvr2_hdw_get_tuner_status(hdw
, vt
);
334 static int pvr2_s_tuner(struct file
*file
, void *priv
, const struct v4l2_tuner
*vt
)
336 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
337 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
342 return pvr2_ctrl_set_value(
343 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_AUDIOMODE
),
347 static int pvr2_s_frequency(struct file
*file
, void *priv
, const struct v4l2_frequency
*vf
)
349 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
350 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
352 struct v4l2_tuner vt
;
354 struct pvr2_ctrl
*ctrlp
;
357 ret
= pvr2_hdw_get_tuner_status(hdw
, &vt
);
360 ctrlp
= pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_INPUT
);
361 ret
= pvr2_ctrl_get_value(ctrlp
, &cur_input
);
364 if (vf
->type
== V4L2_TUNER_RADIO
) {
365 if (cur_input
!= PVR2_CVAL_INPUT_RADIO
)
366 pvr2_ctrl_set_value(ctrlp
, PVR2_CVAL_INPUT_RADIO
);
368 if (cur_input
== PVR2_CVAL_INPUT_RADIO
)
369 pvr2_ctrl_set_value(ctrlp
, PVR2_CVAL_INPUT_TV
);
372 if (vt
.capability
& V4L2_TUNER_CAP_LOW
)
376 return pvr2_ctrl_set_value(
377 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_FREQUENCY
),fv
);
380 static int pvr2_g_frequency(struct file
*file
, void *priv
, struct v4l2_frequency
*vf
)
382 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
383 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
386 struct v4l2_tuner vt
;
389 ret
= pvr2_hdw_get_tuner_status(hdw
, &vt
);
392 ret
= pvr2_ctrl_get_value(
393 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_FREQUENCY
),
398 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_INPUT
),
400 if (cur_input
== PVR2_CVAL_INPUT_RADIO
)
401 vf
->type
= V4L2_TUNER_RADIO
;
403 vf
->type
= V4L2_TUNER_ANALOG_TV
;
404 if (vt
.capability
& V4L2_TUNER_CAP_LOW
)
405 val
= (val
* 2) / 125;
412 static int pvr2_enum_fmt_vid_cap(struct file
*file
, void *priv
, struct v4l2_fmtdesc
*fd
)
414 /* Only one format is supported : mpeg.*/
418 memcpy(fd
, pvr_fmtdesc
, sizeof(struct v4l2_fmtdesc
));
422 static int pvr2_g_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
;
428 memcpy(vf
, &pvr_format
[PVR_FORMAT_PIX
], sizeof(struct v4l2_format
));
431 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_HRES
),
433 vf
->fmt
.pix
.width
= val
;
436 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_VRES
),
438 vf
->fmt
.pix
.height
= val
;
442 static int pvr2_try_fmt_vid_cap(struct file
*file
, void *priv
, struct v4l2_format
*vf
)
444 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
445 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
446 int lmin
, lmax
, ldef
;
447 struct pvr2_ctrl
*hcp
, *vcp
;
448 int h
= vf
->fmt
.pix
.height
;
449 int w
= vf
->fmt
.pix
.width
;
451 hcp
= pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_HRES
);
452 vcp
= pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_VRES
);
454 lmin
= pvr2_ctrl_get_min(hcp
);
455 lmax
= pvr2_ctrl_get_max(hcp
);
456 pvr2_ctrl_get_def(hcp
, &ldef
);
463 lmin
= pvr2_ctrl_get_min(vcp
);
464 lmax
= pvr2_ctrl_get_max(vcp
);
465 pvr2_ctrl_get_def(vcp
, &ldef
);
473 memcpy(vf
, &pvr_format
[PVR_FORMAT_PIX
],
474 sizeof(struct v4l2_format
));
475 vf
->fmt
.pix
.width
= w
;
476 vf
->fmt
.pix
.height
= h
;
480 static int pvr2_s_fmt_vid_cap(struct file
*file
, void *priv
, struct v4l2_format
*vf
)
482 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
483 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
484 struct pvr2_ctrl
*hcp
, *vcp
;
485 int ret
= pvr2_try_fmt_vid_cap(file
, fh
, vf
);
489 hcp
= pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_HRES
);
490 vcp
= pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_VRES
);
491 pvr2_ctrl_set_value(hcp
, vf
->fmt
.pix
.width
);
492 pvr2_ctrl_set_value(vcp
, vf
->fmt
.pix
.height
);
496 static int pvr2_streamon(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
498 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
499 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
500 struct pvr2_v4l2_dev
*pdi
= fh
->pdi
;
503 if (!fh
->pdi
->stream
) {
504 /* No stream defined for this node. This means
505 that we're not currently allowed to stream from
509 ret
= pvr2_hdw_set_stream_type(hdw
, pdi
->config
);
512 return pvr2_hdw_set_streaming(hdw
, !0);
515 static int pvr2_streamoff(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
517 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
518 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
520 if (!fh
->pdi
->stream
) {
521 /* No stream defined for this node. This means
522 that we're not currently allowed to stream from
526 return pvr2_hdw_set_streaming(hdw
, 0);
529 static int pvr2_queryctrl(struct file
*file
, void *priv
,
530 struct v4l2_queryctrl
*vc
)
532 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
533 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
534 struct pvr2_ctrl
*cptr
;
537 if (vc
->id
& V4L2_CTRL_FLAG_NEXT_CTRL
) {
538 cptr
= pvr2_hdw_get_ctrl_nextv4l(
539 hdw
, (vc
->id
& ~V4L2_CTRL_FLAG_NEXT_CTRL
));
541 vc
->id
= pvr2_ctrl_get_v4lid(cptr
);
543 cptr
= pvr2_hdw_get_ctrl_v4l(hdw
, vc
->id
);
546 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
547 "QUERYCTRL id=0x%x not implemented here",
552 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
553 "QUERYCTRL id=0x%x mapping name=%s (%s)",
554 vc
->id
, pvr2_ctrl_get_name(cptr
),
555 pvr2_ctrl_get_desc(cptr
));
556 strlcpy(vc
->name
, pvr2_ctrl_get_desc(cptr
), sizeof(vc
->name
));
557 vc
->flags
= pvr2_ctrl_get_v4lflags(cptr
);
558 pvr2_ctrl_get_def(cptr
, &val
);
559 vc
->default_value
= val
;
560 switch (pvr2_ctrl_get_type(cptr
)) {
562 vc
->type
= V4L2_CTRL_TYPE_MENU
;
564 vc
->maximum
= pvr2_ctrl_get_cnt(cptr
) - 1;
568 vc
->type
= V4L2_CTRL_TYPE_BOOLEAN
;
574 vc
->type
= V4L2_CTRL_TYPE_INTEGER
;
575 vc
->minimum
= pvr2_ctrl_get_min(cptr
);
576 vc
->maximum
= pvr2_ctrl_get_max(cptr
);
580 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
581 "QUERYCTRL id=0x%x name=%s not mappable",
582 vc
->id
, pvr2_ctrl_get_name(cptr
));
588 static int pvr2_querymenu(struct file
*file
, void *priv
, struct v4l2_querymenu
*vm
)
590 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
591 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
592 unsigned int cnt
= 0;
595 ret
= pvr2_ctrl_get_valname(pvr2_hdw_get_ctrl_v4l(hdw
, vm
->id
),
597 vm
->name
, sizeof(vm
->name
) - 1,
603 static int pvr2_g_ctrl(struct file
*file
, void *priv
, struct v4l2_control
*vc
)
605 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
606 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
610 ret
= pvr2_ctrl_get_value(pvr2_hdw_get_ctrl_v4l(hdw
, vc
->id
),
616 static int pvr2_s_ctrl(struct file
*file
, void *priv
, struct v4l2_control
*vc
)
618 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
619 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
621 return pvr2_ctrl_set_value(pvr2_hdw_get_ctrl_v4l(hdw
, vc
->id
),
625 static int pvr2_g_ext_ctrls(struct file
*file
, void *priv
,
626 struct v4l2_ext_controls
*ctls
)
628 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
629 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
630 struct v4l2_ext_control
*ctrl
;
631 struct pvr2_ctrl
*cptr
;
637 for (idx
= 0; idx
< ctls
->count
; idx
++) {
638 ctrl
= ctls
->controls
+ idx
;
639 cptr
= pvr2_hdw_get_ctrl_v4l(hdw
, ctrl
->id
);
641 if (ctls
->which
== V4L2_CTRL_WHICH_DEF_VAL
)
642 pvr2_ctrl_get_def(cptr
, &val
);
644 ret
= pvr2_ctrl_get_value(cptr
, &val
);
649 ctls
->error_idx
= idx
;
652 /* Ensure that if read as a 64 bit value, the user
653 will still get a hopefully sane value */
660 static int pvr2_s_ext_ctrls(struct file
*file
, void *priv
,
661 struct v4l2_ext_controls
*ctls
)
663 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
664 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
665 struct v4l2_ext_control
*ctrl
;
669 /* Default value cannot be changed */
670 if (ctls
->which
== V4L2_CTRL_WHICH_DEF_VAL
)
674 for (idx
= 0; idx
< ctls
->count
; idx
++) {
675 ctrl
= ctls
->controls
+ idx
;
676 ret
= pvr2_ctrl_set_value(
677 pvr2_hdw_get_ctrl_v4l(hdw
, ctrl
->id
),
680 ctls
->error_idx
= idx
;
687 static int pvr2_try_ext_ctrls(struct file
*file
, void *priv
,
688 struct v4l2_ext_controls
*ctls
)
690 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
691 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
692 struct v4l2_ext_control
*ctrl
;
693 struct pvr2_ctrl
*pctl
;
696 /* For the moment just validate that the requested control
698 for (idx
= 0; idx
< ctls
->count
; idx
++) {
699 ctrl
= ctls
->controls
+ idx
;
700 pctl
= pvr2_hdw_get_ctrl_v4l(hdw
, ctrl
->id
);
702 ctls
->error_idx
= idx
;
709 static int pvr2_cropcap(struct file
*file
, void *priv
, struct v4l2_cropcap
*cap
)
711 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
712 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
715 if (cap
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
717 ret
= pvr2_hdw_get_cropcap(hdw
, cap
);
718 cap
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
; /* paranoia */
722 static int pvr2_g_crop(struct file
*file
, void *priv
, struct v4l2_crop
*crop
)
724 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
725 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
729 if (crop
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
731 ret
= pvr2_ctrl_get_value(
732 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPL
), &val
);
736 ret
= pvr2_ctrl_get_value(
737 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPT
), &val
);
741 ret
= pvr2_ctrl_get_value(
742 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPW
), &val
);
746 ret
= pvr2_ctrl_get_value(
747 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPH
), &val
);
750 crop
->c
.height
= val
;
754 static int pvr2_s_crop(struct file
*file
, void *priv
, const struct v4l2_crop
*crop
)
756 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
757 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
760 if (crop
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
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
),
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_cropcap
= pvr2_cropcap
,
801 .vidioc_s_crop
= pvr2_s_crop
,
802 .vidioc_g_crop
= pvr2_g_crop
,
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 printk(KERN_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
) ||
901 !list_empty(&vp
->dev_radio
->devbase
.fh_list
))
903 pvr2_v4l2_destroy_no_lock(vp
);
907 static long pvr2_v4l2_ioctl(struct file
*file
,
908 unsigned int cmd
, unsigned long arg
)
911 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
912 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
915 if (pvrusb2_debug
& PVR2_TRACE_V4LIOCTL
)
916 v4l_printk_ioctl(pvr2_hdw_get_driver_name(hdw
), cmd
);
918 if (!pvr2_hdw_dev_ok(hdw
)) {
919 pvr2_trace(PVR2_TRACE_ERROR_LEGS
,
920 "ioctl failed - bad or no context");
924 ret
= video_ioctl2(file
, cmd
, arg
);
926 pvr2_hdw_commit_ctl(hdw
);
929 if (pvrusb2_debug
& PVR2_TRACE_V4LIOCTL
) {
930 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
931 "pvr2_v4l2_do_ioctl failure, ret=%ld"
932 " command was:", ret
);
933 v4l_printk_ioctl(pvr2_hdw_get_driver_name(hdw
), cmd
);
936 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
937 "pvr2_v4l2_do_ioctl complete, ret=%ld (0x%lx)",
945 static int pvr2_v4l2_release(struct file
*file
)
947 struct pvr2_v4l2_fh
*fhp
= file
->private_data
;
948 struct pvr2_v4l2
*vp
= fhp
->pdi
->v4lp
;
949 struct pvr2_hdw
*hdw
= fhp
->channel
.mc_head
->hdw
;
951 pvr2_trace(PVR2_TRACE_OPEN_CLOSE
,"pvr2_v4l2_release");
954 struct pvr2_stream
*sp
;
955 pvr2_hdw_set_streaming(hdw
,0);
956 sp
= pvr2_ioread_get_stream(fhp
->rhp
);
957 if (sp
) pvr2_stream_set_callback(sp
,NULL
,NULL
);
958 pvr2_ioread_destroy(fhp
->rhp
);
962 v4l2_fh_del(&fhp
->fh
);
963 v4l2_fh_exit(&fhp
->fh
);
964 file
->private_data
= NULL
;
966 pvr2_channel_done(&fhp
->channel
);
967 pvr2_trace(PVR2_TRACE_STRUCT
,
968 "Destroying pvr_v4l2_fh id=%p",fhp
);
969 if (fhp
->input_map
) {
970 kfree(fhp
->input_map
);
971 fhp
->input_map
= NULL
;
974 if (vp
->channel
.mc_head
->disconnect_flag
&&
975 list_empty(&vp
->dev_video
->devbase
.fh_list
) &&
976 list_empty(&vp
->dev_radio
->devbase
.fh_list
)) {
977 pvr2_v4l2_destroy_no_lock(vp
);
983 static int pvr2_v4l2_open(struct file
*file
)
985 struct pvr2_v4l2_dev
*dip
; /* Our own context pointer */
986 struct pvr2_v4l2_fh
*fhp
;
987 struct pvr2_v4l2
*vp
;
988 struct pvr2_hdw
*hdw
;
989 unsigned int input_mask
= 0;
990 unsigned int input_cnt
,idx
;
993 dip
= container_of(video_devdata(file
),struct pvr2_v4l2_dev
,devbase
);
996 hdw
= vp
->channel
.hdw
;
998 pvr2_trace(PVR2_TRACE_OPEN_CLOSE
,"pvr2_v4l2_open");
1000 if (!pvr2_hdw_dev_ok(hdw
)) {
1001 pvr2_trace(PVR2_TRACE_OPEN_CLOSE
,
1002 "pvr2_v4l2_open: hardware not ready");
1006 fhp
= kzalloc(sizeof(*fhp
),GFP_KERNEL
);
1011 v4l2_fh_init(&fhp
->fh
, &dip
->devbase
);
1012 init_waitqueue_head(&fhp
->wait_data
);
1015 pvr2_trace(PVR2_TRACE_STRUCT
,"Creating pvr_v4l2_fh id=%p",fhp
);
1016 pvr2_channel_init(&fhp
->channel
,vp
->channel
.mc_head
);
1018 if (dip
->v4l_type
== VFL_TYPE_RADIO
) {
1019 /* Opening device as a radio, legal input selection subset
1020 is just the radio. */
1021 input_mask
= (1 << PVR2_CVAL_INPUT_RADIO
);
1023 /* Opening the main V4L device, legal input selection
1024 subset includes all analog inputs. */
1025 input_mask
= ((1 << PVR2_CVAL_INPUT_RADIO
) |
1026 (1 << PVR2_CVAL_INPUT_TV
) |
1027 (1 << PVR2_CVAL_INPUT_COMPOSITE
) |
1028 (1 << PVR2_CVAL_INPUT_SVIDEO
));
1030 ret
= pvr2_channel_limit_inputs(&fhp
->channel
,input_mask
);
1032 pvr2_channel_done(&fhp
->channel
);
1033 pvr2_trace(PVR2_TRACE_STRUCT
,
1034 "Destroying pvr_v4l2_fh id=%p (input mask error)",
1041 input_mask
&= pvr2_hdw_get_input_available(hdw
);
1043 for (idx
= 0; idx
< (sizeof(input_mask
) << 3); idx
++) {
1044 if (input_mask
& (1 << idx
)) input_cnt
++;
1046 fhp
->input_cnt
= input_cnt
;
1047 fhp
->input_map
= kzalloc(input_cnt
,GFP_KERNEL
);
1048 if (!fhp
->input_map
) {
1049 pvr2_channel_done(&fhp
->channel
);
1050 pvr2_trace(PVR2_TRACE_STRUCT
,
1051 "Destroying pvr_v4l2_fh id=%p (input map failure)",
1057 for (idx
= 0; idx
< (sizeof(input_mask
) << 3); idx
++) {
1058 if (!(input_mask
& (1 << idx
))) continue;
1059 fhp
->input_map
[input_cnt
++] = idx
;
1063 file
->private_data
= fhp
;
1065 fhp
->fw_mode_flag
= pvr2_hdw_cpufw_get_enabled(hdw
);
1066 v4l2_fh_add(&fhp
->fh
);
1072 static void pvr2_v4l2_notify(struct pvr2_v4l2_fh
*fhp
)
1074 wake_up(&fhp
->wait_data
);
1077 static int pvr2_v4l2_iosetup(struct pvr2_v4l2_fh
*fh
)
1080 struct pvr2_stream
*sp
;
1081 struct pvr2_hdw
*hdw
;
1082 if (fh
->rhp
) return 0;
1084 if (!fh
->pdi
->stream
) {
1085 /* No stream defined for this node. This means that we're
1086 not currently allowed to stream from this node. */
1090 /* First read() attempt. Try to claim the stream and start
1092 if ((ret
= pvr2_channel_claim_stream(&fh
->channel
,
1093 fh
->pdi
->stream
)) != 0) {
1094 /* Someone else must already have it */
1098 fh
->rhp
= pvr2_channel_create_mpeg_stream(fh
->pdi
->stream
);
1100 pvr2_channel_claim_stream(&fh
->channel
,NULL
);
1104 hdw
= fh
->channel
.mc_head
->hdw
;
1105 sp
= fh
->pdi
->stream
->stream
;
1106 pvr2_stream_set_callback(sp
,(pvr2_stream_callback
)pvr2_v4l2_notify
,fh
);
1107 pvr2_hdw_set_stream_type(hdw
,fh
->pdi
->config
);
1108 if ((ret
= pvr2_hdw_set_streaming(hdw
,!0)) < 0) return ret
;
1109 return pvr2_ioread_set_enabled(fh
->rhp
,!0);
1113 static ssize_t
pvr2_v4l2_read(struct file
*file
,
1114 char __user
*buff
, size_t count
, loff_t
*ppos
)
1116 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
1119 if (fh
->fw_mode_flag
) {
1120 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
1124 unsigned int offs
= *ppos
;
1126 tbuf
= kmalloc(PAGE_SIZE
,GFP_KERNEL
);
1127 if (!tbuf
) return -ENOMEM
;
1131 if (c1
> PAGE_SIZE
) c1
= PAGE_SIZE
;
1132 c2
= pvr2_hdw_cpufw_get(hdw
,offs
,tbuf
,c1
);
1138 if (copy_to_user(buff
,tbuf
,c2
)) {
1153 ret
= pvr2_v4l2_iosetup(fh
);
1160 ret
= pvr2_ioread_read(fh
->rhp
,buff
,count
);
1161 if (ret
>= 0) break;
1162 if (ret
!= -EAGAIN
) break;
1163 if (file
->f_flags
& O_NONBLOCK
) break;
1164 /* Doing blocking I/O. Wait here. */
1165 ret
= wait_event_interruptible(
1167 pvr2_ioread_avail(fh
->rhp
) >= 0);
1175 static unsigned int pvr2_v4l2_poll(struct file
*file
, poll_table
*wait
)
1177 unsigned int mask
= 0;
1178 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
1181 if (fh
->fw_mode_flag
) {
1182 mask
|= POLLIN
| POLLRDNORM
;
1187 ret
= pvr2_v4l2_iosetup(fh
);
1188 if (ret
) return POLLERR
;
1191 poll_wait(file
,&fh
->wait_data
,wait
);
1193 if (pvr2_ioread_avail(fh
->rhp
) >= 0) {
1194 mask
|= POLLIN
| POLLRDNORM
;
1201 static const struct v4l2_file_operations vdev_fops
= {
1202 .owner
= THIS_MODULE
,
1203 .open
= pvr2_v4l2_open
,
1204 .release
= pvr2_v4l2_release
,
1205 .read
= pvr2_v4l2_read
,
1206 .unlocked_ioctl
= pvr2_v4l2_ioctl
,
1207 .poll
= pvr2_v4l2_poll
,
1211 static struct video_device vdev_template
= {
1216 static void pvr2_v4l2_dev_init(struct pvr2_v4l2_dev
*dip
,
1217 struct pvr2_v4l2
*vp
,
1222 struct pvr2_hdw
*hdw
;
1226 hdw
= vp
->channel
.mc_head
->hdw
;
1227 dip
->v4l_type
= v4l_type
;
1229 case VFL_TYPE_GRABBER
:
1230 dip
->stream
= &vp
->channel
.mc_head
->video_stream
;
1231 dip
->config
= pvr2_config_mpeg
;
1232 dip
->minor_type
= pvr2_v4l_type_video
;
1235 pr_err(KBUILD_MODNAME
1236 ": Failed to set up pvrusb2 v4l video dev"
1237 " due to missing stream instance\n");
1242 dip
->config
= pvr2_config_vbi
;
1243 dip
->minor_type
= pvr2_v4l_type_vbi
;
1246 case VFL_TYPE_RADIO
:
1247 dip
->stream
= &vp
->channel
.mc_head
->video_stream
;
1248 dip
->config
= pvr2_config_mpeg
;
1249 dip
->minor_type
= pvr2_v4l_type_radio
;
1253 /* Bail out (this should be impossible) */
1254 pr_err(KBUILD_MODNAME
": Failed to set up pvrusb2 v4l dev"
1255 " 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
);