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 "pvrusb2-context.h"
25 #include "pvrusb2-hdw.h"
27 #include "pvrusb2-debug.h"
28 #include "pvrusb2-v4l2.h"
29 #include "pvrusb2-ioread.h"
30 #include <linux/videodev2.h>
31 #include <media/v4l2-common.h>
37 /* V4L no longer provide the ability to set / get a private context pointer
38 (i.e. video_get_drvdata / video_set_drvdata), which means we have to
39 concoct our own context locating mechanism. Supposedly this is intended
40 to simplify driver implementation. It's not clear to me how that can
41 possibly be true. Our solution here is to maintain a lookup table of
42 our context instances, indexed by the minor device number of the V4L
43 device. See pvr2_v4l2_open() for some implications of this approach. */
44 static struct pvr2_v4l2_dev *devices[256];
45 static DEFINE_MUTEX(device_lock);
47 struct pvr2_v4l2_dev {
48 struct pvr2_v4l2 *v4lp;
49 struct video_device *vdev;
50 struct pvr2_context_stream *stream;
52 enum pvr2_config config;
56 struct pvr2_channel channel;
57 struct pvr2_v4l2_dev *dev_info;
58 enum v4l2_priority prio;
59 struct pvr2_ioread *rhp;
61 struct pvr2_v4l2 *vhead;
62 struct pvr2_v4l2_fh *vnext;
63 struct pvr2_v4l2_fh *vprev;
64 wait_queue_head_t wait_data;
69 struct pvr2_channel channel;
70 struct pvr2_v4l2_fh *vfirst;
71 struct pvr2_v4l2_fh *vlast;
73 struct v4l2_prio_state prio;
76 struct pvr2_v4l2_dev video_dev;
79 static int video_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
80 module_param_array(video_nr, int, NULL, 0444);
81 MODULE_PARM_DESC(video_nr, "Offset for device's minor");
83 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 |
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 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 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;
391 ret = pvr2_ctrl_set_value(
392 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),
393 vf->frequency * 62500);
397 case VIDIOC_G_FREQUENCY:
399 struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
401 ret = pvr2_ctrl_get_value(
402 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),
409 case VIDIOC_ENUM_FMT:
411 struct v4l2_fmtdesc *fd = (struct v4l2_fmtdesc *)arg;
413 /* Only one format is supported : mpeg.*/
417 memcpy(fd, pvr_fmtdesc, sizeof(struct v4l2_fmtdesc));
424 struct v4l2_format *vf = (struct v4l2_format *)arg;
427 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
428 memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
429 sizeof(struct v4l2_format));
432 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES),
434 vf->fmt.pix.width = val;
437 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES),
439 vf->fmt.pix.height = val;
442 case V4L2_BUF_TYPE_VBI_CAPTURE:
443 // ????? Still need to figure out to do VBI correctly
456 struct v4l2_format *vf = (struct v4l2_format *)arg;
460 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
461 int h = vf->fmt.pix.height;
462 int w = vf->fmt.pix.width;
466 } else if (h > 625) {
471 } else if (w > 720) {
475 memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
476 sizeof(struct v4l2_format));
477 vf->fmt.pix.width = w;
478 vf->fmt.pix.height = h;
480 if (cmd == VIDIOC_S_FMT) {
482 pvr2_hdw_get_ctrl_by_id(hdw,
486 pvr2_hdw_get_ctrl_by_id(hdw,
491 case V4L2_BUF_TYPE_VBI_CAPTURE:
492 // ????? Still need to figure out to do VBI correctly
502 case VIDIOC_STREAMON:
504 ret = pvr2_hdw_set_stream_type(hdw,dev_info->config);
505 if (ret < 0) return ret;
506 ret = pvr2_hdw_set_streaming(hdw,!0);
510 case VIDIOC_STREAMOFF:
512 ret = pvr2_hdw_set_streaming(hdw,0);
516 case VIDIOC_QUERYCTRL:
518 struct pvr2_ctrl *cptr;
519 struct v4l2_queryctrl *vc = (struct v4l2_queryctrl *)arg;
521 if (vc->id & V4L2_CTRL_FLAG_NEXT_CTRL) {
522 cptr = pvr2_hdw_get_ctrl_nextv4l(
523 hdw,(vc->id & ~V4L2_CTRL_FLAG_NEXT_CTRL));
524 if (cptr) vc->id = pvr2_ctrl_get_v4lid(cptr);
526 cptr = pvr2_hdw_get_ctrl_v4l(hdw,vc->id);
529 pvr2_trace(PVR2_TRACE_V4LIOCTL,
530 "QUERYCTRL id=0x%x not implemented here",
536 pvr2_trace(PVR2_TRACE_V4LIOCTL,
537 "QUERYCTRL id=0x%x mapping name=%s (%s)",
538 vc->id,pvr2_ctrl_get_name(cptr),
539 pvr2_ctrl_get_desc(cptr));
540 strlcpy(vc->name,pvr2_ctrl_get_desc(cptr),sizeof(vc->name));
541 vc->flags = pvr2_ctrl_get_v4lflags(cptr);
542 vc->default_value = pvr2_ctrl_get_def(cptr);
543 switch (pvr2_ctrl_get_type(cptr)) {
545 vc->type = V4L2_CTRL_TYPE_MENU;
547 vc->maximum = pvr2_ctrl_get_cnt(cptr) - 1;
551 vc->type = V4L2_CTRL_TYPE_BOOLEAN;
557 vc->type = V4L2_CTRL_TYPE_INTEGER;
558 vc->minimum = pvr2_ctrl_get_min(cptr);
559 vc->maximum = pvr2_ctrl_get_max(cptr);
563 pvr2_trace(PVR2_TRACE_V4LIOCTL,
564 "QUERYCTRL id=0x%x name=%s not mappable",
565 vc->id,pvr2_ctrl_get_name(cptr));
572 case VIDIOC_QUERYMENU:
574 struct v4l2_querymenu *vm = (struct v4l2_querymenu *)arg;
575 unsigned int cnt = 0;
576 ret = pvr2_ctrl_get_valname(pvr2_hdw_get_ctrl_v4l(hdw,vm->id),
578 vm->name,sizeof(vm->name)-1,
586 struct v4l2_control *vc = (struct v4l2_control *)arg;
588 ret = pvr2_ctrl_get_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
596 struct v4l2_control *vc = (struct v4l2_control *)arg;
597 ret = pvr2_ctrl_set_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
602 case VIDIOC_G_EXT_CTRLS:
604 struct v4l2_ext_controls *ctls =
605 (struct v4l2_ext_controls *)arg;
606 struct v4l2_ext_control *ctrl;
609 for (idx = 0; idx < ctls->count; idx++) {
610 ctrl = ctls->controls + idx;
611 ret = pvr2_ctrl_get_value(
612 pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),&val);
614 ctls->error_idx = idx;
617 /* Ensure that if read as a 64 bit value, the user
618 will still get a hopefully sane value */
625 case VIDIOC_S_EXT_CTRLS:
627 struct v4l2_ext_controls *ctls =
628 (struct v4l2_ext_controls *)arg;
629 struct v4l2_ext_control *ctrl;
631 for (idx = 0; idx < ctls->count; idx++) {
632 ctrl = ctls->controls + idx;
633 ret = pvr2_ctrl_set_value(
634 pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),
637 ctls->error_idx = idx;
644 case VIDIOC_TRY_EXT_CTRLS:
646 struct v4l2_ext_controls *ctls =
647 (struct v4l2_ext_controls *)arg;
648 struct v4l2_ext_control *ctrl;
649 struct pvr2_ctrl *pctl;
651 /* For the moment just validate that the requested control
653 for (idx = 0; idx < ctls->count; idx++) {
654 ctrl = ctls->controls + idx;
655 pctl = pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id);
658 ctls->error_idx = idx;
665 case VIDIOC_LOG_STATUS:
667 pvr2_hdw_trigger_module_log(hdw);
673 ret = v4l_compat_translate_ioctl(inode,file,cmd,
674 arg,pvr2_v4l2_do_ioctl);
677 pvr2_hdw_commit_ctl(hdw);
680 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
681 pvr2_trace(PVR2_TRACE_V4LIOCTL,
682 "pvr2_v4l2_do_ioctl failure, ret=%d",ret);
684 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
685 pvr2_trace(PVR2_TRACE_V4LIOCTL,
686 "pvr2_v4l2_do_ioctl failure, ret=%d"
687 " command was:",ret);
688 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),
693 pvr2_trace(PVR2_TRACE_V4LIOCTL,
694 "pvr2_v4l2_do_ioctl complete, ret=%d (0x%x)",
701 static void pvr2_v4l2_dev_destroy(struct pvr2_v4l2_dev *dip)
703 pvr2_trace(PVR2_TRACE_INIT,
704 "unregistering device video%d [%s]",
705 dip->vdev->minor,pvr2_config_get_name(dip->config));
706 if (dip->ctxt_idx >= 0) {
707 mutex_lock(&device_lock);
708 devices[dip->ctxt_idx] = NULL;
710 mutex_unlock(&device_lock);
712 video_unregister_device(dip->vdev);
716 static void pvr2_v4l2_destroy_no_lock(struct pvr2_v4l2 *vp)
718 pvr2_hdw_v4l_store_minor_number(vp->channel.mc_head->hdw,-1);
719 pvr2_v4l2_dev_destroy(&vp->video_dev);
721 pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr2_v4l2 id=%p",vp);
722 pvr2_channel_done(&vp->channel);
727 void pvr2_v4l2_internal_check(struct pvr2_channel *chp)
729 struct pvr2_v4l2 *vp;
730 vp = container_of(chp,struct pvr2_v4l2,channel);
731 if (!vp->channel.mc_head->disconnect_flag) return;
732 if (vp->vfirst) return;
733 pvr2_v4l2_destroy_no_lock(vp);
737 int pvr2_v4l2_ioctl(struct inode *inode, struct file *file,
738 unsigned int cmd, unsigned long arg)
741 /* Temporary hack : use ivtv api until a v4l2 one is available. */
742 #define IVTV_IOC_G_CODEC 0xFFEE7703
743 #define IVTV_IOC_S_CODEC 0xFFEE7704
744 if (cmd == IVTV_IOC_G_CODEC || cmd == IVTV_IOC_S_CODEC) return 0;
745 return video_usercopy(inode, file, cmd, arg, pvr2_v4l2_do_ioctl);
749 int pvr2_v4l2_release(struct inode *inode, struct file *file)
751 struct pvr2_v4l2_fh *fhp = file->private_data;
752 struct pvr2_v4l2 *vp = fhp->vhead;
753 struct pvr2_context *mp = fhp->vhead->channel.mc_head;
755 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_release");
758 struct pvr2_stream *sp;
759 struct pvr2_hdw *hdw;
760 hdw = fhp->channel.mc_head->hdw;
761 pvr2_hdw_set_streaming(hdw,0);
762 sp = pvr2_ioread_get_stream(fhp->rhp);
763 if (sp) pvr2_stream_set_callback(sp,0,0);
764 pvr2_ioread_destroy(fhp->rhp);
767 v4l2_prio_close(&vp->prio, &fhp->prio);
768 file->private_data = NULL;
770 pvr2_context_enter(mp); do {
772 fhp->vnext->vprev = fhp->vprev;
774 vp->vlast = fhp->vprev;
777 fhp->vprev->vnext = fhp->vnext;
779 vp->vfirst = fhp->vnext;
784 pvr2_channel_done(&fhp->channel);
785 pvr2_trace(PVR2_TRACE_STRUCT,
786 "Destroying pvr_v4l2_fh id=%p",fhp);
788 if (vp->channel.mc_head->disconnect_flag && !vp->vfirst) {
789 pvr2_v4l2_destroy_no_lock(vp);
791 } while (0); pvr2_context_exit(mp);
796 int pvr2_v4l2_open(struct inode *inode, struct file *file)
798 struct pvr2_v4l2_dev *dip = 0; /* Our own context pointer */
799 struct pvr2_v4l2_fh *fhp;
800 struct pvr2_v4l2 *vp;
801 struct pvr2_hdw *hdw;
803 mutex_lock(&device_lock);
804 /* MCI 7-Jun-2006 Even though we're just doing what amounts to an
805 atomic read of the device mapping array here, we still need the
806 mutex. The problem is that there is a tiny race possible when
807 we register the device. We can't update the device mapping
808 array until after the device has been registered, owing to the
809 fact that we can't know the minor device number until after the
810 registration succeeds. And if another thread tries to open the
811 device in the window of time after registration but before the
812 map is updated, then it will get back an erroneous null pointer
813 and the open will result in a spurious failure. The only way to
814 prevent that is to (a) be inside the mutex here before we access
815 the array, and (b) cover the entire registration process later
816 on with this same mutex. Thus if we get inside the mutex here,
817 then we can be assured that the registration process actually
818 completed correctly. This is an unhappy complication from the
819 use of global data in a driver that lives in a preemptible
820 environment. It sure would be nice if the video device itself
821 had a means for storing and retrieving a local context pointer.
822 Oh wait. It did. But now it's gone. Silly me. */
824 unsigned int midx = iminor(file->f_dentry->d_inode);
825 if (midx < sizeof(devices)/sizeof(devices[0])) {
829 mutex_unlock(&device_lock);
831 if (!dip) return -ENODEV; /* Should be impossible but I'm paranoid */
834 hdw = vp->channel.hdw;
836 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_open");
838 if (!pvr2_hdw_dev_ok(hdw)) {
839 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,
840 "pvr2_v4l2_open: hardware not ready");
844 fhp = kmalloc(sizeof(*fhp),GFP_KERNEL);
848 memset(fhp,0,sizeof(*fhp));
850 init_waitqueue_head(&fhp->wait_data);
853 pvr2_context_enter(vp->channel.mc_head); do {
854 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr_v4l2_fh id=%p",fhp);
855 pvr2_channel_init(&fhp->channel,vp->channel.mc_head);
857 fhp->vprev = vp->vlast;
859 vp->vlast->vnext = fhp;
865 } while (0); pvr2_context_exit(vp->channel.mc_head);
868 file->private_data = fhp;
869 v4l2_prio_open(&vp->prio,&fhp->prio);
871 fhp->fw_mode_flag = pvr2_hdw_cpufw_get_enabled(hdw);
877 static void pvr2_v4l2_notify(struct pvr2_v4l2_fh *fhp)
879 wake_up(&fhp->wait_data);
882 static int pvr2_v4l2_iosetup(struct pvr2_v4l2_fh *fh)
885 struct pvr2_stream *sp;
886 struct pvr2_hdw *hdw;
887 if (fh->rhp) return 0;
889 /* First read() attempt. Try to claim the stream and start
891 if ((ret = pvr2_channel_claim_stream(&fh->channel,
892 fh->dev_info->stream)) != 0) {
893 /* Someone else must already have it */
897 fh->rhp = pvr2_channel_create_mpeg_stream(fh->dev_info->stream);
899 pvr2_channel_claim_stream(&fh->channel,0);
903 hdw = fh->channel.mc_head->hdw;
904 sp = fh->dev_info->stream->stream;
905 pvr2_stream_set_callback(sp,(pvr2_stream_callback)pvr2_v4l2_notify,fh);
906 pvr2_hdw_set_stream_type(hdw,fh->dev_info->config);
907 pvr2_hdw_set_streaming(hdw,!0);
908 ret = pvr2_ioread_set_enabled(fh->rhp,!0);
914 static ssize_t pvr2_v4l2_read(struct file *file,
915 char __user *buff, size_t count, loff_t *ppos)
917 struct pvr2_v4l2_fh *fh = file->private_data;
920 if (fh->fw_mode_flag) {
921 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
925 unsigned int offs = *ppos;
927 tbuf = kmalloc(PAGE_SIZE,GFP_KERNEL);
928 if (!tbuf) return -ENOMEM;
932 if (c1 > PAGE_SIZE) c1 = PAGE_SIZE;
933 c2 = pvr2_hdw_cpufw_get(hdw,offs,tbuf,c1);
939 if (copy_to_user(buff,tbuf,c2)) {
954 ret = pvr2_v4l2_iosetup(fh);
961 ret = pvr2_ioread_read(fh->rhp,buff,count);
963 if (ret != -EAGAIN) break;
964 if (file->f_flags & O_NONBLOCK) break;
965 /* Doing blocking I/O. Wait here. */
966 ret = wait_event_interruptible(
968 pvr2_ioread_avail(fh->rhp) >= 0);
976 static unsigned int pvr2_v4l2_poll(struct file *file, poll_table *wait)
978 unsigned int mask = 0;
979 struct pvr2_v4l2_fh *fh = file->private_data;
982 if (fh->fw_mode_flag) {
983 mask |= POLLIN | POLLRDNORM;
988 ret = pvr2_v4l2_iosetup(fh);
989 if (ret) return POLLERR;
992 poll_wait(file,&fh->wait_data,wait);
994 if (pvr2_ioread_avail(fh->rhp) >= 0) {
995 mask |= POLLIN | POLLRDNORM;
1002 static struct file_operations vdev_fops = {
1003 .owner = THIS_MODULE,
1004 .open = pvr2_v4l2_open,
1005 .release = pvr2_v4l2_release,
1006 .read = pvr2_v4l2_read,
1007 .ioctl = pvr2_v4l2_ioctl,
1008 .llseek = no_llseek,
1009 .poll = pvr2_v4l2_poll,
1013 #define VID_HARDWARE_PVRUSB2 38 /* FIXME : need a good value */
1015 static struct video_device vdev_template = {
1016 .owner = THIS_MODULE,
1017 .type = VID_TYPE_CAPTURE | VID_TYPE_TUNER,
1018 .type2 = (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VBI_CAPTURE
1019 | V4L2_CAP_TUNER | V4L2_CAP_AUDIO
1020 | V4L2_CAP_READWRITE),
1021 .hardware = VID_HARDWARE_PVRUSB2,
1026 static void pvr2_v4l2_dev_init(struct pvr2_v4l2_dev *dip,
1027 struct pvr2_v4l2 *vp,
1028 enum pvr2_config cfg)
1038 case pvr2_config_mpeg:
1039 v4l_type = VFL_TYPE_GRABBER;
1040 dip->stream = &vp->channel.mc_head->video_stream;
1042 case pvr2_config_vbi:
1043 v4l_type = VFL_TYPE_VBI;
1045 case pvr2_config_radio:
1046 v4l_type = VFL_TYPE_RADIO;
1049 /* Bail out (this should be impossible) */
1050 err("Failed to set up pvrusb2 v4l dev"
1051 " due to unrecognized config");
1056 err("Failed to set up pvrusb2 v4l dev"
1057 " due to missing stream instance");
1061 dip->vdev = video_device_alloc();
1063 err("Alloc of pvrusb2 v4l video device failed");
1067 memcpy(dip->vdev,&vdev_template,sizeof(vdev_template));
1068 dip->vdev->release = video_device_release;
1069 mutex_lock(&device_lock);
1072 unit_number = pvr2_hdw_get_unit_number(vp->channel.mc_head->hdw);
1073 if ((unit_number >= 0) && (unit_number < PVR_NUM)) {
1074 mindevnum = video_nr[unit_number];
1076 if ((video_register_device(dip->vdev, v4l_type, mindevnum) < 0) &&
1077 (video_register_device(dip->vdev, v4l_type, -1) < 0)) {
1078 err("Failed to register pvrusb2 v4l video device");
1080 pvr2_trace(PVR2_TRACE_INIT,
1081 "registered device video%d [%s]",
1082 dip->vdev->minor,pvr2_config_get_name(dip->config));
1085 if ((dip->vdev->minor < sizeof(devices)/sizeof(devices[0])) &&
1086 (devices[dip->vdev->minor] == NULL)) {
1087 dip->ctxt_idx = dip->vdev->minor;
1088 devices[dip->ctxt_idx] = dip;
1090 mutex_unlock(&device_lock);
1092 pvr2_hdw_v4l_store_minor_number(vp->channel.mc_head->hdw,
1097 struct pvr2_v4l2 *pvr2_v4l2_create(struct pvr2_context *mnp)
1099 struct pvr2_v4l2 *vp;
1101 vp = kmalloc(sizeof(*vp),GFP_KERNEL);
1103 memset(vp,0,sizeof(*vp));
1104 vp->video_dev.ctxt_idx = -1;
1105 pvr2_channel_init(&vp->channel,mnp);
1106 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr2_v4l2 id=%p",vp);
1108 vp->channel.check_func = pvr2_v4l2_internal_check;
1110 /* register streams */
1111 pvr2_v4l2_dev_init(&vp->video_dev,vp,pvr2_config_mpeg);
1118 Stuff for Emacs to see, in order to encourage consistent editing style:
1119 *** Local Variables: ***
1121 *** fill-column: 75 ***
1122 *** tab-width: 8 ***
1123 *** c-basic-offset: 8 ***