4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
5 * Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
18 #include <linux/kernel.h>
19 #include <linux/slab.h>
20 #include "pvrusb2-context.h"
21 #include "pvrusb2-hdw.h"
23 #include "pvrusb2-debug.h"
24 #include "pvrusb2-v4l2.h"
25 #include "pvrusb2-ioread.h"
26 #include <linux/videodev2.h>
27 #include <linux/module.h>
28 #include <media/v4l2-dev.h>
29 #include <media/v4l2-device.h>
30 #include <media/v4l2-fh.h>
31 #include <media/v4l2-common.h>
32 #include <media/v4l2-ioctl.h>
38 struct pvr2_v4l2_dev {
39 struct video_device devbase; /* MUST be first! */
40 struct pvr2_v4l2 *v4lp;
41 struct pvr2_context_stream *stream;
42 /* Information about this device: */
43 enum pvr2_config config; /* Expected stream format */
44 int v4l_type; /* V4L defined type for this device node */
45 enum pvr2_v4l_type minor_type; /* pvr2-understood minor device type */
50 struct pvr2_channel channel;
51 struct pvr2_v4l2_dev *pdi;
52 struct pvr2_ioread *rhp;
54 wait_queue_head_t wait_data;
56 /* Map contiguous ordinal value to input id */
57 unsigned char *input_map;
58 unsigned int input_cnt;
62 struct pvr2_channel channel;
64 /* streams - Note that these must be separately, individually,
65 * allocated pointers. This is because the v4l core is going to
66 * manage their deletion - separately, individually... */
67 struct pvr2_v4l2_dev *dev_video;
68 struct pvr2_v4l2_dev *dev_radio;
71 static int video_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
72 module_param_array(video_nr, int, NULL, 0444);
73 MODULE_PARM_DESC(video_nr, "Offset for device's video dev minor");
74 static int radio_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
75 module_param_array(radio_nr, int, NULL, 0444);
76 MODULE_PARM_DESC(radio_nr, "Offset for device's radio dev minor");
77 static int vbi_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
78 module_param_array(vbi_nr, int, NULL, 0444);
79 MODULE_PARM_DESC(vbi_nr, "Offset for device's vbi dev minor");
81 #define PVR_FORMAT_PIX 0
82 #define PVR_FORMAT_VBI 1
84 static struct v4l2_format pvr_format [] = {
86 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
91 .pixelformat = V4L2_PIX_FMT_MPEG,
92 .field = V4L2_FIELD_INTERLACED,
93 /* FIXME : Don't know what to put here... */
94 .sizeimage = 32 * 1024,
99 .type = V4L2_BUF_TYPE_VBI_CAPTURE,
102 .sampling_rate = 27000000,
104 .samples_per_line = 1443,
105 .sample_format = V4L2_PIX_FMT_GREY,
117 * This is part of Video 4 Linux API. These procedures handle ioctl() calls.
119 static int pvr2_querycap(struct file *file, void *priv, struct v4l2_capability *cap)
121 struct pvr2_v4l2_fh *fh = file->private_data;
122 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
124 strlcpy(cap->driver, "pvrusb2", sizeof(cap->driver));
125 strlcpy(cap->bus_info, pvr2_hdw_get_bus_info(hdw),
126 sizeof(cap->bus_info));
127 strlcpy(cap->card, pvr2_hdw_get_desc(hdw), sizeof(cap->card));
128 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_TUNER |
129 V4L2_CAP_AUDIO | V4L2_CAP_RADIO |
130 V4L2_CAP_READWRITE | V4L2_CAP_DEVICE_CAPS;
131 switch (fh->pdi->devbase.vfl_type) {
132 case VFL_TYPE_GRABBER:
133 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_AUDIO;
136 cap->device_caps = V4L2_CAP_RADIO;
141 cap->device_caps |= V4L2_CAP_TUNER | V4L2_CAP_READWRITE;
145 static int pvr2_g_std(struct file *file, void *priv, v4l2_std_id *std)
147 struct pvr2_v4l2_fh *fh = file->private_data;
148 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
152 ret = pvr2_ctrl_get_value(
153 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_STDCUR), &val);
158 static int pvr2_s_std(struct file *file, void *priv, v4l2_std_id std)
160 struct pvr2_v4l2_fh *fh = file->private_data;
161 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
164 ret = pvr2_ctrl_set_value(
165 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_STDCUR), std);
166 pvr2_hdw_commit_ctl(hdw);
170 static int pvr2_querystd(struct file *file, void *priv, v4l2_std_id *std)
172 struct pvr2_v4l2_fh *fh = file->private_data;
173 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
177 ret = pvr2_ctrl_get_value(
178 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_STDDETECT), &val);
183 static int pvr2_enum_input(struct file *file, void *priv, struct v4l2_input *vi)
185 struct pvr2_v4l2_fh *fh = file->private_data;
186 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
187 struct pvr2_ctrl *cptr;
188 struct v4l2_input tmp;
192 cptr = pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_INPUT);
194 memset(&tmp, 0, sizeof(tmp));
195 tmp.index = vi->index;
196 if (vi->index >= fh->input_cnt)
198 val = fh->input_map[vi->index];
200 case PVR2_CVAL_INPUT_TV:
201 case PVR2_CVAL_INPUT_DTV:
202 case PVR2_CVAL_INPUT_RADIO:
203 tmp.type = V4L2_INPUT_TYPE_TUNER;
205 case PVR2_CVAL_INPUT_SVIDEO:
206 case PVR2_CVAL_INPUT_COMPOSITE:
207 tmp.type = V4L2_INPUT_TYPE_CAMERA;
214 pvr2_ctrl_get_valname(cptr, val,
215 tmp.name, sizeof(tmp.name) - 1, &cnt);
218 /* Don't bother with audioset, since this driver currently
219 always switches the audio whenever the video is
222 /* Handling std is a tougher problem. It doesn't make
223 sense in cases where a device might be multi-standard.
224 We could just copy out the current value for the
225 standard, but it can change over time. For now just
231 static int pvr2_g_input(struct file *file, void *priv, unsigned int *i)
233 struct pvr2_v4l2_fh *fh = file->private_data;
234 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
236 struct pvr2_ctrl *cptr;
240 cptr = pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_INPUT);
242 ret = pvr2_ctrl_get_value(cptr, &val);
244 for (idx = 0; idx < fh->input_cnt; idx++) {
245 if (fh->input_map[idx] == val) {
253 static int pvr2_s_input(struct file *file, void *priv, unsigned int inp)
255 struct pvr2_v4l2_fh *fh = file->private_data;
256 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
259 if (inp >= fh->input_cnt)
261 ret = pvr2_ctrl_set_value(
262 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_INPUT),
264 pvr2_hdw_commit_ctl(hdw);
268 static int pvr2_enumaudio(struct file *file, void *priv, struct v4l2_audio *vin)
270 /* pkt: FIXME: We are returning one "fake" input here
271 which could very well be called "whatever_we_like".
272 This is for apps that want to see an audio input
273 just to feel comfortable, as well as to test if
274 it can do stereo or sth. There is actually no guarantee
275 that the actual audio input cannot change behind the app's
276 back, but most applications should not mind that either.
278 Hopefully, mplayer people will work with us on this (this
279 whole mess is to support mplayer pvr://), or Hans will come
280 up with a more standard way to say "we have inputs but we
281 don 't want you to change them independent of video" which
287 strncpy(vin->name, "PVRUSB2 Audio", 14);
288 vin->capability = V4L2_AUDCAP_STEREO;
292 static int pvr2_g_audio(struct file *file, void *priv, struct v4l2_audio *vin)
294 /* pkt: FIXME: see above comment (VIDIOC_ENUMAUDIO) */
296 strncpy(vin->name, "PVRUSB2 Audio", 14);
297 vin->capability = V4L2_AUDCAP_STEREO;
301 static int pvr2_s_audio(struct file *file, void *priv, const struct v4l2_audio *vout)
308 static int pvr2_g_tuner(struct file *file, void *priv, struct v4l2_tuner *vt)
310 struct pvr2_v4l2_fh *fh = file->private_data;
311 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
314 return -EINVAL; /* Only answer for the 1st tuner */
316 pvr2_hdw_execute_tuner_poll(hdw);
317 return pvr2_hdw_get_tuner_status(hdw, vt);
320 static int pvr2_s_tuner(struct file *file, void *priv, const struct v4l2_tuner *vt)
322 struct pvr2_v4l2_fh *fh = file->private_data;
323 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
329 ret = pvr2_ctrl_set_value(
330 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_AUDIOMODE),
332 pvr2_hdw_commit_ctl(hdw);
336 static int pvr2_s_frequency(struct file *file, void *priv, const struct v4l2_frequency *vf)
338 struct pvr2_v4l2_fh *fh = file->private_data;
339 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
341 struct v4l2_tuner vt;
343 struct pvr2_ctrl *ctrlp;
346 ret = pvr2_hdw_get_tuner_status(hdw, &vt);
349 ctrlp = pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_INPUT);
350 ret = pvr2_ctrl_get_value(ctrlp, &cur_input);
353 if (vf->type == V4L2_TUNER_RADIO) {
354 if (cur_input != PVR2_CVAL_INPUT_RADIO)
355 pvr2_ctrl_set_value(ctrlp, PVR2_CVAL_INPUT_RADIO);
357 if (cur_input == PVR2_CVAL_INPUT_RADIO)
358 pvr2_ctrl_set_value(ctrlp, PVR2_CVAL_INPUT_TV);
361 if (vt.capability & V4L2_TUNER_CAP_LOW)
365 ret = pvr2_ctrl_set_value(
366 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),fv);
367 pvr2_hdw_commit_ctl(hdw);
371 static int pvr2_g_frequency(struct file *file, void *priv, struct v4l2_frequency *vf)
373 struct pvr2_v4l2_fh *fh = file->private_data;
374 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
377 struct v4l2_tuner vt;
380 ret = pvr2_hdw_get_tuner_status(hdw, &vt);
383 ret = pvr2_ctrl_get_value(
384 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_FREQUENCY),
389 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_INPUT),
391 if (cur_input == PVR2_CVAL_INPUT_RADIO)
392 vf->type = V4L2_TUNER_RADIO;
394 vf->type = V4L2_TUNER_ANALOG_TV;
395 if (vt.capability & V4L2_TUNER_CAP_LOW)
396 val = (val * 2) / 125;
403 static int pvr2_enum_fmt_vid_cap(struct file *file, void *priv, struct v4l2_fmtdesc *fd)
405 /* Only one format is supported: MPEG. */
409 fd->pixelformat = V4L2_PIX_FMT_MPEG;
413 static int pvr2_g_fmt_vid_cap(struct file *file, void *priv, struct v4l2_format *vf)
415 struct pvr2_v4l2_fh *fh = file->private_data;
416 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
419 memcpy(vf, &pvr_format[PVR_FORMAT_PIX], sizeof(struct v4l2_format));
422 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_HRES),
424 vf->fmt.pix.width = val;
427 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_VRES),
429 vf->fmt.pix.height = val;
433 static int pvr2_try_fmt_vid_cap(struct file *file, void *priv, struct v4l2_format *vf)
435 struct pvr2_v4l2_fh *fh = file->private_data;
436 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
437 int lmin, lmax, ldef;
438 struct pvr2_ctrl *hcp, *vcp;
439 int h = vf->fmt.pix.height;
440 int w = vf->fmt.pix.width;
442 hcp = pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_HRES);
443 vcp = pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_VRES);
445 lmin = pvr2_ctrl_get_min(hcp);
446 lmax = pvr2_ctrl_get_max(hcp);
447 pvr2_ctrl_get_def(hcp, &ldef);
454 lmin = pvr2_ctrl_get_min(vcp);
455 lmax = pvr2_ctrl_get_max(vcp);
456 pvr2_ctrl_get_def(vcp, &ldef);
464 memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
465 sizeof(struct v4l2_format));
466 vf->fmt.pix.width = w;
467 vf->fmt.pix.height = h;
471 static int pvr2_s_fmt_vid_cap(struct file *file, void *priv, struct v4l2_format *vf)
473 struct pvr2_v4l2_fh *fh = file->private_data;
474 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
475 struct pvr2_ctrl *hcp, *vcp;
476 int ret = pvr2_try_fmt_vid_cap(file, fh, vf);
480 hcp = pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_HRES);
481 vcp = pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_VRES);
482 pvr2_ctrl_set_value(hcp, vf->fmt.pix.width);
483 pvr2_ctrl_set_value(vcp, vf->fmt.pix.height);
484 pvr2_hdw_commit_ctl(hdw);
488 static int pvr2_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
490 struct pvr2_v4l2_fh *fh = file->private_data;
491 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
492 struct pvr2_v4l2_dev *pdi = fh->pdi;
495 if (!fh->pdi->stream) {
496 /* No stream defined for this node. This means
497 that we're not currently allowed to stream from
501 ret = pvr2_hdw_set_stream_type(hdw, pdi->config);
504 return pvr2_hdw_set_streaming(hdw, !0);
507 static int pvr2_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
509 struct pvr2_v4l2_fh *fh = file->private_data;
510 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
512 if (!fh->pdi->stream) {
513 /* No stream defined for this node. This means
514 that we're not currently allowed to stream from
518 return pvr2_hdw_set_streaming(hdw, 0);
521 static int pvr2_queryctrl(struct file *file, void *priv,
522 struct v4l2_queryctrl *vc)
524 struct pvr2_v4l2_fh *fh = file->private_data;
525 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
526 struct pvr2_ctrl *cptr;
529 if (vc->id & V4L2_CTRL_FLAG_NEXT_CTRL) {
530 cptr = pvr2_hdw_get_ctrl_nextv4l(
531 hdw, (vc->id & ~V4L2_CTRL_FLAG_NEXT_CTRL));
533 vc->id = pvr2_ctrl_get_v4lid(cptr);
535 cptr = pvr2_hdw_get_ctrl_v4l(hdw, vc->id);
538 pvr2_trace(PVR2_TRACE_V4LIOCTL,
539 "QUERYCTRL id=0x%x not implemented here",
544 pvr2_trace(PVR2_TRACE_V4LIOCTL,
545 "QUERYCTRL id=0x%x mapping name=%s (%s)",
546 vc->id, pvr2_ctrl_get_name(cptr),
547 pvr2_ctrl_get_desc(cptr));
548 strlcpy(vc->name, pvr2_ctrl_get_desc(cptr), sizeof(vc->name));
549 vc->flags = pvr2_ctrl_get_v4lflags(cptr);
550 pvr2_ctrl_get_def(cptr, &val);
551 vc->default_value = val;
552 switch (pvr2_ctrl_get_type(cptr)) {
554 vc->type = V4L2_CTRL_TYPE_MENU;
556 vc->maximum = pvr2_ctrl_get_cnt(cptr) - 1;
560 vc->type = V4L2_CTRL_TYPE_BOOLEAN;
566 vc->type = V4L2_CTRL_TYPE_INTEGER;
567 vc->minimum = pvr2_ctrl_get_min(cptr);
568 vc->maximum = pvr2_ctrl_get_max(cptr);
572 pvr2_trace(PVR2_TRACE_V4LIOCTL,
573 "QUERYCTRL id=0x%x name=%s not mappable",
574 vc->id, pvr2_ctrl_get_name(cptr));
580 static int pvr2_querymenu(struct file *file, void *priv, struct v4l2_querymenu *vm)
582 struct pvr2_v4l2_fh *fh = file->private_data;
583 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
584 unsigned int cnt = 0;
587 ret = pvr2_ctrl_get_valname(pvr2_hdw_get_ctrl_v4l(hdw, vm->id),
589 vm->name, sizeof(vm->name) - 1,
595 static int pvr2_g_ctrl(struct file *file, void *priv, struct v4l2_control *vc)
597 struct pvr2_v4l2_fh *fh = file->private_data;
598 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
602 ret = pvr2_ctrl_get_value(pvr2_hdw_get_ctrl_v4l(hdw, vc->id),
608 static int pvr2_s_ctrl(struct file *file, void *priv, struct v4l2_control *vc)
610 struct pvr2_v4l2_fh *fh = file->private_data;
611 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
614 ret = pvr2_ctrl_set_value(pvr2_hdw_get_ctrl_v4l(hdw, vc->id),
616 pvr2_hdw_commit_ctl(hdw);
620 static int pvr2_g_ext_ctrls(struct file *file, void *priv,
621 struct v4l2_ext_controls *ctls)
623 struct pvr2_v4l2_fh *fh = file->private_data;
624 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
625 struct v4l2_ext_control *ctrl;
626 struct pvr2_ctrl *cptr;
632 for (idx = 0; idx < ctls->count; idx++) {
633 ctrl = ctls->controls + idx;
634 cptr = pvr2_hdw_get_ctrl_v4l(hdw, ctrl->id);
636 if (ctls->which == V4L2_CTRL_WHICH_DEF_VAL)
637 pvr2_ctrl_get_def(cptr, &val);
639 ret = pvr2_ctrl_get_value(cptr, &val);
644 ctls->error_idx = idx;
647 /* Ensure that if read as a 64 bit value, the user
648 will still get a hopefully sane value */
655 static int pvr2_s_ext_ctrls(struct file *file, void *priv,
656 struct v4l2_ext_controls *ctls)
658 struct pvr2_v4l2_fh *fh = file->private_data;
659 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
660 struct v4l2_ext_control *ctrl;
664 /* Default value cannot be changed */
665 if (ctls->which == V4L2_CTRL_WHICH_DEF_VAL)
669 for (idx = 0; idx < ctls->count; idx++) {
670 ctrl = ctls->controls + idx;
671 ret = pvr2_ctrl_set_value(
672 pvr2_hdw_get_ctrl_v4l(hdw, ctrl->id),
675 ctls->error_idx = idx;
680 pvr2_hdw_commit_ctl(hdw);
684 static int pvr2_try_ext_ctrls(struct file *file, void *priv,
685 struct v4l2_ext_controls *ctls)
687 struct pvr2_v4l2_fh *fh = file->private_data;
688 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
689 struct v4l2_ext_control *ctrl;
690 struct pvr2_ctrl *pctl;
693 /* For the moment just validate that the requested control
695 for (idx = 0; idx < ctls->count; idx++) {
696 ctrl = ctls->controls + idx;
697 pctl = pvr2_hdw_get_ctrl_v4l(hdw, ctrl->id);
699 ctls->error_idx = idx;
706 static int pvr2_cropcap(struct file *file, void *priv, struct v4l2_cropcap *cap)
708 struct pvr2_v4l2_fh *fh = file->private_data;
709 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
712 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
714 ret = pvr2_hdw_get_cropcap(hdw, cap);
715 cap->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; /* paranoia */
719 static int pvr2_g_selection(struct file *file, void *priv,
720 struct v4l2_selection *sel)
722 struct pvr2_v4l2_fh *fh = file->private_data;
723 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
724 struct v4l2_cropcap cap;
728 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
731 cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
733 switch (sel->target) {
734 case V4L2_SEL_TGT_CROP:
735 ret = pvr2_ctrl_get_value(
736 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPL), &val);
740 ret = pvr2_ctrl_get_value(
741 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPT), &val);
745 ret = pvr2_ctrl_get_value(
746 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPW), &val);
750 ret = pvr2_ctrl_get_value(
751 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPH), &val);
756 case V4L2_SEL_TGT_CROP_DEFAULT:
757 ret = pvr2_hdw_get_cropcap(hdw, &cap);
758 sel->r = cap.defrect;
760 case V4L2_SEL_TGT_CROP_BOUNDS:
761 ret = pvr2_hdw_get_cropcap(hdw, &cap);
770 static int pvr2_s_selection(struct file *file, void *priv,
771 struct v4l2_selection *sel)
773 struct pvr2_v4l2_fh *fh = file->private_data;
774 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
777 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
778 sel->target != V4L2_SEL_TGT_CROP)
780 ret = pvr2_ctrl_set_value(
781 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPL),
785 ret = pvr2_ctrl_set_value(
786 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPT),
790 ret = pvr2_ctrl_set_value(
791 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPW),
795 ret = pvr2_ctrl_set_value(
796 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPH),
799 pvr2_hdw_commit_ctl(hdw);
803 static int pvr2_log_status(struct file *file, void *priv)
805 struct pvr2_v4l2_fh *fh = file->private_data;
806 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
808 pvr2_hdw_trigger_module_log(hdw);
812 static const struct v4l2_ioctl_ops pvr2_ioctl_ops = {
813 .vidioc_querycap = pvr2_querycap,
814 .vidioc_s_audio = pvr2_s_audio,
815 .vidioc_g_audio = pvr2_g_audio,
816 .vidioc_enumaudio = pvr2_enumaudio,
817 .vidioc_enum_input = pvr2_enum_input,
818 .vidioc_cropcap = pvr2_cropcap,
819 .vidioc_s_selection = pvr2_s_selection,
820 .vidioc_g_selection = pvr2_g_selection,
821 .vidioc_g_input = pvr2_g_input,
822 .vidioc_s_input = pvr2_s_input,
823 .vidioc_g_frequency = pvr2_g_frequency,
824 .vidioc_s_frequency = pvr2_s_frequency,
825 .vidioc_s_tuner = pvr2_s_tuner,
826 .vidioc_g_tuner = pvr2_g_tuner,
827 .vidioc_g_std = pvr2_g_std,
828 .vidioc_s_std = pvr2_s_std,
829 .vidioc_querystd = pvr2_querystd,
830 .vidioc_log_status = pvr2_log_status,
831 .vidioc_enum_fmt_vid_cap = pvr2_enum_fmt_vid_cap,
832 .vidioc_g_fmt_vid_cap = pvr2_g_fmt_vid_cap,
833 .vidioc_s_fmt_vid_cap = pvr2_s_fmt_vid_cap,
834 .vidioc_try_fmt_vid_cap = pvr2_try_fmt_vid_cap,
835 .vidioc_streamon = pvr2_streamon,
836 .vidioc_streamoff = pvr2_streamoff,
837 .vidioc_queryctrl = pvr2_queryctrl,
838 .vidioc_querymenu = pvr2_querymenu,
839 .vidioc_g_ctrl = pvr2_g_ctrl,
840 .vidioc_s_ctrl = pvr2_s_ctrl,
841 .vidioc_g_ext_ctrls = pvr2_g_ext_ctrls,
842 .vidioc_s_ext_ctrls = pvr2_s_ext_ctrls,
843 .vidioc_try_ext_ctrls = pvr2_try_ext_ctrls,
846 static void pvr2_v4l2_dev_destroy(struct pvr2_v4l2_dev *dip)
848 struct pvr2_hdw *hdw = dip->v4lp->channel.mc_head->hdw;
849 enum pvr2_config cfg = dip->config;
853 /* Construct the unregistration message *before* we actually
854 perform the unregistration step. By doing it this way we don't
855 have to worry about potentially touching deleted resources. */
856 mcnt = scnprintf(msg, sizeof(msg) - 1,
857 "pvrusb2: unregistered device %s [%s]",
858 video_device_node_name(&dip->devbase),
859 pvr2_config_get_name(cfg));
862 pvr2_hdw_v4l_store_minor_number(hdw,dip->minor_type,-1);
868 /* Actual deallocation happens later when all internal references
870 video_unregister_device(&dip->devbase);
872 printk(KERN_INFO "%s\n", msg);
877 static void pvr2_v4l2_dev_disassociate_parent(struct pvr2_v4l2_dev *dip)
880 if (!dip->devbase.v4l2_dev->dev) return;
881 dip->devbase.v4l2_dev->dev = NULL;
882 device_move(&dip->devbase.dev, NULL, DPM_ORDER_NONE);
886 static void pvr2_v4l2_destroy_no_lock(struct pvr2_v4l2 *vp)
889 pvr2_v4l2_dev_destroy(vp->dev_video);
890 vp->dev_video = NULL;
893 pvr2_v4l2_dev_destroy(vp->dev_radio);
894 vp->dev_radio = NULL;
897 pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr2_v4l2 id=%p",vp);
898 pvr2_channel_done(&vp->channel);
903 static void pvr2_video_device_release(struct video_device *vdev)
905 struct pvr2_v4l2_dev *dev;
906 dev = container_of(vdev,struct pvr2_v4l2_dev,devbase);
911 static void pvr2_v4l2_internal_check(struct pvr2_channel *chp)
913 struct pvr2_v4l2 *vp;
914 vp = container_of(chp,struct pvr2_v4l2,channel);
915 if (!vp->channel.mc_head->disconnect_flag) return;
916 pvr2_v4l2_dev_disassociate_parent(vp->dev_video);
917 pvr2_v4l2_dev_disassociate_parent(vp->dev_radio);
918 if (!list_empty(&vp->dev_video->devbase.fh_list) ||
920 !list_empty(&vp->dev_radio->devbase.fh_list))) {
921 pvr2_trace(PVR2_TRACE_STRUCT,
922 "pvr2_v4l2 internal_check exit-empty id=%p", vp);
925 pvr2_v4l2_destroy_no_lock(vp);
929 static int pvr2_v4l2_release(struct file *file)
931 struct pvr2_v4l2_fh *fhp = file->private_data;
932 struct pvr2_v4l2 *vp = fhp->pdi->v4lp;
933 struct pvr2_hdw *hdw = fhp->channel.mc_head->hdw;
935 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_release");
938 struct pvr2_stream *sp;
939 pvr2_hdw_set_streaming(hdw,0);
940 sp = pvr2_ioread_get_stream(fhp->rhp);
941 if (sp) pvr2_stream_set_callback(sp,NULL,NULL);
942 pvr2_ioread_destroy(fhp->rhp);
946 v4l2_fh_del(&fhp->fh);
947 v4l2_fh_exit(&fhp->fh);
948 file->private_data = NULL;
950 pvr2_channel_done(&fhp->channel);
951 pvr2_trace(PVR2_TRACE_STRUCT,
952 "Destroying pvr_v4l2_fh id=%p",fhp);
953 if (fhp->input_map) {
954 kfree(fhp->input_map);
955 fhp->input_map = NULL;
958 if (vp->channel.mc_head->disconnect_flag &&
959 list_empty(&vp->dev_video->devbase.fh_list) &&
961 list_empty(&vp->dev_radio->devbase.fh_list))) {
962 pvr2_v4l2_destroy_no_lock(vp);
968 static int pvr2_v4l2_open(struct file *file)
970 struct pvr2_v4l2_dev *dip; /* Our own context pointer */
971 struct pvr2_v4l2_fh *fhp;
972 struct pvr2_v4l2 *vp;
973 struct pvr2_hdw *hdw;
974 unsigned int input_mask = 0;
975 unsigned int input_cnt,idx;
978 dip = container_of(video_devdata(file),struct pvr2_v4l2_dev,devbase);
981 hdw = vp->channel.hdw;
983 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_open");
985 if (!pvr2_hdw_dev_ok(hdw)) {
986 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,
987 "pvr2_v4l2_open: hardware not ready");
991 fhp = kzalloc(sizeof(*fhp),GFP_KERNEL);
996 v4l2_fh_init(&fhp->fh, &dip->devbase);
997 init_waitqueue_head(&fhp->wait_data);
1000 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr_v4l2_fh id=%p",fhp);
1001 pvr2_channel_init(&fhp->channel,vp->channel.mc_head);
1003 if (dip->v4l_type == VFL_TYPE_RADIO) {
1004 /* Opening device as a radio, legal input selection subset
1005 is just the radio. */
1006 input_mask = (1 << PVR2_CVAL_INPUT_RADIO);
1008 /* Opening the main V4L device, legal input selection
1009 subset includes all analog inputs. */
1010 input_mask = ((1 << PVR2_CVAL_INPUT_RADIO) |
1011 (1 << PVR2_CVAL_INPUT_TV) |
1012 (1 << PVR2_CVAL_INPUT_COMPOSITE) |
1013 (1 << PVR2_CVAL_INPUT_SVIDEO));
1015 ret = pvr2_channel_limit_inputs(&fhp->channel,input_mask);
1017 pvr2_channel_done(&fhp->channel);
1018 pvr2_trace(PVR2_TRACE_STRUCT,
1019 "Destroying pvr_v4l2_fh id=%p (input mask error)",
1021 v4l2_fh_exit(&fhp->fh);
1026 input_mask &= pvr2_hdw_get_input_available(hdw);
1028 for (idx = 0; idx < (sizeof(input_mask) << 3); idx++) {
1029 if (input_mask & (1 << idx)) input_cnt++;
1031 fhp->input_cnt = input_cnt;
1032 fhp->input_map = kzalloc(input_cnt,GFP_KERNEL);
1033 if (!fhp->input_map) {
1034 pvr2_channel_done(&fhp->channel);
1035 pvr2_trace(PVR2_TRACE_STRUCT,
1036 "Destroying pvr_v4l2_fh id=%p (input map failure)",
1038 v4l2_fh_exit(&fhp->fh);
1043 for (idx = 0; idx < (sizeof(input_mask) << 3); idx++) {
1044 if (!(input_mask & (1 << idx))) continue;
1045 fhp->input_map[input_cnt++] = idx;
1049 file->private_data = fhp;
1051 fhp->fw_mode_flag = pvr2_hdw_cpufw_get_enabled(hdw);
1052 v4l2_fh_add(&fhp->fh);
1058 static void pvr2_v4l2_notify(struct pvr2_v4l2_fh *fhp)
1060 wake_up(&fhp->wait_data);
1063 static int pvr2_v4l2_iosetup(struct pvr2_v4l2_fh *fh)
1066 struct pvr2_stream *sp;
1067 struct pvr2_hdw *hdw;
1068 if (fh->rhp) return 0;
1070 if (!fh->pdi->stream) {
1071 /* No stream defined for this node. This means that we're
1072 not currently allowed to stream from this node. */
1076 /* First read() attempt. Try to claim the stream and start
1078 if ((ret = pvr2_channel_claim_stream(&fh->channel,
1079 fh->pdi->stream)) != 0) {
1080 /* Someone else must already have it */
1084 fh->rhp = pvr2_channel_create_mpeg_stream(fh->pdi->stream);
1086 pvr2_channel_claim_stream(&fh->channel,NULL);
1090 hdw = fh->channel.mc_head->hdw;
1091 sp = fh->pdi->stream->stream;
1092 pvr2_stream_set_callback(sp,(pvr2_stream_callback)pvr2_v4l2_notify,fh);
1093 pvr2_hdw_set_stream_type(hdw,fh->pdi->config);
1094 if ((ret = pvr2_hdw_set_streaming(hdw,!0)) < 0) return ret;
1095 return pvr2_ioread_set_enabled(fh->rhp,!0);
1099 static ssize_t pvr2_v4l2_read(struct file *file,
1100 char __user *buff, size_t count, loff_t *ppos)
1102 struct pvr2_v4l2_fh *fh = file->private_data;
1105 if (fh->fw_mode_flag) {
1106 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
1110 unsigned int offs = *ppos;
1112 tbuf = kmalloc(PAGE_SIZE,GFP_KERNEL);
1113 if (!tbuf) return -ENOMEM;
1117 if (c1 > PAGE_SIZE) c1 = PAGE_SIZE;
1118 c2 = pvr2_hdw_cpufw_get(hdw,offs,tbuf,c1);
1124 if (copy_to_user(buff,tbuf,c2)) {
1139 ret = pvr2_v4l2_iosetup(fh);
1146 ret = pvr2_ioread_read(fh->rhp,buff,count);
1147 if (ret >= 0) break;
1148 if (ret != -EAGAIN) break;
1149 if (file->f_flags & O_NONBLOCK) break;
1150 /* Doing blocking I/O. Wait here. */
1151 ret = wait_event_interruptible(
1153 pvr2_ioread_avail(fh->rhp) >= 0);
1161 static __poll_t pvr2_v4l2_poll(struct file *file, poll_table *wait)
1164 struct pvr2_v4l2_fh *fh = file->private_data;
1167 if (fh->fw_mode_flag) {
1168 mask |= EPOLLIN | EPOLLRDNORM;
1173 ret = pvr2_v4l2_iosetup(fh);
1174 if (ret) return EPOLLERR;
1177 poll_wait(file,&fh->wait_data,wait);
1179 if (pvr2_ioread_avail(fh->rhp) >= 0) {
1180 mask |= EPOLLIN | EPOLLRDNORM;
1187 static const struct v4l2_file_operations vdev_fops = {
1188 .owner = THIS_MODULE,
1189 .open = pvr2_v4l2_open,
1190 .release = pvr2_v4l2_release,
1191 .read = pvr2_v4l2_read,
1192 .unlocked_ioctl = video_ioctl2,
1193 .poll = pvr2_v4l2_poll,
1197 static const struct video_device vdev_template = {
1202 static void pvr2_v4l2_dev_init(struct pvr2_v4l2_dev *dip,
1203 struct pvr2_v4l2 *vp,
1208 struct pvr2_hdw *hdw;
1212 hdw = vp->channel.mc_head->hdw;
1213 dip->v4l_type = v4l_type;
1215 case VFL_TYPE_GRABBER:
1216 dip->stream = &vp->channel.mc_head->video_stream;
1217 dip->config = pvr2_config_mpeg;
1218 dip->minor_type = pvr2_v4l_type_video;
1221 pr_err(KBUILD_MODNAME
1222 ": Failed to set up pvrusb2 v4l video dev due to missing stream instance\n");
1227 dip->config = pvr2_config_vbi;
1228 dip->minor_type = pvr2_v4l_type_vbi;
1231 case VFL_TYPE_RADIO:
1232 dip->stream = &vp->channel.mc_head->video_stream;
1233 dip->config = pvr2_config_mpeg;
1234 dip->minor_type = pvr2_v4l_type_radio;
1238 /* Bail out (this should be impossible) */
1239 pr_err(KBUILD_MODNAME ": Failed to set up pvrusb2 v4l dev due to unrecognized config\n");
1243 dip->devbase = vdev_template;
1244 dip->devbase.release = pvr2_video_device_release;
1245 dip->devbase.ioctl_ops = &pvr2_ioctl_ops;
1248 pvr2_ctrl_get_value(
1249 pvr2_hdw_get_ctrl_by_id(hdw,
1250 PVR2_CID_STDAVAIL), &val);
1251 dip->devbase.tvnorms = (v4l2_std_id)val;
1255 unit_number = pvr2_hdw_get_unit_number(hdw);
1256 if (nr_ptr && (unit_number >= 0) && (unit_number < PVR_NUM)) {
1257 mindevnum = nr_ptr[unit_number];
1259 pvr2_hdw_set_v4l2_dev(hdw, &dip->devbase);
1260 if ((video_register_device(&dip->devbase,
1261 dip->v4l_type, mindevnum) < 0) &&
1262 (video_register_device(&dip->devbase,
1263 dip->v4l_type, -1) < 0)) {
1264 pr_err(KBUILD_MODNAME
1265 ": Failed to register pvrusb2 v4l device\n");
1268 printk(KERN_INFO "pvrusb2: registered device %s [%s]\n",
1269 video_device_node_name(&dip->devbase),
1270 pvr2_config_get_name(dip->config));
1272 pvr2_hdw_v4l_store_minor_number(hdw,
1273 dip->minor_type,dip->devbase.minor);
1277 struct pvr2_v4l2 *pvr2_v4l2_create(struct pvr2_context *mnp)
1279 struct pvr2_v4l2 *vp;
1281 vp = kzalloc(sizeof(*vp),GFP_KERNEL);
1283 pvr2_channel_init(&vp->channel,mnp);
1284 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr2_v4l2 id=%p",vp);
1286 vp->channel.check_func = pvr2_v4l2_internal_check;
1288 /* register streams */
1289 vp->dev_video = kzalloc(sizeof(*vp->dev_video),GFP_KERNEL);
1290 if (!vp->dev_video) goto fail;
1291 pvr2_v4l2_dev_init(vp->dev_video,vp,VFL_TYPE_GRABBER);
1292 if (pvr2_hdw_get_input_available(vp->channel.mc_head->hdw) &
1293 (1 << PVR2_CVAL_INPUT_RADIO)) {
1294 vp->dev_radio = kzalloc(sizeof(*vp->dev_radio),GFP_KERNEL);
1295 if (!vp->dev_radio) goto fail;
1296 pvr2_v4l2_dev_init(vp->dev_radio,vp,VFL_TYPE_RADIO);
1301 pvr2_trace(PVR2_TRACE_STRUCT,"Failure creating pvr2_v4l2 id=%p",vp);
1302 pvr2_v4l2_destroy_no_lock(vp);