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 enum pvr2_config config;
47 struct pvr2_channel channel;
48 struct pvr2_v4l2_dev *dev_info;
49 enum v4l2_priority prio;
50 struct pvr2_ioread *rhp;
52 struct pvr2_v4l2 *vhead;
53 struct pvr2_v4l2_fh *vnext;
54 struct pvr2_v4l2_fh *vprev;
55 wait_queue_head_t wait_data;
60 struct pvr2_channel channel;
61 struct pvr2_v4l2_fh *vfirst;
62 struct pvr2_v4l2_fh *vlast;
64 struct v4l2_prio_state prio;
66 /* streams - Note that these must be separately, individually,
67 * allocated pointers. This is because the v4l core is going to
68 * manage their deletion - separately, individually... */
69 struct pvr2_v4l2_dev *dev_video;
70 struct pvr2_v4l2_dev *dev_radio;
73 static int video_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
74 module_param_array(video_nr, int, NULL, 0444);
75 MODULE_PARM_DESC(video_nr, "Offset for device's video dev minor");
76 static int radio_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
77 module_param_array(radio_nr, int, NULL, 0444);
78 MODULE_PARM_DESC(radio_nr, "Offset for device's radio dev minor");
79 static int vbi_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
80 module_param_array(vbi_nr, int, NULL, 0444);
81 MODULE_PARM_DESC(vbi_nr, "Offset for device's vbi dev minor");
83 static struct v4l2_capability pvr_capability ={
85 .card = "Hauppauge WinTV pvr-usb2",
87 .version = KERNEL_VERSION(0,8,0),
88 .capabilities = (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VBI_CAPTURE |
89 V4L2_CAP_TUNER | V4L2_CAP_AUDIO | V4L2_CAP_RADIO |
94 static struct v4l2_tuner pvr_v4l2_tuners[]= {
98 .type = V4L2_TUNER_ANALOG_TV,
99 .capability = (V4L2_TUNER_CAP_NORM |
100 V4L2_TUNER_CAP_STEREO |
101 V4L2_TUNER_CAP_LANG1 |
102 V4L2_TUNER_CAP_LANG2),
105 .rxsubchans = V4L2_TUNER_SUB_STEREO,
106 .audmode = V4L2_TUNER_MODE_STEREO,
109 .reserved = {0,0,0,0}
113 static struct v4l2_fmtdesc pvr_fmtdesc [] = {
116 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
117 .flags = V4L2_FMT_FLAG_COMPRESSED,
118 .description = "MPEG1/2",
119 // This should really be V4L2_PIX_FMT_MPEG, but xawtv
120 // breaks when I do that.
121 .pixelformat = 0, // V4L2_PIX_FMT_MPEG,
122 .reserved = { 0, 0, 0, 0 }
126 #define PVR_FORMAT_PIX 0
127 #define PVR_FORMAT_VBI 1
129 static struct v4l2_format pvr_format [] = {
131 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
136 // This should really be V4L2_PIX_FMT_MPEG,
137 // but xawtv breaks when I do that.
138 .pixelformat = 0, // V4L2_PIX_FMT_MPEG,
139 .field = V4L2_FIELD_INTERLACED,
140 .bytesperline = 0, // doesn't make sense
142 //FIXME : Don't know what to put here...
143 .sizeimage = (32*1024),
144 .colorspace = 0, // doesn't make sense here
150 .type = V4L2_BUF_TYPE_VBI_CAPTURE,
153 .sampling_rate = 27000000,
155 .samples_per_line = 1443,
156 .sample_format = V4L2_PIX_FMT_GREY,
169 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
172 static int pvr2_v4l2_do_ioctl(struct inode *inode, struct file *file,
173 unsigned int cmd, void *arg)
175 struct pvr2_v4l2_fh *fh = file->private_data;
176 struct pvr2_v4l2 *vp = fh->vhead;
177 struct pvr2_v4l2_dev *dev_info = fh->dev_info;
178 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
181 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
182 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),cmd);
185 if (!pvr2_hdw_dev_ok(hdw)) {
186 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
187 "ioctl failed - bad or no context");
197 case VIDIOC_S_FREQUENCY:
198 ret = v4l2_prio_check(&vp->prio, &fh->prio);
204 case VIDIOC_QUERYCAP:
206 struct v4l2_capability *cap = arg;
208 memcpy(cap, &pvr_capability, sizeof(struct v4l2_capability));
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_RADIO:
271 tmp.type = V4L2_INPUT_TYPE_TUNER;
273 case PVR2_CVAL_INPUT_SVIDEO:
274 case PVR2_CVAL_INPUT_COMPOSITE:
275 tmp.type = V4L2_INPUT_TYPE_CAMERA;
284 pvr2_ctrl_get_valname(cptr,vi->index,
285 tmp.name,sizeof(tmp.name)-1,&cnt);
288 /* Don't bother with audioset, since this driver currently
289 always switches the audio whenever the video is
292 /* Handling std is a tougher problem. It doesn't make
293 sense in cases where a device might be multi-standard.
294 We could just copy out the current value for the
295 standard, but it can change over time. For now just
298 memcpy(vi, &tmp, sizeof(tmp));
306 struct pvr2_ctrl *cptr;
307 struct v4l2_input *vi = (struct v4l2_input *)arg;
309 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
311 ret = pvr2_ctrl_get_value(cptr,&val);
318 struct v4l2_input *vi = (struct v4l2_input *)arg;
319 ret = pvr2_ctrl_set_value(
320 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
325 case VIDIOC_ENUMAUDIO:
344 struct v4l2_tuner *vt = (struct v4l2_tuner *)arg;
345 unsigned int status_mask;
347 if (vt->index !=0) break;
349 status_mask = pvr2_hdw_get_signal_status(hdw);
351 memcpy(vt, &pvr_v4l2_tuners[vt->index],
352 sizeof(struct v4l2_tuner));
355 if (status_mask & PVR2_SIGNAL_OK) {
356 if (status_mask & PVR2_SIGNAL_STEREO) {
357 vt->rxsubchans = V4L2_TUNER_SUB_STEREO;
359 vt->rxsubchans = V4L2_TUNER_SUB_MONO;
361 if (status_mask & PVR2_SIGNAL_SAP) {
362 vt->rxsubchans |= (V4L2_TUNER_SUB_LANG1 |
363 V4L2_TUNER_SUB_LANG2);
369 ret = pvr2_ctrl_get_value(
370 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_AUDIOMODE),
378 struct v4l2_tuner *vt=(struct v4l2_tuner *)arg;
383 ret = pvr2_ctrl_set_value(
384 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_AUDIOMODE),
388 case VIDIOC_S_FREQUENCY:
390 const struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
393 if (vf->type == V4L2_TUNER_RADIO) {
398 ret = pvr2_ctrl_set_value(
399 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),fv);
403 case VIDIOC_G_FREQUENCY:
405 struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
407 int cur_input = PVR2_CVAL_INPUT_TV;
408 ret = pvr2_ctrl_get_value(
409 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),
413 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
415 if (cur_input == PVR2_CVAL_INPUT_RADIO) {
416 val = (val * 2) / 125;
418 vf->type = V4L2_TUNER_RADIO;
422 vf->type = V4L2_TUNER_ANALOG_TV;
427 case VIDIOC_ENUM_FMT:
429 struct v4l2_fmtdesc *fd = (struct v4l2_fmtdesc *)arg;
431 /* Only one format is supported : mpeg.*/
435 memcpy(fd, pvr_fmtdesc, sizeof(struct v4l2_fmtdesc));
442 struct v4l2_format *vf = (struct v4l2_format *)arg;
445 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
446 memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
447 sizeof(struct v4l2_format));
450 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES),
452 vf->fmt.pix.width = val;
455 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES),
457 vf->fmt.pix.height = val;
460 case V4L2_BUF_TYPE_VBI_CAPTURE:
461 // ????? Still need to figure out to do VBI correctly
474 struct v4l2_format *vf = (struct v4l2_format *)arg;
478 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
480 struct pvr2_ctrl *hcp,*vcp;
481 int h = vf->fmt.pix.height;
482 int w = vf->fmt.pix.width;
483 hcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES);
484 vcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES);
486 lmin = pvr2_ctrl_get_min(hcp);
487 lmax = pvr2_ctrl_get_max(hcp);
490 } else if (w > lmax) {
493 lmin = pvr2_ctrl_get_min(vcp);
494 lmax = pvr2_ctrl_get_max(vcp);
497 } else if (h > lmax) {
501 memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
502 sizeof(struct v4l2_format));
503 vf->fmt.pix.width = w;
504 vf->fmt.pix.height = h;
506 if (cmd == VIDIOC_S_FMT) {
507 pvr2_ctrl_set_value(hcp,vf->fmt.pix.width);
508 pvr2_ctrl_set_value(vcp,vf->fmt.pix.height);
511 case V4L2_BUF_TYPE_VBI_CAPTURE:
512 // ????? Still need to figure out to do VBI correctly
522 case VIDIOC_STREAMON:
524 ret = pvr2_hdw_set_stream_type(hdw,dev_info->config);
525 if (ret < 0) return ret;
526 ret = pvr2_hdw_set_streaming(hdw,!0);
530 case VIDIOC_STREAMOFF:
532 ret = pvr2_hdw_set_streaming(hdw,0);
536 case VIDIOC_QUERYCTRL:
538 struct pvr2_ctrl *cptr;
539 struct v4l2_queryctrl *vc = (struct v4l2_queryctrl *)arg;
541 if (vc->id & V4L2_CTRL_FLAG_NEXT_CTRL) {
542 cptr = pvr2_hdw_get_ctrl_nextv4l(
543 hdw,(vc->id & ~V4L2_CTRL_FLAG_NEXT_CTRL));
544 if (cptr) vc->id = pvr2_ctrl_get_v4lid(cptr);
546 cptr = pvr2_hdw_get_ctrl_v4l(hdw,vc->id);
549 pvr2_trace(PVR2_TRACE_V4LIOCTL,
550 "QUERYCTRL id=0x%x not implemented here",
556 pvr2_trace(PVR2_TRACE_V4LIOCTL,
557 "QUERYCTRL id=0x%x mapping name=%s (%s)",
558 vc->id,pvr2_ctrl_get_name(cptr),
559 pvr2_ctrl_get_desc(cptr));
560 strlcpy(vc->name,pvr2_ctrl_get_desc(cptr),sizeof(vc->name));
561 vc->flags = pvr2_ctrl_get_v4lflags(cptr);
562 vc->default_value = pvr2_ctrl_get_def(cptr);
563 switch (pvr2_ctrl_get_type(cptr)) {
565 vc->type = V4L2_CTRL_TYPE_MENU;
567 vc->maximum = pvr2_ctrl_get_cnt(cptr) - 1;
571 vc->type = V4L2_CTRL_TYPE_BOOLEAN;
577 vc->type = V4L2_CTRL_TYPE_INTEGER;
578 vc->minimum = pvr2_ctrl_get_min(cptr);
579 vc->maximum = pvr2_ctrl_get_max(cptr);
583 pvr2_trace(PVR2_TRACE_V4LIOCTL,
584 "QUERYCTRL id=0x%x name=%s not mappable",
585 vc->id,pvr2_ctrl_get_name(cptr));
592 case VIDIOC_QUERYMENU:
594 struct v4l2_querymenu *vm = (struct v4l2_querymenu *)arg;
595 unsigned int cnt = 0;
596 ret = pvr2_ctrl_get_valname(pvr2_hdw_get_ctrl_v4l(hdw,vm->id),
598 vm->name,sizeof(vm->name)-1,
606 struct v4l2_control *vc = (struct v4l2_control *)arg;
608 ret = pvr2_ctrl_get_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
616 struct v4l2_control *vc = (struct v4l2_control *)arg;
617 ret = pvr2_ctrl_set_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
622 case VIDIOC_G_EXT_CTRLS:
624 struct v4l2_ext_controls *ctls =
625 (struct v4l2_ext_controls *)arg;
626 struct v4l2_ext_control *ctrl;
629 for (idx = 0; idx < ctls->count; idx++) {
630 ctrl = ctls->controls + idx;
631 ret = pvr2_ctrl_get_value(
632 pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),&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 case VIDIOC_S_EXT_CTRLS:
647 struct v4l2_ext_controls *ctls =
648 (struct v4l2_ext_controls *)arg;
649 struct v4l2_ext_control *ctrl;
651 for (idx = 0; idx < ctls->count; idx++) {
652 ctrl = ctls->controls + idx;
653 ret = pvr2_ctrl_set_value(
654 pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),
657 ctls->error_idx = idx;
664 case VIDIOC_TRY_EXT_CTRLS:
666 struct v4l2_ext_controls *ctls =
667 (struct v4l2_ext_controls *)arg;
668 struct v4l2_ext_control *ctrl;
669 struct pvr2_ctrl *pctl;
671 /* For the moment just validate that the requested control
673 for (idx = 0; idx < ctls->count; idx++) {
674 ctrl = ctls->controls + idx;
675 pctl = pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id);
678 ctls->error_idx = idx;
685 case VIDIOC_LOG_STATUS:
687 pvr2_hdw_trigger_module_log(hdw);
691 #ifdef CONFIG_VIDEO_ADV_DEBUG
692 case VIDIOC_INT_G_REGISTER:
693 case VIDIOC_INT_S_REGISTER:
696 struct v4l2_register *req = (struct v4l2_register *)arg;
697 if (cmd == VIDIOC_INT_S_REGISTER) val = req->val;
698 ret = pvr2_hdw_register_access(
699 hdw,req->i2c_id,req->reg,
700 cmd == VIDIOC_INT_S_REGISTER,&val);
701 if (cmd == VIDIOC_INT_G_REGISTER) req->val = val;
707 ret = v4l_compat_translate_ioctl(inode,file,cmd,
708 arg,pvr2_v4l2_do_ioctl);
711 pvr2_hdw_commit_ctl(hdw);
714 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
715 pvr2_trace(PVR2_TRACE_V4LIOCTL,
716 "pvr2_v4l2_do_ioctl failure, ret=%d",ret);
718 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
719 pvr2_trace(PVR2_TRACE_V4LIOCTL,
720 "pvr2_v4l2_do_ioctl failure, ret=%d"
721 " command was:",ret);
722 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),
727 pvr2_trace(PVR2_TRACE_V4LIOCTL,
728 "pvr2_v4l2_do_ioctl complete, ret=%d (0x%x)",
735 static void pvr2_v4l2_dev_destroy(struct pvr2_v4l2_dev *dip)
737 enum pvr2_config cfg = dip->config;
738 int minor_id = dip->devbase.minor;
739 enum pvr2_v4l_type pvt;
740 struct pvr2_hdw *hdw = dip->v4lp->channel.mc_head->hdw;
743 case pvr2_config_mpeg:
744 pvt = pvr2_v4l_type_video;
746 case pvr2_config_vbi:
747 pvt = pvr2_v4l_type_vbi;
749 case pvr2_config_radio:
750 pvt = pvr2_v4l_type_radio;
752 default: /* paranoia */
753 pvt = pvr2_v4l_type_video;
756 pvr2_hdw_v4l_store_minor_number(hdw,pvt,-1);
762 /* Actual deallocation happens later when all internal references
764 video_unregister_device(&dip->devbase);
767 case pvr2_config_mpeg:
768 printk(KERN_INFO "pvrusb2: unregistered device video%d [%s]\n",
770 pvr2_config_get_name(cfg));
772 case pvr2_config_radio:
773 printk(KERN_INFO "pvrusb2: unregistered device radio%d [%s]\n",
775 pvr2_config_get_name(cfg));
777 case pvr2_config_vbi:
778 printk(KERN_INFO "pvrusb2: unregistered device vbi%d [%s]\n",
780 pvr2_config_get_name(cfg));
789 static void pvr2_v4l2_destroy_no_lock(struct pvr2_v4l2 *vp)
792 pvr2_v4l2_dev_destroy(vp->dev_video);
796 pvr2_v4l2_dev_destroy(vp->dev_radio);
800 pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr2_v4l2 id=%p",vp);
801 pvr2_channel_done(&vp->channel);
806 static void pvr2_video_device_release(struct video_device *vdev)
808 struct pvr2_v4l2_dev *dev;
809 dev = container_of(vdev,struct pvr2_v4l2_dev,devbase);
814 static void pvr2_v4l2_internal_check(struct pvr2_channel *chp)
816 struct pvr2_v4l2 *vp;
817 vp = container_of(chp,struct pvr2_v4l2,channel);
818 if (!vp->channel.mc_head->disconnect_flag) return;
819 if (vp->vfirst) return;
820 pvr2_v4l2_destroy_no_lock(vp);
824 static int pvr2_v4l2_ioctl(struct inode *inode, struct file *file,
825 unsigned int cmd, unsigned long arg)
828 /* Temporary hack : use ivtv api until a v4l2 one is available. */
829 #define IVTV_IOC_G_CODEC 0xFFEE7703
830 #define IVTV_IOC_S_CODEC 0xFFEE7704
831 if (cmd == IVTV_IOC_G_CODEC || cmd == IVTV_IOC_S_CODEC) return 0;
832 return video_usercopy(inode, file, cmd, arg, pvr2_v4l2_do_ioctl);
836 static int pvr2_v4l2_release(struct inode *inode, struct file *file)
838 struct pvr2_v4l2_fh *fhp = file->private_data;
839 struct pvr2_v4l2 *vp = fhp->vhead;
840 struct pvr2_context *mp = fhp->vhead->channel.mc_head;
842 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_release");
845 struct pvr2_stream *sp;
846 struct pvr2_hdw *hdw;
847 hdw = fhp->channel.mc_head->hdw;
848 pvr2_hdw_set_streaming(hdw,0);
849 sp = pvr2_ioread_get_stream(fhp->rhp);
850 if (sp) pvr2_stream_set_callback(sp,NULL,NULL);
851 pvr2_ioread_destroy(fhp->rhp);
855 if (fhp->dev_info->config == pvr2_config_radio) {
857 struct pvr2_hdw *hdw;
858 hdw = fhp->channel.mc_head->hdw;
859 if ((ret = pvr2_ctrl_set_value(
860 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
861 PVR2_CVAL_INPUT_TV))) {
866 v4l2_prio_close(&vp->prio, &fhp->prio);
867 file->private_data = NULL;
869 pvr2_context_enter(mp); do {
871 fhp->vnext->vprev = fhp->vprev;
873 vp->vlast = fhp->vprev;
876 fhp->vprev->vnext = fhp->vnext;
878 vp->vfirst = fhp->vnext;
883 pvr2_channel_done(&fhp->channel);
884 pvr2_trace(PVR2_TRACE_STRUCT,
885 "Destroying pvr_v4l2_fh id=%p",fhp);
887 if (vp->channel.mc_head->disconnect_flag && !vp->vfirst) {
888 pvr2_v4l2_destroy_no_lock(vp);
890 } while (0); pvr2_context_exit(mp);
895 static int pvr2_v4l2_open(struct inode *inode, struct file *file)
897 struct pvr2_v4l2_dev *dip; /* Our own context pointer */
898 struct pvr2_v4l2_fh *fhp;
899 struct pvr2_v4l2 *vp;
900 struct pvr2_hdw *hdw;
902 dip = container_of(video_devdata(file),struct pvr2_v4l2_dev,devbase);
905 hdw = vp->channel.hdw;
907 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_open");
909 if (!pvr2_hdw_dev_ok(hdw)) {
910 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,
911 "pvr2_v4l2_open: hardware not ready");
915 fhp = kmalloc(sizeof(*fhp),GFP_KERNEL);
919 memset(fhp,0,sizeof(*fhp));
921 init_waitqueue_head(&fhp->wait_data);
924 pvr2_context_enter(vp->channel.mc_head); do {
925 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr_v4l2_fh id=%p",fhp);
926 pvr2_channel_init(&fhp->channel,vp->channel.mc_head);
928 /* pk: warning, severe ugliness follows. 18+ only.
929 please blaim V4L(ivtv) for braindamaged interfaces,
930 not the implementor. This is probably flawed, but
931 suggestions on how to do this "right" are welcome! */
932 if (dip->config == pvr2_config_radio) {
934 if ((pvr2_channel_check_stream_no_lock(&fhp->channel,
935 fhp->dev_info->stream)) != 0) {
936 /* We can 't switch modes while streaming */
937 pvr2_channel_done(&fhp->channel);
939 pvr2_context_exit(vp->channel.mc_head);
943 if ((ret = pvr2_ctrl_set_value(
944 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
945 PVR2_CVAL_INPUT_RADIO))) {
946 pvr2_channel_done(&fhp->channel);
948 pvr2_context_exit(vp->channel.mc_head);
954 fhp->vprev = vp->vlast;
956 vp->vlast->vnext = fhp;
962 } while (0); pvr2_context_exit(vp->channel.mc_head);
965 file->private_data = fhp;
966 v4l2_prio_open(&vp->prio,&fhp->prio);
968 fhp->fw_mode_flag = pvr2_hdw_cpufw_get_enabled(hdw);
974 static void pvr2_v4l2_notify(struct pvr2_v4l2_fh *fhp)
976 wake_up(&fhp->wait_data);
979 static int pvr2_v4l2_iosetup(struct pvr2_v4l2_fh *fh)
982 struct pvr2_stream *sp;
983 struct pvr2_hdw *hdw;
984 if (fh->rhp) return 0;
986 /* First read() attempt. Try to claim the stream and start
988 if ((ret = pvr2_channel_claim_stream(&fh->channel,
989 fh->dev_info->stream)) != 0) {
990 /* Someone else must already have it */
994 fh->rhp = pvr2_channel_create_mpeg_stream(fh->dev_info->stream);
996 pvr2_channel_claim_stream(&fh->channel,NULL);
1000 hdw = fh->channel.mc_head->hdw;
1001 sp = fh->dev_info->stream->stream;
1002 pvr2_stream_set_callback(sp,(pvr2_stream_callback)pvr2_v4l2_notify,fh);
1003 pvr2_hdw_set_stream_type(hdw,fh->dev_info->config);
1004 pvr2_hdw_set_streaming(hdw,!0);
1005 ret = pvr2_ioread_set_enabled(fh->rhp,!0);
1011 static ssize_t pvr2_v4l2_read(struct file *file,
1012 char __user *buff, size_t count, loff_t *ppos)
1014 struct pvr2_v4l2_fh *fh = file->private_data;
1017 if (fh->fw_mode_flag) {
1018 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
1022 unsigned int offs = *ppos;
1024 tbuf = kmalloc(PAGE_SIZE,GFP_KERNEL);
1025 if (!tbuf) return -ENOMEM;
1029 if (c1 > PAGE_SIZE) c1 = PAGE_SIZE;
1030 c2 = pvr2_hdw_cpufw_get(hdw,offs,tbuf,c1);
1036 if (copy_to_user(buff,tbuf,c2)) {
1050 if (fh->dev_info->config == pvr2_config_radio) {
1051 /* Radio device nodes on this device
1052 cannot be read or written. */
1057 ret = pvr2_v4l2_iosetup(fh);
1064 ret = pvr2_ioread_read(fh->rhp,buff,count);
1065 if (ret >= 0) break;
1066 if (ret != -EAGAIN) break;
1067 if (file->f_flags & O_NONBLOCK) break;
1068 /* Doing blocking I/O. Wait here. */
1069 ret = wait_event_interruptible(
1071 pvr2_ioread_avail(fh->rhp) >= 0);
1079 static unsigned int pvr2_v4l2_poll(struct file *file, poll_table *wait)
1081 unsigned int mask = 0;
1082 struct pvr2_v4l2_fh *fh = file->private_data;
1085 if (fh->fw_mode_flag) {
1086 mask |= POLLIN | POLLRDNORM;
1090 if (fh->dev_info->config == pvr2_config_radio) {
1091 /* Radio device nodes on this device
1092 cannot be read or written. */
1097 ret = pvr2_v4l2_iosetup(fh);
1098 if (ret) return POLLERR;
1101 poll_wait(file,&fh->wait_data,wait);
1103 if (pvr2_ioread_avail(fh->rhp) >= 0) {
1104 mask |= POLLIN | POLLRDNORM;
1111 static const struct file_operations vdev_fops = {
1112 .owner = THIS_MODULE,
1113 .open = pvr2_v4l2_open,
1114 .release = pvr2_v4l2_release,
1115 .read = pvr2_v4l2_read,
1116 .ioctl = pvr2_v4l2_ioctl,
1117 .llseek = no_llseek,
1118 .poll = pvr2_v4l2_poll,
1122 #define VID_HARDWARE_PVRUSB2 38 /* FIXME : need a good value */
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),
1130 .hardware = VID_HARDWARE_PVRUSB2,
1135 static void pvr2_v4l2_dev_init(struct pvr2_v4l2_dev *dip,
1136 struct pvr2_v4l2 *vp,
1137 enum pvr2_config cfg)
1142 enum pvr2_v4l_type pvt;
1147 switch (dip->config) {
1148 case pvr2_config_mpeg:
1149 v4l_type = VFL_TYPE_GRABBER;
1150 pvt = pvr2_v4l_type_video;
1151 dip->stream = &vp->channel.mc_head->video_stream;
1153 case pvr2_config_vbi:
1154 v4l_type = VFL_TYPE_VBI;
1155 pvt = pvr2_v4l_type_vbi;
1157 case pvr2_config_radio:
1158 v4l_type = VFL_TYPE_RADIO;
1159 pvt = pvr2_v4l_type_radio;
1162 /* Bail out (this should be impossible) */
1163 err("Failed to set up pvrusb2 v4l dev"
1164 " due to unrecognized config");
1168 /* radio device doesn 't need its own stream */
1169 if (!dip->stream && dip->config != pvr2_config_radio) {
1170 err("Failed to set up pvrusb2 v4l dev"
1171 " due to missing stream instance");
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 ((unit_number >= 0) && (unit_number < PVR_NUM)) {
1183 mindevnum = vbi_nr[unit_number];
1185 case VFL_TYPE_RADIO:
1186 mindevnum = radio_nr[unit_number];
1188 case VFL_TYPE_GRABBER:
1190 mindevnum = video_nr[unit_number];
1194 if ((video_register_device(&dip->devbase, v4l_type, mindevnum) < 0) &&
1195 (video_register_device(&dip->devbase, v4l_type, -1) < 0)) {
1196 err("Failed to register pvrusb2 v4l device");
1198 switch (dip->config) {
1199 case pvr2_config_mpeg:
1200 printk(KERN_INFO "pvrusb2: registered device video%d [%s]\n",
1201 dip->devbase.minor & 0x1f,
1202 pvr2_config_get_name(dip->config));
1204 case pvr2_config_radio:
1205 printk(KERN_INFO "pvrusb2: registered device radio%d [%s]\n",
1206 dip->devbase.minor & 0x1f,
1207 pvr2_config_get_name(dip->config));
1209 case pvr2_config_vbi:
1210 printk(KERN_INFO "pvrusb2: registered device vbi%d [%s]\n",
1211 dip->devbase.minor & 0x1f,
1212 pvr2_config_get_name(dip->config));
1218 pvr2_hdw_v4l_store_minor_number(vp->channel.mc_head->hdw,
1219 pvt,dip->devbase.minor);
1223 struct pvr2_v4l2 *pvr2_v4l2_create(struct pvr2_context *mnp)
1225 struct pvr2_v4l2 *vp;
1227 vp = kmalloc(sizeof(*vp),GFP_KERNEL);
1229 memset(vp,0,sizeof(*vp));
1230 vp->dev_video = kmalloc(sizeof(*vp->dev_video),GFP_KERNEL);
1231 vp->dev_radio = kmalloc(sizeof(*vp->dev_radio),GFP_KERNEL);
1232 if (!(vp->dev_video && vp->dev_radio)) {
1233 if (vp->dev_video) kfree(vp->dev_video);
1234 if (vp->dev_radio) kfree(vp->dev_radio);
1238 memset(vp->dev_video,0,sizeof(*vp->dev_video));
1239 memset(vp->dev_radio,0,sizeof(*vp->dev_radio));
1240 pvr2_channel_init(&vp->channel,mnp);
1241 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr2_v4l2 id=%p",vp);
1243 vp->channel.check_func = pvr2_v4l2_internal_check;
1245 /* register streams */
1246 pvr2_v4l2_dev_init(vp->dev_video,vp,pvr2_config_mpeg);
1247 pvr2_v4l2_dev_init(vp->dev_radio,vp,pvr2_config_radio);
1253 Stuff for Emacs to see, in order to encourage consistent editing style:
1254 *** Local Variables: ***
1256 *** fill-column: 75 ***
1257 *** tab-width: 8 ***
1258 *** c-basic-offset: 8 ***