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-common.h>
38 /* V4L no longer provide the ability to set / get a private context pointer
39 (i.e. video_get_drvdata / video_set_drvdata), which means we have to
40 concoct our own context locating mechanism. Supposedly this is intended
41 to simplify driver implementation. It's not clear to me how that can
42 possibly be true. Our solution here is to maintain a lookup table of
43 our context instances, indexed by the minor device number of the V4L
44 device. See pvr2_v4l2_open() for some implications of this approach. */
45 static struct pvr2_v4l2_dev *devices[256];
46 static DEFINE_MUTEX(device_lock);
48 struct pvr2_v4l2_dev {
49 struct pvr2_v4l2 *v4lp;
50 struct video_device *vdev;
51 struct pvr2_context_stream *stream;
53 enum pvr2_config config;
57 struct pvr2_channel channel;
58 struct pvr2_v4l2_dev *dev_info;
59 enum v4l2_priority prio;
60 struct pvr2_ioread *rhp;
62 struct pvr2_v4l2 *vhead;
63 struct pvr2_v4l2_fh *vnext;
64 struct pvr2_v4l2_fh *vprev;
65 wait_queue_head_t wait_data;
70 struct pvr2_channel channel;
71 struct pvr2_v4l2_fh *vfirst;
72 struct pvr2_v4l2_fh *vlast;
74 struct v4l2_prio_state prio;
77 struct pvr2_v4l2_dev video_dev;
80 static int video_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
81 module_param_array(video_nr, int, NULL, 0444);
82 MODULE_PARM_DESC(video_nr, "Offset for device's minor");
84 static struct v4l2_capability pvr_capability ={
86 .card = "Hauppauge WinTV pvr-usb2",
88 .version = KERNEL_VERSION(0,8,0),
89 .capabilities = (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VBI_CAPTURE |
90 V4L2_CAP_TUNER | V4L2_CAP_AUDIO |
95 static struct v4l2_tuner pvr_v4l2_tuners[]= {
99 .type = V4L2_TUNER_ANALOG_TV,
100 .capability = (V4L2_TUNER_CAP_NORM |
101 V4L2_TUNER_CAP_STEREO |
102 V4L2_TUNER_CAP_LANG1 |
103 V4L2_TUNER_CAP_LANG2),
106 .rxsubchans = V4L2_TUNER_SUB_STEREO,
107 .audmode = V4L2_TUNER_MODE_STEREO,
110 .reserved = {0,0,0,0}
114 static struct v4l2_fmtdesc pvr_fmtdesc [] = {
117 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
118 .flags = V4L2_FMT_FLAG_COMPRESSED,
119 .description = "MPEG1/2",
120 // This should really be V4L2_PIX_FMT_MPEG, but xawtv
121 // breaks when I do that.
122 .pixelformat = 0, // V4L2_PIX_FMT_MPEG,
123 .reserved = { 0, 0, 0, 0 }
127 #define PVR_FORMAT_PIX 0
128 #define PVR_FORMAT_VBI 1
130 static struct v4l2_format pvr_format [] = {
132 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
137 // This should really be V4L2_PIX_FMT_MPEG,
138 // but xawtv breaks when I do that.
139 .pixelformat = 0, // V4L2_PIX_FMT_MPEG,
140 .field = V4L2_FIELD_INTERLACED,
141 .bytesperline = 0, // doesn't make sense
143 //FIXME : Don't know what to put here...
144 .sizeimage = (32*1024),
145 .colorspace = 0, // doesn't make sense here
151 .type = V4L2_BUF_TYPE_VBI_CAPTURE,
154 .sampling_rate = 27000000,
156 .samples_per_line = 1443,
157 .sample_format = V4L2_PIX_FMT_GREY,
170 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
173 static int pvr2_v4l2_do_ioctl(struct inode *inode, struct file *file,
174 unsigned int cmd, void *arg)
176 struct pvr2_v4l2_fh *fh = file->private_data;
177 struct pvr2_v4l2 *vp = fh->vhead;
178 struct pvr2_v4l2_dev *dev_info = fh->dev_info;
179 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
182 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
183 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),cmd);
186 if (!pvr2_hdw_dev_ok(hdw)) {
187 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
188 "ioctl failed - bad or no context");
198 case VIDIOC_S_FREQUENCY:
199 ret = v4l2_prio_check(&vp->prio, &fh->prio);
205 case VIDIOC_QUERYCAP:
207 struct v4l2_capability *cap = arg;
209 memcpy(cap, &pvr_capability, sizeof(struct v4l2_capability));
215 case VIDIOC_G_PRIORITY:
217 enum v4l2_priority *p = arg;
219 *p = v4l2_prio_max(&vp->prio);
224 case VIDIOC_S_PRIORITY:
226 enum v4l2_priority *prio = arg;
228 ret = v4l2_prio_change(&vp->prio, &fh->prio, *prio);
234 struct v4l2_standard *vs = (struct v4l2_standard *)arg;
236 ret = pvr2_hdw_get_stdenum_value(hdw,vs,idx+1);
243 ret = pvr2_ctrl_get_value(
244 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR),&val);
245 *(v4l2_std_id *)arg = val;
251 ret = pvr2_ctrl_set_value(
252 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR),
253 *(v4l2_std_id *)arg);
257 case VIDIOC_ENUMINPUT:
259 struct pvr2_ctrl *cptr;
260 struct v4l2_input *vi = (struct v4l2_input *)arg;
261 struct v4l2_input tmp;
264 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
266 memset(&tmp,0,sizeof(tmp));
267 tmp.index = vi->index;
270 case PVR2_CVAL_INPUT_TV:
271 case PVR2_CVAL_INPUT_RADIO:
272 tmp.type = V4L2_INPUT_TYPE_TUNER;
274 case PVR2_CVAL_INPUT_SVIDEO:
275 case PVR2_CVAL_INPUT_COMPOSITE:
276 tmp.type = V4L2_INPUT_TYPE_CAMERA;
285 pvr2_ctrl_get_valname(cptr,vi->index,
286 tmp.name,sizeof(tmp.name)-1,&cnt);
289 /* Don't bother with audioset, since this driver currently
290 always switches the audio whenever the video is
293 /* Handling std is a tougher problem. It doesn't make
294 sense in cases where a device might be multi-standard.
295 We could just copy out the current value for the
296 standard, but it can change over time. For now just
299 memcpy(vi, &tmp, sizeof(tmp));
307 struct pvr2_ctrl *cptr;
308 struct v4l2_input *vi = (struct v4l2_input *)arg;
310 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
312 ret = pvr2_ctrl_get_value(cptr,&val);
319 struct v4l2_input *vi = (struct v4l2_input *)arg;
320 ret = pvr2_ctrl_set_value(
321 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
326 case VIDIOC_ENUMAUDIO:
345 struct v4l2_tuner *vt = (struct v4l2_tuner *)arg;
346 unsigned int status_mask;
348 if (vt->index !=0) break;
350 status_mask = pvr2_hdw_get_signal_status(hdw);
352 memcpy(vt, &pvr_v4l2_tuners[vt->index],
353 sizeof(struct v4l2_tuner));
356 if (status_mask & PVR2_SIGNAL_OK) {
357 if (status_mask & PVR2_SIGNAL_STEREO) {
358 vt->rxsubchans = V4L2_TUNER_SUB_STEREO;
360 vt->rxsubchans = V4L2_TUNER_SUB_MONO;
362 if (status_mask & PVR2_SIGNAL_SAP) {
363 vt->rxsubchans |= (V4L2_TUNER_SUB_LANG1 |
364 V4L2_TUNER_SUB_LANG2);
370 ret = pvr2_ctrl_get_value(
371 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_AUDIOMODE),
379 struct v4l2_tuner *vt=(struct v4l2_tuner *)arg;
384 ret = pvr2_ctrl_set_value(
385 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_AUDIOMODE),
389 case VIDIOC_S_FREQUENCY:
391 const struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
392 ret = pvr2_ctrl_set_value(
393 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),
394 vf->frequency * 62500);
398 case VIDIOC_G_FREQUENCY:
400 struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
402 ret = pvr2_ctrl_get_value(
403 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),
410 case VIDIOC_ENUM_FMT:
412 struct v4l2_fmtdesc *fd = (struct v4l2_fmtdesc *)arg;
414 /* Only one format is supported : mpeg.*/
418 memcpy(fd, pvr_fmtdesc, sizeof(struct v4l2_fmtdesc));
425 struct v4l2_format *vf = (struct v4l2_format *)arg;
428 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
429 memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
430 sizeof(struct v4l2_format));
433 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES),
435 vf->fmt.pix.width = val;
438 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES),
440 vf->fmt.pix.height = val;
443 case V4L2_BUF_TYPE_VBI_CAPTURE:
444 // ????? Still need to figure out to do VBI correctly
457 struct v4l2_format *vf = (struct v4l2_format *)arg;
461 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
463 struct pvr2_ctrl *hcp,*vcp;
464 int h = vf->fmt.pix.height;
465 int w = vf->fmt.pix.width;
466 hcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES);
467 vcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES);
469 lmin = pvr2_ctrl_get_min(hcp);
470 lmax = pvr2_ctrl_get_max(hcp);
473 } else if (w > lmax) {
476 lmin = pvr2_ctrl_get_min(vcp);
477 lmax = pvr2_ctrl_get_max(vcp);
480 } else if (h > lmax) {
484 memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
485 sizeof(struct v4l2_format));
486 vf->fmt.pix.width = w;
487 vf->fmt.pix.height = h;
489 if (cmd == VIDIOC_S_FMT) {
490 pvr2_ctrl_set_value(hcp,vf->fmt.pix.width);
491 pvr2_ctrl_set_value(vcp,vf->fmt.pix.height);
494 case V4L2_BUF_TYPE_VBI_CAPTURE:
495 // ????? Still need to figure out to do VBI correctly
505 case VIDIOC_STREAMON:
507 ret = pvr2_hdw_set_stream_type(hdw,dev_info->config);
508 if (ret < 0) return ret;
509 ret = pvr2_hdw_set_streaming(hdw,!0);
513 case VIDIOC_STREAMOFF:
515 ret = pvr2_hdw_set_streaming(hdw,0);
519 case VIDIOC_QUERYCTRL:
521 struct pvr2_ctrl *cptr;
522 struct v4l2_queryctrl *vc = (struct v4l2_queryctrl *)arg;
524 if (vc->id & V4L2_CTRL_FLAG_NEXT_CTRL) {
525 cptr = pvr2_hdw_get_ctrl_nextv4l(
526 hdw,(vc->id & ~V4L2_CTRL_FLAG_NEXT_CTRL));
527 if (cptr) vc->id = pvr2_ctrl_get_v4lid(cptr);
529 cptr = pvr2_hdw_get_ctrl_v4l(hdw,vc->id);
532 pvr2_trace(PVR2_TRACE_V4LIOCTL,
533 "QUERYCTRL id=0x%x not implemented here",
539 pvr2_trace(PVR2_TRACE_V4LIOCTL,
540 "QUERYCTRL id=0x%x mapping name=%s (%s)",
541 vc->id,pvr2_ctrl_get_name(cptr),
542 pvr2_ctrl_get_desc(cptr));
543 strlcpy(vc->name,pvr2_ctrl_get_desc(cptr),sizeof(vc->name));
544 vc->flags = pvr2_ctrl_get_v4lflags(cptr);
545 vc->default_value = pvr2_ctrl_get_def(cptr);
546 switch (pvr2_ctrl_get_type(cptr)) {
548 vc->type = V4L2_CTRL_TYPE_MENU;
550 vc->maximum = pvr2_ctrl_get_cnt(cptr) - 1;
554 vc->type = V4L2_CTRL_TYPE_BOOLEAN;
560 vc->type = V4L2_CTRL_TYPE_INTEGER;
561 vc->minimum = pvr2_ctrl_get_min(cptr);
562 vc->maximum = pvr2_ctrl_get_max(cptr);
566 pvr2_trace(PVR2_TRACE_V4LIOCTL,
567 "QUERYCTRL id=0x%x name=%s not mappable",
568 vc->id,pvr2_ctrl_get_name(cptr));
575 case VIDIOC_QUERYMENU:
577 struct v4l2_querymenu *vm = (struct v4l2_querymenu *)arg;
578 unsigned int cnt = 0;
579 ret = pvr2_ctrl_get_valname(pvr2_hdw_get_ctrl_v4l(hdw,vm->id),
581 vm->name,sizeof(vm->name)-1,
589 struct v4l2_control *vc = (struct v4l2_control *)arg;
591 ret = pvr2_ctrl_get_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
599 struct v4l2_control *vc = (struct v4l2_control *)arg;
600 ret = pvr2_ctrl_set_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
605 case VIDIOC_G_EXT_CTRLS:
607 struct v4l2_ext_controls *ctls =
608 (struct v4l2_ext_controls *)arg;
609 struct v4l2_ext_control *ctrl;
612 for (idx = 0; idx < ctls->count; idx++) {
613 ctrl = ctls->controls + idx;
614 ret = pvr2_ctrl_get_value(
615 pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),&val);
617 ctls->error_idx = idx;
620 /* Ensure that if read as a 64 bit value, the user
621 will still get a hopefully sane value */
628 case VIDIOC_S_EXT_CTRLS:
630 struct v4l2_ext_controls *ctls =
631 (struct v4l2_ext_controls *)arg;
632 struct v4l2_ext_control *ctrl;
634 for (idx = 0; idx < ctls->count; idx++) {
635 ctrl = ctls->controls + idx;
636 ret = pvr2_ctrl_set_value(
637 pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),
640 ctls->error_idx = idx;
647 case VIDIOC_TRY_EXT_CTRLS:
649 struct v4l2_ext_controls *ctls =
650 (struct v4l2_ext_controls *)arg;
651 struct v4l2_ext_control *ctrl;
652 struct pvr2_ctrl *pctl;
654 /* For the moment just validate that the requested control
656 for (idx = 0; idx < ctls->count; idx++) {
657 ctrl = ctls->controls + idx;
658 pctl = pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id);
661 ctls->error_idx = idx;
668 case VIDIOC_LOG_STATUS:
670 pvr2_hdw_trigger_module_log(hdw);
674 #ifdef CONFIG_VIDEO_ADV_DEBUG
675 case VIDIOC_INT_G_REGISTER:
676 case VIDIOC_INT_S_REGISTER:
679 struct v4l2_register *req = (struct v4l2_register *)arg;
680 if (cmd == VIDIOC_INT_S_REGISTER) val = req->val;
681 ret = pvr2_hdw_register_access(
682 hdw,req->i2c_id,req->reg,
683 cmd == VIDIOC_INT_S_REGISTER,&val);
684 if (cmd == 0) req->val = val;
690 ret = v4l_compat_translate_ioctl(inode,file,cmd,
691 arg,pvr2_v4l2_do_ioctl);
694 pvr2_hdw_commit_ctl(hdw);
697 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
698 pvr2_trace(PVR2_TRACE_V4LIOCTL,
699 "pvr2_v4l2_do_ioctl failure, ret=%d",ret);
701 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
702 pvr2_trace(PVR2_TRACE_V4LIOCTL,
703 "pvr2_v4l2_do_ioctl failure, ret=%d"
704 " command was:",ret);
705 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),
710 pvr2_trace(PVR2_TRACE_V4LIOCTL,
711 "pvr2_v4l2_do_ioctl complete, ret=%d (0x%x)",
718 static void pvr2_v4l2_dev_destroy(struct pvr2_v4l2_dev *dip)
720 printk(KERN_INFO "pvrusb2: unregistering device video%d [%s]\n",
721 dip->vdev->minor,pvr2_config_get_name(dip->config));
722 if (dip->ctxt_idx >= 0) {
723 mutex_lock(&device_lock);
724 devices[dip->ctxt_idx] = NULL;
726 mutex_unlock(&device_lock);
728 video_unregister_device(dip->vdev);
732 static void pvr2_v4l2_destroy_no_lock(struct pvr2_v4l2 *vp)
734 pvr2_hdw_v4l_store_minor_number(vp->channel.mc_head->hdw,-1);
735 pvr2_v4l2_dev_destroy(&vp->video_dev);
737 pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr2_v4l2 id=%p",vp);
738 pvr2_channel_done(&vp->channel);
743 static void pvr2_v4l2_internal_check(struct pvr2_channel *chp)
745 struct pvr2_v4l2 *vp;
746 vp = container_of(chp,struct pvr2_v4l2,channel);
747 if (!vp->channel.mc_head->disconnect_flag) return;
748 if (vp->vfirst) return;
749 pvr2_v4l2_destroy_no_lock(vp);
753 static int pvr2_v4l2_ioctl(struct inode *inode, struct file *file,
754 unsigned int cmd, unsigned long arg)
757 /* Temporary hack : use ivtv api until a v4l2 one is available. */
758 #define IVTV_IOC_G_CODEC 0xFFEE7703
759 #define IVTV_IOC_S_CODEC 0xFFEE7704
760 if (cmd == IVTV_IOC_G_CODEC || cmd == IVTV_IOC_S_CODEC) return 0;
761 return video_usercopy(inode, file, cmd, arg, pvr2_v4l2_do_ioctl);
765 static int pvr2_v4l2_release(struct inode *inode, struct file *file)
767 struct pvr2_v4l2_fh *fhp = file->private_data;
768 struct pvr2_v4l2 *vp = fhp->vhead;
769 struct pvr2_context *mp = fhp->vhead->channel.mc_head;
771 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_release");
774 struct pvr2_stream *sp;
775 struct pvr2_hdw *hdw;
776 hdw = fhp->channel.mc_head->hdw;
777 pvr2_hdw_set_streaming(hdw,0);
778 sp = pvr2_ioread_get_stream(fhp->rhp);
779 if (sp) pvr2_stream_set_callback(sp,NULL,NULL);
780 pvr2_ioread_destroy(fhp->rhp);
783 v4l2_prio_close(&vp->prio, &fhp->prio);
784 file->private_data = NULL;
786 pvr2_context_enter(mp); do {
788 fhp->vnext->vprev = fhp->vprev;
790 vp->vlast = fhp->vprev;
793 fhp->vprev->vnext = fhp->vnext;
795 vp->vfirst = fhp->vnext;
800 pvr2_channel_done(&fhp->channel);
801 pvr2_trace(PVR2_TRACE_STRUCT,
802 "Destroying pvr_v4l2_fh id=%p",fhp);
804 if (vp->channel.mc_head->disconnect_flag && !vp->vfirst) {
805 pvr2_v4l2_destroy_no_lock(vp);
807 } while (0); pvr2_context_exit(mp);
812 static int pvr2_v4l2_open(struct inode *inode, struct file *file)
814 struct pvr2_v4l2_dev *dip = NULL; /* Our own context pointer */
815 struct pvr2_v4l2_fh *fhp;
816 struct pvr2_v4l2 *vp;
817 struct pvr2_hdw *hdw;
819 mutex_lock(&device_lock);
820 /* MCI 7-Jun-2006 Even though we're just doing what amounts to an
821 atomic read of the device mapping array here, we still need the
822 mutex. The problem is that there is a tiny race possible when
823 we register the device. We can't update the device mapping
824 array until after the device has been registered, owing to the
825 fact that we can't know the minor device number until after the
826 registration succeeds. And if another thread tries to open the
827 device in the window of time after registration but before the
828 map is updated, then it will get back an erroneous null pointer
829 and the open will result in a spurious failure. The only way to
830 prevent that is to (a) be inside the mutex here before we access
831 the array, and (b) cover the entire registration process later
832 on with this same mutex. Thus if we get inside the mutex here,
833 then we can be assured that the registration process actually
834 completed correctly. This is an unhappy complication from the
835 use of global data in a driver that lives in a preemptible
836 environment. It sure would be nice if the video device itself
837 had a means for storing and retrieving a local context pointer.
838 Oh wait. It did. But now it's gone. Silly me. */
840 unsigned int midx = iminor(file->f_dentry->d_inode);
841 if (midx < sizeof(devices)/sizeof(devices[0])) {
845 mutex_unlock(&device_lock);
847 if (!dip) return -ENODEV; /* Should be impossible but I'm paranoid */
850 hdw = vp->channel.hdw;
852 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_open");
854 if (!pvr2_hdw_dev_ok(hdw)) {
855 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,
856 "pvr2_v4l2_open: hardware not ready");
860 fhp = kmalloc(sizeof(*fhp),GFP_KERNEL);
864 memset(fhp,0,sizeof(*fhp));
866 init_waitqueue_head(&fhp->wait_data);
869 pvr2_context_enter(vp->channel.mc_head); do {
870 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr_v4l2_fh id=%p",fhp);
871 pvr2_channel_init(&fhp->channel,vp->channel.mc_head);
873 fhp->vprev = vp->vlast;
875 vp->vlast->vnext = fhp;
881 } while (0); pvr2_context_exit(vp->channel.mc_head);
884 file->private_data = fhp;
885 v4l2_prio_open(&vp->prio,&fhp->prio);
887 fhp->fw_mode_flag = pvr2_hdw_cpufw_get_enabled(hdw);
893 static void pvr2_v4l2_notify(struct pvr2_v4l2_fh *fhp)
895 wake_up(&fhp->wait_data);
898 static int pvr2_v4l2_iosetup(struct pvr2_v4l2_fh *fh)
901 struct pvr2_stream *sp;
902 struct pvr2_hdw *hdw;
903 if (fh->rhp) return 0;
905 /* First read() attempt. Try to claim the stream and start
907 if ((ret = pvr2_channel_claim_stream(&fh->channel,
908 fh->dev_info->stream)) != 0) {
909 /* Someone else must already have it */
913 fh->rhp = pvr2_channel_create_mpeg_stream(fh->dev_info->stream);
915 pvr2_channel_claim_stream(&fh->channel,NULL);
919 hdw = fh->channel.mc_head->hdw;
920 sp = fh->dev_info->stream->stream;
921 pvr2_stream_set_callback(sp,(pvr2_stream_callback)pvr2_v4l2_notify,fh);
922 pvr2_hdw_set_stream_type(hdw,fh->dev_info->config);
923 pvr2_hdw_set_streaming(hdw,!0);
924 ret = pvr2_ioread_set_enabled(fh->rhp,!0);
930 static ssize_t pvr2_v4l2_read(struct file *file,
931 char __user *buff, size_t count, loff_t *ppos)
933 struct pvr2_v4l2_fh *fh = file->private_data;
936 if (fh->fw_mode_flag) {
937 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
941 unsigned int offs = *ppos;
943 tbuf = kmalloc(PAGE_SIZE,GFP_KERNEL);
944 if (!tbuf) return -ENOMEM;
948 if (c1 > PAGE_SIZE) c1 = PAGE_SIZE;
949 c2 = pvr2_hdw_cpufw_get(hdw,offs,tbuf,c1);
955 if (copy_to_user(buff,tbuf,c2)) {
970 ret = pvr2_v4l2_iosetup(fh);
977 ret = pvr2_ioread_read(fh->rhp,buff,count);
979 if (ret != -EAGAIN) break;
980 if (file->f_flags & O_NONBLOCK) break;
981 /* Doing blocking I/O. Wait here. */
982 ret = wait_event_interruptible(
984 pvr2_ioread_avail(fh->rhp) >= 0);
992 static unsigned int pvr2_v4l2_poll(struct file *file, poll_table *wait)
994 unsigned int mask = 0;
995 struct pvr2_v4l2_fh *fh = file->private_data;
998 if (fh->fw_mode_flag) {
999 mask |= POLLIN | POLLRDNORM;
1004 ret = pvr2_v4l2_iosetup(fh);
1005 if (ret) return POLLERR;
1008 poll_wait(file,&fh->wait_data,wait);
1010 if (pvr2_ioread_avail(fh->rhp) >= 0) {
1011 mask |= POLLIN | POLLRDNORM;
1018 static struct file_operations vdev_fops = {
1019 .owner = THIS_MODULE,
1020 .open = pvr2_v4l2_open,
1021 .release = pvr2_v4l2_release,
1022 .read = pvr2_v4l2_read,
1023 .ioctl = pvr2_v4l2_ioctl,
1024 .llseek = no_llseek,
1025 .poll = pvr2_v4l2_poll,
1029 #define VID_HARDWARE_PVRUSB2 38 /* FIXME : need a good value */
1031 static struct video_device vdev_template = {
1032 .owner = THIS_MODULE,
1033 .type = VID_TYPE_CAPTURE | VID_TYPE_TUNER,
1034 .type2 = (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VBI_CAPTURE
1035 | V4L2_CAP_TUNER | V4L2_CAP_AUDIO
1036 | V4L2_CAP_READWRITE),
1037 .hardware = VID_HARDWARE_PVRUSB2,
1042 static void pvr2_v4l2_dev_init(struct pvr2_v4l2_dev *dip,
1043 struct pvr2_v4l2 *vp,
1044 enum pvr2_config cfg)
1054 case pvr2_config_mpeg:
1055 v4l_type = VFL_TYPE_GRABBER;
1056 dip->stream = &vp->channel.mc_head->video_stream;
1058 case pvr2_config_vbi:
1059 v4l_type = VFL_TYPE_VBI;
1061 case pvr2_config_radio:
1062 v4l_type = VFL_TYPE_RADIO;
1065 /* Bail out (this should be impossible) */
1066 err("Failed to set up pvrusb2 v4l dev"
1067 " due to unrecognized config");
1072 err("Failed to set up pvrusb2 v4l dev"
1073 " due to missing stream instance");
1077 dip->vdev = video_device_alloc();
1079 err("Alloc of pvrusb2 v4l video device failed");
1083 memcpy(dip->vdev,&vdev_template,sizeof(vdev_template));
1084 dip->vdev->release = video_device_release;
1085 mutex_lock(&device_lock);
1088 unit_number = pvr2_hdw_get_unit_number(vp->channel.mc_head->hdw);
1089 if ((unit_number >= 0) && (unit_number < PVR_NUM)) {
1090 mindevnum = video_nr[unit_number];
1092 if ((video_register_device(dip->vdev, v4l_type, mindevnum) < 0) &&
1093 (video_register_device(dip->vdev, v4l_type, -1) < 0)) {
1094 err("Failed to register pvrusb2 v4l video device");
1096 printk(KERN_INFO "pvrusb2: registered device video%d [%s]\n",
1097 dip->vdev->minor,pvr2_config_get_name(dip->config));
1100 if ((dip->vdev->minor < sizeof(devices)/sizeof(devices[0])) &&
1101 (devices[dip->vdev->minor] == NULL)) {
1102 dip->ctxt_idx = dip->vdev->minor;
1103 devices[dip->ctxt_idx] = dip;
1105 mutex_unlock(&device_lock);
1107 pvr2_hdw_v4l_store_minor_number(vp->channel.mc_head->hdw,
1112 struct pvr2_v4l2 *pvr2_v4l2_create(struct pvr2_context *mnp)
1114 struct pvr2_v4l2 *vp;
1116 vp = kmalloc(sizeof(*vp),GFP_KERNEL);
1118 memset(vp,0,sizeof(*vp));
1119 vp->video_dev.ctxt_idx = -1;
1120 pvr2_channel_init(&vp->channel,mnp);
1121 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr2_v4l2 id=%p",vp);
1123 vp->channel.check_func = pvr2_v4l2_internal_check;
1125 /* register streams */
1126 pvr2_v4l2_dev_init(&vp->video_dev,vp,pvr2_config_mpeg);
1133 Stuff for Emacs to see, in order to encourage consistent editing style:
1134 *** Local Variables: ***
1136 *** fill-column: 75 ***
1137 *** tab-width: 8 ***
1138 *** c-basic-offset: 8 ***