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 /* Information about this device: */
44 enum pvr2_config config; /* Expected stream format */
45 int v4l_type; /* V4L defined type for this device node */
46 enum pvr2_v4l_type minor_type; /* pvr2-understood minor device type */
50 struct pvr2_channel channel;
51 struct pvr2_v4l2_dev *dev_info;
52 enum v4l2_priority prio;
53 struct pvr2_ioread *rhp;
55 struct pvr2_v4l2 *vhead;
56 struct pvr2_v4l2_fh *vnext;
57 struct pvr2_v4l2_fh *vprev;
58 wait_queue_head_t wait_data;
64 struct pvr2_channel channel;
65 struct pvr2_v4l2_fh *vfirst;
66 struct pvr2_v4l2_fh *vlast;
68 struct v4l2_prio_state prio;
70 /* streams - Note that these must be separately, individually,
71 * allocated pointers. This is because the v4l core is going to
72 * manage their deletion - separately, individually... */
73 struct pvr2_v4l2_dev *dev_video;
74 struct pvr2_v4l2_dev *dev_radio;
77 static int video_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
78 module_param_array(video_nr, int, NULL, 0444);
79 MODULE_PARM_DESC(video_nr, "Offset for device's video dev minor");
80 static int radio_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
81 module_param_array(radio_nr, int, NULL, 0444);
82 MODULE_PARM_DESC(radio_nr, "Offset for device's radio dev minor");
83 static int vbi_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
84 module_param_array(vbi_nr, int, NULL, 0444);
85 MODULE_PARM_DESC(vbi_nr, "Offset for device's vbi dev minor");
87 static struct v4l2_capability pvr_capability ={
89 .card = "Hauppauge WinTV pvr-usb2",
91 .version = KERNEL_VERSION(0,8,0),
92 .capabilities = (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VBI_CAPTURE |
93 V4L2_CAP_TUNER | V4L2_CAP_AUDIO | V4L2_CAP_RADIO |
98 static struct v4l2_fmtdesc pvr_fmtdesc [] = {
101 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
102 .flags = V4L2_FMT_FLAG_COMPRESSED,
103 .description = "MPEG1/2",
104 // This should really be V4L2_PIX_FMT_MPEG, but xawtv
105 // breaks when I do that.
106 .pixelformat = 0, // V4L2_PIX_FMT_MPEG,
107 .reserved = { 0, 0, 0, 0 }
111 #define PVR_FORMAT_PIX 0
112 #define PVR_FORMAT_VBI 1
114 static struct v4l2_format pvr_format [] = {
116 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
121 // This should really be V4L2_PIX_FMT_MPEG,
122 // but xawtv breaks when I do that.
123 .pixelformat = 0, // V4L2_PIX_FMT_MPEG,
124 .field = V4L2_FIELD_INTERLACED,
125 .bytesperline = 0, // doesn't make sense
127 //FIXME : Don't know what to put here...
128 .sizeimage = (32*1024),
129 .colorspace = 0, // doesn't make sense here
135 .type = V4L2_BUF_TYPE_VBI_CAPTURE,
138 .sampling_rate = 27000000,
140 .samples_per_line = 1443,
141 .sample_format = V4L2_PIX_FMT_GREY,
152 static const char *get_v4l_name(int v4l_type)
155 case VFL_TYPE_GRABBER: return "video";
156 case VFL_TYPE_RADIO: return "radio";
157 case VFL_TYPE_VBI: return "vbi";
166 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
169 static int pvr2_v4l2_do_ioctl(struct inode *inode, struct file *file,
170 unsigned int cmd, void *arg)
172 struct pvr2_v4l2_fh *fh = file->private_data;
173 struct pvr2_v4l2 *vp = fh->vhead;
174 struct pvr2_v4l2_dev *dev_info = fh->dev_info;
175 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
178 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
179 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),cmd);
182 if (!pvr2_hdw_dev_ok(hdw)) {
183 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
184 "ioctl failed - bad or no context");
194 case VIDIOC_S_FREQUENCY:
195 ret = v4l2_prio_check(&vp->prio, &fh->prio);
201 case VIDIOC_QUERYCAP:
203 struct v4l2_capability *cap = arg;
205 memcpy(cap, &pvr_capability, sizeof(struct v4l2_capability));
206 strlcpy(cap->bus_info,pvr2_hdw_get_bus_info(hdw),
207 sizeof(cap->bus_info));
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:
326 /* pkt: FIXME: We are returning one "fake" input here
327 which could very well be called "whatever_we_like".
328 This is for apps that want to see an audio input
329 just to feel comfortable, as well as to test if
330 it can do stereo or sth. There is actually no guarantee
331 that the actual audio input cannot change behind the app's
332 back, but most applications should not mind that either.
334 Hopefully, mplayer people will work with us on this (this
335 whole mess is to support mplayer pvr://), or Hans will come
336 up with a more standard way to say "we have inputs but we
337 don 't want you to change them independent of video" which
340 struct v4l2_audio *vin = arg;
342 if (vin->index > 0) break;
343 strncpy(vin->name, "PVRUSB2 Audio",14);
344 vin->capability = V4L2_AUDCAP_STEREO;
352 /* pkt: FIXME: see above comment (VIDIOC_ENUMAUDIO) */
353 struct v4l2_audio *vin = arg;
354 memset(vin,0,sizeof(*vin));
356 strncpy(vin->name, "PVRUSB2 Audio",14);
357 vin->capability = V4L2_AUDCAP_STEREO;
369 struct v4l2_tuner *vt = (struct v4l2_tuner *)arg;
371 if (vt->index != 0) break; /* Only answer for the 1st tuner */
373 pvr2_hdw_execute_tuner_poll(hdw);
374 ret = pvr2_hdw_get_tuner_status(hdw,vt);
380 struct v4l2_tuner *vt=(struct v4l2_tuner *)arg;
385 ret = pvr2_ctrl_set_value(
386 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_AUDIOMODE),
391 case VIDIOC_S_FREQUENCY:
393 const struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
395 struct v4l2_tuner vt;
397 struct pvr2_ctrl *ctrlp;
398 ret = pvr2_hdw_get_tuner_status(hdw,&vt);
400 ctrlp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
401 ret = pvr2_ctrl_get_value(ctrlp,&cur_input);
403 if (vf->type == V4L2_TUNER_RADIO) {
404 if (cur_input != PVR2_CVAL_INPUT_RADIO) {
405 pvr2_ctrl_set_value(ctrlp,
406 PVR2_CVAL_INPUT_RADIO);
409 if (cur_input == PVR2_CVAL_INPUT_RADIO) {
410 pvr2_ctrl_set_value(ctrlp,
415 if (vt.capability & V4L2_TUNER_CAP_LOW) {
420 ret = pvr2_ctrl_set_value(
421 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),fv);
425 case VIDIOC_G_FREQUENCY:
427 struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
430 struct v4l2_tuner vt;
431 ret = pvr2_hdw_get_tuner_status(hdw,&vt);
433 ret = pvr2_ctrl_get_value(
434 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),
438 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
440 if (cur_input == PVR2_CVAL_INPUT_RADIO) {
441 vf->type = V4L2_TUNER_RADIO;
443 vf->type = V4L2_TUNER_ANALOG_TV;
445 if (vt.capability & V4L2_TUNER_CAP_LOW) {
446 val = (val * 2) / 125;
454 case VIDIOC_ENUM_FMT:
456 struct v4l2_fmtdesc *fd = (struct v4l2_fmtdesc *)arg;
458 /* Only one format is supported : mpeg.*/
462 memcpy(fd, pvr_fmtdesc, sizeof(struct v4l2_fmtdesc));
469 struct v4l2_format *vf = (struct v4l2_format *)arg;
472 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
473 memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
474 sizeof(struct v4l2_format));
477 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES),
479 vf->fmt.pix.width = val;
482 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES),
484 vf->fmt.pix.height = val;
487 case V4L2_BUF_TYPE_VBI_CAPTURE:
488 // ????? Still need to figure out to do VBI correctly
501 struct v4l2_format *vf = (struct v4l2_format *)arg;
505 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
507 struct pvr2_ctrl *hcp,*vcp;
508 int h = vf->fmt.pix.height;
509 int w = vf->fmt.pix.width;
510 hcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES);
511 vcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES);
513 lmin = pvr2_ctrl_get_min(hcp);
514 lmax = pvr2_ctrl_get_max(hcp);
515 ldef = pvr2_ctrl_get_def(hcp);
518 } else if (w < lmin) {
520 } else if (w > lmax) {
523 lmin = pvr2_ctrl_get_min(vcp);
524 lmax = pvr2_ctrl_get_max(vcp);
525 ldef = pvr2_ctrl_get_def(vcp);
528 } else if (h < lmin) {
530 } else if (h > lmax) {
534 memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
535 sizeof(struct v4l2_format));
536 vf->fmt.pix.width = w;
537 vf->fmt.pix.height = h;
539 if (cmd == VIDIOC_S_FMT) {
540 pvr2_ctrl_set_value(hcp,vf->fmt.pix.width);
541 pvr2_ctrl_set_value(vcp,vf->fmt.pix.height);
544 case V4L2_BUF_TYPE_VBI_CAPTURE:
545 // ????? Still need to figure out to do VBI correctly
555 case VIDIOC_STREAMON:
557 if (!fh->dev_info->stream) {
558 /* No stream defined for this node. This means
559 that we're not currently allowed to stream from
564 ret = pvr2_hdw_set_stream_type(hdw,dev_info->config);
565 if (ret < 0) return ret;
566 ret = pvr2_hdw_set_streaming(hdw,!0);
570 case VIDIOC_STREAMOFF:
572 if (!fh->dev_info->stream) {
573 /* No stream defined for this node. This means
574 that we're not currently allowed to stream from
579 ret = pvr2_hdw_set_streaming(hdw,0);
583 case VIDIOC_QUERYCTRL:
585 struct pvr2_ctrl *cptr;
586 struct v4l2_queryctrl *vc = (struct v4l2_queryctrl *)arg;
588 if (vc->id & V4L2_CTRL_FLAG_NEXT_CTRL) {
589 cptr = pvr2_hdw_get_ctrl_nextv4l(
590 hdw,(vc->id & ~V4L2_CTRL_FLAG_NEXT_CTRL));
591 if (cptr) vc->id = pvr2_ctrl_get_v4lid(cptr);
593 cptr = pvr2_hdw_get_ctrl_v4l(hdw,vc->id);
596 pvr2_trace(PVR2_TRACE_V4LIOCTL,
597 "QUERYCTRL id=0x%x not implemented here",
603 pvr2_trace(PVR2_TRACE_V4LIOCTL,
604 "QUERYCTRL id=0x%x mapping name=%s (%s)",
605 vc->id,pvr2_ctrl_get_name(cptr),
606 pvr2_ctrl_get_desc(cptr));
607 strlcpy(vc->name,pvr2_ctrl_get_desc(cptr),sizeof(vc->name));
608 vc->flags = pvr2_ctrl_get_v4lflags(cptr);
609 vc->default_value = pvr2_ctrl_get_def(cptr);
610 switch (pvr2_ctrl_get_type(cptr)) {
612 vc->type = V4L2_CTRL_TYPE_MENU;
614 vc->maximum = pvr2_ctrl_get_cnt(cptr) - 1;
618 vc->type = V4L2_CTRL_TYPE_BOOLEAN;
624 vc->type = V4L2_CTRL_TYPE_INTEGER;
625 vc->minimum = pvr2_ctrl_get_min(cptr);
626 vc->maximum = pvr2_ctrl_get_max(cptr);
630 pvr2_trace(PVR2_TRACE_V4LIOCTL,
631 "QUERYCTRL id=0x%x name=%s not mappable",
632 vc->id,pvr2_ctrl_get_name(cptr));
639 case VIDIOC_QUERYMENU:
641 struct v4l2_querymenu *vm = (struct v4l2_querymenu *)arg;
642 unsigned int cnt = 0;
643 ret = pvr2_ctrl_get_valname(pvr2_hdw_get_ctrl_v4l(hdw,vm->id),
645 vm->name,sizeof(vm->name)-1,
653 struct v4l2_control *vc = (struct v4l2_control *)arg;
655 ret = pvr2_ctrl_get_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
663 struct v4l2_control *vc = (struct v4l2_control *)arg;
664 ret = pvr2_ctrl_set_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
669 case VIDIOC_G_EXT_CTRLS:
671 struct v4l2_ext_controls *ctls =
672 (struct v4l2_ext_controls *)arg;
673 struct v4l2_ext_control *ctrl;
677 for (idx = 0; idx < ctls->count; idx++) {
678 ctrl = ctls->controls + idx;
679 ret = pvr2_ctrl_get_value(
680 pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),&val);
682 ctls->error_idx = idx;
685 /* Ensure that if read as a 64 bit value, the user
686 will still get a hopefully sane value */
693 case VIDIOC_S_EXT_CTRLS:
695 struct v4l2_ext_controls *ctls =
696 (struct v4l2_ext_controls *)arg;
697 struct v4l2_ext_control *ctrl;
700 for (idx = 0; idx < ctls->count; idx++) {
701 ctrl = ctls->controls + idx;
702 ret = pvr2_ctrl_set_value(
703 pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),
706 ctls->error_idx = idx;
713 case VIDIOC_TRY_EXT_CTRLS:
715 struct v4l2_ext_controls *ctls =
716 (struct v4l2_ext_controls *)arg;
717 struct v4l2_ext_control *ctrl;
718 struct pvr2_ctrl *pctl;
720 /* For the moment just validate that the requested control
723 for (idx = 0; idx < ctls->count; idx++) {
724 ctrl = ctls->controls + idx;
725 pctl = pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id);
728 ctls->error_idx = idx;
735 case VIDIOC_LOG_STATUS:
737 pvr2_hdw_trigger_module_log(hdw);
741 #ifdef CONFIG_VIDEO_ADV_DEBUG
742 case VIDIOC_DBG_S_REGISTER:
743 case VIDIOC_DBG_G_REGISTER:
746 struct v4l2_register *req = (struct v4l2_register *)arg;
747 if (cmd == VIDIOC_DBG_S_REGISTER) val = req->val;
748 ret = pvr2_hdw_register_access(
749 hdw,req->match_type,req->match_chip,req->reg,
750 cmd == VIDIOC_DBG_S_REGISTER,&val);
751 if (cmd == VIDIOC_DBG_G_REGISTER) req->val = val;
757 ret = v4l_compat_translate_ioctl(inode,file,cmd,
758 arg,pvr2_v4l2_do_ioctl);
761 pvr2_hdw_commit_ctl(hdw);
764 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
765 pvr2_trace(PVR2_TRACE_V4LIOCTL,
766 "pvr2_v4l2_do_ioctl failure, ret=%d",ret);
768 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
769 pvr2_trace(PVR2_TRACE_V4LIOCTL,
770 "pvr2_v4l2_do_ioctl failure, ret=%d"
771 " command was:",ret);
772 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),
777 pvr2_trace(PVR2_TRACE_V4LIOCTL,
778 "pvr2_v4l2_do_ioctl complete, ret=%d (0x%x)",
785 static void pvr2_v4l2_dev_destroy(struct pvr2_v4l2_dev *dip)
787 int minor_id = dip->devbase.minor;
788 struct pvr2_hdw *hdw = dip->v4lp->channel.mc_head->hdw;
789 enum pvr2_config cfg = dip->config;
790 int v4l_type = dip->v4l_type;
792 pvr2_hdw_v4l_store_minor_number(hdw,dip->minor_type,-1);
798 /* Actual deallocation happens later when all internal references
800 video_unregister_device(&dip->devbase);
802 printk(KERN_INFO "pvrusb2: unregistered device %s%u [%s]\n",
803 get_v4l_name(v4l_type),minor_id & 0x1f,
804 pvr2_config_get_name(cfg));
809 static void pvr2_v4l2_destroy_no_lock(struct pvr2_v4l2 *vp)
812 pvr2_v4l2_dev_destroy(vp->dev_video);
813 vp->dev_video = NULL;
816 pvr2_v4l2_dev_destroy(vp->dev_radio);
817 vp->dev_radio = NULL;
820 pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr2_v4l2 id=%p",vp);
821 pvr2_channel_done(&vp->channel);
826 static void pvr2_video_device_release(struct video_device *vdev)
828 struct pvr2_v4l2_dev *dev;
829 dev = container_of(vdev,struct pvr2_v4l2_dev,devbase);
834 static void pvr2_v4l2_internal_check(struct pvr2_channel *chp)
836 struct pvr2_v4l2 *vp;
837 vp = container_of(chp,struct pvr2_v4l2,channel);
838 if (!vp->channel.mc_head->disconnect_flag) return;
839 if (vp->vfirst) return;
840 pvr2_v4l2_destroy_no_lock(vp);
844 static int pvr2_v4l2_ioctl(struct inode *inode, struct file *file,
845 unsigned int cmd, unsigned long arg)
848 /* Temporary hack : use ivtv api until a v4l2 one is available. */
849 #define IVTV_IOC_G_CODEC 0xFFEE7703
850 #define IVTV_IOC_S_CODEC 0xFFEE7704
851 if (cmd == IVTV_IOC_G_CODEC || cmd == IVTV_IOC_S_CODEC) return 0;
852 return video_usercopy(inode, file, cmd, arg, pvr2_v4l2_do_ioctl);
856 static int pvr2_v4l2_release(struct inode *inode, struct file *file)
858 struct pvr2_v4l2_fh *fhp = file->private_data;
859 struct pvr2_v4l2 *vp = fhp->vhead;
860 struct pvr2_context *mp = fhp->vhead->channel.mc_head;
861 struct pvr2_hdw *hdw = fhp->channel.mc_head->hdw;
863 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_release");
866 struct pvr2_stream *sp;
867 pvr2_hdw_set_streaming(hdw,0);
868 sp = pvr2_ioread_get_stream(fhp->rhp);
869 if (sp) pvr2_stream_set_callback(sp,NULL,NULL);
870 pvr2_ioread_destroy(fhp->rhp);
874 v4l2_prio_close(&vp->prio, &fhp->prio);
875 file->private_data = NULL;
877 pvr2_context_enter(mp); do {
878 /* Restore the previous input selection, if it makes sense
880 if (fhp->dev_info->v4l_type == VFL_TYPE_RADIO) {
881 struct pvr2_ctrl *cp;
883 cp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
884 pvr2_ctrl_get_value(cp,&pval);
885 /* Only restore if we're still selecting the radio */
886 if (pval == PVR2_CVAL_INPUT_RADIO) {
887 pvr2_ctrl_set_value(cp,fhp->prev_input_val);
888 pvr2_hdw_commit_ctl(hdw);
893 fhp->vnext->vprev = fhp->vprev;
895 vp->vlast = fhp->vprev;
898 fhp->vprev->vnext = fhp->vnext;
900 vp->vfirst = fhp->vnext;
905 pvr2_channel_done(&fhp->channel);
906 pvr2_trace(PVR2_TRACE_STRUCT,
907 "Destroying pvr_v4l2_fh id=%p",fhp);
909 if (vp->channel.mc_head->disconnect_flag && !vp->vfirst) {
910 pvr2_v4l2_destroy_no_lock(vp);
912 } while (0); pvr2_context_exit(mp);
917 static int pvr2_v4l2_open(struct inode *inode, struct file *file)
919 struct pvr2_v4l2_dev *dip; /* Our own context pointer */
920 struct pvr2_v4l2_fh *fhp;
921 struct pvr2_v4l2 *vp;
922 struct pvr2_hdw *hdw;
924 dip = container_of(video_devdata(file),struct pvr2_v4l2_dev,devbase);
927 hdw = vp->channel.hdw;
929 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_open");
931 if (!pvr2_hdw_dev_ok(hdw)) {
932 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,
933 "pvr2_v4l2_open: hardware not ready");
937 fhp = kzalloc(sizeof(*fhp),GFP_KERNEL);
942 init_waitqueue_head(&fhp->wait_data);
945 pvr2_context_enter(vp->channel.mc_head); do {
946 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr_v4l2_fh id=%p",fhp);
947 pvr2_channel_init(&fhp->channel,vp->channel.mc_head);
950 fhp->vprev = vp->vlast;
952 vp->vlast->vnext = fhp;
959 /* Opening the /dev/radioX device implies a mode switch.
960 So execute that here. Note that you can get the
961 IDENTICAL effect merely by opening the normal video
962 device and setting the input appropriately. */
963 if (dip->v4l_type == VFL_TYPE_RADIO) {
964 struct pvr2_ctrl *cp;
965 cp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
966 pvr2_ctrl_get_value(cp,&fhp->prev_input_val);
967 pvr2_ctrl_set_value(cp,PVR2_CVAL_INPUT_RADIO);
968 pvr2_hdw_commit_ctl(hdw);
970 } while (0); pvr2_context_exit(vp->channel.mc_head);
973 file->private_data = fhp;
974 v4l2_prio_open(&vp->prio,&fhp->prio);
976 fhp->fw_mode_flag = pvr2_hdw_cpufw_get_enabled(hdw);
982 static void pvr2_v4l2_notify(struct pvr2_v4l2_fh *fhp)
984 wake_up(&fhp->wait_data);
987 static int pvr2_v4l2_iosetup(struct pvr2_v4l2_fh *fh)
990 struct pvr2_stream *sp;
991 struct pvr2_hdw *hdw;
992 if (fh->rhp) return 0;
994 if (!fh->dev_info->stream) {
995 /* No stream defined for this node. This means that we're
996 not currently allowed to stream from this node. */
1000 /* First read() attempt. Try to claim the stream and start
1002 if ((ret = pvr2_channel_claim_stream(&fh->channel,
1003 fh->dev_info->stream)) != 0) {
1004 /* Someone else must already have it */
1008 fh->rhp = pvr2_channel_create_mpeg_stream(fh->dev_info->stream);
1010 pvr2_channel_claim_stream(&fh->channel,NULL);
1014 hdw = fh->channel.mc_head->hdw;
1015 sp = fh->dev_info->stream->stream;
1016 pvr2_stream_set_callback(sp,(pvr2_stream_callback)pvr2_v4l2_notify,fh);
1017 pvr2_hdw_set_stream_type(hdw,fh->dev_info->config);
1018 pvr2_hdw_set_streaming(hdw,!0);
1019 ret = pvr2_ioread_set_enabled(fh->rhp,!0);
1025 static ssize_t pvr2_v4l2_read(struct file *file,
1026 char __user *buff, size_t count, loff_t *ppos)
1028 struct pvr2_v4l2_fh *fh = file->private_data;
1031 if (fh->fw_mode_flag) {
1032 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
1036 unsigned int offs = *ppos;
1038 tbuf = kmalloc(PAGE_SIZE,GFP_KERNEL);
1039 if (!tbuf) return -ENOMEM;
1043 if (c1 > PAGE_SIZE) c1 = PAGE_SIZE;
1044 c2 = pvr2_hdw_cpufw_get(hdw,offs,tbuf,c1);
1050 if (copy_to_user(buff,tbuf,c2)) {
1065 ret = pvr2_v4l2_iosetup(fh);
1072 ret = pvr2_ioread_read(fh->rhp,buff,count);
1073 if (ret >= 0) break;
1074 if (ret != -EAGAIN) break;
1075 if (file->f_flags & O_NONBLOCK) break;
1076 /* Doing blocking I/O. Wait here. */
1077 ret = wait_event_interruptible(
1079 pvr2_ioread_avail(fh->rhp) >= 0);
1087 static unsigned int pvr2_v4l2_poll(struct file *file, poll_table *wait)
1089 unsigned int mask = 0;
1090 struct pvr2_v4l2_fh *fh = file->private_data;
1093 if (fh->fw_mode_flag) {
1094 mask |= POLLIN | POLLRDNORM;
1099 ret = pvr2_v4l2_iosetup(fh);
1100 if (ret) return POLLERR;
1103 poll_wait(file,&fh->wait_data,wait);
1105 if (pvr2_ioread_avail(fh->rhp) >= 0) {
1106 mask |= POLLIN | POLLRDNORM;
1113 static const struct file_operations vdev_fops = {
1114 .owner = THIS_MODULE,
1115 .open = pvr2_v4l2_open,
1116 .release = pvr2_v4l2_release,
1117 .read = pvr2_v4l2_read,
1118 .ioctl = pvr2_v4l2_ioctl,
1119 .llseek = no_llseek,
1120 .poll = pvr2_v4l2_poll,
1124 #define VID_HARDWARE_PVRUSB2 38 /* FIXME : need a good value */
1126 static struct video_device vdev_template = {
1127 .owner = THIS_MODULE,
1128 .type = VID_TYPE_CAPTURE | VID_TYPE_TUNER,
1129 .type2 = (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VBI_CAPTURE
1130 | V4L2_CAP_TUNER | V4L2_CAP_AUDIO
1131 | V4L2_CAP_READWRITE),
1132 .hardware = VID_HARDWARE_PVRUSB2,
1137 static void pvr2_v4l2_dev_init(struct pvr2_v4l2_dev *dip,
1138 struct pvr2_v4l2 *vp,
1147 dip->v4l_type = v4l_type;
1149 case VFL_TYPE_GRABBER:
1150 dip->stream = &vp->channel.mc_head->video_stream;
1151 dip->config = pvr2_config_mpeg;
1152 dip->minor_type = pvr2_v4l_type_video;
1155 err("Failed to set up pvrusb2 v4l video dev"
1156 " due to missing stream instance");
1161 dip->config = pvr2_config_vbi;
1162 dip->minor_type = pvr2_v4l_type_vbi;
1165 case VFL_TYPE_RADIO:
1166 dip->stream = &vp->channel.mc_head->video_stream;
1167 dip->config = pvr2_config_mpeg;
1168 dip->minor_type = pvr2_v4l_type_radio;
1172 /* Bail out (this should be impossible) */
1173 err("Failed to set up pvrusb2 v4l dev"
1174 " due to unrecognized config");
1178 memcpy(&dip->devbase,&vdev_template,sizeof(vdev_template));
1179 dip->devbase.release = pvr2_video_device_release;
1182 unit_number = pvr2_hdw_get_unit_number(vp->channel.mc_head->hdw);
1183 if (nr_ptr && (unit_number >= 0) && (unit_number < PVR_NUM)) {
1184 mindevnum = nr_ptr[unit_number];
1186 if ((video_register_device(&dip->devbase,
1187 dip->v4l_type, mindevnum) < 0) &&
1188 (video_register_device(&dip->devbase,
1189 dip->v4l_type, -1) < 0)) {
1190 err("Failed to register pvrusb2 v4l device");
1193 printk(KERN_INFO "pvrusb2: registered device %s%u [%s]\n",
1194 get_v4l_name(dip->v4l_type),dip->devbase.minor & 0x1f,
1195 pvr2_config_get_name(dip->config));
1197 pvr2_hdw_v4l_store_minor_number(vp->channel.mc_head->hdw,
1198 dip->minor_type,dip->devbase.minor);
1202 struct pvr2_v4l2 *pvr2_v4l2_create(struct pvr2_context *mnp)
1204 struct pvr2_v4l2 *vp;
1206 vp = kzalloc(sizeof(*vp),GFP_KERNEL);
1208 vp->dev_video = kzalloc(sizeof(*vp->dev_video),GFP_KERNEL);
1209 vp->dev_radio = kzalloc(sizeof(*vp->dev_radio),GFP_KERNEL);
1210 if (!(vp->dev_video && vp->dev_radio)) {
1211 kfree(vp->dev_video);
1212 kfree(vp->dev_radio);
1216 pvr2_channel_init(&vp->channel,mnp);
1217 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr2_v4l2 id=%p",vp);
1219 vp->channel.check_func = pvr2_v4l2_internal_check;
1221 /* register streams */
1222 pvr2_v4l2_dev_init(vp->dev_video,vp,VFL_TYPE_GRABBER);
1223 pvr2_v4l2_dev_init(vp->dev_radio,vp,VFL_TYPE_RADIO);
1229 Stuff for Emacs to see, in order to encourage consistent editing style:
1230 *** Local Variables: ***
1232 *** fill-column: 75 ***
1233 *** tab-width: 8 ***
1234 *** c-basic-offset: 8 ***