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