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-common.h>
35 #include <media/v4l2-ioctl.h>
41 struct pvr2_v4l2_dev {
42 struct video_device devbase; /* MUST be first! */
43 struct pvr2_v4l2 *v4lp;
44 struct pvr2_context_stream *stream;
45 /* Information about this device: */
46 enum pvr2_config config; /* Expected stream format */
47 int v4l_type; /* V4L defined type for this device node */
48 enum pvr2_v4l_type minor_type; /* pvr2-understood minor device type */
52 struct pvr2_channel channel;
53 struct pvr2_v4l2_dev *pdi;
54 enum v4l2_priority prio;
55 struct pvr2_ioread *rhp;
57 struct pvr2_v4l2 *vhead;
58 struct pvr2_v4l2_fh *vnext;
59 struct pvr2_v4l2_fh *vprev;
60 wait_queue_head_t wait_data;
62 /* Map contiguous ordinal value to input id */
63 unsigned char *input_map;
64 unsigned int input_cnt;
68 struct pvr2_channel channel;
69 struct pvr2_v4l2_fh *vfirst;
70 struct pvr2_v4l2_fh *vlast;
72 struct v4l2_prio_state prio;
74 /* streams - Note that these must be separately, individually,
75 * allocated pointers. This is because the v4l core is going to
76 * manage their deletion - separately, individually... */
77 struct pvr2_v4l2_dev *dev_video;
78 struct pvr2_v4l2_dev *dev_radio;
81 static int video_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
82 module_param_array(video_nr, int, NULL, 0444);
83 MODULE_PARM_DESC(video_nr, "Offset for device's video dev minor");
84 static int radio_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
85 module_param_array(radio_nr, int, NULL, 0444);
86 MODULE_PARM_DESC(radio_nr, "Offset for device's radio dev minor");
87 static int vbi_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
88 module_param_array(vbi_nr, int, NULL, 0444);
89 MODULE_PARM_DESC(vbi_nr, "Offset for device's vbi dev minor");
91 static struct v4l2_capability pvr_capability ={
93 .card = "Hauppauge WinTV pvr-usb2",
95 .version = LINUX_VERSION_CODE,
96 .capabilities = (V4L2_CAP_VIDEO_CAPTURE |
97 V4L2_CAP_TUNER | V4L2_CAP_AUDIO | V4L2_CAP_RADIO |
101 static struct v4l2_fmtdesc pvr_fmtdesc [] = {
104 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
105 .flags = V4L2_FMT_FLAG_COMPRESSED,
106 .description = "MPEG1/2",
107 // This should really be V4L2_PIX_FMT_MPEG, but xawtv
108 // breaks when I do that.
109 .pixelformat = 0, // V4L2_PIX_FMT_MPEG,
113 #define PVR_FORMAT_PIX 0
114 #define PVR_FORMAT_VBI 1
116 static struct v4l2_format pvr_format [] = {
118 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
123 // This should really be V4L2_PIX_FMT_MPEG,
124 // but xawtv breaks when I do that.
125 .pixelformat = 0, // V4L2_PIX_FMT_MPEG,
126 .field = V4L2_FIELD_INTERLACED,
127 .bytesperline = 0, // doesn't make sense
129 //FIXME : Don't know what to put here...
130 .sizeimage = (32*1024),
131 .colorspace = 0, // doesn't make sense here
137 .type = V4L2_BUF_TYPE_VBI_CAPTURE,
140 .sampling_rate = 27000000,
142 .samples_per_line = 1443,
143 .sample_format = V4L2_PIX_FMT_GREY,
155 * This is part of Video 4 Linux API. These procedures handle ioctl() calls.
157 static int pvr2_querycap(struct file *file, void *priv, struct v4l2_capability *cap)
159 struct pvr2_v4l2_fh *fh = file->private_data;
160 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
162 memcpy(cap, &pvr_capability, sizeof(struct v4l2_capability));
163 strlcpy(cap->bus_info, pvr2_hdw_get_bus_info(hdw),
164 sizeof(cap->bus_info));
165 strlcpy(cap->card, pvr2_hdw_get_desc(hdw), sizeof(cap->card));
169 static int pvr2_g_priority(struct file *file, void *priv, enum v4l2_priority *p)
171 struct pvr2_v4l2_fh *fh = file->private_data;
172 struct pvr2_v4l2 *vp = fh->vhead;
174 *p = v4l2_prio_max(&vp->prio);
178 static int pvr2_s_priority(struct file *file, void *priv, enum v4l2_priority prio)
180 struct pvr2_v4l2_fh *fh = file->private_data;
181 struct pvr2_v4l2 *vp = fh->vhead;
183 return v4l2_prio_change(&vp->prio, &fh->prio, prio);
186 static int pvr2_g_std(struct file *file, void *priv, v4l2_std_id *std)
188 struct pvr2_v4l2_fh *fh = file->private_data;
189 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
193 ret = pvr2_ctrl_get_value(
194 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_STDCUR), &val);
199 int pvr2_s_std(struct file *file, void *priv, v4l2_std_id *std)
201 struct pvr2_v4l2_fh *fh = file->private_data;
202 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
204 return pvr2_ctrl_set_value(
205 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_STDCUR), *std);
208 static int pvr2_querystd(struct file *file, void *priv, v4l2_std_id *std)
210 struct pvr2_v4l2_fh *fh = file->private_data;
211 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
215 ret = pvr2_ctrl_get_value(
216 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_STDDETECT), &val);
221 static int pvr2_enum_input(struct file *file, void *priv, struct v4l2_input *vi)
223 struct pvr2_v4l2_fh *fh = file->private_data;
224 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
225 struct pvr2_ctrl *cptr;
226 struct v4l2_input tmp;
231 cptr = pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_INPUT);
233 memset(&tmp, 0, sizeof(tmp));
234 tmp.index = vi->index;
236 if (vi->index >= fh->input_cnt)
238 val = fh->input_map[vi->index];
240 case PVR2_CVAL_INPUT_TV:
241 case PVR2_CVAL_INPUT_DTV:
242 case PVR2_CVAL_INPUT_RADIO:
243 tmp.type = V4L2_INPUT_TYPE_TUNER;
245 case PVR2_CVAL_INPUT_SVIDEO:
246 case PVR2_CVAL_INPUT_COMPOSITE:
247 tmp.type = V4L2_INPUT_TYPE_CAMERA;
254 pvr2_ctrl_get_valname(cptr, val,
255 tmp.name, sizeof(tmp.name) - 1, &cnt);
258 /* Don't bother with audioset, since this driver currently
259 always switches the audio whenever the video is
262 /* Handling std is a tougher problem. It doesn't make
263 sense in cases where a device might be multi-standard.
264 We could just copy out the current value for the
265 standard, but it can change over time. For now just
271 static int pvr2_g_input(struct file *file, void *priv, unsigned int *i)
273 struct pvr2_v4l2_fh *fh = file->private_data;
274 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
276 struct pvr2_ctrl *cptr;
280 cptr = pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_INPUT);
282 ret = pvr2_ctrl_get_value(cptr, &val);
284 for (idx = 0; idx < fh->input_cnt; idx++) {
285 if (fh->input_map[idx] == val) {
293 static int pvr2_s_input(struct file *file, void *priv, unsigned int inp)
295 struct pvr2_v4l2_fh *fh = file->private_data;
296 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
298 if (inp >= fh->input_cnt)
300 return pvr2_ctrl_set_value(
301 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_INPUT),
305 static int pvr2_enumaudio(struct file *file, void *priv, struct v4l2_audio *vin)
307 /* pkt: FIXME: We are returning one "fake" input here
308 which could very well be called "whatever_we_like".
309 This is for apps that want to see an audio input
310 just to feel comfortable, as well as to test if
311 it can do stereo or sth. There is actually no guarantee
312 that the actual audio input cannot change behind the app's
313 back, but most applications should not mind that either.
315 Hopefully, mplayer people will work with us on this (this
316 whole mess is to support mplayer pvr://), or Hans will come
317 up with a more standard way to say "we have inputs but we
318 don 't want you to change them independent of video" which
324 strncpy(vin->name, "PVRUSB2 Audio", 14);
325 vin->capability = V4L2_AUDCAP_STEREO;
329 static int pvr2_g_audio(struct file *file, void *priv, struct v4l2_audio *vin)
331 /* pkt: FIXME: see above comment (VIDIOC_ENUMAUDIO) */
333 strncpy(vin->name, "PVRUSB2 Audio", 14);
334 vin->capability = V4L2_AUDCAP_STEREO;
338 static int pvr2_s_audio(struct file *file, void *priv, struct v4l2_audio *vout)
345 static int pvr2_g_tuner(struct file *file, void *priv, struct v4l2_tuner *vt)
347 struct pvr2_v4l2_fh *fh = file->private_data;
348 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
351 return -EINVAL; /* Only answer for the 1st tuner */
353 pvr2_hdw_execute_tuner_poll(hdw);
354 return pvr2_hdw_get_tuner_status(hdw, vt);
357 static int pvr2_s_tuner(struct file *file, void *priv, struct v4l2_tuner *vt)
359 struct pvr2_v4l2_fh *fh = file->private_data;
360 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
365 return pvr2_ctrl_set_value(
366 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_AUDIOMODE),
370 int pvr2_s_frequency(struct file *file, void *priv, struct v4l2_frequency *vf)
372 struct pvr2_v4l2_fh *fh = file->private_data;
373 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
375 struct v4l2_tuner vt;
377 struct pvr2_ctrl *ctrlp;
380 ret = pvr2_hdw_get_tuner_status(hdw, &vt);
383 ctrlp = pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_INPUT);
384 ret = pvr2_ctrl_get_value(ctrlp, &cur_input);
387 if (vf->type == V4L2_TUNER_RADIO) {
388 if (cur_input != PVR2_CVAL_INPUT_RADIO)
389 pvr2_ctrl_set_value(ctrlp, PVR2_CVAL_INPUT_RADIO);
391 if (cur_input == PVR2_CVAL_INPUT_RADIO)
392 pvr2_ctrl_set_value(ctrlp, PVR2_CVAL_INPUT_TV);
395 if (vt.capability & V4L2_TUNER_CAP_LOW)
399 return pvr2_ctrl_set_value(
400 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),fv);
403 static int pvr2_g_frequency(struct file *file, void *priv, struct v4l2_frequency *vf)
405 struct pvr2_v4l2_fh *fh = file->private_data;
406 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
409 struct v4l2_tuner vt;
412 ret = pvr2_hdw_get_tuner_status(hdw, &vt);
415 ret = pvr2_ctrl_get_value(
416 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_FREQUENCY),
421 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_INPUT),
423 if (cur_input == PVR2_CVAL_INPUT_RADIO)
424 vf->type = V4L2_TUNER_RADIO;
426 vf->type = V4L2_TUNER_ANALOG_TV;
427 if (vt.capability & V4L2_TUNER_CAP_LOW)
428 val = (val * 2) / 125;
435 static int pvr2_enum_fmt_vid_cap(struct file *file, void *priv, struct v4l2_fmtdesc *fd)
437 /* Only one format is supported : mpeg.*/
441 memcpy(fd, pvr_fmtdesc, sizeof(struct v4l2_fmtdesc));
445 static int pvr2_g_fmt_vid_cap(struct file *file, void *priv, struct v4l2_format *vf)
447 struct pvr2_v4l2_fh *fh = file->private_data;
448 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
451 memcpy(vf, &pvr_format[PVR_FORMAT_PIX], sizeof(struct v4l2_format));
454 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_HRES),
456 vf->fmt.pix.width = val;
459 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_VRES),
461 vf->fmt.pix.height = val;
465 static int pvr2_try_fmt_vid_cap(struct file *file, void *priv, struct v4l2_format *vf)
467 struct pvr2_v4l2_fh *fh = file->private_data;
468 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
469 int lmin, lmax, ldef;
470 struct pvr2_ctrl *hcp, *vcp;
471 int h = vf->fmt.pix.height;
472 int w = vf->fmt.pix.width;
474 hcp = pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_HRES);
475 vcp = pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_VRES);
477 lmin = pvr2_ctrl_get_min(hcp);
478 lmax = pvr2_ctrl_get_max(hcp);
479 pvr2_ctrl_get_def(hcp, &ldef);
486 lmin = pvr2_ctrl_get_min(vcp);
487 lmax = pvr2_ctrl_get_max(vcp);
488 pvr2_ctrl_get_def(vcp, &ldef);
496 memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
497 sizeof(struct v4l2_format));
498 vf->fmt.pix.width = w;
499 vf->fmt.pix.height = h;
503 static int pvr2_s_fmt_vid_cap(struct file *file, void *priv, struct v4l2_format *vf)
505 struct pvr2_v4l2_fh *fh = file->private_data;
506 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
507 struct pvr2_ctrl *hcp, *vcp;
508 int ret = pvr2_try_fmt_vid_cap(file, fh, vf);
512 hcp = pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_HRES);
513 vcp = pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_VRES);
514 pvr2_ctrl_set_value(hcp, vf->fmt.pix.width);
515 pvr2_ctrl_set_value(vcp, vf->fmt.pix.height);
519 static int pvr2_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
521 struct pvr2_v4l2_fh *fh = file->private_data;
522 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
523 struct pvr2_v4l2_dev *pdi = fh->pdi;
526 if (!fh->pdi->stream) {
527 /* No stream defined for this node. This means
528 that we're not currently allowed to stream from
532 ret = pvr2_hdw_set_stream_type(hdw, pdi->config);
535 return pvr2_hdw_set_streaming(hdw, !0);
538 static int pvr2_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
540 struct pvr2_v4l2_fh *fh = file->private_data;
541 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
543 if (!fh->pdi->stream) {
544 /* No stream defined for this node. This means
545 that we're not currently allowed to stream from
549 return pvr2_hdw_set_streaming(hdw, 0);
552 static int pvr2_queryctrl(struct file *file, void *priv,
553 struct v4l2_queryctrl *vc)
555 struct pvr2_v4l2_fh *fh = file->private_data;
556 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
557 struct pvr2_ctrl *cptr;
562 if (vc->id & V4L2_CTRL_FLAG_NEXT_CTRL) {
563 cptr = pvr2_hdw_get_ctrl_nextv4l(
564 hdw, (vc->id & ~V4L2_CTRL_FLAG_NEXT_CTRL));
566 vc->id = pvr2_ctrl_get_v4lid(cptr);
568 cptr = pvr2_hdw_get_ctrl_v4l(hdw, vc->id);
571 pvr2_trace(PVR2_TRACE_V4LIOCTL,
572 "QUERYCTRL id=0x%x not implemented here",
577 pvr2_trace(PVR2_TRACE_V4LIOCTL,
578 "QUERYCTRL id=0x%x mapping name=%s (%s)",
579 vc->id, pvr2_ctrl_get_name(cptr),
580 pvr2_ctrl_get_desc(cptr));
581 strlcpy(vc->name, pvr2_ctrl_get_desc(cptr), sizeof(vc->name));
582 vc->flags = pvr2_ctrl_get_v4lflags(cptr);
583 pvr2_ctrl_get_def(cptr, &val);
584 vc->default_value = val;
585 switch (pvr2_ctrl_get_type(cptr)) {
587 vc->type = V4L2_CTRL_TYPE_MENU;
589 vc->maximum = pvr2_ctrl_get_cnt(cptr) - 1;
593 vc->type = V4L2_CTRL_TYPE_BOOLEAN;
599 vc->type = V4L2_CTRL_TYPE_INTEGER;
600 vc->minimum = pvr2_ctrl_get_min(cptr);
601 vc->maximum = pvr2_ctrl_get_max(cptr);
605 pvr2_trace(PVR2_TRACE_V4LIOCTL,
606 "QUERYCTRL id=0x%x name=%s not mappable",
607 vc->id, pvr2_ctrl_get_name(cptr));
613 static int pvr2_querymenu(struct file *file, void *priv, struct v4l2_querymenu *vm)
615 struct pvr2_v4l2_fh *fh = file->private_data;
616 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
617 unsigned int cnt = 0;
620 ret = pvr2_ctrl_get_valname(pvr2_hdw_get_ctrl_v4l(hdw, vm->id),
622 vm->name, sizeof(vm->name) - 1,
628 static int pvr2_g_ctrl(struct file *file, void *priv, struct v4l2_control *vc)
630 struct pvr2_v4l2_fh *fh = file->private_data;
631 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
635 ret = pvr2_ctrl_get_value(pvr2_hdw_get_ctrl_v4l(hdw, vc->id),
641 static int pvr2_s_ctrl(struct file *file, void *priv, struct v4l2_control *vc)
643 struct pvr2_v4l2_fh *fh = file->private_data;
644 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
646 return pvr2_ctrl_set_value(pvr2_hdw_get_ctrl_v4l(hdw, vc->id),
650 static int pvr2_g_ext_ctrls(struct file *file, void *priv,
651 struct v4l2_ext_controls *ctls)
653 struct pvr2_v4l2_fh *fh = file->private_data;
654 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
655 struct v4l2_ext_control *ctrl;
661 for (idx = 0; idx < ctls->count; idx++) {
662 ctrl = ctls->controls + idx;
663 ret = pvr2_ctrl_get_value(
664 pvr2_hdw_get_ctrl_v4l(hdw, ctrl->id), &val);
666 ctls->error_idx = idx;
669 /* Ensure that if read as a 64 bit value, the user
670 will still get a hopefully sane value */
677 static int pvr2_s_ext_ctrls(struct file *file, void *priv,
678 struct v4l2_ext_controls *ctls)
680 struct pvr2_v4l2_fh *fh = file->private_data;
681 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
682 struct v4l2_ext_control *ctrl;
687 for (idx = 0; idx < ctls->count; idx++) {
688 ctrl = ctls->controls + idx;
689 ret = pvr2_ctrl_set_value(
690 pvr2_hdw_get_ctrl_v4l(hdw, ctrl->id),
693 ctls->error_idx = idx;
700 static int pvr2_try_ext_ctrls(struct file *file, void *priv,
701 struct v4l2_ext_controls *ctls)
703 struct pvr2_v4l2_fh *fh = file->private_data;
704 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
705 struct v4l2_ext_control *ctrl;
706 struct pvr2_ctrl *pctl;
710 /* For the moment just validate that the requested control
713 for (idx = 0; idx < ctls->count; idx++) {
714 ctrl = ctls->controls + idx;
715 pctl = pvr2_hdw_get_ctrl_v4l(hdw, ctrl->id);
717 ctls->error_idx = idx;
724 static int pvr2_cropcap(struct file *file, void *priv, struct v4l2_cropcap *cap)
726 struct pvr2_v4l2_fh *fh = file->private_data;
727 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
730 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
732 ret = pvr2_hdw_get_cropcap(hdw, cap);
733 cap->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; /* paranoia */
737 static int pvr2_g_crop(struct file *file, void *priv, struct v4l2_crop *crop)
739 struct pvr2_v4l2_fh *fh = file->private_data;
740 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
744 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
746 ret = pvr2_ctrl_get_value(
747 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPL), &val);
751 ret = pvr2_ctrl_get_value(
752 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPT), &val);
756 ret = pvr2_ctrl_get_value(
757 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPW), &val);
761 ret = pvr2_ctrl_get_value(
762 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPH), &val);
765 crop->c.height = val;
769 static int pvr2_s_crop(struct file *file, void *priv, struct v4l2_crop *crop)
771 struct pvr2_v4l2_fh *fh = file->private_data;
772 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
773 struct v4l2_cropcap cap;
776 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
778 cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
779 ret = pvr2_ctrl_set_value(
780 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPL),
784 ret = pvr2_ctrl_set_value(
785 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPT),
789 ret = pvr2_ctrl_set_value(
790 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPW),
794 ret = pvr2_ctrl_set_value(
795 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPH),
802 static int pvr2_log_status(struct file *file, void *priv)
804 struct pvr2_v4l2_fh *fh = file->private_data;
805 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
807 pvr2_hdw_trigger_module_log(hdw);
811 #ifdef CONFIG_VIDEO_ADV_DEBUG
812 static int pvr2_g_register(struct file *file, void *priv, struct v4l2_dbg_register *req)
814 struct pvr2_v4l2_fh *fh = file->private_data;
815 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
819 ret = pvr2_hdw_register_access(
820 hdw, &req->match, req->reg,
826 static int pvr2_s_register(struct file *file, void *priv, struct v4l2_dbg_register *req)
828 struct pvr2_v4l2_fh *fh = file->private_data;
829 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
834 ret = pvr2_hdw_register_access(
835 hdw, &req->match, req->reg,
841 static const struct v4l2_ioctl_ops pvr2_ioctl_ops = {
842 .vidioc_querycap = pvr2_querycap,
843 .vidioc_g_priority = pvr2_g_priority,
844 .vidioc_s_priority = pvr2_s_priority,
845 .vidioc_s_audio = pvr2_s_audio,
846 .vidioc_g_audio = pvr2_g_audio,
847 .vidioc_enumaudio = pvr2_enumaudio,
848 .vidioc_enum_input = pvr2_enum_input,
849 .vidioc_cropcap = pvr2_cropcap,
850 .vidioc_s_crop = pvr2_s_crop,
851 .vidioc_g_crop = pvr2_g_crop,
852 .vidioc_g_input = pvr2_g_input,
853 .vidioc_s_input = pvr2_s_input,
854 .vidioc_g_frequency = pvr2_g_frequency,
855 .vidioc_s_frequency = pvr2_s_frequency,
856 .vidioc_s_tuner = pvr2_s_tuner,
857 .vidioc_g_tuner = pvr2_g_tuner,
858 .vidioc_g_std = pvr2_g_std,
859 .vidioc_s_std = pvr2_s_std,
860 .vidioc_querystd = pvr2_querystd,
861 .vidioc_log_status = pvr2_log_status,
862 .vidioc_enum_fmt_vid_cap = pvr2_enum_fmt_vid_cap,
863 .vidioc_g_fmt_vid_cap = pvr2_g_fmt_vid_cap,
864 .vidioc_s_fmt_vid_cap = pvr2_s_fmt_vid_cap,
865 .vidioc_try_fmt_vid_cap = pvr2_try_fmt_vid_cap,
866 .vidioc_streamon = pvr2_streamon,
867 .vidioc_streamoff = pvr2_streamoff,
868 .vidioc_queryctrl = pvr2_queryctrl,
869 .vidioc_querymenu = pvr2_querymenu,
870 .vidioc_g_ctrl = pvr2_g_ctrl,
871 .vidioc_s_ctrl = pvr2_s_ctrl,
872 .vidioc_g_ext_ctrls = pvr2_g_ext_ctrls,
873 .vidioc_s_ext_ctrls = pvr2_s_ext_ctrls,
874 .vidioc_try_ext_ctrls = pvr2_try_ext_ctrls,
875 #ifdef CONFIG_VIDEO_ADV_DEBUG
876 .vidioc_g_register = pvr2_g_register,
877 .vidioc_s_register = pvr2_s_register,
881 static void pvr2_v4l2_dev_destroy(struct pvr2_v4l2_dev *dip)
883 struct pvr2_hdw *hdw = dip->v4lp->channel.mc_head->hdw;
884 enum pvr2_config cfg = dip->config;
888 /* Construct the unregistration message *before* we actually
889 perform the unregistration step. By doing it this way we don't
890 have to worry about potentially touching deleted resources. */
891 mcnt = scnprintf(msg, sizeof(msg) - 1,
892 "pvrusb2: unregistered device %s [%s]",
893 video_device_node_name(&dip->devbase),
894 pvr2_config_get_name(cfg));
897 pvr2_hdw_v4l_store_minor_number(hdw,dip->minor_type,-1);
903 /* Actual deallocation happens later when all internal references
905 video_unregister_device(&dip->devbase);
907 printk(KERN_INFO "%s\n", msg);
912 static void pvr2_v4l2_dev_disassociate_parent(struct pvr2_v4l2_dev *dip)
915 if (!dip->devbase.parent) return;
916 dip->devbase.parent = NULL;
917 device_move(&dip->devbase.dev, NULL, DPM_ORDER_NONE);
921 static void pvr2_v4l2_destroy_no_lock(struct pvr2_v4l2 *vp)
924 pvr2_v4l2_dev_destroy(vp->dev_video);
925 vp->dev_video = NULL;
928 pvr2_v4l2_dev_destroy(vp->dev_radio);
929 vp->dev_radio = NULL;
932 pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr2_v4l2 id=%p",vp);
933 pvr2_channel_done(&vp->channel);
938 static void pvr2_video_device_release(struct video_device *vdev)
940 struct pvr2_v4l2_dev *dev;
941 dev = container_of(vdev,struct pvr2_v4l2_dev,devbase);
946 static void pvr2_v4l2_internal_check(struct pvr2_channel *chp)
948 struct pvr2_v4l2 *vp;
949 vp = container_of(chp,struct pvr2_v4l2,channel);
950 if (!vp->channel.mc_head->disconnect_flag) return;
951 pvr2_v4l2_dev_disassociate_parent(vp->dev_video);
952 pvr2_v4l2_dev_disassociate_parent(vp->dev_radio);
953 if (vp->vfirst) return;
954 pvr2_v4l2_destroy_no_lock(vp);
958 static long pvr2_v4l2_ioctl(struct file *file,
959 unsigned int cmd, unsigned long arg)
962 struct pvr2_v4l2_fh *fh = file->private_data;
963 struct pvr2_v4l2 *vp = fh->vhead;
964 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
967 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL)
968 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw), cmd);
970 if (!pvr2_hdw_dev_ok(hdw)) {
971 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
972 "ioctl failed - bad or no context");
982 case VIDIOC_S_FREQUENCY:
983 ret = v4l2_prio_check(&vp->prio, fh->prio);
988 ret = video_ioctl2(file, cmd, arg);
990 pvr2_hdw_commit_ctl(hdw);
993 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
994 pvr2_trace(PVR2_TRACE_V4LIOCTL,
995 "pvr2_v4l2_do_ioctl failure, ret=%ld", ret);
997 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
998 pvr2_trace(PVR2_TRACE_V4LIOCTL,
999 "pvr2_v4l2_do_ioctl failure, ret=%ld"
1000 " command was:", ret);
1001 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),
1006 pvr2_trace(PVR2_TRACE_V4LIOCTL,
1007 "pvr2_v4l2_do_ioctl complete, ret=%ld (0x%lx)",
1015 static int pvr2_v4l2_release(struct file *file)
1017 struct pvr2_v4l2_fh *fhp = file->private_data;
1018 struct pvr2_v4l2 *vp = fhp->vhead;
1019 struct pvr2_hdw *hdw = fhp->channel.mc_head->hdw;
1021 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_release");
1024 struct pvr2_stream *sp;
1025 pvr2_hdw_set_streaming(hdw,0);
1026 sp = pvr2_ioread_get_stream(fhp->rhp);
1027 if (sp) pvr2_stream_set_callback(sp,NULL,NULL);
1028 pvr2_ioread_destroy(fhp->rhp);
1032 v4l2_prio_close(&vp->prio, fhp->prio);
1033 file->private_data = NULL;
1036 fhp->vnext->vprev = fhp->vprev;
1038 vp->vlast = fhp->vprev;
1041 fhp->vprev->vnext = fhp->vnext;
1043 vp->vfirst = fhp->vnext;
1048 pvr2_channel_done(&fhp->channel);
1049 pvr2_trace(PVR2_TRACE_STRUCT,
1050 "Destroying pvr_v4l2_fh id=%p",fhp);
1051 if (fhp->input_map) {
1052 kfree(fhp->input_map);
1053 fhp->input_map = NULL;
1056 if (vp->channel.mc_head->disconnect_flag && !vp->vfirst) {
1057 pvr2_v4l2_destroy_no_lock(vp);
1063 static int pvr2_v4l2_open(struct file *file)
1065 struct pvr2_v4l2_dev *dip; /* Our own context pointer */
1066 struct pvr2_v4l2_fh *fhp;
1067 struct pvr2_v4l2 *vp;
1068 struct pvr2_hdw *hdw;
1069 unsigned int input_mask = 0;
1070 unsigned int input_cnt,idx;
1073 dip = container_of(video_devdata(file),struct pvr2_v4l2_dev,devbase);
1076 hdw = vp->channel.hdw;
1078 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_open");
1080 if (!pvr2_hdw_dev_ok(hdw)) {
1081 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,
1082 "pvr2_v4l2_open: hardware not ready");
1086 fhp = kzalloc(sizeof(*fhp),GFP_KERNEL);
1091 init_waitqueue_head(&fhp->wait_data);
1094 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr_v4l2_fh id=%p",fhp);
1095 pvr2_channel_init(&fhp->channel,vp->channel.mc_head);
1097 if (dip->v4l_type == VFL_TYPE_RADIO) {
1098 /* Opening device as a radio, legal input selection subset
1099 is just the radio. */
1100 input_mask = (1 << PVR2_CVAL_INPUT_RADIO);
1102 /* Opening the main V4L device, legal input selection
1103 subset includes all analog inputs. */
1104 input_mask = ((1 << PVR2_CVAL_INPUT_RADIO) |
1105 (1 << PVR2_CVAL_INPUT_TV) |
1106 (1 << PVR2_CVAL_INPUT_COMPOSITE) |
1107 (1 << PVR2_CVAL_INPUT_SVIDEO));
1109 ret = pvr2_channel_limit_inputs(&fhp->channel,input_mask);
1111 pvr2_channel_done(&fhp->channel);
1112 pvr2_trace(PVR2_TRACE_STRUCT,
1113 "Destroying pvr_v4l2_fh id=%p (input mask error)",
1120 input_mask &= pvr2_hdw_get_input_available(hdw);
1122 for (idx = 0; idx < (sizeof(input_mask) << 3); idx++) {
1123 if (input_mask & (1 << idx)) input_cnt++;
1125 fhp->input_cnt = input_cnt;
1126 fhp->input_map = kzalloc(input_cnt,GFP_KERNEL);
1127 if (!fhp->input_map) {
1128 pvr2_channel_done(&fhp->channel);
1129 pvr2_trace(PVR2_TRACE_STRUCT,
1130 "Destroying pvr_v4l2_fh id=%p (input map failure)",
1136 for (idx = 0; idx < (sizeof(input_mask) << 3); idx++) {
1137 if (!(input_mask & (1 << idx))) continue;
1138 fhp->input_map[input_cnt++] = idx;
1142 fhp->vprev = vp->vlast;
1144 vp->vlast->vnext = fhp;
1152 file->private_data = fhp;
1153 v4l2_prio_open(&vp->prio, &fhp->prio);
1155 fhp->fw_mode_flag = pvr2_hdw_cpufw_get_enabled(hdw);
1161 static void pvr2_v4l2_notify(struct pvr2_v4l2_fh *fhp)
1163 wake_up(&fhp->wait_data);
1166 static int pvr2_v4l2_iosetup(struct pvr2_v4l2_fh *fh)
1169 struct pvr2_stream *sp;
1170 struct pvr2_hdw *hdw;
1171 if (fh->rhp) return 0;
1173 if (!fh->pdi->stream) {
1174 /* No stream defined for this node. This means that we're
1175 not currently allowed to stream from this node. */
1179 /* First read() attempt. Try to claim the stream and start
1181 if ((ret = pvr2_channel_claim_stream(&fh->channel,
1182 fh->pdi->stream)) != 0) {
1183 /* Someone else must already have it */
1187 fh->rhp = pvr2_channel_create_mpeg_stream(fh->pdi->stream);
1189 pvr2_channel_claim_stream(&fh->channel,NULL);
1193 hdw = fh->channel.mc_head->hdw;
1194 sp = fh->pdi->stream->stream;
1195 pvr2_stream_set_callback(sp,(pvr2_stream_callback)pvr2_v4l2_notify,fh);
1196 pvr2_hdw_set_stream_type(hdw,fh->pdi->config);
1197 if ((ret = pvr2_hdw_set_streaming(hdw,!0)) < 0) return ret;
1198 return pvr2_ioread_set_enabled(fh->rhp,!0);
1202 static ssize_t pvr2_v4l2_read(struct file *file,
1203 char __user *buff, size_t count, loff_t *ppos)
1205 struct pvr2_v4l2_fh *fh = file->private_data;
1208 if (fh->fw_mode_flag) {
1209 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
1213 unsigned int offs = *ppos;
1215 tbuf = kmalloc(PAGE_SIZE,GFP_KERNEL);
1216 if (!tbuf) return -ENOMEM;
1220 if (c1 > PAGE_SIZE) c1 = PAGE_SIZE;
1221 c2 = pvr2_hdw_cpufw_get(hdw,offs,tbuf,c1);
1227 if (copy_to_user(buff,tbuf,c2)) {
1242 ret = pvr2_v4l2_iosetup(fh);
1249 ret = pvr2_ioread_read(fh->rhp,buff,count);
1250 if (ret >= 0) break;
1251 if (ret != -EAGAIN) break;
1252 if (file->f_flags & O_NONBLOCK) break;
1253 /* Doing blocking I/O. Wait here. */
1254 ret = wait_event_interruptible(
1256 pvr2_ioread_avail(fh->rhp) >= 0);
1264 static unsigned int pvr2_v4l2_poll(struct file *file, poll_table *wait)
1266 unsigned int mask = 0;
1267 struct pvr2_v4l2_fh *fh = file->private_data;
1270 if (fh->fw_mode_flag) {
1271 mask |= POLLIN | POLLRDNORM;
1276 ret = pvr2_v4l2_iosetup(fh);
1277 if (ret) return POLLERR;
1280 poll_wait(file,&fh->wait_data,wait);
1282 if (pvr2_ioread_avail(fh->rhp) >= 0) {
1283 mask |= POLLIN | POLLRDNORM;
1290 static const struct v4l2_file_operations vdev_fops = {
1291 .owner = THIS_MODULE,
1292 .open = pvr2_v4l2_open,
1293 .release = pvr2_v4l2_release,
1294 .read = pvr2_v4l2_read,
1295 .ioctl = pvr2_v4l2_ioctl,
1296 .poll = pvr2_v4l2_poll,
1300 static struct video_device vdev_template = {
1305 static void pvr2_v4l2_dev_init(struct pvr2_v4l2_dev *dip,
1306 struct pvr2_v4l2 *vp,
1309 struct usb_device *usbdev;
1312 struct pvr2_hdw *hdw;
1316 hdw = vp->channel.mc_head->hdw;
1317 usbdev = pvr2_hdw_get_dev(hdw);
1318 dip->v4l_type = v4l_type;
1320 case VFL_TYPE_GRABBER:
1321 dip->stream = &vp->channel.mc_head->video_stream;
1322 dip->config = pvr2_config_mpeg;
1323 dip->minor_type = pvr2_v4l_type_video;
1326 pr_err(KBUILD_MODNAME
1327 ": Failed to set up pvrusb2 v4l video dev"
1328 " due to missing stream instance\n");
1333 dip->config = pvr2_config_vbi;
1334 dip->minor_type = pvr2_v4l_type_vbi;
1337 case VFL_TYPE_RADIO:
1338 dip->stream = &vp->channel.mc_head->video_stream;
1339 dip->config = pvr2_config_mpeg;
1340 dip->minor_type = pvr2_v4l_type_radio;
1344 /* Bail out (this should be impossible) */
1345 pr_err(KBUILD_MODNAME ": Failed to set up pvrusb2 v4l dev"
1346 " due to unrecognized config\n");
1350 memcpy(&dip->devbase,&vdev_template,sizeof(vdev_template));
1351 dip->devbase.release = pvr2_video_device_release;
1352 dip->devbase.ioctl_ops = &pvr2_ioctl_ops;
1355 pvr2_ctrl_get_value(
1356 pvr2_hdw_get_ctrl_by_id(hdw,
1357 PVR2_CID_STDAVAIL), &val);
1358 dip->devbase.tvnorms = (v4l2_std_id)val;
1362 unit_number = pvr2_hdw_get_unit_number(hdw);
1363 if (nr_ptr && (unit_number >= 0) && (unit_number < PVR_NUM)) {
1364 mindevnum = nr_ptr[unit_number];
1366 dip->devbase.parent = &usbdev->dev;
1367 if ((video_register_device(&dip->devbase,
1368 dip->v4l_type, mindevnum) < 0) &&
1369 (video_register_device(&dip->devbase,
1370 dip->v4l_type, -1) < 0)) {
1371 pr_err(KBUILD_MODNAME
1372 ": Failed to register pvrusb2 v4l device\n");
1375 printk(KERN_INFO "pvrusb2: registered device %s [%s]\n",
1376 video_device_node_name(&dip->devbase),
1377 pvr2_config_get_name(dip->config));
1379 pvr2_hdw_v4l_store_minor_number(hdw,
1380 dip->minor_type,dip->devbase.minor);
1384 struct pvr2_v4l2 *pvr2_v4l2_create(struct pvr2_context *mnp)
1386 struct pvr2_v4l2 *vp;
1388 vp = kzalloc(sizeof(*vp),GFP_KERNEL);
1390 pvr2_channel_init(&vp->channel,mnp);
1391 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr2_v4l2 id=%p",vp);
1393 vp->channel.check_func = pvr2_v4l2_internal_check;
1395 /* register streams */
1396 vp->dev_video = kzalloc(sizeof(*vp->dev_video),GFP_KERNEL);
1397 if (!vp->dev_video) goto fail;
1398 pvr2_v4l2_dev_init(vp->dev_video,vp,VFL_TYPE_GRABBER);
1399 if (pvr2_hdw_get_input_available(vp->channel.mc_head->hdw) &
1400 (1 << PVR2_CVAL_INPUT_RADIO)) {
1401 vp->dev_radio = kzalloc(sizeof(*vp->dev_radio),GFP_KERNEL);
1402 if (!vp->dev_radio) goto fail;
1403 pvr2_v4l2_dev_init(vp->dev_radio,vp,VFL_TYPE_RADIO);
1408 pvr2_trace(PVR2_TRACE_STRUCT,"Failure creating pvr2_v4l2 id=%p",vp);
1409 pvr2_v4l2_destroy_no_lock(vp);
1414 Stuff for Emacs to see, in order to encourage consistent editing style:
1415 *** Local Variables: ***
1417 *** fill-column: 75 ***
1418 *** tab-width: 8 ***
1419 *** c-basic-offset: 8 ***