4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
5 * Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/kernel.h>
23 #include <linux/version.h>
24 #include "pvrusb2-context.h"
25 #include "pvrusb2-hdw.h"
27 #include "pvrusb2-debug.h"
28 #include "pvrusb2-v4l2.h"
29 #include "pvrusb2-ioread.h"
30 #include <linux/videodev2.h>
31 #include <media/v4l2-dev.h>
32 #include <media/v4l2-common.h>
33 #include <media/v4l2-ioctl.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;
60 /* Map contiguous ordinal value to input id */
61 unsigned char *input_map;
62 unsigned int input_cnt;
66 struct pvr2_channel channel;
67 struct pvr2_v4l2_fh *vfirst;
68 struct pvr2_v4l2_fh *vlast;
70 struct v4l2_prio_state prio;
72 /* streams - Note that these must be separately, individually,
73 * allocated pointers. This is because the v4l core is going to
74 * manage their deletion - separately, individually... */
75 struct pvr2_v4l2_dev *dev_video;
76 struct pvr2_v4l2_dev *dev_radio;
79 static int video_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
80 module_param_array(video_nr, int, NULL, 0444);
81 MODULE_PARM_DESC(video_nr, "Offset for device's video dev minor");
82 static int radio_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
83 module_param_array(radio_nr, int, NULL, 0444);
84 MODULE_PARM_DESC(radio_nr, "Offset for device's radio dev minor");
85 static int vbi_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
86 module_param_array(vbi_nr, int, NULL, 0444);
87 MODULE_PARM_DESC(vbi_nr, "Offset for device's vbi dev minor");
89 static struct v4l2_capability pvr_capability ={
91 .card = "Hauppauge WinTV pvr-usb2",
93 .version = KERNEL_VERSION(0,8,0),
94 .capabilities = (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VBI_CAPTURE |
95 V4L2_CAP_TUNER | V4L2_CAP_AUDIO | V4L2_CAP_RADIO |
100 static struct v4l2_fmtdesc pvr_fmtdesc [] = {
103 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
104 .flags = V4L2_FMT_FLAG_COMPRESSED,
105 .description = "MPEG1/2",
106 // This should really be V4L2_PIX_FMT_MPEG, but xawtv
107 // breaks when I do that.
108 .pixelformat = 0, // V4L2_PIX_FMT_MPEG,
109 .reserved = { 0, 0, 0, 0 }
113 #define PVR_FORMAT_PIX 0
114 #define PVR_FORMAT_VBI 1
116 static struct v4l2_format pvr_format [] = {
118 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
123 // This should really be V4L2_PIX_FMT_MPEG,
124 // but xawtv breaks when I do that.
125 .pixelformat = 0, // V4L2_PIX_FMT_MPEG,
126 .field = V4L2_FIELD_INTERLACED,
127 .bytesperline = 0, // doesn't make sense
129 //FIXME : Don't know what to put here...
130 .sizeimage = (32*1024),
131 .colorspace = 0, // doesn't make sense here
137 .type = V4L2_BUF_TYPE_VBI_CAPTURE,
140 .sampling_rate = 27000000,
142 .samples_per_line = 1443,
143 .sample_format = V4L2_PIX_FMT_GREY,
154 static const char *get_v4l_name(int v4l_type)
157 case VFL_TYPE_GRABBER: return "video";
158 case VFL_TYPE_RADIO: return "radio";
159 case VFL_TYPE_VBI: return "vbi";
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));
208 strlcpy(cap->bus_info,pvr2_hdw_get_bus_info(hdw),
209 sizeof(cap->bus_info));
210 strlcpy(cap->card,pvr2_hdw_get_desc(hdw),sizeof(cap->card));
216 case VIDIOC_G_PRIORITY:
218 enum v4l2_priority *p = arg;
220 *p = v4l2_prio_max(&vp->prio);
225 case VIDIOC_S_PRIORITY:
227 enum v4l2_priority *prio = arg;
229 ret = v4l2_prio_change(&vp->prio, &fh->prio, *prio);
235 struct v4l2_standard *vs = (struct v4l2_standard *)arg;
237 ret = pvr2_hdw_get_stdenum_value(hdw,vs,idx+1);
244 ret = pvr2_ctrl_get_value(
245 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR),&val);
246 *(v4l2_std_id *)arg = val;
252 ret = pvr2_ctrl_set_value(
253 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR),
254 *(v4l2_std_id *)arg);
258 case VIDIOC_ENUMINPUT:
260 struct pvr2_ctrl *cptr;
261 struct v4l2_input *vi = (struct v4l2_input *)arg;
262 struct v4l2_input tmp;
266 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
268 memset(&tmp,0,sizeof(tmp));
269 tmp.index = vi->index;
271 if ((vi->index < 0) || (vi->index >= fh->input_cnt)) {
275 val = fh->input_map[vi->index];
277 case PVR2_CVAL_INPUT_TV:
278 case PVR2_CVAL_INPUT_DTV:
279 case PVR2_CVAL_INPUT_RADIO:
280 tmp.type = V4L2_INPUT_TYPE_TUNER;
282 case PVR2_CVAL_INPUT_SVIDEO:
283 case PVR2_CVAL_INPUT_COMPOSITE:
284 tmp.type = V4L2_INPUT_TYPE_CAMERA;
293 pvr2_ctrl_get_valname(cptr,val,
294 tmp.name,sizeof(tmp.name)-1,&cnt);
297 /* Don't bother with audioset, since this driver currently
298 always switches the audio whenever the video is
301 /* Handling std is a tougher problem. It doesn't make
302 sense in cases where a device might be multi-standard.
303 We could just copy out the current value for the
304 standard, but it can change over time. For now just
307 memcpy(vi, &tmp, sizeof(tmp));
316 struct pvr2_ctrl *cptr;
317 struct v4l2_input *vi = (struct v4l2_input *)arg;
319 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
321 ret = pvr2_ctrl_get_value(cptr,&val);
323 for (idx = 0; idx < fh->input_cnt; idx++) {
324 if (fh->input_map[idx] == val) {
334 struct v4l2_input *vi = (struct v4l2_input *)arg;
335 if ((vi->index < 0) || (vi->index >= fh->input_cnt)) {
339 ret = pvr2_ctrl_set_value(
340 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
341 fh->input_map[vi->index]);
345 case VIDIOC_ENUMAUDIO:
347 /* pkt: FIXME: We are returning one "fake" input here
348 which could very well be called "whatever_we_like".
349 This is for apps that want to see an audio input
350 just to feel comfortable, as well as to test if
351 it can do stereo or sth. There is actually no guarantee
352 that the actual audio input cannot change behind the app's
353 back, but most applications should not mind that either.
355 Hopefully, mplayer people will work with us on this (this
356 whole mess is to support mplayer pvr://), or Hans will come
357 up with a more standard way to say "we have inputs but we
358 don 't want you to change them independent of video" which
361 struct v4l2_audio *vin = arg;
363 if (vin->index > 0) break;
364 strncpy(vin->name, "PVRUSB2 Audio",14);
365 vin->capability = V4L2_AUDCAP_STEREO;
373 /* pkt: FIXME: see above comment (VIDIOC_ENUMAUDIO) */
374 struct v4l2_audio *vin = arg;
375 memset(vin,0,sizeof(*vin));
377 strncpy(vin->name, "PVRUSB2 Audio",14);
378 vin->capability = V4L2_AUDCAP_STEREO;
390 struct v4l2_tuner *vt = (struct v4l2_tuner *)arg;
392 if (vt->index != 0) break; /* Only answer for the 1st tuner */
394 pvr2_hdw_execute_tuner_poll(hdw);
395 ret = pvr2_hdw_get_tuner_status(hdw,vt);
401 struct v4l2_tuner *vt=(struct v4l2_tuner *)arg;
406 ret = pvr2_ctrl_set_value(
407 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_AUDIOMODE),
412 case VIDIOC_S_FREQUENCY:
414 const struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
416 struct v4l2_tuner vt;
418 struct pvr2_ctrl *ctrlp;
419 ret = pvr2_hdw_get_tuner_status(hdw,&vt);
421 ctrlp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
422 ret = pvr2_ctrl_get_value(ctrlp,&cur_input);
424 if (vf->type == V4L2_TUNER_RADIO) {
425 if (cur_input != PVR2_CVAL_INPUT_RADIO) {
426 pvr2_ctrl_set_value(ctrlp,
427 PVR2_CVAL_INPUT_RADIO);
430 if (cur_input == PVR2_CVAL_INPUT_RADIO) {
431 pvr2_ctrl_set_value(ctrlp,
436 if (vt.capability & V4L2_TUNER_CAP_LOW) {
441 ret = pvr2_ctrl_set_value(
442 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),fv);
446 case VIDIOC_G_FREQUENCY:
448 struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
451 struct v4l2_tuner vt;
452 ret = pvr2_hdw_get_tuner_status(hdw,&vt);
454 ret = pvr2_ctrl_get_value(
455 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),
459 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
461 if (cur_input == PVR2_CVAL_INPUT_RADIO) {
462 vf->type = V4L2_TUNER_RADIO;
464 vf->type = V4L2_TUNER_ANALOG_TV;
466 if (vt.capability & V4L2_TUNER_CAP_LOW) {
467 val = (val * 2) / 125;
475 case VIDIOC_ENUM_FMT:
477 struct v4l2_fmtdesc *fd = (struct v4l2_fmtdesc *)arg;
479 /* Only one format is supported : mpeg.*/
483 memcpy(fd, pvr_fmtdesc, sizeof(struct v4l2_fmtdesc));
490 struct v4l2_format *vf = (struct v4l2_format *)arg;
493 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
494 memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
495 sizeof(struct v4l2_format));
498 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES),
500 vf->fmt.pix.width = val;
503 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES),
505 vf->fmt.pix.height = val;
508 case V4L2_BUF_TYPE_VBI_CAPTURE:
509 // ????? Still need to figure out to do VBI correctly
522 struct v4l2_format *vf = (struct v4l2_format *)arg;
526 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
528 struct pvr2_ctrl *hcp,*vcp;
529 int h = vf->fmt.pix.height;
530 int w = vf->fmt.pix.width;
531 hcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES);
532 vcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES);
534 lmin = pvr2_ctrl_get_min(hcp);
535 lmax = pvr2_ctrl_get_max(hcp);
536 ldef = pvr2_ctrl_get_def(hcp);
539 } else if (w < lmin) {
541 } else if (w > lmax) {
544 lmin = pvr2_ctrl_get_min(vcp);
545 lmax = pvr2_ctrl_get_max(vcp);
546 ldef = pvr2_ctrl_get_def(vcp);
549 } else if (h < lmin) {
551 } else if (h > lmax) {
555 memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
556 sizeof(struct v4l2_format));
557 vf->fmt.pix.width = w;
558 vf->fmt.pix.height = h;
560 if (cmd == VIDIOC_S_FMT) {
561 pvr2_ctrl_set_value(hcp,vf->fmt.pix.width);
562 pvr2_ctrl_set_value(vcp,vf->fmt.pix.height);
565 case V4L2_BUF_TYPE_VBI_CAPTURE:
566 // ????? Still need to figure out to do VBI correctly
576 case VIDIOC_STREAMON:
578 if (!fh->dev_info->stream) {
579 /* No stream defined for this node. This means
580 that we're not currently allowed to stream from
585 ret = pvr2_hdw_set_stream_type(hdw,dev_info->config);
586 if (ret < 0) return ret;
587 ret = pvr2_hdw_set_streaming(hdw,!0);
591 case VIDIOC_STREAMOFF:
593 if (!fh->dev_info->stream) {
594 /* No stream defined for this node. This means
595 that we're not currently allowed to stream from
600 ret = pvr2_hdw_set_streaming(hdw,0);
604 case VIDIOC_QUERYCTRL:
606 struct pvr2_ctrl *cptr;
607 struct v4l2_queryctrl *vc = (struct v4l2_queryctrl *)arg;
609 if (vc->id & V4L2_CTRL_FLAG_NEXT_CTRL) {
610 cptr = pvr2_hdw_get_ctrl_nextv4l(
611 hdw,(vc->id & ~V4L2_CTRL_FLAG_NEXT_CTRL));
612 if (cptr) vc->id = pvr2_ctrl_get_v4lid(cptr);
614 cptr = pvr2_hdw_get_ctrl_v4l(hdw,vc->id);
617 pvr2_trace(PVR2_TRACE_V4LIOCTL,
618 "QUERYCTRL id=0x%x not implemented here",
624 pvr2_trace(PVR2_TRACE_V4LIOCTL,
625 "QUERYCTRL id=0x%x mapping name=%s (%s)",
626 vc->id,pvr2_ctrl_get_name(cptr),
627 pvr2_ctrl_get_desc(cptr));
628 strlcpy(vc->name,pvr2_ctrl_get_desc(cptr),sizeof(vc->name));
629 vc->flags = pvr2_ctrl_get_v4lflags(cptr);
630 vc->default_value = pvr2_ctrl_get_def(cptr);
631 switch (pvr2_ctrl_get_type(cptr)) {
633 vc->type = V4L2_CTRL_TYPE_MENU;
635 vc->maximum = pvr2_ctrl_get_cnt(cptr) - 1;
639 vc->type = V4L2_CTRL_TYPE_BOOLEAN;
645 vc->type = V4L2_CTRL_TYPE_INTEGER;
646 vc->minimum = pvr2_ctrl_get_min(cptr);
647 vc->maximum = pvr2_ctrl_get_max(cptr);
651 pvr2_trace(PVR2_TRACE_V4LIOCTL,
652 "QUERYCTRL id=0x%x name=%s not mappable",
653 vc->id,pvr2_ctrl_get_name(cptr));
660 case VIDIOC_QUERYMENU:
662 struct v4l2_querymenu *vm = (struct v4l2_querymenu *)arg;
663 unsigned int cnt = 0;
664 ret = pvr2_ctrl_get_valname(pvr2_hdw_get_ctrl_v4l(hdw,vm->id),
666 vm->name,sizeof(vm->name)-1,
674 struct v4l2_control *vc = (struct v4l2_control *)arg;
676 ret = pvr2_ctrl_get_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
684 struct v4l2_control *vc = (struct v4l2_control *)arg;
685 ret = pvr2_ctrl_set_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
690 case VIDIOC_G_EXT_CTRLS:
692 struct v4l2_ext_controls *ctls =
693 (struct v4l2_ext_controls *)arg;
694 struct v4l2_ext_control *ctrl;
698 for (idx = 0; idx < ctls->count; idx++) {
699 ctrl = ctls->controls + idx;
700 ret = pvr2_ctrl_get_value(
701 pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),&val);
703 ctls->error_idx = idx;
706 /* Ensure that if read as a 64 bit value, the user
707 will still get a hopefully sane value */
714 case VIDIOC_S_EXT_CTRLS:
716 struct v4l2_ext_controls *ctls =
717 (struct v4l2_ext_controls *)arg;
718 struct v4l2_ext_control *ctrl;
721 for (idx = 0; idx < ctls->count; idx++) {
722 ctrl = ctls->controls + idx;
723 ret = pvr2_ctrl_set_value(
724 pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),
727 ctls->error_idx = idx;
734 case VIDIOC_TRY_EXT_CTRLS:
736 struct v4l2_ext_controls *ctls =
737 (struct v4l2_ext_controls *)arg;
738 struct v4l2_ext_control *ctrl;
739 struct pvr2_ctrl *pctl;
741 /* For the moment just validate that the requested control
744 for (idx = 0; idx < ctls->count; idx++) {
745 ctrl = ctls->controls + idx;
746 pctl = pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id);
749 ctls->error_idx = idx;
756 case VIDIOC_LOG_STATUS:
758 pvr2_hdw_trigger_module_log(hdw);
762 #ifdef CONFIG_VIDEO_ADV_DEBUG
763 case VIDIOC_DBG_S_REGISTER:
764 case VIDIOC_DBG_G_REGISTER:
767 struct v4l2_register *req = (struct v4l2_register *)arg;
768 if (cmd == VIDIOC_DBG_S_REGISTER) val = req->val;
769 ret = pvr2_hdw_register_access(
770 hdw,req->match_type,req->match_chip,req->reg,
771 cmd == VIDIOC_DBG_S_REGISTER,&val);
772 if (cmd == VIDIOC_DBG_G_REGISTER) req->val = val;
778 ret = v4l_compat_translate_ioctl(inode,file,cmd,
779 arg,pvr2_v4l2_do_ioctl);
782 pvr2_hdw_commit_ctl(hdw);
785 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
786 pvr2_trace(PVR2_TRACE_V4LIOCTL,
787 "pvr2_v4l2_do_ioctl failure, ret=%d",ret);
789 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
790 pvr2_trace(PVR2_TRACE_V4LIOCTL,
791 "pvr2_v4l2_do_ioctl failure, ret=%d"
792 " command was:",ret);
793 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),
798 pvr2_trace(PVR2_TRACE_V4LIOCTL,
799 "pvr2_v4l2_do_ioctl complete, ret=%d (0x%x)",
806 static void pvr2_v4l2_dev_destroy(struct pvr2_v4l2_dev *dip)
808 int minor_id = dip->devbase.minor;
809 struct pvr2_hdw *hdw = dip->v4lp->channel.mc_head->hdw;
810 enum pvr2_config cfg = dip->config;
811 int v4l_type = dip->v4l_type;
813 pvr2_hdw_v4l_store_minor_number(hdw,dip->minor_type,-1);
819 /* Actual deallocation happens later when all internal references
821 video_unregister_device(&dip->devbase);
823 printk(KERN_INFO "pvrusb2: unregistered device %s%u [%s]\n",
824 get_v4l_name(v4l_type),minor_id & 0x1f,
825 pvr2_config_get_name(cfg));
830 static void pvr2_v4l2_destroy_no_lock(struct pvr2_v4l2 *vp)
833 pvr2_v4l2_dev_destroy(vp->dev_video);
834 vp->dev_video = NULL;
837 pvr2_v4l2_dev_destroy(vp->dev_radio);
838 vp->dev_radio = NULL;
841 pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr2_v4l2 id=%p",vp);
842 pvr2_channel_done(&vp->channel);
847 static void pvr2_video_device_release(struct video_device *vdev)
849 struct pvr2_v4l2_dev *dev;
850 dev = container_of(vdev,struct pvr2_v4l2_dev,devbase);
855 static void pvr2_v4l2_internal_check(struct pvr2_channel *chp)
857 struct pvr2_v4l2 *vp;
858 vp = container_of(chp,struct pvr2_v4l2,channel);
859 if (!vp->channel.mc_head->disconnect_flag) return;
860 if (vp->vfirst) return;
861 pvr2_v4l2_destroy_no_lock(vp);
865 static int pvr2_v4l2_ioctl(struct inode *inode, struct file *file,
866 unsigned int cmd, unsigned long arg)
869 /* Temporary hack : use ivtv api until a v4l2 one is available. */
870 #define IVTV_IOC_G_CODEC 0xFFEE7703
871 #define IVTV_IOC_S_CODEC 0xFFEE7704
872 if (cmd == IVTV_IOC_G_CODEC || cmd == IVTV_IOC_S_CODEC) return 0;
873 return video_usercopy(inode, file, cmd, arg, pvr2_v4l2_do_ioctl);
877 static int pvr2_v4l2_release(struct inode *inode, struct file *file)
879 struct pvr2_v4l2_fh *fhp = file->private_data;
880 struct pvr2_v4l2 *vp = fhp->vhead;
881 struct pvr2_hdw *hdw = fhp->channel.mc_head->hdw;
883 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_release");
886 struct pvr2_stream *sp;
887 pvr2_hdw_set_streaming(hdw,0);
888 sp = pvr2_ioread_get_stream(fhp->rhp);
889 if (sp) pvr2_stream_set_callback(sp,NULL,NULL);
890 pvr2_ioread_destroy(fhp->rhp);
894 v4l2_prio_close(&vp->prio, &fhp->prio);
895 file->private_data = NULL;
898 fhp->vnext->vprev = fhp->vprev;
900 vp->vlast = fhp->vprev;
903 fhp->vprev->vnext = fhp->vnext;
905 vp->vfirst = fhp->vnext;
910 pvr2_channel_done(&fhp->channel);
911 pvr2_trace(PVR2_TRACE_STRUCT,
912 "Destroying pvr_v4l2_fh id=%p",fhp);
913 if (fhp->input_map) {
914 kfree(fhp->input_map);
915 fhp->input_map = NULL;
918 if (vp->channel.mc_head->disconnect_flag && !vp->vfirst) {
919 pvr2_v4l2_destroy_no_lock(vp);
925 static int pvr2_v4l2_open(struct inode *inode, struct file *file)
927 struct pvr2_v4l2_dev *dip; /* Our own context pointer */
928 struct pvr2_v4l2_fh *fhp;
929 struct pvr2_v4l2 *vp;
930 struct pvr2_hdw *hdw;
931 unsigned int input_mask = 0;
932 unsigned int input_cnt,idx;
935 dip = container_of(video_devdata(file),struct pvr2_v4l2_dev,devbase);
938 hdw = vp->channel.hdw;
940 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_open");
942 if (!pvr2_hdw_dev_ok(hdw)) {
943 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,
944 "pvr2_v4l2_open: hardware not ready");
948 fhp = kzalloc(sizeof(*fhp),GFP_KERNEL);
953 init_waitqueue_head(&fhp->wait_data);
956 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr_v4l2_fh id=%p",fhp);
957 pvr2_channel_init(&fhp->channel,vp->channel.mc_head);
959 if (dip->v4l_type == VFL_TYPE_RADIO) {
960 /* Opening device as a radio, legal input selection subset
961 is just the radio. */
962 input_mask = (1 << PVR2_CVAL_INPUT_RADIO);
964 /* Opening the main V4L device, legal input selection
965 subset includes all analog inputs. */
966 input_mask = ((1 << PVR2_CVAL_INPUT_RADIO) |
967 (1 << PVR2_CVAL_INPUT_TV) |
968 (1 << PVR2_CVAL_INPUT_COMPOSITE) |
969 (1 << PVR2_CVAL_INPUT_SVIDEO));
971 ret = pvr2_channel_limit_inputs(&fhp->channel,input_mask);
973 pvr2_channel_done(&fhp->channel);
974 pvr2_trace(PVR2_TRACE_STRUCT,
975 "Destroying pvr_v4l2_fh id=%p (input mask error)",
982 input_mask &= pvr2_hdw_get_input_available(hdw);
984 for (idx = 0; idx < (sizeof(input_mask) << 3); idx++) {
985 if (input_mask & (1 << idx)) input_cnt++;
987 fhp->input_cnt = input_cnt;
988 fhp->input_map = kzalloc(input_cnt,GFP_KERNEL);
989 if (!fhp->input_map) {
990 pvr2_channel_done(&fhp->channel);
991 pvr2_trace(PVR2_TRACE_STRUCT,
992 "Destroying pvr_v4l2_fh id=%p (input map failure)",
998 for (idx = 0; idx < (sizeof(input_mask) << 3); idx++) {
999 if (!(input_mask & (1 << idx))) continue;
1000 fhp->input_map[input_cnt++] = idx;
1004 fhp->vprev = vp->vlast;
1006 vp->vlast->vnext = fhp;
1014 file->private_data = fhp;
1015 v4l2_prio_open(&vp->prio,&fhp->prio);
1017 fhp->fw_mode_flag = pvr2_hdw_cpufw_get_enabled(hdw);
1023 static void pvr2_v4l2_notify(struct pvr2_v4l2_fh *fhp)
1025 wake_up(&fhp->wait_data);
1028 static int pvr2_v4l2_iosetup(struct pvr2_v4l2_fh *fh)
1031 struct pvr2_stream *sp;
1032 struct pvr2_hdw *hdw;
1033 if (fh->rhp) return 0;
1035 if (!fh->dev_info->stream) {
1036 /* No stream defined for this node. This means that we're
1037 not currently allowed to stream from this node. */
1041 /* First read() attempt. Try to claim the stream and start
1043 if ((ret = pvr2_channel_claim_stream(&fh->channel,
1044 fh->dev_info->stream)) != 0) {
1045 /* Someone else must already have it */
1049 fh->rhp = pvr2_channel_create_mpeg_stream(fh->dev_info->stream);
1051 pvr2_channel_claim_stream(&fh->channel,NULL);
1055 hdw = fh->channel.mc_head->hdw;
1056 sp = fh->dev_info->stream->stream;
1057 pvr2_stream_set_callback(sp,(pvr2_stream_callback)pvr2_v4l2_notify,fh);
1058 pvr2_hdw_set_stream_type(hdw,fh->dev_info->config);
1059 if ((ret = pvr2_hdw_set_streaming(hdw,!0)) < 0) return ret;
1060 return pvr2_ioread_set_enabled(fh->rhp,!0);
1064 static ssize_t pvr2_v4l2_read(struct file *file,
1065 char __user *buff, size_t count, loff_t *ppos)
1067 struct pvr2_v4l2_fh *fh = file->private_data;
1070 if (fh->fw_mode_flag) {
1071 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
1075 unsigned int offs = *ppos;
1077 tbuf = kmalloc(PAGE_SIZE,GFP_KERNEL);
1078 if (!tbuf) return -ENOMEM;
1082 if (c1 > PAGE_SIZE) c1 = PAGE_SIZE;
1083 c2 = pvr2_hdw_cpufw_get(hdw,offs,tbuf,c1);
1089 if (copy_to_user(buff,tbuf,c2)) {
1104 ret = pvr2_v4l2_iosetup(fh);
1111 ret = pvr2_ioread_read(fh->rhp,buff,count);
1112 if (ret >= 0) break;
1113 if (ret != -EAGAIN) break;
1114 if (file->f_flags & O_NONBLOCK) break;
1115 /* Doing blocking I/O. Wait here. */
1116 ret = wait_event_interruptible(
1118 pvr2_ioread_avail(fh->rhp) >= 0);
1126 static unsigned int pvr2_v4l2_poll(struct file *file, poll_table *wait)
1128 unsigned int mask = 0;
1129 struct pvr2_v4l2_fh *fh = file->private_data;
1132 if (fh->fw_mode_flag) {
1133 mask |= POLLIN | POLLRDNORM;
1138 ret = pvr2_v4l2_iosetup(fh);
1139 if (ret) return POLLERR;
1142 poll_wait(file,&fh->wait_data,wait);
1144 if (pvr2_ioread_avail(fh->rhp) >= 0) {
1145 mask |= POLLIN | POLLRDNORM;
1152 static const struct file_operations vdev_fops = {
1153 .owner = THIS_MODULE,
1154 .open = pvr2_v4l2_open,
1155 .release = pvr2_v4l2_release,
1156 .read = pvr2_v4l2_read,
1157 .ioctl = pvr2_v4l2_ioctl,
1158 .llseek = no_llseek,
1159 .poll = pvr2_v4l2_poll,
1163 static struct video_device vdev_template = {
1168 static void pvr2_v4l2_dev_init(struct pvr2_v4l2_dev *dip,
1169 struct pvr2_v4l2 *vp,
1178 dip->v4l_type = v4l_type;
1180 case VFL_TYPE_GRABBER:
1181 dip->stream = &vp->channel.mc_head->video_stream;
1182 dip->config = pvr2_config_mpeg;
1183 dip->minor_type = pvr2_v4l_type_video;
1186 err("Failed to set up pvrusb2 v4l video dev"
1187 " due to missing stream instance");
1192 dip->config = pvr2_config_vbi;
1193 dip->minor_type = pvr2_v4l_type_vbi;
1196 case VFL_TYPE_RADIO:
1197 dip->stream = &vp->channel.mc_head->video_stream;
1198 dip->config = pvr2_config_mpeg;
1199 dip->minor_type = pvr2_v4l_type_radio;
1203 /* Bail out (this should be impossible) */
1204 err("Failed to set up pvrusb2 v4l dev"
1205 " due to unrecognized config");
1209 memcpy(&dip->devbase,&vdev_template,sizeof(vdev_template));
1210 dip->devbase.release = pvr2_video_device_release;
1213 unit_number = pvr2_hdw_get_unit_number(vp->channel.mc_head->hdw);
1214 if (nr_ptr && (unit_number >= 0) && (unit_number < PVR_NUM)) {
1215 mindevnum = nr_ptr[unit_number];
1217 if ((video_register_device(&dip->devbase,
1218 dip->v4l_type, mindevnum) < 0) &&
1219 (video_register_device(&dip->devbase,
1220 dip->v4l_type, -1) < 0)) {
1221 err("Failed to register pvrusb2 v4l device");
1224 printk(KERN_INFO "pvrusb2: registered device %s%u [%s]\n",
1225 get_v4l_name(dip->v4l_type),dip->devbase.minor & 0x1f,
1226 pvr2_config_get_name(dip->config));
1228 pvr2_hdw_v4l_store_minor_number(vp->channel.mc_head->hdw,
1229 dip->minor_type,dip->devbase.minor);
1233 struct pvr2_v4l2 *pvr2_v4l2_create(struct pvr2_context *mnp)
1235 struct pvr2_v4l2 *vp;
1237 vp = kzalloc(sizeof(*vp),GFP_KERNEL);
1239 pvr2_channel_init(&vp->channel,mnp);
1240 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr2_v4l2 id=%p",vp);
1242 vp->channel.check_func = pvr2_v4l2_internal_check;
1244 /* register streams */
1245 vp->dev_video = kzalloc(sizeof(*vp->dev_video),GFP_KERNEL);
1246 if (!vp->dev_video) goto fail;
1247 pvr2_v4l2_dev_init(vp->dev_video,vp,VFL_TYPE_GRABBER);
1248 if (pvr2_hdw_get_input_available(vp->channel.mc_head->hdw) &
1249 (1 << PVR2_CVAL_INPUT_RADIO)) {
1250 vp->dev_radio = kzalloc(sizeof(*vp->dev_radio),GFP_KERNEL);
1251 if (!vp->dev_radio) goto fail;
1252 pvr2_v4l2_dev_init(vp->dev_radio,vp,VFL_TYPE_RADIO);
1257 pvr2_trace(PVR2_TRACE_STRUCT,"Failure creating pvr2_v4l2 id=%p",vp);
1258 pvr2_v4l2_destroy_no_lock(vp);
1263 Stuff for Emacs to see, in order to encourage consistent editing style:
1264 *** Local Variables: ***
1266 *** fill-column: 75 ***
1267 *** tab-width: 8 ***
1268 *** c-basic-offset: 8 ***