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