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