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