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