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/slab.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 <linux/module.h>
33 #include <media/v4l2-dev.h>
34 #include <media/v4l2-common.h>
35 #include <media/v4l2-ioctl.h>
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 /* Information about this device: */
46 enum pvr2_config config; /* Expected stream format */
47 int v4l_type; /* V4L defined type for this device node */
48 enum pvr2_v4l_type minor_type; /* pvr2-understood minor device type */
52 struct pvr2_channel channel;
53 struct pvr2_v4l2_dev *pdi;
54 enum v4l2_priority prio;
55 struct pvr2_ioread *rhp;
57 struct pvr2_v4l2 *vhead;
58 struct pvr2_v4l2_fh *vnext;
59 struct pvr2_v4l2_fh *vprev;
60 wait_queue_head_t wait_data;
62 /* Map contiguous ordinal value to input id */
63 unsigned char *input_map;
64 unsigned int input_cnt;
68 struct pvr2_channel channel;
69 struct pvr2_v4l2_fh *vfirst;
70 struct pvr2_v4l2_fh *vlast;
72 struct v4l2_prio_state prio;
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 = LINUX_VERSION_CODE,
96 .capabilities = (V4L2_CAP_VIDEO_CAPTURE |
97 V4L2_CAP_TUNER | V4L2_CAP_AUDIO | V4L2_CAP_RADIO |
101 static struct v4l2_fmtdesc pvr_fmtdesc [] = {
104 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
105 .flags = V4L2_FMT_FLAG_COMPRESSED,
106 .description = "MPEG1/2",
107 // This should really be V4L2_PIX_FMT_MPEG, but xawtv
108 // breaks when I do that.
109 .pixelformat = 0, // V4L2_PIX_FMT_MPEG,
110 .reserved = { 0, 0, 0, 0 }
114 #define PVR_FORMAT_PIX 0
115 #define PVR_FORMAT_VBI 1
117 static struct v4l2_format pvr_format [] = {
119 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
124 // This should really be V4L2_PIX_FMT_MPEG,
125 // but xawtv breaks when I do that.
126 .pixelformat = 0, // V4L2_PIX_FMT_MPEG,
127 .field = V4L2_FIELD_INTERLACED,
128 .bytesperline = 0, // doesn't make sense
130 //FIXME : Don't know what to put here...
131 .sizeimage = (32*1024),
132 .colorspace = 0, // doesn't make sense here
138 .type = V4L2_BUF_TYPE_VBI_CAPTURE,
141 .sampling_rate = 27000000,
143 .samples_per_line = 1443,
144 .sample_format = V4L2_PIX_FMT_GREY,
158 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
161 static long pvr2_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
163 struct pvr2_v4l2_fh *fh = file->private_data;
164 struct pvr2_v4l2 *vp = fh->vhead;
165 struct pvr2_v4l2_dev *pdi = fh->pdi;
166 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
169 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
170 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),cmd);
173 if (!pvr2_hdw_dev_ok(hdw)) {
174 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
175 "ioctl failed - bad or no context");
185 case VIDIOC_S_FREQUENCY:
186 ret = v4l2_prio_check(&vp->prio, fh->prio);
192 case VIDIOC_QUERYCAP:
194 struct v4l2_capability *cap = arg;
196 memcpy(cap, &pvr_capability, sizeof(struct v4l2_capability));
197 strlcpy(cap->bus_info,pvr2_hdw_get_bus_info(hdw),
198 sizeof(cap->bus_info));
199 strlcpy(cap->card,pvr2_hdw_get_desc(hdw),sizeof(cap->card));
205 case VIDIOC_G_PRIORITY:
207 enum v4l2_priority *p = arg;
209 *p = v4l2_prio_max(&vp->prio);
214 case VIDIOC_S_PRIORITY:
216 enum v4l2_priority *prio = arg;
218 ret = v4l2_prio_change(&vp->prio, &fh->prio, *prio);
224 struct v4l2_standard *vs = (struct v4l2_standard *)arg;
226 ret = pvr2_hdw_get_stdenum_value(hdw,vs,idx+1);
230 case VIDIOC_QUERYSTD:
232 v4l2_std_id *std = arg;
234 ret = pvr2_hdw_get_detected_std(hdw, std);
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;
263 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
265 memset(&tmp,0,sizeof(tmp));
266 tmp.index = vi->index;
268 if (vi->index >= fh->input_cnt) {
272 val = fh->input_map[vi->index];
274 case PVR2_CVAL_INPUT_TV:
275 case PVR2_CVAL_INPUT_DTV:
276 case PVR2_CVAL_INPUT_RADIO:
277 tmp.type = V4L2_INPUT_TYPE_TUNER;
279 case PVR2_CVAL_INPUT_SVIDEO:
280 case PVR2_CVAL_INPUT_COMPOSITE:
281 tmp.type = V4L2_INPUT_TYPE_CAMERA;
290 pvr2_ctrl_get_valname(cptr,val,
291 tmp.name,sizeof(tmp.name)-1,&cnt);
294 /* Don't bother with audioset, since this driver currently
295 always switches the audio whenever the video is
298 /* Handling std is a tougher problem. It doesn't make
299 sense in cases where a device might be multi-standard.
300 We could just copy out the current value for the
301 standard, but it can change over time. For now just
304 memcpy(vi, &tmp, sizeof(tmp));
313 struct pvr2_ctrl *cptr;
314 struct v4l2_input *vi = (struct v4l2_input *)arg;
316 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
318 ret = pvr2_ctrl_get_value(cptr,&val);
320 for (idx = 0; idx < fh->input_cnt; idx++) {
321 if (fh->input_map[idx] == val) {
331 struct v4l2_input *vi = (struct v4l2_input *)arg;
332 if (vi->index >= fh->input_cnt) {
336 ret = pvr2_ctrl_set_value(
337 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
338 fh->input_map[vi->index]);
342 case VIDIOC_ENUMAUDIO:
344 /* pkt: FIXME: We are returning one "fake" input here
345 which could very well be called "whatever_we_like".
346 This is for apps that want to see an audio input
347 just to feel comfortable, as well as to test if
348 it can do stereo or sth. There is actually no guarantee
349 that the actual audio input cannot change behind the app's
350 back, but most applications should not mind that either.
352 Hopefully, mplayer people will work with us on this (this
353 whole mess is to support mplayer pvr://), or Hans will come
354 up with a more standard way to say "we have inputs but we
355 don 't want you to change them independent of video" which
358 struct v4l2_audio *vin = arg;
360 if (vin->index > 0) break;
361 strncpy(vin->name, "PVRUSB2 Audio",14);
362 vin->capability = V4L2_AUDCAP_STEREO;
370 /* pkt: FIXME: see above comment (VIDIOC_ENUMAUDIO) */
371 struct v4l2_audio *vin = arg;
372 memset(vin,0,sizeof(*vin));
374 strncpy(vin->name, "PVRUSB2 Audio",14);
375 vin->capability = V4L2_AUDCAP_STEREO;
382 struct v4l2_tuner *vt = (struct v4l2_tuner *)arg;
384 if (vt->index != 0) break; /* Only answer for the 1st tuner */
386 pvr2_hdw_execute_tuner_poll(hdw);
387 ret = pvr2_hdw_get_tuner_status(hdw,vt);
393 struct v4l2_tuner *vt=(struct v4l2_tuner *)arg;
398 ret = pvr2_ctrl_set_value(
399 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_AUDIOMODE),
404 case VIDIOC_S_FREQUENCY:
406 const struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
408 struct v4l2_tuner vt;
410 struct pvr2_ctrl *ctrlp;
411 ret = pvr2_hdw_get_tuner_status(hdw,&vt);
413 ctrlp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
414 ret = pvr2_ctrl_get_value(ctrlp,&cur_input);
416 if (vf->type == V4L2_TUNER_RADIO) {
417 if (cur_input != PVR2_CVAL_INPUT_RADIO) {
418 pvr2_ctrl_set_value(ctrlp,
419 PVR2_CVAL_INPUT_RADIO);
422 if (cur_input == PVR2_CVAL_INPUT_RADIO) {
423 pvr2_ctrl_set_value(ctrlp,
428 if (vt.capability & V4L2_TUNER_CAP_LOW) {
433 ret = pvr2_ctrl_set_value(
434 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),fv);
438 case VIDIOC_G_FREQUENCY:
440 struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
443 struct v4l2_tuner vt;
444 ret = pvr2_hdw_get_tuner_status(hdw,&vt);
446 ret = pvr2_ctrl_get_value(
447 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),
451 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
453 if (cur_input == PVR2_CVAL_INPUT_RADIO) {
454 vf->type = V4L2_TUNER_RADIO;
456 vf->type = V4L2_TUNER_ANALOG_TV;
458 if (vt.capability & V4L2_TUNER_CAP_LOW) {
459 val = (val * 2) / 125;
467 case VIDIOC_ENUM_FMT:
469 struct v4l2_fmtdesc *fd = (struct v4l2_fmtdesc *)arg;
471 /* Only one format is supported : mpeg.*/
475 memcpy(fd, pvr_fmtdesc, sizeof(struct v4l2_fmtdesc));
482 struct v4l2_format *vf = (struct v4l2_format *)arg;
485 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
486 memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
487 sizeof(struct v4l2_format));
490 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES),
492 vf->fmt.pix.width = val;
495 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES),
497 vf->fmt.pix.height = val;
500 case V4L2_BUF_TYPE_VBI_CAPTURE:
501 // ????? Still need to figure out to do VBI correctly
514 struct v4l2_format *vf = (struct v4l2_format *)arg;
518 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
520 struct pvr2_ctrl *hcp,*vcp;
521 int h = vf->fmt.pix.height;
522 int w = vf->fmt.pix.width;
523 hcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES);
524 vcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES);
526 lmin = pvr2_ctrl_get_min(hcp);
527 lmax = pvr2_ctrl_get_max(hcp);
528 pvr2_ctrl_get_def(hcp, &ldef);
531 } else if (w < lmin) {
533 } else if (w > lmax) {
536 lmin = pvr2_ctrl_get_min(vcp);
537 lmax = pvr2_ctrl_get_max(vcp);
538 pvr2_ctrl_get_def(vcp, &ldef);
541 } else if (h < lmin) {
543 } else if (h > lmax) {
547 memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
548 sizeof(struct v4l2_format));
549 vf->fmt.pix.width = w;
550 vf->fmt.pix.height = h;
552 if (cmd == VIDIOC_S_FMT) {
553 pvr2_ctrl_set_value(hcp,vf->fmt.pix.width);
554 pvr2_ctrl_set_value(vcp,vf->fmt.pix.height);
557 case V4L2_BUF_TYPE_VBI_CAPTURE:
558 // ????? Still need to figure out to do VBI correctly
568 case VIDIOC_STREAMON:
570 if (!fh->pdi->stream) {
571 /* No stream defined for this node. This means
572 that we're not currently allowed to stream from
577 ret = pvr2_hdw_set_stream_type(hdw,pdi->config);
578 if (ret < 0) return ret;
579 ret = pvr2_hdw_set_streaming(hdw,!0);
583 case VIDIOC_STREAMOFF:
585 if (!fh->pdi->stream) {
586 /* No stream defined for this node. This means
587 that we're not currently allowed to stream from
592 ret = pvr2_hdw_set_streaming(hdw,0);
596 case VIDIOC_QUERYCTRL:
598 struct pvr2_ctrl *cptr;
600 struct v4l2_queryctrl *vc = (struct v4l2_queryctrl *)arg;
602 if (vc->id & V4L2_CTRL_FLAG_NEXT_CTRL) {
603 cptr = pvr2_hdw_get_ctrl_nextv4l(
604 hdw,(vc->id & ~V4L2_CTRL_FLAG_NEXT_CTRL));
605 if (cptr) vc->id = pvr2_ctrl_get_v4lid(cptr);
607 cptr = pvr2_hdw_get_ctrl_v4l(hdw,vc->id);
610 pvr2_trace(PVR2_TRACE_V4LIOCTL,
611 "QUERYCTRL id=0x%x not implemented here",
617 pvr2_trace(PVR2_TRACE_V4LIOCTL,
618 "QUERYCTRL id=0x%x mapping name=%s (%s)",
619 vc->id,pvr2_ctrl_get_name(cptr),
620 pvr2_ctrl_get_desc(cptr));
621 strlcpy(vc->name,pvr2_ctrl_get_desc(cptr),sizeof(vc->name));
622 vc->flags = pvr2_ctrl_get_v4lflags(cptr);
623 pvr2_ctrl_get_def(cptr, &val);
624 vc->default_value = val;
625 switch (pvr2_ctrl_get_type(cptr)) {
627 vc->type = V4L2_CTRL_TYPE_MENU;
629 vc->maximum = pvr2_ctrl_get_cnt(cptr) - 1;
633 vc->type = V4L2_CTRL_TYPE_BOOLEAN;
639 vc->type = V4L2_CTRL_TYPE_INTEGER;
640 vc->minimum = pvr2_ctrl_get_min(cptr);
641 vc->maximum = pvr2_ctrl_get_max(cptr);
645 pvr2_trace(PVR2_TRACE_V4LIOCTL,
646 "QUERYCTRL id=0x%x name=%s not mappable",
647 vc->id,pvr2_ctrl_get_name(cptr));
654 case VIDIOC_QUERYMENU:
656 struct v4l2_querymenu *vm = (struct v4l2_querymenu *)arg;
657 unsigned int cnt = 0;
658 ret = pvr2_ctrl_get_valname(pvr2_hdw_get_ctrl_v4l(hdw,vm->id),
660 vm->name,sizeof(vm->name)-1,
668 struct v4l2_control *vc = (struct v4l2_control *)arg;
670 ret = pvr2_ctrl_get_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
678 struct v4l2_control *vc = (struct v4l2_control *)arg;
679 ret = pvr2_ctrl_set_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
684 case VIDIOC_G_EXT_CTRLS:
686 struct v4l2_ext_controls *ctls =
687 (struct v4l2_ext_controls *)arg;
688 struct v4l2_ext_control *ctrl;
692 for (idx = 0; idx < ctls->count; idx++) {
693 ctrl = ctls->controls + idx;
694 ret = pvr2_ctrl_get_value(
695 pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),&val);
697 ctls->error_idx = idx;
700 /* Ensure that if read as a 64 bit value, the user
701 will still get a hopefully sane value */
708 case VIDIOC_S_EXT_CTRLS:
710 struct v4l2_ext_controls *ctls =
711 (struct v4l2_ext_controls *)arg;
712 struct v4l2_ext_control *ctrl;
715 for (idx = 0; idx < ctls->count; idx++) {
716 ctrl = ctls->controls + idx;
717 ret = pvr2_ctrl_set_value(
718 pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),
721 ctls->error_idx = idx;
728 case VIDIOC_TRY_EXT_CTRLS:
730 struct v4l2_ext_controls *ctls =
731 (struct v4l2_ext_controls *)arg;
732 struct v4l2_ext_control *ctrl;
733 struct pvr2_ctrl *pctl;
735 /* For the moment just validate that the requested control
738 for (idx = 0; idx < ctls->count; idx++) {
739 ctrl = ctls->controls + idx;
740 pctl = pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id);
743 ctls->error_idx = idx;
752 struct v4l2_cropcap *cap = (struct v4l2_cropcap *)arg;
753 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
757 ret = pvr2_hdw_get_cropcap(hdw, cap);
758 cap->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; /* paranoia */
763 struct v4l2_crop *crop = (struct v4l2_crop *)arg;
765 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
769 ret = pvr2_ctrl_get_value(
770 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPL), &val);
776 ret = pvr2_ctrl_get_value(
777 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPT), &val);
783 ret = pvr2_ctrl_get_value(
784 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPW), &val);
790 ret = pvr2_ctrl_get_value(
791 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPH), &val);
796 crop->c.height = val;
800 struct v4l2_crop *crop = (struct v4l2_crop *)arg;
801 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
805 ret = pvr2_ctrl_set_value(
806 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPL),
812 ret = pvr2_ctrl_set_value(
813 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPT),
819 ret = pvr2_ctrl_set_value(
820 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPW),
826 ret = pvr2_ctrl_set_value(
827 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPH),
834 case VIDIOC_LOG_STATUS:
836 pvr2_hdw_trigger_module_log(hdw);
840 #ifdef CONFIG_VIDEO_ADV_DEBUG
841 case VIDIOC_DBG_S_REGISTER:
842 case VIDIOC_DBG_G_REGISTER:
845 struct v4l2_dbg_register *req = (struct v4l2_dbg_register *)arg;
846 if (cmd == VIDIOC_DBG_S_REGISTER) val = req->val;
847 ret = pvr2_hdw_register_access(
848 hdw, &req->match, req->reg,
849 cmd == VIDIOC_DBG_S_REGISTER, &val);
850 if (cmd == VIDIOC_DBG_G_REGISTER) req->val = val;
860 pvr2_hdw_commit_ctl(hdw);
863 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
864 pvr2_trace(PVR2_TRACE_V4LIOCTL,
865 "pvr2_v4l2_do_ioctl failure, ret=%ld", ret);
867 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
868 pvr2_trace(PVR2_TRACE_V4LIOCTL,
869 "pvr2_v4l2_do_ioctl failure, ret=%ld"
870 " command was:", ret);
871 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),
876 pvr2_trace(PVR2_TRACE_V4LIOCTL,
877 "pvr2_v4l2_do_ioctl complete, ret=%ld (0x%lx)",
883 static void pvr2_v4l2_dev_destroy(struct pvr2_v4l2_dev *dip)
885 struct pvr2_hdw *hdw = dip->v4lp->channel.mc_head->hdw;
886 enum pvr2_config cfg = dip->config;
890 /* Construct the unregistration message *before* we actually
891 perform the unregistration step. By doing it this way we don't
892 have to worry about potentially touching deleted resources. */
893 mcnt = scnprintf(msg, sizeof(msg) - 1,
894 "pvrusb2: unregistered device %s [%s]",
895 video_device_node_name(&dip->devbase),
896 pvr2_config_get_name(cfg));
899 pvr2_hdw_v4l_store_minor_number(hdw,dip->minor_type,-1);
905 /* Actual deallocation happens later when all internal references
907 video_unregister_device(&dip->devbase);
909 printk(KERN_INFO "%s\n", msg);
914 static void pvr2_v4l2_dev_disassociate_parent(struct pvr2_v4l2_dev *dip)
917 if (!dip->devbase.parent) return;
918 dip->devbase.parent = NULL;
919 device_move(&dip->devbase.dev, NULL, DPM_ORDER_NONE);
923 static void pvr2_v4l2_destroy_no_lock(struct pvr2_v4l2 *vp)
926 pvr2_v4l2_dev_destroy(vp->dev_video);
927 vp->dev_video = NULL;
930 pvr2_v4l2_dev_destroy(vp->dev_radio);
931 vp->dev_radio = NULL;
934 pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr2_v4l2 id=%p",vp);
935 pvr2_channel_done(&vp->channel);
940 static void pvr2_video_device_release(struct video_device *vdev)
942 struct pvr2_v4l2_dev *dev;
943 dev = container_of(vdev,struct pvr2_v4l2_dev,devbase);
948 static void pvr2_v4l2_internal_check(struct pvr2_channel *chp)
950 struct pvr2_v4l2 *vp;
951 vp = container_of(chp,struct pvr2_v4l2,channel);
952 if (!vp->channel.mc_head->disconnect_flag) return;
953 pvr2_v4l2_dev_disassociate_parent(vp->dev_video);
954 pvr2_v4l2_dev_disassociate_parent(vp->dev_radio);
955 if (vp->vfirst) return;
956 pvr2_v4l2_destroy_no_lock(vp);
960 static long pvr2_v4l2_ioctl(struct file *file,
961 unsigned int cmd, unsigned long arg)
964 return video_usercopy(file, cmd, arg, pvr2_v4l2_do_ioctl);
968 static int pvr2_v4l2_release(struct file *file)
970 struct pvr2_v4l2_fh *fhp = file->private_data;
971 struct pvr2_v4l2 *vp = fhp->vhead;
972 struct pvr2_hdw *hdw = fhp->channel.mc_head->hdw;
974 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_release");
977 struct pvr2_stream *sp;
978 pvr2_hdw_set_streaming(hdw,0);
979 sp = pvr2_ioread_get_stream(fhp->rhp);
980 if (sp) pvr2_stream_set_callback(sp,NULL,NULL);
981 pvr2_ioread_destroy(fhp->rhp);
985 v4l2_prio_close(&vp->prio, fhp->prio);
986 file->private_data = NULL;
989 fhp->vnext->vprev = fhp->vprev;
991 vp->vlast = fhp->vprev;
994 fhp->vprev->vnext = fhp->vnext;
996 vp->vfirst = fhp->vnext;
1001 pvr2_channel_done(&fhp->channel);
1002 pvr2_trace(PVR2_TRACE_STRUCT,
1003 "Destroying pvr_v4l2_fh id=%p",fhp);
1004 if (fhp->input_map) {
1005 kfree(fhp->input_map);
1006 fhp->input_map = NULL;
1009 if (vp->channel.mc_head->disconnect_flag && !vp->vfirst) {
1010 pvr2_v4l2_destroy_no_lock(vp);
1016 static int pvr2_v4l2_open(struct file *file)
1018 struct pvr2_v4l2_dev *dip; /* Our own context pointer */
1019 struct pvr2_v4l2_fh *fhp;
1020 struct pvr2_v4l2 *vp;
1021 struct pvr2_hdw *hdw;
1022 unsigned int input_mask = 0;
1023 unsigned int input_cnt,idx;
1026 dip = container_of(video_devdata(file),struct pvr2_v4l2_dev,devbase);
1029 hdw = vp->channel.hdw;
1031 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_open");
1033 if (!pvr2_hdw_dev_ok(hdw)) {
1034 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,
1035 "pvr2_v4l2_open: hardware not ready");
1039 fhp = kzalloc(sizeof(*fhp),GFP_KERNEL);
1044 init_waitqueue_head(&fhp->wait_data);
1047 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr_v4l2_fh id=%p",fhp);
1048 pvr2_channel_init(&fhp->channel,vp->channel.mc_head);
1050 if (dip->v4l_type == VFL_TYPE_RADIO) {
1051 /* Opening device as a radio, legal input selection subset
1052 is just the radio. */
1053 input_mask = (1 << PVR2_CVAL_INPUT_RADIO);
1055 /* Opening the main V4L device, legal input selection
1056 subset includes all analog inputs. */
1057 input_mask = ((1 << PVR2_CVAL_INPUT_RADIO) |
1058 (1 << PVR2_CVAL_INPUT_TV) |
1059 (1 << PVR2_CVAL_INPUT_COMPOSITE) |
1060 (1 << PVR2_CVAL_INPUT_SVIDEO));
1062 ret = pvr2_channel_limit_inputs(&fhp->channel,input_mask);
1064 pvr2_channel_done(&fhp->channel);
1065 pvr2_trace(PVR2_TRACE_STRUCT,
1066 "Destroying pvr_v4l2_fh id=%p (input mask error)",
1073 input_mask &= pvr2_hdw_get_input_available(hdw);
1075 for (idx = 0; idx < (sizeof(input_mask) << 3); idx++) {
1076 if (input_mask & (1 << idx)) input_cnt++;
1078 fhp->input_cnt = input_cnt;
1079 fhp->input_map = kzalloc(input_cnt,GFP_KERNEL);
1080 if (!fhp->input_map) {
1081 pvr2_channel_done(&fhp->channel);
1082 pvr2_trace(PVR2_TRACE_STRUCT,
1083 "Destroying pvr_v4l2_fh id=%p (input map failure)",
1089 for (idx = 0; idx < (sizeof(input_mask) << 3); idx++) {
1090 if (!(input_mask & (1 << idx))) continue;
1091 fhp->input_map[input_cnt++] = idx;
1095 fhp->vprev = vp->vlast;
1097 vp->vlast->vnext = fhp;
1105 file->private_data = fhp;
1106 v4l2_prio_open(&vp->prio, &fhp->prio);
1108 fhp->fw_mode_flag = pvr2_hdw_cpufw_get_enabled(hdw);
1114 static void pvr2_v4l2_notify(struct pvr2_v4l2_fh *fhp)
1116 wake_up(&fhp->wait_data);
1119 static int pvr2_v4l2_iosetup(struct pvr2_v4l2_fh *fh)
1122 struct pvr2_stream *sp;
1123 struct pvr2_hdw *hdw;
1124 if (fh->rhp) return 0;
1126 if (!fh->pdi->stream) {
1127 /* No stream defined for this node. This means that we're
1128 not currently allowed to stream from this node. */
1132 /* First read() attempt. Try to claim the stream and start
1134 if ((ret = pvr2_channel_claim_stream(&fh->channel,
1135 fh->pdi->stream)) != 0) {
1136 /* Someone else must already have it */
1140 fh->rhp = pvr2_channel_create_mpeg_stream(fh->pdi->stream);
1142 pvr2_channel_claim_stream(&fh->channel,NULL);
1146 hdw = fh->channel.mc_head->hdw;
1147 sp = fh->pdi->stream->stream;
1148 pvr2_stream_set_callback(sp,(pvr2_stream_callback)pvr2_v4l2_notify,fh);
1149 pvr2_hdw_set_stream_type(hdw,fh->pdi->config);
1150 if ((ret = pvr2_hdw_set_streaming(hdw,!0)) < 0) return ret;
1151 return pvr2_ioread_set_enabled(fh->rhp,!0);
1155 static ssize_t pvr2_v4l2_read(struct file *file,
1156 char __user *buff, size_t count, loff_t *ppos)
1158 struct pvr2_v4l2_fh *fh = file->private_data;
1161 if (fh->fw_mode_flag) {
1162 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
1166 unsigned int offs = *ppos;
1168 tbuf = kmalloc(PAGE_SIZE,GFP_KERNEL);
1169 if (!tbuf) return -ENOMEM;
1173 if (c1 > PAGE_SIZE) c1 = PAGE_SIZE;
1174 c2 = pvr2_hdw_cpufw_get(hdw,offs,tbuf,c1);
1180 if (copy_to_user(buff,tbuf,c2)) {
1195 ret = pvr2_v4l2_iosetup(fh);
1202 ret = pvr2_ioread_read(fh->rhp,buff,count);
1203 if (ret >= 0) break;
1204 if (ret != -EAGAIN) break;
1205 if (file->f_flags & O_NONBLOCK) break;
1206 /* Doing blocking I/O. Wait here. */
1207 ret = wait_event_interruptible(
1209 pvr2_ioread_avail(fh->rhp) >= 0);
1217 static unsigned int pvr2_v4l2_poll(struct file *file, poll_table *wait)
1219 unsigned int mask = 0;
1220 struct pvr2_v4l2_fh *fh = file->private_data;
1223 if (fh->fw_mode_flag) {
1224 mask |= POLLIN | POLLRDNORM;
1229 ret = pvr2_v4l2_iosetup(fh);
1230 if (ret) return POLLERR;
1233 poll_wait(file,&fh->wait_data,wait);
1235 if (pvr2_ioread_avail(fh->rhp) >= 0) {
1236 mask |= POLLIN | POLLRDNORM;
1243 static const struct v4l2_file_operations vdev_fops = {
1244 .owner = THIS_MODULE,
1245 .open = pvr2_v4l2_open,
1246 .release = pvr2_v4l2_release,
1247 .read = pvr2_v4l2_read,
1248 .ioctl = pvr2_v4l2_ioctl,
1249 .poll = pvr2_v4l2_poll,
1253 static struct video_device vdev_template = {
1258 static void pvr2_v4l2_dev_init(struct pvr2_v4l2_dev *dip,
1259 struct pvr2_v4l2 *vp,
1262 struct usb_device *usbdev;
1268 usbdev = pvr2_hdw_get_dev(vp->channel.mc_head->hdw);
1269 dip->v4l_type = v4l_type;
1271 case VFL_TYPE_GRABBER:
1272 dip->stream = &vp->channel.mc_head->video_stream;
1273 dip->config = pvr2_config_mpeg;
1274 dip->minor_type = pvr2_v4l_type_video;
1277 pr_err(KBUILD_MODNAME
1278 ": Failed to set up pvrusb2 v4l video dev"
1279 " due to missing stream instance\n");
1284 dip->config = pvr2_config_vbi;
1285 dip->minor_type = pvr2_v4l_type_vbi;
1288 case VFL_TYPE_RADIO:
1289 dip->stream = &vp->channel.mc_head->video_stream;
1290 dip->config = pvr2_config_mpeg;
1291 dip->minor_type = pvr2_v4l_type_radio;
1295 /* Bail out (this should be impossible) */
1296 pr_err(KBUILD_MODNAME ": Failed to set up pvrusb2 v4l dev"
1297 " due to unrecognized config\n");
1301 memcpy(&dip->devbase,&vdev_template,sizeof(vdev_template));
1302 dip->devbase.release = pvr2_video_device_release;
1305 unit_number = pvr2_hdw_get_unit_number(vp->channel.mc_head->hdw);
1306 if (nr_ptr && (unit_number >= 0) && (unit_number < PVR_NUM)) {
1307 mindevnum = nr_ptr[unit_number];
1309 dip->devbase.parent = &usbdev->dev;
1310 if ((video_register_device(&dip->devbase,
1311 dip->v4l_type, mindevnum) < 0) &&
1312 (video_register_device(&dip->devbase,
1313 dip->v4l_type, -1) < 0)) {
1314 pr_err(KBUILD_MODNAME
1315 ": Failed to register pvrusb2 v4l device\n");
1318 printk(KERN_INFO "pvrusb2: registered device %s [%s]\n",
1319 video_device_node_name(&dip->devbase),
1320 pvr2_config_get_name(dip->config));
1322 pvr2_hdw_v4l_store_minor_number(vp->channel.mc_head->hdw,
1323 dip->minor_type,dip->devbase.minor);
1327 struct pvr2_v4l2 *pvr2_v4l2_create(struct pvr2_context *mnp)
1329 struct pvr2_v4l2 *vp;
1331 vp = kzalloc(sizeof(*vp),GFP_KERNEL);
1333 pvr2_channel_init(&vp->channel,mnp);
1334 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr2_v4l2 id=%p",vp);
1336 vp->channel.check_func = pvr2_v4l2_internal_check;
1338 /* register streams */
1339 vp->dev_video = kzalloc(sizeof(*vp->dev_video),GFP_KERNEL);
1340 if (!vp->dev_video) goto fail;
1341 pvr2_v4l2_dev_init(vp->dev_video,vp,VFL_TYPE_GRABBER);
1342 if (pvr2_hdw_get_input_available(vp->channel.mc_head->hdw) &
1343 (1 << PVR2_CVAL_INPUT_RADIO)) {
1344 vp->dev_radio = kzalloc(sizeof(*vp->dev_radio),GFP_KERNEL);
1345 if (!vp->dev_radio) goto fail;
1346 pvr2_v4l2_dev_init(vp->dev_radio,vp,VFL_TYPE_RADIO);
1351 pvr2_trace(PVR2_TRACE_STRUCT,"Failure creating pvr2_v4l2 id=%p",vp);
1352 pvr2_v4l2_destroy_no_lock(vp);
1357 Stuff for Emacs to see, in order to encourage consistent editing style:
1358 *** Local Variables: ***
1360 *** fill-column: 75 ***
1361 *** tab-width: 8 ***
1362 *** c-basic-offset: 8 ***