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;
63 struct pvr2_channel channel;
64 struct pvr2_v4l2_fh *vfirst;
65 struct pvr2_v4l2_fh *vlast;
67 struct v4l2_prio_state prio;
69 /* streams - Note that these must be separately, individually,
70 * allocated pointers. This is because the v4l core is going to
71 * manage their deletion - separately, individually... */
72 struct pvr2_v4l2_dev *dev_video;
73 struct pvr2_v4l2_dev *dev_radio;
76 static int video_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
77 module_param_array(video_nr, int, NULL, 0444);
78 MODULE_PARM_DESC(video_nr, "Offset for device's video dev minor");
79 static int radio_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
80 module_param_array(radio_nr, int, NULL, 0444);
81 MODULE_PARM_DESC(radio_nr, "Offset for device's radio dev minor");
82 static int vbi_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
83 module_param_array(vbi_nr, int, NULL, 0444);
84 MODULE_PARM_DESC(vbi_nr, "Offset for device's vbi dev minor");
86 static struct v4l2_capability pvr_capability ={
88 .card = "Hauppauge WinTV pvr-usb2",
90 .version = KERNEL_VERSION(0,8,0),
91 .capabilities = (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VBI_CAPTURE |
92 V4L2_CAP_TUNER | V4L2_CAP_AUDIO | V4L2_CAP_RADIO |
97 static struct v4l2_tuner pvr_v4l2_tuners[]= {
101 .type = V4L2_TUNER_ANALOG_TV,
102 .capability = (V4L2_TUNER_CAP_NORM |
103 V4L2_TUNER_CAP_STEREO |
104 V4L2_TUNER_CAP_LANG1 |
105 V4L2_TUNER_CAP_LANG2),
108 .rxsubchans = V4L2_TUNER_SUB_STEREO,
109 .audmode = V4L2_TUNER_MODE_STEREO,
112 .reserved = {0,0,0,0}
116 static struct v4l2_fmtdesc pvr_fmtdesc [] = {
119 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
120 .flags = V4L2_FMT_FLAG_COMPRESSED,
121 .description = "MPEG1/2",
122 // This should really be V4L2_PIX_FMT_MPEG, but xawtv
123 // breaks when I do that.
124 .pixelformat = 0, // V4L2_PIX_FMT_MPEG,
125 .reserved = { 0, 0, 0, 0 }
129 #define PVR_FORMAT_PIX 0
130 #define PVR_FORMAT_VBI 1
132 static struct v4l2_format pvr_format [] = {
134 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
139 // This should really be V4L2_PIX_FMT_MPEG,
140 // but xawtv breaks when I do that.
141 .pixelformat = 0, // V4L2_PIX_FMT_MPEG,
142 .field = V4L2_FIELD_INTERLACED,
143 .bytesperline = 0, // doesn't make sense
145 //FIXME : Don't know what to put here...
146 .sizeimage = (32*1024),
147 .colorspace = 0, // doesn't make sense here
153 .type = V4L2_BUF_TYPE_VBI_CAPTURE,
156 .sampling_rate = 27000000,
158 .samples_per_line = 1443,
159 .sample_format = V4L2_PIX_FMT_GREY,
170 static const char *get_v4l_name(int v4l_type)
173 case VFL_TYPE_GRABBER: return "video";
174 case VFL_TYPE_RADIO: return "radio";
175 case VFL_TYPE_VBI: return "vbi";
184 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
187 static int pvr2_v4l2_do_ioctl(struct inode *inode, struct file *file,
188 unsigned int cmd, void *arg)
190 struct pvr2_v4l2_fh *fh = file->private_data;
191 struct pvr2_v4l2 *vp = fh->vhead;
192 struct pvr2_v4l2_dev *dev_info = fh->dev_info;
193 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
196 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
197 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),cmd);
200 if (!pvr2_hdw_dev_ok(hdw)) {
201 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
202 "ioctl failed - bad or no context");
212 case VIDIOC_S_FREQUENCY:
213 ret = v4l2_prio_check(&vp->prio, &fh->prio);
219 case VIDIOC_QUERYCAP:
221 struct v4l2_capability *cap = arg;
223 memcpy(cap, &pvr_capability, sizeof(struct v4l2_capability));
229 case VIDIOC_G_PRIORITY:
231 enum v4l2_priority *p = arg;
233 *p = v4l2_prio_max(&vp->prio);
238 case VIDIOC_S_PRIORITY:
240 enum v4l2_priority *prio = arg;
242 ret = v4l2_prio_change(&vp->prio, &fh->prio, *prio);
248 struct v4l2_standard *vs = (struct v4l2_standard *)arg;
250 ret = pvr2_hdw_get_stdenum_value(hdw,vs,idx+1);
257 ret = pvr2_ctrl_get_value(
258 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR),&val);
259 *(v4l2_std_id *)arg = val;
265 ret = pvr2_ctrl_set_value(
266 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR),
267 *(v4l2_std_id *)arg);
271 case VIDIOC_ENUMINPUT:
273 struct pvr2_ctrl *cptr;
274 struct v4l2_input *vi = (struct v4l2_input *)arg;
275 struct v4l2_input tmp;
278 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
280 memset(&tmp,0,sizeof(tmp));
281 tmp.index = vi->index;
284 case PVR2_CVAL_INPUT_TV:
285 case PVR2_CVAL_INPUT_RADIO:
286 tmp.type = V4L2_INPUT_TYPE_TUNER;
288 case PVR2_CVAL_INPUT_SVIDEO:
289 case PVR2_CVAL_INPUT_COMPOSITE:
290 tmp.type = V4L2_INPUT_TYPE_CAMERA;
299 pvr2_ctrl_get_valname(cptr,vi->index,
300 tmp.name,sizeof(tmp.name)-1,&cnt);
303 /* Don't bother with audioset, since this driver currently
304 always switches the audio whenever the video is
307 /* Handling std is a tougher problem. It doesn't make
308 sense in cases where a device might be multi-standard.
309 We could just copy out the current value for the
310 standard, but it can change over time. For now just
313 memcpy(vi, &tmp, sizeof(tmp));
321 struct pvr2_ctrl *cptr;
322 struct v4l2_input *vi = (struct v4l2_input *)arg;
324 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
326 ret = pvr2_ctrl_get_value(cptr,&val);
333 struct v4l2_input *vi = (struct v4l2_input *)arg;
334 ret = pvr2_ctrl_set_value(
335 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
340 case VIDIOC_ENUMAUDIO:
359 struct v4l2_tuner *vt = (struct v4l2_tuner *)arg;
360 unsigned int status_mask;
362 if (vt->index !=0) break;
364 status_mask = pvr2_hdw_get_signal_status(hdw);
366 memcpy(vt, &pvr_v4l2_tuners[vt->index],
367 sizeof(struct v4l2_tuner));
370 if (status_mask & PVR2_SIGNAL_OK) {
371 if (status_mask & PVR2_SIGNAL_STEREO) {
372 vt->rxsubchans = V4L2_TUNER_SUB_STEREO;
374 vt->rxsubchans = V4L2_TUNER_SUB_MONO;
376 if (status_mask & PVR2_SIGNAL_SAP) {
377 vt->rxsubchans |= (V4L2_TUNER_SUB_LANG1 |
378 V4L2_TUNER_SUB_LANG2);
384 ret = pvr2_ctrl_get_value(
385 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_AUDIOMODE),
393 struct v4l2_tuner *vt=(struct v4l2_tuner *)arg;
398 ret = pvr2_ctrl_set_value(
399 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_AUDIOMODE),
403 case VIDIOC_S_FREQUENCY:
405 const struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
408 if (vf->type == V4L2_TUNER_RADIO) {
413 ret = pvr2_ctrl_set_value(
414 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),fv);
418 case VIDIOC_G_FREQUENCY:
420 struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
422 int cur_input = PVR2_CVAL_INPUT_TV;
423 ret = pvr2_ctrl_get_value(
424 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),
428 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
430 if (cur_input == PVR2_CVAL_INPUT_RADIO) {
431 val = (val * 2) / 125;
433 vf->type = V4L2_TUNER_RADIO;
437 vf->type = V4L2_TUNER_ANALOG_TV;
442 case VIDIOC_ENUM_FMT:
444 struct v4l2_fmtdesc *fd = (struct v4l2_fmtdesc *)arg;
446 /* Only one format is supported : mpeg.*/
450 memcpy(fd, pvr_fmtdesc, sizeof(struct v4l2_fmtdesc));
457 struct v4l2_format *vf = (struct v4l2_format *)arg;
460 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
461 memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
462 sizeof(struct v4l2_format));
465 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES),
467 vf->fmt.pix.width = val;
470 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES),
472 vf->fmt.pix.height = val;
475 case V4L2_BUF_TYPE_VBI_CAPTURE:
476 // ????? Still need to figure out to do VBI correctly
489 struct v4l2_format *vf = (struct v4l2_format *)arg;
493 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
495 struct pvr2_ctrl *hcp,*vcp;
496 int h = vf->fmt.pix.height;
497 int w = vf->fmt.pix.width;
498 hcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES);
499 vcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES);
501 lmin = pvr2_ctrl_get_min(hcp);
502 lmax = pvr2_ctrl_get_max(hcp);
505 } else if (w > lmax) {
508 lmin = pvr2_ctrl_get_min(vcp);
509 lmax = pvr2_ctrl_get_max(vcp);
512 } else if (h > lmax) {
516 memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
517 sizeof(struct v4l2_format));
518 vf->fmt.pix.width = w;
519 vf->fmt.pix.height = h;
521 if (cmd == VIDIOC_S_FMT) {
522 pvr2_ctrl_set_value(hcp,vf->fmt.pix.width);
523 pvr2_ctrl_set_value(vcp,vf->fmt.pix.height);
526 case V4L2_BUF_TYPE_VBI_CAPTURE:
527 // ????? Still need to figure out to do VBI correctly
537 case VIDIOC_STREAMON:
539 if (!fh->dev_info->stream) {
540 /* No stream defined for this node. This means
541 that we're not currently allowed to stream from
546 ret = pvr2_hdw_set_stream_type(hdw,dev_info->config);
547 if (ret < 0) return ret;
548 ret = pvr2_hdw_set_streaming(hdw,!0);
552 case VIDIOC_STREAMOFF:
554 if (!fh->dev_info->stream) {
555 /* No stream defined for this node. This means
556 that we're not currently allowed to stream from
561 ret = pvr2_hdw_set_streaming(hdw,0);
565 case VIDIOC_QUERYCTRL:
567 struct pvr2_ctrl *cptr;
568 struct v4l2_queryctrl *vc = (struct v4l2_queryctrl *)arg;
570 if (vc->id & V4L2_CTRL_FLAG_NEXT_CTRL) {
571 cptr = pvr2_hdw_get_ctrl_nextv4l(
572 hdw,(vc->id & ~V4L2_CTRL_FLAG_NEXT_CTRL));
573 if (cptr) vc->id = pvr2_ctrl_get_v4lid(cptr);
575 cptr = pvr2_hdw_get_ctrl_v4l(hdw,vc->id);
578 pvr2_trace(PVR2_TRACE_V4LIOCTL,
579 "QUERYCTRL id=0x%x not implemented here",
585 pvr2_trace(PVR2_TRACE_V4LIOCTL,
586 "QUERYCTRL id=0x%x mapping name=%s (%s)",
587 vc->id,pvr2_ctrl_get_name(cptr),
588 pvr2_ctrl_get_desc(cptr));
589 strlcpy(vc->name,pvr2_ctrl_get_desc(cptr),sizeof(vc->name));
590 vc->flags = pvr2_ctrl_get_v4lflags(cptr);
591 vc->default_value = pvr2_ctrl_get_def(cptr);
592 switch (pvr2_ctrl_get_type(cptr)) {
594 vc->type = V4L2_CTRL_TYPE_MENU;
596 vc->maximum = pvr2_ctrl_get_cnt(cptr) - 1;
600 vc->type = V4L2_CTRL_TYPE_BOOLEAN;
606 vc->type = V4L2_CTRL_TYPE_INTEGER;
607 vc->minimum = pvr2_ctrl_get_min(cptr);
608 vc->maximum = pvr2_ctrl_get_max(cptr);
612 pvr2_trace(PVR2_TRACE_V4LIOCTL,
613 "QUERYCTRL id=0x%x name=%s not mappable",
614 vc->id,pvr2_ctrl_get_name(cptr));
621 case VIDIOC_QUERYMENU:
623 struct v4l2_querymenu *vm = (struct v4l2_querymenu *)arg;
624 unsigned int cnt = 0;
625 ret = pvr2_ctrl_get_valname(pvr2_hdw_get_ctrl_v4l(hdw,vm->id),
627 vm->name,sizeof(vm->name)-1,
635 struct v4l2_control *vc = (struct v4l2_control *)arg;
637 ret = pvr2_ctrl_get_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
645 struct v4l2_control *vc = (struct v4l2_control *)arg;
646 ret = pvr2_ctrl_set_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
651 case VIDIOC_G_EXT_CTRLS:
653 struct v4l2_ext_controls *ctls =
654 (struct v4l2_ext_controls *)arg;
655 struct v4l2_ext_control *ctrl;
658 for (idx = 0; idx < ctls->count; idx++) {
659 ctrl = ctls->controls + idx;
660 ret = pvr2_ctrl_get_value(
661 pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),&val);
663 ctls->error_idx = idx;
666 /* Ensure that if read as a 64 bit value, the user
667 will still get a hopefully sane value */
674 case VIDIOC_S_EXT_CTRLS:
676 struct v4l2_ext_controls *ctls =
677 (struct v4l2_ext_controls *)arg;
678 struct v4l2_ext_control *ctrl;
680 for (idx = 0; idx < ctls->count; idx++) {
681 ctrl = ctls->controls + idx;
682 ret = pvr2_ctrl_set_value(
683 pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),
686 ctls->error_idx = idx;
693 case VIDIOC_TRY_EXT_CTRLS:
695 struct v4l2_ext_controls *ctls =
696 (struct v4l2_ext_controls *)arg;
697 struct v4l2_ext_control *ctrl;
698 struct pvr2_ctrl *pctl;
700 /* For the moment just validate that the requested control
702 for (idx = 0; idx < ctls->count; idx++) {
703 ctrl = ctls->controls + idx;
704 pctl = pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id);
707 ctls->error_idx = idx;
714 case VIDIOC_LOG_STATUS:
716 pvr2_hdw_trigger_module_log(hdw);
720 #ifdef CONFIG_VIDEO_ADV_DEBUG
721 case VIDIOC_INT_G_REGISTER:
722 case VIDIOC_INT_S_REGISTER:
725 struct v4l2_register *req = (struct v4l2_register *)arg;
726 if (cmd == VIDIOC_INT_S_REGISTER) val = req->val;
727 ret = pvr2_hdw_register_access(
728 hdw,req->i2c_id,req->reg,
729 cmd == VIDIOC_INT_S_REGISTER,&val);
730 if (cmd == VIDIOC_INT_G_REGISTER) req->val = val;
736 ret = v4l_compat_translate_ioctl(inode,file,cmd,
737 arg,pvr2_v4l2_do_ioctl);
740 pvr2_hdw_commit_ctl(hdw);
743 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
744 pvr2_trace(PVR2_TRACE_V4LIOCTL,
745 "pvr2_v4l2_do_ioctl failure, ret=%d",ret);
747 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
748 pvr2_trace(PVR2_TRACE_V4LIOCTL,
749 "pvr2_v4l2_do_ioctl failure, ret=%d"
750 " command was:",ret);
751 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),
756 pvr2_trace(PVR2_TRACE_V4LIOCTL,
757 "pvr2_v4l2_do_ioctl complete, ret=%d (0x%x)",
764 static void pvr2_v4l2_dev_destroy(struct pvr2_v4l2_dev *dip)
766 int minor_id = dip->devbase.minor;
767 struct pvr2_hdw *hdw = dip->v4lp->channel.mc_head->hdw;
768 enum pvr2_config cfg = dip->config;
769 int v4l_type = dip->v4l_type;
771 pvr2_hdw_v4l_store_minor_number(hdw,dip->minor_type,-1);
777 /* Actual deallocation happens later when all internal references
779 video_unregister_device(&dip->devbase);
781 printk(KERN_INFO "pvrusb2: unregistered device %s%u [%s]\n",
782 get_v4l_name(v4l_type),minor_id & 0x1f,
783 pvr2_config_get_name(cfg));
788 static void pvr2_v4l2_destroy_no_lock(struct pvr2_v4l2 *vp)
791 pvr2_v4l2_dev_destroy(vp->dev_video);
795 pvr2_v4l2_dev_destroy(vp->dev_radio);
799 pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr2_v4l2 id=%p",vp);
800 pvr2_channel_done(&vp->channel);
805 static void pvr2_video_device_release(struct video_device *vdev)
807 struct pvr2_v4l2_dev *dev;
808 dev = container_of(vdev,struct pvr2_v4l2_dev,devbase);
813 static void pvr2_v4l2_internal_check(struct pvr2_channel *chp)
815 struct pvr2_v4l2 *vp;
816 vp = container_of(chp,struct pvr2_v4l2,channel);
817 if (!vp->channel.mc_head->disconnect_flag) return;
818 if (vp->vfirst) return;
819 pvr2_v4l2_destroy_no_lock(vp);
823 static int pvr2_v4l2_ioctl(struct inode *inode, struct file *file,
824 unsigned int cmd, unsigned long arg)
827 /* Temporary hack : use ivtv api until a v4l2 one is available. */
828 #define IVTV_IOC_G_CODEC 0xFFEE7703
829 #define IVTV_IOC_S_CODEC 0xFFEE7704
830 if (cmd == IVTV_IOC_G_CODEC || cmd == IVTV_IOC_S_CODEC) return 0;
831 return video_usercopy(inode, file, cmd, arg, pvr2_v4l2_do_ioctl);
835 static int pvr2_v4l2_release(struct inode *inode, struct file *file)
837 struct pvr2_v4l2_fh *fhp = file->private_data;
838 struct pvr2_v4l2 *vp = fhp->vhead;
839 struct pvr2_context *mp = fhp->vhead->channel.mc_head;
841 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_release");
844 struct pvr2_stream *sp;
845 struct pvr2_hdw *hdw;
846 hdw = fhp->channel.mc_head->hdw;
847 pvr2_hdw_set_streaming(hdw,0);
848 sp = pvr2_ioread_get_stream(fhp->rhp);
849 if (sp) pvr2_stream_set_callback(sp,NULL,NULL);
850 pvr2_ioread_destroy(fhp->rhp);
854 v4l2_prio_close(&vp->prio, &fhp->prio);
855 file->private_data = NULL;
857 pvr2_context_enter(mp); do {
859 fhp->vnext->vprev = fhp->vprev;
861 vp->vlast = fhp->vprev;
864 fhp->vprev->vnext = fhp->vnext;
866 vp->vfirst = fhp->vnext;
871 pvr2_channel_done(&fhp->channel);
872 pvr2_trace(PVR2_TRACE_STRUCT,
873 "Destroying pvr_v4l2_fh id=%p",fhp);
875 if (vp->channel.mc_head->disconnect_flag && !vp->vfirst) {
876 pvr2_v4l2_destroy_no_lock(vp);
878 } while (0); pvr2_context_exit(mp);
883 static int pvr2_v4l2_open(struct inode *inode, struct file *file)
885 struct pvr2_v4l2_dev *dip; /* Our own context pointer */
886 struct pvr2_v4l2_fh *fhp;
887 struct pvr2_v4l2 *vp;
888 struct pvr2_hdw *hdw;
890 dip = container_of(video_devdata(file),struct pvr2_v4l2_dev,devbase);
893 hdw = vp->channel.hdw;
895 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_open");
897 if (!pvr2_hdw_dev_ok(hdw)) {
898 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,
899 "pvr2_v4l2_open: hardware not ready");
903 fhp = kmalloc(sizeof(*fhp),GFP_KERNEL);
907 memset(fhp,0,sizeof(*fhp));
909 init_waitqueue_head(&fhp->wait_data);
912 pvr2_context_enter(vp->channel.mc_head); do {
913 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr_v4l2_fh id=%p",fhp);
914 pvr2_channel_init(&fhp->channel,vp->channel.mc_head);
916 /* Opening the /dev/radioX device implies a mode switch.
917 So execute that here. Note that you can get the
918 IDENTICAL effect merely by opening the normal video
919 device and setting the input appropriately. */
920 if (dip->v4l_type == VFL_TYPE_RADIO) {
922 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
923 PVR2_CVAL_INPUT_RADIO);
924 pvr2_hdw_commit_ctl(hdw);
928 fhp->vprev = vp->vlast;
930 vp->vlast->vnext = fhp;
936 } while (0); pvr2_context_exit(vp->channel.mc_head);
939 file->private_data = fhp;
940 v4l2_prio_open(&vp->prio,&fhp->prio);
942 fhp->fw_mode_flag = pvr2_hdw_cpufw_get_enabled(hdw);
948 static void pvr2_v4l2_notify(struct pvr2_v4l2_fh *fhp)
950 wake_up(&fhp->wait_data);
953 static int pvr2_v4l2_iosetup(struct pvr2_v4l2_fh *fh)
956 struct pvr2_stream *sp;
957 struct pvr2_hdw *hdw;
958 if (fh->rhp) return 0;
960 if (!fh->dev_info->stream) {
961 /* No stream defined for this node. This means that we're
962 not currently allowed to stream from this node. */
966 /* First read() attempt. Try to claim the stream and start
968 if ((ret = pvr2_channel_claim_stream(&fh->channel,
969 fh->dev_info->stream)) != 0) {
970 /* Someone else must already have it */
974 fh->rhp = pvr2_channel_create_mpeg_stream(fh->dev_info->stream);
976 pvr2_channel_claim_stream(&fh->channel,NULL);
980 hdw = fh->channel.mc_head->hdw;
981 sp = fh->dev_info->stream->stream;
982 pvr2_stream_set_callback(sp,(pvr2_stream_callback)pvr2_v4l2_notify,fh);
983 pvr2_hdw_set_stream_type(hdw,fh->dev_info->config);
984 pvr2_hdw_set_streaming(hdw,!0);
985 ret = pvr2_ioread_set_enabled(fh->rhp,!0);
991 static ssize_t pvr2_v4l2_read(struct file *file,
992 char __user *buff, size_t count, loff_t *ppos)
994 struct pvr2_v4l2_fh *fh = file->private_data;
997 if (fh->fw_mode_flag) {
998 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
1002 unsigned int offs = *ppos;
1004 tbuf = kmalloc(PAGE_SIZE,GFP_KERNEL);
1005 if (!tbuf) return -ENOMEM;
1009 if (c1 > PAGE_SIZE) c1 = PAGE_SIZE;
1010 c2 = pvr2_hdw_cpufw_get(hdw,offs,tbuf,c1);
1016 if (copy_to_user(buff,tbuf,c2)) {
1031 ret = pvr2_v4l2_iosetup(fh);
1038 ret = pvr2_ioread_read(fh->rhp,buff,count);
1039 if (ret >= 0) break;
1040 if (ret != -EAGAIN) break;
1041 if (file->f_flags & O_NONBLOCK) break;
1042 /* Doing blocking I/O. Wait here. */
1043 ret = wait_event_interruptible(
1045 pvr2_ioread_avail(fh->rhp) >= 0);
1053 static unsigned int pvr2_v4l2_poll(struct file *file, poll_table *wait)
1055 unsigned int mask = 0;
1056 struct pvr2_v4l2_fh *fh = file->private_data;
1059 if (fh->fw_mode_flag) {
1060 mask |= POLLIN | POLLRDNORM;
1065 ret = pvr2_v4l2_iosetup(fh);
1066 if (ret) return POLLERR;
1069 poll_wait(file,&fh->wait_data,wait);
1071 if (pvr2_ioread_avail(fh->rhp) >= 0) {
1072 mask |= POLLIN | POLLRDNORM;
1079 static const struct file_operations vdev_fops = {
1080 .owner = THIS_MODULE,
1081 .open = pvr2_v4l2_open,
1082 .release = pvr2_v4l2_release,
1083 .read = pvr2_v4l2_read,
1084 .ioctl = pvr2_v4l2_ioctl,
1085 .llseek = no_llseek,
1086 .poll = pvr2_v4l2_poll,
1090 #define VID_HARDWARE_PVRUSB2 38 /* FIXME : need a good value */
1092 static struct video_device vdev_template = {
1093 .owner = THIS_MODULE,
1094 .type = VID_TYPE_CAPTURE | VID_TYPE_TUNER,
1095 .type2 = (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VBI_CAPTURE
1096 | V4L2_CAP_TUNER | V4L2_CAP_AUDIO
1097 | V4L2_CAP_READWRITE),
1098 .hardware = VID_HARDWARE_PVRUSB2,
1103 static void pvr2_v4l2_dev_init(struct pvr2_v4l2_dev *dip,
1104 struct pvr2_v4l2 *vp,
1113 dip->v4l_type = v4l_type;
1115 case VFL_TYPE_GRABBER:
1116 dip->stream = &vp->channel.mc_head->video_stream;
1117 dip->config = pvr2_config_mpeg;
1118 dip->minor_type = pvr2_v4l_type_video;
1122 dip->config = pvr2_config_vbi;
1123 dip->minor_type = pvr2_v4l_type_vbi;
1126 case VFL_TYPE_RADIO:
1127 dip->config = pvr2_config_pcm;
1128 dip->minor_type = pvr2_v4l_type_radio;
1132 /* Bail out (this should be impossible) */
1133 err("Failed to set up pvrusb2 v4l dev"
1134 " due to unrecognized config");
1138 /* radio device doesn 't need its own stream */
1139 if (!dip->stream && dip->v4l_type == VFL_TYPE_GRABBER) {
1140 err("Failed to set up pvrusb2 v4l dev"
1141 " due to missing stream instance");
1145 memcpy(&dip->devbase,&vdev_template,sizeof(vdev_template));
1146 dip->devbase.release = pvr2_video_device_release;
1149 unit_number = pvr2_hdw_get_unit_number(vp->channel.mc_head->hdw);
1150 if (nr_ptr && (unit_number >= 0) && (unit_number < PVR_NUM)) {
1151 mindevnum = nr_ptr[unit_number];
1153 if ((video_register_device(&dip->devbase,
1154 dip->v4l_type, mindevnum) < 0) &&
1155 (video_register_device(&dip->devbase,
1156 dip->v4l_type, -1) < 0)) {
1157 err("Failed to register pvrusb2 v4l device");
1160 printk(KERN_INFO "pvrusb2: registered device %s%u [%s]\n",
1161 get_v4l_name(dip->v4l_type),dip->devbase.minor & 0x1f,
1162 pvr2_config_get_name(dip->config));
1164 pvr2_hdw_v4l_store_minor_number(vp->channel.mc_head->hdw,
1165 dip->minor_type,dip->devbase.minor);
1169 struct pvr2_v4l2 *pvr2_v4l2_create(struct pvr2_context *mnp)
1171 struct pvr2_v4l2 *vp;
1173 vp = kmalloc(sizeof(*vp),GFP_KERNEL);
1175 memset(vp,0,sizeof(*vp));
1176 vp->dev_video = kmalloc(sizeof(*vp->dev_video),GFP_KERNEL);
1177 vp->dev_radio = kmalloc(sizeof(*vp->dev_radio),GFP_KERNEL);
1178 if (!(vp->dev_video && vp->dev_radio)) {
1179 if (vp->dev_video) kfree(vp->dev_video);
1180 if (vp->dev_radio) kfree(vp->dev_radio);
1184 memset(vp->dev_video,0,sizeof(*vp->dev_video));
1185 memset(vp->dev_radio,0,sizeof(*vp->dev_radio));
1186 pvr2_channel_init(&vp->channel,mnp);
1187 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr2_v4l2 id=%p",vp);
1189 vp->channel.check_func = pvr2_v4l2_internal_check;
1191 /* register streams */
1192 pvr2_v4l2_dev_init(vp->dev_video,vp,VFL_TYPE_GRABBER);
1193 pvr2_v4l2_dev_init(vp->dev_radio,vp,VFL_TYPE_RADIO);
1199 Stuff for Emacs to see, in order to encourage consistent editing style:
1200 *** Local Variables: ***
1202 *** fill-column: 75 ***
1203 *** tab-width: 8 ***
1204 *** c-basic-offset: 8 ***