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