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 video dev minor");
75 static int radio_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
76 module_param_array(radio_nr, int, NULL, 0444);
77 MODULE_PARM_DESC(radio_nr, "Offset for device's radio dev minor");
78 static int vbi_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
79 module_param_array(vbi_nr, int, NULL, 0444);
80 MODULE_PARM_DESC(vbi_nr, "Offset for device's vbi dev minor");
82 static struct v4l2_capability pvr_capability ={
84 .card = "Hauppauge WinTV pvr-usb2",
86 .version = KERNEL_VERSION(0,8,0),
87 .capabilities = (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VBI_CAPTURE |
88 V4L2_CAP_TUNER | V4L2_CAP_AUDIO | V4L2_CAP_RADIO |
93 static struct v4l2_tuner pvr_v4l2_tuners[]= {
97 .type = V4L2_TUNER_ANALOG_TV,
98 .capability = (V4L2_TUNER_CAP_NORM |
99 V4L2_TUNER_CAP_STEREO |
100 V4L2_TUNER_CAP_LANG1 |
101 V4L2_TUNER_CAP_LANG2),
104 .rxsubchans = V4L2_TUNER_SUB_STEREO,
105 .audmode = V4L2_TUNER_MODE_STEREO,
108 .reserved = {0,0,0,0}
112 static struct v4l2_fmtdesc pvr_fmtdesc [] = {
115 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
116 .flags = V4L2_FMT_FLAG_COMPRESSED,
117 .description = "MPEG1/2",
118 // This should really be V4L2_PIX_FMT_MPEG, but xawtv
119 // breaks when I do that.
120 .pixelformat = 0, // V4L2_PIX_FMT_MPEG,
121 .reserved = { 0, 0, 0, 0 }
125 #define PVR_FORMAT_PIX 0
126 #define PVR_FORMAT_VBI 1
128 static struct v4l2_format pvr_format [] = {
130 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
135 // This should really be V4L2_PIX_FMT_MPEG,
136 // but xawtv breaks when I do that.
137 .pixelformat = 0, // V4L2_PIX_FMT_MPEG,
138 .field = V4L2_FIELD_INTERLACED,
139 .bytesperline = 0, // doesn't make sense
141 //FIXME : Don't know what to put here...
142 .sizeimage = (32*1024),
143 .colorspace = 0, // doesn't make sense here
149 .type = V4L2_BUF_TYPE_VBI_CAPTURE,
152 .sampling_rate = 27000000,
154 .samples_per_line = 1443,
155 .sample_format = V4L2_PIX_FMT_GREY,
168 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
171 static int pvr2_v4l2_do_ioctl(struct inode *inode, struct file *file,
172 unsigned int cmd, void *arg)
174 struct pvr2_v4l2_fh *fh = file->private_data;
175 struct pvr2_v4l2 *vp = fh->vhead;
176 struct pvr2_v4l2_dev *dev_info = fh->dev_info;
177 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
180 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
181 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),cmd);
184 if (!pvr2_hdw_dev_ok(hdw)) {
185 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
186 "ioctl failed - bad or no context");
196 case VIDIOC_S_FREQUENCY:
197 ret = v4l2_prio_check(&vp->prio, &fh->prio);
203 case VIDIOC_QUERYCAP:
205 struct v4l2_capability *cap = arg;
207 memcpy(cap, &pvr_capability, sizeof(struct v4l2_capability));
213 case VIDIOC_G_PRIORITY:
215 enum v4l2_priority *p = arg;
217 *p = v4l2_prio_max(&vp->prio);
222 case VIDIOC_S_PRIORITY:
224 enum v4l2_priority *prio = arg;
226 ret = v4l2_prio_change(&vp->prio, &fh->prio, *prio);
232 struct v4l2_standard *vs = (struct v4l2_standard *)arg;
234 ret = pvr2_hdw_get_stdenum_value(hdw,vs,idx+1);
241 ret = pvr2_ctrl_get_value(
242 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR),&val);
243 *(v4l2_std_id *)arg = val;
249 ret = pvr2_ctrl_set_value(
250 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR),
251 *(v4l2_std_id *)arg);
255 case VIDIOC_ENUMINPUT:
257 struct pvr2_ctrl *cptr;
258 struct v4l2_input *vi = (struct v4l2_input *)arg;
259 struct v4l2_input tmp;
262 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
264 memset(&tmp,0,sizeof(tmp));
265 tmp.index = vi->index;
268 case PVR2_CVAL_INPUT_TV:
269 case PVR2_CVAL_INPUT_RADIO:
270 tmp.type = V4L2_INPUT_TYPE_TUNER;
272 case PVR2_CVAL_INPUT_SVIDEO:
273 case PVR2_CVAL_INPUT_COMPOSITE:
274 tmp.type = V4L2_INPUT_TYPE_CAMERA;
283 pvr2_ctrl_get_valname(cptr,vi->index,
284 tmp.name,sizeof(tmp.name)-1,&cnt);
287 /* Don't bother with audioset, since this driver currently
288 always switches the audio whenever the video is
291 /* Handling std is a tougher problem. It doesn't make
292 sense in cases where a device might be multi-standard.
293 We could just copy out the current value for the
294 standard, but it can change over time. For now just
297 memcpy(vi, &tmp, sizeof(tmp));
305 struct pvr2_ctrl *cptr;
306 struct v4l2_input *vi = (struct v4l2_input *)arg;
308 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
310 ret = pvr2_ctrl_get_value(cptr,&val);
317 struct v4l2_input *vi = (struct v4l2_input *)arg;
318 ret = pvr2_ctrl_set_value(
319 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
324 case VIDIOC_ENUMAUDIO:
343 struct v4l2_tuner *vt = (struct v4l2_tuner *)arg;
344 unsigned int status_mask;
346 if (vt->index !=0) break;
348 status_mask = pvr2_hdw_get_signal_status(hdw);
350 memcpy(vt, &pvr_v4l2_tuners[vt->index],
351 sizeof(struct v4l2_tuner));
354 if (status_mask & PVR2_SIGNAL_OK) {
355 if (status_mask & PVR2_SIGNAL_STEREO) {
356 vt->rxsubchans = V4L2_TUNER_SUB_STEREO;
358 vt->rxsubchans = V4L2_TUNER_SUB_MONO;
360 if (status_mask & PVR2_SIGNAL_SAP) {
361 vt->rxsubchans |= (V4L2_TUNER_SUB_LANG1 |
362 V4L2_TUNER_SUB_LANG2);
368 ret = pvr2_ctrl_get_value(
369 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_AUDIOMODE),
377 struct v4l2_tuner *vt=(struct v4l2_tuner *)arg;
382 ret = pvr2_ctrl_set_value(
383 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_AUDIOMODE),
387 case VIDIOC_S_FREQUENCY:
389 const struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
390 ret = pvr2_ctrl_set_value(
391 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),
392 vf->frequency * 62500);
396 case VIDIOC_G_FREQUENCY:
398 struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
400 ret = pvr2_ctrl_get_value(
401 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),
408 case VIDIOC_ENUM_FMT:
410 struct v4l2_fmtdesc *fd = (struct v4l2_fmtdesc *)arg;
412 /* Only one format is supported : mpeg.*/
416 memcpy(fd, pvr_fmtdesc, sizeof(struct v4l2_fmtdesc));
423 struct v4l2_format *vf = (struct v4l2_format *)arg;
426 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
427 memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
428 sizeof(struct v4l2_format));
431 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES),
433 vf->fmt.pix.width = val;
436 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES),
438 vf->fmt.pix.height = val;
441 case V4L2_BUF_TYPE_VBI_CAPTURE:
442 // ????? Still need to figure out to do VBI correctly
455 struct v4l2_format *vf = (struct v4l2_format *)arg;
459 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
461 struct pvr2_ctrl *hcp,*vcp;
462 int h = vf->fmt.pix.height;
463 int w = vf->fmt.pix.width;
464 hcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES);
465 vcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES);
467 lmin = pvr2_ctrl_get_min(hcp);
468 lmax = pvr2_ctrl_get_max(hcp);
471 } else if (w > lmax) {
474 lmin = pvr2_ctrl_get_min(vcp);
475 lmax = pvr2_ctrl_get_max(vcp);
478 } else if (h > lmax) {
482 memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
483 sizeof(struct v4l2_format));
484 vf->fmt.pix.width = w;
485 vf->fmt.pix.height = h;
487 if (cmd == VIDIOC_S_FMT) {
488 pvr2_ctrl_set_value(hcp,vf->fmt.pix.width);
489 pvr2_ctrl_set_value(vcp,vf->fmt.pix.height);
492 case V4L2_BUF_TYPE_VBI_CAPTURE:
493 // ????? Still need to figure out to do VBI correctly
503 case VIDIOC_STREAMON:
505 ret = pvr2_hdw_set_stream_type(hdw,dev_info->config);
506 if (ret < 0) return ret;
507 ret = pvr2_hdw_set_streaming(hdw,!0);
511 case VIDIOC_STREAMOFF:
513 ret = pvr2_hdw_set_streaming(hdw,0);
517 case VIDIOC_QUERYCTRL:
519 struct pvr2_ctrl *cptr;
520 struct v4l2_queryctrl *vc = (struct v4l2_queryctrl *)arg;
522 if (vc->id & V4L2_CTRL_FLAG_NEXT_CTRL) {
523 cptr = pvr2_hdw_get_ctrl_nextv4l(
524 hdw,(vc->id & ~V4L2_CTRL_FLAG_NEXT_CTRL));
525 if (cptr) vc->id = pvr2_ctrl_get_v4lid(cptr);
527 cptr = pvr2_hdw_get_ctrl_v4l(hdw,vc->id);
530 pvr2_trace(PVR2_TRACE_V4LIOCTL,
531 "QUERYCTRL id=0x%x not implemented here",
537 pvr2_trace(PVR2_TRACE_V4LIOCTL,
538 "QUERYCTRL id=0x%x mapping name=%s (%s)",
539 vc->id,pvr2_ctrl_get_name(cptr),
540 pvr2_ctrl_get_desc(cptr));
541 strlcpy(vc->name,pvr2_ctrl_get_desc(cptr),sizeof(vc->name));
542 vc->flags = pvr2_ctrl_get_v4lflags(cptr);
543 vc->default_value = pvr2_ctrl_get_def(cptr);
544 switch (pvr2_ctrl_get_type(cptr)) {
546 vc->type = V4L2_CTRL_TYPE_MENU;
548 vc->maximum = pvr2_ctrl_get_cnt(cptr) - 1;
552 vc->type = V4L2_CTRL_TYPE_BOOLEAN;
558 vc->type = V4L2_CTRL_TYPE_INTEGER;
559 vc->minimum = pvr2_ctrl_get_min(cptr);
560 vc->maximum = pvr2_ctrl_get_max(cptr);
564 pvr2_trace(PVR2_TRACE_V4LIOCTL,
565 "QUERYCTRL id=0x%x name=%s not mappable",
566 vc->id,pvr2_ctrl_get_name(cptr));
573 case VIDIOC_QUERYMENU:
575 struct v4l2_querymenu *vm = (struct v4l2_querymenu *)arg;
576 unsigned int cnt = 0;
577 ret = pvr2_ctrl_get_valname(pvr2_hdw_get_ctrl_v4l(hdw,vm->id),
579 vm->name,sizeof(vm->name)-1,
587 struct v4l2_control *vc = (struct v4l2_control *)arg;
589 ret = pvr2_ctrl_get_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
597 struct v4l2_control *vc = (struct v4l2_control *)arg;
598 ret = pvr2_ctrl_set_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
603 case VIDIOC_G_EXT_CTRLS:
605 struct v4l2_ext_controls *ctls =
606 (struct v4l2_ext_controls *)arg;
607 struct v4l2_ext_control *ctrl;
610 for (idx = 0; idx < ctls->count; idx++) {
611 ctrl = ctls->controls + idx;
612 ret = pvr2_ctrl_get_value(
613 pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),&val);
615 ctls->error_idx = idx;
618 /* Ensure that if read as a 64 bit value, the user
619 will still get a hopefully sane value */
626 case VIDIOC_S_EXT_CTRLS:
628 struct v4l2_ext_controls *ctls =
629 (struct v4l2_ext_controls *)arg;
630 struct v4l2_ext_control *ctrl;
632 for (idx = 0; idx < ctls->count; idx++) {
633 ctrl = ctls->controls + idx;
634 ret = pvr2_ctrl_set_value(
635 pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),
638 ctls->error_idx = idx;
645 case VIDIOC_TRY_EXT_CTRLS:
647 struct v4l2_ext_controls *ctls =
648 (struct v4l2_ext_controls *)arg;
649 struct v4l2_ext_control *ctrl;
650 struct pvr2_ctrl *pctl;
652 /* For the moment just validate that the requested control
654 for (idx = 0; idx < ctls->count; idx++) {
655 ctrl = ctls->controls + idx;
656 pctl = pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id);
659 ctls->error_idx = idx;
666 case VIDIOC_LOG_STATUS:
668 pvr2_hdw_trigger_module_log(hdw);
672 #ifdef CONFIG_VIDEO_ADV_DEBUG
673 case VIDIOC_INT_G_REGISTER:
674 case VIDIOC_INT_S_REGISTER:
677 struct v4l2_register *req = (struct v4l2_register *)arg;
678 if (cmd == VIDIOC_INT_S_REGISTER) val = req->val;
679 ret = pvr2_hdw_register_access(
680 hdw,req->i2c_id,req->reg,
681 cmd == VIDIOC_INT_S_REGISTER,&val);
682 if (cmd == VIDIOC_INT_G_REGISTER) req->val = val;
688 ret = v4l_compat_translate_ioctl(inode,file,cmd,
689 arg,pvr2_v4l2_do_ioctl);
692 pvr2_hdw_commit_ctl(hdw);
695 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
696 pvr2_trace(PVR2_TRACE_V4LIOCTL,
697 "pvr2_v4l2_do_ioctl failure, ret=%d",ret);
699 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
700 pvr2_trace(PVR2_TRACE_V4LIOCTL,
701 "pvr2_v4l2_do_ioctl failure, ret=%d"
702 " command was:",ret);
703 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),
708 pvr2_trace(PVR2_TRACE_V4LIOCTL,
709 "pvr2_v4l2_do_ioctl complete, ret=%d (0x%x)",
716 static void pvr2_v4l2_dev_destroy(struct pvr2_v4l2_dev *dip)
718 printk(KERN_INFO "pvrusb2: unregistering device video%d [%s]\n",
719 dip->devbase.minor,pvr2_config_get_name(dip->config));
725 /* Actual deallocation happens later when all internal references
727 video_unregister_device(&dip->devbase);
731 static void pvr2_v4l2_destroy_no_lock(struct pvr2_v4l2 *vp)
733 pvr2_hdw_v4l_store_minor_number(vp->channel.mc_head->hdw,
734 pvr2_v4l_type_video,-1);
735 pvr2_hdw_v4l_store_minor_number(vp->channel.mc_head->hdw,
736 pvr2_v4l_type_vbi,-1);
737 pvr2_hdw_v4l_store_minor_number(vp->channel.mc_head->hdw,
738 pvr2_v4l_type_radio,-1);
739 pvr2_v4l2_dev_destroy(vp->vdev);
741 pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr2_v4l2 id=%p",vp);
742 pvr2_channel_done(&vp->channel);
747 static void pvr2_video_device_release(struct video_device *vdev)
749 struct pvr2_v4l2_dev *dev;
750 dev = container_of(vdev,struct pvr2_v4l2_dev,devbase);
755 static void pvr2_v4l2_internal_check(struct pvr2_channel *chp)
757 struct pvr2_v4l2 *vp;
758 vp = container_of(chp,struct pvr2_v4l2,channel);
759 if (!vp->channel.mc_head->disconnect_flag) return;
760 if (vp->vfirst) return;
761 pvr2_v4l2_destroy_no_lock(vp);
765 static int pvr2_v4l2_ioctl(struct inode *inode, struct file *file,
766 unsigned int cmd, unsigned long arg)
769 /* Temporary hack : use ivtv api until a v4l2 one is available. */
770 #define IVTV_IOC_G_CODEC 0xFFEE7703
771 #define IVTV_IOC_S_CODEC 0xFFEE7704
772 if (cmd == IVTV_IOC_G_CODEC || cmd == IVTV_IOC_S_CODEC) return 0;
773 return video_usercopy(inode, file, cmd, arg, pvr2_v4l2_do_ioctl);
777 static int pvr2_v4l2_release(struct inode *inode, struct file *file)
779 struct pvr2_v4l2_fh *fhp = file->private_data;
780 struct pvr2_v4l2 *vp = fhp->vhead;
781 struct pvr2_context *mp = fhp->vhead->channel.mc_head;
783 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_release");
786 struct pvr2_stream *sp;
787 struct pvr2_hdw *hdw;
788 hdw = fhp->channel.mc_head->hdw;
789 pvr2_hdw_set_streaming(hdw,0);
790 sp = pvr2_ioread_get_stream(fhp->rhp);
791 if (sp) pvr2_stream_set_callback(sp,NULL,NULL);
792 pvr2_ioread_destroy(fhp->rhp);
796 if (fhp->dev_info->config == pvr2_config_radio) {
798 struct pvr2_hdw *hdw;
799 hdw = fhp->channel.mc_head->hdw;
800 if ((ret = pvr2_ctrl_set_value(
801 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
802 PVR2_CVAL_INPUT_TV))) {
807 v4l2_prio_close(&vp->prio, &fhp->prio);
808 file->private_data = NULL;
810 pvr2_context_enter(mp); do {
812 fhp->vnext->vprev = fhp->vprev;
814 vp->vlast = fhp->vprev;
817 fhp->vprev->vnext = fhp->vnext;
819 vp->vfirst = fhp->vnext;
824 pvr2_channel_done(&fhp->channel);
825 pvr2_trace(PVR2_TRACE_STRUCT,
826 "Destroying pvr_v4l2_fh id=%p",fhp);
828 if (vp->channel.mc_head->disconnect_flag && !vp->vfirst) {
829 pvr2_v4l2_destroy_no_lock(vp);
831 } while (0); pvr2_context_exit(mp);
836 static int pvr2_v4l2_open(struct inode *inode, struct file *file)
838 struct pvr2_v4l2_dev *dip; /* Our own context pointer */
839 struct pvr2_v4l2_fh *fhp;
840 struct pvr2_v4l2 *vp;
841 struct pvr2_hdw *hdw;
843 dip = container_of(video_devdata(file),struct pvr2_v4l2_dev,devbase);
846 hdw = vp->channel.hdw;
848 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_open");
850 if (!pvr2_hdw_dev_ok(hdw)) {
851 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,
852 "pvr2_v4l2_open: hardware not ready");
856 fhp = kmalloc(sizeof(*fhp),GFP_KERNEL);
860 memset(fhp,0,sizeof(*fhp));
862 init_waitqueue_head(&fhp->wait_data);
865 pvr2_context_enter(vp->channel.mc_head); do {
866 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr_v4l2_fh id=%p",fhp);
867 pvr2_channel_init(&fhp->channel,vp->channel.mc_head);
869 /* pk: warning, severe ugliness follows. 18+ only.
870 please blaim V4L(ivtv) for braindamaged interfaces,
871 not the implementor. This is probably flawed, but
872 suggestions on how to do this "right" are welcome! */
873 if (dip->config == pvr2_config_radio) {
875 if ((pvr2_channel_check_stream_no_lock(&fhp->channel,
876 fhp->dev_info->stream)) != 0) {
877 /* We can 't switch modes while streaming */
878 pvr2_channel_done(&fhp->channel);
880 pvr2_context_exit(vp->channel.mc_head);
884 if ((ret = pvr2_ctrl_set_value(
885 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
886 PVR2_CVAL_INPUT_RADIO))) {
887 pvr2_channel_done(&fhp->channel);
889 pvr2_context_exit(vp->channel.mc_head);
895 fhp->vprev = vp->vlast;
897 vp->vlast->vnext = fhp;
903 } while (0); pvr2_context_exit(vp->channel.mc_head);
906 file->private_data = fhp;
907 v4l2_prio_open(&vp->prio,&fhp->prio);
909 fhp->fw_mode_flag = pvr2_hdw_cpufw_get_enabled(hdw);
915 static void pvr2_v4l2_notify(struct pvr2_v4l2_fh *fhp)
917 wake_up(&fhp->wait_data);
920 static int pvr2_v4l2_iosetup(struct pvr2_v4l2_fh *fh)
923 struct pvr2_stream *sp;
924 struct pvr2_hdw *hdw;
925 if (fh->rhp) return 0;
927 /* First read() attempt. Try to claim the stream and start
929 if ((ret = pvr2_channel_claim_stream(&fh->channel,
930 fh->dev_info->stream)) != 0) {
931 /* Someone else must already have it */
935 fh->rhp = pvr2_channel_create_mpeg_stream(fh->dev_info->stream);
937 pvr2_channel_claim_stream(&fh->channel,NULL);
941 hdw = fh->channel.mc_head->hdw;
942 sp = fh->dev_info->stream->stream;
943 pvr2_stream_set_callback(sp,(pvr2_stream_callback)pvr2_v4l2_notify,fh);
944 pvr2_hdw_set_stream_type(hdw,fh->dev_info->config);
945 pvr2_hdw_set_streaming(hdw,!0);
946 ret = pvr2_ioread_set_enabled(fh->rhp,!0);
952 static ssize_t pvr2_v4l2_read(struct file *file,
953 char __user *buff, size_t count, loff_t *ppos)
955 struct pvr2_v4l2_fh *fh = file->private_data;
958 if (fh->fw_mode_flag) {
959 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
963 unsigned int offs = *ppos;
965 tbuf = kmalloc(PAGE_SIZE,GFP_KERNEL);
966 if (!tbuf) return -ENOMEM;
970 if (c1 > PAGE_SIZE) c1 = PAGE_SIZE;
971 c2 = pvr2_hdw_cpufw_get(hdw,offs,tbuf,c1);
977 if (copy_to_user(buff,tbuf,c2)) {
991 if (fh->dev_info->config == pvr2_config_radio) {
992 /* Radio device nodes on this device
993 cannot be read or written. */
998 ret = pvr2_v4l2_iosetup(fh);
1005 ret = pvr2_ioread_read(fh->rhp,buff,count);
1006 if (ret >= 0) break;
1007 if (ret != -EAGAIN) break;
1008 if (file->f_flags & O_NONBLOCK) break;
1009 /* Doing blocking I/O. Wait here. */
1010 ret = wait_event_interruptible(
1012 pvr2_ioread_avail(fh->rhp) >= 0);
1020 static unsigned int pvr2_v4l2_poll(struct file *file, poll_table *wait)
1022 unsigned int mask = 0;
1023 struct pvr2_v4l2_fh *fh = file->private_data;
1026 if (fh->fw_mode_flag) {
1027 mask |= POLLIN | POLLRDNORM;
1031 if (fh->dev_info->config == pvr2_config_radio) {
1032 /* Radio device nodes on this device
1033 cannot be read or written. */
1038 ret = pvr2_v4l2_iosetup(fh);
1039 if (ret) return POLLERR;
1042 poll_wait(file,&fh->wait_data,wait);
1044 if (pvr2_ioread_avail(fh->rhp) >= 0) {
1045 mask |= POLLIN | POLLRDNORM;
1052 static const struct file_operations vdev_fops = {
1053 .owner = THIS_MODULE,
1054 .open = pvr2_v4l2_open,
1055 .release = pvr2_v4l2_release,
1056 .read = pvr2_v4l2_read,
1057 .ioctl = pvr2_v4l2_ioctl,
1058 .llseek = no_llseek,
1059 .poll = pvr2_v4l2_poll,
1063 #define VID_HARDWARE_PVRUSB2 38 /* FIXME : need a good value */
1065 static struct video_device vdev_template = {
1066 .owner = THIS_MODULE,
1067 .type = VID_TYPE_CAPTURE | VID_TYPE_TUNER,
1068 .type2 = (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VBI_CAPTURE
1069 | V4L2_CAP_TUNER | V4L2_CAP_AUDIO
1070 | V4L2_CAP_READWRITE),
1071 .hardware = VID_HARDWARE_PVRUSB2,
1076 static void pvr2_v4l2_dev_init(struct pvr2_v4l2_dev *dip,
1077 struct pvr2_v4l2 *vp,
1078 enum pvr2_config cfg)
1083 enum pvr2_v4l_type pvt;
1089 case pvr2_config_mpeg:
1090 v4l_type = VFL_TYPE_GRABBER;
1091 pvt = pvr2_v4l_type_video;
1092 dip->stream = &vp->channel.mc_head->video_stream;
1094 case pvr2_config_vbi:
1095 v4l_type = VFL_TYPE_VBI;
1096 pvt = pvr2_v4l_type_vbi;
1098 case pvr2_config_radio:
1099 v4l_type = VFL_TYPE_RADIO;
1100 pvt = pvr2_v4l_type_radio;
1103 /* Bail out (this should be impossible) */
1104 err("Failed to set up pvrusb2 v4l dev"
1105 " due to unrecognized config");
1109 /* radio device doesn 't need its own stream */
1110 if (!dip->stream && cfg != pvr2_config_radio) {
1111 err("Failed to set up pvrusb2 v4l dev"
1112 " due to missing stream instance");
1116 memcpy(&dip->devbase,&vdev_template,sizeof(vdev_template));
1117 dip->devbase.release = pvr2_video_device_release;
1120 unit_number = pvr2_hdw_get_unit_number(vp->channel.mc_head->hdw);
1121 if ((unit_number >= 0) && (unit_number < PVR_NUM)) {
1124 mindevnum = vbi_nr[unit_number];
1126 case VFL_TYPE_RADIO:
1127 mindevnum = radio_nr[unit_number];
1129 case VFL_TYPE_GRABBER:
1131 mindevnum = video_nr[unit_number];
1135 if ((video_register_device(&dip->devbase, v4l_type, mindevnum) < 0) &&
1136 (video_register_device(&dip->devbase, v4l_type, -1) < 0)) {
1137 err("Failed to register pvrusb2 v4l device");
1140 case pvr2_config_mpeg:
1141 printk(KERN_INFO "pvrusb2: registered device video%d [%s]\n",
1142 dip->devbase.minor & 0x1f,
1143 pvr2_config_get_name(dip->config));
1145 case pvr2_config_vbi:
1146 printk(KERN_INFO "pvrusb2: registered device vbi%d [%s]\n",
1147 dip->devbase.minor & 0x1f,
1148 pvr2_config_get_name(dip->config));
1150 case pvr2_config_radio:
1151 printk(KERN_INFO "pvrusb2: registered device radio%d [%s]\n",
1152 dip->devbase.minor & 0x1f,
1153 pvr2_config_get_name(dip->config));
1159 pvr2_hdw_v4l_store_minor_number(vp->channel.mc_head->hdw,
1160 pvt,dip->devbase.minor);
1164 struct pvr2_v4l2 *pvr2_v4l2_create(struct pvr2_context *mnp)
1166 struct pvr2_v4l2 *vp;
1168 vp = kmalloc(sizeof(*vp),GFP_KERNEL);
1170 memset(vp,0,sizeof(*vp));
1171 vp->vdev = kmalloc(sizeof(*vp->vdev)*PVR2_NR_STREAMS,GFP_KERNEL);
1176 memset(vp->vdev,0,sizeof(*vp->vdev)*PVR2_NR_STREAMS);
1177 pvr2_channel_init(&vp->channel,mnp);
1178 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr2_v4l2 id=%p",vp);
1180 vp->channel.check_func = pvr2_v4l2_internal_check;
1182 /* register streams */
1183 pvr2_v4l2_dev_init(&vp->vdev[0],vp,pvr2_config_mpeg);
1184 pvr2_v4l2_dev_init(&vp->vdev[2],vp,pvr2_config_radio);
1190 Stuff for Emacs to see, in order to encourage consistent editing style:
1191 *** Local Variables: ***
1193 *** fill-column: 75 ***
1194 *** tab-width: 8 ***
1195 *** c-basic-offset: 8 ***