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>
35 #define PVR2_NR_STREAMS 3
41 struct pvr2_v4l2_dev {
42 struct video_device devbase; /* MUST be first! */
43 struct pvr2_v4l2 *v4lp;
44 struct pvr2_context_stream *stream;
45 enum pvr2_config config;
49 struct pvr2_channel channel;
50 struct pvr2_v4l2_dev *dev_info;
51 enum v4l2_priority prio;
52 struct pvr2_ioread *rhp;
54 struct pvr2_v4l2 *vhead;
55 struct pvr2_v4l2_fh *vnext;
56 struct pvr2_v4l2_fh *vprev;
57 wait_queue_head_t wait_data;
62 struct pvr2_channel channel;
63 struct pvr2_v4l2_fh *vfirst;
64 struct pvr2_v4l2_fh *vlast;
66 struct v4l2_prio_state prio;
69 struct pvr2_v4l2_dev *vdev;
72 static int video_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
73 module_param_array(video_nr, int, NULL, 0444);
74 MODULE_PARM_DESC(video_nr, "Offset for device's minor");
76 static struct v4l2_capability pvr_capability ={
78 .card = "Hauppauge WinTV pvr-usb2",
80 .version = KERNEL_VERSION(0,8,0),
81 .capabilities = (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VBI_CAPTURE |
82 V4L2_CAP_TUNER | V4L2_CAP_AUDIO | V4L2_CAP_RADIO |
87 static struct v4l2_tuner pvr_v4l2_tuners[]= {
91 .type = V4L2_TUNER_ANALOG_TV,
92 .capability = (V4L2_TUNER_CAP_NORM |
93 V4L2_TUNER_CAP_STEREO |
94 V4L2_TUNER_CAP_LANG1 |
95 V4L2_TUNER_CAP_LANG2),
98 .rxsubchans = V4L2_TUNER_SUB_STEREO,
99 .audmode = V4L2_TUNER_MODE_STEREO,
102 .reserved = {0,0,0,0}
106 static struct v4l2_fmtdesc pvr_fmtdesc [] = {
109 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
110 .flags = V4L2_FMT_FLAG_COMPRESSED,
111 .description = "MPEG1/2",
112 // This should really be V4L2_PIX_FMT_MPEG, but xawtv
113 // breaks when I do that.
114 .pixelformat = 0, // V4L2_PIX_FMT_MPEG,
115 .reserved = { 0, 0, 0, 0 }
119 #define PVR_FORMAT_PIX 0
120 #define PVR_FORMAT_VBI 1
122 static struct v4l2_format pvr_format [] = {
124 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
129 // This should really be V4L2_PIX_FMT_MPEG,
130 // but xawtv breaks when I do that.
131 .pixelformat = 0, // V4L2_PIX_FMT_MPEG,
132 .field = V4L2_FIELD_INTERLACED,
133 .bytesperline = 0, // doesn't make sense
135 //FIXME : Don't know what to put here...
136 .sizeimage = (32*1024),
137 .colorspace = 0, // doesn't make sense here
143 .type = V4L2_BUF_TYPE_VBI_CAPTURE,
146 .sampling_rate = 27000000,
148 .samples_per_line = 1443,
149 .sample_format = V4L2_PIX_FMT_GREY,
162 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
165 static int pvr2_v4l2_do_ioctl(struct inode *inode, struct file *file,
166 unsigned int cmd, void *arg)
168 struct pvr2_v4l2_fh *fh = file->private_data;
169 struct pvr2_v4l2 *vp = fh->vhead;
170 struct pvr2_v4l2_dev *dev_info = fh->dev_info;
171 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
174 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
175 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),cmd);
178 if (!pvr2_hdw_dev_ok(hdw)) {
179 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
180 "ioctl failed - bad or no context");
190 case VIDIOC_S_FREQUENCY:
191 ret = v4l2_prio_check(&vp->prio, &fh->prio);
197 case VIDIOC_QUERYCAP:
199 struct v4l2_capability *cap = arg;
201 memcpy(cap, &pvr_capability, sizeof(struct v4l2_capability));
207 case VIDIOC_G_PRIORITY:
209 enum v4l2_priority *p = arg;
211 *p = v4l2_prio_max(&vp->prio);
216 case VIDIOC_S_PRIORITY:
218 enum v4l2_priority *prio = arg;
220 ret = v4l2_prio_change(&vp->prio, &fh->prio, *prio);
226 struct v4l2_standard *vs = (struct v4l2_standard *)arg;
228 ret = pvr2_hdw_get_stdenum_value(hdw,vs,idx+1);
235 ret = pvr2_ctrl_get_value(
236 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR),&val);
237 *(v4l2_std_id *)arg = val;
243 ret = pvr2_ctrl_set_value(
244 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR),
245 *(v4l2_std_id *)arg);
249 case VIDIOC_ENUMINPUT:
251 struct pvr2_ctrl *cptr;
252 struct v4l2_input *vi = (struct v4l2_input *)arg;
253 struct v4l2_input tmp;
256 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
258 memset(&tmp,0,sizeof(tmp));
259 tmp.index = vi->index;
262 case PVR2_CVAL_INPUT_TV:
263 case PVR2_CVAL_INPUT_RADIO:
264 tmp.type = V4L2_INPUT_TYPE_TUNER;
266 case PVR2_CVAL_INPUT_SVIDEO:
267 case PVR2_CVAL_INPUT_COMPOSITE:
268 tmp.type = V4L2_INPUT_TYPE_CAMERA;
277 pvr2_ctrl_get_valname(cptr,vi->index,
278 tmp.name,sizeof(tmp.name)-1,&cnt);
281 /* Don't bother with audioset, since this driver currently
282 always switches the audio whenever the video is
285 /* Handling std is a tougher problem. It doesn't make
286 sense in cases where a device might be multi-standard.
287 We could just copy out the current value for the
288 standard, but it can change over time. For now just
291 memcpy(vi, &tmp, sizeof(tmp));
299 struct pvr2_ctrl *cptr;
300 struct v4l2_input *vi = (struct v4l2_input *)arg;
302 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
304 ret = pvr2_ctrl_get_value(cptr,&val);
311 struct v4l2_input *vi = (struct v4l2_input *)arg;
312 ret = pvr2_ctrl_set_value(
313 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
318 case VIDIOC_ENUMAUDIO:
337 struct v4l2_tuner *vt = (struct v4l2_tuner *)arg;
338 unsigned int status_mask;
340 if (vt->index !=0) break;
342 status_mask = pvr2_hdw_get_signal_status(hdw);
344 memcpy(vt, &pvr_v4l2_tuners[vt->index],
345 sizeof(struct v4l2_tuner));
348 if (status_mask & PVR2_SIGNAL_OK) {
349 if (status_mask & PVR2_SIGNAL_STEREO) {
350 vt->rxsubchans = V4L2_TUNER_SUB_STEREO;
352 vt->rxsubchans = V4L2_TUNER_SUB_MONO;
354 if (status_mask & PVR2_SIGNAL_SAP) {
355 vt->rxsubchans |= (V4L2_TUNER_SUB_LANG1 |
356 V4L2_TUNER_SUB_LANG2);
362 ret = pvr2_ctrl_get_value(
363 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_AUDIOMODE),
371 struct v4l2_tuner *vt=(struct v4l2_tuner *)arg;
376 ret = pvr2_ctrl_set_value(
377 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_AUDIOMODE),
381 case VIDIOC_S_FREQUENCY:
383 const struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
384 ret = pvr2_ctrl_set_value(
385 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),
386 vf->frequency * 62500);
390 case VIDIOC_G_FREQUENCY:
392 struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
394 ret = pvr2_ctrl_get_value(
395 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),
402 case VIDIOC_ENUM_FMT:
404 struct v4l2_fmtdesc *fd = (struct v4l2_fmtdesc *)arg;
406 /* Only one format is supported : mpeg.*/
410 memcpy(fd, pvr_fmtdesc, sizeof(struct v4l2_fmtdesc));
417 struct v4l2_format *vf = (struct v4l2_format *)arg;
420 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
421 memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
422 sizeof(struct v4l2_format));
425 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES),
427 vf->fmt.pix.width = val;
430 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES),
432 vf->fmt.pix.height = val;
435 case V4L2_BUF_TYPE_VBI_CAPTURE:
436 // ????? Still need to figure out to do VBI correctly
449 struct v4l2_format *vf = (struct v4l2_format *)arg;
453 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
455 struct pvr2_ctrl *hcp,*vcp;
456 int h = vf->fmt.pix.height;
457 int w = vf->fmt.pix.width;
458 hcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES);
459 vcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES);
461 lmin = pvr2_ctrl_get_min(hcp);
462 lmax = pvr2_ctrl_get_max(hcp);
465 } else if (w > lmax) {
468 lmin = pvr2_ctrl_get_min(vcp);
469 lmax = pvr2_ctrl_get_max(vcp);
472 } else if (h > lmax) {
476 memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
477 sizeof(struct v4l2_format));
478 vf->fmt.pix.width = w;
479 vf->fmt.pix.height = h;
481 if (cmd == VIDIOC_S_FMT) {
482 pvr2_ctrl_set_value(hcp,vf->fmt.pix.width);
483 pvr2_ctrl_set_value(vcp,vf->fmt.pix.height);
486 case V4L2_BUF_TYPE_VBI_CAPTURE:
487 // ????? Still need to figure out to do VBI correctly
497 case VIDIOC_STREAMON:
499 ret = pvr2_hdw_set_stream_type(hdw,dev_info->config);
500 if (ret < 0) return ret;
501 ret = pvr2_hdw_set_streaming(hdw,!0);
505 case VIDIOC_STREAMOFF:
507 ret = pvr2_hdw_set_streaming(hdw,0);
511 case VIDIOC_QUERYCTRL:
513 struct pvr2_ctrl *cptr;
514 struct v4l2_queryctrl *vc = (struct v4l2_queryctrl *)arg;
516 if (vc->id & V4L2_CTRL_FLAG_NEXT_CTRL) {
517 cptr = pvr2_hdw_get_ctrl_nextv4l(
518 hdw,(vc->id & ~V4L2_CTRL_FLAG_NEXT_CTRL));
519 if (cptr) vc->id = pvr2_ctrl_get_v4lid(cptr);
521 cptr = pvr2_hdw_get_ctrl_v4l(hdw,vc->id);
524 pvr2_trace(PVR2_TRACE_V4LIOCTL,
525 "QUERYCTRL id=0x%x not implemented here",
531 pvr2_trace(PVR2_TRACE_V4LIOCTL,
532 "QUERYCTRL id=0x%x mapping name=%s (%s)",
533 vc->id,pvr2_ctrl_get_name(cptr),
534 pvr2_ctrl_get_desc(cptr));
535 strlcpy(vc->name,pvr2_ctrl_get_desc(cptr),sizeof(vc->name));
536 vc->flags = pvr2_ctrl_get_v4lflags(cptr);
537 vc->default_value = pvr2_ctrl_get_def(cptr);
538 switch (pvr2_ctrl_get_type(cptr)) {
540 vc->type = V4L2_CTRL_TYPE_MENU;
542 vc->maximum = pvr2_ctrl_get_cnt(cptr) - 1;
546 vc->type = V4L2_CTRL_TYPE_BOOLEAN;
552 vc->type = V4L2_CTRL_TYPE_INTEGER;
553 vc->minimum = pvr2_ctrl_get_min(cptr);
554 vc->maximum = pvr2_ctrl_get_max(cptr);
558 pvr2_trace(PVR2_TRACE_V4LIOCTL,
559 "QUERYCTRL id=0x%x name=%s not mappable",
560 vc->id,pvr2_ctrl_get_name(cptr));
567 case VIDIOC_QUERYMENU:
569 struct v4l2_querymenu *vm = (struct v4l2_querymenu *)arg;
570 unsigned int cnt = 0;
571 ret = pvr2_ctrl_get_valname(pvr2_hdw_get_ctrl_v4l(hdw,vm->id),
573 vm->name,sizeof(vm->name)-1,
581 struct v4l2_control *vc = (struct v4l2_control *)arg;
583 ret = pvr2_ctrl_get_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
591 struct v4l2_control *vc = (struct v4l2_control *)arg;
592 ret = pvr2_ctrl_set_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
597 case VIDIOC_G_EXT_CTRLS:
599 struct v4l2_ext_controls *ctls =
600 (struct v4l2_ext_controls *)arg;
601 struct v4l2_ext_control *ctrl;
604 for (idx = 0; idx < ctls->count; idx++) {
605 ctrl = ctls->controls + idx;
606 ret = pvr2_ctrl_get_value(
607 pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),&val);
609 ctls->error_idx = idx;
612 /* Ensure that if read as a 64 bit value, the user
613 will still get a hopefully sane value */
620 case VIDIOC_S_EXT_CTRLS:
622 struct v4l2_ext_controls *ctls =
623 (struct v4l2_ext_controls *)arg;
624 struct v4l2_ext_control *ctrl;
626 for (idx = 0; idx < ctls->count; idx++) {
627 ctrl = ctls->controls + idx;
628 ret = pvr2_ctrl_set_value(
629 pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),
632 ctls->error_idx = idx;
639 case VIDIOC_TRY_EXT_CTRLS:
641 struct v4l2_ext_controls *ctls =
642 (struct v4l2_ext_controls *)arg;
643 struct v4l2_ext_control *ctrl;
644 struct pvr2_ctrl *pctl;
646 /* For the moment just validate that the requested control
648 for (idx = 0; idx < ctls->count; idx++) {
649 ctrl = ctls->controls + idx;
650 pctl = pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id);
653 ctls->error_idx = idx;
660 case VIDIOC_LOG_STATUS:
662 pvr2_hdw_trigger_module_log(hdw);
666 #ifdef CONFIG_VIDEO_ADV_DEBUG
667 case VIDIOC_INT_G_REGISTER:
668 case VIDIOC_INT_S_REGISTER:
671 struct v4l2_register *req = (struct v4l2_register *)arg;
672 if (cmd == VIDIOC_INT_S_REGISTER) val = req->val;
673 ret = pvr2_hdw_register_access(
674 hdw,req->i2c_id,req->reg,
675 cmd == VIDIOC_INT_S_REGISTER,&val);
676 if (cmd == VIDIOC_INT_G_REGISTER) req->val = val;
682 ret = v4l_compat_translate_ioctl(inode,file,cmd,
683 arg,pvr2_v4l2_do_ioctl);
686 pvr2_hdw_commit_ctl(hdw);
689 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
690 pvr2_trace(PVR2_TRACE_V4LIOCTL,
691 "pvr2_v4l2_do_ioctl failure, ret=%d",ret);
693 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
694 pvr2_trace(PVR2_TRACE_V4LIOCTL,
695 "pvr2_v4l2_do_ioctl failure, ret=%d"
696 " command was:",ret);
697 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),
702 pvr2_trace(PVR2_TRACE_V4LIOCTL,
703 "pvr2_v4l2_do_ioctl complete, ret=%d (0x%x)",
710 static void pvr2_v4l2_dev_destroy(struct pvr2_v4l2_dev *dip)
712 printk(KERN_INFO "pvrusb2: unregistering device video%d [%s]\n",
713 dip->devbase.minor,pvr2_config_get_name(dip->config));
719 /* Actual deallocation happens later when all internal references
721 video_unregister_device(&dip->devbase);
725 static void pvr2_v4l2_destroy_no_lock(struct pvr2_v4l2 *vp)
727 pvr2_hdw_v4l_store_minor_number(vp->channel.mc_head->hdw,
728 pvr2_config_mpeg-1,-1);
729 pvr2_hdw_v4l_store_minor_number(vp->channel.mc_head->hdw,
730 pvr2_config_vbi-1,-1);
731 pvr2_hdw_v4l_store_minor_number(vp->channel.mc_head->hdw,
732 pvr2_config_radio-1,-1);
733 pvr2_v4l2_dev_destroy(vp->vdev);
735 pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr2_v4l2 id=%p",vp);
736 pvr2_channel_done(&vp->channel);
741 static void pvr2_video_device_release(struct video_device *vdev)
743 struct pvr2_v4l2_dev *dev;
744 dev = container_of(vdev,struct pvr2_v4l2_dev,devbase);
749 static void pvr2_v4l2_internal_check(struct pvr2_channel *chp)
751 struct pvr2_v4l2 *vp;
752 vp = container_of(chp,struct pvr2_v4l2,channel);
753 if (!vp->channel.mc_head->disconnect_flag) return;
754 if (vp->vfirst) return;
755 pvr2_v4l2_destroy_no_lock(vp);
759 static int pvr2_v4l2_ioctl(struct inode *inode, struct file *file,
760 unsigned int cmd, unsigned long arg)
763 /* Temporary hack : use ivtv api until a v4l2 one is available. */
764 #define IVTV_IOC_G_CODEC 0xFFEE7703
765 #define IVTV_IOC_S_CODEC 0xFFEE7704
766 if (cmd == IVTV_IOC_G_CODEC || cmd == IVTV_IOC_S_CODEC) return 0;
767 return video_usercopy(inode, file, cmd, arg, pvr2_v4l2_do_ioctl);
771 static int pvr2_v4l2_release(struct inode *inode, struct file *file)
773 struct pvr2_v4l2_fh *fhp = file->private_data;
774 struct pvr2_v4l2 *vp = fhp->vhead;
775 struct pvr2_context *mp = fhp->vhead->channel.mc_head;
777 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_release");
780 struct pvr2_stream *sp;
781 struct pvr2_hdw *hdw;
782 hdw = fhp->channel.mc_head->hdw;
783 pvr2_hdw_set_streaming(hdw,0);
784 sp = pvr2_ioread_get_stream(fhp->rhp);
785 if (sp) pvr2_stream_set_callback(sp,NULL,NULL);
786 pvr2_ioread_destroy(fhp->rhp);
790 if (fhp->dev_info->config == pvr2_config_radio) {
792 struct pvr2_hdw *hdw;
793 hdw = fhp->channel.mc_head->hdw;
794 if ((ret = pvr2_ctrl_set_value(
795 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
796 PVR2_CVAL_INPUT_TV))) {
801 v4l2_prio_close(&vp->prio, &fhp->prio);
802 file->private_data = NULL;
804 pvr2_context_enter(mp); do {
806 fhp->vnext->vprev = fhp->vprev;
808 vp->vlast = fhp->vprev;
811 fhp->vprev->vnext = fhp->vnext;
813 vp->vfirst = fhp->vnext;
818 pvr2_channel_done(&fhp->channel);
819 pvr2_trace(PVR2_TRACE_STRUCT,
820 "Destroying pvr_v4l2_fh id=%p",fhp);
822 if (vp->channel.mc_head->disconnect_flag && !vp->vfirst) {
823 pvr2_v4l2_destroy_no_lock(vp);
825 } while (0); pvr2_context_exit(mp);
830 static int pvr2_v4l2_open(struct inode *inode, struct file *file)
832 struct pvr2_v4l2_dev *dip; /* Our own context pointer */
833 struct pvr2_v4l2_fh *fhp;
834 struct pvr2_v4l2 *vp;
835 struct pvr2_hdw *hdw;
837 dip = container_of(video_devdata(file),struct pvr2_v4l2_dev,devbase);
840 hdw = vp->channel.hdw;
842 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_open");
844 if (!pvr2_hdw_dev_ok(hdw)) {
845 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,
846 "pvr2_v4l2_open: hardware not ready");
850 fhp = kmalloc(sizeof(*fhp),GFP_KERNEL);
854 memset(fhp,0,sizeof(*fhp));
856 init_waitqueue_head(&fhp->wait_data);
859 pvr2_context_enter(vp->channel.mc_head); do {
860 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr_v4l2_fh id=%p",fhp);
861 pvr2_channel_init(&fhp->channel,vp->channel.mc_head);
863 /* pk: warning, severe ugliness follows. 18+ only.
864 please blaim V4L(ivtv) for braindamaged interfaces,
865 not the implementor. This is probably flawed, but
866 suggestions on how to do this "right" are welcome! */
867 if (dip->config == pvr2_config_radio) {
869 if ((pvr2_channel_check_stream_no_lock(&fhp->channel,
870 fhp->dev_info->stream)) != 0) {
871 /* We can 't switch modes while streaming */
872 pvr2_channel_done(&fhp->channel);
874 pvr2_context_exit(vp->channel.mc_head);
878 if ((ret = pvr2_ctrl_set_value(
879 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
880 PVR2_CVAL_INPUT_RADIO))) {
881 pvr2_channel_done(&fhp->channel);
883 pvr2_context_exit(vp->channel.mc_head);
889 fhp->vprev = vp->vlast;
891 vp->vlast->vnext = fhp;
897 } while (0); pvr2_context_exit(vp->channel.mc_head);
900 file->private_data = fhp;
901 v4l2_prio_open(&vp->prio,&fhp->prio);
903 fhp->fw_mode_flag = pvr2_hdw_cpufw_get_enabled(hdw);
909 static void pvr2_v4l2_notify(struct pvr2_v4l2_fh *fhp)
911 wake_up(&fhp->wait_data);
914 static int pvr2_v4l2_iosetup(struct pvr2_v4l2_fh *fh)
917 struct pvr2_stream *sp;
918 struct pvr2_hdw *hdw;
919 if (fh->rhp) return 0;
921 /* First read() attempt. Try to claim the stream and start
923 if ((ret = pvr2_channel_claim_stream(&fh->channel,
924 fh->dev_info->stream)) != 0) {
925 /* Someone else must already have it */
929 fh->rhp = pvr2_channel_create_mpeg_stream(fh->dev_info->stream);
931 pvr2_channel_claim_stream(&fh->channel,NULL);
935 hdw = fh->channel.mc_head->hdw;
936 sp = fh->dev_info->stream->stream;
937 pvr2_stream_set_callback(sp,(pvr2_stream_callback)pvr2_v4l2_notify,fh);
938 pvr2_hdw_set_stream_type(hdw,fh->dev_info->config);
939 pvr2_hdw_set_streaming(hdw,!0);
940 ret = pvr2_ioread_set_enabled(fh->rhp,!0);
946 static ssize_t pvr2_v4l2_read(struct file *file,
947 char __user *buff, size_t count, loff_t *ppos)
949 struct pvr2_v4l2_fh *fh = file->private_data;
952 if (fh->fw_mode_flag) {
953 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
957 unsigned int offs = *ppos;
959 tbuf = kmalloc(PAGE_SIZE,GFP_KERNEL);
960 if (!tbuf) return -ENOMEM;
964 if (c1 > PAGE_SIZE) c1 = PAGE_SIZE;
965 c2 = pvr2_hdw_cpufw_get(hdw,offs,tbuf,c1);
971 if (copy_to_user(buff,tbuf,c2)) {
985 if (fh->dev_info->config == pvr2_config_radio) {
986 /* Radio device nodes on this device
987 cannot be read or written. */
992 ret = pvr2_v4l2_iosetup(fh);
999 ret = pvr2_ioread_read(fh->rhp,buff,count);
1000 if (ret >= 0) break;
1001 if (ret != -EAGAIN) break;
1002 if (file->f_flags & O_NONBLOCK) break;
1003 /* Doing blocking I/O. Wait here. */
1004 ret = wait_event_interruptible(
1006 pvr2_ioread_avail(fh->rhp) >= 0);
1014 static unsigned int pvr2_v4l2_poll(struct file *file, poll_table *wait)
1016 unsigned int mask = 0;
1017 struct pvr2_v4l2_fh *fh = file->private_data;
1020 if (fh->fw_mode_flag) {
1021 mask |= POLLIN | POLLRDNORM;
1025 if (fh->dev_info->config == pvr2_config_radio) {
1026 /* Radio device nodes on this device
1027 cannot be read or written. */
1032 ret = pvr2_v4l2_iosetup(fh);
1033 if (ret) return POLLERR;
1036 poll_wait(file,&fh->wait_data,wait);
1038 if (pvr2_ioread_avail(fh->rhp) >= 0) {
1039 mask |= POLLIN | POLLRDNORM;
1046 static const struct file_operations vdev_fops = {
1047 .owner = THIS_MODULE,
1048 .open = pvr2_v4l2_open,
1049 .release = pvr2_v4l2_release,
1050 .read = pvr2_v4l2_read,
1051 .ioctl = pvr2_v4l2_ioctl,
1052 .llseek = no_llseek,
1053 .poll = pvr2_v4l2_poll,
1057 #define VID_HARDWARE_PVRUSB2 38 /* FIXME : need a good value */
1059 static struct video_device vdev_template = {
1060 .owner = THIS_MODULE,
1061 .type = VID_TYPE_CAPTURE | VID_TYPE_TUNER,
1062 .type2 = (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VBI_CAPTURE
1063 | V4L2_CAP_TUNER | V4L2_CAP_AUDIO
1064 | V4L2_CAP_READWRITE),
1065 .hardware = VID_HARDWARE_PVRUSB2,
1070 static void pvr2_v4l2_dev_init(struct pvr2_v4l2_dev *dip,
1071 struct pvr2_v4l2 *vp,
1072 enum pvr2_config cfg)
1082 case pvr2_config_mpeg:
1083 v4l_type = VFL_TYPE_GRABBER;
1084 dip->stream = &vp->channel.mc_head->video_stream;
1086 case pvr2_config_vbi:
1087 v4l_type = VFL_TYPE_VBI;
1089 case pvr2_config_radio:
1090 v4l_type = VFL_TYPE_RADIO;
1093 /* Bail out (this should be impossible) */
1094 err("Failed to set up pvrusb2 v4l dev"
1095 " due to unrecognized config");
1099 /* radio device doesn 't need its own stream */
1100 if (!dip->stream && cfg != pvr2_config_radio) {
1101 err("Failed to set up pvrusb2 v4l dev"
1102 " due to missing stream instance");
1106 memcpy(&dip->devbase,&vdev_template,sizeof(vdev_template));
1107 dip->devbase.release = pvr2_video_device_release;
1110 unit_number = pvr2_hdw_get_unit_number(vp->channel.mc_head->hdw);
1111 if ((unit_number >= 0) && (unit_number < PVR_NUM)) {
1112 mindevnum = video_nr[unit_number];
1114 if ((video_register_device(&dip->devbase, v4l_type, mindevnum) < 0) &&
1115 (video_register_device(&dip->devbase, v4l_type, -1) < 0)) {
1116 err("Failed to register pvrusb2 v4l device");
1119 case pvr2_config_mpeg:
1120 printk(KERN_INFO "pvrusb2: registered device video%d [%s]\n",
1121 dip->devbase.minor,pvr2_config_get_name(dip->config));
1123 case pvr2_config_vbi:
1124 printk(KERN_INFO "pvrusb2: registered device vbi%d [%s]\n",
1125 dip->devbase.minor - MINOR_VFL_TYPE_VBI_MIN,
1126 pvr2_config_get_name(dip->config));
1128 case pvr2_config_radio:
1129 printk(KERN_INFO "pvrusb2: registered device radio%d [%s]\n",
1130 dip->devbase.minor - MINOR_VFL_TYPE_RADIO_MIN,
1131 pvr2_config_get_name(dip->config));
1137 pvr2_hdw_v4l_store_minor_number(vp->channel.mc_head->hdw,
1138 cfg-1,dip->devbase.minor);
1142 struct pvr2_v4l2 *pvr2_v4l2_create(struct pvr2_context *mnp)
1144 struct pvr2_v4l2 *vp;
1146 vp = kmalloc(sizeof(*vp),GFP_KERNEL);
1148 memset(vp,0,sizeof(*vp));
1149 vp->vdev = kmalloc(sizeof(*vp->vdev)*PVR2_NR_STREAMS,GFP_KERNEL);
1154 memset(vp->vdev,0,sizeof(*vp->vdev)*PVR2_NR_STREAMS);
1155 pvr2_channel_init(&vp->channel,mnp);
1156 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr2_v4l2 id=%p",vp);
1158 vp->channel.check_func = pvr2_v4l2_internal_check;
1160 /* register streams */
1161 pvr2_v4l2_dev_init(&vp->vdev[0],vp,pvr2_config_mpeg);
1162 pvr2_v4l2_dev_init(&vp->vdev[2],vp,pvr2_config_radio);
1168 Stuff for Emacs to see, in order to encourage consistent editing style:
1169 *** Local Variables: ***
1171 *** fill-column: 75 ***
1172 *** tab-width: 8 ***
1173 *** c-basic-offset: 8 ***