• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-R7000-V1.0.7.12_1.2.5/components/opensource/linux/linux-2.6.36/drivers/media/video/pvrusb2/
1/*
2 *
3 *
4 *  Copyright (C) 2005 Mike Isely <isely@pobox.com>
5 *  Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
6 *
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
10 *
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.
15 *
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
19 *
20 */
21
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"
27#include "pvrusb2.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>
34#include <media/v4l2-ioctl.h>
35
36struct pvr2_v4l2_dev;
37struct pvr2_v4l2_fh;
38struct pvr2_v4l2;
39
40struct pvr2_v4l2_dev {
41	struct video_device devbase; /* MUST be first! */
42	struct pvr2_v4l2 *v4lp;
43	struct pvr2_context_stream *stream;
44	/* Information about this device: */
45	enum pvr2_config config; /* Expected stream format */
46	int v4l_type; /* V4L defined type for this device node */
47	enum pvr2_v4l_type minor_type; /* pvr2-understood minor device type */
48};
49
50struct pvr2_v4l2_fh {
51	struct pvr2_channel channel;
52	struct pvr2_v4l2_dev *pdi;
53	enum v4l2_priority prio;
54	struct pvr2_ioread *rhp;
55	struct file *file;
56	struct pvr2_v4l2 *vhead;
57	struct pvr2_v4l2_fh *vnext;
58	struct pvr2_v4l2_fh *vprev;
59	wait_queue_head_t wait_data;
60	int fw_mode_flag;
61	/* Map contiguous ordinal value to input id */
62	unsigned char *input_map;
63	unsigned int input_cnt;
64};
65
66struct pvr2_v4l2 {
67	struct pvr2_channel channel;
68	struct pvr2_v4l2_fh *vfirst;
69	struct pvr2_v4l2_fh *vlast;
70
71	struct v4l2_prio_state prio;
72
73	/* streams - Note that these must be separately, individually,
74	 * allocated pointers.  This is because the v4l core is going to
75	 * manage their deletion - separately, individually...  */
76	struct pvr2_v4l2_dev *dev_video;
77	struct pvr2_v4l2_dev *dev_radio;
78};
79
80static int video_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
81module_param_array(video_nr, int, NULL, 0444);
82MODULE_PARM_DESC(video_nr, "Offset for device's video dev minor");
83static int radio_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
84module_param_array(radio_nr, int, NULL, 0444);
85MODULE_PARM_DESC(radio_nr, "Offset for device's radio dev minor");
86static int vbi_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
87module_param_array(vbi_nr, int, NULL, 0444);
88MODULE_PARM_DESC(vbi_nr, "Offset for device's vbi dev minor");
89
90static struct v4l2_capability pvr_capability ={
91	.driver         = "pvrusb2",
92	.card           = "Hauppauge WinTV pvr-usb2",
93	.bus_info       = "usb",
94	.version        = KERNEL_VERSION(0, 9, 0),
95	.capabilities   = (V4L2_CAP_VIDEO_CAPTURE |
96			   V4L2_CAP_TUNER | V4L2_CAP_AUDIO | V4L2_CAP_RADIO |
97			   V4L2_CAP_READWRITE),
98	.reserved       = {0,0,0,0}
99};
100
101static struct v4l2_fmtdesc pvr_fmtdesc [] = {
102	{
103		.index          = 0,
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 }
111	}
112};
113
114#define PVR_FORMAT_PIX  0
115#define PVR_FORMAT_VBI  1
116
117static struct v4l2_format pvr_format [] = {
118	[PVR_FORMAT_PIX] = {
119		.type   = V4L2_BUF_TYPE_VIDEO_CAPTURE,
120		.fmt    = {
121			.pix        = {
122				.width          = 720,
123				.height             = 576,
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
129						      // here
130				.sizeimage          = (32*1024),
131				.colorspace     = 0, // doesn't make sense here
132				.priv           = 0
133			}
134		}
135	},
136	[PVR_FORMAT_VBI] = {
137		.type   = V4L2_BUF_TYPE_VBI_CAPTURE,
138		.fmt    = {
139			.vbi        = {
140				.sampling_rate = 27000000,
141				.offset = 248,
142				.samples_per_line = 1443,
143				.sample_format = V4L2_PIX_FMT_GREY,
144				.start = { 0, 0 },
145				.count = { 0, 0 },
146				.flags = 0,
147				.reserved = { 0, 0 }
148			}
149		}
150	}
151};
152
153
154/*
155 * pvr_ioctl()
156 *
157 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
158 *
159 */
160static long pvr2_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
161{
162	struct pvr2_v4l2_fh *fh = file->private_data;
163	struct pvr2_v4l2 *vp = fh->vhead;
164	struct pvr2_v4l2_dev *pdi = fh->pdi;
165	struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
166	long ret = -EINVAL;
167
168	if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
169		v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),cmd);
170	}
171
172	if (!pvr2_hdw_dev_ok(hdw)) {
173		pvr2_trace(PVR2_TRACE_ERROR_LEGS,
174			   "ioctl failed - bad or no context");
175		return -EFAULT;
176	}
177
178	/* check priority */
179	switch (cmd) {
180	case VIDIOC_S_CTRL:
181	case VIDIOC_S_STD:
182	case VIDIOC_S_INPUT:
183	case VIDIOC_S_TUNER:
184	case VIDIOC_S_FREQUENCY:
185		ret = v4l2_prio_check(&vp->prio, fh->prio);
186		if (ret)
187			return ret;
188	}
189
190	switch (cmd) {
191	case VIDIOC_QUERYCAP:
192	{
193		struct v4l2_capability *cap = arg;
194
195		memcpy(cap, &pvr_capability, sizeof(struct v4l2_capability));
196		strlcpy(cap->bus_info,pvr2_hdw_get_bus_info(hdw),
197			sizeof(cap->bus_info));
198		strlcpy(cap->card,pvr2_hdw_get_desc(hdw),sizeof(cap->card));
199
200		ret = 0;
201		break;
202	}
203
204	case VIDIOC_G_PRIORITY:
205	{
206		enum v4l2_priority *p = arg;
207
208		*p = v4l2_prio_max(&vp->prio);
209		ret = 0;
210		break;
211	}
212
213	case VIDIOC_S_PRIORITY:
214	{
215		enum v4l2_priority *prio = arg;
216
217		ret = v4l2_prio_change(&vp->prio, &fh->prio, *prio);
218		break;
219	}
220
221	case VIDIOC_ENUMSTD:
222	{
223		struct v4l2_standard *vs = (struct v4l2_standard *)arg;
224		int idx = vs->index;
225		ret = pvr2_hdw_get_stdenum_value(hdw,vs,idx+1);
226		break;
227	}
228
229	case VIDIOC_G_STD:
230	{
231		int val = 0;
232		ret = pvr2_ctrl_get_value(
233			pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR),&val);
234		*(v4l2_std_id *)arg = val;
235		break;
236	}
237
238	case VIDIOC_S_STD:
239	{
240		ret = pvr2_ctrl_set_value(
241			pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR),
242			*(v4l2_std_id *)arg);
243		break;
244	}
245
246	case VIDIOC_ENUMINPUT:
247	{
248		struct pvr2_ctrl *cptr;
249		struct v4l2_input *vi = (struct v4l2_input *)arg;
250		struct v4l2_input tmp;
251		unsigned int cnt;
252		int val;
253
254		cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
255
256		memset(&tmp,0,sizeof(tmp));
257		tmp.index = vi->index;
258		ret = 0;
259		if (vi->index >= fh->input_cnt) {
260			ret = -EINVAL;
261			break;
262		}
263		val = fh->input_map[vi->index];
264		switch (val) {
265		case PVR2_CVAL_INPUT_TV:
266		case PVR2_CVAL_INPUT_DTV:
267		case PVR2_CVAL_INPUT_RADIO:
268			tmp.type = V4L2_INPUT_TYPE_TUNER;
269			break;
270		case PVR2_CVAL_INPUT_SVIDEO:
271		case PVR2_CVAL_INPUT_COMPOSITE:
272			tmp.type = V4L2_INPUT_TYPE_CAMERA;
273			break;
274		default:
275			ret = -EINVAL;
276			break;
277		}
278		if (ret < 0) break;
279
280		cnt = 0;
281		pvr2_ctrl_get_valname(cptr,val,
282				      tmp.name,sizeof(tmp.name)-1,&cnt);
283		tmp.name[cnt] = 0;
284
285		/* Don't bother with audioset, since this driver currently
286		   always switches the audio whenever the video is
287		   switched. */
288
289		/* Handling std is a tougher problem.  It doesn't make
290		   sense in cases where a device might be multi-standard.
291		   We could just copy out the current value for the
292		   standard, but it can change over time.  For now just
293		   leave it zero. */
294
295		memcpy(vi, &tmp, sizeof(tmp));
296
297		ret = 0;
298		break;
299	}
300
301	case VIDIOC_G_INPUT:
302	{
303		unsigned int idx;
304		struct pvr2_ctrl *cptr;
305		struct v4l2_input *vi = (struct v4l2_input *)arg;
306		int val;
307		cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
308		val = 0;
309		ret = pvr2_ctrl_get_value(cptr,&val);
310		vi->index = 0;
311		for (idx = 0; idx < fh->input_cnt; idx++) {
312			if (fh->input_map[idx] == val) {
313				vi->index = idx;
314				break;
315			}
316		}
317		break;
318	}
319
320	case VIDIOC_S_INPUT:
321	{
322		struct v4l2_input *vi = (struct v4l2_input *)arg;
323		if (vi->index >= fh->input_cnt) {
324			ret = -ERANGE;
325			break;
326		}
327		ret = pvr2_ctrl_set_value(
328			pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
329			fh->input_map[vi->index]);
330		break;
331	}
332
333	case VIDIOC_ENUMAUDIO:
334	{
335		struct v4l2_audio *vin = arg;
336		ret = -EINVAL;
337		if (vin->index > 0) break;
338		strncpy(vin->name, "PVRUSB2 Audio",14);
339		vin->capability = V4L2_AUDCAP_STEREO;
340		ret = 0;
341		break;
342		break;
343	}
344
345	case VIDIOC_G_AUDIO:
346	{
347		struct v4l2_audio *vin = arg;
348		memset(vin,0,sizeof(*vin));
349		vin->index = 0;
350		strncpy(vin->name, "PVRUSB2 Audio",14);
351		vin->capability = V4L2_AUDCAP_STEREO;
352		ret = 0;
353		break;
354	}
355
356	case VIDIOC_S_AUDIO:
357	{
358		ret = -EINVAL;
359		break;
360	}
361	case VIDIOC_G_TUNER:
362	{
363		struct v4l2_tuner *vt = (struct v4l2_tuner *)arg;
364
365		if (vt->index != 0) break; /* Only answer for the 1st tuner */
366
367		pvr2_hdw_execute_tuner_poll(hdw);
368		ret = pvr2_hdw_get_tuner_status(hdw,vt);
369		break;
370	}
371
372	case VIDIOC_S_TUNER:
373	{
374		struct v4l2_tuner *vt=(struct v4l2_tuner *)arg;
375
376		if (vt->index != 0)
377			break;
378
379		ret = pvr2_ctrl_set_value(
380			pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_AUDIOMODE),
381			vt->audmode);
382		break;
383	}
384
385	case VIDIOC_S_FREQUENCY:
386	{
387		const struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
388		unsigned long fv;
389		struct v4l2_tuner vt;
390		int cur_input;
391		struct pvr2_ctrl *ctrlp;
392		ret = pvr2_hdw_get_tuner_status(hdw,&vt);
393		if (ret != 0) break;
394		ctrlp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
395		ret = pvr2_ctrl_get_value(ctrlp,&cur_input);
396		if (ret != 0) break;
397		if (vf->type == V4L2_TUNER_RADIO) {
398			if (cur_input != PVR2_CVAL_INPUT_RADIO) {
399				pvr2_ctrl_set_value(ctrlp,
400						    PVR2_CVAL_INPUT_RADIO);
401			}
402		} else {
403			if (cur_input == PVR2_CVAL_INPUT_RADIO) {
404				pvr2_ctrl_set_value(ctrlp,
405						    PVR2_CVAL_INPUT_TV);
406			}
407		}
408		fv = vf->frequency;
409		if (vt.capability & V4L2_TUNER_CAP_LOW) {
410			fv = (fv * 125) / 2;
411		} else {
412			fv = fv * 62500;
413		}
414		ret = pvr2_ctrl_set_value(
415			pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),fv);
416		break;
417	}
418
419	case VIDIOC_G_FREQUENCY:
420	{
421		struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
422		int val = 0;
423		int cur_input;
424		struct v4l2_tuner vt;
425		ret = pvr2_hdw_get_tuner_status(hdw,&vt);
426		if (ret != 0) break;
427		ret = pvr2_ctrl_get_value(
428			pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),
429			&val);
430		if (ret != 0) break;
431		pvr2_ctrl_get_value(
432			pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
433			&cur_input);
434		if (cur_input == PVR2_CVAL_INPUT_RADIO) {
435			vf->type = V4L2_TUNER_RADIO;
436		} else {
437			vf->type = V4L2_TUNER_ANALOG_TV;
438		}
439		if (vt.capability & V4L2_TUNER_CAP_LOW) {
440			val = (val * 2) / 125;
441		} else {
442			val /= 62500;
443		}
444		vf->frequency = val;
445		break;
446	}
447
448	case VIDIOC_ENUM_FMT:
449	{
450		struct v4l2_fmtdesc *fd = (struct v4l2_fmtdesc *)arg;
451
452		/* Only one format is supported : mpeg.*/
453		if (fd->index != 0)
454			break;
455
456		memcpy(fd, pvr_fmtdesc, sizeof(struct v4l2_fmtdesc));
457		ret = 0;
458		break;
459	}
460
461	case VIDIOC_G_FMT:
462	{
463		struct v4l2_format *vf = (struct v4l2_format *)arg;
464		int val;
465		switch(vf->type) {
466		case V4L2_BUF_TYPE_VIDEO_CAPTURE:
467			memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
468			       sizeof(struct v4l2_format));
469			val = 0;
470			pvr2_ctrl_get_value(
471				pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES),
472				&val);
473			vf->fmt.pix.width = val;
474			val = 0;
475			pvr2_ctrl_get_value(
476				pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES),
477				&val);
478			vf->fmt.pix.height = val;
479			ret = 0;
480			break;
481		case V4L2_BUF_TYPE_VBI_CAPTURE:
482			// ????? Still need to figure out to do VBI correctly
483			ret = -EINVAL;
484			break;
485		default:
486			ret = -EINVAL;
487			break;
488		}
489		break;
490	}
491
492	case VIDIOC_TRY_FMT:
493	case VIDIOC_S_FMT:
494	{
495		struct v4l2_format *vf = (struct v4l2_format *)arg;
496
497		ret = 0;
498		switch(vf->type) {
499		case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
500			int lmin,lmax,ldef;
501			struct pvr2_ctrl *hcp,*vcp;
502			int h = vf->fmt.pix.height;
503			int w = vf->fmt.pix.width;
504			hcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES);
505			vcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES);
506
507			lmin = pvr2_ctrl_get_min(hcp);
508			lmax = pvr2_ctrl_get_max(hcp);
509			pvr2_ctrl_get_def(hcp, &ldef);
510			if (w == -1) {
511				w = ldef;
512			} else if (w < lmin) {
513				w = lmin;
514			} else if (w > lmax) {
515				w = lmax;
516			}
517			lmin = pvr2_ctrl_get_min(vcp);
518			lmax = pvr2_ctrl_get_max(vcp);
519			pvr2_ctrl_get_def(vcp, &ldef);
520			if (h == -1) {
521				h = ldef;
522			} else if (h < lmin) {
523				h = lmin;
524			} else if (h > lmax) {
525				h = lmax;
526			}
527
528			memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
529			       sizeof(struct v4l2_format));
530			vf->fmt.pix.width = w;
531			vf->fmt.pix.height = h;
532
533			if (cmd == VIDIOC_S_FMT) {
534				pvr2_ctrl_set_value(hcp,vf->fmt.pix.width);
535				pvr2_ctrl_set_value(vcp,vf->fmt.pix.height);
536			}
537		} break;
538		case V4L2_BUF_TYPE_VBI_CAPTURE:
539			// ????? Still need to figure out to do VBI correctly
540			ret = -EINVAL;
541			break;
542		default:
543			ret = -EINVAL;
544			break;
545		}
546		break;
547	}
548
549	case VIDIOC_STREAMON:
550	{
551		if (!fh->pdi->stream) {
552			/* No stream defined for this node.  This means
553			   that we're not currently allowed to stream from
554			   this node. */
555			ret = -EPERM;
556			break;
557		}
558		ret = pvr2_hdw_set_stream_type(hdw,pdi->config);
559		if (ret < 0) return ret;
560		ret = pvr2_hdw_set_streaming(hdw,!0);
561		break;
562	}
563
564	case VIDIOC_STREAMOFF:
565	{
566		if (!fh->pdi->stream) {
567			/* No stream defined for this node.  This means
568			   that we're not currently allowed to stream from
569			   this node. */
570			ret = -EPERM;
571			break;
572		}
573		ret = pvr2_hdw_set_streaming(hdw,0);
574		break;
575	}
576
577	case VIDIOC_QUERYCTRL:
578	{
579		struct pvr2_ctrl *cptr;
580		int val;
581		struct v4l2_queryctrl *vc = (struct v4l2_queryctrl *)arg;
582		ret = 0;
583		if (vc->id & V4L2_CTRL_FLAG_NEXT_CTRL) {
584			cptr = pvr2_hdw_get_ctrl_nextv4l(
585				hdw,(vc->id & ~V4L2_CTRL_FLAG_NEXT_CTRL));
586			if (cptr) vc->id = pvr2_ctrl_get_v4lid(cptr);
587		} else {
588			cptr = pvr2_hdw_get_ctrl_v4l(hdw,vc->id);
589		}
590		if (!cptr) {
591			pvr2_trace(PVR2_TRACE_V4LIOCTL,
592				   "QUERYCTRL id=0x%x not implemented here",
593				   vc->id);
594			ret = -EINVAL;
595			break;
596		}
597
598		pvr2_trace(PVR2_TRACE_V4LIOCTL,
599			   "QUERYCTRL id=0x%x mapping name=%s (%s)",
600			   vc->id,pvr2_ctrl_get_name(cptr),
601			   pvr2_ctrl_get_desc(cptr));
602		strlcpy(vc->name,pvr2_ctrl_get_desc(cptr),sizeof(vc->name));
603		vc->flags = pvr2_ctrl_get_v4lflags(cptr);
604		pvr2_ctrl_get_def(cptr, &val);
605		vc->default_value = val;
606		switch (pvr2_ctrl_get_type(cptr)) {
607		case pvr2_ctl_enum:
608			vc->type = V4L2_CTRL_TYPE_MENU;
609			vc->minimum = 0;
610			vc->maximum = pvr2_ctrl_get_cnt(cptr) - 1;
611			vc->step = 1;
612			break;
613		case pvr2_ctl_bool:
614			vc->type = V4L2_CTRL_TYPE_BOOLEAN;
615			vc->minimum = 0;
616			vc->maximum = 1;
617			vc->step = 1;
618			break;
619		case pvr2_ctl_int:
620			vc->type = V4L2_CTRL_TYPE_INTEGER;
621			vc->minimum = pvr2_ctrl_get_min(cptr);
622			vc->maximum = pvr2_ctrl_get_max(cptr);
623			vc->step = 1;
624			break;
625		default:
626			pvr2_trace(PVR2_TRACE_V4LIOCTL,
627				   "QUERYCTRL id=0x%x name=%s not mappable",
628				   vc->id,pvr2_ctrl_get_name(cptr));
629			ret = -EINVAL;
630			break;
631		}
632		break;
633	}
634
635	case VIDIOC_QUERYMENU:
636	{
637		struct v4l2_querymenu *vm = (struct v4l2_querymenu *)arg;
638		unsigned int cnt = 0;
639		ret = pvr2_ctrl_get_valname(pvr2_hdw_get_ctrl_v4l(hdw,vm->id),
640					    vm->index,
641					    vm->name,sizeof(vm->name)-1,
642					    &cnt);
643		vm->name[cnt] = 0;
644		break;
645	}
646
647	case VIDIOC_G_CTRL:
648	{
649		struct v4l2_control *vc = (struct v4l2_control *)arg;
650		int val = 0;
651		ret = pvr2_ctrl_get_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
652					  &val);
653		vc->value = val;
654		break;
655	}
656
657	case VIDIOC_S_CTRL:
658	{
659		struct v4l2_control *vc = (struct v4l2_control *)arg;
660		ret = pvr2_ctrl_set_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
661					  vc->value);
662		break;
663	}
664
665	case VIDIOC_G_EXT_CTRLS:
666	{
667		struct v4l2_ext_controls *ctls =
668			(struct v4l2_ext_controls *)arg;
669		struct v4l2_ext_control *ctrl;
670		unsigned int idx;
671		int val;
672		ret = 0;
673		for (idx = 0; idx < ctls->count; idx++) {
674			ctrl = ctls->controls + idx;
675			ret = pvr2_ctrl_get_value(
676				pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),&val);
677			if (ret) {
678				ctls->error_idx = idx;
679				break;
680			}
681			/* Ensure that if read as a 64 bit value, the user
682			   will still get a hopefully sane value */
683			ctrl->value64 = 0;
684			ctrl->value = val;
685		}
686		break;
687	}
688
689	case VIDIOC_S_EXT_CTRLS:
690	{
691		struct v4l2_ext_controls *ctls =
692			(struct v4l2_ext_controls *)arg;
693		struct v4l2_ext_control *ctrl;
694		unsigned int idx;
695		ret = 0;
696		for (idx = 0; idx < ctls->count; idx++) {
697			ctrl = ctls->controls + idx;
698			ret = pvr2_ctrl_set_value(
699				pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),
700				ctrl->value);
701			if (ret) {
702				ctls->error_idx = idx;
703				break;
704			}
705		}
706		break;
707	}
708
709	case VIDIOC_TRY_EXT_CTRLS:
710	{
711		struct v4l2_ext_controls *ctls =
712			(struct v4l2_ext_controls *)arg;
713		struct v4l2_ext_control *ctrl;
714		struct pvr2_ctrl *pctl;
715		unsigned int idx;
716		/* For the moment just validate that the requested control
717		   actually exists. */
718		ret = 0;
719		for (idx = 0; idx < ctls->count; idx++) {
720			ctrl = ctls->controls + idx;
721			pctl = pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id);
722			if (!pctl) {
723				ret = -EINVAL;
724				ctls->error_idx = idx;
725				break;
726			}
727		}
728		break;
729	}
730
731	case VIDIOC_CROPCAP:
732	{
733		struct v4l2_cropcap *cap = (struct v4l2_cropcap *)arg;
734		if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
735			ret = -EINVAL;
736			break;
737		}
738		ret = pvr2_hdw_get_cropcap(hdw, cap);
739		cap->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; /* paranoia */
740		break;
741	}
742	case VIDIOC_G_CROP:
743	{
744		struct v4l2_crop *crop = (struct v4l2_crop *)arg;
745		int val = 0;
746		if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
747			ret = -EINVAL;
748			break;
749		}
750		ret = pvr2_ctrl_get_value(
751			pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPL), &val);
752		if (ret != 0) {
753			ret = -EINVAL;
754			break;
755		}
756		crop->c.left = val;
757		ret = pvr2_ctrl_get_value(
758			pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPT), &val);
759		if (ret != 0) {
760			ret = -EINVAL;
761			break;
762		}
763		crop->c.top = val;
764		ret = pvr2_ctrl_get_value(
765			pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPW), &val);
766		if (ret != 0) {
767			ret = -EINVAL;
768			break;
769		}
770		crop->c.width = val;
771		ret = pvr2_ctrl_get_value(
772			pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPH), &val);
773		if (ret != 0) {
774			ret = -EINVAL;
775			break;
776		}
777		crop->c.height = val;
778	}
779	case VIDIOC_S_CROP:
780	{
781		struct v4l2_crop *crop = (struct v4l2_crop *)arg;
782		struct v4l2_cropcap cap;
783		if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
784			ret = -EINVAL;
785			break;
786		}
787		cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
788		ret = pvr2_ctrl_set_value(
789			pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPL),
790			crop->c.left);
791		if (ret != 0) {
792			ret = -EINVAL;
793			break;
794		}
795		ret = pvr2_ctrl_set_value(
796			pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPT),
797			crop->c.top);
798		if (ret != 0) {
799			ret = -EINVAL;
800			break;
801		}
802		ret = pvr2_ctrl_set_value(
803			pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPW),
804			crop->c.width);
805		if (ret != 0) {
806			ret = -EINVAL;
807			break;
808		}
809		ret = pvr2_ctrl_set_value(
810			pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPH),
811			crop->c.height);
812		if (ret != 0) {
813			ret = -EINVAL;
814			break;
815		}
816	}
817	case VIDIOC_LOG_STATUS:
818	{
819		pvr2_hdw_trigger_module_log(hdw);
820		ret = 0;
821		break;
822	}
823#ifdef CONFIG_VIDEO_ADV_DEBUG
824	case VIDIOC_DBG_S_REGISTER:
825	case VIDIOC_DBG_G_REGISTER:
826	{
827		u64 val;
828		struct v4l2_dbg_register *req = (struct v4l2_dbg_register *)arg;
829		if (cmd == VIDIOC_DBG_S_REGISTER) val = req->val;
830		ret = pvr2_hdw_register_access(
831			hdw, &req->match, req->reg,
832			cmd == VIDIOC_DBG_S_REGISTER, &val);
833		if (cmd == VIDIOC_DBG_G_REGISTER) req->val = val;
834		break;
835	}
836#endif
837
838	default :
839		ret = v4l_compat_translate_ioctl(file, cmd,
840						 arg, pvr2_v4l2_do_ioctl);
841	}
842
843	pvr2_hdw_commit_ctl(hdw);
844
845	if (ret < 0) {
846		if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
847			pvr2_trace(PVR2_TRACE_V4LIOCTL,
848				   "pvr2_v4l2_do_ioctl failure, ret=%ld", ret);
849		} else {
850			if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
851				pvr2_trace(PVR2_TRACE_V4LIOCTL,
852					   "pvr2_v4l2_do_ioctl failure, ret=%ld"
853					   " command was:", ret);
854				v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),
855						cmd);
856			}
857		}
858	} else {
859		pvr2_trace(PVR2_TRACE_V4LIOCTL,
860			   "pvr2_v4l2_do_ioctl complete, ret=%ld (0x%lx)",
861			   ret, ret);
862	}
863	return ret;
864}
865
866static void pvr2_v4l2_dev_destroy(struct pvr2_v4l2_dev *dip)
867{
868	struct pvr2_hdw *hdw = dip->v4lp->channel.mc_head->hdw;
869	enum pvr2_config cfg = dip->config;
870	char msg[80];
871	unsigned int mcnt;
872
873	/* Construct the unregistration message *before* we actually
874	   perform the unregistration step.  By doing it this way we don't
875	   have to worry about potentially touching deleted resources. */
876	mcnt = scnprintf(msg, sizeof(msg) - 1,
877			 "pvrusb2: unregistered device %s [%s]",
878			 video_device_node_name(&dip->devbase),
879			 pvr2_config_get_name(cfg));
880	msg[mcnt] = 0;
881
882	pvr2_hdw_v4l_store_minor_number(hdw,dip->minor_type,-1);
883
884	/* Paranoia */
885	dip->v4lp = NULL;
886	dip->stream = NULL;
887
888	/* Actual deallocation happens later when all internal references
889	   are gone. */
890	video_unregister_device(&dip->devbase);
891
892	printk(KERN_INFO "%s\n", msg);
893
894}
895
896
897static void pvr2_v4l2_dev_disassociate_parent(struct pvr2_v4l2_dev *dip)
898{
899	if (!dip) return;
900	if (!dip->devbase.parent) return;
901	dip->devbase.parent = NULL;
902	device_move(&dip->devbase.dev, NULL, DPM_ORDER_NONE);
903}
904
905
906static void pvr2_v4l2_destroy_no_lock(struct pvr2_v4l2 *vp)
907{
908	if (vp->dev_video) {
909		pvr2_v4l2_dev_destroy(vp->dev_video);
910		vp->dev_video = NULL;
911	}
912	if (vp->dev_radio) {
913		pvr2_v4l2_dev_destroy(vp->dev_radio);
914		vp->dev_radio = NULL;
915	}
916
917	pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr2_v4l2 id=%p",vp);
918	pvr2_channel_done(&vp->channel);
919	kfree(vp);
920}
921
922
923static void pvr2_video_device_release(struct video_device *vdev)
924{
925	struct pvr2_v4l2_dev *dev;
926	dev = container_of(vdev,struct pvr2_v4l2_dev,devbase);
927	kfree(dev);
928}
929
930
931static void pvr2_v4l2_internal_check(struct pvr2_channel *chp)
932{
933	struct pvr2_v4l2 *vp;
934	vp = container_of(chp,struct pvr2_v4l2,channel);
935	if (!vp->channel.mc_head->disconnect_flag) return;
936	pvr2_v4l2_dev_disassociate_parent(vp->dev_video);
937	pvr2_v4l2_dev_disassociate_parent(vp->dev_radio);
938	if (vp->vfirst) return;
939	pvr2_v4l2_destroy_no_lock(vp);
940}
941
942
943static long pvr2_v4l2_ioctl(struct file *file,
944			   unsigned int cmd, unsigned long arg)
945{
946
947	return video_usercopy(file, cmd, arg, pvr2_v4l2_do_ioctl);
948}
949
950
951static int pvr2_v4l2_release(struct file *file)
952{
953	struct pvr2_v4l2_fh *fhp = file->private_data;
954	struct pvr2_v4l2 *vp = fhp->vhead;
955	struct pvr2_hdw *hdw = fhp->channel.mc_head->hdw;
956
957	pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_release");
958
959	if (fhp->rhp) {
960		struct pvr2_stream *sp;
961		pvr2_hdw_set_streaming(hdw,0);
962		sp = pvr2_ioread_get_stream(fhp->rhp);
963		if (sp) pvr2_stream_set_callback(sp,NULL,NULL);
964		pvr2_ioread_destroy(fhp->rhp);
965		fhp->rhp = NULL;
966	}
967
968	v4l2_prio_close(&vp->prio, fhp->prio);
969	file->private_data = NULL;
970
971	if (fhp->vnext) {
972		fhp->vnext->vprev = fhp->vprev;
973	} else {
974		vp->vlast = fhp->vprev;
975	}
976	if (fhp->vprev) {
977		fhp->vprev->vnext = fhp->vnext;
978	} else {
979		vp->vfirst = fhp->vnext;
980	}
981	fhp->vnext = NULL;
982	fhp->vprev = NULL;
983	fhp->vhead = NULL;
984	pvr2_channel_done(&fhp->channel);
985	pvr2_trace(PVR2_TRACE_STRUCT,
986		   "Destroying pvr_v4l2_fh id=%p",fhp);
987	if (fhp->input_map) {
988		kfree(fhp->input_map);
989		fhp->input_map = NULL;
990	}
991	kfree(fhp);
992	if (vp->channel.mc_head->disconnect_flag && !vp->vfirst) {
993		pvr2_v4l2_destroy_no_lock(vp);
994	}
995	return 0;
996}
997
998
999static int pvr2_v4l2_open(struct file *file)
1000{
1001	struct pvr2_v4l2_dev *dip; /* Our own context pointer */
1002	struct pvr2_v4l2_fh *fhp;
1003	struct pvr2_v4l2 *vp;
1004	struct pvr2_hdw *hdw;
1005	unsigned int input_mask = 0;
1006	unsigned int input_cnt,idx;
1007	int ret = 0;
1008
1009	dip = container_of(video_devdata(file),struct pvr2_v4l2_dev,devbase);
1010
1011	vp = dip->v4lp;
1012	hdw = vp->channel.hdw;
1013
1014	pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_open");
1015
1016	if (!pvr2_hdw_dev_ok(hdw)) {
1017		pvr2_trace(PVR2_TRACE_OPEN_CLOSE,
1018			   "pvr2_v4l2_open: hardware not ready");
1019		return -EIO;
1020	}
1021
1022	fhp = kzalloc(sizeof(*fhp),GFP_KERNEL);
1023	if (!fhp) {
1024		return -ENOMEM;
1025	}
1026
1027	init_waitqueue_head(&fhp->wait_data);
1028	fhp->pdi = dip;
1029
1030	pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr_v4l2_fh id=%p",fhp);
1031	pvr2_channel_init(&fhp->channel,vp->channel.mc_head);
1032
1033	if (dip->v4l_type == VFL_TYPE_RADIO) {
1034		/* Opening device as a radio, legal input selection subset
1035		   is just the radio. */
1036		input_mask = (1 << PVR2_CVAL_INPUT_RADIO);
1037	} else {
1038		/* Opening the main V4L device, legal input selection
1039		   subset includes all analog inputs. */
1040		input_mask = ((1 << PVR2_CVAL_INPUT_RADIO) |
1041			      (1 << PVR2_CVAL_INPUT_TV) |
1042			      (1 << PVR2_CVAL_INPUT_COMPOSITE) |
1043			      (1 << PVR2_CVAL_INPUT_SVIDEO));
1044	}
1045	ret = pvr2_channel_limit_inputs(&fhp->channel,input_mask);
1046	if (ret) {
1047		pvr2_channel_done(&fhp->channel);
1048		pvr2_trace(PVR2_TRACE_STRUCT,
1049			   "Destroying pvr_v4l2_fh id=%p (input mask error)",
1050			   fhp);
1051
1052		kfree(fhp);
1053		return ret;
1054	}
1055
1056	input_mask &= pvr2_hdw_get_input_available(hdw);
1057	input_cnt = 0;
1058	for (idx = 0; idx < (sizeof(input_mask) << 3); idx++) {
1059		if (input_mask & (1 << idx)) input_cnt++;
1060	}
1061	fhp->input_cnt = input_cnt;
1062	fhp->input_map = kzalloc(input_cnt,GFP_KERNEL);
1063	if (!fhp->input_map) {
1064		pvr2_channel_done(&fhp->channel);
1065		pvr2_trace(PVR2_TRACE_STRUCT,
1066			   "Destroying pvr_v4l2_fh id=%p (input map failure)",
1067			   fhp);
1068		kfree(fhp);
1069		return -ENOMEM;
1070	}
1071	input_cnt = 0;
1072	for (idx = 0; idx < (sizeof(input_mask) << 3); idx++) {
1073		if (!(input_mask & (1 << idx))) continue;
1074		fhp->input_map[input_cnt++] = idx;
1075	}
1076
1077	fhp->vnext = NULL;
1078	fhp->vprev = vp->vlast;
1079	if (vp->vlast) {
1080		vp->vlast->vnext = fhp;
1081	} else {
1082		vp->vfirst = fhp;
1083	}
1084	vp->vlast = fhp;
1085	fhp->vhead = vp;
1086
1087	fhp->file = file;
1088	file->private_data = fhp;
1089	v4l2_prio_open(&vp->prio, &fhp->prio);
1090
1091	fhp->fw_mode_flag = pvr2_hdw_cpufw_get_enabled(hdw);
1092
1093	return 0;
1094}
1095
1096
1097static void pvr2_v4l2_notify(struct pvr2_v4l2_fh *fhp)
1098{
1099	wake_up(&fhp->wait_data);
1100}
1101
1102static int pvr2_v4l2_iosetup(struct pvr2_v4l2_fh *fh)
1103{
1104	int ret;
1105	struct pvr2_stream *sp;
1106	struct pvr2_hdw *hdw;
1107	if (fh->rhp) return 0;
1108
1109	if (!fh->pdi->stream) {
1110		/* No stream defined for this node.  This means that we're
1111		   not currently allowed to stream from this node. */
1112		return -EPERM;
1113	}
1114
1115	/* First read() attempt.  Try to claim the stream and start
1116	   it... */
1117	if ((ret = pvr2_channel_claim_stream(&fh->channel,
1118					     fh->pdi->stream)) != 0) {
1119		/* Someone else must already have it */
1120		return ret;
1121	}
1122
1123	fh->rhp = pvr2_channel_create_mpeg_stream(fh->pdi->stream);
1124	if (!fh->rhp) {
1125		pvr2_channel_claim_stream(&fh->channel,NULL);
1126		return -ENOMEM;
1127	}
1128
1129	hdw = fh->channel.mc_head->hdw;
1130	sp = fh->pdi->stream->stream;
1131	pvr2_stream_set_callback(sp,(pvr2_stream_callback)pvr2_v4l2_notify,fh);
1132	pvr2_hdw_set_stream_type(hdw,fh->pdi->config);
1133	if ((ret = pvr2_hdw_set_streaming(hdw,!0)) < 0) return ret;
1134	return pvr2_ioread_set_enabled(fh->rhp,!0);
1135}
1136
1137
1138static ssize_t pvr2_v4l2_read(struct file *file,
1139			      char __user *buff, size_t count, loff_t *ppos)
1140{
1141	struct pvr2_v4l2_fh *fh = file->private_data;
1142	int ret;
1143
1144	if (fh->fw_mode_flag) {
1145		struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
1146		char *tbuf;
1147		int c1,c2;
1148		int tcnt = 0;
1149		unsigned int offs = *ppos;
1150
1151		tbuf = kmalloc(PAGE_SIZE,GFP_KERNEL);
1152		if (!tbuf) return -ENOMEM;
1153
1154		while (count) {
1155			c1 = count;
1156			if (c1 > PAGE_SIZE) c1 = PAGE_SIZE;
1157			c2 = pvr2_hdw_cpufw_get(hdw,offs,tbuf,c1);
1158			if (c2 < 0) {
1159				tcnt = c2;
1160				break;
1161			}
1162			if (!c2) break;
1163			if (copy_to_user(buff,tbuf,c2)) {
1164				tcnt = -EFAULT;
1165				break;
1166			}
1167			offs += c2;
1168			tcnt += c2;
1169			buff += c2;
1170			count -= c2;
1171			*ppos += c2;
1172		}
1173		kfree(tbuf);
1174		return tcnt;
1175	}
1176
1177	if (!fh->rhp) {
1178		ret = pvr2_v4l2_iosetup(fh);
1179		if (ret) {
1180			return ret;
1181		}
1182	}
1183
1184	for (;;) {
1185		ret = pvr2_ioread_read(fh->rhp,buff,count);
1186		if (ret >= 0) break;
1187		if (ret != -EAGAIN) break;
1188		if (file->f_flags & O_NONBLOCK) break;
1189		/* Doing blocking I/O.  Wait here. */
1190		ret = wait_event_interruptible(
1191			fh->wait_data,
1192			pvr2_ioread_avail(fh->rhp) >= 0);
1193		if (ret < 0) break;
1194	}
1195
1196	return ret;
1197}
1198
1199
1200static unsigned int pvr2_v4l2_poll(struct file *file, poll_table *wait)
1201{
1202	unsigned int mask = 0;
1203	struct pvr2_v4l2_fh *fh = file->private_data;
1204	int ret;
1205
1206	if (fh->fw_mode_flag) {
1207		mask |= POLLIN | POLLRDNORM;
1208		return mask;
1209	}
1210
1211	if (!fh->rhp) {
1212		ret = pvr2_v4l2_iosetup(fh);
1213		if (ret) return POLLERR;
1214	}
1215
1216	poll_wait(file,&fh->wait_data,wait);
1217
1218	if (pvr2_ioread_avail(fh->rhp) >= 0) {
1219		mask |= POLLIN | POLLRDNORM;
1220	}
1221
1222	return mask;
1223}
1224
1225
1226static const struct v4l2_file_operations vdev_fops = {
1227	.owner      = THIS_MODULE,
1228	.open       = pvr2_v4l2_open,
1229	.release    = pvr2_v4l2_release,
1230	.read       = pvr2_v4l2_read,
1231	.ioctl      = pvr2_v4l2_ioctl,
1232	.poll       = pvr2_v4l2_poll,
1233};
1234
1235
1236static struct video_device vdev_template = {
1237	.fops       = &vdev_fops,
1238};
1239
1240
1241static void pvr2_v4l2_dev_init(struct pvr2_v4l2_dev *dip,
1242			       struct pvr2_v4l2 *vp,
1243			       int v4l_type)
1244{
1245	struct usb_device *usbdev;
1246	int mindevnum;
1247	int unit_number;
1248	int *nr_ptr = NULL;
1249	dip->v4lp = vp;
1250
1251	usbdev = pvr2_hdw_get_dev(vp->channel.mc_head->hdw);
1252	dip->v4l_type = v4l_type;
1253	switch (v4l_type) {
1254	case VFL_TYPE_GRABBER:
1255		dip->stream = &vp->channel.mc_head->video_stream;
1256		dip->config = pvr2_config_mpeg;
1257		dip->minor_type = pvr2_v4l_type_video;
1258		nr_ptr = video_nr;
1259		if (!dip->stream) {
1260			pr_err(KBUILD_MODNAME
1261				": Failed to set up pvrusb2 v4l video dev"
1262				" due to missing stream instance\n");
1263			return;
1264		}
1265		break;
1266	case VFL_TYPE_VBI:
1267		dip->config = pvr2_config_vbi;
1268		dip->minor_type = pvr2_v4l_type_vbi;
1269		nr_ptr = vbi_nr;
1270		break;
1271	case VFL_TYPE_RADIO:
1272		dip->stream = &vp->channel.mc_head->video_stream;
1273		dip->config = pvr2_config_mpeg;
1274		dip->minor_type = pvr2_v4l_type_radio;
1275		nr_ptr = radio_nr;
1276		break;
1277	default:
1278		/* Bail out (this should be impossible) */
1279		pr_err(KBUILD_MODNAME ": Failed to set up pvrusb2 v4l dev"
1280		    " due to unrecognized config\n");
1281		return;
1282	}
1283
1284	memcpy(&dip->devbase,&vdev_template,sizeof(vdev_template));
1285	dip->devbase.release = pvr2_video_device_release;
1286
1287	mindevnum = -1;
1288	unit_number = pvr2_hdw_get_unit_number(vp->channel.mc_head->hdw);
1289	if (nr_ptr && (unit_number >= 0) && (unit_number < PVR_NUM)) {
1290		mindevnum = nr_ptr[unit_number];
1291	}
1292	dip->devbase.parent = &usbdev->dev;
1293	if ((video_register_device(&dip->devbase,
1294				   dip->v4l_type, mindevnum) < 0) &&
1295	    (video_register_device(&dip->devbase,
1296				   dip->v4l_type, -1) < 0)) {
1297		pr_err(KBUILD_MODNAME
1298			": Failed to register pvrusb2 v4l device\n");
1299	}
1300
1301	printk(KERN_INFO "pvrusb2: registered device %s [%s]\n",
1302	       video_device_node_name(&dip->devbase),
1303	       pvr2_config_get_name(dip->config));
1304
1305	pvr2_hdw_v4l_store_minor_number(vp->channel.mc_head->hdw,
1306					dip->minor_type,dip->devbase.minor);
1307}
1308
1309
1310struct pvr2_v4l2 *pvr2_v4l2_create(struct pvr2_context *mnp)
1311{
1312	struct pvr2_v4l2 *vp;
1313
1314	vp = kzalloc(sizeof(*vp),GFP_KERNEL);
1315	if (!vp) return vp;
1316	pvr2_channel_init(&vp->channel,mnp);
1317	pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr2_v4l2 id=%p",vp);
1318
1319	vp->channel.check_func = pvr2_v4l2_internal_check;
1320
1321	/* register streams */
1322	vp->dev_video = kzalloc(sizeof(*vp->dev_video),GFP_KERNEL);
1323	if (!vp->dev_video) goto fail;
1324	pvr2_v4l2_dev_init(vp->dev_video,vp,VFL_TYPE_GRABBER);
1325	if (pvr2_hdw_get_input_available(vp->channel.mc_head->hdw) &
1326	    (1 << PVR2_CVAL_INPUT_RADIO)) {
1327		vp->dev_radio = kzalloc(sizeof(*vp->dev_radio),GFP_KERNEL);
1328		if (!vp->dev_radio) goto fail;
1329		pvr2_v4l2_dev_init(vp->dev_radio,vp,VFL_TYPE_RADIO);
1330	}
1331
1332	return vp;
1333 fail:
1334	pvr2_trace(PVR2_TRACE_STRUCT,"Failure creating pvr2_v4l2 id=%p",vp);
1335	pvr2_v4l2_destroy_no_lock(vp);
1336	return NULL;
1337}
1338
1339/*
1340  Stuff for Emacs to see, in order to encourage consistent editing style:
1341  *** Local Variables: ***
1342  *** mode: c ***
1343  *** fill-column: 75 ***
1344  *** tab-width: 8 ***
1345  *** c-basic-offset: 8 ***
1346  *** End: ***
1347  */
1348