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