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