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));
211 case VIDIOC_G_PRIORITY:
213 enum v4l2_priority *p = arg;
215 *p = v4l2_prio_max(&vp->prio);
220 case VIDIOC_S_PRIORITY:
222 enum v4l2_priority *prio = arg;
224 ret = v4l2_prio_change(&vp->prio, &fh->prio, *prio);
230 struct v4l2_standard *vs = (struct v4l2_standard *)arg;
232 ret = pvr2_hdw_get_stdenum_value(hdw,vs,idx+1);
239 ret = pvr2_ctrl_get_value(
240 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR),&val);
241 *(v4l2_std_id *)arg = val;
247 ret = pvr2_ctrl_set_value(
248 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR),
249 *(v4l2_std_id *)arg);
253 case VIDIOC_ENUMINPUT:
255 struct pvr2_ctrl *cptr;
256 struct v4l2_input *vi = (struct v4l2_input *)arg;
257 struct v4l2_input tmp;
260 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
262 memset(&tmp,0,sizeof(tmp));
263 tmp.index = vi->index;
266 case PVR2_CVAL_INPUT_TV:
267 case PVR2_CVAL_INPUT_RADIO:
268 tmp.type = V4L2_INPUT_TYPE_TUNER;
270 case PVR2_CVAL_INPUT_SVIDEO:
271 case PVR2_CVAL_INPUT_COMPOSITE:
272 tmp.type = V4L2_INPUT_TYPE_CAMERA;
281 pvr2_ctrl_get_valname(cptr,vi->index,
282 tmp.name,sizeof(tmp.name)-1,&cnt);
285 /* Don't bother with audioset, since this driver currently
286 always switches the audio whenever the video is
289 /* Handling std is a tougher problem. It doesn't make
290 sense in cases where a device might be multi-standard.
291 We could just copy out the current value for the
292 standard, but it can change over time. For now just
295 memcpy(vi, &tmp, sizeof(tmp));
303 struct pvr2_ctrl *cptr;
304 struct v4l2_input *vi = (struct v4l2_input *)arg;
306 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
308 ret = pvr2_ctrl_get_value(cptr,&val);
315 struct v4l2_input *vi = (struct v4l2_input *)arg;
316 ret = pvr2_ctrl_set_value(
317 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
322 case VIDIOC_ENUMAUDIO:
341 struct v4l2_tuner *vt = (struct v4l2_tuner *)arg;
342 pvr2_hdw_execute_tuner_poll(hdw);
343 ret = pvr2_hdw_get_tuner_status(hdw,vt);
349 struct v4l2_tuner *vt=(struct v4l2_tuner *)arg;
354 ret = pvr2_ctrl_set_value(
355 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_AUDIOMODE),
359 case VIDIOC_S_FREQUENCY:
361 const struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
363 struct v4l2_tuner vt;
365 struct pvr2_ctrl *ctrlp;
366 ret = pvr2_hdw_get_tuner_status(hdw,&vt);
368 ctrlp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
369 ret = pvr2_ctrl_get_value(ctrlp,&cur_input);
371 if (vf->type == V4L2_TUNER_RADIO) {
372 if (cur_input != PVR2_CVAL_INPUT_RADIO) {
373 pvr2_ctrl_set_value(ctrlp,
374 PVR2_CVAL_INPUT_RADIO);
377 if (cur_input == PVR2_CVAL_INPUT_RADIO) {
378 pvr2_ctrl_set_value(ctrlp,
383 if (vt.capability & V4L2_TUNER_CAP_LOW) {
388 ret = pvr2_ctrl_set_value(
389 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),fv);
393 case VIDIOC_G_FREQUENCY:
395 struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
398 struct v4l2_tuner vt;
399 ret = pvr2_hdw_get_tuner_status(hdw,&vt);
401 ret = pvr2_ctrl_get_value(
402 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),
406 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
408 if (cur_input == PVR2_CVAL_INPUT_RADIO) {
409 vf->type = V4L2_TUNER_RADIO;
411 vf->type = V4L2_TUNER_ANALOG_TV;
413 if (vt.capability & V4L2_TUNER_CAP_LOW) {
414 val = (val * 2) / 125;
422 case VIDIOC_ENUM_FMT:
424 struct v4l2_fmtdesc *fd = (struct v4l2_fmtdesc *)arg;
426 /* Only one format is supported : mpeg.*/
430 memcpy(fd, pvr_fmtdesc, sizeof(struct v4l2_fmtdesc));
437 struct v4l2_format *vf = (struct v4l2_format *)arg;
440 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
441 memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
442 sizeof(struct v4l2_format));
445 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES),
447 vf->fmt.pix.width = val;
450 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES),
452 vf->fmt.pix.height = val;
455 case V4L2_BUF_TYPE_VBI_CAPTURE:
456 // ????? Still need to figure out to do VBI correctly
469 struct v4l2_format *vf = (struct v4l2_format *)arg;
473 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
475 struct pvr2_ctrl *hcp,*vcp;
476 int h = vf->fmt.pix.height;
477 int w = vf->fmt.pix.width;
478 hcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES);
479 vcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES);
481 lmin = pvr2_ctrl_get_min(hcp);
482 lmax = pvr2_ctrl_get_max(hcp);
485 } else if (w > lmax) {
488 lmin = pvr2_ctrl_get_min(vcp);
489 lmax = pvr2_ctrl_get_max(vcp);
492 } else if (h > lmax) {
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;
501 if (cmd == VIDIOC_S_FMT) {
502 pvr2_ctrl_set_value(hcp,vf->fmt.pix.width);
503 pvr2_ctrl_set_value(vcp,vf->fmt.pix.height);
506 case V4L2_BUF_TYPE_VBI_CAPTURE:
507 // ????? Still need to figure out to do VBI correctly
517 case VIDIOC_STREAMON:
519 if (!fh->dev_info->stream) {
520 /* No stream defined for this node. This means
521 that we're not currently allowed to stream from
526 ret = pvr2_hdw_set_stream_type(hdw,dev_info->config);
527 if (ret < 0) return ret;
528 ret = pvr2_hdw_set_streaming(hdw,!0);
532 case VIDIOC_STREAMOFF:
534 if (!fh->dev_info->stream) {
535 /* No stream defined for this node. This means
536 that we're not currently allowed to stream from
541 ret = pvr2_hdw_set_streaming(hdw,0);
545 case VIDIOC_QUERYCTRL:
547 struct pvr2_ctrl *cptr;
548 struct v4l2_queryctrl *vc = (struct v4l2_queryctrl *)arg;
550 if (vc->id & V4L2_CTRL_FLAG_NEXT_CTRL) {
551 cptr = pvr2_hdw_get_ctrl_nextv4l(
552 hdw,(vc->id & ~V4L2_CTRL_FLAG_NEXT_CTRL));
553 if (cptr) vc->id = pvr2_ctrl_get_v4lid(cptr);
555 cptr = pvr2_hdw_get_ctrl_v4l(hdw,vc->id);
558 pvr2_trace(PVR2_TRACE_V4LIOCTL,
559 "QUERYCTRL id=0x%x not implemented here",
565 pvr2_trace(PVR2_TRACE_V4LIOCTL,
566 "QUERYCTRL id=0x%x mapping name=%s (%s)",
567 vc->id,pvr2_ctrl_get_name(cptr),
568 pvr2_ctrl_get_desc(cptr));
569 strlcpy(vc->name,pvr2_ctrl_get_desc(cptr),sizeof(vc->name));
570 vc->flags = pvr2_ctrl_get_v4lflags(cptr);
571 vc->default_value = pvr2_ctrl_get_def(cptr);
572 switch (pvr2_ctrl_get_type(cptr)) {
574 vc->type = V4L2_CTRL_TYPE_MENU;
576 vc->maximum = pvr2_ctrl_get_cnt(cptr) - 1;
580 vc->type = V4L2_CTRL_TYPE_BOOLEAN;
586 vc->type = V4L2_CTRL_TYPE_INTEGER;
587 vc->minimum = pvr2_ctrl_get_min(cptr);
588 vc->maximum = pvr2_ctrl_get_max(cptr);
592 pvr2_trace(PVR2_TRACE_V4LIOCTL,
593 "QUERYCTRL id=0x%x name=%s not mappable",
594 vc->id,pvr2_ctrl_get_name(cptr));
601 case VIDIOC_QUERYMENU:
603 struct v4l2_querymenu *vm = (struct v4l2_querymenu *)arg;
604 unsigned int cnt = 0;
605 ret = pvr2_ctrl_get_valname(pvr2_hdw_get_ctrl_v4l(hdw,vm->id),
607 vm->name,sizeof(vm->name)-1,
615 struct v4l2_control *vc = (struct v4l2_control *)arg;
617 ret = pvr2_ctrl_get_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
625 struct v4l2_control *vc = (struct v4l2_control *)arg;
626 ret = pvr2_ctrl_set_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
631 case VIDIOC_G_EXT_CTRLS:
633 struct v4l2_ext_controls *ctls =
634 (struct v4l2_ext_controls *)arg;
635 struct v4l2_ext_control *ctrl;
638 for (idx = 0; idx < ctls->count; idx++) {
639 ctrl = ctls->controls + idx;
640 ret = pvr2_ctrl_get_value(
641 pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),&val);
643 ctls->error_idx = idx;
646 /* Ensure that if read as a 64 bit value, the user
647 will still get a hopefully sane value */
654 case VIDIOC_S_EXT_CTRLS:
656 struct v4l2_ext_controls *ctls =
657 (struct v4l2_ext_controls *)arg;
658 struct v4l2_ext_control *ctrl;
660 for (idx = 0; idx < ctls->count; idx++) {
661 ctrl = ctls->controls + idx;
662 ret = pvr2_ctrl_set_value(
663 pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),
666 ctls->error_idx = idx;
673 case VIDIOC_TRY_EXT_CTRLS:
675 struct v4l2_ext_controls *ctls =
676 (struct v4l2_ext_controls *)arg;
677 struct v4l2_ext_control *ctrl;
678 struct pvr2_ctrl *pctl;
680 /* For the moment just validate that the requested control
682 for (idx = 0; idx < ctls->count; idx++) {
683 ctrl = ctls->controls + idx;
684 pctl = pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id);
687 ctls->error_idx = idx;
694 case VIDIOC_LOG_STATUS:
696 pvr2_hdw_trigger_module_log(hdw);
700 #ifdef CONFIG_VIDEO_ADV_DEBUG
701 case VIDIOC_INT_G_REGISTER:
702 case VIDIOC_INT_S_REGISTER:
705 struct v4l2_register *req = (struct v4l2_register *)arg;
706 if (cmd == VIDIOC_INT_S_REGISTER) val = req->val;
707 ret = pvr2_hdw_register_access(
708 hdw,req->i2c_id,req->reg,
709 cmd == VIDIOC_INT_S_REGISTER,&val);
710 if (cmd == VIDIOC_INT_G_REGISTER) req->val = val;
716 ret = v4l_compat_translate_ioctl(inode,file,cmd,
717 arg,pvr2_v4l2_do_ioctl);
720 pvr2_hdw_commit_ctl(hdw);
723 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
724 pvr2_trace(PVR2_TRACE_V4LIOCTL,
725 "pvr2_v4l2_do_ioctl failure, ret=%d",ret);
727 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
728 pvr2_trace(PVR2_TRACE_V4LIOCTL,
729 "pvr2_v4l2_do_ioctl failure, ret=%d"
730 " command was:",ret);
731 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),
736 pvr2_trace(PVR2_TRACE_V4LIOCTL,
737 "pvr2_v4l2_do_ioctl complete, ret=%d (0x%x)",
744 static void pvr2_v4l2_dev_destroy(struct pvr2_v4l2_dev *dip)
746 int minor_id = dip->devbase.minor;
747 struct pvr2_hdw *hdw = dip->v4lp->channel.mc_head->hdw;
748 enum pvr2_config cfg = dip->config;
749 int v4l_type = dip->v4l_type;
751 pvr2_hdw_v4l_store_minor_number(hdw,dip->minor_type,-1);
757 /* Actual deallocation happens later when all internal references
759 video_unregister_device(&dip->devbase);
761 printk(KERN_INFO "pvrusb2: unregistered device %s%u [%s]\n",
762 get_v4l_name(v4l_type),minor_id & 0x1f,
763 pvr2_config_get_name(cfg));
768 static void pvr2_v4l2_destroy_no_lock(struct pvr2_v4l2 *vp)
771 pvr2_v4l2_dev_destroy(vp->dev_video);
775 pvr2_v4l2_dev_destroy(vp->dev_radio);
779 pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr2_v4l2 id=%p",vp);
780 pvr2_channel_done(&vp->channel);
785 static void pvr2_video_device_release(struct video_device *vdev)
787 struct pvr2_v4l2_dev *dev;
788 dev = container_of(vdev,struct pvr2_v4l2_dev,devbase);
793 static void pvr2_v4l2_internal_check(struct pvr2_channel *chp)
795 struct pvr2_v4l2 *vp;
796 vp = container_of(chp,struct pvr2_v4l2,channel);
797 if (!vp->channel.mc_head->disconnect_flag) return;
798 if (vp->vfirst) return;
799 pvr2_v4l2_destroy_no_lock(vp);
803 static int pvr2_v4l2_ioctl(struct inode *inode, struct file *file,
804 unsigned int cmd, unsigned long arg)
807 /* Temporary hack : use ivtv api until a v4l2 one is available. */
808 #define IVTV_IOC_G_CODEC 0xFFEE7703
809 #define IVTV_IOC_S_CODEC 0xFFEE7704
810 if (cmd == IVTV_IOC_G_CODEC || cmd == IVTV_IOC_S_CODEC) return 0;
811 return video_usercopy(inode, file, cmd, arg, pvr2_v4l2_do_ioctl);
815 static int pvr2_v4l2_release(struct inode *inode, struct file *file)
817 struct pvr2_v4l2_fh *fhp = file->private_data;
818 struct pvr2_v4l2 *vp = fhp->vhead;
819 struct pvr2_context *mp = fhp->vhead->channel.mc_head;
820 struct pvr2_hdw *hdw = fhp->channel.mc_head->hdw;
822 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_release");
825 struct pvr2_stream *sp;
826 pvr2_hdw_set_streaming(hdw,0);
827 sp = pvr2_ioread_get_stream(fhp->rhp);
828 if (sp) pvr2_stream_set_callback(sp,NULL,NULL);
829 pvr2_ioread_destroy(fhp->rhp);
833 v4l2_prio_close(&vp->prio, &fhp->prio);
834 file->private_data = NULL;
836 pvr2_context_enter(mp); do {
837 /* Restore the previous input selection, if it makes sense
839 if (fhp->dev_info->v4l_type == VFL_TYPE_RADIO) {
840 struct pvr2_ctrl *cp;
842 cp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
843 pvr2_ctrl_get_value(cp,&pval);
844 /* Only restore if we're still selecting the radio */
845 if (pval == PVR2_CVAL_INPUT_RADIO) {
846 pvr2_ctrl_set_value(cp,fhp->prev_input_val);
847 pvr2_hdw_commit_ctl(hdw);
852 fhp->vnext->vprev = fhp->vprev;
854 vp->vlast = fhp->vprev;
857 fhp->vprev->vnext = fhp->vnext;
859 vp->vfirst = fhp->vnext;
864 pvr2_channel_done(&fhp->channel);
865 pvr2_trace(PVR2_TRACE_STRUCT,
866 "Destroying pvr_v4l2_fh id=%p",fhp);
868 if (vp->channel.mc_head->disconnect_flag && !vp->vfirst) {
869 pvr2_v4l2_destroy_no_lock(vp);
871 } while (0); pvr2_context_exit(mp);
876 static int pvr2_v4l2_open(struct inode *inode, struct file *file)
878 struct pvr2_v4l2_dev *dip; /* Our own context pointer */
879 struct pvr2_v4l2_fh *fhp;
880 struct pvr2_v4l2 *vp;
881 struct pvr2_hdw *hdw;
883 dip = container_of(video_devdata(file),struct pvr2_v4l2_dev,devbase);
886 hdw = vp->channel.hdw;
888 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_open");
890 if (!pvr2_hdw_dev_ok(hdw)) {
891 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,
892 "pvr2_v4l2_open: hardware not ready");
896 fhp = kzalloc(sizeof(*fhp),GFP_KERNEL);
901 init_waitqueue_head(&fhp->wait_data);
904 pvr2_context_enter(vp->channel.mc_head); do {
905 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr_v4l2_fh id=%p",fhp);
906 pvr2_channel_init(&fhp->channel,vp->channel.mc_head);
909 fhp->vprev = vp->vlast;
911 vp->vlast->vnext = fhp;
918 /* Opening the /dev/radioX device implies a mode switch.
919 So execute that here. Note that you can get the
920 IDENTICAL effect merely by opening the normal video
921 device and setting the input appropriately. */
922 if (dip->v4l_type == VFL_TYPE_RADIO) {
923 struct pvr2_ctrl *cp;
924 cp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
925 pvr2_ctrl_get_value(cp,&fhp->prev_input_val);
926 pvr2_ctrl_set_value(cp,PVR2_CVAL_INPUT_RADIO);
927 pvr2_hdw_commit_ctl(hdw);
929 } while (0); pvr2_context_exit(vp->channel.mc_head);
932 file->private_data = fhp;
933 v4l2_prio_open(&vp->prio,&fhp->prio);
935 fhp->fw_mode_flag = pvr2_hdw_cpufw_get_enabled(hdw);
941 static void pvr2_v4l2_notify(struct pvr2_v4l2_fh *fhp)
943 wake_up(&fhp->wait_data);
946 static int pvr2_v4l2_iosetup(struct pvr2_v4l2_fh *fh)
949 struct pvr2_stream *sp;
950 struct pvr2_hdw *hdw;
951 if (fh->rhp) return 0;
953 if (!fh->dev_info->stream) {
954 /* No stream defined for this node. This means that we're
955 not currently allowed to stream from this node. */
959 /* First read() attempt. Try to claim the stream and start
961 if ((ret = pvr2_channel_claim_stream(&fh->channel,
962 fh->dev_info->stream)) != 0) {
963 /* Someone else must already have it */
967 fh->rhp = pvr2_channel_create_mpeg_stream(fh->dev_info->stream);
969 pvr2_channel_claim_stream(&fh->channel,NULL);
973 hdw = fh->channel.mc_head->hdw;
974 sp = fh->dev_info->stream->stream;
975 pvr2_stream_set_callback(sp,(pvr2_stream_callback)pvr2_v4l2_notify,fh);
976 pvr2_hdw_set_stream_type(hdw,fh->dev_info->config);
977 pvr2_hdw_set_streaming(hdw,!0);
978 ret = pvr2_ioread_set_enabled(fh->rhp,!0);
984 static ssize_t pvr2_v4l2_read(struct file *file,
985 char __user *buff, size_t count, loff_t *ppos)
987 struct pvr2_v4l2_fh *fh = file->private_data;
990 if (fh->fw_mode_flag) {
991 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
995 unsigned int offs = *ppos;
997 tbuf = kmalloc(PAGE_SIZE,GFP_KERNEL);
998 if (!tbuf) return -ENOMEM;
1002 if (c1 > PAGE_SIZE) c1 = PAGE_SIZE;
1003 c2 = pvr2_hdw_cpufw_get(hdw,offs,tbuf,c1);
1009 if (copy_to_user(buff,tbuf,c2)) {
1024 ret = pvr2_v4l2_iosetup(fh);
1031 ret = pvr2_ioread_read(fh->rhp,buff,count);
1032 if (ret >= 0) break;
1033 if (ret != -EAGAIN) break;
1034 if (file->f_flags & O_NONBLOCK) break;
1035 /* Doing blocking I/O. Wait here. */
1036 ret = wait_event_interruptible(
1038 pvr2_ioread_avail(fh->rhp) >= 0);
1046 static unsigned int pvr2_v4l2_poll(struct file *file, poll_table *wait)
1048 unsigned int mask = 0;
1049 struct pvr2_v4l2_fh *fh = file->private_data;
1052 if (fh->fw_mode_flag) {
1053 mask |= POLLIN | POLLRDNORM;
1058 ret = pvr2_v4l2_iosetup(fh);
1059 if (ret) return POLLERR;
1062 poll_wait(file,&fh->wait_data,wait);
1064 if (pvr2_ioread_avail(fh->rhp) >= 0) {
1065 mask |= POLLIN | POLLRDNORM;
1072 static const struct file_operations vdev_fops = {
1073 .owner = THIS_MODULE,
1074 .open = pvr2_v4l2_open,
1075 .release = pvr2_v4l2_release,
1076 .read = pvr2_v4l2_read,
1077 .ioctl = pvr2_v4l2_ioctl,
1078 .llseek = no_llseek,
1079 .poll = pvr2_v4l2_poll,
1083 #define VID_HARDWARE_PVRUSB2 38 /* FIXME : need a good value */
1085 static struct video_device vdev_template = {
1086 .owner = THIS_MODULE,
1087 .type = VID_TYPE_CAPTURE | VID_TYPE_TUNER,
1088 .type2 = (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VBI_CAPTURE
1089 | V4L2_CAP_TUNER | V4L2_CAP_AUDIO
1090 | V4L2_CAP_READWRITE),
1091 .hardware = VID_HARDWARE_PVRUSB2,
1096 static void pvr2_v4l2_dev_init(struct pvr2_v4l2_dev *dip,
1097 struct pvr2_v4l2 *vp,
1106 dip->v4l_type = v4l_type;
1108 case VFL_TYPE_GRABBER:
1109 dip->stream = &vp->channel.mc_head->video_stream;
1110 dip->config = pvr2_config_mpeg;
1111 dip->minor_type = pvr2_v4l_type_video;
1114 err("Failed to set up pvrusb2 v4l video dev"
1115 " due to missing stream instance");
1120 dip->config = pvr2_config_vbi;
1121 dip->minor_type = pvr2_v4l_type_vbi;
1124 case VFL_TYPE_RADIO:
1125 dip->stream = &vp->channel.mc_head->video_stream;
1126 dip->config = pvr2_config_mpeg;
1127 dip->minor_type = pvr2_v4l_type_radio;
1131 /* Bail out (this should be impossible) */
1132 err("Failed to set up pvrusb2 v4l dev"
1133 " due to unrecognized config");
1137 memcpy(&dip->devbase,&vdev_template,sizeof(vdev_template));
1138 dip->devbase.release = pvr2_video_device_release;
1141 unit_number = pvr2_hdw_get_unit_number(vp->channel.mc_head->hdw);
1142 if (nr_ptr && (unit_number >= 0) && (unit_number < PVR_NUM)) {
1143 mindevnum = nr_ptr[unit_number];
1145 if ((video_register_device(&dip->devbase,
1146 dip->v4l_type, mindevnum) < 0) &&
1147 (video_register_device(&dip->devbase,
1148 dip->v4l_type, -1) < 0)) {
1149 err("Failed to register pvrusb2 v4l device");
1152 printk(KERN_INFO "pvrusb2: registered device %s%u [%s]\n",
1153 get_v4l_name(dip->v4l_type),dip->devbase.minor & 0x1f,
1154 pvr2_config_get_name(dip->config));
1156 pvr2_hdw_v4l_store_minor_number(vp->channel.mc_head->hdw,
1157 dip->minor_type,dip->devbase.minor);
1161 struct pvr2_v4l2 *pvr2_v4l2_create(struct pvr2_context *mnp)
1163 struct pvr2_v4l2 *vp;
1165 vp = kzalloc(sizeof(*vp),GFP_KERNEL);
1167 vp->dev_video = kzalloc(sizeof(*vp->dev_video),GFP_KERNEL);
1168 vp->dev_radio = kzalloc(sizeof(*vp->dev_radio),GFP_KERNEL);
1169 if (!(vp->dev_video && vp->dev_radio)) {
1170 kfree(vp->dev_video);
1171 kfree(vp->dev_radio);
1175 pvr2_channel_init(&vp->channel,mnp);
1176 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr2_v4l2 id=%p",vp);
1178 vp->channel.check_func = pvr2_v4l2_internal_check;
1180 /* register streams */
1181 pvr2_v4l2_dev_init(vp->dev_video,vp,VFL_TYPE_GRABBER);
1182 pvr2_v4l2_dev_init(vp->dev_radio,vp,VFL_TYPE_RADIO);
1188 Stuff for Emacs to see, in order to encourage consistent editing style:
1189 *** Local Variables: ***
1191 *** fill-column: 75 ***
1192 *** tab-width: 8 ***
1193 *** c-basic-offset: 8 ***