1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
5 * Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
8 #include <linux/kernel.h>
9 #include <linux/slab.h>
10 #include "pvrusb2-context.h"
11 #include "pvrusb2-hdw.h"
13 #include "pvrusb2-debug.h"
14 #include "pvrusb2-v4l2.h"
15 #include "pvrusb2-ioread.h"
16 #include <linux/videodev2.h>
17 #include <linux/module.h>
18 #include <media/v4l2-dev.h>
19 #include <media/v4l2-device.h>
20 #include <media/v4l2-fh.h>
21 #include <media/v4l2-common.h>
22 #include <media/v4l2-ioctl.h>
28 struct pvr2_v4l2_dev {
29 struct video_device devbase; /* MUST be first! */
30 struct pvr2_v4l2 *v4lp;
31 struct pvr2_context_stream *stream;
32 /* Information about this device: */
33 enum pvr2_config config; /* Expected stream format */
34 int v4l_type; /* V4L defined type for this device node */
35 enum pvr2_v4l_type minor_type; /* pvr2-understood minor device type */
40 struct pvr2_channel channel;
41 struct pvr2_v4l2_dev *pdi;
42 struct pvr2_ioread *rhp;
44 wait_queue_head_t wait_data;
46 /* Map contiguous ordinal value to input id */
47 unsigned char *input_map;
48 unsigned int input_cnt;
52 struct pvr2_channel channel;
54 /* streams - Note that these must be separately, individually,
55 * allocated pointers. This is because the v4l core is going to
56 * manage their deletion - separately, individually... */
57 struct pvr2_v4l2_dev *dev_video;
58 struct pvr2_v4l2_dev *dev_radio;
61 static int video_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
62 module_param_array(video_nr, int, NULL, 0444);
63 MODULE_PARM_DESC(video_nr, "Offset for device's video dev minor");
64 static int radio_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
65 module_param_array(radio_nr, int, NULL, 0444);
66 MODULE_PARM_DESC(radio_nr, "Offset for device's radio dev minor");
67 static int vbi_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
68 module_param_array(vbi_nr, int, NULL, 0444);
69 MODULE_PARM_DESC(vbi_nr, "Offset for device's vbi dev minor");
71 #define PVR_FORMAT_PIX 0
72 #define PVR_FORMAT_VBI 1
74 static struct v4l2_format pvr_format [] = {
76 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
81 .pixelformat = V4L2_PIX_FMT_MPEG,
82 .field = V4L2_FIELD_INTERLACED,
83 /* FIXME : Don't know what to put here... */
84 .sizeimage = 32 * 1024,
89 .type = V4L2_BUF_TYPE_VBI_CAPTURE,
92 .sampling_rate = 27000000,
94 .samples_per_line = 1443,
95 .sample_format = V4L2_PIX_FMT_GREY,
107 * This is part of Video 4 Linux API. These procedures handle ioctl() calls.
109 static int pvr2_querycap(struct file *file, void *priv, struct v4l2_capability *cap)
111 struct pvr2_v4l2_fh *fh = file->private_data;
112 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
114 strscpy(cap->driver, "pvrusb2", sizeof(cap->driver));
115 strscpy(cap->bus_info, pvr2_hdw_get_bus_info(hdw),
116 sizeof(cap->bus_info));
117 strscpy(cap->card, pvr2_hdw_get_desc(hdw), sizeof(cap->card));
118 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_TUNER |
119 V4L2_CAP_AUDIO | V4L2_CAP_RADIO |
120 V4L2_CAP_READWRITE | V4L2_CAP_DEVICE_CAPS;
121 switch (fh->pdi->devbase.vfl_type) {
122 case VFL_TYPE_GRABBER:
123 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_AUDIO;
126 cap->device_caps = V4L2_CAP_RADIO;
131 cap->device_caps |= V4L2_CAP_TUNER | V4L2_CAP_READWRITE;
135 static int pvr2_g_std(struct file *file, void *priv, v4l2_std_id *std)
137 struct pvr2_v4l2_fh *fh = file->private_data;
138 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
142 ret = pvr2_ctrl_get_value(
143 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_STDCUR), &val);
148 static int pvr2_s_std(struct file *file, void *priv, v4l2_std_id std)
150 struct pvr2_v4l2_fh *fh = file->private_data;
151 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
154 ret = pvr2_ctrl_set_value(
155 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_STDCUR), std);
156 pvr2_hdw_commit_ctl(hdw);
160 static int pvr2_querystd(struct file *file, void *priv, v4l2_std_id *std)
162 struct pvr2_v4l2_fh *fh = file->private_data;
163 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
167 ret = pvr2_ctrl_get_value(
168 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_STDDETECT), &val);
173 static int pvr2_enum_input(struct file *file, void *priv, struct v4l2_input *vi)
175 struct pvr2_v4l2_fh *fh = file->private_data;
176 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
177 struct pvr2_ctrl *cptr;
178 struct v4l2_input tmp;
182 cptr = pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_INPUT);
184 memset(&tmp, 0, sizeof(tmp));
185 tmp.index = vi->index;
186 if (vi->index >= fh->input_cnt)
188 val = fh->input_map[vi->index];
190 case PVR2_CVAL_INPUT_TV:
191 case PVR2_CVAL_INPUT_DTV:
192 case PVR2_CVAL_INPUT_RADIO:
193 tmp.type = V4L2_INPUT_TYPE_TUNER;
195 case PVR2_CVAL_INPUT_SVIDEO:
196 case PVR2_CVAL_INPUT_COMPOSITE:
197 tmp.type = V4L2_INPUT_TYPE_CAMERA;
204 pvr2_ctrl_get_valname(cptr, val,
205 tmp.name, sizeof(tmp.name) - 1, &cnt);
208 /* Don't bother with audioset, since this driver currently
209 always switches the audio whenever the video is
212 /* Handling std is a tougher problem. It doesn't make
213 sense in cases where a device might be multi-standard.
214 We could just copy out the current value for the
215 standard, but it can change over time. For now just
221 static int pvr2_g_input(struct file *file, void *priv, unsigned int *i)
223 struct pvr2_v4l2_fh *fh = file->private_data;
224 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
226 struct pvr2_ctrl *cptr;
230 cptr = pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_INPUT);
232 ret = pvr2_ctrl_get_value(cptr, &val);
234 for (idx = 0; idx < fh->input_cnt; idx++) {
235 if (fh->input_map[idx] == val) {
243 static int pvr2_s_input(struct file *file, void *priv, unsigned int inp)
245 struct pvr2_v4l2_fh *fh = file->private_data;
246 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
249 if (inp >= fh->input_cnt)
251 ret = pvr2_ctrl_set_value(
252 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_INPUT),
254 pvr2_hdw_commit_ctl(hdw);
258 static int pvr2_enumaudio(struct file *file, void *priv, struct v4l2_audio *vin)
260 /* pkt: FIXME: We are returning one "fake" input here
261 which could very well be called "whatever_we_like".
262 This is for apps that want to see an audio input
263 just to feel comfortable, as well as to test if
264 it can do stereo or sth. There is actually no guarantee
265 that the actual audio input cannot change behind the app's
266 back, but most applications should not mind that either.
268 Hopefully, mplayer people will work with us on this (this
269 whole mess is to support mplayer pvr://), or Hans will come
270 up with a more standard way to say "we have inputs but we
271 don 't want you to change them independent of video" which
277 strscpy(vin->name, "PVRUSB2 Audio", sizeof(vin->name));
278 vin->capability = V4L2_AUDCAP_STEREO;
282 static int pvr2_g_audio(struct file *file, void *priv, struct v4l2_audio *vin)
284 /* pkt: FIXME: see above comment (VIDIOC_ENUMAUDIO) */
286 strscpy(vin->name, "PVRUSB2 Audio", sizeof(vin->name));
287 vin->capability = V4L2_AUDCAP_STEREO;
291 static int pvr2_s_audio(struct file *file, void *priv, const struct v4l2_audio *vout)
298 static int pvr2_g_tuner(struct file *file, void *priv, struct v4l2_tuner *vt)
300 struct pvr2_v4l2_fh *fh = file->private_data;
301 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
304 return -EINVAL; /* Only answer for the 1st tuner */
306 pvr2_hdw_execute_tuner_poll(hdw);
307 return pvr2_hdw_get_tuner_status(hdw, vt);
310 static int pvr2_s_tuner(struct file *file, void *priv, const struct v4l2_tuner *vt)
312 struct pvr2_v4l2_fh *fh = file->private_data;
313 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
319 ret = pvr2_ctrl_set_value(
320 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_AUDIOMODE),
322 pvr2_hdw_commit_ctl(hdw);
326 static int pvr2_s_frequency(struct file *file, void *priv, const struct v4l2_frequency *vf)
328 struct pvr2_v4l2_fh *fh = file->private_data;
329 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
331 struct v4l2_tuner vt;
333 struct pvr2_ctrl *ctrlp;
336 ret = pvr2_hdw_get_tuner_status(hdw, &vt);
339 ctrlp = pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_INPUT);
340 ret = pvr2_ctrl_get_value(ctrlp, &cur_input);
343 if (vf->type == V4L2_TUNER_RADIO) {
344 if (cur_input != PVR2_CVAL_INPUT_RADIO)
345 pvr2_ctrl_set_value(ctrlp, PVR2_CVAL_INPUT_RADIO);
347 if (cur_input == PVR2_CVAL_INPUT_RADIO)
348 pvr2_ctrl_set_value(ctrlp, PVR2_CVAL_INPUT_TV);
351 if (vt.capability & V4L2_TUNER_CAP_LOW)
355 ret = pvr2_ctrl_set_value(
356 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),fv);
357 pvr2_hdw_commit_ctl(hdw);
361 static int pvr2_g_frequency(struct file *file, void *priv, struct v4l2_frequency *vf)
363 struct pvr2_v4l2_fh *fh = file->private_data;
364 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
367 struct v4l2_tuner vt;
370 ret = pvr2_hdw_get_tuner_status(hdw, &vt);
373 ret = pvr2_ctrl_get_value(
374 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_FREQUENCY),
379 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_INPUT),
381 if (cur_input == PVR2_CVAL_INPUT_RADIO)
382 vf->type = V4L2_TUNER_RADIO;
384 vf->type = V4L2_TUNER_ANALOG_TV;
385 if (vt.capability & V4L2_TUNER_CAP_LOW)
386 val = (val * 2) / 125;
393 static int pvr2_enum_fmt_vid_cap(struct file *file, void *priv, struct v4l2_fmtdesc *fd)
395 /* Only one format is supported: MPEG. */
399 fd->pixelformat = V4L2_PIX_FMT_MPEG;
403 static int pvr2_g_fmt_vid_cap(struct file *file, void *priv, struct v4l2_format *vf)
405 struct pvr2_v4l2_fh *fh = file->private_data;
406 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
409 memcpy(vf, &pvr_format[PVR_FORMAT_PIX], sizeof(struct v4l2_format));
412 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_HRES),
414 vf->fmt.pix.width = val;
417 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_VRES),
419 vf->fmt.pix.height = val;
423 static int pvr2_try_fmt_vid_cap(struct file *file, void *priv, struct v4l2_format *vf)
425 struct pvr2_v4l2_fh *fh = file->private_data;
426 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
427 int lmin, lmax, ldef;
428 struct pvr2_ctrl *hcp, *vcp;
429 int h = vf->fmt.pix.height;
430 int w = vf->fmt.pix.width;
432 hcp = pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_HRES);
433 vcp = pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_VRES);
435 lmin = pvr2_ctrl_get_min(hcp);
436 lmax = pvr2_ctrl_get_max(hcp);
437 pvr2_ctrl_get_def(hcp, &ldef);
444 lmin = pvr2_ctrl_get_min(vcp);
445 lmax = pvr2_ctrl_get_max(vcp);
446 pvr2_ctrl_get_def(vcp, &ldef);
454 memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
455 sizeof(struct v4l2_format));
456 vf->fmt.pix.width = w;
457 vf->fmt.pix.height = h;
461 static int pvr2_s_fmt_vid_cap(struct file *file, void *priv, struct v4l2_format *vf)
463 struct pvr2_v4l2_fh *fh = file->private_data;
464 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
465 struct pvr2_ctrl *hcp, *vcp;
466 int ret = pvr2_try_fmt_vid_cap(file, fh, vf);
470 hcp = pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_HRES);
471 vcp = pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_VRES);
472 pvr2_ctrl_set_value(hcp, vf->fmt.pix.width);
473 pvr2_ctrl_set_value(vcp, vf->fmt.pix.height);
474 pvr2_hdw_commit_ctl(hdw);
478 static int pvr2_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
480 struct pvr2_v4l2_fh *fh = file->private_data;
481 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
482 struct pvr2_v4l2_dev *pdi = fh->pdi;
485 if (!fh->pdi->stream) {
486 /* No stream defined for this node. This means
487 that we're not currently allowed to stream from
491 ret = pvr2_hdw_set_stream_type(hdw, pdi->config);
494 return pvr2_hdw_set_streaming(hdw, !0);
497 static int pvr2_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
499 struct pvr2_v4l2_fh *fh = file->private_data;
500 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
502 if (!fh->pdi->stream) {
503 /* No stream defined for this node. This means
504 that we're not currently allowed to stream from
508 return pvr2_hdw_set_streaming(hdw, 0);
511 static int pvr2_queryctrl(struct file *file, void *priv,
512 struct v4l2_queryctrl *vc)
514 struct pvr2_v4l2_fh *fh = file->private_data;
515 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
516 struct pvr2_ctrl *cptr;
519 if (vc->id & V4L2_CTRL_FLAG_NEXT_CTRL) {
520 cptr = pvr2_hdw_get_ctrl_nextv4l(
521 hdw, (vc->id & ~V4L2_CTRL_FLAG_NEXT_CTRL));
523 vc->id = pvr2_ctrl_get_v4lid(cptr);
525 cptr = pvr2_hdw_get_ctrl_v4l(hdw, vc->id);
528 pvr2_trace(PVR2_TRACE_V4LIOCTL,
529 "QUERYCTRL id=0x%x not implemented here",
534 pvr2_trace(PVR2_TRACE_V4LIOCTL,
535 "QUERYCTRL id=0x%x mapping name=%s (%s)",
536 vc->id, pvr2_ctrl_get_name(cptr),
537 pvr2_ctrl_get_desc(cptr));
538 strscpy(vc->name, pvr2_ctrl_get_desc(cptr), sizeof(vc->name));
539 vc->flags = pvr2_ctrl_get_v4lflags(cptr);
540 pvr2_ctrl_get_def(cptr, &val);
541 vc->default_value = val;
542 switch (pvr2_ctrl_get_type(cptr)) {
544 vc->type = V4L2_CTRL_TYPE_MENU;
546 vc->maximum = pvr2_ctrl_get_cnt(cptr) - 1;
550 vc->type = V4L2_CTRL_TYPE_BOOLEAN;
556 vc->type = V4L2_CTRL_TYPE_INTEGER;
557 vc->minimum = pvr2_ctrl_get_min(cptr);
558 vc->maximum = pvr2_ctrl_get_max(cptr);
562 pvr2_trace(PVR2_TRACE_V4LIOCTL,
563 "QUERYCTRL id=0x%x name=%s not mappable",
564 vc->id, pvr2_ctrl_get_name(cptr));
570 static int pvr2_querymenu(struct file *file, void *priv, struct v4l2_querymenu *vm)
572 struct pvr2_v4l2_fh *fh = file->private_data;
573 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
574 unsigned int cnt = 0;
577 ret = pvr2_ctrl_get_valname(pvr2_hdw_get_ctrl_v4l(hdw, vm->id),
579 vm->name, sizeof(vm->name) - 1,
585 static int pvr2_g_ctrl(struct file *file, void *priv, struct v4l2_control *vc)
587 struct pvr2_v4l2_fh *fh = file->private_data;
588 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
592 ret = pvr2_ctrl_get_value(pvr2_hdw_get_ctrl_v4l(hdw, vc->id),
598 static int pvr2_s_ctrl(struct file *file, void *priv, struct v4l2_control *vc)
600 struct pvr2_v4l2_fh *fh = file->private_data;
601 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
604 ret = pvr2_ctrl_set_value(pvr2_hdw_get_ctrl_v4l(hdw, vc->id),
606 pvr2_hdw_commit_ctl(hdw);
610 static int pvr2_g_ext_ctrls(struct file *file, void *priv,
611 struct v4l2_ext_controls *ctls)
613 struct pvr2_v4l2_fh *fh = file->private_data;
614 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
615 struct v4l2_ext_control *ctrl;
616 struct pvr2_ctrl *cptr;
622 for (idx = 0; idx < ctls->count; idx++) {
623 ctrl = ctls->controls + idx;
624 cptr = pvr2_hdw_get_ctrl_v4l(hdw, ctrl->id);
626 if (ctls->which == V4L2_CTRL_WHICH_DEF_VAL)
627 pvr2_ctrl_get_def(cptr, &val);
629 ret = pvr2_ctrl_get_value(cptr, &val);
634 ctls->error_idx = idx;
637 /* Ensure that if read as a 64 bit value, the user
638 will still get a hopefully sane value */
645 static int pvr2_s_ext_ctrls(struct file *file, void *priv,
646 struct v4l2_ext_controls *ctls)
648 struct pvr2_v4l2_fh *fh = file->private_data;
649 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
650 struct v4l2_ext_control *ctrl;
654 /* Default value cannot be changed */
655 if (ctls->which == V4L2_CTRL_WHICH_DEF_VAL)
659 for (idx = 0; idx < ctls->count; idx++) {
660 ctrl = ctls->controls + idx;
661 ret = pvr2_ctrl_set_value(
662 pvr2_hdw_get_ctrl_v4l(hdw, ctrl->id),
665 ctls->error_idx = idx;
670 pvr2_hdw_commit_ctl(hdw);
674 static int pvr2_try_ext_ctrls(struct file *file, void *priv,
675 struct v4l2_ext_controls *ctls)
677 struct pvr2_v4l2_fh *fh = file->private_data;
678 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
679 struct v4l2_ext_control *ctrl;
680 struct pvr2_ctrl *pctl;
683 /* For the moment just validate that the requested control
685 for (idx = 0; idx < ctls->count; idx++) {
686 ctrl = ctls->controls + idx;
687 pctl = pvr2_hdw_get_ctrl_v4l(hdw, ctrl->id);
689 ctls->error_idx = idx;
696 static int pvr2_g_pixelaspect(struct file *file, void *priv,
697 int type, struct v4l2_fract *f)
699 struct pvr2_v4l2_fh *fh = file->private_data;
700 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
701 struct v4l2_cropcap cap = { .type = type };
704 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
706 ret = pvr2_hdw_get_cropcap(hdw, &cap);
708 *f = cap.pixelaspect;
712 static int pvr2_g_selection(struct file *file, void *priv,
713 struct v4l2_selection *sel)
715 struct pvr2_v4l2_fh *fh = file->private_data;
716 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
717 struct v4l2_cropcap cap;
721 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
724 cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
726 switch (sel->target) {
727 case V4L2_SEL_TGT_CROP:
728 ret = pvr2_ctrl_get_value(
729 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPL), &val);
733 ret = pvr2_ctrl_get_value(
734 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPT), &val);
738 ret = pvr2_ctrl_get_value(
739 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPW), &val);
743 ret = pvr2_ctrl_get_value(
744 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPH), &val);
749 case V4L2_SEL_TGT_CROP_DEFAULT:
750 ret = pvr2_hdw_get_cropcap(hdw, &cap);
751 sel->r = cap.defrect;
753 case V4L2_SEL_TGT_CROP_BOUNDS:
754 ret = pvr2_hdw_get_cropcap(hdw, &cap);
763 static int pvr2_s_selection(struct file *file, void *priv,
764 struct v4l2_selection *sel)
766 struct pvr2_v4l2_fh *fh = file->private_data;
767 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
770 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
771 sel->target != V4L2_SEL_TGT_CROP)
773 ret = pvr2_ctrl_set_value(
774 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPL),
778 ret = pvr2_ctrl_set_value(
779 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPT),
783 ret = pvr2_ctrl_set_value(
784 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPW),
788 ret = pvr2_ctrl_set_value(
789 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPH),
792 pvr2_hdw_commit_ctl(hdw);
796 static int pvr2_log_status(struct file *file, void *priv)
798 struct pvr2_v4l2_fh *fh = file->private_data;
799 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
801 pvr2_hdw_trigger_module_log(hdw);
805 static const struct v4l2_ioctl_ops pvr2_ioctl_ops = {
806 .vidioc_querycap = pvr2_querycap,
807 .vidioc_s_audio = pvr2_s_audio,
808 .vidioc_g_audio = pvr2_g_audio,
809 .vidioc_enumaudio = pvr2_enumaudio,
810 .vidioc_enum_input = pvr2_enum_input,
811 .vidioc_g_pixelaspect = pvr2_g_pixelaspect,
812 .vidioc_s_selection = pvr2_s_selection,
813 .vidioc_g_selection = pvr2_g_selection,
814 .vidioc_g_input = pvr2_g_input,
815 .vidioc_s_input = pvr2_s_input,
816 .vidioc_g_frequency = pvr2_g_frequency,
817 .vidioc_s_frequency = pvr2_s_frequency,
818 .vidioc_s_tuner = pvr2_s_tuner,
819 .vidioc_g_tuner = pvr2_g_tuner,
820 .vidioc_g_std = pvr2_g_std,
821 .vidioc_s_std = pvr2_s_std,
822 .vidioc_querystd = pvr2_querystd,
823 .vidioc_log_status = pvr2_log_status,
824 .vidioc_enum_fmt_vid_cap = pvr2_enum_fmt_vid_cap,
825 .vidioc_g_fmt_vid_cap = pvr2_g_fmt_vid_cap,
826 .vidioc_s_fmt_vid_cap = pvr2_s_fmt_vid_cap,
827 .vidioc_try_fmt_vid_cap = pvr2_try_fmt_vid_cap,
828 .vidioc_streamon = pvr2_streamon,
829 .vidioc_streamoff = pvr2_streamoff,
830 .vidioc_queryctrl = pvr2_queryctrl,
831 .vidioc_querymenu = pvr2_querymenu,
832 .vidioc_g_ctrl = pvr2_g_ctrl,
833 .vidioc_s_ctrl = pvr2_s_ctrl,
834 .vidioc_g_ext_ctrls = pvr2_g_ext_ctrls,
835 .vidioc_s_ext_ctrls = pvr2_s_ext_ctrls,
836 .vidioc_try_ext_ctrls = pvr2_try_ext_ctrls,
839 static void pvr2_v4l2_dev_destroy(struct pvr2_v4l2_dev *dip)
841 struct pvr2_hdw *hdw = dip->v4lp->channel.mc_head->hdw;
842 enum pvr2_config cfg = dip->config;
846 /* Construct the unregistration message *before* we actually
847 perform the unregistration step. By doing it this way we don't
848 have to worry about potentially touching deleted resources. */
849 mcnt = scnprintf(msg, sizeof(msg) - 1,
850 "pvrusb2: unregistered device %s [%s]",
851 video_device_node_name(&dip->devbase),
852 pvr2_config_get_name(cfg));
855 pvr2_hdw_v4l_store_minor_number(hdw,dip->minor_type,-1);
861 /* Actual deallocation happens later when all internal references
863 video_unregister_device(&dip->devbase);
865 pr_info("%s\n", msg);
870 static void pvr2_v4l2_dev_disassociate_parent(struct pvr2_v4l2_dev *dip)
873 if (!dip->devbase.v4l2_dev->dev) return;
874 dip->devbase.v4l2_dev->dev = NULL;
875 device_move(&dip->devbase.dev, NULL, DPM_ORDER_NONE);
879 static void pvr2_v4l2_destroy_no_lock(struct pvr2_v4l2 *vp)
882 pvr2_v4l2_dev_destroy(vp->dev_video);
883 vp->dev_video = NULL;
886 pvr2_v4l2_dev_destroy(vp->dev_radio);
887 vp->dev_radio = NULL;
890 pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr2_v4l2 id=%p",vp);
891 pvr2_channel_done(&vp->channel);
896 static void pvr2_video_device_release(struct video_device *vdev)
898 struct pvr2_v4l2_dev *dev;
899 dev = container_of(vdev,struct pvr2_v4l2_dev,devbase);
904 static void pvr2_v4l2_internal_check(struct pvr2_channel *chp)
906 struct pvr2_v4l2 *vp;
907 vp = container_of(chp,struct pvr2_v4l2,channel);
908 if (!vp->channel.mc_head->disconnect_flag) return;
909 pvr2_v4l2_dev_disassociate_parent(vp->dev_video);
910 pvr2_v4l2_dev_disassociate_parent(vp->dev_radio);
911 if (!list_empty(&vp->dev_video->devbase.fh_list) ||
912 !list_empty(&vp->dev_radio->devbase.fh_list))
914 pvr2_v4l2_destroy_no_lock(vp);
918 static int pvr2_v4l2_release(struct file *file)
920 struct pvr2_v4l2_fh *fhp = file->private_data;
921 struct pvr2_v4l2 *vp = fhp->pdi->v4lp;
922 struct pvr2_hdw *hdw = fhp->channel.mc_head->hdw;
924 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_release");
927 struct pvr2_stream *sp;
928 pvr2_hdw_set_streaming(hdw,0);
929 sp = pvr2_ioread_get_stream(fhp->rhp);
930 if (sp) pvr2_stream_set_callback(sp,NULL,NULL);
931 pvr2_ioread_destroy(fhp->rhp);
935 v4l2_fh_del(&fhp->fh);
936 v4l2_fh_exit(&fhp->fh);
937 file->private_data = NULL;
939 pvr2_channel_done(&fhp->channel);
940 pvr2_trace(PVR2_TRACE_STRUCT,
941 "Destroying pvr_v4l2_fh id=%p",fhp);
942 if (fhp->input_map) {
943 kfree(fhp->input_map);
944 fhp->input_map = NULL;
947 if (vp->channel.mc_head->disconnect_flag &&
948 list_empty(&vp->dev_video->devbase.fh_list) &&
949 list_empty(&vp->dev_radio->devbase.fh_list)) {
950 pvr2_v4l2_destroy_no_lock(vp);
956 static int pvr2_v4l2_open(struct file *file)
958 struct pvr2_v4l2_dev *dip; /* Our own context pointer */
959 struct pvr2_v4l2_fh *fhp;
960 struct pvr2_v4l2 *vp;
961 struct pvr2_hdw *hdw;
962 unsigned int input_mask = 0;
963 unsigned int input_cnt,idx;
966 dip = container_of(video_devdata(file),struct pvr2_v4l2_dev,devbase);
969 hdw = vp->channel.hdw;
971 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_open");
973 if (!pvr2_hdw_dev_ok(hdw)) {
974 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,
975 "pvr2_v4l2_open: hardware not ready");
979 fhp = kzalloc(sizeof(*fhp),GFP_KERNEL);
984 v4l2_fh_init(&fhp->fh, &dip->devbase);
985 init_waitqueue_head(&fhp->wait_data);
988 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr_v4l2_fh id=%p",fhp);
989 pvr2_channel_init(&fhp->channel,vp->channel.mc_head);
991 if (dip->v4l_type == VFL_TYPE_RADIO) {
992 /* Opening device as a radio, legal input selection subset
993 is just the radio. */
994 input_mask = (1 << PVR2_CVAL_INPUT_RADIO);
996 /* Opening the main V4L device, legal input selection
997 subset includes all analog inputs. */
998 input_mask = ((1 << PVR2_CVAL_INPUT_RADIO) |
999 (1 << PVR2_CVAL_INPUT_TV) |
1000 (1 << PVR2_CVAL_INPUT_COMPOSITE) |
1001 (1 << PVR2_CVAL_INPUT_SVIDEO));
1003 ret = pvr2_channel_limit_inputs(&fhp->channel,input_mask);
1005 pvr2_channel_done(&fhp->channel);
1006 pvr2_trace(PVR2_TRACE_STRUCT,
1007 "Destroying pvr_v4l2_fh id=%p (input mask error)",
1009 v4l2_fh_exit(&fhp->fh);
1014 input_mask &= pvr2_hdw_get_input_available(hdw);
1016 for (idx = 0; idx < (sizeof(input_mask) << 3); idx++) {
1017 if (input_mask & (1 << idx)) input_cnt++;
1019 fhp->input_cnt = input_cnt;
1020 fhp->input_map = kzalloc(input_cnt,GFP_KERNEL);
1021 if (!fhp->input_map) {
1022 pvr2_channel_done(&fhp->channel);
1023 pvr2_trace(PVR2_TRACE_STRUCT,
1024 "Destroying pvr_v4l2_fh id=%p (input map failure)",
1026 v4l2_fh_exit(&fhp->fh);
1031 for (idx = 0; idx < (sizeof(input_mask) << 3); idx++) {
1032 if (!(input_mask & (1 << idx))) continue;
1033 fhp->input_map[input_cnt++] = idx;
1037 file->private_data = fhp;
1039 fhp->fw_mode_flag = pvr2_hdw_cpufw_get_enabled(hdw);
1040 v4l2_fh_add(&fhp->fh);
1046 static void pvr2_v4l2_notify(struct pvr2_v4l2_fh *fhp)
1048 wake_up(&fhp->wait_data);
1051 static int pvr2_v4l2_iosetup(struct pvr2_v4l2_fh *fh)
1054 struct pvr2_stream *sp;
1055 struct pvr2_hdw *hdw;
1056 if (fh->rhp) return 0;
1058 if (!fh->pdi->stream) {
1059 /* No stream defined for this node. This means that we're
1060 not currently allowed to stream from this node. */
1064 /* First read() attempt. Try to claim the stream and start
1066 if ((ret = pvr2_channel_claim_stream(&fh->channel,
1067 fh->pdi->stream)) != 0) {
1068 /* Someone else must already have it */
1072 fh->rhp = pvr2_channel_create_mpeg_stream(fh->pdi->stream);
1074 pvr2_channel_claim_stream(&fh->channel,NULL);
1078 hdw = fh->channel.mc_head->hdw;
1079 sp = fh->pdi->stream->stream;
1080 pvr2_stream_set_callback(sp,(pvr2_stream_callback)pvr2_v4l2_notify,fh);
1081 pvr2_hdw_set_stream_type(hdw,fh->pdi->config);
1082 if ((ret = pvr2_hdw_set_streaming(hdw,!0)) < 0) return ret;
1083 return pvr2_ioread_set_enabled(fh->rhp,!0);
1087 static ssize_t pvr2_v4l2_read(struct file *file,
1088 char __user *buff, size_t count, loff_t *ppos)
1090 struct pvr2_v4l2_fh *fh = file->private_data;
1093 if (fh->fw_mode_flag) {
1094 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
1098 unsigned int offs = *ppos;
1100 tbuf = kmalloc(PAGE_SIZE,GFP_KERNEL);
1101 if (!tbuf) return -ENOMEM;
1105 if (c1 > PAGE_SIZE) c1 = PAGE_SIZE;
1106 c2 = pvr2_hdw_cpufw_get(hdw,offs,tbuf,c1);
1112 if (copy_to_user(buff,tbuf,c2)) {
1127 ret = pvr2_v4l2_iosetup(fh);
1134 ret = pvr2_ioread_read(fh->rhp,buff,count);
1135 if (ret >= 0) break;
1136 if (ret != -EAGAIN) break;
1137 if (file->f_flags & O_NONBLOCK) break;
1138 /* Doing blocking I/O. Wait here. */
1139 ret = wait_event_interruptible(
1141 pvr2_ioread_avail(fh->rhp) >= 0);
1149 static __poll_t pvr2_v4l2_poll(struct file *file, poll_table *wait)
1152 struct pvr2_v4l2_fh *fh = file->private_data;
1155 if (fh->fw_mode_flag) {
1156 mask |= EPOLLIN | EPOLLRDNORM;
1161 ret = pvr2_v4l2_iosetup(fh);
1162 if (ret) return EPOLLERR;
1165 poll_wait(file,&fh->wait_data,wait);
1167 if (pvr2_ioread_avail(fh->rhp) >= 0) {
1168 mask |= EPOLLIN | EPOLLRDNORM;
1175 static const struct v4l2_file_operations vdev_fops = {
1176 .owner = THIS_MODULE,
1177 .open = pvr2_v4l2_open,
1178 .release = pvr2_v4l2_release,
1179 .read = pvr2_v4l2_read,
1180 .unlocked_ioctl = video_ioctl2,
1181 .poll = pvr2_v4l2_poll,
1185 static const struct video_device vdev_template = {
1190 static void pvr2_v4l2_dev_init(struct pvr2_v4l2_dev *dip,
1191 struct pvr2_v4l2 *vp,
1196 struct pvr2_hdw *hdw;
1200 hdw = vp->channel.mc_head->hdw;
1201 dip->v4l_type = v4l_type;
1203 case VFL_TYPE_GRABBER:
1204 dip->stream = &vp->channel.mc_head->video_stream;
1205 dip->config = pvr2_config_mpeg;
1206 dip->minor_type = pvr2_v4l_type_video;
1209 pr_err(KBUILD_MODNAME
1210 ": Failed to set up pvrusb2 v4l video dev due to missing stream instance\n");
1215 dip->config = pvr2_config_vbi;
1216 dip->minor_type = pvr2_v4l_type_vbi;
1219 case VFL_TYPE_RADIO:
1220 dip->stream = &vp->channel.mc_head->video_stream;
1221 dip->config = pvr2_config_mpeg;
1222 dip->minor_type = pvr2_v4l_type_radio;
1226 /* Bail out (this should be impossible) */
1227 pr_err(KBUILD_MODNAME ": Failed to set up pvrusb2 v4l dev due to unrecognized config\n");
1231 dip->devbase = vdev_template;
1232 dip->devbase.release = pvr2_video_device_release;
1233 dip->devbase.ioctl_ops = &pvr2_ioctl_ops;
1236 pvr2_ctrl_get_value(
1237 pvr2_hdw_get_ctrl_by_id(hdw,
1238 PVR2_CID_STDAVAIL), &val);
1239 dip->devbase.tvnorms = (v4l2_std_id)val;
1243 unit_number = pvr2_hdw_get_unit_number(hdw);
1244 if (nr_ptr && (unit_number >= 0) && (unit_number < PVR_NUM)) {
1245 mindevnum = nr_ptr[unit_number];
1247 pvr2_hdw_set_v4l2_dev(hdw, &dip->devbase);
1248 if ((video_register_device(&dip->devbase,
1249 dip->v4l_type, mindevnum) < 0) &&
1250 (video_register_device(&dip->devbase,
1251 dip->v4l_type, -1) < 0)) {
1252 pr_err(KBUILD_MODNAME
1253 ": Failed to register pvrusb2 v4l device\n");
1256 pr_info("pvrusb2: registered device %s [%s]\n",
1257 video_device_node_name(&dip->devbase),
1258 pvr2_config_get_name(dip->config));
1260 pvr2_hdw_v4l_store_minor_number(hdw,
1261 dip->minor_type,dip->devbase.minor);
1265 struct pvr2_v4l2 *pvr2_v4l2_create(struct pvr2_context *mnp)
1267 struct pvr2_v4l2 *vp;
1269 vp = kzalloc(sizeof(*vp),GFP_KERNEL);
1271 pvr2_channel_init(&vp->channel,mnp);
1272 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr2_v4l2 id=%p",vp);
1274 vp->channel.check_func = pvr2_v4l2_internal_check;
1276 /* register streams */
1277 vp->dev_video = kzalloc(sizeof(*vp->dev_video),GFP_KERNEL);
1278 if (!vp->dev_video) goto fail;
1279 pvr2_v4l2_dev_init(vp->dev_video,vp,VFL_TYPE_GRABBER);
1280 if (pvr2_hdw_get_input_available(vp->channel.mc_head->hdw) &
1281 (1 << PVR2_CVAL_INPUT_RADIO)) {
1282 vp->dev_radio = kzalloc(sizeof(*vp->dev_radio),GFP_KERNEL);
1283 if (!vp->dev_radio) goto fail;
1284 pvr2_v4l2_dev_init(vp->dev_radio,vp,VFL_TYPE_RADIO);
1289 pvr2_trace(PVR2_TRACE_STRUCT,"Failure creating pvr2_v4l2 id=%p",vp);
1290 pvr2_v4l2_destroy_no_lock(vp);