5 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
6 * Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/kernel.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 <media/v4l2-dev.h>
33 #include <media/v4l2-common.h>
39 struct pvr2_v4l2_dev {
40 struct video_device devbase; /* MUST be first! */
41 struct pvr2_v4l2 *v4lp;
42 struct pvr2_context_stream *stream;
43 /* Information about this device: */
44 enum pvr2_config config; /* Expected stream format */
45 int v4l_type; /* V4L defined type for this device node */
46 enum pvr2_v4l_type minor_type; /* pvr2-understood minor device type */
50 struct pvr2_channel channel;
51 struct pvr2_v4l2_dev *dev_info;
52 enum v4l2_priority prio;
53 struct pvr2_ioread *rhp;
55 struct pvr2_v4l2 *vhead;
56 struct pvr2_v4l2_fh *vnext;
57 struct pvr2_v4l2_fh *vprev;
58 wait_queue_head_t wait_data;
64 struct pvr2_channel channel;
65 struct pvr2_v4l2_fh *vfirst;
66 struct pvr2_v4l2_fh *vlast;
68 struct v4l2_prio_state prio;
70 /* streams - Note that these must be separately, individually,
71 * allocated pointers. This is because the v4l core is going to
72 * manage their deletion - separately, individually... */
73 struct pvr2_v4l2_dev *dev_video;
74 struct pvr2_v4l2_dev *dev_radio;
77 static int video_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
78 module_param_array(video_nr, int, NULL, 0444);
79 MODULE_PARM_DESC(video_nr, "Offset for device's video dev minor");
80 static int radio_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
81 module_param_array(radio_nr, int, NULL, 0444);
82 MODULE_PARM_DESC(radio_nr, "Offset for device's radio dev minor");
83 static int vbi_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
84 module_param_array(vbi_nr, int, NULL, 0444);
85 MODULE_PARM_DESC(vbi_nr, "Offset for device's vbi dev minor");
87 static struct v4l2_capability pvr_capability ={
89 .card = "Hauppauge WinTV pvr-usb2",
91 .version = KERNEL_VERSION(0,8,0),
92 .capabilities = (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VBI_CAPTURE |
93 V4L2_CAP_TUNER | V4L2_CAP_AUDIO | V4L2_CAP_RADIO |
98 static struct v4l2_fmtdesc pvr_fmtdesc [] = {
101 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
102 .flags = V4L2_FMT_FLAG_COMPRESSED,
103 .description = "MPEG1/2",
104 // This should really be V4L2_PIX_FMT_MPEG, but xawtv
105 // breaks when I do that.
106 .pixelformat = 0, // V4L2_PIX_FMT_MPEG,
107 .reserved = { 0, 0, 0, 0 }
111 #define PVR_FORMAT_PIX 0
112 #define PVR_FORMAT_VBI 1
114 static struct v4l2_format pvr_format [] = {
116 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
121 // This should really be V4L2_PIX_FMT_MPEG,
122 // but xawtv breaks when I do that.
123 .pixelformat = 0, // V4L2_PIX_FMT_MPEG,
124 .field = V4L2_FIELD_INTERLACED,
125 .bytesperline = 0, // doesn't make sense
127 //FIXME : Don't know what to put here...
128 .sizeimage = (32*1024),
129 .colorspace = 0, // doesn't make sense here
135 .type = V4L2_BUF_TYPE_VBI_CAPTURE,
138 .sampling_rate = 27000000,
140 .samples_per_line = 1443,
141 .sample_format = V4L2_PIX_FMT_GREY,
152 static const char *get_v4l_name(int v4l_type)
155 case VFL_TYPE_GRABBER: return "video";
156 case VFL_TYPE_RADIO: return "radio";
157 case VFL_TYPE_VBI: return "vbi";
166 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
169 static int pvr2_v4l2_do_ioctl(struct inode *inode, struct file *file,
170 unsigned int cmd, void *arg)
172 struct pvr2_v4l2_fh *fh = file->private_data;
173 struct pvr2_v4l2 *vp = fh->vhead;
174 struct pvr2_v4l2_dev *dev_info = fh->dev_info;
175 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
178 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
179 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),cmd);
182 if (!pvr2_hdw_dev_ok(hdw)) {
183 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
184 "ioctl failed - bad or no context");
194 case VIDIOC_S_FREQUENCY:
195 ret = v4l2_prio_check(&vp->prio, &fh->prio);
201 case VIDIOC_QUERYCAP:
203 struct v4l2_capability *cap = arg;
205 memcpy(cap, &pvr_capability, sizeof(struct v4l2_capability));
206 strlcpy(cap->bus_info,pvr2_hdw_get_bus_info(hdw),
207 sizeof(cap->bus_info));
208 strlcpy(cap->card,pvr2_hdw_get_desc(hdw),sizeof(cap->card));
214 case VIDIOC_G_PRIORITY:
216 enum v4l2_priority *p = arg;
218 *p = v4l2_prio_max(&vp->prio);
223 case VIDIOC_S_PRIORITY:
225 enum v4l2_priority *prio = arg;
227 ret = v4l2_prio_change(&vp->prio, &fh->prio, *prio);
233 struct v4l2_standard *vs = (struct v4l2_standard *)arg;
235 ret = pvr2_hdw_get_stdenum_value(hdw,vs,idx+1);
242 ret = pvr2_ctrl_get_value(
243 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR),&val);
244 *(v4l2_std_id *)arg = val;
250 ret = pvr2_ctrl_set_value(
251 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR),
252 *(v4l2_std_id *)arg);
256 case VIDIOC_ENUMINPUT:
258 struct pvr2_ctrl *cptr;
259 struct v4l2_input *vi = (struct v4l2_input *)arg;
260 struct v4l2_input tmp;
263 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
265 memset(&tmp,0,sizeof(tmp));
266 tmp.index = vi->index;
269 case PVR2_CVAL_INPUT_TV:
270 case PVR2_CVAL_INPUT_DTV:
271 case PVR2_CVAL_INPUT_RADIO:
272 tmp.type = V4L2_INPUT_TYPE_TUNER;
274 case PVR2_CVAL_INPUT_SVIDEO:
275 case PVR2_CVAL_INPUT_COMPOSITE:
276 tmp.type = V4L2_INPUT_TYPE_CAMERA;
285 pvr2_ctrl_get_valname(cptr,vi->index,
286 tmp.name,sizeof(tmp.name)-1,&cnt);
289 /* Don't bother with audioset, since this driver currently
290 always switches the audio whenever the video is
293 /* Handling std is a tougher problem. It doesn't make
294 sense in cases where a device might be multi-standard.
295 We could just copy out the current value for the
296 standard, but it can change over time. For now just
299 memcpy(vi, &tmp, sizeof(tmp));
307 struct pvr2_ctrl *cptr;
308 struct v4l2_input *vi = (struct v4l2_input *)arg;
310 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
312 ret = pvr2_ctrl_get_value(cptr,&val);
319 struct v4l2_input *vi = (struct v4l2_input *)arg;
320 ret = pvr2_ctrl_set_value(
321 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
326 case VIDIOC_ENUMAUDIO:
328 /* pkt: FIXME: We are returning one "fake" input here
329 which could very well be called "whatever_we_like".
330 This is for apps that want to see an audio input
331 just to feel comfortable, as well as to test if
332 it can do stereo or sth. There is actually no guarantee
333 that the actual audio input cannot change behind the app's
334 back, but most applications should not mind that either.
336 Hopefully, mplayer people will work with us on this (this
337 whole mess is to support mplayer pvr://), or Hans will come
338 up with a more standard way to say "we have inputs but we
339 don 't want you to change them independent of video" which
342 struct v4l2_audio *vin = arg;
344 if (vin->index > 0) break;
345 strncpy(vin->name, "PVRUSB2 Audio",14);
346 vin->capability = V4L2_AUDCAP_STEREO;
354 /* pkt: FIXME: see above comment (VIDIOC_ENUMAUDIO) */
355 struct v4l2_audio *vin = arg;
356 memset(vin,0,sizeof(*vin));
358 strncpy(vin->name, "PVRUSB2 Audio",14);
359 vin->capability = V4L2_AUDCAP_STEREO;
371 struct v4l2_tuner *vt = (struct v4l2_tuner *)arg;
373 if (vt->index != 0) break; /* Only answer for the 1st tuner */
375 pvr2_hdw_execute_tuner_poll(hdw);
376 ret = pvr2_hdw_get_tuner_status(hdw,vt);
382 struct v4l2_tuner *vt=(struct v4l2_tuner *)arg;
387 ret = pvr2_ctrl_set_value(
388 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_AUDIOMODE),
393 case VIDIOC_S_FREQUENCY:
395 const struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
397 struct v4l2_tuner vt;
399 struct pvr2_ctrl *ctrlp;
400 ret = pvr2_hdw_get_tuner_status(hdw,&vt);
402 ctrlp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
403 ret = pvr2_ctrl_get_value(ctrlp,&cur_input);
405 if (vf->type == V4L2_TUNER_RADIO) {
406 if (cur_input != PVR2_CVAL_INPUT_RADIO) {
407 pvr2_ctrl_set_value(ctrlp,
408 PVR2_CVAL_INPUT_RADIO);
411 if (cur_input == PVR2_CVAL_INPUT_RADIO) {
412 pvr2_ctrl_set_value(ctrlp,
417 if (vt.capability & V4L2_TUNER_CAP_LOW) {
422 ret = pvr2_ctrl_set_value(
423 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),fv);
427 case VIDIOC_G_FREQUENCY:
429 struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
432 struct v4l2_tuner vt;
433 ret = pvr2_hdw_get_tuner_status(hdw,&vt);
435 ret = pvr2_ctrl_get_value(
436 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),
440 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
442 if (cur_input == PVR2_CVAL_INPUT_RADIO) {
443 vf->type = V4L2_TUNER_RADIO;
445 vf->type = V4L2_TUNER_ANALOG_TV;
447 if (vt.capability & V4L2_TUNER_CAP_LOW) {
448 val = (val * 2) / 125;
456 case VIDIOC_ENUM_FMT:
458 struct v4l2_fmtdesc *fd = (struct v4l2_fmtdesc *)arg;
460 /* Only one format is supported : mpeg.*/
464 memcpy(fd, pvr_fmtdesc, sizeof(struct v4l2_fmtdesc));
471 struct v4l2_format *vf = (struct v4l2_format *)arg;
474 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
475 memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
476 sizeof(struct v4l2_format));
479 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES),
481 vf->fmt.pix.width = val;
484 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES),
486 vf->fmt.pix.height = val;
489 case V4L2_BUF_TYPE_VBI_CAPTURE:
490 // ????? Still need to figure out to do VBI correctly
503 struct v4l2_format *vf = (struct v4l2_format *)arg;
507 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
509 struct pvr2_ctrl *hcp,*vcp;
510 int h = vf->fmt.pix.height;
511 int w = vf->fmt.pix.width;
512 hcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES);
513 vcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES);
515 lmin = pvr2_ctrl_get_min(hcp);
516 lmax = pvr2_ctrl_get_max(hcp);
517 ldef = pvr2_ctrl_get_def(hcp);
520 } else if (w < lmin) {
522 } else if (w > lmax) {
525 lmin = pvr2_ctrl_get_min(vcp);
526 lmax = pvr2_ctrl_get_max(vcp);
527 ldef = pvr2_ctrl_get_def(vcp);
530 } else if (h < lmin) {
532 } else if (h > lmax) {
536 memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
537 sizeof(struct v4l2_format));
538 vf->fmt.pix.width = w;
539 vf->fmt.pix.height = h;
541 if (cmd == VIDIOC_S_FMT) {
542 pvr2_ctrl_set_value(hcp,vf->fmt.pix.width);
543 pvr2_ctrl_set_value(vcp,vf->fmt.pix.height);
546 case V4L2_BUF_TYPE_VBI_CAPTURE:
547 // ????? Still need to figure out to do VBI correctly
557 case VIDIOC_STREAMON:
559 if (!fh->dev_info->stream) {
560 /* No stream defined for this node. This means
561 that we're not currently allowed to stream from
566 ret = pvr2_hdw_set_stream_type(hdw,dev_info->config);
567 if (ret < 0) return ret;
568 ret = pvr2_hdw_set_streaming(hdw,!0);
572 case VIDIOC_STREAMOFF:
574 if (!fh->dev_info->stream) {
575 /* No stream defined for this node. This means
576 that we're not currently allowed to stream from
581 ret = pvr2_hdw_set_streaming(hdw,0);
585 case VIDIOC_QUERYCTRL:
587 struct pvr2_ctrl *cptr;
588 struct v4l2_queryctrl *vc = (struct v4l2_queryctrl *)arg;
590 if (vc->id & V4L2_CTRL_FLAG_NEXT_CTRL) {
591 cptr = pvr2_hdw_get_ctrl_nextv4l(
592 hdw,(vc->id & ~V4L2_CTRL_FLAG_NEXT_CTRL));
593 if (cptr) vc->id = pvr2_ctrl_get_v4lid(cptr);
595 cptr = pvr2_hdw_get_ctrl_v4l(hdw,vc->id);
598 pvr2_trace(PVR2_TRACE_V4LIOCTL,
599 "QUERYCTRL id=0x%x not implemented here",
605 pvr2_trace(PVR2_TRACE_V4LIOCTL,
606 "QUERYCTRL id=0x%x mapping name=%s (%s)",
607 vc->id,pvr2_ctrl_get_name(cptr),
608 pvr2_ctrl_get_desc(cptr));
609 strlcpy(vc->name,pvr2_ctrl_get_desc(cptr),sizeof(vc->name));
610 vc->flags = pvr2_ctrl_get_v4lflags(cptr);
611 vc->default_value = pvr2_ctrl_get_def(cptr);
612 switch (pvr2_ctrl_get_type(cptr)) {
614 vc->type = V4L2_CTRL_TYPE_MENU;
616 vc->maximum = pvr2_ctrl_get_cnt(cptr) - 1;
620 vc->type = V4L2_CTRL_TYPE_BOOLEAN;
626 vc->type = V4L2_CTRL_TYPE_INTEGER;
627 vc->minimum = pvr2_ctrl_get_min(cptr);
628 vc->maximum = pvr2_ctrl_get_max(cptr);
632 pvr2_trace(PVR2_TRACE_V4LIOCTL,
633 "QUERYCTRL id=0x%x name=%s not mappable",
634 vc->id,pvr2_ctrl_get_name(cptr));
641 case VIDIOC_QUERYMENU:
643 struct v4l2_querymenu *vm = (struct v4l2_querymenu *)arg;
644 unsigned int cnt = 0;
645 ret = pvr2_ctrl_get_valname(pvr2_hdw_get_ctrl_v4l(hdw,vm->id),
647 vm->name,sizeof(vm->name)-1,
655 struct v4l2_control *vc = (struct v4l2_control *)arg;
657 ret = pvr2_ctrl_get_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
665 struct v4l2_control *vc = (struct v4l2_control *)arg;
666 ret = pvr2_ctrl_set_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
671 case VIDIOC_G_EXT_CTRLS:
673 struct v4l2_ext_controls *ctls =
674 (struct v4l2_ext_controls *)arg;
675 struct v4l2_ext_control *ctrl;
679 for (idx = 0; idx < ctls->count; idx++) {
680 ctrl = ctls->controls + idx;
681 ret = pvr2_ctrl_get_value(
682 pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),&val);
684 ctls->error_idx = idx;
687 /* Ensure that if read as a 64 bit value, the user
688 will still get a hopefully sane value */
695 case VIDIOC_S_EXT_CTRLS:
697 struct v4l2_ext_controls *ctls =
698 (struct v4l2_ext_controls *)arg;
699 struct v4l2_ext_control *ctrl;
702 for (idx = 0; idx < ctls->count; idx++) {
703 ctrl = ctls->controls + idx;
704 ret = pvr2_ctrl_set_value(
705 pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),
708 ctls->error_idx = idx;
715 case VIDIOC_TRY_EXT_CTRLS:
717 struct v4l2_ext_controls *ctls =
718 (struct v4l2_ext_controls *)arg;
719 struct v4l2_ext_control *ctrl;
720 struct pvr2_ctrl *pctl;
722 /* For the moment just validate that the requested control
725 for (idx = 0; idx < ctls->count; idx++) {
726 ctrl = ctls->controls + idx;
727 pctl = pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id);
730 ctls->error_idx = idx;
737 case VIDIOC_LOG_STATUS:
739 pvr2_hdw_trigger_module_log(hdw);
743 #ifdef CONFIG_VIDEO_ADV_DEBUG
744 case VIDIOC_DBG_S_REGISTER:
745 case VIDIOC_DBG_G_REGISTER:
748 struct v4l2_register *req = (struct v4l2_register *)arg;
749 if (cmd == VIDIOC_DBG_S_REGISTER) val = req->val;
750 ret = pvr2_hdw_register_access(
751 hdw,req->match_type,req->match_chip,req->reg,
752 cmd == VIDIOC_DBG_S_REGISTER,&val);
753 if (cmd == VIDIOC_DBG_G_REGISTER) req->val = val;
759 ret = v4l_compat_translate_ioctl(inode,file,cmd,
760 arg,pvr2_v4l2_do_ioctl);
763 pvr2_hdw_commit_ctl(hdw);
766 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
767 pvr2_trace(PVR2_TRACE_V4LIOCTL,
768 "pvr2_v4l2_do_ioctl failure, ret=%d",ret);
770 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
771 pvr2_trace(PVR2_TRACE_V4LIOCTL,
772 "pvr2_v4l2_do_ioctl failure, ret=%d"
773 " command was:",ret);
774 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),
779 pvr2_trace(PVR2_TRACE_V4LIOCTL,
780 "pvr2_v4l2_do_ioctl complete, ret=%d (0x%x)",
787 static void pvr2_v4l2_dev_destroy(struct pvr2_v4l2_dev *dip)
789 int minor_id = dip->devbase.minor;
790 struct pvr2_hdw *hdw = dip->v4lp->channel.mc_head->hdw;
791 enum pvr2_config cfg = dip->config;
792 int v4l_type = dip->v4l_type;
794 pvr2_hdw_v4l_store_minor_number(hdw,dip->minor_type,-1);
800 /* Actual deallocation happens later when all internal references
802 video_unregister_device(&dip->devbase);
804 printk(KERN_INFO "pvrusb2: unregistered device %s%u [%s]\n",
805 get_v4l_name(v4l_type),minor_id & 0x1f,
806 pvr2_config_get_name(cfg));
811 static void pvr2_v4l2_destroy_no_lock(struct pvr2_v4l2 *vp)
814 pvr2_v4l2_dev_destroy(vp->dev_video);
815 vp->dev_video = NULL;
818 pvr2_v4l2_dev_destroy(vp->dev_radio);
819 vp->dev_radio = NULL;
822 pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr2_v4l2 id=%p",vp);
823 pvr2_channel_done(&vp->channel);
828 static void pvr2_video_device_release(struct video_device *vdev)
830 struct pvr2_v4l2_dev *dev;
831 dev = container_of(vdev,struct pvr2_v4l2_dev,devbase);
836 static void pvr2_v4l2_internal_check(struct pvr2_channel *chp)
838 struct pvr2_v4l2 *vp;
839 vp = container_of(chp,struct pvr2_v4l2,channel);
840 if (!vp->channel.mc_head->disconnect_flag) return;
841 if (vp->vfirst) return;
842 pvr2_v4l2_destroy_no_lock(vp);
846 static int pvr2_v4l2_ioctl(struct inode *inode, struct file *file,
847 unsigned int cmd, unsigned long arg)
850 /* Temporary hack : use ivtv api until a v4l2 one is available. */
851 #define IVTV_IOC_G_CODEC 0xFFEE7703
852 #define IVTV_IOC_S_CODEC 0xFFEE7704
853 if (cmd == IVTV_IOC_G_CODEC || cmd == IVTV_IOC_S_CODEC) return 0;
854 return video_usercopy(inode, file, cmd, arg, pvr2_v4l2_do_ioctl);
858 static int pvr2_v4l2_release(struct inode *inode, struct file *file)
860 struct pvr2_v4l2_fh *fhp = file->private_data;
861 struct pvr2_v4l2 *vp = fhp->vhead;
862 struct pvr2_context *mp = fhp->vhead->channel.mc_head;
863 struct pvr2_hdw *hdw = fhp->channel.mc_head->hdw;
865 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_release");
868 struct pvr2_stream *sp;
869 pvr2_hdw_set_streaming(hdw,0);
870 sp = pvr2_ioread_get_stream(fhp->rhp);
871 if (sp) pvr2_stream_set_callback(sp,NULL,NULL);
872 pvr2_ioread_destroy(fhp->rhp);
876 v4l2_prio_close(&vp->prio, &fhp->prio);
877 file->private_data = NULL;
879 pvr2_context_enter(mp); do {
880 /* Restore the previous input selection, if it makes sense
882 if (fhp->dev_info->v4l_type == VFL_TYPE_RADIO) {
883 struct pvr2_ctrl *cp;
885 cp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
886 pvr2_ctrl_get_value(cp,&pval);
887 /* Only restore if we're still selecting the radio */
888 if (pval == PVR2_CVAL_INPUT_RADIO) {
889 pvr2_ctrl_set_value(cp,fhp->prev_input_val);
890 pvr2_hdw_commit_ctl(hdw);
895 fhp->vnext->vprev = fhp->vprev;
897 vp->vlast = fhp->vprev;
900 fhp->vprev->vnext = fhp->vnext;
902 vp->vfirst = fhp->vnext;
907 pvr2_channel_done(&fhp->channel);
908 pvr2_trace(PVR2_TRACE_STRUCT,
909 "Destroying pvr_v4l2_fh id=%p",fhp);
911 if (vp->channel.mc_head->disconnect_flag && !vp->vfirst) {
912 pvr2_v4l2_destroy_no_lock(vp);
914 } while (0); pvr2_context_exit(mp);
919 static int pvr2_v4l2_open(struct inode *inode, struct file *file)
921 struct pvr2_v4l2_dev *dip; /* Our own context pointer */
922 struct pvr2_v4l2_fh *fhp;
923 struct pvr2_v4l2 *vp;
924 struct pvr2_hdw *hdw;
926 dip = container_of(video_devdata(file),struct pvr2_v4l2_dev,devbase);
929 hdw = vp->channel.hdw;
931 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_open");
933 if (!pvr2_hdw_dev_ok(hdw)) {
934 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,
935 "pvr2_v4l2_open: hardware not ready");
939 fhp = kzalloc(sizeof(*fhp),GFP_KERNEL);
944 init_waitqueue_head(&fhp->wait_data);
947 pvr2_context_enter(vp->channel.mc_head); do {
948 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr_v4l2_fh id=%p",fhp);
949 pvr2_channel_init(&fhp->channel,vp->channel.mc_head);
952 fhp->vprev = vp->vlast;
954 vp->vlast->vnext = fhp;
961 /* Opening the /dev/radioX device implies a mode switch.
962 So execute that here. Note that you can get the
963 IDENTICAL effect merely by opening the normal video
964 device and setting the input appropriately. */
965 if (dip->v4l_type == VFL_TYPE_RADIO) {
966 struct pvr2_ctrl *cp;
967 cp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
968 pvr2_ctrl_get_value(cp,&fhp->prev_input_val);
969 pvr2_ctrl_set_value(cp,PVR2_CVAL_INPUT_RADIO);
970 pvr2_hdw_commit_ctl(hdw);
972 } while (0); pvr2_context_exit(vp->channel.mc_head);
975 file->private_data = fhp;
976 v4l2_prio_open(&vp->prio,&fhp->prio);
978 fhp->fw_mode_flag = pvr2_hdw_cpufw_get_enabled(hdw);
984 static void pvr2_v4l2_notify(struct pvr2_v4l2_fh *fhp)
986 wake_up(&fhp->wait_data);
989 static int pvr2_v4l2_iosetup(struct pvr2_v4l2_fh *fh)
992 struct pvr2_stream *sp;
993 struct pvr2_hdw *hdw;
994 if (fh->rhp) return 0;
996 if (!fh->dev_info->stream) {
997 /* No stream defined for this node. This means that we're
998 not currently allowed to stream from this node. */
1002 /* First read() attempt. Try to claim the stream and start
1004 if ((ret = pvr2_channel_claim_stream(&fh->channel,
1005 fh->dev_info->stream)) != 0) {
1006 /* Someone else must already have it */
1010 fh->rhp = pvr2_channel_create_mpeg_stream(fh->dev_info->stream);
1012 pvr2_channel_claim_stream(&fh->channel,NULL);
1016 hdw = fh->channel.mc_head->hdw;
1017 sp = fh->dev_info->stream->stream;
1018 pvr2_stream_set_callback(sp,(pvr2_stream_callback)pvr2_v4l2_notify,fh);
1019 pvr2_hdw_set_stream_type(hdw,fh->dev_info->config);
1020 if ((ret = pvr2_hdw_set_streaming(hdw,!0)) < 0) return ret;
1021 return pvr2_ioread_set_enabled(fh->rhp,!0);
1025 static ssize_t pvr2_v4l2_read(struct file *file,
1026 char __user *buff, size_t count, loff_t *ppos)
1028 struct pvr2_v4l2_fh *fh = file->private_data;
1031 if (fh->fw_mode_flag) {
1032 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
1036 unsigned int offs = *ppos;
1038 tbuf = kmalloc(PAGE_SIZE,GFP_KERNEL);
1039 if (!tbuf) return -ENOMEM;
1043 if (c1 > PAGE_SIZE) c1 = PAGE_SIZE;
1044 c2 = pvr2_hdw_cpufw_get(hdw,offs,tbuf,c1);
1050 if (copy_to_user(buff,tbuf,c2)) {
1065 ret = pvr2_v4l2_iosetup(fh);
1072 ret = pvr2_ioread_read(fh->rhp,buff,count);
1073 if (ret >= 0) break;
1074 if (ret != -EAGAIN) break;
1075 if (file->f_flags & O_NONBLOCK) break;
1076 /* Doing blocking I/O. Wait here. */
1077 ret = wait_event_interruptible(
1079 pvr2_ioread_avail(fh->rhp) >= 0);
1087 static unsigned int pvr2_v4l2_poll(struct file *file, poll_table *wait)
1089 unsigned int mask = 0;
1090 struct pvr2_v4l2_fh *fh = file->private_data;
1093 if (fh->fw_mode_flag) {
1094 mask |= POLLIN | POLLRDNORM;
1099 ret = pvr2_v4l2_iosetup(fh);
1100 if (ret) return POLLERR;
1103 poll_wait(file,&fh->wait_data,wait);
1105 if (pvr2_ioread_avail(fh->rhp) >= 0) {
1106 mask |= POLLIN | POLLRDNORM;
1113 static const struct file_operations vdev_fops = {
1114 .owner = THIS_MODULE,
1115 .open = pvr2_v4l2_open,
1116 .release = pvr2_v4l2_release,
1117 .read = pvr2_v4l2_read,
1118 .ioctl = pvr2_v4l2_ioctl,
1119 .llseek = no_llseek,
1120 .poll = pvr2_v4l2_poll,
1124 static struct video_device vdev_template = {
1125 .owner = THIS_MODULE,
1126 .type = VID_TYPE_CAPTURE | VID_TYPE_TUNER,
1127 .type2 = (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VBI_CAPTURE
1128 | V4L2_CAP_TUNER | V4L2_CAP_AUDIO
1129 | V4L2_CAP_READWRITE),
1134 static void pvr2_v4l2_dev_init(struct pvr2_v4l2_dev *dip,
1135 struct pvr2_v4l2 *vp,
1144 dip->v4l_type = v4l_type;
1146 case VFL_TYPE_GRABBER:
1147 dip->stream = &vp->channel.mc_head->video_stream;
1148 dip->config = pvr2_config_mpeg;
1149 dip->minor_type = pvr2_v4l_type_video;
1152 err("Failed to set up pvrusb2 v4l video dev"
1153 " due to missing stream instance");
1158 dip->config = pvr2_config_vbi;
1159 dip->minor_type = pvr2_v4l_type_vbi;
1162 case VFL_TYPE_RADIO:
1163 dip->stream = &vp->channel.mc_head->video_stream;
1164 dip->config = pvr2_config_mpeg;
1165 dip->minor_type = pvr2_v4l_type_radio;
1169 /* Bail out (this should be impossible) */
1170 err("Failed to set up pvrusb2 v4l dev"
1171 " due to unrecognized config");
1175 memcpy(&dip->devbase,&vdev_template,sizeof(vdev_template));
1176 dip->devbase.release = pvr2_video_device_release;
1179 unit_number = pvr2_hdw_get_unit_number(vp->channel.mc_head->hdw);
1180 if (nr_ptr && (unit_number >= 0) && (unit_number < PVR_NUM)) {
1181 mindevnum = nr_ptr[unit_number];
1183 if ((video_register_device(&dip->devbase,
1184 dip->v4l_type, mindevnum) < 0) &&
1185 (video_register_device(&dip->devbase,
1186 dip->v4l_type, -1) < 0)) {
1187 err("Failed to register pvrusb2 v4l device");
1190 printk(KERN_INFO "pvrusb2: registered device %s%u [%s]\n",
1191 get_v4l_name(dip->v4l_type),dip->devbase.minor & 0x1f,
1192 pvr2_config_get_name(dip->config));
1194 pvr2_hdw_v4l_store_minor_number(vp->channel.mc_head->hdw,
1195 dip->minor_type,dip->devbase.minor);
1199 struct pvr2_v4l2 *pvr2_v4l2_create(struct pvr2_context *mnp)
1201 struct pvr2_v4l2 *vp;
1203 vp = kzalloc(sizeof(*vp),GFP_KERNEL);
1205 vp->dev_video = kzalloc(sizeof(*vp->dev_video),GFP_KERNEL);
1206 vp->dev_radio = kzalloc(sizeof(*vp->dev_radio),GFP_KERNEL);
1207 if (!(vp->dev_video && vp->dev_radio)) {
1208 kfree(vp->dev_video);
1209 kfree(vp->dev_radio);
1213 pvr2_channel_init(&vp->channel,mnp);
1214 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr2_v4l2 id=%p",vp);
1216 vp->channel.check_func = pvr2_v4l2_internal_check;
1218 /* register streams */
1219 pvr2_v4l2_dev_init(vp->dev_video,vp,VFL_TYPE_GRABBER);
1220 pvr2_v4l2_dev_init(vp->dev_radio,vp,VFL_TYPE_RADIO);
1226 Stuff for Emacs to see, in order to encourage consistent editing style:
1227 *** Local Variables: ***
1229 *** fill-column: 75 ***
1230 *** tab-width: 8 ***
1231 *** c-basic-offset: 8 ***