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;
67 struct pvr2_v4l2_dev *vdev;
70 static int video_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
71 module_param_array(video_nr, int, NULL, 0444);
72 MODULE_PARM_DESC(video_nr, "Offset for device's minor");
74 static struct v4l2_capability pvr_capability ={
76 .card = "Hauppauge WinTV pvr-usb2",
78 .version = KERNEL_VERSION(0,8,0),
79 .capabilities = (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VBI_CAPTURE |
80 V4L2_CAP_TUNER | V4L2_CAP_AUDIO |
85 static struct v4l2_tuner pvr_v4l2_tuners[]= {
89 .type = V4L2_TUNER_ANALOG_TV,
90 .capability = (V4L2_TUNER_CAP_NORM |
91 V4L2_TUNER_CAP_STEREO |
92 V4L2_TUNER_CAP_LANG1 |
93 V4L2_TUNER_CAP_LANG2),
96 .rxsubchans = V4L2_TUNER_SUB_STEREO,
97 .audmode = V4L2_TUNER_MODE_STEREO,
100 .reserved = {0,0,0,0}
104 static struct v4l2_fmtdesc pvr_fmtdesc [] = {
107 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
108 .flags = V4L2_FMT_FLAG_COMPRESSED,
109 .description = "MPEG1/2",
110 // This should really be V4L2_PIX_FMT_MPEG, but xawtv
111 // breaks when I do that.
112 .pixelformat = 0, // V4L2_PIX_FMT_MPEG,
113 .reserved = { 0, 0, 0, 0 }
117 #define PVR_FORMAT_PIX 0
118 #define PVR_FORMAT_VBI 1
120 static struct v4l2_format pvr_format [] = {
122 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
127 // This should really be V4L2_PIX_FMT_MPEG,
128 // but xawtv breaks when I do that.
129 .pixelformat = 0, // V4L2_PIX_FMT_MPEG,
130 .field = V4L2_FIELD_INTERLACED,
131 .bytesperline = 0, // doesn't make sense
133 //FIXME : Don't know what to put here...
134 .sizeimage = (32*1024),
135 .colorspace = 0, // doesn't make sense here
141 .type = V4L2_BUF_TYPE_VBI_CAPTURE,
144 .sampling_rate = 27000000,
146 .samples_per_line = 1443,
147 .sample_format = V4L2_PIX_FMT_GREY,
160 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
163 static int pvr2_v4l2_do_ioctl(struct inode *inode, struct file *file,
164 unsigned int cmd, void *arg)
166 struct pvr2_v4l2_fh *fh = file->private_data;
167 struct pvr2_v4l2 *vp = fh->vhead;
168 struct pvr2_v4l2_dev *dev_info = fh->dev_info;
169 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
172 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
173 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),cmd);
176 if (!pvr2_hdw_dev_ok(hdw)) {
177 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
178 "ioctl failed - bad or no context");
188 case VIDIOC_S_FREQUENCY:
189 ret = v4l2_prio_check(&vp->prio, &fh->prio);
195 case VIDIOC_QUERYCAP:
197 struct v4l2_capability *cap = arg;
199 memcpy(cap, &pvr_capability, sizeof(struct v4l2_capability));
205 case VIDIOC_G_PRIORITY:
207 enum v4l2_priority *p = arg;
209 *p = v4l2_prio_max(&vp->prio);
214 case VIDIOC_S_PRIORITY:
216 enum v4l2_priority *prio = arg;
218 ret = v4l2_prio_change(&vp->prio, &fh->prio, *prio);
224 struct v4l2_standard *vs = (struct v4l2_standard *)arg;
226 ret = pvr2_hdw_get_stdenum_value(hdw,vs,idx+1);
233 ret = pvr2_ctrl_get_value(
234 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR),&val);
235 *(v4l2_std_id *)arg = val;
241 ret = pvr2_ctrl_set_value(
242 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR),
243 *(v4l2_std_id *)arg);
247 case VIDIOC_ENUMINPUT:
249 struct pvr2_ctrl *cptr;
250 struct v4l2_input *vi = (struct v4l2_input *)arg;
251 struct v4l2_input tmp;
254 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
256 memset(&tmp,0,sizeof(tmp));
257 tmp.index = vi->index;
260 case PVR2_CVAL_INPUT_TV:
261 case PVR2_CVAL_INPUT_RADIO:
262 tmp.type = V4L2_INPUT_TYPE_TUNER;
264 case PVR2_CVAL_INPUT_SVIDEO:
265 case PVR2_CVAL_INPUT_COMPOSITE:
266 tmp.type = V4L2_INPUT_TYPE_CAMERA;
275 pvr2_ctrl_get_valname(cptr,vi->index,
276 tmp.name,sizeof(tmp.name)-1,&cnt);
279 /* Don't bother with audioset, since this driver currently
280 always switches the audio whenever the video is
283 /* Handling std is a tougher problem. It doesn't make
284 sense in cases where a device might be multi-standard.
285 We could just copy out the current value for the
286 standard, but it can change over time. For now just
289 memcpy(vi, &tmp, sizeof(tmp));
297 struct pvr2_ctrl *cptr;
298 struct v4l2_input *vi = (struct v4l2_input *)arg;
300 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
302 ret = pvr2_ctrl_get_value(cptr,&val);
309 struct v4l2_input *vi = (struct v4l2_input *)arg;
310 ret = pvr2_ctrl_set_value(
311 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
316 case VIDIOC_ENUMAUDIO:
335 struct v4l2_tuner *vt = (struct v4l2_tuner *)arg;
336 unsigned int status_mask;
338 if (vt->index !=0) break;
340 status_mask = pvr2_hdw_get_signal_status(hdw);
342 memcpy(vt, &pvr_v4l2_tuners[vt->index],
343 sizeof(struct v4l2_tuner));
346 if (status_mask & PVR2_SIGNAL_OK) {
347 if (status_mask & PVR2_SIGNAL_STEREO) {
348 vt->rxsubchans = V4L2_TUNER_SUB_STEREO;
350 vt->rxsubchans = V4L2_TUNER_SUB_MONO;
352 if (status_mask & PVR2_SIGNAL_SAP) {
353 vt->rxsubchans |= (V4L2_TUNER_SUB_LANG1 |
354 V4L2_TUNER_SUB_LANG2);
360 ret = pvr2_ctrl_get_value(
361 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_AUDIOMODE),
369 struct v4l2_tuner *vt=(struct v4l2_tuner *)arg;
374 ret = pvr2_ctrl_set_value(
375 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_AUDIOMODE),
379 case VIDIOC_S_FREQUENCY:
381 const struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
382 ret = pvr2_ctrl_set_value(
383 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),
384 vf->frequency * 62500);
388 case VIDIOC_G_FREQUENCY:
390 struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
392 ret = pvr2_ctrl_get_value(
393 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),
400 case VIDIOC_ENUM_FMT:
402 struct v4l2_fmtdesc *fd = (struct v4l2_fmtdesc *)arg;
404 /* Only one format is supported : mpeg.*/
408 memcpy(fd, pvr_fmtdesc, sizeof(struct v4l2_fmtdesc));
415 struct v4l2_format *vf = (struct v4l2_format *)arg;
418 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
419 memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
420 sizeof(struct v4l2_format));
423 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES),
425 vf->fmt.pix.width = val;
428 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES),
430 vf->fmt.pix.height = val;
433 case V4L2_BUF_TYPE_VBI_CAPTURE:
434 // ????? Still need to figure out to do VBI correctly
447 struct v4l2_format *vf = (struct v4l2_format *)arg;
451 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
453 struct pvr2_ctrl *hcp,*vcp;
454 int h = vf->fmt.pix.height;
455 int w = vf->fmt.pix.width;
456 hcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES);
457 vcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES);
459 lmin = pvr2_ctrl_get_min(hcp);
460 lmax = pvr2_ctrl_get_max(hcp);
463 } else if (w > lmax) {
466 lmin = pvr2_ctrl_get_min(vcp);
467 lmax = pvr2_ctrl_get_max(vcp);
470 } else if (h > lmax) {
474 memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
475 sizeof(struct v4l2_format));
476 vf->fmt.pix.width = w;
477 vf->fmt.pix.height = h;
479 if (cmd == VIDIOC_S_FMT) {
480 pvr2_ctrl_set_value(hcp,vf->fmt.pix.width);
481 pvr2_ctrl_set_value(vcp,vf->fmt.pix.height);
484 case V4L2_BUF_TYPE_VBI_CAPTURE:
485 // ????? Still need to figure out to do VBI correctly
495 case VIDIOC_STREAMON:
497 ret = pvr2_hdw_set_stream_type(hdw,dev_info->config);
498 if (ret < 0) return ret;
499 ret = pvr2_hdw_set_streaming(hdw,!0);
503 case VIDIOC_STREAMOFF:
505 ret = pvr2_hdw_set_streaming(hdw,0);
509 case VIDIOC_QUERYCTRL:
511 struct pvr2_ctrl *cptr;
512 struct v4l2_queryctrl *vc = (struct v4l2_queryctrl *)arg;
514 if (vc->id & V4L2_CTRL_FLAG_NEXT_CTRL) {
515 cptr = pvr2_hdw_get_ctrl_nextv4l(
516 hdw,(vc->id & ~V4L2_CTRL_FLAG_NEXT_CTRL));
517 if (cptr) vc->id = pvr2_ctrl_get_v4lid(cptr);
519 cptr = pvr2_hdw_get_ctrl_v4l(hdw,vc->id);
522 pvr2_trace(PVR2_TRACE_V4LIOCTL,
523 "QUERYCTRL id=0x%x not implemented here",
529 pvr2_trace(PVR2_TRACE_V4LIOCTL,
530 "QUERYCTRL id=0x%x mapping name=%s (%s)",
531 vc->id,pvr2_ctrl_get_name(cptr),
532 pvr2_ctrl_get_desc(cptr));
533 strlcpy(vc->name,pvr2_ctrl_get_desc(cptr),sizeof(vc->name));
534 vc->flags = pvr2_ctrl_get_v4lflags(cptr);
535 vc->default_value = pvr2_ctrl_get_def(cptr);
536 switch (pvr2_ctrl_get_type(cptr)) {
538 vc->type = V4L2_CTRL_TYPE_MENU;
540 vc->maximum = pvr2_ctrl_get_cnt(cptr) - 1;
544 vc->type = V4L2_CTRL_TYPE_BOOLEAN;
550 vc->type = V4L2_CTRL_TYPE_INTEGER;
551 vc->minimum = pvr2_ctrl_get_min(cptr);
552 vc->maximum = pvr2_ctrl_get_max(cptr);
556 pvr2_trace(PVR2_TRACE_V4LIOCTL,
557 "QUERYCTRL id=0x%x name=%s not mappable",
558 vc->id,pvr2_ctrl_get_name(cptr));
565 case VIDIOC_QUERYMENU:
567 struct v4l2_querymenu *vm = (struct v4l2_querymenu *)arg;
568 unsigned int cnt = 0;
569 ret = pvr2_ctrl_get_valname(pvr2_hdw_get_ctrl_v4l(hdw,vm->id),
571 vm->name,sizeof(vm->name)-1,
579 struct v4l2_control *vc = (struct v4l2_control *)arg;
581 ret = pvr2_ctrl_get_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
589 struct v4l2_control *vc = (struct v4l2_control *)arg;
590 ret = pvr2_ctrl_set_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
595 case VIDIOC_G_EXT_CTRLS:
597 struct v4l2_ext_controls *ctls =
598 (struct v4l2_ext_controls *)arg;
599 struct v4l2_ext_control *ctrl;
602 for (idx = 0; idx < ctls->count; idx++) {
603 ctrl = ctls->controls + idx;
604 ret = pvr2_ctrl_get_value(
605 pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),&val);
607 ctls->error_idx = idx;
610 /* Ensure that if read as a 64 bit value, the user
611 will still get a hopefully sane value */
618 case VIDIOC_S_EXT_CTRLS:
620 struct v4l2_ext_controls *ctls =
621 (struct v4l2_ext_controls *)arg;
622 struct v4l2_ext_control *ctrl;
624 for (idx = 0; idx < ctls->count; idx++) {
625 ctrl = ctls->controls + idx;
626 ret = pvr2_ctrl_set_value(
627 pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),
630 ctls->error_idx = idx;
637 case VIDIOC_TRY_EXT_CTRLS:
639 struct v4l2_ext_controls *ctls =
640 (struct v4l2_ext_controls *)arg;
641 struct v4l2_ext_control *ctrl;
642 struct pvr2_ctrl *pctl;
644 /* For the moment just validate that the requested control
646 for (idx = 0; idx < ctls->count; idx++) {
647 ctrl = ctls->controls + idx;
648 pctl = pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id);
651 ctls->error_idx = idx;
658 case VIDIOC_LOG_STATUS:
660 pvr2_hdw_trigger_module_log(hdw);
664 #ifdef CONFIG_VIDEO_ADV_DEBUG
665 case VIDIOC_INT_G_REGISTER:
666 case VIDIOC_INT_S_REGISTER:
669 struct v4l2_register *req = (struct v4l2_register *)arg;
670 if (cmd == VIDIOC_INT_S_REGISTER) val = req->val;
671 ret = pvr2_hdw_register_access(
672 hdw,req->i2c_id,req->reg,
673 cmd == VIDIOC_INT_S_REGISTER,&val);
674 if (cmd == VIDIOC_INT_G_REGISTER) req->val = val;
680 ret = v4l_compat_translate_ioctl(inode,file,cmd,
681 arg,pvr2_v4l2_do_ioctl);
684 pvr2_hdw_commit_ctl(hdw);
687 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
688 pvr2_trace(PVR2_TRACE_V4LIOCTL,
689 "pvr2_v4l2_do_ioctl failure, ret=%d",ret);
691 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
692 pvr2_trace(PVR2_TRACE_V4LIOCTL,
693 "pvr2_v4l2_do_ioctl failure, ret=%d"
694 " command was:",ret);
695 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),
700 pvr2_trace(PVR2_TRACE_V4LIOCTL,
701 "pvr2_v4l2_do_ioctl complete, ret=%d (0x%x)",
708 static void pvr2_v4l2_dev_destroy(struct pvr2_v4l2_dev *dip)
710 printk(KERN_INFO "pvrusb2: unregistering device video%d [%s]\n",
711 dip->devbase.minor,pvr2_config_get_name(dip->config));
717 /* Actual deallocation happens later when all internal references
719 video_unregister_device(&dip->devbase);
723 static void pvr2_v4l2_destroy_no_lock(struct pvr2_v4l2 *vp)
725 pvr2_hdw_v4l_store_minor_number(vp->channel.mc_head->hdw,
726 pvr2_config_mpeg-1,-1);
727 pvr2_hdw_v4l_store_minor_number(vp->channel.mc_head->hdw,
728 pvr2_config_vbi-1,-1);
729 pvr2_hdw_v4l_store_minor_number(vp->channel.mc_head->hdw,
730 pvr2_config_radio-1,-1);
731 pvr2_v4l2_dev_destroy(vp->vdev);
733 pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr2_v4l2 id=%p",vp);
734 pvr2_channel_done(&vp->channel);
739 static void pvr2_video_device_release(struct video_device *vdev)
741 struct pvr2_v4l2_dev *dev;
742 dev = container_of(vdev,struct pvr2_v4l2_dev,devbase);
747 static void pvr2_v4l2_internal_check(struct pvr2_channel *chp)
749 struct pvr2_v4l2 *vp;
750 vp = container_of(chp,struct pvr2_v4l2,channel);
751 if (!vp->channel.mc_head->disconnect_flag) return;
752 if (vp->vfirst) return;
753 pvr2_v4l2_destroy_no_lock(vp);
757 static int pvr2_v4l2_ioctl(struct inode *inode, struct file *file,
758 unsigned int cmd, unsigned long arg)
761 /* Temporary hack : use ivtv api until a v4l2 one is available. */
762 #define IVTV_IOC_G_CODEC 0xFFEE7703
763 #define IVTV_IOC_S_CODEC 0xFFEE7704
764 if (cmd == IVTV_IOC_G_CODEC || cmd == IVTV_IOC_S_CODEC) return 0;
765 return video_usercopy(inode, file, cmd, arg, pvr2_v4l2_do_ioctl);
769 static int pvr2_v4l2_release(struct inode *inode, struct file *file)
771 struct pvr2_v4l2_fh *fhp = file->private_data;
772 struct pvr2_v4l2 *vp = fhp->vhead;
773 struct pvr2_context *mp = fhp->vhead->channel.mc_head;
775 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_release");
778 struct pvr2_stream *sp;
779 struct pvr2_hdw *hdw;
780 hdw = fhp->channel.mc_head->hdw;
781 pvr2_hdw_set_streaming(hdw,0);
782 sp = pvr2_ioread_get_stream(fhp->rhp);
783 if (sp) pvr2_stream_set_callback(sp,NULL,NULL);
784 pvr2_ioread_destroy(fhp->rhp);
787 v4l2_prio_close(&vp->prio, &fhp->prio);
788 file->private_data = NULL;
790 pvr2_context_enter(mp); do {
792 fhp->vnext->vprev = fhp->vprev;
794 vp->vlast = fhp->vprev;
797 fhp->vprev->vnext = fhp->vnext;
799 vp->vfirst = fhp->vnext;
804 pvr2_channel_done(&fhp->channel);
805 pvr2_trace(PVR2_TRACE_STRUCT,
806 "Destroying pvr_v4l2_fh id=%p",fhp);
808 if (vp->channel.mc_head->disconnect_flag && !vp->vfirst) {
809 pvr2_v4l2_destroy_no_lock(vp);
811 } while (0); pvr2_context_exit(mp);
816 static int pvr2_v4l2_open(struct inode *inode, struct file *file)
818 struct pvr2_v4l2_dev *dip; /* Our own context pointer */
819 struct pvr2_v4l2_fh *fhp;
820 struct pvr2_v4l2 *vp;
821 struct pvr2_hdw *hdw;
823 dip = container_of(video_devdata(file),struct pvr2_v4l2_dev,devbase);
826 hdw = vp->channel.hdw;
828 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_open");
830 if (!pvr2_hdw_dev_ok(hdw)) {
831 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,
832 "pvr2_v4l2_open: hardware not ready");
836 fhp = kmalloc(sizeof(*fhp),GFP_KERNEL);
840 memset(fhp,0,sizeof(*fhp));
842 init_waitqueue_head(&fhp->wait_data);
845 pvr2_context_enter(vp->channel.mc_head); do {
846 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr_v4l2_fh id=%p",fhp);
847 pvr2_channel_init(&fhp->channel,vp->channel.mc_head);
849 fhp->vprev = vp->vlast;
851 vp->vlast->vnext = fhp;
857 } while (0); pvr2_context_exit(vp->channel.mc_head);
860 file->private_data = fhp;
861 v4l2_prio_open(&vp->prio,&fhp->prio);
863 fhp->fw_mode_flag = pvr2_hdw_cpufw_get_enabled(hdw);
869 static void pvr2_v4l2_notify(struct pvr2_v4l2_fh *fhp)
871 wake_up(&fhp->wait_data);
874 static int pvr2_v4l2_iosetup(struct pvr2_v4l2_fh *fh)
877 struct pvr2_stream *sp;
878 struct pvr2_hdw *hdw;
879 if (fh->rhp) return 0;
881 /* First read() attempt. Try to claim the stream and start
883 if ((ret = pvr2_channel_claim_stream(&fh->channel,
884 fh->dev_info->stream)) != 0) {
885 /* Someone else must already have it */
889 fh->rhp = pvr2_channel_create_mpeg_stream(fh->dev_info->stream);
891 pvr2_channel_claim_stream(&fh->channel,NULL);
895 hdw = fh->channel.mc_head->hdw;
896 sp = fh->dev_info->stream->stream;
897 pvr2_stream_set_callback(sp,(pvr2_stream_callback)pvr2_v4l2_notify,fh);
898 pvr2_hdw_set_stream_type(hdw,fh->dev_info->config);
899 pvr2_hdw_set_streaming(hdw,!0);
900 ret = pvr2_ioread_set_enabled(fh->rhp,!0);
906 static ssize_t pvr2_v4l2_read(struct file *file,
907 char __user *buff, size_t count, loff_t *ppos)
909 struct pvr2_v4l2_fh *fh = file->private_data;
912 if (fh->fw_mode_flag) {
913 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
917 unsigned int offs = *ppos;
919 tbuf = kmalloc(PAGE_SIZE,GFP_KERNEL);
920 if (!tbuf) return -ENOMEM;
924 if (c1 > PAGE_SIZE) c1 = PAGE_SIZE;
925 c2 = pvr2_hdw_cpufw_get(hdw,offs,tbuf,c1);
931 if (copy_to_user(buff,tbuf,c2)) {
946 ret = pvr2_v4l2_iosetup(fh);
953 ret = pvr2_ioread_read(fh->rhp,buff,count);
955 if (ret != -EAGAIN) break;
956 if (file->f_flags & O_NONBLOCK) break;
957 /* Doing blocking I/O. Wait here. */
958 ret = wait_event_interruptible(
960 pvr2_ioread_avail(fh->rhp) >= 0);
968 static unsigned int pvr2_v4l2_poll(struct file *file, poll_table *wait)
970 unsigned int mask = 0;
971 struct pvr2_v4l2_fh *fh = file->private_data;
974 if (fh->fw_mode_flag) {
975 mask |= POLLIN | POLLRDNORM;
980 ret = pvr2_v4l2_iosetup(fh);
981 if (ret) return POLLERR;
984 poll_wait(file,&fh->wait_data,wait);
986 if (pvr2_ioread_avail(fh->rhp) >= 0) {
987 mask |= POLLIN | POLLRDNORM;
994 static const struct file_operations vdev_fops = {
995 .owner = THIS_MODULE,
996 .open = pvr2_v4l2_open,
997 .release = pvr2_v4l2_release,
998 .read = pvr2_v4l2_read,
999 .ioctl = pvr2_v4l2_ioctl,
1000 .llseek = no_llseek,
1001 .poll = pvr2_v4l2_poll,
1005 #define VID_HARDWARE_PVRUSB2 38 /* FIXME : need a good value */
1007 static struct video_device vdev_template = {
1008 .owner = THIS_MODULE,
1009 .type = VID_TYPE_CAPTURE | VID_TYPE_TUNER,
1010 .type2 = (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VBI_CAPTURE
1011 | V4L2_CAP_TUNER | V4L2_CAP_AUDIO
1012 | V4L2_CAP_READWRITE),
1013 .hardware = VID_HARDWARE_PVRUSB2,
1018 static void pvr2_v4l2_dev_init(struct pvr2_v4l2_dev *dip,
1019 struct pvr2_v4l2 *vp,
1020 enum pvr2_config cfg)
1030 case pvr2_config_mpeg:
1031 v4l_type = VFL_TYPE_GRABBER;
1032 dip->stream = &vp->channel.mc_head->video_stream;
1034 case pvr2_config_vbi:
1035 v4l_type = VFL_TYPE_VBI;
1037 case pvr2_config_radio:
1038 v4l_type = VFL_TYPE_RADIO;
1041 /* Bail out (this should be impossible) */
1042 err("Failed to set up pvrusb2 v4l dev"
1043 " due to unrecognized config");
1048 err("Failed to set up pvrusb2 v4l dev"
1049 " due to missing stream instance");
1053 memcpy(&dip->devbase,&vdev_template,sizeof(vdev_template));
1054 dip->devbase.release = pvr2_video_device_release;
1057 unit_number = pvr2_hdw_get_unit_number(vp->channel.mc_head->hdw);
1058 if ((unit_number >= 0) && (unit_number < PVR_NUM)) {
1059 mindevnum = video_nr[unit_number];
1061 if ((video_register_device(&dip->devbase, v4l_type, mindevnum) < 0) &&
1062 (video_register_device(&dip->devbase, v4l_type, -1) < 0)) {
1063 err("Failed to register pvrusb2 v4l video device");
1065 printk(KERN_INFO "pvrusb2: registered device video%d [%s]\n",
1066 dip->devbase.minor,pvr2_config_get_name(dip->config));
1069 pvr2_hdw_v4l_store_minor_number(vp->channel.mc_head->hdw,
1070 cfg-1,dip->devbase.minor);
1074 struct pvr2_v4l2 *pvr2_v4l2_create(struct pvr2_context *mnp)
1076 struct pvr2_v4l2 *vp;
1078 vp = kmalloc(sizeof(*vp),GFP_KERNEL);
1080 memset(vp,0,sizeof(*vp));
1081 vp->vdev = kmalloc(sizeof(*vp->vdev),GFP_KERNEL);
1086 memset(vp->vdev,0,sizeof(*vp->vdev));
1087 pvr2_channel_init(&vp->channel,mnp);
1088 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr2_v4l2 id=%p",vp);
1090 vp->channel.check_func = pvr2_v4l2_internal_check;
1092 /* register streams */
1093 pvr2_v4l2_dev_init(vp->vdev,vp,pvr2_config_mpeg);
1099 Stuff for Emacs to see, in order to encourage consistent editing style:
1100 *** Local Variables: ***
1102 *** fill-column: 75 ***
1103 *** tab-width: 8 ***
1104 *** c-basic-offset: 8 ***