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 /* Map contiguous ordinal value to input id */
71 unsigned char *input_map;
72 unsigned int input_cnt;
74 /* streams - Note that these must be separately, individually,
75 * allocated pointers. This is because the v4l core is going to
76 * manage their deletion - separately, individually... */
77 struct pvr2_v4l2_dev *dev_video;
78 struct pvr2_v4l2_dev *dev_radio;
81 static int video_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
82 module_param_array(video_nr, int, NULL, 0444);
83 MODULE_PARM_DESC(video_nr, "Offset for device's video dev minor");
84 static int radio_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
85 module_param_array(radio_nr, int, NULL, 0444);
86 MODULE_PARM_DESC(radio_nr, "Offset for device's radio dev minor");
87 static int vbi_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
88 module_param_array(vbi_nr, int, NULL, 0444);
89 MODULE_PARM_DESC(vbi_nr, "Offset for device's vbi dev minor");
91 static struct v4l2_capability pvr_capability ={
93 .card = "Hauppauge WinTV pvr-usb2",
95 .version = KERNEL_VERSION(0,8,0),
96 .capabilities = (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VBI_CAPTURE |
97 V4L2_CAP_TUNER | V4L2_CAP_AUDIO | V4L2_CAP_RADIO |
102 static struct v4l2_fmtdesc pvr_fmtdesc [] = {
105 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
106 .flags = V4L2_FMT_FLAG_COMPRESSED,
107 .description = "MPEG1/2",
108 // This should really be V4L2_PIX_FMT_MPEG, but xawtv
109 // breaks when I do that.
110 .pixelformat = 0, // V4L2_PIX_FMT_MPEG,
111 .reserved = { 0, 0, 0, 0 }
115 #define PVR_FORMAT_PIX 0
116 #define PVR_FORMAT_VBI 1
118 static struct v4l2_format pvr_format [] = {
120 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
125 // This should really be V4L2_PIX_FMT_MPEG,
126 // but xawtv breaks when I do that.
127 .pixelformat = 0, // V4L2_PIX_FMT_MPEG,
128 .field = V4L2_FIELD_INTERLACED,
129 .bytesperline = 0, // doesn't make sense
131 //FIXME : Don't know what to put here...
132 .sizeimage = (32*1024),
133 .colorspace = 0, // doesn't make sense here
139 .type = V4L2_BUF_TYPE_VBI_CAPTURE,
142 .sampling_rate = 27000000,
144 .samples_per_line = 1443,
145 .sample_format = V4L2_PIX_FMT_GREY,
156 static const char *get_v4l_name(int v4l_type)
159 case VFL_TYPE_GRABBER: return "video";
160 case VFL_TYPE_RADIO: return "radio";
161 case VFL_TYPE_VBI: return "vbi";
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));
210 strlcpy(cap->bus_info,pvr2_hdw_get_bus_info(hdw),
211 sizeof(cap->bus_info));
212 strlcpy(cap->card,pvr2_hdw_get_desc(hdw),sizeof(cap->card));
218 case VIDIOC_G_PRIORITY:
220 enum v4l2_priority *p = arg;
222 *p = v4l2_prio_max(&vp->prio);
227 case VIDIOC_S_PRIORITY:
229 enum v4l2_priority *prio = arg;
231 ret = v4l2_prio_change(&vp->prio, &fh->prio, *prio);
237 struct v4l2_standard *vs = (struct v4l2_standard *)arg;
239 ret = pvr2_hdw_get_stdenum_value(hdw,vs,idx+1);
246 ret = pvr2_ctrl_get_value(
247 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR),&val);
248 *(v4l2_std_id *)arg = val;
254 ret = pvr2_ctrl_set_value(
255 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR),
256 *(v4l2_std_id *)arg);
260 case VIDIOC_ENUMINPUT:
262 struct pvr2_ctrl *cptr;
263 struct v4l2_input *vi = (struct v4l2_input *)arg;
264 struct v4l2_input tmp;
268 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
270 memset(&tmp,0,sizeof(tmp));
271 tmp.index = vi->index;
273 if ((vi->index < 0) || (vi->index >= vp->input_cnt)) {
277 val = vp->input_map[vi->index];
279 case PVR2_CVAL_INPUT_TV:
280 case PVR2_CVAL_INPUT_DTV:
281 case PVR2_CVAL_INPUT_RADIO:
282 tmp.type = V4L2_INPUT_TYPE_TUNER;
284 case PVR2_CVAL_INPUT_SVIDEO:
285 case PVR2_CVAL_INPUT_COMPOSITE:
286 tmp.type = V4L2_INPUT_TYPE_CAMERA;
295 pvr2_ctrl_get_valname(cptr,val,
296 tmp.name,sizeof(tmp.name)-1,&cnt);
299 /* Don't bother with audioset, since this driver currently
300 always switches the audio whenever the video is
303 /* Handling std is a tougher problem. It doesn't make
304 sense in cases where a device might be multi-standard.
305 We could just copy out the current value for the
306 standard, but it can change over time. For now just
309 memcpy(vi, &tmp, sizeof(tmp));
318 struct pvr2_ctrl *cptr;
319 struct v4l2_input *vi = (struct v4l2_input *)arg;
321 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
323 ret = pvr2_ctrl_get_value(cptr,&val);
325 for (idx = 0; idx < vp->input_cnt; idx++) {
326 if (vp->input_map[idx] == val) {
336 struct v4l2_input *vi = (struct v4l2_input *)arg;
337 if ((vi->index < 0) || (vi->index >= vp->input_cnt)) {
341 ret = pvr2_ctrl_set_value(
342 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
343 vp->input_map[vi->index]);
347 case VIDIOC_ENUMAUDIO:
349 /* pkt: FIXME: We are returning one "fake" input here
350 which could very well be called "whatever_we_like".
351 This is for apps that want to see an audio input
352 just to feel comfortable, as well as to test if
353 it can do stereo or sth. There is actually no guarantee
354 that the actual audio input cannot change behind the app's
355 back, but most applications should not mind that either.
357 Hopefully, mplayer people will work with us on this (this
358 whole mess is to support mplayer pvr://), or Hans will come
359 up with a more standard way to say "we have inputs but we
360 don 't want you to change them independent of video" which
363 struct v4l2_audio *vin = arg;
365 if (vin->index > 0) break;
366 strncpy(vin->name, "PVRUSB2 Audio",14);
367 vin->capability = V4L2_AUDCAP_STEREO;
375 /* pkt: FIXME: see above comment (VIDIOC_ENUMAUDIO) */
376 struct v4l2_audio *vin = arg;
377 memset(vin,0,sizeof(*vin));
379 strncpy(vin->name, "PVRUSB2 Audio",14);
380 vin->capability = V4L2_AUDCAP_STEREO;
392 struct v4l2_tuner *vt = (struct v4l2_tuner *)arg;
394 if (vt->index != 0) break; /* Only answer for the 1st tuner */
396 pvr2_hdw_execute_tuner_poll(hdw);
397 ret = pvr2_hdw_get_tuner_status(hdw,vt);
403 struct v4l2_tuner *vt=(struct v4l2_tuner *)arg;
408 ret = pvr2_ctrl_set_value(
409 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_AUDIOMODE),
414 case VIDIOC_S_FREQUENCY:
416 const struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
418 struct v4l2_tuner vt;
420 struct pvr2_ctrl *ctrlp;
421 ret = pvr2_hdw_get_tuner_status(hdw,&vt);
423 ctrlp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
424 ret = pvr2_ctrl_get_value(ctrlp,&cur_input);
426 if (vf->type == V4L2_TUNER_RADIO) {
427 if (cur_input != PVR2_CVAL_INPUT_RADIO) {
428 pvr2_ctrl_set_value(ctrlp,
429 PVR2_CVAL_INPUT_RADIO);
432 if (cur_input == PVR2_CVAL_INPUT_RADIO) {
433 pvr2_ctrl_set_value(ctrlp,
438 if (vt.capability & V4L2_TUNER_CAP_LOW) {
443 ret = pvr2_ctrl_set_value(
444 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),fv);
448 case VIDIOC_G_FREQUENCY:
450 struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
453 struct v4l2_tuner vt;
454 ret = pvr2_hdw_get_tuner_status(hdw,&vt);
456 ret = pvr2_ctrl_get_value(
457 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),
461 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
463 if (cur_input == PVR2_CVAL_INPUT_RADIO) {
464 vf->type = V4L2_TUNER_RADIO;
466 vf->type = V4L2_TUNER_ANALOG_TV;
468 if (vt.capability & V4L2_TUNER_CAP_LOW) {
469 val = (val * 2) / 125;
477 case VIDIOC_ENUM_FMT:
479 struct v4l2_fmtdesc *fd = (struct v4l2_fmtdesc *)arg;
481 /* Only one format is supported : mpeg.*/
485 memcpy(fd, pvr_fmtdesc, sizeof(struct v4l2_fmtdesc));
492 struct v4l2_format *vf = (struct v4l2_format *)arg;
495 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
496 memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
497 sizeof(struct v4l2_format));
500 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES),
502 vf->fmt.pix.width = val;
505 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES),
507 vf->fmt.pix.height = val;
510 case V4L2_BUF_TYPE_VBI_CAPTURE:
511 // ????? Still need to figure out to do VBI correctly
524 struct v4l2_format *vf = (struct v4l2_format *)arg;
528 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
530 struct pvr2_ctrl *hcp,*vcp;
531 int h = vf->fmt.pix.height;
532 int w = vf->fmt.pix.width;
533 hcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES);
534 vcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES);
536 lmin = pvr2_ctrl_get_min(hcp);
537 lmax = pvr2_ctrl_get_max(hcp);
538 ldef = pvr2_ctrl_get_def(hcp);
541 } else if (w < lmin) {
543 } else if (w > lmax) {
546 lmin = pvr2_ctrl_get_min(vcp);
547 lmax = pvr2_ctrl_get_max(vcp);
548 ldef = pvr2_ctrl_get_def(vcp);
551 } else if (h < lmin) {
553 } else if (h > lmax) {
557 memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
558 sizeof(struct v4l2_format));
559 vf->fmt.pix.width = w;
560 vf->fmt.pix.height = h;
562 if (cmd == VIDIOC_S_FMT) {
563 pvr2_ctrl_set_value(hcp,vf->fmt.pix.width);
564 pvr2_ctrl_set_value(vcp,vf->fmt.pix.height);
567 case V4L2_BUF_TYPE_VBI_CAPTURE:
568 // ????? Still need to figure out to do VBI correctly
578 case VIDIOC_STREAMON:
580 if (!fh->dev_info->stream) {
581 /* No stream defined for this node. This means
582 that we're not currently allowed to stream from
587 ret = pvr2_hdw_set_stream_type(hdw,dev_info->config);
588 if (ret < 0) return ret;
589 ret = pvr2_hdw_set_streaming(hdw,!0);
593 case VIDIOC_STREAMOFF:
595 if (!fh->dev_info->stream) {
596 /* No stream defined for this node. This means
597 that we're not currently allowed to stream from
602 ret = pvr2_hdw_set_streaming(hdw,0);
606 case VIDIOC_QUERYCTRL:
608 struct pvr2_ctrl *cptr;
609 struct v4l2_queryctrl *vc = (struct v4l2_queryctrl *)arg;
611 if (vc->id & V4L2_CTRL_FLAG_NEXT_CTRL) {
612 cptr = pvr2_hdw_get_ctrl_nextv4l(
613 hdw,(vc->id & ~V4L2_CTRL_FLAG_NEXT_CTRL));
614 if (cptr) vc->id = pvr2_ctrl_get_v4lid(cptr);
616 cptr = pvr2_hdw_get_ctrl_v4l(hdw,vc->id);
619 pvr2_trace(PVR2_TRACE_V4LIOCTL,
620 "QUERYCTRL id=0x%x not implemented here",
626 pvr2_trace(PVR2_TRACE_V4LIOCTL,
627 "QUERYCTRL id=0x%x mapping name=%s (%s)",
628 vc->id,pvr2_ctrl_get_name(cptr),
629 pvr2_ctrl_get_desc(cptr));
630 strlcpy(vc->name,pvr2_ctrl_get_desc(cptr),sizeof(vc->name));
631 vc->flags = pvr2_ctrl_get_v4lflags(cptr);
632 vc->default_value = pvr2_ctrl_get_def(cptr);
633 switch (pvr2_ctrl_get_type(cptr)) {
635 vc->type = V4L2_CTRL_TYPE_MENU;
637 vc->maximum = pvr2_ctrl_get_cnt(cptr) - 1;
641 vc->type = V4L2_CTRL_TYPE_BOOLEAN;
647 vc->type = V4L2_CTRL_TYPE_INTEGER;
648 vc->minimum = pvr2_ctrl_get_min(cptr);
649 vc->maximum = pvr2_ctrl_get_max(cptr);
653 pvr2_trace(PVR2_TRACE_V4LIOCTL,
654 "QUERYCTRL id=0x%x name=%s not mappable",
655 vc->id,pvr2_ctrl_get_name(cptr));
662 case VIDIOC_QUERYMENU:
664 struct v4l2_querymenu *vm = (struct v4l2_querymenu *)arg;
665 unsigned int cnt = 0;
666 ret = pvr2_ctrl_get_valname(pvr2_hdw_get_ctrl_v4l(hdw,vm->id),
668 vm->name,sizeof(vm->name)-1,
676 struct v4l2_control *vc = (struct v4l2_control *)arg;
678 ret = pvr2_ctrl_get_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
686 struct v4l2_control *vc = (struct v4l2_control *)arg;
687 ret = pvr2_ctrl_set_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
692 case VIDIOC_G_EXT_CTRLS:
694 struct v4l2_ext_controls *ctls =
695 (struct v4l2_ext_controls *)arg;
696 struct v4l2_ext_control *ctrl;
700 for (idx = 0; idx < ctls->count; idx++) {
701 ctrl = ctls->controls + idx;
702 ret = pvr2_ctrl_get_value(
703 pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),&val);
705 ctls->error_idx = idx;
708 /* Ensure that if read as a 64 bit value, the user
709 will still get a hopefully sane value */
716 case VIDIOC_S_EXT_CTRLS:
718 struct v4l2_ext_controls *ctls =
719 (struct v4l2_ext_controls *)arg;
720 struct v4l2_ext_control *ctrl;
723 for (idx = 0; idx < ctls->count; idx++) {
724 ctrl = ctls->controls + idx;
725 ret = pvr2_ctrl_set_value(
726 pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),
729 ctls->error_idx = idx;
736 case VIDIOC_TRY_EXT_CTRLS:
738 struct v4l2_ext_controls *ctls =
739 (struct v4l2_ext_controls *)arg;
740 struct v4l2_ext_control *ctrl;
741 struct pvr2_ctrl *pctl;
743 /* For the moment just validate that the requested control
746 for (idx = 0; idx < ctls->count; idx++) {
747 ctrl = ctls->controls + idx;
748 pctl = pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id);
751 ctls->error_idx = idx;
758 case VIDIOC_LOG_STATUS:
760 pvr2_hdw_trigger_module_log(hdw);
764 #ifdef CONFIG_VIDEO_ADV_DEBUG
765 case VIDIOC_DBG_S_REGISTER:
766 case VIDIOC_DBG_G_REGISTER:
769 struct v4l2_register *req = (struct v4l2_register *)arg;
770 if (cmd == VIDIOC_DBG_S_REGISTER) val = req->val;
771 ret = pvr2_hdw_register_access(
772 hdw,req->match_type,req->match_chip,req->reg,
773 cmd == VIDIOC_DBG_S_REGISTER,&val);
774 if (cmd == VIDIOC_DBG_G_REGISTER) req->val = val;
780 ret = v4l_compat_translate_ioctl(inode,file,cmd,
781 arg,pvr2_v4l2_do_ioctl);
784 pvr2_hdw_commit_ctl(hdw);
787 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
788 pvr2_trace(PVR2_TRACE_V4LIOCTL,
789 "pvr2_v4l2_do_ioctl failure, ret=%d",ret);
791 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
792 pvr2_trace(PVR2_TRACE_V4LIOCTL,
793 "pvr2_v4l2_do_ioctl failure, ret=%d"
794 " command was:",ret);
795 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),
800 pvr2_trace(PVR2_TRACE_V4LIOCTL,
801 "pvr2_v4l2_do_ioctl complete, ret=%d (0x%x)",
808 static void pvr2_v4l2_dev_destroy(struct pvr2_v4l2_dev *dip)
810 int minor_id = dip->devbase.minor;
811 struct pvr2_hdw *hdw = dip->v4lp->channel.mc_head->hdw;
812 enum pvr2_config cfg = dip->config;
813 int v4l_type = dip->v4l_type;
815 pvr2_hdw_v4l_store_minor_number(hdw,dip->minor_type,-1);
821 /* Actual deallocation happens later when all internal references
823 video_unregister_device(&dip->devbase);
825 printk(KERN_INFO "pvrusb2: unregistered device %s%u [%s]\n",
826 get_v4l_name(v4l_type),minor_id & 0x1f,
827 pvr2_config_get_name(cfg));
832 static void pvr2_v4l2_destroy_no_lock(struct pvr2_v4l2 *vp)
835 pvr2_v4l2_dev_destroy(vp->dev_video);
836 vp->dev_video = NULL;
839 pvr2_v4l2_dev_destroy(vp->dev_radio);
840 vp->dev_radio = NULL;
843 kfree(vp->input_map);
844 vp->input_map = NULL;
847 pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr2_v4l2 id=%p",vp);
848 pvr2_channel_done(&vp->channel);
853 static void pvr2_video_device_release(struct video_device *vdev)
855 struct pvr2_v4l2_dev *dev;
856 dev = container_of(vdev,struct pvr2_v4l2_dev,devbase);
861 static void pvr2_v4l2_internal_check(struct pvr2_channel *chp)
863 struct pvr2_v4l2 *vp;
864 vp = container_of(chp,struct pvr2_v4l2,channel);
865 if (!vp->channel.mc_head->disconnect_flag) return;
866 if (vp->vfirst) return;
867 pvr2_v4l2_destroy_no_lock(vp);
871 static int pvr2_v4l2_ioctl(struct inode *inode, struct file *file,
872 unsigned int cmd, unsigned long arg)
875 /* Temporary hack : use ivtv api until a v4l2 one is available. */
876 #define IVTV_IOC_G_CODEC 0xFFEE7703
877 #define IVTV_IOC_S_CODEC 0xFFEE7704
878 if (cmd == IVTV_IOC_G_CODEC || cmd == IVTV_IOC_S_CODEC) return 0;
879 return video_usercopy(inode, file, cmd, arg, pvr2_v4l2_do_ioctl);
883 static int pvr2_v4l2_release(struct inode *inode, struct file *file)
885 struct pvr2_v4l2_fh *fhp = file->private_data;
886 struct pvr2_v4l2 *vp = fhp->vhead;
887 struct pvr2_context *mp = fhp->vhead->channel.mc_head;
888 struct pvr2_hdw *hdw = fhp->channel.mc_head->hdw;
890 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_release");
893 struct pvr2_stream *sp;
894 pvr2_hdw_set_streaming(hdw,0);
895 sp = pvr2_ioread_get_stream(fhp->rhp);
896 if (sp) pvr2_stream_set_callback(sp,NULL,NULL);
897 pvr2_ioread_destroy(fhp->rhp);
901 v4l2_prio_close(&vp->prio, &fhp->prio);
902 file->private_data = NULL;
904 pvr2_context_enter(mp); do {
905 /* Restore the previous input selection, if it makes sense
907 if (fhp->dev_info->v4l_type == VFL_TYPE_RADIO) {
908 struct pvr2_ctrl *cp;
910 cp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
911 pvr2_ctrl_get_value(cp,&pval);
912 /* Only restore if we're still selecting the radio */
913 if (pval == PVR2_CVAL_INPUT_RADIO) {
914 pvr2_ctrl_set_value(cp,fhp->prev_input_val);
915 pvr2_hdw_commit_ctl(hdw);
920 fhp->vnext->vprev = fhp->vprev;
922 vp->vlast = fhp->vprev;
925 fhp->vprev->vnext = fhp->vnext;
927 vp->vfirst = fhp->vnext;
932 pvr2_channel_done(&fhp->channel);
933 pvr2_trace(PVR2_TRACE_STRUCT,
934 "Destroying pvr_v4l2_fh id=%p",fhp);
936 if (vp->channel.mc_head->disconnect_flag && !vp->vfirst) {
937 pvr2_v4l2_destroy_no_lock(vp);
939 } while (0); pvr2_context_exit(mp);
944 static int pvr2_v4l2_open(struct inode *inode, struct file *file)
946 struct pvr2_v4l2_dev *dip; /* Our own context pointer */
947 struct pvr2_v4l2_fh *fhp;
948 struct pvr2_v4l2 *vp;
949 struct pvr2_hdw *hdw;
951 dip = container_of(video_devdata(file),struct pvr2_v4l2_dev,devbase);
954 hdw = vp->channel.hdw;
956 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_open");
958 if (!pvr2_hdw_dev_ok(hdw)) {
959 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,
960 "pvr2_v4l2_open: hardware not ready");
964 fhp = kzalloc(sizeof(*fhp),GFP_KERNEL);
969 init_waitqueue_head(&fhp->wait_data);
972 pvr2_context_enter(vp->channel.mc_head); do {
973 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr_v4l2_fh id=%p",fhp);
974 pvr2_channel_init(&fhp->channel,vp->channel.mc_head);
977 fhp->vprev = vp->vlast;
979 vp->vlast->vnext = fhp;
986 /* Opening the /dev/radioX device implies a mode switch.
987 So execute that here. Note that you can get the
988 IDENTICAL effect merely by opening the normal video
989 device and setting the input appropriately. */
990 if (dip->v4l_type == VFL_TYPE_RADIO) {
991 struct pvr2_ctrl *cp;
992 cp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
993 pvr2_ctrl_get_value(cp,&fhp->prev_input_val);
994 pvr2_ctrl_set_value(cp,PVR2_CVAL_INPUT_RADIO);
995 pvr2_hdw_commit_ctl(hdw);
997 } while (0); pvr2_context_exit(vp->channel.mc_head);
1000 file->private_data = fhp;
1001 v4l2_prio_open(&vp->prio,&fhp->prio);
1003 fhp->fw_mode_flag = pvr2_hdw_cpufw_get_enabled(hdw);
1009 static void pvr2_v4l2_notify(struct pvr2_v4l2_fh *fhp)
1011 wake_up(&fhp->wait_data);
1014 static int pvr2_v4l2_iosetup(struct pvr2_v4l2_fh *fh)
1017 struct pvr2_stream *sp;
1018 struct pvr2_hdw *hdw;
1019 if (fh->rhp) return 0;
1021 if (!fh->dev_info->stream) {
1022 /* No stream defined for this node. This means that we're
1023 not currently allowed to stream from this node. */
1027 /* First read() attempt. Try to claim the stream and start
1029 if ((ret = pvr2_channel_claim_stream(&fh->channel,
1030 fh->dev_info->stream)) != 0) {
1031 /* Someone else must already have it */
1035 fh->rhp = pvr2_channel_create_mpeg_stream(fh->dev_info->stream);
1037 pvr2_channel_claim_stream(&fh->channel,NULL);
1041 hdw = fh->channel.mc_head->hdw;
1042 sp = fh->dev_info->stream->stream;
1043 pvr2_stream_set_callback(sp,(pvr2_stream_callback)pvr2_v4l2_notify,fh);
1044 pvr2_hdw_set_stream_type(hdw,fh->dev_info->config);
1045 if ((ret = pvr2_hdw_set_streaming(hdw,!0)) < 0) return ret;
1046 return pvr2_ioread_set_enabled(fh->rhp,!0);
1050 static ssize_t pvr2_v4l2_read(struct file *file,
1051 char __user *buff, size_t count, loff_t *ppos)
1053 struct pvr2_v4l2_fh *fh = file->private_data;
1056 if (fh->fw_mode_flag) {
1057 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
1061 unsigned int offs = *ppos;
1063 tbuf = kmalloc(PAGE_SIZE,GFP_KERNEL);
1064 if (!tbuf) return -ENOMEM;
1068 if (c1 > PAGE_SIZE) c1 = PAGE_SIZE;
1069 c2 = pvr2_hdw_cpufw_get(hdw,offs,tbuf,c1);
1075 if (copy_to_user(buff,tbuf,c2)) {
1090 ret = pvr2_v4l2_iosetup(fh);
1097 ret = pvr2_ioread_read(fh->rhp,buff,count);
1098 if (ret >= 0) break;
1099 if (ret != -EAGAIN) break;
1100 if (file->f_flags & O_NONBLOCK) break;
1101 /* Doing blocking I/O. Wait here. */
1102 ret = wait_event_interruptible(
1104 pvr2_ioread_avail(fh->rhp) >= 0);
1112 static unsigned int pvr2_v4l2_poll(struct file *file, poll_table *wait)
1114 unsigned int mask = 0;
1115 struct pvr2_v4l2_fh *fh = file->private_data;
1118 if (fh->fw_mode_flag) {
1119 mask |= POLLIN | POLLRDNORM;
1124 ret = pvr2_v4l2_iosetup(fh);
1125 if (ret) return POLLERR;
1128 poll_wait(file,&fh->wait_data,wait);
1130 if (pvr2_ioread_avail(fh->rhp) >= 0) {
1131 mask |= POLLIN | POLLRDNORM;
1138 static const struct file_operations vdev_fops = {
1139 .owner = THIS_MODULE,
1140 .open = pvr2_v4l2_open,
1141 .release = pvr2_v4l2_release,
1142 .read = pvr2_v4l2_read,
1143 .ioctl = pvr2_v4l2_ioctl,
1144 .llseek = no_llseek,
1145 .poll = pvr2_v4l2_poll,
1149 static struct video_device vdev_template = {
1150 .owner = THIS_MODULE,
1151 .type = VID_TYPE_CAPTURE | VID_TYPE_TUNER,
1152 .type2 = (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VBI_CAPTURE
1153 | V4L2_CAP_TUNER | V4L2_CAP_AUDIO
1154 | V4L2_CAP_READWRITE),
1159 static void pvr2_v4l2_dev_init(struct pvr2_v4l2_dev *dip,
1160 struct pvr2_v4l2 *vp,
1169 dip->v4l_type = v4l_type;
1171 case VFL_TYPE_GRABBER:
1172 dip->stream = &vp->channel.mc_head->video_stream;
1173 dip->config = pvr2_config_mpeg;
1174 dip->minor_type = pvr2_v4l_type_video;
1177 err("Failed to set up pvrusb2 v4l video dev"
1178 " due to missing stream instance");
1183 dip->config = pvr2_config_vbi;
1184 dip->minor_type = pvr2_v4l_type_vbi;
1187 case VFL_TYPE_RADIO:
1188 dip->stream = &vp->channel.mc_head->video_stream;
1189 dip->config = pvr2_config_mpeg;
1190 dip->minor_type = pvr2_v4l_type_radio;
1194 /* Bail out (this should be impossible) */
1195 err("Failed to set up pvrusb2 v4l dev"
1196 " due to unrecognized config");
1200 memcpy(&dip->devbase,&vdev_template,sizeof(vdev_template));
1201 dip->devbase.release = pvr2_video_device_release;
1204 unit_number = pvr2_hdw_get_unit_number(vp->channel.mc_head->hdw);
1205 if (nr_ptr && (unit_number >= 0) && (unit_number < PVR_NUM)) {
1206 mindevnum = nr_ptr[unit_number];
1208 if ((video_register_device(&dip->devbase,
1209 dip->v4l_type, mindevnum) < 0) &&
1210 (video_register_device(&dip->devbase,
1211 dip->v4l_type, -1) < 0)) {
1212 err("Failed to register pvrusb2 v4l device");
1215 printk(KERN_INFO "pvrusb2: registered device %s%u [%s]\n",
1216 get_v4l_name(dip->v4l_type),dip->devbase.minor & 0x1f,
1217 pvr2_config_get_name(dip->config));
1219 pvr2_hdw_v4l_store_minor_number(vp->channel.mc_head->hdw,
1220 dip->minor_type,dip->devbase.minor);
1224 struct pvr2_v4l2 *pvr2_v4l2_create(struct pvr2_context *mnp)
1226 struct pvr2_v4l2 *vp;
1227 struct pvr2_hdw *hdw;
1228 unsigned int input_mask,input_cnt,idx;
1230 vp = kzalloc(sizeof(*vp),GFP_KERNEL);
1232 pvr2_channel_init(&vp->channel,mnp);
1233 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr2_v4l2 id=%p",vp);
1235 vp->channel.check_func = pvr2_v4l2_internal_check;
1237 hdw = vp->channel.mc_head->hdw;
1238 input_mask = pvr2_hdw_get_input_available(hdw);
1240 for (idx = 0; idx < (sizeof(input_mask) << 3); idx++) {
1241 if (input_mask & (1 << idx)) input_cnt++;
1243 vp->input_cnt = input_cnt;
1244 vp->input_map = kzalloc(input_cnt,GFP_KERNEL);
1245 if (!vp->input_map) goto fail;
1247 for (idx = 0; idx < (sizeof(input_mask) << 3); idx++) {
1248 if (!(input_mask & (1 << idx))) continue;
1249 vp->input_map[input_cnt++] = idx;
1252 /* register streams */
1253 vp->dev_video = kzalloc(sizeof(*vp->dev_video),GFP_KERNEL);
1254 if (!vp->dev_video) goto fail;
1255 pvr2_v4l2_dev_init(vp->dev_video,vp,VFL_TYPE_GRABBER);
1256 if (input_mask & (1 << PVR2_CVAL_INPUT_RADIO)) {
1257 vp->dev_radio = kzalloc(sizeof(*vp->dev_radio),GFP_KERNEL);
1258 if (!vp->dev_radio) goto fail;
1259 pvr2_v4l2_dev_init(vp->dev_radio,vp,VFL_TYPE_RADIO);
1264 pvr2_trace(PVR2_TRACE_STRUCT,"Failure creating pvr2_v4l2 id=%p",vp);
1265 pvr2_v4l2_destroy_no_lock(vp);
1270 Stuff for Emacs to see, in order to encourage consistent editing style:
1271 *** Local Variables: ***
1273 *** fill-column: 75 ***
1274 *** tab-width: 8 ***
1275 *** c-basic-offset: 8 ***