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