4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
5 * Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/kernel.h>
23 #include <linux/slab.h>
24 #include <linux/version.h>
25 #include "pvrusb2-context.h"
26 #include "pvrusb2-hdw.h"
28 #include "pvrusb2-debug.h"
29 #include "pvrusb2-v4l2.h"
30 #include "pvrusb2-ioread.h"
31 #include <linux/videodev2.h>
32 #include <linux/module.h>
33 #include <media/v4l2-dev.h>
34 #include <media/v4l2-device.h>
35 #include <media/v4l2-common.h>
36 #include <media/v4l2-ioctl.h>
42 struct pvr2_v4l2_dev {
43 struct video_device devbase; /* MUST be first! */
44 struct pvr2_v4l2 *v4lp;
45 struct pvr2_context_stream *stream;
46 /* Information about this device: */
47 enum pvr2_config config; /* Expected stream format */
48 int v4l_type; /* V4L defined type for this device node */
49 enum pvr2_v4l_type minor_type; /* pvr2-understood minor device type */
53 struct pvr2_channel channel;
54 struct pvr2_v4l2_dev *pdi;
55 enum v4l2_priority prio;
56 struct pvr2_ioread *rhp;
58 struct pvr2_v4l2 *vhead;
59 struct pvr2_v4l2_fh *vnext;
60 struct pvr2_v4l2_fh *vprev;
61 wait_queue_head_t wait_data;
63 /* Map contiguous ordinal value to input id */
64 unsigned char *input_map;
65 unsigned int input_cnt;
69 struct pvr2_channel channel;
70 struct pvr2_v4l2_fh *vfirst;
71 struct pvr2_v4l2_fh *vlast;
73 struct v4l2_prio_state prio;
75 /* streams - Note that these must be separately, individually,
76 * allocated pointers. This is because the v4l core is going to
77 * manage their deletion - separately, individually... */
78 struct pvr2_v4l2_dev *dev_video;
79 struct pvr2_v4l2_dev *dev_radio;
82 static int video_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
83 module_param_array(video_nr, int, NULL, 0444);
84 MODULE_PARM_DESC(video_nr, "Offset for device's video dev minor");
85 static int radio_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
86 module_param_array(radio_nr, int, NULL, 0444);
87 MODULE_PARM_DESC(radio_nr, "Offset for device's radio dev minor");
88 static int vbi_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
89 module_param_array(vbi_nr, int, NULL, 0444);
90 MODULE_PARM_DESC(vbi_nr, "Offset for device's vbi dev minor");
92 static struct v4l2_fmtdesc pvr_fmtdesc [] = {
95 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
96 .flags = V4L2_FMT_FLAG_COMPRESSED,
97 .description = "MPEG1/2",
98 // This should really be V4L2_PIX_FMT_MPEG, but xawtv
99 // breaks when I do that.
100 .pixelformat = 0, // V4L2_PIX_FMT_MPEG,
104 #define PVR_FORMAT_PIX 0
105 #define PVR_FORMAT_VBI 1
107 static struct v4l2_format pvr_format [] = {
109 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
114 // This should really be V4L2_PIX_FMT_MPEG,
115 // but xawtv breaks when I do that.
116 .pixelformat = 0, // V4L2_PIX_FMT_MPEG,
117 .field = V4L2_FIELD_INTERLACED,
118 .bytesperline = 0, // doesn't make sense
120 //FIXME : Don't know what to put here...
121 .sizeimage = (32*1024),
122 .colorspace = 0, // doesn't make sense here
128 .type = V4L2_BUF_TYPE_VBI_CAPTURE,
131 .sampling_rate = 27000000,
133 .samples_per_line = 1443,
134 .sample_format = V4L2_PIX_FMT_GREY,
146 * This is part of Video 4 Linux API. These procedures handle ioctl() calls.
148 static int pvr2_querycap(struct file *file, void *priv, struct v4l2_capability *cap)
150 struct pvr2_v4l2_fh *fh = file->private_data;
151 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
153 strlcpy(cap->driver, "pvrusb2", sizeof(cap->driver));
154 strlcpy(cap->bus_info, pvr2_hdw_get_bus_info(hdw),
155 sizeof(cap->bus_info));
156 strlcpy(cap->card, pvr2_hdw_get_desc(hdw), sizeof(cap->card));
157 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_TUNER |
158 V4L2_CAP_AUDIO | V4L2_CAP_RADIO |
159 V4L2_CAP_READWRITE | V4L2_CAP_DEVICE_CAPS;
160 switch (fh->pdi->devbase.vfl_type) {
161 case VFL_TYPE_GRABBER:
162 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_AUDIO;
165 cap->device_caps = V4L2_CAP_RADIO;
168 cap->device_caps |= V4L2_CAP_TUNER | V4L2_CAP_READWRITE;
172 static int pvr2_g_priority(struct file *file, void *priv, enum v4l2_priority *p)
174 struct pvr2_v4l2_fh *fh = file->private_data;
175 struct pvr2_v4l2 *vp = fh->vhead;
177 *p = v4l2_prio_max(&vp->prio);
181 static int pvr2_s_priority(struct file *file, void *priv, enum v4l2_priority prio)
183 struct pvr2_v4l2_fh *fh = file->private_data;
184 struct pvr2_v4l2 *vp = fh->vhead;
186 return v4l2_prio_change(&vp->prio, &fh->prio, prio);
189 static int pvr2_g_std(struct file *file, void *priv, v4l2_std_id *std)
191 struct pvr2_v4l2_fh *fh = file->private_data;
192 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
196 ret = pvr2_ctrl_get_value(
197 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_STDCUR), &val);
202 static int pvr2_s_std(struct file *file, void *priv, v4l2_std_id std)
204 struct pvr2_v4l2_fh *fh = file->private_data;
205 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
207 return pvr2_ctrl_set_value(
208 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_STDCUR), std);
211 static int pvr2_querystd(struct file *file, void *priv, v4l2_std_id *std)
213 struct pvr2_v4l2_fh *fh = file->private_data;
214 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
218 ret = pvr2_ctrl_get_value(
219 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_STDDETECT), &val);
224 static int pvr2_enum_input(struct file *file, void *priv, struct v4l2_input *vi)
226 struct pvr2_v4l2_fh *fh = file->private_data;
227 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
228 struct pvr2_ctrl *cptr;
229 struct v4l2_input tmp;
233 cptr = pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_INPUT);
235 memset(&tmp, 0, sizeof(tmp));
236 tmp.index = vi->index;
237 if (vi->index >= fh->input_cnt)
239 val = fh->input_map[vi->index];
241 case PVR2_CVAL_INPUT_TV:
242 case PVR2_CVAL_INPUT_DTV:
243 case PVR2_CVAL_INPUT_RADIO:
244 tmp.type = V4L2_INPUT_TYPE_TUNER;
246 case PVR2_CVAL_INPUT_SVIDEO:
247 case PVR2_CVAL_INPUT_COMPOSITE:
248 tmp.type = V4L2_INPUT_TYPE_CAMERA;
255 pvr2_ctrl_get_valname(cptr, val,
256 tmp.name, sizeof(tmp.name) - 1, &cnt);
259 /* Don't bother with audioset, since this driver currently
260 always switches the audio whenever the video is
263 /* Handling std is a tougher problem. It doesn't make
264 sense in cases where a device might be multi-standard.
265 We could just copy out the current value for the
266 standard, but it can change over time. For now just
272 static int pvr2_g_input(struct file *file, void *priv, unsigned int *i)
274 struct pvr2_v4l2_fh *fh = file->private_data;
275 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
277 struct pvr2_ctrl *cptr;
281 cptr = pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_INPUT);
283 ret = pvr2_ctrl_get_value(cptr, &val);
285 for (idx = 0; idx < fh->input_cnt; idx++) {
286 if (fh->input_map[idx] == val) {
294 static int pvr2_s_input(struct file *file, void *priv, unsigned int inp)
296 struct pvr2_v4l2_fh *fh = file->private_data;
297 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
299 if (inp >= fh->input_cnt)
301 return pvr2_ctrl_set_value(
302 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_INPUT),
306 static int pvr2_enumaudio(struct file *file, void *priv, struct v4l2_audio *vin)
308 /* pkt: FIXME: We are returning one "fake" input here
309 which could very well be called "whatever_we_like".
310 This is for apps that want to see an audio input
311 just to feel comfortable, as well as to test if
312 it can do stereo or sth. There is actually no guarantee
313 that the actual audio input cannot change behind the app's
314 back, but most applications should not mind that either.
316 Hopefully, mplayer people will work with us on this (this
317 whole mess is to support mplayer pvr://), or Hans will come
318 up with a more standard way to say "we have inputs but we
319 don 't want you to change them independent of video" which
325 strncpy(vin->name, "PVRUSB2 Audio", 14);
326 vin->capability = V4L2_AUDCAP_STEREO;
330 static int pvr2_g_audio(struct file *file, void *priv, struct v4l2_audio *vin)
332 /* pkt: FIXME: see above comment (VIDIOC_ENUMAUDIO) */
334 strncpy(vin->name, "PVRUSB2 Audio", 14);
335 vin->capability = V4L2_AUDCAP_STEREO;
339 static int pvr2_s_audio(struct file *file, void *priv, const struct v4l2_audio *vout)
346 static int pvr2_g_tuner(struct file *file, void *priv, struct v4l2_tuner *vt)
348 struct pvr2_v4l2_fh *fh = file->private_data;
349 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
352 return -EINVAL; /* Only answer for the 1st tuner */
354 pvr2_hdw_execute_tuner_poll(hdw);
355 return pvr2_hdw_get_tuner_status(hdw, vt);
358 static int pvr2_s_tuner(struct file *file, void *priv, const struct v4l2_tuner *vt)
360 struct pvr2_v4l2_fh *fh = file->private_data;
361 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
366 return pvr2_ctrl_set_value(
367 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_AUDIOMODE),
371 static int pvr2_s_frequency(struct file *file, void *priv, const struct v4l2_frequency *vf)
373 struct pvr2_v4l2_fh *fh = file->private_data;
374 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
376 struct v4l2_tuner vt;
378 struct pvr2_ctrl *ctrlp;
381 ret = pvr2_hdw_get_tuner_status(hdw, &vt);
384 ctrlp = pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_INPUT);
385 ret = pvr2_ctrl_get_value(ctrlp, &cur_input);
388 if (vf->type == V4L2_TUNER_RADIO) {
389 if (cur_input != PVR2_CVAL_INPUT_RADIO)
390 pvr2_ctrl_set_value(ctrlp, PVR2_CVAL_INPUT_RADIO);
392 if (cur_input == PVR2_CVAL_INPUT_RADIO)
393 pvr2_ctrl_set_value(ctrlp, PVR2_CVAL_INPUT_TV);
396 if (vt.capability & V4L2_TUNER_CAP_LOW)
400 return pvr2_ctrl_set_value(
401 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),fv);
404 static int pvr2_g_frequency(struct file *file, void *priv, struct v4l2_frequency *vf)
406 struct pvr2_v4l2_fh *fh = file->private_data;
407 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
410 struct v4l2_tuner vt;
413 ret = pvr2_hdw_get_tuner_status(hdw, &vt);
416 ret = pvr2_ctrl_get_value(
417 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_FREQUENCY),
422 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_INPUT),
424 if (cur_input == PVR2_CVAL_INPUT_RADIO)
425 vf->type = V4L2_TUNER_RADIO;
427 vf->type = V4L2_TUNER_ANALOG_TV;
428 if (vt.capability & V4L2_TUNER_CAP_LOW)
429 val = (val * 2) / 125;
436 static int pvr2_enum_fmt_vid_cap(struct file *file, void *priv, struct v4l2_fmtdesc *fd)
438 /* Only one format is supported : mpeg.*/
442 memcpy(fd, pvr_fmtdesc, sizeof(struct v4l2_fmtdesc));
446 static int pvr2_g_fmt_vid_cap(struct file *file, void *priv, struct v4l2_format *vf)
448 struct pvr2_v4l2_fh *fh = file->private_data;
449 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
452 memcpy(vf, &pvr_format[PVR_FORMAT_PIX], sizeof(struct v4l2_format));
455 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_HRES),
457 vf->fmt.pix.width = val;
460 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_VRES),
462 vf->fmt.pix.height = val;
466 static int pvr2_try_fmt_vid_cap(struct file *file, void *priv, struct v4l2_format *vf)
468 struct pvr2_v4l2_fh *fh = file->private_data;
469 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
470 int lmin, lmax, ldef;
471 struct pvr2_ctrl *hcp, *vcp;
472 int h = vf->fmt.pix.height;
473 int w = vf->fmt.pix.width;
475 hcp = pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_HRES);
476 vcp = pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_VRES);
478 lmin = pvr2_ctrl_get_min(hcp);
479 lmax = pvr2_ctrl_get_max(hcp);
480 pvr2_ctrl_get_def(hcp, &ldef);
487 lmin = pvr2_ctrl_get_min(vcp);
488 lmax = pvr2_ctrl_get_max(vcp);
489 pvr2_ctrl_get_def(vcp, &ldef);
497 memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
498 sizeof(struct v4l2_format));
499 vf->fmt.pix.width = w;
500 vf->fmt.pix.height = h;
504 static int pvr2_s_fmt_vid_cap(struct file *file, void *priv, struct v4l2_format *vf)
506 struct pvr2_v4l2_fh *fh = file->private_data;
507 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
508 struct pvr2_ctrl *hcp, *vcp;
509 int ret = pvr2_try_fmt_vid_cap(file, fh, vf);
513 hcp = pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_HRES);
514 vcp = pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_VRES);
515 pvr2_ctrl_set_value(hcp, vf->fmt.pix.width);
516 pvr2_ctrl_set_value(vcp, vf->fmt.pix.height);
520 static int pvr2_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
522 struct pvr2_v4l2_fh *fh = file->private_data;
523 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
524 struct pvr2_v4l2_dev *pdi = fh->pdi;
527 if (!fh->pdi->stream) {
528 /* No stream defined for this node. This means
529 that we're not currently allowed to stream from
533 ret = pvr2_hdw_set_stream_type(hdw, pdi->config);
536 return pvr2_hdw_set_streaming(hdw, !0);
539 static int pvr2_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
541 struct pvr2_v4l2_fh *fh = file->private_data;
542 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
544 if (!fh->pdi->stream) {
545 /* No stream defined for this node. This means
546 that we're not currently allowed to stream from
550 return pvr2_hdw_set_streaming(hdw, 0);
553 static int pvr2_queryctrl(struct file *file, void *priv,
554 struct v4l2_queryctrl *vc)
556 struct pvr2_v4l2_fh *fh = file->private_data;
557 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
558 struct pvr2_ctrl *cptr;
561 if (vc->id & V4L2_CTRL_FLAG_NEXT_CTRL) {
562 cptr = pvr2_hdw_get_ctrl_nextv4l(
563 hdw, (vc->id & ~V4L2_CTRL_FLAG_NEXT_CTRL));
565 vc->id = pvr2_ctrl_get_v4lid(cptr);
567 cptr = pvr2_hdw_get_ctrl_v4l(hdw, vc->id);
570 pvr2_trace(PVR2_TRACE_V4LIOCTL,
571 "QUERYCTRL id=0x%x not implemented here",
576 pvr2_trace(PVR2_TRACE_V4LIOCTL,
577 "QUERYCTRL id=0x%x mapping name=%s (%s)",
578 vc->id, pvr2_ctrl_get_name(cptr),
579 pvr2_ctrl_get_desc(cptr));
580 strlcpy(vc->name, pvr2_ctrl_get_desc(cptr), sizeof(vc->name));
581 vc->flags = pvr2_ctrl_get_v4lflags(cptr);
582 pvr2_ctrl_get_def(cptr, &val);
583 vc->default_value = val;
584 switch (pvr2_ctrl_get_type(cptr)) {
586 vc->type = V4L2_CTRL_TYPE_MENU;
588 vc->maximum = pvr2_ctrl_get_cnt(cptr) - 1;
592 vc->type = V4L2_CTRL_TYPE_BOOLEAN;
598 vc->type = V4L2_CTRL_TYPE_INTEGER;
599 vc->minimum = pvr2_ctrl_get_min(cptr);
600 vc->maximum = pvr2_ctrl_get_max(cptr);
604 pvr2_trace(PVR2_TRACE_V4LIOCTL,
605 "QUERYCTRL id=0x%x name=%s not mappable",
606 vc->id, pvr2_ctrl_get_name(cptr));
612 static int pvr2_querymenu(struct file *file, void *priv, struct v4l2_querymenu *vm)
614 struct pvr2_v4l2_fh *fh = file->private_data;
615 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
616 unsigned int cnt = 0;
619 ret = pvr2_ctrl_get_valname(pvr2_hdw_get_ctrl_v4l(hdw, vm->id),
621 vm->name, sizeof(vm->name) - 1,
627 static int pvr2_g_ctrl(struct file *file, void *priv, struct v4l2_control *vc)
629 struct pvr2_v4l2_fh *fh = file->private_data;
630 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
634 ret = pvr2_ctrl_get_value(pvr2_hdw_get_ctrl_v4l(hdw, vc->id),
640 static int pvr2_s_ctrl(struct file *file, void *priv, struct v4l2_control *vc)
642 struct pvr2_v4l2_fh *fh = file->private_data;
643 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
645 return pvr2_ctrl_set_value(pvr2_hdw_get_ctrl_v4l(hdw, vc->id),
649 static int pvr2_g_ext_ctrls(struct file *file, void *priv,
650 struct v4l2_ext_controls *ctls)
652 struct pvr2_v4l2_fh *fh = file->private_data;
653 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
654 struct v4l2_ext_control *ctrl;
660 for (idx = 0; idx < ctls->count; idx++) {
661 ctrl = ctls->controls + idx;
662 ret = pvr2_ctrl_get_value(
663 pvr2_hdw_get_ctrl_v4l(hdw, ctrl->id), &val);
665 ctls->error_idx = idx;
668 /* Ensure that if read as a 64 bit value, the user
669 will still get a hopefully sane value */
676 static int pvr2_s_ext_ctrls(struct file *file, void *priv,
677 struct v4l2_ext_controls *ctls)
679 struct pvr2_v4l2_fh *fh = file->private_data;
680 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
681 struct v4l2_ext_control *ctrl;
686 for (idx = 0; idx < ctls->count; idx++) {
687 ctrl = ctls->controls + idx;
688 ret = pvr2_ctrl_set_value(
689 pvr2_hdw_get_ctrl_v4l(hdw, ctrl->id),
692 ctls->error_idx = idx;
699 static int pvr2_try_ext_ctrls(struct file *file, void *priv,
700 struct v4l2_ext_controls *ctls)
702 struct pvr2_v4l2_fh *fh = file->private_data;
703 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
704 struct v4l2_ext_control *ctrl;
705 struct pvr2_ctrl *pctl;
708 /* For the moment just validate that the requested control
710 for (idx = 0; idx < ctls->count; idx++) {
711 ctrl = ctls->controls + idx;
712 pctl = pvr2_hdw_get_ctrl_v4l(hdw, ctrl->id);
714 ctls->error_idx = idx;
721 static int pvr2_cropcap(struct file *file, void *priv, struct v4l2_cropcap *cap)
723 struct pvr2_v4l2_fh *fh = file->private_data;
724 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
727 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
729 ret = pvr2_hdw_get_cropcap(hdw, cap);
730 cap->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; /* paranoia */
734 static int pvr2_g_crop(struct file *file, void *priv, struct v4l2_crop *crop)
736 struct pvr2_v4l2_fh *fh = file->private_data;
737 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
741 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
743 ret = pvr2_ctrl_get_value(
744 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPL), &val);
748 ret = pvr2_ctrl_get_value(
749 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPT), &val);
753 ret = pvr2_ctrl_get_value(
754 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPW), &val);
758 ret = pvr2_ctrl_get_value(
759 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPH), &val);
762 crop->c.height = val;
766 static int pvr2_s_crop(struct file *file, void *priv, const struct v4l2_crop *crop)
768 struct pvr2_v4l2_fh *fh = file->private_data;
769 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
772 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
774 ret = pvr2_ctrl_set_value(
775 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPL),
779 ret = pvr2_ctrl_set_value(
780 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPT),
784 ret = pvr2_ctrl_set_value(
785 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPW),
789 ret = pvr2_ctrl_set_value(
790 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPH),
797 static int pvr2_log_status(struct file *file, void *priv)
799 struct pvr2_v4l2_fh *fh = file->private_data;
800 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
802 pvr2_hdw_trigger_module_log(hdw);
806 static const struct v4l2_ioctl_ops pvr2_ioctl_ops = {
807 .vidioc_querycap = pvr2_querycap,
808 .vidioc_g_priority = pvr2_g_priority,
809 .vidioc_s_priority = pvr2_s_priority,
810 .vidioc_s_audio = pvr2_s_audio,
811 .vidioc_g_audio = pvr2_g_audio,
812 .vidioc_enumaudio = pvr2_enumaudio,
813 .vidioc_enum_input = pvr2_enum_input,
814 .vidioc_cropcap = pvr2_cropcap,
815 .vidioc_s_crop = pvr2_s_crop,
816 .vidioc_g_crop = pvr2_g_crop,
817 .vidioc_g_input = pvr2_g_input,
818 .vidioc_s_input = pvr2_s_input,
819 .vidioc_g_frequency = pvr2_g_frequency,
820 .vidioc_s_frequency = pvr2_s_frequency,
821 .vidioc_s_tuner = pvr2_s_tuner,
822 .vidioc_g_tuner = pvr2_g_tuner,
823 .vidioc_g_std = pvr2_g_std,
824 .vidioc_s_std = pvr2_s_std,
825 .vidioc_querystd = pvr2_querystd,
826 .vidioc_log_status = pvr2_log_status,
827 .vidioc_enum_fmt_vid_cap = pvr2_enum_fmt_vid_cap,
828 .vidioc_g_fmt_vid_cap = pvr2_g_fmt_vid_cap,
829 .vidioc_s_fmt_vid_cap = pvr2_s_fmt_vid_cap,
830 .vidioc_try_fmt_vid_cap = pvr2_try_fmt_vid_cap,
831 .vidioc_streamon = pvr2_streamon,
832 .vidioc_streamoff = pvr2_streamoff,
833 .vidioc_queryctrl = pvr2_queryctrl,
834 .vidioc_querymenu = pvr2_querymenu,
835 .vidioc_g_ctrl = pvr2_g_ctrl,
836 .vidioc_s_ctrl = pvr2_s_ctrl,
837 .vidioc_g_ext_ctrls = pvr2_g_ext_ctrls,
838 .vidioc_s_ext_ctrls = pvr2_s_ext_ctrls,
839 .vidioc_try_ext_ctrls = pvr2_try_ext_ctrls,
842 static void pvr2_v4l2_dev_destroy(struct pvr2_v4l2_dev *dip)
844 struct pvr2_hdw *hdw = dip->v4lp->channel.mc_head->hdw;
845 enum pvr2_config cfg = dip->config;
849 /* Construct the unregistration message *before* we actually
850 perform the unregistration step. By doing it this way we don't
851 have to worry about potentially touching deleted resources. */
852 mcnt = scnprintf(msg, sizeof(msg) - 1,
853 "pvrusb2: unregistered device %s [%s]",
854 video_device_node_name(&dip->devbase),
855 pvr2_config_get_name(cfg));
858 pvr2_hdw_v4l_store_minor_number(hdw,dip->minor_type,-1);
864 /* Actual deallocation happens later when all internal references
866 video_unregister_device(&dip->devbase);
868 printk(KERN_INFO "%s\n", msg);
873 static void pvr2_v4l2_dev_disassociate_parent(struct pvr2_v4l2_dev *dip)
876 if (!dip->devbase.v4l2_dev->dev) return;
877 dip->devbase.v4l2_dev->dev = NULL;
878 device_move(&dip->devbase.dev, NULL, DPM_ORDER_NONE);
882 static void pvr2_v4l2_destroy_no_lock(struct pvr2_v4l2 *vp)
885 pvr2_v4l2_dev_destroy(vp->dev_video);
886 vp->dev_video = NULL;
889 pvr2_v4l2_dev_destroy(vp->dev_radio);
890 vp->dev_radio = NULL;
893 pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr2_v4l2 id=%p",vp);
894 pvr2_channel_done(&vp->channel);
899 static void pvr2_video_device_release(struct video_device *vdev)
901 struct pvr2_v4l2_dev *dev;
902 dev = container_of(vdev,struct pvr2_v4l2_dev,devbase);
907 static void pvr2_v4l2_internal_check(struct pvr2_channel *chp)
909 struct pvr2_v4l2 *vp;
910 vp = container_of(chp,struct pvr2_v4l2,channel);
911 if (!vp->channel.mc_head->disconnect_flag) return;
912 pvr2_v4l2_dev_disassociate_parent(vp->dev_video);
913 pvr2_v4l2_dev_disassociate_parent(vp->dev_radio);
914 if (vp->vfirst) return;
915 pvr2_v4l2_destroy_no_lock(vp);
919 static long pvr2_v4l2_ioctl(struct file *file,
920 unsigned int cmd, unsigned long arg)
923 struct pvr2_v4l2_fh *fh = file->private_data;
924 struct pvr2_v4l2 *vp = fh->vhead;
925 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
928 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL)
929 v4l_printk_ioctl(pvr2_hdw_get_driver_name(hdw), cmd);
931 if (!pvr2_hdw_dev_ok(hdw)) {
932 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
933 "ioctl failed - bad or no context");
943 case VIDIOC_S_FREQUENCY:
944 ret = v4l2_prio_check(&vp->prio, fh->prio);
949 ret = video_ioctl2(file, cmd, arg);
951 pvr2_hdw_commit_ctl(hdw);
954 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
955 pvr2_trace(PVR2_TRACE_V4LIOCTL,
956 "pvr2_v4l2_do_ioctl failure, ret=%ld"
957 " command was:", ret);
958 v4l_printk_ioctl(pvr2_hdw_get_driver_name(hdw), cmd);
961 pvr2_trace(PVR2_TRACE_V4LIOCTL,
962 "pvr2_v4l2_do_ioctl complete, ret=%ld (0x%lx)",
970 static int pvr2_v4l2_release(struct file *file)
972 struct pvr2_v4l2_fh *fhp = file->private_data;
973 struct pvr2_v4l2 *vp = fhp->vhead;
974 struct pvr2_hdw *hdw = fhp->channel.mc_head->hdw;
976 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_release");
979 struct pvr2_stream *sp;
980 pvr2_hdw_set_streaming(hdw,0);
981 sp = pvr2_ioread_get_stream(fhp->rhp);
982 if (sp) pvr2_stream_set_callback(sp,NULL,NULL);
983 pvr2_ioread_destroy(fhp->rhp);
987 v4l2_prio_close(&vp->prio, fhp->prio);
988 file->private_data = NULL;
991 fhp->vnext->vprev = fhp->vprev;
993 vp->vlast = fhp->vprev;
996 fhp->vprev->vnext = fhp->vnext;
998 vp->vfirst = fhp->vnext;
1003 pvr2_channel_done(&fhp->channel);
1004 pvr2_trace(PVR2_TRACE_STRUCT,
1005 "Destroying pvr_v4l2_fh id=%p",fhp);
1006 if (fhp->input_map) {
1007 kfree(fhp->input_map);
1008 fhp->input_map = NULL;
1011 if (vp->channel.mc_head->disconnect_flag && !vp->vfirst) {
1012 pvr2_v4l2_destroy_no_lock(vp);
1018 static int pvr2_v4l2_open(struct file *file)
1020 struct pvr2_v4l2_dev *dip; /* Our own context pointer */
1021 struct pvr2_v4l2_fh *fhp;
1022 struct pvr2_v4l2 *vp;
1023 struct pvr2_hdw *hdw;
1024 unsigned int input_mask = 0;
1025 unsigned int input_cnt,idx;
1028 dip = container_of(video_devdata(file),struct pvr2_v4l2_dev,devbase);
1031 hdw = vp->channel.hdw;
1033 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_open");
1035 if (!pvr2_hdw_dev_ok(hdw)) {
1036 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,
1037 "pvr2_v4l2_open: hardware not ready");
1041 fhp = kzalloc(sizeof(*fhp),GFP_KERNEL);
1046 init_waitqueue_head(&fhp->wait_data);
1049 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr_v4l2_fh id=%p",fhp);
1050 pvr2_channel_init(&fhp->channel,vp->channel.mc_head);
1052 if (dip->v4l_type == VFL_TYPE_RADIO) {
1053 /* Opening device as a radio, legal input selection subset
1054 is just the radio. */
1055 input_mask = (1 << PVR2_CVAL_INPUT_RADIO);
1057 /* Opening the main V4L device, legal input selection
1058 subset includes all analog inputs. */
1059 input_mask = ((1 << PVR2_CVAL_INPUT_RADIO) |
1060 (1 << PVR2_CVAL_INPUT_TV) |
1061 (1 << PVR2_CVAL_INPUT_COMPOSITE) |
1062 (1 << PVR2_CVAL_INPUT_SVIDEO));
1064 ret = pvr2_channel_limit_inputs(&fhp->channel,input_mask);
1066 pvr2_channel_done(&fhp->channel);
1067 pvr2_trace(PVR2_TRACE_STRUCT,
1068 "Destroying pvr_v4l2_fh id=%p (input mask error)",
1075 input_mask &= pvr2_hdw_get_input_available(hdw);
1077 for (idx = 0; idx < (sizeof(input_mask) << 3); idx++) {
1078 if (input_mask & (1 << idx)) input_cnt++;
1080 fhp->input_cnt = input_cnt;
1081 fhp->input_map = kzalloc(input_cnt,GFP_KERNEL);
1082 if (!fhp->input_map) {
1083 pvr2_channel_done(&fhp->channel);
1084 pvr2_trace(PVR2_TRACE_STRUCT,
1085 "Destroying pvr_v4l2_fh id=%p (input map failure)",
1091 for (idx = 0; idx < (sizeof(input_mask) << 3); idx++) {
1092 if (!(input_mask & (1 << idx))) continue;
1093 fhp->input_map[input_cnt++] = idx;
1097 fhp->vprev = vp->vlast;
1099 vp->vlast->vnext = fhp;
1107 file->private_data = fhp;
1108 v4l2_prio_open(&vp->prio, &fhp->prio);
1110 fhp->fw_mode_flag = pvr2_hdw_cpufw_get_enabled(hdw);
1116 static void pvr2_v4l2_notify(struct pvr2_v4l2_fh *fhp)
1118 wake_up(&fhp->wait_data);
1121 static int pvr2_v4l2_iosetup(struct pvr2_v4l2_fh *fh)
1124 struct pvr2_stream *sp;
1125 struct pvr2_hdw *hdw;
1126 if (fh->rhp) return 0;
1128 if (!fh->pdi->stream) {
1129 /* No stream defined for this node. This means that we're
1130 not currently allowed to stream from this node. */
1134 /* First read() attempt. Try to claim the stream and start
1136 if ((ret = pvr2_channel_claim_stream(&fh->channel,
1137 fh->pdi->stream)) != 0) {
1138 /* Someone else must already have it */
1142 fh->rhp = pvr2_channel_create_mpeg_stream(fh->pdi->stream);
1144 pvr2_channel_claim_stream(&fh->channel,NULL);
1148 hdw = fh->channel.mc_head->hdw;
1149 sp = fh->pdi->stream->stream;
1150 pvr2_stream_set_callback(sp,(pvr2_stream_callback)pvr2_v4l2_notify,fh);
1151 pvr2_hdw_set_stream_type(hdw,fh->pdi->config);
1152 if ((ret = pvr2_hdw_set_streaming(hdw,!0)) < 0) return ret;
1153 return pvr2_ioread_set_enabled(fh->rhp,!0);
1157 static ssize_t pvr2_v4l2_read(struct file *file,
1158 char __user *buff, size_t count, loff_t *ppos)
1160 struct pvr2_v4l2_fh *fh = file->private_data;
1163 if (fh->fw_mode_flag) {
1164 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
1168 unsigned int offs = *ppos;
1170 tbuf = kmalloc(PAGE_SIZE,GFP_KERNEL);
1171 if (!tbuf) return -ENOMEM;
1175 if (c1 > PAGE_SIZE) c1 = PAGE_SIZE;
1176 c2 = pvr2_hdw_cpufw_get(hdw,offs,tbuf,c1);
1182 if (copy_to_user(buff,tbuf,c2)) {
1197 ret = pvr2_v4l2_iosetup(fh);
1204 ret = pvr2_ioread_read(fh->rhp,buff,count);
1205 if (ret >= 0) break;
1206 if (ret != -EAGAIN) break;
1207 if (file->f_flags & O_NONBLOCK) break;
1208 /* Doing blocking I/O. Wait here. */
1209 ret = wait_event_interruptible(
1211 pvr2_ioread_avail(fh->rhp) >= 0);
1219 static unsigned int pvr2_v4l2_poll(struct file *file, poll_table *wait)
1221 unsigned int mask = 0;
1222 struct pvr2_v4l2_fh *fh = file->private_data;
1225 if (fh->fw_mode_flag) {
1226 mask |= POLLIN | POLLRDNORM;
1231 ret = pvr2_v4l2_iosetup(fh);
1232 if (ret) return POLLERR;
1235 poll_wait(file,&fh->wait_data,wait);
1237 if (pvr2_ioread_avail(fh->rhp) >= 0) {
1238 mask |= POLLIN | POLLRDNORM;
1245 static const struct v4l2_file_operations vdev_fops = {
1246 .owner = THIS_MODULE,
1247 .open = pvr2_v4l2_open,
1248 .release = pvr2_v4l2_release,
1249 .read = pvr2_v4l2_read,
1250 .ioctl = pvr2_v4l2_ioctl,
1251 .poll = pvr2_v4l2_poll,
1255 static struct video_device vdev_template = {
1260 static void pvr2_v4l2_dev_init(struct pvr2_v4l2_dev *dip,
1261 struct pvr2_v4l2 *vp,
1266 struct pvr2_hdw *hdw;
1270 hdw = vp->channel.mc_head->hdw;
1271 dip->v4l_type = v4l_type;
1273 case VFL_TYPE_GRABBER:
1274 dip->stream = &vp->channel.mc_head->video_stream;
1275 dip->config = pvr2_config_mpeg;
1276 dip->minor_type = pvr2_v4l_type_video;
1279 pr_err(KBUILD_MODNAME
1280 ": Failed to set up pvrusb2 v4l video dev"
1281 " due to missing stream instance\n");
1286 dip->config = pvr2_config_vbi;
1287 dip->minor_type = pvr2_v4l_type_vbi;
1290 case VFL_TYPE_RADIO:
1291 dip->stream = &vp->channel.mc_head->video_stream;
1292 dip->config = pvr2_config_mpeg;
1293 dip->minor_type = pvr2_v4l_type_radio;
1297 /* Bail out (this should be impossible) */
1298 pr_err(KBUILD_MODNAME ": Failed to set up pvrusb2 v4l dev"
1299 " due to unrecognized config\n");
1303 dip->devbase = vdev_template;
1304 dip->devbase.release = pvr2_video_device_release;
1305 dip->devbase.ioctl_ops = &pvr2_ioctl_ops;
1308 pvr2_ctrl_get_value(
1309 pvr2_hdw_get_ctrl_by_id(hdw,
1310 PVR2_CID_STDAVAIL), &val);
1311 dip->devbase.tvnorms = (v4l2_std_id)val;
1315 unit_number = pvr2_hdw_get_unit_number(hdw);
1316 if (nr_ptr && (unit_number >= 0) && (unit_number < PVR_NUM)) {
1317 mindevnum = nr_ptr[unit_number];
1319 pvr2_hdw_set_v4l2_dev(hdw, &dip->devbase);
1320 if ((video_register_device(&dip->devbase,
1321 dip->v4l_type, mindevnum) < 0) &&
1322 (video_register_device(&dip->devbase,
1323 dip->v4l_type, -1) < 0)) {
1324 pr_err(KBUILD_MODNAME
1325 ": Failed to register pvrusb2 v4l device\n");
1328 printk(KERN_INFO "pvrusb2: registered device %s [%s]\n",
1329 video_device_node_name(&dip->devbase),
1330 pvr2_config_get_name(dip->config));
1332 pvr2_hdw_v4l_store_minor_number(hdw,
1333 dip->minor_type,dip->devbase.minor);
1337 struct pvr2_v4l2 *pvr2_v4l2_create(struct pvr2_context *mnp)
1339 struct pvr2_v4l2 *vp;
1341 vp = kzalloc(sizeof(*vp),GFP_KERNEL);
1343 pvr2_channel_init(&vp->channel,mnp);
1344 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr2_v4l2 id=%p",vp);
1346 vp->channel.check_func = pvr2_v4l2_internal_check;
1348 /* register streams */
1349 vp->dev_video = kzalloc(sizeof(*vp->dev_video),GFP_KERNEL);
1350 if (!vp->dev_video) goto fail;
1351 pvr2_v4l2_dev_init(vp->dev_video,vp,VFL_TYPE_GRABBER);
1352 if (pvr2_hdw_get_input_available(vp->channel.mc_head->hdw) &
1353 (1 << PVR2_CVAL_INPUT_RADIO)) {
1354 vp->dev_radio = kzalloc(sizeof(*vp->dev_radio),GFP_KERNEL);
1355 if (!vp->dev_radio) goto fail;
1356 pvr2_v4l2_dev_init(vp->dev_radio,vp,VFL_TYPE_RADIO);
1361 pvr2_trace(PVR2_TRACE_STRUCT,"Failure creating pvr2_v4l2 id=%p",vp);
1362 pvr2_v4l2_destroy_no_lock(vp);
1367 Stuff for Emacs to see, in order to encourage consistent editing style:
1368 *** Local Variables: ***
1370 *** fill-column: 75 ***
1371 *** tab-width: 8 ***
1372 *** c-basic-offset: 8 ***